--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.
This commit is contained in:
Niko1221 committed 2026-10-07 03:34:33 +02:00
1 parent 26609953a5
commit 56ae98dde9
2 files changed
+15 -3

No files matched your search

+9 -3
View File
@@ -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<int64_t>(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<int64_t>(cells, hold);
const int64_t target = std::max<int64_t>(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;