-
Notifications
You must be signed in to change notification settings - Fork 120
Expand file tree
/
Copy pathtest_reduction.py
More file actions
608 lines (516 loc) · 24 KB
/
test_reduction.py
File metadata and controls
608 lines (516 loc) · 24 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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
# SPDX-FileCopyrightText: Copyright (c) <2025> NVIDIA CORPORATION & AFFILIATES. All rights reserved.
#
# SPDX-License-Identifier: Apache-2.0
import operator
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 cuda.tile import TileSyntaxError
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_two_axes(reduce_op):
@ct.kernel
def kernel(input, output,
TILE: ct.Constant[int],
N: ct.Constant[int],
M: ct.Constant[int],
keepdims: ct.Constant[bool],
a1: ct.Constant[int],
a2: ct.Constant[int]):
px = ct.bid(0)
rows = ct.load(input, index=(px, 0, 0), shape=(TILE, N, M))
out = reduce_op(rows, axis=(a1, a2), 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)
@pytest.mark.parametrize("axes", [(1, 2), (2, 1), (-1, -2), (-2, -1), (-2, 2), (2, -2)])
def test_reduce_maxminf_two_axes(shape, tile, dtype, keepdims, reduce_op, torch_op, axes):
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_two_axes(reduce_op)
ct.launch(torch.cuda.current_stream(), grid, kernel,
(x, y, tile, shape[1], shape[2], keepdims, *axes))
ref_result = torch_op(x, dim=(1, 2), keepdim=keepdims)
torch.testing.assert_close(y, ref_result)
def test_reduce_repeated_axis_error():
@ct.kernel
def kernel(x):
tx = ct.load(x, (0, 0), (16, 16))
ct.sum(tx, axis=(1, 1))
x = torch.rand((16, 16), dtype=torch.float32, device="cuda")
with pytest.raises(TileTypeError, match="Repeated reduction axis 1"):
ct.launch(torch.cuda.current_stream(), (1,), kernel, (x,))
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)
@pytest.mark.parametrize("flavor", ["lambda", "def", "operator"])
def test_custom_reduction_simple(flavor: str):
@ct.kernel
def kernel_lambda(x, y):
xt = ct.load(x, (0, 0), (16, 16))
yt = ct.reduce(xt, 0, lambda a, b: a + b, 0)
ct.store(y, (0,), yt)
@ct.kernel
def kernel_def(x, y):
def f(a, b):
return a + b
xt = ct.load(x, (0, 0), (16, 16))
yt = ct.reduce(xt, 0, f, 0)
ct.store(y, (0,), yt)
@ct.kernel
def kernel_operator(x, y):
xt = ct.load(x, (0, 0), (16, 16))
yt = ct.reduce(xt, 0, operator.add, 0)
ct.store(y, (0,), yt)
kernel = locals()[f"kernel_{flavor}"]
x = torch.arange(256, dtype=torch.int32, device="cuda").reshape(16, 16)
ref = torch.sum(x, 0, dtype=torch.int32)
y = torch.zeros((16,), dtype=torch.int32, device="cuda")
ct.launch(torch.cuda.current_stream(), (1,), kernel, (x, y))
assert_equal(y, ref)
def test_custom_reduction_keepdims():
@ct.kernel
def kernel(x, y):
xt = ct.load(x, (0, 0), (16, 16))
yt = ct.reduce(xt, 0, lambda a, b: a + b, 0, keepdims=True)
ct.store(y, (0, 0), yt)
x = torch.arange(256, dtype=torch.int32, device="cuda").reshape(16, 16)
ref = torch.sum(x, 0, dtype=torch.int32, keepdim=True)
y = torch.zeros((1, 16), dtype=torch.int32, device="cuda")
ct.launch(torch.cuda.current_stream(), (1,), kernel, (x, y))
assert_equal(y, ref)
def test_custom_reduction_last_axis():
@ct.kernel
def kernel(x, y):
xt = ct.load(x, (0, 0), (16, 16))
yt = ct.reduce(xt, -1, lambda a, b: a + b, 0)
ct.store(y, (0,), yt)
x = torch.arange(256, dtype=torch.int32, device="cuda").reshape(16, 16)
ref = torch.sum(x, -1, dtype=torch.int32)
y = torch.zeros((16,), dtype=torch.int32, device="cuda")
ct.launch(torch.cuda.current_stream(), (1,), kernel, (x, y))
assert_equal(y, ref)
def test_custom_reduction_minimum_with_index():
def minimum_with_index(a_val, a_idx, b_val, b_idx):
lt = a_val < b_val
eq = a_val == b_val
mask = lt | (eq & (a_idx < b_idx))
return ct.where(mask, a_val, b_val), ct.where(mask, a_idx, b_idx)
@ct.kernel
def kernel(x, y, yi):
xt = ct.load(x, (0, 0), (4, 8))
idx = ct.arange(8, dtype=ct.int32)
yt, yit = ct.reduce((xt, idx), 1, minimum_with_index, (2**31-1, 0))
ct.store(y, (0,), yt)
ct.store(yi, (0,), yit)
x = torch.tensor(
[
[5, 10, 7, 23, 8, 9, -4, 4],
[2, 2, 2, 2, 2, 2, 2, 2],
[17, 16, 15, 14, 13, 12, 11, 10],
[13, 13, 12, 12, 9, 9, 11, 11],
],
dtype=torch.int32, device="cuda"
)
y_ref = torch.tensor([-4, 2, 10, 9], dtype=torch.int32, device="cuda")
yi_ref = torch.tensor([6, 0, 7, 4], dtype=torch.int32, device="cuda")
y = torch.zeros((4,), dtype=torch.int32, device="cuda")
yi = torch.zeros((4,), dtype=torch.int32, device="cuda")
ct.launch(torch.cuda.current_stream(), (1,), kernel, (x, y, yi))
assert_equal(y, y_ref)
assert_equal(yi, yi_ref)
def test_custom_reduction_with_capture():
@ct.kernel
def kernel(x, p, y):
modulo = ct.gather(p, ())
xt = ct.load(x, (0, 0), (16, 16))
yt = ct.reduce(xt, -1, lambda a, b: (a + b) % modulo, 0)
ct.store(y, (0,), yt)
x = torch.arange(256, dtype=torch.int32, device="cuda").reshape(16, 16)
p = torch.tensor(5, dtype=torch.int32, device="cuda")
ref = torch.sum(x, -1, dtype=torch.int32) % 5
y = torch.zeros((16,), dtype=torch.int32, device="cuda")
ct.launch(torch.cuda.current_stream(), (1,), kernel, (x, p, y))
assert_equal(y, ref)
def test_custom_reduction_welford():
@ct.kernel
def kernel(x, w, y):
def combine(mean_a, m2_a, weight_a, mean_b, m2_b, weight_b):
delta = mean_b - mean_a
new_weight = weight_a + weight_b
wb_normalized = ct.where(new_weight == 0, 0, weight_b / new_weight)
new_mean = mean_a + delta * wb_normalized
new_m2 = m2_a + m2_b + delta * delta * weight_a * wb_normalized
return new_mean, new_m2, new_weight
tx = ct.load(x, (0,), 1024)
tw = ct.load(w, (0,), 1024)
tm2 = ct.zeros((1024,), dtype=tx.dtype)
mean, m2, _ = ct.reduce((tx, tm2, tw), 0, combine, (0, 0, 0))
ct.scatter(y, 0, mean)
ct.scatter(y, 1, m2)
x = torch.randn((1024,), dtype=torch.float32, device="cuda") * 3.0 + 1.5
w = torch.randint(0, 100, (1024,), device="cuda")
w_sum = torch.sum(w).item()
ref_mean = torch.sum(x * w / w_sum).item()
ref_var = torch.cov(x, fweights=w).item()
y = torch.zeros((2,), dtype=torch.float32, device="cuda")
ct.launch(torch.cuda.current_stream(), (1,), kernel, (x, w.to(torch.float32), y))
mean, m2 = y.tolist()
var = m2 / w_sum
assert abs(mean - ref_mean) < 1e-3
assert abs(var - ref_var) < 1e-3
def test_custom_reduction_ifelse_not_supported():
@ct.kernel
def kernel(x, y):
def f(a, b):
if ct.bid(0) == 0:
return a + b
else:
return (a + b) % 5
xt = ct.load(x, (0, 0), (16, 16))
yt = ct.reduce(xt, -1, f, 0)
ct.store(y, (0,), yt)
x = torch.arange(256, dtype=torch.int32, device="cuda").reshape(16, 16)
y = torch.zeros((16,), dtype=torch.int32, device="cuda")
with pytest.raises(TileSyntaxError, match="Branching inside reduction body is not supported"):
ct.launch(torch.cuda.current_stream(), (1,), kernel, (x, y))
def test_custom_reduction_loop_not_supported():
@ct.kernel
def kernel(x, y):
def f(a, b):
res = ct.zeros((), dtype=ct.int32)
for i in range(ct.bid(0) + 1):
res += (a + b) * i
return res
xt = ct.load(x, (0, 0), (16, 16))
yt = ct.reduce(xt, -1, f, 0)
ct.store(y, (0,), yt)
x = torch.arange(256, dtype=torch.int32, device="cuda").reshape(16, 16)
y = torch.zeros((16,), dtype=torch.int32, device="cuda")
with pytest.raises(TileSyntaxError, match="Loops inside reduction body are not supported"):
ct.launch(torch.cuda.current_stream(), (1,), kernel, (x, y))
def test_custom_reduction_printf_not_supported():
@ct.kernel
def kernel(x, y):
def f(a, b):
ct.printf("%d %d", a, b)
return a + b
xt = ct.load(x, (0, 0), (16, 16))
yt = ct.reduce(xt, -1, f, 0)
ct.store(y, (0,), yt)
x = torch.arange(256, dtype=torch.int32, device="cuda").reshape(16, 16)
y = torch.zeros((16,), dtype=torch.int32, device="cuda")
with pytest.raises(TileSyntaxError, match="Operations with memory effects"
" are not supported inside reduction body"):
ct.launch(torch.cuda.current_stream(), (1,), kernel, (x, y))
def test_custom_reduction_tile_load_not_supported():
@ct.kernel
def kernel(x, y):
def f(a, b):
return ct.load(x, (a, b), (1, 1)).item()
xt = ct.load(x, (0, 0), (16, 16))
yt = ct.reduce(xt, -1, f, 0)
ct.store(y, (0,), yt)
x = torch.arange(256, dtype=torch.int32, device="cuda").reshape(16, 16)
y = torch.zeros((16,), dtype=torch.int32, device="cuda")
with pytest.raises(TileSyntaxError, match="Operations with memory effects"
" are not supported inside reduction body"):
ct.launch(torch.cuda.current_stream(), (1,), kernel, (x, y))