-
Notifications
You must be signed in to change notification settings - Fork 2.8k
fix: loading of datasets from Disk(#7373) #7489
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
@nepfaff Could you confirm if this fixes the issue for you? I checked Memray, and everything looked good on my end. Install: |
Will aim to get to this soon. I don't have a rapid testing pipeline setup but need to wait for some AWS nodes to become free |
I now set up a small experiment: # Log initial RAM usage
process = psutil.Process(os.getpid())
initial_ram = process.memory_info().rss / (1024 * 1024) # Convert to MB
logging.info(f"Initial RAM usage: {initial_ram:.2f} MB")
chunk_datasets = [
Dataset.load_from_disk(dataset_path, keep_in_memory=False) for _ in range(N)
]
combined_dataset = concatenate_datasets(chunk_datasets)
# Log final RAM usage
final_ram = process.memory_info().rss / (1024 * 1024) # Convert to MB
ram_diff = final_ram - initial_ram
logging.info(f"Final RAM usage: {final_ram:.2f} MB")
logging.info(f"RAM usage increase: {ram_diff:.2f} MB") The RAM usage is linearly correlated with For my test dataset:
Unfortunately, your patch doesn't seem to change this: pip install git+https://github.com/sam-hey/datasets.git@fix/concatenate_datasets
pip list | grep datasets
datasets 3.5.1.dev0 Gives exactly the same RAM statistics. Edit: The results are a bit flawed as the memory increase all seems to come from |
Thanks a lot, @nepfaff, for taking a look at this! It seems that This behavior might be related to this bug: apache/arrow#34423 ![]() |
memory_mapped_stream.close() | ||
return pa_table |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is it really ok to close the memory map, given the memory mapped table is still in use ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, pa_table
includes all information and is a copy: see the example of read_all()
. https://arrow.apache.org/docs/python/ipc.html
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think read_all()
doesn't load the data in memory here, it loads buffers that are memory mapped from disk
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I can confirm that this works:
import pyarrow as pa
BATCH_SIZE = 10000
NUM_BATCHES = 1000
schema = pa.schema([pa.field('nums', pa.int32())])
# Write in stream format
with pa.OSFile('bigfile.arrow', 'wb') as sink:
with pa.ipc.new_stream(sink, schema) as writer:
for _ in range(NUM_BATCHES):
batch = pa.record_batch([pa.array(range(BATCH_SIZE), type=pa.int32())], schema)
writer.write(batch)
# Read the stream back
with pa.memory_map('bigfile.arrow', 'rb') as source:
with pa.ipc.open_stream(source) as reader:
table = reader.read_all()
print("LEN:", table.num_rows)
print("RSS: {}MB".format(pa.total_allocated_bytes() >> 20))
# Read the first batch
print("")
print("First batch:")
print(table[0][0:BATCH_SIZE])
Out:
LEN: 10000000
RSS: 0MB
First batch:
[
[
0,
1,
2,
3,
4,
...
9995,
9996,
9997,
9998,
9999
]
]
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@lasuomela Thanks a lot for checking! I’m currently on vacation and without a laptop to verify it myself.
@lhoestq Would this be sufficient proof for you?
The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update. |
Fixes dataset loading from disk by ensuring that memory maps and streams are properly closed.
For more details, see #7373.