Skip to content

[BugFix] NonTensor.encode must return a NonTensorData #2944

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

Merged
merged 1 commit into from
May 8, 2025
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
11 changes: 9 additions & 2 deletions test/test_specs.py
Original file line number Diff line number Diff line change
Expand Up @@ -3940,10 +3940,17 @@ def test_device_cast(self):
assert comp["nontensor"].device == torch.device("cpu")

def test_encode(self):
nt = NonTensor(shape=(1,))
r = nt.encode("a string")
assert isinstance(r, NonTensorData)
assert r.shape == nt.shape

comp = Composite(device="cpu")
comp["nontensor"] = NonTensor(shape=())
comp["nontensor"] = nt
r = comp.encode({"nontensor": "a string"})
assert isinstance(r["nontensor"], str)
assert isinstance(r, TensorDict)
assert isinstance(r.get("nontensor"), NonTensorData)
assert r.get("nontensor").shape == (1,)


@pytest.mark.skipif(not torch.cuda.is_available(), reason="not cuda device")
Expand Down
2 changes: 1 addition & 1 deletion torchrl/data/tensor_specs.py
Original file line number Diff line number Diff line change
Expand Up @@ -2918,7 +2918,7 @@ def _encode_eager(
*,
ignore_device: bool = False,
) -> torch.Tensor | TensorDictBase:
return val
return NonTensorData(val, device=self.device, batch_size=self.shape)


class _UnboundedMeta(abc.ABCMeta):
Expand Down
Loading