Skip to content

Commit

Permalink
[BugFix] Extend RB with lazy stack
Browse files Browse the repository at this point in the history
ghstack-source-id: a0be9a2840ab6f090605a3e1d2f47a4f00ac5183
Pull Request resolved: #2453
  • Loading branch information
vmoens committed Sep 25, 2024
1 parent 33e86c5 commit 1aca00e
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 10 deletions.
15 changes: 15 additions & 0 deletions test/test_rb.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
assert_allclose_td,
is_tensor_collection,
is_tensorclass,
LazyStackedTensorDict,
tensorclass,
TensorDict,
TensorDictBase,
Expand Down Expand Up @@ -715,6 +716,20 @@ def test_storage_state_dict(self, storage_in, storage_out, init_out, backend):
s = new_replay_buffer.sample()
assert (s.exclude("index") == 1).all()

@pytest.mark.parametrize("storage_type", [LazyMemmapStorage, LazyTensorStorage])
def test_extend_lazystack(self, storage_type):

rb = ReplayBuffer(
storage=storage_type(6),
batch_size=2,
)
td1 = TensorDict(a=torch.rand(5, 4, 8), batch_size=5)
td2 = TensorDict(a=torch.rand(5, 3, 8), batch_size=5)
ltd = LazyStackedTensorDict(td1, td2, stack_dim=1)
rb.extend(ltd)
rb.sample(3)
assert len(rb) == 5

@pytest.mark.parametrize("device_data", get_default_devices())
@pytest.mark.parametrize("storage_type", [LazyMemmapStorage, LazyTensorStorage])
@pytest.mark.parametrize("data_type", ["tensor", "tc", "td", "pytree"])
Expand Down
23 changes: 13 additions & 10 deletions torchrl/data/replay_buffers/storages.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
from __future__ import annotations

import abc

import logging
import os
import textwrap
import warnings
Expand Down Expand Up @@ -1116,16 +1118,17 @@ def max_size_along_dim0(data_shape):
out = data.clone().to(self.device)
out = out.expand(max_size_along_dim0(data.shape))
out = out.memmap_like(prefix=self.scratch_dir, existsok=self.existsok)
for key, tensor in sorted(
out.items(include_nested=True, leaves_only=True), key=str
):
try:
filesize = os.path.getsize(tensor.filename) / 1024 / 1024
torchrl_logger.debug(
f"\t{key}: {tensor.filename}, {filesize} Mb of storage (size: {tensor.shape})."
)
except (AttributeError, RuntimeError):
pass
if torchrl_logger.getEffectiveLevel() == logging.DEBUG:
for key, tensor in sorted(
out.items(include_nested=True, leaves_only=True), key=str
):
try:
filesize = os.path.getsize(tensor.filename) / 1024 / 1024
torchrl_logger.debug(
f"\t{key}: {tensor.filename}, {filesize} Mb of storage (size: {tensor.shape})."
)
except (AttributeError, RuntimeError):
pass
else:
out = _init_pytree(self.scratch_dir, max_size_along_dim0, data)
self._storage = out
Expand Down

1 comment on commit 1aca00e

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Performance Alert ⚠️

Possible performance regression was detected for benchmark 'CPU Benchmark Results'.
Benchmark result of this commit is worse than the previous benchmark result exceeding threshold 2.

Benchmark suite Current: 1aca00e Previous: 33e86c5 Ratio
benchmarks/test_replaybuffer_benchmark.py::test_rb_sample[TensorDictPrioritizedReplayBuffer-LazyMemmapStorage-None-10000] 663.32410142256 iter/sec (stddev: 0.03129223487250282) 1611.2795060990045 iter/sec (stddev: 0.00004398806299705637) 2.43

This comment was automatically generated by workflow using github-action-benchmark.

CC: @vmoens

Please sign in to comment.