Skip to content

Fix send_to_device crashing on defaultdict batches - #4162

Open
mayuriphad wants to merge 1 commit into
huggingface:mainfrom
mayuriphad:fix-send-to-device-defaultdict
Open

Fix send_to_device crashing on defaultdict batches#4162
mayuriphad wants to merge 1 commit into
huggingface:mainfrom
mayuriphad:fix-send-to-device-defaultdict

Conversation

@mayuriphad

Copy link
Copy Markdown

Summary

send_to_device reconstructs Mapping batches via type(tensor)(new_mapping). For collections.defaultdict, the constructor's first positional argument is the default_factory, not initial data, so this raises TypeError: first argument must be callable or None whenever a DataLoader collate function returns a defaultdict batch.

Fixes #4154.

Change

In src/accelerate/utils/operations.py::send_to_device, when the mapping being converted is a defaultdict, construct the new instance with type(tensor)(tensor.default_factory) and populate it via .update(new_data) instead of passing the data dict as the constructor's first argument. Other Mapping subclasses keep the previous behavior.

Test plan

  • Added tests/test_utils.py::UtilsTester::test_send_to_device_defaultdict, a regression test that reproduces the issue (fails before the fix with the exact TypeError from the report, passes after) and checks that the default_factory and existing values are preserved.
  • Ran python -m pytest tests/test_utils.py -k send_to_device -v -- all 3 tests pass.
  • Ran the full tests/test_utils.py suite -- same pass/fail results as before the change (one pre-existing, unrelated failure: test_convert_to_fp32, a torch._inductor/torch.compile issue in this local Windows/CPU environment, not touched by this PR).

AI disclosure: AI assistance was used to help investigate the issue and draft this fix and test. I reviewed the change and test results myself.

send_to_device reconstructed Mapping instances via type(tensor)(new_mapping),
but defaultdict's constructor takes default_factory as its first positional
argument rather than initial data, causing
"TypeError: first argument must be callable or None" whenever a DataLoader
collate_fn returned a defaultdict batch (fixes huggingface#4154).

Preserve the default_factory and populate the new defaultdict via update()
instead.
Copilot AI lite review requested due to automatic review settings August 13, 2026 15:51

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@mayuriphad

Copy link
Copy Markdown
Author

Hi maintainers — just checking in on this PR since it hasn't had a maintainer review yet. Happy to make any changes needed; let me know if anything's unclear or if I should split/rework it. Thanks for your time!

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

3 participants