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

[BugFix] Fix non-full TensorStorage indexing #1730

Merged
merged 2 commits into from
Dec 4, 2023
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
8 changes: 8 additions & 0 deletions test/test_rb.py
Original file line number Diff line number Diff line change
Expand Up @@ -797,6 +797,14 @@ def test_index(self, rbtype, storage, size, prefetch):
b = b.all()
assert b

def test_index_nonfull(self, rbtype, storage, size, prefetch):
# checks that indexing the buffer before it's full gives the accurate view of the data
rb = self._get_rb(rbtype, storage=storage, size=size, prefetch=prefetch)
data = self._get_data(rbtype, size=size - 1)
rb.extend(data)
assert len(rb[: size - 1]) == size - 1
assert len(rb[size - 2 :]) == 1


def test_multi_loops():
"""Tests that one can iterate multiple times over a buffer without rep."""
Expand Down
6 changes: 5 additions & 1 deletion torchrl/data/replay_buffers/storages.py
Original file line number Diff line number Diff line change
Expand Up @@ -448,11 +448,15 @@ def set( # noqa: F811
self._storage[cursor] = data

def get(self, index: Union[int, Sequence[int], slice]) -> Any:
if self._len < self.max_size:
storage = self._storage[: self._len]
else:
storage = self._storage
if not self.initialized:
raise RuntimeError(
"Cannot get an item from an unitialized LazyMemmapStorage"
)
out = self._storage[index]
out = storage[index]
if is_tensor_collection(out):
out = _reset_batch_size(out)
return out.unlock_()
Expand Down
Loading