Skip to content

[BugFix] Keep keys when cat_tensors or stack_tensors fails - #2645

Open
YeonwooSung wants to merge 2 commits into
pytorch:mainfrom
YeonwooSung:fix/cat-stack-tensors-atomic
Open

YeonwooSung wants to merge 2 commits into
pytorch:mainfrom
YeonwooSung:fix/cat-stack-tensors-atomic

Conversation

@YeonwooSung

Copy link
Copy Markdown

Problem

TensorDictBase.cat_tensors and TensorDictBase.stack_tensors remove the input keys before calling torch.cat / torch.stack when keep_entries is false (the default). If the tensors cannot be combined, the call raises RuntimeError after those keys are already gone, and out_key is never written. The same thing happens when set rejects the result, for example because concatenating along a batch dimension no longer matches batch_size.

On success, keep_entries=False still removes the input keys and stores the result at out_key, including when out_key is one of the inputs.

Fix

Both methods now read the entries and build the result first. Keys are removed only after that tensor exists. If a later set fails, the removed entries are written back and the original exception is re-raised. A shape mismatch therefore leaves the tensordict untouched.

Tests

test/tensordict/test_methods.py covers:

  • cat_tensors("a", "b", out_key="c", dim=1) when the last dimensions differ
  • stack_tensors on entries with different shapes
  • set rejecting the result, including when out_key already exists or is one of the inputs
  • success when out_key is one of the inputs, with and without keep_entries

Before the fix, the failure tests left "a" and "b" missing. After the fix:

pytest test/tensordict/test_methods.py::TestTensorDicts::test_cat_tensors test/tensordict/test_methods.py::TestTensorDicts::test_cat_tensors_shape_mismatch_preserves_entries test/tensordict/test_methods.py::TestTensorDicts::test_cat_tensors_set_failure_preserves_entries test/tensordict/test_methods.py::TestTensorDicts::test_cat_tensors_out_key_is_input test/tensordict/test_methods.py::TestTensorDicts::test_stack_tensors test/tensordict/test_methods.py::TestTensorDicts::test_stack_tensors_shape_mismatch_preserves_entries test/tensordict/test_methods.py::TestTensorDicts::test_stack_tensors_set_failure_preserves_entries test/tensordict/test_methods.py::TestTensorDicts::test_stack_tensors_out_key_is_input -q --tb=short

120 passed, 108 skipped. The skips are the other container types; these cases run on TensorDict. Ruff check and format are clean on the touched files.

… removed

A shape mismatch or a rejected set drops the entries that were popped
before torch.cat or torch.stack runs, and out_key is never written.
Cover both methods, including when out_key is already an entry.
Compute the concatenated or stacked tensor before removing input entries.
If writing the result fails, put those entries back and re-raise.
@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 Test bug Something isn't working 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.

1 participant