Skip to content

Commit c23ac5f

Browse files
committed
Split benchmark for unqry pointwise operators
1 parent 8bcd5c9 commit c23ac5f

68 files changed

Lines changed: 1648 additions & 608 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

benchmark/performance_utils.py

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

699699

700+
class UnaryPointwiseBenchmark(Benchmark):
701+
"""
702+
Base class for benchmarking unary 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+
sp_shapes_3d = [(64, 64, 2**i) for i in range(0, 15, 4)]
710+
return special_shapes_2d + sp_shapes_3d
711+
712+
def get_input_iter(self, cur_dtype) -> Generator:
713+
for shape in self.shapes:
714+
inp = generate_tensor_input(shape, cur_dtype, self.device)
715+
yield inp,
716+
717+
def get_tflops(self, op, *args, **kwargs):
718+
shape = list(args[0].shape)
719+
return torch.tensor(shape).prod().item()
720+
721+
722+
class UnaryPointwiseOutBenchmark(UnaryPointwiseBenchmark):
723+
def get_input_iter(self, cur_dtype) -> Generator:
724+
for shape in self.shapes:
725+
inp = generate_tensor_input(shape, cur_dtype, self.device)
726+
out = torch.empty_like(inp)
727+
yield inp, {"out": out}
728+
729+
700730
def generate_tensor_input(shape, dtype, device):
701731
if dtype in FLOAT_DTYPES:
702732
return torch.randn(shape, dtype=dtype, device=device)

benchmark/test_abs.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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.abs
9+
def test_abs():
10+
bench = base.UnaryPointwiseBenchmark(
11+
op_name="abs", torch_op=torch.abs, dtypes=attrs.FLOAT_DTYPES
12+
)
13+
bench.run()
14+
15+
16+
@pytest.mark.abs_
17+
def test_abs_inplace():
18+
bench = base.UnaryPointwiseBenchmark(
19+
op_name="abs_", torch_op=torch.abs_, dtypes=attrs.FLOAT_DTYPES, is_inplace=True
20+
)
21+
bench.run()

benchmark/test_absolute.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
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.absolute
9+
def test_absolute():
10+
bench = base.UnaryPointwiseBenchmark(
11+
op_name="absolute", torch_op=torch.absolute, dtypes=attrs.FLOAT_DTYPES
12+
)
13+
bench.run()

benchmark/test_acos.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
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.acos
9+
def test_acos():
10+
bench = base.UnaryPointwiseBenchmark(
11+
op_name="acos", torch_op=torch.acos, dtypes=attrs.FLOAT_DTYPES
12+
)
13+
bench.run()

benchmark/test_alias_copy.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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.alias_copy
9+
def test_alias_copy():
10+
bench = base.UnaryPointwiseBenchmark(
11+
op_name="alias_copy",
12+
torch_op=torch.ops.aten.alias_copy,
13+
dtypes=attrs.FLOAT_DTYPES,
14+
)
15+
bench.run()
16+
17+
18+
@pytest.mark.alias_copy_out
19+
def test_alias_copy_out():
20+
bench = base.UnaryPointwiseOutBenchmark(
21+
op_name="alias_copy_out",
22+
torch_op=torch.ops.aten.alias_copy,
23+
dtypes=attrs.FLOAT_DTYPES,
24+
)
25+
bench.run()

benchmark/test_angle.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
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.angle
9+
def test_angle():
10+
bench = base.UnaryPointwiseBenchmark(
11+
op_name="angle",
12+
torch_op=torch.angle,
13+
dtypes=attrs.COMPLEX_DTYPES
14+
+ [torch.float32]
15+
+ attrs.INT_DTYPES
16+
+ attrs.BOOL_DTYPES,
17+
)
18+
bench.run()
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import pytest
2+
import torch
3+
4+
import flag_gems
5+
6+
from . import attri_util as attrs
7+
from . import performance_utils as base
8+
9+
10+
class RepetitionPenaltyBenchmark(base.Benchmark):
11+
def __init__(self, op_name, torch_op, dtypes):
12+
super().__init__(op_name, torch_op, dtypes)
13+
self.gems_op = None
14+
15+
def set_shapes(self, shape_file_path=None):
16+
self.shapes = [
17+
(1, 1024),
18+
(1, 4096),
19+
(1, 8192),
20+
(8, 4096),
21+
(16, 4096),
22+
(32, 1024),
23+
(8, 8192),
24+
(64, 32000),
25+
]
26+
27+
def get_input_iter(self, dtype):
28+
for shape in self.shapes:
29+
num_seqs, vocab_size = shape
30+
yield (
31+
torch.randn(shape, dtype=dtype, device=self.device),
32+
torch.randint(0, 2, shape, dtype=torch.bool, device=self.device),
33+
torch.randint(0, 2, shape, dtype=torch.bool, device=self.device),
34+
torch.empty(num_seqs, dtype=dtype, device=self.device).uniform_(
35+
1.0, 2.0
36+
),
37+
)
38+
39+
def set_gems(self, gems_op):
40+
self.gems_op = gems_op
41+
42+
43+
UNSUPPORTED_VENDORS = {
44+
"metax",
45+
"kunlunxin",
46+
"iluvatar",
47+
"mthreads",
48+
"hygon",
49+
"cambricon",
50+
}
51+
52+
53+
@pytest.mark.skipif(base.SkipVersion("vllm", "<0.4"), reason="vLLM <0.4 not supported")
54+
@pytest.mark.skipif(not torch.cuda.is_available(), reason="CUDA required")
55+
@pytest.mark.skipif(
56+
flag_gems.vendor_name in UNSUPPORTED_VENDORS, reason="Vendor not supported"
57+
)
58+
@pytest.mark.apply_repetition_penalties
59+
def test_apply_repetition_penalties():
60+
vllm_ops = pytest.importorskip("vllm._custom_ops")
61+
62+
bench = RepetitionPenaltyBenchmark(
63+
op_name="apply_repetition_penalties",
64+
torch_op=vllm_ops.apply_repetition_penalties,
65+
dtypes=attrs.FLOAT_DTYPES,
66+
)
67+
bench.set_gems(flag_gems.apply_repetition_penalties)
68+
bench.run()

benchmark/test_arcsinh.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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.arcsinh
9+
def test_arcsinh():
10+
bench = base.UnaryPointwiseBenchmark(
11+
op_name="arcsinh", torch_op=torch.arcsinh, dtypes=attrs.FLOAT_DTYPES
12+
)
13+
bench.run()
14+
15+
16+
@pytest.mark.arcsinh_
17+
def test_arcsinh_inplace():
18+
bench = base.UnaryPointwiseBenchmark(
19+
op_name="arcsinh_",
20+
torch_op=lambda a: a.arcsinh_(),
21+
dtypes=attrs.FLOAT_DTYPES,
22+
is_inplace=True,
23+
)
24+
bench.run()
25+
26+
27+
@pytest.mark.arcsinh_out
28+
def test_arcsinh_out():
29+
bench = base.UnaryPointwiseOutBenchmark(
30+
op_name="arcsinh_out",
31+
torch_op=torch.arcsinh,
32+
dtypes=attrs.FLOAT_DTYPES,
33+
)
34+
bench.run()

benchmark/test_arctanh.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.arctanh_
8+
def test_arctanh_inplace():
9+
bench = base.UnaryPointwiseBenchmark(
10+
op_name="arctanh_",
11+
torch_op=lambda a: a.arctanh_(),
12+
dtypes=attrs.FLOAT_DTYPES,
13+
is_inplace=True,
14+
)
15+
bench.run()

benchmark/test_asinh.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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.asinh
9+
def test_asinh():
10+
bench = base.UnaryPointwiseBenchmark(
11+
op_name="asinh", torch_op=torch.asinh, dtypes=attrs.FLOAT_DTYPES
12+
)
13+
bench.run()
14+
15+
16+
@pytest.mark.asinh_
17+
def test_asinh_inplace():
18+
bench = base.UnaryPointwiseBenchmark(
19+
op_name="asinh_",
20+
torch_op=lambda a: a.asinh_(),
21+
dtypes=attrs.FLOAT_DTYPES,
22+
is_inplace=True,
23+
)
24+
bench.run()

0 commit comments

Comments
 (0)