Preserve defaultdict batches during device placement - #4155
Open
aswanth-07 wants to merge 1 commit into
Open
Conversation
Signed-off-by: aswanth-07 <a.aswanth0707@gmail.com>
aswanth-07
marked this pull request as ready for review
August 9, 2026 11:12
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What does this PR do?
Preserves standard-library
defaultdictbatches whilesend_to_devicerecursively moves their values.send_to_devicecurrently reconstructs every mapping astype(mapping)(items). Fordefaultdict, that positional argument is interpreted as the default factory, so prepared dataloaders crash before yielding a batch. This change rebuilds adefaultdictwith both its originaldefault_factoryand 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 passedpytest tests/test_utils.py -q -k 'not test_convert_to_fp32'- 43 passed, 5 skippedruff check .- passedruff format --check .- 184 files already formattedAccelerator.prepare(DataLoader(...))reproduction - fixed on CPUThe one excluded test invokes
torch.compileand fails on this Windows host because the MSVCclcompiler 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.