Conversation cache: opt-in ready checkpoint pool STRATA_CKPT_POOL_READY=N, gathered small checkpoint copies STRATA_CKPT_GATHER=1, park traces (#3779, #3712)

Problem (reporter, CMP 170HX, PCIe Gen2 x4): a conversation switch's park still
paid 38-52 ms for the live state, because the STRATA_CKPT_POOL pool was empty at
every park (a switch takes two checkpoints), so the 118 MB GDN vector was
allocated fresh and page-faulted; and a checkpoint's 37 small synchronous
device-to-host copies queue behind the 4 MiB DMA pieces of STRATA_CKPT_ASYNC.

Change:
- STRATA_CKPT_POOL_READY=N (opt-in, 1..16, with STRATA_CKPT_POOL on): the pool
  keeps N ready checkpoints, pre-sized to the session's state sizes with their
  pages in, topped up on a background thread after any take (not with a layer
  split).
- STRATA_CKPT_GATHER=1 (CUDA, opt-in): the small running-state vectors (ple, and
  each layer's indexer tail, dead row and block position) are gathered device to
  device into one buffer and fetched with one copy into a pinned buffer.
- The park stage is allocated rounded up to 64 MiB (a growing conversation does not
  cudaFree/cudaMalloc it every time) and a VRAM-resident K/V skips the pointer
  attribute query.
- The background helpers (pool top-up, the checkpoint DMA, the park DMA) leave the
  host's pinned core (aux_cpus.hpp); no output depends on where a thread runs.
- A parked image whose live GDN state is still being copied joins before its
  buffers are freed (SavedConversation::live_pending).
- STRATA_PARK_TRACE prints the joins, the live-state vectors' readiness and the
  K/V sub-timers.

Differs from the PR as submitted: the PR made STRATA_CKPT_POOL=1 allocate two
ready checkpoints at startup (+236 MB of host RSS) and read STRATA_CKPT_POOL=N as
a count. That changes what an existing switch does, so here STRATA_CKPT_POOL=1
keeps its meaning (the pool holds dropped checkpoints only, nothing is allocated
ahead of time) and the ready pool has its own variable, STRATA_CKPT_POOL_READY.
A user of the PR's STRATA_CKPT_POOL=2 sets STRATA_CKPT_POOL=1
STRATA_CKPT_POOL_READY=2. The PR's one-checkpoint refill after a park with
STRATA_PARK_ASYNC is replaced by the top-up, as in the PR, and so is only active
with the new variable. Merged by hand into #2526's park_source.

With none of the new variables set: the stage size is rounded up only when the
park stage is on, the extra code is trace output behind STRATA_PARK_TRACE and
the join calls.

Reporter's numbers (linxuhao, CMP 170HX): park at a 3K context 64-75 -> 21-40 ms
(all park switches together, A/B/A conversation check identical, greedy 11 of 11
identical). Theirs, not measured here.

Original change by linxuhao (PR #3779, commit 56088e71).
This commit is contained in:
linxuhao authored and Niko1221 committed 2026-10-27 00:52:52 +01:00
1 parent f73b3c86a4
commit ca378670b2
5 files changed
+233 -39

No files matched your search

+48 -12
View File
@@ -264,6 +264,20 @@ bool ckpt_pool_env() {
}();
return on;
}
// STRATA_CKPT_POOL_READY=N (opt-in, N = 1..16, read once; only with the pool on, STRATA_CKPT_POOL): the pool keeps N ready
// checkpoints, each a running-state image with its pages in (~118 MB on Qwen3.8-Flash-Next), allocated on a background
// thread after any take. Unset: the pool holds the dropped checkpoints only, as with STRATA_CKPT_POOL=1 before - that
// variable alone allocates nothing ahead of time and its value is not a count.
size_t ckpt_pool_ready_env() {
static const size_t n = [] {
const char* v = std::getenv("STRATA_CKPT_POOL_READY");
const long k = v ? std::atol(v) : 0;
return k >= 1 ? (size_t) std::min(16L, k) : (size_t) 0;
}();
return n;
}
// how many dropped checkpoints the pool keeps (a batch admission drops and takes about two): 2, or more when STRATA_CKPT_POOL_READY asks
size_t ckpt_pool_cap_env() { return std::max<size_t>(2, ckpt_pool_ready_env()); }
// STRATA_CKPT_THREADS=1 (read once; off by default): a layer split's checkpoint saves every stage's part at once, the
// first on the calling thread and each later stage's on a thread of its own device (kept for the process: the copies'
// private streams are per thread), instead of one stage after the other. Each part is the same copy of the same state
@@ -1813,11 +1827,14 @@ bool checkpoint_restore(const ConvCheckpoint& c, strata::core::SessionState& ss,
/// STRATA_CKPT_POOL: the storage of dropped checkpoints, for the next save or slot copy to reuse. `give` keeps a
/// checkpoint's vectors (its tokens, pictures and marks cleared), `take` hands one back (an empty checkpoint when none
/// is kept). At most `cap` are kept (a batch admission drops and takes about two), the rest are freed as before.
/// is kept). At most `cap` are kept (a batch admission drops and takes about two; STRATA_CKPT_POOL=N), the rest are
/// freed as before.
class CheckpointPool {
public:
explicit CheckpointPool(bool on, size_t cap = 2) : on_(on), cap_(cap) {}
bool on() const { return on_; }
size_t cap() const { return cap_; }
size_t held() { std::lock_guard<std::mutex> lk(mu_); return free_.size(); }
void give(ConvCheckpoint&& c) {
if (!on_ || c.gdn.empty()) return;
strata::core::conversation_checkpoint_join(c.gdn.data());
@@ -1890,7 +1907,6 @@ public:
done_.wait(lk, [&] { return pending_ == 0; });
job_ = nullptr;
}
private:
static void spread_current_thread() {
#if defined(__linux__)
// wide first: the mask the aux placement keeps as this thread's own is the one restore() puts back (a request
@@ -1902,6 +1918,7 @@ private:
strata::aux_cpus::pin_current_thread(); // (a no-op while the feature is off)
#endif
}
private:
void loop(size_t k) {
strata::platform::name_this_thread("strata-ckpt", (int) k);
spread_current_thread();
@@ -9557,7 +9574,7 @@ int main(int argc, char** argv) {
strata::prefill::KvaFloorRegistry kva_floors;
// STRATA_CKPT_POOL: the dropped checkpoints' storage, reused. STRATA_SLOT_CKPT_MOVE turns it on too: a moved
// checkpoint frees nothing at the next prune, so every save would fault in fresh pages (17 -> 40-60 ms)
CheckpointPool ckpt_pool(ckpt_pool_env() || slot_ckpt_move_env());
CheckpointPool ckpt_pool(ckpt_pool_env() || slot_ckpt_move_env(), ckpt_pool_cap_env());
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)
// --prefill-pipe: the plan of the prompt segment being read (read_part sets it around its runs), for the
@@ -9681,6 +9698,32 @@ int main(int argc, char** argv) {
// into the parked image instead of being copied); a background allocation then refills STRATA_CKPT_POOL
std::function<bool(const ConvCheckpoint&)> park_drops;
std::future<void> pool_refill;
// STRATA_CKPT_POOL_READY=N (with STRATA_CKPT_POOL): the pool holds N checkpoints whose vectors already have the
// session's sizes (the ones conversation_checkpoint_save resizes to, so a pooled one is never resized) with their
// pages in: a fresh 118 MB vector costs ~40 ms of page faults at a save. Topped up on a background thread, off the
// host's core, whenever a take leaves it short; one top-up at a time. Allocation only: a save overwrites every byte.
// Not with a layer split (its checkpoints carry stage parts; saved as before). Unset: no ready checkpoints, nothing
// allocated ahead of time.
auto pool_topup = [&] {
if (ckpt_pool_ready_env() == 0 || !ckpt_pool.on() || !stages.empty()) return;
const ConvStateSizes pool_z = conv_state_sizes(g, ss);
if (pool_z.gdn == 0) return;
if (pool_refill.valid() && pool_refill.wait_for(std::chrono::seconds(0)) != std::future_status::ready) return;
const size_t have = ckpt_pool.held();
if (have >= ckpt_pool_ready_env()) return;
pool_refill = std::async(std::launch::async, [&ckpt_pool, want = ckpt_pool_ready_env() - have, z = pool_z,
layers = (size_t) std::max<int64_t>(ss.qsa_alloc, 0),
ple = ss.ple_hist != nullptr] {
StageThreads::spread_current_thread(); // (a thread the host starts inherits its one CPU: aux_cpus.hpp)
for (size_t k = 0; k < want; ++k) {
ConvCheckpoint c;
c.gdn.resize(z.gdn); c.ple.resize(ple ? z.ple : 0); c.tails.resize(layers * z.tail);
c.dead.resize(layers * z.dead); c.block_pos.resize(layers * z.block_pos);
ckpt_pool.give(std::move(c));
}
});
};
pool_topup(); // the first request's checkpoints find the pool full
auto park_source = [&](const ParkSource& src, size_t held) -> bool {
// #2049: parking copies the outgoing conversation's running checkpoints and K/V into host memory, which
// is a silent stretch of the "request" stage; when the OS has paged that memory out it takes minutes.
@@ -9840,20 +9883,12 @@ int main(int argc, char** argv) {
}
checks.erase(checks.begin() + (std::ptrdiff_t) w, checks.end());
}
const size_t moved_gdn = moved.empty() ? 0 : moved.front().gdn.size();
const bool saved = strata::core::conversation_snapshot_save(image, view, src.session, g, draft0, err,
std::move(reuse), &reused_bytes, storage.empty() ? nullptr : &storage,
moved.empty() ? nullptr : &moved);
for (ConvCheckpoint& c : storage) ckpt_pool.give(std::move(c));
if (!saved) return false;
// the next checkpoint save would fault in fresh pages for what moved out: one is allocated now
if (moved_gdn > 0 && ckpt_pool.on() &&
(!pool_refill.valid() || pool_refill.wait_for(std::chrono::seconds(0)) == std::future_status::ready))
pool_refill = std::async(std::launch::async, [&ckpt_pool, moved_gdn] {
ConvCheckpoint c;
c.gdn.resize(moved_gdn);
ckpt_pool.give(std::move(c));
});
pool_topup(); // what the park took (and what moved out) is allocated again, in the background
for (size_t k = 0; k < n_st; ++k) { // the later stages (the last one with the draft layer's K/V)
auto& st = stages[k];
const strata::core::OnDevice on(st->dev);
@@ -9970,6 +10005,7 @@ int main(int argc, char** argv) {
return true;
}
ConvCheckpoint c = parts == nullptr ? ckpt_pool.take() : ConvCheckpoint{}; // (STRATA_CKPT_POOL)
if (parts == nullptr) pool_topup(); // the next save finds one ready
c.ids.assign(cur.begin(), cur.begin() + L);
c.imgs = imgs_below(req_imgs, L);
if (parts != nullptr) {