Archived
The gemma4_bf16 architecture (sliding and global attention, the proportional RoPE ladder, the assistant head as a drafter that reads the trunk's cache, the vision tower), its w4a8 decode junctions, the h512/gqa8 and h256/gqa2 attention kernels, compressed-tensors int4 import, multi-EOS from generation_config, the call/reasoning reply format, a window group that keeps the keys a prefill chunk still reads, and window snapshots in checkpoint slots so a prefix hit on a sliding-window trunk resumes. Fixes on top: the investigation switches and diagnostic blocks out of the served paths; int4 import through the checkpoint import framework; attention_k_eq_v from the config; the fused junctions only where every reader takes their pair; a shared-KV head binds no group of its own; the compose file without HSA_ENABLE_IPC_MODE_LEGACY (TP2 fails with it on ROCm 10.1); a checkpoint carries a window only where a hit resumes from it; the README says the linked container is keptn42's calibrated GPTQ build and what the in-tree recipe gives.
This commit is contained in:
commit
7c7f755931
84 files changed
+10292
-230
No files matched your search
@@ -94,6 +94,13 @@ struct AdvanceArgs {
|
||||
* the host's position keeps the host's three components, which is what a media token needs. */
|
||||
int64_t rope_off = -1;
|
||||
int32_t rope_pitch = 0;
|
||||
/* A SERIAL HEAD WITH NO CACHE OF ITS OWN (rad_drafter_shared_kv), read by ADV_HIST and
|
||||
* ADV_ROUND alone: its history pass is one row a sequence at the committed position, and every
|
||||
* round sits there too, its context ending at that position. `rot_shift` is where those rows
|
||||
* sit for the rotary, 0 or 1 past the committed position. Zero for every other head, whose
|
||||
* passes advance exactly as they always did. */
|
||||
int32_t shared_kv = 0;
|
||||
int32_t rot_shift = 0;
|
||||
};
|
||||
|
||||
/* Issue one advance on `s`. The rows are independent, so the card runs them in parallel. */
|
||||
|
||||
@@ -109,6 +109,22 @@ RAD_ADV_FN void adv_row(const AdvanceArgs& a, int i) {
|
||||
return;
|
||||
}
|
||||
case ADV_HIST: {
|
||||
if (a.shared_kv) {
|
||||
/* A HEAD THAT READS THE TRUNK'S CACHE AND KEEPS NONE (rad_drafter_shared_kv). There is
|
||||
* no history to fill, so the pass is round 1 alone and its one row is the last one of
|
||||
* the run below: the committed position, the trunk's row there, the token the trunk
|
||||
* chose after it. Its context is the committed positions and NOT the verify step's
|
||||
* whole run -- the cache past the committed position holds the K and V of the drafts
|
||||
* this verify rejected. The host staged the run's first trunk row; the card adds the
|
||||
* drafts it accepted. */
|
||||
int32_t* out = adv_i32(a, a.out_off);
|
||||
tok[k] = a.src[rout + S.acc];
|
||||
adv_pos(a, pos, k, S.head + a.rot_shift);
|
||||
out[k] += S.acc;
|
||||
ctx[i] = S.head;
|
||||
adv_groups(a, i, k, S.head, 1);
|
||||
return;
|
||||
}
|
||||
/* `aux` positions the host already knew the tokens of, then the verify step's own
|
||||
* positions up to the last one it committed, each with the token committed after it --
|
||||
* the sampler's pick there, since the drafts before it agreed with it. The pass has a
|
||||
@@ -133,12 +149,14 @@ RAD_ADV_FN void adv_row(const AdvanceArgs& a, int i) {
|
||||
}
|
||||
case ADV_ROUND: {
|
||||
/* Round 2 onward: round 1 is the history pass's last row (ADV_HIST above), so every
|
||||
* round embeds the proposal of the round before it. */
|
||||
const int32_t base = S.head + a.round - 1;
|
||||
* round embeds the proposal of the round before it. A head with no cache of its own does
|
||||
* not step forward: it stays at the committed position over the committed context, and
|
||||
* what it proposed reaches the next round through its own hidden state. */
|
||||
const int32_t base = a.shared_kv ? S.head : S.head + a.round - 1;
|
||||
const int32_t t = a.src[(int64_t)i * a.src_pitch];
|
||||
S.tok[a.round - 1] = t;
|
||||
tok[k] = t;
|
||||
adv_pos(a, pos, k, base);
|
||||
adv_pos(a, pos, k, a.shared_kv ? base + a.rot_shift : base);
|
||||
ctx[i] = base;
|
||||
adv_groups(a, i, k, base, 1);
|
||||
return;
|
||||
|
||||
@@ -185,8 +185,12 @@ void BatchBuilder::size_set(Set& s, const std::vector<KVGroupInfo>& groups) {
|
||||
s.groups[gi].block_size = e.block_size;
|
||||
int64_t mb = 1;
|
||||
if (e.paged) {
|
||||
int64_t span = (e.kind == RAD_KV_WINDOW && e.window > 0 && e.window < max_ctx_)
|
||||
? e.window : max_ctx_;
|
||||
/* A WINDOW row holds the window behind the step's first query AND the step's own
|
||||
* tokens (KVManager::ensure keeps both), so it is sized for a whole chunk past the
|
||||
* window -- plus the speculative lookahead, which a verify row carries on top. */
|
||||
const int64_t wspan = e.window + max_tok_ + 2 * ((int64_t)std::max(cfg_.n_spec, 0) + 1);
|
||||
int64_t span = (e.kind == RAD_KV_WINDOW && e.window > 0 && wspan < max_ctx_)
|
||||
? wspan : max_ctx_;
|
||||
mb = (span + e.block_size - 1) / e.block_size + 1;
|
||||
}
|
||||
s.groups[gi].max_blocks = mb;
|
||||
|
||||
+14
-5
@@ -47,12 +47,16 @@ int chunk_geometry_resolve(const Program& prog, const Config& cfg, ChunkGeometry
|
||||
* the same block size -- libr4d's is 16 for all of them -- but a hybrid model with a
|
||||
* sliding-window group served by a different kernel is exactly the case §7.2 is written for,
|
||||
* and lcm is the answer that is right for both. */
|
||||
bool has_linear = false;
|
||||
bool has_linear = false, has_window = false;
|
||||
for (const KVGroupInfo& kg : prog.kv_groups) {
|
||||
if (kg.decl.kind == RAD_KV_FULL || kg.decl.kind == RAD_KV_WINDOW) {
|
||||
if (kg.block_size > 0) {
|
||||
g.attn_block = lcm64(g.attn_block, kg.block_size);
|
||||
note(g.why, "attn_block", kg.block_size, kg.name.c_str());
|
||||
/* A drafter's window is recomputed by draft refill; only the trunk's needs a
|
||||
* snapshot for a prefix hit to resume. */
|
||||
if (kg.decl.kind == RAD_KV_WINDOW && !kg.layers.empty() && !kg.drafter)
|
||||
has_window = true;
|
||||
}
|
||||
} else {
|
||||
has_linear = true;
|
||||
@@ -123,11 +127,14 @@ int chunk_geometry_resolve(const Program& prog, const Config& cfg, ChunkGeometry
|
||||
* because down can reach zero and because a longer interval costs retention, not correctness.
|
||||
*
|
||||
* No linear state means no checkpoints: the interval is a property of a state kind that this
|
||||
* model does not have, and pretending otherwise would split chunks for nothing. */
|
||||
if (has_linear && cfg.checkpoint_interval > 0)
|
||||
* model does not have, and pretending otherwise would split chunks for nothing. The one
|
||||
* exception is a trunk window under a prefix cache, which snapshots the window behind a
|
||||
* prompt's tail (window_ckpt) and does not split chunks for it. */
|
||||
if ((has_linear || (has_window && cfg.prefix_cache)) && cfg.checkpoint_interval > 0)
|
||||
g.checkpoint_interval = align_up(cfg.checkpoint_interval, g.quantum);
|
||||
else
|
||||
g.checkpoint_interval = 0;
|
||||
g.window_ckpt = !has_linear && g.checkpoint_interval > 0;
|
||||
|
||||
/* One full quantum has to fit in one step. Below that, every chunk is short, no chunk ever
|
||||
* ends on a boundary, and the checkpoint lands wherever the step happened to end -- which is
|
||||
@@ -150,8 +157,10 @@ int chunk_geometry_resolve(const Program& prog, const Config& cfg, ChunkGeometry
|
||||
* always written, and that clamp comes FIRST. Printing only the budget would hide it -- every
|
||||
* budget above the interval runs the same chunk length, so a sweep over them reads as
|
||||
* "prefill is flat against the chunk" when nothing about the chunk changed. The clamp itself
|
||||
* is correct; a budget that silently does nothing is what has to be visible. */
|
||||
const int64_t eff = (g.checkpoint_interval > 0 && g.checkpoint_interval < cfg.max_tok)
|
||||
* is correct; a budget that silently does nothing is what has to be visible. A window
|
||||
* snapshot is taken at the prompt's tail and splits nothing. */
|
||||
const int64_t eff = (g.checkpoint_interval > 0 && !g.window_ckpt &&
|
||||
g.checkpoint_interval < cfg.max_tok)
|
||||
? g.checkpoint_interval : cfg.max_tok;
|
||||
RAD_INFO("chunk geometry: quantum %lld (attn block %lld, state chunk %lld), checkpoint "
|
||||
"interval %lld, max_tok %lld -> prefill chunks of at most %lld%s",
|
||||
|
||||
@@ -48,6 +48,12 @@ struct ChunkGeometry {
|
||||
/* Every contributor by name, for the startup line and for the refusal. Built once. */
|
||||
std::string why;
|
||||
|
||||
/* NO LINEAR STATE, A SLIDING WINDOW: the checkpoint snapshots the window behind a prompt's
|
||||
* tail (KVManager::has_window_snapshot) and nothing else. Chunks are not split at the
|
||||
* interval -- a window snapshot in the middle of a prompt buys a later hit nothing its tail
|
||||
* does not -- and the interval is only the shortest prompt worth a snapshot. */
|
||||
bool window_ckpt = false;
|
||||
|
||||
bool checkpoints() const { return checkpoint_interval > 0; }
|
||||
};
|
||||
|
||||
|
||||
@@ -46,6 +46,20 @@ struct IKVManager {
|
||||
* free list is empty, which is a scheduling signal and not an error: the caller preempts a
|
||||
* victim and retries. */
|
||||
virtual int ensure(uint64_t seq, int64_t n_tokens) = 0;
|
||||
/* The same, for a step whose first query sits at `keep_from` (the request's n_computed): a
|
||||
* WINDOW group then keeps every block that query can still read, however long the step is.
|
||||
* See KVManager::ensure. The default is for a manager with no windowed group. */
|
||||
virtual int ensure_from(uint64_t seq, int64_t n_tokens, int64_t keep_from) {
|
||||
(void)keep_from;
|
||||
return ensure(seq, n_tokens);
|
||||
}
|
||||
/* A committed step: a WINDOW group gives back what no later query can read. A no-op for a
|
||||
* manager with no windowed group. See KVManager::trim_window. */
|
||||
virtual void trim_window(uint64_t seq, int64_t keep_from) { (void)seq; (void)keep_from; }
|
||||
/* The window snapshot of a prefix hit (KVManager::has_window_snapshot / seed_window). The
|
||||
* defaults are a manager with none: no hit on a windowed trunk is ever resumed. */
|
||||
virtual bool has_window_snapshot() const { return false; }
|
||||
virtual int seed_window(uint64_t seq, int64_t pos) { (void)seq; (void)pos; return -1; }
|
||||
|
||||
/* Adopt blocks a prefix-cache hit found. `n_tokens` must be a whole multiple of the group's
|
||||
* block size -- a partial block is never shared, because the next token written into it would
|
||||
@@ -282,6 +296,10 @@ struct KVBinding final : IKVManager {
|
||||
bool has_sequence(uint64_t s) const override { return m->has_sequence(s); }
|
||||
void free_sequence(uint64_t s) override { m->free_sequence(s); }
|
||||
int ensure(uint64_t s, int64_t n) override { return m->ensure(s, n); }
|
||||
int ensure_from(uint64_t s, int64_t n, int64_t k) override { return m->ensure(s, n, k); }
|
||||
void trim_window(uint64_t s, int64_t k) override { m->trim_window(s, k); }
|
||||
bool has_window_snapshot() const override { return m->has_window_snapshot(); }
|
||||
int seed_window(uint64_t s, int64_t p) override { return m->seed_window(s, p); }
|
||||
int adopt(uint64_t s, int32_t g, const std::vector<int32_t>& b, int64_t n) override {
|
||||
return m->adopt(s, g, b, n);
|
||||
}
|
||||
|
||||
+104
-13
@@ -87,6 +87,21 @@ int Scheduler::init(Program& prog, const Config& cfg, IKVManager* kv, IPrefixCac
|
||||
n_paged, pc->cached_groups().size(),
|
||||
hit_holes_draft_ ? "LEAVES a hole in a drafter's own KV" : "leaves no hole");
|
||||
}
|
||||
/* THE TRUNK'S SHARE of that question: a paged group a TRUNK layer reads (a bound layer below
|
||||
* the trunk's layer count — a drafter borrows from base_layer and above) that the cache does
|
||||
* not index. Its hole is not repairable by recomputing the tail; see scheduler.h. */
|
||||
hit_holes_trunk_ = 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 (hit_holes_trunk_ && 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");
|
||||
draft_refill_ = 0;
|
||||
if (cfg.n_spec > 0 && prog.drafter.kind == RAD_DRAFT_BLOCK && prog.drafter.window > 0 &&
|
||||
hit_holes_draft_) {
|
||||
@@ -95,6 +110,14 @@ int Scheduler::init(Program& prog, const Config& cfg, IKVManager* kv, IPrefixCac
|
||||
"drafter's window is recomputed", (long long)draft_refill_);
|
||||
}
|
||||
|
||||
serial_shared_ = cfg.n_spec > 0 && prog.drafter.kind == RAD_DRAFT_SERIAL &&
|
||||
prog.drafter_shared_kv;
|
||||
serial_rot_shift_ = serial_shared_ ? prog.drafter_pos_offset : 0;
|
||||
if (serial_shared_)
|
||||
RAD_INFO("serial drafter: reads the trunk's cache and keeps none -- no history pass, one "
|
||||
"row a drafting sequence, every round at the committed position%s",
|
||||
serial_rot_shift_ ? " (rotated one past it)" : "");
|
||||
|
||||
DraftController::Params dp;
|
||||
dp.max_depth = cfg.n_spec;
|
||||
/* THE ADAPTIVE WINDOW IS THE SERIAL DRAFTER'S POLICY (draft.h): it prices a draft as one more
|
||||
@@ -726,6 +749,7 @@ int64_t Scheduler::plan_chunk(int64_t n_computed, int64_t n_target, int64_t budg
|
||||
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) next = INT64_MAX; /* a window snapshot: the tail only */
|
||||
if (n_computed + want > next) want = next - n_computed;
|
||||
if (n_computed + want == next) {
|
||||
if (checkpoint_pos) *checkpoint_pos = next;
|
||||
@@ -778,6 +802,7 @@ int64_t Scheduler::plan_chunk(int64_t n_computed, int64_t n_target, int64_t budg
|
||||
bool Scheduler::last_checkpoint(int64_t pos, int64_t target) const {
|
||||
if (!geo_.checkpoints()) return false;
|
||||
const int64_t I = geo_.checkpoint_interval;
|
||||
if (geo_.window_ckpt) { const int64_t t = tail_checkpoint(target); return t > 0 && t <= pos; }
|
||||
if ((pos / I + 1) * I <= target) return false;
|
||||
return tail_checkpoint(target) <= pos;
|
||||
}
|
||||
@@ -789,6 +814,15 @@ 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;
|
||||
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 (a generation
|
||||
* prompt it replaces with the reply), and a snapshot past where the two renderings part is
|
||||
* one no hit can reach -- measured: every prompt whose length left fewer than ~8 tokens in
|
||||
* its last block missed. */
|
||||
if (geo_.window_ckpt) {
|
||||
const int64_t t = (target - std::max<int64_t>(Q, 16)) / Q * Q;
|
||||
return t >= I ? t : -1;
|
||||
}
|
||||
return tail > I && tail - tail / I * I >= std::max<int64_t>(Q, I / 4) ? tail : -1;
|
||||
}
|
||||
|
||||
@@ -864,7 +898,7 @@ int64_t Scheduler::busy_cap(int64_t n_computed, int64_t n_target, int64_t budget
|
||||
cap = std::max(cap, floor);
|
||||
const int64_t remaining = n_target - n_computed;
|
||||
int64_t span = remaining; /* where this run of chunks must end */
|
||||
if (geo_.checkpoints()) {
|
||||
if (geo_.checkpoints() && !geo_.window_ckpt) {
|
||||
const int64_t I = geo_.checkpoint_interval;
|
||||
span = std::min(span, ((n_computed / I) + 1) * I - n_computed);
|
||||
}
|
||||
@@ -978,6 +1012,36 @@ int Scheduler::admit_one(SchedReq& s, int64_t budget, int64_t reserve) {
|
||||
* snapshot to resume from -- that is every recurrent layer continuing from whatever the
|
||||
* slot last held, and it reads as fluent text rather than as a failure. */
|
||||
if (reuse > 0 && has_linear_state_ && h.checkpoint_slot < 0) reuse = 0;
|
||||
/* A WINDOW SNAPSHOT COVERS THE TRUNK'S WINDOWED GROUPS -- at the checkpoint and nowhere
|
||||
* else. The lookup clamped the hit to it (PrefixCache: 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 &&
|
||||
h.checkpoint_slot >= 0 && reuse == h.n_linear_tokens;
|
||||
|
||||
/* THE WINDOWED-GROUP CLAMP — a prefix hit on a model whose OWN (trunk) KV includes a
|
||||
* paged group the cache does not index. prefix.cpp skips RAD_KV_WINDOW groups — their
|
||||
* blocks below the window are recycled, so no block list describes them; the comment
|
||||
* there says a hit "costs the sequences ... a windowed layer whose cache starts where
|
||||
* the hit ended" — this is that cost in KV corruption. The adopted blocks cover the
|
||||
* cached groups ONLY, while `used = ctx_len + n_tokens` (batch.cpp) makes every
|
||||
* windowed trunk layer read the positions below ctx_len that this sequence NEVER WROTE
|
||||
* — and grab_block's min-heap (kv.cpp) hands this sequence the very blocks the most
|
||||
* recent tenant wrote. The model then attends to the PREVIOUS REQUEST'S KV as its own
|
||||
* prefix: fluent text carrying the last request's CONTENT. Zero the reuse; the whole
|
||||
* prompt re-prefills, which is what a window snapshot (below) exists to avoid -- seconds
|
||||
* to minutes of first-token time on a long agent turn.
|
||||
*
|
||||
* NOT the drafter's hole. A drafter's own group (bound at base_layer and above — see
|
||||
* scheduler.h) is repaired by draft refill, which CAPS the hit so the recompute covers
|
||||
* the drafter's window while the trunk reuses the rest; zeroing here would delete that
|
||||
* design. The trunk's hole, which no tail-recompute can fix, is this clamp's own. */
|
||||
if (reuse > 0 && hit_holes_trunk_ && !win_hit) {
|
||||
reuse = 0;
|
||||
RAD_DEBUG("admit %llu: prefix hit %lld zeroed -- a trunk paged group is not "
|
||||
"prefix-cached (windowed) and no window snapshot covers it; re-prefilling",
|
||||
(unsigned long long)r.id,
|
||||
(long long)h.n_attn_tokens);
|
||||
}
|
||||
|
||||
/* Claim the cached blocks. Without this the hit is only an accounting entry: the sequence
|
||||
* would skip computing those tokens and then attend to blocks it does not own. adopt()
|
||||
@@ -998,11 +1062,18 @@ int Scheduler::admit_one(SchedReq& s, int64_t budget, int64_t reserve) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
/* The windowed groups start at the floor of the window behind the hit, in fresh
|
||||
* blocks the entry's restore fills from the snapshot. */
|
||||
if (reuse > 0 && win_hit && kv_->seed_window(r.id, reuse) < 0) {
|
||||
kv_->preempt(r.id);
|
||||
reuse = 0;
|
||||
}
|
||||
}
|
||||
|
||||
r.n_computed = reuse;
|
||||
r.n_cached = reuse;
|
||||
s.ckpt_src = (reuse > 0 && reuse == h.n_linear_tokens) ? h.checkpoint_slot : -1;
|
||||
s.ckpt_src = (reuse > 0 && reuse == h.n_linear_tokens &&
|
||||
(has_linear_state_ || win_hit)) ? h.checkpoint_slot : -1;
|
||||
s.hit_attn = h.n_attn_tokens;
|
||||
}
|
||||
|
||||
@@ -1026,7 +1097,7 @@ int Scheduler::admit_one(SchedReq& s, int64_t budget, int64_t reserve) {
|
||||
/* THE CACHE GIVES WAY TO A NEW REQUEST as it does to a running one: with nothing running,
|
||||
* no other path would ever take blocks back from it, and the request would wait for good. */
|
||||
int rounds = 0;
|
||||
while (kv_->ensure(r.id, r.n_computed + want) < 0) {
|
||||
while (kv_->ensure_from(r.id, r.n_computed + want, r.n_computed) < 0) {
|
||||
if (++rounds > 256 || !cfg_.prefix_cache || pc_->evict(256) <= 0) {
|
||||
rc = RAD_E_NOMEM;
|
||||
break;
|
||||
@@ -1217,7 +1288,7 @@ const RadBatch* Scheduler::step() {
|
||||
static const int kMaxEvictRounds = 256;
|
||||
int evict_rounds = 0;
|
||||
bool failed = false;
|
||||
while (kv_->ensure(r.id, r.n_computed + want) < 0) {
|
||||
while (kv_->ensure_from(r.id, r.n_computed + want, r.n_computed) < 0) {
|
||||
/* THE CACHE GIVES ITS BLOCKS BACK BEFORE A RUNNING REQUEST DOES. Everything the prefix
|
||||
* cache holds is a saving on work that has already been done once; everything a
|
||||
* running request holds is work in flight. Evicting first is therefore strictly the
|
||||
@@ -1429,7 +1500,7 @@ const RadBatch* Scheduler::step() {
|
||||
int64_t ckpt = -1;
|
||||
const int64_t grown = e.ckpt_slot < 0 && limit > had
|
||||
? plan_chunk(r.n_computed, s.seq_len(), limit, &ckpt, r.checkpoint_at) : had;
|
||||
if (grown > had && kv_->ensure(r.id, r.n_computed + grown) >= 0) {
|
||||
if (grown > had && kv_->ensure_from(r.id, r.n_computed + grown, r.n_computed) >= 0) {
|
||||
e.n_tokens = (int32_t)grown;
|
||||
e.produces_token = (r.n_computed + grown >= s.seq_len());
|
||||
if (ckpt > 0) {
|
||||
@@ -1681,7 +1752,7 @@ const RadBatch* Scheduler::step_next_locked() {
|
||||
const int32_t d = s.draft_next;
|
||||
const int64_t want = 1 + d;
|
||||
if (ctx_limit_ > 0 && ctx_up + want > ctx_limit_) return nullptr;
|
||||
if (kv_->ensure(r.id, ctx_up + want) < 0) return nullptr;
|
||||
if (kv_->ensure_from(r.id, ctx_up + want, r.n_computed) < 0) return nullptr;
|
||||
|
||||
StepEntry n;
|
||||
n.req = &r;
|
||||
@@ -1822,6 +1893,10 @@ int Scheduler::commit(const StepResult& res) {
|
||||
|
||||
if (e.is_prefill) {
|
||||
r.n_computed += e.n_tokens;
|
||||
/* The chunk is committed: a window group gives its blocks below the window back now,
|
||||
* not at this sequence's next ensure(), which may be steps away while others
|
||||
* prefill. */
|
||||
kv_->trim_window(r.id, r.n_computed);
|
||||
step_prefill_tok_ += e.n_tokens;
|
||||
server::sink_prompt_progress(r.sink, r.n_computed, (int32_t)r.prompt.size(),
|
||||
r.n_cached);
|
||||
@@ -2158,7 +2233,8 @@ int Scheduler::serial_begin(int* rounds, SerialRow* out, int max) {
|
||||
* unwritten in the head's layer for good. The same goes for a row too near the end of the
|
||||
* context for its rounds, whose last positions do not exist. */
|
||||
if (can_draft && ctx_limit_ > 0 && head_max + *rounds > ctx_limit_) can_draft = false;
|
||||
if (can_draft && kv_->ensure(r.id, head_max + *rounds) < 0) can_draft = false;
|
||||
if (can_draft && kv_->ensure_from(r.id, head_max + *rounds, r.n_computed) < 0)
|
||||
can_draft = false;
|
||||
|
||||
/* THE HISTORY THIS STEP OWES, in two parts. Before the verified positions, the positions
|
||||
* the step computed whose next token the host holds -- a prefill chunk's prompt -- from
|
||||
@@ -2179,9 +2255,19 @@ int Scheduler::serial_begin(int* rounds, SerialRow* out, int max) {
|
||||
const int64_t owed_end = e.produces_token ? vstart : (int64_t)e.ctx_len + e.n_tokens;
|
||||
int64_t hs = s.draft_filled > e.ctx_len ? s.draft_filled : (int64_t)e.ctx_len;
|
||||
if (hs > owed_end) hs = owed_end;
|
||||
const int64_t lead = owed_end - hs;
|
||||
int64_t lead = owed_end - hs;
|
||||
const int64_t verified = e.produces_token ? e.n_spec + 1 : 0;
|
||||
const int64_t hist_n = lead + verified;
|
||||
int64_t hist_n = lead + verified;
|
||||
|
||||
/* A HEAD WITH NO CACHE OF ITS OWN IS OWED NO HISTORY (rad_drafter_shared_kv). Its one row
|
||||
* is round 1's -- the committed position, whichever of the verified ones the card says it
|
||||
* is -- and a sequence that does not draft has nothing to run at all. */
|
||||
if (serial_shared_) {
|
||||
if (!can_draft) continue;
|
||||
hs = vstart;
|
||||
lead = 0;
|
||||
hist_n = 1;
|
||||
}
|
||||
|
||||
if (!can_draft && hist_n <= 0) continue; /* nothing owed and nothing to draft */
|
||||
|
||||
@@ -2191,7 +2277,9 @@ int Scheduler::serial_begin(int* rounds, SerialRow* out, int max) {
|
||||
m.card = e.card;
|
||||
m.hist_row = (int32_t)(base + (hs - e.ctx_len));
|
||||
m.hist_n = (int32_t)hist_n;
|
||||
m.hist_pos = hs;
|
||||
/* The shared head's one row sits wherever the card's acceptance puts it; the host stages
|
||||
* the furthest that can be, as it does for every round. */
|
||||
m.hist_pos = serial_shared_ ? head_max : hs;
|
||||
m.dev = e.produces_token;
|
||||
m.dev_tok = (int32_t)(base + (vstart - e.ctx_len));
|
||||
m.dev_out = e.dev_out;
|
||||
@@ -2250,8 +2338,10 @@ const RadBatch* Scheduler::serial_batch(int round, int step) {
|
||||
e.card = m.card;
|
||||
e.n_tokens = round == 0 ? m.hist_n : 1;
|
||||
/* A round's position is the card's; the host stages the furthest it can be, which is what
|
||||
* every host-side ceiling reads. The history's are known here exactly. */
|
||||
e.ctx_len = (int32_t)(round == 0 ? m.hist_pos : m.base_pos + (round - 1));
|
||||
* every host-side ceiling reads. The history's are known here exactly. A head with no
|
||||
* cache of its own does not step forward, so neither does its ceiling. */
|
||||
e.ctx_len = (int32_t)(round == 0 ? m.hist_pos
|
||||
: m.base_pos + (serial_shared_ ? 0 : round - 1));
|
||||
e.n_spec = 0;
|
||||
/* One-based and constant: a head pass commits exactly the tokens it was handed. The head
|
||||
* issues no linear-state op, so nothing reads it; it is set rather than left at a value
|
||||
@@ -2409,7 +2499,8 @@ int Scheduler::block_begin(int block, int depth, int window, int32_t mask, Block
|
||||
/* The query block writes positions n_computed .. n_computed + block - 1, and none of them
|
||||
* may be at or past the end of the context. */
|
||||
const bool ok_ctx = ctx_limit_ <= 0 || r.n_computed + block <= ctx_limit_;
|
||||
const bool ok_kv = ok_room && ok_ctx && kv_->ensure(r.id, r.n_computed + block) >= 0;
|
||||
const bool ok_kv = ok_room && ok_ctx &&
|
||||
kv_->ensure_from(r.id, r.n_computed + block, r.n_computed) >= 0;
|
||||
static const bool dbg = [] { const char* v = std::getenv("RADIANCE_DEBUG_DRAFT");
|
||||
return v && *v && *v != '0'; }();
|
||||
if (dbg) {
|
||||
|
||||
+26
-2
@@ -191,8 +191,14 @@ public:
|
||||
|
||||
/* The batch the last build produced for one rank; see BatchBuilder::batch_for. */
|
||||
const RadBatch* batch_for(int rank) const { return builder_.batch_for(rank); }
|
||||
/* The advance's view of that build on one rank; see BatchBuilder::advance_args. */
|
||||
AdvanceArgs advance_args(int rank) const { return builder_.advance_args(rank); }
|
||||
/* The advance's view of that build on one rank; see BatchBuilder::advance_args. It carries
|
||||
* the serial head's shape as well, which only the draft passes' modes read. */
|
||||
AdvanceArgs advance_args(int rank) const {
|
||||
AdvanceArgs a = builder_.advance_args(rank);
|
||||
a.shared_kv = serial_shared_ ? 1 : 0;
|
||||
a.rot_shift = serial_rot_shift_;
|
||||
return a;
|
||||
}
|
||||
void fini();
|
||||
|
||||
/* ---------------------------------------------------------- off the step path */
|
||||
@@ -505,6 +511,18 @@ private:
|
||||
* also skips on a block-size mismatch. A bound like this lives in exactly one place; a second
|
||||
* copy is free to drift out of step with the first. */
|
||||
bool hit_holes_draft_ = true;
|
||||
/* DOES A HIT HOLE THE TRUNK ITSELF? hit_holes_draft_ is the drafter's guard, and draft_refill_
|
||||
* repairs a drafter hole by recomputing the prompt's TAIL. A TRUNK group the cache does not
|
||||
* index admits no such repair: the adopted blocks cover the cached groups only, while the
|
||||
* windowed trunk layers read the positions below ctx_len this sequence never wrote — the KV of
|
||||
* whoever held these blocks last. So a trunk hole refuses the hit outright.
|
||||
*
|
||||
* THE TRUNK IS WHICH GROUPS? The one a drafter borrows is bound at `base_layer` and above —
|
||||
* the drafter's layers run after the trunk's (rad_bind_layer_kv at the drafter declare), so a
|
||||
* group with a bound layer below meta.n_layers belongs to the trunk and a hit must cover it;
|
||||
* a drafter's own group is the refill's business, not this clamp's. Read off the Program,
|
||||
* which carries both the bindings and the trunk's layer count. */
|
||||
bool hit_holes_trunk_ = false;
|
||||
/* DRAFT REFILL. Where a hit leaves a hole in a WINDOWED block drafter's cache, the drafter is
|
||||
* declined until its window has slid past the hole, which on the follow-up turn of a chat --
|
||||
* the turn a prefix hit is for -- is most or all of the reply. So the hit is ended one window
|
||||
@@ -514,6 +532,12 @@ private:
|
||||
* that point would give back more than two windows is kept whole. The drafter's window, or 0
|
||||
* where no hit can leave such a hole. */
|
||||
int64_t draft_refill_ = 0;
|
||||
/* A SERIAL HEAD WITH NO CACHE OF ITS OWN (rad_drafter_shared_kv), off the Program at init. It
|
||||
* owes no history, so serial_begin gives it one row a drafting sequence and none to a sequence
|
||||
* that does not draft, and its rounds stay at the committed position (advance.h). False and 0
|
||||
* for every other drafter, whose passes are staged exactly as before. */
|
||||
bool serial_shared_ = false;
|
||||
int32_t serial_rot_shift_ = 0;
|
||||
BatchBuilder builder_;
|
||||
DraftController draft_;
|
||||
|
||||
|
||||
Reference in new issue
Block a user