conversation cache: never park one conversation with another's retained K/V (#1503)
With --batch and conversation parking, a restore keeps the restored K/V (ConversationCache::retain) so the next park of the main session copies only what changed. The reuse tracked a token count (unchanged_tokens), not whose tokens, and copy_from_slot (a batch slot giving its conversation back to the main session) replaced the main session's K/V without dropping the retained image. The next park reused the earlier conversation's K/V for the new one's prefix: a restore of the new conversation then answered from the other one's history. retain() now records the ids of the conversation the K/V belongs to; a park calls take_reuse_for(live), which reuses only the prefix live shares with those ids (nothing when they share none or the ids are unknown), and one stderr line is printed whenever a reuse is cut. copy_from_slot drops the retained image before it replaces the main session's K/V. Same edits in sycl/src/program/generate.cpp. The guard only cuts reuse: at most one full copy of a parked conversation's K/V, the saved bytes are otherwise identical; no flag, no default change. Ported from PR #1503 onto the current tree (the first-stage ids sit after the stages field so the existing aggregate initialisers keep their meaning). conversation_cache_test gains the cross-conversation cases of the PR and the logged sequence (restore P, a slot hands Q to the main session, park Q): another conversation never gets the K/V, a diverging one only the shared prefix on every stage, a slot hand-back drops it, unknown identity is never reused. Removing the ids limit makes the first case fail.
This commit is contained in:
1 parent
6c01f85add
commit
f8ff3420d9
4 files changed
+100
-8
No files matched your search
@@ -9632,7 +9632,12 @@ int main(int argc, char** argv) {
|
||||
~MergeBack() { if (on && !strata::core::conversation_checkpoints_merge(std::move(cs), checks)) checks.clear(); }
|
||||
} merge_back{cs, checks, n_st > 0};
|
||||
const strata::core::ConversationView view{live, live_imgs, n_st > 0 ? cs.stage0 : checks, cvec_cached};
|
||||
auto reuse = conversations.take_reuse();
|
||||
const int64_t retained_n = conversations.retained_tokens();
|
||||
auto reuse = conversations.take_reuse_for(live); // never another conversation's K/V
|
||||
if (retained_n > 0 && reuse.unchanged_tokens < retained_n)
|
||||
std::fprintf(stderr, "strata serve: conversation cache: retained K/V reused for %lld of its %lld tokens "
|
||||
"(the rest is not this conversation's)\n", (long long) reuse.unchanged_tokens,
|
||||
(long long) retained_n);
|
||||
std::vector<strata::core::ConversationKvReuse> stage_reuse = std::move(reuse.stages);
|
||||
stage_reuse.resize(n_st);
|
||||
reuse.stages.clear();
|
||||
@@ -11607,6 +11612,8 @@ int main(int argc, char** argv) {
|
||||
// conversation an idle slot holds reads on from there. Every stage, the same calls.
|
||||
// `at`: one of the slot's checkpoints - only the K/V up to it is copied and its state restored instead
|
||||
auto copy_from_slot = [&](int b, const ConvCheckpoint* at, std::string& e) -> bool {
|
||||
// the main session's K/V is replaced by the slot's: K/V retained from a restore described the old one
|
||||
conversations.take_reuse();
|
||||
const int64_t upto = at != nullptr ? (int64_t) at->ids.size() : (int64_t) bs[(size_t) b].ids.size();
|
||||
if (at != nullptr && at->stage_parts.size() != stages.size()) { e = "a checkpoint without its stage parts"; return false; }
|
||||
if (slot_copy_direct && at == nullptr) {
|
||||
@@ -13048,7 +13055,7 @@ int main(int argc, char** argv) {
|
||||
if (std::getenv("STRATA_SNAPSHOT_FULL_CAPTURE") == nullptr) {
|
||||
std::vector<std::vector<strata::core::ConversationKv>> stage_kv; // every stage's, for its next park
|
||||
for (auto& si : incoming->stage_images) stage_kv.push_back(std::move(si.kv));
|
||||
conversations.retain(std::move(incoming->kv), int64_t(live.size()), std::move(stage_kv));
|
||||
conversations.retain(std::move(incoming->kv), int64_t(live.size()), std::move(stage_kv), live);
|
||||
}
|
||||
incoming.reset(); // Running-state/checkpoint copies are no longer needed.
|
||||
lent_slot = -1; // (the main session holds the parked conversation now)
|
||||
|
||||
Reference in new issue
Block a user