gemm_w8a8.cuh 20.5 KB
Newer Older
muyangli's avatar
muyangli committed
1
2
3
4
5
6
7
8
9
10
#pragma once

#include "gemm_base.cuh"

namespace nunchaku::kernels {

class GEMM_W8A8 : public GEMMBase<GEMMConfig_W8A8> {
public:
    using psum_warp = std::array<packed_psum_t, WARP_M_TILES * WARP_N_TILES>;

Muyang Li's avatar
Muyang Li committed
11
    __device__ __forceinline__ static packed_psum_t mma(packed_act_t act, packed_wgt_t wgt, packed_psum_t psum) {
muyangli's avatar
muyangli committed
12
        // packed_psum_t psum;
fengzch's avatar
fengzch committed
13
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
40
41
42
43
44
45
46
47
48
        // asm volatile("mma.sync.aligned.m16n8k32.row.col.s32.s8.s8.s32 "
        //              "{%0,  %1,  %2,  %3},"
        //              "{%4,  %5,  %6,  %7},"
        //              "{%8,  %9},"
        //              "{%10,  %11,  %12,  %13};\n"
        //              : "=r"(psum.data[0]), "=r"(psum.data[1]), "=r"(psum.data[2]), "=r"(psum.data[3])
        //              : "r"(act.x),
        //                "r"(act.y),
        //                "r"(act.z),
        //                "r"(act.w),
        //                "r"(wgt.x),
        //                "r"(wgt.y),
        //                // "r"(0), "r"(0), "r"(0), "r"(0)
        //                "r"(psum.data[0]),
        //                "r"(psum.data[1]),
        //                "r"(psum.data[2]),
        //                "r"(psum.data[3]));
        // asm volatile("mma.sync.aligned.m16n8k32.row.col.s32.s8.s8.s32 "
        //              "{%0,  %1,  %2,  %3},"
        //              "{%4,  %5,  %6,  %7},"
        //              "{%8,  %9},"
        //              "{%10,  %11,  %12,  %13};\n"
        //              : "=r"(psum.data[4]), "=r"(psum.data[5]), "=r"(psum.data[6]), "=r"(psum.data[7])
        //              : "r"(act.x),
        //                "r"(act.y),
        //                "r"(act.z),
        //                "r"(act.w),
        //                "r"(wgt.z),
        //                "r"(wgt.w),
        //                // "r"(0), "r"(0), "r"(0), "r"(0)
        //                "r"(psum.data[4]),
        //                "r"(psum.data[5]),
        //                "r"(psum.data[6]),
        //                "r"(psum.data[7]));
        // printf("%s-%s-%d: asm not supportted in Hip yet!\n", __FILE__, __func__, __LINE__);
        
muyangli's avatar
muyangli committed
49
50
51
        return psum;
    }

Muyang Li's avatar
Muyang Li committed
52
    __device__ __forceinline__ static void compute(act_warp A, wgt_warp W, psum_warp &psum) {
muyangli's avatar
muyangli committed
53
54
55
        const int laneId = threadIdx.x % WARP_SIZE;
        const int warpId = threadIdx.x / WARP_SIZE;

Muyang Li's avatar
Muyang Li committed
56
#pragma unroll
muyangli's avatar
muyangli committed
57
        for (int j = 0; j < WARP_N_TILES; j++) {
Muyang Li's avatar
Muyang Li committed
58
#pragma unroll
muyangli's avatar
muyangli committed
59
60
61
62
63
64
65
66
67
68
69
70
            for (int i = 0; i < WARP_M_TILES; i++) {
                psum[i * WARP_N_TILES + j] = mma(A[i], W[j], psum[i * WARP_N_TILES + j]);
            }
        }
    }

    /**
     * each warp quantizes a INSN_M * INSN_K (16 * 32) matrix
     * input is per-warp (in global memory / shared memory)
     * oscales is per-warp (in shared memory)
     * output is per-thread (in regs)
     * shmem must be at least INSN_M * (INSN_K * sizeof(element) + 16) (16 * 32 = 512 Bytes)
Muyang Li's avatar
Muyang Li committed
71
72
     * default to quantize activation, if quantize weight, input should be column-majored and output should be
     * transposed ({x, y, z, w} = {x, z, y, w})
muyangli's avatar
muyangli committed
73
74
     */
    template<bool input_shmem = false>
Muyang Li's avatar
Muyang Li committed
75
76
    __device__ __forceinline__ static void
    quantize_w8a8_warp(const half_t *input, const half_t *oscales, int stride, packed_act_t &output, void *shmem) {
muyangli's avatar
muyangli committed
77
78
79
80
81
82
83
84
        const int laneId = threadIdx.x % WARP_SIZE;

        constexpr int QUANTIZE_BITWIDTH = 8;
        // constexpr int QUANTIZE_BITMASK = 0xff;
        // constexpr int QVALUE_MAX = 128;   // 4 bit => [-128, 127]

        // 1 lane = 1 pack
        // 1 warp = 32 lanes = 32 packs = 1 packwarp
Muyang Li's avatar
Muyang Li committed
85
86
87
88
89
        // a pack is {a0, ..., a7} in figure
        // https://docs.nvidia.com/cuda/parallel-thread-execution/index.html?highlight=ex2#mma-16864-a PACK_SIZE * 4 =
        // INSN_K / 2
        constexpr int PACK_SIZE             = INSN_K / 8; // = 4 for 8bit
        constexpr int NUM_PACKS_PER_ROW     = INSN_K / PACK_SIZE;
muyangli's avatar
muyangli committed
90
        constexpr int NUM_ROWS_PER_PACKWARP = PACK_SIZE * WARP_SIZE / INSN_K;
Muyang Li's avatar
Muyang Li committed
91
92
        constexpr int NUM_PACKWARPS         = INSN_M / NUM_ROWS_PER_PACKWARP;
        using packed_input                  = std::array<half_t, PACK_SIZE>;
muyangli's avatar
muyangli committed
93
94
95
96

        packed_input packs[NUM_PACKWARPS];

        // load
Muyang Li's avatar
Muyang Li committed
97
#pragma unroll
muyangli's avatar
muyangli committed
98
99
100
        for (int i = 0; i < NUM_PACKWARPS; i++) {
            int rowId = i * NUM_ROWS_PER_PACKWARP + laneId / NUM_PACKS_PER_ROW;
            int colId = laneId % NUM_PACKS_PER_ROW * PACK_SIZE;
Muyang Li's avatar
Muyang Li committed
101
            packs[i]  = load<input_shmem>(reinterpret_cast<const packed_input *>(input + rowId * stride + colId));
muyangli's avatar
muyangli committed
102
103
104
105
        }

        // quantize
        using matrix_t = uint32_t[INSN_M][NUM_PACKS_PER_ROW];
Muyang Li's avatar
Muyang Li committed
106
107
        matrix_t &mat  = *reinterpret_cast<matrix_t *>(shmem);
#pragma unroll
muyangli's avatar
muyangli committed
108
109
110
111
112
113
114
        for (int i = 0; i < NUM_PACKWARPS; i++) {
            const int row = i * NUM_ROWS_PER_PACKWARP + laneId / NUM_PACKS_PER_ROW;
            const int col = laneId % NUM_PACKS_PER_ROW;

            float rscale = cuda_frcp(float(oscales[row]));

            uint32_t qpack = 0;
Muyang Li's avatar
Muyang Li committed
115
#pragma unroll
muyangli's avatar
muyangli committed
116
117
118
119
120
121
122
            for (int j = 0; j < PACK_SIZE; j += 2) {
                // half2_t hval = __hmul2(half2_t(rscale, rscale), half2_t(packs[i][j], packs[i][j + 1]));
                float2 fval = half22float2(half2_t(packs[i][j], packs[i][j + 1])) * float2(rscale, rscale);
                qpack |= quantize_float2<QUANTIZE_BITWIDTH, false>(fval) << (j * QUANTIZE_BITWIDTH);
            }
            mat[row][col] = qpack;
        }
limm's avatar
limm committed
123
124
        // __syncwarp();
	__builtin_amdgcn_wave_barrier();
Muyang Li's avatar
Muyang Li committed
125

muyangli's avatar
muyangli committed
126
127
128
129
130
        // convert to imma format
        int row = laneId % 16;
        int col = laneId / 16 * 4;
        ldmatrix(&mat[row][col], output);

limm's avatar
limm committed
131
132
        // __syncwarp();
	__builtin_amdgcn_wave_barrier();
muyangli's avatar
muyangli committed
133
134
135
136
137
138
    }

    /**
     * each warp finds absmax from a row
     */
    template<bool fuse_glu = false>
Muyang Li's avatar
Muyang Li committed
139
140
    __device__ __forceinline__ static half_t
    findmax_warp(const half_t *input, half_t *output_shmem, int K, bool alwaysfalse) {
muyangli's avatar
muyangli committed
141
142
        const int laneId = threadIdx.x % WARP_SIZE;

Muyang Li's avatar
Muyang Li committed
143
        using packed_input       = std::array<half2_t, 4>;
muyangli's avatar
muyangli committed
144
145
        using packed_gated_input = std::array<half_t, 4>;

Muyang Li's avatar
Muyang Li committed
146
        constexpr int PACK_SIZE  = sizeof(packed_input) / sizeof(half_t);
muyangli's avatar
muyangli committed
147
148
        constexpr int NUM_STAGES = 2;

Muyang Li's avatar
Muyang Li committed
149
        half2_t maxvalue2 = {0, 0};
muyangli's avatar
muyangli committed
150
151
        packed_input pack[NUM_STAGES];

Muyang Li's avatar
Muyang Li committed
152
#pragma unroll
muyangli's avatar
muyangli committed
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
        for (int k = 0; k < NUM_STAGES - 1; k++) {
            const int idx = k * PACK_SIZE * WARP_SIZE + laneId * PACK_SIZE;
            if (idx < K) {
                pack[k] = load(reinterpret_cast<const packed_input *>(&input[idx]));
            } else {
                pack[k].fill(half2_t(0, 0));
            }
        }

        // int dummy = 0;

        // FIXME: pipeline does not work
        // TODO: store quantized data to shmem (instead of half)

        for (int k1 = 0; k1 < ceilDiv(K, PACK_SIZE * WARP_SIZE); k1 += NUM_STAGES) {
Muyang Li's avatar
Muyang Li committed
168
#pragma unroll
muyangli's avatar
muyangli committed
169
            for (int k2 = 0; k2 < NUM_STAGES; k2++) {
Muyang Li's avatar
Muyang Li committed
170

muyangli's avatar
muyangli committed
171
                const int nextidx = (k1 + k2 + NUM_STAGES - 1) * PACK_SIZE * WARP_SIZE + laneId * PACK_SIZE;
Muyang Li's avatar
Muyang Li committed
172
                const int nextk2  = (k2 + NUM_STAGES - 1) % NUM_STAGES;
muyangli's avatar
muyangli committed
173
174
175
176
177
178
179
180
181
182
183
184

                if (nextidx < K) {
                    pack[nextk2] = load(reinterpret_cast<const packed_input *>(&input[nextidx]));
                } else {
                    pack[nextk2].fill(half2_t(0, 0));
                }

                packed_input &p = pack[k2];

                if constexpr (fuse_glu) {
                    packed_gated_input gated;

Muyang Li's avatar
Muyang Li committed
185
#pragma unroll
muyangli's avatar
muyangli committed
186
187
                    for (int j = 0; j < p.size(); j++) {
                        gated[j] = p[j].x * gelu_half(p[j].y);
Muyang Li's avatar
Muyang Li committed
188
189
                        p[j].x   = gated[j];
                        p[j].y   = 0;
muyangli's avatar
muyangli committed
190
191
192
193
194
195
196
197
                    }

                    int idx = (k1 + k2) * PACK_SIZE / 2 * WARP_SIZE + laneId * PACK_SIZE / 2;
                    if (idx < K) {
                        store<true>(reinterpret_cast<packed_gated_input *>(&output_shmem[idx]), gated);
                    }
                }

Muyang Li's avatar
Muyang Li committed
198
#pragma unroll
muyangli's avatar
muyangli committed
199
200
201
202
203
204
205
206
                for (int j = 0; j < p.size(); j++) {
                    maxvalue2 = __hmax2(maxvalue2, __habs2(p[j]));
                }
            }
        }

        // unused_var(dummy, alwaysfalse);

Muyang Li's avatar
Muyang Li committed
207
#pragma unroll
muyangli's avatar
muyangli committed
208
        for (int mask = 32 / 2; mask > 0; mask /= 2) {
209
210
211
212
213
214
215
216
            __half2 m;
            m.x = float(maxvalue2.x);
            m.y = float(maxvalue2.y);
            auto temp = __shfl_xor(m, mask);
            __hip_bfloat162 n;
            n.x = float(temp.x);
            n.y = float(temp.y);
            maxvalue2 = __hmax2(maxvalue2, n);
muyangli's avatar
muyangli committed
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
        }

        return __hmax(maxvalue2.x, maxvalue2.y);
    }

    // each thread block quantize WARP_M * K tile (32 * K)
    template<bool fuse_glu>
    struct quantize_w8a8_act_kernel {
        static constexpr bool check(int M, int K) {
            const int K2 = fuse_glu ? K / 2 : K;
            return M % WARP_M == 0 && K2 % WARP_K == 0;
        }
        static constexpr dim3 gridSize(int M, int K) {
            return dim3(M / WARP_M);
        }
        static constexpr dim3 blockSize(int M, int K) {
            return dim3(NUM_WARPS * 32);
        }
        static constexpr size_t smemSize(int M, int K) {
            if constexpr (!fuse_glu) {
                return 0;
            }
            const int K2 = fuse_glu ? K / 2 : K;
            return INSN_M * K2 * sizeof(half_t);
        }

Muyang Li's avatar
Muyang Li committed
243
244
        __device__ void
        operator()(const half_t *input, packed_act_t *output, packed_ascale_t *oscales, int K, bool alwaysfalse) {
muyangli's avatar
muyangli committed
245
246
247
248
249
250
251
            // for quantize kernel
            const int laneId = threadIdx.x % WARP_SIZE;
            const int warpId = threadIdx.x / WARP_SIZE;

            const int numWarps = blockDim.x / WARP_SIZE;

            // for GEMM kernel
Muyang Li's avatar
Muyang Li committed
252
            const int bm         = blockIdx.x / (BLOCK_M / WARP_M);
muyangli's avatar
muyangli committed
253
254
            const int gemmWarpId = blockIdx.x % (BLOCK_M / WARP_M);

255
            __shared__ __attribute__((aligned(128))) half_t oscale_shmem[WARP_M];
muyangli's avatar
muyangli committed
256
            // __shared__ alignas(128) half_t maxv_shmem[WARP_M];
257
            __shared__ __attribute__((aligned(128))) uint8_t tmp_shmem[NUM_WARPS][512];
muyangli's avatar
muyangli committed
258
259
260
261
262
263
264
265
266
267

            const int K2 = fuse_glu ? K / 2 : K;

            // INSN_M * K2
            extern __shared__ uint8_t smem[];
            half_t *shmem = reinterpret_cast<half_t *>(smem);

            for (int tileM = 0; tileM < WARP_M_TILES; tileM++) {

                for (int i = warpId; i < INSN_M; i += numWarps) {
Muyang Li's avatar
Muyang Li committed
268
                    const int rowLocal  = tileM * INSN_M + i;
muyangli's avatar
muyangli committed
269
270
271
272
273
274
275
276
277
278
                    const int rowGlobal = blockIdx.x * WARP_M + rowLocal;

                    half_t maxv = findmax_warp<fuse_glu>(input + rowGlobal * K, shmem + i * K2, K, alwaysfalse);
                    oscale_shmem[rowLocal] = maxv / half_t(127);
                    // rscale_shmem[rowLocal] = half_t(127) / maxv;
                    // maxv_shmem[rowLocal] = maxv;
                }
                __syncthreads();

                for (int bk = warpId; bk < K2 / WARP_K; bk += numWarps) {
Muyang Li's avatar
Muyang Li committed
279
                    const int rowLocal  = tileM * INSN_M;
muyangli's avatar
muyangli committed
280
                    const int rowGlobal = blockIdx.x * WARP_M + rowLocal;
Muyang Li's avatar
Muyang Li committed
281
                    const int col       = bk * WARP_K;
muyangli's avatar
muyangli committed
282
283
284
285

                    packed_act_t tmpout;

                    if constexpr (fuse_glu) {
Muyang Li's avatar
Muyang Li committed
286
                        quantize_w8a8_warp<true>(shmem + col, oscale_shmem + rowLocal, K2, tmpout, &tmp_shmem[warpId]);
muyangli's avatar
muyangli committed
287
288
                    } else {
                        quantize_w8a8_warp<false>(
Muyang Li's avatar
Muyang Li committed
289
                            input + rowGlobal * K + col, oscale_shmem + rowLocal, K, tmpout, &tmp_shmem[warpId]);
muyangli's avatar
muyangli committed
290
                    }
Muyang Li's avatar
Muyang Li committed
291
292
293
294
295

                    store(&output[(((bm * K2 / WARP_K + bk) * NUM_WARPS + gemmWarpId) * WARP_M_TILES + tileM) *
                                      WARP_SIZE +
                                  laneId],
                          tmpout);
muyangli's avatar
muyangli committed
296
297
298
299
300
                }
                __syncthreads();
            }

            // [M / BLOCK_M, 1, NUM_WARPS, ASCALES_NUM_PACKS, ASCALES_VALID_LANES] of packed_ascale_t
Muyang Li's avatar
Muyang Li committed
301
302
            pack_ascales(oscale_shmem,
                         &oscales[(bm * NUM_WARPS + gemmWarpId) * ASCALES_NUM_PACKS * ASCALES_VALID_LANES]);
muyangli's avatar
muyangli committed
303
304
305
        }
    };

Muyang Li's avatar
Muyang Li committed
306
    __device__ __forceinline__ static gated_fpsum_warp apply_glu(fpsum_warp fpsum) {
muyangli's avatar
muyangli committed
307
308
309
310
311
312
        gated_fpsum_warp result;
        for (int i = 0; i < WARP_M_TILES; i++) {
            for (int j = 0; j < WARP_N_TILES; j++) {
                for (int k = 0; k < 4; k++) {
                    half_t &dst = result[i * WARP_N_TILES + j].data[k];
                    half2_t src = fpsum[i * WARP_N_TILES + j].data[k];
Muyang Li's avatar
Muyang Li committed
313
                    dst         = src.x * gelu_half(src.y);
muyangli's avatar
muyangli committed
314
315
316
317
318
319
320
                }
            }
        }
        return result;
    }

    static constexpr int unpack_gated_fpsum_shmem_size = INSN_M * (WARP_N / 2 + 8) * sizeof(half_t);
Muyang Li's avatar
Muyang Li committed
321
322
    __device__ __forceinline__ static void
    unpack_gated_fpsum(gated_fpsum_warp fpsum, half_t *output, int stride, void *shmem) {
muyangli's avatar
muyangli committed
323
324
325
        const int laneId = threadIdx.x % WARP_SIZE;

        constexpr int PACK_SIZE = WARP_N / 2 / WARP_SIZE;
Muyang Li's avatar
Muyang Li committed
326
        using pack_t            = std::array<half_t, PACK_SIZE>;
muyangli's avatar
muyangli committed
327
328
329

        // +8 to prevent bank conflicts
        using matrix_t = half_t[INSN_M][WARP_N / 2 + 8];
Muyang Li's avatar
Muyang Li committed
330
        matrix_t &mat  = *reinterpret_cast<matrix_t *>(shmem);
muyangli's avatar
muyangli committed
331
332
333

        for (int i = 0; i < WARP_M_TILES; i++) {
            for (int j = 0; j < WARP_N_TILES; j++) {
Muyang Li's avatar
Muyang Li committed
334
335
336
337
338
                packed_gated_fpsum_t &fsum                          = fpsum[i * WARP_N_TILES + j];
                int row                                             = laneId / 4;
                int col                                             = laneId % 4 + j * INSN_N / 2;
                *reinterpret_cast<half_t *>(&mat[row][col + 0])     = fsum.data[0];
                *reinterpret_cast<half_t *>(&mat[row][col + 4])     = fsum.data[2];
muyangli's avatar
muyangli committed
339
340
341
                *reinterpret_cast<half_t *>(&mat[row + 8][col + 4]) = fsum.data[1];
                *reinterpret_cast<half_t *>(&mat[row + 8][col + 4]) = fsum.data[3];
            }
limm's avatar
limm committed
342
343
            // __syncwarp();
	    __builtin_amdgcn_wave_barrier();
muyangli's avatar
muyangli committed
344
345
346
347
348

            for (int row = 0; row < INSN_M; row++) {
                pack_t pack = *reinterpret_cast<pack_t *>(&mat[row][laneId * PACK_SIZE]);
                store(reinterpret_cast<pack_t *>(&output[(i * INSN_M + row) * stride + laneId * PACK_SIZE]), pack);
            }
limm's avatar
limm committed
349
350
            // __syncwarp();
	    __builtin_amdgcn_wave_barrier();
muyangli's avatar
muyangli committed
351
352
353
354
355
        }
    }

    // out: [M, N] <=> [..., NUM_WARPS, WARP_M, N] of half
    template<typename Epilogue>
Muyang Li's avatar
Muyang Li committed
356
357
358
359
360
361
362
363
364
365
366
    __device__ __forceinline__ static void gemm_w8a8_block(const BlockInfo binfo,
                                                           const packed_act_t *act,
                                                           const packed_wgt_t *wgt,
                                                           const packed_ascale_t *ascales,
                                                           const packed_wscale_t *wscales,
                                                           // half_t *out,
                                                           int M,
                                                           int N,
                                                           int K,
                                                           Epilogue::Arguments epilogeParams,
                                                           bool alwaysfalse) {
muyangli's avatar
muyangli committed
367
368
369
370
371
        constexpr int NUM_STAGES = 2;

        const int laneId = threadIdx.x % WARP_SIZE;
        const int warpId = threadIdx.x / WARP_SIZE;

Muyang Li's avatar
Muyang Li committed
372
373
374
375
376
        act_warp A[NUM_STAGES]; // 8
        wgt_warp W[NUM_STAGES]; // 32
        ascale_warp ascale;     // 1
        wscale_warp wscale;     // 2
        psum_warp psum;         // 128
muyangli's avatar
muyangli committed
377
378
379
380
381
382
383
384
385
386

        for (auto &pack : psum) {
            for (int i = 0; i < 8; i++) {
                pack.data[i] = 0;
            }
        }

        // load_wscale<true>(wscales, wscale[0], true);
        // load_wscale<false>(wscales, wscale[1], true);
        // load_wscale<false>(wscales, wscale[2], true);
Muyang Li's avatar
Muyang Li committed
387

muyangli's avatar
muyangli committed
388
389
390
391
392
393
394
        load_ascale(ascales, 0, M, ascale, true);
        load_wscale(wscales, 0, N, wscale, true);

        for (int k = 0; k < NUM_STAGES - 1; k++) {
            load_act(act, k, K, A[k], true);
            load_wgt(wgt, k, K, W[k], true);
        }
Muyang Li's avatar
Muyang Li committed
395

muyangli's avatar
muyangli committed
396
397
398
        int dummy = 0;

        for (int k1 = 0; k1 < K / WARP_K; k1 += NUM_STAGES) {
Muyang Li's avatar
Muyang Li committed
399
#pragma unroll
muyangli's avatar
muyangli committed
400
401
            for (int k2 = 0; k2 < NUM_STAGES; k2++) {
                int nextk = k1 + k2 + NUM_STAGES - 1;
Muyang Li's avatar
Muyang Li committed
402
                int idx   = (k2 + NUM_STAGES - 1) % NUM_STAGES;
muyangli's avatar
muyangli committed
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
                bool pred = nextk < K / WARP_K;
                load_act(act, nextk, K, A[idx], pred);
                load_wgt(wgt, nextk, K, W[idx], pred);
                // load_wscale<false>(wscales, wscale[idx], pred);

                // __syncthreads();
                // if (alwaysfalse) {
                //     dummy = clock();
                // }

                // if (alwaysfalse) {
                //     dummy = clock();
                // }

                compute(A[k2], W[k2], psum);

                // if (alwaysfalse) {
                //     dummy = clock();
                // }

fengzch's avatar
fengzch committed
423
424
                asm volatile ("membar.cta;");

muyangli's avatar
muyangli committed
425
426
427
428
429
430
431
            }
        }

        unused_var(dummy, alwaysfalse);

        f32psum_warp f32psum;

Muyang Li's avatar
Muyang Li committed
432
#pragma unroll
muyangli's avatar
muyangli committed
433
        for (int i = 0; i < f32psum.size(); i++) {
Muyang Li's avatar
Muyang Li committed
434
#pragma unroll
muyangli's avatar
muyangli committed
435
436
437
438
439
            for (int j = 0; j < 8; j++) {
                f32psum[i].data[j] = 0;
            }
        }

Muyang Li's avatar
Muyang Li committed
440
        apply_scales([&](int i, int j) { return psum[i * WARP_N_TILES + j]; }, ascale, wscale, f32psum);
muyangli's avatar
muyangli committed
441
442
443
444
445
446
447
448
449
450
451
452
453
454

        fpsum_warp fpsum = packed_fp32_to_fp16(f32psum);

        // if (blockIdx.x == 0 && blockIdx.y == 0 && threadIdx.x % 32 == 0) {

        //     printf("warpId = %d fpsum = %f\n", warpId, (float)fpsum[0].data[0].x);
        // }

        Epilogue()(binfo, fpsum, M, N, K, epilogeParams);
    }

    // out : [M / BLOCK_M, BLOCK_M, N / BLOCK_N, BLOCK_N]
    template<typename Epilogue>
    struct gemm_w8a8_kernel {
fengzch-das's avatar
fengzch-das committed
455
        static constexpr int MIN_ARCH = std::is_same_v<half_t, __nv_bfloat16> ? 800 : 750;
Muyang Li's avatar
Muyang Li committed
456
457
458
459
460
461
462
463
464
465
466
        __device__ void operator()(const packed_act_t *act,
                                   const packed_wgt_t *wgt,
                                   const packed_ascale_t *ascales,
                                   const packed_wscale_t *wscales,
                                   // half_t *out,
                                   int M,
                                   int N,
                                   int K,
                                   Epilogue::Arguments epilogueArgs,
                                   bool swapBlockXY,
                                   bool alwaysfalse) {
muyangli's avatar
muyangli committed
467
            BlockInfo binfo = {
Muyang Li's avatar
Muyang Li committed
468
469
                .bm         = (int)blockIdx.x,
                .bn         = (int)blockIdx.y,
muyangli's avatar
muyangli committed
470
471
472
473
474
475
476
477
478
479
480
481
                .numBlocksM = (int)gridDim.x,
                .numBlocksN = (int)gridDim.y,
            };

            if (swapBlockXY) {
                std::swap(binfo.bm, binfo.bn);
                std::swap(binfo.numBlocksM, binfo.numBlocksN);
            }

            const int bm = binfo.bm;
            const int bn = binfo.bn;

Muyang Li's avatar
Muyang Li committed
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
            gemm_w8a8_block<Epilogue>(binfo,
                                      act + bm * (K / WARP_K) * NUM_WARPS * WARP_M_TILES * WARP_SIZE,
                                      wgt + bn * (K / WARP_K) * WARP_N_TILES * WARP_SIZE,
                                      ascales + bm * (1) * NUM_WARPS * ASCALES_NUM_PACKS *
                                                    ASCALES_VALID_LANES, // only 1 group in W8A8
                                      wscales + bn * (1) * WSCALES_NUM_PACKS * WSCALES_VALID_LANES,
                                      // #if 1
                                      //                 out + (bm * BLOCK_M * N) + bn * BLOCK_N,
                                      // #else
                                      //                 out + (bm * BLOCK_M * N / 2) + bn * BLOCK_N / 2,
                                      // #endif
                                      M,
                                      N,
                                      K,
                                      epilogueArgs,
                                      alwaysfalse);
muyangli's avatar
muyangli committed
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
        }
    };

#if 0
    struct EpilogueGLU {
        struct Arguments { size_t unused; };

        __device__ __forceinline__
        void operator()(const BlockInfo binfo, fpsum_warp fpsum, half_t *out, int M, int N, int K, Arguments args) {
            const int warpId = threadIdx.x / WARP_SIZE;

            gated_fpsum_warp gated_fpsum = apply_glu(fpsum);

            __shared__ alignas(128) uint8_t shmem[NUM_WARPS][ceilDiv(unpack_gated_fpsum_shmem_size, 128) * 128];
            unpack_gated_fpsum(gated_fpsum, out + warpId * WARP_M * N / 2, N / 2, shmem[warpId]);
        }
    };
#endif
};

Muyang Li's avatar
Muyang Li committed
518
}; // namespace nunchaku::kernels