Closed
Description
In an env when the episode terminates early, I would like to still save in the buffer and fixed size episodes from it. This currently fails for me giving the error
RuntimeError: indexed destination TensorDict batch size is torch.Size([1001]) (batch_size = torch.Size([1000, 1001]), index=5), which differs from the source batch size torch.Size([40])
To Reproduce
The actual trace for my env is below
Traceback (most recent call last):
File "/env/lib/python3.11/site-packages/tensordict/tensordict.py", line 3754, in __setitem__
value = value.expand(indexed_bs)
^^^^^^^^^^^^^^^^^^^^^^^^
File "/env/lib/python3.11/site-packages/tensordict/tensordict.py", line 4430, in expand
raise RuntimeError(
RuntimeError: Incompatible expanded shape: The expanded shape length at non-singleton dimension should be same as the original length. target_shape = (1001,), existing_shape = torch.Size([40])
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "train.py", line 56, in train
trainer.train()
File "trainer/online_trainer.py", line 93, in train
self._ep_idx = self.buffer.add(torch.cat(self._tds))
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "buffer.py", line 99, in add
self._buffer.add(tds)
File "/env/lib/python3.11/site-packages/torchrl/data/replay_buffers/replay_buffers.py", line 255, in add
return self._add(data)
^^^^^^^^^^^^^^^
File "/env/lib/python3.11/site-packages/torchrl/data/replay_buffers/replay_buffers.py", line 259, in _add
index = self._writer.add(data)
^^^^^^^^^^^^^^^^^^^^^^
File "/env/lib/python3.11/site-packages/torchrl/data/replay_buffers/writers.py", line 55, in add
self._storage[self._cursor] = data
~~~~~~~~~~~~~^^^^^^^^^^^^^^
File "/zfs/aditya/workspace/tdmpc2/env/lib/python3.11/site-packages/torchrl/data/replay_buffers/storages.py", line 72, in __setitem__
ret = self.set(index, value)
^^^^^^^^^^^^^^^^^^^^^^
File "/env/lib/python3.11/site-packages/torchrl/data/replay_buffers/storages.py", line 323, in set
self._storage[cursor] = data
~~~~~~~~~~~~~^^^^^^^^
File "/env/lib/python3.11/site-packages/tensordict/tensordict.py", line 3756, in __setitem__
raise RuntimeError(
RuntimeError: indexed destination TensorDict batch size is torch.Size([1001]) (batch_size = torch.Size([1000, 1001]), index=5), which differs from the source batch size torch.Size([40])
I distilled down the whole thing to the following script
import torchrl
from torchrl.data.replay_buffers.storages import TensorStorage
data = TensorDict({
"some data": torch.randn(1, 10, 11),
("some", "nested", "data"): torch.randn(1, 10, 11, 12),
}, batch_size=[1, 10, 11])
store = TensorStorage(data)
data = TensorDict({
"some data": torch.randn(1, 11, 11),
("some", "nested", "data"): torch.randn(1, 11, 11, 12),
}, batch_size=[1, 10, 11])
store[1] = data
Expected behavior
The tensordict should be saved? Ultimately what I really want is to have a buffer that can save variable amount of steps in different episodes.
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.2.1 1.23.5 3.11.5 (main, Sep 2 2023, 14:16:33) [GCC 13.2.1 20230801] linux