[BUG] RewardSum not working with ParallelEnv
Describe the bug
When using the RewardSum transform within a ParallelEnv, cumulative reward is not computed; instead, the ('next', 'episore_reward') key contains the instantaneous reward.
To Reproduce
Script to reproduce observed behaviour:
from torchrl.envs.libs.gym import GymEnv
from torchrl.envs import TransformedEnv, ParallelEnv
from torchrl.envs.transforms import RewardSum
def make_env():
e = GymEnv("CartPole-v1")
e = TransformedEnv(e, RewardSum())
return e
if __name__ == '__main__':
e = make_env()
print(e.rollout(100)['next', 'episode_reward'])
pe = ParallelEnv(2, make_env)
print(pe.rollout(100)['next', 'episode_reward'])
tensor([[ 1.],
[ 2.],
[ 3.],
[ 4.],
[ 5.],
[ 6.],
[ 7.],
[ 8.],
[ 9.],
[10.],
[11.],
[12.],
[13.],
[14.],
[15.],
[16.],
[17.],
[18.],
[19.],
[20.],
[21.],
[22.],
[23.],
[24.],
[25.],
[26.]])
tensor([[[1.],
[1.],
[1.],
[1.],
[1.],
[1.],
[1.],
[1.],
[1.],
[1.],
[1.],
[1.],
[1.],
[1.]],
[[1.],
[1.],
[1.],
[1.],
[1.],
[1.],
[1.],
[1.],
[1.],
[1.],
[1.],
[1.],
[1.],
[1.]]])
Expected behavior
The ('next', 'episore_reward') key in the rollout tensordict returned by ParallelEnv should contain the cumulative reward.
System info
- Python 3.10.12
- torch 2.0.1
import torchrl, numpy, sys
print(torchrl.__version__, numpy.__version__, sys.version, sys.platform)
0.1.1 1.25.1 3.10.12 (main, Jul 5 2023, 18:54:27) [GCC 11.2.0] linux
Additional context
The issue seems to not affect the SerialEnv class.
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)
Can you have a look at #1454?
For some reason I am unable to run the code.
When cloning the fix branch and trying to execute the code with the updated transform, I get ImportError: cannot import name 'unravel_key' from 'tensordict'.
Also, I tried to subclass RewardSum (from current torchrl pip release) and adding only the new method implemented in #1454, but I get KeyError: '_state_spec'.
You need the latest version of tensordict too. You can install it via a clone of the GitHub repo or tensordict-nighlty on pypi.
You need the latest version of tensordict too. You can install it via a clone of the GitHub repo or tensordict-nighlty on pypi.
Hello! I had the same issue about the unravel_key from tensordict and followed your instructions to install tensordict using the GitHub repo. I am now getting a new error:
from tensordict import PYTREE_REGISTERED_TDS, TensorDict
ImportError: cannot import name 'PYTREE_REGISTERED_TDS' from 'tensordict' (unknown location)
Would anyone happen to know what's wrong? For context, I am running on an M1 Macbook
You need the latest version of tensordict too. You can install it via a clone of the GitHub repo or tensordict-nighlty on pypi.
Hello! I had the same issue about the
unravel_keyfrom tensordict and followed your instructions to install tensordict using the GitHub repo. I am now getting a new error:from tensordict import PYTREE_REGISTERED_TDS, TensorDict ImportError: cannot import name 'PYTREE_REGISTERED_TDS' from 'tensordict' (unknown location)Would anyone happen to know what's wrong? For context, I am running on an M1 Macbook
You could try to make sure you don't clone the repo to a directory named 'tensordict'. It confuses pythons module lookup.
You need the latest version of tensordict too. You can install it via a clone of the GitHub repo or tensordict-nighlty on pypi.
Okay, I confirm that with the latest tensordict version the bug is gone.
Would anyone happen to know what's wrong? For context, I am running on an M1 Macbook.
I had a similar import error when installing tensordict-nightly via pip, but installing with pip install git+https://github.com/pytorch-labs/tensordict.git solved the issue.
Could be that you did not uninstall the other tensordict. One is named tensordict-nightly and the other tensordict and I think importlib gets the latter by default.