Skip to content

send_to_device crashes on defaultdict batches #4154

Description

@aswanth-07

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

  • The official example scripts
  • My own modified scripts

Tasks

  • One of the official example tasks
  • My own task or dataset

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.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions