-
Notifications
You must be signed in to change notification settings - Fork 120
Expand file tree
/
Copy pathtest_control_flow.py
More file actions
1034 lines (914 loc) · 31.9 KB
/
test_control_flow.py
File metadata and controls
1034 lines (914 loc) · 31.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
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
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# SPDX-FileCopyrightText: Copyright (c) <2025> NVIDIA CORPORATION & AFFILIATES. All rights reserved.
#
# SPDX-License-Identifier: Apache-2.0
import pytest
import torch
from math import ceil
import cuda.tile as ct
from cuda.tile._exception import TileTypeError, TileSyntaxError
from util import assert_equal
class TestForLoop:
@staticmethod
@ct.kernel
def plus_n_one_arg(x, n, tile: ct.Constant[int]):
i = ct.bid(0)
xi = ct.load(x, index=(i,), shape=(tile,))
for _ in range(n):
xi += 1
ct.store(x, index=(i,), tile=xi)
@staticmethod
@ct.kernel
def plus_n_two_args(x, n, tile: ct.Constant[int]):
i = ct.bid(0)
xi = ct.load(x, index=(i,), shape=(tile,))
for _ in range(0, n):
xi += 1
ct.store(x, index=(i,), tile=xi)
@staticmethod
@ct.kernel
def plus_n_step2(x, n, tile: ct.Constant[int]):
i = ct.bid(0)
xi = ct.load(x, index=(i,), shape=(tile,))
for _ in range(0, n * 2, 2):
xi += 1
ct.store(x, index=(i,), tile=xi)
@staticmethod
@ct.kernel
def plus_n_two_loops(x, n, tile: ct.Constant[int]):
i = ct.bid(0)
xi = ct.load(x, index=(i,), shape=(tile,))
k = 2
for _ in range(k):
xi += 1
for _ in range(k, n):
xi += 1
ct.store(x, index=(i,), tile=xi)
@staticmethod
@ct.kernel
def plus_n_two_loops_once_each(x, n, tile: ct.Constant[int]):
i = ct.bid(0)
xi = ct.load(x, index=(i,), shape=(tile,))
xi2 = ct.load(x, index=(i,), shape=(tile,))
for _ in range(n):
xi += 1
for _ in range(n):
xi2 += 1
ct.store(x, index=(i,), tile=xi)
@staticmethod
@ct.kernel
def plus_n_scalar_acc(x, n, tile: ct.Constant[int]):
i = ct.bid(0)
acc = 0
for _ in range(n):
acc += 1
tx = ct.full((tile,), acc, dtype=x.dtype)
ct.store(x, index=(i,), tile=tx)
@pytest.mark.parametrize(
"func_name",
[
"plus_n_one_arg",
"plus_n_two_args",
"plus_n_step2",
"plus_n_two_loops",
"plus_n_two_loops_once_each",
"plus_n_scalar_acc",
],
)
def test_basic_for_loop(self, func_name):
func = getattr(self, func_name)
N = 256
tile = 128
x = torch.zeros(N, dtype=torch.float32, device='cuda')
n = 5
grid = ((N // tile), 1, 1)
ct.launch(torch.cuda.current_stream(), grid, func, (x, n, tile))
ref = torch.full_like(x, n)
assert_equal(x, ref)
@staticmethod
@ct.kernel
def plus_n_nested_for_loops(x, n, tile: ct.Constant[int]):
i = ct.bid(0)
xi = ct.load(x, index=(i,), shape=(tile,))
for index in range(n):
for index2 in range(n):
xi += 1
ct.store(x, index=(i,), tile=xi)
@staticmethod
def plus_n_nested_for_loops_ref(x, n):
for index in range(n):
for index2 in range(n):
x += 1
return x
@staticmethod
@ct.kernel
def plus_n_nested_for_loops_for_two_variables(x, n, tile: ct.Constant[int]):
i = ct.bid(0)
xi = ct.load(x, index=(i,), shape=(tile,))
xi2 = ct.load(x, index=(i,), shape=(tile,))
for index in range(n):
xi += 1
for index2 in range(n):
xi2 += 1
x_out = xi + xi2
ct.store(x, index=(i,), tile=x_out)
@staticmethod
def plus_n_nested_for_loops_for_two_variables_ref(x, n):
x2 = x.clone()
for index in range(n):
x += 1
for index2 in range(n):
x2 += 1
return x + x2
@pytest.mark.parametrize(
"func_name",
[
"plus_n_nested_for_loops",
"plus_n_nested_for_loops_for_two_variables",
],
)
def test_nested_for_loops(self, func_name):
func = getattr(self, func_name)
ref_func = getattr(self, f"{func_name}_ref")
N = 256
tile = 128
x = torch.zeros(N, dtype=torch.float32, device='cuda')
ref = torch.zeros_like(x)
n = 5
grid = ((N // tile), 1, 1)
ct.launch(torch.cuda.current_stream(), grid, func, (x, n, tile))
ref = ref_func(ref, n)
assert_equal(x, ref)
@staticmethod
@ct.kernel
def plus_n_until_2(x, n, tile: ct.Constant[int]):
i = ct.bid(0)
xi = ct.load(x, index=(i,), shape=(tile,))
for index in range(n):
if index == 2:
break
xi += 1
ct.store(x, index=(i,), tile=xi)
@staticmethod
def plus_n_until_2_ref(x, n):
for index in range(n):
if index == 2:
break
x += 1
@pytest.mark.parametrize("n", [5, 1])
def test_break_in_for_loop(self, n):
N = 256
tile = 128
x = torch.zeros(N, dtype=torch.float32, device='cuda')
grid = ((N // tile), 1, 1)
with pytest.raises(TileSyntaxError, match="Break in a for loop is not supported"):
# TODO(Issue-103): Break in for needs to be transformed to loopOp
ct.launch(torch.cuda.current_stream(), grid, self.plus_n_until_2, (x, n, tile))
@staticmethod
@ct.kernel
def tuple_fibonacci(x):
t = ct.load(x, index=(0,), shape=(1,)), ct.load(x, index=(1,), shape=(1,))
for i in range(5):
t = t[1], t[0] + t[1]
ct.store(x, index=(2,), tile=t[1])
def test_tuple_carried_variable(self):
x = torch.ones(3, dtype=torch.float32, device="cuda")
ct.launch(torch.cuda.current_stream(), (1, 1, 1), self.tuple_fibonacci, (x,))
assert x.cpu().numpy()[2] == 13.0
class TestWhileLoop:
@staticmethod
@ct.kernel
def plus_n(x, n, tile: ct.Constant[int]):
i = ct.bid(0)
xi = ct.load(x, index=(i,), shape=(tile,))
count = 0
while count < n:
xi += 1
count += 1
ct.store(x, index=(i,), tile=xi)
def test_basic_while_loop(self):
N = 256
tile = 128
x = torch.zeros(N, dtype=torch.float32, device='cuda')
n = 5
grid = ((N // tile), 1, 1)
ct.launch(torch.cuda.current_stream(), grid, self.plus_n, (x, n, tile))
ref = torch.full_like(x, n)
assert_equal(x, ref)
@staticmethod
@ct.kernel
def break_in_while(x, n, tile: ct.Constant[int]):
i = ct.bid(0)
xi = ct.load(x, index=(i,), shape=(tile,))
count = 0
while count < n:
if count == 2:
break
xi += 1
count += 1
ct.store(x, index=(i,), tile=xi)
@staticmethod
def break_in_while_ref(x, n):
count = 0
while count < n:
if count == 2:
break
x += 1
count += 1
def test_break_in_while_loop(self):
N = 256
tile = 128
x = torch.zeros(N, dtype=torch.float32, device='cuda')
ref = torch.zeros_like(x)
grid = ((N // tile), 1, 1)
n = 5
ct.launch(torch.cuda.current_stream(), grid, self.break_in_while, (x, n, tile))
self.break_in_while_ref(ref, n)
assert_equal(x, ref)
@staticmethod
@ct.kernel
def continue_in_while(x, n, tile: ct.Constant[int]):
i = ct.bid(0)
xi = ct.load(x, index=(i,), shape=(tile,))
count = 0
while count < n:
count += 1
if count > 2:
continue
xi += 1
ct.store(x, index=(i,), tile=xi)
@staticmethod
def continue_in_while_ref(x, n):
count = 0
while count < n:
count += 1
if count > 2:
continue
x += 1
def test_continue_in_while_loop(self):
N = 256
tile = 128
x = torch.zeros(N, dtype=torch.float32, device='cuda')
ref = x.clone()
grid = ((N // tile), 1, 1)
n = 5
ct.launch(torch.cuda.current_stream(), grid, self.continue_in_while, (x, n, tile))
self.continue_in_while_ref(ref, n)
assert_equal(x, ref)
@staticmethod
@ct.kernel
def constant_assigned_inside_loop(x):
a = ct.bid(0) + 3.0
while ct.bid(0) == 1:
a = 10.0 # This shouldn't be constant-propagated as the result of the loop
break
t = ct.full((1,), a, x.dtype)
ct.store(x, (0,), t)
def test_constant_assigned_inside_loop(self):
x = torch.zeros(1, dtype=torch.float32, device='cuda')
ct.launch(torch.cuda.current_stream(), (1,), self.constant_assigned_inside_loop, (x,))
assert x.cpu().item() == 3.0
@staticmethod
@ct.kernel
def same_constant_two_branches_inside_loop(x):
a = 0
while True:
if ct.bid(0) == 0:
a = 1
break
a = 1
break
# Use `a` as tile shape to verify that it has been inferred as constant
t = ct.full((a,), 3.0, x.dtype)
ct.store(x, (0,), t)
def test_same_constant_two_branches_inside_loop(self):
x = torch.zeros(1, dtype=torch.float32, device='cuda')
ct.launch(torch.cuda.current_stream(), (1,),
self.same_constant_two_branches_inside_loop, (x,))
assert x.cpu().item() == 3.0
@staticmethod
@ct.kernel
def different_constants_two_branches_inside_loop(x):
a = 0
while True:
if ct.bid(0) == 0:
a = 1
break
a = 2
break
# This should error out because `a` is not a constant
t = ct.full((a,), 3.0, x.dtype)
ct.store(x, (0,), t)
def test_different_constant_two_branches_inside_loop(self):
x = torch.zeros(1, dtype=torch.float32, device='cuda')
with pytest.raises(TileTypeError,
match='Invalid argument "shape" of full\\(\\): Expected a const'):
ct.launch(torch.cuda.current_stream(), (1,),
self.different_constants_two_branches_inside_loop, (x,))
@staticmethod
@ct.kernel
def break_const_value(x):
a = 0
while True:
if ct.bid(0) == 0:
# the result variable will be const
a = 1
break
# even if we increment a
# the continue should not propagate non-constness
a += 1
t = ct.full((a,), 1.0, ct.int32)
ct.store(x, (0,), t)
def test_break_with_const_value(self):
x = torch.zeros(1, dtype=torch.int32, device='cuda')
ct.launch(torch.cuda.current_stream(), (1,),
self.break_const_value, (x,))
assert x.item() == 1.0
class TestIfCondtion:
@staticmethod
@ct.kernel
def plus_one_if_true(x, condition: bool, tile: ct.Constant[int]):
i = ct.bid(0)
xi = ct.load(x, index=(i,), shape=(tile,))
if condition:
xi += 1
ct.store(x, index=(i,), tile=xi)
@staticmethod
def plus_one_if_true_ref(x, condition: bool):
if condition:
x += 1
@staticmethod
@ct.kernel
def plus_or_minus_one(x, condition: bool, tile: ct.Constant[int]):
i = ct.bid(0)
xi = ct.load(x, index=(i,), shape=(tile,))
if condition:
xi += 1
else:
xi -= 1
ct.store(x, index=(i,), tile=xi)
@staticmethod
def plus_or_minus_one_ref(x, condition: bool):
if condition:
x += 1
else:
x -= 1
@pytest.mark.parametrize("func_name", ["plus_one_if_true", "plus_or_minus_one"])
@pytest.mark.parametrize("condition", [True, False, 1, 0])
def test_basic_if(self, func_name, condition):
func = getattr(self, func_name)
ref_func = getattr(self, f"{func_name}_ref")
N = 256
tile = 128
x = torch.zeros(N, dtype=torch.float32, device='cuda')
ref = x.clone()
grid = ((N // tile), 1, 1)
ct.launch(torch.cuda.current_stream(), grid, func, (x, condition, tile))
ref_func(ref, condition)
assert_equal(x, ref)
@staticmethod
@ct.kernel
def plus_one_two_ifs(x, condition0: bool, condition1: bool, tile: ct.Constant[int]):
i = ct.bid(0)
xi = ct.load(x, index=(i,), shape=(tile,))
if condition0:
xi += 1
if condition1:
xi += 1
ct.store(x, index=(i,), tile=xi)
@pytest.mark.parametrize("condition0", [1, 0])
@pytest.mark.parametrize("condition1", [1, 0])
def test_two_ifs(self, condition0, condition1):
N = 256
tile = 128
x = torch.zeros(N, dtype=torch.float32, device='cuda')
ref = torch.zeros_like(x)
grid = ((N // tile), 1, 1)
ct.launch(torch.cuda.current_stream(), grid, self.plus_one_two_ifs,
(x, condition0, condition1, tile))
ref += condition0 + condition1
assert_equal(x, ref)
@staticmethod
@ct.kernel
def plus_one_nested_ifs(x, condition0: bool, condition1: bool, tile: ct.Constant[int]):
i = ct.bid(0)
xi = ct.load(x, index=(i,), shape=(tile,))
if condition0:
xi += 1
if condition1:
xi += 1
else:
xi -= 1
ct.store(x, index=(i,), tile=xi)
@staticmethod
def plus_one_nested_ifs_ref(x, condition0: bool, condition1: bool):
if condition0:
x += 1
if condition1:
x += 1
else:
x -= 1
@pytest.mark.parametrize("condition0", [True, False])
@pytest.mark.parametrize("condition1", [True, False])
def test_nested_ifs(self, condition0, condition1):
N = 256
tile = 128
x = torch.zeros(N, dtype=torch.float32, device='cuda')
ref = torch.zeros_like(x)
grid = ((N // tile), 1, 1)
ct.launch(torch.cuda.current_stream(), grid, self.plus_one_nested_ifs,
(x, condition0, condition1, tile))
self.plus_one_nested_ifs_ref(ref, condition0, condition1)
assert_equal(x, ref)
@staticmethod
@ct.kernel
def plus_one_two_conditions_and(x, condition0: bool, condition1: bool, tile: ct.Constant[int]):
i = ct.bid(0)
xi = ct.load(x, index=(i,), shape=(tile,))
if condition0 and condition1:
xi += 1
ct.store(x, index=(i,), tile=xi)
@staticmethod
def plus_one_two_conditions_and_ref(x, condition0: bool, condition1: bool):
if condition0 and condition1:
x += 1
@staticmethod
@ct.kernel
def plus_one_and_in_variable(x, condition0: bool, condition1: bool, tile: ct.Constant[int]):
i = ct.bid(0)
xi = ct.load(x, index=(i,), shape=(tile,))
cond = condition0 and condition1
if cond:
xi += 1
ct.store(x, index=(i,), tile=xi)
@staticmethod
def plus_one_and_in_variable_ref(x, condition0: bool, condition1: bool):
cond = condition0 and condition1
if cond:
x += 1
@staticmethod
@ct.kernel
def plus_one_two_conditions_or(x, condition0: bool, condition1: bool, tile: ct.Constant[int]):
i = ct.bid(0)
xi = ct.load(x, index=(i,), shape=(tile,))
if condition0 or condition1:
xi += 1
ct.store(x, index=(i,), tile=xi)
@staticmethod
def plus_one_two_conditions_or_ref(x, condition0: bool, condition1: bool):
if condition0 or condition1:
x += 1
@pytest.mark.parametrize(
"func_name",
[
"plus_one_two_conditions_and",
"plus_one_and_in_variable",
"plus_one_two_conditions_or",
],
)
@pytest.mark.parametrize("condition0", [1, 0])
@pytest.mark.parametrize("condition1", [1, 0])
def test_if_two_conditions(self, func_name, condition0, condition1):
func = getattr(self, func_name)
ref_func = getattr(self, func_name + "_ref")
N = 256
tile = 128
x = torch.zeros(N, dtype=torch.float32, device='cuda')
ref = torch.zeros_like(x)
grid = ((N // tile), 1, 1)
ct.launch(torch.cuda.current_stream(), grid, func, (x, condition0, condition1, tile))
ref_func(ref, condition0, condition1)
assert_equal(x, ref)
@staticmethod
@ct.kernel
def multiple_conditions_in_if(
x, condition0: bool | int, condition1: bool | int, condition2: bool | int,
base: int, tile: ct.Constant[int]
):
i = ct.bid(0)
xi = ct.load(x, index=(i,), shape=(tile,))
if condition0 or condition1 or condition2:
xi += 1
else:
xi -= 1
if condition0 and condition1 and condition2:
xi += 1
if condition0 or condition1 and condition2 and base > 50:
xi += 1
if condition0 and (condition1 or condition2) or base > 50:
xi += 1
cond = (condition0 or condition1) and condition2
if cond:
xi += 1
ct.store(x, index=(i,), tile=xi)
@staticmethod
def multiple_conditions_in_if_ref(
x, condition0: bool | int, condition1: bool | int, condition2: bool | int, base: int
):
if condition0 or condition1 or condition2:
x += 1
else:
x -= 1
if condition0 and condition1 and condition2:
x += 1
if condition0 or condition1 and condition2 and base > 50:
x += 1
if condition0 and (condition1 or condition2) or base > 50:
x += 1
cond = (condition0 or condition1) and condition2
if cond:
x += 1
@pytest.mark.parametrize("condition0", [1, 0, True, False])
@pytest.mark.parametrize("condition1", [5, 0, True, False])
@pytest.mark.parametrize("condition2", [-5, 0, True, False])
@pytest.mark.parametrize("base", [100, 0])
def test_if_multiple_conditions(self, condition0, condition1, condition2, base):
N = 256
tile = 128
x = torch.zeros(N, dtype=torch.float32, device='cuda')
ref = torch.zeros_like(x)
grid = ((N // tile), 1, 1)
ct.launch(torch.cuda.current_stream(), grid, self.multiple_conditions_in_if,
(x, condition0, condition1, condition2, base, tile))
self.multiple_conditions_in_if_ref(ref, condition0, condition1, condition2, base)
assert_equal(x, ref)
@staticmethod
@ct.kernel
def if_else_assignment(
x, condition0: bool, tile: ct.Constant[int]
):
i = ct.bid(0)
xi = ct.load(x, index=(i,), shape=(tile,))
xi = xi + 1 if condition0 else xi - 1
ct.store(x, index=(i,), tile=xi)
@staticmethod
def if_else_assignment_ref(
x, condition0: bool
):
return x + 1 if condition0 else x - 1
@pytest.mark.parametrize("condition0", [True, False])
def test_if_else_assignment(self, condition0):
N = 256
tile = 128
x = torch.zeros(N, dtype=torch.float32, device='cuda')
ref = torch.zeros_like(x)
grid = ((N // tile), 1, 1)
ct.launch(torch.cuda.current_stream(), grid, self.if_else_assignment,
(x, condition0, tile))
ref = self.if_else_assignment_ref(ref, condition0)
assert_equal(x, ref)
@staticmethod
@ct.kernel
def if_else_assignment_type_mismatch(
x, condition0: bool, tile: ct.Constant[int]
):
i = ct.bid(0)
xi = ct.load(x, index=(i,), shape=(tile,))
xi = condition0 if condition0 else xi - 1
ct.store(x, index=(i,), tile=xi)
@pytest.mark.parametrize("condition0", [True, False])
def test_if_else_assignment_type_mismatch(self, condition0):
N = 256
tile = 128
x = torch.zeros(N, dtype=torch.float32, device='cuda')
grid = ((N // tile), 1, 1)
with pytest.raises(TileTypeError):
ct.launch(torch.cuda.current_stream(), grid, self.if_else_assignment_type_mismatch,
(x, condition0, tile))
@staticmethod
@ct.kernel
def if_else_assignment_type_match(
x, condition0: bool, tile: ct.Constant[int]
):
i = ct.bid(0)
xi = ct.load(x, index=(i,), shape=(tile,))
cond = condition0 if condition0 else 100
xi += cond
ct.store(x, index=(i,), tile=xi)
@staticmethod
def if_else_assignment_type_match_ref(
x, condition0: int
):
return x + condition0 if condition0 else x + 100
@pytest.mark.parametrize("condition0", [5, 0])
def test_if_else_assignment_type_match(self, condition0):
N = 256
tile = 128
x = torch.zeros(N, dtype=torch.float32, device='cuda')
ref = torch.zeros_like(x)
grid = ((N // tile), 1, 1)
ct.launch(torch.cuda.current_stream(), grid, self.if_else_assignment_type_match,
(x, condition0, tile))
ref = self.if_else_assignment_type_match_ref(ref, condition0)
assert_equal(x, ref)
@staticmethod
@ct.kernel
def chain_comparison(
x, left, right, tile: ct.Constant[int]
):
i = ct.bid(0)
xi = ct.load(x, index=(i,), shape=(tile,))
if left < i < right:
xi += 1
ct.store(x, index=(i,), tile=xi)
@staticmethod
def chain_comparison_ref(
x, left, right, N, tile
):
for i in range(N):
tile_id = i // tile
if left < tile_id < right:
x[i] += 1
return x
def test_chain_comparison(self):
N = 256
tile = 128
x = torch.zeros(N, dtype=torch.float32, device='cuda')
ref = torch.zeros_like(x)
left = 0
right = 2
grid = ((N // tile), 1, 1)
ct.launch(torch.cuda.current_stream(), grid, self.chain_comparison,
(x, left, right, tile))
ref = self.chain_comparison_ref(ref, left, right, N, tile)
assert_equal(x, ref)
@staticmethod
@ct.kernel
def tuple_if_else(x):
t = ct.load(x, index=(0,), shape=(1,))
if ct.bid(0) > 0:
a = t, t
else:
a = t + 3, t + 5
ct.store(x, index=(1,), tile=a[0])
ct.store(x, index=(2,), tile=a[1])
def test_if_else_tuple_result(self):
x = torch.ones(3, dtype=torch.float32, device="cuda")
ct.launch(torch.cuda.current_stream(), (1,), self.tuple_if_else, (x,))
assert x.cpu().numpy()[1] == 4.0
assert x.cpu().numpy()[2] == 6.0
@staticmethod
@ct.kernel
def array_if_else(x, y):
if ct.bid(0) == 0:
a = x
else:
a = y
tile = ct.full((1,), ct.bid(0) + 10, dtype=x.dtype)
ct.store(a, index=(0,), tile=tile)
def test_if_else_array_result(self):
x = torch.zeros([1], dtype=torch.int32, device="cuda")
y = torch.zeros([1], dtype=torch.int32, device="cuda")
ct.launch(torch.cuda.current_stream(), (2,), self.array_if_else, (x, y))
assert x.cpu().numpy()[0] == 10
assert y.cpu().numpy()[0] == 11
class TestMixedControlFlow:
@staticmethod
@ct.kernel
def plus_n_skip_2(x, n, tile: ct.Constant[int]):
i = ct.bid(0)
xi = ct.load(x, index=(i,), shape=(tile,))
for index in range(n):
if index == 2:
continue
xi += 1
ct.store(x, index=(i,), tile=xi)
@staticmethod
def plus_n_skip_2_ref(x, n):
for index in range(n):
if index == 2:
continue
x += 1
@pytest.mark.parametrize("n", [5, 1])
def test_basic_continue(self, n):
N = 256
tile = 128
x = torch.zeros(N, dtype=torch.float32, device='cuda')
ref = torch.zeros_like(x)
grid = ((N // tile), 1, 1)
ct.launch(torch.cuda.current_stream(), grid, self.plus_n_skip_2, (x, n, tile))
self.plus_n_skip_2_ref(ref, n)
assert_equal(x, ref)
@staticmethod
@ct.kernel
def more_nested_control_flow(x, n, cond: bool, tile: ct.Constant[int]):
i = ct.bid(0)
xi = ct.load(x, index=(i,), shape=(tile,))
for index in range(n):
if cond:
xi += 1
for index2 in range(n):
if index2 == 2:
continue
else:
xi += 1
ct.store(x, index=(i,), tile=xi)
@staticmethod
def more_nested_control_flow_ref(x, n, cond: bool):
for index in range(n):
if cond:
x += 1
for index2 in range(n):
if index2 == 2:
continue
else:
x += 1
@pytest.mark.parametrize("n", [5, 1])
def test_more_nested_control_flow(self, n):
N = 256
tile = 128
x = torch.zeros(N, dtype=torch.float32, device='cuda')
ref = torch.zeros_like(x)
grid = ((N // tile), 1, 1)
cond = False
ct.launch(torch.cuda.current_stream(), grid, self.more_nested_control_flow,
(x, n, cond, tile))
self.more_nested_control_flow_ref(ref, n, cond)
assert_equal(x, ref)
@staticmethod
@ct.kernel
def switch_cases_kernel(x, option: int, tile: ct.Constant[int]):
i = ct.bid(0)
xi = ct.load(x, index=(i,), shape=(tile,))
match option:
case 0:
xi = xi
case 1:
xi += 1
case 2:
xi += 2
case 3:
xi += 3
ct.store(x, index=(i,), tile=xi)
def test_switch_cases(self):
pytest.xfail("TODO: Unsupported syntax `match`")
N = 256
tile = 128
x = torch.zeros(N, dtype=torch.float32, device='cuda')
ref = torch.zeros_like(x)
grid = ((N // tile), 1, 1)
option = 1
ct.launch(torch.cuda.current_stream(), grid, self.switch_cases_kernel, (x, option, tile))
ref += 1
assert_equal(x, ref)
class TestUndefinedVariable:
@staticmethod
@ct.kernel
def valid_undefined_variable(x, y, tile: ct.Constant[int]):
i = ct.bid(0)
xi = ct.load(x, index=(i,), shape=(tile,))
if i == 0:
# acc is undefined variable, but not used after the if statement
acc = ct.load(x, index=(i,), shape=(tile,))
yi = acc + xi
else:
yi = xi
ct.store(y, index=(i,), tile=yi)
def test_valid_undefined_variable(self):
N = 256
tile = 128
x = torch.ones(N, dtype=torch.float32, device='cuda')
y = torch.zeros(N, dtype=torch.float32, device='cuda')
grid = ((N // tile), 1, 1)
ct.launch(torch.cuda.current_stream(), grid, self.valid_undefined_variable, (x, y, tile))
ref = torch.ones_like(x)
ref[:tile] += 1
assert_equal(y, ref)
@staticmethod
@ct.kernel
def valid_undefined_variable_in_loop(x, y, tile: ct.Constant[int]):
i = ct.bid(0)
xi = ct.load(x, index=(i,), shape=(tile,))
for _ in range(10):
if i == 0:
# acc is undefined variable, but not used after the if statement
acc = ct.load(x, index=(i,), shape=(tile,))
yi = acc + xi
else:
yi = xi
ct.store(y, index=(i,), tile=yi)
def test_valid_undefined_variable_in_loop(self):
N = 256
tile = 128
x = torch.ones(N, dtype=torch.float32, device='cuda')
y = torch.zeros(N, dtype=torch.float32, device='cuda')
grid = ((N // tile), 1, 1)
ct.launch(torch.cuda.current_stream(), grid, self.valid_undefined_variable, (x, y, tile))
ref = torch.ones_like(x)
ref[:tile] += 1
assert_equal(y, ref)
@staticmethod
@ct.kernel
def invalid_undefined_variable(x, y, tile: ct.Constant[int]):
i = ct.bid(0)
xi = ct.load(x, index=(i,), shape=(tile,))
if i == 0:
# acc is undefined variable, and is used after the if statement
acc = ct.load(y, index=(i,), shape=(tile,))
yi = acc + xi
else:
yi = xi
yi += acc
ct.store(y, index=(i,), tile=yi)
def test_invalid_undefined_variable(self):
N = 256
tile = 128
x = torch.zeros(N, dtype=torch.float32, device='cuda')
y = torch.zeros(N, dtype=torch.float32, device='cuda')
grid = ((N // tile), 1, 1)
with pytest.raises(TileTypeError):
ct.launch(torch.cuda.current_stream(), grid, self.invalid_undefined_variable,
(x, y, tile))
@staticmethod
@ct.kernel
def same_constant_both_branches(x):
if ct.bid(0) == 1:
a = 1
else:
a = 1
# Use `a` as tile shape to make sure it is inferred as constant
t = ct.full((a,), 3.0, x.dtype)
ct.store(x, (0,), t)
def test_same_constant_both_branches(self):
x = torch.zeros(1, dtype=torch.float32, device='cuda')
ct.launch(torch.cuda.current_stream(), (1,), self.same_constant_both_branches, (x,))
assert x.cpu().item() == 3.0
@staticmethod
@ct.kernel
def different_constant_two_branches(x):
if ct.bid(0) == 1:
a = 1
else:
a = 2
# This should raise a type error because a non-constant value is used as tile shape
t = ct.full((a,), 3.0, x.dtype)
ct.store(x, (0,), t)
def test_different_constant_two_branches(self):
x = torch.zeros(1, dtype=torch.float32, device='cuda')
with pytest.raises(TileTypeError,
match='Invalid argument "shape" of full\\(\\): Expected a const'):
ct.launch(torch.cuda.current_stream(), (1,), self.different_constant_two_branches, (x,))
@staticmethod
@ct.kernel
def loop3_kernel(
input,
output,
):
bid_d = ct.bid(0)
bid_h = ct.bid(1)
bid_w = ct.bid(2)
max_val = ct.full((1, 1, 1), ct.float32("-inf"), dtype=ct.float32)
for d_idx in range(1):
for h_idx in range(1):
for w_idx in range(1):
if d_idx >= 0 and h_idx >= 0 and w_idx >= 0:
val = ct.load(
input,
index=(d_idx, h_idx, w_idx),
shape=(1, 1, 1),
)
max_val = max(max_val, val)
ct.store(output, index=(bid_d, bid_h, bid_w), tile=max_val)
def test_3loops(self):
depth, height, width = 8, 16, 16
input = torch.randn(depth, height, width, dtype=torch.float32, device="cuda")
output = torch.zeros_like(input)
ct.launch(torch.cuda.current_stream(), (1,), self.loop3_kernel, (input, output))
@ct.kernel
def early_return_kernel(x, y, output,
B: ct.Constant[int], N: ct.Constant[int], early_return: bool):
px = ct.bid(0)
tile_x = ct.load(x, index=(px, 0), shape=(B, N))
tile_y = ct.load(y, index=(px, 0), shape=(B, 1))
if early_return:
return
out = tile_x + tile_y
ct.store(output, index=(px, 0), tile=out)