Skip to content

[BUG] NonTensorStack.reshape gives the wrong batch size for a stack with a zero-size dim #2415

Open

Description

@peterdsharpe

On a NonTensorStack with batch size [3, 0], reshape(1, -1) returns batch size [3, 0]; torch gives [1, 0].

import torch

from tensordict import NonTensorData, NonTensorStack

s = NonTensorStack(*[NonTensorData("a", batch_size=[0]) for _ in range(3)])
print(s.batch_size, s.reshape(1, -1).batch_size, torch.zeros(3, 0).reshape(1, -1).shape)
torch.Size([3, 0]) torch.Size([3, 0]) torch.Size([1, 0])

Expected: torch.Size([1, 0]).

Cause, not traced to the line: NonTensorStack.reshape (tensordict/tensorclass.py:6396) reshapes a grid of member positions, which is empty here, and regroups the stack with _view or _select_positions; one of them keeps the old outer shape when there are no positions.

Found while reviewing #2410, which sends view of a zero-size lazy stack to reshape; for NonTensorStack it makes view give the same wrong sizes.

tensordict main (924b72a), 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 12, 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