Skip to content

Add configurable pin_memory and prefetch_factor for DataLoader#347

Open
stephengreen wants to merge 3 commits into
mainfrom
configurable-dataloader-memory
Open

Add configurable pin_memory and prefetch_factor for DataLoader#347
stephengreen wants to merge 3 commits into
mainfrom
configurable-dataloader-memory

Conversation

@stephengreen

Copy link
Copy Markdown
Member

Summary

  • Adds pin_memory and prefetch_factor options to training settings under local section
  • Allows users to tune memory vs speed tradeoffs during training
  • Reduces default prefetch_factor from 2 to 1 for lower memory usage

Resolves #308.

Details

Investigation of memory usage during training revealed that pin_memory=True combined 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=False increases training time by ~20%, so keeping the default True is recommended
  • prefetch_factor=1 (reduced from default 2) does not slow down training at all, while reducing memory usage

Test plan

  • Verified training runs with default settings (pin_memory=True, prefetch_factor=1)
  • Verified pin_memory=False works (slower but lower memory)
  • Verified prefetch_factor=1 vs prefetch_factor=2 (no speed difference, lower memory)

🤖 Generated with Claude Code

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>
@stephengreen

Copy link
Copy Markdown
Member Author

Added documentation for the new settings in docs/source/training.md, including the memory usage formula and performance findings.

@stephengreen

Copy link
Copy Markdown
Member Author

@srashtig

@annalena-k annalena-k left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread examples/fmpe_model/train_settings.yaml Outdated
Comment thread dingo/gw/training/train_pipeline.py Outdated
Comment thread dingo/gw/training/train_pipeline.py
Comment thread examples/toy_npe_model/train_settings.yaml
Comment thread dingo/core/utils/torchutils.py

@annalena-k annalena-k left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tested the following with 64 CPUs and 63 workers:

  • pin_memory=True & prefetch_factor=1 required 128GB RAM and the GPU load looked stable (roughly constant over batches)
  • pin_memory=False & prefetch_factor=1 required 95 GB RAM, but the GPU load was unstable (see screenshot, maybe longer training would have solved this)
image
  • pin_memory=True & prefetch_factor=2 required ~270 GB RAM and looked stable (after longer training time)
image

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

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The same applies to prefetch_factor and all example files.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

RAM usage on using leave_waveforms_on_disk = True

3 participants