-
Notifications
You must be signed in to change notification settings - Fork 47
Expand file tree
/
Copy pathbench_rms_norm.py
More file actions
72 lines (50 loc) · 2.24 KB
/
Copy pathbench_rms_norm.py
File metadata and controls
72 lines (50 loc) · 2.24 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
from typing import Optional
import pytest
import torch
from benchmarks.benchmark_base import BenchmarkBase, BenchmarkReport
from tileops.manifest import load_workloads
from tileops.ops.norm.rms_norm import RMSNormFwdOp
from workloads.rms_norm import RMSNormTest
class _RMSNormTestBaseline(RMSNormTest):
"""Adds baseline ref_program for benchmark profiling."""
def ref_program(self, x: torch.Tensor, weight: torch.Tensor) -> torch.Tensor:
x_f32 = x.float()
rms = torch.sqrt(x_f32.pow(2).mean(dim=-1, keepdim=True) + self.eps)
return ((x_f32 / rms) * weight.float()).to(x.dtype)
_OP_NAME = "RMSNormFwdOp"
class RMSNormBenchmark(BenchmarkBase[RMSNormTest]):
_roofline_cache: Optional[tuple[float, float]] = None
def __init__(self, test, op):
super().__init__(test)
self._op = op
def _get_roofline(self) -> tuple[float, float]:
if self._roofline_cache is None:
self._roofline_cache = self._op.eval_roofline()
return self._roofline_cache
def calculate_flops(self) -> Optional[float]:
return self._get_roofline()[0]
def calculate_memory(self) -> Optional[float]:
return self._get_roofline()[1]
def _manifest_params():
"""Convert manifest workloads to pytest params: (m, n, dtype, tune)."""
params = []
for w in load_workloads(_OP_NAME):
m, n = w["x_shape"]
label = w.get("label", f"{m}x{n}")
for dtype_str in w["dtypes"]:
dtype = getattr(torch, dtype_str)
params.append(pytest.param(m, n, dtype, True,
id=f"{label}-{dtype_str}"))
return params
@pytest.mark.parametrize("m, n, dtype, tune", _manifest_params())
def test_rms_norm_bench(m: int, n: int, dtype: torch.dtype, tune: bool) -> None:
test = _RMSNormTestBaseline(m, n, dtype)
inputs = test.gen_inputs()
op = RMSNormFwdOp(normalized_shape=(n,), dtype=dtype, tune=tune)
bm = RMSNormBenchmark(test, op)
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-ref")
if __name__ == "__main__":
pytest.main([__file__, "-vvs"])