splitkv_mla.cuh 43.2 KB
Newer Older
1
2
3
4
#pragma once

#include "splitkv_mla.h"

zhanghj2's avatar
zhanghj2 committed
5
6
7
8
9
10
// #include <cuda_fp8.h>
// #include <math_constants.h>
// #include <cutlass/barrier.h>
// #include <cutlass/arch/barrier.h>
// #include <cutlass/arch/reg_reconfig.h>
// #include <cutlass/cluster_launch.hpp>
11
12
13
14
15
16
17

#include <kerutils/kerutils.cuh>

#include "utils.h"
#include "components/dequant.h"
#include "components/helpers.h"
#include "config.h"
zhanghj2's avatar
zhanghj2 committed
18
#include "softmax.h"
19
20
using namespace cute;

zhanghj2's avatar
zhanghj2 committed
21
namespace gfx93::decode::sparse_fp8 {
zhanghj2's avatar
zhanghj2 committed
22
#define CUDART_L2E_F            1.442695041F
23
24
25

static constexpr float MAX_INIT_VAL = -1e30;    // Prevent (-inf) - (-inf) = nan

zhanghj2's avatar
zhanghj2 committed
26
27
28
29
30
31
32
template<ModelType MODEL_TYPE, int NUM_HEADS>
__device__ void KernelTemplate<MODEL_TYPE, NUM_HEADS>::compute_attn_1rowblock_splitkv_sparse_mla_fp8(const SparseAttnDecodeParams &params, const DecodingSchedMeta& sched_meta, int batch_idx)
{
    using Element = cutlass::bfloat16_t;
    using index_t = int64_t;
    const int tidx = threadIdx.x;
    const int lane_idx = tidx % 64;
33
    const int warp_idx = __builtin_amdgcn_readfirstlane(tidx / 64);
zhanghj2's avatar
zhanghj2 committed
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
    const int head_block_idx = NUM_M_BLOCKS == 1 ? 0 : blockIdx.x;
    const int s_q_idx = blockIdx.y;
    extern __shared__ char shared_memory[];
    SharedMemoryPlan &plan = *reinterpret_cast<SharedMemoryPlan*>(shared_memory);

    const index_t row_offset_q = batch_idx * params.stride_q_b + head_block_idx * BLOCK_M * params.stride_q_h_q + s_q_idx * params.stride_q_s_q;
    Tensor gQ = make_tensor(make_gmem_ptr(reinterpret_cast<Element *>(params.q) + row_offset_q),
                            Shape<Int<BLOCK_M>, Int<HEAD_DIM_K>>{},
                            make_stride(params.stride_q_h_q, _1{}));
    const index_t row_offset_k = 0;
    Tensor gK = make_tensor(make_gmem_ptr(reinterpret_cast<uint8_t *>(params.kv) + row_offset_k),
                            Shape<Int<TOPK_BLOCK_SIZE>, Int<HEAD_DIM_K>>{},
                            make_stride(params.stride_kv_row, _1{}));
    Tensor sV = make_tensor(make_smem_ptr(plan.smem_v.data()), SmemLayoutV{});
    Tensor sK = make_tensor(make_smem_ptr(plan.smem_v.data()), SmemLayoutK{});
    Tensor sP = make_tensor(make_smem_ptr(plan.smem_p.data()), SmemLayoutP{});    
    Tensor sVt = make_tensor(sV.data(), SmemLayoutVtransposed{});
    Tensor sVtNoSwizzle = make_tensor(sV.data(), SmemLayoutVtransposedNoSwizzle{});
    Tensor sRow_max_reduce_buffer = make_tensor(make_smem_ptr(plan.smem_row_max.data()), SmemLayoutRow{});    
    Tensor sRow_sum_reduce_buffer = make_tensor(make_smem_ptr(plan.smem_row_sum.data()), SmemLayoutRow{});

    const index_t row_offset_topk =  batch_idx * params.stride_indices_b + s_q_idx * params.stride_indices_s_q; // todo
    int* gIndices = reinterpret_cast<int *>(params.indices) + row_offset_topk;
    int* gExtraIndices = params.extra_indices + batch_idx*params.stride_extra_indices_b + s_q_idx*params.stride_extra_indices_s_q; // (extra_topk) : (1)
    TiledMMA tiled_mma = TiledMma{}; 
    auto thr_mma = tiled_mma.get_thread_slice(tidx);
    TiledMMA tiled_mma_16x16x32 = TiledMma_16_16_32{}; 
    auto thr_mma_16x16x32 = tiled_mma_16x16x32.get_thread_slice(tidx);
    TiledMMA tiled_mma_o = TiledMma_O{}; 
    auto thr_mma_o = tiled_mma_o.get_thread_slice(tidx);
64
#if 0
zhanghj2's avatar
zhanghj2 committed
65
66
67
68
69
70
71
72
73
74
    // load Q
    auto gmem_tiled_copy_Q = make_tiled_copy_A(Copy_Atom<DefaultCopy, Element>{}, tiled_mma);
    auto gmem_thr_copy_Q = gmem_tiled_copy_Q.get_thread_slice(tidx);
    Tensor tSgQ = gmem_thr_copy_Q.partition_S(gQ);
    Tensor tSrQ = thr_mma.partition_fragment_A(gQ);
    Tensor cQ = make_identity_tensor(make_shape(size<0>(gQ), size<1>(gQ)));
    Tensor tQcQ = gmem_thr_copy_Q.partition_S(cQ);
    Tensor tQpQ = make_tensor<bool>(make_shape(size<2>(tSgQ)));
    flash::copy</*Is_even_MN=*/false, /*Is_even_K=*/true>(gmem_tiled_copy_Q, tSgQ, tSrQ, tQcQ, tQpQ, params.h_q - head_block_idx * BLOCK_M);
    __syncthreads();
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
#else
    Tensor tSrQ = thr_mma.partition_fragment_A(gQ);
    // 需要的最大空间为 16 * 576 * 2
    Element* s_q = reinterpret_cast<Element *>(shared_memory);
    auto lds_direct_copy_q = [&](const int k_idx, const int offset_k) {
        // static_assert(offset_k == 0 || offset_k == 1);
        // static_assert(k_idx < 3);
        struct PtrWrapper {
            uint32_t former;
            uint32_t latter;
        };
        PtrWrapper glob_ptr;
        *(uint64_t*)&glob_ptr = reinterpret_cast<uint64_t>(gQ.data().get());
        uint32x4_t global_addr = {0};
        global_addr[0] = (glob_ptr.former);
        global_addr[1] = (glob_ptr.latter);
        global_addr[2] = 0x80000000;
        global_addr[3] = 0x00020000;
        constexpr int elements_per_thread = 8;
        constexpr int bytes_per_warp = 64 * 8 * 2;
        constexpr int bytes_per_block = bytes_per_warp * 4;
        const int row_idx = lane_idx % 16;
        const int col_idx = lane_idx / 16;
        const int row_offset = row_idx;
        if constexpr (MODEL_TYPE == ModelType::V32)
        {
            int col_offset;
            if (k_idx == 2)
            {
                col_offset = k_idx * 256 + warp_idx * 8 + col_idx * 16;
            }
            else
            {
                col_offset = k_idx * 256 + warp_idx * 64 + col_idx * 16 + offset_k * 8;
            }
            int offset_v = (row_offset * params.stride_q_h_q + col_offset) * 2;
            if (head_block_idx * BLOCK_M + row_idx >= params.h_q) {
                offset_v = -1;
            }
            if (k_idx == 2 && warp_idx >= 2)
            {
                offset_v = -1;
            }
            const int offset_s = 0;
            int ldsAddrPerWave = reinterpret_cast<size_t>(s_q) + warp_idx * bytes_per_warp + k_idx * bytes_per_block
                + offset_k * 3 * bytes_per_block;
            asm volatile(
                "s_mov_b32 m0, %1 \n\t"
zhanghj2's avatar
zhanghj2 committed
123
                "s_nop 0 \n\t"
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
                "buffer_load_dwordx4 %0, %2, %3 ,offen  offset:0, lds \n" ::"v"(offset_v),
                "s"(ldsAddrPerWave), "s"(global_addr), "s"(offset_s)
            :);  
        }
        else
        {
            const int col_offset = k_idx * 256 + warp_idx * 64 + col_idx * 16 + offset_k * 8;
            int offset_v = (row_offset * params.stride_q_h_q + col_offset) * 2;
            if (head_block_idx * BLOCK_M + row_idx >= params.h_q) {
                offset_v = -1;
            }

            const int offset_s = 0;
            int ldsAddrPerWave = reinterpret_cast<size_t>(s_q) + warp_idx * bytes_per_warp + k_idx * bytes_per_block
                + offset_k * 2 * bytes_per_block;
            asm volatile(
                "s_mov_b32 m0, %1 \n\t"
zhanghj2's avatar
zhanghj2 committed
141
                "s_nop 0 \n\t"
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
                "buffer_load_dwordx4 %0, %2, %3 ,offen  offset:0, lds \n" ::"v"(offset_v),
                "s"(ldsAddrPerWave), "s"(global_addr), "s"(offset_s)
            :);  
        }
  
    };

    if constexpr (MODEL_TYPE == ModelType::V32)
    {
        // __builtin_amdgcn_sched_barrier(0);
        lds_direct_copy_q(0, 0);
        lds_direct_copy_q(1, 0);
        lds_direct_copy_q(0, 1);
        lds_direct_copy_q(1, 1);
        lds_direct_copy_q(2, 0);
        Element* s_q_read_ptr = s_q + lane_idx * 8;
        asm volatile("s_waitcnt vmcnt(4) \n s_barrier"); 
        for (int k = 0; k < 4; k++)
        {
            for (int i = 0; i < 8; i++)
            {
                tSrQ(i, 0, k) = s_q_read_ptr[i];
            }
            s_q_read_ptr += 16 * 32;
        }
        asm volatile("s_waitcnt vmcnt(3) \n s_barrier"); 
        for (int k = 4; k < 8; k++)
        {
            for (int i = 0; i < 8; i++)
            {
                tSrQ(i, 0, k) = s_q_read_ptr[i];
            }
            s_q_read_ptr += 16 * 32;
        }
        asm volatile("s_waitcnt vmcnt(2) \n s_barrier"); 
        s_q_read_ptr = s_q + lane_idx * 8 + 3 * 4 * 16 * 4 * 8;
        for (int k = 0; k < 4; k++)
        {
            for (int i = 8; i < 16; i++)
            {
                tSrQ(i, 0, k) = s_q_read_ptr[i - 8];
            }
            s_q_read_ptr += 16 * 32;
        }
        asm volatile("s_waitcnt vmcnt(1) \n s_barrier"); 
        for (int k = 4; k < 8; k++)
        {
            for (int i = 8; i < 16; i++)
            {
                tSrQ(i, 0, k) = s_q_read_ptr[i - 8];
            }
            s_q_read_ptr += 16 * 32;
        }
        asm volatile("s_waitcnt vmcnt(0) \n s_barrier"); 
        s_q_read_ptr = s_q + lane_idx * 8 + 2 * 4 * 16 * 4 * 8;
        for (int k = 8; k < 9; k++)
        {
            for (int i = 0; i < 8; i++)
            {
                tSrQ(i, 0, k) = s_q_read_ptr[i];
            }
            s_q_read_ptr += 16 * 32;
        }
        for (int k = 8; k < 9; k++)
        {
            for (int i = 8; i < 16; i++)
            {
                tSrQ(i, 0, k) = s_q_read_ptr[i-8];
            }
            s_q_read_ptr += 16 * 32;
        }
        // __syncthreads();
        asm volatile("s_waitcnt lgkmcnt(0) \n s_barrier"); 
        // __builtin_amdgcn_sched_barrier(0);
    }
    else
    {    
        // __builtin_amdgcn_sched_barrier(0);
        lds_direct_copy_q(0, 0);
        lds_direct_copy_q(1, 0);
        lds_direct_copy_q(0, 1);
        lds_direct_copy_q(1, 1);

        Element* s_q_read_ptr = s_q + lane_idx * 8;
        asm volatile("s_waitcnt vmcnt(3) \n s_barrier"); 
        for (int k = 0; k < 4; k++)
        {
            for (int i = 0; i < 8; i++)
            {
                tSrQ(i, 0, k) = s_q_read_ptr[i];
            }
            s_q_read_ptr += 16 * 32;
        }
        asm volatile("s_waitcnt vmcnt(2) \n s_barrier"); 
        for (int k = 4; k < 8; k++)
        {
            for (int i = 0; i < 8; i++)
            {
                tSrQ(i, 0, k) = s_q_read_ptr[i];
            }
            s_q_read_ptr += 16 * 32;
        }
        asm volatile("s_waitcnt vmcnt(1) \n s_barrier"); 
        for (int k = 0; k < 4; k++)
        {
            for (int i = 8; i < 16; i++)
            {
                tSrQ(i, 0, k) = s_q_read_ptr[i - 8];
            }
            s_q_read_ptr += 16 * 32;
        }
        asm volatile("s_waitcnt vmcnt(0) \n s_barrier"); 
        for (int k = 4; k < 8; k++)
        {
            for (int i = 8; i < 16; i++)
            {
                tSrQ(i, 0, k) = s_q_read_ptr[i - 8];
            }
            s_q_read_ptr += 16 * 32;
        }
        asm volatile("s_waitcnt lgkmcnt(0) \n s_barrier"); 
        // __builtin_amdgcn_sched_barrier(0);
    }
   
#endif
zhanghj2's avatar
zhanghj2 committed
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
    // zhj debug
    // if (head_block_idx == 0)
    // {
    //     printf("tidx = %d, %.2f %.2f %.2f %.2f \n", tidx, float(tSrQ(0)), float(tSrQ(1)), float(tSrQ(2)), float(tSrQ(3)));
    // }
    Tensor tSrK  = thr_mma.partition_fragment_B(gK); 
    auto smem_tiled_copy_K = make_tiled_copy_B(Copy_Atom<DefaultCopy, Element>{}, tiled_mma_16x16x32);
    auto smem_thr_copy_K = smem_tiled_copy_K.get_thread_slice(tidx);
    Tensor tOsV = smem_thr_copy_K.partition_S(sK);

    auto smem_tiled_copy_V = make_tiled_copy_B(Copy_Atom<GFX928_DS_READ_DS_M32x16_B16, Element>{}, tiled_mma_o);
    auto smem_thr_copy_V = smem_tiled_copy_V.get_thread_slice(tidx);
    Tensor tOsVt = smem_thr_copy_V.partition_S(sVt);
    Tensor tOrVt  = thr_mma_o.partition_fragment_B(sVt);
    Tensor tOrVt_copy_view = smem_thr_copy_V.retile_D(tOrVt);

    const auto gK_data = gK.data();
    typedef unsigned int __hip_fp8x4_storage_t;
    typedef unsigned short int __hip_fp8x2_storage_t;
    typedef unsigned char __hip_fp8_storage_t;
    typedef  __fp16  __fp16x8_t __attribute__((ext_vector_type(8)));
288
    typedef  __fp16  __fp16x4_t __attribute__((ext_vector_type(4)));
289
    typedef  int  v2i  __attribute__((ext_vector_type(2)));
zhanghj2's avatar
zhanghj2 committed
290
291
292
293
294
295
296
297
298
299
    
    union Fp8_storage{
        __fp16x8_t data_128;
        __hip_fp8x4_storage_t fp8_array[4];
    };

    union bf16_storage{
        uint32x4_t data_128;
        uint16_t data_array[8];
    };
300
301
302
    struct MainloopArgs {
        int start_block_idx, end_block_idx;
        bool is_no_split;
zhanghj2's avatar
zhanghj2 committed
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
        // The following fields are only valid for MODEL1
        int topk_length, extra_topk_length, num_orig_kv_blocks;
    };

    auto get_cur_req_info = [&](int batch_idx) -> MainloopArgs {
        MainloopArgs args;
        int total_topk_padded;
        if constexpr (MODEL_TYPE == ModelType::V32) {
            total_topk_padded = params.topk;
        } else {
            int topk_length = params.topk_length ? __ldg(params.topk_length + batch_idx) : params.topk;
            int orig_topk_padded = max(ku::ceil(topk_length, (int)TOPK_BLOCK_SIZE), (int)TOPK_BLOCK_SIZE);
            int extra_topk_length = params.extra_topk_length ? __ldg(params.extra_topk_length + batch_idx) : params.extra_topk;
            total_topk_padded = orig_topk_padded + ku::ceil(extra_topk_length, (int)TOPK_BLOCK_SIZE);
            args.topk_length = topk_length;
            args.extra_topk_length = extra_topk_length;
            args.num_orig_kv_blocks = orig_topk_padded / TOPK_BLOCK_SIZE;
        }

        args.start_block_idx = batch_idx == sched_meta.begin_req_idx ? sched_meta.begin_block_idx : 0;
        args.end_block_idx = batch_idx == sched_meta.end_req_idx ? sched_meta.end_block_idx : total_topk_padded / TOPK_BLOCK_SIZE;
        args.is_no_split = batch_idx == sched_meta.begin_req_idx ? !sched_meta.is_first_req_splitted : (batch_idx == sched_meta.end_req_idx ? !sched_meta.is_last_req_splitted : true);

        return args;
    };
zhanghj2's avatar
zhanghj2 committed
329
330
331
332

    Tensor acc_o = partition_fragment_C(tiled_mma_o, Shape<Int<BLOCK_M>, Int<HEAD_DIM_V>>{});
    clear(acc_o);
    flash::Softmax<size<1>(acc_o)> softmax;
333
    MainloopArgs args = get_cur_req_info(batch_idx);
zhanghj2's avatar
zhanghj2 committed
334
335
336
337
338

    struct IsOrigBlock {};
    struct IsExtraBlock {};
    auto process_one_block = [&](int block_idx, auto is_extra_block_t) {
        static constexpr bool IS_EXTRA_BLOCK = std::is_same_v<decltype(is_extra_block_t), IsExtraBlock>;
zhanghj2's avatar
zhanghj2 committed
339
340
341
        Tensor acc_s = partition_fragment_C(tiled_mma, Shape<Int<BLOCK_M>, Int<TOPK_BLOCK_SIZE>>{}); 
        clear(acc_s);
        int col_idx = lane_idx / 16;
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361

        int* indices_base;
        int page_block_size;
        int64_t k_block_stride, k_row_stride;
        uint8_t* k_ptr;
        if constexpr (!IS_EXTRA_BLOCK) {
            indices_base = gIndices + (block_idx)*TOPK_BLOCK_SIZE;
            page_block_size = params.page_block_size;
            k_block_stride = params.stride_kv_block;
            k_row_stride = params.stride_kv_row;
            k_ptr = (uint8_t*)params.kv;
        } else {
           indices_base = gExtraIndices + (block_idx-args.num_orig_kv_blocks)*TOPK_BLOCK_SIZE;
           page_block_size = params.extra_page_block_size;
           k_block_stride = params.stride_extra_kv_block;
           k_row_stride = params.stride_extra_kv_row;
           k_ptr = (uint8_t*)params.extra_kv;
        }
        [[maybe_unused]] int topk_length = IS_EXTRA_BLOCK ? args.extra_topk_length : args.topk_length;
        [[maybe_unused]] int rel_block_idx = IS_EXTRA_BLOCK ? (block_idx - args.num_orig_kv_blocks) : block_idx;
zhanghj2's avatar
zhanghj2 committed
362
        int token_index = indices_base[(lane_idx % 16) + warp_idx * 16];
363
        if constexpr (MODEL_TYPE == ModelType::MODEL1) {
zhanghj2's avatar
zhanghj2 committed
364
            if (rel_block_idx*TOPK_BLOCK_SIZE + (lane_idx % 16) + warp_idx * 16 >= topk_length)
365
            {
zhanghj2's avatar
zhanghj2 committed
366
                token_index = -1;
367
368
            }
        }
zhanghj2's avatar
zhanghj2 committed
369
370
        int block_index = token_index == -1 ? 0 : (int)((uint32_t)token_index/(uint32_t)page_block_size);   // Use uint32_t division and mod to improve performance        const int token_indexrel_idx_in_block = (token_index + page_block_size) % page_block_size;
        int rel_idx_in_block = (uint32_t)token_index % (uint32_t)page_block_size;   // NOTE When token_index is -1 (UINT_MAX), UINT_MAX%page_block_size < page_block_size, so there will be no illegal-memory-access error
371
        const index_t offset_k = (index_t)(block_index) * k_block_stride;
zhanghj2's avatar
zhanghj2 committed
372
        uint8_t* gK_base;
373
374
        float scales[NUM_SCALES];
        if constexpr (MODEL_TYPE == ModelType::V32) {
zhanghj2's avatar
zhanghj2 committed
375
            gK_base = k_ptr + offset_k + rel_idx_in_block * k_row_stride;
376
377
378
379
380
381
382
383
384
385
386
            float* scale_ptr = (float*)(gK_base + HEAD_DIM_NOPE);
            static_assert(NUM_SCALES == 4);
            static_assert(HEAD_DIM_NOPE == 512);
            if (token_index == -1)
            {
                for (int i = 0; i < NUM_SCALES; i++)
                {
                    scales[i] = 0.0f;
                }
            }
            else
zhanghj2's avatar
zhanghj2 committed
387
            {
388
389
390
391
392
393
                for (int i = 0; i < NUM_SCALES; i++)
                {
                    scales[i] = scale_ptr[i];
                }
            }
        } else {
zhanghj2's avatar
zhanghj2 committed
394
            gK_base = k_ptr + offset_k + rel_idx_in_block*(HEAD_DIM_NOPE + HEAD_DIM_ROPE*2);;
395
            static_assert(NUM_SCALES == 8);
396
            #if 1
zhanghj2's avatar
zhanghj2 committed
397
            uint8_t* scale_ptr =  k_ptr + offset_k + page_block_size*(HEAD_DIM_NOPE+HEAD_DIM_ROPE*2) + rel_idx_in_block*NUM_SCALES;
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
            if (token_index == -1)
            {
                for (int i = 0; i < NUM_SCALES; i++)
                {
                    scales[i] = 0.0f;
                }
            }
            else
            {
                union Scale_e8m0
                {
                    __fp16x4_t tmp;
                    __hip_fp8_storage_t fp8_e8m0[NUM_SCALES];
                };
                Scale_e8m0 scale_e8m0;
                scale_e8m0.tmp = *(__fp16x4_t*)(scale_ptr);
                union Fp32{
                    uint32_t as_bits;
                    float as_value;
                };
                Fp32 fp32;
zhanghj2's avatar
zhanghj2 committed
419
                for (int i = 0; i < NUM_SCALES - 1; i++)
420
421
422
423
                {
                    fp32.as_bits = (scale_e8m0.fp8_e8m0[i] << 23);
                    scales[i] = fp32.as_value;
                }
zhanghj2's avatar
zhanghj2 committed
424

zhanghj2's avatar
zhanghj2 committed
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
            #else
            struct PtrWrapper {
                uint32_t former;
                uint32_t latter;
            };
            PtrWrapper glob_ptr;
            *(uint64_t*)&glob_ptr = reinterpret_cast<uint64_t>(k_ptr + offset_k + page_block_size*(HEAD_DIM_NOPE+HEAD_DIM_ROPE*2));
            uint32x4_t global_addr = {0};
            global_addr[0] = (glob_ptr.former);
            global_addr[1] = (glob_ptr.latter);
            global_addr[2] = 0x80000000;
            global_addr[3] = 0x00020000;
            int offset_v = token_index == -1 ? -1: rel_idx_in_block*NUM_SCALES;
            union Scale_e8m0
            {
                v2i tmp;
                __hip_fp8_storage_t fp8_e8m0[NUM_SCALES];
            };
            Scale_e8m0 scale_e8m0;
            scale_e8m0.tmp = __builtin_amdgcn_buffer_load_dwordx2(global_addr, 0, offset_v, 0, 0);
            union Fp32{
                uint32_t as_bits;
                float as_value;
            };
            Fp32 fp32;
            for (int i = 0; i < NUM_SCALES - 1; i++)
            {
                fp32.as_bits = (scale_e8m0.fp8_e8m0[i] << 23);
                scales[i] = fp32.as_value;
            }
            #endif
zhanghj2's avatar
zhanghj2 committed
457
458
459
460
461

                // if (block0() && threadIdx.x < 64)
                // {
                //     printf("tidx = %d, %.3f %.2f %.2f \n",tidx, scales[0], scales[1], scales[2]);
                // }
zhanghj2's avatar
zhanghj2 committed
462
        }
463

zhanghj2's avatar
zhanghj2 committed
464
        // // zhj debug
zhanghj2's avatar
zhanghj2 committed
465
466
        // if (head_block_idx == 0 && threadIdx.x < 64)
        // {
zhanghj2's avatar
zhanghj2 committed
467
        //     printf("tidx = %d, %.2f %.2f %.2f %.2f %d offset_k = %d rel_idx_in_block = %d params.stride_kv_row = %d %p params.kv  = %p \n", tidx, float(scales[0]), float(scales[1]), float(scales[2]), float(scales[3]), 
zhanghj2's avatar
zhanghj2 committed
468
469
        //     token_index,
        //     offset_k,
zhanghj2's avatar
zhanghj2 committed
470
        //     rel_idx_in_block,
zhanghj2's avatar
zhanghj2 committed
471
472
473
474
475
        //     params.stride_kv_row,
        //     gK_base,
        //     params.kv 
        //     );
        // }
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
        auto dequant_to_bf16 = [&](const Fp8_storage& data0, const float& kv_scale, int idx) -> std::tuple<Element, Element, Element, Element> {
            #if defined(__gfx938__)
            auto res1 =  __builtin_amdgcn_cvt_pk_f32_fp8(data0.fp8_array[idx/4], false);
            auto res2 =  __builtin_amdgcn_cvt_pk_f32_fp8(data0.fp8_array[idx/4], true);

            auto f1 = res1[0];
            auto f2 = res1[1];
            auto f3 = res2[0];
            auto f4 = res2[1];           
            #else
            const auto fp8x2_low = *reinterpret_cast<const __hip_fp8x2_storage_t*>(&data0.fp8_array[idx / 4]);
            const auto fp8x2_high = *(reinterpret_cast<const __hip_fp8x2_storage_t*>(&(data0.fp8_array[idx / 4])) + 1);
            auto f1 =  flash::fp8e4m3_to_fp32(static_cast<__hip_fp8_storage_t>((fp8x2_low << 8) >> 8));
            auto f2 =  flash::fp8e4m3_to_fp32(static_cast<__hip_fp8_storage_t>((fp8x2_low >> 8)));
            auto f3 =  flash::fp8e4m3_to_fp32(static_cast<__hip_fp8_storage_t>((fp8x2_high << 8) >> 8));
            auto f4 =  flash::fp8e4m3_to_fp32(static_cast<__hip_fp8_storage_t>((fp8x2_high) >> 8));
            #endif

            f1 *= kv_scale;
            f2 *= kv_scale;
            f3 *= kv_scale;
            f4 *= kv_scale;
            cutlass::NumericConverter<Element, float, cutlass::FloatRoundStyle::round_toward_zero> convert_;
            auto rst0 = convert_(f1);
            auto rst1 = convert_(f2);
            auto rst2 = convert_(f3);
            auto rst3 = convert_(f4);

            return {rst0, rst1, rst2, rst3};
        };
        if constexpr (MODEL_TYPE == ModelType::V32)
zhanghj2's avatar
zhanghj2 committed
507
        {
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
            Fp8_storage data[4];
            for (int k_idx = 4; k_idx < 8; k_idx++) 
            {
                if (token_index == -1) {
                    data[k_idx - 4].data_128 = {0};
                } else {
                    data[k_idx - 4].data_128 = *((__fp16x8_t*)(gK_base + col_idx * 16 + k_idx * 64));
                }
            }
            for (int k_idx = 4; k_idx < 8; k_idx++)
            {
                for (int j = 0; j < 16; j+=4) {
                    auto [rst0, rst1, rst2, rst3] = dequant_to_bf16(data[k_idx - 4], scales[k_idx / 2], j);
                    
                    tSrK(j, 0, k_idx) = rst0;
                    tSrK(j + 1, 0, k_idx) = rst1;
                    tSrK(j + 2, 0, k_idx) = rst2;
                    tSrK(j + 3, 0, k_idx) = rst3;

                }
                // cute::copy(smem_tiled_copy_K, tSrK(_, _, k_idx), tOsV(_, _, k_idx % 4));
                // __builtin_amdgcn_sched_barrier(0);

                #pragma unroll
                for (int j = 0; j < 8; j++) {
                    tOsV(j, 0, (k_idx - 4) * 2) =  tSrK(j, 0, k_idx);
                }
                #pragma unroll     
                for (int j = 8; j < 16; j++) {
                    tOsV(j - 8, 0, (k_idx - 4) * 2 + 1) =  tSrK(j, 0, k_idx);
                }    
                // __builtin_amdgcn_sched_barrier(0);

                cute::gemm(tiled_mma, tSrQ(_, _, k_idx), tSrK(_, _, k_idx), acc_s);
zhanghj2's avatar
zhanghj2 committed
542
543
            }
        }
544
        else
zhanghj2's avatar
zhanghj2 committed
545
        {
546
547
548
549
550
551
552
553
            Fp8_storage data[3];
            for (int k_idx = 4; k_idx < 7; k_idx++) 
            {
                if (token_index == -1) {
                    data[k_idx - 4].data_128 = {0};
                } else {
                    data[k_idx - 4].data_128 = *((__fp16x8_t*)(gK_base + col_idx * 16 + k_idx * 64));
                }
zhanghj2's avatar
zhanghj2 committed
554
            }
555
556
557
            for (int k_idx = 4; k_idx < 7; k_idx++)
            {
                for (int j = 0; j < 16; j+=4) {
zhanghj2's avatar
zhanghj2 committed
558
                    auto [rst0, rst1, rst2, rst3] = dequant_to_bf16(data[k_idx - 4], scales[k_idx], j);
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
                    
                    tSrK(j, 0, k_idx) = rst0;
                    tSrK(j + 1, 0, k_idx) = rst1;
                    tSrK(j + 2, 0, k_idx) = rst2;
                    tSrK(j + 3, 0, k_idx) = rst3;

                }
                // cute::copy(smem_tiled_copy_K, tSrK(_, _, k_idx), tOsV(_, _, k_idx % 4));
                // __builtin_amdgcn_sched_barrier(0);

                #pragma unroll
                for (int j = 0; j < 8; j++) {
                    tOsV(j, 0, (k_idx - 4) * 2) =  tSrK(j, 0, k_idx);
                }
                #pragma unroll     
                for (int j = 8; j < 16; j++) {
                    tOsV(j - 8, 0, (k_idx - 4) * 2 + 1) =  tSrK(j, 0, k_idx);
                }    
                // __builtin_amdgcn_sched_barrier(0);

                cute::gemm(tiled_mma, tSrQ(_, _, k_idx), tSrK(_, _, k_idx), acc_s);
zhanghj2's avatar
zhanghj2 committed
580
            }
zhanghj2's avatar
zhanghj2 committed
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
        //             if (head_block_idx == 0)
        // {
        //     printf("tidx = %d, %.2f %.2f %.2f %.2f \n", tidx, float(acc_s(0)), float(acc_s(1)), float(acc_s(2)), float(acc_s(3)));
        // }
            bf16_storage bf16_data0;
            bf16_storage bf16_data1;
            if (token_index == -1)
            {
                bf16_data0.data_128 = {0};
                bf16_data1.data_128 = {0};
            }
            else
            {
                bf16_data0.data_128 = *((uint32x4_t*)(gK_base + col_idx * 16 * 2 + HEAD_DIM_NOPE));
                bf16_data1.data_128 = *((uint32x4_t*)(gK_base + col_idx * 16 * 2 + 8 * 2 + HEAD_DIM_NOPE));
            }
            for (int j = 0; j < 8; j++) {
                auto rst = cutlass::bfloat16_t::bitcast(bf16_data0.data_array[j]);
                tSrK(j, 0, 7) = rst;
            }
            for (int j = 8; j < 16; j++) {
                auto rst = cutlass::bfloat16_t::bitcast(bf16_data1.data_array[j - 8]);
                tSrK(j, 0, 7) = rst;
            }
            constexpr static int k_idx = 7;
zhanghj2's avatar
zhanghj2 committed
606
            // if (block0())
zhanghj2's avatar
zhanghj2 committed
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
            // {
            //     printf(" %.4f %.4f %.4f %.4f \n", 
            //         float(tSrK(0, 0, 7)),
            //         float(tSrK(1, 0, 7)),
            //         float(tSrK(2, 0, 7)),
            //         float(tSrK(3, 0, 7))

            //     );
            // }
            cute::gemm(tiled_mma, tSrQ(_, _, 7), tSrK(_, _, 7), acc_s);    
            #pragma unroll
            for (int j = 0; j < 8; j++) {
                // tOsV(j, 0, (k_idx - 4) * 2) =  Element(j);
                tOsV(j, 0, (k_idx - 4) * 2) =  tSrK(j, 0, k_idx);
            }
            #pragma unroll     
            for (int j = 8; j < 16; j++) {
                tOsV(j - 8, 0, (k_idx - 4) * 2 + 1) =  tSrK(j, 0, k_idx);
            }  
zhanghj2's avatar
zhanghj2 committed
626
        }
627

zhanghj2's avatar
zhanghj2 committed
628
629
        __syncthreads();
        __builtin_amdgcn_sched_barrier(0);
zhanghj2's avatar
zhanghj2 committed
630
631
632
633
634
635
636
637
        flash::__ds_read_m32x16_row_col_rrow_alt<0, 0, 2>(tOsVt, tOrVt_copy_view);
        flash::__ds_read_m32x16_row_col_rrow_alt<0, 1, 2>(tOsVt, tOrVt_copy_view);
        flash::__ds_read_m32x16_row_col_rrow_alt<0, 2, 2>(tOsVt, tOrVt_copy_view);
        flash::__ds_read_m32x16_row_col_rrow_alt<0, 3, 2>(tOsVt, tOrVt_copy_view);
        flash::__ds_read_m32x16_row_col_rrow_alt<1, 0, 3>(tOsVt, tOrVt_copy_view);
        flash::__ds_read_m32x16_row_col_rrow_alt<1, 1, 3>(tOsVt, tOrVt_copy_view);
        flash::__ds_read_m32x16_row_col_rrow_alt<1, 2, 3>(tOsVt, tOrVt_copy_view);
        flash::__ds_read_m32x16_row_col_rrow_alt<1, 3, 3>(tOsVt, tOrVt_copy_view);
zhanghj2's avatar
zhanghj2 committed
638

zhanghj2's avatar
zhanghj2 committed
639
        __syncthreads();
zhanghj2's avatar
zhanghj2 committed
640
641
642
643
644
645
        // if (block0() && threadIdx.x >= 192)
        // {
        //     printf(" %.4f %.4f %.4f %.4f %p %p\n", 
        //         float(tOsVt(0, 3, 0)), float(tOsVt(1, 3, 0)), float( tSrK(8, 0, 7)), float( tSrK(9, 0, 7)),  
        //         &(tOsVt(0, 1, 3)), &(tOsV(0, 0, 7)));
        // }
zhanghj2's avatar
zhanghj2 committed
646
        __builtin_amdgcn_sched_barrier(0);
647
        Fp8_storage data[4];
zhanghj2's avatar
zhanghj2 committed
648
649
650
651
652
653
654
655
656
657
658
659
        // __ds_read_m64x16_row_col_rrow<0, 0, 4>(tOsVt, tOrVt_copy_view);
        for (int k_idx = 0; k_idx < 4; k_idx++)
        {
            if (token_index == -1) {
                data[k_idx].data_128 = {0};
            } else {
                data[k_idx].data_128 = *((__fp16x8_t*)(gK_base + col_idx * 16 + k_idx * 64));
            }        
        }
        for (int k_idx = 0; k_idx < 4; k_idx++)
        {
            for (int j = 0; j < 16; j+=4) {
660
                auto [rst0, rst1, rst2, rst3] = dequant_to_bf16(data[k_idx], scales[MODEL_TYPE == ModelType::V32 ? k_idx / 2 : k_idx], j);
zhanghj2's avatar
zhanghj2 committed
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
                tSrK(j, 0, k_idx) = rst0;
                tSrK(j + 1, 0, k_idx) = rst1;
                tSrK(j + 2, 0, k_idx) = rst2;
                tSrK(j + 3, 0, k_idx) = rst3;

            }
            // for (int j = 0; j < 16; j++) {
            //     tOsV(j % 8, 0, (k_idx % 4) * 2 + ( j / 8) ) = tSrK(j, 0, k_idx);
            // }
            // __builtin_amdgcn_sched_barrier(0);
            #pragma unroll
            for (int j = 0; j < 8; j++) {
                tOsV(j, 0, k_idx * 2) =  tSrK(j, 0, k_idx);
            }
            #pragma unroll     
            for (int j = 8; j < 16; j++) {
                tOsV(j - 8, 0, k_idx * 2 + 1) =  tSrK(j, 0, k_idx);
            }   
            // __builtin_amdgcn_sched_barrier(0);  
            cute::gemm(tiled_mma, tSrQ(_, _, k_idx), tSrK(_, _, k_idx), acc_s);
        }

        __syncthreads();
zhanghj2's avatar
zhanghj2 committed
684
685
686
687
        flash::__ds_read_m32x16_row_col_rrow_alt<0, 0, 0>(tOsVt, tOrVt_copy_view);
        flash::__ds_read_m32x16_row_col_rrow_alt<0, 1, 0>(tOsVt, tOrVt_copy_view);
        flash::__ds_read_m32x16_row_col_rrow_alt<0, 2, 0>(tOsVt, tOrVt_copy_view);
        flash::__ds_read_m32x16_row_col_rrow_alt<0, 3, 0>(tOsVt, tOrVt_copy_view);
zhanghj2's avatar
zhanghj2 committed
688

689
        if constexpr (MODEL_TYPE == ModelType::V32)
zhanghj2's avatar
zhanghj2 committed
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
        {
            bf16_storage bf16_data0;
            bf16_storage bf16_data1;
            bf16_data0.data_128 = *((uint32x4_t*)(gK_base + col_idx * 16 * 2 + 512 + 16));
            bf16_data1.data_128 = *((uint32x4_t*)(gK_base + col_idx * 16 * 2 + 8 * 2 + 512 + 16));
            for (int j = 0; j < 8; j++) {
                auto rst = cutlass::bfloat16_t::bitcast(bf16_data0.data_array[j]);
                tSrK(j, 0, 8) = rst;
            }
            for (int j = 8; j < 16; j++) {
                auto rst = cutlass::bfloat16_t::bitcast(bf16_data1.data_array[j - 8]);
                tSrK(j, 0, 8) = rst;
            }
            cute::gemm(tiled_mma, tSrQ(_, _, 8), tSrK(_, _, 8), acc_s);    

        }
        // zhj debug
        // if (head_block_idx == 0)
        // {
        //     printf("tidx = %d, %.2f %.2f %.2f %.2f \n", tidx, float(acc_s(0)), float(acc_s(1)), float(acc_s(2)), float(acc_s(3)));
        // }
        asm volatile("s_waitcnt lgkmcnt(0) \n\t s_barrier\n\t");

        Tensor cS = make_identity_tensor(Shape<Int<BLOCK_M>, Int<TOPK_BLOCK_SIZE>>{});
        Tensor tScS = thr_mma.partition_C(cS);
zhanghj2's avatar
zhanghj2 committed
715
716
717
718
719
720
721
        auto is_index_valid = [&](int index) -> bool {
            if constexpr (MODEL_TYPE == ModelType::V32) {
                return indices_base[int(get<1>(tScS(index)))] != -1;
            } else {
                return indices_base[int(get<1>(tScS(index)))] != -1 && (rel_block_idx*TOPK_BLOCK_SIZE + int(get<1>(tScS(index))) < topk_length);
            }
        };
zhanghj2's avatar
zhanghj2 committed
722
        for (int i = 0; i < size(acc_s); ++i) {
zhanghj2's avatar
zhanghj2 committed
723
724
            // int idx = indices_base[int(get<1>(tScS(i)))] ;
            if (not is_index_valid(i)) acc_s(i) = -INFINITY;
zhanghj2's avatar
zhanghj2 committed
725
        }
zhanghj2's avatar
zhanghj2 committed
726
        block_idx == args.start_block_idx
zhanghj2's avatar
zhanghj2 committed
727
728
        ? softmax.template softmax_rescale_o_prefill</*Is_first=*/true,  /*Check_inf=*/Is_causal>(acc_s, acc_o, sRow_max_reduce_buffer, params.sm_scale_div_log2)
        :   softmax.template softmax_rescale_o_prefill</*Is_first=*/false, /*Check_inf=*/Is_causal>(acc_s, acc_o, sRow_max_reduce_buffer, params.sm_scale_div_log2);
zhanghj2's avatar
zhanghj2 committed
729
730
731
732
        // if (head_block_idx == 0 && batch_idx == 0)
        // {
        //     printf("tidx = %d, %.2f %.2f %.2f %.2f \n", tidx, float(acc_s(0)), float(acc_s(1)), float(acc_s(2)), float(acc_s(3)));
        // }
zhanghj2's avatar
zhanghj2 committed
733
734
735
736
737
        Tensor rP = flash::convert_type<Element>(acc_s);
        Tensor tOrP = flash::convert_layout_acc_Aregs(tiled_mma, tiled_mma_o, rP, sP);

        {
            // __ds_read_m32x16_row_col<0, 0>(tOsVt, tOrVt_copy_view);
zhanghj2's avatar
zhanghj2 committed
738
            flash::__ds_read_m32x16_row_col_alt<1, 0>(tOsVt, tOrVt_copy_view);
zhanghj2's avatar
zhanghj2 committed
739
740
741
            // __ds_read_m32x16_row_col<2, 0>(tOsVt, tOrVt_copy_view);

            // __ds_read_m32x16_row_col<0, 1>(tOsVt, tOrVt_copy_view);
zhanghj2's avatar
zhanghj2 committed
742
            flash::__ds_read_m32x16_row_col_alt<1, 1>(tOsVt, tOrVt_copy_view);
zhanghj2's avatar
zhanghj2 committed
743
744
745
746
            // __ds_read_m32x16_row_col<2, 1>(tOsVt, tOrVt_copy_view);
            cute::gemm(tiled_mma_o, tOrP(_, _, 0), tOrVt(_, _, 0), acc_o);
            cute::gemm(tiled_mma_o, tOrP(_, _, 1), tOrVt(_, _, 1), acc_o);
            // __ds_read_m32x16_row_col<0, 2>(tOsVt, tOrVt_copy_view);
zhanghj2's avatar
zhanghj2 committed
747
            flash::__ds_read_m32x16_row_col_alt<1, 2>(tOsVt, tOrVt_copy_view);
zhanghj2's avatar
zhanghj2 committed
748
749
750
751
            // __ds_read_m32x16_row_col<2, 2>(tOsVt, tOrVt_copy_view);
            
            
            // __ds_read_m32x16_row_col<0, 3>(tOsVt, tOrVt_copy_view);
zhanghj2's avatar
zhanghj2 committed
752
            flash::__ds_read_m32x16_row_col_alt<1, 3>(tOsVt, tOrVt_copy_view);
zhanghj2's avatar
zhanghj2 committed
753
754
755
756
757
758
            // __ds_read_m32x16_row_col<2, 3>(tOsVt, tOrVt_copy_view);
            
            
            cute::gemm(tiled_mma_o, tOrP(_, _, 2), tOrVt(_, _, 2), acc_o);
            cute::gemm(tiled_mma_o, tOrP(_, _, 3), tOrVt(_, _, 3), acc_o);
        }
zhanghj2's avatar
zhanghj2 committed
759
760
    };

761
762
763
764
765
766
767
768
769
770
771
    if constexpr (MODEL_TYPE == ModelType::V32) {
        for (int block_idx = args.start_block_idx; block_idx < args.end_block_idx; block_idx++) {
            process_one_block(block_idx, IsOrigBlock{});
        }
    } else {
        for (int block_idx = args.start_block_idx; block_idx < min(args.num_orig_kv_blocks, args.end_block_idx); ++block_idx) {
            process_one_block(block_idx, IsOrigBlock{});
        }
        for (int block_idx = max(args.start_block_idx, args.num_orig_kv_blocks); block_idx < args.end_block_idx; ++block_idx) {
            process_one_block(block_idx, IsExtraBlock{});
        }
zhanghj2's avatar
zhanghj2 committed
772
    }
zhanghj2's avatar
zhanghj2 committed
773
774
775
776
    // if (head_block_idx == 0 && threadIdx.x < 64 && batch_idx == 0)
    // {
    //     printf(" %.4f %.4f \n", acc_o(0), acc_o(1));
    // }
zhanghj2's avatar
zhanghj2 committed
777
778
779
780
781
782
783
784
785
786
    auto float2bf16 = [] (float s) -> uint16_t {
        uint32_t x32 = reinterpret_cast<uint32_t const &>(s);
        #ifndef FLASH_MLA_BF16_TYPE
        #define FLASH_MLA_BF16_TYPE 0
        #endif
        #if FLASH_MLA_BF16_TYPE == 1
        x32 += 0x8000u;
        #endif
        return uint16_t(x32 >> 16);
    };
zhanghj2's avatar
zhanghj2 committed
787
788
789
790
791
792
793
    if (args.is_no_split) {
        int start_head_idx = head_block_idx*BLOCK_M;
        Tensor lse = softmax.template normalize_softmax_lse<false>(acc_o, sRow_sum_reduce_buffer, params.sm_scale);
        const index_t row_offset_o = batch_idx * params.stride_o_b + start_head_idx * params.stride_o_h_q + s_q_idx * params.stride_o_s_q ;
        Tensor gO = make_tensor(make_gmem_ptr(reinterpret_cast<Element *>(params.out) + row_offset_o),
                                        Shape<Int<BLOCK_M>, Int<HEAD_DIM_V>>{},
                                        make_stride(params.stride_o_h_q, _1{}));
zhanghj2's avatar
zhanghj2 committed
794
795
        if (params.attn_sink != nullptr) {
            float rAttn_sink = __ldg((float*)params.attn_sink + start_head_idx + lane_idx % 16); 
zhanghj2's avatar
zhanghj2 committed
796
797
798
799
800
801
802
803
            if (flash::is_positive_infinity(rAttn_sink))
            {
                for (int i = 0; i < size(acc_o); i++)
                {
                    acc_o(i) = 0.0f;
                } 
            }
            else
zhanghj2's avatar
zhanghj2 committed
804
            {
zhanghj2's avatar
zhanghj2 committed
805
806
807
808
809
810
811
812
813
814
                if (!flash::is_positive_infinity(lse(0)))
                {
                    float lse_exp2 = __builtin_amdgcn_exp2f(lse[0] * CUDART_L2E_F);
                    float rAttn_sink_exp2 = __builtin_amdgcn_exp2f(rAttn_sink * CUDART_L2E_F);
                    float o_scale = lse_exp2 / (lse_exp2 + rAttn_sink_exp2);
                    for (int i = 0; i < size(acc_o); i++)
                    {
                        acc_o(i) *= o_scale;
                    }
                }
zhanghj2's avatar
zhanghj2 committed
815
            }
zhanghj2's avatar
zhanghj2 committed
816

zhanghj2's avatar
zhanghj2 committed
817
        }
zhanghj2's avatar
zhanghj2 committed
818
819
820
821
822
823
824
825
826
827
828
829
830
831
        // if (block0() && tidx % 16 == 0)
        // {
        //     printf(" tidx = %d %.3f %.3f %.3f %.3f %.3f %.3f %.3f %3f \n ", 
        //         tidx,
        //         float(acc_o(0)),
        //         float(acc_o(1)),
        //         float(acc_o(2)),
        //         float(acc_o(3)),
        //         float(acc_o(4)),
        //         float(acc_o(5)),
        //         float(acc_o(6)),
        //         float(acc_o(7))
        //     );
        // }
zhanghj2's avatar
zhanghj2 committed
832
833
        float* gSoftmaxLse = (float*)params.lse + batch_idx * params.stride_lse_b + start_head_idx + s_q_idx * params.stride_lse_s_q;	// (BLOCK_M) : (1)
        {
zhanghj2's avatar
zhanghj2 committed
834
835
            // auto rO = flash::convert_type<Element>(acc_o);
            using result_type = cutlass::Array<Element, 2>;
zhanghj2's avatar
zhanghj2 committed
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
            int row, col;
            const int warpId = tidx / 64;
            const int laneId = tidx % 64;
            for (int mi = 0; mi < size<1>(acc_o); ++mi) {
                row = mi * BLOCK_M + laneId % 16;
                if (row < params.h_q) {
                    for (int ni = 0; ni < size<2>(acc_o); ++ni) {
                        // col = (laneId / 16) + ni * 128 + warpId * 32 ;
                        // 为了使用global_loadx4指令, V矩阵吸入lds的时候 N方向发生了了交换
                        /*
                        ------------------- N 方向----------------------
                        |0 1 ... 7 16 ... 31 40 ... 47 56... 64 8 .. 15 32 ... 39
                        |
                        |
                        k
                        方向
                        |
                        |
                        |
                        */
zhanghj2's avatar
zhanghj2 committed
856
                        col = (laneId / 16) * 2 + ni * 128 + (warpId % 2) * 8 + (warpId / 2) * 64;
zhanghj2's avatar
zhanghj2 committed
857
                        for (int i = 0; i < 4; i ++) {
zhanghj2's avatar
zhanghj2 committed
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878

                            #if defined(__gfx938__)
                            auto d =  __builtin_hcu_cvt_pk_bf16_f32(0, acc_o(i, mi, ni), 0, acc_o(i + 4, mi, ni), 0);
                            auto res = reinterpret_cast<result_type const &>(d);
                            #else
                            result_type res;
                            Element e0, e1;
                            e0.storage = float2bf16(acc_o(i, mi, ni));
                            e1.storage = float2bf16(acc_o(i + 4, mi, ni));
                            res[0] = e0;
                            res[1] = e1;
                            #endif
                            // gO(row, col) = res[0];
                            // gO(row, col + 1) = res[1];
                            *(result_type*)(&gO(row, col)) = res;
                            col += 16;    
                            // for (int j = 0; j < 2; j++) {
                            //     gO(row, col) = rO(i * 2 + j, mi, ni);
                            //     col += 4;
                            // }
                            // col += 8;
zhanghj2's avatar
zhanghj2 committed
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
                        }
                        // for (int ei = 0; ei < size<0>(acc_o); ++ei) {
                        //     gO(row, col) = rO(ei, mi, ni);
                        //     col += 4;
                        // }
                    }
                    gSoftmaxLse[row] = lse(mi);
                }


                // if (s_q_idx == 1)
                // {
                //     printf(" %.2f \n", lse(mi));
                // }

                
                // gMax_logits[row] = softmax.row_max(mi) * params.sm_scale_div_log2;
            
            }
        }


    } else {
        int start_head_idx = head_block_idx*BLOCK_M;
        Tensor lse = softmax.template normalize_softmax_lse<false, true>(acc_o, sRow_sum_reduce_buffer, params.sm_scale);
        int n_split_idx = batch_idx == sched_meta.begin_req_idx ? sched_meta.begin_split_idx : 0;
        int split_idx = __ldg(params.num_splits_ptr+batch_idx) + n_split_idx;
        float* oaccum_ptr = (float*)params.o_accum + split_idx*params.stride_o_accum_split + s_q_idx*params.stride_o_accum_s_q + start_head_idx*params.stride_o_accum_h_q;	// (BLOCK_M, HEAD_DIM_V) : (params.stride_o_accum_h_q, 1)
        Tensor gOaccum = make_tensor(make_gmem_ptr(oaccum_ptr), make_layout(
            Shape<Int<BLOCK_M>, Int<HEAD_DIM_V>>{},
            make_stride(params.stride_o_accum_h_q, _1{})
        ));
        float* gSoftmaxLseAccum = (float*)params.lse_accum + split_idx*params.stride_lse_accum_split + s_q_idx*params.stride_lse_accum_s_q + start_head_idx;	// (BLOCK_M) : (1)
        {
            // auto rO = flash::convert_type<Element>(acc_o);
            int row, col;
            const int warpId = tidx / 64;
            const int laneId = tidx % 64;
            for (int mi = 0; mi < size<1>(acc_o); ++mi) {
                row = mi * BLOCK_M + laneId % 16;
                if (row < params.h_q) {
                    for (int ni = 0; ni < size<2>(acc_o); ++ni) {
                        // col = (laneId / 16) + ni * 128 + warpId * 32 ;
                        // for (int ei = 0; ei < size<0>(acc_o); ++ei) {
                        //     gOaccum(row, col) = acc_o(ei, mi, ni);
                        //     col += 4;
                        // }
zhanghj2's avatar
zhanghj2 committed
926
                        col = (laneId / 16) * 2 + ni * 128 + (warpId % 2) * 8 + (warpId / 2) * 64;
zhanghj2's avatar
zhanghj2 committed
927
                        for (int i = 0; i < 4; i ++) {
zhanghj2's avatar
zhanghj2 committed
928
929
930
931
932
933
934
                            gOaccum(row, col) = acc_o(i, mi, ni);
                            gOaccum(row, col + 1) = acc_o(i + 4, mi, ni);
                            // for (int j = 0; j < 2; j++) {
                            //     gOaccum(row, col) = acc_o(i * 2 + j, mi, ni);
                            //     col += 4;
                            // }
                            col += 16;
zhanghj2's avatar
zhanghj2 committed
935
936
937
938
939
940
941
942
943
944
945
946
947
                        }
                    }

                    gSoftmaxLseAccum[row] = lse(mi);
                }

                // gMax_logits[row] = softmax.row_max(mi) * params.sm_scale_div_log2;
            
            }
        }
    }

}
948
949
950


template<ModelType MODEL_TYPE, int NUM_HEADS>
zhanghj2's avatar
zhanghj2 committed
951
__device__ void KernelTemplate<MODEL_TYPE, NUM_HEADS>::devfunc(const SparseAttnDecodeParams &params) {
zhanghj2's avatar
zhanghj2 committed
952
953
    const int partition_idx = blockIdx.z;
    DecodingSchedMeta sched_meta = params.tile_scheduler_metadata_ptr[partition_idx];
954

zhanghj2's avatar
zhanghj2 committed
955
956
957
958
959
960
961
962
963
964
965
966
    if (sched_meta.begin_req_idx >= params.b) return;
    for (int batch_idx = sched_meta.begin_req_idx; batch_idx <= sched_meta.end_req_idx; ++batch_idx) {
        // if (threadIdx.x == 0)
        // {
        //     printf(" batch_idx = %d end_req_idx = %d \n ", batch_idx, sched_meta.end_req_idx);
        // }
        if (batch_idx > sched_meta.begin_req_idx) {
            __syncthreads();  
        }
        compute_attn_1rowblock_splitkv_sparse_mla_fp8(params, sched_meta, batch_idx);
    
    }
967
968
}

zhanghj2's avatar
zhanghj2 committed
969
970
971
template<typename Kernel>
__global__ void __launch_bounds__(Kernel::NUM_THREADS, 1)
flash_fwd_splitkv_mla_fp8_sparse_kernel(const SparseAttnDecodeParams params) {
zhanghj2's avatar
zhanghj2 committed
972
#if defined(__gfx936__) || defined(__gfx938__)
zhanghj2's avatar
zhanghj2 committed
973
    Kernel::devfunc(params);
zhanghj2's avatar
zhanghj2 committed
974
#endif
975
976
977
978
979
980
981
982
}

template<ModelType MODEL_TYPE, int NUM_HEADS>
void KernelTemplate<MODEL_TYPE, NUM_HEADS>::run(const SparseAttnDecodeParams &params) {
    KU_ASSERT(params.h_kv == 1);
    KU_ASSERT(params.topk % TOPK_BLOCK_SIZE == 0);
    KU_ASSERT(params.d_qk == HEAD_DIM_K);
    KU_ASSERT(params.d_v == HEAD_DIM_V);
zhanghj2's avatar
zhanghj2 committed
983
    // KU_ASSERT(params.h_q % BLOCK_M == 0);
984
985
986
987
988
989
990
991
992
993
994
    if constexpr (MODEL_TYPE == ModelType::MODEL1) {
        constexpr int BYTES_PER_TOKEN = HEAD_DIM_NOPE + 2*HEAD_DIM_ROPE + 8;
        KU_ASSERT(params.stride_kv_row == BYTES_PER_TOKEN, "Each page block in KV cache must be contiguous for head64 sparse fp8 decoding attention in MODEL1");  // Each block must be contiguous
        if (params.extra_kv != nullptr) {
            KU_ASSERT(params.stride_extra_kv_row == BYTES_PER_TOKEN, "Each page block in extra KV cache must be contiguous for head64 sparse fp8 decoding attention in MODEL1");  // Each block must be contiguous
        }
    } else {
        KU_ASSERT(params.extra_kv == nullptr, "V3.2 does not support extra KV cache");
        KU_ASSERT(params.topk_length == nullptr, "V3.2 does not support dynamic topk length");
        KU_ASSERT(params.stride_kv_row == 656);  // number of bytes per token (512 fp8 + 4 float32 + 64 bfloat16)
    }
zhanghj2's avatar
zhanghj2 committed
995
    auto mla_kernel = &flash_fwd_splitkv_mla_fp8_sparse_kernel<KernelTemplate<MODEL_TYPE, NUM_HEADS>>;
zhanghj2's avatar
zhanghj2 committed
996
    constexpr size_t smem_size = 32768; // lds复用
zhanghj2's avatar
zhanghj2 committed
997
998
999
1000
    // zhj debug
    // printf("NUM_M_BLOCKS = %d smem_size = %d \n",NUM_M_BLOCKS, smem_size);
    mla_kernel<<<dim3(NUM_M_BLOCKS, params.s_q, params.num_sm_parts), NUM_THREADS, smem_size, params.stream>>>(params);

1001
1002
1003
1004
1005
1006
1007
1008
}

template<ModelType MODEL_TYPE, int NUM_HEADS>
void run_flash_splitkv_mla_fp8_sparse_kernel(const SparseAttnDecodeParams &params) {
    KernelTemplate<MODEL_TYPE, NUM_HEADS>::run(params);
}

}
zhanghj2's avatar
zhanghj2 committed
1009
1010
1011