test_tilelang_primitives_mma.py 9.54 KB
Newer Older
1
2
3
4
5
6
7
8
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.

from tilelang import tvm as tvm
import tilelang.testing
import tilelang as tl
from tilelang import primitives as P

9

10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
def matmul_ssr(
    M,
    N,
    K,
    block_M,
    block_N,
    block_K,
    trans_A,
    trans_B,
    in_dtype,
    out_dtype,
    accum_dtype,
    num_stages,
    threads,
):
    A_shape = (K, M) if trans_A else (M, K)
    B_shape = (N, K) if trans_B else (K, N)
    A_shared_shape = (block_K, block_M) if trans_A else (block_M, block_K)
    B_shared_shape = (block_N, block_K) if trans_B else (block_K, block_N)
LeiWang1999's avatar
LeiWang1999 committed
29
    shared_scope = "shared"  # or "shared.dyn" for dynamic shared memory
30
31
32
33
    import tilelang.language as T

    @T.prim_func
    def main(
34
35
36
            A: T.Buffer(A_shape, in_dtype),
            B: T.Buffer(B_shape, in_dtype),
            C: T.Buffer((M, N), out_dtype),
37
    ):
38
        with T.Kernel(T.ceildiv(N, block_N), T.ceildiv(M, block_M), threads=threads) as (bx, by):
LeiWang1999's avatar
LeiWang1999 committed
39
40
            A_shared = T.alloc_shared(A_shared_shape, in_dtype, scope=shared_scope)
            B_shared = T.alloc_shared(B_shared_shape, in_dtype, scope=shared_scope)
41
42
            C_local = T.alloc_fragment((block_M, block_N), accum_dtype)
            T.clear(C_local)
43
            for ko in T.Pipelined(T.ceildiv(K, block_K), num_stages=num_stages):
44
                if trans_A:
45
                    T.copy(A[ko * block_K, by * block_M], A_shared)
46
                else:
47
                    T.copy(A[by * block_M, ko * block_K], A_shared)
48
                if trans_B:
49
                    T.copy(B[bx * block_N, ko * block_K], B_shared)
50
                else:
51
                    T.copy(B[ko * block_K, bx * block_N], B_shared)
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
                P.gemm(A_shared, B_shared, C_local, trans_A, trans_B)
            T.copy(C_local, C[by * block_M, bx * block_N])

    return main


def run_matmul_ssr(
    M,
    N,
    K,
    trans_A,
    trans_B,
    in_dtype,
    out_dtype,
    dtypeAccum,
    block_M,
    block_N,
    block_K,
    num_stages=3,
    num_threads=128,
):
    program = matmul_ssr(
        M,
        N,
        K,
        block_M,
        block_N,
        block_K,
        trans_A,
        trans_B,
        in_dtype,
        out_dtype,
        dtypeAccum,
        num_stages,
        num_threads,
    )
    mod, params = tl.lower(program)
    mod = tl.Profiler(mod, params, [2], tl.TensorSupplyType.Integer)
LeiWang1999's avatar
LeiWang1999 committed
90
    print(mod.get_kernel_source())
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106

    def ref_program(A, B):
        import torch

        if trans_A:
            A = A.T
        if trans_B:
            B = B.T
        C = torch.matmul(A.to(torch.float), B.to(torch.float))
        C = C.to(torch.__getattribute__(out_dtype))
        return C

    mod.assert_allclose(ref_program, atol=1e-2, rtol=1e-2)


def test_gemm_f16f16f16_nt_ssr():
107
108
109
110
    run_matmul_ssr(
        16, 16, 16, False, True, "float16", "float16", "float16", 16, 16, 16, 0, num_threads=32)
    run_matmul_ssr(
        128, 128, 128, False, True, "float16", "float16", "float16", 32, 32, 32, 0, num_threads=64)
111
112
113
114
115
116
117
118
119
120
121
122
123
    run_matmul_ssr(
        1024,
        1024,
        1024,
        False,
        True,
        "float16",
        "float16",
        "float16",
        128,
        128,
        32,
        2,
124
        num_threads=128)
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146


def matmul_rsr(
    M,
    N,
    K,
    block_M,
    block_N,
    block_K,
    trans_A,
    trans_B,
    in_dtype,
    out_dtype,
    accum_dtype,
    num_stages,
    threads,
):
    A_shape = (K, M) if trans_A else (M, K)
    B_shape = (N, K) if trans_B else (K, N)
    A_shared_shape = (block_K, block_M) if trans_A else (block_M, block_K)
    B_shared_shape = (block_N, block_K) if trans_B else (block_K, block_N)
    A_local_shape = A_shared_shape
LeiWang1999's avatar
LeiWang1999 committed
147
    shared_scope = "shared"  # or "shared.dyn" for dynamic shared memory
148
149
150
151
    import tilelang.language as T

    @T.prim_func
    def main(
152
153
154
            A: T.Buffer(A_shape, in_dtype),
            B: T.Buffer(B_shape, in_dtype),
            C: T.Buffer((M, N), out_dtype),
155
    ):
156
        with T.Kernel(T.ceildiv(N, block_N), T.ceildiv(M, block_M), threads=threads) as (bx, by):
LeiWang1999's avatar
LeiWang1999 committed
157
158
            A_shared = T.alloc_shared(A_shared_shape, in_dtype, scope=shared_scope)
            B_shared = T.alloc_shared(B_shared_shape, in_dtype, scope=shared_scope)
159
160
161
            A_local = T.alloc_fragment(A_local_shape, in_dtype)
            C_local = T.alloc_fragment((block_M, block_N), accum_dtype)
            T.clear(C_local)
162
            for ko in T.Pipelined(T.ceildiv(K, block_K), num_stages=num_stages):
163
                if trans_A:
164
                    T.copy(A[ko * block_K, by * block_M], A_shared)
165
                else:
166
                    T.copy(A[by * block_M, ko * block_K], A_shared)
167
                if trans_B:
168
                    T.copy(B[bx * block_N, ko * block_K], B_shared)
169
                else:
170
                    T.copy(B[ko * block_K, bx * block_N], B_shared)
LeiWang1999's avatar
LeiWang1999 committed
171
                T.copy(A_shared, A_local)
172
                P.gemm(A_local, B_shared, C_local, trans_A, trans_B)
LeiWang1999's avatar
LeiWang1999 committed
173
                # T.gemm(A_local, B_shared, C_local, trans_A, trans_B)
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
            T.copy(C_local, C[by * block_M, bx * block_N])

    return main


def run_matmul_rsr(
    M,
    N,
    K,
    trans_A,
    trans_B,
    in_dtype,
    out_dtype,
    dtypeAccum,
    block_M,
    block_N,
    block_K,
    num_stages=3,
    num_threads=128,
):
    program = matmul_rsr(
        M,
        N,
        K,
        block_M,
        block_N,
        block_K,
        trans_A,
        trans_B,
        in_dtype,
        out_dtype,
        dtypeAccum,
        num_stages,
        num_threads,
    )
    mod, params = tl.lower(program)
    mod = tl.Profiler(mod, params, [2], tl.TensorSupplyType.Integer)
LeiWang1999's avatar
LeiWang1999 committed
211
    print(mod.get_kernel_source())
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226

    def ref_program(A, B):
        import torch

        if trans_A:
            A = A.T
        if trans_B:
            B = B.T
        C = torch.matmul(A.to(torch.float), B.to(torch.float))
        C = C.to(torch.__getattribute__(out_dtype))
        return C

    mod.assert_allclose(ref_program, atol=1e-2, rtol=1e-2)


LeiWang1999's avatar
LeiWang1999 committed
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
# TODO(lei): Fix the test case in future release
# Now it has some bugs related to is_m_first
# def test_gemm_f16f16f16_nt_rsr():
#     run_matmul_rsr(
#         1024,
#         1024,
#         1024,
#         False,
#         True,
#         "float16",
#         "float16",
#         "float16",
#         128,
#         128,
#         32,
#         0,
#         num_threads=128,
#     )
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


def matmul_rrr(
    M,
    N,
    K,
    block_M,
    block_N,
    block_K,
    trans_A,
    trans_B,
    in_dtype,
    out_dtype,
    accum_dtype,
    num_stages,
    threads,
):
    A_shape = (K, M) if trans_A else (M, K)
    B_shape = (N, K) if trans_B else (K, N)
    A_shared_shape = (block_K, block_M) if trans_A else (block_M, block_K)
    B_shared_shape = (block_N, block_K) if trans_B else (block_K, block_N)
    A_local_shape = A_shared_shape
    B_local_shape = B_shared_shape
    import tilelang.language as T

    @T.prim_func
    def main(
272
273
274
            A: T.Buffer(A_shape, in_dtype),
            B: T.Buffer(B_shape, in_dtype),
            C: T.Buffer((M, N), out_dtype),
275
    ):
276
        with T.Kernel(T.ceildiv(N, block_N), T.ceildiv(M, block_M), threads=threads) as (bx, by):
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
            A_shared = T.alloc_shared(A_shared_shape, in_dtype)
            A_local = T.alloc_fragment(A_local_shape, in_dtype)
            B_shared = T.alloc_shared(B_shared_shape, in_dtype)
            B_local = T.alloc_fragment(B_local_shape, in_dtype)
            C_local = T.alloc_fragment((block_M, block_N), accum_dtype)
            T.clear(C_local)
            for k in T.Pipelined(T.ceildiv(K, block_K), num_stages=num_stages):
                if trans_A:
                    T.copy(A[k * block_K, by * block_M], A_shared)
                    T.copy(A_shared, A_local)
                else:
                    T.copy(A[by * block_M, k * block_K], A_shared)
                    T.copy(A_shared, A_local)
                if trans_B:
                    T.copy(B[bx * block_N, k * block_K], B_shared)
                    T.copy(B_shared, B_local)
                else:
                    T.copy(B[k * block_K, bx * block_N], B_shared)
                    T.copy(B_shared, B_local)
                P.gemm(A_local, B_local, C_local, trans_A, trans_B)
            T.copy(C_local, C[by * block_M, bx * block_N])

    return main


def run_matmul_rrr(
    M,
    N,
    K,
    trans_A,
    trans_B,
    in_dtype,
    out_dtype,
    dtypeAccum,
    block_M,
    block_N,
    block_K,
    num_stages=3,
    num_threads=128,
):
    program = matmul_rrr(
        M,
        N,
        K,
        block_M,
        block_N,
        block_K,
        trans_A,
        trans_B,
        in_dtype,
        out_dtype,
        dtypeAccum,
        num_stages,
        num_threads,
    )
    mod, params = tl.lower(program)
    mod = tl.Profiler(mod, params, [2], tl.TensorSupplyType.Integer)

    def ref_program(A, B):
        import torch

        if trans_A:
            A = A.T
        if trans_B:
            B = B.T
        C = torch.matmul(A.to(torch.float), B.to(torch.float))
        C = C.to(torch.__getattribute__(out_dtype))
        return C

    mod.assert_allclose(ref_program, atol=1e-2, rtol=1e-2)


LeiWang1999's avatar
LeiWang1999 committed
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
# def test_gemm_f16f16f16_nt_rrr():
#     run_matmul_rrr(
#         1024,
#         1024,
#         1024,
#         False,
#         True,
#         "float16",
#         "float16",
#         "float16",
#         128,
#         128,
#         32,
#         2,
#     )

if __name__ == "__main__":
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
    # tilelang.testing.main()
    run_matmul_rsr(
        128,
        128,
        128,
        False,
        True,
        "float16",
        "float16",
        "float16",
        128,
        128,
        32,
        0,
        num_threads=128,
    )