Add configurable pin_memory and prefetch_factor for DataLoader#347
Add configurable pin_memory and prefetch_factor for DataLoader#347stephengreen wants to merge 3 commits into
Conversation
Adds `pin_memory` and `prefetch_factor` options to training settings under `local`. This allows users to tune memory vs speed tradeoffs: - `pin_memory` (default True): Uses pinned memory for faster GPU transfers but increases memory usage - `prefetch_factor` (default 1): Controls how many batches each worker prefetches; lower values reduce memory Reduces default prefetch_factor from 2 to 1 for lower memory usage. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Add documentation for the new DataLoader memory tuning options to the training docs, including memory usage estimates. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
|
Added documentation for the new settings in |
annalena-k
left a comment
There was a problem hiding this comment.
I have a few questions, but these are minor things.
I haven't tested the implementation myself, so I can't judge how much this change reduces the memory footprint.
There was a problem hiding this comment.
I tested the following with 64 CPUs and 63 workers:
pin_memory=True&prefetch_factor=1required 128GB RAM and the GPU load looked stable (roughly constant over batches)pin_memory=False&prefetch_factor=1required 95 GB RAM, but the GPU load was unstable (see screenshot, maybe longer training would have solved this)
pin_memory=True&prefetch_factor=2required ~270 GB RAM and looked stable (after longer training time)
I found the way the new options are included in the settings file confusing.
This is conceptually different from how we previously included comments in the example settings files, e.g. the early stopping is commented (disabled) and the user can uncomment it to enable it. Similar for local_cache_path. Defaults like leave_waveforms_on_disk: True are not commented.
I would recommend to stay consistent and not comment defaults.
| max_epochs_per_run: 500 | ||
| max_time_per_run: 360000 | ||
| leave_waveforms_on_disk: True | ||
| # pin_memory: True # Set False to reduce memory usage, but slows down transfer of batches from CPU to GPU |
There was a problem hiding this comment.
When I was testing this, it was confusing that this line is commented out.
As far as I understood, pin_memory=True by default, right?
Therefore, uncommenting this line actually does nothing. This is conceptually different from how we previously included comments in the example settings files, e.g. the early stopping is commented (disabled) and the user can uncomment it to enable it. Similar for local_cache_path. Defaults like leave_waveforms_on_disk: True are not commented.
I would recommend to stay consistent and not comment defaults to make it easy to understand for the user.
There was a problem hiding this comment.
The same applies to prefetch_factor and all example files.
Summary
pin_memoryandprefetch_factoroptions to training settings underlocalsectionprefetch_factorfrom 2 to 1 for lower memory usageResolves #308.
Details
Investigation of memory usage during training revealed that
pin_memory=Truecombined with the prefetch queue can significantly increase memory consumption. This PR:pin_memory(default True): Uses pinned memory for faster GPU transfers but increases memory usage. Can be set to False to save memory at cost of slower data transfer.prefetch_factor(default 1, reduced from 2): Controls how many batches each worker prefetches. Lower values reduce memory usage.Memory usage scales roughly as:
num_workers × prefetch_factor × batch_size × data_size, and is roughly doubled when using pinned memory.Performance testing
In a realistic training setup:
pin_memory=Falseincreases training time by ~20%, so keeping the defaultTrueis recommendedprefetch_factor=1(reduced from default 2) does not slow down training at all, while reducing memory usageTest plan
pin_memory=True,prefetch_factor=1)pin_memory=Falseworks (slower but lower memory)prefetch_factor=1vsprefetch_factor=2(no speed difference, lower memory)🤖 Generated with Claude Code