okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
6ca60e9a0
main
1// src/prefill/moe_ws_gu_iq3.cu - weight-stationary gate/up products (see moe_ws.hpp): IQ3_XXS and IQ3_S, [1280 x 2560] 2// read in place. A CTA = 128 threads = 128 weight rows of one expert (a row per thread, all of the chunk's activation 3// rows TPP at a time, the sums in registers); K advances 128 values a step (half a 256-value block = 4 sub-blocks): 4// the step's bytes of the 128 rows and the step's q8_1 block of each token go to shared memory (the activations split 5// to int16 pairs once), then each thread decodes its row's 4 sub-blocks once per pass and runs them against the tokens. 6// Bits: per 32-value sub-block MMQ's int8 values (ggml_cuda_mmq_load_tiles_iq3_*: grid entries, its __vcmpne4 sign 7// masks applied as (g ^ s) + (s & 0x01010101) == __vsub4(g ^ s, s), every grid byte >= 1) and scale expression, then 8// MMQ's epilogue acc = fma(dx * dy, float(sum), acc), sub-blocks in K order. (Measured on a GP100 die: 9// ws / per-group MMQ 0.31-0.35 at 2.5 rows per expert, 0.49-0.52 at 10, 0.67-0.76 at 35, 10// 0.78-0.79 at 160; bit-identical, compute-sanitizer clean with exact-size matrices.) 11// 12// Staging without per-lane branches: rows are 980 / 1100 B, so block kc starts 2 * (kc & 1) bytes into a 4-byte word 13// for the whole CTA - an even block's words are two aligned loads and a funnel shift (the second load ends at most 2 14// bytes into block kc + 1 of the same row), an odd block's are aligned; d sits the other way round (an odd block's d is 15// the high half of the word 2 bytes before it, inside block kc - 1). Every load of a lane is issued before its stores, 16// and nothing outside a row is read (the last row's last block is odd). 17#include "common.cuh" 18#include "mmq.cuh" 19 20#include "moe_ws.cuh" 21 22#include <cstdio> 23#include <cstdlib> 24 25namespace strata::prefill::ws::detail { 26namespace { 27 28constexpr int GU_ROWS = 1280, GU_K = 2560, NB = GU_K / QK_K, NSTEP = 2 * NB, NT = 128, PIECES = GU_ROWS / NT; 29 30void ck(cudaError_t e, const char* what) { 31 if (e != cudaSuccess) { 32 std::fprintf(stderr, "prefill ws: %s: %s\n", what, cudaGetErrorString(e)); 33 std::exit(1); 34 } 35} 36 37// ---- IQ3_XXS: 98-B blocks; per row and step 8 grid-index words, 4 aux words (signs + scale) and d (stride 13). 38// Every lane slot is one word type: grid words rows tid/8 + 16 i (word tid%8), aux words rows tid/4 + 32 i (word 39// tid%4), d the thread's own row. Token groups of TG: one branch per group on a partial pass (a group's tokens past 40// the count run on copies of row nt - 1 and are not written), none on a full pass. 41namespace xxs3 { 42constexpr int BB = (int) sizeof(block_iq3_xxs), ROWB = NB * BB, CWS = 13; 43static_assert(BB == 98 && ROWB % 4 == 0, "block_iq3_xxs"); 44 45template <int TPP, int CH, int TG, bool FULL> 46__device__ __forceinline__ void sb_compute(const int* __restrict__ cw, const int s, const uint32_t* __restrict__ grid, 47 const int2* __restrict__ lut, const int (*act_s)[4][16], 48 const float (*dy_s)[4], const int nt, float (&acc)[TPP]) { 49 int wv[16]; 50 float dx; 51 { 52 const uint32_t qa = (uint32_t) cw[2 * s + 0], qb = (uint32_t) cw[2 * s + 1]; // 8 grid indices 53 const uint32_t aux32 = (uint32_t) cw[8 + s]; 54#pragma unroll 55 for (int l = 0; l < QR3_XXS; ++l) { 56 const uint32_t qw = l < 2 ? qa : qb; 57 const uint32_t g0 = grid[(qw >> (16 * (l & 1))) & 0xFF], g1 = grid[(qw >> (16 * (l & 1) + 8)) & 0xFF]; 58 const int2 sg = lut[(aux32 >> (7 * l)) & 0x7F]; 59 split2((int) ((g0 ^ (uint32_t) sg.x) + ((uint32_t) sg.x & 0x01010101u)), wv[4 * l + 0], wv[4 * l + 1]); 60 split2((int) ((g1 ^ (uint32_t) sg.y) + ((uint32_t) sg.y & 0x01010101u)), wv[4 * l + 2], wv[4 * l + 3]); 61 } 62 const int ls = aux32 >> 28; 63 const float d = __ushort_as_half((unsigned short) (cw[12] & 0xFFFF)); 64 dx = (ls*d + d/2)/2; // MMQ's expression, verbatim 65 } 66#pragma unroll 67 for (int t0 = 0; t0 < TPP; t0 += TG) { 68 if (FULL || t0 < nt) { 69#pragma unroll 70 for (int t = t0; t < t0 + TG; ++t) { 71 const int4* a4 = (const int4*) act_s[t][s]; 72 const int4 a0 = a4[0], a1 = a4[1], a2 = a4[2], a3 = a4[3]; 73 const int a[16] = {a0.x, a0.y, a0.z, a0.w, a1.x, a1.y, a1.z, a1.w, a2.x, a2.y, a2.z, a2.w, a3.x, a3.y, a3.z, a3.w}; 74 const float dy = dy_s[t][s]; 75 int sc[CH]; 76 sc[0] = kMagicI; 77#pragma unroll 78 for (int c = 1; c < CH; ++c) sc[c] = 0; 79#pragma unroll 80 for (int m = 0; m < 16; ++m) sc[m % CH] = mad2(wv[m], a[m], sc[m % CH]); 81 int sum = sc[0]; 82#pragma unroll 83 for (int c = 1; c < CH; ++c) sum += sc[c]; 84 acc[t] = __fmaf_rn(__fmul_rn(dx, dy), __fsub_rn(__int_as_float(sum), kMagicF), acc[t]); 85 } 86 } 87 } 88} 89 90template <int TPP, int CH, int TG> 91__global__ void __launch_bounds__(NT, 1) gu_kernel(const Chunk* __restrict__ chunks, const char* __restrict__ w, 92 const int64_t* __restrict__ xoff, const block_q8_1_mmq* __restrict__ xq, 93 const int total_rows, float* __restrict__ dst) { 94 static_assert(TPP % TG == 0 && (TPP * 32) % NT == 0 && TPP * 4 <= NT, ""); 95 constexpr int NG = 8, RG = NT / 8; // grid-index-word slots: rows tid/8 + i*RG, word tid%8 96 constexpr int NX = 4, RX = NT / 4; // aux-word slots: rows tid/4 + i*RX, word tid%4 97 constexpr int NA = TPP * 32 / NT; 98 __shared__ uint32_t grid_s[256]; 99 __shared__ int2 lut_s[128]; 100 __shared__ __align__(16) int act_s[TPP][4][16]; 101 __shared__ float dy_s[TPP][4]; 102 __shared__ int wsm[NT * CWS]; 103 const int tid = threadIdx.x; 104 for (int i = tid; i < 256; i += NT) grid_s[i] = iq3xxs_grid[i]; 105 for (int i = tid; i < 128; i += NT) { 106 const uint32_t signs = unpack_ksigns((uint8_t) i); 107 lut_s[i] = make_int2(__vcmpne4(signs & 0x08040201, 0), __vcmpne4(signs & 0x80402010, 0)); 108 } 109 int r0; 110 const Chunk c = cta_work<PIECES, NT>(chunks, r0); 111 const char* __restrict__ W = w + xoff[c.j] + (size_t) r0 * ROWB; 112 const char* __restrict__ Wg = W + (size_t) (tid >> 3) * ROWB + 4 * (tid & 7); // + i*RG*ROWB + kc*BB + 32h 113 const char* __restrict__ Wx = W + (size_t) (tid >> 2) * ROWB + 64 + 4 * (tid & 3); // + i*RX*ROWB + kc*BB + 16h 114 const char* __restrict__ Wd = W + (size_t) tid * ROWB; 115 int* __restrict__ sg_ = wsm + (tid >> 3) * CWS + (tid & 7); 116 int* __restrict__ sx_ = wsm + (tid >> 2) * CWS + 8 + (tid & 3); 117 int* __restrict__ sd_ = wsm + tid * CWS + 12; 118 const int* cw = wsm + tid * CWS; 119 for (int p0 = 0; p0 < c.rows; p0 += TPP) { 120 const int nt = min(TPP, c.rows - p0); 121 const int trow0 = c.row0 + p0; 122 float acc[TPP]; 123#pragma unroll 124 for (int t = 0; t < TPP; ++t) acc[t] = 0.0f; 125#pragma unroll 1 126 for (int hc = 0; hc < NSTEP; ++hc) { 127 const int kc = hc >> 1, h = hc & 1; 128 const int* xr = (const int*) (xq + (size_t) hc * total_rows + trow0); 129 __syncthreads(); 130 int av[NA], dv = 0; 131#pragma unroll 132 for (int i = 0; i < NA; ++i) { 133 const int idx = tid + i * NT, t = idx >> 5; 134 av[i] = __ldg(xr + min(t, nt - 1) * 36 + 4 + (idx & 31)); // clamped: no branch, never past row nt-1 135 } 136 if (TPP * 4 == NT || tid < TPP * 4) dv = __ldg(xr + min(tid >> 2, nt - 1) * 36 + (tid & 3)); 137 const char* bg = Wg + kc * BB + 32 * h; // even kc: the aligned word 2 bytes before the data word 138 const char* bx = Wx + kc * BB + 16 * h; 139 if (kc & 1) { // odd block: data words aligned (at +2); d = high half of the word 2 bytes before the block 140 int vg[NG], vx[NX]; 141#pragma unroll 142 for (int i = 0; i < NG; ++i) vg[i] = __ldg((const int*) (bg + 2 + (size_t) i * RG * ROWB)); 143#pragma unroll 144 for (int i = 0; i < NX; ++i) vx[i] = __ldg((const int*) (bx + 2 + (size_t) i * RX * ROWB)); 145 const int vd = __ldg((const int*) (Wd + kc * BB - 2)); 146#pragma unroll 147 for (int i = 0; i < NG; ++i) sg_[i * RG * CWS] = vg[i]; 148#pragma unroll 149 for (int i = 0; i < NX; ++i) sx_[i * RX * CWS] = vx[i]; 150 *sd_ = (int) ((uint32_t) vd >> 16); 151 } else { // even block: data words 2 bytes into a word: two aligned words + funnel shift; d = low half 152 int lg[NG], hg[NG], lx[NX], hx[NX]; 153#pragma unroll 154 for (int i = 0; i < NG; ++i) { 155 lg[i] = __ldg((const int*) (bg + (size_t) i * RG * ROWB)); 156 hg[i] = __ldg((const int*) (bg + 4 + (size_t) i * RG * ROWB)); 157 } 158#pragma unroll 159 for (int i = 0; i < NX; ++i) { 160 lx[i] = __ldg((const int*) (bx + (size_t) i * RX * ROWB)); 161 hx[i] = __ldg((const int*) (bx + 4 + (size_t) i * RX * ROWB)); 162 } 163 const int vd = __ldg((const int*) (Wd + kc * BB)); 164#pragma unroll 165 for (int i = 0; i < NG; ++i) sg_[i * RG * CWS] = (int) __funnelshift_r((uint32_t) lg[i], (uint32_t) hg[i], 16); 166#pragma unroll 167 for (int i = 0; i < NX; ++i) sx_[i * RX * CWS] = (int) __funnelshift_r((uint32_t) lx[i], (uint32_t) hx[i], 16); 168 *sd_ = vd; 169 } 170#pragma unroll 171 for (int i = 0; i < NA; ++i) { 172 const int idx = tid + i * NT, t = idx >> 5, wd = idx & 31; 173 int lo, hi; 174 split2(av[i], lo, hi); 175 *(int2*) &act_s[t][wd >> 3][2 * (wd & 7)] = make_int2(lo, hi); 176 } 177 if (TPP * 4 == NT || tid < TPP * 4) dy_s[tid >> 2][tid & 3] = __int_as_float(dv); 178 __syncthreads(); 179 if (nt == TPP) { 180#pragma unroll 1 181 for (int s = 0; s < 4; ++s) sb_compute<TPP, CH, TG, true>(cw, s, grid_s, lut_s, act_s, dy_s, nt, acc); 182 } else { 183#pragma unroll 1 184 for (int s = 0; s < 4; ++s) sb_compute<TPP, CH, TG, false>(cw, s, grid_s, lut_s, act_s, dy_s, nt, acc); 185 } 186 } 187#pragma unroll 188 for (int t = 0; t < TPP; ++t) 189 if (t < nt) dst[(size_t) (trow0 + t) * GU_ROWS + r0 + tid] = acc[t]; 190 } 191} 192} // namespace xxs3 193 194// ---- IQ3_S: 110-B blocks; per row and step a compact copy of the bytes the 4 sub-blocks read - qs 32 B, signs 16 B, 195// qh 4 B, d and the 2 scale bytes: 14 words, row stride 15 - staged by lanes 0-13 / 16-29 of each warp (two rows per 196// warp and k), each word built from two aligned loads by one PRMT. The sign masks come from a 256-entry table of MMQ's 197// own __vcmpne4 expressions. Token groups of TG behind one `t < nt` branch. 198namespace s3 { 199constexpr int BB = 110, ROWB = NB * BB, CW = 14, CWS = 15, NW = NT / 32, KN = NT / (2 * NW); 200 201__device__ __forceinline__ int2 mmq_signs(const int sb) { // ggml_cuda_mmq_load_tiles_iq3_s's, verbatim 202 const int s0 = __vcmpne4(((sb & 0x03) << 7) | ((sb & 0x0C) << 21), 0x00000000); 203 const int s1 = __vcmpne4(((sb & 0x30) << 3) | ((sb & 0xC0) << 17), 0x00000000); 204 return make_int2(s0, s1); 205} 206 207// sub-block s of the staged half: kqsx = 4 h + s's 8 int8x4 words and scale. cw: [0..7] qs, [8..11] sign bytes, 208// [12] qh, [13] d | the half's 2 scale bytes << 16 209__device__ __forceinline__ float dec(const int* __restrict__ cw, const int s, const uint32_t* __restrict__ grid, 210 const int2* __restrict__ lut, int (&q)[8]) { 211 const int2 qs_packed = make_int2(cw[2 * s + 0], cw[2 * s + 1]); 212 const uint8_t* qs = (const uint8_t*) &qs_packed; 213 const int qh = (cw[12] >> (8 * s)) & 0xFF; 214 const int signs_packed_32 = cw[8 + s]; 215 const int w13 = cw[13]; 216#pragma unroll 217 for (int l = 0; l < QR3_S; ++l) { 218 const int2 grid_pos = make_int2(grid[qs[2 * l + 0] | ((qh << (8 - 2 * l)) & 0x100)], 219 grid[qs[2 * l + 1] | ((qh << (7 - 2 * l)) & 0x100)]); 220 const int2 sg = lut[(signs_packed_32 >> (8 * l)) & 0xFF]; 221 q[2 * l + 0] = (grid_pos.x ^ sg.x) + (sg.x & 0x01010101); 222 q[2 * l + 1] = (grid_pos.y ^ sg.y) + (sg.y & 0x01010101); 223 } 224 const int ls = 1 + 2 * (((w13 >> (16 + 8 * (s >> 1))) >> (4 * (s & 1))) & 0x0F); 225 const float d = __ushort_as_half((unsigned short) (w13 & 0xFFFF)); 226 return ls * d; 227} 228 229// compact word ci of step half h in a block of parity odd: its two aligned words (offsets from the block) and selector 230__device__ __forceinline__ void stage_map(const int ci, const int h, const int odd, int& o0, int& o1, int& sel) { 231 const int off = ci < 8 ? 2 + 32 * h + 4 * ci : ci < 12 ? 74 + 16 * h + 4 * (ci - 8) : 66 + 4 * h; 232 const bool dw = ci == 13; 233 o0 = dw ? (odd ? -2 : 0) : (odd ? off : off - 2); 234 o1 = dw ? (odd ? 106 : 104 + 4 * h) : (odd ? off : off + 2); 235 sel = dw ? (odd ? (h ? 0x7632 : 0x5432) : (h ? 0x5410 : 0x7610)) : (odd ? 0x3210 : 0x5432); 236} 237 238template <int TPP, int TG> 239__device__ __forceinline__ void sb_compute(const int* __restrict__ cw, const int s, const uint32_t* __restrict__ grid, 240 const int2* __restrict__ lut, const int (*act_s)[4][16], const float (*dy_s)[4], 241 const int nt, float (&acc)[TPP]) { 242 int q[8]; 243 const float dx = dec(cw, s, grid, lut, q); 244 int wv[16]; 245#pragma unroll 246 for (int m = 0; m < 8; ++m) split2(q[m], wv[2 * m], wv[2 * m + 1]); 247#pragma unroll 248 for (int t0 = 0; t0 < TPP; t0 += TG) { 249 if (t0 < nt) { 250#pragma unroll 251 for (int t = t0; t < t0 + TG; ++t) { 252 const int4* a4 = (const int4*) act_s[t][s]; 253 const int4 a0 = a4[0], a1 = a4[1], a2 = a4[2], a3 = a4[3]; 254 const float dy = dy_s[t][s]; 255 int s0 = 0, s1 = 0, s2 = 0, s3 = 0; 256 s0 = mad2(wv[0], a0.x, s0); s1 = mad2(wv[1], a0.y, s1); s2 = mad2(wv[2], a0.z, s2); s3 = mad2(wv[3], a0.w, s3); 257 s0 = mad2(wv[4], a1.x, s0); s1 = mad2(wv[5], a1.y, s1); s2 = mad2(wv[6], a1.z, s2); s3 = mad2(wv[7], a1.w, s3); 258 s0 = mad2(wv[8], a2.x, s0); s1 = mad2(wv[9], a2.y, s1); s2 = mad2(wv[10], a2.z, s2); s3 = mad2(wv[11], a2.w, s3); 259 s0 = mad2(wv[12], a3.x, s0); s1 = mad2(wv[13], a3.y, s1); s2 = mad2(wv[14], a3.z, s2); s3 = mad2(wv[15], a3.w, s3); 260 acc[t] = __fmaf_rn(__fmul_rn(dx, dy), __int2float_rn((s0 + s1) + (s2 + s3)), acc[t]); 261 } 262 } 263 } 264} 265 266template <int TPP, int TG> 267__global__ void __launch_bounds__(NT) gu_kernel(const Chunk* __restrict__ chunks, const char* __restrict__ w, 268 const int64_t* __restrict__ xoff, const block_q8_1_mmq* __restrict__ xq, 269 const int total_rows, float* __restrict__ dst) { 270 static_assert(TPP % TG == 0, ""); 271 constexpr size_t KSTR = (size_t) 2 * NW * ROWB; // a staging lane's rows are 2 * NW apart 272 __shared__ uint32_t grid_s[512]; 273 __shared__ int2 lut_s[256]; 274 __shared__ __align__(16) int act_s[TPP][4][16]; 275 __shared__ float dy_s[TPP][4]; 276 __shared__ int wsm[NT * CWS]; 277 for (int i = threadIdx.x; i < 512; i += NT) grid_s[i] = iq3s_grid[i]; 278 for (int i = threadIdx.x; i < 256; i += NT) lut_s[i] = mmq_signs(i); 279 int r0; 280 const Chunk c = cta_work<PIECES, NT>(chunks, r0); 281 const int lane = threadIdx.x & 31, warp = threadIdx.x >> 5; 282 const int ci = lane & 15, rsub = lane >> 4; // lanes 0-13 stage one row, 16-29 the next; ci = the compact word 283 const bool cact = ci < CW; 284 const char* __restrict__ Wr = w + xoff[c.j] + (size_t) (r0 + 2 * warp + rsub) * ROWB; // + k * KSTR 285 int* __restrict__ wsl = wsm + (2 * warp + rsub) * CWS + ci; // + k * 2 NW * CWS 286 for (int p0 = 0; p0 < c.rows; p0 += TPP) { 287 const int nt = min(TPP, c.rows - p0); 288 const int trow0 = c.row0 + p0; 289 float acc[TPP]; 290#pragma unroll 291 for (int t = 0; t < TPP; ++t) acc[t] = 0.0f; 292#pragma unroll 1 293 for (int hc = 0; hc < NSTEP; ++hc) { 294 __syncthreads(); 295 if (cact) { // branch-free inside: every lane issues its 2 * KN loads, then its KN stores 296 int o0, o1, sel; 297 stage_map(ci, hc & 1, (hc >> 1) & 1, o0, o1, sel); 298 const char* b = Wr + (hc >> 1) * BB; 299 int v0[KN], v1[KN]; 300#pragma unroll 301 for (int k = 0; k < KN; ++k) v0[k] = __ldg((const int*) (b + k * KSTR + o0)); 302#pragma unroll 303 for (int k = 0; k < KN; ++k) v1[k] = __ldg((const int*) (b + k * KSTR + o1)); 304#pragma unroll 305 for (int k = 0; k < KN; ++k) wsl[k * 2 * NW * CWS] = (int) __byte_perm(v0[k], v1[k], sel); 306 } 307 { // the step's activations: nt consecutive 144-B blocks, loads first, then split + store 308 constexpr int NA = (TPP * 36 + NT - 1) / NT; 309 int av[NA]; 310 const int* xr = (const int*) (xq + (size_t) hc * total_rows + trow0); 311#pragma unroll 312 for (int i = 0; i < NA; ++i) { 313 const int idx = threadIdx.x + i * NT; 314 if (idx < nt * 36) av[i] = __ldg(xr + idx); 315 } 316#pragma unroll 317 for (int i = 0; i < NA; ++i) { 318 const int idx = threadIdx.x + i * NT; 319 if (idx < nt * 36) { 320 const int t = idx / 36, wd = idx - t * 36; 321 if (wd < 4) { 322 dy_s[t][wd] = __int_as_float(av[i]); 323 } else { 324 const int m = wd - 4; 325 int lo, hi; 326 split2(av[i], lo, hi); 327 *(int2*) &act_s[t][m / 8][2 * (m % 8)] = make_int2(lo, hi); 328 } 329 } 330 } 331 } 332 __syncthreads(); 333 const int* cw = wsm + threadIdx.x * CWS; 334#pragma unroll 1 335 for (int s = 0; s < 4; ++s) sb_compute<TPP, TG>(cw, s, grid_s, lut_s, act_s, dy_s, nt, acc); 336 } 337#pragma unroll 338 for (int t = 0; t < TPP; ++t) 339 if (t < nt) dst[(size_t) (trow0 + t) * GU_ROWS + r0 + threadIdx.x] = acc[t]; 340 } 341} 342} // namespace s3 343 344template <typename F> 345void go(F kernel, const Layer& L, cudaStream_t s) { 346 kernel<<<(unsigned) (L.nchunks * PIECES), NT, 0, s>>>(L.chunks, (const char*) L.w, L.xoff, (const block_q8_1_mmq*) L.xq, 347 (int) L.rows, L.gu); 348} 349 350} // namespace 351 352// tokens per pass by u (the measured crossovers): IQ3_XXS 8 below 5 rows per expert, 16 below 25, then 32 (4-token 353// groups); IQ3_S 8 below 4, 16 below 15, then 32 354void gu_iq3_xxs(const Layer& L, double u, cudaStream_t s) { 355 if (L.nchunks <= 0) return; 356 if (u < 5.0) go(xxs3::gu_kernel<8, 4, 2>, L, s); 357 else if (u < 25.0) go(xxs3::gu_kernel<16, 4, 2>, L, s); 358 else go(xxs3::gu_kernel<32, 2, 4>, L, s); 359 ck(cudaGetLastError(), "gate/up IQ3_XXS"); 360} 361 362void gu_iq3_s(const Layer& L, double u, cudaStream_t s) { 363 if (L.nchunks <= 0) return; 364 if (u < 4.0) go(s3::gu_kernel<8, 2>, L, s); 365 else if (u < 15.0) go(s3::gu_kernel<16, 2>, L, s); 366 else go(s3::gu_kernel<32, 4>, L, s); 367 ck(cudaGetLastError(), "gate/up IQ3_S"); 368} 369 370} // namespace strata::prefill::ws::detail