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

[BUG] Expected a tensordict with shape==env.shape, got torch.Size([1]) and torch.Size([]) #1925

Closed
whatdhack opened this issue Feb 18, 2024 · 3 comments · Fixed by #1938
Closed
Assignees
Labels
bug Something isn't working

Comments

@whatdhack
Copy link

Describe the bug

What is the env.shape ? Does not appear to be defined anywhere

To Reproduce

import torchrl
env = GymEnv("CartPole-v1",  device=device)
tdact = TensorDict({"action":torch.tensor([1])}, batch_size=(1,))
env.step(tdact)
Traceback (most recent call last):
  File ... 
*** RuntimeError: Expected a tensordict with shape==env.shape, got torch.Size([1]) and torch.Size([])

Expected behavior

A clear and concise description of what you expected to happen.

Screenshots

No stacktrace

System info

Describe the characteristic of your environment:

  • Describe how the library was installed (pip, source, ...)
  • Python version
  • Versions of any other relevant libraries
import torchrl, numpy, sys
print(torchrl.__version__, numpy.__version__, sys.version, sys.platform)

0.3.0 1.26.1 3.10.13 (main, Sep 11 2023, 13:44:35) [GCC 11.2.0] linux

Additional context

Add any other context about the problem here.

Reason and Possible fixes

If you know or suspect the reason for this bug, paste the code lines and suggest modifications.

Checklist

  • [x ] I have checked that there is no similar issue in the repo (required)
  • [x ] I have read the documentation (required)
  • [ x] I have provided a minimal working example to reproduce the bug (required)
@whatdhack whatdhack added the bug Something isn't working label Feb 18, 2024
@vmoens
Copy link
Contributor

vmoens commented Feb 19, 2024

The error comes from the fact that the tensordict should have an empty batch size like the env. Shape and batch size are synonymous but I agree the error is misleading

@whatdhack
Copy link
Author

@vmoens , thanks for the tip. With tdact = TensorDict({"action":torch.tensor([1])}, batch_size=torch.Size([])) , the code snippet did not throw that error. Agree that the "error" message is confusing, it being not even an error. What is the reason for that check to be there ?

@vmoens
Copy link
Contributor

vmoens commented Feb 20, 2024

Because we want to enforce that the leading dims of the tensors match the environment batch size (and therefore comply with the specs).
If your tensordict has a batch size of [1] it means that all its content has a leading dimension of [1] and hence that you're running an environment that has a batch size (even if it's just 1).

Once you start executing envs in parallel, we build circular buffers to pass the data across the workers, using the env specs as indicators of the shape of the buffers. If you have data that is not properly shaped using some conventions it's easy to break things as the data won't fit in the buffers.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants