Skip to content

[BugFix] Reject a names list whose length is not batch_dims - #2653

Open
YeonwooSung wants to merge 2 commits into
pytorch:mainfrom
YeonwooSung:fix/names-length-check
Open

YeonwooSung wants to merge 2 commits into
pytorch:mainfrom
YeonwooSung:fix/names-length-check

Conversation

@YeonwooSung

Copy link
Copy Markdown

Fixes #2600

TensorDictBase._set_names counted None entries and, when that count equalled batch_dims, cleared the names and returned before checking that len(value) == batch_dims. A list one entry too long was therefore accepted whenever it held exactly batch_dims Nones, and every name was dropped:

TensorDict(a=torch.zeros(3), batch_size=[3], names=["s", None]).names  # was [None], no error
# names=["s", "t"] already raised ValueError

The same shortcut ran for a lazy stack. s.names = ["s", "t", None] on members of batch [3] forwards ["t", None] to each member, which has exactly one None. The member cleared its names and "t" was dropped. Checking the length in the base method rejects that list before any name is written on the member, so the members are not left half-updated.

The length check now comes first and raises the existing ValueError (the length of the dimension names must equate the tensordict batch_dims attribute). Names that were already set are left unchanged. An all-None list of the right length still clears names. A right-length mix of strings and Nones still assigns.

This is a silent data loss, so it raises now. It is not a scheduled default change.

A list with exactly batch_dims None entries was accepted and every name was dropped. Lock the length check, the all-None clear, and the lazy stack that forwards the extra entry to its members.
_set_names cleared names whenever a list held exactly batch_dims None entries, and it did that before checking the length. A longer list then dropped every name, including when a lazy stack forwarded the extra entry to its members. Check the length first. An all-None list of the right length still clears names.
@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Oct 14, 2026
@github-actions github-actions Bot added bug Something isn't working Test labels Oct 14, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. Test

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] A names list one entry too long is accepted when it holds exactly batch_dims None entries, and its names are dropped

1 participant