radiance 2.1.0

New models. Recursive Flint (39.8B, about 2.5B active) by everplays (#422): `arch/flint`, as an
IQ8R container with rotated int8 experts or a four-bit one, with a sink-aware WMMA prefill
attention and the fused attention prologue at `rotary_dim` 0. Meta Muse Glimmer 30B by
launch80_assets (#423): bf16, an int4 trunk or IQ8R, its windowed vision tower, its DFlash
drafter, the ATEM tool dialect and context extension. Ling-3.0-tiny by RikshaDriver (#441):
served from its GGUF releases or as IQ8R on `bailing_hybrid`, with six more libr3d kernels. Block
FP8, MXFP4 and NVFP4 conversions of the three are not served. IQ8R containers of all three
(round to nearest, no calibration) are on Hugging Face under StillDeadcode: flint-iq8r,
muse-glimmer-30b-iq8r-dflash and ling-3.0-tiny-iq8r. They were checked for sane answers on two
R9700; nothing was benchmarked, and Muse Glimmer's pictures, video and tool calls were not tried.

Speech. Breeze-TTS-2 clones a voice from a reference recording and its transcript, with an
optional delivery direction, by farcryjohn (#436). The published Breeze container needs
converting again with the updated recipe before it can.

Tools. A model entry with `diagnose: DIR` runs a fixed workload on the engine instead of serving
and writes a report: step times, tokens a step, draft acceptance, TTFT and prefill rates by
category, concurrency and prompt length, and with `diagnose-baseline` a token-for-token comparison
against an earlier report (docs/DIAGNOSE.md), by Code000 (#418).

Scheduler. `mixed-step-target-ms` holds a mixed prefill and decode step to a service time, off by
default, by tnhnyzc (#413). `scheduler.stuck-exit` (60s by default, 0 never) ends the process when
the device has advanced nothing for that long, so a restart policy brings a hung server back
(#409, #415, #428).

Fixed. Draft trees with the int8 KV cache, which stopped Gemma 4 31B with DFlash2 on its first
request (#454, diagnosed by aelfwyne). The expert loan held at zero by a few leftover cache blocks
after long sessions (#455, diagnosed by thekk1). The mapped tier's pages kept in the page cache
across a load and a restore, by C_Tamas (#421). Replayed tool calls with blank arguments, and
arguments that are not JSON refused naming the call, by dschmitz (#444). The dashboard's card
temperature reads the junction sensor, by dschmitz (#453). The VRAM shortfall report says what the
KL mode needs, by baKon (#448). The out-of-tree plugin test builds its plugins in parallel and
takes its toolchain from the tree.

Plugin ABI 40: out-of-tree plugins need to be rebuilt.

Thanks to @everplays, @launch80_assets, @RikshaDriver, @farcryjohn, @Code000, @tnhnyzc, @C_Tamas, @dschmitz, @baKon, @aelfwyne and @thekk1.
This commit is contained in:
Deadcode committed 2026-11-15 16:00:05 +01:00
1 parent ddcb9c69bb
commit 2a2b756371
166 files changed
+20749 -327

No files matched your search

+1 -1
View File
@@ -31,7 +31,7 @@ __global__ __launch_bounds__(kThreads) void r4dchain_step_advance(rad::AdvanceAr
}
/* ADV_COMPACT: block (x, y) is work item x (a pool, layer and head) of batch row y; its threads
* split the K|V row into 16-byte chunks. Chained like the advance where launches chain, every
* split the K|V row into 16-byte chunks and its last words. Chained like the advance where launches chain, every
* block waiting for the kernels before it and counting itself in after its moves. */
constexpr int kCompactThreads = 64;
__device__ __forceinline__ void step_compact_body(const rad::AdvanceArgs& a) {
+7 -2
View File
@@ -320,7 +320,9 @@ RAD_ADV_FN void adv_row(const AdvanceArgs& a, int i) {
}
/* ADV_COMPACT for one (batch row, pool, layer, head): the moves the row's accepted path asks
* for, `c0` and every `cstep`-th 16-byte chunk of each K|V row. Node path[t] stored at
* for, `c0` and every `cstep`-th 16-byte chunk of each K|V row. A row is whole 32-bit words; the
* words past its last whole chunk (an int8 cache's two f16 scales) go with the caller whose chunk
* set the next chunk would be in. Node path[t] stored at
* base + path[t] and belongs at base + t + 1. path[] increases (a parent's index is below its
* child's) and path[t] >= t + 1, so in t order no move overwrites a node a later move still reads:
* a destination base + t + 1 is either the node's own place or a place a node already moved out
@@ -340,7 +342,8 @@ RAD_ADV_FN void adv_compact(const AdvanceArgs& a, int i, int p, int l, int h, in
const int32_t base = S.head - S.acc;
const int32_t bs = e.block_size;
char* lay = P.base + (int64_t)l * P.layer_stride;
const int32_t chunks = P.row_bytes / 16;
const int32_t chunks = P.row_bytes / 16, tail = P.row_bytes % 16 / 4;
const bool tails = tail > 0 && chunks % cstep == c0;
for (int32_t t = 0; t < np; ++t) {
const int32_t src = base + S.path[t], dst = base + t + 1;
if (src == dst) continue;
@@ -355,6 +358,8 @@ RAD_ADV_FN void adv_compact(const AdvanceArgs& a, int i, int p, int l, int h, in
v3 = from[4 * c + 3];
to[4 * c] = v0; to[4 * c + 1] = v1; to[4 * c + 2] = v2; to[4 * c + 3] = v3;
}
if (tails)
for (int32_t j = 0; j < tail; ++j) to[4 * chunks + j] = from[4 * chunks + j];
}
}
+88 -2
View File
@@ -43,9 +43,14 @@ struct SchedMetrics {
* (Scheduler::step_next). Every other step leaves the card idle during the host's commit and
* staging. */
int64_t steps_ahead = 0;
/* Prefill chunks capped to leave room for decode rows or a waiting admission in the same
* step (--busy-prefill-chunk). Zero under `off`. */
/* Prefill chunks capped to leave room for other work in the same step, by the fixed busy
* policy or the adaptive mixed-step target. */
int64_t prefill_chunks_capped = 0;
/* --mixed-step-target-ms: mixed steps observed, those over their target, and the current
* seeded or learned aggregate prefill-row cap (0 when the policy is disabled). */
int64_t mixed_step_target_steps = 0;
int64_t mixed_step_target_misses = 0;
int64_t mixed_step_target_prefill_cap = 0;
/* Cumulative nanoseconds spent inside steps, total and for decode steps only. Wall time over
* a step count is not a step time: it charges idle gaps between requests to steps and mixes
@@ -213,4 +218,85 @@ private:
double mean_ = 0.0; /* the rejection reference */
};
/* Feedback controller for --mixed-step-target-ms. Completed mixed-step service time includes the
* forward and drafter work, but not queueing or HTTP transport. Scale rows by target/observed when the
* target is missed; grow only after three representative steps below 95% of the target and by at
* most a quarter of the measured headroom. A one-quantum probe lets a conservative cap recover
* when fixed decode cost makes proportional scaling alone predict no larger legal chunk.
*
* The controller is deliberately model-agnostic. It may take a cap below an expert stager's
* throughput floor: satisfying an explicitly requested latency target while decoders are active
* is the point of the flag. With the flag off no caller consults it. */
struct MixedStepTarget {
void reset() { cap_ = 0; good_ = 0; }
/* A new server has no mixed-step timing yet. Begin at a conservative fraction of that
* model's declared mixed-step capacity instead of allowing one unbounded calibration miss;
* subsequent observations replace or grow this seed exactly like any learned cap. */
void seed(int64_t rows, int64_t quantum, int64_t max_rows) {
if (rows <= 0 || max_rows <= 0) return;
const int64_t q = quantum > 0 ? quantum : 1;
max_rows = std::max<int64_t>(q, max_rows / q * q);
rows = std::max<int64_t>(q, rows / q * q);
cap_ = std::min(rows, max_rows);
good_ = 0;
}
void observe(int64_t rows, int64_t ns, int64_t target_ns, int64_t quantum,
int64_t max_rows, bool representative = true) {
if (rows <= 0 || ns <= 0 || target_ns <= 0 || max_rows <= 0) return;
const int64_t q = quantum > 0 ? quantum : 1;
max_rows = max_rows / q * q;
if (max_rows < q) max_rows = q;
long double scaled = (long double)rows * (long double)target_ns / (long double)ns;
int64_t want = scaled >= (long double)max_rows ? max_rows : (int64_t)scaled;
want = want / q * q;
if (want < q) want = q;
if (cap_ == 0) {
cap_ = want;
good_ = 0;
return;
}
if (ns > target_ns) {
good_ = 0;
/* A prompt tail did not exercise a reusable limit. A checkpoint, fixed busy cap or
* other policy boundary still leaves the prompt unfinished and is representative:
* reducing this controller below that boundary can shorten the next mixed step. */
if (!representative) return;
/* A representative miss corrects immediately. */
if (want < cap_) cap_ = want;
return;
}
/* A short tail did not exercise the cap and cannot justify growing it. Near-target steps
* reset the recovery streak; three clearly fast full-cap samples earn one probe. */
if (!representative || ns * 20 >= target_ns * 19 || cap_ >= max_rows) {
good_ = 0;
return;
}
if (++good_ < 3) return;
good_ = 0;
const int64_t goal = std::max<int64_t>(want, std::min(max_rows, cap_ + q));
int64_t grow = (goal - cap_) / 4;
if (grow < q) grow = q;
cap_ = cap_ + grow;
cap_ = cap_ / q * q;
if (cap_ > goal) cap_ = goal;
if (cap_ > max_rows) cap_ = max_rows;
}
int64_t cap() const { return cap_; }
int64_t limit(int64_t budget, int64_t quantum) const {
if (cap_ <= 0 || budget <= 0) return budget;
const int64_t q = quantum > 0 ? quantum : 1;
const int64_t c = cap_ / q * q;
return c > 0 && c < budget ? c : budget;
}
private:
int64_t cap_ = 0;
int good_ = 0;
};
} /* namespace rad */
+175 -14
View File
@@ -57,6 +57,15 @@ int Scheduler::init(Program& prog, const Config& cfg, IKVManager* kv, IPrefixCac
cfg_ = cfg;
kv_ = kv;
pc_ = pc;
/* THE STEP RECORD EXISTS ONLY FOR --diagnose. Sized here, once, and never on the serving
* path: 64 Ki steps is minutes of decode at any step rate this engine reaches and 2.5 MiB, and
* a record a step is work a serving process has no reader for. A reader that falls further
* behind is told how many it lost, not handed the wrong ones. The recorder is chosen here so
* the step path calls one instead of testing the mode on every step. */
if (cfg_.diagnose_mode()) {
step_ring_.reset(1u << 16);
record_step_ = &Scheduler::record_step;
}
/* The engine resolves the geometry once, because the prefix cache needs its checkpoint
* interval first; a caller without one (tests) resolves its own from prog and cfg. Both
@@ -143,10 +152,48 @@ int Scheduler::init(Program& prog, const Config& cfg, IKVManager* kv, IPrefixCac
"prompt unfinished while other sequences decode, decode-only steps follow for up "
"to %.2f of the chunk step's measured time (at most %lld steps)",
cfg.prefill_interleave_share, (long long)kInterleaveAutoMax);
mixed_target_.reset();
m_.mixed_step_target_prefill_cap = 0;
if (cfg.mixed_step_target_ms > 0) {
const int64_t Q = std::max<int64_t>(geo_.quantum, 1);
/* A mixed step never receives a span's wide lone-prefill budget. Validate the same
* shared budget step() will use, or a spanned deployment can start even though no legal
* prefill quantum fits beside a decoder. */
const int64_t mixed_budget = geo_.shared_tok;
const int64_t draft_rows = 1 + std::max<int64_t>(cfg.n_spec, 0);
const int64_t mixed_floor = cfg.max_seqs > 1 ? Q + draft_rows : 0;
if ((cfg.busy_prefill_chunk > 0 && cfg.busy_prefill_chunk < Q) ||
(mixed_floor > 0 && mixed_budget < mixed_floor)) {
RAD_ERR("--mixed-step-target-ms needs room for one legal prefill quantum and "
"the active decode rows: quantum=%lld, max_seqs=%lld, draft_rows=%lld, "
"mixed_step_budget=%lld, max_num_batched_tokens=%lld, busy_prefill_chunk=%lld",
(long long)Q, (long long)cfg.max_seqs, (long long)draft_rows,
(long long)mixed_budget,
(long long)cfg.max_tok, (long long)cfg.busy_prefill_chunk);
return RAD_E_INVAL;
}
const int64_t max_prefill_quanta = mixed_budget > draft_rows
? (mixed_budget - draft_rows) / Q : 0;
if (cfg.max_seqs > 1 && cfg.max_seqs - 1 > max_prefill_quanta)
RAD_WARN("--mixed-step-target-ms: this batch budget can carry at most %lld legal "
"prefill quantum(s) beside a speculative decode row; with more simultaneous "
"prefills, priority and arrival order continue across later steps",
(long long)max_prefill_quanta);
mixed_target_.seed(std::max<int64_t>(Q, geo_.shared_tok / 4), Q, geo_.shared_tok);
m_.mixed_step_target_prefill_cap = mixed_target_.cap();
RAD_INFO("scheduler: --mixed-step-target-ms %lld: completed mixed steps teach an adaptive "
"aggregate prefill-row cap; while another sequence decodes the cap starts at "
"%lld rows and aims for a full mixed-step duration near %lld ms (a soft target)",
(long long)cfg.mixed_step_target_ms, (long long)mixed_target_.cap(),
(long long)cfg.mixed_step_target_ms);
}
interleave_left_ = 0;
ema_chunk_s_ = 0.0;
ema_decode_s_ = 0.0;
held_prefill_ = 0;
mixed_target_active_ = false;
mixed_target_prefill_planned_ = 0;
mixed_target_prefill_continues_ = false;
draft_refill_ = 0;
if (cfg.n_spec > 0 && prog.drafter.kind == RAD_DRAFT_BLOCK && prog.drafter.window > 0 &&
hit_holes_draft_) {
@@ -1298,23 +1345,24 @@ void Scheduler::set_stage_rows(int64_t rows) {
* about the size of the reserve, as the next step's whole chunk. When that would be under the
* floor, the interval is split into two chunks at or above the floor instead. */
int64_t Scheduler::busy_cap(int64_t n_computed, int64_t n_target, int64_t budget,
int64_t reserve, bool shared, bool rows_only) const {
int64_t reserve, bool shared, bool rows_only,
int64_t mixed_target_shares, int64_t mixed_target_decode_reserve) const {
const int64_t mode = cfg_.busy_prefill_chunk;
if (mode == 0 || budget <= 0) return budget;
if (budget <= 0 || (mode == 0 && !mixed_target_active_)) return budget;
/* Auto holds a chunk back only for work that would otherwise get nothing this step: rows
* behind it, or a request waiting to start. Rows planned ahead of it are in the step anyway.
* An explicit N trades prefill speed for every other row's latency, so it applies whenever
* the chunk shares the step. */
if (reserve <= 0 && !(mode > 0 && shared)) return budget;
if (reserve <= 0 && !(mode > 0 && shared) && !(mixed_target_active_ && shared)) return budget;
reserve = std::max<int64_t>(reserve, 0);
const int64_t Q = std::max<int64_t>(geo_.quantum, 1);
int64_t cap = budget - reserve;
int64_t cap = mode == 0 ? budget : budget - reserve;
if (mode > 0) {
/* The operator's number, with no floor; set_stage_rows warns when it is under the
* stager's step. */
cap = std::min(cap, mode);
} else {
} else if (mode < 0) {
const int64_t floor = std::max(Q, stage_rows_ / Q * Q);
cap = std::max(cap, floor);
const int64_t remaining = n_target - n_computed;
@@ -1331,6 +1379,25 @@ int64_t Scheduler::busy_cap(int64_t n_computed, int64_t n_target, int64_t budget
if (!rows_only && cap < span && span - cap < floor && span - floor >= floor)
cap = span - floor;
}
/* Apply the decoder reservation after the auto staging floor. Otherwise the floor can raise
* a reduced cap back to the whole budget, exclude the decoder, and prevent feedback. */
if (mixed_target_active_ && mixed_target_decode_reserve > 0)
cap = std::min(cap, budget - mixed_target_decode_reserve);
if (mixed_target_active_ && mixed_target_.cap() > 0) {
mixed_target_shares = std::max<int64_t>(0, mixed_target_shares);
/* Share the aggregate allowance evenly among this prefill and those expected later in
* the step. Stable queue order must not let the first request consume the cap while the
* others receive only a minimum quantum on every step. If the learned cap cannot hold
* one quantum apiece, the effective allowance grows just enough to make progress; this
* may miss an impossible target, but it cannot silently starve a queued prompt. */
const int64_t floor = mixed_target_prefill_planned_ + (mixed_target_shares + 1) * Q;
const int64_t allowance = std::max(mixed_target_.cap(), floor);
const int64_t left = std::max<int64_t>(0, allowance - mixed_target_prefill_planned_);
const int64_t participants = mixed_target_shares + 1;
const int64_t fair = left / participants / Q * Q;
if (fair < Q || cap < Q) return 0;
cap = std::min(cap, fair);
}
return std::min(std::max(cap, Q), budget);
}
@@ -1397,7 +1464,8 @@ bool Scheduler::preempt_back() {
/* ------------------------------------------------------------------ admission */
/* Admission checks the KV budget before a request enters running; a request admitted without the
* blocks to run would be preempted on its first step, a full recompute for nothing. */
int Scheduler::admit_one(SchedReq& s, int64_t budget, int64_t reserve) {
int Scheduler::admit_one(SchedReq& s, int64_t budget, int64_t reserve,
int64_t mixed_target_shares) {
Request& r = *s.req;
/* A REQUEST THAT WAS PREEMPTED WAITS FOR ROOM FOR THE WHOLE RECOMPUTE, plus one block of
@@ -1622,11 +1690,13 @@ int Scheduler::admit_one(SchedReq& s, int64_t budget, int64_t reserve) {
/* An admission shares the step the same way: otherwise a cold prompt with requests
* queued behind it takes every token left. The hit is known by now, so the cap applies
* to what really has to run. */
const int64_t cap = busy_cap(r.n_computed, target, budget, reserve, !plan_.e.empty());
const int64_t cap = busy_cap(r.n_computed, target, budget, reserve, !plan_.e.empty(),
false, mixed_target_shares, 0);
const int64_t end = chunk_end(r, target);
want = plan_chunk(r.n_computed, end, cap, &ckpt, r.checkpoint_at, &r);
/* A run read both ways (bidi_fit) the share cannot hold takes the step instead. */
if (want <= 0 && cap < budget && prog_->encoder.bidirectional)
if (want <= 0 && cap < budget && prog_->encoder.bidirectional &&
(!mixed_target_active_ || r.media))
want = plan_chunk(r.n_computed, end, budget, &ckpt, r.checkpoint_at, &r);
else if (cap < budget && want > 0)
admit_full_ = plan_chunk(r.n_computed, end, budget, nullptr, r.checkpoint_at, &r);
@@ -1775,6 +1845,8 @@ const RadBatch* Scheduler::step() {
plan_.spec_is_tree = tree_;
step_prefill_tok_ = 0;
step_decode_tok_ = 0;
mixed_target_prefill_planned_ = 0;
mixed_target_prefill_continues_ = false;
/* A preempted prefill waits for the whole-recompute gate so it cannot loop. That wait is
* bounded here, for every overdue preempted request, not only the queue head: a head that
@@ -1865,7 +1937,7 @@ const RadBatch* Scheduler::step() {
bool any_decoding = false;
bool hold_prefill = false;
held_prefill_ = 0;
if (cfg_.prefill_interleave != 0) {
if (cfg_.prefill_interleave != 0 || cfg_.mixed_step_target_ms > 0) {
for (int32_t slot : running_) {
const SchedReq& s = *reqs_[(size_t)slot];
if (!s.req || !s.running) continue;
@@ -1875,8 +1947,9 @@ const RadBatch* Scheduler::step() {
break;
}
if (!any_decoding) interleave_left_ = 0;
hold_prefill = interleave_left_ > 0;
hold_prefill = cfg_.prefill_interleave != 0 && interleave_left_ > 0;
}
mixed_target_active_ = cfg_.mixed_step_target_ms > 0 && any_decoding;
/* A span is for a prefill alone in its step (ChunkGeometry::span). A spanned step takes as
* long as the intervals it carries, so a decode row riding along would wait that long for its
@@ -1920,7 +1993,7 @@ const RadBatch* Scheduler::step() {
* to be walked needs 1 + the deepest window it can take, each prefill row a share, plus one
* share when a request waits and a slot is free. Counted down as the loop walks, so a row's
* reserve is what is behind it. */
const bool share_step = cfg_.busy_prefill_chunk != 0;
const bool share_step = cfg_.busy_prefill_chunk != 0 || mixed_target_active_;
const int64_t Q = std::max<int64_t>(geo_.quantum, 1);
/* An eighth of this step's budget, which under a span is shared_tok for a shared step. */
const int64_t share_tok = std::max<int64_t>(Q, budget / 8 / Q * Q);
@@ -1934,6 +2007,10 @@ const RadBatch* Scheduler::step() {
if (pf) ++behind_pf; else behind_dec += dec_row_tok;
}
}
const int64_t admit_room = std::min<int64_t>(
(int64_t)free_cards_.size(), cfg_.max_seqs - (int64_t)running_.size());
const int64_t mixed_target_waits = mixed_target_active_
? std::min<int64_t>((int64_t)waiting_.size(), std::max<int64_t>(0, admit_room)) : 0;
const bool admit_waits = share_step && !waiting_.empty() && !free_cards_.empty() &&
(int64_t)running_.size() < cfg_.max_seqs;
/* The first prefill chunk held back this step (its index in plan_.e before the sort) and what
@@ -1977,13 +2054,15 @@ const RadBatch* Scheduler::step() {
if (prefill && hold_prefill) { ++held_prefill_; ++i; continue; }
if (prefill) {
const int64_t shares = share_step ? behind_pf + (admit_waits ? 1 : 0) : 0;
const int64_t mixed_target_shares = mixed_target_active_ ? behind_pf + mixed_target_waits : 0;
const int64_t reserve = share_step ? behind_dec + share_tok * shares : 0;
const int64_t cap = busy_cap(r.n_computed, target, budget, reserve, !plan_.e.empty(),
/*rows_only=*/shares == 0);
/*rows_only=*/shares == 0, mixed_target_shares, behind_dec);
const int64_t end = chunk_end(r, target);
want = plan_chunk(r.n_computed, end, cap, &ckpt, r.checkpoint_at, &r);
/* A run read both ways (bidi_fit) the share cannot hold takes the step instead. */
if (want <= 0 && cap < budget && prog_->encoder.bidirectional)
if (want <= 0 && cap < budget && prog_->encoder.bidirectional &&
(!mixed_target_active_ || r.media))
want = plan_chunk(r.n_computed, end, budget, &ckpt, r.checkpoint_at, &r);
else if (cap < budget && want > 0 && capped_at < 0) {
const int64_t full = plan_chunk(r.n_computed, end, budget, nullptr,
@@ -2129,6 +2208,9 @@ const RadBatch* Scheduler::step() {
s.sched_tokens = (int32_t)want;
plan_.e.push_back(e);
plan_.n_tok += want;
if (prefill && mixed_target_active_) {
mixed_target_prefill_planned_ += want;
}
if (!prefill && s.n_draft > plan_.n_spec) plan_.n_spec = s.n_draft;
budget -= want;
++i;
@@ -2173,7 +2255,15 @@ const RadBatch* Scheduler::step() {
? share_tok : 0;
admit_full_ = 0;
admit_next_step_ = false;
const int as = admit_one(s, budget, areserve);
const int64_t mixed_target_shares = mixed_target_active_
? std::min<int64_t>(
(int64_t)waiting_.size() - (int64_t)at - 1,
std::min<int64_t>(
cfg_.max_seqs - (int64_t)plan_.e.size() - 1,
(int64_t)free_cards_.size() - 1))
: 0;
const int as = admit_one(s, budget, areserve,
std::max<int64_t>(0, mixed_target_shares));
/* A preempted request waiting for its whole-recompute gate lets the next one be asked;
* an overdue one does not. The room the overdue path made this step is for it, and a
* request queued behind it would take that room back: a victim just forced off for it,
@@ -2237,6 +2327,9 @@ const RadBatch* Scheduler::step() {
s.entry = (int32_t)plan_.e.size();
plan_.e.push_back(e);
plan_.n_tok += s.sched_tokens;
if (mixed_target_active_) {
mixed_target_prefill_planned_ += s.sched_tokens;
}
budget -= s.sched_tokens;
took_.push_back(at);
++at;
@@ -2266,6 +2359,11 @@ const RadBatch* Scheduler::step() {
* to it at most. */
int64_t limit = std::min(capped_full, had + budget);
if (cfg_.busy_prefill_chunk > 0) limit = std::min(limit, std::max(had, cfg_.busy_prefill_chunk));
if (mixed_target_active_ && mixed_target_.cap() > 0) {
const int64_t other = std::max<int64_t>(0, mixed_target_prefill_planned_ - had);
const int64_t left = std::max<int64_t>(0, mixed_target_.cap() - other);
limit = std::min(limit, std::max(had, left));
}
int64_t ckpt = -1;
const int64_t grown = e.ckpt_slot < 0 && limit > had
? plan_chunk(r.n_computed, chunk_end(r, s.seq_len()), limit, &ckpt, r.checkpoint_at, &r)
@@ -2276,6 +2374,7 @@ const RadBatch* Scheduler::step() {
if (ckpt > 0) reserve_ckpt(e, r, ckpt, (int32_t)(ckpt - r.n_computed - 1));
s.sched_tokens = (int32_t)grown;
plan_.n_tok += grown - had;
if (mixed_target_active_) mixed_target_prefill_planned_ += grown - had;
budget -= grown - had;
}
if (e.n_tokens < capped_full) ++m_.prefill_chunks_capped;
@@ -2313,6 +2412,11 @@ const RadBatch* Scheduler::step() {
plan_.phase = any_pf && n_dec ? RAD_PHASE_MIXED
: any_pf ? RAD_PHASE_PREFILL
: RAD_PHASE_DECODE;
if (mixed_target_active_) {
mixed_target_prefill_continues_ = false;
for (const StepEntry& e : plan_.e)
if (e.is_prefill && !e.produces_token) mixed_target_prefill_continues_ = true;
}
/* Step-shape counters. `steps_mixed` / `steps` shows how often a step carries both kinds of
* row. `decode_rows_unspeculated` counts every decode row that took no draft, whatever the
@@ -2907,6 +3011,18 @@ int Scheduler::commit(StepResult& res) {
*
* Host timing, always available. Per-op device timing adds synchronisations that change the
* mover's slot supply, so profiled runs are not comparable (spec §16). */
void Scheduler::record_step(int64_t ns, int64_t end_ns) {
diag::StepRecord r;
r.step = m_.steps;
r.phase = plan_.phase;
r.n_seq = (int32_t)plan_.e.size();
r.prefill_tok = (int32_t)step_prefill_tok_;
r.decode_tok = (int32_t)step_decode_tok_;
r.ns = ns;
r.end_ns = end_ns;
step_ring_.push(r);
}
void Scheduler::step_finished() {
std::lock_guard<std::mutex> lk(mu_);
if (step_start_.time_since_epoch().count() == 0) return; /* no step has run */
@@ -2915,6 +3031,9 @@ void Scheduler::step_finished() {
const int64_t ns =
std::chrono::duration_cast<std::chrono::nanoseconds>(step_end - step_start_).count();
m_.step_ns += ns;
mixed_target_observe_locked(ns);
(this->*record_step_)(ns, std::chrono::duration_cast<std::chrono::nanoseconds>(
step_end.time_since_epoch()).count());
if (plan_.phase == RAD_PHASE_DECODE) {
m_.step_ns_decode += ns;
/* The single-sequence floor over the step just timed (rad::DecodeFloor). A mixed step
@@ -2972,6 +3091,48 @@ void Scheduler::step_finished() {
step_start_ = {};
}
/* The mixed-step target's one timing entry point. `step_finished` supplies the live bracket;
* note_mixed_step_cost supplies a deterministic bracket to scheduler tests and host simulations. */
void Scheduler::mixed_target_observe_locked(int64_t ns) {
int64_t rows = 0;
for (const StepEntry& e : plan_.e) if (e.is_prefill) rows += e.n_tokens;
if (cfg_.mixed_step_target_ms <= 0 || plan_.phase != RAD_PHASE_MIXED ||
rows <= 0 || plan_.n_seq_decode <= 0 || ns <= 0) return;
const int64_t target_ns = cfg_.mixed_step_target_ms * 1000000ll;
++m_.mixed_step_target_steps;
if (ns > target_ns) ++m_.mixed_step_target_misses;
const int64_t before = mixed_target_.cap();
mixed_target_.observe(rows, ns, target_ns, std::max<int64_t>(geo_.quantum, 1),
geo_.shared_tok, mixed_target_prefill_continues_);
m_.mixed_step_target_prefill_cap = mixed_target_.cap();
if (mixed_target_.cap() != before && (int)log_level() >= (int)Log::Debug)
RAD_DEBUG("scheduler: mixed-step target observed %.2f ms over %lld prefill token(s); "
"adaptive mixed-prefill cap %lld -> %lld for a %lld ms target",
(double)ns / 1e6, (long long)rows, (long long)before,
(long long)mixed_target_.cap(), (long long)cfg_.mixed_step_target_ms);
}
void Scheduler::note_mixed_step_cost(int64_t ns) {
std::lock_guard<std::mutex> lk(mu_);
mixed_target_observe_locked(ns);
}
uint64_t Scheduler::step_record_count() const {
std::lock_guard<std::mutex> lk(mu_);
return step_ring_.count();
}
size_t Scheduler::step_record_capacity() const {
std::lock_guard<std::mutex> lk(mu_);
return step_ring_.capacity();
}
uint64_t Scheduler::step_records(uint64_t from, std::vector<diag::StepRecord>* out) const {
std::lock_guard<std::mutex> lk(mu_);
return step_ring_.copy_since(from, out);
}
/* ------------------------------------------------------------------ the serial drafter */
/* Which sequences may take a serial draft after the step just run, and what each needs. Called
* while plan() still describes that step, since everything here indexes into it: the head reads
+37 -2
View File
@@ -17,6 +17,7 @@
#include "lookup.h"
#include "metrics.h"
#include "ngram.h"
#include "diag/diagnose.h"
#include <chrono>
#include <condition_variable>
@@ -230,6 +231,17 @@ public:
void take_completed(std::vector<uint64_t>* out);
int reap(uint64_t id);
/* ---------------------------------------------------------- the step record (--diagnose)
*
* One diag::StepRecord per committed step, in a ring sized at init and only with --diagnose: a
* serving process keeps no ring and records nothing. A reader takes every step a phase ran --
* its shape and its time -- without a second clock. Both calls take the lock; `step_records`
* appends what came after `from` and returns how many of those the ring overwrote before they
* were read (diag/diagnose.h). */
uint64_t step_record_count() const;
size_t step_record_capacity() const; /* 0 without --diagnose */
uint64_t step_records(uint64_t from, std::vector<diag::StepRecord>* out) const;
/* ---------------------------------------------------------- the lock, for the engine
*
* Every mutation of the KV manager and the prefix cache happens under this lock; neither has
@@ -374,6 +386,9 @@ public:
* so the draft rounds are charged to the step (see the definition). Without it the counters
* stay correct but there is no timing. */
void step_finished();
/* Feed the open mixed step a completed service time, as step_finished does. Visible for
* deterministic scheduler tests and timed host simulations. */
void note_mixed_step_cost(int64_t ns);
/* ---------------------------------------------------- the serial drafter (spec §10)
*
@@ -514,7 +529,8 @@ public:
* and `shared` says rows are already planned ahead of it. Returns `budget` when
* --busy-prefill-chunk is off, or when auto has nothing to reserve. */
int64_t busy_cap(int64_t n_computed, int64_t n_target, int64_t budget, int64_t reserve,
bool shared = false, bool rows_only = false) const;
bool shared = false, bool rows_only = false,
int64_t mixed_target_shares = 0, int64_t mixed_target_decode_reserve = 0) const;
/* Where a prefill chunk of a `n_target`-token sequence may end. For a sequence with a runtime
* window (IKVManager::set_window) a chunk ends at the prompt, and past it the output a
* recompute replays runs one token a step: the ring rewrites its slots in place, so a
@@ -800,6 +816,13 @@ private:
* throughput window; metrics() decays what it reports by this. */
std::chrono::steady_clock::time_point last_step_at_ = std::chrono::steady_clock::now();
int64_t step_prefill_tok_ = 0, step_decode_tok_ = 0;
/* The step record, picked once at init: with --diagnose the ring is sized and record_step is
* written into it, without it step_finished calls a no-op. One indirect call a step, no test
* of the mode and no ring on the serving path. Callers of step_finished hold mu_. */
void record_step(int64_t ns, int64_t end_ns);
void record_no_step(int64_t, int64_t) {}
void (Scheduler::*record_step_)(int64_t, int64_t) = &Scheduler::record_no_step;
diag::StepRing step_ring_;
/* --prefill-interleave: decode-only steps still owed before the next chunk, and how many
* prefilling requests the last step() held back. step_next_locked stops issuing ahead once
* the hold is spent, or the chunk would wait as long as the decoders keep stepping ahead. */
@@ -812,6 +835,17 @@ private:
static constexpr int64_t kInterleaveAutoMax = 64;
double ema_chunk_s_ = 0.0, ema_decode_s_ = 0.0;
void step_cost_locked(bool chunk, double seconds);
/* --mixed-step-target-ms: learned mixed-step cap and whether this planning pass has a decoder
* whose output latency the cap protects. */
MixedStepTarget mixed_target_;
bool mixed_target_active_ = false;
/* Aggregate prefill rows already admitted to this mixed step. The learned cap is a budget
* for the step, not a per-request allowance. */
int64_t mixed_target_prefill_planned_ = 0;
/* At least one prefill in the issued mixed step has work beyond this chunk. Unlike a true
* prompt tail, a policy or checkpoint-limited chunk is representative feedback. */
bool mixed_target_prefill_continues_ = false;
void mixed_target_observe_locked(int64_t ns);
/* The throughput window (step_finished): each kind's tokens and the wall time the steps
* covered, both decayed with the same time constant, so their ratio is tokens over time. */
double tps_win_prefill_ = 0.0, tps_win_decode_ = 0.0, tps_win_time_ = 0.0;
@@ -875,7 +909,8 @@ private:
/* RAD_OK, or a negative status when the request cannot be admitted now, nor anything behind
* it. `reserve`: tokens of the step to leave for the requests queued behind this one
* (--busy-prefill-chunk; busy_cap); 0 plans the chunk on the whole budget. */
int admit_one(SchedReq& s, int64_t budget, int64_t reserve = 0);
int admit_one(SchedReq& s, int64_t budget, int64_t reserve = 0,
int64_t mixed_target_shares = 0);
/* Positive sentinel from admit_one: the request waits (its context is still being read off
* disk, or a preempted prefill has no room yet) and the walk may ask the one behind it. */
static constexpr int kAdmitWait = 1;