Skip to content

[BugFix] Fix Binary reshaping #3084

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
Jul 18, 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
2 changes: 2 additions & 0 deletions test/test_specs.py
Original file line number Diff line number Diff line change
Expand Up @@ -3913,6 +3913,7 @@ def test_expand(self):
disc = Categorical(shape=(-1, 1, 2), n=4)
moneh = MultiOneHot(shape=(-1, 1, 2, 7), nvec=[3, 4])
mdisc = MultiCategorical(shape=(-1, 1, 2, 2), nvec=[3, 4])
binary = Binary(shape=(-1, 1, 2))

spec = Composite(
unb=unb,
Expand All @@ -3922,6 +3923,7 @@ def test_expand(self):
disc=disc,
moneh=moneh,
mdisc=mdisc,
binary=binary,
shape=(-1, 1, 2),
)
assert spec.shape == (-1, 1, 2)
Expand Down
16 changes: 9 additions & 7 deletions torchrl/data/tensor_specs.py
Original file line number Diff line number Diff line change
Expand Up @@ -4430,12 +4430,14 @@ def __init__(
raise ValueError(
f"'n' must be zero for spec {self.__class__} when using an empty shape"
)
else:
elif n is not None:
if shape[-1] != n:
raise ValueError(
f"The last value of the shape must match 'n' for spec {self.__class__}. "
f"Got n={n} and shape={shape}."
)
else:
n = shape[-1]

super().__init__(n=2, shape=shape, device=device, dtype=dtype)
self.encode = self._encode_eager
Expand All @@ -4449,15 +4451,15 @@ def expand(self, *shape):
f"shape of the {self.__class__.__name__} spec in expand()."
)
return self.__class__(
n=self.shape[-1] if len(self.shape) > 0 else None,
n=shape[-1] if len(shape) > 0 else None,
shape=shape,
device=self.device,
dtype=self.dtype,
)

def _reshape(self, shape):
return self.__class__(
n=self.shape[-1] if len(self.shape) > 0 else None,
n=shape[-1] if len(shape) > 0 else None,
shape=shape,
device=self.device,
dtype=self.dtype,
Expand All @@ -4470,7 +4472,7 @@ def _unflatten(self, dim, sizes):
.shape
)
return self.__class__(
n=self.shape[-1] if len(self.shape) > 0 else None,
n=shape[-1] if len(shape) > 0 else None,
shape=shape,
device=self.device,
dtype=self.dtype,
Expand All @@ -4481,7 +4483,7 @@ def squeeze(self, dim=None):
if shape is None:
return self
return self.__class__(
n=self.shape[-1] if len(self.shape) > 0 else None,
n=shape[-1] if len(shape) > 0 else None,
shape=shape,
device=self.device,
dtype=self.dtype,
Expand All @@ -4490,7 +4492,7 @@ def squeeze(self, dim=None):
def unsqueeze(self, dim: int):
shape = _unsqueezed_shape(self.shape, dim)
return self.__class__(
n=self.shape[-1] if len(self.shape) > 0 else None,
n=shape[-1] if len(shape) > 0 else None,
shape=shape,
device=self.device,
dtype=self.dtype,
Expand All @@ -4510,7 +4512,7 @@ def unbind(self, dim: int = 0):
shape = tuple(s for i, s in enumerate(self.shape) if i != dim)
return tuple(
self.__class__(
n=self.shape[-1] if len(self.shape) > 0 else None,
n=shape[-1] if len(shape) > 0 else None,
shape=shape,
device=self.device,
dtype=self.dtype,
Expand Down
Loading