okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
fd19246dc
main
1// src/prefill/lat_q3k_swiglu.cu - see include/strata/prefill/lat_q3k_swiglu.hpp. 2// 3// M2b of the q3k latent A stage: SwiGLU and the q8_1 of H, the pass moe_fused.cu's latent_kernel<true> does in its 4// epilogue, here on the flat gate/up rows M2a writes. A warp is one 32-value block of one entry, so the block's amax 5// is one xor reduction over the warp; the code position and the block's d are q8_1_store's (iq_kernels.cu:2865: the 6// amax, `q8_1_finite(amax / 127)`, `q8_1_quant`), i.e. the engine's own quantizer bit for bit. 7#include "strata/prefill/lat_q3k_swiglu.hpp" 8#include "strata/kernels/q8_1_finite.hpp" 9 10#include <cuda_fp16.h> 11#include <cuda_runtime.h> 12 13#include <cstdio> 14#include <cstdlib> 15 16namespace strata::prefill { 17namespace { 18 19void dck(cudaError_t e, const char* what) { 20 if (e != cudaSuccess) { 21 std::fprintf(stderr, "lat q3k swiglu: %s: %s\n", what, cudaGetErrorString(e)); 22 std::exit(1); 23 } 24} 25 26constexpr int kFF = 640; // features per entry 27constexpr int kBlocks = kFF / 32; // 20 q8_1 blocks per entry 28constexpr int kHqRow = 800; // 10 chunks x 80 B 29constexpr int kThreads = 256; // 8 warps 30 31__global__ void __launch_bounds__(kThreads) 32lat_q3k_swiglu_kernel(const float* __restrict__ gate, const float* __restrict__ up, int64_t rows, 33 uint8_t* __restrict__ hq, float* __restrict__ hout) { 34#if defined(__CUDA_ARCH__) && __CUDA_ARCH__ >= 800 35 const int lane = threadIdx.x & 31, wid = threadIdx.x >> 5; 36 const long long i = (long long) blockIdx.x * (kThreads / 32) + wid; // one warp per (entry, 32-value block) 37 if (i >= rows * kBlocks) return; 38 const long long e = i / kBlocks; 39 const int b = (int) (i % kBlocks); 40 const int v = lane; 41 const int64_t at = e * kFF + 32 * b + v; 42 const float g = gate[at], u = up[at]; 43 // the engine's product, rounded on its own (__fmul_rn), as moe_fused.cu's latent epilogue and swiglu_q8_1_entries 44 const float h = __fmul_rn(__fdiv_rn(g, 1.0f + __expf(-g)), u); 45 if (hout != nullptr) hout[at] = h; 46 float am = fabsf(h); 47#pragma unroll 48 for (int o = 16; o > 0; o >>= 1) am = fmaxf(am, __shfl_xor_sync(0xffffffffu, am, o)); 49 const float d = strata::kernels::q8_1_finite(__fdiv_rn(am, 127.0f)); 50 const int8_t qv = strata::kernels::q8_1_quant(h, d, am); 51 uint8_t* row = hq + (size_t) e * kHqRow + (size_t) (b >> 1) * 80; 52 // q8_1_store's permuted position: sub-block 2 * (b & 1) + (v / 16), byte 4 * (v & 3) + ((v & 15) / 4) 53 row[16 * (2 * (b & 1) + (v >> 4)) + 4 * (v & 3) + ((v & 15) >> 2)] = (uint8_t) qv; 54 if (lane == 0) *(__half*) (row + 64 + 2 * (b & 1)) = __float2half(d); 55#else 56 (void) gate; (void) up; (void) rows; (void) hq; (void) hout; 57#endif 58} 59 60} // namespace 61 62void lat_q3k_swiglu_q8(const float* gate, const float* up, int64_t rows, void* hq, float* hout, void* stream) { 63 if (rows <= 0 || gate == nullptr || up == nullptr || hq == nullptr) return; 64#if defined(__HIPCC__) 65 std::fprintf(stderr, "lat q3k swiglu: the latent A stage's kernels are CUDA-only\n"); 66 std::exit(1); 67#else 68 const long long n = rows * kBlocks; // one warp a block 69 const long long blocks = (n + (kThreads / 32) - 1) / (kThreads / 32); 70 lat_q3k_swiglu_kernel<<<(unsigned) blocks, kThreads, 0, (cudaStream_t) stream>>>(gate, up, rows, (uint8_t*) hq, 71 hout); 72 dck(cudaGetLastError(), "launch"); 73#endif 74} 75 76} // namespace strata::prefill