Skip to content

Commit e67204d

Browse files
Merge branch 'master' into competition/conv-transpose2d
2 parents 4db142c + 2b083bd commit e67204d

13 files changed

Lines changed: 620 additions & 580 deletions

benchmark/performance_utils.py

Lines changed: 58 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
BenchMode,
2626
OperationAttribute,
2727
check_metric_dependencies,
28+
model_shapes,
2829
)
2930
from .conftest import Config, emit_record_logger
3031

@@ -492,9 +493,9 @@ def set_more_shapes(self):
492493
more_shapes_3d = [(100, 2**i, 100) for i in (0, 8, 16)]
493494
return more_shapes_1d + more_shapes_2d + more_shapes_3d
494495

495-
def get_input_iter(self, cur_dtype) -> Generator:
496+
def get_input_iter(self, dtype) -> Generator:
496497
for shape in self.shapes:
497-
yield from self.input_fn(shape, cur_dtype, self.device)
498+
yield from self.input_fn(shape, dtype, self.device)
498499

499500

500501
class GenericBenchmarkFilterShapes(GenericBenchmark):
@@ -641,6 +642,61 @@ def get_tflops(self, op, *args, **kwargs):
641642
return torch.tensor(inp_shape).prod().item() * 2
642643

643644

645+
class BlasBenchmark(Benchmark):
646+
"""
647+
benchmark for blas
648+
"""
649+
650+
DEFAULT_METRICS = DEFAULT_METRICS[:] + ["tflops"]
651+
652+
def __init__(self, *args, input_fn, **kwargs):
653+
super().__init__(*args, **kwargs)
654+
self.input_fn = input_fn
655+
656+
def get_input_iter(self, dtype) -> Generator:
657+
for b, m, n, k in self.shapes:
658+
yield from self.input_fn(b, m, n, k, dtype, self.device, False)
659+
660+
if Config.bench_level == BenchLevel.COMPREHENSIVE:
661+
for b, m, n, k in self.shapes:
662+
yield from self.input_fn(b, m, n, k, dtype, self.device, True)
663+
664+
def set_more_shapes(self):
665+
large_k_shapes = [
666+
[8, 1848, 1536, 151936],
667+
[8, 1848, 1536, 128256],
668+
[8, 1848, 1536, 152064],
669+
[8, 4096, 1, 152064],
670+
]
671+
672+
model_shaps = model_shapes()
673+
return large_k_shapes + model_shaps
674+
675+
def get_tflops(self, op, *args, **kwargs):
676+
total_flops = 0
677+
# shape(m,k)(k,n)
678+
# total_flops mxnx2k
679+
if self.op_name == "mm":
680+
total_flops = args[0].shape[0] * args[0].shape[1] * args[1].shape[1] * 2
681+
682+
# shape(m,n)(n,p)
683+
# total_flops mxpx(2n+1)
684+
elif self.op_name == "addmm":
685+
total_flops = (
686+
args[0].shape[0] * args[1].shape[1] * (args[1].shape[0] * 2 + 1)
687+
)
688+
# total_flops bxnxpx2m
689+
elif self.op_name == "bmm":
690+
total_flops = (
691+
args[0].shape[0]
692+
* args[0].shape[1]
693+
* args[1].shape[2]
694+
* 2
695+
* args[0].shape[2]
696+
)
697+
return total_flops
698+
699+
644700
def generate_tensor_input(shape, dtype, device):
645701
if dtype in FLOAT_DTYPES:
646702
return torch.randn(shape, dtype=dtype, device=device)

benchmark/test_addmm.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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+
def _input_fn(b, m, n, k, dtype, device, b_column_major):
11+
inp1 = torch.randn([m, k], dtype=dtype, device=device)
12+
bias = torch.randn([m, n], dtype=dtype, device=device)
13+
if b_column_major:
14+
inp2 = torch.randn([n, k], dtype=dtype, device=device)
15+
yield bias, inp1, inp2.t(),
16+
else:
17+
inp2 = torch.randn([k, n], dtype=dtype, device=device)
18+
yield bias, inp1, inp2,
19+
20+
21+
@pytest.mark.addmm
22+
def test_addmm(monkeypatch):
23+
if flag_gems.vendor_name == "mthreads":
24+
monkeypatch.setenv("MUSA_ENABLE_SQMMA", "1")
25+
26+
bench = base.BlasBenchmark(
27+
op_name="addmm",
28+
input_fn=_input_fn,
29+
torch_op=torch.addmm,
30+
dtypes=attrs.FLOAT_DTYPES,
31+
)
32+
33+
bench.run()

benchmark/test_addmv.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
from typing import Generator
2+
3+
import pytest
4+
import torch
5+
6+
from . import performance_utils as base
7+
from .attri_util import FLOAT_DTYPES
8+
9+
10+
class AddmvBenchmark(base.GenericBenchmark2DOnly):
11+
def set_more_shapes(self):
12+
return []
13+
14+
def get_input_iter(self, dtype) -> Generator:
15+
for m, n in self.shapes:
16+
yield from self.input_fn(m, n, dtype, self.device)
17+
18+
19+
def _input_fn(m, n, cur_dtype, device):
20+
mat = torch.randn([m, n], dtype=cur_dtype, device=device)
21+
vec = torch.randn([n], dtype=cur_dtype, device=device)
22+
bias = torch.randn([m], dtype=cur_dtype, device=device)
23+
24+
# torch.addmv(bias, mat, vec)
25+
yield bias, mat, vec
26+
27+
28+
@pytest.mark.addmv
29+
def test_addmv():
30+
bench = AddmvBenchmark(
31+
op_name="addmv",
32+
input_fn=_input_fn,
33+
torch_op=torch.addmv,
34+
dtypes=FLOAT_DTYPES,
35+
)
36+
bench.run()

benchmark/test_addr.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
from typing import Generator
2+
3+
import pytest
4+
import torch
5+
6+
from benchmark.attri_util import FLOAT_DTYPES
7+
8+
from . import performance_utils as base
9+
10+
11+
class AddrBenchmark(base.BlasBenchmark):
12+
def set_more_shapes(self):
13+
return []
14+
15+
def get_input_iter(self, dtype) -> Generator:
16+
for shape in self.shapes:
17+
m, n = shape[0], shape[1]
18+
yield from self.input_fn(m, n, dtype, self.device)
19+
20+
21+
def _input_fn(m, n, cur_dtype, device):
22+
inp1 = torch.randn([m, n], dtype=cur_dtype, device=device)
23+
inp2 = torch.randn([m], dtype=cur_dtype, device=device)
24+
inp3 = torch.randn([n], dtype=cur_dtype, device=device)
25+
yield inp1, inp2, inp3, {"alpha": 0.5, "beta": 0.5}
26+
27+
28+
@pytest.mark.addr
29+
def test_addr():
30+
bench = AddrBenchmark(
31+
op_name="addr",
32+
input_fn=_input_fn,
33+
torch_op=torch.Tensor.addr,
34+
dtypes=FLOAT_DTYPES,
35+
)
36+
bench.run()

benchmark/test_baddbmm.py

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import pytest
2+
import torch
3+
4+
from . import attri_util as attrs
5+
from . import performance_utils as base
6+
7+
8+
class BaddbmmBenchmark(base.BlasBenchmark):
9+
def set_more_shapes(self):
10+
model_shapes_list = attrs.model_shapes()
11+
12+
skip_shapes = [
13+
(4, 8192, 128256, 4096),
14+
(4, 8192, 152064, 3584),
15+
]
16+
17+
filtered = []
18+
for shape in model_shapes_list:
19+
if shape not in skip_shapes:
20+
filtered.append(shape)
21+
22+
return filtered
23+
24+
def get_tflops(self, op, *args, **kwargs):
25+
# shape(b,m,k)(b,k,n)
26+
# total_flops = b * m * n * (2 * k + 1)
27+
total_flops = (
28+
args[1].shape[0]
29+
* args[1].shape[1]
30+
* args[2].shape[2]
31+
* (args[1].shape[2] * 2 + 1)
32+
)
33+
return total_flops
34+
35+
36+
def _input_fn(b, m, n, k, dtype, device, b_column_major):
37+
inp1 = torch.randn([b, m, k], dtype=dtype, device=device, requires_grad=True)
38+
39+
if b_column_major:
40+
inp2 = torch.randn([b, n, k], dtype=dtype, device=device, requires_grad=True)
41+
inp2 = inp2.transpose(1, 2).contiguous()
42+
else:
43+
inp2 = torch.randn([b, k, n], dtype=dtype, device=device, requires_grad=True)
44+
45+
bias = torch.randn([b, m, n], dtype=dtype, device=device, requires_grad=True)
46+
47+
yield bias, inp1, inp2
48+
49+
50+
@pytest.mark.baddbmm
51+
def test_baddbmm():
52+
bench = BaddbmmBenchmark(
53+
op_name="baddbmm",
54+
input_fn=_input_fn,
55+
torch_op=torch.baddbmm,
56+
dtypes=attrs.FLOAT_DTYPES,
57+
)
58+
59+
bench.run()

0 commit comments

Comments
 (0)