From 3171291420e762ce744cb74121197cc55da76f20 Mon Sep 17 00:00:00 2001 From: Niko1221 Date: Mon, 26 Oct 2026 16:18:49 +0100 Subject: [PATCH 1/5] KVA: STRATA_KVA_SKIP=moe (opt-in) - the late layers of an approximated chunk run their whole attention half and skip only the MoE half The default stays the state-only form (byte-identical). With STRATA_KVA_SKIP=moe the GDN recurrence (gate projection, output normalization, output projection) and the QSA attention (query side, selection, attention, output projection) run in full for layers >= split, their outputs go into the residual, and the loop leaves before the MoE half; the expert transfers of those layers are still skipped. The KVA line says "mode moe-skip". Motivation: the extraction losses of the state-only form come from late-layer K/V and state built from a stale residual; this keeps the attention halves' contribution in it. kva_test covers the switch. --- docs/KVA.md | 1 + include/strata/prefill/kva.hpp | 6 ++++++ src/prefill/kva_test.cpp | 13 ++++++++++++- src/prefill/prefill.cpp | 30 ++++++++++++++++++++++-------- 4 files changed, 41 insertions(+), 9 deletions(-) diff --git a/docs/KVA.md b/docs/KVA.md index 5274bd3e..e94a4dfc 100644 --- a/docs/KVA.md +++ b/docs/KVA.md @@ -81,6 +81,7 @@ there, with a line when it is set). | `STRATA_KVA_SUFFIX` | 2048 | fully computed tail positions; see above | | `STRATA_KVA_MIN_PROMPT` | 4096 | shorter prompts stay on the full path | | `STRATA_KVA_DRAFT_FLOOR` | off | `1`: the draft layer skips the draft cells of approximated chunks (research) | +| `STRATA_KVA_SKIP` | state | `moe` (opt-in): the late layers of an approximated chunk run their whole attention half (GDN and QSA outputs into the residual) and skip only the MoE half and its expert transfers; the log line says `mode moe-skip`. Slower than the default state-only form, closer to the exact residual | | `STRATA_KVA_ONLY` | all | `gdn` or `qsa`: approximate only that layer type | | `STRATA_KVA_MODE` | identity | `identity`, `oracle`, `linear` (linear is not implemented) | | `STRATA_KVA_CAPTURE` | — | directory: write every late layer's real input for calibration (the read is exact while capturing) | diff --git a/include/strata/prefill/kva.hpp b/include/strata/prefill/kva.hpp index 874566d2..ea672709 100644 --- a/include/strata/prefill/kva.hpp +++ b/include/strata/prefill/kva.hpp @@ -62,6 +62,10 @@ inline KvaEngine& kva_engine() { struct KvaConfig { enum class Mode { Identity, Oracle, Linear }; enum class Only { All, Gdn, Qsa }; + /// What a late layer of an approximated chunk still does: StateOnly (the default) advances only its state (GDN + /// recurrence, K/V and indexer pages); Moe runs the whole attention half (outputs into the residual) and skips only + /// the MoE half and its expert transfers. + enum class Skip { StateOnly, Moe }; bool on = true; ///< on unless STRATA_KVA=0 (or this build has no path) bool explicit_on = false; ///< STRATA_KVA was set to something other than 0: every declined read says why @@ -75,6 +79,7 @@ struct KvaConfig { int64_t min_prompt = 4096; ///< STRATA_KVA_MIN_PROMPT: shorter prompts stay on the full path Mode mode = Mode::Identity; ///< STRATA_KVA_MODE=identity|oracle|linear Only only = Only::All; ///< STRATA_KVA_ONLY=all|gdn|qsa: which late layers take the approximate path + Skip skip = Skip::StateOnly; ///< STRATA_KVA_SKIP=moe: skip only the MoE half of the late layers (opt-in) std::string oracle_dir; ///< STRATA_KVA_ORACLE std::string capture_dir; ///< STRATA_KVA_CAPTURE std::string projector; ///< STRATA_KVA_PROJECTOR @@ -165,6 +170,7 @@ inline KvaConfig kva_config_from_env() { else if (std::string(m) == "linear") k.mode = KvaConfig::Mode::Linear; else k.mode = KvaConfig::Mode::Identity; } + if (const char* sk = std::getenv("STRATA_KVA_SKIP"); sk != nullptr && std::string(sk) == "moe") k.skip = KvaConfig::Skip::Moe; if (const char* o = std::getenv("STRATA_KVA_ONLY"); o != nullptr && *o != '\0') { if (std::string(o) == "gdn") k.only = KvaConfig::Only::Gdn; else if (std::string(o) == "qsa") k.only = KvaConfig::Only::Qsa; diff --git a/src/prefill/kva_test.cpp b/src/prefill/kva_test.cpp index be6f2ef8..0789e331 100644 --- a/src/prefill/kva_test.cpp +++ b/src/prefill/kva_test.cpp @@ -31,7 +31,7 @@ static void put(const char* k, const char* v) { static void clear_all() { for (const char* k : {"STRATA_KVA", "STRATA_KVA_SPLIT", "STRATA_KVA_SUFFIX", "STRATA_KVA_MIN_PROMPT", "STRATA_KVA_PARITY", "STRATA_KVA_CAPTURE", "STRATA_KVA_CAPTURE_FP32", "STRATA_KVA_ORACLE", "STRATA_KVA_ORACLE_FP32", - "STRATA_KVA_PROJECTOR", "STRATA_KVA_MODE", "STRATA_KVA_ONLY", "STRATA_KVA_DRAFT_FLOOR"}) + "STRATA_KVA_PROJECTOR", "STRATA_KVA_MODE", "STRATA_KVA_ONLY", "STRATA_KVA_DRAFT_FLOOR", "STRATA_KVA_SKIP"}) unset(k); } static bool is(const char* got, const char* want) { return got != nullptr && std::strcmp(got, want) == 0; } @@ -118,6 +118,17 @@ int main() { } clear_all(); + // ---- STRATA_KVA_SKIP: the default is the state-only form; "moe" runs the attention halves and skips the MoE half + { + CHECK(kva_config_from_env().skip == KvaConfig::Skip::StateOnly); + put("STRATA_KVA_SKIP", "moe"); + CHECK(kva_config_from_env().skip == KvaConfig::Skip::Moe); + put("STRATA_KVA_SKIP", "state"); + CHECK(kva_config_from_env().skip == KvaConfig::Skip::StateOnly); + put("STRATA_KVA_SKIP", "bogus"); + CHECK(kva_config_from_env().skip == KvaConfig::Skip::StateOnly); + unset("STRATA_KVA_SKIP"); + } // ---- "not applied => identical to STRATA_KVA=0", the parts that need no GPU // (1) by default the draft layer is 0.2.6's own code; the cell floor is a research option, wired only when asked { diff --git a/src/prefill/prefill.cpp b/src/prefill/prefill.cpp index 8fd781a5..5ccfeb95 100644 --- a/src/prefill/prefill.cpp +++ b/src/prefill/prefill.cpp @@ -3272,6 +3272,10 @@ bool Prefill::run_impl(const int64_t* tokens, int64_t n, int64_t pos0, std::stri const bool kva_layer = kva_approx && l >= kva_split && (kva.only == KvaConfig::Only::All || (kva.only == KvaConfig::Only::Gdn) == !core::is_qsa_layer(g, l)); + // STRATA_KVA_SKIP=moe: the layer's attention half (GDN or QSA, outputs included) runs in full and only the MoE + // half is skipped; `kva_state_only` is the default form, where only the state advances + const bool kva_moe_only = kva_layer && kva.skip == KvaConfig::Skip::Moe; + const bool kva_state_only = kva_layer && !kva_moe_only; const bool kva_subst = kva_layer && kva.mode == KvaConfig::Mode::Oracle; const bool kva_cap_layer = kva.capture && LB == 0 && (LE < 0 || LE == g.n_layers) && l >= kva_split; if (l == std::max(LB, 1)) { // the PLE rows, read from layer 1 on (a wait for the SSD read of the first chunk lands on "ple") @@ -3341,6 +3345,15 @@ bool Prefill::run_impl(const int64_t* tokens, int64_t n, int64_t pos0, std::stri normed = false; // the previous half's norm of R is stale once the input is replaced } for (int half = 0; half < 2; ++half) { + if (kva_moe_only && half == 1) { + // KVA moe-skip: the attention half wrote its output into R; this layer's MoE half has no consumer + // left for an approximated chunk, so it (and its expert transfers) is skipped. R is not normed for + // it (the write after the attention half normed it with this layer's FFN weights). + normed = false; + consumed = std::max(consumed, seq_start[(size_t) l + 1]); + give_back(consumed); + break; + } // ---- the hyper-connection read of this half const char* pre = half == 0 ? "hc_attn_" : "hc_ffn_"; const std::string sn = std::string(pre) + "norm.weight", sd = std::string(pre) + "down.weight", @@ -3441,7 +3454,7 @@ bool Prefill::run_impl(const int64_t* tokens, int64_t n, int64_t pos0, std::stri const char* v = std::getenv("STRATA_KVA_GDN_NORM"); return v != nullptr && std::atoi(v) != 0; }(); - if ((!kva_layer || kva_gdn_norm) && + if ((!kva_state_only || kva_gdn_norm) && !native_proj(m.gemm, wg, m.mixed_h, m.z, T, v.name("attn_gate.weight"), err)) return false; if (!bf16_proj(m.gemm, wa, m.mixed_bf, m.ab, T, v.name("ssm_alpha.weight"), err, 2 * HV, m.mixed_bf_lo)) return false; if (!bf16_proj(m.gemm, wb, m.mixed_bf, m.ab + HV, T, v.name("ssm_beta.weight"), err, 2 * HV, m.mixed_bf_lo)) return false; @@ -3450,16 +3463,16 @@ bool Prefill::run_impl(const int64_t* tokens, int64_t n, int64_t pos0, std::stri gdn_conv(conv, m.qkv, (const float*) wc->data, m.hbuf, T, EPS, m.cs); pt.mark(kPfGdnRec, cs); const int64_t ld_y = pf_pad() && T >= std::max(pf_switch_min_t(), 64) ? ZV + ZV_PAD : 0; - if (kva_layer && !kva_gdn_norm) // KVA: the recurrence without the output normalization + if (kva_state_only && !kva_gdn_norm) // KVA: the recurrence without the output normalization gdn_recurrence_state_only(state, m.hbuf, m.gate, m.beta, m.y, T, m.cs); else gdn_recurrence(state, m.hbuf, m.gate, m.beta, m.z, (const float*) wnm->data, EPS, m.y, m.y_h, T, m.cs, ld_y); pt.mark(kPfGdnOut, cs); - if (!kva_layer) // KVA: the out projection feeds only the residual write and the MoE half + if (!kva_state_only) // KVA: the out projection feeds only the residual write and the MoE half if (!native_proj(m.gemm, wo, m.y_h, m.bo, T, v.name("ssm_out.weight"), err, 0, ld_y)) return false; ++gdn_index; - if (kva_layer) { + if (kva_state_only) { // the layer consumed no expert: its stream entries must still be released, or the issuer // thread fills the ring and the next fully computed layer waits for a copy that never comes consumed = std::max(consumed, seq_start[(size_t) l + 1]); @@ -3481,9 +3494,9 @@ bool Prefill::run_impl(const int64_t* tokens, int64_t n, int64_t pos0, std::stri if (!native_proj(m.gemm, wk, m.mixed_h, m.Kc, T, v.name("attn_k.weight"), err)) return false; if (!native_proj(m.gemm, wv, m.mixed_h, m.Vc, T, v.name("attn_v.weight"), err)) return false; // KVA: the query projections have no consumer when only the KV/indexer state advances - if (!kva_layer && !native_proj(m.gemm, wq, m.mixed_h, m.Qf, T, v.name("attn_q.weight"), err)) return false; + if (!kva_state_only && !native_proj(m.gemm, wq, m.mixed_h, m.Qf, T, v.name("attn_q.weight"), err)) return false; if (!bf16_proj(m.gemm, wik, m.mixed_bf, m.idx_raw, T, v.name("indexer.k_proj.weight"), err, 0, m.mixed_bf_lo)) return false; - if (!kva_layer && !bf16_proj(m.gemm, wiq, m.mixed_bf, m.q_idx, T, v.name("indexer.q_proj.weight"), err, 0, m.mixed_bf_lo)) return false; + if (!kva_state_only && !bf16_proj(m.gemm, wiq, m.mixed_bf, m.q_idx, T, v.name("indexer.q_proj.weight"), err, 0, m.mixed_bf_lo)) return false; rms_rows(m.Kc, (const float*) wkn->data, T * 2, 256, 256, EPS, m.cs); rope(m.Kc, T, 2, 256, 512, p0, strata::kernels::rope_scaling(), m.cs); // KV streaming: this layer's cells [0, p0) come in from the host copy to the staging pool, and the @@ -3575,7 +3588,7 @@ bool Prefill::run_impl(const int64_t* tokens, int64_t n, int64_t pos0, std::stri // KVA: the KV pages and the indexer's keys/pool are complete for this chunk. Nothing that // reads them (query side, selection, attention, output projection, the MoE half) has a // consumer left, so the layer ends here. - if (kva_layer) { + if (kva_state_only) { ++qsa_index; if (!kv_prefetch_after(l + 1, qsa_index)) return false; // no expert is consumed here: release the stream entries so the issuer cannot stall @@ -5274,7 +5287,8 @@ bool Prefill::run_impl(const int64_t* tokens, int64_t n, int64_t pos0, std::stri } } if (kva_run || kva.capture) { - const char* mode = kva.mode == KvaConfig::Mode::Oracle ? "oracle" + const char* mode = kva.skip == KvaConfig::Skip::Moe ? "moe-skip" + : kva.mode == KvaConfig::Mode::Oracle ? "oracle" : kva.mode == KvaConfig::Mode::Linear ? "linear" : "identity"; const char* only = kva.only == KvaConfig::Only::Gdn ? ", gdn layers only" : kva.only == KvaConfig::Only::Qsa ? ", qsa layers only" : ""; From e3450e455218ee2d0e8c70e40b5588250ed05e11 Mon Sep 17 00:00:00 2001 From: Niko1221 Date: Mon, 26 Oct 2026 16:36:31 +0100 Subject: [PATCH 2/5] KVA ships OFF in 0.2.7: STRATA_KVA unset is the exact 0.2.6 engine, STRATA_KVA=1 turns it on with an 8192-token exact tail The quality runs found lost records and invented rows in long extraction tasks and defects in agent loops with a ~10K system prompt when most of the prompt was approximated. Opt-in now: unset (or 0) nothing runs, the draft layer included. Switched on, the tail is 8192 tokens (STRATA_KVA_SUFFIX overrides), the safest configuration measured. A model other than Qwen3.8-Flash-Next's geometry gets a one-line warning instead of a refusal. Kept: the untouched draft layer (cell floor behind STRATA_KVA_DRAFT_FLOOR=1), the decline reasons, STRATA_KVA_SKIP=moe, the tests. docs/KVA.md says why it is off and how to turn it on; kva_test checks unset = off and STRATA_KVA=1 = on with the 8192 tail. --- docs/KVA.md | 53 ++++++++++++++++++++++++---------- include/strata/prefill/kva.hpp | 28 +++++++++--------- src/prefill/kva_test.cpp | 48 ++++++++++++++---------------- src/prefill/prefill.cpp | 16 ++++++---- src/program/generate.cpp | 2 +- 5 files changed, 84 insertions(+), 63 deletions(-) diff --git a/docs/KVA.md b/docs/KVA.md index e94a4dfc..15aacaae 100644 --- a/docs/KVA.md +++ b/docs/KVA.md @@ -1,4 +1,4 @@ -# KVA: the faster prompt read for long cold prompts (on by default since 0.2.7) +# KVA: a faster prompt read for long cold prompts (opt-in, off by default) KVA ("approximate late-layer state") reads a long prompt exactly through layer 23, then for the late layers 24–47 builds **only their recurrent state** — the GDN recurrence and convolution history, the QSA K/V pages and @@ -7,9 +7,31 @@ the indexer keys — instead of running the whole layer. The attention output, t results. The last positions of the prompt are then read fully, exactly as before, and decode runs the ordinary full path against the state KVA built. -It is an approximation: the answers can differ from the exact read (see "Measured" for how much). **`STRATA_KVA=0` -turns it off and gives the exact 0.2.6 prompt path.** The Strata app shows this as the "Faster long prompts" switch -in Expert settings; it sends `STRATA_KVA=0` when the switch is off. +**It is off by default.** Unset, the engine is 0.2.6's, prompt path and draft layer included. Set `STRATA_KVA=1` to +turn it on (the Strata app shows it as the "Faster long prompts" switch in Expert settings, off until the user turns +it on; the Terminal passes `STRATA_KVA=1`). When it is on, the engine uses the safest configuration measured: an +**8192-token exact tail** (`STRATA_KVA_SUFFIX` changes it). + +## Why it is off + +KVA is an approximation, and the 0.2.7 quality runs found where it costs answers. The runs compare KVA off and on on the +same build, greedy, with a noise run (off against off): + +- **Extraction of many scattered records** (a list of 8 register entries in a 14K document, 6 such tasks at 14K and + 30K): with the 2048-token tail, 4 of 6 tasks passed against 6 of 6 exact, 249 of 270 facts found against 270. + The model silently dropped 3 of 8 records at 14K and, at 30K, started its list with an invented row + (the request's own reference id leaked into the answer). Neither exact run did either. +- **Agent loops with a ~10K-token system prompt** (handbook, tool docs, rules): the first 8192 tokens, about 84 % of + the system prompt, were approximated. Two of three runs had defects the exact runs did not: a final answer that + left out the totals, and a split-order quote with wrong arithmetic (the price of 80 units for 70, a shortfall of + 40 instead of 50). +- Needle retrieval, unanswerable questions, short summaries, code and JSON were unchanged. The speed gain is real: + about 1.3x prompt speed at 14K and 1.5x at 30K on a Tesla P100. + +The 8192-token tail approximates nothing below roughly 16K tokens (the chunks are 8192 tokens, and a chunk is +approximated only when it ends before the tail) and about half of a 30K prompt. It was the only variant that passed the +same runs on the Radeon AI PRO R9700 and tied the exact arm on the RTX 5070. Its speed gain is correspondingly smaller +than the figures above, which were measured with the 2048-token tail. ## When it runs @@ -21,14 +43,13 @@ KVA runs when all of these hold, and says so in the engine log: - the prompt (or the part of it read in one go, see below) has at least `STRATA_KVA_MIN_PROMPT` tokens (4096) and more than the fully computed tail plus 64. -Otherwise the prompt is read exactly. An engine that was started with `STRATA_KVA=1` logs the reason for every -read it turned away (`strata prefill: KVA configured but NOT applied: `); with the default it logs only -a long prompt turned away for an unusual reason (a layer split, pictures), and once at start-up when the engine -configuration rules KVA out for the whole run. +Otherwise the prompt is read exactly. An engine started with `STRATA_KVA=1` logs the reason for every read it turned +away (`strata prefill: KVA configured but NOT applied: `), and once at start-up when the engine configuration +rules KVA out for the whole run. Without `STRATA_KVA=1` it logs nothing about KVA. ## The fully computed tail and the draft layer -The last `suffix` positions (2048, or `STRATA_KVA_SUFFIX`) are always read exactly. +The last `suffix` positions (8192, or `STRATA_KVA_SUFFIX`) are always read exactly. The MTP draft layer builds its K/V from the final residuals of the prompt. An approximated chunk has no final residual of its own, so its draft cells are built from the rows that chunk does have. By default nothing about the @@ -37,8 +58,8 @@ attends to the same cells as with `STRATA_KVA=0`. The target model verifies ever how many tokens a verify window keeps (decode speed), never the answer. Measured on the P100 (IQ3_XXS, auto cache and prefill, 14K and 30K prompts, 256 tokens), the draft layer attends to -those cells with acceptance within noise of the exact read. A forced small window (the first default-on build set -`--mtp-window 2048` for every session) and the research option below both cost acceptance on faster cards. +those cells with acceptance within noise of the exact read at 14K and a few points lower at 30K (decode 6 % lower at +30K). A forced small window (`--mtp-window 2048`) and the research option below both cost acceptance on faster cards. `STRATA_KVA_DRAFT_FLOOR=1` (research): the draft layer gets a *cell floor* per session, the first cell built from a real row. It builds no draft cell below it and attends to `[max(floor, pos - window), pos)`, the exact tail growing with @@ -62,7 +83,7 @@ greater than 0 and is read exactly. debug check) differ with KVA on, as they would between any two reads that take different paths. - **A cold read again** (the cache evicted the conversation, a new chat with the same system prompt) approximates its own prefix again. -- **`STRATA_KVA=0`** restores 0.2.6: every prompt is read exactly, the draft window is 32768 cells, nothing else +- **Unset (or `STRATA_KVA=0`)** is 0.2.6: every prompt is read exactly, the draft window is 32768 cells, nothing else changes. ## Layer splits, batch slots, SYCL, Metal @@ -76,9 +97,9 @@ there, with a line when it is set). | variable | default | what it does | |---|---:|---| -| `STRATA_KVA` | on | `0` turns KVA off (the exact 0.2.6 prompt read). `1` is the same as the default but logs every read it turned away | +| `STRATA_KVA` | off | `1` turns KVA on (with the 8192-token tail). Unset or `0`: the exact 0.2.6 prompt read and draft layer | | `STRATA_KVA_SPLIT` | 24 | first approximated layer (zero-based) | -| `STRATA_KVA_SUFFIX` | 2048 | fully computed tail positions; see above | +| `STRATA_KVA_SUFFIX` | 8192 | fully computed tail positions; see above. 2048 is the fastest setting and the one the extraction and agent defects were found with | | `STRATA_KVA_MIN_PROMPT` | 4096 | shorter prompts stay on the full path | | `STRATA_KVA_DRAFT_FLOOR` | off | `1`: the draft layer skips the draft cells of approximated chunks (research) | | `STRATA_KVA_SKIP` | state | `moe` (opt-in): the late layers of an approximated chunk run their whole attention half (GDN and QSA outputs into the residual) and skip only the MoE half and its expert transfers; the log line says `mode moe-skip`. Slower than the default state-only form, closer to the exact residual | @@ -92,10 +113,10 @@ there, with a line when it is set). Every read says what it did: -- applied: `strata prefill: KVA on: split 24, suffix 2048, approximated 5120 of 7533 positions in 5 of 8 chunks, mode identity` +- applied (here with `STRATA_KVA_SUFFIX=2048`): `strata prefill: KVA on: split 24, suffix 2048, approximated 5120 of 7533 positions in 5 of 8 chunks, mode identity` - turned away: `strata prefill: KVA configured but NOT applied: (prompt … positions, min_prompt …, suffix …, split …)` -A quality comparison needs the control arm to say `STRATA_KVA=0` (an unset variable is now KVA). +A quality comparison needs the KVA arm to say `STRATA_KVA=1`; the control arm leaves it unset or says `STRATA_KVA=0`. ## Measured (Tesla P100 16 GB, Qwen3.8-Flash-Next IQ3_XXS, chunk 1024, expert cache 1500, MTP window 2048) diff --git a/include/strata/prefill/kva.hpp b/include/strata/prefill/kva.hpp index ea672709..f765fb85 100644 --- a/include/strata/prefill/kva.hpp +++ b/include/strata/prefill/kva.hpp @@ -1,4 +1,4 @@ -// include/strata/prefill/kva.hpp - STRATA_KVA: the approximate late-layer prompt read (on by default since 0.2.7). +// include/strata/prefill/kva.hpp - STRATA_KVA: the approximate late-layer prompt read (opt-in since 0.2.7). // // After layer `split` the prompt's late layers need only their recurrent state (GDN recurrence + conv history) // and their KV/indexer caches; the attention output, the output projection, the MoE half and its expert @@ -14,8 +14,10 @@ // oracle a captured real input per late layer (STRATA_KVA_ORACLE=; the plumbing/parity mode), // linear a fitted low-rank projector (STRATA_KVA_PROJECTOR=; not implemented in this step). // -// 0.2.7: the read is ON by default for a cold prompt of STRATA_KVA_MIN_PROMPT tokens or more on one CUDA/HIP -// device. STRATA_KVA=0 brings back the exact 0.2.6 prompt path and the 0.2.6 draft window. Everything is read from +// 0.2.7: the read is OFF unless STRATA_KVA=1 is set (the quality runs found lost records and invented rows in long +// extraction and agent tasks, docs/KVA.md). Unset, nothing here runs and the engine is 0.2.6's. Switched on, it applies +// to a cold prompt of STRATA_KVA_MIN_PROMPT tokens or more on one CUDA/HIP device, with an 8192-token exact tail +// (the safest configuration measured). Everything is read from // the environment once per process. // // What the engine tells this header at start-up (kva_engine()): whether its configuration rules the read out for the @@ -47,7 +49,7 @@ inline constexpr bool kKvaBackend = true; #endif /// The fully computed tail of every approximated read. -inline constexpr int64_t kKvaSuffixDefault = 2048; +inline constexpr int64_t kKvaSuffixDefault = 8192; /// What the running engine knows and the environment cannot say. struct KvaEngine { @@ -67,7 +69,7 @@ struct KvaConfig { /// the MoE half and its expert transfers. enum class Skip { StateOnly, Moe }; - bool on = true; ///< on unless STRATA_KVA=0 (or this build has no path) + bool on = false; ///< STRATA_KVA=1 (and this build has a path); unset or 0: off bool explicit_on = false; ///< STRATA_KVA was set to something other than 0: every declined read says why bool capture = false; ///< STRATA_KVA_CAPTURE=: dump every late layer's real input rows bool capture_fp32 = false; ///< STRATA_KVA_CAPTURE_FP32=1: fp32 records (else bf16; fp32 is for parity runs) @@ -84,7 +86,7 @@ struct KvaConfig { std::string capture_dir; ///< STRATA_KVA_CAPTURE std::string projector; ///< STRATA_KVA_PROJECTOR - /// The fully computed tail of a read: STRATA_KVA_SUFFIX when set, otherwise 2048. + /// The fully computed tail of a read: STRATA_KVA_SUFFIX when set, otherwise 8192. int64_t effective_suffix() const { return suffix_env > 0 ? suffix_env : kKvaSuffixDefault; } /// Does the draft layer need the cell floor at all? Only when a read can approximate something: KVA on, not ruled @@ -96,17 +98,13 @@ struct KvaConfig { /// Why this read cannot take the approximate path, or nullptr when it can: a cold read of one prompt (not a /// continuation, no layer split, no pictures, no DFlash2 taps), on an engine that allows it, long enough for a /// suffix to leave approximated positions behind it. `routine` (optional) is set when the reason is an - /// everyday one (a continuation, a short prompt) that an engine running KVA by default does not log. + /// everyday one (a continuation, a short prompt) that is not worth a log line. const char* decline(const KvaEngine& e, int64_t pos0, int64_t n, int64_t lb, int64_t le, int64_t n_layers, - bool pictures = false, bool taps = false, bool* routine = nullptr, - bool unvalidated_model = false) const { + bool pictures = false, bool taps = false, bool* routine = nullptr) const { if (routine) *routine = false; - if (!on) return "off (STRATA_KVA=0)"; + if (!on) return "off (STRATA_KVA is not 1)"; if (capture) return "capture mode"; if (!e.blocked.empty()) return e.blocked.c_str(); - // The split layer, the state-only layers and the suffix rule were measured on one geometry (Qwen3.8-Flash-Next, - // 48 layers, 512 experts). Another model reads exactly unless STRATA_KVA=1 asks for it. - if (unvalidated_model && !explicit_on) return "KVA was not validated for this model (STRATA_KVA=1 forces it)"; if (pos0 != 0) { if (routine) *routine = true; return "a continuation read (pos0 != 0)"; @@ -143,8 +141,8 @@ inline KvaConfig kva_config_from_env() { KvaConfig k; const char* sw = std::getenv("STRATA_KVA"); const bool set = sw != nullptr && *sw != '\0'; - k.on = !set || sw[0] != '0'; - k.explicit_on = set && sw[0] != '0'; + k.on = set && sw[0] != '0'; + k.explicit_on = k.on; if (!kKvaBackend) k.on = false; k.split = kva_env_i64("STRATA_KVA_SPLIT", 24); k.suffix_env = std::max(0, kva_env_i64("STRATA_KVA_SUFFIX", 0)); diff --git a/src/prefill/kva_test.cpp b/src/prefill/kva_test.cpp index 0789e331..1715a90a 100644 --- a/src/prefill/kva_test.cpp +++ b/src/prefill/kva_test.cpp @@ -37,30 +37,34 @@ static void clear_all() { static bool is(const char* got, const char* want) { return got != nullptr && std::strcmp(got, want) == 0; } int main() { - // ---- the switch: on by default, STRATA_KVA=0 off, anything else on and chatty + // ---- the switch: off unless STRATA_KVA=1 (0.2.7 ships KVA opt-in; unset is the exact 0.2.6 engine) clear_all(); { const KvaConfig c = kva_config_from_env(); - CHECK(c.on && !c.explicit_on); + CHECK(!c.on && !c.explicit_on); // unset: off CHECK(c.split == 24 && c.min_prompt == 4096 && c.suffix_env == 0); CHECK(c.mode == KvaConfig::Mode::Identity && c.only == KvaConfig::Only::All && !c.capture); + KvaEngine eng; + CHECK(!c.wants_cell_floor(eng, true)); + CHECK(is(c.decline(eng, 0, 30000, 0, -1, 48), "off (STRATA_KVA is not 1)")); // a 30K cold prompt is read exactly } put("STRATA_KVA", "0"); { const KvaConfig c = kva_config_from_env(); CHECK(!c.on && !c.explicit_on); } put("STRATA_KVA", "1"); { const KvaConfig c = kva_config_from_env(); CHECK(c.on && c.explicit_on); } put("STRATA_KVA", ""); // an empty value is "not set" - { const KvaConfig c = kva_config_from_env(); CHECK(c.on && !c.explicit_on); } + { const KvaConfig c = kva_config_from_env(); CHECK(!c.on && !c.explicit_on); } clear_all(); + put("STRATA_KVA", "1"); // everything below runs with KVA switched on - // ---- the tail: 2048, or STRATA_KVA_SUFFIX; the draft window plays no part (the cell floor protects the draft layer) + // ---- the tail: 8192 when switched on (the safest configuration measured), or STRATA_KVA_SUFFIX { const KvaConfig c = kva_config_from_env(); - CHECK(c.effective_suffix() == kKvaSuffixDefault && kKvaSuffixDefault == 2048); + CHECK(c.on && c.effective_suffix() == kKvaSuffixDefault && kKvaSuffixDefault == 8192); put("STRATA_KVA_SUFFIX", "3000"); const KvaConfig e = kva_config_from_env(); CHECK(e.effective_suffix() == 3000); - clear_all(); + unset("STRATA_KVA_SUFFIX"); } // ---- who is turned away, and why (the first reason that holds is the one named) @@ -68,42 +72,33 @@ int main() { const KvaConfig c = kva_config_from_env(); KvaEngine eng; bool routine = false; - // a cold 14.6K prompt on one device: applies + // a cold 14.6K prompt on one device: applies (14600 > 8192 + 64) CHECK(c.decline(eng, 0, 14600, 0, -1, 48) == nullptr); CHECK(c.decline(eng, 0, 14600, 0, 48, 48) == nullptr); // le == n_layers is the whole trunk too // a continuation (turn 2 of a chat, a resumed prompt): routine CHECK(is(c.decline(eng, 9000, 14600, 0, -1, 48, false, false, &routine), "a continuation read (pos0 != 0)") && routine); - // short prompts and a prompt the tail would swallow: routine + // short prompts and a prompt the 8192-token tail would swallow: routine CHECK(is(c.decline(eng, 0, 3000, 0, -1, 48, false, false, &routine), "the prompt is shorter than STRATA_KVA_MIN_PROMPT") && routine); - CHECK(c.decline(eng, 0, 4096, 0, -1, 48) == nullptr); // 4096 > 2112 + 64: it applies - put("STRATA_KVA_SUFFIX", "5000"); - { const KvaConfig s = kva_config_from_env(); - CHECK(is(s.decline(eng, 0, 5000, 0, -1, 48, false, false, &routine), "the suffix leaves too few positions to approximate") && routine); } - clear_all(); + CHECK(is(c.decline(eng, 0, 8256, 0, -1, 48, false, false, &routine), "the suffix leaves too few positions to approximate") && routine); + CHECK(c.decline(eng, 0, 8257, 0, -1, 48) == nullptr); + put("STRATA_KVA_SUFFIX", "2048"); + { const KvaConfig s = kva_config_from_env(); CHECK(s.decline(eng, 0, 4096, 0, -1, 48) == nullptr); } + unset("STRATA_KVA_SUFFIX"); // not routine: the user may want to know CHECK(is(c.decline(eng, 0, 14600, 24, -1, 48, false, false, &routine), "a layer split (not the whole trunk)") && !routine); CHECK(is(c.decline(eng, 0, 14600, 0, 24, 48), "a layer split (not the whole trunk)")); CHECK(is(c.decline(eng, 0, 14600, 0, -1, 48, true), "a prompt with pictures")); CHECK(is(c.decline(eng, 0, 14600, 0, -1, 48, false, true), "the DFlash2 drafter reads the late layers' hidden states")); - // a model the split and the tail were not measured on reads exactly unless STRATA_KVA=1 asks - CHECK(is(c.decline(eng, 0, 14600, 0, -1, 40, false, false, &routine, true), - "KVA was not validated for this model (STRATA_KVA=1 forces it)") && !routine); - put("STRATA_KVA", "1"); - { const KvaConfig f = kva_config_from_env(); CHECK(f.decline(eng, 0, 14600, 0, -1, 40, false, false, nullptr, true) == nullptr); } - clear_all(); - // no draft layer at all: nothing to cover // the engine rules the whole process out (batch slots, a layer split, a peer device) KvaEngine blocked = eng; blocked.blocked = "batch slots (--batch)"; CHECK(is(c.decline(blocked, 0, 14600, 0, -1, 48, false, false, &routine), "batch slots (--batch)") && !routine); - // off wins over everything, capture over the rest - put("STRATA_KVA", "0"); - { const KvaConfig o = kva_config_from_env(); CHECK(is(o.decline(eng, 0, 14600, 0, -1, 48), "off (STRATA_KVA=0)")); } - put("STRATA_KVA", "1"); + // capture wins over the rest put("STRATA_KVA_CAPTURE", "/tmp/kva-cap"); { const KvaConfig o = kva_config_from_env(); CHECK(o.capture && is(o.decline(eng, 0, 14600, 0, -1, 48), "capture mode")); } - clear_all(); + unset("STRATA_KVA_CAPTURE"); } + clear_all(); // ---- the research modes still parse put("STRATA_KVA_ORACLE", "/tmp/o"); @@ -133,8 +128,9 @@ int main() { // (1) by default the draft layer is 0.2.6's own code; the cell floor is a research option, wired only when asked { KvaEngine eng; + put("STRATA_KVA", "1"); const KvaConfig on = kva_config_from_env(); - CHECK(!on.draft_floor && !on.wants_cell_floor(eng, true)); + CHECK(on.on && !on.draft_floor && !on.wants_cell_floor(eng, true)); put("STRATA_KVA_DRAFT_FLOOR", "1"); const KvaConfig fl = kva_config_from_env(); CHECK(fl.draft_floor && fl.wants_cell_floor(eng, true)); diff --git a/src/prefill/prefill.cpp b/src/prefill/prefill.cpp index 5ccfeb95..7528c5d2 100644 --- a/src/prefill/prefill.cpp +++ b/src/prefill/prefill.cpp @@ -2720,8 +2720,15 @@ bool Prefill::run_impl(const int64_t* tokens, int64_t n, int64_t pos0, std::stri const int64_t kva_suffix = kva.effective_suffix(); bool kva_routine = false; const char* kva_why_not = kva.decline(kva_eng, pos0, n, LB, LE < 0 ? -1 : LE, (int64_t) g.n_layers, - embd_rows != nullptr, !tap_layers_.empty(), &kva_routine, - g.n_layers != 48 || g.n_expert != 512); + embd_rows != nullptr, !tap_layers_.empty(), &kva_routine); + if (kva_why_not == nullptr && (g.n_layers != 48 || g.n_expert != 512)) { + // the split layer and the tail were measured on one geometry; STRATA_KVA=1 is the user's call, but say so once + static std::atomic warned{false}; + if (!warned.exchange(true)) + std::fprintf(stderr, "strata prefill: WARNING: KVA was only validated on Qwen3.8-Flash-Next (48 layers, 512 " + "experts); this model has %lld layers and %lld experts\n", (long long) g.n_layers, + (long long) g.n_expert); + } const bool kva_run = kva_why_not == nullptr; const int64_t kva_split = std::min(std::max(kva.split, 1), g.n_layers); const int64_t kva_frontier = std::max(0, n - kva_suffix); @@ -5312,9 +5319,8 @@ bool Prefill::run_impl(const int64_t* tokens, int64_t n, int64_t pos0, std::stri } } else if (kva.on && (kva.explicit_on || (n >= kva.min_prompt && !kva_routine && kva_eng.blocked.empty()))) { // KVA-8 (observability): a run that was configured for KVA but fell back completely must say so and why, - // or an operator cannot tell it apart from a run where the feature was never asked for. With KVA on by - // default only a long prompt that was turned away for an unusual reason is worth a line (STRATA_KVA=1 asks - // for every reason). + // or an operator cannot tell it apart from a run where the feature was never asked for. (KVA only runs + // when STRATA_KVA=1 asked for it, so every reason is worth a line.) std::fprintf(stderr, "strata prefill: KVA configured but NOT applied: %s (prompt %lld positions, " "min_prompt %lld, suffix %lld, split %lld)\n", kva_why_not != nullptr ? kva_why_not : "not applicable", (long long) n, (long long) kva.min_prompt, diff --git a/src/program/generate.cpp b/src/program/generate.cpp index dbc5b6b0..a718c9cf 100644 --- a/src/program/generate.cpp +++ b/src/program/generate.cpp @@ -5509,7 +5509,7 @@ int main(int argc, char** argv) { // slots' draft-KV copies use this one too static const strata::core::ModelGeometry draft_geometry{}; std::vector> slot_mtp; - // KVA (0.2.7: on by default, STRATA_KVA=0 is the exact 0.2.6 path): what only the engine knows. A layer split and + // KVA (0.2.7: opt-in with STRATA_KVA=1; unset is the exact 0.2.6 path): what only the engine knows. A layer split and // the batch slots read their prompts in pieces and in stages, which the one-device, one-cold-read rule of the // approximate prompt read cannot follow, so they stay on the exact path (and say so here, once). The draft layer // (MTP) keeps its window, whatever it is: a session KVA approximated tells it the first cell built from a real row From 25a62e0fb3d87e942b11513731218d88f9907ee8 Mon Sep 17 00:00:00 2001 From: Niko1221 Date: Mon, 26 Oct 2026 16:40:59 +0100 Subject: [PATCH 3/5] KVA: keep the geometry gate - a model whose shape KVA was not measured on reads exactly; STRATA_KVA_ANY_MODEL=1 is the research override The app toggle will write STRATA_KVA=1 for Qwen models in general; the split layer, the tail and the measured quality apply to Qwen3.8-Flash-Next's shape only (48 layers, 512 experts). Any other shape (Qwen3.6-35B-A3B, ...) now logs "KVA configured but NOT applied: this model's shape (L layers, E experts) is not one KVA was measured on" and reads exactly; the routine reasons (short prompt, continuation) still come first. STRATA_KVA_ANY_MODEL=1 allows it, with one warning. kva_test and docs/KVA.md cover both. --- docs/KVA.md | 6 +++++- include/strata/prefill/kva.hpp | 11 ++++++++++- src/prefill/kva_test.cpp | 11 ++++++++++- src/prefill/prefill.cpp | 24 +++++++++++++++--------- 4 files changed, 40 insertions(+), 12 deletions(-) diff --git a/docs/KVA.md b/docs/KVA.md index 15aacaae..96a1ca3e 100644 --- a/docs/KVA.md +++ b/docs/KVA.md @@ -37,7 +37,10 @@ than the figures above, which were measured with the 2048-token tail. KVA runs when all of these hold, and says so in the engine log: -- Qwen3.8-Flash-Next on CUDA or HIP (not SYCL, not Metal; Qwen3.6 and the other models read exactly); +- Qwen3.8-Flash-Next on CUDA or HIP (not SYCL, not Metal). The split layer and the tail were measured on one shape, 48 + layers and 512 experts. Any other model (Qwen3.6-35B-A3B, other layer or expert counts) reads exactly even with + `STRATA_KVA=1` and logs `KVA configured but NOT applied: this model's shape (L layers, E experts) is not one KVA was + measured on`. `STRATA_KVA_ANY_MODEL=1` (research) lets it run there anyway, with one warning; - one GPU, no `--batch` slots, no `--peer-device`, no DFlash2 drafter; - the read is cold (it starts at position 0 of the session) and the text has no pictures; - the prompt (or the part of it read in one go, see below) has at least `STRATA_KVA_MIN_PROMPT` tokens (4096) and @@ -98,6 +101,7 @@ there, with a line when it is set). | variable | default | what it does | |---|---:|---| | `STRATA_KVA` | off | `1` turns KVA on (with the 8192-token tail). Unset or `0`: the exact 0.2.6 prompt read and draft layer | +| `STRATA_KVA_ANY_MODEL` | off | `1` (research): also run KVA on a model whose shape (layers, experts) it was not measured on, with a warning | | `STRATA_KVA_SPLIT` | 24 | first approximated layer (zero-based) | | `STRATA_KVA_SUFFIX` | 8192 | fully computed tail positions; see above. 2048 is the fastest setting and the one the extraction and agent defects were found with | | `STRATA_KVA_MIN_PROMPT` | 4096 | shorter prompts stay on the full path | diff --git a/include/strata/prefill/kva.hpp b/include/strata/prefill/kva.hpp index f765fb85..c98f4352 100644 --- a/include/strata/prefill/kva.hpp +++ b/include/strata/prefill/kva.hpp @@ -48,6 +48,9 @@ inline constexpr bool kKvaBackend = false; inline constexpr bool kKvaBackend = true; #endif +/// The reason decline() gives for a model whose shape KVA was not measured on (prefill.cpp prints it with the numbers). +inline constexpr const char* kKvaShapeReason = "this model's shape is not one KVA was measured on"; + /// The fully computed tail of every approximated read. inline constexpr int64_t kKvaSuffixDefault = 8192; @@ -75,6 +78,7 @@ struct KvaConfig { bool capture_fp32 = false; ///< STRATA_KVA_CAPTURE_FP32=1: fp32 records (else bf16; fp32 is for parity runs) bool oracle_fp32 = false; ///< STRATA_KVA_ORACLE_FP32=1: the oracle files are fp32 (else bf16) bool parity = false; ///< STRATA_KVA_PARITY=1: report the state-only path against the full path + bool any_model = false; ///< STRATA_KVA_ANY_MODEL=1 (research): also run on a shape KVA was not measured on, with a warning bool draft_floor = false; ///< STRATA_KVA_DRAFT_FLOOR=1: the draft layer skips the cells of approximated chunks (research; default: it reads them) int64_t split = 24; ///< STRATA_KVA_SPLIT: first approximated layer (zero-based) int64_t suffix_env = 0; ///< STRATA_KVA_SUFFIX: fully computed tail positions (0 = not set: kKvaSuffixDefault) @@ -100,7 +104,8 @@ struct KvaConfig { /// suffix to leave approximated positions behind it. `routine` (optional) is set when the reason is an /// everyday one (a continuation, a short prompt) that is not worth a log line. const char* decline(const KvaEngine& e, int64_t pos0, int64_t n, int64_t lb, int64_t le, int64_t n_layers, - bool pictures = false, bool taps = false, bool* routine = nullptr) const { + bool pictures = false, bool taps = false, bool* routine = nullptr, + bool unmeasured_shape = false) const { if (routine) *routine = false; if (!on) return "off (STRATA_KVA is not 1)"; if (capture) return "capture mode"; @@ -120,6 +125,9 @@ struct KvaConfig { if (routine) *routine = true; return "the suffix leaves too few positions to approximate"; } + // the split layer, the state-only layers and the tail were measured on one geometry (Qwen3.8-Flash-Next, 48 + // layers, 512 experts); another model reads exactly unless the research override asks for it + if (unmeasured_shape && !any_model) return kKvaShapeReason; return nullptr; } }; @@ -149,6 +157,7 @@ inline KvaConfig kva_config_from_env() { k.min_prompt = kva_env_i64("STRATA_KVA_MIN_PROMPT", 4096); k.parity = kva_env_flag("STRATA_KVA_PARITY", false); k.draft_floor = kva_env_flag("STRATA_KVA_DRAFT_FLOOR", false); + k.any_model = kva_env_flag("STRATA_KVA_ANY_MODEL", false); if (const char* d = std::getenv("STRATA_KVA_CAPTURE"); d != nullptr && *d != '\0') { k.capture = true; k.capture_dir = d; diff --git a/src/prefill/kva_test.cpp b/src/prefill/kva_test.cpp index 1715a90a..9c332185 100644 --- a/src/prefill/kva_test.cpp +++ b/src/prefill/kva_test.cpp @@ -31,7 +31,7 @@ static void put(const char* k, const char* v) { static void clear_all() { for (const char* k : {"STRATA_KVA", "STRATA_KVA_SPLIT", "STRATA_KVA_SUFFIX", "STRATA_KVA_MIN_PROMPT", "STRATA_KVA_PARITY", "STRATA_KVA_CAPTURE", "STRATA_KVA_CAPTURE_FP32", "STRATA_KVA_ORACLE", "STRATA_KVA_ORACLE_FP32", - "STRATA_KVA_PROJECTOR", "STRATA_KVA_MODE", "STRATA_KVA_ONLY", "STRATA_KVA_DRAFT_FLOOR", "STRATA_KVA_SKIP"}) + "STRATA_KVA_PROJECTOR", "STRATA_KVA_MODE", "STRATA_KVA_ONLY", "STRATA_KVA_DRAFT_FLOOR", "STRATA_KVA_SKIP", "STRATA_KVA_ANY_MODEL"}) unset(k); } static bool is(const char* got, const char* want) { return got != nullptr && std::strcmp(got, want) == 0; } @@ -93,6 +93,15 @@ int main() { KvaEngine blocked = eng; blocked.blocked = "batch slots (--batch)"; CHECK(is(c.decline(blocked, 0, 14600, 0, -1, 48, false, false, &routine), "batch slots (--batch)") && !routine); + // a model whose shape KVA was not measured on (not 48 layers / 512 experts) is turned away, not warned about; + // the routine reasons (a short prompt, a continuation) still come first + CHECK(is(c.decline(eng, 0, 14600, 0, -1, 40, false, false, &routine, true), kKvaShapeReason) && !routine); + CHECK(is(c.decline(eng, 0, 3000, 0, -1, 40, false, false, &routine, true), "the prompt is shorter than STRATA_KVA_MIN_PROMPT") && routine); + CHECK(c.decline(eng, 0, 14600, 0, -1, 48, false, false, &routine, false) == nullptr); // the measured shape applies + put("STRATA_KVA_ANY_MODEL", "1"); // the research override allows it (prefill.cpp then warns once) + { const KvaConfig a = kva_config_from_env(); CHECK(a.any_model && a.decline(eng, 0, 14600, 0, -1, 40, false, false, nullptr, true) == nullptr); } + unset("STRATA_KVA_ANY_MODEL"); + CHECK(!kva_config_from_env().any_model); // capture wins over the rest put("STRATA_KVA_CAPTURE", "/tmp/kva-cap"); { const KvaConfig o = kva_config_from_env(); CHECK(o.capture && is(o.decline(eng, 0, 14600, 0, -1, 48), "capture mode")); } diff --git a/src/prefill/prefill.cpp b/src/prefill/prefill.cpp index 7528c5d2..5c858fc9 100644 --- a/src/prefill/prefill.cpp +++ b/src/prefill/prefill.cpp @@ -2720,14 +2720,15 @@ bool Prefill::run_impl(const int64_t* tokens, int64_t n, int64_t pos0, std::stri const int64_t kva_suffix = kva.effective_suffix(); bool kva_routine = false; const char* kva_why_not = kva.decline(kva_eng, pos0, n, LB, LE < 0 ? -1 : LE, (int64_t) g.n_layers, - embd_rows != nullptr, !tap_layers_.empty(), &kva_routine); + embd_rows != nullptr, !tap_layers_.empty(), &kva_routine, + g.n_layers != 48 || g.n_expert != 512); if (kva_why_not == nullptr && (g.n_layers != 48 || g.n_expert != 512)) { - // the split layer and the tail were measured on one geometry; STRATA_KVA=1 is the user's call, but say so once + // STRATA_KVA_ANY_MODEL=1 (research): allowed on a shape KVA was not measured on, with a warning once static std::atomic warned{false}; if (!warned.exchange(true)) - std::fprintf(stderr, "strata prefill: WARNING: KVA was only validated on Qwen3.8-Flash-Next (48 layers, 512 " - "experts); this model has %lld layers and %lld experts\n", (long long) g.n_layers, - (long long) g.n_expert); + std::fprintf(stderr, "strata prefill: WARNING: STRATA_KVA_ANY_MODEL=1: KVA was only measured on Qwen3.8-Flash-Next " + "(48 layers, 512 experts); this model has %lld layers and %lld experts\n", + (long long) g.n_layers, (long long) g.n_expert); } const bool kva_run = kva_why_not == nullptr; const int64_t kva_split = std::min(std::max(kva.split, 1), g.n_layers); @@ -5321,10 +5322,15 @@ bool Prefill::run_impl(const int64_t* tokens, int64_t n, int64_t pos0, std::stri // KVA-8 (observability): a run that was configured for KVA but fell back completely must say so and why, // or an operator cannot tell it apart from a run where the feature was never asked for. (KVA only runs // when STRATA_KVA=1 asked for it, so every reason is worth a line.) - std::fprintf(stderr, "strata prefill: KVA configured but NOT applied: %s (prompt %lld positions, " - "min_prompt %lld, suffix %lld, split %lld)\n", - kva_why_not != nullptr ? kva_why_not : "not applicable", (long long) n, (long long) kva.min_prompt, - (long long) kva_suffix, (long long) kva_split); + if (kva_why_not == kKvaShapeReason) + std::fprintf(stderr, "strata prefill: KVA configured but NOT applied: this model's shape (%lld layers, %lld experts) " + "is not one KVA was measured on (STRATA_KVA_ANY_MODEL=1 allows it)\n", + (long long) g.n_layers, (long long) g.n_expert); + else + std::fprintf(stderr, "strata prefill: KVA configured but NOT applied: %s (prompt %lld positions, " + "min_prompt %lld, suffix %lld, split %lld)\n", + kva_why_not != nullptr ? kva_why_not : "not applicable", (long long) n, (long long) kva.min_prompt, + (long long) kva_suffix, (long long) kva_split); } if (std::getenv("STRATA_STATE_HASH_GDN") != nullptr) { // debug: the GDN states as the prompt path leaves them cudaStreamSynchronize(m.cs); From 76cd7d79701ccfa51a2973e4e88dc93b08ed8f63 Mon Sep 17 00:00:00 2001 From: Niko1221 Date: Mon, 26 Oct 2026 16:55:44 +0100 Subject: [PATCH 4/5] KVA: the one-time note says how many positions were approximated, not where the tail starts (chunks round the frontier down) --- src/prefill/prefill.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/prefill/prefill.cpp b/src/prefill/prefill.cpp index 5c858fc9..08b529e6 100644 --- a/src/prefill/prefill.cpp +++ b/src/prefill/prefill.cpp @@ -5316,7 +5316,7 @@ bool Prefill::run_impl(const int64_t* tokens, int64_t n, int64_t pos0, std::stri "the first %lld positions of this prompt; a follow-up turn that continues from " "this state reads its new tokens exactly on top of them (nothing is approximated " "twice); STRATA_KVA=0 reads every prompt exactly\n", - (long long) kva_split, (long long) kva_frontier); + (long long) kva_split, (long long) kva_positions); } } else if (kva.on && (kva.explicit_on || (n >= kva.min_prompt && !kva_routine && kva_eng.blocked.empty()))) { // KVA-8 (observability): a run that was configured for KVA but fell back completely must say so and why, From bda300508212acd5ce8467c54e66e6bdebd41886 Mon Sep 17 00:00:00 2001 From: Niko1221 Date: Mon, 26 Oct 2026 17:02:12 +0100 Subject: [PATCH 5/5] KVA, when switched on, approximates layers 36-47 with a 2048-token exact tail (the measured configuration); STRATA_KVA_SPLIT / STRATA_KVA_SUFFIX override On the RTX 5070 (--agent-long, 22 tasks) split 36 kept agents at 6/10 (exact 4 and 7) and extraction at 585/606 facts (exact 585) for +11% prompt speed at 14K and +18% at 30K; the split-24 default passed 0/10 agent runs and found 556 facts, a tail of 8192 with split 24 found 574. KVA stays off unless STRATA_KVA=1. docs/KVA.md gives the numbers and the reasons; kva_test checks the opt-in's split 36 / tail 2048 and the overrides. The geometry gate is unchanged. --- docs/KVA.md | 57 +++++++++++++++++----------------- include/strata/prefill/kva.hpp | 16 +++++----- src/prefill/kva_test.cpp | 22 +++++++------ 3 files changed, 50 insertions(+), 45 deletions(-) diff --git a/docs/KVA.md b/docs/KVA.md index 96a1ca3e..45733855 100644 --- a/docs/KVA.md +++ b/docs/KVA.md @@ -1,37 +1,36 @@ # KVA: a faster prompt read for long cold prompts (opt-in, off by default) -KVA ("approximate late-layer state") reads a long prompt exactly through layer 23, then for the late layers -24–47 builds **only their recurrent state** — the GDN recurrence and convolution history, the QSA K/V pages and -the indexer keys — instead of running the whole layer. The attention output, the output projection, the MoE half +KVA ("approximate late-layer state") reads a long prompt exactly through the early layers, then for the late layers +builds **only their recurrent state** — the GDN recurrence and convolution history, the QSA K/V pages and the +indexer keys — instead of running the whole layer. The attention output, the output projection, the MoE half (router, experts, shared expert) and the late layers' expert transfers are skipped, because nothing reads their results. The last positions of the prompt are then read fully, exactly as before, and decode runs the ordinary full path against the state KVA built. **It is off by default.** Unset, the engine is 0.2.6's, prompt path and draft layer included. Set `STRATA_KVA=1` to turn it on (the Strata app shows it as the "Faster long prompts" switch in Expert settings, off until the user turns -it on; the Terminal passes `STRATA_KVA=1`). When it is on, the engine uses the safest configuration measured: an -**8192-token exact tail** (`STRATA_KVA_SUFFIX` changes it). +it on; the Terminal passes `STRATA_KVA=1`). Switched on, it uses the configuration the 0.2.7 quality runs found +cleanest: **split 36** (only layers 36–47 of 48 are approximated) and a **2048-token exact tail**. Both can be +changed with `STRATA_KVA_SPLIT` and `STRATA_KVA_SUFFIX`. -## Why it is off +## Why it is off, and why split 36 -KVA is an approximation, and the 0.2.7 quality runs found where it costs answers. The runs compare KVA off and on on the -same build, greedy, with a noise run (off against off): +KVA is an approximation, and the 0.2.7 quality runs (greedy, same build, KVA off against on, with a noise run of off +against off) show where it costs answers when more layers are approximated: -- **Extraction of many scattered records** (a list of 8 register entries in a 14K document, 6 such tasks at 14K and - 30K): with the 2048-token tail, 4 of 6 tasks passed against 6 of 6 exact, 249 of 270 facts found against 270. - The model silently dropped 3 of 8 records at 14K and, at 30K, started its list with an invented row - (the request's own reference id leaked into the answer). Neither exact run did either. -- **Agent loops with a ~10K-token system prompt** (handbook, tool docs, rules): the first 8192 tokens, about 84 % of - the system prompt, were approximated. Two of three runs had defects the exact runs did not: a final answer that - left out the totals, and a split-order quote with wrong arithmetic (the price of 80 units for 70, a shortfall of - 40 instead of 50). -- Needle retrieval, unanswerable questions, short summaries, code and JSON were unchanged. The speed gain is real: - about 1.3x prompt speed at 14K and 1.5x at 30K on a Tesla P100. - -The 8192-token tail approximates nothing below roughly 16K tokens (the chunks are 8192 tokens, and a chunk is -approximated only when it ends before the tail) and about half of a 30K prompt. It was the only variant that passed the -same runs on the Radeon AI PRO R9700 and tied the exact arm on the RTX 5070. Its speed gain is correspondingly smaller -than the figures above, which were measured with the 2048-token tail. +- **Split 24 (the first version, layers 24–47).** On long extraction tasks (a list of scattered register entries in a + 14K–30K document) 4 of 6 tasks passed against 6 of 6 exact, 249 of 270 facts found against 270 on a Tesla P100: + records were silently dropped (3 of 8 at 14K) and a 30K answer began with an invented row, the request's own reference + id. Agent loops with a ~10K-token system prompt, of which the first 8192 tokens (about 84 %) were approximated, had + a final answer without its totals and a quote with wrong arithmetic. On the RTX 5070 (`--agent-long`, 22 tasks) the + split-24 default passed 0 of 10 agent runs and found 556 extraction facts, by applying the handbook's rules wrongly + in the approximated region; exact passed 4 and 7 of 10 in two runs. A tail of 8192 tokens with split 24 found 574. +- **Split 36.** On the same 22 tasks on the RTX 5070: agents 6 of 10 (exact: 4 and 7), extraction 585 of 606 facts + (exact: 585), with prompt speed +11 % at 14K and +18 % at 30K. This is the cleanest configuration measured and the + one `STRATA_KVA=1` selects. +- Needle retrieval, unanswerable questions, short summaries, code and JSON were unchanged in every variant. +- Split 24 is faster (about 1.3x at 14K and 1.5x at 30K on the P100); the extra layers that stay exact are what buy + the quality. ## When it runs @@ -52,7 +51,7 @@ rules KVA out for the whole run. Without `STRATA_KVA=1` it logs nothing about KV ## The fully computed tail and the draft layer -The last `suffix` positions (8192, or `STRATA_KVA_SUFFIX`) are always read exactly. +The last `suffix` positions (2048, or `STRATA_KVA_SUFFIX`) are always read exactly. The MTP draft layer builds its K/V from the final residuals of the prompt. An approximated chunk has no final residual of its own, so its draft cells are built from the rows that chunk does have. By default nothing about the @@ -100,10 +99,10 @@ there, with a line when it is set). | variable | default | what it does | |---|---:|---| -| `STRATA_KVA` | off | `1` turns KVA on (with the 8192-token tail). Unset or `0`: the exact 0.2.6 prompt read and draft layer | +| `STRATA_KVA` | off | `1` turns KVA on (split 36, 2048-token tail). Unset or `0`: the exact 0.2.6 prompt read and draft layer | | `STRATA_KVA_ANY_MODEL` | off | `1` (research): also run KVA on a model whose shape (layers, experts) it was not measured on, with a warning | -| `STRATA_KVA_SPLIT` | 24 | first approximated layer (zero-based) | -| `STRATA_KVA_SUFFIX` | 8192 | fully computed tail positions; see above. 2048 is the fastest setting and the one the extraction and agent defects were found with | +| `STRATA_KVA_SPLIT` | 36 | first approximated layer (zero-based); 24 approximates more, is faster and lost records and agent loops | +| `STRATA_KVA_SUFFIX` | 2048 | fully computed tail positions; see above | | `STRATA_KVA_MIN_PROMPT` | 4096 | shorter prompts stay on the full path | | `STRATA_KVA_DRAFT_FLOOR` | off | `1`: the draft layer skips the draft cells of approximated chunks (research) | | `STRATA_KVA_SKIP` | state | `moe` (opt-in): the late layers of an approximated chunk run their whole attention half (GDN and QSA outputs into the residual) and skip only the MoE half and its expert transfers; the log line says `mode moe-skip`. Slower than the default state-only form, closer to the exact residual | @@ -117,12 +116,12 @@ there, with a line when it is set). Every read says what it did: -- applied (here with `STRATA_KVA_SUFFIX=2048`): `strata prefill: KVA on: split 24, suffix 2048, approximated 5120 of 7533 positions in 5 of 8 chunks, mode identity` +- applied: `strata prefill: KVA on: split 36, suffix 2048, approximated 5120 of 7533 positions in 5 of 8 chunks, mode identity` - turned away: `strata prefill: KVA configured but NOT applied: (prompt … positions, min_prompt …, suffix …, split …)` A quality comparison needs the KVA arm to say `STRATA_KVA=1`; the control arm leaves it unset or says `STRATA_KVA=0`. -## Measured (Tesla P100 16 GB, Qwen3.8-Flash-Next IQ3_XXS, chunk 1024, expert cache 1500, MTP window 2048) +## Measured with split 24 and a 2048-token tail (Tesla P100 16 GB, Qwen3.8-Flash-Next IQ3_XXS, chunk 1024, expert cache 1500, MTP window 2048) | prompt tokens | control prompt tok/s | KVA prompt tok/s | speedup | approximated | residual error on the suffix | |---:|---:|---:|---:|---:|---:| diff --git a/include/strata/prefill/kva.hpp b/include/strata/prefill/kva.hpp index c98f4352..341cd72f 100644 --- a/include/strata/prefill/kva.hpp +++ b/include/strata/prefill/kva.hpp @@ -16,9 +16,9 @@ // // 0.2.7: the read is OFF unless STRATA_KVA=1 is set (the quality runs found lost records and invented rows in long // extraction and agent tasks, docs/KVA.md). Unset, nothing here runs and the engine is 0.2.6's. Switched on, it applies -// to a cold prompt of STRATA_KVA_MIN_PROMPT tokens or more on one CUDA/HIP device, with an 8192-token exact tail -// (the safest configuration measured). Everything is read from -// the environment once per process. +// to a cold prompt of STRATA_KVA_MIN_PROMPT tokens or more on one CUDA/HIP device, approximating only the layers from +// 36 on, with a 2048-token exact tail (the configuration the 0.2.7 quality runs found cleanest; STRATA_KVA_SPLIT and +// STRATA_KVA_SUFFIX override). Everything is read from the environment once per process. // // What the engine tells this header at start-up (kva_engine()): whether its configuration rules the read out for the // whole process (batch slots, a layer split, a peer device). @@ -52,7 +52,9 @@ inline constexpr bool kKvaBackend = true; inline constexpr const char* kKvaShapeReason = "this model's shape is not one KVA was measured on"; /// The fully computed tail of every approximated read. -inline constexpr int64_t kKvaSuffixDefault = 8192; +inline constexpr int64_t kKvaSuffixDefault = 2048; +/// The first approximated layer when switched on: 36 of 48 (the cleanest measured; 24 lost records and agent loops). +inline constexpr int64_t kKvaSplitDefault = 36; /// What the running engine knows and the environment cannot say. struct KvaEngine { @@ -80,7 +82,7 @@ struct KvaConfig { bool parity = false; ///< STRATA_KVA_PARITY=1: report the state-only path against the full path bool any_model = false; ///< STRATA_KVA_ANY_MODEL=1 (research): also run on a shape KVA was not measured on, with a warning bool draft_floor = false; ///< STRATA_KVA_DRAFT_FLOOR=1: the draft layer skips the cells of approximated chunks (research; default: it reads them) - int64_t split = 24; ///< STRATA_KVA_SPLIT: first approximated layer (zero-based) + int64_t split = kKvaSplitDefault; ///< STRATA_KVA_SPLIT: first approximated layer (zero-based) int64_t suffix_env = 0; ///< STRATA_KVA_SUFFIX: fully computed tail positions (0 = not set: kKvaSuffixDefault) int64_t min_prompt = 4096; ///< STRATA_KVA_MIN_PROMPT: shorter prompts stay on the full path Mode mode = Mode::Identity; ///< STRATA_KVA_MODE=identity|oracle|linear @@ -90,7 +92,7 @@ struct KvaConfig { std::string capture_dir; ///< STRATA_KVA_CAPTURE std::string projector; ///< STRATA_KVA_PROJECTOR - /// The fully computed tail of a read: STRATA_KVA_SUFFIX when set, otherwise 8192. + /// The fully computed tail of a read: STRATA_KVA_SUFFIX when set, otherwise 2048. int64_t effective_suffix() const { return suffix_env > 0 ? suffix_env : kKvaSuffixDefault; } /// Does the draft layer need the cell floor at all? Only when a read can approximate something: KVA on, not ruled @@ -152,7 +154,7 @@ inline KvaConfig kva_config_from_env() { k.on = set && sw[0] != '0'; k.explicit_on = k.on; if (!kKvaBackend) k.on = false; - k.split = kva_env_i64("STRATA_KVA_SPLIT", 24); + k.split = kva_env_i64("STRATA_KVA_SPLIT", kKvaSplitDefault); k.suffix_env = std::max(0, kva_env_i64("STRATA_KVA_SUFFIX", 0)); k.min_prompt = kva_env_i64("STRATA_KVA_MIN_PROMPT", 4096); k.parity = kva_env_flag("STRATA_KVA_PARITY", false); diff --git a/src/prefill/kva_test.cpp b/src/prefill/kva_test.cpp index 9c332185..d87bece4 100644 --- a/src/prefill/kva_test.cpp +++ b/src/prefill/kva_test.cpp @@ -42,7 +42,7 @@ int main() { { const KvaConfig c = kva_config_from_env(); CHECK(!c.on && !c.explicit_on); // unset: off - CHECK(c.split == 24 && c.min_prompt == 4096 && c.suffix_env == 0); + CHECK(c.split == 36 && c.min_prompt == 4096 && c.suffix_env == 0 && kKvaSplitDefault == 36); CHECK(c.mode == KvaConfig::Mode::Identity && c.only == KvaConfig::Only::All && !c.capture); KvaEngine eng; CHECK(!c.wants_cell_floor(eng, true)); @@ -57,10 +57,13 @@ int main() { clear_all(); put("STRATA_KVA", "1"); // everything below runs with KVA switched on - // ---- the tail: 8192 when switched on (the safest configuration measured), or STRATA_KVA_SUFFIX + // ---- switched on: split 36 and a 2048-token tail (the configuration measured), or STRATA_KVA_SPLIT / STRATA_KVA_SUFFIX { const KvaConfig c = kva_config_from_env(); - CHECK(c.on && c.effective_suffix() == kKvaSuffixDefault && kKvaSuffixDefault == 8192); + CHECK(c.on && c.split == 36 && c.effective_suffix() == kKvaSuffixDefault && kKvaSuffixDefault == 2048); + put("STRATA_KVA_SPLIT", "30"); + CHECK(kva_config_from_env().split == 30); + unset("STRATA_KVA_SPLIT"); put("STRATA_KVA_SUFFIX", "3000"); const KvaConfig e = kva_config_from_env(); CHECK(e.effective_suffix() == 3000); @@ -72,17 +75,18 @@ int main() { const KvaConfig c = kva_config_from_env(); KvaEngine eng; bool routine = false; - // a cold 14.6K prompt on one device: applies (14600 > 8192 + 64) + // a cold 14.6K prompt on one device: applies (14600 > 2048 + 64) CHECK(c.decline(eng, 0, 14600, 0, -1, 48) == nullptr); CHECK(c.decline(eng, 0, 14600, 0, 48, 48) == nullptr); // le == n_layers is the whole trunk too // a continuation (turn 2 of a chat, a resumed prompt): routine CHECK(is(c.decline(eng, 9000, 14600, 0, -1, 48, false, false, &routine), "a continuation read (pos0 != 0)") && routine); - // short prompts and a prompt the 8192-token tail would swallow: routine + // short prompts and a prompt the tail would swallow: routine CHECK(is(c.decline(eng, 0, 3000, 0, -1, 48, false, false, &routine), "the prompt is shorter than STRATA_KVA_MIN_PROMPT") && routine); - CHECK(is(c.decline(eng, 0, 8256, 0, -1, 48, false, false, &routine), "the suffix leaves too few positions to approximate") && routine); - CHECK(c.decline(eng, 0, 8257, 0, -1, 48) == nullptr); - put("STRATA_KVA_SUFFIX", "2048"); - { const KvaConfig s = kva_config_from_env(); CHECK(s.decline(eng, 0, 4096, 0, -1, 48) == nullptr); } + CHECK(c.decline(eng, 0, 4096, 0, -1, 48) == nullptr); // 4096 is the first prompt that is long enough + put("STRATA_KVA_SUFFIX", "5000"); // a longer tail swallows a prompt of 5000 and not one of 5065 + { const KvaConfig s = kva_config_from_env(); + CHECK(is(s.decline(eng, 0, 5000, 0, -1, 48, false, false, &routine), "the suffix leaves too few positions to approximate") && routine); + CHECK(s.decline(eng, 0, 5065, 0, -1, 48) == nullptr); } unset("STRATA_KVA_SUFFIX"); // not routine: the user may want to know CHECK(is(c.decline(eng, 0, 14600, 24, -1, 48, false, false, &routine), "a layer split (not the whole trunk)") && !routine);