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;