Qwen3.8-Flash-Next on any consumer hardware: one-click install for Windows / Linux. Strata inference engine, OpenAI/Anthropic API on localhost, optional image input.
C++
GDN decode step and activation quantizes: bitwise faster paths (STRATA_GDN_SPLIT=1, opt-in STRATA_QFUSE=1)
Niko122122abb92dffef
diff --git a/include/strata/core/verify.hpp b/include/strata/core/verify.hppindex 34f9ca90c..a0c0dde25 100644--- a/include/strata/core/verify.hpp+++ b/include/strata/core/verify.hpp@@ -176,6 +176,7 @@ private:bool device_plan_ = false; ///< E-6: resident-only layers planned on the device (STRATA_VERIFY_DEVICE_PLAN)bool resident_ = false; ///< STRATA_VERIFY_RESIDENT: every expert resident, no host step per layeruint32_t* skip_ = nullptr; ///< E-6: per group, the ring whose plan the device built (0: the host's)+ unsigned* qcnt_ = nullptr; ///< S26 STRATA_QFUSE: the HC read's q8_1 group counters (n_embd / 32)unsigned long long* slot_off_d_ = nullptr; ///< E-6: the slot offsets on the deviceint64_t lb_ = 0, le_ = -1; ///< set_stage: the layers this verifier runs (-1: to the last)const float* hand_in_ = nullptr;diff --git a/include/strata/kernels/fused_gr.hpp b/include/strata/kernels/fused_gr.hppindex 427eae203..d56f21b33 100644--- a/include/strata/kernels/fused_gr.hpp+++ b/include/strata/kernels/fused_gr.hpp@@ -41,6 +41,10 @@ struct FusedGrArgs {const uint8_t* q8_down = nullptr; ///< Q8_0 [hc_lr][hc*n_embd]const uint8_t* q8_up = nullptr; ///< Q8_0 [hc*n_embd][hc_lr]const uint8_t* q8_inject = nullptr;///< Q8_0 [hc][hc*n_embd]+ /// S26 STRATA_QFUSE=1 (fused_gr_read_multi, the default and Q8_0 reads): also write `mixed`'s q8_1 image here (the+ /// bytes native_quantize_q8_1 would write); q8_cnt = n_embd / 32 zeroed counters owned by the caller (token 0's)+ uint8_t* q8_mixed = nullptr;+ unsigned* q8_cnt = nullptr;};bool fused_gr_supported(int64_t n_embd, int64_t hc, int64_t hc_lr);@@ -51,7 +55,8 @@ void fused_gr_read(const FusedGrArgs& a, void* stream);/// weight pointers and eps must be the same for every t); `xn_scratch` is n_tok * hc * n_embd floats. Every/// token's outputs are bitwise `fused_gr_read(a[t])`.constexpr int kFusedGrMaxT = 8;-void fused_gr_read_multi(const FusedGrArgs* a, int n_tok, float* xn_scratch, void* stream,+/// Returns true when it also wrote the q8_1 images (every token's q8_mixed set and this read supports it).+bool fused_gr_read_multi(const FusedGrArgs* a, int n_tok, float* xn_scratch, void* stream,unsigned long long* stamp_buf = nullptr, int stamp_i0 = 0);/// The multi read's variants (#315; not main's opt-in STRATA_GR_V3 read, which sums in another order), all computingdiff --git a/include/strata/kernels/verify_kernels.hpp b/include/strata/kernels/verify_kernels.hppindex 50d951a69..11f31b617 100644--- a/include/strata/kernels/verify_kernels.hpp+++ b/include/strata/kernels/verify_kernels.hpp@@ -34,7 +34,9 @@ void gdn_ab_multi(const float* x, const uint16_t* w_alpha, const uint16_t* w_bet/// *n_keep tokens are run and the state is written (commit; `y` may be scratch). Bitwise `fused_gdn_step_norm`.void gdn_step_norm_multi(float* state, const float* h, int conv_channels, const float* gate, const float* beta,const float* z, const float* gamma, float eps, float* y, int h_k, int h_v, int n_tok,- const int32_t* n_keep, void* stream, int t_out_begin = 0);+ const int32_t* n_keep, void* stream, int t_out_begin = 0, void* xq_out = nullptr);+/// (xq_out, S26 STRATA_QFUSE: also write the q8_1 image of output rows [t_out_begin, n_tok) there - the bytes+/// native_quantize_q8_1(y + t_out_begin * value_dim, xq_out, value_dim, n_tok - t_out_begin) would write.)/// Spin until *flag >= value (a mapped host flag). The value is fixed at capture, so several rings can be/// outstanding at once (the split verify window keeps two).void wait_flag_ge(const uint32_t* flag, uint32_t value, void* stream);diff --git a/src/core/verify.cpp b/src/core/verify.cppindex 10849d756..f690ccb19 100644--- a/src/core/verify.cpp+++ b/src/core/verify.cpp@@ -57,6 +57,8 @@ using Clock = std::chrono::steady_clock;double ms_since(Clock::time_point t) { return std::chrono::duration<double, std::milli>(Clock::now() - t).count(); }const bool g_dbg = std::getenv("STRATA_VERIFY_DEBUG") != nullptr;const bool g_qdedup = [] { const char* v = std::getenv("STRATA_VERIFY_QDEDUP"); return v != nullptr && std::atoi(v) != 0; }();+// S26 STRATA_QFUSE=1: activation q8_1 images written by their producers (the GDN output norm) - the same bytes+const bool g_qfuse = [] { const char* v = std::getenv("STRATA_QFUSE"); return v != nullptr && std::atoi(v) != 0; }();#define VDBG(...) do { if (g_dbg) { std::fprintf(stderr, "verify dbg: " __VA_ARGS__); std::fflush(stderr); } } while (0)struct Bump {@@ -176,6 +178,7 @@ Verifier::~Verifier() {for (auto& e : exec_)if (e) cudaGraphExecDestroy(e);if (commit_exec_) cudaGraphExecDestroy(commit_exec_);+ if (qcnt_) cudaFree(qcnt_);if (cs_) cudaStreamDestroy(cs_);if (copy_) { cudaStreamSynchronize(copy_); cudaStreamDestroy(copy_); }if (commit_done_) cudaEventDestroy(commit_done_);@@ -378,6 +381,14 @@ bool Verifier::init(const WeightTable& wt, const ModelGeometry& g, SessionState&: "not every expert is resident - the host-planned window stays");}}+ if (g_qfuse) { // S26: the HC read's q8_1 group counters, zeroed once (each launch leaves them zero)+ if (cudaMalloc((void**) &qcnt_, sizeof(unsigned) * (size_t) (g.n_embd / 32)) != cudaSuccess ||+ cudaMemset(qcnt_, 0, sizeof(unsigned) * (size_t) (g.n_embd / 32)) != cudaSuccess) {+ cudaGetLastError();+ if (qcnt_) cudaFree(qcnt_);+ qcnt_ = nullptr;+ }+ }if (device_plan_) {bool ok2 = cudaMalloc((void**) &skip_, 64) == cudaSuccess && cudaMemset(skip_, 0, 64) == cudaSuccess;if (ok2 && hits.slot_off != nullptr && hits.n_slots > 0) {@@ -520,11 +531,16 @@ bool Verifier::record_window(int T, cudaStream_t cs, std::string& err) {a.q8_inject = (const uint8_t*) wi[half]->hc_q8;a.eps = EPS; a.lo = lo_ + t * g.hc_lr; a.rs = rs_ + t * HC;a.inject_out = inj_out + t * HC; a.mixed = mixed_ + t * N;+ if (qcnt_ != nullptr) { // S26 STRATA_QFUSE: the consumer's q8_1 image written by the read itself+ a.q8_cnt = qcnt_;+ if (half == 0) a.q8_mixed = xq_ + (size_t) (t - tb) * (N / 32) * 36;+ else if (strata::kernels::cpu::expert_layout().native) a.q8_mixed = nat_xq_ + (size_t) t * (N / 32) * 36;+ }}- fused_gr_read_multi(fa, n, xn_ + (size_t) tb * HC * N, cs, (prof_on_ && grp == 0) ? prof_ : nullptr,+ return fused_gr_read_multi(fa, n, xn_ + (size_t) tb * HC * N, cs, (prof_on_ && grp == 0) ? prof_ : nullptr,(int) (l * kProfPer + (half == 0 ? 27 : 30)));};- gr_read_group(0, pending, inj2_, inj_);+ const bool q8_attn = gr_read_group(0, pending, inj2_, inj_); // true: xq_ holds mixed's q8_1 (STRATA_QFUSE)stamp(l, 1, grp);float* xm = mixed_ + tb * N;try {@@ -546,7 +562,7 @@ bool Verifier::record_window(int T, cudaStream_t cs, std::string& err) {float* hb = h_L_ + (size_t) gi * MT * C;float* gate = gate_L_ + (size_t) gi * MT * HV;float* beta = beta_L_ + (size_t) gi * MT * HV;- native_quantize_q8_1(xm, xq_, (int) N, n, cs);+ if (!q8_attn) native_quantize_q8_1(xm, xq_, (int) N, n, cs);native_mmvq(wqkv->native_type, wqkv->native_data, xq_, qkv + (size_t) tb * C, (int) N, (int) C, n, cs);stamp(l, 2, grp);gdn_conv_l2_multi(conv, qkv, (const float*) wc->data, hb, (int) C, (int) (2 * HK), EPS, n, cs, tb);@@ -559,9 +575,9 @@ bool Verifier::record_window(int T, cudaStream_t cs, std::string& err) {stamp(l, 5, grp);// the recurrence from the untouched state over tokens [0, te); outputs only for this group'sgdn_step_norm_multi(state, hb, (int) C, gate, beta, z_, (const float*) wnm->data, EPS, y_, (int) HK,- (int) HV, te, nullptr, cs, tb);+ (int) HV, te, nullptr, cs, tb, g_qfuse ? (void*) xq_ : nullptr);stamp(l, 6, grp);- native_quantize_q8_1(y_ + (size_t) tb * ZV, xq_, (int) ZV, n, cs);+ if (!g_qfuse) native_quantize_q8_1(y_ + (size_t) tb * ZV, xq_, (int) ZV, n, cs); // STRATA_QFUSE: done abovenative_mmvq(wout->native_type, wout->native_data, xq_, bo_ + tb * N, (int) ZV, (int) N, n, cs);} else {// ======================= QSA =======================@@ -586,7 +602,7 @@ bool Verifier::record_window(int T, cudaStream_t cs, std::string& err) {// the per-token GEMVs / norms / RoPEs / copies of this layer as one launch over the// window's rows each - row-wise identical arithmetic (STRATA_DEC_BATCH=0: token by token)const bool qb = dec_batch && n > 1 && native_qsa_enabled() && native_rope_enabled() && !st.kv_q4;- native_quantize_q8_1(xm, xq_, (int) N, n, cs);+ if (!q8_attn) native_quantize_q8_1(xm, xq_, (int) N, n, cs);if (qb) bf16_gemv_fp32_mmvf_multi(mixed_ + tb * N, N, (const uint16_t*) wik->data, idx_raw + tb * ID, ID, N, ID, n, cs);else for (int t = tb; t < te; ++t)bf16_gemv_fp32_mmvf(mixed_ + t * N, (const uint16_t*) wik->data, idx_raw + t * ID, (int) N, (int) ID, cs);@@ -689,7 +705,7 @@ bool Verifier::record_window(int T, cudaStream_t cs, std::string& err) {return false;}stamp(l, 16, grp);- gr_read_group(1, true, inj_, inj2_);+ const bool q8_ffn = gr_read_group(1, true, inj_, inj2_); // true: nat_xq_ holds the MoE input's q8_1// the window's rows routed in 2 launches (one router GEMV reading the weight once, one// top-10) instead of 2 per token; every row's arithmetic is the single-token call's (STRATA_DEC_BATCH=0: old)const WeightRef* w_router = v.get("ffn_gate_inp.weight");@@ -730,7 +746,7 @@ bool Verifier::record_window(int T, cudaStream_t cs, std::string& err) {// read it (quantize_q8_1_rows and native_quantize_q8_1 write the same bytes), and the BF16 copy of xm// is skipped when the shared expert's native BF16 gate (which never reads it) is on.const bool qdedup = g_qdedup && strata::kernels::cpu::expert_layout().native;- if (qdedup) quantize_q8_1_rows(xm, n, N, nat_xq_ + (size_t) tb * (N / 32) * 36, cs);+ if (qdedup && !q8_ffn) quantize_q8_1_rows(xm, n, N, nat_xq_ + (size_t) tb * (N / 32) * 36, cs);if (!(qdedup && shared_expert_native_bf16())) {if (dec_batch) f32_to_bf16_bulk(mixed_ + tb * N, sh_bf16_ + tb * N, (int64_t) n * N, cs); // contiguous rowselse for (int t = tb; t < te; ++t) f32_to_bf16_bulk(mixed_ + t * N, sh_bf16_ + t * N, N, cs);@@ -746,7 +762,7 @@ bool Verifier::record_window(int T, cudaStream_t cs, std::string& err) {}if (strata::kernels::cpu::expert_layout().native) {if (!(g_qdedup && strata::kernels::cpu::expert_layout().native))- quantize_q8_1_rows(xm, n, N, nat_xq_ + (size_t) tb * (N / 32) * 36, cs);+ if (!q8_ffn) quantize_q8_1_rows(xm, n, N, nat_xq_ + (size_t) tb * (N / 32) * 36, cs);} elsequantize_q8_0_scaled(xm, hit_xq_ + (size_t) tb * (N / 32) * 34, hit_xs_ + (size_t) tb * (N / 32), (int64_t) n * N, cs);stamp(l, 18, grp);@@ -1026,7 +1042,8 @@ bool Verifier::capture_commit(std::string& err) {gdn_conv_commit(conv, qkv, (int) C, commit_, cs_);gdn_step_norm_multi(state, h_L_ + (size_t) gdn_index * MT * C, (int) C, gate_L_ + (size_t) gdn_index * MT * HV,beta_L_ + (size_t) gdn_index * MT * HV, z_, (const float*) wnm->data, EPS, y_dummy_,- (int) g.ssm_k_heads, (int) HV, (int) MT, commit_, cs_);+ (int) g.ssm_k_heads, (int) HV, (int) MT, commit_, cs_,+ (int) MT); // S26: t_out_begin = MT - the replay's outputs (y_dummy_) are never read++gdn_index;} else {const QsaState& st = ss.qsa_states[qsa_index];diff --git a/src/kernels/cuda/fused_gr.cu b/src/kernels/cuda/fused_gr.cuindex d6515ac37..4546dd050 100644--- a/src/kernels/cuda/fused_gr.cu+++ b/src/kernels/cuda/fused_gr.cu@@ -145,6 +145,41 @@ struct GrMulti {int T;};++// S26 STRATA_QFUSE=1: the q8_1 image of `mixed`, written by the up kernels below. A q8_1 block is 32 columns and an+// up block owns UPM_COLS = 16, so the second of the two blocks that own a 32-column group (a per-group counter,+// incremented after the block's writes are fenced, reset by that block for the next launch) reads the 32 values back+// and quantizes them with native_quantize_q8_1_kernel's quantizer: one warp per token, the same XOR-tree max and sum,+// d = amax / 127, roundf(x / d), ds = (d, sum) - the bytes the separate launch writes.+struct GrQ81 { half2 ds; int8_t qs[32]; };+static_assert(2 * 16 == 32, "two up blocks per q8_1 group");+__device__ __forceinline__ void gr_q8_tail(const GrMulti& m, int d0) {+ __shared__ int last;+ __threadfence();+ __syncthreads();+ if (threadIdx.x == 0) {+ unsigned* c = m.a[0].q8_cnt + d0 / 32;+ last = atomicAdd(c, 1u) == 1u;+ if (last) atomicExch(c, 0u);+ }+ __syncthreads();+ if (!last) return;+ __threadfence();+ const int warp = threadIdx.x >> 5, lane = threadIdx.x & 31;+ if (warp >= m.T) return;+ const int c0 = (d0 / 32) * 32;+ const float xi = *reinterpret_cast<const volatile float*>(m.a[warp].mixed + c0 + lane);+ float amax = fabsf(xi), sum = xi;+#pragma unroll+ for (int o = 16; o > 0; o >>= 1) amax = fmaxf(amax, __shfl_xor_sync(0xffffffffu, amax, o));+#pragma unroll+ for (int o = 16; o > 0; o >>= 1) sum += __shfl_xor_sync(0xffffffffu, sum, o);+ const float d = amax / 127.0f;+ const int8_t q = amax == 0.0f ? 0 : roundf(xi / d);+ GrQ81* y = reinterpret_cast<GrQ81*>(m.a[warp].q8_mixed) + c0 / 32;+ y->qs[lane] = q;+ if (lane == 0) y->ds = make_half2(d, sum);+}// Step 1 of `gr_down_kernel`, one block per token, same threads and reduction order: rs[t] and xn[t] to global.__global__ void __launch_bounds__(THREADS) gr_norm_multi_kernel(GrMulti m) {__shared__ float part[WARPS][HC];@@ -306,6 +341,7 @@ __global__ void __launch_bounds__(THREADS) gr_up_multi_kernel(GrMulti m) {for (int c = 0; c < HC; ++c) s += g[k][c][col];m.a[k].mixed[d0 + col] = s / (float) HC;}+ if (m.a[0].q8_mixed != nullptr) gr_q8_tail(m, d0); // S26 STRATA_QFUSE}@@ -997,6 +1033,7 @@ __global__ void __launch_bounds__(THREADS) gr_up_q8_kernel(GrMulti m, const floafor (int c = 0; c < HC; ++c) sum += g[k][c][col];m.a[k].mixed[d0 + col] = sum / (float) HC;}+ if (m.a[0].q8_mixed != nullptr) gr_q8_tail(m, d0); // S26 STRATA_QFUSE}template <int T> void launch_q8_t(const GrMulti& m, float* part, float* ssg, cudaStream_t st, unsigned long long* stamp_buf,@@ -1023,7 +1060,7 @@ void launch_q8(const GrMulti& m, float* scratch, cudaStream_t st, unsigned long} // namespace-void fused_gr_read_multi(const FusedGrArgs* a, int n_tok, float* xn_scratch, void* stream, unsigned long long* stamp_buf,+bool fused_gr_read_multi(const FusedGrArgs* a, int n_tok, float* xn_scratch, void* stream, unsigned long long* stamp_buf,int stamp_i0) {if (n_tok < 1 || n_tok > kFusedGrMaxT || xn_scratch == nullptr) {std::fprintf(stderr, "fused_gr_read_multi: invalid arguments\n");@@ -1041,6 +1078,11 @@ void fused_gr_read_multi(const FusedGrArgs* a, int n_tok, float* xn_scratch, voistd::exit(1);}}+ // S26 STRATA_QFUSE: every token needs its q8_1 destination and token 0 the counters; otherwise none is written+ bool q8 = a[0].q8_cnt != nullptr;+ for (int t = 0; t < n_tok; ++t) q8 = q8 && a[t].q8_mixed != nullptr;+ if (!q8)+ for (int t = 0; t < n_tok; ++t) m.a[t].q8_mixed = nullptr;m.xn = xn_scratch;m.T = n_tok;cudaStream_t st = (cudaStream_t) stream;@@ -1051,7 +1093,7 @@ void fused_gr_read_multi(const FusedGrArgs* a, int n_tok, float* xn_scratch, voistd::fprintf(stderr, "fused_gr_read_multi q8: %s\n", cudaGetErrorString(eq));std::exit(1);}- return;+ return q8;}// STRATA_GR_V3=1: the two-kernel read above (another summation order - opt-in)static const bool v3 = [] { const char* v = std::getenv("STRATA_GR_V3"); return v != nullptr && std::atoi(v) != 0; }();@@ -1095,7 +1137,7 @@ void fused_gr_read_multi(const FusedGrArgs* a, int n_tok, float* xn_scratch, voistd::fprintf(stderr, "fused_gr_read_multi v3: %s\n", cudaGetErrorString(e3));std::exit(1);}- return;+ return false; // the v3 read: no q8_1 (the caller quantizes)}// the default read (STRATA_GR_V3 unset): v1, or the bitwise-equal v2 / v3 this card's check acceptedlaunch_multi(m, fused_gr_variant(), st, stamp_buf, stamp_i0);@@ -1104,6 +1146,7 @@ void fused_gr_read_multi(const FusedGrArgs* a, int n_tok, float* xn_scratch, voistd::fprintf(stderr, "fused_gr_read_multi: %s\n", cudaGetErrorString(e));std::exit(1);}+ return q8;}bool fused_gr_supported(int64_t n_embd, int64_t hc, int64_t hc_lr) {diff --git a/src/kernels/cuda/verify_kernels.cu b/src/kernels/cuda/verify_kernels.cuindex 97072de32..7637538b6 100644--- a/src/kernels/cuda/verify_kernels.cu+++ b/src/kernels/cuda/verify_kernels.cu@@ -5,6 +5,7 @@#include "strata/kernels/verify_kernels.hpp"#include "strata/kernels/dp4a.hpp"+#include <cuda_fp16.h>#include <cuda_runtime.h>#include <cstdio>@@ -112,6 +113,25 @@ __global__ void __launch_bounds__(256) gdn_ab_multi_kernel(const float* __restri}}+// S26 (STRATA_QFUSE=1): the q8_1 image of a 32-column block, written by the warp that holds those columns with+// native_quantize_q8_1_kernel's quantizer (the same XOR-tree max and sum, d = amax / 127, roundf(x / d), ds = (d, sum)).+struct GdnQ81 { half2 ds; int8_t qs[32]; };+__device__ __forceinline__ void gdn_q8_1_store(GdnQ81* __restrict__ xq, size_t idx, float xi) {+ // no contraction: xi is a product here, and fma(a, b, shfl) would round the first sum unlike the separate+ // quantizer, which reads xi from memory (S26 harness: ds.sum differed by 1 ulp without this)+#pragma clang fp contract(off)+ float amax = fabsf(xi), sum = xi;+#pragma unroll+ for (int o = 16; o > 0; o >>= 1) amax = fmaxf(amax, __shfl_xor_sync(0xffffffffu, amax, o));+#pragma unroll+ for (int o = 16; o > 0; o >>= 1) sum += __shfl_xor_sync(0xffffffffu, sum, o);+ const float d = amax / 127.0f;+ const int8_t q = amax == 0.0f ? 0 : roundf(xi / d);+ xq[idx / 32].qs[idx % 32] = q;+ if (idx % 32 == 0) xq[idx / 32].ds = make_half2(d, sum);+}++template<bool Q>__global__ void __launch_bounds__(S * RG) gdn_step_norm_multi_kernel(float* __restrict__ state,const float* __restrict__ hbuf, int C,const float* __restrict__ gate,@@ -119,7 +139,8 @@ __global__ void __launch_bounds__(S * RG) gdn_step_norm_multi_kernel(float* __reconst float* __restrict__ z,const float* __restrict__ gamma, float eps,float* __restrict__ y, int h_k, int h_v, int T,- const int32_t* __restrict__ n_keep, int t_out_begin) {+ const int32_t* __restrict__ n_keep, int t_out_begin,+ GdnQ81* __restrict__ xq) {__shared__ float sk[S], sq[S];__shared__ float red[RG][S];__shared__ float wsum[S * RG / 32];@@ -171,7 +192,13 @@ __global__ void __launch_bounds__(S * RG) gdn_step_norm_multi_kernel(float* __reconst float ss = wsum[0] + wsum[1] + wsum[2] + wsum[3];const float scale = rsqrtf(ss / (float) S + eps);const float zz = z[(size_t) t * value_dim + head * S + col];- y[(size_t) t * value_dim + head * S + col] = oc * scale * gamma[col] * (1.0f / (1.0f + __expf(-zz)));+ if constexpr (Q) {+ const float yv = oc * scale * gamma[col] * (1.0f / (1.0f + __expf(-zz)));+ y[(size_t) t * value_dim + head * S + col] = yv;+ gdn_q8_1_store(xq, (size_t) (t - t_out_begin) * value_dim + head * S + col, yv);+ } else {+ y[(size_t) t * value_dim + head * S + col] = oc * scale * gamma[col] * (1.0f / (1.0f + __expf(-zz)));+ }}}if (n_keep != nullptr && n > 0) {@@ -186,7 +213,11 @@ __global__ void __launch_bounds__(S * RG) gdn_step_norm_multi_kernel(float* __re// at ~60 GB/s. Every column's arithmetic is the original's; the output norm, which needs all 128 columns of a head,// moves to gdn_out_norm_kernel: this kernel leaves the unnormalized output `oc` in y and the second kernel sums the// squares by the same warps (columns 32w..32w+31, the same butterfly) in the same order.+// S26: OUT = false (the commit, whose outputs nobody reads) drops the output reduction and its two barriers; the+// norm kernel below has the old kernel's code shape (S x RG threads, rg 0 holding oc), which makes it bitwise equal -+// the 128-thread version differed from the old kernel by 1 ulp in ~7% of the outputs.constexpr int GS_COLS = 32;+template<bool OUT>__global__ void __launch_bounds__(GS_COLS * RG) gdn_step_split_kernel(float* __restrict__ state,const float* __restrict__ hbuf, int C,const float* __restrict__ gate,@@ -230,6 +261,7 @@ __global__ void __launch_bounds__(GS_COLS * RG) gdn_step_split_kernel(float* __rs[r] = fmaf(g, s[r], sk[rg * RPG + r] * delta);o = fmaf(s[r], sq[rg * RPG + r], o);}+ if (!OUT) continue;__syncthreads();red[rg][lc] = o;__syncthreads();@@ -243,24 +275,108 @@ __global__ void __launch_bounds__(GS_COLS * RG) gdn_step_split_kernel(float* __r}}-// y = oc * rsqrt(mean(oc^2) + eps) * gamma * sigmoid(z) per head and token, with gdn_step_norm_multi_kernel's sums-__global__ void __launch_bounds__(S) gdn_out_norm_kernel(const float* __restrict__ z, const float* __restrict__ gamma,- float eps, float* __restrict__ y, int h_v, int T,- const int32_t* __restrict__ n_keep, int t_out_begin) {- __shared__ float wsum[S / 32];- const int head = blockIdx.x, t = blockIdx.y + t_out_begin, col = threadIdx.x;++// S26: the state-only replay (the commit) with its per-token inputs prefetched - gate, beta and the v column of every+// row up front, the next row's k / q while the current one computes. The arithmetic and its order are+// gdn_step_split_kernel<false>'s (gfx1151 harness: state bitwise equal; 44.5 -> 39.1 us at 1 row, 47.4 -> 40.5 at 2,+// 53.8 -> 42.9 at 4, h_v 48). With outputs the prefetch lost (register pressure), so the window keeps the plain split.+template<int MAXT>+__global__ void __launch_bounds__(GS_COLS * RG) gdn_state_replay_kernel(float* __restrict__ state,+ const float* __restrict__ hbuf, int C,+ const float* __restrict__ gate,+ const float* __restrict__ beta, int h_k,+ int h_v, int T, const int32_t* __restrict__ n_keep) {+ __shared__ float sk[S], sq[S];+ __shared__ float red[RG][GS_COLS];+ const int head = blockIdx.x;+ const int c0 = blockIdx.y * GS_COLS;+ const int lc = threadIdx.x;+ const int col = c0 + lc;+ const int rg = threadIdx.y;+ const int tid = rg * GS_COLS + lc;+ const int qh = head % h_k;+ const int qk = S * h_k;+ const int n = n_keep ? *n_keep : T;+ float gv[MAXT], bv[MAXT], vv[MAXT];+#pragma unroll+ for (int t = 0; t < MAXT; ++t) {+ if (t < n) {+ gv[t] = gate[(size_t) t * h_v + head];+ bv[t] = beta[(size_t) t * h_v + head];+ vv[t] = hbuf[(size_t) t * C + 2 * qk + head * S + col];+ }+ }+ float kn = 0.0f, qn = 0.0f;+ if (n > 0) { kn = hbuf[qk + qh * S + tid]; qn = hbuf[qh * S + tid]; }+ float s[RPG];+ float* base = state + ((size_t) (rg * RPG) * h_v + head) * S + col;+ const size_t row_stride = (size_t) h_v * S;+#pragma unroll+ for (int r = 0; r < RPG; ++r) s[r] = base[r * row_stride];+#pragma unroll+ for (int t = 0; t < MAXT; ++t) {+ if (t >= n) break;+ __syncthreads();+ sk[tid] = kn; sq[tid] = qn;+ __syncthreads();+ if (t + 1 < n) {+ const float* hn = hbuf + (size_t) (t + 1) * C;+ kn = hn[qk + qh * S + tid]; qn = hn[qh * S + tid];+ }+ const float g = __expf(gv[t]);+ float kv = 0.0f;+#pragma unroll+ for (int r = 0; r < RPG; ++r) kv = fmaf(s[r], sk[rg * RPG + r], kv);+ red[rg][lc] = kv;+ __syncthreads();+ const float kv_col = red[0][lc] + red[1][lc] + red[2][lc] + red[3][lc];+ const float delta = (vv[t] - g * kv_col) * bv[t];+ float o = 0.0f; // unused: kept because the measured code had it (without it: 51 us instead of 39 at 1 row)+#pragma unroll+ for (int r = 0; r < RPG; ++r) {+ s[r] = fmaf(g, s[r], sk[rg * RPG + r] * delta);+ o = fmaf(s[r], sq[rg * RPG + r], o);+ }+ (void) o;+ }+ if (n_keep != nullptr && n > 0) {+#pragma unroll+ for (int r = 0; r < RPG; ++r) base[r * row_stride] = s[r];+ }+}+// y = oc * rsqrt(mean(oc^2) + eps) * gamma * sigmoid(z) per head and token: gdn_step_norm_multi_kernel's tail with its+// code shape (S x RG threads; rg 0 holds oc, the other row groups contribute 0 to wsum), reading oc from y+template<bool Q>+__global__ void __launch_bounds__(S * RG) gdn_out_norm_kernel(const float* __restrict__ z, const float* __restrict__ gamma,+ float eps, float* __restrict__ y, int h_v, int T,+ const int32_t* __restrict__ n_keep, int t_out_begin,+ GdnQ81* __restrict__ xq) {+ __shared__ float wsum[S * RG / 32];+ const int head = blockIdx.x, t = blockIdx.y + t_out_begin, col = threadIdx.x, rg = threadIdx.y;+ const int tid = rg * S + col;const int n = n_keep ? *n_keep : T;if (t >= n) return;const int value_dim = S * h_v;- const float oc = y[(size_t) t * value_dim + head * S + col];- float sq_part = oc * oc;+ float oc = 0.0f, sq_part = 0.0f;+ if (rg == 0) {+ oc = y[(size_t) t * value_dim + head * S + col];+ sq_part = oc * oc;+ }for (int o2 = 16; o2 > 0; o2 >>= 1) sq_part += __shfl_xor_sync(0xffffffffu, sq_part, o2);- if ((col & 31) == 0) wsum[col >> 5] = sq_part;+ if ((tid & 31) == 0) wsum[tid >> 5] = sq_part;__syncthreads();- const float ss = wsum[0] + wsum[1] + wsum[2] + wsum[3];- const float scale = rsqrtf(ss / (float) S + eps);- const float zz = z[(size_t) t * value_dim + head * S + col];- y[(size_t) t * value_dim + head * S + col] = oc * scale * gamma[col] * (1.0f / (1.0f + __expf(-zz)));+ if (rg == 0) {+ const float ss = wsum[0] + wsum[1] + wsum[2] + wsum[3];+ const float scale = rsqrtf(ss / (float) S + eps);+ const float zz = z[(size_t) t * value_dim + head * S + col];+ if constexpr (Q) {+ const float yv = oc * scale * gamma[col] * (1.0f / (1.0f + __expf(-zz)));+ y[(size_t) t * value_dim + head * S + col] = yv;+ gdn_q8_1_store(xq, (size_t) (t - t_out_begin) * value_dim + head * S + col, yv);+ } else {+ y[(size_t) t * value_dim + head * S + col] = oc * scale * gamma[col] * (1.0f / (1.0f + __expf(-zz)));+ }+ }}__global__ void embedding_gather_dev_kernel(const uint8_t* __restrict__ codes, const float* __restrict__ scales,@@ -494,24 +610,37 @@ void gdn_ab_multi(const float* x, const uint16_t* w_alpha, const uint16_t* w_betvoid gdn_step_norm_multi(float* state, const float* h, int conv_channels, const float* gate, const float* beta,const float* z, const float* gamma, float eps, float* y, int h_k, int h_v, int n_tok,- const int32_t* n_keep, void* stream, int t_out_begin) {+ const int32_t* n_keep, void* stream, int t_out_begin, void* xq_out) {+ GdnQ81* const xq = static_cast<GdnQ81*>(xq_out); // S26: non-null = also the outputs' q8_1 (STRATA_QFUSE)if (!state || !h || !gate || !beta || !z || !gamma || !y || h_k <= 0 || h_v % h_k || n_tok < 1 ||n_tok > kVerifyMaxT) {std::fprintf(stderr, "gdn_step_norm_multi: invalid arguments\n");std::exit(1);}static const bool split = [] { const char* v = std::getenv("STRATA_GDN_SPLIT"); return v && v[0] == '1'; }();- if (split) {- gdn_step_split_kernel<<<dim3((unsigned) h_v, S / GS_COLS), dim3(GS_COLS, RG), 0, (cudaStream_t) stream>>>(- state, h, conv_channels, gate, beta, y, h_k, h_v, n_tok, n_keep, t_out_begin);- if (n_tok > t_out_begin)- gdn_out_norm_kernel<<<dim3((unsigned) h_v, (unsigned) (n_tok - t_out_begin)), S, 0, (cudaStream_t) stream>>>(- z, gamma, eps, y, h_v, n_tok, n_keep, t_out_begin);+ // S26: the split runs where it is faster (gfx1151 harness, h_v 48: the commit 8-19% per call; a window of 2+ rows+ // 3-13%; one row: no gain, the old kernel stays) - bitwise equal in every case+ if (split && (t_out_begin >= n_tok || n_tok - t_out_begin >= 2)) {+ const dim3 grid((unsigned) h_v, S / GS_COLS), block(GS_COLS, RG);+ if (t_out_begin >= n_tok) { // no outputs (the commit): the state only+ gdn_state_replay_kernel<kVerifyMaxT><<<grid, block, 0, (cudaStream_t) stream>>>(+ state, h, conv_channels, gate, beta, h_k, h_v, n_tok, n_keep);+ } else {+ gdn_step_split_kernel<true><<<grid, block, 0, (cudaStream_t) stream>>>(+ state, h, conv_channels, gate, beta, y, h_k, h_v, n_tok, n_keep, t_out_begin);+ const dim3 ng((unsigned) h_v, (unsigned) (n_tok - t_out_begin));+ if (xq) gdn_out_norm_kernel<true><<<ng, dim3(S, RG), 0, (cudaStream_t) stream>>>(z, gamma, eps, y, h_v, n_tok, n_keep, t_out_begin, xq);+ else gdn_out_norm_kernel<false><<<ng, dim3(S, RG), 0, (cudaStream_t) stream>>>(z, gamma, eps, y, h_v, n_tok, n_keep, t_out_begin, nullptr);+ }check("gdn_step_norm_multi (split)");return;}- gdn_step_norm_multi_kernel<<<(unsigned) h_v, dim3(S, RG), 0, (cudaStream_t) stream>>>(- state, h, conv_channels, gate, beta, z, gamma, eps, y, h_k, h_v, n_tok, n_keep, t_out_begin);+ if (xq && t_out_begin < n_tok)+ gdn_step_norm_multi_kernel<true><<<(unsigned) h_v, dim3(S, RG), 0, (cudaStream_t) stream>>>(+ state, h, conv_channels, gate, beta, z, gamma, eps, y, h_k, h_v, n_tok, n_keep, t_out_begin, xq);+ else+ gdn_step_norm_multi_kernel<false><<<(unsigned) h_v, dim3(S, RG), 0, (cudaStream_t) stream>>>(+ state, h, conv_channels, gate, beta, z, gamma, eps, y, h_k, h_v, n_tok, n_keep, t_out_begin, nullptr);check("gdn_step_norm_multi");}diff --git a/src/kernels/gr_parity.cpp b/src/kernels/gr_parity.cppindex b5ceb76c5..ce1196786 100644--- a/src/kernels/gr_parity.cpp+++ b/src/kernels/gr_parity.cpp@@ -21,6 +21,7 @@// Asserted as a property, not as a value, because that is what the source comment claims.#include "strata/kernels/gr.hpp"#include "strata/kernels/fused_gr.hpp"+#include "strata/kernels/native_mmvq.hpp"#include <cuda_runtime.h>@@ -405,6 +406,54 @@ int fused_multi_lds_parity(const float* d_norm, const uint16_t* d_down, const ui++bad;}++ // S26 STRATA_QFUSE: the read's own q8_1 image of `mixed` must be the bytes native_quantize_q8_1 writes from it -+ // for every T (1..8), directly and through a captured graph replayed twice (the group counters must reset)+ {+ uint8_t *d_q = nullptr, *d_ref = nullptr;+ unsigned* d_cnt = nullptr;+ const size_t qbytes = (size_t) T * (N / 32) * 36;+ check(cudaMalloc(&d_q, qbytes), "qfuse q8");+ check(cudaMalloc(&d_ref, qbytes), "qfuse ref");+ check(cudaMalloc(&d_cnt, (N / 32) * sizeof(unsigned)), "qfuse counters");+ check(cudaMemset(d_cnt, 0, (N / 32) * sizeof(unsigned)), "qfuse counters zero");+ int qbad = 0;+ std::vector<uint8_t> hq(qbytes), hr(qbytes);+ for (int tt = 1; tt <= T && !qbad; ++tt) {+ std::vector<FusedGrArgs> qa(args.begin(), args.begin() + tt);+ for (int t = 0; t < tt; ++t) { qa[t].q8_mixed = d_q + (size_t) t * (N / 32) * 36; qa[t].q8_cnt = d_cnt; }+ for (int rep = 0; rep < 3 && !qbad; ++rep) {+ check(cudaMemsetAsync(d_q, 0x5a, qbytes, stream), "qfuse poison");+ bool wrote = false;+ cudaGraph_t qg = nullptr;+ cudaGraphExec_t qx = nullptr;+ if (rep == 0) {+ wrote = fused_gr_read_multi(qa.data(), tt, d_xn, stream);+ } else { // a captured read, replayed (twice: rep 1 and 2 use fresh captures, each replayed twice)+ check(cudaStreamBeginCapture(stream, cudaStreamCaptureModeThreadLocal), "qfuse begin");+ wrote = fused_gr_read_multi(qa.data(), tt, d_xn, stream);+ check(cudaStreamEndCapture(stream, &qg), "qfuse end");+ check(cudaGraphInstantiate(&qx, qg, nullptr, nullptr, 0), "qfuse instantiate");+ check(cudaGraphLaunch(qx, stream), "qfuse replay 1");+ check(cudaMemsetAsync(d_q, 0x5a, qbytes, stream), "qfuse poison 2");+ check(cudaGraphLaunch(qx, stream), "qfuse replay 2");+ }+ strata::kernels::native_quantize_q8_1(d_mixed, d_ref, N, tt, stream);+ check(cudaStreamSynchronize(stream), "qfuse sync");+ if (qx) { cudaGraphExecDestroy(qx); cudaGraphDestroy(qg); }+ if (v3) { std::printf(" QFUSE: the v3 read writes no q8_1 (%s)\n", wrote ? "WRONG: it says it did" : "ok"); qbad += wrote; break; }+ check(cudaMemcpy(hq.data(), d_q, (size_t) tt * (N / 32) * 36, cudaMemcpyDeviceToHost), "qfuse read");+ check(cudaMemcpy(hr.data(), d_ref, (size_t) tt * (N / 32) * 36, cudaMemcpyDeviceToHost), "qfuse ref read");+ if (!wrote || std::memcmp(hq.data(), hr.data(), (size_t) tt * (N / 32) * 36) != 0) {+ std::printf(" QFUSE: T=%d rep %d: %s\n", tt, rep, wrote ? "q8_1 bytes differ" : "not written");+ ++qbad;+ }+ }+ }+ std::printf(" fused GR read + q8_1 (STRATA_QFUSE), T 1..8, direct and graph replays: %s\n", qbad ? "FAIL" : "pass");+ bad += qbad;+ cudaFree(d_q); cudaFree(d_ref); cudaFree(d_cnt);+ }std::printf(" fused GR multi max-T=8 LDS launch and changing graph replay %s\n",bad == 0 ? "pass" : "FAIL");check(cudaGraphExecDestroy(graph_exec), "multi graph exec destroy");