okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
fa6367a98
main
1// src/kernels/cuda/s_gemv_pair.cu - gate/up dual GEMV + SwiGLU in ONE kernel (llama.cpp's `mul_mat_gated`). 2// 3// The shared expert ran THREE launches for its two projections and their SwiGLU: the gate GEMV, the up 4// GEMV, then a pass over both halves. This kernel computes `out[o] = silu(gate_row_o) * up_row_o` in one 5// launch - one read of the activation for both rows - and never materialises `up`. 6// 7// BITWISE against the unfused pipeline: each row is accumulated by the SAME row function the standalone 8// kernel uses (`strata/kernels/s_rowdev.cuh`, shared with nothing else), and the SwiGLU expression is the 9// shared one (`strata/kernels/swiglu.cuh`). A float store/load is lossless, so computing the product from 10// the register values is what the two-kernel pipeline computed from memory. `fusions_parity` checks this 11// bitwise. 12// 13// The kernel supports every (gate form, up form) pair the S-family dispatch produces: a side is either the 14// S2 row (`s2_gemv_q8`, code_bits 2, Q8_0 activations) or an S4/S8 row (`s_gemv_q8_*_split`, Q8_0 or Q8_K 15// activations). The per-side kind is a run-time integer picked ONCE per row, outside the accumulation 16// loops. Anything else (a non-32 `tpr`, an unknown width) falls back to the unfused sequence. 17#include "strata/kernels/s_gemv.hpp" 18#include "strata/kernels/s_rowdev.cuh" 19#include "strata/kernels/swiglu.cuh" 20 21#include <cuda_runtime.h> 22 23#include <cstdio> 24#include <cstdlib> 25 26namespace strata::kernels { 27namespace { 28 29// side kinds: 0 = S2 row (Q8_0 act), 1 = split<Q8K>, 2 = split<Q8_0>; widths 4 and 8 fold into `kind` 30// below via the template argument chosen at dispatch. 31enum { SideS2 = 0, SideQ8K = 1, SideQ80 = 2 }; 32 33__device__ __forceinline__ float side_row(int kind, int cb, const uint8_t* x80, const uint8_t* x8k, 34 const uint8_t* codes, const float* scales, const float* off, 35 long long n_in, long long o, int lane, int bias, int codebook, 36 int group_shift, int has_offset, int code_bits, 37 float* s2partial, int tpr) { 38 if (kind == SideS2) { 39 row_s2_q8(x80, codes, scales, n_in, o, lane, tpr, s2partial); 40 return s2partial[0]; 41 } 42 const uint8_t* x = kind == SideQ8K ? x8k : x80; 43 switch (code_bits) { 44 case 4: 45 return kind == SideQ8K ? row_s_q8<4, true>(x, codes, scales, off, n_in, o, lane, bias, codebook, 46 group_shift, has_offset) 47 : row_s_q8<4, false>(x, codes, scales, off, n_in, o, lane, bias, codebook, 48 group_shift, has_offset); 49 default: 50 return kind == SideQ8K ? row_s_q8<8, true>(x, codes, scales, off, n_in, o, lane, bias, codebook, 51 group_shift, has_offset) 52 : row_s_q8<8, false>(x, codes, scales, off, n_in, o, lane, bias, codebook, 53 group_shift, has_offset); 54 } 55} 56 57struct SideArgs { 58 int kind, code_bits, bias, codebook, group_shift, has_offset; 59 const uint8_t *codes, *x80, *x8k; 60 const float* scales; 61 const float* off; 62}; 63 64// ONE WARP PER OUTPUT PAIR: gate row o and up row o, two accumulations, one SwiGLU store. 65__global__ void pair_kernel(SideArgs g, SideArgs u, float* __restrict__ out, long long n_in, long long n_out, 66 int swilu_kind, int tpr) { 67 __shared__ float s2p[8][32]; // the S2 tree, per warp (8 warps per block) 68 const int warp_in_block = threadIdx.x >> 5; 69 const long long o = (long long) blockIdx.x * (blockDim.x >> 5) + warp_in_block; 70 if (o >= n_out) return; 71 const int lane = threadIdx.x & 31; 72 const float ag = side_row(g.kind, g.code_bits, g.x80, g.x8k, g.codes, g.scales, g.off, n_in, o, lane, 73 g.bias, g.codebook, g.group_shift, g.has_offset, g.code_bits, 74 s2p[warp_in_block], tpr); 75 const float au = side_row(u.kind, u.code_bits, u.x80, u.x8k, u.codes, u.scales, u.off, n_in, o, lane, 76 u.bias, u.codebook, u.group_shift, u.has_offset, u.code_bits, 77 s2p[warp_in_block], tpr); 78 if (lane != 0) return; 79 float v; 80 if (swilu_kind == 0) v = swilu_legacy(ag, au); 81 else if (swilu_kind == 1) v = swilu_fast(ag, au); 82 else v = swilu_native(ag, au); 83 out[o] = v; 84} 85 86bool side_of(const SForm& f, int64_t n_in, const uint8_t* x80, const uint8_t* x8k, const uint8_t* codes, 87 const float* scales, const float* off, SideArgs* out) { 88 if (f.code_bits == 2) { 89 out->kind = SideS2; 90 } else if (f.code_bits == 4 || f.code_bits == 8) { 91 out->kind = f.act_kind == 1 ? SideQ8K : SideQ80; 92 } else { 93 return false; 94 } 95 out->code_bits = f.code_bits; 96 out->bias = f.code_bias; 97 out->codebook = (int) f.codebook; 98 out->group_shift = 0; 99 while ((1 << out->group_shift) < f.group_elems) ++out->group_shift; 100 if ((1 << out->group_shift) != f.group_elems) return false; 101 out->has_offset = f.has_offset ? 1 : 0; 102 out->codes = codes; 103 out->scales = scales; 104 out->off = off; 105 out->x80 = x80; 106 out->x8k = x8k; 107 (void) n_in; 108 return true; 109} 110 111} // namespace 112 113bool s_gemv_pair_silu(const uint8_t* x_q8_0, const uint8_t* x_q8k, const SForm& form_g, const uint8_t* codes_g, 114 const float* scales_g, const float* off_g, const SForm& form_u, const uint8_t* codes_u, 115 const float* scales_u, const float* off_u, float* out, int64_t n_in, int64_t n_out, 116 int threads_per_row, int swilu_kind, void* stream) { 117 if (n_in <= 0 || n_out <= 0) return false; 118 // the S2 row's tree is shaped by `threads_per_row`; the standalone kernel takes any tpr, but this 119 // warp-per-pair form reproduces it only at 32 (the engine's TPR is 32 everywhere). 120 if (threads_per_row != 32) return false; 121 SideArgs g, u; 122 if (!side_of(form_g, n_in, x_q8_0, x_q8k, codes_g, scales_g, off_g, &g)) return false; 123 if (!side_of(form_u, n_in, x_q8_0, x_q8k, codes_u, scales_u, off_u, &u)) return false; 124 125 const int threads = 256; // 8 warps = 8 output pairs 126 const unsigned grid = (unsigned) ((n_out + 7) / 8); 127 pair_kernel<<<grid, threads, 0, (cudaStream_t) stream>>>(g, u, out, n_in, n_out, swilu_kind, threads_per_row); 128 const cudaError_t e = cudaGetLastError(); 129 if (e != cudaSuccess) { 130 std::fprintf(stderr, "s_gemv_pair_silu launch: %s\n", cudaGetErrorString(e)); 131 std::exit(1); 132 } 133 return true; 134} 135 136} // namespace strata::kernels