You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
convert_tensor fails on default-collated defaultdict batches聽#3890
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.
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.
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.
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.
@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:
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.
Activity
parth-bansal081 commentedon Oct 13, 2026
Hi @ktz03 , Is this issue still open to contribute?
vfdev-5 commentedon Oct 13, 2026
@ktz03 why users would use defaultdict as in your example?
Did you find this issue from a ML project or an AI agent hinted you that?
ktz03 commentedon Oct 13, 2026
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.
vfdev-5 commentedon Oct 13, 2026
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.
ktz03 commentedon Oct 13, 2026
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.
vfdev-5 commentedon Oct 13, 2026
@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:
ignite/tests/ignite/test_utils.py
Line 14 in bffc0f4
ktz03 commentedon Oct 13, 2026
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.Fix defaultdict batch tensor conversion (pytorch#3891)