Skip to content

[BUG] Reductions with reduce=True raise a torch.cat error on a tensordict without tensor leaves #2497

Open

Description

@peterdsharpe

On a NonTensorData, an empty TensorDict, or a tensordict that holds only non-tensor entries, every reduction with reduce=True (sum, max, cummax and the others) raises ValueError: torch.cat(): expected a non-empty list of Tensors. The message does not say that the cause is the absence of tensor leaves. Without reduce=True, NonTensorData("text", batch_size=[3]).cummax(0) returns a result.

import torch
from tensordict import NonTensorData, TensorDict

for td in (NonTensorData("text", batch_size=[3]), TensorDict(batch_size=[3])):
    try:
        td.cummax(0, reduce=True)
    except ValueError as e:
        print(f"{type(e).__name__}: {e}")
print(torch.cummax(torch.zeros(0), 0).values)
ValueError: torch.cat(): expected a non-empty list of Tensors
ValueError: torch.cat(): expected a non-empty list of Tensors
tensor([])

Expected: either a clear error that reduce=True needs at least one tensor leaf, or the reduction of an empty tensor, as torch gives for torch.cummax(torch.zeros(0), 0) or torch.zeros(0).sum(). Torch itself raises for max and amax of an empty tensor without a dim, so the second choice does not give a result for every reduction.

Cause: the further_reduce branch of _cast_reduction collects the tensor leaves with _values_list(True, True, is_leaf=_NESTED_TENSORS_AS_LISTS) and passes them to torch.cat (tensordict/_base/reductions.py:1978-1984 without a dim, :1990-2019 with one) without checking that the list is non-empty. Non-tensor leaves are not in the list, so a tensordict that holds only them gives an empty list.

Possible fixes: raise a ValueError naming the cause when the list is empty, or reduce torch.empty(0) in that case. Either changes only calls that raise today. The choice between the two is a judgment call.

tensordict main (eba7879), torch 2.14.1, Python 3.12.12.

Activity

  1. added
    bugSomething isn't working
    triagedMaintainers have seen this issue and commented their findings.
    on Oct 13, 2026
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

    bugSomething isn't workingtriagedMaintainers have seen this issue and commented their findings.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions