okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
32f0c44f0
main
1// src/kernels/cuda/q8_sm60.cuh - a Q8_0 decode GEMV for Pascal GP100 (compute capability 6.0), opt-in with 2// STRATA_Q8_SM60=1. Included by native_mmvq.cu, and by a standalone parity/speed check, so both run this code. 3// 4// Reads the packed planes of STRATA_Q8_PACKED (the SAME int8 values row-major, n_in bytes per row, then one fp16 scale 5// per 32 of them) for every Q8_0 dense matrix and the head, whatever their shape. Why the packed kernel above is slow 6// on a GP100 die (measured on PH402, 1050 MHz: ~137 GB/s for the dense projections, ~160 for the head, against 7// 220-270 for this layout): one workgroup per row, eight bytes per thread per pass and a __syncthreads() per row - a 8// 2560-wide row is 80 blocks, so the reduction costs more than the row - and the window's activations read as single 9// ints, because a q8_1 block is 36 bytes and its values start at byte 4. 10// 11// Here: 12// * a workgroup first copies the window's activations into shared memory as an int8 plane per column and a float 13// scale per 32 (once; the workgroup then loops over rows), so every activation read is a 16-byte shared load; 14// * TPR threads own a row (a whole warp for n_in >= 2048, a half or a quarter for narrower rows) and read its 15// weights 16 bytes at a time through the read-only cache; the dot of 4 ints is STRATA_DP4A (vmad on sm_60); 16// * the sum is a shuffle within those TPR lanes - no shared memory, no barrier per row. 17// The per-32 products are added in another order than the exact kernels', so outputs are not bitwise theirs (float 18// rounding only; the int8 dots are exact). 19#pragma once 20 21namespace q8sm60 { 22 23constexpr int THREADS = 256; 24constexpr std::size_t MAX_SMEM = 48 * 1024; // a Pascal workgroup's static + dynamic limit 25constexpr int MAX_COLS = 8; // columns of one launch (a window of 9..16 rows runs in passes) 26 27inline std::size_t smem_bytes(int ncols, int n_in) { 28 return std::size_t(ncols) * std::size_t(n_in) + std::size_t(ncols) * std::size_t(n_in / 32) * sizeof(float); 29} 30 31template<int NCOLS, int TPR> 32__launch_bounds__(THREADS) 33__global__ void kernel(const int8_t* __restrict__ qs, const half* __restrict__ dpl, const Q81Block* __restrict__ x, 34 float* __restrict__ y, int n_in, int n_out) { 35 extern __shared__ int4 q8sm60_smem[]; 36 int8_t* sx = reinterpret_cast<int8_t*>(q8sm60_smem); // [NCOLS][n_in] 37 float* sd = reinterpret_cast<float*>(sx + std::size_t(NCOLS) * n_in); // [NCOLS][n_in / 32] 38 const int nb = n_in / 32; 39 for (int b = int(threadIdx.x); b < NCOLS * nb; b += THREADS) { // column j = b / nb, block b % nb 40 const Q81Block* xb = x + b; 41 const int* src = reinterpret_cast<const int*>(xb->qs); 42 int* dst = reinterpret_cast<int*>(sx + std::size_t(b) * 32); 43#pragma unroll 44 for (int i = 0; i < 8; ++i) dst[i] = src[i]; 45 sd[b] = __low2float(xb->ds); 46 } 47 __syncthreads(); 48 49 constexpr int RPB = THREADS / TPR; 50 const int lane = int(threadIdx.x) % TPR; 51 const int rloc = int(threadIdx.x) / TPR; 52 const int nch = n_in / 16; // 16-byte chunks, two per block 53 const int groups = (n_out + RPB - 1) / RPB; 54 for (int g = int(blockIdx.x); g < groups; g += int(gridDim.x)) { 55 const int row = g * RPB + rloc; 56 float acc[NCOLS]; 57#pragma unroll 58 for (int j = 0; j < NCOLS; ++j) acc[j] = 0.0f; 59 if (row < n_out) { 60 const int4* w = reinterpret_cast<const int4*>(qs + std::size_t(row) * n_in); 61 const half* d = dpl + std::size_t(row) * nb; 62 for (int c = lane; c < nch; c += TPR) { 63 const int4 wv = __ldg(w + c); 64 const float dw = __half2float(d[c >> 1]); 65#pragma unroll 66 for (int j = 0; j < NCOLS; ++j) { 67 const int4 xv = reinterpret_cast<const int4*>(sx + std::size_t(j) * n_in)[c]; 68 int s = STRATA_DP4A(wv.x, xv.x, 0); 69 s = STRATA_DP4A(wv.y, xv.y, s); 70 s = STRATA_DP4A(wv.z, xv.z, s); 71 s = STRATA_DP4A(wv.w, xv.w, s); 72 acc[j] += dw * sd[j * nb + (c >> 1)] * float(s); 73 } 74 } 75 } 76#pragma unroll 77 for (int j = 0; j < NCOLS; ++j) { // every lane takes part, row or not 78 float v = acc[j]; 79#pragma unroll 80 for (int o = TPR / 2; o > 0; o >>= 1) v += __shfl_xor_sync(0xffffffffu, v, o, TPR); 81 if (lane == 0 && row < n_out) y[std::size_t(j) * n_out + row] = v; 82 } 83 } 84} 85 86// One workgroup per SM would leave too few rows in flight on a 48-SM die; four per SM is what the shapes of 87// Qwen3.8-Flash-Next measured best (STRATA_Q8_SM60_BPS overrides it). 88inline int blocks_per_sm() { 89 static const int v = [] { 90 const char* e = std::getenv("STRATA_Q8_SM60_BPS"); 91 const int n = e ? std::atoi(e) : 0; 92 return n > 0 ? n : 4; 93 }(); 94 return v; 95} 96 97inline int sm_count() { 98 int dev = 0, n = 0; 99 if (cudaGetDevice(&dev) != cudaSuccess || cudaDeviceGetAttribute(&n, cudaDevAttrMultiProcessorCount, dev) != cudaSuccess) 100 return 48; 101 return n > 0 ? n : 48; 102} 103 104template<int NCOLS, int TPR> 105void launch_one(const int8_t* qs, const half* d, const Q81Block* x, float* y, int n_in, int n_out, cudaStream_t s) { 106 constexpr int RPB = THREADS / TPR; 107 const int groups = (n_out + RPB - 1) / RPB; 108 const int grid = (std::min)(groups, sm_count() * blocks_per_sm()); 109 kernel<NCOLS, TPR><<<unsigned(grid), THREADS, smem_bytes(NCOLS, n_in), s>>>(qs, d, x, y, n_in, n_out); 110} 111 112template<int NCOLS> 113void launch_cols(const int8_t* qs, const half* d, const Q81Block* x, float* y, int n_in, int n_out, cudaStream_t s) { 114 if (n_in >= 2048) launch_one<NCOLS, 32>(qs, d, x, y, n_in, n_out, s); 115 else if (n_in >= 1024) launch_one<NCOLS, 16>(qs, d, x, y, n_in, n_out, s); 116 else launch_one<NCOLS, 8>(qs, d, x, y, n_in, n_out, s); 117} 118 119// false: a shape this kernel does not take (the caller runs its usual kernel) 120inline bool launch(const int8_t* qs, const half* d, const Q81Block* x, float* y, int n_in, int n_out, int ncols, 121 cudaStream_t s) { 122 if (n_in % 32 != 0 || n_in < 128 || ncols < 1 || ncols > MAX_COLS || smem_bytes(ncols, n_in) > MAX_SMEM) return false; 123 switch (ncols) { 124 case 1: launch_cols<1>(qs, d, x, y, n_in, n_out, s); break; 125 case 2: launch_cols<2>(qs, d, x, y, n_in, n_out, s); break; 126 case 3: launch_cols<3>(qs, d, x, y, n_in, n_out, s); break; 127 case 4: launch_cols<4>(qs, d, x, y, n_in, n_out, s); break; 128 case 5: launch_cols<5>(qs, d, x, y, n_in, n_out, s); break; 129 case 6: launch_cols<6>(qs, d, x, y, n_in, n_out, s); break; 130 case 7: launch_cols<7>(qs, d, x, y, n_in, n_out, s); break; 131 default: launch_cols<8>(qs, d, x, y, n_in, n_out, s); break; 132 } 133 return true; 134} 135 136// GGUF Q8_0 blocks (34 bytes, 2-byte aligned) -> the packed planes, on the device (the head's 0.6 GiB stays off the host) 137__global__ void pack_kernel(const Q80Block* __restrict__ blocks, int8_t* __restrict__ qs, half* __restrict__ d, 138 std::size_t count) { 139 const std::size_t b = std::size_t(blockIdx.x) * blockDim.x + threadIdx.x; 140 if (b >= count) return; 141 const Q80Block& src = blocks[b]; 142 const uint16_t* in = reinterpret_cast<const uint16_t*>(src.qs); 143 uint16_t* out = reinterpret_cast<uint16_t*>(qs + b * 32); 144#pragma unroll 145 for (int i = 0; i < 16; ++i) out[i] = in[i]; 146 d[b] = src.d; 147} 148 149} // namespace q8sm60