diff --git a/core/engine.cpp b/core/engine.cpp index f5d0d25e..7af29aab 100644 --- a/core/engine.cpp +++ b/core/engine.cpp @@ -1479,13 +1479,14 @@ int Engine::serve(std::unique_ptr watch) { auto* b = static_cast*>(pc_iface_.get()); /* Called inside Scheduler::step() under its lock, which makes touching the cache and rank * 0's manager safe; nothing here may call back into the scheduler. */ - b->restore_fn = [this](const Request& req) { + b->restore_fn = [this, b](const Request& req) { const uint64_t t0 = rad_mono_ns(); PrefixCache& pc = ranks_[0]->prefix; - std::vector mk; - media_keys(req, &mk, cvec_salt_for(req.sp.use_control_vector)); + /* Room is made before anything is planned (PCBinding::restore_plan): a chain the free + * list cannot hold would otherwise come back only as far as it reaches. One block + * table serves every rank, so what rank 0's cache gives up is room on every rank. */ const std::vector want = - pc.restorable_from(req.prompt, mk); + b->restore_plan(req, cvec_salt_for(req.sp.use_control_vector)); if (want.empty()) return; /* The whole chain in one restore. With 4-token blocks a long conversation is tens of * thousands of entries; batched, it is one launch per (group, layer) per staging batch diff --git a/core/mem/prefix.cpp b/core/mem/prefix.cpp index 820e1abb..a2a418d8 100644 --- a/core/mem/prefix.cpp +++ b/core/mem/prefix.cpp @@ -5,6 +5,7 @@ #include #include +#include namespace rad { @@ -241,6 +242,22 @@ std::vector PrefixCache::chain_all(const std::vector& tokens return hs; } +/* A snapshot is on the chain when its key equals the chain hash at its position. */ +std::unordered_map::iterator +PrefixCache::deepest_ckpt(const std::vector& hs, int64_t limit) const { + auto best = ck_by_prefix_.end(); + for (auto it = ck_by_prefix_.begin(); it != ck_by_prefix_.end(); ++it) { + const Ckpt& c = it->second; + if (!c.valid || c.slot < 0 || c.pos <= 0 || c.pos > limit || c.pos % block_size_ != 0 || + c.pos / block_size_ > (int64_t)hs.size()) + continue; + if (best != ck_by_prefix_.end() && c.pos <= best->second.pos) continue; + if (hs[(size_t)(c.pos / block_size_ - 1)] != it->first) continue; + best = it; + } + return best; +} + void PrefixCache::touch(const BlockHash& h, uint64_t now_ns) const { auto it = map_.find(h); if (it == map_.end() || !it->second.in_lru) return; @@ -274,23 +291,7 @@ CacheHit PrefixCache::lookup(const std::vector& tokens, /* Back a full hit off by one block so one token remains to compute a logit from. */ if (matched * block_size_ >= (int64_t)tokens.size() && matched > 0) --matched; - /* Deepest valid snapshot at or below `limit` on this chain. Checkpoints sit on the interval or - * at a prompt's tail (Scheduler::plan_chunk), i.e. on any block boundary, so iterate the - * snapshots (a few hundred) rather than the block positions (tens of thousands); a snapshot is - * on this chain when its key equals the chain hash at its position. */ - auto deepest = [&](int64_t limit) { - auto best = ck_by_prefix_.end(); - for (auto it = ck_by_prefix_.begin(); it != ck_by_prefix_.end(); ++it) { - const Ckpt& c = it->second; - if (!c.valid || c.slot < 0 || c.pos <= 0 || c.pos > limit || - c.pos % block_size_ != 0) - continue; - if (best != ck_by_prefix_.end() && c.pos <= best->second.pos) continue; - if (hs[(size_t)(c.pos / block_size_ - 1)] != it->first) continue; - best = it; - } - return best; - }; + auto deepest = [&](int64_t limit) { return deepest_ckpt(hs, limit); }; /* Tokens a hit of `m` blocks lets a sequence skip: all of them, or with recurrent state only * up to the deepest checkpoint (Scheduler::admit_one resumes there). */ auto reusable = [&](int64_t m) -> int64_t { @@ -460,7 +461,8 @@ void PrefixCache::take_over(uint64_t session, const std::vector& hs, /* ------------------------------------------------------------------ idle session tiers */ std::vector PrefixCache::restorable_from(const std::vector& tokens, - const std::vector& mm) const { + const std::vector& mm, + int64_t fit) const { std::vector out; if (!enabled_ || !tiers_ || tokens.empty()) return out; @@ -474,15 +476,45 @@ std::vector PrefixCache::restorable_from(const std::vector& * how far the maintenance pass had got; the scheduler chunks the rest of the prefill against * that length, and greedy output is tie-level sensitive to the GEMM shapes (§19). Restoring * the whole chain makes the hit length a function of the prompt. */ + std::vector at; /* each entry's block on the chain */ for (int64_t b = 0; b < n_full; ++b) { auto it = map_.find(hs[(size_t)b]); if (it == map_.end()) break; if (!same_tokens(it->second, tokens.data() + b * block_size_)) break; - if (tiers_->resident_off_device(hs[(size_t)b])) out.push_back(hs[(size_t)b]); + if (!tiers_->resident_off_device(hs[(size_t)b])) continue; + out.push_back(hs[(size_t)b]); + at.push_back(b); } + if (out.empty() || (int64_t)out.size() <= fit) return out; + + /* Restored up to `fit`, the hit ends at the first entry left behind. */ + ++stats_.restore_cuts; + int64_t keep = std::max(0, fit); + if (has_linear_) { + const auto best = deepest_ckpt(hs, at[(size_t)keep] * block_size_); + const int64_t upto = best == ck_by_prefix_.end() ? 0 : best->second.pos / block_size_; + keep = std::lower_bound(at.begin(), at.begin() + keep, upto) - at.begin(); + } + out.resize((size_t)keep); return out; } +int64_t PrefixCache::restore_room() const { + if (!kv_ || groups_.empty()) return 0; + int64_t room = INT64_MAX; + for (int32_t g : groups_) room = std::min(room, kv_->total_blocks(g) - kv_->held_blocks(g)); + return std::max(0, room); +} + +int64_t PrefixCache::trim_for(const std::vector& tokens, + const std::vector& mm, int64_t n_blocks) { + if (!enabled_ || n_blocks <= 0) return 0; + const std::vector hs = chain_all(tokens, mm, (int64_t)tokens.size() / block_size_); + std::unordered_set spare(hs.begin(), hs.end()); + for (const auto& h : held_) spare.insert(h.first); + return trim_sparing(n_blocks, &spare); +} + void PrefixCache::drop_offdevice(const BlockHash& key) { auto it = map_.find(key); if (it == map_.end()) return; @@ -569,7 +601,10 @@ int64_t PrefixCache::evict_lru(int64_t n) { return done; } -int64_t PrefixCache::trim(int64_t n_blocks) { +int64_t PrefixCache::trim(int64_t n_blocks) { return trim_sparing(n_blocks, nullptr); } + +int64_t PrefixCache::trim_sparing(int64_t n_blocks, + const std::unordered_set* spare) { /* Tiers attached but off count as no tiers: no copy will ever land, so do not wait for one. */ const bool keep = tiers_ && tiers_->host_on(); const uint64_t now = rad_mono_ns(); @@ -581,6 +616,7 @@ int64_t PrefixCache::trim(int64_t n_blocks) { auto m = map_.find(h); if (m == map_.end()) { it = lru_.erase(it); continue; } Entry& e = m->second; + if (spare && spare->count(h)) { ++it; continue; } if (shared_with_live(e)) { /* In use: to the back, as in evict_lru. */ auto next = std::next(it); diff --git a/core/mem/prefix.h b/core/mem/prefix.h index 2f171fb1..c88c405a 100644 --- a/core/mem/prefix.h +++ b/core/mem/prefix.h @@ -44,6 +44,7 @@ #include #include #include +#include #include namespace rad { @@ -253,9 +254,24 @@ public: /* The off-device entries this prompt's chain would hit if restored, in order, from where * lookup stopped. The caller promotes them and looks up again; empty means the prefix ended * on a miss or token mismatch. Separate from lookup because lookup is const and has no - * device; a promotion allocates blocks and runs a scatter between steps. */ + * device; a promotion allocates blocks and runs a scatter between steps. + * + * `fit` is how many entries the pool can take. A longer chain is cut, and counted + * (Stats::restore_cuts): with recurrent state at the deepest snapshot the shorter prefix + * reaches, since the hit resumes there and blocks past it would come back for nothing; + * without, at `fit`. */ std::vector restorable_from(const std::vector& tokens, - const std::vector& mm) const; + const std::vector& mm, + int64_t fit = INT64_MAX) const; + /* Entries a restore can place now: free blocks against the carve, the least over the cached + * groups. Lent blocks count, since KVManager::reserve_blocks recalls the loan for them. */ + int64_t restore_room() const; + /* Room for a restore of this prompt's chain: trim(), sparing the chain itself (its blocks + * would only have to come back too) and every chain a waiting request holds (hold()), whose + * next attempt would restore it again. Nothing is lost; the reserve's trim after the next + * publish puts the pool's free quarter back. */ + int64_t trim_for(const std::vector& tokens, const std::vector& mm, + int64_t n_blocks); /* The tiers lost the bytes. Erases the entry without releasing blocks: an off-device entry * owns none, and its recorded ids now belong to other sequences. Installed as @@ -341,6 +357,8 @@ public: int64_t drops = 0; /* Entries and snapshots removed by --prefix-cache-ttl, from any tier. */ int64_t expired = 0, ckpt_expired = 0; + /* Restores from the host tier cut short because the pool could not hold the chain. */ + int64_t restore_cuts = 0; }; const Stats& stats() const { return stats_; } @@ -376,6 +394,9 @@ private: const std::vector& mm, int64_t n_blocks) const; void touch(const BlockHash& h, uint64_t now_ns) const; + /* trim(), leaving the entries in `spare` (when given) where they are. */ + int64_t trim_sparing(int64_t n_blocks, + const std::unordered_set* spare); /* A live sequence also holds one of this entry's blocks, so releasing it frees nothing. */ bool shared_with_live(const Entry& e) const; /* Whether `e` holds the block_size_ token ids at `src`, so a hash collision is a miss. */ @@ -396,6 +417,11 @@ private: /* For the debug lines: whether `c` is its session's deepest snapshot, and seconds since a * monotonic stamp. */ bool is_tip(const Ckpt& c) const; + /* The deepest valid snapshot on the chain `hs` at or below `limit` tokens; end() if none. + * Checkpoints sit on any block boundary (the interval or a prompt's tail), so this walks the + * snapshots, a few hundred, rather than the chain's blocks. */ + std::unordered_map::iterator + deepest_ckpt(const std::vector& hs, int64_t limit) const; static double idle_s(uint64_t used_ns); KVManager* kv_ = nullptr; diff --git a/core/sched/mem_iface.h b/core/sched/mem_iface.h index 40c3fc90..05934b33 100644 --- a/core/sched/mem_iface.h +++ b/core/sched/mem_iface.h @@ -170,6 +170,8 @@ struct IPrefixCache { virtual CkptCounts ckpt_counts() const { return {}; } /* Entries expired by --prefix-cache-ttl; not counted as evictions. */ virtual int64_t expired() const { return 0; } + /* Restores the pool could not hold whole (PrefixCache::Stats::restore_cuts). */ + virtual int64_t restore_cuts() const { return 0; } virtual int64_t ckpt_expired() const { return 0; } /* Tokens of context held only by the cache right now. Blocks a running sequence also reads @@ -346,6 +348,21 @@ struct PCBinding final : IPrefixCache { void restore(const Request& r) override { if (restore_fn) restore_fn(r); } + /* The entries restore_fn brings back, in chain order: the whole chain if the pool can be made + * to hold it, otherwise cut (PrefixCache::restorable_from). Room is made as evict() makes it, + * dropping entries with a landed host copy, then landing pending copies and dropping those, but + * never erasing one: a restore is worth no lost conversation. The pool's free quarter can go + * to it; the reserve's trim after the next publish frees it again. */ + std::vector restore_plan(const Request& r, uint64_t salt) { + media_keys(r, &mm_, salt); + std::vector want = m->restorable_from(r.prompt, mm_); + const int64_t need = (int64_t)want.size() - m->restore_room(); + if (need <= 0) return want; + const int64_t k = m->trim_for(r.prompt, mm_, need); + if (k < need && flush_fn && flush_fn()) (void)m->trim_for(r.prompt, mm_, need - k); + /* Landing copies may have moved records; take the chain again against the room made. */ + return m->restorable_from(r.prompt, mm_, m->restore_room()); + } void begin_step() override { m->begin_step(); } void hold(const Request& r) override { media_keys(r, &mm_, salt_id); @@ -405,6 +422,7 @@ struct PCBinding final : IPrefixCache { return { st.ckpt_retired, st.ckpt_lru, st.ckpt_refused }; } int64_t expired() const override { return m->stats().expired; } + int64_t restore_cuts() const override { return m->stats().restore_cuts; } int64_t ckpt_expired() const override { return m->stats().ckpt_expired; } int64_t held_tokens() const override { return m->unshared_tokens(); } diff --git a/core/sched/metrics.h b/core/sched/metrics.h index 801cec72..a4aa80cf 100644 --- a/core/sched/metrics.h +++ b/core/sched/metrics.h @@ -94,11 +94,17 @@ struct SchedMetrics { int64_t prefix_expired = 0, prefix_ckpt_expired = 0; /* --prefix-cache-ttl */ /* Admissions whose attention hit was cut to nothing: no snapshot at or below it (recurrent * state or a window snapshot), a windowed trunk group no window snapshot covers, or blocks - * that could not be adopted. `hit_cut_tokens` is the checkpoint shortfall: prompt tokens the - * attention cache held and the admission computed again, because the hit resumed at a - * snapshot below it or was cut to nothing. */ + * that could not be adopted. `hit_cut_resumed_below` counts those that resumed at a + * snapshot more than one checkpoint interval below the hit: the retention ladder had + * nothing nearer, or the restore brought back only part of the chain. `hit_cut_tokens` is + * the checkpoint shortfall: prompt tokens the attention cache held and the admission + * computed again, because the hit resumed at a snapshot below it or was cut to nothing. */ int64_t hit_cut_no_snapshot = 0, hit_cut_window = 0, hit_cut_adopt = 0; + int64_t hit_cut_resumed_below = 0; int64_t hit_cut_tokens = 0; + /* Restores from the host tier the pool could not hold whole, even after giving up the VRAM + * copies of other conversations (PrefixCache::Stats::restore_cuts). */ + int64_t prefix_restore_cuts = 0; /* Tokens of context held only by the cache, i.e. what eviction would free. Excludes blocks * shared with a running sequence. Same token axis as the paged groups, so comparable with * kv_tokens_used. */ diff --git a/core/sched/scheduler.cpp b/core/sched/scheduler.cpp index f2bac4e9..7a5667db 100644 --- a/core/sched/scheduler.cpp +++ b/core/sched/scheduler.cpp @@ -973,6 +973,11 @@ int Scheduler::admit_one(SchedReq& s, int64_t budget, int64_t reserve) { RAD_DEBUG("admit %llu: prefix hit of %lld tokens reuses nothing -- no snapshot at or " "below it", (unsigned long long)r.id, (long long)h.n_attn_tokens); } else if (reuse < h.n_attn_tokens) { + /* Within an interval of the hit is the ordinary case: a turn's tail past its last + * checkpoint. Further down, the snapshots in between are gone, or the attention + * blocks reach past what the tiers could restore of the chain. */ + if (geo_.checkpoints() && h.n_attn_tokens - reuse > geo_.checkpoint_interval) + s.hit_cut = SchedReq::HitCut::ResumedBelow; RAD_DEBUG("admit %llu: prefix hit of %lld tokens resumes at the snapshot at %lld", (unsigned long long)r.id, (long long)h.n_attn_tokens, (long long)reuse); } @@ -1092,6 +1097,7 @@ int Scheduler::admit_one(SchedReq& s, int64_t budget, int64_t reserve) { case SchedReq::HitCut::NoSnapshot: ++m_.hit_cut_no_snapshot; break; case SchedReq::HitCut::Window: ++m_.hit_cut_window; break; case SchedReq::HitCut::Adopt: ++m_.hit_cut_adopt; break; + case SchedReq::HitCut::ResumedBelow: ++m_.hit_cut_resumed_below; break; case SchedReq::HitCut::None: break; } } @@ -2625,6 +2631,7 @@ SchedMetrics Scheduler::metrics_locked() const { m.prefix_ckpt_refused = cc.refused; m.prefix_expired = pc_->expired(); m.prefix_ckpt_expired = pc_->ckpt_expired(); + m.prefix_restore_cuts = pc_->restore_cuts(); m.prefix_held_tokens = pc_->held_tokens(); } m.attn_hit_rate = m.prompt_tokens ? (float)((double)m.attn_cached_tokens / (double)m.prompt_tokens) : 0.0f; diff --git a/core/sched/scheduler.h b/core/sched/scheduler.h index 7919e54e..d89c323e 100644 --- a/core/sched/scheduler.h +++ b/core/sched/scheduler.h @@ -107,8 +107,9 @@ struct SchedReq { * fit would inflate the prompt and hit totals. */ int32_t hit_attn = 0; bool hit_owed = false; - /* Why that hit was cut to nothing, for the counters (SchedMetrics::hit_cut_*). */ - enum class HitCut : uint8_t { None, NoSnapshot, Window, Adopt }; + /* Why that hit was cut, for the counters (SchedMetrics::hit_cut_*): to nothing, or to a + * snapshot more than an interval below it. */ + enum class HitCut : uint8_t { None, NoSnapshot, Window, Adopt, ResumedBelow }; HitCut hit_cut = HitCut::None; /* The whole sequence is prompt ++ output; n_computed indexes into it. */ diff --git a/core/server/adapters.h b/core/server/adapters.h index 6da4c418..a624d5e7 100644 --- a/core/server/adapters.h +++ b/core/server/adapters.h @@ -102,6 +102,8 @@ inline SchedMetrics bridge_metrics(const rad::SchedMetrics& m) { o.hit_cut_no_snapshot = m.hit_cut_no_snapshot; o.hit_cut_window = m.hit_cut_window; o.hit_cut_adopt = m.hit_cut_adopt; + o.hit_cut_resumed_below = m.hit_cut_resumed_below; + o.prefix_restore_cuts = m.prefix_restore_cuts; o.hit_cut_tokens = m.hit_cut_tokens; o.prefix_expired = m.prefix_expired; o.prefix_ckpt_expired = m.prefix_ckpt_expired; diff --git a/core/server/dashboard.cpp b/core/server/dashboard.cpp index 9a383322..53b976ba 100644 --- a/core/server/dashboard.cpp +++ b/core/server/dashboard.cpp @@ -131,6 +131,7 @@ HttpResponse Server::handle_sessions(const HttpRequest&) { o["moved"] = { { "to_host", c.to_host }, { "dropped", c.dropped }, { "promoted", c.promoted }, { "host_drops", c.host_drops }, { "held_back", c.held_back }, + { "restore_cuts", sm.prefix_restore_cuts }, { "failures", c.failures } }; /* Snapshot store on a hybrid model: bytes per snapshot and slot count. `bytes_each` is 0 * on a model with no recurrent state. */ @@ -150,7 +151,8 @@ HttpResponse Server::handle_sessions(const HttpRequest&) { { "refused", sm.prefix_ckpt_refused }, { "hit_cuts", { { "no_snapshot", sm.hit_cut_no_snapshot }, { "windowed_group", sm.hit_cut_window }, - { "adopt_failed", sm.hit_cut_adopt } } }, + { "adopt_failed", sm.hit_cut_adopt }, + { "resumed_below", sm.hit_cut_resumed_below } } }, { "hit_cut_tokens", sm.hit_cut_tokens } }; /* Whether a tier exists to move sessions to. A session is recorded whether or not it can be * demoted, so a non-empty table only says something is held. */ @@ -222,7 +224,8 @@ HttpResponse Server::handle_stats(const HttpRequest&) { o["ckpt_refused"] = m.prefix_ckpt_refused; o["hit_cuts"] = { { "no_snapshot", m.hit_cut_no_snapshot }, { "windowed_group", m.hit_cut_window }, - { "adopt_failed", m.hit_cut_adopt } }; + { "adopt_failed", m.hit_cut_adopt }, + { "resumed_below", m.hit_cut_resumed_below } }; o["hit_cut_tokens"] = (double)m.hit_cut_tokens; o["prefill_tps"] = m.prefill_tps; o["decode_tps"] = m.decode_tps; diff --git a/core/server/iface.h b/core/server/iface.h index 8cd70dd4..a017ca6e 100644 --- a/core/server/iface.h +++ b/core/server/iface.h @@ -72,7 +72,10 @@ struct SchedMetrics { /* Admissions whose prefix hit was cut to nothing, by reason, and the checkpoint shortfall: * attention-cached prompt tokens computed again (rad::SchedMetrics::hit_cut_tokens). */ int64_t hit_cut_no_snapshot = 0, hit_cut_window = 0, hit_cut_adopt = 0; + int64_t hit_cut_resumed_below = 0; /* resumed more than an interval below the hit */ int64_t hit_cut_tokens = 0; + /* Host-tier restores the pool could not hold whole (rad::SchedMetrics::prefix_restore_cuts). */ + int64_t prefix_restore_cuts = 0; /* Removed by --prefix-cache-ttl, cumulative. Not evictions: nothing wanted the memory. */ int64_t prefix_expired = 0, prefix_ckpt_expired = 0; /* Decoupled prefix caching (spec §7.3). `prefix_hit_rate` is the attention hit rate, as in diff --git a/core/server/metrics.cpp b/core/server/metrics.cpp index f258d219..8bae8246 100644 --- a/core/server/metrics.cpp +++ b/core/server/metrics.cpp @@ -196,6 +196,8 @@ void Metrics::sample(const SchedMetrics& m) { c_cut_no_snapshot_.advance_to(m.hit_cut_no_snapshot); c_cut_window_.advance_to(m.hit_cut_window); c_cut_adopt_.advance_to(m.hit_cut_adopt); + c_cut_resumed_below_.advance_to(m.hit_cut_resumed_below); + c_restore_cuts_.advance_to(m.prefix_restore_cuts); c_cut_tokens_.advance_to(m.hit_cut_tokens); g_linear_hit_.set(m.linear_hit_rate); } @@ -449,13 +451,20 @@ std::string Metrics::render() const { "Checkpoints not written because every snapshot slot was in use by a restore " "in the same step.", L, c_ckpt_refused_.get()); w.help("radiance:prefix_hit_cuts_total", - "Admissions whose prefix hit reused nothing, by reason: no snapshot at or below " - "the hit, a windowed group no window snapshot covers, or blocks that could not be " - "adopted.", "counter"); + "Admissions whose prefix hit was cut, by reason: reused nothing for want of a " + "snapshot at or below the hit, for a windowed group no window snapshot covers, or " + "for blocks that could not be adopted; or resumed at a snapshot more than one " + "checkpoint interval below the hit.", "counter"); labelled("radiance:prefix_hit_cuts_total", "reason", "no_snapshot", c_cut_no_snapshot_.get()); labelled("radiance:prefix_hit_cuts_total", "reason", "windowed_group", c_cut_window_.get()); labelled("radiance:prefix_hit_cuts_total", "reason", "adopt_failed", c_cut_adopt_.get()); + labelled("radiance:prefix_hit_cuts_total", "reason", "resumed_below", + c_cut_resumed_below_.get()); + w.counter("radiance:prefix_restore_cuts_total", + "Restores from the host tier the pool could not hold whole, even after giving " + "up the VRAM copies of other stored conversations; each came back to a " + "snapshot short of its end.", L, c_restore_cuts_.get()); w.counter("radiance:prefix_hit_cut_tokens_total", "Prompt tokens the attention cache held and admission computed again, because " "the hit resumed at a snapshot below it or reused nothing.", L, diff --git a/core/server/metrics.h b/core/server/metrics.h index 11ba72c4..a4aedb0e 100644 --- a/core/server/metrics.h +++ b/core/server/metrics.h @@ -153,7 +153,8 @@ private: Counter c_prefix_evict_, c_prefix_ckpt_evict_; Counter c_prefix_expired_, c_prefix_ckpt_expired_; Counter c_ckpt_retired_, c_ckpt_lru_, c_ckpt_refused_; - Counter c_cut_no_snapshot_, c_cut_window_, c_cut_adopt_, c_cut_tokens_; + Counter c_cut_no_snapshot_, c_cut_window_, c_cut_adopt_, c_cut_resumed_below_, c_cut_tokens_; + Counter c_restore_cuts_; Gauge g_linear_hit_; Counter c_success_[(size_t)SuccessReason::N_]; Counter c_rejected_, c_disconnect_, c_overflow_; diff --git a/tests/dashboard_test.cpp b/tests/dashboard_test.cpp index 86c41002..e8f0126b 100644 --- a/tests/dashboard_test.cpp +++ b/tests/dashboard_test.cpp @@ -122,6 +122,8 @@ struct Dash { sched.m.prefix_ckpt_lru = 3; sched.m.hit_cut_no_snapshot = 2; sched.m.hit_cut_tokens = 9000; + sched.m.hit_cut_resumed_below = 4; + sched.m.prefix_restore_cuts = 1; sched.m.draft_depth_seen = 2; sched.m.draft_pos_reached[0] = 10; sched.m.draft_pos_accepted[0] = 8; sched.m.draft_pos_reached[1] = 8; sched.m.draft_pos_accepted[1] = 2; @@ -239,6 +241,8 @@ TEST(sessions_lists_each_conversation_and_what_the_tiers_did) { CHECK(s.value("enabled", false)); CHECK_EQ(s["linear"]["evictions"].value("store_full", (int64_t)0), (int64_t)3); CHECK_EQ(s["linear"]["hit_cuts"].value("no_snapshot", (int64_t)0), (int64_t)2); + CHECK_EQ(s["linear"]["hit_cuts"].value("resumed_below", (int64_t)0), (int64_t)4); + CHECK_EQ(s["moved"].value("restore_cuts", (int64_t)0), (int64_t)1); CHECK_EQ(s["linear"].value("hit_cut_tokens", (int64_t)0), (int64_t)9000); } diff --git a/tests/mem_test.cpp b/tests/mem_test.cpp index 07ba5c45..2ad190f4 100644 --- a/tests/mem_test.cpp +++ b/tests/mem_test.cpp @@ -6101,8 +6101,7 @@ TEST(the_cache_tiers_and_pool_agree_over_random_interleavings) { /* The engine's restore (the binding's restore_fn) without the transfer: which entries come back, * into which blocks, and what the tiers and the cache are told. Returns how many came back. */ static int64_t engine_restore(TierFixture& x, PCBinding& b, const Request& r) { - (void)b; - const std::vector want = x.f.pc.restorable_from(r.prompt, {}); + const std::vector want = b.restore_plan(r, 0); std::vector jobs; for (const BlockHash& h : want) { TierJob j; @@ -6184,6 +6183,7 @@ TEST(a_restore_gives_up_other_clean_copies_to_bring_the_whole_chain_back) { CHECK_EQ(x.f.pc.stats().evictions, evictions); CHECK_EQ(x.f.pc.size(), 2 * s.n); CHECK_EQ((int64_t)x.f.pc.restorable_from(s.b, {}).size(), 2 * s.n - x.f.pc.resident()); + CHECK_EQ(x.f.pc.stats().restore_cuts, 0ll); } /* With recurrent state the hit resumes at the deepest snapshot the restored prefix reaches. Cut @@ -6201,6 +6201,7 @@ TEST(a_restored_hybrid_conversation_resumes_at_its_tip_snapshot) { const CacheHit hit = x.f.pc.lookup(s.a, {}); CHECK_EQ(hit.n_attn_tokens, (int32_t)(s.n * 16)); CHECK_EQ(hit.n_linear_tokens, (int32_t)(s.n * 16)); + CHECK_EQ(x.f.pc.stats().restore_cuts, 0ll); } /* When the pool cannot be made to hold the chain, what comes back ends at a snapshot: blocks past @@ -6233,6 +6234,30 @@ TEST(a_restore_the_pool_cannot_hold_stops_at_a_snapshot_and_spares_its_own_chain CHECK_EQ(hit.n_linear_tokens, (int32_t)snap); CHECK_EQ(hit.n_attn_tokens, (int32_t)std::max(snap, resident * 16)); CHECK_EQ(x.f.pc.lookup(s.b, {}).n_attn_tokens, (int32_t)(s.n * 16)); + CHECK_EQ(x.f.pc.stats().restore_cuts, 1ll); +} + +/* A chain another waiting request holds is kept too: that request restored it and was refused, + * and taking it would only have its next attempt restore it again. */ +TEST(a_restore_does_not_take_a_chain_a_waiting_request_holds) { + TierFixture x; + CHECK_OK(x.build()); + const TwoSessions s = two_sessions(x, /*b_live=*/false, /*a_snapshots=*/false); + copy_now(x.t); /* B is clean, so a trim could take it */ + x.f.pc.hold(9, s.b, {}); + const int64_t room = x.f.kv.total_blocks(G_FULL) - x.f.kv.held_blocks(G_FULL); + const int64_t drops = x.f.pc.stats().drops; + + PCBinding b(&x.f.pc); + CHECK_EQ(engine_restore(x, b, turn(3, s.a, {})), room); + CHECK_EQ(x.f.pc.stats().drops, drops); + CHECK_EQ(x.f.pc.lookup(s.b, {}).n_attn_tokens, (int32_t)(s.n * 16)); + CHECK_EQ(x.f.pc.stats().restore_cuts, 1ll); + + x.f.pc.release(9); + const int64_t rest = (int64_t)x.f.pc.restorable_from(s.a, {}).size(); + CHECK_EQ(engine_restore(x, b, turn(3, s.a, {})), rest); + CHECK_EQ(x.f.pc.lookup(s.a, {}).n_attn_tokens, (int32_t)(s.n * 16)); } /* Tools and tests build and tear down the tier executors and pools repeatedly, and on a card every diff --git a/tests/sched_test.cpp b/tests/sched_test.cpp index 294e8b7b..f4983389 100644 --- a/tests/sched_test.cpp +++ b/tests/sched_test.cpp @@ -824,6 +824,7 @@ TEST(a_prefix_hit_starts_the_request_at_the_last_usable_checkpoint) { /* The checkpoint shortfall: the 48 cached tokens above the snapshot are computed again. */ CHECK_EQ(m.hit_cut_tokens, 48); CHECK_EQ(m.hit_cut_no_snapshot, 0); + CHECK_EQ(m.hit_cut_resumed_below, 0); /* within an interval: a turn's ordinary tail */ /* The snapshot this step takes is not offered to anyone until the step has run: the * reservation happens when the chunk is planned, the state exists only afterwards. */ @@ -833,6 +834,26 @@ TEST(a_prefix_hit_starts_the_request_at_the_last_usable_checkpoint) { CHECK_EQ(f.pc.committed[0], 512); /* ctx_len 256 + ckpt_tok 255 + 1 */ } +/* A hit that resumes more than an interval below the attention blocks is counted with its own + * reason: the snapshots in between are gone, or a restore brought back only part of the chain, + * and hit_cut_tokens alone would not say how many admissions it was. */ +TEST(a_hit_resuming_far_below_its_blocks_is_counted_as_cut) { + Fixture f; + f.build(); + CHECK_OK(f.start()); + f.pc.seed(784); /* 49 blocks */ + f.pc.lin = 256; /* 528 tokens below, past the 256-token interval */ + f.pc.src = 3; + + uint64_t a = f.submit(1000, 8); + CHECK(f.sch.step() != nullptr); + CHECK_EQ(f.sch.find(a)->n_computed, 256); + const SchedMetrics m = f.sch.metrics(); + CHECK_EQ(m.hit_cut_resumed_below, 1); + CHECK_EQ(m.hit_cut_no_snapshot, 0); + CHECK_EQ(m.hit_cut_tokens, 528); +} + /* A hit that skipped tokens with no snapshot to resume from would corrupt the linear state, so it * is refused outright rather than served with a zeroed state. */ TEST(a_hit_with_no_checkpoint_slot_reuses_nothing) { diff --git a/tests/server_test.cpp b/tests/server_test.cpp index 0c6cf36a..4ed78b23 100644 --- a/tests/server_test.cpp +++ b/tests/server_test.cpp @@ -2391,6 +2391,7 @@ TEST(snapshot_evictions_and_cut_hits_are_counted_by_reason) { server::SchedMetrics s; s.prefix_ckpt_retired = 5; s.prefix_ckpt_lru = 3; s.prefix_ckpt_refused = 1; s.hit_cut_no_snapshot = 2; s.hit_cut_window = 0; s.hit_cut_adopt = 4; + s.hit_cut_resumed_below = 6; s.prefix_restore_cuts = 7; s.hit_cut_tokens = 76092; m.sample(s); PromDoc d = parse_prom(m.render()); @@ -2411,6 +2412,8 @@ TEST(snapshot_evictions_and_cut_hits_are_counted_by_reason) { CHECK_NEAR(value(C + ",reason=\"no_snapshot\"}"), 2.0, 1e-9); CHECK_NEAR(value(C + ",reason=\"windowed_group\"}"), 0.0, 1e-9); CHECK_NEAR(value(C + ",reason=\"adopt_failed\"}"), 4.0, 1e-9); + CHECK_NEAR(value(C + ",reason=\"resumed_below\"}"), 6.0, 1e-9); + CHECK_NEAR(value("radiance:prefix_restore_cuts_total" + L + "}"), 7.0, 1e-9); CHECK_NEAR(value("radiance:prefix_hit_cut_tokens_total" + L + "}"), 76092.0, 1e-9); }