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/core/verify.cpp b/src/core/verify.cpp index 90c1853a..8a4309e4 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 ======================= @@ -947,10 +949,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); @@ -2391,7 +2398,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(); @@ -2560,13 +2570,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(); @@ -2765,7 +2776,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_; diff --git a/src/program/generate.cpp b/src/program/generate.cpp index 224166d0..9aba23fd 100644 --- a/src/program/generate.cpp +++ b/src/program/generate.cpp @@ -5730,9 +5730,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(); @@ -8943,7 +8947,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; 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: