Skip to content

[BUG] unravel_key_to_tuple returns empty tuple when non-str elements are present #1356

@LCarmi

Description

@LCarmi

Describe the bug

_unravel_key_to_tuple returns empty tuple when elements that are not tuple or str are present in the key. The expected behavior should be to drop the non-str elements, and return the other elements as a flat tuple.

To Reproduce

>>> import tensordict
>>> ok_key = ("agent", "observation")
>>> ko_key = (None, "observation")
>>> tensordict.utils._unravel_key_to_tuple(ok_key)
('next', 'agent', 'observation')
>>> tensordict.utils._unravel_key_to_tuple(ko_key)
()

Expected behavior

The expected behavior is the path taken when torch.is_compiling() == True

>>> tuple(subk for k in ok_key for subk in tensordict.utils._unravel_key_to_tuple(k))
('next', 'agent', 'observation')
>>> tuple(subk for k in ko_key for subk in tensordict.utils._unravel_key_to_tuple(k))
('next', 'observation')

Reason and Possible fixes

In the file https://github.com/pytorch/tensordict/blob/main/tensordict/csrc/utils.cpp, we can see that

if (_key.size() == 0) {
  return py::make_tuple();
}

which discards the previously accumulated keys in newkey

Checklist

  • I have checked that there is no similar issue in the repo (required)
  • I have read the documentation (required)
  • I have provided a minimal working example to reproduce the bug (required)

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions