Skip to content

Preserve defaultdict batches during device placement - #4155

Open
aswanth-07 wants to merge 1 commit into
huggingface:mainfrom
aswanth-07:preserve-defaultdict-device-placement
Open

Preserve defaultdict batches during device placement#4155
aswanth-07 wants to merge 1 commit into
huggingface:mainfrom
aswanth-07:preserve-defaultdict-device-placement

Conversation

@aswanth-07

@aswanth-07 aswanth-07 commented Aug 9, 2026

Copy link
Copy Markdown

What does this PR do?

Preserves standard-library defaultdict batches while send_to_device recursively moves their values.

send_to_device currently reconstructs every mapping as type(mapping)(items). For defaultdict, that positional argument is interpreted as the default factory, so prepared dataloaders crash before yielding a batch. This change rebuilds a defaultdict with both its original default_factory and the moved values, while leaving the existing path unchanged for other mapping types.

Fixes #4154

Validation

  • pytest tests/test_utils.py::UtilsTester::test_send_to_device -q - 1 passed
  • pytest tests/test_utils.py -q -k 'not test_convert_to_fp32' - 43 passed, 5 skipped
  • Full repository ruff check . - passed
  • Full repository ruff format --check . - 184 files already formatted
  • Public Accelerator.prepare(DataLoader(...)) reproduction - fixed on CPU

The one excluded test invokes torch.compile and fails on this Windows host because the MSVC cl compiler is unavailable; it is unrelated to this mapping change.

Before submitting

AI disclosure: I used Codex to help identify and reproduce the bug, search for duplicates, implement the focused patch, run validation, and draft this PR. I reviewed and understand the change and will personally handle review feedback.

Signed-off-by: aswanth-07 <a.aswanth0707@gmail.com>
@aswanth-07
aswanth-07 marked this pull request as ready for review August 9, 2026 11:12
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.

send_to_device crashes on defaultdict batches

1 participant