From 35435e3d26809167ba32e2e00c53f2ae20d36460 Mon Sep 17 00:00:00 2001 From: Benjamin Larsson Date: Wed, 7 Oct 2026 00:57:49 +0200 Subject: [PATCH 1/5] verify: the batched K/V append writes a streamed K8V4 state's host copy (#1188, #1169) #783's batched append of a verify window's cells passed no host copy for the hybrid K8V4 format (int8 and q4_0 pass st.host, and the per-token path passes K8V4's two halves). With --kv-resident, a block that was not resident when its cells were appended came back from the host copy without them, so the tokens a window wrote were lost: free generation degenerated into repetition (#1188, #1135) and parked conversations lost their decode-written turns (#1169). The batched path now passes the same host halves as the per-token path. --- src/core/verify.cpp | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/core/verify.cpp b/src/core/verify.cpp index 2811c9cf..0acea637 100644 --- a/src/core/verify.cpp +++ b/src/core/verify.cpp @@ -947,10 +947,15 @@ bool Verifier::record_window(int T, cudaStream_t cs, std::string& err) { const float* kc_b = kcur_ + tb * NKV * HD; const float* vc_b = vcur_ + tb * NKV * HD; if (st.kv_hybrid) { // K8V4: the unused half's lanes folded onto the used pool (layer.cpp) + // #1188: a streamed state's host copy gets the window's cells too, as the per-token path below + // writes them - without it a block that was not resident when its cells were appended came + // back from the host copy without them, and free generation degenerated into repetition + const KvHostPools hk = kv_hybrid_k_half(st.host), hv = kv_hybrid_v_half(st.host); + const bool mirror = st.host.present(); kv_append_q8_steps(st.k_q, st.k_q, st.k_scale, st.k_scale, st.page_table, step_b, kStepCount, - kc_b, kc_b, (int) (NKV * HD), n, s, cs, nullptr); + kc_b, kc_b, (int) (NKV * HD), n, s, cs, mirror ? &hk : nullptr); kv_append_q4_steps(st.v_q4, st.v_q4, st.page_table, step_b, kStepCount, n, vc_b, vc_b, s, cs, - nullptr); + mirror ? &hv : nullptr); } else if (st.kv_q4) kv_append_q4_steps(st.k_q4, st.v_q4, st.page_table, step_b, kStepCount, n, kc_b, vc_b, s, cs, &st.host); From 7ef55e19599f1ef009894b3260b40cdbe0d59ba9 Mon Sep 17 00:00:00 2001 From: wOvAN Date: Wed, 7 Oct 2026 00:57:49 +0200 Subject: [PATCH 2/5] verify batch: service the doorbell graph per-window (ar_on), not per-process (#1118) A stage that was 100% resident at init takes the doorbell graph while a prompt loan/shrink/swap is in flight (ar_off_), and that graph waits on host doorbells the skipped service never raises: one GPU spins at 100% while the host sits in cudaStreamSynchronize, until the watchdog fires with the previous stage's stale label. Key the all-resident service skip in run_slot_rows, batch_poll and service off ar_on(), as run() already does. PR #1102 (win10ogod) is the same fix for run_slot_rows and batch_poll; this one also covers service(). Co-authored-by: win10ogod --- src/core/verify.cpp | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/src/core/verify.cpp b/src/core/verify.cpp index 0acea637..6cb77fb2 100644 --- a/src/core/verify.cpp +++ b/src/core/verify.cpp @@ -2358,7 +2358,10 @@ bool Verifier::run_slot_rows(const int* rows, int S, const int32_t* tokens, cons // ("verify batch: layer K never rang (graph finished)" - K is that stage's first layer), and with it the graph // sits on the PLE wait until the 20 s timeout. As run() does: raise the PLE flag the graph's first wait reads, // let the graph run to the end, and skip the host's per-layer service (there is nothing to serve). - if (all_resident_) { + // #646 + #871: the skip is per-window (ar_on), not per-process (all_resident_): a stage that was 100% + // resident at init takes the doorbell graph while a prompt loan/shrink/swap is in flight (ar_off_), and that + // graph waits on host doorbells the skipped service never raises (GPU at 100%, host in the sync below). + if (ar_on()) { if (ss_->ple.ready() && ple_stage()) { std::atomic_thread_fence(std::memory_order_seq_cst); _mm_sfence(); @@ -2527,13 +2530,14 @@ int Verifier::batch_poll(PoolMultiFn pool, void* user, std::string& err) { const int S = last_t_; // #646: an all-resident stage's graph raises no host doorbells (see run_slot_rows): nothing to serve per layer, // so the poll is just "has the graph finished" - except the PLE flag, which the graph's first wait reads and - // only the host can raise (the same raise run_slot_rows makes). - if (all_resident_ && ss_->ple.ready() && ple_stage()) { + // only the host can raise (the same raise run_slot_rows makes). Per-window (ar_on): with a loan in flight the + // doorbell graph needs the per-layer service below. + if (ar_on() && ss_->ple.ready() && ple_stage()) { std::atomic_thread_fence(std::memory_order_seq_cst); _mm_sfence(); *h_flag_ = 1; } - while (!all_resident_ && b_k_ < b_steps_) { + while (!ar_on() && b_k_ < b_steps_) { const uint32_t want = (uint32_t) (b_k_ + 1); if (*seq < want) { const auto now = Clock::now(); @@ -2732,7 +2736,8 @@ int Verifier::service(PoolMultiFn pool, void* user, std::string& err) { ms_host += ms_since(tp); return true; }; - if (all_resident_) { // the zero-doorbell graph never rings: it waits only for flag 1 (stage 0's PLE rows) + if (ar_on()) { // the zero-doorbell graph never rings: it waits only for flag 1 (stage 0's PLE rows). + // Per-window: with a loan in flight (ar_off_) the doorbell graph needs the per-layer service below. if (!gather_ple()) return -1; *(volatile uint32_t*) h_flag_ = 1; fl_k_ = fl_total_; From 0a22c467471aeac78431ba107da8c4de54ccd750 Mon Sep 17 00:00:00 2001 From: Niko1221 Date: Wed, 7 Oct 2026 00:58:23 +0200 Subject: [PATCH 3/5] verify: a batch's GDN output is quantized to q8_1 under STRATA_QFUSE too (#1139) STRATA_QFUSE=1 (the gfx1151 arch default) has gdn_step_norm_multi write the out-projection's q8_1 image itself and skips native_quantize_q8_1. The batched recurrence (batch_rec_) calls gdn_step_norm_multi once per slot with no q8_1 destination, so with --batch the projection read stale bytes and every concurrent answer was corrupt. The batched case now quantizes as without QFUSE; the fused image holds the same bytes (gr_parity), so batched output equals solo. Single-request paths are unchanged. --- src/core/verify.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/core/verify.cpp b/src/core/verify.cpp index 6cb77fb2..1a971f59 100644 --- a/src/core/verify.cpp +++ b/src/core/verify.cpp @@ -884,7 +884,9 @@ bool Verifier::record_window(int T, cudaStream_t cs, std::string& err) { gdn_step_norm_multi(state, hb, (int) C, gate, beta, z_, (const float*) wnm->data, EPS, y_, (int) HK, (int) HV, te, self_commit ? one_ : nullptr, cs, tb, g_qfuse() ? (void*) xq_ : nullptr); stamp(l, 6, grp); - if (!g_qfuse()) native_quantize_q8_1(y_ + (size_t) tb * ZV, xq_, (int) ZV, n, cs); // STRATA_QFUSE: done above + // STRATA_QFUSE: done above by gdn_step_norm_multi - but a batch's per-slot recurrence passes it no q8_1 + // destination (#1139: the out-projection read stale bytes), so that case quantizes here as without it + if (!g_qfuse() || batch_rec_) native_quantize_q8_1(y_ + (size_t) tb * ZV, xq_, (int) ZV, n, cs); native_mmvq(wout->native_type, wout->native_data, xq_, bo_ + tb * N, (int) ZV, (int) N, n, cs); } else { // ======================= QSA ======================= From 26609953a53321e0aa21146f19d425ad9734ae5e Mon Sep 17 00:00:00 2001 From: Niko1221 Date: Wed, 7 Oct 2026 00:58:23 +0200 Subject: [PATCH 4/5] tools/batch_test.py: --long-tokens N reads the last prompt from N tokens of filler beside the earlier slots' windows (a prompt loan under --batch) --- tools/batch_test.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/tools/batch_test.py b/tools/batch_test.py index ea384d9b..5dd61f54 100644 --- a/tools/batch_test.py +++ b/tools/batch_test.py @@ -75,6 +75,9 @@ def main(): ap.add_argument("--keys", default="", help='sampling keys for every request, e.g. "temperature=0.7 top_k=20"') ap.add_argument("--extra", default="", help='more engine arguments in one string, e.g. "--adapt-every 1000000"') ap.add_argument("--dump", default="", help="write every slot's tokens (solo and batch) as JSON: an A/B of two engine builds") + ap.add_argument("--long-tokens", type=int, default=0, + help="the LAST prompt is read from about this many tokens of filler text first: its chunks run " + "beside the earlier slots' windows (the prompt path lends experts then)") ap.add_argument("--mt-min", default="1", help="STRATA_IQ_MT_MIN for the engine (1: exact; empty: the default)") a = ap.parse_args() cfg = json.loads(Path(a.config).read_text()) @@ -82,6 +85,12 @@ def main(): prompts = [] for q in QUESTIONS[: a.n]: text = f"<|im_start|>user\n{q}<|im_end|>\n<|im_start|>assistant\n\n\n\n\n" + if a.long_tokens and q is QUESTIONS[a.n - 1]: + para = ("The committee reviewed item %d of the long agenda, noting the budget, the schedule and the open " + "risks, and asked the staff to report back next quarter. ") + per = max(1, len(tok.encode(para % 100))) + filler = "".join(para % i for i in range(a.long_tokens // per + 1)) + text = f"<|im_start|>user\n{filler}\n\n{q}<|im_end|>\n<|im_start|>assistant\n\n\n\n\n" prompts.append(tok.encode(text, parse_special=True)) env = {"STRATA_DECODE_TIMING": "1", **({"STRATA_VERIFY_PROFILE": "1"} if os.environ.get("PROF") else {})} if a.mt_min: From 56ae98dde9fbab12cfd1fb736b3e613d086f2112 Mon Sep 17 00:00:00 2001 From: Niko1221 Date: Wed, 7 Oct 2026 02:29:11 +0200 Subject: [PATCH 5/5] --kv-grow: STRATA_KV_GROW_HOLD=1 keeps the K/V as long as a parked conversation (#1128) kvg_trim took only the short request's cell count, so every short request in a long conversation's session shrank the K/V to 8192 cells and gave the expert slots back, and the conversation's next turn grew it again and moved the experts again (a parked conversation is restored through kvg_ensure). With STRATA_KV_GROW_HOLD=1 the trim keeps the longest parked conversation's cells (+256) while it is parked. Opt-in: the default trims to the request alone as in 0.1.40, so the K/V gives back its VRAM as before. Output is unchanged; only --kv-grow is affected. --- include/strata/core/conversation_cache.hpp | 6 ++++++ src/program/generate.cpp | 12 +++++++++--- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/include/strata/core/conversation_cache.hpp b/include/strata/core/conversation_cache.hpp index c28ae01f..86e04c6b 100644 --- a/include/strata/core/conversation_cache.hpp +++ b/include/strata/core/conversation_cache.hpp @@ -191,6 +191,12 @@ public: size_t bytes() const { return bytes_ + reuse_.bytes(); } size_t size() const { return entries_.size(); } size_t evictions() const { return evictions_; } + // the longest parked conversation, in tokens (--kv-grow keeps the K/V that long while it could be restored) + int64_t longest_tokens() const { + int64_t n = 0; + for (const auto& e : entries_) n = std::max(n, (int64_t) e.live.ids.size()); + return n; + } // Retain only the restored K/V buffers, not duplicate running checkpoints. // This optimization never evicts a parked conversation to make itself fit. diff --git a/src/program/generate.cpp b/src/program/generate.cpp index 04b30939..24114a61 100644 --- a/src/program/generate.cpp +++ b/src/program/generate.cpp @@ -5433,9 +5433,13 @@ int main(int argc, char** argv) { }; // A request that needs far fewer cells than the K/V holds gives the rest back: the slots refill with the // profile's hottest experts the GPU does not hold. Run on a quiet device (as kvg_ensure's `quiesce`). - auto kvg_trim = [&](int64_t cells) -> bool { + auto kvg_trim = [&](int64_t cells, int64_t hold) -> bool { // `hold`: the cells a parked conversation could need if (!kvg.on) return true; - const int64_t target = std::max(kvg.step, (cells + kvg.step - 1) / kvg.step * kvg.step); + // #1128: a parked conversation comes back through kvg_ensure's grow, so a K/V that fell to a short request's + // size is grown again (its experts shuffled again) after every short request in between: the K/V keeps the + // longest parked conversation's cells while it is parked + const int64_t want = std::max(cells, hold); + const int64_t target = std::max(kvg.step, (want + kvg.step - 1) / kvg.step * kvg.step); if (kvg.cells < target + 2 * kvg.step || kvg.lo >= kvg.top) return true; strata::core::qsa_kv_elastic_shrink(target, [&](strata::core::VmmChunk h) { kvg.spare.push_back(h); }); kvg.cells = strata::core::qsa_kv_elastic_cells(); @@ -8637,7 +8641,9 @@ int main(int argc, char** argv) { // so cells past this prompt's are no longer anyone's - far more than it needs go back to the cache if (kvg.on) { kv_quiesce(); - if (!kvg_trim(n + 256)) { + // STRATA_KV_GROW_HOLD=1 (opt-in): keep the K/V as long as the longest parked conversation; default: trim to this request alone, as 0.1.40 + static const bool hold_on = [] { const char* v = std::getenv("STRATA_KV_GROW_HOLD"); return v != nullptr && std::atoi(v) != 0; }(); + if (!kvg_trim(n + 256, hold_on ? conversations.longest_tokens() + 256 : 0)) { std::printf("ERR the K/V could not give its VRAM back to the expert cache\n"); std::fflush(stdout); return 1;