Skip to content

Commit

Permalink
[Feature] NonTensorData(*sequence_of_any)
Browse files Browse the repository at this point in the history
ghstack-source-id: 97575533232f31a72147f9403cbf9dccfb814514
Pull Request resolved: #1160
  • Loading branch information
vmoens committed Jan 7, 2025
1 parent ca56651 commit aba471a
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
7 changes: 6 additions & 1 deletion tensordict/tensorclass.py
Original file line number Diff line number Diff line change
Expand Up @@ -936,6 +936,8 @@ def wrapper(
batch_size = torch.Size(())
else:
batch_size = kwargs.pop("batch_size", torch.Size(()))
if isinstance(batch_size, int):
batch_size = (batch_size,)
if "names" in required_params:
names = None
else:
Expand Down Expand Up @@ -997,7 +999,7 @@ def wrapper(

# convert the non tensor data in a regular data
kwargs = {
key: value.data if is_non_tensor(value) else value
key: value.data if isinstance(value, NonTensorData) else value
for key, value in kwargs.items()
}
__init__(self, **kwargs)
Expand Down Expand Up @@ -3259,6 +3261,9 @@ class NonTensorStack(LazyStackedTensorDict):
_is_non_tensor: bool = True

def __init__(self, *args, **kwargs):
args = [
arg if is_tensor_collection(arg) else NonTensorData(arg) for arg in args
]
super().__init__(*args, **kwargs)
if not all(is_non_tensor(item) for item in self.tensordicts):
raise RuntimeError("All tensordicts must be non-tensors.")
Expand Down
14 changes: 14 additions & 0 deletions test/test_tensordict.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
make_tensordict,
PersistentTensorDict,
set_get_defaults_to_none,
TensorClass,
TensorDict,
)
from tensordict._lazy import _CustomOpTensorDict
Expand Down Expand Up @@ -10955,6 +10956,19 @@ def test_non_tensor_call(self):
assert td["a"] == -1
assert td["b"] == 1

def test_non_tensor_from_list(self):
class X(TensorClass):
non_tensor: str = None

x = X(batch_size=3)
x.non_tensor = NonTensorStack.from_list(["a", "b", "c"])
assert x[0].non_tensor == "a"
assert x[1].non_tensor == "b"

x = X(non_tensor=NonTensorStack("a", "b", "c"), batch_size=3)
assert x[0].non_tensor == "a"
assert x[1].non_tensor == "b"

def test_nontensor_dict(self, non_tensor_data):
assert (
TensorDict.from_dict(non_tensor_data.to_dict(), auto_batch_size=True)
Expand Down

0 comments on commit aba471a

Please sign in to comment.