forked from tile-ai/TileOPs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbench_instance_norm.py
More file actions
103 lines (76 loc) · 3.49 KB
/
Copy pathbench_instance_norm.py
File metadata and controls
103 lines (76 loc) · 3.49 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
96
97
98
99
100
101
102
103
from tileops.utils import get_backend_name
DEVICE = get_backend_name()
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.instance_norm import (
InstanceNormFwdOp,
InstanceNormFwdOpNoAffine,
)
from workloads.instance_norm import InstanceNormTest
_OP_NAME = "InstanceNormFwdOp"
_OP_NAME_NO_AFFINE = "InstanceNormFwdOpNoAffine"
class InstanceNormBenchmark(BenchmarkBase[InstanceNormTest]):
_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 _build_params(workloads):
params = []
for w in workloads:
shape = w["x_shape"]
n, c, spatial = shape[0], shape[1], tuple(shape[2:])
label = w.get("label", f"{n}x{c}x{'x'.join(map(str, spatial))}")
for dtype_str in w["dtypes"]:
dtype = getattr(torch, dtype_str)
params.append(pytest.param(n, c, spatial, dtype, True,
id=f"{label}-{dtype_str}"))
return params
_AFFINE_PARAMS = _build_params(load_workloads(_OP_NAME))
_NO_AFFINE_PARAMS = _build_params(load_workloads(_OP_NAME_NO_AFFINE))
@pytest.mark.parametrize("n, c, spatial, dtype, tune", _AFFINE_PARAMS)
def test_instance_norm_bench(n: int, c: int, spatial: tuple,
dtype: torch.dtype, tune: bool) -> None:
test = InstanceNormTest(n, c, spatial, dtype)
x, weight, bias = test.gen_inputs()
op = InstanceNormFwdOp(N=n, C=c, spatial=spatial, dtype=dtype, tune=tune)
bm = InstanceNormBenchmark(test, op)
result = bm.profile(op, x, weight, bias)
BenchmarkReport.record(op, locals(), result, tag="tileops")
# Baseline: torch.nn.functional.instance_norm
def baseline_fn(x, weight, bias):
return F.instance_norm(x, weight=weight, bias=bias, eps=1e-5)
result_bl = bm.profile(baseline_fn, x, weight, bias)
BenchmarkReport.record(op, locals(), result_bl, tag="torch")
@pytest.mark.parametrize("n, c, spatial, dtype, tune", _NO_AFFINE_PARAMS)
def test_instance_norm_no_affine_bench(n: int, c: int, spatial: tuple,
dtype: torch.dtype, tune: bool) -> None:
test = InstanceNormTest(n, c, spatial, dtype)
x, _, _ = test.gen_inputs()
op = InstanceNormFwdOpNoAffine(
N=n, C=c, spatial=spatial, dtype=dtype, tune=tune,
)
bm = InstanceNormBenchmark(test, op)
# Running stats are required positional args (R16) but ignored on the
# use_input_stats=True path; pass placeholders.
rm = torch.zeros(c, dtype=torch.float32, device=DEVICE)
rv = torch.ones(c, dtype=torch.float32, device=DEVICE)
result = bm.profile(op, x, rm, rv)
BenchmarkReport.record(op, locals(), result, tag="tileops")
def baseline_no_affine(x):
return F.instance_norm(x, weight=None, bias=None, eps=1e-5)
result_bl = bm.profile(baseline_no_affine, x)
BenchmarkReport.record(op, locals(), result_bl, tag="torch")
if __name__ == "__main__":
pytest.main([__file__, "-vvs"])