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

[BugFix] FIx non-tensor writing in modules #822

Merged
merged 1 commit into from
Jun 19, 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
4 changes: 3 additions & 1 deletion tensordict/nn/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -1196,7 +1196,9 @@ def forward(
) from err
else:
raise err
if isinstance(tensors, (dict, TensorDictBase)):
if isinstance(tensors, (dict, TensorDictBase)) and all(
key in tensors for key in self.out_keys
):
if isinstance(tensors, dict):
keys = unravel_key_list(list(tensors.keys()))
values = tensors.values()
Expand Down
22 changes: 21 additions & 1 deletion test/test_nn.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,13 @@
import pytest
import torch

from tensordict import LazyStackedTensorDict, tensorclass, TensorDict
from tensordict import (
LazyStackedTensorDict,
NonTensorData,
NonTensorStack,
tensorclass,
TensorDict,
)
from tensordict._tensordict import unravel_key_list
from tensordict.nn import (
dispatch,
Expand Down Expand Up @@ -365,6 +371,20 @@ def test_stateful_probabilistic_kwargs(
assert td.shape == torch.Size([3])
assert td.get("out").shape == torch.Size([3, 4])

def test_nontensor(self):
tdm = TensorDictModule(
lambda: NonTensorStack(NonTensorData(1), NonTensorData(2)),
in_keys=[],
out_keys=["out"],
)
assert tdm(TensorDict({}))["out"] == [1, 2]
tdm = TensorDictModule(
lambda: "a string!",
in_keys=[],
out_keys=["out"],
)
assert tdm(TensorDict({}))["out"] == "a string!"

@pytest.mark.parametrize(
"out_keys",
[
Expand Down
Loading