test_tilelang_language_alloc.py 3.53 KB
Newer Older
1
import tilelang.testing
2
from tilelang import language as T
3
4
5
6
7
8
9
10
11


def alloc_var(
    N,
    block_N,
    dtype,
):
    @T.prim_func
    def main(
12
13
        A: T.Tensor((N,), dtype),
        B: T.Tensor((N,), dtype),
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
    ):
        with T.Kernel(T.ceildiv(N, block_N), threads=block_N) as bx:
            A_shared = T.alloc_shared([block_N], dtype)
            tmp = T.alloc_var(dtype)
            tmp = 1  # noqa: F841
            T.copy(A[bx * block_N], A_shared)
            T.copy(A_shared, B[bx * block_N])

    return main


def run_alloc_var(
    N,
    block_N,
    dtype,
    min=None,
    max=None,
):
    program = alloc_var(N, block_N, dtype)

    kernel = tilelang.compile(program, out_idx=[1])
    code = kernel.get_kernel_source()
    assert "tmp =" in code


def test_alloc_var():
40
    run_alloc_var(1024, 128, T.float16)
41
42
43
44
45
46
47
48
49
50
51


def alloc_var_add(
    N,
    block_N,
    dtype,
):
    import tilelang.language as T

    @T.prim_func
    def main(
52
53
        A: T.Tensor((N,), dtype),
        B: T.Tensor((N,), dtype),
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
    ):
        with T.Kernel(T.ceildiv(N, block_N), threads=block_N) as bx:
            A_shared = T.alloc_shared([block_N], dtype)
            tmp = T.alloc_var(dtype)
            tmp = 1  # noqa: F841
            T.copy(A[bx * block_N], A_shared)
            for i in T.Parallel(block_N):
                A_shared[i] = A_shared[i] + tmp
            T.copy(A_shared, B[bx * block_N])

    return main


def run_alloc_var_add(
    N,
    block_N,
    dtype,
):
    program = alloc_var_add(N, block_N, dtype)

    kernel = tilelang.compile(program, out_idx=[1])
    code = kernel.get_kernel_source()
    assert "tmp =" in code


def test_alloc_var_add():
80
    run_alloc_var_add(1024, 128, T.float16)
81
82


83
84
85
86
87
88
89
90
91
92
def alloc_var_with_initializer(
    N,
    block_N,
    dtype,
    init_value,
):
    import tilelang.language as T

    @T.prim_func
    def main(
93
94
        A: T.Tensor((N,), dtype),
        B: T.Tensor((N,), dtype),
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
    ):
        with T.Kernel(T.ceildiv(N, block_N), threads=block_N) as bx:
            tmp = T.alloc_var(dtype, init_value)
            T.copy(A[bx * block_N], B[bx * block_N])
            for i in T.Parallel(block_N):
                B[bx * block_N + i] = tmp

    return main


def run_alloc_var_with_initializer(
    N,
    block_N,
    dtype,
    init_value,
):
    program = alloc_var_with_initializer(N, block_N, dtype, init_value)

    kernel = tilelang.compile(program, out_idx=[1])
    code = kernel.get_kernel_source()
    assert f"= {init_value};" in code


def test_alloc_var_with_initializer():
119
    run_alloc_var_with_initializer(256, 64, T.int32, 5)
120
121
122
123
124
125
126
127
128
129
130


def alloc_multi_vars_with_initializer(
    N,
    block_N,
    dtype,
):
    import tilelang.language as T

    @T.prim_func
    def main(
131
132
        A: T.Tensor((N,), dtype),
        B: T.Tensor((N,), dtype),
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
    ):
        with T.Kernel(T.ceildiv(N, block_N), threads=block_N) as bx:
            tmp0 = T.alloc_var(dtype, 1)
            tmp1 = T.alloc_var(dtype, 2)
            T.copy(A[bx * block_N], B[bx * block_N])
            for i in T.Parallel(block_N):
                B[bx * block_N + i] = tmp0 + tmp1

    return main


def run_alloc_multi_vars_with_initializer(
    N,
    block_N,
    dtype,
):
    program = alloc_multi_vars_with_initializer(N, block_N, dtype)

    kernel = tilelang.compile(program, out_idx=[1])
152
    code = kernel.get_kernel_source(kernel_only=True)
153
154
155
156
157
    assert code.count("= 1;") == 1
    assert code.count("= 2;") == 1


def test_alloc_multi_vars_with_initializer():
158
    run_alloc_multi_vars_with_initializer(256, 64, T.int32)
159
160


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