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
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
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.

# pylint: disable=invalid-name
"""Reduction rule for operators including softmax, layer norm, RMS norm, etc"""
from typing import List, Union
from functools import reduce

from tvm import tir
from tvm.target import Target

from ..base import normalize_prim_func, try_inline_contiguous_spatial
from ..base.analysis import get_root_block, get_reduction_blocks, BlockInfo
from .base import GPUScheduleRule


class GeneralReduction(GPUScheduleRule):
    """General Reduction rule for operators including softmax, layer norm, RMS norm, etc"""

    def apply(  # pylint: disable=too-many-locals
        self,
        func: tir.PrimFunc,
        target: Target,
        _: bool,
    ) -> Union[None, tir.Schedule, List[tir.Schedule]]:
        if not isinstance(func, tir.PrimFunc) or not self.is_target_available(target):
            return None

        if target.kind.name == "cuda":
            len_tx = 256
            unroll_depth = 256
        else:
            len_tx = 64
            unroll_depth = 64

        sch = tir.Schedule(func)
        block_infos = normalize_prim_func(sch)
        block_infos = try_inline_contiguous_spatial(sch, block_infos)
        if block_infos is None or len(block_infos) == 0:
            return None

        dom_kind = block_infos[0].dom_kind()
        num_leading_s = len(dom_kind) - len(dom_kind.lstrip("S"))
        num_trailing_r = len(dom_kind) - len(dom_kind.rstrip("R"))

        # Align the number of block iters of the last block.
        num_last_block_iter = len(block_infos[-1].dom_kind())
        if num_last_block_iter < len(dom_kind):
            index_map = tir.IndexMap.from_func(
                lambda *iters: (
                    [tir.const(0, iters[0].dtype)] * (len(dom_kind) - num_last_block_iter)
                    + list(iters)
                ),
                ndim=num_last_block_iter,
            )
            sch.transform_block_layout(block_infos[-1].block_rv, index_map)

        try:
            # TODO: fix num_leading_s = 0 case
            assert num_trailing_r > 0
            for block in block_infos[1:-1]:
                assert block.dom_kind() == dom_kind
            assert block_infos[-1].is_injective()
            assert len(block_infos[-1].dom_kind()) <= len(dom_kind)
        except AssertionError:
            return None

        loops = sch.get_loops(block_infos[-1].block_rv)
        bx = sch.fuse(*loops[:num_leading_s])
        r_loop, tx = sch.split(loops[-1], [None, len_tx])
        sch.reorder(tx, r_loop)
        sch.bind(bx, "blockIdx.x")
        sch.bind(tx, "threadIdx.x")
        sch.annotate(r_loop, ann_key="pragma_auto_unroll_max_step", ann_val=unroll_depth)
        sch.annotate(r_loop, ann_key="pragma_unroll_explicit", ann_val=1)

        for block in reversed(block_infos[:-1]):
            block = block.block_rv
            for i, _ in enumerate(sch.get(block).writes):
                sch.set_scope(block, buffer_index=i, storage_scope="shared")
            sch.compute_at(block, bx, preserve_unit_loops=True)
            r_loop = sch.fuse(*sch.get_loops(block)[-num_trailing_r:])
            r_loop, tx = sch.split(r_loop, [None, len_tx])
            sch.reorder(tx, r_loop)
            sch.bind(tx, "threadIdx.x")
            sch.annotate(r_loop, ann_key="pragma_auto_unroll_max_step", ann_val=unroll_depth)
            sch.annotate(r_loop, ann_key="pragma_unroll_explicit", ann_val=1)

        # TODO: It's just a workaround to avoid unroll spatial loops, because of the bug of
        # the pass lower-thread-allreduce. We should fix it in the future.
        # sch.annotate(bx, ann_key="pragma_auto_unroll_max_step", ann_val=unroll_depth)
        # sch.annotate(bx, ann_key="pragma_unroll_explicit", ann_val=1)
        return sch

    def sch_inner_reduction_with_config(  # pylint: disable=too-many-locals,too-many-branches,too-many-return-statements
        self,
        func: tir.PrimFunc,
        config,
    ):
        block_factors = config.block
        thread_factors = config.thread
        reduce_therad_factors = config.reduce_thread

        # For inter thread reduction case, one thread must only compute one element
        assert thread_factors == block_factors

        # inline all the other blocks
        sch = tir.Schedule(func)
        block_infos = normalize_prim_func(sch)

        schedule_block: tir.schedule.BlockRV = None
        reduction_blocks: List[tir.schedule.BlockRV] = []
        for block in block_infos:
            s_loops: List[tir.schedule.LoopRV] = []
            r_loops: List[tir.schedule.LoopRV] = []
            o_loops: List[tir.schedule.LoopRV] = []
            dom_kind = block.dom_kind()
            block_rv = block.block_rv

            if (
                any(
                    [
                        sch.get(loop_rv).thread_binding is not None
                        for loop_rv in sch.get_loops(block_rv)
                    ]
                )
                or len(sch.get_loops(block.block_rv)) == 0
            ):
                continue

            for loop, iter_type in zip(sch.get_loops(block_rv), dom_kind):
                {"S": s_loops, "R": r_loops, "O": o_loops}[iter_type].append(loop)

            if not s_loops:
                s_loops.append(sch.add_unit_loop(block_rv))
            if len(r_loops) > 0:
                # always use the last reduction block for scheduling
                schedule_block = block
                reduction_blocks.append(block_rv)

        # Align the number of block iters of the last block.
        dom_kind = schedule_block.dom_kind()
        num_leading_s = len(dom_kind) - len(dom_kind.lstrip("S"))
        num_trailing_r = len(dom_kind) - len(dom_kind.rstrip("R"))

        schedule_block = schedule_block.block_rv
        loops = sch.get_loops(schedule_block)
        s_loops = loops[:num_leading_s]
        r_loops = loops[-num_trailing_r:]

        block_axis = []
        thread_axis = []

        for s_loop, block_factor in zip(s_loops, block_factors):
            block_loop, thread_loop = sch.split(s_loop, factors=[None, block_factor])
            block_axis.append(block_loop)
            thread_axis.append(thread_loop)

        axis_order = block_axis + thread_axis

        sch.reorder(*axis_order)
        blck_fused = sch.fuse(*block_axis)
        thrd_fused = sch.fuse(*thread_axis)
        sch.bind(blck_fused, "blockIdx.x")
        sch.bind(thrd_fused, "threadIdx.y")

        reduce_outer_axis, reduce_inner_axis, reduce_inter_threads = [], [], []
        for i in config.raxis_order:
            loop = r_loops[i]
            ro, ri = sch.split(loop, factors=[None, config.rstep[i]])
            ri, thd = sch.split(ri, factors=[None, config.reduce_thread[i]])
            reduce_inter_threads.append(thd)
            reduce_outer_axis.append(ro)
            reduce_inner_axis.append(ri)

        axis_order = reduce_inter_threads + reduce_outer_axis + reduce_inner_axis
        sch.reorder(*axis_order)
        fused_reduce_inter_threads = sch.fuse(*reduce_inter_threads)
        sch.bind(fused_reduce_inter_threads, "threadIdx.x")

        def prod(iterable):
            return reduce(lambda x, y: x * y, iterable, 1)

        reg_tile = sch.cache_write(schedule_block, 0, "local")

        # todo(lei): should add the shared_inputs/stride memory pad analysis at shared memory fusion stage.
        for i, input_region in enumerate(sch.get(schedule_block).reads):
            if input_region.buffer.name not in config.cached_tensors:
                continue

            # otherwise cooperative fetch in shared memory.
            cache_shared = sch.cache_read(schedule_block, i, "shared")
            sch.compute_at(cache_shared, reduce_outer_axis[-1])

            dim_offset = (
                len(reduce_inner_axis) + len(reduce_outer_axis) + 2
            )  # outer loops are: blck_fused, thrd_fused, vthread_axis, reduce_outer_axis
            if input_region.buffer.name in config.vectorize:
                vectorize = config.vectorize[input_region.buffer.name]
            else:
                vectorize = 1

            loops = sch.get_loops(cache_shared)
            if len(loops) == dim_offset:
                # handle fetching only one element
                loops.append(sch.add_unit_loop(schedule_block))
            assert len(loops) > dim_offset

            _, ty, tx, tv = sch.split(
                sch.fuse(*loops[dim_offset:]),
                factors=[
                    None,
                    int(prod(thread_factors)),
                    int(prod(reduce_therad_factors)),
                    vectorize,
                ],
            )
            sch.vectorize(tv)
            sch.bind(ty, "threadIdx.y")
            sch.bind(tx, "threadIdx.x")

        sch.reverse_compute_at(reg_tile, thrd_fused)

        # resolve compute_at
        block_infos = try_inline_contiguous_spatial(sch, block_infos)
        if block_infos is None or len(block_infos) == 0:
            return None
        return sch

    def sch_outer_reduction_with_config(  # pylint: disable=too-many-locals,too-many-branches,too-many-return-statements
        self,
        func: tir.PrimFunc,
        config,
    ):
        block_factors = config.block
        thread_factors = config.thread
        step_factors = config.step

        # inline all the other blocks
        sch = tir.Schedule(func)
        block_infos = normalize_prim_func(sch)

        schedule_block: BlockInfo = None
        for block in block_infos:
            s_loops: List[tir.schedule.LoopRV] = []
            r_loops: List[tir.schedule.LoopRV] = []
            o_loops: List[tir.schedule.LoopRV] = []
            dom_kind = block.dom_kind()
            block_rv = block.block_rv

            if (
                any(
                    [
                        sch.get(loop_rv).thread_binding is not None
                        for loop_rv in sch.get_loops(block_rv)
                    ]
                )
                or len(sch.get_loops(block.block_rv)) == 0
            ):
                continue

            for loop, iter_type in zip(sch.get_loops(block_rv), dom_kind):
                {"S": s_loops, "R": r_loops, "O": o_loops}[iter_type].append(loop)

            if not s_loops:
                s_loops.append(sch.add_unit_loop(block_rv))
            if len(r_loops) > 0:
                # always use the last reduction block for scheduling
                schedule_block = block

        # Align the number of block iters of the last block.
        dom_kind = schedule_block.dom_kind()
        num_leading_s = len(dom_kind) - len(dom_kind.lstrip("S"))
        num_trailing_r = len(dom_kind) - len(dom_kind.rstrip("R"))

        num_last_block_iter = len(block_infos[-1].dom_kind())
        if num_last_block_iter < len(dom_kind):
            index_map = tir.IndexMap.from_func(
                lambda *iters: (
                    [tir.const(0, iters[0].dtype)] * (len(dom_kind) - num_last_block_iter)
                    + list(iters)
                ),
                ndim=num_last_block_iter,
            )
            sch.transform_block_layout(block_infos[-1].block_rv, index_map)

        schedule_block = schedule_block.block_rv
        loops = sch.get_loops(schedule_block)
        s_loops = loops[:num_leading_s]
        r_loops = loops[-num_trailing_r:]

        reg_tile = sch.cache_write(schedule_block, 0, "local")

        block_axis = []
        vthread_axis = []
        thread_axis = []
        inner_axis = []
        for s_loop, block_factor, step_factor, thread_factor in zip(
            s_loops, block_factors, step_factors, thread_factors
        ):
            block_loop, inner_loop = sch.split(s_loop, factors=[None, block_factor])
            vthread_loop, inner_loop = sch.split(
                inner_loop, factors=[None, thread_factor * step_factor]
            )
            thread_loop, inner_loop = sch.split(inner_loop, factors=[None, step_factor])
            block_axis.append(block_loop)
            vthread_axis.append(vthread_loop)
            thread_axis.append(thread_loop)
            inner_axis.append(inner_loop)

        reduce_outer_axis, reduce_inner_axis = [], []
        for i in config.raxis_order:
            loop = r_loops[i]
            ro, ri = sch.split(loop, factors=[None, config.rstep[i]])
            reduce_outer_axis.append(ro)
            reduce_inner_axis.append(ri)

        vthread_axis = list(reversed(vthread_axis))  # inner virtual thread first
        axis_order = (
            block_axis
            + vthread_axis
            + thread_axis
            + reduce_outer_axis
            + reduce_inner_axis
            + inner_axis
        )

        sch.reorder(*axis_order)
        blck_fused = sch.fuse(*block_axis)
        thrd_fused = sch.fuse(*thread_axis)
        sch.bind(blck_fused, "blockIdx.x")
        sch.bind(thrd_fused, "threadIdx.x")
        if len(vthread_axis) > 3:
            vthread_axis = vthread_axis[0:2] + [sch.fuse(*vthread_axis[2:])]
        for i, ax in enumerate(vthread_axis):
            sch.bind(ax, "vthread" + [".x", ".y", ".z"][i])

        # todo(lei): should add the shared_inputs/stride memory pad analysis at shared memory fusion stage.
        for i, input_region in enumerate(sch.get(schedule_block).reads):
            if input_region.buffer.name not in config.cached_tensors:
                continue

            # otherwise cooperative fetch in shared memory.
            cache_shared = sch.cache_read(schedule_block, i, "shared")
            sch.compute_at(cache_shared, reduce_outer_axis[-1])

            dim_offset = (
                len(vthread_axis) + len(reduce_outer_axis) + 2
            )  # outer loops are: blck_fused, thrd_fused, vthread_axis, reduce_outer_axis
            if input_region.buffer.name in config.vectorize:
                vectorize = config.vectorize[input_region.buffer.name]
            else:
                vectorize = 1

            loops = sch.get_loops(cache_shared)
            if len(loops) == dim_offset:
                # handle fetching only one element
                loops.append(sch.add_unit_loop(schedule_block))
            assert len(loops) > dim_offset

            def prod(iterable):
                return reduce(lambda x, y: x * y, iterable, 1)

            _, tx, tv = sch.split(
                sch.fuse(*loops[dim_offset:]), factors=[None, int(prod(thread_factors)), vectorize]
            )
            sch.vectorize(tv)
            sch.bind(tx, "threadIdx.x")

        sch.reverse_compute_at(reg_tile, thrd_fused)

        sch.decompose_reduction(schedule_block, reduce_outer_axis[0])

        # resolve compute_at
        block_infos = try_inline_contiguous_spatial(sch, block_infos)
        if block_infos is None or len(block_infos) == 0:
            return None

        return sch

    def sch_mutiple_reductions_with_config(  # pylint: disable=too-many-locals,too-many-branches,too-many-return-statements
        self,
        func: tir.PrimFunc,
        config,
    ):
        block_factors = config.block
        thread_factors = config.thread
        reduce_therad_factors = config.reduce_thread

        sch = tir.Schedule(func)
        block_infos = normalize_prim_func(sch)
        block_infos = try_inline_contiguous_spatial(sch, block_infos)
        if block_infos is None or len(block_infos) == 0:
            return None

        def prod(iterable):
            return reduce(lambda x, y: x * y, iterable, 1)

        len_tx = prod(thread_factors) * prod(reduce_therad_factors)
        block_factor = prod(block_factors)

        dom_kind = block_infos[0].dom_kind()
        num_leading_s = len(dom_kind) - len(dom_kind.lstrip("S"))
        num_trailing_r = len(dom_kind) - len(dom_kind.rstrip("R"))

        # Align the number of block iters of the last block.
        num_last_block_iter = len(block_infos[-1].dom_kind())
        if num_last_block_iter < len(dom_kind):
            index_map = tir.IndexMap.from_func(
                lambda *iters: (
                    [tir.const(0, iters[0].dtype)] * (len(dom_kind) - num_last_block_iter)
                    + list(iters)
                ),
                ndim=num_last_block_iter,
            )
            sch.transform_block_layout(block_infos[-1].block_rv, index_map)

        try:
            # TODO: fix num_leading_s = 0 case
            assert num_trailing_r > 0
            for block in block_infos[1:-1]:
                assert block.dom_kind() == dom_kind
            assert block_infos[-1].is_injective()
            assert len(block_infos[-1].dom_kind()) <= len(dom_kind)
        except AssertionError:
            return None

        loops = sch.get_loops(block_infos[-1].block_rv)
        bx, _ = sch.split(sch.fuse(*loops[:num_leading_s]), factors=[None, block_factor])
        r_loop, tx = sch.split(loops[-1], [None, len_tx])
        sch.reorder(tx, r_loop)
        sch.bind(bx, "blockIdx.x")
        sch.bind(tx, "threadIdx.x")

        for block in reversed(block_infos[:-1]):
            block = block.block_rv
            for i, _ in enumerate(sch.get(block).writes):
                sch.set_scope(block, buffer_index=i, storage_scope="shared")
            sch.compute_at(block, bx, preserve_unit_loops=True)
            r_loop = sch.fuse(*sch.get_loops(block)[-num_trailing_r:])
            r_loop, tx = sch.split(r_loop, [None, len_tx])
            sch.reorder(tx, r_loop)
            sch.bind(tx, "threadIdx.x")

        return sch

    def apply_config(  # pylint: disable=too-many-locals,missing-docstring
        self,
        func: tir.PrimFunc,
        config,
    ) -> tir.Schedule:
        # check the number of reduction blocks
        sch = tir.Schedule(func)
        root_block = get_root_block(sch)
        blocks = sch.get_child_blocks(root_block)
        reduction_blocks = get_reduction_blocks(sch, blocks)
        if len(reduction_blocks) > 1:
            # schedule for multiple reduction blocks (e.g. softmax)
            return self.sch_mutiple_reductions_with_config(func, config)

        if any([t > 1 for t in config.reduce_thread]):
            # todo(lei) should implement block reduction schedule
            return self.sch_inner_reduction_with_config(func, config)
        else:
            return self.sch_outer_reduction_with_config(func, config)