forked from tile-ai/TileOPs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbench_batch_norm.py
More file actions
185 lines (136 loc) · 6.7 KB
/
Copy pathbench_batch_norm.py
File metadata and controls
185 lines (136 loc) · 6.7 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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
from tileops.utils import get_backend_name
DEVICE = get_backend_name()
"""Benchmark for BatchNormFwdOp and BatchNormBwdOp.
Compares TileOPs vs PyTorch cuDNN batch norm on common ResNet-style shapes.
"""
import math
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.batch_norm import BatchNormBwdOp, BatchNormFwdOp
from workloads.batch_norm import BatchNormBwdTest, BatchNormFwdTest
_FWD_OP_NAME = "BatchNormFwdOp"
_BWD_OP_NAME = "BatchNormBwdOp"
# ---------------------------------------------------------------------------
# Benchmark classes
# ---------------------------------------------------------------------------
class BatchNormFwdBenchmark(BenchmarkBase[BatchNormFwdTest]):
_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]
class BatchNormBwdBenchmark(BenchmarkBase[BatchNormBwdTest]):
_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]
# ---------------------------------------------------------------------------
# Benchmark helpers
# ---------------------------------------------------------------------------
def _make_inputs(N, C, spatial, dtype, device=DEVICE):
shape = (N, C, *spatial)
x = torch.randn(*shape, device=device, dtype=dtype)
weight = torch.randn(C, device=device, dtype=torch.float32)
bias = torch.randn(C, device=device, dtype=torch.float32)
running_mean = torch.zeros(C, device=device, dtype=torch.float32)
running_var = torch.ones(C, device=device, dtype=torch.float32)
return x, weight, bias, running_mean, running_var
def _make_bwd_inputs(N, C, spatial, dtype, device=DEVICE):
x, weight, bias, running_mean, running_var = _make_inputs(N, C, spatial, dtype, device)
grad_out = torch.randn_like(x)
L = N * math.prod(spatial) if spatial else N
x_cl = x.float().permute(1, 0, *range(2, x.ndim)).reshape(C, L).contiguous()
mean = x_cl.mean(dim=1)
var = x_cl.var(dim=1, unbiased=False)
rstd = 1.0 / torch.sqrt(var + 1e-5)
return grad_out, x, weight, mean, rstd
def _torch_bn_fwd(x, weight, bias, running_mean, running_var):
return torch.nn.functional.batch_norm(
x.float(), running_mean.clone(), running_var.clone(),
weight.float(), bias.float(), training=True)
def _torch_bn_bwd(grad_out, x, weight, mean, rstd):
"""PyTorch reference backward via autograd."""
with torch.enable_grad():
x32 = x.float().requires_grad_(True)
w32 = weight.float().requires_grad_(True)
b32 = torch.zeros(x.shape[1], device=x.device, dtype=torch.float32, requires_grad=True)
rm = torch.zeros(x.shape[1], device=x.device, dtype=torch.float32)
rv = torch.ones(x.shape[1], device=x.device, dtype=torch.float32)
y = torch.nn.functional.batch_norm(
x32, rm, rv, w32, b32, training=True, eps=1e-5)
y.backward(grad_out.float())
return x32.grad, w32.grad, b32.grad
# ---------------------------------------------------------------------------
# Manifest-driven params
# ---------------------------------------------------------------------------
def _manifest_fwd_params():
params = []
for w in load_workloads(_FWD_OP_NAME):
shape = w["x_shape"]
N, C, spatial = shape[0], shape[1], tuple(shape[2:])
label = w.get("label", f"{N}x{C}")
for dtype_str in w["dtypes"]:
dtype = getattr(torch, dtype_str)
params.append(pytest.param(N, C, spatial, dtype, True, False,
id=f"{label}-{dtype_str}"))
return params
def _manifest_bwd_params():
params = []
for w in load_workloads(_BWD_OP_NAME):
shape = w["x_shape"]
N, C, spatial = shape[0], shape[1], tuple(shape[2:])
label = w.get("label", f"{N}x{C}")
for dtype_str in w["dtypes"]:
dtype = getattr(torch, dtype_str)
params.append(pytest.param(N, C, spatial, dtype,
id=f"{label}-{dtype_str}"))
return params
# ---------------------------------------------------------------------------
# Benchmark tests
# ---------------------------------------------------------------------------
@pytest.mark.parametrize("N, C, spatial, dtype, training, tune", _manifest_fwd_params())
def test_batch_norm_fwd_bench(N, C, spatial, dtype, training, tune):
x, weight, bias, running_mean, running_var = _make_inputs(N, C, spatial, dtype)
# Manifest input order: (x, running_mean, running_var, weight, bias).
inputs = (x, running_mean, running_var, weight, bias)
op = BatchNormFwdOp(N, C, tuple(spatial), dtype=dtype, training=training, tune=tune)
test = BatchNormFwdTest(N, C, spatial, dtype, training)
bm = BatchNormFwdBenchmark(test, op)
result = bm.profile(lambda *a: op(*a), *inputs)
spatial = str(spatial) # stringify tuple so it survives BenchmarkReport.record filtering
BenchmarkReport.record(op, locals(), result, tag="tileops")
result_bl = bm.profile(
lambda x, rm, rv, w, b: _torch_bn_fwd(x, w, b, rm, rv), *inputs,
)
BenchmarkReport.record(op, locals(), result_bl, tag="torch-cudnn")
@pytest.mark.parametrize("N, C, spatial, dtype", _manifest_bwd_params())
def test_batch_norm_bwd_bench(N, C, spatial, dtype):
inputs = _make_bwd_inputs(N, C, spatial, dtype)
op = BatchNormBwdOp(N, C, *spatial, dtype=dtype)
test = BatchNormBwdTest(N, C, spatial, dtype)
bm = BatchNormBwdBenchmark(test, op)
result = bm.profile(op, *inputs)
spatial = str(spatial) # stringify tuple so it survives BenchmarkReport.record filtering
BenchmarkReport.record(op, locals(), result, tag="tileops")
result_bl = bm.profile(_torch_bn_bwd, *inputs)
BenchmarkReport.record(op, locals(), result_bl, tag="torch-autograd")
if __name__ == "__main__":
pytest.main([__file__, "-vvs"])