forked from facebookexperimental/triton
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_tlx.py
More file actions
6627 lines (5526 loc) · 258 KB
/
test_tlx.py
File metadata and controls
6627 lines (5526 loc) · 258 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
import math
import itertools
import pytest
import torch
import re
import triton
import triton.language as tl
from triton._internal_testing import is_hopper_or_newer, is_blackwell, is_hopper, is_hip
import triton.language.extra.tlx as tlx
from typing import Optional
import traceback
import triton.runtime.driver as driver
from triton.tools.tensor_descriptor import TensorDescriptor
@pytest.mark.skipif(not is_hopper_or_newer(), reason="Need Hopper or newer")
@pytest.mark.parametrize("BLOCK_SIZE", [(1024)])
def test_async_tasks(BLOCK_SIZE, device):
@triton.jit
def add2_warp_specialized_kernel(
x_ptr,
y_ptr,
z_ptr,
a_ptr,
b_ptr,
c_ptr,
n_elements,
BLOCK_SIZE: tl.constexpr,
):
pid = tl.program_id(axis=0)
block_start = pid * BLOCK_SIZE
with tlx.async_tasks():
with tlx.async_task("default", registers=120, replicate=2):
offsets = block_start + tl.arange(0, BLOCK_SIZE)
mask = offsets < n_elements
x = tl.load(x_ptr + offsets, mask=mask)
y = tl.load(y_ptr + offsets, mask=mask)
replica_id = tlx.async_task_replica_id()
x1 = x + replica_id
y1 = y - replica_id
output = x1 + y1
tl.store(z_ptr + offsets, output, mask=mask)
with tlx.async_task(num_warps=1, registers=100, replicate=2):
offsets = block_start + tl.arange(0, BLOCK_SIZE)
mask = offsets < n_elements
a = tl.load(a_ptr + offsets, mask=mask)
b = tl.load(b_ptr + offsets, mask=mask)
replica_id = tlx.async_task_replica_id()
# This no-op is just to test that replica_id
# is correctly passed to the kernel
a1 = a + replica_id
b1 = b - replica_id
output = a1 + b1
tl.store(c_ptr + offsets, output, mask=mask)
def dual_add(x, y, a, b):
return x + y, a + b
torch.manual_seed(0)
size = 98432
x = torch.rand(size, device=device)
y = torch.rand(size, device=device)
a = torch.rand(size, device=device)
b = torch.rand(size, device=device)
output1 = torch.empty_like(x)
output2 = torch.empty_like(a)
n_elements = output1.numel()
grid = lambda meta: (triton.cdiv(n_elements, meta["BLOCK_SIZE"]), )
kernel = add2_warp_specialized_kernel[grid](
x,
y,
output1,
a,
b,
output2,
n_elements,
BLOCK_SIZE,
num_warps=4,
)
ttgir = kernel.asm["ttgir"]
# print(ttgir)
pattern_ws = r"ttg.warp_specialize(.*) attributes {requestedRegisters = array<i32: 120, 100, 100>}"
assert re.search(pattern_ws, ttgir, flags=re.DOTALL)
pattern_p0 = r"partition0\([^\n]*\)\s+num_warps\(4\)"
assert re.search(pattern_p0, ttgir, flags=re.DOTALL)
pattern_p1 = r"partition1\([^\n]*\)\s+num_warps\(1\)"
assert re.search(pattern_p1, ttgir, flags=re.DOTALL)
pattern_p2 = r"partition2\([^\n]*\)\s+num_warps\(1\)"
assert re.search(pattern_p2, ttgir, flags=re.DOTALL)
# Check that the replica_id is correctly passed to non-default regions
# TTIR/TTGIR should be something like:
# partition0(...) {
# %a1 = arith.constant dense<0.000000e+00> : tensor<1024xf32, #blocked>
# ...
# %13 = arith.addf %9, %cst
# ...}
# partition1(...) {
# %cst = arith.constant dense<1.000000e+00> : tensor<1024xf32, #blocked>
# ...
# %13 = arith.addf %9, %cst
# %14 = arith.subf %12, %cst
# ...}
pattern_cst = r"= arith.constant dense\<.*\>"
found = re.findall(pattern_cst, ttgir)
assert len(found) == 4, "Expected 4 cst by calling `tlx.async_task_replica_id()` in all regions"
assert found[0] != found[1], "Two matches MUST be different"
assert "dense<0.0" in found[0] and "dense<1.0" in found[1], "Expected 0.0 and 1.0 as replica_id"
ref_out1, ref_out2 = dual_add(x, y, a, b)
torch.testing.assert_close(output1, ref_out1, check_dtype=False)
torch.testing.assert_close(output2, ref_out2, check_dtype=False)
@pytest.mark.skipif(not is_hopper_or_newer(), reason="Need Hopper or newer")
@pytest.mark.parametrize("BLOCK_SIZE", [(1024)])
@pytest.mark.parametrize("ENABLE_SECOND_TASK", [True, False])
def test_async_tasks_constexpr_guard(BLOCK_SIZE, ENABLE_SECOND_TASK, device):
"""Test that a tl.constexpr if-check can guard an async_task within async_tasks.
The first async_task (default) is always present. The second async_task
is conditionally included based on the ENABLE_SECOND_TASK constexpr flag.
Both configurations should produce the correct result.
"""
@triton.jit
def add_kernel_conditional_task(
x_ptr,
y_ptr,
z_ptr,
a_ptr,
b_ptr,
c_ptr,
n_elements,
BLOCK_SIZE: tl.constexpr,
ENABLE_SECOND_TASK: tl.constexpr,
):
pid = tl.program_id(axis=0)
block_start = pid * BLOCK_SIZE
with tlx.async_tasks():
with tlx.async_task("default", registers=120):
offsets = block_start + tl.arange(0, BLOCK_SIZE)
mask = offsets < n_elements
x = tl.load(x_ptr + offsets, mask=mask)
y = tl.load(y_ptr + offsets, mask=mask)
output = x + y
tl.store(z_ptr + offsets, output, mask=mask)
if ENABLE_SECOND_TASK:
with tlx.async_task(num_warps=1, registers=100):
offsets = block_start + tl.arange(0, BLOCK_SIZE)
mask = offsets < n_elements
a = tl.load(a_ptr + offsets, mask=mask)
b = tl.load(b_ptr + offsets, mask=mask)
output = a + b
tl.store(c_ptr + offsets, output, mask=mask)
torch.manual_seed(0)
size = 98432
x = torch.rand(size, device=device)
y = torch.rand(size, device=device)
a = torch.rand(size, device=device)
b = torch.rand(size, device=device)
output_z = torch.empty_like(x)
output_c = torch.empty_like(a)
n_elements = output_z.numel()
grid = lambda meta: (triton.cdiv(n_elements, meta["BLOCK_SIZE"]), )
kernel = add_kernel_conditional_task[grid](
x,
y,
output_z,
a,
b,
output_c,
n_elements,
BLOCK_SIZE,
ENABLE_SECOND_TASK,
num_warps=4,
)
ttgir = kernel.asm["ttgir"]
if ENABLE_SECOND_TASK:
assert re.search(r"ttg.warp_specialize",
ttgir), ("Expected warp_specialize in TTGIR when ENABLE_SECOND_TASK=True")
assert re.search(r"partition0\([^\n]*\)\s+num_warps\(1\)", ttgir,
flags=re.DOTALL), ("Expected partition0 with num_warps(1) when ENABLE_SECOND_TASK=True")
else:
assert not re.search(r"ttg.warp_specialize",
ttgir), ("Did not expect warp_specialize in TTGIR when ENABLE_SECOND_TASK=False")
torch.testing.assert_close(output_z, x + y, check_dtype=False)
if ENABLE_SECOND_TASK:
torch.testing.assert_close(output_c, a + b, check_dtype=False)
@pytest.mark.skipif(not is_hopper_or_newer(), reason="Need Hopper or newer")
@pytest.mark.parametrize("BLOCK_SIZE", [(1024)])
@pytest.mark.parametrize("USE_LARGE_DEFAULT", [True, False])
def test_async_tasks_constexpr_select_default(BLOCK_SIZE, USE_LARGE_DEFAULT, device):
"""Test that a constexpr if/else can select between two different default tasks.
Both branches of the if/else contain a default async_task, but only one
survives constexpr resolution. This exercises the num_default == 1 assertion
which must hold after resolution, not before.
"""
@triton.jit
def kernel_select_default(
x_ptr,
y_ptr,
z_ptr,
a_ptr,
b_ptr,
c_ptr,
n_elements,
BLOCK_SIZE: tl.constexpr,
USE_LARGE_DEFAULT: tl.constexpr,
):
pid = tl.program_id(axis=0)
block_start = pid * BLOCK_SIZE
with tlx.async_tasks():
if USE_LARGE_DEFAULT:
with tlx.async_task("default", warp_group_start_id=0):
offsets = block_start + tl.arange(0, BLOCK_SIZE)
mask = offsets < n_elements
x = tl.load(x_ptr + offsets, mask=mask)
y = tl.load(y_ptr + offsets, mask=mask)
tl.store(z_ptr + offsets, x + y, mask=mask)
else:
with tlx.async_task("default", warp_group_start_id=1):
offsets = block_start + tl.arange(0, BLOCK_SIZE)
mask = offsets < n_elements
x = tl.load(x_ptr + offsets, mask=mask)
y = tl.load(y_ptr + offsets, mask=mask)
tl.store(z_ptr + offsets, x * y, mask=mask)
with tlx.async_task(num_warps=1, registers=100):
offsets = block_start + tl.arange(0, BLOCK_SIZE)
mask = offsets < n_elements
a = tl.load(a_ptr + offsets, mask=mask)
b = tl.load(b_ptr + offsets, mask=mask)
tl.store(c_ptr + offsets, a + b, mask=mask)
torch.manual_seed(0)
size = 98432
x = torch.rand(size, device=device)
y = torch.rand(size, device=device)
a = torch.rand(size, device=device)
b = torch.rand(size, device=device)
output_z = torch.empty_like(x)
output_c = torch.empty_like(a)
n_elements = output_z.numel()
grid = lambda meta: (triton.cdiv(n_elements, meta["BLOCK_SIZE"]), )
kernel = kernel_select_default[grid](
x,
y,
output_z,
a,
b,
output_c,
n_elements,
BLOCK_SIZE,
USE_LARGE_DEFAULT,
num_warps=4,
)
ttgir = kernel.asm["ttgir"]
assert re.search(r"ttg.warp_specialize", ttgir), "Expected warp_specialize in TTGIR"
# Verify the non-default task always ran (a + b → c)
torch.testing.assert_close(output_c, a + b, check_dtype=False)
# Verify which default was selected by the constexpr condition
if USE_LARGE_DEFAULT:
torch.testing.assert_close(output_z, x + y, check_dtype=False)
else:
torch.testing.assert_close(output_z, x * y, check_dtype=False)
@pytest.mark.skipif(not is_hopper_or_newer(), reason="Need Hopper or newer")
@pytest.mark.parametrize("BLOCK_SIZE", [(64)])
def test_local_load(BLOCK_SIZE, device):
@triton.jit
def local_load(
x_ptr,
y_ptr,
output_ptr,
n_elements,
BLOCK_SIZE: tl.constexpr,
):
pid = tl.program_id(axis=0)
block_start = pid * BLOCK_SIZE
offsets = block_start + tl.arange(0, BLOCK_SIZE)
mask = offsets < n_elements
x_ptr_offsets = x_ptr + offsets
y_ptr_offsets = y_ptr + offsets
buffers = tlx.local_alloc((BLOCK_SIZE, ), tl.float32, 3)
tlx.async_load(x_ptr_offsets, buffers[0], mask=mask)
tlx.async_load(y_ptr_offsets, buffers[1], mask=mask)
tlx.async_load_commit_group()
tlx.async_load_wait_group(tl.constexpr(0))
x_local = tlx.local_load(buffers[0])
y_local = tlx.local_load(buffers[1])
local_add = x_local + y_local
tl.store(output_ptr + offsets, local_add, mask=mask)
torch.manual_seed(0)
size = 256
x = torch.rand(size, dtype=torch.float32, device=device)
y = torch.rand(size, dtype=torch.float32, device=device)
output = torch.empty_like(x)
n_elements = x.numel()
grid = lambda meta: (triton.cdiv(n_elements, meta["BLOCK_SIZE"]), )
kernel = local_load[grid](x, y, output, n_elements, BLOCK_SIZE)
assert kernel.asm["ttgir"].count("ttg.local_alloc") == 1
assert kernel.asm["ttgir"].count("ttg.memdesc_index") == 2
assert kernel.asm["ttgir"].count("ttg.async_copy_global_to_local") == 2
assert kernel.asm["ttgir"].count("ttg.async_commit_group") == 1
assert kernel.asm["ttgir"].count("ttg.async_wait") == 1
assert kernel.asm["ttgir"].count("ttg.local_load") == 2
torch.testing.assert_close(x + y, output)
@pytest.mark.skipif(not is_hopper_or_newer(), reason="Need Hopper or newer")
@pytest.mark.parametrize("BLOCK_SIZE", [(4)])
def test_local_slice(BLOCK_SIZE, device):
@triton.jit
def local_load(
x_ptr,
output_ptr,
n_elements,
BLOCK_SIZE: tl.constexpr,
):
pid = tl.program_id(axis=0)
block_start = pid * BLOCK_SIZE
offsets = block_start + tl.arange(0, BLOCK_SIZE)
x_ptr_offsets = x_ptr + offsets
buffers = tlx.local_alloc((BLOCK_SIZE, ), tl.float32, 1)
tlx.async_load(x_ptr_offsets, buffers[0])
tlx.async_load_commit_group()
tlx.async_load_wait_group(tl.constexpr(0))
buffer_0 = tlx.local_slice(buffers[0], [0], [BLOCK_SIZE // 2])
buffer_1 = tlx.local_slice(buffers[0], [BLOCK_SIZE // 2], [BLOCK_SIZE // 2])
x_0 = tlx.local_load(buffer_0)
x_1 = tlx.local_load(buffer_1)
offsets = block_start + tl.arange(0, BLOCK_SIZE // 2)
output_ptr_offsets = output_ptr + offsets
tl.store(output_ptr_offsets, x_0)
tl.store(output_ptr_offsets + BLOCK_SIZE // 2, x_1)
torch.manual_seed(0)
size = 4
x = torch.rand(size, dtype=torch.float32, device=device)
output = torch.empty_like(x)
n_elements = x.numel()
grid = lambda meta: (triton.cdiv(n_elements, meta["BLOCK_SIZE"]), )
kernel = local_load[grid](x, output, n_elements, BLOCK_SIZE)
assert kernel.asm["ttgir"].count("ttg.local_alloc") == 1
assert kernel.asm["ttgir"].count("ttg.memdesc_index") == 1
assert kernel.asm["ttgir"].count("ttg.async_copy_global_to_local") == 1
assert kernel.asm["ttgir"].count("ttg.async_commit_group") == 1
assert kernel.asm["ttgir"].count("ttg.async_wait") == 1
assert kernel.asm["ttgir"].count("ttg.local_load") == 2
torch.testing.assert_close(x, output)
def _generate_test_params():
"""Generate test parameters with filtering for memory constraints."""
dims_mn = [16, 32, 64, 128, 512]
dims_k = [16, 32, 64]
dtype = torch.float16
params = []
for M, N, K in itertools.product(dims_mn, dims_mn, dims_k):
device_props = str(torch.cuda.get_device_properties())
matmul_size = (M * K + K * N) * dtype.itemsize
max_shared_mem = driver.active.utils.get_device_properties(driver.active.get_current_device())["max_shared_mem"]
if matmul_size > max_shared_mem:
continue
# TODO: Investigate why this test fails on gfx942 with M=512, N=512, K=16
if "gfx942" in device_props and M == 512 and N == 512 and K == 16:
params.append(pytest.param(M, N, K, marks=pytest.mark.xfail()))
elif "H100" in device_props and M == 512 and N == 512 and K == 64:
# This shape incurs excessive register pressure and fails on H100
params.append(pytest.param(M, N, K, marks=pytest.mark.xfail()))
else:
params.append((M, N, K))
return params
# Test tl.dot wit tlx smem ops
# Tests tl.load->tlx_local_store->tlx_local_load->tl.dot
@pytest.mark.skipif(is_blackwell(), reason="Not tested on Blackwell")
@pytest.mark.parametrize("M,N,K", _generate_test_params())
def test_tl_dot_with_tlx_smem_load_store(M, N, K, device):
@triton.jit
def dot_kernel(
X,
stride_xm,
stride_xk,
Y,
stride_yk,
stride_yn,
Z,
stride_zm,
stride_zn,
BLOCK_M: tl.constexpr,
BLOCK_N: tl.constexpr,
BLOCK_K: tl.constexpr,
):
off_m = tl.arange(0, BLOCK_M)
off_n = tl.arange(0, BLOCK_N)
off_k = tl.arange(0, BLOCK_K)
a_ptrs = X + (off_m[:, None] * stride_xm + off_k[None, :] * stride_xk)
b_ptrs = Y + (off_k[:, None] * stride_yk + off_n[None, :] * stride_yn)
buf_alloc_a = tlx.local_alloc((BLOCK_M, BLOCK_K), tlx.dtype_of(X), 1)
buf_alloc_b = tlx.local_alloc((BLOCK_K, BLOCK_N), tlx.dtype_of(Y), 1)
a_smem_view = buf_alloc_a[0]
b_smem_view = buf_alloc_b[0]
a_load_reg = tl.load(a_ptrs)
b_load_reg = tl.load(b_ptrs)
tlx.local_store(a_smem_view, a_load_reg)
tlx.local_store(b_smem_view, b_load_reg)
a_tile = tlx.local_load(a_smem_view)
b_tile = tlx.local_load(b_smem_view)
c_tile = tl.dot(a_tile, b_tile)
c = c_tile.to(tlx.dtype_of(Z))
c_ptrs = Z + stride_zm * off_m[:, None] + stride_zn * off_n[None, :]
tl.store(c_ptrs, c)
torch.manual_seed(0)
# Note: This test may fail for other shapes/kwargs until
# reg->shared layout propagation is implemented tlx layout propagation
dtype = torch.float16
print(f"{M=}, {N=}, {K=}")
x = torch.randn((M, K), device=device, dtype=dtype)
y = torch.randn((K, N), device=device, dtype=dtype)
z = torch.zeros((M, N), device=device, dtype=dtype)
# test smem
kern_kwargs = {"BLOCK_M": M, "BLOCK_K": K, "BLOCK_N": N}
dot_kernel[(1, 1)](
x,
x.stride(0),
x.stride(1),
y,
y.stride(0),
y.stride(1),
z,
z.stride(0),
z.stride(1),
**kern_kwargs,
)
z_ref = torch.matmul(x, y)
torch.testing.assert_close(z, z_ref)
# Tests tl.load->tlx_local_store->tlx_local_load
# This is a smem load/store test variant that does not use
# async_load, so this test can be run on platforms where
# async_load has no/limited support
@pytest.mark.parametrize("BLOCK_SIZE", [(64)])
def test_load_store_smem_with_tl_load(BLOCK_SIZE, device):
@triton.jit
def smem_reg_store_load(
x_ptr,
y_ptr,
output_ptr,
n_elements,
BLOCK_SIZE: tl.constexpr,
):
pid = tl.program_id(axis=0)
block_start = pid * BLOCK_SIZE
offsets = block_start + tl.arange(0, BLOCK_SIZE)
mask = offsets < n_elements
smem_buffers = tlx.local_alloc((BLOCK_SIZE, ), tl.float32, 3)
x_smem = tlx.local_view(smem_buffers, 0)
y_smem = tlx.local_view(smem_buffers, 1)
x_tile = tl.load(x_ptr + offsets, mask=mask)
y_tile = tl.load(y_ptr + offsets, mask=mask)
tlx.local_store(x_smem, x_tile)
tlx.local_store(y_smem, y_tile)
x_reg = tlx.local_load(x_smem)
y_reg = tlx.local_load(y_smem)
local_add = x_reg + y_reg
tl.store(output_ptr + offsets, local_add, mask=mask)
torch.manual_seed(0)
size = 256
x = torch.rand(size, dtype=torch.float32, device=device)
y = torch.rand(size, dtype=torch.float32, device=device)
output = torch.empty_like(x)
n_elements = x.numel()
grid = lambda meta: (triton.cdiv(n_elements, meta["BLOCK_SIZE"]), )
kernel = smem_reg_store_load[grid](x, y, output, n_elements, BLOCK_SIZE)
assert kernel.asm["ttgir"].count("ttg.local_alloc") == 1
assert kernel.asm["ttgir"].count("ttg.memdesc_index") == 2
assert kernel.asm["ttgir"].count("ttg.local_load") == 2
assert kernel.asm["ttgir"].count("ttg.local_store") == 2
torch.testing.assert_close(x + y, output)
@pytest.mark.skipif(not is_hopper_or_newer(), reason="Need Hopper or newer")
@pytest.mark.parametrize("BLOCK_SIZE", [(64)])
def test_local_store(BLOCK_SIZE, device):
@triton.jit
def local_load_store(
x_ptr,
y_ptr,
output_ptr,
n_elements,
BLOCK_SIZE: tl.constexpr,
):
pid = tl.program_id(axis=0)
block_start = pid * BLOCK_SIZE
offsets = block_start + tl.arange(0, BLOCK_SIZE)
mask = offsets < n_elements
x_ptr_offsets = x_ptr + offsets
y_ptr_offsets = y_ptr + offsets
buffers = tlx.local_alloc((BLOCK_SIZE, ), tl.float32, tl.constexpr(4))
buffer0 = tlx.local_view(buffers, 0)
buffer1 = tlx.local_view(buffers, 1)
buffer2 = tlx.local_view(buffers, 2)
tlx.async_load(x_ptr_offsets, buffer0, mask=mask)
tlx.async_load(y_ptr_offsets, buffer1, mask=mask)
tlx.async_load_commit_group()
tlx.async_load_wait_group(tl.constexpr(0))
x_local = tlx.local_load(buffer0)
y_local = tlx.local_load(buffer1)
local_add = x_local + y_local
# store result into buffer2 and then load it
tlx.local_store(buffer2, local_add)
result = tlx.local_load(buffer2)
tl.store(output_ptr + offsets, result, mask=mask)
torch.manual_seed(0)
size = 256
x = torch.rand(size, dtype=torch.float32, device=device)
y = torch.rand(size, dtype=torch.float32, device=device)
output = torch.empty_like(x)
n_elements = x.numel()
grid = lambda meta: (triton.cdiv(n_elements, meta["BLOCK_SIZE"]), )
kernel = local_load_store[grid](x, y, output, n_elements, BLOCK_SIZE)
assert kernel.asm["ttgir"].count("ttg.local_alloc") == 1
assert kernel.asm["ttgir"].count("ttg.memdesc_index") == 3
assert kernel.asm["ttgir"].count("ttg.async_copy_global_to_local") == 2
assert kernel.asm["ttgir"].count("ttg.async_commit_group") == 1
assert kernel.asm["ttgir"].count("ttg.async_wait") == 1
assert kernel.asm["ttgir"].count("ttg.local_load") == 3
assert kernel.asm["ttgir"].count("ttg.local_store") == 1
torch.testing.assert_close(x + y, output)
@pytest.mark.skipif(not is_blackwell(), reason="Need Blackwell")
@pytest.mark.parametrize("BLOCK_SIZE", [(64)])
def test_tmem_alloc_index(BLOCK_SIZE, device):
@triton.jit
def kernel(BLOCK_SIZE: tl.constexpr, ):
buffers = tlx.local_alloc((BLOCK_SIZE, BLOCK_SIZE), tl.float32, tl.constexpr(2), tlx.storage_kind.tmem)
buffer0 = tlx.local_view(buffers, 0) # noqa: F841
buffer1 = tlx.local_view(buffers, 1) # noqa: F841
grid = lambda meta: (1, )
kerenl_info = kernel[grid](BLOCK_SIZE)
# TODO: check numerics once tmem load/store is ready
kerenl_info.asm["ttgir"]
assert kerenl_info.asm["ttgir"].count("kernel") == 1
@pytest.mark.skipif(not is_blackwell(), reason="Need Blackwell")
@pytest.mark.parametrize("BLOCK_SIZE_M, BLOCK_SIZE_N", [(64, 64), (64, 8), (128, 16)])
def test_tmem_load_store(BLOCK_SIZE_M, BLOCK_SIZE_N, device):
@triton.jit
def tmem_load_store_kernel(
x_ptr,
stride_m,
stride_n,
BLOCK_SIZE_M: tl.constexpr,
BLOCK_SIZE_N: tl.constexpr,
):
offs_m = tl.arange(0, BLOCK_SIZE_M)
offs_n = tl.arange(0, BLOCK_SIZE_N)
x_ptr_offsets = x_ptr + (offs_m[:, None] * stride_m + offs_n[None, :] * stride_n)
a = tl.full((BLOCK_SIZE_M, BLOCK_SIZE_N), 1.0, tl.float32)
buffers = tlx.local_alloc((BLOCK_SIZE_M, BLOCK_SIZE_N), tl.float32, tl.constexpr(1), tlx.storage_kind.tmem)
buffer1 = tlx.local_view(buffers, 0)
tlx.local_store(buffer1, a)
b = tlx.local_load(buffer1)
# b == a == tensor of 1.0
tl.store(x_ptr_offsets, b + 2)
x = torch.rand((BLOCK_SIZE_M, BLOCK_SIZE_N), dtype=torch.float32, device=device)
grid = lambda meta: (1, )
kerenl_info = tmem_load_store_kernel[grid](x, x.stride(0), x.stride(1), BLOCK_SIZE_M, BLOCK_SIZE_N)
assert kerenl_info.asm["ttir"].count("ttng.tmem_store") == 1
assert kerenl_info.asm["ttir"].count("ttng.tmem_load") == 1
assert kerenl_info.asm["ttgir"].count("kernel") == 1
assert kerenl_info.asm["ttgir"].count("ttng.tmem_alloc") == 1
assert kerenl_info.asm["ttgir"].count("ttng.tmem_store") == 1
assert kerenl_info.asm["ttgir"].count("ttng.tmem_load") == 1
ref_out = torch.ones_like(x) + 2
torch.testing.assert_close(x, ref_out)
@pytest.mark.skipif(not is_blackwell(), reason="Need Blackwell")
@pytest.mark.parametrize("BLOCK_SIZE_M, BLOCK_SIZE_N", [(128, 64)])
def test_tmem_subslice(BLOCK_SIZE_M, BLOCK_SIZE_N, device):
@triton.jit
def tmem_subslice_kernel(
x_ptr,
stride_m,
stride_n,
BLOCK_SIZE_M: tl.constexpr,
BLOCK_SIZE_N: tl.constexpr,
):
offs_m = tl.arange(0, BLOCK_SIZE_M)
offs_n1 = tl.arange(0, BLOCK_SIZE_N // 4)
offs_n2 = tl.arange(BLOCK_SIZE_N // 4, BLOCK_SIZE_N // 2)
offs_n3 = tl.arange(BLOCK_SIZE_N // 2, 3 * BLOCK_SIZE_N // 4)
offs_n4 = tl.arange(3 * BLOCK_SIZE_N // 4, BLOCK_SIZE_N)
x_ptr_offsets1 = x_ptr + (offs_m[:, None] * stride_m + offs_n1[None, :] * stride_n)
x_ptr_offsets2 = x_ptr + (offs_m[:, None] * stride_m + offs_n2[None, :] * stride_n)
x_ptr_offsets3 = x_ptr + (offs_m[:, None] * stride_m + offs_n3[None, :] * stride_n)
x_ptr_offsets4 = x_ptr + (offs_m[:, None] * stride_m + offs_n4[None, :] * stride_n)
a = tl.full((BLOCK_SIZE_M, BLOCK_SIZE_N), 1.0, tl.float32)
buffers = tlx.local_alloc((BLOCK_SIZE_M, BLOCK_SIZE_N), tl.float32, tl.constexpr(1), tlx.storage_kind.tmem)
buffer1 = tlx.local_view(buffers, 0)
tlx.local_store(buffer1, a)
subslice1 = tlx.subslice(buffer1, 0, BLOCK_SIZE_N // 4)
subslice2 = tlx.subslice(buffer1, BLOCK_SIZE_N // 4, BLOCK_SIZE_N // 4)
subslice3 = tlx.subslice(buffer1, BLOCK_SIZE_N // 2, BLOCK_SIZE_N // 4)
subslice4 = tlx.local_slice(buffer1, [0, 3 * BLOCK_SIZE_N // 4], [BLOCK_SIZE_M, BLOCK_SIZE_N // 4])
b1 = tlx.local_load(subslice1)
b2 = tlx.local_load(subslice2)
b3 = tlx.local_load(subslice3)
b4 = tlx.local_load(subslice4)
# b == a == tensor of 1.0
tl.store(x_ptr_offsets1, b1 + 2)
tl.store(x_ptr_offsets2, b2 + 2)
tl.store(x_ptr_offsets3, b3 + 2)
tl.store(x_ptr_offsets4, b4 + 2)
x = torch.rand((BLOCK_SIZE_M, BLOCK_SIZE_N), dtype=torch.float32, device=device)
grid = lambda meta: (1, )
kerenl_info = tmem_subslice_kernel[grid](x, x.stride(0), x.stride(1), BLOCK_SIZE_M, BLOCK_SIZE_N)
assert kerenl_info.asm["ttir"].count("ttng.tmem_store") == 1
assert kerenl_info.asm["ttir"].count("ttng.tmem_load") == 4
assert kerenl_info.asm["ttgir"].count("kernel") == 1
assert kerenl_info.asm["ttgir"].count("ttng.tmem_alloc") == 1
assert kerenl_info.asm["ttgir"].count("ttng.tmem_store") == 1
assert kerenl_info.asm["ttgir"].count("ttng.tmem_load") == 4
ref_out = torch.ones_like(x) + 2
torch.testing.assert_close(x, ref_out)
def test_thread_id(device):
@triton.jit
def store_from_thread_0_kernel(
output_ptr,
value,
n_elements,
axis: tl.constexpr,
BLOCK_SIZE: tl.constexpr,
):
pid = tl.program_id(axis=0)
block_start = pid * BLOCK_SIZE
offsets = block_start + tl.arange(0, BLOCK_SIZE)
mask = offsets < n_elements
tid = tlx.thread_id(axis)
if tid == 0:
tl.store(output_ptr + offsets, value, mask=mask)
output = torch.zeros(32, dtype=torch.int32, device="cuda")
n_elements = output.numel()
value = 42
store_from_thread_0_kernel[(1, )](output, value, n_elements, 0, 32, num_warps=1)
torch.cuda.synchronize()
expected_output = torch.zeros(32, dtype=torch.int32, device="cuda")
expected_output[0] = value
torch.testing.assert_close(output, expected_output)
@pytest.mark.skipif(not is_hopper_or_newer(), reason="Need Hopper or newer")
def test_custer_cta_rank(device):
@triton.jit
def test_cta_0_kernel(
output_ptr,
n_elements,
BLOCK_SIZE: tl.constexpr,
):
pid = tl.program_id(axis=0)
block_start = pid * BLOCK_SIZE
offsets = block_start + tl.arange(0, BLOCK_SIZE)
mask = offsets < n_elements
# without multi-cta cluster launch, this test does not validate much except
# the fact that the IR lowering flow works
cta_id = tlx.cluster_cta_rank()
tl.store(output_ptr + offsets, cta_id, mask=mask)
tensor_size = 32
# init with 1, expected to be filled with 0
output = torch.ones(tensor_size, dtype=torch.int32, device=device)
kernel = test_cta_0_kernel[(1, )](output, tensor_size, tensor_size, num_warps=1)
ttgir = kernel.asm["ttgir"]
assert ttgir.count("nvgpu.cluster_id") == 1
torch.cuda.synchronize()
expected_output = torch.zeros(tensor_size, dtype=torch.int32, device=device)
torch.testing.assert_close(output, expected_output)
@pytest.mark.skipif(not is_hopper_or_newer(), reason="Need Hopper or newer")
def test_clock64(device):
@triton.jit
def clock64_from_thread_0_kernel(
output_ptr,
value,
n_elements,
BLOCK_SIZE: tl.constexpr,
):
pid = tl.program_id(axis=0)
block_start = pid * BLOCK_SIZE
offsets = block_start + tl.arange(0, BLOCK_SIZE)
mask = offsets < n_elements
tid = tlx.thread_id(0)
if pid == 0 and tid == 0:
start = tlx.clock64()
tl.store(output_ptr + offsets, value, mask=mask)
end = tlx.clock64()
tl.device_print("Cycles elapsed: ", end - start)
output = torch.zeros(32, dtype=torch.int32, device="cuda")
n_elements = output.numel()
value = 42
kernel = clock64_from_thread_0_kernel[(1, )](output, value, n_elements, 32, num_warps=1)
assert kernel.asm["ttgir"].count("ttg.clock64") == 2
assert kernel.asm["ptx"].count("%clock64") == 2
@pytest.mark.skipif(not is_hopper_or_newer(), reason="Need Hopper or newer")
@pytest.mark.parametrize("BLOCK_SIZE", [(64)])
def test_async_wait(BLOCK_SIZE, device):
@triton.jit
def async_wait_kernel(
input_ptr,
output_ptr,
n_elements,
BLOCK_SIZE: tl.constexpr,
):
pid = tl.program_id(axis=0)
block_start = pid * BLOCK_SIZE
offsets = block_start + tl.arange(0, BLOCK_SIZE)
mask = offsets < n_elements
input_ptr_offsets = input_ptr + offsets
buffers = tlx.local_alloc((BLOCK_SIZE, ), tl.float32, tl.constexpr(1))
buffer = tlx.local_view(buffers, 0)
tlx.async_load(input_ptr_offsets, buffer, mask=mask)
tlx.async_load_commit_group()
tlx.async_load_wait_group(tl.constexpr(0))
x = tlx.local_load(buffer)
tl.store(output_ptr + offsets, x, mask=mask)
@triton.jit
def async_wait_token_kernel(
input_ptr,
output_ptr,
n_elements,
BLOCK_SIZE: tl.constexpr,
):
pid = tl.program_id(axis=0)
block_start = pid * BLOCK_SIZE
offsets = block_start + tl.arange(0, BLOCK_SIZE)
mask = offsets < n_elements
input_ptr_offsets = input_ptr + offsets
buffers = tlx.local_alloc((BLOCK_SIZE, ), tl.float32, tl.constexpr(1))
buffer = tlx.local_view(buffers, 0)
token = tlx.async_load(input_ptr_offsets, buffer, mask=mask)
token = tlx.async_load_commit_group([token])
tlx.async_load_wait_group(tl.constexpr(0), [token])
x = tlx.local_load(buffer)
tl.store(output_ptr + offsets, x, mask=mask)
torch.manual_seed(0)
size = 64
x = torch.rand(size, dtype=torch.float32, device=device)
output = torch.empty_like(x)
n_elements = x.numel()
grid = lambda meta: (triton.cdiv(n_elements, meta["BLOCK_SIZE"]), )
kernel = async_wait_kernel[grid](x, output, n_elements, BLOCK_SIZE)
assert kernel.asm["ttgir"].count("ttg.async_copy_global_to_local") == 1
assert kernel.asm["ttgir"].count("ttg.async_commit_group") == 1
assert kernel.asm["ttgir"].count("ttg.async_wait") == 1
torch.testing.assert_close(x, output)
kernel = async_wait_token_kernel[grid](x, output, n_elements, BLOCK_SIZE)
torch.testing.assert_close(x, output)
@pytest.mark.skipif(not is_hopper_or_newer(), reason="Need Hopper or newer")
def test_local_trans(device):
@triton.jit
def local_trans_kernel(
input_ptr,
output_ptr,
M,
N,
BLOCK_SIZE_M: tl.constexpr,
BLOCK_SIZE_N: tl.constexpr,
):
pid_m = tl.program_id(0)
pid_n = tl.program_id(1)
# Compute tile offset in global memory
off_m = pid_m * BLOCK_SIZE_M + tl.arange(0, BLOCK_SIZE_M)
off_n = pid_n * BLOCK_SIZE_N + tl.arange(0, BLOCK_SIZE_N)
# Compute global offsets
input_offset = off_m[:, None] * N + off_n[None, :]
output_offset = off_n[:, None] * M + off_m[None, :]
buffers = tlx.local_alloc((BLOCK_SIZE_M, BLOCK_SIZE_N), tl.float32, tl.constexpr(1))
buffer0 = tlx.local_view(buffers, 0)
tlx.async_load(input_ptr + input_offset, buffer0)
tlx.async_load_commit_group()
tlx.async_load_wait_group(tl.constexpr(0))
buffer1 = tlx.local_trans(buffer0)
transposed = tlx.local_load(buffer1)
tl.store(output_ptr + output_offset, transposed)
torch.manual_seed(0)
M, N = 32, 64
BLOCK_SIZE_M, BLOCK_SIZE_N = 32, 64
x = torch.rand((M, N), dtype=torch.float32, device=device)
y = torch.empty((N, M), dtype=torch.float32, device=device)
grid = lambda meta: (triton.cdiv(M, BLOCK_SIZE_M), triton.cdiv(N, BLOCK_SIZE_N))
kernel = local_trans_kernel[grid](x, y, M, N, BLOCK_SIZE_M=BLOCK_SIZE_M, BLOCK_SIZE_N=BLOCK_SIZE_N, num_warps=1)
assert kernel.asm["ttgir"].count("ttg.memdesc_trans") == 1
torch.testing.assert_close(y, x.T)
@pytest.mark.skipif(not is_blackwell(), reason="Need Blackwell")
def test_local_reinterpret(device):
@triton.jit
def local_reinterpret_kernel(
x32_ptr,
y32_ptr,
x16_ptr,
y16_ptr,
BLOCK_SIZE_M: tl.constexpr,
BLOCK_SIZE_N: tl.constexpr,
):
pid_m = tl.program_id(0)
pid_n = tl.program_id(1)
# Compute tile offset in global memory
off_m = pid_m * BLOCK_SIZE_M + tl.arange(0, BLOCK_SIZE_M)
off_n = pid_n * BLOCK_SIZE_N + tl.arange(0, BLOCK_SIZE_N)
# Compute global offsets
input_offset = off_m[:, None] * BLOCK_SIZE_N + off_n[None, :]
output_offset = off_m[:, None] * BLOCK_SIZE_N + off_n[None, :]
tmem_buffers = tlx.local_alloc((BLOCK_SIZE_M, BLOCK_SIZE_N), tl.float32, tl.constexpr(1), tlx.storage_kind.tmem)
tmem_buffer_0 = tlx.local_view(tmem_buffers, 0)
# x32 GMEM -> x32 SMEM -> x32 Reg -> x32 TMEM -> x32 Reg -> y32 GMEM
smem_buffers32 = tlx.local_alloc((BLOCK_SIZE_M, BLOCK_SIZE_N), tl.float32, tl.constexpr(1),
tlx.storage_kind.smem)
smem_buffer_32_0 = tlx.local_view(smem_buffers32, 0)
tlx.async_load(x32_ptr + input_offset, smem_buffer_32_0)
tlx.async_load_commit_group()
tlx.async_load_wait_group(tl.constexpr(0))
x32_reg = tlx.local_load(smem_buffer_32_0)
tlx.local_store(tmem_buffer_0, x32_reg)
x32_reg_from_tmem = tlx.local_load(tmem_buffer_0)
tl.store(y32_ptr + output_offset, x32_reg_from_tmem)
# x16 GMEM -> x16 SMEM -> x16 Reg -> x16 TMEM -> x16 Reg -> y16 GMEM
smem_buffers16 = tlx.local_alloc((BLOCK_SIZE_M, BLOCK_SIZE_N), tl.float16, tl.constexpr(1),
tlx.storage_kind.smem)
smem_buffer_16_0 = tlx.local_view(smem_buffers16, 0)
tlx.async_load(x16_ptr + input_offset, smem_buffer_16_0)
tlx.async_load_commit_group()
tlx.async_load_wait_group(tl.constexpr(0))
reinterpreted = tlx.local_reinterpret(tmem_buffer_0, tl.float16)
x16_reg = tlx.local_load(smem_buffer_16_0)
tlx.local_store(reinterpreted, x16_reg)
x16_reg_from_tmem = tlx.local_load(reinterpreted)
tl.store(y16_ptr + output_offset, x16_reg_from_tmem)
torch.manual_seed(0)
M, N = 64, 128
BLOCK_SIZE_M, BLOCK_SIZE_N = M, N
x32 = torch.rand((M, N), dtype=torch.float32, device=device)
y32 = torch.zeros((M, N), dtype=torch.float32, device=device)
x16 = torch.rand((M, N), dtype=torch.float16, device=device)
y16 = torch.zeros((M, N), dtype=torch.float16, device=device)
grid = lambda meta: (1, )
kernel = local_reinterpret_kernel[grid](x32, y32, x16, y16, BLOCK_SIZE_M=BLOCK_SIZE_M, BLOCK_SIZE_N=BLOCK_SIZE_N)
assert kernel.asm["ttgir"].count("ttg.memdesc_reinterpret") == 1
assert kernel.asm["ttgir"].count("ttng.tmem_store") == 2
assert kernel.asm["ttgir"].count("ttng.tmem_alloc") == 1
torch.testing.assert_close(x32, y32)
torch.testing.assert_close(x16, y16)
@pytest.mark.skipif(not is_blackwell(), reason="Need Blackwell")
def test_local_reinterpret_swizzled(device):
@triton.jit
def local_reinterpret_swizzled_kernel(
a_ptr,
stride_am,
stride_ak,
b_ptr,
stride_bk,
stride_bn,
c_ptr,
stride_cm,
stride_cn,
BLOCK_M: tl.constexpr,
BLOCK_N: tl.constexpr,
BLOCK_K: tl.constexpr,
OUT_DTYPE: tl.constexpr,
):
offs_m = tl.arange(0, BLOCK_M)
offs_n = tl.arange(0, BLOCK_N)
offs_k = tl.arange(0, BLOCK_K)
a_ptrs = a_ptr + (tl.arange(0, BLOCK_M // 2)[:, None] * stride_am + offs_k[None, :] * stride_ak)
a_ptrs2 = a_ptr + (tl.arange(BLOCK_M // 2, BLOCK_M)[:, None] * stride_am + offs_k[None, :] * stride_ak)
b_ptrs = b_ptr + (offs_k[:, None] * stride_bk + offs_n[None, :] * stride_bn)
# async load a and b into SMEM
buf_alloc_a = tlx.local_alloc((BLOCK_M // 2, BLOCK_K), tl.float16, tl.constexpr(2))
buf_alloc_b = tlx.local_alloc((BLOCK_K, BLOCK_N), tl.float16, tl.constexpr(1))
b_smem = tlx.local_view(buf_alloc_b, 0)
# load half of a each time
tlx.async_load(a_ptrs, buf_alloc_a[0])
tlx.async_load(a_ptrs2, buf_alloc_a[1])
tlx.async_load(b_ptrs, b_smem)
tlx.async_load_commit_group()
tlx.async_load_wait_group(tl.constexpr(0))
buffers = tlx.local_alloc((BLOCK_M, BLOCK_N), tl.float32, tl.constexpr(1), tlx.storage_kind.tmem)
acc_tmem = tlx.local_view(buffers, 0)
# reinterpret a into one big tensor
a_reinterpreted = tlx.local_reinterpret(buf_alloc_a, tl.float16, [BLOCK_M, BLOCK_K])
# no barrier, tcgen5 mma synchronous semantic, compiler auto inserts barrier and wait
tlx.async_dot(a_reinterpreted, b_smem, acc_tmem, use_acc=False, mBarriers=[], out_dtype=OUT_DTYPE)
result = tlx.local_load(acc_tmem)
c = result.to(tl.float16)
c_ptrs = c_ptr + stride_cm * offs_m[:, None] + stride_cn * offs_n[None, :]