// Copyright (c) 2026 Peasant Smith and the Project Maya contributors (MIT; https://github.com/mw00/project-maya, v1.1.17). // Ported into Strata (third_party/project-maya/NOTICE.md, LICENSE); changes are in the git history of this file. // src/kernels/cuda/exl3_legacy.cu - v1.1.8's EXL3 decode kernels, kept for NVIDIA GPUs before sm_80 (V100, T4, // RTX 20): #180's decode kernels run the tiles on the tensor cores from sm_80 on, and their CUDA-core fallback was // slower here than these (a V100 with EXL3-3.05bpw: decode 4.9 -> 4.4 tok/s). exl3.cu's mv / moe_gate_up / moe_down // / win_experts / prepare / several_ok call these on such a GPU (STRATA_GLM_EXL3_DECODE=legacy|new picks one on any // GPU); the prompt path is exl3.cu's on every GPU. The code below is v1.1.8's as it was, the prompt path's kernels // left out. // // The codebooks and the trellis window arithmetic follow exllamav3 (MIT, turboderp - exllamav3_ext/quant/ // codebook.cuh, exl3_dq.cuh, hadamard_inner.cuh); the kernels around them are shaped for this engine's decode: // // * a block owns 128 outputs (8 tiles - exactly one output rotation block) over a slice of at most 4096 inputs; // its 8 warps take the slice's 16-row tile rows in turn, each staging the row's 8 tiles in shared memory with // coalesced loads and decoding 8 weights per lane (one fragment: 2 columns x 4 rows), F32 accumulation; // * the input rotation runs per block on the slice it reads (F32 or q8_1 in, FP16 in shared memory), so the // engine's activation buffers are used as they are; // * a matrix with more than 4096 inputs, or too few output blocks to fill the device, splits its inputs over // blocks: the partial sums go to a per-stream scratch and the LAST block of an output block adds them up in // split order (deterministic) and runs the output rotation. #if !defined(STRATA_USE_HIP) // (AMD runs exl3.cu's kernels; EXL3 does not run there anyway) #include "strata/dsv41/exl3.hpp" #include #include #include #include #include #include #include #include namespace strata::dsv41k::exl3::legacy { namespace { constexpr int kThreads = 256, kWarps = 8; constexpr int kCols = 128; // outputs per block: 8 tiles, one rotation block constexpr int kMaxKR = 4096; // inputs per block (the rotated slice lives in shared memory) constexpr float kHad = 0.088388347648f; // 1 / sqrt(128) struct Q81 { // the engine's q8_1 block (ggml block_q8_1): d, sum, 32 int8 __half2 ds; int8_t qs[32]; }; std::atomic g_launch_errors{0}; void launch_check(const char* what) { const cudaError_t e = cudaGetLastError(); if (e != cudaSuccess) { std::fprintf(stderr, "exl3 %s: %s\n", what, cudaGetErrorString(e)); g_launch_errors.fetch_add(1, std::memory_order_relaxed); } } // ---------------------------------------------------------------- the codebooks __device__ __forceinline__ uint32_t bytesum_plus(uint32_t x, uint32_t acc) { #if defined(STRATA_USE_HIP) return acc + (x & 0xffu) + ((x >> 8) & 0xffu) + ((x >> 16) & 0xffu) + (x >> 24); #else return __dp4a(x, 0x01010101u, acc); #endif } // two states -> two values, FP16 arithmetic as exllamav3's decode_3inst_2 (the tensor-core kernels take the half2) template __device__ __forceinline__ __half2 decode2h(uint32_t s0, uint32_t s1) { if constexpr (cb == 2) { const uint32_t a = bytesum_plus(s0 * 0x83DCD12Du, 0x6400u); const uint32_t b = bytesum_plus(s1 * 0x83DCD12Du, 0x6400u); const __half2 h = __halves2half2(__ushort_as_half((unsigned short) a), __ushort_as_half((unsigned short) b)); return __hfma2(h, __half2half2(__ushort_as_half(0x1eee)), __half2half2(__ushort_as_half(0xc931))); } else { uint32_t x0 = cb == 1 ? s0 * 0xCBAC1FEDu : s0 * 89226354u + 64248484u; uint32_t x1 = cb == 1 ? s1 * 0xCBAC1FEDu : s1 * 89226354u + 64248484u; x0 = (x0 & 0x8fff8fffu) ^ 0x3b603b60u; x1 = (x1 & 0x8fff8fffu) ^ 0x3b603b60u; const __half2 lo = __halves2half2(__ushort_as_half((unsigned short) (x0 & 0xffffu)), __ushort_as_half((unsigned short) (x1 & 0xffffu))); const __half2 hi = __halves2half2(__ushort_as_half((unsigned short) (x0 >> 16)), __ushort_as_half((unsigned short) (x1 >> 16))); return __hadd2(lo, hi); } } template __device__ __forceinline__ float2 decode2(uint32_t s0, uint32_t s1) { return __half22float2(decode2h(s0, s1)); } // step p's state: the 16-bit window ending at bit (p + 1) K of the tile's MSB-first bit stream (w: its 8 K words), // wrapping around the tile (the + 256 K keeps the start non-negative) template __device__ __forceinline__ uint32_t state_at(const uint32_t* w, int p) { constexpr int W = 8 * K; const int b1 = (p + 1 + 256) * K; const int i1 = (b1 - 1) >> 5, i0 = (b1 - 16) >> 5; const int sh = ((i1 + 1) << 5) - b1; const uint64_t m = ((uint64_t) w[i0 % W] << 32) | (uint64_t) w[i1 % W]; return (uint32_t) (m >> sh) & 0xffffu; } // lane's 8 steps (lane * 8 + j): v[j] sits at tile row (lane & 3) * 2 + (j & 1) + 8 (j >> 1 & 1), column // (lane >> 2) + 8 (j >> 2) template __device__ __forceinline__ void decode8(const uint32_t* w, int lane, float* v) { const int p0 = lane * 8; #pragma unroll for (int j = 0; j < 8; j += 2) { const float2 d = decode2(state_at(w, p0 + j), state_at(w, p0 + j + 1)); v[j] = d.x; v[j + 1] = d.y; } } // ---------------------------------------------------------------- the rotation // the 128-point transform of the 128 values a warp holds 4 per lane (value lane * 4 + i), over sqrt(128) __device__ __forceinline__ void had128(float* v, int lane) { const float s0 = v[0] + v[1], d0 = v[0] - v[1], s1 = v[2] + v[3], d1 = v[2] - v[3]; v[0] = s0 + s1; v[1] = d0 + d1; v[2] = s0 - s1; v[3] = d0 - d1; #pragma unroll for (int m = 1; m < 32; m <<= 1) { #pragma unroll for (int i = 0; i < 4; ++i) { const float o = __shfl_xor_sync(0xffffffffu, v[i], m); v[i] = (lane & m) ? o - v[i] : v[i] + o; } } #pragma unroll for (int i = 0; i < 4; ++i) v[i] *= kHad; } __device__ __forceinline__ float h2f(uint16_t b) { return __half2float(__ushort_as_half(b)); } // xs[i] = H(x * suh)[k0 + i] for i < kr (kr % 128 == 0); x from xf (F32) or xq (q8_1); every warp of the block __device__ void rotate_in(const float* __restrict__ xf, const Q81* __restrict__ xq, const uint16_t* __restrict__ suh, int k0, int kr, __half* xs) { const int lane = threadIdx.x & 31, warp = threadIdx.x >> 5; for (int c = warp; c < kr / 128; c += kWarps) { const int base = k0 + c * 128 + lane * 4; float v[4]; if (xf != nullptr) { #pragma unroll for (int i = 0; i < 4; ++i) v[i] = xf[base + i]; } else { const Q81* b = xq + base / 32; const float d = __low2float(b->ds); #pragma unroll for (int i = 0; i < 4; ++i) v[i] = d * (float) b->qs[base % 32 + i]; } #pragma unroll for (int i = 0; i < 4; ++i) v[i] *= h2f(suh[base + i]); had128(v, lane); #pragma unroll for (int i = 0; i < 4; ++i) xs[c * 128 + lane * 4 + i] = __float2half_rn(v[i]); } } // ---------------------------------------------------------------- the block GEMV // a lane's 4 weights of one column times the activation's 4 inputs, added to a: the rounding spelled out, so the // one-activation and the several-activation GEMVs (block_gemv, block_gemv_n) give each activation the same bits __device__ __forceinline__ float dot4_acc(float a, const float* v, float x0, float x1, float x2, float x3) { return __fadd_rn(a, __fmaf_rn(v[3], x3, __fmaf_rn(v[2], x2, __fmaf_rn(v[1], x1, __fmul_rn(v[0], x0))))); } // red[c] (c < 128) = sum over the 16-row slices [ks0, ks1) of x' . W'[:, tc0 * 16 + c], x' = xs from slice ks0 on. // tr: the matrix's first tile, ld tiles per slice row. stage: kWarps * 64 K words, red: kWarps * 128 floats. // Ends with a __syncthreads (red[0..127] complete). template __device__ void block_gemv(const uint32_t* __restrict__ tr, int ld, int tc0, int ks0, int ks1, const __half* xs, uint32_t* stage, float* red) { constexpr int W = 8 * K, TW = 8 * W; const int lane = threadIdx.x & 31, warp = threadIdx.x >> 5; uint32_t* st = stage + warp * TW; float acc[8][2]; #pragma unroll for (int t = 0; t < 8; ++t) acc[t][0] = acc[t][1] = 0.0f; const int kk = (lane & 3) * 2; for (int ks = ks0 + warp; ks < ks1; ks += kWarps) { const uint32_t* src = tr + ((size_t) ks * ld + tc0) * W; __syncwarp(); #pragma unroll for (int i = lane; i < TW; i += 32) st[i] = src[i]; __syncwarp(); const __half* xk = xs + (ks - ks0) * 16; const float x0 = __half2float(xk[kk]), x1 = __half2float(xk[kk + 1]); const float x2 = __half2float(xk[kk + 8]), x3 = __half2float(xk[kk + 9]); #pragma unroll for (int t = 0; t < 8; ++t) { float v[8]; decode8(st + t * W, lane, v); acc[t][0] = dot4_acc(acc[t][0], v, x0, x1, x2, x3); acc[t][1] = dot4_acc(acc[t][1], v + 4, x0, x1, x2, x3); } } // the 4 lanes of one lane / 4 hold the same two columns of every tile #pragma unroll for (int t = 0; t < 8; ++t) #pragma unroll for (int c = 0; c < 2; ++c) { acc[t][c] += __shfl_xor_sync(0xffffffffu, acc[t][c], 1); acc[t][c] += __shfl_xor_sync(0xffffffffu, acc[t][c], 2); } if ((lane & 3) == 0) #pragma unroll for (int t = 0; t < 8; ++t) { red[warp * kCols + t * 16 + (lane >> 2)] = acc[t][0]; red[warp * kCols + t * 16 + 8 + (lane >> 2)] = acc[t][1]; } __syncthreads(); if (threadIdx.x < kCols) { float s = 0.0f; #pragma unroll for (int w = 0; w < kWarps; ++w) s += red[w * kCols + threadIdx.x]; red[threadIdx.x] = s; } __syncthreads(); } // block_gemv for nc activations at once (nc <= kNC; activation c's rotated slice at xs + c * kMaxKR): each lane decodes // its 8 weights of a tile once and adds them into every activation's sums - as exllamav3's small-m GEMV and fused MoE // kernels share a tile's decode among the rows of an MMA, but with block_gemv's own F32 arithmetic, so outc[c * 128 + // i] (i < 128) is bit for bit block_gemv's red[i] for activation c alone. Ends with a __syncthreads. constexpr int kNC = 4; template __device__ void block_gemv_n(const uint32_t* __restrict__ tr, int ld, int tc0, int ks0, int ks1, const __half* xs, int nc, uint32_t* stage, float* red, float* outc) { constexpr int W = 8 * K, TW = 8 * W; const int lane = threadIdx.x & 31, warp = threadIdx.x >> 5; uint32_t* st = stage + warp * TW; float acc[kNC][8][2]; #pragma unroll for (int c = 0; c < kNC; ++c) #pragma unroll for (int t = 0; t < 8; ++t) acc[c][t][0] = acc[c][t][1] = 0.0f; const int kk = (lane & 3) * 2; for (int ks = ks0 + warp; ks < ks1; ks += kWarps) { const uint32_t* src = tr + ((size_t) ks * ld + tc0) * W; __syncwarp(); #pragma unroll for (int i = lane; i < TW; i += 32) st[i] = src[i]; __syncwarp(); float x[kNC][4]; #pragma unroll for (int c = 0; c < kNC; ++c) { const __half* xk = xs + c * kMaxKR + (ks - ks0) * 16; if (c < nc) { x[c][0] = __half2float(xk[kk]); x[c][1] = __half2float(xk[kk + 1]); x[c][2] = __half2float(xk[kk + 8]); x[c][3] = __half2float(xk[kk + 9]); } } #pragma unroll for (int t = 0; t < 8; ++t) { float v[8]; decode8(st + t * W, lane, v); #pragma unroll for (int c = 0; c < kNC; ++c) if (c < nc) { acc[c][t][0] = dot4_acc(acc[c][t][0], v, x[c][0], x[c][1], x[c][2], x[c][3]); acc[c][t][1] = dot4_acc(acc[c][t][1], v + 4, x[c][0], x[c][1], x[c][2], x[c][3]); } } } // per activation, block_gemv's reduction #pragma unroll for (int c = 0; c < kNC; ++c) { if (c >= nc) break; #pragma unroll for (int t = 0; t < 8; ++t) #pragma unroll for (int h = 0; h < 2; ++h) { acc[c][t][h] += __shfl_xor_sync(0xffffffffu, acc[c][t][h], 1); acc[c][t][h] += __shfl_xor_sync(0xffffffffu, acc[c][t][h], 2); } if ((lane & 3) == 0) #pragma unroll for (int t = 0; t < 8; ++t) { red[warp * kCols + t * 16 + (lane >> 2)] = acc[c][t][0]; red[warp * kCols + t * 16 + 8 + (lane >> 2)] = acc[c][t][1]; } __syncthreads(); if (threadIdx.x < kCols) { float s = 0.0f; #pragma unroll for (int w = 0; w < kWarps; ++w) s += red[w * kCols + threadIdx.x]; outc[c * kCols + threadIdx.x] = s; } __syncthreads(); } } // one warp: y[c] = alpha * (H(red) * svh)[c] + bias[c] for the block's 128 outputs __device__ __forceinline__ void epilogue(const float* red, const uint16_t* svh, float alpha, const float* bias, float* y) { const int lane = threadIdx.x & 31; float v[4]; #pragma unroll for (int i = 0; i < 4; ++i) v[i] = red[lane * 4 + i]; had128(v, lane); #pragma unroll for (int i = 0; i < 4; ++i) { const int c = lane * 4 + i; y[c] = __fmaf_rn(__fmul_rn(alpha, v[i]), h2f(svh[c]), bias != nullptr ? bias[c] : 0.0f); } } // ---------------------------------------------------------------- mv struct KJob { const uint32_t* tr; const uint16_t* suh; const uint16_t* svh; const float* xf; const Q81* xq; float* y; const float* bias; float* part; // S x n partial sums (S > 1); mv_n_kernel: nc x S x n unsigned* cnt; // n / 128 counters, zero between launches float alpha; int k, n, ld, S, kr; int nc; // mv_n_kernel: the activations (1..kNC), xf / xq / y advanced by these per activation size_t xf_cs, xq_cs, y_cs; // (floats, bytes, floats) }; struct KBatch { KJob j[kMaxMvJobs]; int blk_end[kMaxMvJobs]; int n; }; // the dynamic shared memory of the several-activation kernels (mv_n_kernel, win_gu_kernel, win_down_kernel): // red[kWarps][128] outc[kNC][128] gu[2][kNC][128] (F32), stage[kWarps * 64 K] (words), xs[kNC][kMaxKR] (FP16) - up to // 58 KB, past the 48 KB a launch gets without asking (set_smem) constexpr size_t smem_n_bytes(int K) { return (size_t) (kWarps * kCols + kNC * kCols + 2 * kNC * kCols) * 4 + (size_t) kWarps * 64 * K * 4 + (size_t) kNC * kMaxKR * 2; } struct SmemN { float *red, *outc, *gu; uint32_t* stage; __half* xs; }; template __device__ __forceinline__ SmemN smem_n(unsigned char* p) { SmemN s; s.red = (float*) p; s.outc = s.red + kWarps * kCols; s.gu = s.outc + kNC * kCols; s.stage = (uint32_t*) (s.gu + 2 * kNC * kCols); s.xs = (__half*) (s.stage + kWarps * 64 * K); return s; } template __global__ void __launch_bounds__(kThreads) mv_kernel(const KBatch b) { __shared__ __half xs[kMaxKR]; __shared__ uint32_t stage[kWarps * 64 * K]; __shared__ float red[kWarps * kCols]; __shared__ int last; int ji = 0; while ((int) blockIdx.x >= b.blk_end[ji]) ++ji; const KJob& J = b.j[ji]; const int local = (int) blockIdx.x - (ji > 0 ? b.blk_end[ji - 1] : 0); const int g = local / J.S, sp = local % J.S; const int k0 = sp * J.kr, k1 = min(J.k, k0 + J.kr); rotate_in(J.xf, J.xq, J.suh, k0, k1 - k0, xs); __syncthreads(); block_gemv(J.tr, J.ld, g * 8, k0 / 16, k1 / 16, xs, stage, red); const int warp = threadIdx.x >> 5; if (J.S == 1) { if (warp == 0) epilogue(red, J.svh + g * kCols, J.alpha, J.bias != nullptr ? J.bias + g * kCols : nullptr, J.y + g * kCols); return; } if (threadIdx.x < kCols) J.part[(size_t) sp * J.n + g * kCols + threadIdx.x] = red[threadIdx.x]; __threadfence(); __syncthreads(); if (threadIdx.x == 0) last = atomicAdd(&J.cnt[g], 1u) == (unsigned) (J.S - 1); __syncthreads(); if (!last) return; __threadfence(); if (threadIdx.x < kCols) { float s = 0.0f; for (int i = 0; i < J.S; ++i) s += *(volatile const float*) &J.part[(size_t) i * J.n + g * kCols + threadIdx.x]; red[threadIdx.x] = s; } __syncthreads(); if (warp == 0) epilogue(red, J.svh + g * kCols, J.alpha, J.bias != nullptr ? J.bias + g * kCols : nullptr, J.y + g * kCols); if (threadIdx.x == 0) J.cnt[g] = 0u; } // mv_kernel for a job's nc activations at once (a verify window's tokens): the same input split, the weights read and // decoded once for all of them (block_gemv_n), each activation's partials and sums in mv_kernel's order - every // activation's y is mv_kernel's for it alone, bit for bit template __global__ void __launch_bounds__(kThreads) mv_n_kernel(const KBatch b) { extern __shared__ __align__(16) unsigned char smem_raw[]; const SmemN sm = smem_n(smem_raw); __shared__ int last; int ji = 0; while ((int) blockIdx.x >= b.blk_end[ji]) ++ji; const KJob& J = b.j[ji]; const int local = (int) blockIdx.x - (ji > 0 ? b.blk_end[ji - 1] : 0); const int g = local / J.S, sp = local % J.S; const int k0 = sp * J.kr, k1 = min(J.k, k0 + J.kr); for (int c = 0; c < J.nc; ++c) rotate_in(J.xf != nullptr ? J.xf + c * J.xf_cs : nullptr, J.xq != nullptr ? (const Q81*) ((const uint8_t*) J.xq + c * J.xq_cs) : nullptr, J.suh, k0, k1 - k0, sm.xs + c * kMaxKR); __syncthreads(); block_gemv_n(J.tr, J.ld, g * 8, k0 / 16, k1 / 16, sm.xs, J.nc, sm.stage, sm.red, sm.outc); const int warp = threadIdx.x >> 5; const float* bias = J.bias != nullptr ? J.bias + g * kCols : nullptr; if (J.S == 1) { if (warp < J.nc) epilogue(sm.outc + warp * kCols, J.svh + g * kCols, J.alpha, bias, J.y + warp * J.y_cs + g * kCols); return; } if (threadIdx.x < kCols) for (int c = 0; c < J.nc; ++c) J.part[((size_t) c * J.S + sp) * J.n + g * kCols + threadIdx.x] = sm.outc[c * kCols + threadIdx.x]; __threadfence(); __syncthreads(); if (threadIdx.x == 0) last = atomicAdd(&J.cnt[g], 1u) == (unsigned) (J.S - 1); __syncthreads(); if (!last) return; __threadfence(); if (threadIdx.x < kCols) for (int c = 0; c < J.nc; ++c) { float s = 0.0f; for (int i = 0; i < J.S; ++i) s += *(volatile const float*) &J.part[((size_t) c * J.S + i) * J.n + g * kCols + threadIdx.x]; sm.outc[c * kCols + threadIdx.x] = s; } __syncthreads(); if (warp < J.nc) epilogue(sm.outc + warp * kCols, J.svh + g * kCols, J.alpha, bias, J.y + warp * J.y_cs + g * kCols); if (threadIdx.x == 0) J.cnt[g] = 0u; } // ---------------------------------------------------------------- the routed experts struct KMoe { const unsigned long long* plan_ptr; const float* plan_w; const float* cpu_part; const int* cpu_flag; const float* x; float* h; // [k][n_ff] const float* sh_out; float* out; float* part; // [k][n_embd] unsigned* cnt; // n_embd / 128 size_t suh[3], svh[3], tr[3]; int k, n_embd, n_ff; float limit; }; // the pieces the token's experts (moe_*_kernel) and a window's grouped experts (win_*_kernel) share, so each window // token gets the token path's bits: one warp's output block of 128 (H, then svh) ... __device__ __forceinline__ void block_out(const float* red, const uint16_t* svh, float* dst) { const int lane = threadIdx.x & 31; float v[4]; #pragma unroll for (int i = 0; i < 4; ++i) v[i] = red[lane * 4 + i]; had128(v, lane); #pragma unroll for (int i = 0; i < 4; ++i) dst[lane * 4 + i] = __fmul_rn(v[i], h2f(svh[lane * 4 + i])); } // ... the clamped swiglu ... __device__ __forceinline__ float swiglu(float gv, float uv, float limit) { if (limit > 0.0f) { gv = fminf(gv, limit); uv = fminf(fmaxf(uv, -limit), limit); } return __fmul_rn(__fdiv_rn(gv, __fadd_rn(1.0f, expf(-gv))), uv); } // ... and one output's sum: the experts' rows in plan order (p[i]: expert i's, 0 for one not on the device), the CPU // lane's part when flagged (cpu_part: null when not), the shared expert's row (sh: 0 without one) __device__ __forceinline__ float combine(const float* plan_w, const float* p, int k, const float* cpu_part, float sh) { float m = 0.0f; for (int i = 0; i < k; ++i) m = __fmaf_rn(plan_w[i], p[i], m); if (cpu_part != nullptr) m = __fadd_rn(m, *cpu_part); return __fadd_rn(m, sh); } // grid (n_ff / 128, k): expert blockIdx.y's gate and up for 128 h values, then the clamped swiglu template __global__ void __launch_bounds__(kThreads) moe_gate_up_kernel(const KMoe a) { __shared__ __half xs[2][kMaxKR]; __shared__ uint32_t stage[kWarps * 64 * K]; __shared__ float red[kWarps * kCols]; __shared__ float gu[2][kCols]; const int ei = blockIdx.y, g = blockIdx.x; const unsigned long long ptr = a.plan_ptr[ei]; if (ptr == 0ull) return; const uint8_t* blob = (const uint8_t*) ptr; const int warp = threadIdx.x >> 5; for (int r = 0; r < 2; ++r) rotate_in(a.x, nullptr, (const uint16_t*) (blob + a.suh[r]), 0, a.n_embd, xs[r]); __syncthreads(); for (int r = 0; r < 2; ++r) { block_gemv((const uint32_t*) (blob + a.tr[r]), a.n_ff / 16, g * 8, 0, a.n_embd / 16, xs[r], stage, red); if (warp == 0) block_out(red, (const uint16_t*) (blob + a.svh[r]) + g * kCols, gu[r]); __syncthreads(); } if (threadIdx.x < kCols) a.h[(size_t) ei * a.n_ff + g * kCols + threadIdx.x] = swiglu(gu[0][threadIdx.x], gu[1][threadIdx.x], a.limit); } // grid (n_embd / 128, k): expert blockIdx.y's down rows for 128 outputs; the last block of each output block adds // the experts up in plan order (as the engine's moe_down), then the CPU lane's part and the shared expert template __global__ void __launch_bounds__(kThreads) moe_down_kernel(const KMoe a) { __shared__ __half xs[kMaxKR]; __shared__ uint32_t stage[kWarps * 64 * K]; __shared__ float red[kWarps * kCols]; __shared__ int last; const int ei = blockIdx.y, g = blockIdx.x; const unsigned long long ptr = a.plan_ptr[ei]; const int warp = threadIdx.x >> 5; float* part = a.part + (size_t) ei * a.n_embd + g * kCols; if (ptr != 0ull) { const uint8_t* blob = (const uint8_t*) ptr; rotate_in(a.h + (size_t) ei * a.n_ff, nullptr, (const uint16_t*) (blob + a.suh[2]), 0, a.n_ff, xs); __syncthreads(); block_gemv((const uint32_t*) (blob + a.tr[2]), a.n_embd / 16, g * 8, 0, a.n_ff / 16, xs, stage, red); if (warp == 0) block_out(red, (const uint16_t*) (blob + a.svh[2]) + g * kCols, part); } else if (threadIdx.x < kCols) { part[threadIdx.x] = 0.0f; } __threadfence(); __syncthreads(); if (threadIdx.x == 0) last = atomicAdd(&a.cnt[g], 1u) == (unsigned) (a.k - 1); __syncthreads(); if (!last) return; __threadfence(); if (threadIdx.x < kCols) { const int r = g * kCols + threadIdx.x; float p[8]; for (int i = 0; i < a.k; ++i) p[i] = *(volatile const float*) &a.part[(size_t) i * a.n_embd + r]; a.out[r] = combine(a.plan_w, p, a.k, a.cpu_flag != nullptr && *a.cpu_flag ? a.cpu_part + r : nullptr, a.sh_out != nullptr ? a.sh_out[r] : 0.0f); } if (threadIdx.x == 0) a.cnt[g] = 0u; } // ---------------------------------------------------------------- a verify window's routed experts // gf::moe_win_experts' grouping (group g: one expert, its blob at grp_ptr[g]; its entries e = t * 8 + i at // grp_ent[grp_start[g] ..)), each group's weights read and decoded once for up to kNC of its tokens at a time (as // exllamav3's fused MoE runs a run of slots that picked one expert as the rows of one MMA tile), with the token // path's arithmetic per entry: every token's output is moe_gate_up + moe_down's for it alone, bit for bit struct KWin { const unsigned long long* grp_ptr; const int *grp_start, *grp_ent, *n_groups; const unsigned long long* plan_ptr; const float* plan_w; const float* cpu_part; const int* cpu_flag; const float* x; size_t x_stride; float* h; // [T * 8][n_ff] float* part; // [T * 8][n_embd] const float* sh_out; // [T][n_embd] float* out; size_t out_stride; size_t suh[3], svh[3], tr[3]; int k, n_embd, n_ff; float limit; }; // grid (n_ff / 128, groups at most): group blockIdx.y's gate and up for 128 h values of each entry, the clamped swiglu template __global__ void __launch_bounds__(kThreads) win_gu_kernel(const KWin a) { extern __shared__ __align__(16) unsigned char smem_raw[]; const SmemN sm = smem_n(smem_raw); const int grp = blockIdx.y, g = blockIdx.x; if (grp >= *a.n_groups) return; const uint8_t* blob = (const uint8_t*) a.grp_ptr[grp]; const int warp = threadIdx.x >> 5; const int m1 = a.grp_start[grp + 1]; for (int m0 = a.grp_start[grp]; m0 < m1; m0 += kNC) { const int nc = min(kNC, m1 - m0); for (int r = 0; r < 2; ++r) { for (int c = 0; c < nc; ++c) rotate_in(a.x + (size_t) (a.grp_ent[m0 + c] >> 3) * a.x_stride, nullptr, (const uint16_t*) (blob + a.suh[r]), 0, a.n_embd, sm.xs + c * kMaxKR); __syncthreads(); block_gemv_n((const uint32_t*) (blob + a.tr[r]), a.n_ff / 16, g * 8, 0, a.n_embd / 16, sm.xs, nc, sm.stage, sm.red, sm.outc); if (warp < nc) block_out(sm.outc + warp * kCols, (const uint16_t*) (blob + a.svh[r]) + g * kCols, sm.gu + (r * kNC + warp) * kCols); __syncthreads(); } if (threadIdx.x < kCols) for (int c = 0; c < nc; ++c) a.h[(size_t) a.grp_ent[m0 + c] * a.n_ff + g * kCols + threadIdx.x] = swiglu(sm.gu[c * kCols + threadIdx.x], sm.gu[(kNC + c) * kCols + threadIdx.x], a.limit); __syncthreads(); } } // grid (n_embd / 128, groups at most): group blockIdx.y's down rows for 128 outputs of each entry into its part rows template __global__ void __launch_bounds__(kThreads) win_down_kernel(const KWin a) { extern __shared__ __align__(16) unsigned char smem_raw[]; const SmemN sm = smem_n(smem_raw); const int grp = blockIdx.y, g = blockIdx.x; if (grp >= *a.n_groups) return; const uint8_t* blob = (const uint8_t*) a.grp_ptr[grp]; const int warp = threadIdx.x >> 5; const int m1 = a.grp_start[grp + 1]; for (int m0 = a.grp_start[grp]; m0 < m1; m0 += kNC) { const int nc = min(kNC, m1 - m0); for (int c = 0; c < nc; ++c) rotate_in(a.h + (size_t) a.grp_ent[m0 + c] * a.n_ff, nullptr, (const uint16_t*) (blob + a.suh[2]), 0, a.n_ff, sm.xs + c * kMaxKR); __syncthreads(); block_gemv_n((const uint32_t*) (blob + a.tr[2]), a.n_embd / 16, g * 8, 0, a.n_ff / 16, sm.xs, nc, sm.stage, sm.red, sm.outc); if (warp < nc) block_out(sm.outc + warp * kCols, (const uint16_t*) (blob + a.svh[2]) + g * kCols, a.part + (size_t) a.grp_ent[m0 + warp] * a.n_embd + g * kCols); __syncthreads(); } } // grid (n_embd / 128, T): each token's outputs, moe_down_kernel's combine (an entry off the device adds 0) __global__ void __launch_bounds__(kCols) win_combine_kernel(const KWin a) { const int t = blockIdx.y, r = blockIdx.x * kCols + threadIdx.x; float p[8]; for (int i = 0; i < a.k; ++i) { const int e = t * 8 + i; p[i] = a.plan_ptr[e] != 0ull ? a.part[(size_t) e * a.n_embd + r] : 0.0f; } a.out[(size_t) t * a.out_stride + r] = combine(a.plan_w + t * 8, p, a.k, a.cpu_flag[t] ? a.cpu_part + (size_t) t * a.n_embd + r : nullptr, a.sh_out != nullptr ? a.sh_out[(size_t) t * a.n_embd + r] : 0.0f); } // ---------------------------------------------------------------- dispatch // F(K, cb) for K 1..8 and the three codebooks template