Skip to content

Pin BatchData so DataLoader pin_memory takes effect - #7

Merged
davek44 merged 1 commit into
mainfrom
loader-pin-memory
Oct 10, 2026
Merged

davek44 merged 1 commit into
mainfrom
loader-pin-memory

Conversation

@davek44

@davek44 davek44 commented Oct 9, 2026

Copy link
Copy Markdown
Collaborator

pin_memory=True (the CUDA default) was a silent no-op for BatchData: torch's pin thread pins only objects that expose pin_memory(). Every batch was copied host→device synchronously from pageable memory.

  • BatchData.pin_memory() pins every non-None tensor.
  • _unpack_batch / _mlm_unpack copy with non_blocking=True.
  • train_folds.md: size num_workers to the example size.

On an H100 at 786 kb × 8361 targets, batch 4, the copy goes from 0.175 s to 0.063 s, about 3–5% of step time. The pinned cache adds ~28 GB of host RAM at 16 workers, so a memory-limited host can set pin_mem: false.

Tests: new test_batchdata_pin_memory (CUDA only); the dataset, train, MLM, multidataset and sampler suites pass.

🤖 Generated with Claude Code

DataLoader(pin_memory=True) pins only objects that expose pin_memory(), so
BatchData batches were copied host->device synchronously from pageable
memory. Add BatchData.pin_memory() and copy with non_blocking=True. Note in
train_folds.md that num_workers must scale with example size.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@davek44
davek44 merged commit 7d4973e into main Oct 10, 2026
3 checks passed
@davek44
davek44 deleted the loader-pin-memory branch October 10, 2026 04:19
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.

1 participant