From 5f81c5dc47455483e6ff2e4bd9bfbd4755e80aff Mon Sep 17 00:00:00 2001 From: Niko1221 Date: Tue, 20 Oct 2026 17:57:26 +0200 Subject: [PATCH 1/2] Gyro decode (CUDA): trellis codebook in shared memory, 8-lane rows for the 160-dot gate/up and the 40-dot down The int8 codebook lookups of tq_load went through the global table: a warp's 32 random 2-byte reads touched ~20 L1 sectors each (native_expert_bench, ncu: 51M sectors for 2.8M requests, L1 64% busy). The grouped gate/up and down kernels now stage the 4 KB codebook per block (stage_iq_grid, STRATA_IQ_STAGE_GRID=0 reads it in place) and take 4 rows per warp: gate/up K=160 (new row_dot_160_sub8) and the K=40 down (row_dot_40_sub8, as IQ4_NL), 32 rows per block so the staging is amortised and no lane idles. The sums keep row_dot_multi's tree (lane sums of t, t+8, t+16, t+24, then the same xor steps), so the outputs are bit for bit the old ones: native_expert_bench prints an FNV hash of the outputs now, equal for base and new on Gyro-S, Gyro-M and Q2_0 blobs. RTX 3060, 24 groups, 1 token: Gyro-M gate/up 221 -> 140 us, down 182 -> 86 us; Gyro-S 228 -> 157 and 173 -> 89. --- src/kernels/cuda/iq_kernels.cu | 172 +++++++++++++++++++++++----- src/kernels/native_expert_bench.cpp | 4 + 2 files changed, 146 insertions(+), 30 deletions(-) diff --git a/src/kernels/cuda/iq_kernels.cu b/src/kernels/cuda/iq_kernels.cu index 4d988f1a..769b674a 100644 --- a/src/kernels/cuda/iq_kernels.cu +++ b/src/kernels/cuda/iq_kernels.cu @@ -671,8 +671,11 @@ __device__ __forceinline__ void tq_states4(const uint8_t* qs, int l, uint32_t s[ // sum|gpu - ref| / sum|ref| < 3e-2 (the bound every expert type's CPU and GPU paths get there); the prompt path's // fp16 dequantizers against to_float at < 1e-6, their fp16 output equal to the fp16 of the float one. struct TqLane { int q[4]; float d; }; -template -__device__ __forceinline__ TqLane tq_load(const void* __restrict__ vbq, int kbx, int iqs) { +// STAGE: the codebook is read from the block's shared-memory copy (s_lut, stage_iq_grid) instead of the global table: a +// warp's 32 random 2-byte lookups touch ~20 sectors of the global table's L1 lines but only a few shared banks. +// The same values either way. +template +__device__ __forceinline__ TqLane tq_load(const void* __restrict__ vbq, int kbx, int iqs, const uint32_t* __restrict__ s_lut = nullptr) { const auto* b = (const typename TqInfo::block_t*) vbq + kbx; uint32_t s[4]; tq_states4(b->qs, iqs / 4, s); @@ -680,7 +683,12 @@ __device__ __forceinline__ TqLane tq_load(const void* __restrict__ vbq, int kbx, #pragma unroll for (int i = 0; i < 4; ++i) { const uint32_t x = (s[i] & TQ_STATE_MASK) * 0x9e3779b1u; - r.q[i] = (int) ((uint32_t) tq_lut_i8[x >> 21] | ((uint32_t) tq_lut_i8[(x >> 10) & 2047u] << 16)); + if constexpr (STAGE) { + const uint16_t* lut = (const uint16_t*) s_lut; + r.q[i] = (int) ((uint32_t) lut[x >> 21] | ((uint32_t) lut[(x >> 10) & 2047u] << 16)); + } else { + r.q[i] = (int) ((uint32_t) tq_lut_i8[x >> 21] | ((uint32_t) tq_lut_i8[(x >> 10) & 2047u] << 16)); + } } r.d = tq_h2f(b->d); return r; @@ -813,7 +821,8 @@ template inline constexpr bool kSplit = false; template inline constexpr bool kq_type = (TY == 12 || TY == 13 || TY == 8); // not Q5_1: its Split apply is not bitwise the old dot (FMA contraction), see notes-151-amd inline bool g_kq_split = [] { const char* v = std::getenv("STRATA_KQ_SPLIT"); return v != nullptr && v[0] == '1'; }(); template inline bool split_off() { return kq_type && !g_kq_split; } -template inline constexpr bool kStageIqGrid = (TY == 16 || TY == 17 || TY == 18 || TY == 21 || TY == 22 || TY == 29); +template inline constexpr bool kStageIqGrid = (TY == 16 || TY == 17 || TY == 18 || TY == 21 || TY == 22 || TY == 29 || + TY == 144 || TY == 145 || TY == 146); // 144-146: the trellis codebook template<> inline constexpr bool kSplit<16> = true; template<> struct Split<16> { // IQ2_XXS @@ -1363,9 +1372,9 @@ template<> inline constexpr bool kSplit<145> = true; template<> inline constexpr bool kSplit<146> = true; template struct SplitTq { using W = TqLane; - template // (no grid table to stage: the codebook is read where it is) - __device__ static W load(const void* __restrict__ vbq, int kbx, int iqs, const uint32_t* __restrict__ = nullptr) { - return tq_load(vbq, kbx, iqs); + template // STAGE_GRID: the codebook comes from the shared copy stage_iq_grid made + __device__ static W load(const void* __restrict__ vbq, int kbx, int iqs, const uint32_t* __restrict__ s_lut = nullptr) { + return tq_load(vbq, kbx, iqs, s_lut); } __device__ static float apply(const W& r, const block_q8_1* __restrict__ bq8_1, int iqs) { return tq_apply(r, bq8_1, iqs); } }; @@ -1397,23 +1406,29 @@ __device__ __forceinline__ void row_dot_multi(const uint8_t* row, const block_q8 // 8-lane sub-warp row dot for K = nb * ipb == 40 (TD == 20, IQ4_NL with n_ff == 640): // 4 rows per warp (32 rows per 256-thread block), 100% active lanes, bitwise identical addition tree to row_dot_multi. -template +// the weight part of lane (kbx, iqs): through the staged codebook (STAGE) or in place +template +__device__ __forceinline__ typename S::W split_load_st(const uint8_t* row, int kbx, int iqs, const uint32_t* __restrict__ s_grid) { + if constexpr (STAGE) return S::template load(row, kbx, iqs, s_grid); + else return S::load(row, kbx, iqs); +} +template __device__ __forceinline__ void row_dot_40_sub8(const uint8_t* row, const block_q8_1* x, const int (&off)[NC], int n, - int t, float (&s)[NC]) { + int t, float (&s)[NC], const uint32_t* __restrict__ s_grid = nullptr) { using F = Fmt; using S = Split; #pragma unroll for (int c = 0; c < NC; ++c) s[c] = 0.0f; for (int k = t; k < 40; k += 32) { const int kbx = k / F::ipb, iqs = F::step * (k % F::ipb); - const typename S::W w = S::load(row, kbx, iqs); + const typename S::W w = split_load_st(row, kbx, iqs, s_grid); #pragma unroll for (int c = 0; c < NC; ++c) if (EXACT_N || c < n) s[c] += S::apply(w, x + off[c] + kbx * (F::qk / 32), iqs); } { const int k = t + 16, kbx = k / F::ipb, iqs = F::step * (k % F::ipb); - const typename S::W w = S::load(row, kbx, iqs); + const typename S::W w = split_load_st(row, kbx, iqs, s_grid); #pragma unroll for (int c = 0; c < NC; ++c) { if (EXACT_N || c < n) { @@ -1428,14 +1443,14 @@ __device__ __forceinline__ void row_dot_40_sub8(const uint8_t* row, const block_ for (int c = 0; c < NC; ++c) s8[c] = 0.0f; { const int k = t + 8, kbx = k / F::ipb, iqs = F::step * (k % F::ipb); - const typename S::W w = S::load(row, kbx, iqs); + const typename S::W w = split_load_st(row, kbx, iqs, s_grid); #pragma unroll for (int c = 0; c < NC; ++c) if (EXACT_N || c < n) s8[c] += S::apply(w, x + off[c] + kbx * (F::qk / 32), iqs); } { const int k = t + 24, kbx = k / F::ipb, iqs = F::step * (k % F::ipb); - const typename S::W w = S::load(row, kbx, iqs); + const typename S::W w = split_load_st(row, kbx, iqs, s_grid); #pragma unroll for (int c = 0; c < NC; ++c) { if (EXACT_N || c < n) { @@ -1573,6 +1588,43 @@ __device__ __forceinline__ void row_dot_80_sub16(const uint8_t* row, const block } } +// 8-lane sub-warp row dot for K = nb * ipb == 160 (the APR trellis gate/up types at n_embd 2560): 4 rows per warp (32 per +// 256-thread block). Bitwise row_dot_multi's tree: its lane L sums the calls k = L, L + 32, ... L + 128 in that order, then +// warp_sum adds lane + 16, then + 8, then 4, 2, 1. Here lane t of 8 keeps the four sums of the lanes t, t + 8, t + 16 and +// t + 24 (v = 0..3) and adds them (v0 + v2) + (v1 + v3), which is what the first two steps give; 3 steps follow. A trellis +// block is 8 lanes, so lane t always takes lane t of block v + 4 j: its bit offsets are the same for all 20 calls. +template +__device__ __forceinline__ void row_dot_160_sub8(const uint8_t* row, const block_q8_1* x, const int (&off)[NC], int n, + int t, float (&s)[NC], const uint32_t* __restrict__ s_grid = nullptr) { + using F = Fmt; + using S = Split; + static_assert(F::ipb == 8 && F::step == 4, "one trellis block is 8 lanes of 4 steps"); + float p[4][NC]; +#pragma unroll + for (int v = 0; v < 4; ++v) +#pragma unroll + for (int c = 0; c < NC; ++c) p[v][c] = 0.0f; +#pragma unroll + for (int j = 0; j < 5; ++j) { +#pragma unroll + for (int v = 0; v < 4; ++v) { + const int kbx = v + 4 * j, iqs = F::step * t; + const typename S::W w = split_load_st(row, kbx, iqs, s_grid); +#pragma unroll + for (int c = 0; c < NC; ++c) + if (EXACT_N || c < n) p[v][c] += S::apply(w, x + off[c] + kbx * (F::qk / 32), iqs); + } + } +#pragma unroll + for (int c = 0; c < NC; ++c) { + if (EXACT_N || c < n) { + s[c] = __fadd_rn(__fadd_rn(p[0][c], p[2][c]), __fadd_rn(p[1][c], p[3][c])); +#pragma unroll + for (int o = 4; o > 0; o >>= 1) s[c] += __shfl_xor_sync(0xffffffffu, s[c], o); + } + } +} + // 147-verify: row_dot_80_sub16 for two rows (a gate row and its up row) at once, both rows' weight loads of a step issued // before either is applied; each row's sums are row_dot_80_sub16's, in its order (the same bits). template @@ -1680,6 +1732,12 @@ __device__ __forceinline__ const uint32_t* stage_iq_grid(uint32_t* s_buf, int ti for (int i = tid; i < 1024; i += nthreads) dst[i] = src[i]; __syncthreads(); return s_buf; + } else if constexpr (TY == 144 || TY == 145 || TY == 146) { + // the int8 trellis codebook: 2048 two-byte entries = 1024 words + const uint32_t* src = (const uint32_t*) tq_lut_i8; + for (int i = tid; i < 1024; i += nthreads) s_buf[i] = src[i]; + __syncthreads(); + return s_buf; } else if constexpr (TY == 18) { // 256 codebook words, then two 128-word tables of the per-byte sign masks (signs 0-3, signs 4-7). A // mask depends only on the 7 bits it is unpacked from, and iq3xxs_grid's bytes are all >= 4, so the @@ -1706,7 +1764,7 @@ __device__ __forceinline__ const uint32_t* stage_iq_grid(uint32_t* s_buf, int ti template> struct IqGridWords { - static constexpr int value = !STAGE_GRID ? 4 : (TY == 22 || TY == 29) ? 2048 : (TY == 17 || TY == 21) ? 1024 : (TY == 16 || TY == 18) ? 512 : 4; + static constexpr int value = !STAGE_GRID ? 4 : (TY == 22 || TY == 29) ? 2048 : (TY == 17 || TY == 21 || TY == 144 || TY == 145 || TY == 146) ? 1024 : (TY == 16 || TY == 18) ? 512 : 4; }; // mmvq_kernel with the columns taken NC at a time. After warp_sum every lane holds the same sum, so lane c stores @@ -1812,7 +1870,47 @@ __global__ void __launch_bounds__(256) native_gu_multi_kernel(const unsigned lon STRATA_SHARED_ALIGN16_U32(s_grid_buf, IqGridWords::value); const uint32_t* s_grid = stage_iq_grid(s_grid_buf, threadIdx.x, 256); const int nb = (int) (L.n_embd / Fmt::qk), xb = (int) (L.n_embd / 32); - if constexpr (SUB16) { + if constexpr (SUB16 && (TG == 144 || TG == 145)) { + if (nb * Fmt::ipb == 160) { // the trellis rows: 160 dots at n_embd 2560, 8 lanes a row + const int subwarp = threadIdx.x >> 3, t = threadIdx.x & 7; + const int row = blockIdx.x * 32 + subwarp; // 0 .. 2*n_ff + if (row >= 2 * L.n_ff) return; + const bool is_up = row >= L.n_ff; + const int r = is_up ? row - (int) L.n_ff : row; + const size_t w_off = (is_up ? L.up_off : 0) + (size_t) r * L.gu_row; + float* dst = is_up ? up : gate; + for (int g = blockIdx.y; g < ng; g += gridDim.y) { + const uint8_t* wr = (const uint8_t*) grp_ptr[g] + w_off; + const float sc = L.tail_off ? ((const float*) (grp_ptr[g] + L.tail_off))[is_up ? 1 : 0] : 1.0f; + const int e0 = grp_start[g], e1 = grp_start[g + 1]; + for (int e = e0; e < e1; e += GRP_NC) { + const int n = min(GRP_NC, e1 - e); + if (n == 1) { + const int off1[1] = { ent_tok[e] * 80 }; + float s1[1]; + row_dot_160_sub8(wr, xq, off1, 1, t, s1, s_grid); + if (t == 0) dst[(size_t) e * L.n_ff + r] = s1[0] * sc; + } else if (n == 2) { + const int off2[2] = { ent_tok[e] * 80, ent_tok[e + 1] * 80 }; + float s2[2]; + row_dot_160_sub8(wr, xq, off2, 2, t, s2, s_grid); + if (t < 2) dst[(size_t) (e + t) * L.n_ff + r] = s2[t] * sc; + } else if (n == 3) { + const int off3[3] = { ent_tok[e] * 80, ent_tok[e + 1] * 80, ent_tok[e + 2] * 80 }; + float s3[3]; + row_dot_160_sub8(wr, xq, off3, 3, t, s3, s_grid); + if (t < 3) dst[(size_t) (e + t) * L.n_ff + r] = s3[t] * sc; + } else { + const int off4[4] = { ent_tok[e] * 80, ent_tok[e + 1] * 80, ent_tok[e + 2] * 80, ent_tok[e + 3] * 80 }; + float s4[4]; + row_dot_160_sub8(wr, xq, off4, 4, t, s4, s_grid); + if (t < 4) dst[(size_t) (e + t) * L.n_ff + r] = s4[t] * sc; + } + } + } + return; + } + } else if constexpr (SUB16) { if (xb == 80 && nb * Fmt::ipb == 80) { // row_dot_80_sub16 covers exactly 80 dots (not the 128-value TQ blocks) const int subwarp = threadIdx.x >> 4, t = threadIdx.x & 15; const int row = blockIdx.x * 16 + subwarp; // 0 .. 2*n_ff @@ -1955,15 +2053,15 @@ __global__ void __launch_bounds__(256) native_down_kernel(const unsigned long lo } // the 8-lane down rows: IQ4_NL's K = 40, NVFP4's K = 20 -template +template __device__ __forceinline__ void row_dot_sub8(const uint8_t* row, const block_q8_1* x, const int (&off)[NC], int n, int t, - float (&s)[NC]) { + float (&s)[NC], const uint32_t* __restrict__ s_grid = nullptr) { if constexpr (TD == 40) row_dot_20_sub8(row, x, off, n, t, s); - else row_dot_40_sub8(row, x, off, n, t, s); + else row_dot_40_sub8(row, x, off, n, t, s, s_grid); } // native_down_kernel with the entries taken GRP_NC at a time -template +template __global__ void __launch_bounds__(256) native_down_multi_kernel(const unsigned long long* __restrict__ grp_ptr, const int32_t* __restrict__ grp_start, const int32_t* __restrict__ n_groups, @@ -1973,8 +2071,10 @@ __global__ void __launch_bounds__(256) native_down_multi_kernel(const unsigned l const int ng = *n_groups; if (blockIdx.y >= ng) return; __shared__ uint32_t s_hq_buf[GRP_NC * 20 * 9]; + STRATA_SHARED_ALIGN16_U32(s_grid_buf, IqGridWords::value); + const uint32_t* s_grid = stage_iq_grid(s_grid_buf, threadIdx.x, 256); const int nb = (int) (L.n_ff / Fmt::qk), hb = (int) (L.n_ff / 32); - if constexpr (SUB_DOWN && (TD == 20 || TD == 40)) { // IQ4_NL (K 40) and NVFP4 (K 20): 8-lane rows + if constexpr (SUB_DOWN && (TD == 20 || TD == 40 || TD == 144 || TD == 146)) { // IQ4_NL / TQ2_T / TQK7 (K 40) and NVFP4 (K 20): 8-lane rows if (hb == 20) { const int r = blockIdx.x * 32 + (threadIdx.x >> 3); const int t = threadIdx.x & 7; @@ -1996,22 +2096,22 @@ __global__ void __launch_bounds__(256) native_down_multi_kernel(const unsigned l if (n == 1) { const int off1[1] = { 0 }; float s1[1]; - row_dot_sub8(wr, s_hq, off1, 1, t, s1); + row_dot_sub8(wr, s_hq, off1, 1, t, s1, s_grid); if (t == 0) out[(size_t) ent_dst[e] * L.n_embd + r] = s1[0] * sd; } else if (n == 2) { const int off2[2] = { 0, 20 }; float s2[2]; - row_dot_sub8(wr, s_hq, off2, 2, t, s2); + row_dot_sub8(wr, s_hq, off2, 2, t, s2, s_grid); if (t < 2) out[(size_t) ent_dst[e + t] * L.n_embd + r] = s2[t] * sd; } else if (n == 3) { const int off3[3] = { 0, 20, 40 }; float s3[3]; - row_dot_sub8(wr, s_hq, off3, 3, t, s3); + row_dot_sub8(wr, s_hq, off3, 3, t, s3, s_grid); if (t < 3) out[(size_t) ent_dst[e + t] * L.n_embd + r] = s3[t] * sd; } else { const int off4[4] = { 0, 20, 40, 60 }; float s4[4]; - row_dot_sub8(wr, s_hq, off4, 4, t, s4); + row_dot_sub8(wr, s_hq, off4, 4, t, s4, s_grid); if (t < 4) out[(size_t) ent_dst[e + t] * L.n_embd + r] = s4[t] * sd; } } @@ -2086,19 +2186,19 @@ __global__ void __launch_bounds__(256) native_down_multi_kernel(const unsigned l if (n == 1) { const int off1[1] = { 0 }; float s1[1]; - row_dot_multi(wr, s_hq, off1, 1, nb, lane, s1); + row_dot_multi(wr, s_hq, off1, 1, nb, lane, s1, s_grid); if (lane == 0) out[(size_t) ent_dst[e] * L.n_embd + r] = s1[0] * sd; } else if (n == 2) { const int off2[2] = { 0, 20 }; float s2[2]; - row_dot_multi(wr, s_hq, off2, 2, nb, lane, s2); + row_dot_multi(wr, s_hq, off2, 2, nb, lane, s2, s_grid); if (lane < 2) out[(size_t) ent_dst[e + lane] * L.n_embd + r] = s2[lane] * sd; } else { int off[GRP_NC]; #pragma unroll for (int c = 0; c < GRP_NC; ++c) off[c] = min(c, n - 1) * 20; float s[GRP_NC]; - row_dot_multi(wr, s_hq, off, n, nb, lane, s); + row_dot_multi(wr, s_hq, off, n, nb, lane, s, s_grid); #pragma unroll for (int c = 0; c < GRP_NC; ++c) if (c < n && lane == c) out[(size_t) ent_dst[e + c] * L.n_embd + r] = s[c] * sd; @@ -2112,7 +2212,7 @@ __global__ void __launch_bounds__(256) native_down_multi_kernel(const unsigned l #pragma unroll for (int c = 0; c < GRP_NC; ++c) off[c] = (e + min(c, n - 1)) * hb; float s[GRP_NC]; - row_dot_multi(wr, hq, off, n, nb, lane, s); + row_dot_multi(wr, hq, off, n, nb, lane, s, s_grid); #pragma unroll for (int c = 0; c < GRP_NC; ++c) if (c < n && lane == c) out[(size_t) ent_dst[e + c] * L.n_embd + r] = s[c] * sd; @@ -3811,7 +3911,10 @@ void launch_gu(dim3 grid, cudaStream_t s, const unsigned long long* grp_ptr, con float* gate, float* up) { // the sub-16 rows (row_dot_80_sub16) assume 80 dots per row: (n_embd / qk) * ipb == 80. The APR trellis types // (qk 128, ipb 8: 160 dots at n_embd 2560) take the full-warp multi kernel, as before the sub-16 kernels. - const bool g_no_sub16_gu = ::strata::kernels::g_no_sub16_gu || (L.n_embd / Fmt::qk) * Fmt::ipb != 80; + constexpr bool tq = (TG == 144 || TG == 145); // 160 dots at n_embd 2560: the 8-lane rows (32 rows a block) + const bool g_no_sub16_gu = ::strata::kernels::g_no_sub16_gu || (!tq && (L.n_embd / Fmt::qk) * Fmt::ipb != 80) || + (tq && (L.n_embd / Fmt::qk) * Fmt::ipb != 160); + if (tq && !g_no_sub16_gu) grid.x = (unsigned) ((2 * L.n_ff + 31) / 32); if constexpr (!kSplit) native_gu_kernel<<>>(grp_ptr, grp_start, n_groups, ent_tok, X, L, gate, up); else if (g_old_kernels || split_off()) native_gu_kernel<<>>(grp_ptr, grp_start, n_groups, ent_tok, X, L, gate, up); else if constexpr (kStageIqGrid) { @@ -3882,6 +3985,15 @@ void launch_down(dim3 grid, cudaStream_t s, const unsigned long long* grp_ptr, c float* out) { if constexpr (!kSplit) native_down_kernel<<>>(grp_ptr, grp_start, n_groups, ent_dst, hq, L, out); else if (g_old_kernels || split_off()) native_down_kernel<<>>(grp_ptr, grp_start, n_groups, ent_dst, hq, L, out); + else if constexpr (TD == 144 || TD == 146) { // the trellis codebook in shared memory (STRATA_IQ_STAGE_GRID=0: read in place) + if (g_no_sub16_gu) { + if (g_stage_grid) native_down_multi_kernel<<>>(grp_ptr, grp_start, n_groups, ent_dst, hq, L, out); + else native_down_multi_kernel<<>>(grp_ptr, grp_start, n_groups, ent_dst, hq, L, out); + } else { + if (g_stage_grid) native_down_multi_kernel<<>>(grp_ptr, grp_start, n_groups, ent_dst, hq, L, out); + else native_down_multi_kernel<<>>(grp_ptr, grp_start, n_groups, ent_dst, hq, L, out); + } + } else if (g_no_sub16_gu) native_down_multi_kernel<<>>(grp_ptr, grp_start, n_groups, ent_dst, hq, L, out); else native_down_multi_kernel<<>>(grp_ptr, grp_start, n_groups, ent_dst, hq, L, out); } @@ -4948,7 +5060,7 @@ void native_expert_grouped(const NativeExpertLayout& L, const unsigned long long check("native_expert_grouped/swiglu"); } if (g_exp_phase == 1) return; - const int d_rows = (!g_old_kernels && !g_no_sub16_gu && L.n_ff == 640) ? (L.d_type == 20 || L.d_type == 40 ? 32 : (L.d_type == 42 ? 16 : 8)) : 8; + const int d_rows = (!g_old_kernels && !g_no_sub16_gu && L.n_ff == 640) ? (L.d_type == 20 || L.d_type == 40 || L.d_type == 144 || L.d_type == 146 ? 32 : (L.d_type == 42 ? 16 : 8)) : 8; const dim3 gd((unsigned) ((L.n_embd + d_rows - 1) / d_rows), (unsigned) gy); #if STRATA_EXP_LAYOUTS const int em0d = sm75_down_em(em0, cap_groups); diff --git a/src/kernels/native_expert_bench.cpp b/src/kernels/native_expert_bench.cpp index 7f16e220..48234612 100644 --- a/src/kernels/native_expert_bench.cpp +++ b/src/kernels/native_expert_bench.cpp @@ -15,6 +15,7 @@ #include #include +#include #include #include #include @@ -151,6 +152,9 @@ int main(int argc, char** argv) { for (int round = 0; round < 3; ++round) // interleaved, the minimum: the clocks wander between runs for (int k = 0; k < 2; ++k) for (int p = 0; p < 3; ++p) us[k][p] = std::fmin(us[k][p], time(k ? mode : mref, p)); + uint64_t fnv = 1469598103934665603ull; // FNV-1a of the output bytes: equal hashes across two builds = bitwise equal outputs + for (size_t i = 0; i < got.size() * 4; ++i) fnv = (fnv ^ reinterpret_cast(got.data())[i]) * 1099511628211ull; + std::printf("hash %016llx\n", (unsigned long long) fnv); std::printf("layer %2d %-7s/%-6s G %d T %d | mode %d: all %7.1f us gu %7.1f us (%3.0f GB/s) down %6.1f us (%3.0f GB/s)" " | mode %d: all %7.1f us gu %7.1f us (%3.0f GB/s) down %6.1f us (%3.0f GB/s) | %s\n", l, ggml_type_name((ggml_type) f.gu_type), ggml_type_name((ggml_type) f.d_type), G, T, From aae92ceb264f7fb2973ac5db06e3afd28931ff1f Mon Sep 17 00:00:00 2001 From: Niko1221 Date: Wed, 21 Oct 2026 00:14:08 +0200 Subject: [PATCH 2/2] Gyro: decode speed on NVIDIA (docs, 0.2.2 notes), Gyro-M's slowdown against Q2_0 16-21% GYRO.md gets the measured RTX 3060 numbers of the shared-memory codebook / 8-lane row kernels (Gyro-S 53.6 -> 62.8 tok/s, Gyro-M 50.6 -> 56.1, 5 interleaved pairs, identity gate identical), the release notes one line, and the places that said Gyro-M decodes about 20% slower than Q2_0 now say 16-21% (RTX 3060 12 GB after the kernels: 16%, R9700: 17-21%). The translated READMEs keep the old figure. --- README.md | 4 ++-- docs/GYRO.md | 29 +++++++++++++++++++++++++++-- docs/MODELS.md | 4 ++-- docs/release-notes-0.2.2.md | 1 + setup.py | 2 +- tools/strata_mcp.py | 2 +- 6 files changed, 34 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index d0afcab5..9b3b8c7a 100644 --- a/README.md +++ b/README.md @@ -157,7 +157,7 @@ Smaller sizes are faster. Larger sizes are a bit smarter. | Size | Memory it needs (RAM + VRAM) | Download | What it is | | --- | ---: | ---: | --- | | **Gyro-S** | 27.6 GB | 58.5 GB | For PCs that fit nothing else. Quality is lower than IQ2_XS. | -| **Gyro-M** | 35 GB | 92.0 GB | About IQ3_XXS quality with about 20% less GPU memory. It writes answers 17-21% slower than Q2_0 on a 12 GB card and on an R9700. | +| **Gyro-M** | 35 GB | 92.0 GB | About IQ3_XXS quality with about 20% less GPU memory. It writes answers 16% (12 GB RTX 3060) to 21% (R9700) slower than Q2_0. | Gyro runs on NVIDIA and AMD cards (AMD is experimental) on Intel Arc cards on Linux (tested on an Arc A750) and, from 0.2.2, on Apple Silicon Macs (Gyro-S on a 32 GB Mac mini: 9 tok/s, streaming from the SSD; [docs/MAC.md](docs/MAC.md#gyro-s-and-gyro-m-agentionais-trellis-experts)). Details and measurements: [docs/GYRO.md](docs/GYRO.md). Start it with `strata launch --model gyro-s`. @@ -167,7 +167,7 @@ Gyro runs on NVIDIA and AMD cards (AMD is experimental) on Intel Arc cards on Li | Your memory (RAM + VRAM) | Take | Notes | | --- | --- | --- | | **32 GB** | **Gyro-S**, or the **[Coder](docs/MODELS.md#coder)** for code | The Coder needs 32 GB of RAM and keeps half of the experts, so it is weaker outside code | -| **40 GB** | **[Swift 1.5](docs/MODELS.md#swift-15)** IQ2_XS (39.2 GB), or **Gyro-M** (35 GB) | Swift thinks for a shorter time before it answers. Gyro-M is close to IQ3_XXS in quality, but it writes answers 17-21% slower than Q2_0 | +| **40 GB** | **[Swift 1.5](docs/MODELS.md#swift-15)** IQ2_XS (39.2 GB), or **Gyro-M** (35 GB) | Swift thinks for a shorter time before it answers. Gyro-M is close to IQ3_XXS in quality, but it writes answers 16-21% slower than Q2_0 | | **48 GB** | **IQ3_XXS** (47.0 GB) | Slower, better | | **64 GB** | **IQ3_S** (54.8 GB), the original or Swift 1.5 | The slowest, the best: it matches the full model on the published tests | | **96 GB or more** | Unsloth's **UD-IQ4_XS** (about 72 GB of RAM) to **Q8_0** (about 136 GB), 4 to 8 bit | Closer to the full model. With less RAM, Strata reads part of them from the SSD, which is much slower | diff --git a/docs/GYRO.md b/docs/GYRO.md index c7b3610d..453bc2a8 100644 --- a/docs/GYRO.md +++ b/docs/GYRO.md @@ -12,14 +12,14 @@ is anybody's default. | **Download** (the n-gram table is inside the file) | **58.5 GB** | **92.0 GB** | | **Memory it needs** (VRAM + RAM together) | **27.6 GB** | **35 GB** | | Experts (trellis) | TQK6 gate/up, TQK7 down | TQ2_T | -| In plain words | the smallest option, for PCs with little memory; lower quality than the 2-3-bit sizes | about IQ3_XXS quality with about 20% less GPU memory; decodes about 20% slower than Q2_0 on 12 GB cards | +| In plain words | the smallest option, for PCs with little memory; lower quality than the 2-3-bit sizes | about IQ3_XXS quality with about 20% less GPU memory; decodes about 16% slower than Q2_0 on a 12 GB RTX 3060 (23% slower before engine 0.2.2) | About half of each file is the n-gram (PLE) table: it stays on the SSD and is read row by row for every token, so keep the file on a fast local drive. The memory figure is what the weights take once loaded. Not defaults, by measurement (our evaluation, 2026-10): against Q2_0 at the same GPU bytes Gyro-M halves the KL divergence (0.159 against 0.312 on 128 chunks) and is level with IQ3_XXS (0.157), but decodes 17-21% slower on a 12 GB -card (RTX 3060) and an R9700, and its reasoning set scored lower than Q2_0's (49/60 against 57/60). Gyro-S is 13% worse +card (RTX 3060, measured before engine 0.2.2: 16% since) and an R9700, and its reasoning set scored lower than Q2_0's (49/60 against 57/60). Gyro-S is 13% worse in KL than IQ2_XS (0.268 against 0.236) with 24% fewer bytes. So: pick Gyro-S where nothing bigger fits, Gyro-M when the 20% memory saving over IQ3_XXS matters. Setup and the MCP server mention them only where the other sizes do not fit the PC; they warn, never refuse. Gyro-L (agentionai's 50 GiB file, 2026-10-12) is not supported: it uses another @@ -118,6 +118,31 @@ short of one cache slot and the engine stops with that sentence. It starts with put `--ple-io ram` in the config for Gyro (it does for the other packs on an i915 card). One run used about 24 GB of RAM for the expert arena. Keep the file on a fast SSD. +## Decode speed on NVIDIA (engine 0.2.2) + +The trellis decode kernels read the codebook (2048 two-byte entries, 8 random lookups for every 16 weights) from the global +table. On an RTX 3060 that made the gate/up and down kernels wait on the L1 cache, not on memory: 18 L1 sectors for every +lookup request, the card's L1 at 64% busy while the DRAM was 27% busy. The kernels now keep the 4 KB codebook in shared memory +(`STRATA_IQ_STAGE_GRID=0` reads it in place again) and take four rows per warp, 32 rows per thread block, so the copy is shared +and no lane idles in the 160-dot gate/up rows or the 40-dot down rows. The sums are added in the same order as before, so the +answers are bit for bit the old ones: the identity gate (10 prompts, 4096 and 20000 token prompts) is identical on Gyro-S, +Gyro-M, Q2_0 and IQ3_XXS, and the Q2_0 and IQ3_XXS kernels did not change. + +RTX 3060 12 GB, Core Ultra 7, Ubuntu, CUDA 13.4, default flags (MTP draft 4, auto cache), 5 interleaved pairs of whole engine +starts, medians of the story, code and facts prompts (200 tokens each), engine 0.2.1 against 0.2.2. Gyro files of the first +release; the pinned revision is checked in the next block. + +| | decode before, tok/s | after | gain | pairs faster | 4K / 20K prompt | +| --- | ---: | ---: | ---: | --- | --- | +| Gyro-S | 53.6 | 62.8 | +17.2% | 4 of 5 (8-9 of 10 requests per prompt) | unchanged (1.008, 1.000) | +| Gyro-M | 50.6 | 56.1 | +10.9% | 5 of 5 | unchanged (1.013, 0.997) | +| Q2_0 (not changed) | 66.9 | 66.1 | 0.99 (noise) | 2 of 5 | unchanged | + +Against Q2_0 on the same card, before: Gyro-S 53.8 against 65.7 tok/s (18% slower), Gyro-M 51.7 against 67.6 (24% slower). +After: Gyro-S about 6% slower, Gyro-M about 16% slower. What is left of Gyro-M's gap is mostly its dense weights: in a +decode profile its Q8_0 matrix-vector kernels take 26% of the GPU time, and they read as many bytes as the card's memory +delivers. The trellis kernels themselves are limited by the shared-memory bank conflicts of the random codebook lookups now. Prompt speed is not changed by this: Gyro prompts stay 8% (Gyro-S) and 11-19% (Gyro-M) slower than Q2_0's. + ## Credit and licenses The trellis types, the Hadamard rotation, the CPU and GPU kernels and the pack step were written by Laurent Zuijdwijk diff --git a/docs/MODELS.md b/docs/MODELS.md index 7662a6d1..f3b03ba3 100644 --- a/docs/MODELS.md +++ b/docs/MODELS.md @@ -19,7 +19,7 @@ other size stays a choice. A PC with a card of 12 GB and 32 GB of RAM is in the | Your memory (RAM + VRAM) | Pre-selected | Notes | | --- | --- | --- | | **32 GB** | **Gyro-S**, or the **Coder** for code | The Coder needs 32 GB of RAM and keeps half of the experts, so it is weaker outside code ([why](#coder)). Gyro-S has lower quality than the 2-3-bit sizes, and the 2-bit Q2_0 decodes faster | -| **40 GB** | **Swift 1.5** IQ2_XS (39.2 GB) | Gyro-M (35 GB) is the other choice: close to IQ3_XXS in quality, but it writes answers 17-21% slower than Q2_0. Gyro runs on NVIDIA, AMD and Intel Arc cards and, from 0.2.2, on Macs (Gyro-S measured on a 32 GB Mac mini) | +| **40 GB** | **Swift 1.5** IQ2_XS (39.2 GB) | Gyro-M (35 GB) is the other choice: close to IQ3_XXS in quality, but it writes answers 16-21% slower than Q2_0. Gyro runs on NVIDIA, AMD and Intel Arc cards and, from 0.2.2, on Macs (Gyro-S measured on a 32 GB Mac mini) | | **48 GB** | **IQ3_XXS** (47.0 GB) | Slower, better | | **64 GB** | **IQ3_S** (54.8 GB), the original. Swift 1.5's IQ3_S is an equal choice | The slowest, the best: it matches the full model on the published tests | | **96 GB or more** | [Unsloth's UD-IQ4_XS to Q8_0](#unsloths-files-ud-iq4_xs-to-q8_0), 4 to 8 bit | UD-IQ4_XS keeps every expert in RAM from about 80 GB of RAM, Q8_0 from about 136 GB. Up to 192 GB, the 5-8-bit files fit in RAM. Setup still pre-selects IQ3_S here, except on a Strix Halo | @@ -181,7 +181,7 @@ experts in a rotated basis. They are the smallest options and never setup's defa | Size | Download | Memory it needs (VRAM + RAM) | In plain words | | --- | ---: | ---: | --- | | **Gyro-S** | 58.5 GB | 27.6 GB | the smallest option, for PCs with little memory; lower quality | -| **Gyro-M** | 92.0 GB | 35 GB | about IQ3_XXS quality with about 20% less GPU memory; decodes about 20% slower than Q2_0 on 12 GB cards | +| **Gyro-M** | 92.0 GB | 35 GB | about IQ3_XXS quality with about 20% less GPU memory; decodes 16-21% slower than Q2_0 (RTX 3060 12 GB, R9700) | Gyro runs on NVIDIA and AMD (experimental) cards and on Intel Arc cards on Linux (tested on an Arc A750; the Arc engine is built from this release's source, see [INTEL.md](INTEL.md)) and, from 0.2.2, on Apple Silicon Macs ([MAC.md](MAC.md#gyro-s-and-gyro-m-agentionais-trellis-experts): Gyro-S is measured on a 32 GB Mac mini, Gyro-M only tensor by tensor). Gyro reads pictures (the same rule as above). On NVIDIA and AMD it needs engine 0.1.45.1 or newer; if the engine is older, the app says "This model needs a newer engine" and offers Update diff --git a/docs/release-notes-0.2.2.md b/docs/release-notes-0.2.2.md index ec569876..2d416d47 100644 --- a/docs/release-notes-0.2.2.md +++ b/docs/release-notes-0.2.2.md @@ -3,6 +3,7 @@ ## What is new - **Gyro runs on Intel Arc (Linux, tested on an Arc A750 only).** The SYCL engine has the trellis kernels (TQ2_T, TQK6, TQK7) and the Hadamard rotation, so Gyro-S and Gyro-M load and answer on an Arc card. Setup lists them like for any card, warns and never refuses when VRAM and RAM together are short, and keeps Gyro's n-gram table on the SSD. On the A750 (8 GB, Ryzen 5 5600X, 62 GB of RAM) Gyro-S reads a 1,400-token prompt at 82 tok/s (40 before the matrix-unit launch took the trellis types), a 20,000-token prompt at 195 tok/s, and writes 15-16 tok/s with the MTP draft. The card holds 410 of 24,576 experts, so the CPU and the PCIe link carry the decode; for these packs the engine gives the link a larger share (0.48 instead of 0.29, +16% decode). On real tensors of both files the kernels equal a host replay of their own arithmetic in every bit, and 7 of 10 test prompts give the same 48 tokens as the CUDA engine (the other three agree for 6 to 9 tokens and then differ at a near-tie; first-token KL 1e-4 to 1e-2). Gyro-M needs `--max-context 8192 --prefill 1024` on an 8 GB card. Not tested: B-series cards and two Arc cards (the second card is refused with a message). The CUDA comparison used the first-release Gyro-S file; the pinned file passes the kernel checks and answers correctly. Gyro still does not run on the Mac. Details: [GYRO.md](GYRO.md#intel-arc-sycl). +- **Gyro decodes faster on NVIDIA cards.** The gate/up and down kernels for agentionai's trellis experts now keep the codebook in shared memory and take four rows per warp. On an RTX 3060 (12 GB) Gyro-S went from 53.6 to 62.8 tokens per second (+17%) and Gyro-M from 50.6 to 56.1 (+11%), 5 interleaved pairs each; prompts are unchanged. The answers are bit for bit the old ones (identity gate on Gyro-S, Gyro-M, Q2_0 and IQ3_XXS), and Q2_0 and IQ3_XXS are not touched. Gyro-S is now about 6% and Gyro-M about 16% behind Q2_0 on that card (18% and 24% before); `STRATA_IQ_STAGE_GRID=0` goes back to the old lookup. Cards of other generations were not measured. - **An engine package for the RX 6000 series (built, not tested on a real RX 6000 card).** Linux has `rx6000` and Windows has `rdna2`, each with GPU code for gfx1030 and gfx1031 (RX 6800, 6900 and 6950 series, RX 6700 XT). They are offered to a PC whose card reports one of those two and to no other: the RX 7000, RX 9000 and Strix Halo packages are unchanged, and a PC with an RX 7000 or 9000 card next to an RX 6000 card keeps the package it had. The engine runs plain kernels on these cards (RDNA2 has no matrix cores, so none of the RX 7000 / RX 9000 or Radeon 780M prompt paths apply) and plain hipBLAS (hipBLASLt ships no kernels for gfx1030, so there is no tuning table). Setup and the app say once that this package was built and not tested on a real RX 6000 card. What was checked: the Linux package builds, every program in it has code for exactly gfx1030 and gfx1031, the rocBLAS kernel files for both are in it, every library it imports is in the package or belongs to the system, and the whole engine (all targets, tests included) compiles for gfx1030 and gfx1031. What was not: a model on an RX 6000 card, on either system, and the Windows package itself (the Windows build scripts take the new architecture list, but no Windows `rdna2` package has been built). Earlier reports on these cards came from people who built the engine from source ([docs/AMD_HIP.md](AMD_HIP.md#rdna2-gfx1030)). If something goes wrong, open an issue with the end of `strata-.log`. - **The CPU expert pool splits per NUMA node on a machine with two or more NUMA nodes (built from the report in #3201, not tested on a two-socket machine).** Until now the pool kept its workers on the host thread's socket, because workers on the other socket read the experts across the link and were slower. The split gives each socket its own slice of the pool with its own task counter, and places each socket's half of every CPU-side expert in that socket's memory, so a slice reads local memory only. The reporter measured +10 to +14 % decode on a dual Xeon Gold 6226 (59.3 against 54.05 tok/s with the IOMMU in passthrough); a second reporter's dual Xeon E5-2670 v2 preferred more workers across both sockets, which the split also uses. It starts by itself when the machine has two or more NUMA nodes with CPUs and runs only when it can (Linux, two nodes, pinned threads, enough free memory per node); otherwise the pool is exactly what it was and the log says why. **A machine with one NUMA node runs the same code path as 0.2.1:** the identity gate (10 prompts, exact_pp 4K and 20K) is byte-identical and the single-request speed A/B is not slower on the one-node test boxes (numbers in the notes of the branch). `STRATA_NUMA_POOL=0` turns the split off; `STRATA_NUMA_SPLIT=1` still asks for it on any machine. The start-up log says what it did: `NUMA pool on: 2 NUMA nodes found ...` or `NUMA pool off: `. On Windows the node count is read but the split itself is Linux-only for now, so a two-node Windows PC logs the reason and runs as before. The logic runs in unit tests on made-up two-, three- and four-node machines; we have no two-socket machine, so the placement and the speed on real ones are the reporters' numbers, not ours. Do not wrap the server in `numactl --cpunodebind` or `--membind`: it pulls both halves onto one node. Thanks to baptisterajaut for the branch this is built on, and to xrip and Tworks25 for their numbers. Please send the start-up lines and a decode run with and without `STRATA_NUMA_POOL=0` from a two-socket machine. - **The API key is optional.** The server, `strata launch`, the Terminal and the app no longer refuse to share without a key. Say so (`--no-api-key`, or `"api_key": "none"` in the server config) and it starts, with one plain warning that anyone who can reach the port can use the model and its tools; a public Cloudflare address gets a stronger one. The Terminal and the app still offer to make a key and do by default. In the app, Remove key now also works while local sharing or the tunnel runs. Fixes the refusals reported in #3198 and #3401. diff --git a/setup.py b/setup.py index 90850afe..a5527ebc 100644 --- a/setup.py +++ b/setup.py @@ -462,7 +462,7 @@ MODELS = { "download_gb": 58.5, "ram_gb": 27.6, "arena_gb": 25.8, "families": ("gyro",), "mem_total": True, "file": "Qwen3.8-Flash-Next-Gyro-S-TQ1_0.gguf", "shards": 1, "engine": GYRO_ENGINE, "vision": True}, "Gyro-M": {"about": "about IQ3_XXS quality with about 20% less GPU memory (35 GB of VRAM + RAM together); decodes " - "about 20% slower than Q2_0 on 12 GB cards", + "about 16-21% slower than Q2_0 on 12 GB cards", "download_gb": 92.0, "ram_gb": 35.0, "arena_gb": 32.1, "families": ("gyro",), "mem_total": True, "file": "Qwen3.8-Flash-Next-Gyro-M-TQ2_0.gguf", "shards": 1, "engine": GYRO_ENGINE, "vision": True}, "NVFP4": {"about": "best quality of the NVFP4 versions (GPTQ, sergqwer's strata-nvfp4 pack); RTX 50 has FP4 tensor cores, " diff --git a/tools/strata_mcp.py b/tools/strata_mcp.py index eab64915..424bb94e 100644 --- a/tools/strata_mcp.py +++ b/tools/strata_mcp.py @@ -83,7 +83,7 @@ FALLBACK_MODELS = { "download_gb": 58.5, "ram_gb": 27.6, "arena_gb": 25.8, "families": ("gyro",), "mem_total": True, "vision": True}, "Gyro-M": {"about": "about IQ3_XXS quality with about 20% less GPU memory (35 GB of VRAM + RAM together); decodes " - "about 20% slower than Q2_0 on 12 GB cards", + "about 16-21% slower than Q2_0 on 12 GB cards", "engine": (0, 1, 45, 1), "download_gb": 92.0, "ram_gb": 35.0, "arena_gb": 32.1, "families": ("gyro",), "mem_total": True, "vision": True},