forked from tile-ai/TileOPs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbench_gemm.py
More file actions
57 lines (40 loc) · 1.96 KB
/
Copy pathbench_gemm.py
File metadata and controls
57 lines (40 loc) · 1.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
from typing import Optional
import pytest
import torch
from benchmarks.benchmark_base import BenchmarkBase, BenchmarkReport
from tileops.ops import GemmOp
from workloads.gemm import GemmTest
class _GemmTestBaseline(GemmTest):
"""Adds baseline ref_program for benchmark profiling."""
def ref_program(self, a: torch.Tensor, b: torch.Tensor) -> torch.Tensor:
if self.trans_a:
a = a.T
if self.trans_b:
b = b.T
return torch.matmul(a, b)
class GemmBenchmark(BenchmarkBase[GemmTest]):
def calculate_flops(self) -> Optional[float]:
return 2.0 * self.workload.m * self.workload.n * self.workload.k
def calculate_memory(self) -> Optional[float]:
t = self.workload
return (t.m * t.k + t.k * t.n + t.m * t.n) * t.dtype.itemsize
_GEMM_BENCH_PARAMS = [
pytest.param(1024, 1024, 1024, torch.float16, False, False, False, id="square-fp16"),
pytest.param(1, 7168, 16384, torch.float16, False, True, True, id="wide-fp16"),
pytest.param(1, 18432, 7168, torch.bfloat16, False, True, True, id="wide-alt-bf16"),
pytest.param(7168, 1, 16384, torch.float16, False, False, True, id="thin-n-fp16"),
pytest.param(18432, 1, 7168, torch.bfloat16, False, False, True, id="thin-n-alt-bf16"),
]
@pytest.mark.parametrize("m, n, k, dtype, trans_a, trans_b, tune", _GEMM_BENCH_PARAMS)
def test_gemm_bench(m: int, n: int, k: int, dtype: torch.dtype, trans_a: bool, trans_b: bool,
tune: bool) -> None:
test = _GemmTestBaseline(m, n, k, dtype, trans_a, trans_b)
bm = GemmBenchmark(test)
inputs = test.gen_inputs()
op = GemmOp(m, n, k, trans_a=trans_a, trans_b=trans_b, dtype=dtype, tune=tune)
result = bm.profile(op, *inputs)
BenchmarkReport.record(op, locals(), result, tag="tileops")
result_bl = bm.profile(test.ref_program, *inputs)
BenchmarkReport.record(op, locals(), result_bl, tag="torch-cublas")
if __name__ == "__main__":
pytest.main([__file__, "-vvs"])