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
[BUG] Reductions with reduce=True raise a torch.cat error on a tensordict without tensor leaves #2497
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.
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