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

[Feature] Dynamic specs #2143

Merged
merged 27 commits into from
May 31, 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 May 30, 2024
commit b0c208f8ae89243c13b4ec83189970c8a5216b8f
7 changes: 4 additions & 3 deletions test/test_libs.py
Original file line number Diff line number Diff line change
Expand Up @@ -365,9 +365,10 @@ def stack_tuples(item):
# sample_pt = torch.utils._pytree.tree_map(lambda x: torch.stack(list(x)), sample_pt, is_leaf=lambda x: isinstance(x, tuple))
spec = _gym_to_torchrl_spec_transform(space)
rand = spec.rand()
assert spec.is_in(rand)
assert rand in spec
assert sample_pt in spec, (rand, sample_pt)

assert spec.contains(rand), (rand, spec)
assert spec.contains(sample_pt), (rand, sample_pt)

space_recon = _torchrl_to_gym_spec_transform(spec)
assert space_recon == space, (space_recon, space)
rand_numpy = rand.numpy()
Expand Down
11 changes: 9 additions & 2 deletions torchrl/data/tensor_specs.py
Original file line number Diff line number Diff line change
Expand Up @@ -1254,8 +1254,15 @@ def type_check(self, value: torch.Tensor, key: str = None) -> None:
spec.type_check(val)

def is_in(self, value) -> bool:
# We don't use unbind because value could be a tuple
return all(value in spec for (value, spec) in zip(value, self._specs))
if self.dim == 0:
# We don't use unbind because value could be a tuple or a nested tensor
return all(
spec.contains(value) for (value, spec) in zip(value, self._specs)
)
return all(
spec.contains(value)
for (value, spec) in zip(value.unbind(self.dim), self._specs)
)

@property
def space(self):
Expand Down
Loading