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:
1 parent
53f72571fb
commit
d9cb22d287
3 files changed
+70
-2
No files matched your search
@@ -335,6 +335,26 @@ public:
|
||||
return dropped;
|
||||
}
|
||||
size_t superseded() const { return superseded_; }
|
||||
/// --conversation-cache-park-gain N: the longest parked entry this conversation's outgoing state supersedes, in
|
||||
/// tokens, 0 when it has none. It is the entry drop_superseded() would remove, so the caller can tell whether a
|
||||
/// park now would only rewrite what a turn from now drops: the prefix such an entry holds is this conversation's
|
||||
/// prefix, and the conversation only appends, so the copy stays usable for the reuse it was made for.
|
||||
size_t parked_prefix_tokens(const std::vector<int32_t>& ids, const std::vector<ConversationImageKey>& images,
|
||||
const std::vector<ConversationCheckpoint>& checkpoints, bool cvec) const {
|
||||
size_t longest = 0;
|
||||
for (const auto& e : entries_) {
|
||||
if (e.cvec != cvec) continue;
|
||||
const ConversationCheckpoint* deepest = nullptr;
|
||||
for (const auto& c : e.checkpoints)
|
||||
if (!deepest || c.ids.size() > deepest->ids.size()) deepest = &c;
|
||||
if (!deepest || deepest->ids.empty()) continue;
|
||||
bool holds = deepest->ids == ids && deepest->imgs == images;
|
||||
for (const auto& k : checkpoints)
|
||||
if (!holds && k.ids == deepest->ids && k.imgs == deepest->imgs) holds = true;
|
||||
if (holds) longest = std::max(longest, deepest->ids.size());
|
||||
}
|
||||
return longest;
|
||||
}
|
||||
|
||||
bool put(SavedConversation&& image, size_t held = 0) {
|
||||
const size_t n = image.bytes();
|
||||
|
||||
@@ -282,6 +282,33 @@ int main() {
|
||||
check(!cache.put(std::move(huge)) && cache.size() == 5 && cache.superseded() == 1,
|
||||
"an oversized put drops nothing");
|
||||
}
|
||||
{
|
||||
// --conversation-cache-park-gain N: parked_prefix_tokens reports what a park now would supersede, so a
|
||||
// conversation that advanced fewer than N tokens since that copy keeps it instead of writing the whole
|
||||
// snapshot again for the next turn to drop. It must name exactly the entries drop_superseded() would remove.
|
||||
auto cp = [](std::vector<int32_t> ids) { ConversationCheckpoint c; c.ids = std::move(ids); return c; };
|
||||
ConversationCache cache(1 << 20, 8);
|
||||
check(cache.parked_prefix_tokens({1, 2, 3}, {}, {}, true) == 0, "an empty cache supersedes nothing");
|
||||
|
||||
const std::vector<int32_t> live_ids = {1, 2, 3, 4, 60, 70};
|
||||
const std::vector<ConversationCheckpoint> chain = {cp({1, 2, 3, 4}), cp({1, 2, 3, 4, 60})};
|
||||
SavedConversation same = image({1, 2, 3, 4, 60, 70});
|
||||
same.checkpoints = chain;
|
||||
check(cache.put(std::move(same)), "park a conversation with a checkpoint chain");
|
||||
check(cache.parked_prefix_tokens(live_ids, {}, chain, true) == 5,
|
||||
"the entry the outgoing chain supersedes is reported by its deepest checkpoint");
|
||||
|
||||
const std::vector<ConversationCheckpoint> other_chain = {cp({1, 2, 3})};
|
||||
check(cache.parked_prefix_tokens({1, 2, 3, 40, 50}, {}, other_chain, true) == 0,
|
||||
"a conversation sharing only the root supersedes nothing");
|
||||
check(cache.parked_prefix_tokens(live_ids, {}, chain, false) == 0,
|
||||
"the other steering mode is a different entry");
|
||||
check(cache.parked_prefix_tokens({1, 2, 3, 4, 60, 70, 71}, {}, chain, true) == 5,
|
||||
"an appended tail keeps the reported depth at the parked one");
|
||||
SavedConversation no_cp = image({1, 2, 3, 9});
|
||||
check(cache.put(std::move(no_cp)) && cache.parked_prefix_tokens({1, 2, 3, 9}, {}, {}, true) == 0,
|
||||
"an entry without checkpoints is never reported");
|
||||
}
|
||||
{
|
||||
ConversationCache disabled(0,4), no_slots(1024,0);
|
||||
check(!disabled.enabled() && !no_slots.enabled(), "both disable switches");
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user