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] Memmap tensordict benchmarks #432

Merged
merged 1 commit into from
Jun 19, 2023
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
43 changes: 42 additions & 1 deletion benchmarks/common/memmap_benchmarks_test.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import argparse

import pytest
import torch

from tensordict import MemmapTensor
from tensordict import MemmapTensor, TensorDict


def get_available_devices():
Expand All @@ -23,6 +25,13 @@ def memmap_tensor(request):
return MemmapTensor(3, 4, 5, device=request.param)


@pytest.fixture
def td_memmap():
return TensorDict(
{str(i): torch.zeros(3, 40) + i for i in range(30)}, [3, 40]
).memmap_()


@pytest.mark.parametrize("device", get_available_devices())
def test_creation(benchmark, device):
benchmark.pedantic(
Expand All @@ -48,3 +57,35 @@ def test_stack(benchmark, memmap_tensor):
benchmark.pedantic(
torch.stack, args=([memmap_tensor] * 2, 0), rounds=10, iterations=1
)


def test_memmaptd_index(benchmark, td_memmap):
benchmark.pedantic(
lambda td: td[0],
args=(td_memmap,),
iterations=1,
rounds=1000,
)


def test_memmaptd_index_astensor(benchmark, td_memmap):
benchmark.pedantic(
lambda td: td[0].as_tensor(),
args=(td_memmap,),
iterations=1,
rounds=1000,
)


def test_memmaptd_index_op(benchmark, td_memmap):
benchmark.pedantic(
lambda td: td[0].apply(lambda x: x + 1),
args=(td_memmap,),
iterations=1,
rounds=1000,
)


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