diff --git a/docs/PROMPT_CACHE_TAIL.md b/docs/PROMPT_CACHE_TAIL.md new file mode 100644 index 00000000..836e3b9e --- /dev/null +++ b/docs/PROMPT_CACHE_TAIL.md @@ -0,0 +1,33 @@ +# Optional checkpoint near the prompt tail + +`--prompt-cache-tail` saves at most one extra mid-prompt checkpoint near the end +of a request, at a boundary the batched prefill already reaches. It is off by +default and applies to single-GPU serve with positive `--prompt-cache` and +`--prompt-cache-every` values. + +A branch can share most of a prompt but diverge before its final cached state. +If the periodic checkpoint before that branch is far away, the engine has to +read the gap again. The extra checkpoint can reduce that gap. It uses the +existing bounded checkpoint cache; it does not add another unbounded history. + +The tail is the first chunk boundary within one chunk of the prompt's end, where the chunk is the size that chunk +really had (`--prefill auto` chunks differ from `--prefill N`). The tail checkpoint is evicted first (only one stays +alive, the root is never dropped), so it never pushes out a periodic checkpoint when `--prompt-cache` is full. + +The option does not split prefill chunks or alter the periodic checkpoint +schedule. It selects the first completed batched chunk within one configured +chunk of the prompt's end, excluding the short final suffix controlled by +`--short-read`. If a periodic checkpoint already falls there, the same state is +saved once. Reread diagnostics and layer splits keep their existing behavior. + +For example, with a 6144-token chunk and a 16384-token checkpoint interval, +the first periodic checkpoint is at 18432 and the next would be at 36864. +A branch sharing 32769 tokens cannot use the latter. A near-tail checkpoint +at 30720 can reuse more of that prefix, +without changing how the original prompt was computed. The benefit depends +on the request and the branch position; it is not a universal speed increase. + +This is a separate conservative alternative to the Strata-2080Ti Daily's +checkpoint-grid patch. The grid changes chunk boundaries to reach exact +checkpoint intervals and failed its local paired numerical gate. This option +does not claim that the original grid patch has been fixed or validated. diff --git a/include/strata/program/conv_cache.hpp b/include/strata/program/conv_cache.hpp index 7fe4bfa7..100856f0 100644 --- a/include/strata/program/conv_cache.hpp +++ b/include/strata/program/conv_cache.hpp @@ -40,4 +40,18 @@ inline size_t eviction_victim(const uint64_t* stamps, size_t n, int64_t cap) { return v; } +/// --prompt-cache-tail: the extra checkpoint near the prompt's end (`tail[i]`) is the first to go - it serves +/// only a branch of the last request - unless it is the newest item (the one just saved: `n > 1` and the newest +/// stamp). With no such item this is the plain policy. Never the root. +inline size_t eviction_victim(const uint64_t* stamps, size_t n, int64_t cap, const bool* tail) { + if (tail != nullptr && n >= 2 && cap >= 2) { + size_t newest = 0; + for (size_t i = 1; i < n; ++i) + if (stamps[i] > stamps[newest]) newest = i; + for (size_t i = 1; i < n; ++i) + if (tail[i] && i != newest) return i; + } + return eviction_victim(stamps, n, cap); +} + } // namespace strata::program::conv_cache diff --git a/src/program/conv_cache_test.cpp b/src/program/conv_cache_test.cpp index f4a0c2b7..3c0e1938 100644 --- a/src/program/conv_cache_test.cpp +++ b/src/program/conv_cache_test.cpp @@ -62,6 +62,16 @@ int main() { const size_t b = eviction_victim(stamps.data(), stamps.size(), 4); check(a == 1 && b == 1, "ties among equally stale leaves: the earlier index, deterministically"); } + { // --prompt-cache-tail: the tail checkpoint goes first; never the root, never the newest item + const std::vector st = {1, 2, 3, 4, 5, 6, 7}; + const bool t2[7] = {false, false, false, true, false, false, false}; + check(eviction_victim(st.data(), st.size(), 6, t2) == 3, "a stale tail checkpoint leaves before the oldest leaf"); + const bool t3[7] = {false, false, false, false, false, false, true}; + check(eviction_victim(st.data(), st.size(), 6, t3) == 1, "the newest item, even a tail, is not the victim"); + const bool t4[7] = {true, false, false, false, false, false, false}; + check(eviction_victim(st.data(), st.size(), 6, t4) == 1, "the root is never the victim, tail flag or not"); + check(eviction_victim(st.data(), st.size(), 6, nullptr) == 1, "no tail flags: the plain policy"); + } std::printf(g_fail ? "FAIL\n" : "PASS\n"); return g_fail ? 1 : 0; } diff --git a/src/program/generate.cpp b/src/program/generate.cpp index 75deb2ff..763b3fde 100644 --- a/src/program/generate.cpp +++ b/src/program/generate.cpp @@ -83,6 +83,7 @@ #include #include #include +#include #include #include #include @@ -575,6 +576,7 @@ struct Options { int64_t conversation_cache_min_free_mib = 2560; /// --serve: also keep a checkpoint every N freshly read prompt tokens (0 = only at the last turn boundary) int64_t prompt_cache_every = 16384; + bool prompt_cache_tail = false; // optional extra checkpoint at an existing near-tail chunk boundary /// --serve: a prompt read from token 0 is also checkpointed at its first turn boundary - the end of the system /// prompt, which every chat of the same client shares - when that is at least N tokens (0 = never) int64_t prompt_cache_root = 2048; @@ -719,6 +721,8 @@ void usage() { " (stage 0 runs the next window while stage 1 verifies this one); docs/MULTI_GPU.md\n" " --trim-stage-weights an explicit --layer-split: each GPU loads only its own layers' dense weights\n" " --prompt-cache-every N --serve: also checkpoint every N fresh prompt tokens (default 16384, 0 = off)\n" + " --prompt-cache-tail --serve, single GPU: one extra checkpoint near the prompt's end, at an existing\n" + " chunk boundary (default off; requires --prompt-cache and --prompt-cache-every > 0)\n" " --turn-token ID --serve: the token that opens a chat turn (default 248045, <|im_start|>)\n" " --tail-role-token ID --serve: a turn of this role right before the last turn is left out of\n" " the conversation checkpoint (a trailing effort turn; default -1 = off)\n" @@ -1656,6 +1660,7 @@ int main(int argc, char** argv) { else if (a == "--conversation-cache-min-free-mib") o.conversation_cache_min_free_mib = number; else o.conversation_cache_slots = (int) number; } + else if (a == "--prompt-cache-tail") o.prompt_cache_tail = true; else if (a == "--prompt-cache-every") o.prompt_cache_every = std::max(0LL, std::atoll(next("--prompt-cache-every"))); else if (a == "--prompt-cache-root") o.prompt_cache_root = std::max(0LL, std::atoll(next("--prompt-cache-root"))); else if (a == "--turn-token") o.turn_token = std::atoll(next("--turn-token")); @@ -5849,6 +5854,7 @@ int main(int argc, char** argv) { bool live_ok = false; std::vector checks; uint64_t check_clock = 0; // the checkpoints' LRU clock; creation and every use advance it + int64_t tail_ckpt_len = -1; // --prompt-cache-tail: the length of the one tail checkpoint alive (-1 = none) bool cvec_cached = true; // the control vector's state the live session and the checkpoints were read with // without --mtp the conversation cache stays on: a parked image carries the draft layer's K/V only when there is one // (the snapshots accept a null draft) @@ -5987,6 +5993,7 @@ int main(int argc, char** argv) { } }; int64_t pp_total = 0, pp_from = 0, pp_next_check = 0; + bool pp_tail_saved = false; // #471: the position the prompt pass has read up to (a chunk's or a window's end): what a request cancelled // mid-read reports as read, instead of the whole prompt int64_t pp_reached = 0; @@ -6015,11 +6022,15 @@ int main(int argc, char** argv) { ") - a GPU hang; on Windows the driver is then reset"; return false; }; - auto checkpoint_at = [&](int64_t L, std::vector* parts = nullptr) -> bool { + auto checkpoint_at = [&](int64_t L, std::vector* parts = nullptr, bool as_tail = false) -> bool { ckpt_why.clear(); if (o.prompt_cache <= 0 || L < 1) return true; for (ConvCheckpoint& c : checks) - if ((int64_t) c.ids.size() == L) { c.used = ++check_clock; return true; } + if ((int64_t) c.ids.size() == L) { + c.used = ++check_clock; + if (!as_tail && L == tail_ckpt_len) tail_ckpt_len = -1; // a periodic one lands here: a normal item now + return true; + } ConvCheckpoint c; c.ids.assign(cur.begin(), cur.begin() + L); c.imgs = imgs_below(req_imgs, L); @@ -6042,13 +6053,29 @@ int main(int argc, char** argv) { } } c.used = ++check_clock; + if (as_tail) { // only one tail checkpoint stays alive: the previous request's goes + const int64_t old_tail = tail_ckpt_len; + checks.erase(std::remove_if(checks.begin(), checks.end(), [&](const ConvCheckpoint& k) { + return old_tail >= 0 && (int64_t) k.ids.size() == old_tail; + }), checks.end()); + tail_ckpt_len = L; + } else if (L == tail_ckpt_len) { + tail_ckpt_len = -1; // the flagged tail was dropped and a normal checkpoint takes its length + } checks.push_back(std::move(c)); while ((int) checks.size() > o.prompt_cache) { std::vector stamps; + std::vector is_tail; stamps.reserve(checks.size()); - for (const ConvCheckpoint& k : checks) stamps.push_back(k.used); - const size_t victim = strata::program::conv_cache::eviction_victim(stamps.data(), stamps.size(), - o.prompt_cache); + for (const ConvCheckpoint& k : checks) { + stamps.push_back(k.used); + is_tail.push_back(tail_ckpt_len >= 0 && (int64_t) k.ids.size() == tail_ckpt_len); + } + // the tail checkpoint serves only a branch of the last request: it leaves before a periodic one + std::unique_ptr tail_flags(new bool[is_tail.size()]); + for (size_t i = 0; i < is_tail.size(); ++i) tail_flags[i] = is_tail[i] != 0; + const size_t victim = strata::program::conv_cache::eviction_victim( + stamps.data(), stamps.size(), o.prompt_cache, tail_flags.get()); checks.erase(checks.begin() + (std::ptrdiff_t) victim); } return true; @@ -6071,7 +6098,13 @@ int main(int argc, char** argv) { strata::core::progress_at("reading the prompt (batched), done up to token", done); strata::core::progress_beat(); std::fflush(stdout); - if (o.prompt_cache_every > 0 && done >= pp_next_check) { + const bool periodic_checkpoint = o.prompt_cache_every > 0 && done >= pp_next_check; + // Keep the prefill's existing chunk geometry. One extra near-tail checkpoint + // can serve a branch whose shared prefix ends before the final cached state. + const bool tail_checkpoint = o.prompt_cache_tail && o.prompt_cache > 0 && o.prompt_cache_every > 0 && + !multi_gpu && !pp_tail_saved && pp_total - done > std::max(1, o.short_read) && + pp_total - done <= T; // this chunk's real size (--prefill auto chunks differ from o.prefill_chunk) + if (periodic_checkpoint || tail_checkpoint) { bool saved = false; if (multi_gpu) { // the stages' parts, saved when each of them read this chunk std::vector parts; @@ -6085,10 +6118,11 @@ int main(int argc, char** argv) { for (const ConvCheckpoint& k : parts) complete = complete && !k.gdn.empty(); saved = !complete || checkpoint_at(done, &parts); // an incomplete set: no checkpoint here } else { - saved = checkpoint_at(done); + saved = checkpoint_at(done, nullptr, tail_checkpoint && !periodic_checkpoint); } if (!saved) { e = "saving a conversation checkpoint failed" + ckpt_why; return false; } - pp_next_check = done + o.prompt_cache_every; + if (periodic_checkpoint) pp_next_check = done + o.prompt_cache_every; + if (tail_checkpoint) pp_tail_saved = true; } return true; }; @@ -7644,6 +7678,7 @@ int main(int argc, char** argv) { pp_reached = read_from; pp_t0 = r0; pp_next_check = reread_to > 0 || !req_ckpt ? INT64_MAX : resume + o.prompt_cache_every; // ckpt=0: none + pp_tail_saved = reread_to > 0 || !req_ckpt; // ckpt=0 (#861): no tail checkpoint either { std::lock_guard lk(part_mu); part_at.clear();