// 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/ds4/ds4_gguf.cu - a GGUF's BF16 matrices and MXFP4 experts on the device (see ds4_gguf.cuh). #include "ds4_gguf.cuh" #include "ds4_iq.cuh" #include "ds4_numerics.cuh" #include #if !defined(STRATA_USE_HIP) #include #endif #include #include #include #include #include #include #include namespace strata::ds4::gguf { namespace { std::atomic g_errors{0}; void check(const char* what) { const cudaError_t e = cudaGetLastError(); if (e != cudaSuccess) { std::fprintf(stderr, "ds4 gguf %s: %s\n", what, cudaGetErrorString(e)); g_errors.fetch_add(1, std::memory_order_relaxed); } } void fail(const char* what) { std::fprintf(stderr, "ds4 gguf %s\n", what); g_errors.fetch_add(1, std::memory_order_relaxed); } constexpr int kWarps = 8, kThreads = kWarps * 32; constexpr int kMaxEnt = 16; // a group's entries in one pass (a window's tokens at most) __device__ __forceinline__ float warp_sum(float v) { #pragma unroll for (int o = 16; o > 0; o >>= 1) v += __shfl_xor_sync(0xffffffffu, v, o); return v; } // ---------------------------------------------------------------- BF16 matrices struct BfJob { const void* w; size_t ld; // values a row (BF16), bytes a row (MXFP4) const float* x; size_t xcs; float* y; size_t ycs; const float* bias; float alpha; int k, n, ncol; const int8_t* xq; // (Q8_0: the activations rounded to int8, q8_quant's) const float* xd; }; struct BfJobs { BfJob j[x3::kMaxMvJobs]; }; // grid (max rows / 8, jobs): a warp a row, each of its activations' dot in F32 - a lane 8 BF16 weights (16 bytes) and // 8 activations a step (VEC), or one of each (rows or activations not 16-byte aligned) template __global__ void __launch_bounds__(kThreads) bf16_mv_kernel(const BfJobs J) { const BfJob& b = J.j[blockIdx.y]; const int row = blockIdx.x * kWarps + (threadIdx.x >> 5), lane = threadIdx.x & 31; if (row >= b.n) return; const uint16_t* w = (const uint16_t*) b.w + (size_t) row * b.ld; float acc[NC]; #pragma unroll for (int c = 0; c < NC; ++c) acc[c] = 0.0f; if (VEC) { for (int i = lane * 8; i < b.k; i += 256) { const uint4 q = *(const uint4*) (w + i); const float wf[8] = {__uint_as_float(q.x << 16), __uint_as_float(q.x & 0xffff0000u), __uint_as_float(q.y << 16), __uint_as_float(q.y & 0xffff0000u), __uint_as_float(q.z << 16), __uint_as_float(q.z & 0xffff0000u), __uint_as_float(q.w << 16), __uint_as_float(q.w & 0xffff0000u)}; #pragma unroll for (int c = 0; c < NC; ++c) if (c < b.ncol) { const float4* xp = (const float4*) (b.x + c * b.xcs + i); const float4 x0 = __ldg(xp), x1 = __ldg(xp + 1); float a = acc[c]; a = fmaf(wf[0], x0.x, a); a = fmaf(wf[1], x0.y, a); a = fmaf(wf[2], x0.z, a); a = fmaf(wf[3], x0.w, a); a = fmaf(wf[4], x1.x, a); a = fmaf(wf[5], x1.y, a); a = fmaf(wf[6], x1.z, a); a = fmaf(wf[7], x1.w, a); acc[c] = a; } } } else { for (int i = lane; i < b.k; i += 32) { const float wf = __uint_as_float((uint32_t) w[i] << 16); #pragma unroll for (int c = 0; c < NC; ++c) if (c < b.ncol) acc[c] = fmaf(wf, b.x[c * b.xcs + i], acc[c]); } } #pragma unroll for (int c = 0; c < NC; ++c) if (c < b.ncol) { const float v = warp_sum(acc[c]); if (lane == 0) b.y[c * b.ycs + row] = b.alpha * v + (b.bias != nullptr ? b.bias[row] : 0.0f); } } __global__ void q8_rows_kernel(const uint8_t* __restrict__ w, size_t ld, int k, int n, uint16_t* __restrict__ dst, bool bf16) { const size_t total = (size_t) n * (size_t) k; for (size_t i = (size_t) blockIdx.x * blockDim.x + threadIdx.x; i < total; i += (size_t) gridDim.x * blockDim.x) { const size_t r = i / (size_t) k, c = i - r * (size_t) k; const uint8_t* p = w + r * ld + (c / 32) * kQ8Bytes; const float v = __half2float(__ushort_as_half(*(const uint16_t*) p)) * (float) (int8_t) p[2 + c % 32]; dst[i] = bf16 ? strata::ds4::bf16_rn(v) : __half_as_ushort(__float2half_rn(v)); } } __global__ void bf16_rows_kernel(const uint16_t* __restrict__ w, size_t ld, int k, int n, uint16_t* __restrict__ dst, bool bf16) { const size_t total = (size_t) n * (size_t) k; for (size_t i = (size_t) blockIdx.x * blockDim.x + threadIdx.x; i < total; i += (size_t) gridDim.x * blockDim.x) { const size_t r = i / (size_t) k, c = i - r * (size_t) k; const uint16_t b = w[r * ld + c]; dst[i] = bf16 ? b : __half_as_ushort(__float2half_rn(__uint_as_float((uint32_t) b << 16))); } } // ---------------------------------------------------------------- MXFP4 experts __device__ __forceinline__ float e8m0_half(uint32_t e) { // 2^(e - 128), ggml's GGML_E8M0_TO_FP32_HALF return __uint_as_float(e < 2 ? 0x00200000u << e : (e - 1) << 23); } __device__ __forceinline__ void lut_init(float* lut) { if (threadIdx.x < 16) { const float v[8] = {0.0f, 1.0f, 2.0f, 3.0f, 4.0f, 6.0f, 8.0f, 12.0f}; lut[threadIdx.x] = threadIdx.x < 8 ? v[threadIdx.x] : -v[threadIdx.x - 8]; } } // block p's 32 values, exactly (a code times a power of two) __device__ __forceinline__ void mx_block(const uint8_t* __restrict__ p, const float* lut, float* w) { const float d = e8m0_half(p[0]); #pragma unroll for (int j = 0; j < 16; ++j) { const uint32_t q = p[1 + j]; w[j] = lut[q & 15u] * d; w[j + 16] = lut[q >> 4] * d; } } __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); } __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); } // a Q8_0 block's 32 values, exactly (an FP16 scale times an int8; the block is only 2-byte aligned) __device__ __forceinline__ void q8_block(const uint8_t* __restrict__ p, float* w) { const float d = __half2float(__ushort_as_half(*(const uint16_t*) p)); const uint16_t* q = (const uint16_t*) (p + 2); #pragma unroll for (int j = 0; j < 16; ++j) { const uint16_t v = q[j]; w[2 * j] = d * (float) (int8_t) (v & 0xff); w[2 * j + 1] = d * (float) (int8_t) (v >> 8); } } // ---- int8 activations (llama.cpp's MMVQ: its q8_1 blocks' rounding) // rows of k F32 values (row r at x + r ldx; k % 32 == 0) -> q[r][k] int8 and d[r][k / 32] = max|x| / 127 per 32 __global__ void __launch_bounds__(kThreads) q8_quant_kernel(const float* __restrict__ x, size_t ldx, int k, int8_t* __restrict__ q, float* __restrict__ d) { const int r = blockIdx.y, b = blockIdx.x * kWarps + (threadIdx.x >> 5), lane = threadIdx.x & 31; if (b * 32 >= k) return; const float v = x[(size_t) r * ldx + (size_t) b * 32 + lane]; float am = fabsf(v); #pragma unroll for (int o = 16; o > 0; o >>= 1) am = fmaxf(am, __shfl_xor_sync(0xffffffffu, am, o)); const float sc = am / 127.0f; q[(size_t) r * k + (size_t) b * 32 + lane] = (int8_t) (am > 0.0f ? __float2int_rn(v / sc) : 0); if (lane == 0) d[(size_t) r * (k / 32) + b] = sc; } void q8_quant(const float* x, size_t ldx, int rows, int k, int8_t* q, float* d, cudaStream_t s) { const dim3 grid((unsigned) ((k / 32 + kWarps - 1) / kWarps), (unsigned) rows); q8_quant_kernel<<>>(x, ldx, k, q, d); } // Several activations' rounding in one launch (a GEMV call's jobs: wo_a's eight groups, the shared expert's two, ...): item z is // q8_quant's (x, ldx, rows, k, q, d), the same per-element arithmetic, so the bytes are the single launches'. Identical items // (the same x, stride, rows and k: two matrices reading one input) are rounded once and share the result. constexpr int kQuantItems = x3::kMaxMvJobs; struct QuantItems { const float* x[kQuantItems]; size_t ldx[kQuantItems]; int k[kQuantItems], rows[kQuantItems]; int8_t* q[kQuantItems]; float* d[kQuantItems]; int n; }; __global__ void __launch_bounds__(kThreads) q8_quant_multi_kernel(QuantItems it) { const int z = blockIdx.z; if (z >= it.n) return; const int r = blockIdx.y, b = blockIdx.x * kWarps + (threadIdx.x >> 5), lane = threadIdx.x & 31; const int k = it.k[z]; if (r >= it.rows[z] || b * 32 >= k) return; const float v = it.x[z][(size_t) r * it.ldx[z] + (size_t) b * 32 + lane]; float am = fabsf(v); #pragma unroll for (int o = 16; o > 0; o >>= 1) am = fmaxf(am, __shfl_xor_sync(0xffffffffu, am, o)); const float sc = am / 127.0f; it.q[z][(size_t) r * k + (size_t) b * 32 + lane] = (int8_t) (am > 0.0f ? __float2int_rn(v / sc) : 0); if (lane == 0) it.d[z][(size_t) r * (k / 32) + b] = sc; } // adds an item unless an identical one is queued; returns the item whose result to read (its q and d) int quant_add(QuantItems& it, const float* x, size_t ldx, int rows, int k, int8_t* q, float* d) { static const bool nodedup = std::getenv("DS4_QUANT_NODEDUP") != nullptr; for (int i = 0; i < it.n && !nodedup; ++i) if (it.x[i] == x && it.ldx[i] == ldx && it.rows[i] == rows && it.k[i] == k) return i; const int i = it.n++; it.x[i] = x; it.ldx[i] = ldx; it.rows[i] = rows; it.k[i] = k; it.q[i] = q; it.d[i] = d; return i; } void quant_flush(const QuantItems& it, cudaStream_t s) { if (it.n <= 0) return; static const bool single = std::getenv("DS4_QUANT_SINGLE") != nullptr; if (single) { for (int i = 0; i < it.n; ++i) q8_quant(it.x[i], it.ldx[i], it.rows[i], it.k[i], it.q[i], it.d[i], s); return; } int kmax = 0, rmax = 0; for (int i = 0; i < it.n; ++i) { kmax = std::max(kmax, it.k[i]); rmax = std::max(rmax, it.rows[i]); } const dim3 grid((unsigned) ((kmax / 32 + kWarps - 1) / kWarps), (unsigned) rmax, (unsigned) it.n); q8_quant_multi_kernel<<>>(it); } // 8 E2M1 codes (a word of 4 code bytes) -> their values as int8: .x the low nibbles' (values 0..3 of the word's bytes), // .y the high nibbles' - llama.cpp's get_int_from_table_16 (MIT), the table kvalues_mxfp4 __device__ __forceinline__ int2 e2m1_x8(uint32_t q4) { const uint32_t t0 = 0x03020100u, t1 = 0x0C080604u, t2 = 0xFDFEFF00u, t3 = 0xF4F8FAFCu; const uint32_t sel = 0x32103210u | ((q4 & 0x88888888u) >> 1); uint32_t tmp[2]; #pragma unroll for (int i = 0; i < 2; ++i) { const uint32_t idx = (q4 >> (16 * i)) & 0x7777u; const uint32_t lo = __byte_perm(t0, t1, idx), hi = __byte_perm(t2, t3, idx); tmp[i] = __byte_perm(lo, hi, sel >> (16 * i)); } return make_int2((int) __byte_perm(tmp[0], tmp[1], 0x6420), (int) __byte_perm(tmp[0], tmp[1], 0x7531)); } // an MXFP4 block's codes (4 words) against 32 int8 activations (xa: values 0..15, xb: 16..31) __device__ __forceinline__ int mx_dot(const uint4& q, const int4& xa, const int4& xb) { int sum = 0; int2 v = e2m1_x8(q.x); sum = __dp4a(v.x, xa.x, sum); sum = __dp4a(v.y, xb.x, sum); v = e2m1_x8(q.y); sum = __dp4a(v.x, xa.y, sum); sum = __dp4a(v.y, xb.y, sum); v = e2m1_x8(q.z); sum = __dp4a(v.x, xa.z, sum); sum = __dp4a(v.y, xb.z, sum); v = e2m1_x8(q.w); sum = __dp4a(v.x, xa.w, sum); sum = __dp4a(v.y, xb.w, sum); return sum; } // the warp copies the blocks [b0, b0 + nbk) of a row (BB bytes each; the row 4-byte aligned, nbk BB % 4 == 0) into its // stage in 4-byte words; every lane calls it template __device__ __forceinline__ void stage_chunk(const uint8_t* row, int b0, int nbk, uint32_t* st) { const uint32_t* src = (const uint32_t*) (row + (size_t) b0 * BB); __syncwarp(); // (the lanes are done with the chunk before) for (int i = threadIdx.x & 31; i < nbk * BB / 4; i += 32) st[i] = src[i]; __syncwarp(); } // a row's next chunk of 32 blocks (BB bytes each) in the lanes' registers, then into the warp's stage template struct Fetch { static constexpr int W = (32 * BB / 4 + 31) / 32; // words a lane uint32_t r[W]; int n = 0; __device__ __forceinline__ void load(const uint8_t* row, int b0, int nbk) { const uint32_t* src = (const uint32_t*) (row + (size_t) b0 * BB); n = nbk * BB / 4; #pragma unroll for (int j = 0; j < W; ++j) { const int i = (int) (threadIdx.x & 31) + 32 * j; r[j] = i < n ? src[i] : 0u; } } __device__ __forceinline__ void store(uint32_t* st) const { __syncwarp(); // (the lanes are done with the chunk before) #pragma unroll for (int j = 0; j < W; ++j) { const int i = (int) (threadIdx.x & 31) + 32 * j; if (i < n) st[i] = r[j]; } __syncwarp(); } }; // MXFP4 block i of a stage (17-byte blocks from a 4-byte aligned start): its scale byte, its 16 code bytes as 4 words __device__ __forceinline__ uint32_t mx_words_s(const uint32_t* st, int i, uint4& q) { const int o = 17 * i, w = o >> 2, sh = (o & 3) * 8; const uint32_t a0 = st[w], a1 = st[w + 1], a2 = st[w + 2], a3 = st[w + 3], a4 = st[w + 4]; const uint32_t s0 = __funnelshift_r(a0, a1, sh), s1 = __funnelshift_r(a1, a2, sh), s2 = __funnelshift_r(a2, a3, sh), s3 = __funnelshift_r(a3, a4, sh), s4 = a4 >> sh; q.x = __funnelshift_r(s0, s1, 8); q.y = __funnelshift_r(s1, s2, 8); q.z = __funnelshift_r(s2, s3, 8); q.w = __funnelshift_r(s3, s4, 8); return s0 & 0xffu; } // ... the same from global memory, byte by byte (rows not 4-byte aligned) __device__ __forceinline__ uint32_t mx_words_g(const uint8_t* __restrict__ p, uint4& q) { uint32_t w[4]; #pragma unroll for (int j = 0; j < 4; ++j) w[j] = (uint32_t) p[1 + 4 * j] | (uint32_t) p[2 + 4 * j] << 8 | (uint32_t) p[3 + 4 * j] << 16 | (uint32_t) p[4 + 4 * j] << 24; q = make_uint4(w[0], w[1], w[2], w[3]); return p[0]; } // Q8_0 block i of a stage (34-byte blocks): its scale, its 32 int8 as 8 words __device__ __forceinline__ float q8_words_s(const uint32_t* st, int i, int* v) { const int o = 34 * i, w = o >> 2, sh = (o & 3) * 8; uint32_t a[9]; #pragma unroll for (int j = 0; j < 9; ++j) a[j] = st[w + j]; uint32_t sw[9]; #pragma unroll for (int j = 0; j < 8; ++j) sw[j] = __funnelshift_r(a[j], a[j + 1], sh); sw[8] = a[8] >> sh; #pragma unroll for (int j = 0; j < 8; ++j) v[j] = (int) __funnelshift_r(sw[j], sw[j + 1], 16); return __half2float(__ushort_as_half((uint16_t) (sw[0] & 0xffffu))); } __device__ __forceinline__ float q8_words_g(const uint8_t* __restrict__ p, int* v) { #pragma unroll for (int j = 0; j < 8; ++j) v[j] = (int) ((uint32_t) p[2 + 4 * j] | (uint32_t) p[3 + 4 * j] << 8 | (uint32_t) p[4 + 4 * j] << 16 | (uint32_t) p[5 + 4 * j] << 24); return __half2float(__ushort_as_half((uint16_t) (p[0] | p[1] << 8))); } // grid (max rows / 8, jobs): a Q8_0 matrix's rows against the jobs' int8 activations (q8_quant's: activation c of job // j at xq + c k, its scales at xd + c k / 32) - a warp a row, a lane a block a step, the blocks staged (ST) or read template __global__ void __launch_bounds__(kThreads) q8_mv_kernel(const BfJobs J) { __shared__ __align__(16) uint32_t stage[kWarps][32 * kQ8Bytes / 4]; const BfJob& b = J.j[blockIdx.y]; const int row = blockIdx.x * kWarps + (threadIdx.x >> 5), lane = threadIdx.x & 31; if (row >= b.n) return; const uint8_t* w = (const uint8_t*) b.w + (size_t) row * b.ld; const int nb = b.k / 32; float acc[NC]; #pragma unroll for (int c = 0; c < NC; ++c) acc[c] = 0.0f; Fetch F; if (ST) F.load(w, 0, min(32, nb)); for (int b0 = 0; b0 < nb; b0 += 32) { const int nbk = min(32, nb - b0), kb = b0 + lane; if (ST) { F.store(stage[threadIdx.x >> 5]); if (b0 + 32 < nb) F.load(w, b0 + 32, min(32, nb - b0 - 32)); } if (lane >= nbk) continue; int v[8]; const float dw = ST ? q8_words_s(stage[threadIdx.x >> 5], lane, v) : q8_words_g(w + (size_t) kb * kQ8Bytes, v); #pragma unroll for (int c = 0; c < NC; ++c) if (c < b.ncol) { const int4* xp = (const int4*) (b.xq + (size_t) c * b.k + (size_t) kb * 32); const int4 xa = xp[0], xb = xp[1]; int sum = __dp4a(v[0], xa.x, 0); sum = __dp4a(v[1], xa.y, sum); sum = __dp4a(v[2], xa.z, sum); sum = __dp4a(v[3], xa.w, sum); sum = __dp4a(v[4], xb.x, sum); sum = __dp4a(v[5], xb.y, sum); sum = __dp4a(v[6], xb.z, sum); sum = __dp4a(v[7], xb.w, sum); acc[c] = fmaf(dw * b.xd[(size_t) c * nb + kb], (float) sum, acc[c]); } } #pragma unroll for (int c = 0; c < NC; ++c) if (c < b.ncol) { const float v = warp_sum(acc[c]); if (lane == 0) b.y[c * b.ycs + row] = b.alpha * v + (b.bias != nullptr ? b.bias[row] : 0.0f); } } // grid (max rows / 8, jobs): bf16_mv_kernel for MXFP4 (Q8 false) or Q8_0 (Q8 true) matrices - a lane a 32-value block // a step, dequantised once for the job's activations template __global__ void __launch_bounds__(kThreads) mx_mv_kernel(const BfJobs J) { __shared__ float lut[16]; lut_init(lut); __syncthreads(); const BfJob& b = J.j[blockIdx.y]; const int row = blockIdx.x * kWarps + (threadIdx.x >> 5), lane = threadIdx.x & 31; if (row >= b.n) return; const uint8_t* w = (const uint8_t*) b.w + (size_t) row * b.ld; float acc[NC]; #pragma unroll for (int c = 0; c < NC; ++c) acc[c] = 0.0f; for (int kb = lane; kb < b.k / kMxBlock; kb += 32) { float wv[32]; if (Q8) q8_block(w + (size_t) kb * kQ8Bytes, wv); else mx_block(w + (size_t) kb * kMxBytes, lut, wv); #pragma unroll for (int c = 0; c < NC; ++c) if (c < b.ncol) { const float* x = b.x + c * b.xcs + kb * kMxBlock; float a = acc[c]; if (VEC) { #pragma unroll for (int q = 0; q < 8; ++q) { const float4 xv = __ldg((const float4*) x + q); a = fmaf(wv[4 * q], xv.x, a); a = fmaf(wv[4 * q + 1], xv.y, a); a = fmaf(wv[4 * q + 2], xv.z, a); a = fmaf(wv[4 * q + 3], xv.w, a); } } else { #pragma unroll for (int j = 0; j < 32; ++j) a = fmaf(wv[j], x[j], a); } acc[c] = a; } } #pragma unroll for (int c = 0; c < NC; ++c) if (c < b.ncol) { const float v = warp_sum(acc[c]); if (lane == 0) b.y[c * b.ycs + row] = b.alpha * v + (b.bias != nullptr ? b.bias[row] : 0.0f); } } // a token's experts (grp_ptr null: group i = expert i of the plan, plan_ptr[i] its blob or 0 off the device, its one // entry i, every entry's input x) or a window's groups (group g: one expert, blob grp_ptr[g], entries e = t * 8 + i at // grp_ent[grp_start[g] ..), entry e's input x + (e >> 3) x_stride) struct MxArgs { const unsigned long long* plan_ptr; const unsigned long long* grp_ptr; const int *grp_start, *grp_ent, *n_groups; const float* x; size_t x_stride; float* h; // [entries][n_ff] float* part; // [entries][n_embd] size_t tr[3]; int n_embd, n_ff; float limit; const int8_t* xq; // x rounded to int8 (a token's: row 0; a window's: row t), q8_quant's rows of n_embd const float* xd; const int8_t* hq; // h rounded to int8, row e = the entry, rows of n_ff const float* hd; }; __device__ __forceinline__ bool mx_group(const MxArgs& a, int grp, const uint8_t*& blob, int& m0, int& m1) { if (a.grp_ptr == nullptr) { const unsigned long long p = a.plan_ptr[grp]; if (p == 0ull) return false; blob = (const uint8_t*) p; m0 = grp; m1 = grp + 1; return true; } if (grp >= *a.n_groups) return false; blob = (const uint8_t*) a.grp_ptr[grp]; m0 = a.grp_start[grp]; m1 = a.grp_start[grp + 1]; return true; } // grid (n_ff / 8, groups): a warp a row of the gate and of the up, for NE of the group's entries a pass (each weight // block decoded once for them) against the entries' int8 x -> h = the clamped swiglu template __global__ void __launch_bounds__(kThreads) mx_gate_up_kernel(const MxArgs a) { __shared__ __align__(16) uint32_t stage[kWarps][2][32 * kMxBytes / 4]; __shared__ int es[NE], xr[NE]; const uint8_t* blob; int m0, m1; if (!mx_group(a, blockIdx.y, blob, m0, m1)) return; // (uniform) const int row = blockIdx.x * kWarps + (threadIdx.x >> 5), lane = threadIdx.x & 31, wp = threadIdx.x >> 5; const size_t rb = mx_row_bytes(a.n_embd); const uint8_t* g = blob + a.tr[0] + (size_t) row * rb; const uint8_t* u = blob + a.tr[1] + (size_t) row * rb; const int nb = a.n_embd / kMxBlock; for (int mm = m0; mm < m1; mm += NE) { const int ne = min(NE, m1 - mm); __syncthreads(); if ((int) threadIdx.x < ne) { const int e = a.grp_ptr != nullptr ? a.grp_ent[mm + threadIdx.x] : mm + threadIdx.x; es[threadIdx.x] = e; xr[threadIdx.x] = a.grp_ptr != nullptr ? e >> 3 : 0; } __syncthreads(); if (row >= a.n_ff) continue; float ag[NE], au[NE]; #pragma unroll for (int i = 0; i < NE; ++i) ag[i] = au[i] = 0.0f; Fetch FG, FU; if (ST) { FG.load(g, 0, min(32, nb)); FU.load(u, 0, min(32, nb)); } for (int b0 = 0; b0 < nb; b0 += 32) { const int nbk = min(32, nb - b0), b = b0 + lane; if (ST) { FG.store(stage[wp][0]); FU.store(stage[wp][1]); if (b0 + 32 < nb) { FG.load(g, b0 + 32, min(32, nb - b0 - 32)); FU.load(u, b0 + 32, min(32, nb - b0 - 32)); } } if (lane >= nbk) continue; uint4 qg, qu; const uint32_t eg = ST ? mx_words_s(stage[wp][0], lane, qg) : mx_words_g(g + (size_t) b * kMxBytes, qg); const uint32_t eu = ST ? mx_words_s(stage[wp][1], lane, qu) : mx_words_g(u + (size_t) b * kMxBytes, qu); const float dg = e8m0_half(eg), du = e8m0_half(eu); #pragma unroll for (int i = 0; i < NE; ++i) if (i < ne) { const int4* xp = (const int4*) (a.xq + (size_t) xr[i] * a.n_embd + (size_t) b * kMxBlock); const int4 xa = xp[0], xb = xp[1]; const float dx = a.xd[(size_t) xr[i] * nb + b]; ag[i] = fmaf(dg * dx, (float) mx_dot(qg, xa, xb), ag[i]); au[i] = fmaf(du * dx, (float) mx_dot(qu, xa, xb), au[i]); } } #pragma unroll for (int i = 0; i < NE; ++i) if (i < ne) { const float gv = warp_sum(ag[i]), uv = warp_sum(au[i]); if (lane == 0) a.h[(size_t) es[i] * a.n_ff + row] = swiglu(gv, uv, a.limit); } } } // grid (n_embd / 8, groups): a warp a row of the down for NE entries a pass, against their int8 h -> part template __global__ void __launch_bounds__(kThreads) mx_down_kernel(const MxArgs a) { __shared__ __align__(16) uint32_t stage[kWarps][32 * kMxBytes / 4]; __shared__ int es[NE]; const uint8_t* blob; int m0, m1; if (!mx_group(a, blockIdx.y, blob, m0, m1)) return; const int row = blockIdx.x * kWarps + (threadIdx.x >> 5), lane = threadIdx.x & 31, wp = threadIdx.x >> 5; const uint8_t* d = blob + a.tr[2] + (size_t) row * mx_row_bytes(a.n_ff); const int nb = a.n_ff / kMxBlock; for (int mm = m0; mm < m1; mm += NE) { const int ne = min(NE, m1 - mm); __syncthreads(); if ((int) threadIdx.x < ne) es[threadIdx.x] = a.grp_ptr != nullptr ? a.grp_ent[mm + threadIdx.x] : mm + threadIdx.x; __syncthreads(); if (row >= a.n_embd) continue; float ad[NE]; #pragma unroll for (int i = 0; i < NE; ++i) ad[i] = 0.0f; Fetch FD; if (ST) FD.load(d, 0, min(32, nb)); for (int b0 = 0; b0 < nb; b0 += 32) { const int nbk = min(32, nb - b0), b = b0 + lane; if (ST) { FD.store(stage[wp]); if (b0 + 32 < nb) FD.load(d, b0 + 32, min(32, nb - b0 - 32)); } if (lane >= nbk) continue; uint4 q; const float dd = e8m0_half(ST ? mx_words_s(stage[wp], lane, q) : mx_words_g(d + (size_t) b * kMxBytes, q)); #pragma unroll for (int i = 0; i < NE; ++i) if (i < ne) { const int4* hp = (const int4*) (a.hq + (size_t) es[i] * a.n_ff + (size_t) b * kMxBlock); const int4 ha = hp[0], hb = hp[1]; ad[i] = fmaf(dd * a.hd[(size_t) es[i] * nb + b], (float) mx_dot(q, ha, hb), ad[i]); } } #pragma unroll for (int i = 0; i < NE; ++i) if (i < ne) { const float v = warp_sum(ad[i]); if (lane == 0) a.part[(size_t) es[i] * a.n_embd + row] = v; } } } struct CombArgs { const unsigned long long* plan_ptr; // [T][8] const float* plan_w; // [T][8] const float* part; // [T * 8][n_embd] const float* cpu_part; size_t cpu_stride; const int* cpu_flag; const float* sh_out; // [T][n_embd] float* out; size_t out_stride; int k, n_embd; }; // grid (n_embd / 128, T): each token's outputs - the experts in plan order (an expert off the device adds 0), then // the CPU lane's part and the shared expert's (x3's combine) __global__ void __launch_bounds__(128) mx_combine_kernel(const CombArgs a) { const int t = blockIdx.y, r = blockIdx.x * 128 + threadIdx.x; if (r >= a.n_embd) return; 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 != nullptr && a.cpu_flag[t] ? a.cpu_part + (size_t) t * a.cpu_stride + r : nullptr, a.sh_out != nullptr ? a.sh_out[(size_t) t * a.n_embd + r] : 0.0f); } __global__ void mx_wait_kernel(const unsigned* p, unsigned v) { if (threadIdx.x != 0) return; while (*(volatile const unsigned*) p != v) { #if defined(__CUDA_ARCH__) && __CUDA_ARCH__ >= 700 __nanosleep(200); #endif } __threadfence_system(); } // ---------------------------------------------------------------- the prompt path's expert sets // a set's expert i (blob wbase + i stride) owns the sorted rows [bounds[i], bounds[i + 1]); local row = sorted row - // row0. A block: BM of its rows x BN outputs, the weights dequantised a 32-value block at a time into shared memory, // F32 FMAs (each thread TM x TN outputs) constexpr int BM = 64, BN = 64, TM = 4, TN = 4; static_assert((BM / TM) * (BN / TN) == kThreads, "one output tile a thread"); struct SetK { const uint8_t* wbase; size_t stride; size_t tr[3]; const int* bounds; int row0; int n_embd, n_ff; float limit; const float* xf; // gate / up: F32 input rows x[idx[local]] (ldx) const int* idx; const __half* xh; // down: F16 rows (ldx) int ldx; __half* h; // gate / up: F16 out float* y; // down: F32 out int ldo; int skip_le; // the experts of at most this many rows left out (the light kernels took them; ds4_iq.cuh) }; // one 32-value block of BN weight rows (rows c0 .., row bytes rb, block kb) -> ws[32][BN + 1] __device__ __forceinline__ void mx_tile(const uint8_t* w, size_t rb, int c0, int kb, const float* lut, float (*ws)[BN + 1]) { const int r = threadIdx.x >> 2, q = threadIdx.x & 3; // row r of the tile, values [8 q, 8 q + 8) const uint8_t* p = w + (size_t) (c0 + r) * rb + (size_t) kb * kMxBytes; const float d = e8m0_half(p[0]); const int b0 = (q & 1) * 8, hi = q >> 1; #pragma unroll for (int j = 0; j < 8; ++j) { const uint32_t c = p[1 + b0 + j]; ws[q * 8 + j][r] = lut[hi ? (c >> 4) : (c & 15u)] * d; } } template __global__ void __launch_bounds__(kThreads) mx_set_kernel(const SetK a) { __shared__ float lut[16]; __shared__ float wg[32][BN + 1]; __shared__ float wu[GATE_UP ? 32 : 1][BN + 1]; __shared__ float xs[BM][33]; __shared__ int src[BM]; lut_init(lut); const int e = blockIdx.z; const int lo = a.bounds[e] - a.row0, hi = a.bounds[e + 1] - a.row0; const int r0 = lo + blockIdx.x * BM; if (r0 >= hi || hi - lo <= a.skip_le) return; // (uniform: before any barrier but lut_init's writes, which nobody // reads then; skip_le: the light kernels' experts) const int nr = min(BM, hi - r0); const int c0 = blockIdx.y * BN; const uint8_t* blob = a.wbase + (size_t) e * a.stride; const int K = GATE_UP ? a.n_embd : a.n_ff; const size_t rb = mx_row_bytes(K); const int tid = (int) threadIdx.x; if (tid < BM) src[tid] = tid < nr ? (GATE_UP ? a.idx[r0 + tid] : r0 + tid) : -1; __syncthreads(); const int ty = threadIdx.x / (BN / TN), tx = threadIdx.x % (BN / TN); float ag[TM][TN], au[TM][TN]; #pragma unroll for (int i = 0; i < TM; ++i) #pragma unroll for (int j = 0; j < TN; ++j) ag[i][j] = au[i][j] = 0.0f; for (int kb = 0; kb < K / kMxBlock; ++kb) { mx_tile(blob + a.tr[GATE_UP ? 0 : 2], rb, c0, kb, lut, wg); if (GATE_UP) mx_tile(blob + a.tr[1], rb, c0, kb, lut, wu); for (int i = threadIdx.x; i < BM * kMxBlock; i += kThreads) { // the rows' 32 inputs const int r = i / kMxBlock, c = i % kMxBlock, sr = src[r]; float v = 0.0f; if (sr >= 0) { const size_t at = (size_t) sr * a.ldx + (size_t) kb * kMxBlock + c; v = GATE_UP ? a.xf[at] : __half2float(a.xh[at]); } xs[r][c] = v; } __syncthreads(); #pragma unroll 8 for (int kk = 0; kk < kMxBlock; ++kk) { float xv[TM], gw[TN], uw[TN]; #pragma unroll for (int i = 0; i < TM; ++i) xv[i] = xs[ty * TM + i][kk]; #pragma unroll for (int j = 0; j < TN; ++j) { gw[j] = wg[kk][tx * TN + j]; if (GATE_UP) uw[j] = wu[kk][tx * TN + j]; } #pragma unroll for (int i = 0; i < TM; ++i) #pragma unroll for (int j = 0; j < TN; ++j) { ag[i][j] = fmaf(xv[i], gw[j], ag[i][j]); if (GATE_UP) au[i][j] = fmaf(xv[i], uw[j], au[i][j]); } } __syncthreads(); } #pragma unroll for (int i = 0; i < TM; ++i) { const int r = ty * TM + i; if (r >= nr) continue; const size_t lr = (size_t) (r0 + r); #pragma unroll for (int j = 0; j < TN; ++j) { const int c = c0 + tx * TN + j; if (GATE_UP) a.h[lr * a.ldo + c] = __float2half_rn(swiglu(ag[i][j], au[i][j], a.limit)); else a.y[lr * a.ldo + c] = ag[i][j]; } } } // the same on the tensor cores: a block BM rows x BN outputs, 8 warps each 16 rows x 32 outputs (2 fragments, gate and // up: 4), a 32-value MXFP4 block of K a step (2 WMMA k-steps), the tiles in shared memory as F16 #if !defined(STRATA_USE_HIP) namespace wm = nvcuda::wmma; constexpr int kLdT = 40; // halves a tile row (32 + 8: WMMA wants a multiple of 8; the pad spreads the banks) template __global__ void __launch_bounds__(kThreads) mx_set_tc_kernel(const SetK a) { struct Tiles { __half x[BM][kLdT], wg[BN][kLdT], wu[GATE_UP ? BN : 1][kLdT]; }; struct Out { float g[BM][BN + 4], u[GATE_UP ? BM : 1][BN + 4]; }; __shared__ __align__(32) unsigned char smem[sizeof(Tiles) > sizeof(Out) ? sizeof(Tiles) : sizeof(Out)]; __shared__ float lut[16]; __shared__ int src[BM]; Tiles& T = *reinterpret_cast(smem); Out& O = *reinterpret_cast(smem); lut_init(lut); const int e = blockIdx.z; const int lo = a.bounds[e] - a.row0, hi = a.bounds[e + 1] - a.row0; const int r0 = lo + blockIdx.x * BM; if (r0 >= hi || hi - lo <= a.skip_le) return; // (uniform; skip_le: the light kernels' experts) const int nr = min(BM, hi - r0); const int c0 = blockIdx.y * BN; const uint8_t* blob = a.wbase + (size_t) e * a.stride; const int K = GATE_UP ? a.n_embd : a.n_ff; const size_t rb = mx_row_bytes(K); const int tid = (int) threadIdx.x, warp = tid >> 5; if (tid < BM) src[tid] = tid < nr ? (GATE_UP ? a.idx[r0 + tid] : r0 + tid) : -1; __syncthreads(); const int wm_ = warp >> 1, wn = (warp & 1) * 2; // the warp's 16 rows, its 2 x 16 outputs wm::fragment fg[2], fu[2]; for (int j = 0; j < 2; ++j) { wm::fill_fragment(fg[j], 0.0f); wm::fill_fragment(fu[j], 0.0f); } const int tr = tid >> 2, tq = tid & 3; // the thread's tile row, its 8 values [8 tq, 8 tq + 8) for (int kb = 0; kb < K / kMxBlock; ++kb) { { // the weights: row tr of the outputs, values 8 tq .. for (int m = 0; m < (GATE_UP ? 2 : 1); ++m) { const uint8_t* q = blob + a.tr[GATE_UP ? m : 2] + (size_t) (c0 + tr) * rb + (size_t) kb * kMxBytes; const float d = e8m0_half(q[0]); const int b0 = (tq & 1) * 8, hn = tq >> 1; __half* dst = m == 0 ? &T.wg[tr][tq * 8] : &T.wu[tr][tq * 8]; #pragma unroll for (int j = 0; j < 8; ++j) { const uint32_t c = q[1 + b0 + j]; dst[j] = __float2half_rn(lut[hn ? (c >> 4) : (c & 15u)] * d); } } // the inputs: row tr, values 8 tq .. const int sr = src[tr]; #pragma unroll for (int j = 0; j < 8; ++j) { __half v = __float2half_rn(0.0f); if (sr >= 0) { const size_t at = (size_t) sr * a.ldx + (size_t) kb * kMxBlock + tq * 8 + j; v = GATE_UP ? __float2half_rn(a.xf[at]) : a.xh[at]; } T.x[tr][tq * 8 + j] = v; } } __syncthreads(); #pragma unroll for (int kk = 0; kk < 32; kk += 16) { wm::fragment fa; wm::load_matrix_sync(fa, &T.x[wm_ * 16][kk], kLdT); #pragma unroll for (int j = 0; j < 2; ++j) { wm::fragment fb; wm::load_matrix_sync(fb, &T.wg[(wn + j) * 16][kk], kLdT); wm::mma_sync(fg[j], fa, fb, fg[j]); if (GATE_UP) { wm::load_matrix_sync(fb, &T.wu[(wn + j) * 16][kk], kLdT); wm::mma_sync(fu[j], fa, fb, fu[j]); } } } __syncthreads(); } for (int j = 0; j < 2; ++j) { wm::store_matrix_sync(&O.g[wm_ * 16][(wn + j) * 16], fg[j], BN + 4, wm::mem_row_major); if (GATE_UP) wm::store_matrix_sync(&O.u[wm_ * 16][(wn + j) * 16], fu[j], BN + 4, wm::mem_row_major); } __syncthreads(); for (int i = tid; i < BM * BN; i += kThreads) { const int r = i / BN, c = i % BN; if (r >= nr) continue; const size_t lr = (size_t) (r0 + r); if (GATE_UP) a.h[lr * a.ldo + c0 + c] = __float2half_rn(swiglu(O.g[r][c], O.u[r][c], a.limit)); else a.y[lr * a.ldo + c0 + c] = O.g[r][c]; } } #endif #if defined(STRATA_USE_HIP) // the same on an AMD wave32 GPU's WMMA (RDNA3 / RDNA3.5 gfx11, RDNA4 gfx12): 8 waves each 16 rows x 32 outputs, the // tiles in shared memory as F16, F32 accumulation - each lane gives its row / column's 16 values of a k-step (gfx12: // its half of them), and holds 8 of the 16 x 16 results (rows 2 i + hi on gfx11, 8 hi + i on gfx12; column lane % 16) using h16v = _Float16 __attribute__((ext_vector_type(16))); using f8v = float __attribute__((ext_vector_type(8))); __device__ __forceinline__ f8v set_wmma(h16v a, h16v b, f8v c) { #if defined(__gfx1100__) || defined(__gfx1101__) || defined(__gfx1102__) || defined(__gfx1103__) || \ defined(__gfx1150__) || defined(__gfx1151__) || defined(__gfx1152__) return __builtin_amdgcn_wmma_f32_16x16x16_f16_w32(a, b, c); #elif defined(__gfx1200__) || defined(__gfx1201__) using h8 = _Float16 __attribute__((ext_vector_type(8))); const int k0 = (threadIdx.x & 31) / 16 * 8; h8 aa, bb; #pragma unroll for (int i = 0; i < 8; ++i) { aa[i] = a[k0 + i]; bb[i] = b[k0 + i]; } return __builtin_amdgcn_wmma_f32_16x16x16_f16_w32_gfx12(aa, bb, c); #else __builtin_trap(); return c; #endif } __device__ __forceinline__ int set_wmma_row(int i, int hi) { #if defined(__gfx1200__) || defined(__gfx1201__) return 8 * hi + i; #else return 2 * i + hi; #endif } constexpr int kLdH = 40; // halves a tile row (32 + 8: the pad spreads the banks) template __global__ void __launch_bounds__(kThreads) mx_set_wmma_kernel(const SetK a) { struct Tiles { _Float16 x[BM][kLdH], wg[BN][kLdH], wu[GATE_UP ? BN : 1][kLdH]; }; struct Out { float g[BM][BN + 4], u[GATE_UP ? BM : 1][BN + 4]; }; __shared__ __align__(16) unsigned char smem[sizeof(Tiles) > sizeof(Out) ? sizeof(Tiles) : sizeof(Out)]; __shared__ float lut[16]; __shared__ int src[BM]; Tiles& T = *reinterpret_cast(smem); Out& O = *reinterpret_cast(smem); lut_init(lut); const int e = blockIdx.z; const int lo = a.bounds[e] - a.row0, hi_r = a.bounds[e + 1] - a.row0; const int r0 = lo + blockIdx.x * BM; if (r0 >= hi_r || hi_r - lo <= a.skip_le) return; // (uniform; skip_le: the light kernels' experts) const int nr = min(BM, hi_r - r0); const int c0 = blockIdx.y * BN; const uint8_t* blob = a.wbase + (size_t) e * a.stride; const int K = GATE_UP ? a.n_embd : a.n_ff; const size_t rb = mx_row_bytes(K); const int tid = (int) threadIdx.x, warp = tid >> 5, lane = tid & 31, l16 = lane & 15, hi = lane >> 4; if (tid < BM) src[tid] = tid < nr ? (GATE_UP ? a.idx[r0 + tid] : r0 + tid) : -1; __syncthreads(); const int wm_ = warp >> 1, wn = (warp & 1) * 2; // the wave's 16 rows, its 2 x 16 outputs f8v fg[2], fu[2]; #pragma unroll for (int j = 0; j < 2; ++j) #pragma unroll for (int i = 0; i < 8; ++i) fg[j][i] = fu[j][i] = 0.0f; const int tr = tid >> 2, tq = tid & 3; // the thread's tile row, its 8 values [8 tq, 8 tq + 8) for (int kb = 0; kb < K / kMxBlock; ++kb) { for (int m = 0; m < (GATE_UP ? 2 : 1); ++m) { const uint8_t* q = blob + a.tr[GATE_UP ? m : 2] + (size_t) (c0 + tr) * rb + (size_t) kb * kMxBytes; const float d = e8m0_half(q[0]); const int b0 = (tq & 1) * 8, hn = tq >> 1; _Float16* dst = m == 0 ? &T.wg[tr][tq * 8] : &T.wu[tr][tq * 8]; #pragma unroll for (int j = 0; j < 8; ++j) { const uint32_t cc = q[1 + b0 + j]; dst[j] = (_Float16) (lut[hn ? (cc >> 4) : (cc & 15u)] * d); } } const int sr = src[tr]; #pragma unroll for (int j = 0; j < 8; ++j) { float v = 0.0f; if (sr >= 0) { const size_t at = (size_t) sr * a.ldx + (size_t) kb * kMxBlock + tq * 8 + j; v = GATE_UP ? a.xf[at] : __half2float(a.xh[at]); } T.x[tr][tq * 8 + j] = (_Float16) v; } __syncthreads(); #pragma unroll for (int kk = 0; kk < 32; kk += 16) { h16v av; #pragma unroll for (int i = 0; i < 16; ++i) av[i] = T.x[wm_ * 16 + l16][kk + i]; #pragma unroll for (int j = 0; j < 2; ++j) { h16v bv; #pragma unroll for (int i = 0; i < 16; ++i) bv[i] = T.wg[(wn + j) * 16 + l16][kk + i]; fg[j] = set_wmma(av, bv, fg[j]); if (GATE_UP) { #pragma unroll for (int i = 0; i < 16; ++i) bv[i] = T.wu[(wn + j) * 16 + l16][kk + i]; fu[j] = set_wmma(av, bv, fu[j]); } } } __syncthreads(); } #pragma unroll for (int j = 0; j < 2; ++j) #pragma unroll for (int i = 0; i < 8; ++i) { const int r = wm_ * 16 + set_wmma_row(i, hi), cc = (wn + j) * 16 + l16; O.g[r][cc] = fg[j][i]; if (GATE_UP) O.u[r][cc] = fu[j][i]; } __syncthreads(); for (int i = tid; i < BM * BN; i += kThreads) { const int r = i / BN, cc = i % BN; if (r >= nr) continue; const size_t lr = (size_t) (r0 + r); if (GATE_UP) a.h[lr * a.ldo + c0 + cc] = __float2half_rn(swiglu(O.g[r][cc], O.u[r][cc], a.limit)); else a.y[lr * a.ldo + c0 + cc] = O.g[r][cc]; } } #endif bool tc_ok() { // sm_70 and later (WMMA F16) / AMD gfx11, gfx12 (wave32 WMMA); STRATA_DS4_NO_TC=1 keeps the F32 kernel #if defined(STRATA_USE_HIP) static const bool ok = [] { int dev = 0; cudaGetDevice(&dev); cudaDeviceProp prop{}; if (cudaGetDeviceProperties(&prop, dev) != cudaSuccess) return false; const char* v = std::getenv("STRATA_DS4_NO_TC"); if (v != nullptr && v[0] == '1') return false; const char* arch = prop.gcnArchName; for (const char* p : {"gfx1100", "gfx1101", "gfx1102", "gfx1103", "gfx1150", "gfx1151", "gfx1152", "gfx1200", "gfx1201"}) if (std::strncmp(arch, p, 7) == 0) return true; return false; }(); return ok; #else static const bool ok = [] { int dev = 0, major = 0; cudaGetDevice(&dev); cudaDeviceGetAttribute(&major, cudaDevAttrComputeCapabilityMajor, dev); const char* v = std::getenv("STRATA_DS4_NO_TC"); return major >= 7 && !(v != nullptr && v[0] == '1'); }(); return ok; #endif } // ---------------------------------------------------------------- scratch constexpr int kMaxEmbd = 8192; constexpr int kMaxQK = 32768; // a Q8_0 GEMV's inputs at most struct Scratch { float* part = nullptr; // [8][kMaxEmbd]: a token's experts' down outputs int8_t *xq = nullptr, *hq = nullptr, *mq = nullptr; // int8 rows: x [kMaxEnt], h [kMaxEnt * 8], the GEMVs' float *xd = nullptr, *hd = nullptr, *md = nullptr; // [kMaxMvJobs * kMaxMvCols] inputs; their scales }; std::mutex g_mu; std::unordered_map g_scratch; Scratch* scratch(cudaStream_t s) { std::lock_guard lk(g_mu); auto it = g_scratch.find(s); if (it != g_scratch.end()) return &it->second; Scratch sc; const size_t nx = (size_t) kMaxEnt * kMaxEmbd, nh = (size_t) kMaxEnt * 8 * kMaxEmbd, nm = (size_t) x3::kMaxMvJobs * x3::kMaxMvCols * kMaxQK; if (cudaMalloc(&sc.part, (size_t) 8 * kMaxEmbd * sizeof(float)) != cudaSuccess || cudaMalloc(&sc.xq, nx) != cudaSuccess || cudaMalloc(&sc.xd, nx / 32 * 4) != cudaSuccess || cudaMalloc(&sc.hq, nh) != cudaSuccess || cudaMalloc(&sc.hd, nh / 32 * 4) != cudaSuccess || cudaMalloc(&sc.mq, nm) != cudaSuccess || cudaMalloc(&sc.md, nm / 32 * 4) != cudaSuccess) { fail("scratch: cudaMalloc failed"); return nullptr; } return &g_scratch.emplace(s, sc).first->second; } bool mx_geometry(const char* who, int k, int n_embd, int n_ff) { if (k < 1 || k > 8 || n_embd % BN != 0 || n_ff % BN != 0 || n_embd > kMaxEmbd || n_ff % kMxBlock != 0) { std::fprintf(stderr, "ds4 gguf %s: geometry not covered (k %d n_embd %d n_ff %d)\n", who, k, n_embd, n_ff); g_errors.fetch_add(1, std::memory_order_relaxed); return false; } return true; } } // namespace // ---------------------------------------------------------------- entry points bool prepare(cudaStream_t s) { return scratch(s) != nullptr; } int launch_errors() { return g_errors.load(std::memory_order_relaxed); } size_t type_row_bytes(int type, int64_t k) { if (type == kTypeMXFP4) return k % kMxBlock == 0 ? mx_row_bytes(k) : 0; // known(), not supported(): a type with no device kernel still has a layout, and its rows are read by the CPU lane return iq::known(type) && k % iq::block_values(type) == 0 ? iq::row_bytes(type, k) : 0; } int type_block(int type) { return type == kTypeMXFP4 ? kMxBlock : iq::known(type) ? iq::block_values(type) : 0; } const char* type_name(int type) { return type == kTypeMXFP4 ? "MXFP4" : iq::name(type); } bool type_on_device(int type) { return type == kTypeMXFP4 || iq::supported(type); } namespace { size_t row_stride(const x3::Mat& m) { // values (BF16) or bytes (MXFP4, Q8_0, a low-bit type) a row if (m.ld > 0) return (size_t) m.ld; return m.fmt == kFmtMXFP4 ? mx_row_bytes(m.k) : m.fmt == kFmtQ8_0 ? q8_row_bytes(m.k) : m.fmt == kFmtGG ? type_row_bytes(m.K, m.k) : (size_t) m.k; } // a layer's low-bit experts as ds4_iq's arguments (false: its types not covered - printed) bool iq_args(const x3::ExpertLayout& lay, int n_embd, int n_ff, float limit, iq::Args& ia, const char* who) { ia = iq::Args{}; for (int r = 0; r < 3; ++r) ia.tr[r] = lay.trellis[r]; ia.n_embd = n_embd; ia.n_ff = n_ff; ia.limit = limit; ia.tg = expert_type(lay, 0); ia.td = expert_type(lay, 2); if (ia.tg != expert_type(lay, 1) || !iq::supported(ia.tg) || !iq::supported(ia.td) || n_embd % iq::block_values(ia.tg) != 0 || n_ff % iq::block_values(ia.td) != 0) { std::fprintf(stderr, "ds4 gguf %s: expert types %s / %s / %s (n_embd %d, n_ff %d) not covered\n", who, type_name(expert_type(lay, 0)), type_name(expert_type(lay, 1)), type_name(expert_type(lay, 2)), n_embd, n_ff); g_errors.fetch_add(1, std::memory_order_relaxed); return false; } return true; } template void mv_launch(int f, const BfJobs& J, int nj, int max_n, bool vec, cudaStream_t s) { const dim3 grid((unsigned) ((max_n + kWarps - 1) / kWarps), (unsigned) nj); if (f == 1) { if (vec) mx_mv_kernel<<>>(J); else mx_mv_kernel<<>>(J); } else if (f == 2) { // (vec here: every row 4-byte aligned, so staged) if (vec) q8_mv_kernel<<>>(J); else q8_mv_kernel<<>>(J); } else { if (vec) bf16_mv_kernel<<>>(J); else bf16_mv_kernel<<>>(J); } } void mv_set(int f, const BfJobs& J, int nj, int max_n, int max_c, bool vec, cudaStream_t s) { if (nj == 0) return; if (max_c <= 1) mv_launch<1>(f, J, nj, max_n, vec, s); else if (max_c <= 2) mv_launch<2>(f, J, nj, max_n, vec, s); else if (max_c <= 4) mv_launch<4>(f, J, nj, max_n, vec, s); else if (max_c <= 8) mv_launch<8>(f, J, nj, max_n, vec, s); else mv_launch<16>(f, J, nj, max_n, vec, s); } } // namespace x3::Mat view(const x3::Mat& m, int c0, int nc) { x3::Mat v = m; const size_t ld = row_stride(m); v.ld = (int) ld; v.raw = m.fmt != kFmtBF16 ? (const void*) ((const uint8_t*) m.raw + (size_t) c0 * ld) : (const void*) ((const uint16_t*) m.raw + (size_t) c0 * ld); v.n = nc; return v; } bool mv(const x3::MvJob* jobs, int n, cudaStream_t s) { if (n <= 0 || n > x3::kMaxMvJobs) return false; BfJobs J[3] = {}; // the BF16 jobs, the MXFP4 ones, the Q8_0 ones: a launch each int nj[3] = {0, 0, 0}, max_n[3] = {0, 0, 0}, max_c[3] = {1, 1, 1}; bool vec[3] = {true, true, true}; iq::MvJob IJ[x3::kMaxMvJobs]; // the low-bit ones (ds4_iq: a launch a type) int nij = 0; QuantItems QI; // the int8 roundings of this call's jobs, one launch (identical inputs once) QI.n = 0; for (int i = 0; i < n; ++i) { const x3::MvJob& q = jobs[i]; const x3::Mat& m = *q.m; if (m.fmt == kFmtGG) { // the activations rounded to int8 (job i's rows of the scratch), then ds4_iq's GEMV Scratch* sc = scratch(s); if (sc == nullptr || m.raw == nullptr || q.xf == nullptr || q.y == nullptr || q.ncol < 1 || q.ncol > x3::kMaxMvCols || m.k > kMaxQK || type_row_bytes(m.K, m.k) == 0) { std::fprintf(stderr, "ds4 gguf mv: job %d unsupported (type %s k %d n %d)\n", i, type_name(m.K), m.k, m.n); g_errors.fetch_add(1, std::memory_order_relaxed); return false; } const size_t at = (size_t) i * x3::kMaxMvCols * kMaxQK; const int qi = quant_add(QI, q.xf, q.ncol > 1 ? q.xf_cs : (size_t) m.k, q.ncol, m.k, sc->mq + at, sc->md + at / 32); iq::MvJob& b = IJ[nij++]; b.w = (const uint8_t*) m.raw; b.ld = row_stride(m); b.type = m.K; b.k = m.k; b.n = m.n; b.ncol = q.ncol; b.xq = QI.q[qi]; b.xd = QI.d[qi]; b.y = q.y; b.ycs = q.y_cs; b.bias = q.bias; b.alpha = q.alpha; continue; } const int f = m.fmt == kFmtMXFP4 ? 1 : m.fmt == kFmtQ8_0 ? 2 : 0; if ((m.fmt != kFmtBF16 && m.fmt != kFmtMXFP4 && m.fmt != kFmtQ8_0) || m.raw == nullptr || q.xf == nullptr || q.y == nullptr || q.ncol < 1 || q.ncol > x3::kMaxMvCols || m.k <= 0 || m.n <= 0 || (f != 0 && m.k % kMxBlock != 0)) { std::fprintf(stderr, "ds4 gguf mv: job %d unsupported (fmt %d k %d n %d, %d activations)\n", i, m.fmt, m.k, m.n, q.ncol); g_errors.fetch_add(1, std::memory_order_relaxed); return false; } BfJob& b = J[f].j[nj[f]++]; b.w = m.raw; b.ld = row_stride(m); b.x = q.xf; b.xcs = q.xf_cs; b.y = q.y; b.ycs = q.y_cs; b.bias = q.bias; b.alpha = q.alpha; b.k = m.k; b.n = m.n; b.ncol = q.ncol; max_n[f] = std::max(max_n[f], m.n); max_c[f] = std::max(max_c[f], q.ncol); const bool xa = ((uintptr_t) b.x & 15u) == 0 && (q.ncol == 1 || b.xcs % 4 == 0); if (f == 2) { // the activations rounded to int8 first (each job its own rows of the scratch) Scratch* sc = scratch(s); if (sc == nullptr || m.k > kMaxQK) { fail("mv: Q8_0 inputs past the scratch"); return false; } const size_t at = (size_t) i * x3::kMaxMvCols * kMaxQK; const int qi = quant_add(QI, q.xf, q.ncol > 1 ? q.xf_cs : (size_t) m.k, q.ncol, m.k, sc->mq + at, sc->md + at / 32); b.xq = QI.q[qi]; b.xd = QI.d[qi]; vec[f] = vec[f] && b.ld % 4 == 0 && ((uintptr_t) b.w & 3u) == 0 && (m.k / 32) % 2 == 0; } else { vec[f] = vec[f] && xa && (f != 0 || (m.k % 8 == 0 && b.ld % 8 == 0 && ((uintptr_t) b.w & 15u) == 0)); } } quant_flush(QI, s); for (int f = 0; f < 3; ++f) mv_set(f, J[f], nj[f], max_n[f], max_c[f], vec[f], s); if (nij > 0 && !iq::mv(IJ, nij, s)) { fail("mv: a low-bit job not covered"); return false; } check("mv"); return true; } void rows(const x3::Mat& m, uint16_t* dst, bool bf16, cudaStream_t s) { if (m.fmt == kFmtGG && m.raw != nullptr) { // a K-quant / i-quant dense matrix (the IST-DAS sets' Q5_K): its own dequantizer if (!iq::dequant_rows(m.K, m.raw, row_stride(m), m.k, m.n, dst, bf16, s)) fail("rows: a low-bit type not covered"); return; } if ((m.fmt != kFmtBF16 && m.fmt != kFmtQ8_0) || m.raw == nullptr) { fail("rows: not a BF16 or Q8_0 matrix"); return; } const size_t ld = row_stride(m); const size_t total = (size_t) m.n * (size_t) m.k; const unsigned blocks = (unsigned) std::min((total + 255) / 256, 65535); if (m.fmt == kFmtQ8_0) q8_rows_kernel<<>>((const uint8_t*) m.raw, ld, m.k, m.n, dst, bf16); else bf16_rows_kernel<<>>((const uint16_t*) m.raw, ld, m.k, m.n, dst, bf16); check("rows"); } namespace { // (staged when the matrix's rows are 4-byte aligned: 17 nb % 4 == 0; the blobs' offsets are) template struct GateUpK { static void run(const MxArgs& m, dim3 grid, cudaStream_t s) { if (mx_row_bytes(m.n_embd) % 4 == 0) mx_gate_up_kernel<<>>(m); else mx_gate_up_kernel<<>>(m); } }; template struct DownK { static void run(const MxArgs& m, dim3 grid, cudaStream_t s) { if (mx_row_bytes(m.n_ff) % 4 == 0) mx_down_kernel<<>>(m); else mx_down_kernel<<>>(m); } }; template