diff --git a/cmake/backends/cpu.cmake b/cmake/backends/cpu.cmake index 40df15aa3a..5e5bdf3ddd 100644 --- a/cmake/backends/cpu.cmake +++ b/cmake/backends/cpu.cmake @@ -74,6 +74,10 @@ if(STRATA_BUILD_TESTS OR STRATA_BUILD_CONVERSATION_TESTS) add_executable(message_boundary_test src/program/message_boundary_test.cpp) target_include_directories(message_boundary_test PRIVATE include) add_test(NAME message_boundary_test COMMAND message_boundary_test) + # --serve: the live branch a request leaves is only the reply it replaces (not parked) + add_executable(reply_branch_test src/program/reply_branch_test.cpp) + target_include_directories(reply_branch_test PRIVATE include) + add_test(NAME reply_branch_test COMMAND reply_branch_test) # a layer split's ring override and the lend slots sized with it: the RAM copy must keep every borrowed slot (CPU) add_executable(split_ring_test src/program/split_ring_test.cpp) target_include_directories(split_ring_test PRIVATE include) diff --git a/include/strata/program/reply_branch.hpp b/include/strata/program/reply_branch.hpp new file mode 100644 index 0000000000..510f3dcd48 --- /dev/null +++ b/include/strata/program/reply_branch.hpp @@ -0,0 +1,42 @@ +#pragma once + +#include +#include +#include + +namespace strata::program { + +// --serve: is the live conversation's tail past `resume` only the reply this prompt replaces? +// +// A request that resumes from a checkpoint (not from the live session) parks the live branch first, so a later request +// can come back to it. When the checkpoint is the live branch's own turn checkpoint - the history before its last +// assistant turn - and the prompt opens an assistant turn at that same place again, the client has re-sent the history +// with that reply dropped, edited or about to be regenerated (an OpenAI-style client sends every earlier reply back +// without its reasoning, so this is every next turn of such a client). The branch it leaves holds nothing but that one +// reply, which no request of the same client sends back as it was: parking it copies the whole conversation to host +// memory on the request's path (~130-290 ms on an R9700) for a copy that only ever gets dropped as superseded. +// +// True when all of these hold: +// - `live` continues past `resume` (live[resume] exists) with a turn opening there (live[resume] == turn_token) and +// no other turn after it: what follows is one assistant turn, its header and the reply generated after it; +// - the prompt opens a turn at the same place with the same role token (ids[resume] == turn_token, +// ids[resume + 1] == live[resume + 1]): the same assistant turn, re-sent or opened again; +// - live and the prompt agree on [0, resume) (the caller resumes from a checkpoint of exactly those tokens; checked +// here again so the rule stands alone). +// Pictures are the caller's: it asks only when the live branch has none at or past `resume`. +template +bool live_tail_is_replaced_reply(const std::vector& live, const std::vector& ids, int64_t resume, + int64_t turn_token) { + if (turn_token < 0 || resume < 1) return false; + const auto r = static_cast(resume); + if (live.size() < r + 2 || ids.size() < r + 2) return false; + if (static_cast(live[r]) != turn_token || static_cast(ids[r]) != turn_token) return false; + if (static_cast(live[r + 1]) != static_cast(ids[r + 1])) return false; + for (std::size_t i = r + 1; i < live.size(); ++i) + if (static_cast(live[i]) == turn_token) return false; + for (std::size_t i = 0; i < r; ++i) + if (static_cast(live[i]) != static_cast(ids[i])) return false; + return true; +} + +} // namespace strata::program diff --git a/src/program/generate.cpp b/src/program/generate.cpp index e40ce74d3d..369827ee7d 100644 --- a/src/program/generate.cpp +++ b/src/program/generate.cpp @@ -110,6 +110,7 @@ #include "strata/program/prefill_plan.hpp" #include "strata/program/draft_kv_plan.hpp" #include "strata/program/message_boundary.hpp" +#include "strata/program/reply_branch.hpp" #include "strata/spec/draft_policy.hpp" #include "strata/spec/draft_vocab.hpp" #include "strata/spec/window_rule.hpp" @@ -16417,6 +16418,25 @@ 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; + // The live branch past this request's resume point is only the reply this prompt replaces: the request resumes from + // the live branch's own turn checkpoint and opens the same assistant turn there again (an OpenAI-style client's + // next turn sends the reply back without its reasoning; an edited reply; a regenerate). That branch is not parked: + // no request of the client sends the reply back as it was, and its park is a copy of the whole conversation on + // this request's path that the next park drops as superseded (reply_branch.hpp). A real switch (another + // conversation, a parked one coming back, a batch slot, a borrow, another cvec) parks as before. + bool reply_replaced = false; + if (!from_live && !incoming && !borrow && slot_source < 0 && lent_slot < 0 && live_ok && !pin_sibling && + want_cvec == cvec_cached && resume > 0 && imgs_below(live_imgs, resume) == live_imgs) + for (const ConvCheckpoint& c : checks) + if ((int64_t) c.ids.size() == resume && starts_with(c.ids, c.imgs) && + strata::program::live_tail_is_replaced_reply(live, ids, resume, o.turn_token)) { + reply_replaced = true; + break; + } + if (reply_replaced) + std::fprintf(stderr, "strata serve: conversation cache: the reply this request replaces is not parked (%lld " + "tokens past the turn checkpoint at %lld)\n", (long long) ((int64_t) live.size() - resume), + (long long) resume); // --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 @@ -16461,7 +16481,7 @@ int main(int argc, char** argv) { return 1; } } - } else if ((!from_live || incoming || slot_source >= 0) && !pin_sibling && park_worth && + } else if ((!from_live || incoming || slot_source >= 0) && !pin_sibling && !reply_replaced && park_worth && !park_current(incoming ? incoming->bytes() : 0)) { std::printf("ERR %s\n", err.c_str()); return 1; diff --git a/src/program/reply_branch_test.cpp b/src/program/reply_branch_test.cpp new file mode 100644 index 0000000000..25dc1f58fa --- /dev/null +++ b/src/program/reply_branch_test.cpp @@ -0,0 +1,71 @@ +// (asserts stay on in a Release build: the test is its own check) +#undef NDEBUG +#include "strata/program/reply_branch.hpp" + +#include +#include +#include +#include + +using strata::program::live_tail_is_replaced_reply; + +namespace { +constexpr int64_t S = 900; // <|im_start|> +constexpr int64_t E = 901; // <|im_end|> +constexpr int64_t SYS = 10, USER = 11, ASSIST = 12; + +std::vector cat(std::initializer_list> parts) { + std::vector v; + for (const auto& p : parts) v.insert(v.end(), p.begin(), p.end()); + return v; +} +std::vector to32(const std::vector& v) { return std::vector(v.begin(), v.end()); } +} // namespace + +int main() { + const std::vector sys = {S, SYS, 1, 2, 3, E}; + const std::vector user1 = {S, USER, 4, 5, 6, E}; + const std::vector head = {S, ASSIST, 7}; // the generation header (<|im_start|>assistant\n...) + const std::vector reasoning = {20, 21, 22, 23}; + const std::vector answer = {30, 31, 32}; + const std::vector user2 = {S, USER, 8, 9, E}; + const auto history = cat({sys, user1}); + const int64_t turn = static_cast(history.size()); // the turn checkpoint: [0, turn) + // live: the last request's prompt + its reply (reasoning and answer), the last emitted token not fed + const auto live = to32(cat({history, head, reasoning, answer})); + int cases = 0; + + // (b) reasoning dropped: the history renders the assistant turn with the answer only, then the next user turn + assert(live_tail_is_replaced_reply(live, cat({history, {S, ASSIST}, answer, {E}, user2, head}), turn, S)); ++cases; + // (c) one word of the answer changed + assert(live_tail_is_replaced_reply(live, cat({history, {S, ASSIST}, {30, 99, 32}, {E}, user2, head}), turn, S)); ++cases; + // regenerate: the same history again, a new assistant turn opens where the dropped one was + assert(live_tail_is_replaced_reply(live, cat({history, head}), turn, S)); ++cases; + // the live branch was only a prompt read (prefill), nothing generated past the header + assert(live_tail_is_replaced_reply(to32(cat({history, head})), cat({history, {S, ASSIST}, answer, {E}, user2, head}), turn, + S)); ++cases; + + // a new USER turn where the reply was (the client left the reply out entirely): another kind of request, park as before + assert(!live_tail_is_replaced_reply(live, cat({history, user2, head}), turn, S)); ++cases; + // the live tail holds more than one turn (a tool result, a user turn after the reply): not just one reply + assert(!live_tail_is_replaced_reply(to32(cat({history, head, answer, {E}, user2, head, answer})), + cat({history, {S, ASSIST}, answer, {E}, user2, head}), turn, S)); ++cases; + // resume not at a turn opening of the live branch (a periodic or root checkpoint in the middle of a message) + assert(!live_tail_is_replaced_reply(live, cat({history, head}), turn - 2, S)); ++cases; + // the prompt differs before resume (another conversation): never + { + auto other = cat({history, head}); + other[2] = 77; + assert(!live_tail_is_replaced_reply(live, other, turn, S)); ++cases; + } + // the live branch ends at the checkpoint (nothing past it) or one token past it: nothing to say about a reply + assert(!live_tail_is_replaced_reply(to32(history), cat({history, head}), turn, S)); ++cases; + assert(!live_tail_is_replaced_reply(to32(cat({history, {S}})), cat({history, head}), turn, S)); ++cases; + // the prompt ends right at the turn opening (one token past resume): no role to compare + assert(!live_tail_is_replaced_reply(live, cat({history, {S}}), turn, S)); ++cases; + // no turn token configured, resume 0 + assert(!live_tail_is_replaced_reply(live, cat({history, head}), turn, -1)); ++cases; + assert(!live_tail_is_replaced_reply(live, cat({history, head}), 0, S)); ++cases; + std::cout << "SUMMARY reply_branch cases=" << cases << " passed\n"; + return 0; +}