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

[Benchmark] Add benchmark for compiled ReplayBuffer.extend/sample #2514

Merged
merged 1 commit into from
Oct 25, 2024
Merged
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
60 changes: 60 additions & 0 deletions benchmarks/test_replaybuffer_benchmark.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
LazyMemmapStorage,
LazyTensorStorage,
ListStorage,
ReplayBuffer,
TensorDictPrioritizedReplayBuffer,
TensorDictReplayBuffer,
)
Expand Down Expand Up @@ -172,6 +173,65 @@ def test_rb_populate(benchmark, rb, storage, sampler, size):
)


class create_tensor_rb:
def __init__(self, rb, storage, sampler, size=1_000_000, iters=100):
self.storage = storage
self.rb = rb
self.sampler = sampler
self.size = size
self.iters = iters

def __call__(self):
kwargs = {}
if self.sampler is not None:
kwargs["sampler"] = self.sampler()
if self.storage is not None:
kwargs["storage"] = self.storage(10 * self.size)

rb = self.rb(batch_size=3, **kwargs)
data = torch.randn(self.size, 1)
return ((rb, data, self.iters), {})


def extend_and_sample(rb, td, iters):
for _ in range(iters):
rb.extend(td)
rb.sample()


def extend_and_sample_compiled(rb, td, iters):
@torch.compile
def fn(td):
rb.extend(td)
rb.sample()

for _ in range(iters):
fn(td)


@pytest.mark.parametrize(
"rb,storage,sampler,size,iters,compiled",
[
[ReplayBuffer, LazyTensorStorage, RandomSampler, 1000, 100, True],
[ReplayBuffer, LazyTensorStorage, RandomSampler, 1000, 100, False],
],
)
def test_rb_extend_sample(benchmark, rb, storage, sampler, size, iters, compiled):
benchmark.pedantic(
extend_and_sample_compiled if compiled else extend_and_sample,
setup=create_tensor_rb(
rb=rb,
storage=storage,
sampler=sampler,
size=size,
iters=iters,
),
iterations=1,
warmup_rounds=10,
rounds=50,
)


if __name__ == "__main__":
args, unknown = argparse.ArgumentParser().parse_known_args()
pytest.main([__file__, "--capture", "no", "--exitfirst"] + unknown)
Loading