forked from tile-ai/TileOPs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbench_pool.py
More file actions
369 lines (321 loc) · 11.5 KB
/
Copy pathbench_pool.py
File metadata and controls
369 lines (321 loc) · 11.5 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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
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.kernels.pool.common import pool_output_dim
from tileops.ops import AvgPool1dOp, AvgPool2dOp, AvgPool3dOp
class AvgPool1dBenchCase:
def __init__(
self,
n: int,
c_in: int,
l_in: int,
kernel_size: int,
stride: Optional[int],
padding: int,
ceil_mode: bool,
count_include_pad: bool,
dtype: torch.dtype,
) -> None:
self.n = n
self.c_in = c_in
self.l_in = l_in
self.kernel_size = kernel_size
self.stride = kernel_size if stride is None else stride
self.padding = padding
self.ceil_mode = ceil_mode
self.count_include_pad = count_include_pad
self.dtype = dtype
def gen_inputs(self) -> tuple[torch.Tensor]:
x = torch.randn(self.n, self.l_in, self.c_in, device=DEVICE, dtype=self.dtype).contiguous()
return (x,)
def ref_program(self, x: torch.Tensor) -> torch.Tensor:
return F.avg_pool1d(
x,
kernel_size=self.kernel_size,
stride=self.stride,
padding=self.padding,
ceil_mode=self.ceil_mode,
count_include_pad=self.count_include_pad,
)
class AvgPool1dBenchmark(BenchmarkBase[AvgPool1dBenchCase]):
def calculate_flops(self) -> Optional[float]:
t = self.workload
out_l = pool_output_dim(t.l_in, t.kernel_size, t.stride, t.padding, t.ceil_mode)
return t.n * t.c_in * out_l * t.kernel_size
def calculate_memory(self) -> Optional[float]:
t = self.workload
out_l = pool_output_dim(t.l_in, t.kernel_size, t.stride, t.padding, t.ceil_mode)
return (t.n * t.c_in * t.l_in + t.n * t.c_in * out_l) * t.dtype.itemsize
_AVG_POOL1D_BENCH_PARAMS = [
pytest.param(4, 128, 4096, 3, 2, 1, False, True, torch.float16, True, id="audio-downsample-fp16"),
pytest.param(2, 256, 32000, 5, 4, 2, False, True, torch.float16, True, id="long-temporal-fp16"),
pytest.param(2, 128, 2048, 4, 2, 1, True, False, torch.bfloat16, True, id="ceil-bf16"),
]
@pytest.mark.parametrize(
"n, c_in, l_in, kernel_size, stride, padding, ceil_mode, count_include_pad, dtype, tune",
_AVG_POOL1D_BENCH_PARAMS,
)
def test_avg_pool1d_bench(
n: int,
c_in: int,
l_in: int,
kernel_size: int,
stride: Optional[int],
padding: int,
ceil_mode: bool,
count_include_pad: bool,
dtype: torch.dtype,
tune: bool,
) -> None:
test = AvgPool1dBenchCase(n, c_in, l_in, kernel_size, stride, padding, ceil_mode, count_include_pad, dtype)
bm = AvgPool1dBenchmark(test)
inputs = test.gen_inputs()
(x,) = inputs
x_ncl = x.permute(0, 2, 1).contiguous()
op = AvgPool1dOp(
n=n,
c_in=c_in,
l_in=l_in,
kernel_size=kernel_size,
stride=stride,
padding=padding,
ceil_mode=ceil_mode,
count_include_pad=count_include_pad,
dtype=dtype,
tune=tune,
)
result = bm.profile(op, *inputs)
BenchmarkReport.record("avg_pool1d", locals(), result, tag="tileops")
result_bl = bm.profile(test.ref_program, x_ncl)
BenchmarkReport.record("avg_pool1d", locals(), result_bl, tag="torch-ref")
class AvgPool2dBenchCase:
def __init__(
self,
n: int,
c_in: int,
h_in: int,
w_in: int,
kernel_size: tuple[int, int],
stride: Optional[tuple[int, int]],
padding: tuple[int, int],
ceil_mode: bool,
count_include_pad: bool,
divisor_override: Optional[int],
dtype: torch.dtype,
) -> None:
self.n = n
self.c_in = c_in
self.h_in = h_in
self.w_in = w_in
self.kernel_size = kernel_size
self.stride = kernel_size if stride is None else stride
self.padding = padding
self.ceil_mode = ceil_mode
self.count_include_pad = count_include_pad
self.divisor_override = divisor_override
self.dtype = dtype
def gen_inputs(self) -> tuple[torch.Tensor]:
x = torch.randn(self.n, self.h_in, self.w_in, self.c_in, device=DEVICE, dtype=self.dtype).contiguous()
return (x,)
def ref_program(self, x: torch.Tensor) -> torch.Tensor:
return F.avg_pool2d(
x,
kernel_size=self.kernel_size,
stride=self.stride,
padding=self.padding,
ceil_mode=self.ceil_mode,
count_include_pad=self.count_include_pad,
divisor_override=self.divisor_override,
)
class AvgPool2dBenchmark(BenchmarkBase[AvgPool2dBenchCase]):
def calculate_flops(self) -> Optional[float]:
t = self.workload
out_h = pool_output_dim(t.h_in, t.kernel_size[0], t.stride[0], t.padding[0], t.ceil_mode)
out_w = pool_output_dim(t.w_in, t.kernel_size[1], t.stride[1], t.padding[1], t.ceil_mode)
return t.n * t.c_in * out_h * out_w * t.kernel_size[0] * t.kernel_size[1]
def calculate_memory(self) -> Optional[float]:
t = self.workload
out_h = pool_output_dim(t.h_in, t.kernel_size[0], t.stride[0], t.padding[0], t.ceil_mode)
out_w = pool_output_dim(t.w_in, t.kernel_size[1], t.stride[1], t.padding[1], t.ceil_mode)
return (t.n * t.c_in * t.h_in * t.w_in + t.n * t.c_in * out_h * out_w) * t.dtype.itemsize
_AVG_POOL2D_BENCH_PARAMS = [
pytest.param(2, 64, 112, 112, (3, 3), (2, 2), (1, 1), False, True, None, torch.float16, True, id="vision-3x3-s2"),
pytest.param(2, 128, 56, 56, (5, 5), (2, 2), (2, 2), False, True, None, torch.float16, True, id="vision-5x5-s2"),
pytest.param(3, 96, 55, 57, (3, 5), (2, 2), (1, 2), True, False, 7, torch.bfloat16, True, id="ceil-divisor-bf16"),
]
@pytest.mark.parametrize(
"n, c_in, h_in, w_in, kernel_size, stride, padding, ceil_mode, count_include_pad, divisor_override, dtype, tune",
_AVG_POOL2D_BENCH_PARAMS,
)
def test_avg_pool2d_bench(
n: int,
c_in: int,
h_in: int,
w_in: int,
kernel_size: tuple[int, int],
stride: Optional[tuple[int, int]],
padding: tuple[int, int],
ceil_mode: bool,
count_include_pad: bool,
divisor_override: Optional[int],
dtype: torch.dtype,
tune: bool,
) -> None:
test = AvgPool2dBenchCase(
n,
c_in,
h_in,
w_in,
kernel_size,
stride,
padding,
ceil_mode,
count_include_pad,
divisor_override,
dtype,
)
bm = AvgPool2dBenchmark(test)
inputs = test.gen_inputs()
(x,) = inputs
x_nchw = x.permute(0, 3, 1, 2).contiguous()
op = AvgPool2dOp(
n=n,
c_in=c_in,
h_in=h_in,
w_in=w_in,
kernel_size=kernel_size,
stride=stride,
padding=padding,
ceil_mode=ceil_mode,
count_include_pad=count_include_pad,
divisor_override=divisor_override,
dtype=dtype,
tune=tune,
)
result = bm.profile(op, *inputs)
BenchmarkReport.record("avg_pool2d", locals(), result, tag="tileops")
result_bl = bm.profile(test.ref_program, x_nchw)
BenchmarkReport.record("avg_pool2d", locals(), result_bl, tag="torch-ref")
class AvgPool3dBenchCase:
def __init__(
self,
n: int,
c_in: int,
d_in: int,
h_in: int,
w_in: int,
kernel_size: tuple[int, int, int],
stride: Optional[tuple[int, int, int]],
padding: tuple[int, int, int],
ceil_mode: bool,
count_include_pad: bool,
divisor_override: Optional[int],
dtype: torch.dtype,
) -> None:
self.n = n
self.c_in = c_in
self.d_in = d_in
self.h_in = h_in
self.w_in = w_in
self.kernel_size = kernel_size
self.stride = kernel_size if stride is None else stride
self.padding = padding
self.ceil_mode = ceil_mode
self.count_include_pad = count_include_pad
self.divisor_override = divisor_override
self.dtype = dtype
def gen_inputs(self) -> tuple[torch.Tensor]:
x = torch.randn(
self.n, self.d_in, self.h_in, self.w_in, self.c_in, device=DEVICE, dtype=self.dtype
).contiguous()
return (x,)
def ref_program(self, x: torch.Tensor) -> torch.Tensor:
return F.avg_pool3d(
x,
kernel_size=self.kernel_size,
stride=self.stride,
padding=self.padding,
ceil_mode=self.ceil_mode,
count_include_pad=self.count_include_pad,
divisor_override=self.divisor_override,
)
class AvgPool3dBenchmark(BenchmarkBase[AvgPool3dBenchCase]):
def calculate_flops(self) -> Optional[float]:
t = self.workload
out_d = pool_output_dim(t.d_in, t.kernel_size[0], t.stride[0], t.padding[0], t.ceil_mode)
out_h = pool_output_dim(t.h_in, t.kernel_size[1], t.stride[1], t.padding[1], t.ceil_mode)
out_w = pool_output_dim(t.w_in, t.kernel_size[2], t.stride[2], t.padding[2], t.ceil_mode)
return t.n * t.c_in * out_d * out_h * out_w * t.kernel_size[0] * t.kernel_size[1] * t.kernel_size[2]
def calculate_memory(self) -> Optional[float]:
t = self.workload
out_d = pool_output_dim(t.d_in, t.kernel_size[0], t.stride[0], t.padding[0], t.ceil_mode)
out_h = pool_output_dim(t.h_in, t.kernel_size[1], t.stride[1], t.padding[1], t.ceil_mode)
out_w = pool_output_dim(t.w_in, t.kernel_size[2], t.stride[2], t.padding[2], t.ceil_mode)
return (
t.n * t.c_in * t.d_in * t.h_in * t.w_in + t.n * t.c_in * out_d * out_h * out_w
) * t.dtype.itemsize
_AVG_POOL3D_BENCH_PARAMS = [
pytest.param(1, 32, 16, 56, 56, (2, 2, 2), (2, 2, 2), (0, 0, 0), False, True, None, torch.float16, True, id="video-2x2x2"),
pytest.param(2, 64, 8, 28, 28, (2, 3, 3), (2, 2, 2), (1, 1, 1), True, False, None, torch.float16, True, id="ceil-video"),
pytest.param(2, 24, 10, 20, 22, (2, 2, 3), (2, 2, 2), (0, 1, 1), False, True, 7, torch.bfloat16, True, id="divisor-bf16"),
]
@pytest.mark.parametrize(
"n, c_in, d_in, h_in, w_in, kernel_size, stride, padding, ceil_mode, count_include_pad, divisor_override, dtype, tune",
_AVG_POOL3D_BENCH_PARAMS,
)
def test_avg_pool3d_bench(
n: int,
c_in: int,
d_in: int,
h_in: int,
w_in: int,
kernel_size: tuple[int, int, int],
stride: Optional[tuple[int, int, int]],
padding: tuple[int, int, int],
ceil_mode: bool,
count_include_pad: bool,
divisor_override: Optional[int],
dtype: torch.dtype,
tune: bool,
) -> None:
test = AvgPool3dBenchCase(
n,
c_in,
d_in,
h_in,
w_in,
kernel_size,
stride,
padding,
ceil_mode,
count_include_pad,
divisor_override,
dtype,
)
bm = AvgPool3dBenchmark(test)
inputs = test.gen_inputs()
(x,) = inputs
x_ncdhw = x.permute(0, 4, 1, 2, 3).contiguous()
op = AvgPool3dOp(
n=n,
c_in=c_in,
d_in=d_in,
h_in=h_in,
w_in=w_in,
kernel_size=kernel_size,
stride=stride,
padding=padding,
ceil_mode=ceil_mode,
count_include_pad=count_include_pad,
divisor_override=divisor_override,
dtype=dtype,
tune=tune,
)
result = bm.profile(op, *inputs)
BenchmarkReport.record("avg_pool3d", locals(), result, tag="tileops")
result_bl = bm.profile(test.ref_program, x_ncdhw)
BenchmarkReport.record("avg_pool3d", locals(), result_bl, tag="torch-ref")