System Info
Accelerate version: 1.14.0; also reproduced on 1.15.0.dev0 at 16cb6eb8
- Platform: Windows-10-10.0.26200-SP0
- Python version: 3.11.9
- NumPy version: 2.4.6
- PyTorch version: 2.13.0+cpu
- PyTorch accelerator: N/A
- Accelerate default config: Not found
Information
Tasks
Reproduction
Accelerator.prepare() fails during iteration when a PyTorch DataLoader collator returns a standard-library collections.defaultdict:
from collections import defaultdict
import torch
from torch.utils.data import DataLoader
from accelerate import Accelerator
def collate(examples):
return defaultdict(list, input_ids=torch.tensor(examples))
accelerator = Accelerator(cpu=True)
dataloader = accelerator.prepare(
DataLoader([1, 2], batch_size=2, collate_fn=collate)
)
print(next(iter(dataloader)))
Actual result:
TypeError: first argument must be callable or None
This also reproduces directly with the public utility:
from accelerate.utils import send_to_device
send_to_device(
defaultdict(list, input_ids=torch.tensor([1, 2])),
"cpu",
)
send_to_device reconstructs every Mapping as type(tensor)(new_mapping). For defaultdict, the first positional constructor argument is the default factory rather than initial data, which causes the exception.
Relevant source:
|
skip_keys = [skip_keys] |
|
elif skip_keys is None: |
|
skip_keys = [] |
|
return type(tensor)( |
|
{ |
|
k: t if k in skip_keys else send_to_device(t, device, non_blocking=non_blocking, skip_keys=skip_keys) |
|
for k, t in tensor.items() |
|
} |
|
) |
|
else: |
I have a focused fix and regression test prepared.
Expected behavior
Prepared dataloaders should preserve the mapping type, tensor values, and default_factory, and yield the batch without error. For the reproduction above, the expected batch is a defaultdict containing tensor([1, 2]), with missing keys still producing [].
AI disclosure: I used Codex to help identify and independently reproduce this behavior on the current release and main, search for duplicates, test a focused fix, and draft this report. I reviewed the reproduction, patch, and tests and will personally handle follow-up.
System Info
Accelerateversion: 1.14.0; also reproduced on 1.15.0.dev0 at16cb6eb8Information
Tasks
Reproduction
Accelerator.prepare()fails during iteration when a PyTorchDataLoadercollator returns a standard-librarycollections.defaultdict:Actual result:
This also reproduces directly with the public utility:
send_to_devicereconstructs everyMappingastype(tensor)(new_mapping). Fordefaultdict, the first positional constructor argument is the default factory rather than initial data, which causes the exception.Relevant source:
accelerate/src/accelerate/utils/operations.py
Lines 181 to 190 in 16cb6eb
I have a focused fix and regression test prepared.
Expected behavior
Prepared dataloaders should preserve the mapping type, tensor values, and
default_factory, and yield the batch without error. For the reproduction above, the expected batch is adefaultdictcontainingtensor([1, 2]), with missing keys still producing[].AI disclosure: I used Codex to help identify and independently reproduce this behavior on the current release and main, search for duplicates, test a focused fix, and draft this report. I reviewed the reproduction, patch, and tests and will personally handle follow-up.