Skip to content

Commit 5b9cb7f

Browse files
authored
Split benchmark for binary pointwise operators (#2691)
1 parent 994bed0 commit 5b9cb7f

33 files changed

Lines changed: 635 additions & 150 deletions

benchmark/performance_utils.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -697,6 +697,53 @@ def get_tflops(self, op, *args, **kwargs):
697697
return total_flops
698698

699699

700+
class BinaryPointwiseBenchmark(Benchmark):
701+
"""
702+
Base class for benchmarking binary pointwise operations.
703+
"""
704+
705+
DEFAULT_METRICS = DEFAULT_METRICS[:] + ["tflops"]
706+
707+
def set_more_shapes(self):
708+
special_shapes_2d = [[1024, 2**i] for i in range(0, 20, 4)]
709+
shapes_3d = [[64, 64, 2**i] for i in range(0, 20, 4)]
710+
return special_shapes_2d + shapes_3d
711+
712+
def get_input_iter(self, dtype) -> Generator:
713+
for shape in self.shapes:
714+
inp1 = generate_tensor_input(shape, dtype, self.device)
715+
inp2 = generate_tensor_input(shape, dtype, self.device)
716+
yield inp1, inp2
717+
718+
def get_tflops(self, op, *args, **kwargs):
719+
shape1 = list(args[0].shape)
720+
shape2 = list(args[0].shape)
721+
return torch.tensor(shape1).prod().item() + torch.tensor(shape2).prod().item()
722+
723+
724+
class ScalarBinaryPointwiseBenchmark(Benchmark):
725+
"""
726+
Base class for benchmarking binary pointwise operations with scalar input.
727+
"""
728+
729+
DEFAULT_METRICS = DEFAULT_METRICS[:] + ["tflops"]
730+
731+
def set_more_shapes(self):
732+
special_shapes_2d = [[1024, 2**i] for i in range(0, 20, 4)]
733+
shapes_3d = [[64, 64, 2**i] for i in range(0, 20, 4)]
734+
return special_shapes_2d + shapes_3d
735+
736+
def get_input_iter(self, cur_dtype) -> Generator:
737+
for shape in self.shapes:
738+
inp1 = 0.001 # Scalar input
739+
inp2 = generate_tensor_input(shape, cur_dtype, self.device)
740+
yield inp1, inp2
741+
742+
def get_tflops(self, op, *args, **kwargs):
743+
shape = list(args[1].shape) # Second argument is the tensor
744+
return torch.tensor(shape).prod().item()
745+
746+
700747
def generate_tensor_input(shape, dtype, device):
701748
if dtype in FLOAT_DTYPES:
702749
return torch.randn(shape, dtype=dtype, device=device)

benchmark/test_add.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import pytest
2+
import torch
3+
4+
from . import attri_util as attrs
5+
from . import performance_utils as base
6+
7+
8+
@pytest.mark.add
9+
def test_add():
10+
bench = base.BinaryPointwiseBenchmark(
11+
op_name="add",
12+
torch_op=torch.add,
13+
dtypes=attrs.FLOAT_DTYPES + attrs.COMPLEX_DTYPES,
14+
)
15+
bench.run()
16+
17+
18+
@pytest.mark.add_
19+
def test_add_inplace():
20+
bench = base.BinaryPointwiseBenchmark(
21+
op_name="add_",
22+
torch_op=lambda a, b: a.add_(b),
23+
dtypes=attrs.FLOAT_DTYPES,
24+
is_inplace=True,
25+
)
26+
bench.run()

benchmark/test_allclose.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import pytest
2+
import torch
3+
4+
from . import attri_util as attrs
5+
from . import performance_utils as base
6+
7+
8+
@pytest.mark.allclose
9+
def test_allclose():
10+
bench = base.BinaryPointwiseBenchmark(
11+
op_name="allclose",
12+
torch_op=torch.allclose,
13+
dtypes=attrs.FLOAT_DTYPES + attrs.INT_DTYPES,
14+
)
15+
bench.run()

benchmark/test_binary_pointwise_perf.py

Lines changed: 0 additions & 150 deletions
This file was deleted.

benchmark/test_bitwise_and.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import pytest
2+
import torch
3+
4+
from . import attri_util as attrs
5+
from . import performance_utils as base
6+
7+
8+
@pytest.mark.bitwise_and
9+
def test_bitwise_and():
10+
bench = base.BinaryPointwiseBenchmark(
11+
op_name="bitwise_and",
12+
torch_op=torch.bitwise_and,
13+
dtypes=attrs.INT_DTYPES + attrs.BOOL_DTYPES,
14+
)
15+
bench.run()
16+
17+
18+
@pytest.mark.bitwise_and_
19+
def test_bitwise_and_inplace():
20+
bench = base.BinaryPointwiseBenchmark(
21+
op_name="bitwise_and_",
22+
torch_op=lambda a, b: a.bitwise_and_(b),
23+
dtypes=attrs.INT_DTYPES + attrs.BOOL_DTYPES,
24+
is_inplace=True,
25+
)
26+
bench.run()

benchmark/test_bitwise_or.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import pytest
2+
import torch
3+
4+
from . import attri_util as attrs
5+
from . import performance_utils as base
6+
7+
8+
@pytest.mark.bitwise_or
9+
def test_bitwise_or():
10+
bench = base.BinaryPointwiseBenchmark(
11+
op_name="bitwise_or",
12+
torch_op=torch.bitwise_or,
13+
dtypes=attrs.INT_DTYPES + attrs.BOOL_DTYPES,
14+
)
15+
bench.run()
16+
17+
18+
@pytest.mark.bitwise_or_
19+
def test_bitwise_or_inplace():
20+
bench = base.BinaryPointwiseBenchmark(
21+
op_name="bitwise_or_",
22+
torch_op=lambda a, b: a.bitwise_or_(b),
23+
dtypes=attrs.INT_DTYPES + attrs.BOOL_DTYPES,
24+
is_inplace=True,
25+
)
26+
bench.run()

benchmark/test_div.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import pytest
2+
import torch
3+
4+
from . import attri_util as attrs
5+
from . import performance_utils as base
6+
7+
8+
# TODO(0x45f): Fix OOM when dtypes includes COMPLEX_DTYPES is included (Issue #2693).
9+
@pytest.mark.div
10+
def test_div():
11+
bench = base.BinaryPointwiseBenchmark(
12+
op_name="div",
13+
torch_op=torch.div,
14+
dtypes=attrs.FLOAT_DTYPES,
15+
)
16+
bench.run()
17+
18+
19+
@pytest.mark.div_
20+
def test_div_inplace():
21+
bench = base.BinaryPointwiseBenchmark(
22+
op_name="div_",
23+
torch_op=lambda a, b: a.div_(b),
24+
dtypes=attrs.FLOAT_DTYPES,
25+
is_inplace=True,
26+
)
27+
bench.run()

benchmark/test_dunder_ior.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import pytest
2+
3+
from . import attri_util as attrs
4+
from . import performance_utils as base
5+
6+
7+
@pytest.mark.dunder_ior
8+
def test_dunder_ior_inplace():
9+
bench = base.BinaryPointwiseBenchmark(
10+
op_name="dunder_ior",
11+
torch_op=lambda a, b: a.__ior__(b),
12+
dtypes=attrs.INT_DTYPES + attrs.BOOL_DTYPES,
13+
is_inplace=True,
14+
)
15+
bench.run()

benchmark/test_dunder_or.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import pytest
2+
3+
from . import attri_util as attrs
4+
from . import performance_utils as base
5+
6+
7+
@pytest.mark.dunder_or
8+
def test_dunder_or():
9+
bench = base.BinaryPointwiseBenchmark(
10+
op_name="dunder_or",
11+
torch_op=lambda a, b: a | b,
12+
dtypes=attrs.INT_DTYPES + attrs.BOOL_DTYPES,
13+
)
14+
bench.run()

benchmark/test_eq.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import pytest
2+
import torch
3+
4+
from . import attri_util as attrs
5+
from . import performance_utils as base
6+
7+
8+
@pytest.mark.eq
9+
def test_eq():
10+
bench = base.BinaryPointwiseBenchmark(
11+
op_name="eq",
12+
torch_op=torch.eq,
13+
dtypes=attrs.FLOAT_DTYPES,
14+
)
15+
bench.run()

0 commit comments

Comments
 (0)