Skip to content

[BUG] Operations on Spec of Stacked CompositeSpec with different keys crashes #1343

@hyerra

Description

@hyerra

Describe the bug

I'm trying to print a CompositeSpec, but the current implementation crashes with a KeyError. This is related to #1201.
cc: @matteobettini

To Reproduce

Please try running the following code example:

import torch
from torchrl.data.tensor_specs import (
    CompositeSpec,
    UnboundedContinuousTensorSpec,
)

specs = [CompositeSpec({
    "observation_0": UnboundedContinuousTensorSpec(
        shape=torch.Size([128, 128, 3]),
        device="cpu",
        dtype=torch.float32)}),
    CompositeSpec({
        "observation_1": UnboundedContinuousTensorSpec(
        shape=torch.Size([128, 128, 3]),
        device="cpu",
        dtype=torch.float32)}), CompositeSpec({
    "observation_2": UnboundedContinuousTensorSpec(
        shape=torch.Size([128, 128, 3]),
        device="cpu",
        dtype=torch.float32)}), CompositeSpec({
    "observation_3": UnboundedContinuousTensorSpec(
        shape=torch.Size([4]),
        device="cpu",
        dtype=torch.float32)})]

spec = torch.stack(specs, dim=0)
print(spec)

You will see the crash. Here is a traceback that I've seen:

Traceback (most recent call last):
  File "/Users/hyerra/Desktop/test.py", line 27, in <module>
    print(spec)
  File "/Users/hyerra/Library/Caches/pypoetry/virtualenvs/.../lib/python3.10/site-packages/torchrl/data/tensor_specs.py", line 3165, in __repr__
    [indent(f"{k}: {repr(item)}", 4 * " ") for k, item in self.items()]
  File "/Users/hyerra/Library/Caches/pypoetry/virtualenvs/.../lib/python3.10/site-packages/torchrl/data/tensor_specs.py", line 3165, in <listcomp>
    [indent(f"{k}: {repr(item)}", 4 * " ") for k, item in self.items()]
  File "/Users/hyerra/Library/Caches/pypoetry/virtualenvs/.../lib/python3.10/site-packages/torchrl/data/tensor_specs.py", line 3139, in items
    yield key, self[key]
  File "/Users/hyerra/Library/Caches/pypoetry/virtualenvs/.../lib/python3.10/site-packages/torchrl/data/tensor_specs.py", line 760, in __getitem__
    [composite_spec[item] for composite_spec in self._specs], dim=self.dim
  File "/Users/hyerra/Library/Caches/pypoetry/virtualenvs/.../lib/python3.10/site-packages/torchrl/data/tensor_specs.py", line 760, in <listcomp>
    [composite_spec[item] for composite_spec in self._specs], dim=self.dim
  File "/Users/hyerra/Library/Caches/pypoetry/virtualenvs/.../lib/python3.10/site-packages/torchrl/data/tensor_specs.py", line 2624, in __getitem__
    return self._specs[idx]
KeyError: 'observation_0'

Expected behavior

I should be able to print the spec, especially if it was created successfully.

System info

Describe the characteristic of your environment:

import torchrl, numpy, sys
print(torchrl.__version__, numpy.__version__, sys.version, sys.platform)

None 1.24.3 3.10.11 (main, Apr 7 2023, 07:24:53) [Clang 14.0.0 (clang-1400.0.29.202)] darwin

Checklist

  • I have checked that there is no similar issue in the repo (required)
  • I have read the documentation (required)
  • I have provided a minimal working example to reproduce the bug (required)

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions