Skip to content

Commit

Permalink
[Benchmark] Benchmark tensorclass ops (#790)
Browse files Browse the repository at this point in the history
  • Loading branch information
vmoens authored May 24, 2024
1 parent 9cfd428 commit d652d02
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 0 deletions.
5 changes: 5 additions & 0 deletions benchmarks/common/common_ops_test.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
# Copyright (c) Meta Platforms, Inc. and affiliates.
#
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.

import argparse

import pytest
Expand Down
5 changes: 5 additions & 0 deletions benchmarks/common/memmap_benchmarks_test.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
# Copyright (c) Meta Platforms, Inc. and affiliates.
#
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.

import argparse
import pathlib
import time
Expand Down
5 changes: 5 additions & 0 deletions benchmarks/common/pytree_benchmarks_test.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
# Copyright (c) Meta Platforms, Inc. and affiliates.
#
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.

import pytest
import torch

Expand Down
6 changes: 6 additions & 0 deletions benchmarks/nn/functional_benchmarks_test.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
# Copyright (c) Meta Platforms, Inc. and affiliates.
#
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.


# we use deepcopy as our implementation modifies the modules in-place
import argparse
from copy import deepcopy
Expand Down
59 changes: 59 additions & 0 deletions benchmarks/tensorclass/test_tensorclass_speed.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
# Copyright (c) Meta Platforms, Inc. and affiliates.
#
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.


import argparse

import pytest
import torch

from tensordict.prototype import tensorclass


@tensorclass
class MyData:
a: torch.Tensor
b: torch.Tensor
c: str
d: "MyData" = None


def test_tc_init(benchmark):
z = torch.zeros(())
o = torch.ones(())
benchmark(lambda: MyData(a=z, b=o, c="a string", d=None))


def test_tc_init_nested(benchmark):
z = torch.zeros(())
o = torch.ones(())
benchmark(
lambda: MyData(a=z, b=o, c="a string", d=MyData(a=z, b=o, c="a string", d=None))
)


def test_tc_first_layer_tensor(benchmark):
d = MyData(a=0, b=1, c="a string", d=MyData(None, None, None))
benchmark(lambda: d.a)


def test_tc_first_layer_nontensor(benchmark):
d = MyData(a=0, b=1, c="a string", d=MyData(None, None, None))
benchmark(lambda: d.c)


def test_tc_second_layer_tensor(benchmark):
d = MyData(a=0, b=1, c="a string", d=MyData(torch.zeros(()), None, None))
benchmark(lambda: d.d.a)


def test_tc_second_layer_nontensor(benchmark):
d = MyData(a=0, b=1, c="a string", d=MyData(torch.zeros(()), None, "a string"))
benchmark(lambda: d.d.c)


if __name__ == "__main__":
args, unknown = argparse.ArgumentParser().parse_known_args()
pytest.main([__file__, "--capture", "no", "--exitfirst"] + unknown)
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
# Copyright (c) Meta Platforms, Inc. and affiliates.
#
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.

import pytest
import torch

Expand Down

0 comments on commit d652d02

Please sign in to comment.