Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature] NonTensorStack.data #1132

Merged
merged 1 commit into from
Dec 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions tensordict/_lazy.py
Original file line number Diff line number Diff line change
Expand Up @@ -2127,10 +2127,10 @@ def __getitem__(self, index: IndexType) -> Any:
if index_key:
leaf = self._get_tuple(index_key, NO_DEFAULT)
if is_non_tensor(leaf):
result = getattr(leaf, "data", NO_DEFAULT)
if result is NO_DEFAULT:
# Only lazy stacks of non tensors are actually tensordict instances
if isinstance(leaf, TensorDictBase):
return leaf.tolist()
return result
return leaf.data
return leaf
split_index = self._split_index(index)
converted_idx = split_index["index_dict"]
Expand Down
6 changes: 3 additions & 3 deletions tensordict/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -6268,10 +6268,10 @@ def _get_tuple(self, key, default): ...
def _get_tuple_maybe_non_tensor(self, key, default):
result = self._get_tuple(key, default)
if is_non_tensor(result):
result_data = getattr(result, "data", NO_DEFAULT)
if result_data is NO_DEFAULT:
# Only lazy stacks of non tensors are actually tensordict instances
if isinstance(result, TensorDictBase):
return result.tolist()
return result_data
return result.data
return result

def get_at(
Expand Down
11 changes: 10 additions & 1 deletion tensordict/tensorclass.py
Original file line number Diff line number Diff line change
Expand Up @@ -3445,7 +3445,16 @@ def update_at_(

@property
def data(self):
raise AttributeError
"""Attempts to return the unique value in the stack.

Raises a ValueError if there is more than one unique value.
"""
try:
return NonTensorData._stack_non_tensor(
self.tensordicts, raise_if_non_unique=True
).data
except ValueError:
raise AttributeError("Cannot get the non-unique data of a NonTensorStack. Use .tolist() instead.")


_register_tensor_class(NonTensorStack)
Expand Down
Loading