forked from NVIDIA/cutile-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_reduction.py
More file actions
376 lines (329 loc) · 15.9 KB
/
test_reduction.py
File metadata and controls
376 lines (329 loc) · 15.9 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
370
371
372
373
374
375
376
# SPDX-FileCopyrightText: Copyright (c) <2025> NVIDIA CORPORATION & AFFILIATES. All rights reserved.
#
# SPDX-License-Identifier: Apache-2.0
import pytest
import torch
from torch.testing import make_tensor
from typing import Optional, Tuple
from conftest import float_dtypes, int_dtypes, dtype_id
from math import ceil
from util import filecheck, assert_equal, get_bytecode
import cuda.tile as ct
from cuda.tile._exception import TileTypeError
from cuda.tile._numeric_semantics import RoundingMode as RMd
def _squeezed_zeros_like(x, axis: Optional[int | Tuple[int, ...]], keepdims: bool):
shape = x.shape
if axis is None:
squeezed_shape = (1,) * (len(shape) if keepdims else 0)
else:
# Normalize to tuple
if isinstance(axis, int):
axis = (axis,)
axis = tuple(a % x.ndim for a in axis) # handle negative axes
axis_set = set(axis)
squeezed_shape = []
for i, dim in enumerate(shape):
if i in axis_set:
if keepdims:
squeezed_shape.append(1)
else:
squeezed_shape.append(dim)
return torch.zeros(squeezed_shape, dtype=x.dtype, device="cuda")
def make_reduce_axis1_2d(reduce_op):
@ct.kernel
def kernel(
input, output, B: ct.Constant[int], N: ct.Constant[int], keepdims: ct.Constant[bool]
):
px = ct.bid(0)
rows = ct.load(input, index=(px, 0), shape=(B, N))
out = reduce_op(rows, axis=1, keepdims=keepdims)
if keepdims:
ct.store(output, index=(px, 0), tile=out)
else:
ct.store(output, index=(px, ), tile=out)
return kernel
def make_reduce_axis1_3d(reduce_op):
@ct.kernel
def kernel(
input, output,
B: ct.Constant[int], N: ct.Constant[int], M: ct.Constant[int], keepdims: ct.Constant[bool]
):
px = ct.bid(0)
rows = ct.load(input, index=(px, 0, 0), shape=(B, N, M))
out = reduce_op(rows, axis=1, keepdims=keepdims)
if keepdims:
ct.store(output, index=(px, 0, 0), tile=out)
else:
ct.store(output, index=(px, 0), tile=out)
return kernel
maxmin_cases = [
pytest.param(ct.max, torch.amax, id="max"),
pytest.param(ct.min, torch.amin, id="min"),
]
@pytest.mark.parametrize("shape", [(512, 128)])
@pytest.mark.parametrize("tile", [16])
@pytest.mark.parametrize("dtype", float_dtypes, ids=dtype_id)
@pytest.mark.parametrize("keepdims", [True, False])
@pytest.mark.parametrize("reduce_op, torch_op", maxmin_cases)
def test_reduce_maxminf(shape, tile, dtype, keepdims, reduce_op, torch_op):
x = torch.rand(shape, dtype=dtype, device="cuda") * 2 - 1
y = _squeezed_zeros_like(x, axis=1, keepdims=keepdims)
grid = (ceil(shape[0] / tile), 1, 1)
kernel = make_reduce_axis1_2d(reduce_op)
ct.launch(torch.cuda.current_stream(), grid, kernel, (x, y, tile, shape[1], keepdims))
ref_result = torch_op(x, dim=1, keepdim=keepdims)
torch.testing.assert_close(y, ref_result)
@pytest.mark.parametrize("shape", [(512, 128)])
@pytest.mark.parametrize("tile", [16])
@pytest.mark.parametrize("dtype", int_dtypes, ids=dtype_id)
@pytest.mark.parametrize("low", [-100])
@pytest.mark.parametrize("high", [-20, 100])
@pytest.mark.parametrize("keepdims", [True, False])
@pytest.mark.parametrize("reduce_op, torch_op", maxmin_cases)
def test_reduce_maxmini(shape, tile, dtype, low, high, keepdims, reduce_op, torch_op):
x = torch.randint(low, high + 1, shape, dtype=dtype, device="cuda")
y = _squeezed_zeros_like(x, axis=1, keepdims=keepdims)
grid = (ceil(shape[0] / tile), 1, 1)
kernel = make_reduce_axis1_2d(reduce_op)
ct.launch(torch.cuda.current_stream(), grid, kernel, (x, y, tile, shape[1], keepdims))
ref_result = torch_op(x, dim=1, keepdim=keepdims)
torch.testing.assert_close(y, ref_result)
torch.testing.assert_close(y, ref_result)
def make_reduce_axisNone(reduce_op):
@ct.kernel
def kernel(input, output,
B: ct.Constant[int],
N: ct.Constant[int],
keepdims: ct.Constant[bool]):
rows = ct.load(input, index=(0, 0), shape=(B, N))
if keepdims:
out = reduce_op(rows, axis=None, keepdims=keepdims)
else:
out = reduce_op(rows, axis=None, keepdims=keepdims)
out = ct.full((1, 1), out.item(), dtype=out.dtype)
ct.store(output, index=(0, 0), tile=out)
return kernel
@pytest.mark.parametrize("shape", [(512, 128)])
@pytest.mark.parametrize("dtype", float_dtypes, ids=dtype_id)
@pytest.mark.parametrize("keepdims", [True, False])
@pytest.mark.parametrize("reduce_op, torch_op", maxmin_cases)
def test_reduce_maxminf_all_axes(shape, dtype, keepdims, reduce_op, torch_op):
x = torch.rand(shape, dtype=dtype, device="cuda") * 2 - 1
grid = (1, 1, 1)
kernel = make_reduce_axisNone(reduce_op)
if keepdims:
y = _squeezed_zeros_like(x, axis=None, keepdims=keepdims)
ct.launch(torch.cuda.current_stream(), grid, kernel, (x, y, shape[0], shape[1], keepdims))
else:
y = torch.zeros((1,) * len(shape), dtype=dtype, device="cuda")
ct.launch(torch.cuda.current_stream(), grid, kernel, (x, y, shape[0], shape[1], keepdims))
y = y.squeeze()
ref_result = torch_op(x, dim=None, keepdim=keepdims)
torch.testing.assert_close(y, ref_result)
def make_reduce_max_axis12(reduce_op):
@ct.kernel
def kernel(input, output,
TILE: ct.Constant[int],
N: ct.Constant[int],
M: ct.Constant[int],
keepdims: ct.Constant[bool]):
px = ct.bid(0)
rows = ct.load(input, index=(px, 0, 0), shape=(TILE, N, M))
out = reduce_op(rows, axis=(1, 2), keepdims=keepdims)
if keepdims:
ct.store(output, index=(px, 0, 0), tile=out)
else:
ct.store(output, index=(px, ), tile=out)
return kernel
@pytest.mark.parametrize("shape", [(32, 32, 64)])
@pytest.mark.parametrize("tile", [16])
@pytest.mark.parametrize("dtype", float_dtypes, ids=dtype_id)
@pytest.mark.parametrize("keepdims", [True, False])
@pytest.mark.parametrize("reduce_op, torch_op", maxmin_cases)
def test_reduce_maxminf_axis12(shape, tile, dtype, keepdims, reduce_op, torch_op):
x = torch.rand(shape, dtype=dtype, device="cuda") * 2 - 1
y = _squeezed_zeros_like(x, axis=(1, 2), keepdims=keepdims)
grid = (ceil(shape[0] / tile), 1, 1)
kernel = make_reduce_max_axis12(reduce_op)
ct.launch(torch.cuda.current_stream(), grid, kernel, (x, y, tile, shape[1], shape[2], keepdims))
ref_result = torch_op(x, dim=(1, 2), keepdim=keepdims)
torch.testing.assert_close(y, ref_result)
sumprod_cases = [
pytest.param(ct.sum, torch.sum, id="sum"),
pytest.param(ct.prod, torch.prod, id="prod"),
]
@pytest.mark.parametrize("shape", [(512, 128)])
@pytest.mark.parametrize("tile", [16])
@pytest.mark.parametrize("dtype", float_dtypes, ids=dtype_id)
@pytest.mark.parametrize("keepdims", [True, False])
@pytest.mark.parametrize("reduce_op, torch_op", sumprod_cases)
def test_reduce_sumprodf(shape, tile, dtype, keepdims, reduce_op, torch_op):
if reduce_op is ct.sum and (dtype is torch.bfloat16 or dtype is torch.float16):
pytest.xfail("Bf16/Fp16 reduce sum introduce a difference from torch.")
x = torch.rand(shape, dtype=dtype, device="cuda") * 2 - 1
y = _squeezed_zeros_like(x, axis=1, keepdims=keepdims)
grid = (ceil(shape[0] / tile), 1, 1)
kernel = make_reduce_axis1_2d(reduce_op)
ct.launch(torch.cuda.current_stream(), grid, kernel, (x, y, tile, shape[1], keepdims))
ref_result = torch_op(x, dim=1, keepdim=keepdims).to(dtype)
torch.testing.assert_close(y, ref_result)
@pytest.mark.parametrize("shape", [(512, 128)])
@pytest.mark.parametrize("tile", [16])
@pytest.mark.parametrize("dtype", int_dtypes, ids=dtype_id)
@pytest.mark.parametrize("keepdims", [True, False])
@pytest.mark.parametrize("reduce_op, torch_op", sumprod_cases)
def test_reduce_sumprodi(shape, tile, dtype, keepdims, reduce_op, torch_op):
x = torch.randint(-100, 100, shape, dtype=dtype, device="cuda")
y = _squeezed_zeros_like(x, axis=1, keepdims=keepdims)
grid = (ceil(shape[0] / tile), 1, 1)
kernel = make_reduce_axis1_2d(reduce_op)
ct.launch(torch.cuda.current_stream(), grid, kernel, (x, y, tile, shape[1], keepdims))
ref_result = torch_op(x, dim=1, keepdim=keepdims).to(dtype)
torch.testing.assert_close(y, ref_result)
@pytest.mark.parametrize("shape", [(512, 128)])
@pytest.mark.parametrize("tile", [16])
@pytest.mark.parametrize("keepdims", [True, False])
@pytest.mark.parametrize("reduce_op, torch_op", sumprod_cases)
def test_reduce_sumprodb(shape, tile, keepdims, reduce_op, torch_op):
x = torch.randint(0, 2, shape, dtype=torch.bool, device="cuda")
y = _squeezed_zeros_like(x, axis=1, keepdims=keepdims).to(torch.int32)
grid = (ceil(shape[0] / tile), 1, 1)
kernel = make_reduce_axis1_2d(reduce_op)
ct.launch(torch.cuda.current_stream(), grid, kernel, (x, y, tile, shape[1], keepdims))
ref_result = torch_op(x, dim=1, keepdim=keepdims).to(torch.int32)
torch.testing.assert_close(y, ref_result)
@pytest.mark.parametrize("shape", [(512, 128)])
@pytest.mark.parametrize("dtype", float_dtypes, ids=dtype_id)
@pytest.mark.parametrize("keepdims", [True, False])
@pytest.mark.parametrize("reduce_op, torch_op", sumprod_cases)
def test_reduce_sumprodf_all_axes(shape, dtype, keepdims, reduce_op, torch_op):
x = torch.rand(shape, dtype=dtype, device="cuda")
grid = (1, 1, 1)
kernel = make_reduce_axisNone(reduce_op)
if keepdims:
y = _squeezed_zeros_like(x, axis=None, keepdims=True)
ct.launch(torch.cuda.current_stream(), grid, kernel, (x, y, shape[0], shape[1], keepdims))
else:
y = torch.zeros((1,) * len(shape), dtype=dtype, device="cuda")
ct.launch(torch.cuda.current_stream(), grid, kernel, (x, y, shape[0], shape[1], keepdims))
y = y.squeeze()
if torch_op is torch.sum:
ref_result = torch_op(x, dim=None, keepdim=keepdims)
# Sum can be unstable, so we compare the average.
atol, rtol = (1e-5, 1e-6) if dtype is torch.float32 else (1e-5, 1e-2)
torch.testing.assert_close(y / x.numel(), ref_result / x.numel(), atol=atol, rtol=rtol)
else:
ref_result = torch_op(x)
if keepdims:
ref_result = ref_result.reshape([1] * x.ndim)
torch.testing.assert_close(y, ref_result)
def make_sumprod_rounding_mode(reduce_op, rounding_mode):
@ct.kernel
def kernel(input, output, B: ct.Constant[int], N: ct.Constant[int]):
px = ct.bid(0)
rows = ct.load(input, index=(px, 0), shape=(B, N))
out = reduce_op(rows, axis=1, keepdims=True, rounding_mode=rounding_mode)
ct.store(output, index=(px, 0), tile=out)
return kernel
@pytest.mark.use_mlir
@pytest.mark.parametrize("shape", [(512, 128)])
@pytest.mark.parametrize("tile", [16])
@pytest.mark.parametrize("dtype", float_dtypes, ids=dtype_id)
@pytest.mark.parametrize("op_func, tile_op", [(ct.sum, "addf"), (ct.prod, "mulf")])
@pytest.mark.parametrize("rounding_mode",
[RMd.RN, RMd.RZ, RMd.RM, RMd.RP, RMd.FULL, RMd.APPROX, RMd.RZI])
def test_reduce_sumprodf_rounding_mode(
shape, tile, dtype, op_func, tile_op, rounding_mode
):
should_raise_rounding_mode = rounding_mode in [RMd.RZI, RMd.APPROX, RMd.FULL]
x = make_tensor(shape, dtype=dtype, device='cuda')
y = _squeezed_zeros_like(x, axis=1, keepdims=True)
grid = (ceil(shape[0] / tile), 1, 1)
kernel = make_sumprod_rounding_mode(op_func, rounding_mode)
if should_raise_rounding_mode:
with pytest.raises(TileTypeError,
match=fr"Rounding mode {rounding_mode.value} is not supported"):
ct.launch(torch.cuda.current_stream(), grid, kernel, (x, y, tile, shape[1]))
else:
bytecode = get_bytecode(kernel, (x, y, tile, shape[1]))
if rounding_mode is RMd.RN:
# Rmd.RN as the default rounding mode is not included in the mlir text
check_directive = "// CHECK-NOT: rounding<{{[^>]*}}>"
else:
check_directive = (
f"// CHECK: %[[RES:.*]] = {tile_op} %[[A:.*]] rounding<{rounding_mode.value}>"
)
filecheck(bytecode, check_directive)
ct.launch(torch.cuda.current_stream(), grid, kernel, (x, y, tile, shape[1]))
def make_reduce_flush_to_zero(reduce_op, flush_to_zero):
@ct.kernel
def kernel(input, output, B: ct.Constant[int], N: ct.Constant[int]):
px = ct.bid(0)
rows = ct.load(input, index=(px, 0), shape=(B, N))
out = reduce_op(rows, axis=1, keepdims=True, flush_to_zero=flush_to_zero)
ct.store(output, index=(px, 0), tile=out)
return kernel
@pytest.mark.use_mlir
@pytest.mark.parametrize("shape", [(512, 128)])
@pytest.mark.parametrize("tile", [16])
@pytest.mark.parametrize("dtype", float_dtypes, ids=dtype_id)
@pytest.mark.parametrize("reduce_op, tile_op",
[(ct.max, "maxf"), (ct.min, "minf"), (ct.sum, "addf"), (ct.prod, "mulf")])
@pytest.mark.parametrize("flush_to_zero", [True, False])
def test_reduce_flush_to_zero(shape, tile, dtype, reduce_op, tile_op, flush_to_zero):
should_raise = flush_to_zero and (dtype != torch.float32)
x = make_tensor(shape, dtype=dtype, device='cuda')
y = _squeezed_zeros_like(x, axis=1, keepdims=True)
grid = (ceil(shape[0] / tile), 1, 1)
kernel = make_reduce_flush_to_zero(reduce_op, flush_to_zero)
if should_raise:
with pytest.raises(TileTypeError,
match=r"Flush to zero can only be used for float32 type"):
ct.launch(torch.cuda.current_stream(), grid, kernel, (x, y, tile, shape[1]))
else:
bytecode = get_bytecode(kernel, (x, y, tile, shape[1]))
if flush_to_zero:
check_directive = f"// CHECK: %[[RES:.*]] = {tile_op} %[[A:.*]] flush_to_zero :"
else:
check_directive = f"// CHECK: %[[RES:.*]] = {tile_op} %[[A:.*]]{{{{[[:space:]]*}}}}:"
filecheck(bytecode, check_directive)
ct.launch(torch.cuda.current_stream(), grid, kernel, (x, y, tile, shape[1]))
argmaxmin_cases = [
pytest.param(ct.argmax, torch.argmax, id="argmax"),
pytest.param(ct.argmin, torch.argmin, id="argmin"),
]
@pytest.mark.parametrize("shape", [(32, 16), (2, 4, 4)])
@pytest.mark.parametrize("tile", [16])
@pytest.mark.parametrize("dtype", float_dtypes+int_dtypes, ids=dtype_id)
@pytest.mark.parametrize("keepdims", [True, False])
@pytest.mark.parametrize("reduce_op, torch_op", argmaxmin_cases)
def test_reduce_argmaxmin(shape, tile, dtype, keepdims, reduce_op, torch_op):
x = make_tensor(shape, dtype=dtype, device='cuda')
y = _squeezed_zeros_like(x, axis=1, keepdims=keepdims).to(torch.int32)
grid = (ceil(shape[0] / tile), 1, 1)
if len(shape) == 2:
kernel = make_reduce_axis1_2d(reduce_op)
args = (x, y, tile, shape[1], keepdims)
else:
kernel = make_reduce_axis1_3d(reduce_op)
args = (x, y, tile, shape[1], shape[2], keepdims)
ct.launch(torch.cuda.current_stream(), grid, kernel, args)
ref_result = torch_op(x, dim=1, keepdim=keepdims).to(torch.int32)
assert_equal(y, ref_result)
@pytest.mark.parametrize("shape", [(512, 128)])
@pytest.mark.parametrize("dtype", float_dtypes + int_dtypes, ids=dtype_id)
@pytest.mark.parametrize("reduce_op, torch_op", argmaxmin_cases)
@pytest.mark.parametrize("keepdims", [True, False])
def test_reduce_argmaxmin_all_axes(shape, dtype, reduce_op, torch_op, keepdims):
x = make_tensor(shape, dtype=dtype, device='cuda')
grid = (1, 1, 1)
kernel = make_reduce_axisNone(reduce_op)
if keepdims:
y = _squeezed_zeros_like(x, axis=None, keepdims=keepdims).to(torch.int32)
ct.launch(torch.cuda.current_stream(), grid, kernel, (x, y, shape[0], shape[1], keepdims))
else:
y = torch.zeros((1,) * len(shape), dtype=dtype, device="cuda").to(torch.int32)
ct.launch(torch.cuda.current_stream(), grid, kernel, (x, y, shape[0], shape[1], keepdims))
y = y.squeeze()
ref_result = torch_op(x, dim=None, keepdim=keepdims).to(torch.int32)
assert_equal(y, ref_result)