GPT6-CACHE-Salva un checkpoint opzionale vicino alla fine del prompt

Co-Authored-By: Codex <noreply@openai.com>
This commit is contained in:
imanuandCodex authored and Niko1221 committed 2026-10-05 19:57:35 +02:00
1 parent f71a128126
commit 9c139846d5
4 files changed
+100 -8

No files matched your search

+43 -8
View File
@@ -83,6 +83,7 @@
#include <array>
#include <chrono>
#include <algorithm>
#include <memory>
#include <iostream>
#include <functional>
#include <future>
@@ -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<ConvCheckpoint> 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<ConvCheckpoint>* parts = nullptr) -> bool {
auto checkpoint_at = [&](int64_t L, std::vector<ConvCheckpoint>* 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<uint64_t> stamps;
std::vector<char> 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<bool[]> 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<int64_t>(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<ConvCheckpoint> 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<std::mutex> lk(part_mu);
part_at.clear();