From 58729711d952149196a9c64fc789043fdfa1ffbc Mon Sep 17 00:00:00 2001 From: Niko1221 Date: Sat, 3 Oct 2026 06:00:25 +0200 Subject: [PATCH 1/3] STRATA_HC_Q8=1: inject rows as the exact BF16, the final mixer from Q8_0 too The GGUF's F32 inject rows hold BF16-exact values (conversions.json: "the values are BF16's"), so quantizing them to Q8_0 only added error: the Q8_0 read now takes the pack's BF16 inject rows (STRATA_HC_Q8_INJECT=1 keeps the Q8_0 copy). The final mixer before the head (output_hc_down/up, also BF16-rounded in the pack) now reads the GGUF's Q8_0 values through the same multi read. --- src/core/native_dense.cpp | 6 +++++- src/core/verify.cpp | 18 +++++++++++++++++- src/kernels/cuda/fused_gr.cu | 18 ++++++++++++++---- 3 files changed, 36 insertions(+), 6 deletions(-) diff --git a/src/core/native_dense.cpp b/src/core/native_dense.cpp index aae66aec..62a42db7 100644 --- a/src/core/native_dense.cpp +++ b/src/core/native_dense.cpp @@ -24,6 +24,7 @@ bool hc_q8_requested() { return on; } bool hc_q8_name(const std::string& name) { + if (name == "output_hc_down.weight" || name == "output_hc_up.weight") return true; // S25: the final mixer too if (name.rfind("blk.", 0) != 0) return false; static const char* suffixes[] = {".hc_attn_down.weight", ".hc_attn_up.weight", ".hc_attn_inject.weight", ".hc_ffn_down.weight", ".hc_ffn_up.weight", ".hc_ffn_inject.weight"}; @@ -225,7 +226,10 @@ bool NativeDense::load(const std::vector& shards, WeightTable& tabl } if (hc_q8_requested()) for (const auto& tensor : gguf.tensors()) { - const bool f32_inject = tensor.type == 0 && tensor.name.ends_with("_inject.weight"); + // S25: the F32 inject rows hold BF16-exact values: the read takes the pack's BF16 rows unless + // STRATA_HC_Q8_INJECT=1 asks for a Q8_0 copy + static const bool q8_inject = [] { const char* v = std::getenv("STRATA_HC_Q8_INJECT"); return v && v[0] == '1'; }(); + const bool f32_inject = q8_inject && tensor.type == 0 && tensor.name.ends_with("_inject.weight"); if ((tensor.type != 8 && !f32_inject) || !hc_q8_name(tensor.name) || tensor.shape.size() != 2) continue; auto found = table.table_.find(tensor.name); if (found == table.table_.end() || found->second.hc_q8 != nullptr) continue; diff --git a/src/core/verify.cpp b/src/core/verify.cpp index e631ee74..57a0a2da 100644 --- a/src/core/verify.cpp +++ b/src/core/verify.cpp @@ -829,7 +829,23 @@ bool Verifier::record_window(int T, cudaStream_t cs, std::string& err) { const WeightRef *hn = wt.find("output_hc_norm.weight"), *hd = wt.find("output_hc_down.weight"), *hu = wt.find("output_hc_up.weight"); if (!hn || !hd || !hu) { err = "verify: an output_hc_* weight is missing"; return false; } - for (int t = 0; t < T; ++t) { + // S25 (STRATA_HC_Q8=1): the final mixer from the GGUF's Q8_0 projections too, through the same multi read + // (no pending write, no inject) + const bool mix_q8 = hd->hc_q8 != nullptr && hu->hc_q8 != nullptr && head_ != nullptr && head_->loaded(); + if (mix_q8) { + FusedGrArgs fa[kFusedGrMaxT]; + for (int t = 0; t < T; ++t) { + FusedGrArgs& a = fa[t]; + a.R = Rt(t); a.R_out = Rt(t); a.apply = false; + a.w_norm = (const float*) hn->data; a.w_down = (const uint16_t*) hd->data; + a.w_up = (const uint16_t*) hu->data; a.w_inject = nullptr; + a.q8_down = (const uint8_t*) hd->hc_q8; a.q8_up = (const uint8_t*) hu->hc_q8; + a.eps = EPS; a.lo = lo_ + t * g.hc_lr; a.rs = rs_ + t * HC; + a.mixed = head_mixed_ + t * N; + } + fused_gr_read_multi(fa, T, xn_, cs); + } + for (int t = 0; t < T && !mix_q8; ++t) { BlockBuffers bb = ss.block; bb.R = Rt(t); bb.mixed = head_mixed_ + t * N; diff --git a/src/kernels/cuda/fused_gr.cu b/src/kernels/cuda/fused_gr.cu index d6515ac3..ec7ddb56 100644 --- a/src/kernels/cuda/fused_gr.cu +++ b/src/kernels/cuda/fused_gr.cu @@ -827,7 +827,10 @@ __global__ void __launch_bounds__(THREADS) gr_down_q8_kernel(GrMulti m, float* _ const int t = threadIdx.x, lane = t & 31, warp = t >> 5; const int rg = blockIdx.x, kc = blockIdx.y, c = kc / Q8_CPS; const bool inj = rg == Q8_RG; - const int nrows = inj ? ((m.a[0].q8_inject != nullptr && warp < HC) ? 1 : 0) : Q8_RPW; + // S25: the inject rows are F32 in the GGUF with BF16-exact values, so by default they are read as the pack's BF16 + // (exact); a Q8_0 copy (STRATA_HC_Q8_INJECT=1) is the other option + const bool inj_bf16 = inj && m.a[0].q8_inject == nullptr; + const int nrows = inj ? (((m.a[0].q8_inject != nullptr || m.a[0].w_inject != nullptr) && warp < HC) ? 1 : 0) : Q8_RPW; const int row0 = inj ? warp : (rg * WARPS + warp) * Q8_RPW; const uint8_t* wb = inj ? m.a[0].q8_inject : m.a[0].q8_down; const int sub = lane & 7, bl = lane >> 3; // lanes 8b..8b+7: one Q8_0 block, 4 values each @@ -835,7 +838,7 @@ __global__ void __launch_bounds__(THREADS) gr_down_q8_kernel(GrMulti m, float* _ float dq[Q8_RPW][Q8_SPB]; #pragma unroll for (int r = 0; r < Q8_RPW; ++r) { - if (r >= nrows) break; + if (r >= nrows || inj_bf16) break; const uint8_t* row = wb + (size_t) (row0 + r) * (D / 32) * Q8B + (size_t) kc * (Q8_KC / 32) * Q8B; #pragma unroll for (int s = 0; s < Q8_SPB; ++s) { @@ -883,8 +886,15 @@ __global__ void __launch_bounds__(THREADS) gr_down_q8_kernel(GrMulti m, float* _ for (int k = 0; k < T; ++k) acc[k] = 0.0f; #pragma unroll for (int s = 0; s < Q8_SPB; ++s) { - const float4 w = q8_four(q[r][s], dq[r][s]); const int v = 32 * (4 * s + bl) + 4 * sub; + float4 w; + if (inj_bf16) { + const uint2 b = *reinterpret_cast(m.a[0].w_inject + (size_t) row0 * D + (size_t) kc * Q8_KC + v); + w = make_float4(__uint_as_float(b.x << 16), __uint_as_float(b.x & 0xffff0000u), + __uint_as_float(b.y << 16), __uint_as_float(b.y & 0xffff0000u)); + } else { + w = q8_four(q[r][s], dq[r][s]); + } #pragma unroll for (int k = 0; k < T; ++k) { const float4 x = *reinterpret_cast(&xs[k][v]); @@ -1044,7 +1054,7 @@ void fused_gr_read_multi(const FusedGrArgs* a, int n_tok, float* xn_scratch, voi m.xn = xn_scratch; m.T = n_tok; cudaStream_t st = (cudaStream_t) stream; - if (a[0].q8_down != nullptr && a[0].q8_up != nullptr && (a[0].w_inject == nullptr || a[0].q8_inject != nullptr)) { + if (a[0].q8_down != nullptr && a[0].q8_up != nullptr) { // inject: Q8_0 copy, or the BF16 rows launch_q8(m, xn_scratch, st, stamp_buf, stamp_i0); // S23 experiment: STRATA_HC_Q8=1 const cudaError_t eq = cudaGetLastError(); if (eq != cudaSuccess) { From d92c9feb78ce3082b5f1df834e9001117908f508 Mon Sep 17 00:00:00 2001 From: Niko1221 Date: Sat, 3 Oct 2026 06:59:29 +0200 Subject: [PATCH 2/3] Verify PLE block: the window's key and value projections at once (opt-in STRATA_PLE_BATCH=1, bit-identical) Layer 1's PLE block ran its key (10240 x 2560) and value (2560 x 2560) GEMVs once per window row, reading ~40 MB of weights per row. Both depend only on the row's n-gram embedding, so with the switch the group's rows go through the multi-row forms of the same kernels (native MMVQ over n columns / bf16_gemv_fp32_mmvf_multi, each row's arithmetic the single-row call's) and ple_block copies its row's results instead of recomputing them. The history-dependent rest of the block stays row by row. --- include/strata/core/verify.hpp | 2 ++ include/strata/kernels/ple.hpp | 6 ++++++ src/core/verify.cpp | 27 ++++++++++++++++++++++++++- src/kernels/cuda/ple.cu | 9 +++++++-- 4 files changed, 41 insertions(+), 3 deletions(-) diff --git a/include/strata/core/verify.hpp b/include/strata/core/verify.hpp index 5717eb4f..6e0bf4ec 100644 --- a/include/strata/core/verify.hpp +++ b/include/strata/core/verify.hpp @@ -198,6 +198,8 @@ private: VerifyHits hits_; const NativeHead* head_ = nullptr; int max_t_ = 0; + float* ple_key_ = nullptr; ///< STRATA_PLE_BATCH: the window rows' PLE key / value projections + float* ple_val_ = nullptr; int last_t_ = 0; int64_t last_pos0_ = 0; int32_t last_tokens_[8] = {}; diff --git a/include/strata/kernels/ple.hpp b/include/strata/kernels/ple.hpp index fabad838..30afb01d 100644 --- a/include/strata/kernels/ple.hpp +++ b/include/strata/kernels/ple.hpp @@ -33,6 +33,7 @@ namespace strata::kernels { /// Opt in to pinned CUDA BF16/F32 MMVF for the PLE value projection only (default false). /// Configure before session capture; captured graphs retain their selected projection kernels. void ple_set_native_bf16(bool enabled); +bool ple_native_bf16(); /// Opt in to pinned CUDA postprojection norms, gate, convolution and residual /// arithmetic (default false). Set before capture; existing graphs keep their @@ -70,6 +71,11 @@ struct PleWeights { /// Plan v0.3 P6: the IQ model files keep `ple_key` in BF16 ([n_embd, hc_dim], `w[o*n_embd+i]`); when set it /// replaces both key paths above. const uint16_t* key_bf16 = nullptr; + /// S25 (STRATA_PLE_BATCH=1): the key (hc_dim) and value (n_embd) projections of `emb`, already computed by the + /// caller for a window's rows at once (the same kernels' multi-row forms); ple_block then copies them instead of + /// running its own GEMVs + const float* pre_key = nullptr; + const float* pre_value = nullptr; }; /// Everything the block produces, in the order `ple_layer_xcheck`'s oracle writes it. Any pointer may be diff --git a/src/core/verify.cpp b/src/core/verify.cpp index 57a0a2da..812074d7 100644 --- a/src/core/verify.cpp +++ b/src/core/verify.cpp @@ -320,6 +320,7 @@ bool Verifier::init(const WeightTable& wt, const ModelGeometry& g, SessionState& sh_up_ = b.take(T * (uint64_t) g.n_ff); sh_g_ = b.take(T + 4); head_logits_ = b.take(T * (uint64_t) n_vocab_); hist_snap_ = b.take(T * HS); + ple_key_ = b.take(T * (uint64_t) strata::kernels::NG_HC_DIM); ple_val_ = b.take(T * N); }; Bump count; carve(count); @@ -472,13 +473,37 @@ bool Verifier::record_window(int T, cudaStream_t cs, std::string& err) { bool pending = l > 0 && !cvec().covers(l - 1); if (l == 1 && ple_on) { float* normalized = (float*) ((uint8_t*) ss.ple.scratch + ple_block_scratch_bytes()); + // S25 (STRATA_PLE_BATCH=1): the key and value projections depend only on each row's n-gram embedding, so + // the group's rows run through the multi-row forms of the same kernels (the weights read once); every + // row's arithmetic is the single-row call's. The history-dependent rest stays row by row. + static const bool ple_batch = [] { const char* e = std::getenv("STRATA_PLE_BATCH"); return e && e[0] == '1'; }(); + PleWeights pw = ss.ple.w; + const bool nkey = pw.key_native_data != nullptr && pw.key_bf16 == nullptr; + const bool batched = ple_batch && n > 1 && (pw.key_bf16 != nullptr || nkey) && ple_native_bf16(); + if (batched) { + try { + if (pw.key_bf16 != nullptr) + bf16_gemv_fp32_mmvf_multi(ple_ + tb * N, N, pw.key_bf16, ple_key_ + (size_t) tb * NG_HC_DIM, + NG_HC_DIM, N, NG_HC_DIM, n, cs); + else { + native_quantize_q8_1(ple_ + tb * N, xq_, (int) N, n, cs); + native_mmvq(pw.key_native_type, pw.key_native_data, xq_, ple_key_ + (size_t) tb * NG_HC_DIM, + (int) N, NG_HC_DIM, n, cs); + } + bf16_gemv_fp32_mmvf_multi(ple_ + tb * N, N, pw.value_bf16, ple_val_ + tb * N, N, N, N, n, cs); + } catch (const std::exception& e) { + err = std::string("verify PLE batch: ") + e.what(); + return false; + } + } for (int t = tb; t < te; ++t) { + if (batched) { pw.pre_key = ple_key_ + (size_t) t * NG_HC_DIM; pw.pre_value = ple_val_ + t * N; } gr_write(Rt(t), bo_ + t * N, inj2_ + t * HC, gs, Rt(t), cs); PleOut po; po.normalized = normalized; po.result = Rt(t); try { - ple_block(ple_ + t * N, Rt(t), ss.ple.hist, ss.ple.w, po, ss.ple.scratch, cs); + ple_block(ple_ + t * N, Rt(t), ss.ple.hist, pw, po, ss.ple.scratch, cs); ple_history_advance(ss.ple.hist, normalized, cs); } catch (const std::exception& e) { err = std::string("verify PLE: ") + e.what(); diff --git a/src/kernels/cuda/ple.cu b/src/kernels/cuda/ple.cu index 6585bbaa..3f37ab7a 100644 --- a/src/kernels/cuda/ple.cu +++ b/src/kernels/cuda/ple.cu @@ -197,6 +197,7 @@ void ck(cudaError_t e, const char* what) { } // namespace void ple_set_native_bf16(bool enabled) { native_bf16 = enabled; } +bool ple_native_bf16() { return native_bf16; } void ple_set_native_postops(bool enabled) { native_postops = enabled; } bool ple_native_postops_enabled() { return native_postops; } @@ -303,7 +304,9 @@ void ple_block(const float* emb, const float* hidden, const float* hist_rows, co // ---- key = grouped_norm(ple_key @ emb). The optional native projection // follows pinned CUDA Q8_1 MMVQ; the default retains its canonical Q8_0 path. - if (w.key_bf16 != nullptr) { + if (w.pre_key != nullptr) { + cudaMemcpyAsync(d_key, w.pre_key, (size_t) hc_dim * sizeof(float), cudaMemcpyDeviceToDevice, st); + } else if (w.key_bf16 != nullptr) { bf16_gemv_fp32_mmvf(emb, w.key_bf16, d_key, n_embd, hc_dim, stream); } else if (native_key) { native_quantize_q8_1(emb, w.key_native_q8_1, n_embd, 1, stream); @@ -319,7 +322,9 @@ void ple_block(const float* emb, const float* hidden, const float* hist_rows, co } // The value projection's independent option leaves the nonlinear PLE operations unchanged. - if (native_bf16) { + if (w.pre_value != nullptr) { + cudaMemcpyAsync(d_value, w.pre_value, (size_t) n_embd * sizeof(float), cudaMemcpyDeviceToDevice, st); + } else if (native_bf16) { bf16_gemv_fp32_mmvf(emb, w.value_bf16, d_value, n_embd, n_embd, stream); } else { to_bf16_kernel<<<(n_embd + THREADS - 1) / THREADS, THREADS, 0, st>>>(emb, d_emb16, n_embd); From 48d10a4996d7cc2637031d6f8d83c407e9017725 Mon Sep 17 00:00:00 2001 From: Niko1221 Date: Sat, 3 Oct 2026 07:24:46 +0200 Subject: [PATCH 3/3] Prompt path: STRATA_PREFILL_DUMP_R_ALL=FILE, every position's final residual as BF16 (opt-in, for distillation data) Appends [n][hc*n_embd] BF16 rows (round-to-nearest-even) in position order for every batched prompt chunk: the input the MTP draft layer reads, for training it against this trunk. Off unless the variable is set. --- src/prefill/prefill.cpp | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/src/prefill/prefill.cpp b/src/prefill/prefill.cpp index e4ddd1c1..83ddd1b5 100644 --- a/src/prefill/prefill.cpp +++ b/src/prefill/prefill.cpp @@ -2110,6 +2110,28 @@ bool Prefill::run(const int64_t* tokens, int64_t n, int64_t pos0, std::string& e std::fclose(f); } } + if (const char* dump = std::getenv("STRATA_PREFILL_DUMP_R_ALL")) { + // S25 (draft-layer distillation data, opt-in): every position's final multi-stream residual as BF16 + // (round-to-nearest-even), rows in position order, appended across chunks and requests: [n][hc*n_embd] + cudaStreamSynchronize(m.cs); + if (std::FILE* f = std::fopen(dump, "ab")) { + constexpr int64_t kRows = 512; + std::vector rows((size_t) (kRows * D)); + std::vector out((size_t) (kRows * D)); + for (int64_t t0 = 0; t0 < T; t0 += kRows) { + const int64_t nr = std::min(kRows, T - t0); + cudaMemcpy(rows.data(), m.R + t0 * D, (size_t) (nr * D) * 4, cudaMemcpyDeviceToHost); + for (int64_t i = 0; i < nr * D; ++i) { + uint32_t u; + std::memcpy(&u, &rows[(size_t) i], 4); + u += 0x7fffu + ((u >> 16) & 1u); + out[(size_t) i] = (uint16_t) (u >> 16); + } + std::fwrite(out.data(), 2, (size_t) (nr * D), f); + } + std::fclose(f); + } + } if (on_chunk || on_stage_chunk) { const auto toc = Clock::now(); if (cudaStreamSynchronize(m.cs) != cudaSuccess) {