forked from tile-ai/TileOPs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbench_layer_norm.py
More file actions
95 lines (71 loc) · 2.88 KB
/
Copy pathbench_layer_norm.py
File metadata and controls
95 lines (71 loc) · 2.88 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
from typing import Optional
import pytest
import torch
import torch.nn.functional as F
from benchmarks.benchmark_base import BenchmarkBase, BenchmarkReport
from tileops.manifest import load_workloads
from tileops.ops.norm.layer_norm import LayerNormFwdOp
from workloads.layer_norm import LayerNormTest
_OP_NAME = "LayerNormFwdOp"
class LayerNormBenchmark(BenchmarkBase[LayerNormTest]):
_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():
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, False,
id=f"{label}-{dtype_str}"))
return params
@pytest.mark.parametrize("m, n, dtype, tune", _manifest_params())
def test_layer_norm_bench(m: int, n: int, dtype: torch.dtype, tune: bool) -> None:
test = LayerNormTest(m, n, dtype)
inputs = test.gen_inputs()
op = LayerNormFwdOp(normalized_shape=(n,), dtype=dtype, tune=tune)
bm = LayerNormBenchmark(test, op)
result = bm.profile(op, *inputs)
BenchmarkReport.record(op, locals(), result, tag="tileops")
# Baseline uses torch.nn.functional.layer_norm
def baseline_fn(x, weight, bias):
return F.layer_norm(x, (n,), weight=weight, bias=bias, eps=1e-5)
result_bl = bm.profile(baseline_fn, *inputs)
BenchmarkReport.record(op, locals(), result_bl, tag="torch")
@pytest.mark.parametrize(
"m, n, dtype, tune",
[
pytest.param(256, 1024, torch.float16, False, id="musa-smoke-fp16"),
pytest.param(256, 1024, torch.bfloat16, False, id="musa-smoke-bf16"),
],
)
def test_layer_norm_bench_musa_smoke(
m: int,
n: int,
dtype: torch.dtype,
tune: bool,
) -> None:
"""Small benchmark proof cases for MUSA bring-up."""
test = LayerNormTest(m, n, dtype)
inputs = test.gen_inputs()
op = LayerNormFwdOp(M=m, N=n, dtype=dtype, tune=tune)
bm = LayerNormBenchmark(test, op)
result = bm.profile(op, *inputs)
BenchmarkReport.record(op, locals(), result, tag="tileops")
def baseline_fn(x, weight, bias):
return F.layer_norm(x, (n,), weight=weight, bias=bias, eps=1e-5)
result_bl = bm.profile(baseline_fn, *inputs)
BenchmarkReport.record(op, locals(), result_bl, tag="torch")
if __name__ == "__main__":
pytest.main([__file__, "-vvs"])