Skip to content

convert_tensor fails on default-collated defaultdict batches聽#3890

Closed

Description

@ktz03

馃悰 Bug description

convert_tensor raises TypeError: first argument must be callable or None for a collections.defaultdict containing tensors. PyTorch's default DataLoader collation preserves this mapping type, so the same error stops create_supervised_trainer when it uses the default prepare_batch, including on CPU.

from collections import defaultdict

import torch
from ignite.engine import create_supervised_trainer

features = defaultdict(list, {"x": torch.tensor([1.0])})
loader = torch.utils.data.DataLoader(
    [(features, torch.tensor([0.0]))] * 2, batch_size=2
)
model = torch.nn.Linear(1, 1)
optimizer = torch.optim.SGD(model.parameters(), lr=0.1)
trainer = create_supervised_trainer(
    model,
    optimizer,
    torch.nn.MSELoss(),
    device="cpu",
    model_fn=lambda model, batch: model(batch["x"]),
)
trainer.run(loader)

The batch returned by the DataLoader is a defaultdict with its original factory. apply_to_type reconstructs it as type(x)(converted_values), which supplies the converted dictionary as the factory rather than as the contents. The same issue occurs with a None factory and with nested defaultdict values. Ordinary dict and OrderedDict batches complete training and evaluation.

Expected behavior: recursively convert the tensors while preserving the mapping's default factory, allowing the trainer to process the batch.

Environment

  • PyTorch Version: 2.8.0+cpu
  • Ignite Version: source checkout of master at bffc0f45158e5290f5901fb00b106987fdc779fb (0.6.0)
  • OS: Windows 11, build 26200
  • How you installed Ignite: source checkout on PYTHONPATH
  • Python version: 3.12.14
  • Any other relevant information: reproduced with native DataLoader default collation and the trainer's default batch preparation; flat and nested mappings with None and list factories fail.

Activity

  1. parth-bansal081 commented on Oct 13, 2026

    @parth-bansal081
    Contributor

    Hi @ktz03 , Is this issue still open to contribute?

  2. vfdev-5 commented on Oct 13, 2026

    @vfdev-5
    Collaborator

    @ktz03 why users would use defaultdict as in your example?

    features = defaultdict(list, {"x": torch.tensor([1.0])})
    

    Did you find this issue from a ML project or an AI agent hinted you that?

  3. ktz03 commented on Oct 13, 2026

    @ktz03
    ContributorAuthor

    This was identified with AI assistance, then reproduced and human-reviewed. The example is a minimal reproducer: PyTorch preserves defaultdict during collation, but Ignite fails when rebuilding it. The fix and regression tests are in #3891.

  4. vfdev-5 commented on Oct 13, 2026

    @vfdev-5
    Collaborator

    Can you provide the link to the PyTorch code where they handle defaultdict during collation?

    I still do not understand why user would use defaultdict as input sample.

  5. ktz03 commented on Oct 13, 2026

    @ktz03
    ContributorAuthor

    PyTorch's implementation copies and updates mutable mappings, preserving defaultdict's factory.

    The snippet is a minimal compatibility reproducer, not a recommended data format. I agree practical relevance matters and will follow Ignite's intended support scope.

  6. vfdev-5 commented on Oct 13, 2026

    @vfdev-5
    Collaborator

    @ktz03 thanks for the link on the PyTorch code!
    I like their code and prefer it compared to what your AI agent coded in your PR #3891.
    I recommend to update your PR and use the same if/else branching as in PyTorch (and their comment etc, and put the link on the pytorch code).
    As for the tests, all the changes in your PR are inappropriate (too much unrelated code which we do not want to maintain), revert everything and just make another test case in this test:

    def test_convert_tensor():
    . The change should be minimal as possible.

  7. ktz03 commented on Oct 13, 2026

    @ktz03
    ContributorAuthor

    Updated #3891 to follow PyTorch's mapping branches, including the comments and source link. The tests are reduced to one case in the existing test_convert_tensor, and the focused checks pass.

  8. added a commit that references this issue on Oct 14, 2026
    7eb1d1f
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