Repository navigation
[BugFix] Keep keys when cat_tensors or stack_tensors fails - #2645
Open
YeonwooSung wants to merge 2 commits into
Open
YeonwooSung wants to merge 2 commits into
YeonwooSung wants to merge 2 commits into
Conversation
… 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.
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
TensorDictBase.cat_tensorsandTensorDictBase.stack_tensorsremove the input keys before callingtorch.cat/torch.stackwhenkeep_entriesis false (the default). If the tensors cannot be combined, the call raisesRuntimeErrorafter those keys are already gone, andout_keyis never written. The same thing happens whensetrejects the result, for example because concatenating along a batch dimension no longer matchesbatch_size.On success,
keep_entries=Falsestill removes the input keys and stores the result atout_key, including whenout_keyis 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
setfails, 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.pycovers:cat_tensors("a", "b", out_key="c", dim=1)when the last dimensions differstack_tensorson entries with different shapessetrejecting the result, including whenout_keyalready exists or is one of the inputsout_keyis one of the inputs, with and withoutkeep_entriesBefore 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=short120 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.