-
Notifications
You must be signed in to change notification settings - Fork 76
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Benchmark] Benchmark tensorclass ops (#790)
- Loading branch information
Showing
6 changed files
with
85 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
5 changes: 5 additions & 0 deletions
5
benchmarks/tensorclass/torch_functions.py → ...marks/tensorclass/test_torch_functions.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters