From c2b9b8ea625643efca55a1dd62f01ebe65c79e8a Mon Sep 17 00:00:00 2001 From: aimighty-bot Date: Sun, 1 Nov 2026 14:33:42 +0000 Subject: [PATCH] KVA: interleaved batch reads with a global prompt frontier --- include/strata/prefill/kva.hpp | 4 ++-- include/strata/prefill/prefill.hpp | 3 +++ src/prefill/prefill.cpp | 7 ++++--- src/program/generate.cpp | 5 +++-- 4 files changed, 12 insertions(+), 7 deletions(-) diff --git a/include/strata/prefill/kva.hpp b/include/strata/prefill/kva.hpp index 341cd72f..bdb4d37b 100644 --- a/include/strata/prefill/kva.hpp +++ b/include/strata/prefill/kva.hpp @@ -107,12 +107,12 @@ struct KvaConfig { /// 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 unmeasured_shape = false) const { + bool unmeasured_shape = false, bool cold_read = false) const { if (routine) *routine = false; if (!on) return "off (STRATA_KVA is not 1)"; if (capture) return "capture mode"; if (!e.blocked.empty()) return e.blocked.c_str(); - if (pos0 != 0) { + if (pos0 != 0 && !cold_read) { if (routine) *routine = true; return "a continuation read (pos0 != 0)"; } diff --git a/include/strata/prefill/prefill.hpp b/include/strata/prefill/prefill.hpp index 3e2e13c0..5d497e96 100644 --- a/include/strata/prefill/prefill.hpp +++ b/include/strata/prefill/prefill.hpp @@ -220,6 +220,8 @@ public: /// on), i.e. the first position whose final rows are real; 0 when that run approximated nothing. Raised chunk by /// chunk, before on_chunk reports the chunk, so the draft layer can skip the chunk's cells. Reset by every run. int64_t kva_floor() const { return kva_floor_; } + /// KVA: a cold prompt read in several batch pieces; the full part length, so the frontier spans the whole part. + void set_kva_total(int64_t n) { kva_total_ = n; } /// Grows this stage's (and the later stages') pinned upload slots to what a text chunk of `T` tokens uploads, so /// a run of such chunks allocates no pinned memory (STRATA_PIPED_CONCURRENT: an allocation or free on the read's /// thread takes the driver's lock). Before the run, with nothing of this path in flight. False: a slot could @@ -317,6 +319,7 @@ private: bool single_chunk_ = false; ///< a later stage: the prompt is one chunk (set by the stage before) int64_t stopped_at_ = -1; ///< stop_at_chunk ended the last run after this many tokens (-1: it read all) int64_t kva_floor_ = 0; ///< kva_floor() + int64_t kva_total_ = 0; ///< set_kva_total() bool pipe_chunked_ = false; ///< a later stage: the chunks came from a --prefill-pipe plan (set likewise) int64_t force_chunk_ = 0; ///< force_chunk, for the next run (0: none) bool force_single_ = false, force_planned_ = false; diff --git a/src/prefill/prefill.cpp b/src/prefill/prefill.cpp index 5182a41c..ebf21e0e 100644 --- a/src/prefill/prefill.cpp +++ b/src/prefill/prefill.cpp @@ -2952,7 +2952,7 @@ bool Prefill::run_impl(const int64_t* tokens, int64_t n, int64_t pos0, std::stri 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); + g.n_layers != 48 || g.n_expert != 512, kva_total_ > 0); if (kva_why_not == nullptr && (g.n_layers != 48 || g.n_expert != 512)) { // STRATA_KVA_ANY_MODEL=1 (research): allowed on a shape KVA was not measured on, with a warning once static std::atomic warned{false}; @@ -2963,11 +2963,12 @@ bool Prefill::run_impl(const int64_t* tokens, int64_t n, int64_t pos0, std::stri } 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); + const int64_t kva_total = kva_total_ > 0 ? kva_total_ : n; + const int64_t kva_frontier = std::max(0, kva_total - kva_suffix); static KvaTap kva_capture; // process-wide: one cold read at a time, so the per-layer position is enough static KvaTap kva_oracle; int64_t kva_positions = 0, kva_chunks = 0; // what actually took the approximate path (diagnostics) - kva_floor_ = 0; // this run's: raised below as chunks are approximated + if (kva_total_ <= 0 || pos0 == 0) kva_floor_ = 0; // one cold part keeps its floor across pieces if (kva.capture && kva_capture.f.empty()) kva_capture.open(kva.capture_dir, kva.capture_fp32); if (kva.capture && pos0 == 0 && hand_in_ == nullptr) kva_capture.begin_prompt(); // one prompt per capture if (kva_run && kva.mode == KvaConfig::Mode::Oracle && kva_oracle.f.empty()) diff --git a/src/program/generate.cpp b/src/program/generate.cpp index ed008f75..710f6ad2 100644 --- a/src/program/generate.cpp +++ b/src/program/generate.cpp @@ -15656,10 +15656,10 @@ int main(int argc, char** argv) { return v != nullptr ? std::max(0.0, std::atof(v)) : 0.0; }(); const bool pr_on = piped && pr_piece > 0; - const bool kva_whole = kva_cold(a0, b0); + sp.set_kva_total(kva_cold(a0, b0) ? b0 - a0 : 0); auto piece_now = [&]() -> int64_t { return pr_on ? strata::program::piped_read_piece(pr_piece, C) - : strata::program::batch_read_piece(o.batch > 0 && !kva_whole, piped, !stages.empty(), batch_on(), C, + : strata::program::batch_read_piece(o.batch > 0, piped, !stages.empty(), batch_on(), C, yield_chunks); }; // the slots decode through the pipeline for budget_ms, drained before the next read @@ -15917,6 +15917,7 @@ int main(int argc, char** argv) { for (const int64_t to : cuts) { if (to <= at) continue; err.clear(); + if (!kva_cold(at, to)) sp.set_kva_total(0); const bool win = windows_ok(at, to) && !kva_cold(at, to); if (win && !refill(err)) { std::printf("ERR refilling a lent slot failed: %s\n", err.c_str());