Conversation cache: --conversation-cache-park-gain N stops a turn that grew a few hundred tokens from rewriting a whole snapshot (RTX 5090 Laptop, IQ3_XXS: 1,418 parks -> 1, 2,251.9 GB -> 3.26 GB host copy at 4.7 % read back; decode -3.4 %, inside the run-to-run spread)

A conversation that only advanced a few hundred tokens since its last park does not need a fresh 3 GB copy: the entry it would supersede still holds this conversation's prefix, and the next turn drops the copy anyway (drop_superseded). Reported in #2264 over 1,418 park events on one long single conversation.

The flag is opt-in and defaults to 0, which parks every turn exactly as before. What this adds is a way to ask for the gate without a rebuild: on the reporter's rig it is 3.26 GB instead of 9.79 GB per benchmark, the superseded class disappears, and the decode median moves -3.4 %, inside that arm's 92.2-124.1 tok/s spread.

parked_prefix_tokens() reports exactly the entries drop_superseded() would remove - it finds them the same way, by the deepest checkpoint the outgoing chain still holds - so the gate cannot name a different entry than the park would drop. conversation_cache_test covers it (7 checks; 4,226 pass).

Fixes #2264
This commit is contained in:
Krypto-Whitehat authored and Niko1221 committed 2026-10-15 05:42:53 +02:00
1 parent 53f72571fb
commit d9cb22d287
3 files changed
+70 -2

No files matched your search

+23 -2
View File
@@ -637,6 +637,11 @@ struct Options {
/// conversation_cache_slots whatever its length, so without it a client that interleaves short side requests
/// with one long conversation evicts the long one (oldest first). 0 = park every conversation.
int64_t conversation_cache_min_tokens = 0;
/// --serve: park only after the conversation has advanced this many tokens past the copy it would supersede.
/// A conversation that moved a few hundred tokens writes a full snapshot again, and the turn after that drops it
/// (drop_superseded) after writing the same bytes once more, so on a long single conversation every turn pays a
/// whole-snapshot copy for nothing. 0 = park every turn, as before.
int64_t conversation_cache_park_gain = 0;
/// --memory-limit-mib N / STRATA_MEMORY_LIMIT_MIB: the total RAM this engine's container or cgroup may use, for a
/// container that cannot see its real limit (0 or unset = none; the flag wins over the variable)
int64_t memory_limit_mib = -1;
@@ -792,6 +797,9 @@ void usage() {
" wins; 0 = none). Parking and session save/restore keep their floor under it\n"
" --conversation-cache-min-tokens N --serve: park only conversations of at least N tokens, so\n"
" short side requests do not take the slots a long one needs (default 0 = park any)\n"
" --conversation-cache-park-gain N --serve: park only after the conversation advanced N tokens past\n"
" the copy it would supersede, so a turn that grew by a few hundred tokens does not\n"
" write a whole snapshot that the next turn drops (default 0 = park every turn)\n"
" --session-min-free-mib N --serve: disk space a SAVE must leave free (default 4096; 0 = no check)\n"
" --session-save-reclaim --serve: reclaim unpinned host caches if SAVE needs RAM (opt-in)\n"
" --vram-elastic --serve (#533, opt-in, NVIDIA): the expert cache in segments (--vram-segment-mib,\n"
@@ -2106,11 +2114,13 @@ int main(int argc, char** argv) {
else if (a == "--prompt-cache") o.prompt_cache = std::max(0, std::atoi(next("--prompt-cache")));
else if (a == "--conversation-cache-mib" || a == "--conversation-cache-slots" ||
a == "--conversation-cache-min-free-mib" || a == "--conversation-cache-min-tokens" ||
a == "--conversation-cache-park-gain" ||
a == "--session-min-free-mib" || a == "--memory-limit-mib") {
const std::string value = next(a.c_str());
int64_t number = 0;
const auto result = std::from_chars(value.data(), value.data() + value.size(), number);
const int64_t limit = (a == "--conversation-cache-slots" || a == "--conversation-cache-min-tokens")
const int64_t limit = (a == "--conversation-cache-slots" || a == "--conversation-cache-min-tokens" ||
a == "--conversation-cache-park-gain")
? INT32_MAX : INT64_MAX / (1024 * 1024);
if (result.ec != std::errc{} || result.ptr != value.data() + value.size() || number < 0 || number > limit) {
std::fprintf(stderr, "%s needs a nonnegative integer within range\n", a.c_str());
@@ -2119,6 +2129,7 @@ int main(int argc, char** argv) {
if (a == "--conversation-cache-mib") o.conversation_cache_mib = number;
else if (a == "--conversation-cache-min-free-mib") o.conversation_cache_min_free_mib = number;
else if (a == "--conversation-cache-min-tokens") o.conversation_cache_min_tokens = number;
else if (a == "--conversation-cache-park-gain") o.conversation_cache_park_gain = number;
else if (a == "--session-min-free-mib") o.session_min_free_mib = number;
else if (a == "--memory-limit-mib") o.memory_limit_mib = number;
else o.conversation_cache_slots = (int) number;
@@ -11020,7 +11031,17 @@ int main(int argc, char** argv) {
resume == req_pin && live_ok)
for (const ConvCheckpoint& c : checks)
if ((int64_t) c.ids.size() == resume && c.pinned) pin_sibling = true;
if ((!from_live || incoming || slot_source >= 0) && !pin_sibling && !park_current(incoming ? incoming->bytes() : 0)) {
// --conversation-cache-park-gain N: a park now would supersede the entry parked_prefix_tokens() reports (the copy
// this conversation wrote last), so a conversation that advanced fewer than N tokens since then keeps it:
// it still holds this conversation's prefix, and re-parking writes the whole snapshot only for the next
// turn to drop it again. 0 (the default) parks every turn, as before.
const size_t park_held = o.conversation_cache_park_gain > 0
? conversations.parked_prefix_tokens(live, live_imgs, checks, cvec_cached)
: 0;
const bool park_worth = o.conversation_cache_park_gain <= 0 || park_held == 0 ||
(int64_t) live.size() >= (int64_t) park_held + o.conversation_cache_park_gain;
if ((!from_live || incoming || slot_source >= 0) && !pin_sibling && park_worth &&
!park_current(incoming ? incoming->bytes() : 0)) {
std::printf("ERR %s\n", err.c_str());
return 1;
}