test_tilelang_fragment_loop_checker.py 4.02 KB
Newer Older
1
import tilelang
2
import tilelang.testing
3
4
5
6
7
import tilelang.language as T
import pytest


@tilelang.jit
8
def simple_invalid_loop(dtype: str = "bfloat16", accum_dtype: str = "float32", num_threads: int = 128):
9
10
11
12
    A = T.dynamic("A")

    @T.prim_func
    def main(
13
        data: T.Tensor((128, A), dtype),  # type: ignore
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
    ):
        with T.Kernel(128, threads=num_threads) as (tid,):
            data_frag = T.alloc_fragment([128], accum_dtype)

            for i in T.Parallel(128):
                if i < A:
                    data_frag[i] = data[tid, i]

            for i in T.Parallel(A):
                data_frag[i] = 0

    return main


@tilelang.jit
29
def nested_invalid_loop(dtype: str = "bfloat16", accum_dtype: str = "float32", num_threads: int = 128):
30
31
32
33
    A = T.dynamic("A")

    @T.prim_func
    def main(
34
        data: T.Tensor((128, A), dtype),  # type: ignore
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
    ):
        with T.Kernel(128, threads=num_threads) as (tid,):
            data_frag = T.alloc_fragment([128], accum_dtype)

            for i in T.Parallel(128):
                if i < A:
                    data_frag[i] = data[tid, i]

            for i in T.Parallel(A // 64):
                for j in T.Parallel(64):
                    data_frag[i * 64 + j] = 0

    return main


@tilelang.jit
51
def invalid_loop_with_complex_dataflow(dtype: str = "bfloat16", accum_dtype: str = "float32", num_threads: int = 128):
52
53
54
55
    A = T.dynamic("A")

    @T.prim_func
    def main(
56
        data: T.Tensor((128, A), dtype),  # type: ignore
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
    ):
        with T.Kernel(128, threads=num_threads) as (tid,):
            data_frag = T.alloc_fragment([128], accum_dtype)

            for i in T.Parallel(128):
                if i < A:
                    data_frag[i] = data[tid, i]

            for i in T.Parallel(A):
                data_frag[64 // 2 + i % 64] = 0

    return main


@tilelang.jit
72
def valid_loop_not_use_loop_var(dtype: str = "bfloat16", accum_dtype: str = "float32", num_threads: int = 128):
73
74
75
76
    A = T.dynamic("A")

    @T.prim_func
    def main(
77
        data: T.Tensor((128, A), dtype),  # type: ignore
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
    ):
        with T.Kernel(128, threads=num_threads) as (tid,):
            data_frag = T.alloc_fragment([128], accum_dtype)

            for i in T.Parallel(128):
                if i < A:
                    data_frag[i] = data[tid, i]

            for i in T.Parallel(A):  # noqa: B007
                for j in T.Parallel(64):
                    data_frag[j] = 0  # This is valid because we don't use i

    return main


@tilelang.jit
94
def valid_loop_not_frag(dtype: str = "bfloat16", accum_dtype: str = "float32", num_threads: int = 128):
95
96
97
98
    A = T.dynamic("A")

    @T.prim_func
    def main(
99
        data: T.Tensor((128, A), dtype),  # type: ignore
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
    ):
        with T.Kernel(128, threads=num_threads) as (tid,):
            data_shared = T.alloc_shared([128], accum_dtype)

            for i in T.Parallel(128):
                if i < A:
                    data_shared[i] = data[tid, i]

            for i in T.Parallel(A):
                data_shared[i] = 0  # Valid because this is shared memory

    return main


@tilelang.jit
115
def valid_loop_serial(dtype: str = "bfloat16", accum_dtype: str = "float32", num_threads: int = 128):
116
117
118
119
    A = T.dynamic("A")

    @T.prim_func
    def main(
120
        data: T.Tensor((128, A), dtype),  # type: ignore
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
    ):
        with T.Kernel(128, threads=num_threads) as (tid,):
            data_shared = T.alloc_shared([128], accum_dtype)

            for i in T.Parallel(128):
                if i < A:
                    data_shared[i] = data[tid, i]

            for i in T.serial(A):
                data_shared[i] = 0  # Valid because this is serial

    return main


def test_invalid_loop():
    with pytest.raises(ValueError):
        simple_invalid_loop()
    with pytest.raises(ValueError):
        nested_invalid_loop()
    with pytest.raises(ValueError):
        invalid_loop_with_complex_dataflow()


def test_valid_loop():
    valid_loop_not_use_loop_var()
    valid_loop_not_frag()
    valid_loop_serial()


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