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,Feature] Allow non-tensor data in envs #1944

Merged
merged 11 commits into from
Jun 19, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
amend
  • Loading branch information
vmoens committed Jun 11, 2024
commit 976043009ec688f0eb347ecbd54516921e32bce3
52 changes: 0 additions & 52 deletions torchrl/data/tensor_specs.py
Original file line number Diff line number Diff line change
Expand Up @@ -4414,58 +4414,6 @@ def locked(self):
return self._locked


class NonTensorSpec(TensorSpec):
"""Tensor spec for non-tensor data.

This spec has no space or dtype, but the device and shape can be specified
(as it is the case for :class:`~tensordict.NonTensorData`).

"""

shape: torch.Size
space: Union[None, Box] = None
device: torch.device | None = None
dtype: torch.dtype = None
domain: str = "continuous"

def __init__(self, shape=(), device=None):
if device is not None:
device = torch.device(device)
return super().__init__(shape=shape, space=None, device=device, dtype=None)

def rand(self, shape=None) -> torch.Tensor:
return NonTensorData(None, batch_size=self.shape)

def zero(self, shape=None) -> torch.Tensor:
return NonTensorData(None, batch_size=self.shape)

def expand(self, *shape):
if len(shape) == 1 and not isinstance(shape[0], int):
shape = shape[0]
return NonTensorSpec(device=self.device, shape=shape)

def is_in(self, val: torch.Tensor) -> bool:
return True

def index(self, index: INDEX_TYPING, tensor_to_index: torch.Tensor) -> torch.Tensor:
raise NotImplementedError

def to(self, dest):
if dest is None:
return self
if isinstance(dest, torch.dtype):
dest_dtype = dest
dest_device = self.device
else:
dest_dtype = self.dtype
dest_device = torch.device(dest)
if dest_device == self.device and dest_dtype == self.dtype:
return self
return self.__class__(shape=self.shape, device=dest)

def clone(self) -> "TensorSpec":
return self.__class__(shape=self.shape, device=self.device)


class LazyStackedCompositeSpec(_LazyStackedMixin[CompositeSpec], CompositeSpec):
"""A lazy representation of a stack of composite specs.
Expand Down
4 changes: 2 additions & 2 deletions torchrl/envs/batched_envs.py
Original file line number Diff line number Diff line change
Expand Up @@ -671,8 +671,8 @@ def _create_td(self) -> None:
self.full_reward_spec,
self.full_done_spec,
):
for key, spec in spec.items(True, True):
if isinstance(spec, NonTensorSpec):
for key, _spec in spec.items(True, True):
if isinstance(_spec, NonTensorSpec):
non_tensor_keys.append(key)
self._non_tensor_keys = non_tensor_keys

Expand Down
Loading