okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
bc09a17e5
main
1// src/prefill/moe_helpers.cu - see include/strata/prefill/moe_mmq.hpp. The prompt experts' helpers that need nothing of llama.cpp's 2// ggml-cuda: the gathers of expert blobs into group buffers, the SwiGLU passes, the row table and the buffer sizes. P4-SPLIT: cut 3// unchanged from moe_mmq.cu into Strata's own prompt library (strata_pf_own), so that a build without llama.cpp's MMQ keeps them. 4#include "strata/prefill/moe_mmq.hpp" 5 6#include "strata/prefill/moe_fused.hpp" // kMoeEmbd, kMoeFF: the compiled geometry's widths 7 8#include "strata/quant/types.hpp" // the block geometry of a type id (Strata's own type table, no ggml) 9 10#include <cuda_runtime.h> 11 12#include <cstdint> 13#include <cstdio> 14#include <cstdlib> 15 16namespace strata::prefill::mmq { 17namespace { 18void ck(cudaError_t e, const char* what) { 19 if (e != cudaSuccess) { 20 std::fprintf(stderr, "prefill mmq: %s: %s\n", what, cudaGetErrorString(e)); 21 std::exit(1); 22 } 23} 24 25int64_t pad512(int64_t n) { return (n + 511) / 512 * 512; } 26 27// MMQ's q8_1 activation rows, for the buffer sizes: block_q8_1_mmq is 4 scales and 128 int8 values (144 bytes) for 4 x QK8_1 = 128 28// values (ggml-cuda/mmq.cuh; moe_mmq.cu checks both numbers against the real struct where it is compiled) 29constexpr size_t kQ8_1MmqBytes = 144; 30constexpr int kQK8_1 = 32; 31 32// ... plus, in the same launch, the expert's 16-byte NVFP4 tail (tail -> tail_dst) and the zeroed MMQ tails after 33// the group's last slot (nz uint4 after ab_dst's and c_dst's copies): one launch per expert instead of a kernel, 34// a 16-byte copy and, per group, two memsets. 35__global__ void copy16_kernel(const uint4* __restrict__ a, int64_t na, const uint4* __restrict__ b, int64_t nb, 36 uint4* __restrict__ ab_dst, const uint4* __restrict__ c, int64_t nc, uint4* __restrict__ c_dst, 37 const uint4* __restrict__ tail, uint4* __restrict__ tail_dst, int64_t nz) { 38 const int64_t i = (int64_t) blockIdx.x * blockDim.x + threadIdx.x; 39 const int64_t n = na + nb + nc; 40 if (i < na) ab_dst[i] = a[i]; 41 else if (i < na + nb) ab_dst[i] = b[i - na]; 42 else if (i < n) c_dst[i - na - nb] = c[i - na - nb]; 43 else if (i < n + nz) ab_dst[na + nb + (i - n)] = make_uint4(0, 0, 0, 0); 44 else if (i < n + 2 * nz) c_dst[nc + (i - n - nz)] = make_uint4(0, 0, 0, 0); 45 else if (i == n + 2 * nz && tail != nullptr) *tail_dst = *tail; 46} 47// copy16_kernel for an MMQ group: blockIdx.y is the expert (first + y); the last one (n - 1) also zeroes nz uint4 48// after its gu and d slots, and each copies its NVFP4 tail (tail_off, in uint4; tail_dst null = none) 49struct GroupArgs { 50 const uint8_t* blob[kGatherGroupMax]; 51 int64_t up_off, down_off, gu_stride, d_stride, tail_off; // in uint4 52}; 53__global__ void copy16_group_kernel(GroupArgs ga, int first, int n, int64_t na, int64_t nc, int64_t nz, 54 uint4* __restrict__ gu_dst, uint4* __restrict__ d_dst, uint4* __restrict__ tail_dst) { 55 const int q = first + (int) blockIdx.y; 56 const uint4* src = (const uint4*) ga.blob[q]; 57 uint4* ab = gu_dst + (int64_t) q * ga.gu_stride; 58 uint4* cd = d_dst + (int64_t) q * ga.d_stride; 59 const int64_t i = (int64_t) blockIdx.x * blockDim.x + threadIdx.x; 60 const int64_t m = 2 * na + nc; 61 if (i < na) ab[i] = src[i]; 62 else if (i < 2 * na) ab[i] = src[ga.up_off + (i - na)]; 63 else if (i < m) cd[i - 2 * na] = src[ga.down_off + (i - 2 * na)]; 64 else if (i < m + nz) { if (q == n - 1) ab[ga.gu_stride + (i - m)] = make_uint4(0, 0, 0, 0); } 65 else if (i < m + 2 * nz) { if (q == n - 1) cd[ga.d_stride + (i - m - nz)] = make_uint4(0, 0, 0, 0); } 66 else if (i == m + 2 * nz && tail_dst != nullptr) tail_dst[q] = src[ga.tail_off]; 67} 68__global__ void copy1_kernel(const uint8_t* __restrict__ a, int64_t na, const uint8_t* __restrict__ b, int64_t nb, 69 uint8_t* __restrict__ ab_dst, const uint8_t* __restrict__ c, int64_t nc, uint8_t* __restrict__ c_dst) { 70 const int64_t i = (int64_t) blockIdx.x * blockDim.x + threadIdx.x; 71 if (i < na) ab_dst[i] = a[i]; 72 else if (i < na + nb) ab_dst[i] = b[i - na]; 73 else if (i < na + nb + nc) c_dst[i - na - nb] = c[i - na - nb]; 74} 75 76// Strata blob: gate/up codes [2 F][E / 4 B], down codes [E][F / 4 B], gate/up scales [2 F][E / 64] f16, down scales 77// [E][F / 64] f16 (the layout of prefill/kernels.cu's blob_dequant_kernel; E, F the compiled geometry's: Flash-Next 78// [1280][640 B], [2560][160 B], [1280][40], [2560][10]). A GGUF Q2_0 block is {f16 d; 16 code 79// bytes} with the same 2-bit codes in the same order, so a block is a scale and a 16-byte run of codes. 80constexpr int Q2_GU_ROWS = fused::kMoeGuRows, Q2_ROW_GU = fused::kMoeEmbd / 4, Q2_ROW_D = fused::kMoeFF / 4, Q2_SC_GU = fused::kMoeEmbd / 64, Q2_SC_D = fused::kMoeFF / 64; 81static_assert(fused::kMoeEmbd % 64 == 0 && fused::kMoeFF % 64 == 0, "the Strata Q2 blob holds whole 64-weight blocks"); 82__global__ void strata_q2_kernel(const uint8_t* __restrict__ blob, uint16_t* __restrict__ gu, uint16_t* __restrict__ dn) { 83 constexpr size_t O_D_CODES = (size_t) Q2_GU_ROWS * Q2_ROW_GU, O_GU_SC = O_D_CODES + (size_t) fused::kMoeEmbd * Q2_ROW_D, 84 O_D_SC = O_GU_SC + (size_t) Q2_GU_ROWS * Q2_SC_GU * 2; 85 const int64_t i = (int64_t) blockIdx.x * blockDim.x + threadIdx.x; // one GGUF block 86 const int64_t n_gu = (int64_t) Q2_GU_ROWS * Q2_SC_GU, n_d = (int64_t) fused::kMoeEmbd * Q2_SC_D; 87 const uint8_t* codes; 88 const uint16_t* scale; 89 uint16_t* out; 90 if (i < n_gu) { 91 const int64_t row = i / Q2_SC_GU, b = i % Q2_SC_GU; 92 codes = blob + row * Q2_ROW_GU + b * 16; 93 scale = (const uint16_t*) (blob + O_GU_SC) + row * Q2_SC_GU + b; 94 out = gu + i * 9; 95 } else if (i < n_gu + n_d) { 96 const int64_t j = i - n_gu, row = j / Q2_SC_D, b = j % Q2_SC_D; 97 codes = blob + O_D_CODES + row * Q2_ROW_D + b * 16; 98 scale = (const uint16_t*) (blob + O_D_SC) + row * Q2_SC_D + b; 99 out = dn + j * 9; 100 } else { 101 return; 102 } 103 const uint4 q = *(const uint4*) codes; 104 const uint16_t* qh = (const uint16_t*) &q; 105 out[0] = *scale; 106#pragma unroll 107 for (int k = 0; k < 8; ++k) out[1 + k] = qh[k]; 108} 109 110__global__ void swiglu_kernel(const float* __restrict__ gu, float* __restrict__ h, int64_t rows, int64_t n_ff, 111 bool interleaved) { 112 const int64_t i = (int64_t) blockIdx.x * blockDim.x + threadIdx.x; 113 if (i >= rows * n_ff) return; 114 const int64_t r = i / n_ff, k = i % n_ff; 115 const float* row = gu + r * 2 * n_ff; 116 const float g = interleaved ? row[2 * k] : row[k], u = interleaved ? row[2 * k + 1] : row[n_ff + k]; 117 h[i] = g / (1.0f + __expf(-g)) * u; 118} 119 120// swiglu_kernel on the MMQ gate/up output with each expert's gate and up scales applied as it is read: the products 121// scale_gu_rows stored, without the pass that stored them 122__global__ void swiglu_scaled_kernel(const float* __restrict__ gu, float* __restrict__ h, int64_t rows, int64_t n_ff, 123 const int32_t* __restrict__ bounds, int n, const float* __restrict__ tails, 124 int64_t row0) { 125 const int64_t i = (int64_t) blockIdx.x * blockDim.x + threadIdx.x; 126 if (i >= rows * n_ff) return; 127 const int64_t r = i / n_ff, k = i % n_ff, ra = row0 + r; 128 int q = 0; 129 while (q + 1 < n && ra >= bounds[q + 1]) ++q; 130 const float* row = gu + r * 2 * n_ff; 131 const float g = row[k] * tails[4 * q], u = row[n_ff + k] * tails[4 * q + 1]; 132 h[i] = g / (1.0f + __expf(-g)) * u; 133} 134__global__ void down_row_scales_kernel(float* __restrict__ sd, const int32_t* __restrict__ bounds, int n, 135 const float* __restrict__ tails, int64_t nrows) { 136 const int64_t r = (int64_t) blockIdx.x * blockDim.x + threadIdx.x; 137 if (r >= nrows) return; 138 int q = 0; 139 while (q + 1 < n && r >= bounds[q + 1]) ++q; 140 sd[r] = tails[4 * q + 2]; 141} 142 143__global__ void iota_kernel(int32_t* dst, int64_t n) { 144 const int64_t i = (int64_t) blockIdx.x * blockDim.x + threadIdx.x; 145 if (i < n) dst[i] = (int32_t) i; 146} 147 148unsigned blocks(int64_t n) { return (unsigned) ((n + 255) / 256); } 149 150} // namespace 151 152// The block geometry comes from Strata's type table (src/quant/types.cpp): the GGUF format's ids with ggml's sizes, the trellis types 153// 144-149 included (128 weights; 34 / 26 / 30 / 42 / 50 / 58 bytes); tests/quant/type_geometry_oracle.cpp checks it against ggml's 154// own table. An id the table does not know has no size here (0): no prompt path reads such a matrix. 155size_t matrix_bytes(int t, int64_t rows, int64_t cols) { 156 const ::strata::quant::TypeInfo* ti = ::strata::quant::type_info(t); 157 if (ti == nullptr) return 0; 158 return (size_t) rows * (size_t) (cols / ti->blck) * ti->bytes; 159} 160 161size_t q8_bytes(int64_t rows, int64_t cols) { 162 return (size_t) rows * (size_t) pad512(cols) * kQ8_1MmqBytes / (4 * kQK8_1) + 128 * kQ8_1MmqBytes; 163} 164 165void gather_native(const void* gate, const void* up, size_t gu_half_bytes, const void* down, size_t d_bytes, 166 void* gu_dst, void* d_dst, void* stream, const void* tail, void* tail_dst, size_t zero_bytes) { 167 const cudaStream_t s = (cudaStream_t) stream; 168 const bool a16 = ((uintptr_t) gate | (uintptr_t) up | (uintptr_t) down | (uintptr_t) gu_dst | (uintptr_t) d_dst | 169 (uintptr_t) tail | (uintptr_t) tail_dst | gu_half_bytes | d_bytes | zero_bytes) % 16 == 0; 170 if (a16) { 171 const int64_t na = (int64_t) gu_half_bytes / 16, nc = (int64_t) d_bytes / 16, nz = (int64_t) zero_bytes / 16; 172 copy16_kernel<<<blocks(2 * na + nc + 2 * nz + 1), 256, 0, s>>>( 173 (const uint4*) gate, na, (const uint4*) up, na, (uint4*) gu_dst, (const uint4*) down, nc, (uint4*) d_dst, 174 (const uint4*) tail, (uint4*) tail_dst, nz); 175 } else { 176 if (tail) cudaMemcpyAsync(tail_dst, tail, 16, cudaMemcpyDeviceToDevice, s); 177 if (zero_bytes) { 178 cudaMemsetAsync((uint8_t*) gu_dst + 2 * gu_half_bytes, 0, zero_bytes, s); 179 cudaMemsetAsync((uint8_t*) d_dst + d_bytes, 0, zero_bytes, s); 180 } 181 const int64_t na = (int64_t) gu_half_bytes, nc = (int64_t) d_bytes; 182 copy1_kernel<<<blocks(2 * na + nc), 256, 0, s>>>((const uint8_t*) gate, na, (const uint8_t*) up, na, 183 (uint8_t*) gu_dst, (const uint8_t*) down, nc, (uint8_t*) d_dst); 184 } 185 ck(cudaGetLastError(), "gather_native"); 186} 187 188bool gather_native_group(const GatherGroup& g, size_t up_off, size_t gu_half_bytes, size_t down_off, size_t d_bytes, 189 void* gu_dst, size_t gu_stride, void* d_dst, size_t d_stride, void* stream, size_t tail_off, 190 void* tail_dst, size_t zero_bytes) { 191 if (g.first < 0 || g.n <= g.first || g.n > kGatherGroupMax) return false; 192 if (tail_off == 0) tail_dst = nullptr; 193 uintptr_t a = (uintptr_t) gu_dst | (uintptr_t) d_dst | up_off | gu_half_bytes | down_off | d_bytes | gu_stride | d_stride | 194 tail_off | (uintptr_t) tail_dst | zero_bytes; 195 for (int q = g.first; q < g.n; ++q) a |= (uintptr_t) g.blob[q]; 196 if (a % 16 != 0) return false; 197 GroupArgs ga{}; 198 for (int q = g.first; q < g.n; ++q) ga.blob[q] = g.blob[q]; 199 ga.up_off = (int64_t) up_off / 16; 200 ga.down_off = (int64_t) down_off / 16; 201 ga.gu_stride = (int64_t) gu_stride / 16; 202 ga.d_stride = (int64_t) d_stride / 16; 203 ga.tail_off = (int64_t) tail_off / 16; 204 const int64_t na = (int64_t) gu_half_bytes / 16, nc = (int64_t) d_bytes / 16, nz = (int64_t) zero_bytes / 16; 205 const int64_t work = 2 * na + nc + 2 * nz + (tail_dst != nullptr ? 1 : 0); 206 copy16_group_kernel<<<dim3(blocks(work), (unsigned) (g.n - g.first)), 256, 0, (cudaStream_t) stream>>>( 207 ga, g.first, g.n, na, nc, nz, (uint4*) gu_dst, (uint4*) d_dst, (uint4*) tail_dst); 208 ck(cudaGetLastError(), "gather_native_group"); 209 return true; 210} 211 212void gather_strata_q2(const uint8_t* blob, void* gu_dst, void* d_dst, void* stream) { 213 strata_q2_kernel<<<blocks((int64_t) Q2_GU_ROWS * Q2_SC_GU + (int64_t) fused::kMoeEmbd * Q2_SC_D), 256, 0, (cudaStream_t) stream>>>(blob, (uint16_t*) gu_dst, 214 (uint16_t*) d_dst); 215 ck(cudaGetLastError(), "gather_strata_q2"); 216} 217 218void swiglu_scaled(const float* gu, float* h, int64_t rows, int64_t n_ff, const int32_t* bounds, int n, 219 const float* tails, int64_t row0, void* stream) { 220 if (rows <= 0) return; 221 swiglu_scaled_kernel<<<blocks(rows * n_ff), 256, 0, (cudaStream_t) stream>>>(gu, h, rows, n_ff, bounds, n, tails, row0); 222 ck(cudaGetLastError(), "swiglu_scaled"); 223} 224void down_row_scales(float* sd, const int32_t* bounds, int n, const float* tails, int64_t nrows, void* stream) { 225 if (nrows <= 0 || n <= 0) return; 226 down_row_scales_kernel<<<blocks(nrows), 256, 0, (cudaStream_t) stream>>>(sd, bounds, n, tails, nrows); 227 ck(cudaGetLastError(), "down_row_scales"); 228} 229 230void swiglu(const float* gu, float* h, int64_t rows, int64_t n_ff, bool interleaved, void* stream) { 231 if (rows <= 0) return; 232 swiglu_kernel<<<blocks(rows * n_ff), 256, 0, (cudaStream_t) stream>>>(gu, h, rows, n_ff, interleaved); 233 ck(cudaGetLastError(), "swiglu"); 234} 235 236void iota(int32_t* dst, int64_t n, void* stream) { 237 if (n <= 0) return; 238 iota_kernel<<<blocks(n), 256, 0, (cudaStream_t) stream>>>(dst, n); 239 ck(cudaGetLastError(), "iota"); 240} 241 242} // namespace strata::prefill::mmq