test_tilelang_language_ternary.py 1.24 KB
Newer Older
1
2
3
4
5
6
import tilelang
import tilelang.language as T
import torch
import tilelang.testing


7
8
9
@tilelang.jit(
    out_idx=[1],
)
10
11
12
def tilelang_ternary(M, N, block_M, block_N, dtype="float16"):
    @T.prim_func
    def main(
13
14
        A: T.Tensor((M, N), dtype),
        B: T.Tensor((M, N), dtype),
15
16
17
18
    ):
        # Initialize Kernel Context
        with T.Kernel(T.ceildiv(N, block_N), T.ceildiv(M, block_M), threads=128) as (bx, by):
            for i, j in T.Parallel(block_M, block_N):
19
                B[by * block_M + i, bx * block_N + j] = A[by * block_M + i, bx * block_N + j] if (by * block_M + i) < (M // 2) else 0
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

    return main


def run_tilelang_ternary(M=128, N=128, block_M=32, block_N=32, dtype="float16"):
    kernel = tilelang_ternary(M, N, block_M, block_N, dtype)
    a = torch.randn(M, N, device="cuda", dtype=getattr(torch, dtype))
    b = kernel(a)
    ref_b = torch.zeros_like(b)
    for i in range(M):
        for j in range(N):
            if i < M // 2:
                ref_b[i, j] = a[i, j]
            else:
                ref_b[i, j] = 0

    torch.testing.assert_close(b, ref_b, rtol=1e-2, atol=1e-2)


def test_tilelang_ternary():
    run_tilelang_ternary(M=128, N=128, block_M=32, block_N=32)


if __name__ == "__main__":
    tilelang.testing.main()