dispatch.h 7.48 KB
Newer Older
shenzhe's avatar
shenzhe committed
1
2
3
4
5
6
7
8
9
10
11
#pragma once

#include <algorithm>

#include <hip/hip_runtime.h>

#include "legacy/include/flash.h"
#include "legacy/include/kernel_traits.h"
#include "legacy/include/static_switch.h"
#include "legacy/src/flash_fwd_b16_mla.h"

shenzhe's avatar
shenzhe committed
12
13
#include "legacy/src/flash_fwd_reduce.h"

shenzhe's avatar
shenzhe committed
14
15
namespace gfx93::fwd::dsa_mls {

shenzhe's avatar
shenzhe committed
16
17
18
19
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
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
template<typename Kernel_traits, const bool Tail, typename Params>
void run_dsa_mla_splitkv_reduce(Params& params, hipStream_t stream) {
    static_assert(Kernel_traits::kHeadDimV == 512,
                  "run_dsa_mla_splitkv_reduce only supports hdimv == 512");
    using Element = typename Kernel_traits::Element;
    using SplitkvAccumType = typename Kernel_traits::SplitkvAccumType;

    Flash_fwd_mla_reduce_params reduce_params;
    reduce_params.softmax_lse_ptr = params.softmax_lse_ptr;
    reduce_params.oaccum_ptr = params.oaccum_ptr;
    reduce_params.o_ptr = params.o_ptr;
    reduce_params.cu_seqlens_k = params.cu_seqlens_k;
    reduce_params.num_splits = params.num_splits;
    reduce_params.partition_size = params.partition_size;
    reduce_params.h = params.h;
    reduce_params.ngroups = params.ngroups;
    reduce_params.seqlen_q = params.seqlen_q;
    reduce_params.layout = params.layout;
    reduce_params.topk_length = params.topk_length;
    reduce_params.attn_sink = params.attn_sink;
    reduce_params.extra_topk_length = params.extra_topk_length;
    reduce_params.topk = params.topk;
    reduce_params.extra_topk = params.extra_topk;

    if (params.num_splits > 1) {
        dim3 block(256);
        dim3 grid(params.b * params.h * params.seqlen_q, 4);
        constexpr int MAX_NUM_SPLITS = 64;
        if (params.num_splits > MAX_NUM_SPLITS) {
            printf("\x1b[31mnum_splits %d is larger than limit %d, and thus won't execute the kernel\033[0m\n",
                   params.num_splits, MAX_NUM_SPLITS);
            return;
        }
        if (params.num_splits == 2) {
            ::flash_mla_splitkv_reduce_kernel<SplitkvAccumType, Element, 2, true, Tail, Kernel_traits::kHeadDimV>
                <<<grid, block, 0, stream>>>(reduce_params);
        } else if (params.num_splits == 4) {
            ::flash_mla_splitkv_reduce_kernel<SplitkvAccumType, Element, 4, true, Tail, Kernel_traits::kHeadDimV>
                <<<grid, block, 0, stream>>>(reduce_params);
        } else if (params.num_splits == 8) {
            ::flash_mla_splitkv_reduce_kernel<SplitkvAccumType, Element, 8, true, Tail, Kernel_traits::kHeadDimV>
                <<<grid, block, 0, stream>>>(reduce_params);
        } else if (params.num_splits == 16) {
            ::flash_mla_splitkv_reduce_kernel<SplitkvAccumType, Element, 16, true, Tail, Kernel_traits::kHeadDimV>
                <<<grid, block, 0, stream>>>(reduce_params);
        } else if (params.num_splits == 32) {
            ::flash_mla_splitkv_reduce_kernel<SplitkvAccumType, Element, 32, true, Tail, Kernel_traits::kHeadDimV>
                <<<grid, block, 0, stream>>>(reduce_params);
        } else if (params.num_splits == 64) {
            ::flash_mla_splitkv_reduce_kernel<SplitkvAccumType, Element, 64, true, Tail, Kernel_traits::kHeadDimV>
                <<<grid, block, 0, stream>>>(reduce_params);
        } else {
            printf("\x1b[31mnum_splits %d is not supported yet, and thus won't execute the kernel\033[0m\n",
                   params.num_splits);
        }
    }
}

shenzhe's avatar
shenzhe committed
74
template<typename T, int Headdim, int HeaddimV>
75
static inline void run_dsa_prefill_nopage_64_dispatch(Flash_fwd_mla_params_dsa& params, hipStream_t stream) {
shenzhe's avatar
shenzhe committed
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
    constexpr int kBlockM = 64;
    constexpr int kBlockN = 64;
    constexpr int WARP_M = 16;

    dim3 dimBlock;
    dimBlock.x = std::min((kBlockM / WARP_M) * 64, 1024);
    dimBlock.y = 1;
    dimBlock.z = 1;

    dim3 dimGrid;
    dimGrid.x = (params.seqlen_q + kBlockM - 1) / kBlockM;
    dimGrid.y = 1;
    dimGrid.z = params.b;

    using Kernel_traits = Flash_fwd_kernel_traits<
        Headdim, HeaddimV, kBlockM, kBlockN, 32, WARP_M, 64, 2,
        false, false, T, T>;

    constexpr bool Is_dropout = false;
    constexpr bool IsEvenMNConst = false;

shenzhe's avatar
shenzhe committed
97
98
99
100
101
102
    constexpr int REUSE_KV = 1;
    const bool has_extra = params.extra_sparse_indices != nullptr && params.extra_topk > 0;
    if (params.num_splits == 1) {
        BOOL_SWITCH(params.mtp > 1, Is_MTP, [&] {
            BOOL_SWITCH(params.is_causal, Is_causal, [&] {
                BOOL_SWITCH(has_extra, Has_extra, [&] {
103
104
105
106
107
108
                    BOOL_SWITCH(params.decode_use_c_load, DecodeCLoad, [&] {
                        flash::flash_fwd_mla_decode_kernel_gfx938_dsa_nopage_64<
                            Kernel_traits, true, Is_dropout, false, Is_causal,
                            IsEvenMNConst, true, false, Is_MTP, 0, DecodeCLoad, Has_extra, Flash_fwd_mla_params_dsa>
                            <<<dimGrid, dimBlock, 21 * 1024, stream>>>(params);
                    });
shenzhe's avatar
shenzhe committed
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
                });
            });
        });
    } else if (params.num_splits != 0) {
        dimGrid.y = params.num_splits;
        BOOL_SWITCH(params.mtp > 1, Is_MTP, [&] {
            BOOL_SWITCH(params.is_causal, Is_causal, [&] {
                BOOL_SWITCH(has_extra, Has_extra, [&] {
                    flash::flash_fwd_mla_decode_kernel_gfx938_dsa_nopage_64_splitkv<
                        Kernel_traits, true, Is_dropout, false, Is_causal,
                        IsEvenMNConst, true, false, Is_MTP, 0, Has_extra, Flash_fwd_mla_params_dsa>
                        <<<dimGrid, dimBlock, 21 * 1024, stream>>>(params);
                });
            });
        });
        run_dsa_mla_splitkv_reduce<Kernel_traits, false>(params, stream);
    } else {
        BOOL_SWITCH(params.mtp > 1, Is_MTP, [&] {
            BOOL_SWITCH(params.is_causal, Is_causal, [&] {
                if (params.topk == 2048) {
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
                    constexpr bool CanUseFastTopk2048 = Headdim == 576 && HeaddimV == 512;
                    if constexpr (CanUseFastTopk2048) {
                        if (params.seqlen_k < params.topk) {
                            flash::flash_fwd_mla_decode_kernel_gfx938_dsa_prefill_topk2048_fast_nopage_64<
                                Kernel_traits, true, Is_dropout, false, Is_causal,
                                IsEvenMNConst, true, false, Is_MTP, 0, true, Flash_fwd_mla_params_dsa>
                                <<<dimGrid, dimBlock, 21 * 1024, stream>>>(params);
                        } else {
                            flash::flash_fwd_mla_decode_kernel_gfx938_dsa_prefill_topk2048_fast_nopage_64<
                                Kernel_traits, true, Is_dropout, false, Is_causal,
                                IsEvenMNConst, true, false, Is_MTP, 0, false, Flash_fwd_mla_params_dsa>
                                <<<dimGrid, dimBlock, 21 * 1024, stream>>>(params);
                        }
                    } else {
                        flash::flash_fwd_mla_decode_kernel_gfx938_dsa_prefill_nopage_64<
                            Kernel_traits, true, Is_dropout, false, Is_causal,
                            IsEvenMNConst, true, false, Is_MTP, 0, Flash_fwd_mla_params_dsa>
                            <<<dimGrid, dimBlock, 21 * 1024, stream>>>(params);
                    }
shenzhe's avatar
shenzhe committed
148
149
150
151
152
153
154
                } else {
                    flash::flash_fwd_mla_decode_kernel_gfx938_dsa_prefill_nopage_64_topk1024<
                        Kernel_traits, true, Is_dropout, false, Is_causal,
                        IsEvenMNConst, true, false, Is_MTP, 0, Flash_fwd_mla_params_dsa>
                        <<<dimGrid, dimBlock, 21 * 1024, stream>>>(params);
                }
            });
shenzhe's avatar
shenzhe committed
155
        });
shenzhe's avatar
shenzhe committed
156
    }
shenzhe's avatar
shenzhe committed
157
158
159
}

}  // namespace gfx93::fwd::dsa_mls