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

[Quality] better error message for CompositeSpec shape mismatch #2223

Merged
merged 1 commit into from
Jun 11, 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
init
  • Loading branch information
vmoens committed Jun 11, 2024
commit 7db222e7a22bac389aeccdd882401ed0c9589f94
16 changes: 16 additions & 0 deletions test/test_specs.py
Original file line number Diff line number Diff line change
Expand Up @@ -1907,6 +1907,22 @@ def test_unboundeddiscrete(
assert spec == torch.stack(spec.unbind(0), 0)
assert spec == torch.stack(spec.unbind(-1), -1)

def test_composite_encode_err(self):
c = CompositeSpec(
a=UnboundedContinuousTensorSpec(
1,
),
b=UnboundedContinuousTensorSpec(
2,
),
)
with pytest.raises(KeyError, match="The CompositeSpec instance with keys"):
c.encode({"c": 0})
with pytest.raises(
RuntimeError, match="raised a RuntimeError. Scroll up to know more"
):
c.encode({"a": 0, "b": 0})


@pytest.mark.parametrize(
"device",
Expand Down
4 changes: 4 additions & 0 deletions torchrl/data/tensor_specs.py
Original file line number Diff line number Diff line change
Expand Up @@ -3979,6 +3979,10 @@ def encode(
raise KeyError(
f"The CompositeSpec instance with keys {self.keys()} does not have a '{key}' key."
)
except RuntimeError as err:
raise RuntimeError(
f"Encoding key {key} raised a RuntimeError. Scroll up to know more."
) from err
return out

def __repr__(self) -> str:
Expand Down
Loading