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] NonTensor should not convert anything to numpy #2771

Open
wants to merge 3 commits into
base: gh/vmoens/88/base
Choose a base branch
from
Open
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
15 changes: 14 additions & 1 deletion torchrl/data/tensor_specs.py
Original file line number Diff line number Diff line change
Expand Up @@ -2619,6 +2619,19 @@ def unbind(self, dim: int = 0):
for i in range(self.shape[dim])
)

def to_numpy(
self, val: torch.Tensor | TensorDictBase, safe: bool = None
) -> np.ndarray | dict:
return val

def encode(
self,
val: np.ndarray | torch.Tensor | TensorDictBase,
*,
ignore_device: bool = False,
) -> torch.Tensor | TensorDictBase:
return val


class _UnboundedMeta(abc.ABCMeta):
def __call__(cls, *args, **kwargs):
Expand Down Expand Up @@ -4918,7 +4931,7 @@ def rand(self, shape: torch.Size = None) -> TensorDictBase:
_dict[key] = item.rand(shape)
# No need to run checks since we know Composite is compliant with
# TensorDict requirements
return TensorDict._new_unsafe(
return TensorDict(
_dict,
batch_size=_size([*shape, *self.shape]),
device=self._device,
Expand Down
5 changes: 3 additions & 2 deletions torchrl/envs/gym_like.py
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,7 @@ def _step(self, tensordict: TensorDictBase) -> TensorDictBase:
for key, val in TensorDict(obs_dict, []).items(True, True)
)
else:
tensordict_out = TensorDict._new_unsafe(
tensordict_out = TensorDict(
obs_dict,
batch_size=tensordict.batch_size,
)
Expand Down Expand Up @@ -376,7 +376,8 @@ def _reset(

source = self.read_obs(obs)

tensordict_out = TensorDict._new_unsafe(
# _new_unsafe cannot be used because it won't wrap non-tensor correctly
tensordict_out = TensorDict(
source=source,
batch_size=self.batch_size,
)
Expand Down
Loading