Merge main into clef-flash-arch-plugin

This commit is contained in:
Brandon Bertelsen committed 2026-10-28 22:53:52 -04:00
commit d24cf32a13
114 files changed
+26614 -621

No files matched your search

+43 -2
View File
@@ -143,15 +143,49 @@ int chunk_geometry_resolve(const Program& prog, const Config& cfg, ChunkGeometry
* checkpoints land wherever a step ends (§7.3). */
g.min_max_tok = g.quantum;
*out = g;
if (cfg.max_tok < g.min_max_tok) {
*out = g;
RAD_ERR("--max-num-batched-tokens is %lld but this model's chunk geometry needs at least "
"%lld, so a chunk can end on a linear-state boundary: %s",
(long long)cfg.max_tok, (long long)g.min_max_tok, g.why.c_str());
return RAD_E_INVAL;
}
/* The resume grid (--prefix-resume exact): where an idle server's prefill chunks start. A
* prefill alone in its step takes the budget, rounded down to the quantum, and stops at every
* interval boundary it reaches where chunks split at the interval (linear state, or periodic
* window snapshots); a span carries h intervals from the second one on. An interval past the
* budget must then be a multiple of the budget's chunk, or the chunk after a checkpoint would
* start off the grid every cold run cuts: rounded up, as the quantum rounds it, since a longer
* interval costs only retention. */
const int64_t I0 = g.checkpoint_interval;
const bool splits = I0 > 0 && (!g.window_ckpt || g.window_periodic);
const int64_t budget_chunk = cfg.max_tok / g.quantum * g.quantum;
g.exact = cfg.prefix_exact && cfg.prefix_cache;
if (g.exact && splits && I0 > budget_chunk && I0 % budget_chunk != 0) {
g.checkpoint_interval = align_up(I0, budget_chunk);
RAD_WARN("--checkpoint-interval %lld resolves to %lld: --prefix-resume exact needs it a "
"multiple of the %lld-token prefill chunk (--max-num-batched-tokens %lld) so "
"every checkpoint is where a cold prefill's chunk ends; give a multiple of %lld, "
"or --prefix-resume nearest to keep %lld", (long long)cfg.checkpoint_interval,
(long long)g.checkpoint_interval, (long long)budget_chunk, (long long)cfg.max_tok,
(long long)budget_chunk, (long long)I0);
}
if (g.exact) {
const int64_t I = g.checkpoint_interval;
if (splits && I <= budget_chunk) {
const int64_t h = g.span > 1 ? std::max<int64_t>(1, std::min(g.span, cfg.max_tok / I))
: 1;
g.grid = h * I;
g.grid_origin = h > 1 ? I : 0;
} else {
g.grid = budget_chunk;
g.grid_origin = 0;
}
}
*out = g;
/* Log the effective chunk length. `plan_chunk` splits at the checkpoint interval first, so
* when the interval is below --max-num-batched-tokens, raising the budget does not change the
* chunk; the log line makes that visible. tip-only window snapshots do not
@@ -167,6 +201,13 @@ int chunk_geometry_resolve(const Program& prog, const Config& cfg, ChunkGeometry
eff >= cfg.max_tok ? ""
: g.span > 1 ? " (the checkpoint interval times the span, not the budget)"
: " (the checkpoint interval, not the budget)");
if (g.exact)
RAD_INFO("prefix resume: exact -- a hit resumes only where an idle server's cold prefill "
"of the same prompt starts a chunk (every %lld tokens%s) and a prompt's tail "
"snapshot is taken where its last chunk starts, so the hit recomputes the cold "
"run's own chunks and answers bit-identically; a turn re-prefills up to one chunk "
"more than --prefix-resume nearest", (long long)g.grid,
g.grid_origin > 0 ? fmt(" from %lld", (long long)g.grid_origin).c_str() : "");
if (cfg.prefill_span > 1) {
/* What the span does here, said once: the intervals the budget holds can be fewer than
* the flag asks for, and a model without an interval grid spans nothing. */
+22
View File
@@ -60,7 +60,29 @@ struct ChunkGeometry {
* tip-only, which keeps the tail alone and so splits no chunk at the interval. */
bool window_periodic = false;
/* --prefix-resume exact (Config::prefix_exact): a prefix hit resumes only where a cold prefill
* of the same prompt starts a chunk, so the tokens a hit recomputes are cut into the chunks a
* cold run cuts them into. A kernel may round a row differently at another chunk length (a
* split-K count chosen from M, a small-M path); computed in the same chunks, the hit's output
* is the cold run's, bit for bit, on an idle server. The grid is where an idle server's prefill
* chunks start: 0, then grid_origin + k * grid. `grid` is the checkpoint interval where chunks
* split at it and it fits the budget, times the span from the second interval on (origin = the
* interval), and otherwise --max-num-batched-tokens rounded down to the quantum (the interval
* is then rounded up to a multiple of it). Zero when not exact. */
bool exact = false;
int64_t grid = 0;
int64_t grid_origin = 0;
bool checkpoints() const { return checkpoint_interval > 0; }
/* The deepest grid position at or below `pos`; `pos` itself when not exact. */
int64_t grid_floor(int64_t pos) const {
if (pos <= 0) return 0;
if (!exact || grid <= 0) return pos;
if (pos < grid_origin) return 0;
return grid_origin + (pos - grid_origin) / grid * grid;
}
bool on_grid(int64_t pos) const { return grid_floor(pos) == pos; }
};
/* The budget of a step a span does not take (ChunkGeometry::shared_tok), from the configuration
+6 -4
View File
@@ -172,7 +172,7 @@ struct IPrefixCache {
/* Longest cached prefix, plus the checkpoint covering the linear state for a prefix of it.
* `blocks_out` receives block ids per cached group, in cached_groups() order, for
* IKVManager::adopt. `cap`, when not null, ends the hit early (HitCap, core/mem/prefix.h). */
* IKVManager::adopt: n_attn_tokens over the group's own block size of them. `cap`, when not null, ends the hit early (HitCap, core/mem/prefix.h). */
virtual CacheHit lookup(const Request& r, std::vector<std::vector<int32_t>>* blocks_out,
const HitCap* cap) const = 0;
virtual const std::vector<int32_t>& cached_groups() const = 0;
@@ -187,10 +187,12 @@ struct IPrefixCache {
/* Release the least recently used entries and their KV references. The scheduler tries this
* before preempting a running request. */
virtual int64_t evict(int64_t n_entries) = 0;
/* Free up to `n_blocks` for the reserve without losing anything a later turn needs. Unlike
/* Free up to `n_entries` for the reserve without losing anything a later turn needs. Unlike
* evict(), it skips entries a live sequence still holds (no block would be freed) and entries
* whose only copy is in VRAM. Returns the number of blocks freed. */
virtual int64_t trim(int64_t n_blocks) = 0;
* whose only copy is in VRAM. An entry is one block in each cached group, or more in a group
* whose blocks are shorter than the entry (PrefixCache::blocks_per_entry). Returns the number
* of entries freed. */
virtual int64_t trim(int64_t n_entries) = 0;
/* Drop every entry in every tier. Implementations with idle tiers must override this: there,
* evict() keeps an entry whose host copy has landed. Returns the number of entries dropped. */
+96 -19
View File
@@ -10,6 +10,7 @@
#include <chrono>
#include <cstdio>
#include <cstdlib>
#include <numeric>
namespace rad {
@@ -97,9 +98,9 @@ int Scheduler::init(Program& prog, const Config& cfg, IKVManager* kv, IPrefixCac
if (kv->block_tokens((int32_t)i) <= 0) continue;
++n_paged;
const auto& cg = pc->cached_groups();
/* Snapshots are taken only where the trunk has a window to snapshot too
* (ChunkGeometry::window_ckpt); without them no drafter window rides in one. */
if (cfg.prefix_cache && geo_.window_ckpt && kv->window_snapshot((int32_t)i) &&
/* Snapshots are taken only on a checkpoint grid (ChunkGeometry::checkpoints()), the
* window's alone or beside linear state; without one no window rides in a slot. */
if (cfg.prefix_cache && geo_.checkpoints() && kv->window_snapshot((int32_t)i) &&
std::find(cg.begin(), cg.end(), (int32_t)i) == cg.end())
++n_snap;
}
@@ -110,19 +111,27 @@ int Scheduler::init(Program& prog, const Config& cfg, IKVManager* kv, IPrefixCac
}
/* The trunk's share: a paged group a trunk layer reads (a bound layer below the trunk's layer
* count; a drafter borrows from base_layer up) that the cache does not index. Recomputing the
* tail cannot repair that hole; see scheduler.h. */
* tail cannot repair that hole; see scheduler.h. A windowed group is restored by a hit that
* resumes at a window snapshot (a checkpoint slot carries the trunk's window, with or without
* linear state beside it); any other such group refuses every hit. */
hit_holes_trunk_ = false;
hit_needs_window_ = false;
for (size_t i = 0; i < prog.kv_groups.size(); ++i) {
if (kv->block_tokens((int32_t)i) <= 0) continue;
bool trunk = false;
for (int l : prog.kv_groups[i].layers) if (l < prog.meta.n_layers) { trunk = true; break; }
if (!trunk) continue;
const std::vector<int32_t>& cg = pc->cached_groups();
if (std::find(cg.begin(), cg.end(), (int32_t)i) == cg.end()) { hit_holes_trunk_ = true; break; }
if (std::find(cg.begin(), cg.end(), (int32_t)i) != cg.end()) continue;
if (prog.kv_groups[i].decl.kind == RAD_KV_WINDOW) hit_needs_window_ = true;
else hit_holes_trunk_ = true;
}
if (hit_holes_trunk_ && cfg.prefix_cache)
RAD_INFO("prefix cache: a trunk layer's paged group is neither prefix-cached nor carried "
"by a checkpoint slot -- a hit re-prefills");
else if (hit_needs_window_ && cfg.prefix_cache)
RAD_INFO("prefix cache: a trunk layer's windowed group is not prefix-cached -- a hit "
"resumes from a window snapshot (a checkpoint slot) or re-prefills");
"resumes from a window snapshot (a checkpoint slot)");
if (cfg.prefill_interleave > 0)
RAD_INFO("scheduler: --prefill-interleave %lld: after a prefill chunk that leaves its "
"prompt unfinished while other sequences decode, the next %lld steps carry "
@@ -470,14 +479,26 @@ void Scheduler::trim_prefix_cache() {
if (!pc_ || !cfg_.prefix_cache) return;
const std::vector<int32_t>& cg = pc_->cached_groups();
if (cg.empty()) return;
const int32_t g = cg[0];
const int64_t total = kv_->total_blocks(g);
if (total <= 0) return;
const int64_t reserve = total / 4; /* a quarter of the pool stays free */
/* Free against the carve, not free_blocks(): on an elastic pool free_blocks() is bounded by
* the live mark, so a pool that returned its unused top would read as nearly full and the
* trim would drain the cache for room the pool can grow back into. */
const int64_t short_by = reserve - (total - kv_->held_blocks(g));
/* An entry covers the lcm of the cached groups' blocks and holds that over a group's block of
* its blocks (PrefixCache::blocks_per_entry); the trim asks for entries, as many as the group
* furthest short needs. */
int64_t entry = 0;
for (int32_t g : cg) {
const int64_t bs = kvgeom_.g[(size_t)g].block_size;
if (bs > 0) entry = entry > 0 ? std::lcm(entry, bs) : bs;
}
int64_t short_by = 0;
for (int32_t g : cg) {
const int64_t total = kv_->total_blocks(g), bs = kvgeom_.g[(size_t)g].block_size;
if (total <= 0 || bs <= 0) continue;
const int64_t reserve = total / 4; /* a quarter of the pool stays free */
/* Free against the carve, not free_blocks(): on an elastic pool free_blocks() is bounded
* by the live mark, so a pool that returned its unused top would read as nearly full and
* the trim would drain the cache for room the pool can grow back into. */
const int64_t blocks = reserve - (total - kv_->held_blocks(g));
const int64_t per = std::max<int64_t>(entry / bs, 1);
if (blocks > 0) short_by = std::max(short_by, (blocks + per - 1) / per);
}
if (short_by > 0) (void)pc_->trim(short_by);
}
@@ -850,7 +871,38 @@ int64_t Scheduler::plan_chunk(int64_t n_computed, int64_t n_target, int64_t budg
int64_t want = remaining < budget ? remaining : budget;
if (geo_.checkpoints()) {
if (geo_.checkpoints() && geo_.exact) {
/* --prefix-resume exact: checkpoints only where an idle cold prefill of this prompt starts
* a chunk (ChunkGeometry::grid), so a hit that resumes at one recomputes the cold run's
* own chunks. Splits as below at the interval and a span's end; the tail snapshot is at
* the start of the prompt's last chunk (tail_checkpoint), a boundary an idle chunk already
* ends on, so it costs a copy and no step. A step that shares the budget (another
* request, decode rows) may straddle it, and is split there. */
const int64_t I = geo_.checkpoint_interval;
const bool periodic = !geo_.window_ckpt || geo_.window_periodic;
const int64_t next = periodic ? ((n_computed / I) + 1) * I : INT64_MAX;
int64_t far = next;
if (geo_.span > 1 && span_now_ && n_computed >= I && n_computed % I == 0)
far = n_computed + geo_.span * I;
if (n_computed + want > far)
want = far - n_computed;
else if (n_computed + want > next && n_computed + want < n_target)
want = (n_computed + want) / I * I - n_computed;
const int64_t tail = tail_checkpoint(n_target);
if (tail > n_computed && n_computed + want >= tail) {
want = tail - n_computed;
if (checkpoint_pos) *checkpoint_pos = tail;
} else if (periodic && (n_computed + want) % I == 0 && geo_.on_grid(n_computed + want)) {
if (checkpoint_pos) *checkpoint_pos = n_computed + want;
}
/* A request's own checkpoint (Request::checkpoint_at) as below, wherever it falls: the
* head the request shares with others resumes there by design. */
const int64_t keep = keep_at / geo_.quantum * geo_.quantum;
if (keep > n_computed && keep <= n_computed + want && keep <= n_target) {
want = keep - n_computed;
if (checkpoint_pos) *checkpoint_pos = keep;
}
} else if (geo_.checkpoints()) {
const int64_t I = geo_.checkpoint_interval;
int64_t next = ((n_computed / I) + 1) * I; /* strictly after n_computed */
if (geo_.window_ckpt && !geo_.window_periodic) {
@@ -934,6 +986,16 @@ int64_t Scheduler::plan_chunk(int64_t n_computed, int64_t n_target, int64_t budg
int64_t Scheduler::tail_checkpoint(int64_t target) const {
const int64_t I = geo_.checkpoint_interval, Q = geo_.quantum;
if (I <= 0 || Q <= 0) return -1;
if (geo_.exact) {
/* --prefix-resume exact: where the prompt's last chunk starts on the resume grid, so an
* exact repeat re-prefills that chunk as the cold run did, and an appended turn that chunk
* and what it added. A window snapshot keeps its margin below (next comment): the grid
* point under the end less a block. Past the first interval, as below. */
const int64_t margin = geo_.window_ckpt ? std::max<int64_t>(Q, 16) : 1;
const int64_t t = geo_.grid_floor(target - margin);
if (geo_.window_ckpt) return t >= I ? t : -1;
return t > I ? t : -1;
}
const int64_t tail = target / Q * Q;
/* A window snapshot is taken at the tail of any prompt of an interval or more, a block short
* of the end: the next turn re-renders the last few tokens (the generation prompt is replaced
@@ -1136,6 +1198,20 @@ int Scheduler::admit_one(SchedReq& s, int64_t budget, int64_t reserve) {
if (reuse > last) reuse = last;
if (reuse < 0) reuse = 0;
/* --prefix-resume exact: resume only on the grid an idle cold prefill chunks this prompt
* on (ChunkGeometry::grid), so the tokens recomputed below are cut into the cold run's own
* chunks. A snapshot is already on it (plan_chunk writes none off it, but for a request's
* own Request::checkpoint_at); a hit at a block (no snapshot needed) steps back to it. */
if (geo_.exact && reuse > 0 &&
!(geo_.checkpoints() && h.checkpoint_slot >= 0 && reuse == h.n_linear_tokens)) {
const int32_t g = (int32_t)geo_.grid_floor(reuse);
if (g < reuse)
RAD_DEBUG("admit %llu: prefix hit of %lld tokens resumes at %lld, on the "
"%lld-token chunk grid (--prefix-resume exact)", (unsigned long long)r.id,
(long long)reuse, (long long)g, (long long)geo_.grid);
reuse = g;
}
/* A hit on a hybrid model needs a checkpoint. The adopted blocks cover the paged
* attention groups, but nothing covers a recurrent group, whose state lives in a
* per-sequence slot; skipping tokens also requires restoring that state (`ckpt_src`,
@@ -1158,9 +1234,10 @@ int Scheduler::admit_one(SchedReq& s, int64_t budget, int64_t reserve) {
(unsigned long long)r.id, (long long)h.n_attn_tokens, (long long)reuse);
}
/* A window snapshot covers the trunk's windowed groups at the checkpoint only. The lookup
* clamped the hit to it (a window counts as state there), so a hit with a slot is exactly
* at a snapshot; one without is no hit. */
const bool win_hit = geo_.window_ckpt && kv_->has_window_snapshot() && reuse > 0 &&
* clamped the hit to it (a window counts as state there, and linear state beside it
* resumes there too), so a hit with a slot is exactly at a snapshot; one without is no
* hit. */
const bool win_hit = geo_.checkpoints() && kv_->has_window_snapshot() && reuse > 0 &&
h.checkpoint_slot >= 0 && reuse == h.n_linear_tokens;
/* Windowed-group clamp: the trunk's own KV includes a paged group the cache does not
@@ -1173,7 +1250,7 @@ int Scheduler::admit_one(SchedReq& s, int64_t budget, int64_t reserve) {
*
* A drafter's own group (bound at base_layer and above) is not this clamp's business:
* draft refill caps the hit so the recompute covers the drafter's window. */
if (reuse > 0 && hit_holes_trunk_ && !win_hit) {
if (reuse > 0 && (hit_holes_trunk_ || (hit_needs_window_ && !win_hit))) {
reuse = 0;
s.hit_cut = SchedReq::HitCut::Window;
RAD_DEBUG("admit %llu: prefix hit %lld zeroed -- a trunk paged group is not "
+6 -2
View File
@@ -491,12 +491,16 @@ private:
* place. */
bool hit_holes_draft_ = true;
/* Does a hit leave a hole in the trunk: a paged group with a trunk layer (bound below
* meta.n_layers; drafter layers bind from `base_layer` up) that the cache does not index?
* Unlike a drafter hole, recomputing the tail cannot repair it; see admit_one. */
* meta.n_layers; drafter layers bind from `base_layer` up) that the cache does not index and
* no checkpoint slot carries? Unlike a drafter hole, recomputing the tail cannot repair it;
* every hit is refused (admit_one). */
bool hit_holes_trunk_ = false;
/* The end-of-generation ids, sorted and unique (set_eog), and a lookup in them. */
std::vector<int32_t> eog_;
bool is_eog(int32_t id) const;
/* A windowed trunk group, which the cache does not index: a hit stands only where it resumes
* at a window snapshot (IKVManager::has_window_snapshot), which restores the window. */
bool hit_needs_window_ = false;
/* R-SWA: the groups whose KV manager carries per-sequence runtime windows, with the W a
* sequence is armed with (IKVManager::runtime_window_tokens). Empty for every model that does
* not declare one, in which case admission and commit are exactly as before. A sequence with