sched: a bound on the preempted-prefill gate's wait

This commit is contained in:
baKon authored and StillDeadcode committed 2026-10-30 10:21:57 +01:00
1 parent 0792d86375
commit 33108e488b
16 files changed
+1522 -150

No files matched your search

+199 -43
View File
@@ -11,6 +11,7 @@
#include <cstdio>
#include <cstdlib>
#include <numeric>
#include <string>
namespace rad {
@@ -164,17 +165,47 @@ int Scheduler::init(Program& prog, const Config& cfg, IKVManager* kv, IPrefixCac
* A block drafter proposes its block in one pass, but each verified draft is still a verify
* row, which is not free on a MoE that reads each row's routed experts. So it takes the same
* policy with fixed rounds: each sequence verifies the first min(D, its stop) of the block.
* Prompt lookup runs no pass and keeps the fixed window; the outcome is logged either way. */
* Prompt lookup as the only drafter runs no pass either, and takes the policy the same way:
* its drafts feed each sequence's estimate and its proposal is cut to the sequence's window.
* As a fallback beside a head it keeps the step's window and stays out of the head's
* estimate. The outcome is logged either way. */
lookup_adapt_ = false;
/* The verify-path harness (scheduler.h): --debug-draft-oracle names a file of token ids,
* whitespace or comma separated, a plain greedy run's continuation of the one prompt the test
* sends; kDebugMiss of the drafts are corrupted. The text must stay the plain run's whatever is
* accepted. */
debug_oracle_.clear();
if (const char* f = cfg.debug_draft_oracle.c_str(); *f && cfg.n_spec > 0) {
if (FILE* fp = std::fopen(f, "r")) {
std::string text;
char buf[4096];
for (size_t k; (k = std::fread(buf, 1, sizeof buf, fp)) > 0;) text.append(buf, k);
std::fclose(fp);
for (char& ch : text)
if ((ch < '0' || ch > '9') && ch != '-') ch = ' ';
const char* q = text.c_str();
for (char* e = nullptr;; q = e) {
const long long v = std::strtoll(q, &e, 10);
if (e == q) break;
if (v >= 0 && v < prog.meta.n_vocab) debug_oracle_.push_back((int32_t)v);
}
}
RAD_WARN("--debug-draft-oracle: prompt lookup proposes from %zu fixed ids in %s, "
"%.0f%% of drafts corrupted -- a verify-path test, not a drafter",
debug_oracle_.size(), f, 100.0 * kDebugMiss);
}
if (cfg.draft_window == DraftWindow::Adaptive) {
const RadDrafterDecl& dd = prog.drafter;
if (cfg.n_spec <= 0)
RAD_WARN("--draft-window adaptive: this deployment does not speculate "
"(--num-speculative-tokens %d), so there is no window to adapt", cfg.n_spec);
else if ((dd.kind != RAD_DRAFT_SERIAL && dd.kind != RAD_DRAFT_BLOCK) || dd.depth <= 0)
RAD_WARN("--draft-window adaptive: the window is fixed at %d with prompt lookup only "
"-- the adaptive window prices draft passes and verify rows, and prompt "
"lookup runs no pass", cfg.n_spec);
else {
else if ((dd.kind != RAD_DRAFT_SERIAL && dd.kind != RAD_DRAFT_BLOCK) || dd.depth <= 0) {
dp.adapt = true;
lookup_adapt_ = true;
RAD_INFO("--draft-window adaptive with prompt lookup only: each sequence verifies its "
"lookup's first min(D, its stop) drafts, %d at most, priced like a head's",
cfg.n_spec);
} else {
dp.adapt = true;
if (dd.kind == RAD_DRAFT_BLOCK)
RAD_INFO("--draft-window adaptive with a block drafter: the block of %d is "
@@ -183,6 +214,20 @@ int Scheduler::init(Program& prog, const Config& cfg, IKVManager* kv, IPrefixCac
}
}
RAD_TRY(draft_.configure(dp));
/* Prompt lookup as the deployment's only drafter verifies a proposal only when it pays
* (lookup.h). Not configurable: the one deployment property it needs, the cost of a verify
* row, is measured. */
{
const RadDrafterDecl& dd = prog.drafter;
const bool head =
(dd.kind == RAD_DRAFT_SERIAL || dd.kind == RAD_DRAFT_BLOCK) && dd.depth > 0;
lookup_gate_on_ = cfg.n_spec > 0 && !head;
lookup_.configure(lookup_gate_on_ ? cfg.n_spec : 0);
if (lookup_gate_on_)
RAD_INFO("prompt lookup: a proposal is verified when its sequence's record says it "
"pays for its verify rows at the measured step cost; one held back is still "
"scored against the text that follows");
}
draft_est_.clear();
draft_est_.reserve((size_t)cfg.max_seqs);
@@ -263,17 +308,6 @@ int Scheduler::add_locked(std::unique_ptr<Request> r) {
/* An embedding input runs in one step or not at all (Request::embed); one longer than a step
* could never be admitted. */
if (r->embed && (int64_t)r->prompt.size() > cfg_.max_tok) return RAD_E_INVAL;
/* Every prompt id, tokenised or client-supplied, names a real vocabulary row: an id past the
* embedding table is refused at admission, never staged into a pass. Raw id arrays reach here
* straight from the request body, and an op failing mid-step is a device fault (spec §17) —
* the whole engine would die for one bad number. Same bound as the scored-prompt check below.
*/
for (int32_t t : r->prompt)
if (t < 0 || t >= prog_->meta.n_vocab) {
RAD_WARN("request %llu refused: prompt token id %d is outside the vocabulary [0, %lld)",
(unsigned long long)r->id, t, (long long)prog_->meta.n_vocab);
return RAD_E_INVAL;
}
/* Nor could a prompt the KV pool cannot hold even alone: admission takes only what the pool
* holds, so it would wait at the head of the queue, and everything behind it, forever. The
* prompt is the least a request needs: its first token comes from the prefill's last row.
@@ -314,6 +348,7 @@ int Scheduler::add_locked(std::unique_ptr<Request> r) {
s.arrival = ++arrival_;
s.arrived_us = mono_us();
s.first_tok_us = 0;
s.preempted_us = 0;
s.slot = slot;
s.card = -1;
s.running = false;
@@ -321,6 +356,7 @@ int Scheduler::add_locked(std::unique_ptr<Request> r) {
/* A new request's text, so a new estimate. */
s.adapt.depth = 0;
s.adapt.held = 0;
s.look.started = false;
s.sched_tokens = 0;
s.entry = -1;
s.entry_next = -1;
@@ -555,6 +591,16 @@ void Scheduler::close_request(SchedReq& s, ReqState st, const char* reason, bool
}
if (cfg_.prefix_cache && pc_) pc_->release(*s.req);
/* Prompt lookup's index is this text's alone. */
s.ngram_ix.reset();
if (lookup_gate_on_ && s.look.started && s.look.proposed > 0)
RAD_DEBUG("prompt lookup: request %llu made %lld proposals, held back %lld (%lld drafts, "
"%lld of those scored came true); a %d-draft verify costs %.2f plain steps",
(unsigned long long)s.req->id, (long long)s.look.proposed,
(long long)s.look.held, (long long)s.look.held_drafts,
(long long)s.look.held_hits, lookup_.max_depth(),
lookup_.step_cost(1 + lookup_.max_depth()) / lookup_.step_cost(1));
s.look.started = false;
/* Fold what the draft head did on this text into the next text's starting estimate, once:
* marking it unstarted makes a second close add nothing. */
if (draft_.adaptive()) {
@@ -846,28 +892,40 @@ int Scheduler::requests_locked(ReqStat* out, int max) const {
*
* Longest pattern and most recent occurrence first: a longer match predicts better, and falling
* back to shorter ones only on a miss keeps common bigrams from dragging acceptance down. A
* declared draft head calls set_draft() before step(), so this only fills an empty proposal. */
int Scheduler::draft_ngram(SchedReq& s, int depth) const {
* declared draft head calls set_draft() before step(), so this only fills an empty proposal.
*
* The occurrence comes from the sequence's own index (ngram.h): O(pattern) a lookup, plus the
* n-grams the last commit added, rather than a scan of the context. */
int Scheduler::draft_ngram(SchedReq& s, int depth, int* match) const {
if (match) *match = -1;
if (depth <= 0 || !s.req) return 0;
const Request& r = *s.req;
const int64_t np = (int64_t)r.prompt.size(), no = (int64_t)r.output.size();
const int64_t n = np + no;
if (n < 2) return 0;
auto at = [&](int64_t i) -> int32_t {
return i < np ? r.prompt[(size_t)i] : r.output[(size_t)(i - np)];
};
for (int k = kNgramMax; k >= kNgramMin; --k) {
if (n < (int64_t)k + 1) continue;
for (int64_t j = n - k - 1; j >= 0; --j) {
bool hit = true;
for (int t = 0; t < k && hit; ++t) hit = (at(j + t) == at(n - k + t));
if (!hit) continue;
int d = 0;
while (d < depth && j + k + d < n) { s.draft[d] = at(j + k + d); ++d; }
return d;
TokenSeq seq;
seq.a = r.prompt.data();
seq.na = (int64_t)r.prompt.size();
seq.b = r.output.data();
seq.nb = (int64_t)r.output.size();
if (seq.size() < 2) return 0;
if (depth > RAD_SCHED_MAX_SPEC) depth = RAD_SCHED_MAX_SPEC;
if (!debug_oracle_.empty()) {
/* The harness: ids n.. of the plain run's continuation, each replaced by its neighbour id
* where a hash of (n, position) falls under the miss rate. */
const int64_t n = seq.nb;
int d = 0;
for (; d < depth && n + d < (int64_t)debug_oracle_.size(); ++d) {
uint64_t h = (uint64_t)(n * RAD_SCHED_MAX_SPEC + d + 1) * 0x9E3779B97F4A7C15ull;
h ^= h >> 31;
h *= 0xbf58476d1ce4e5b9ull;
h ^= h >> 29;
const bool miss = (double)(h >> 11) * 0x1.0p-53 < kDebugMiss;
const int32_t t = debug_oracle_[(size_t)(n + d)];
s.draft[d] = miss ? (int32_t)((t + 1) % (int32_t)prog_->meta.n_vocab) : t;
}
return d;
}
return 0;
if (s.ngram_ix.owner() != r.id) s.ngram_ix.reset(r.id);
return s.ngram_ix.propose(seq, cfg_.spec_ngram_min, cfg_.spec_ngram_max, depth, s.draft, match,
LookupGate::kLongMatch);
}
/* ------------------------------------------------------------------ chunking */
@@ -1145,6 +1203,7 @@ void Scheduler::preempt_back() {
r.n_cached = 0;
r.n_accepted = 0;
r.state = ReqState::Preempted;
s.preempted_us = mono_us();
s.drafter_reset();
/* Also clear the request's own window. The sampler stages 1 + Request::n_draft rows
* (Sampler::build_step), and a drafter may have set it for the step this preemption removed;
@@ -1419,6 +1478,7 @@ int Scheduler::admit_one(SchedReq& s, int64_t budget, int64_t reserve) {
if (s.card < 0) { s.card = free_cards_.back(); free_cards_.pop_back(); }
r.state = ReqState::Running;
s.preempted_us = 0;
s.running = true;
server::sink_admitted(r.sink);
server::sink_prompt_progress(r.sink, r.n_computed, (int32_t)r.prompt.size(), r.n_cached);
@@ -1475,6 +1535,15 @@ bool Scheduler::preempted_can_prefill(const SchedReq& s) const {
return true;
}
bool Scheduler::preempt_wait_overdue(const SchedReq& s) const {
if (!s.req || s.req->state != ReqState::Preempted) return false;
/* 0 means fall back on the next step; a timed request must have been preempted before. */
if (s.preempted_us <= 0) return false;
if (cfg_.preempt_wait_ms == 0) return true;
const int64_t waited_ms = (mono_us() - s.preempted_us) / 1000;
return waited_ms >= cfg_.preempt_wait_ms;
}
/* ------------------------------------------------------------------ the step */
const RadBatch* Scheduler::step() {
std::lock_guard<std::mutex> lk(mu_);
@@ -1494,6 +1563,45 @@ const RadBatch* Scheduler::step() {
step_prefill_tok_ = 0;
step_decode_tok_ = 0;
/* 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
* still cannot fit after one victim must not leave the rest of the queue waiting forever.
* Reclaim cache blocks once, then preempt the lowest-priority running request until one
* overdue request can prefill or nothing is left running. The admission pass below sees the
* reclaimed blocks; nothing is planned for a victim this step. */
bool overdue = false;
for (int32_t slot : waiting_) {
const SchedReq& s = *reqs_[(size_t)slot];
if (s.req && s.req->state == ReqState::Preempted && preempt_wait_overdue(s)) {
overdue = true;
break;
}
}
if (overdue) {
bool evicted = false;
while (!running_.empty()) {
SchedReq* fit = nullptr;
for (int32_t slot : waiting_) {
SchedReq& s = *reqs_[(size_t)slot];
if (!s.req || s.req->state != ReqState::Preempted || !preempt_wait_overdue(s))
continue;
if (preempted_can_prefill(s)) { fit = &s; break; }
}
if (fit) break;
if (!evicted && pc_ && cfg_.prefix_cache) {
(void)pc_->evict(kEvictBlocks);
evicted = true;
continue;
}
const SchedReq& victim = *reqs_[(size_t)running_.back()];
RAD_DEBUG("step: preempted prefill overdue; preempting the lowest-priority running "
"request %llu",
victim.req ? (unsigned long long)victim.req->id : 0ull);
++m_.preempt_forced;
preempt_back();
}
}
int64_t budget = cfg_.max_tok;
bool preempted_here = false;
/* --prefill-interleave: after a step that carried a chunk beside decoding sequences, with the
@@ -1578,6 +1686,19 @@ const RadBatch* Scheduler::step() {
int32_t capped_at = -1;
int64_t capped_full = 0;
/* The verify gate's view of the step: every decoding sequence's own row, before any drafts. */
if (lookup_gate_on_) {
gate_rows_ = 0;
for (int32_t slot : running_) {
const SchedReq& g = *reqs_[(size_t)slot];
if (!g.req) continue;
const Request& gr = *g.req;
if (gr.n_computed >= (int64_t)gr.prompt.size() && gr.n_computed + 1 >= g.seq_len())
++gate_rows_;
}
gate_tokens_ = (double)gate_rows_;
}
/* ---------------------------------------------------------- running requests first */
/* Continuous batching: requests already holding blocks are served before new ones are
* admitted, so new work never preempts work half done. */
@@ -1630,8 +1751,29 @@ const RadBatch* Scheduler::step() {
bool ngram = false;
if (!r.may_speculate) s.n_draft = 0;
else if (s.n_draft == 0) {
s.n_draft = (int32_t)draft_ngram(s, draft_.step_window());
/* As the only drafter under the adaptive window, the lookup is the head: its
* proposal is cut to the sequence's own window. */
int match = -1;
s.n_draft = (int32_t)draft_ngram(s, lookup_adapt_ ? cap : draft_.step_window(),
&match);
ngram = s.n_draft > 0;
/* The verify gate (lookup.h). Every proposal is offered, so a held one is still
* scored against the text; the --debug-draft-oracle harness (match < 0) is a
* verify-path test and always verified. */
if (ngram && lookup_gate_on_ && match >= 0) {
const int64_t pos = (int64_t)r.prompt.size() + (int64_t)r.output.size();
lookup_.offer(s.look, pos, s.draft, s.n_draft, match);
const double gain =
lookup_.expect(s.look, LookupGate::class_of(match), s.n_draft);
if (lookup_.pays(gain, s.n_draft, gate_rows_, gate_tokens_)) {
gate_rows_ += s.n_draft;
gate_tokens_ += gain;
} else {
lookup_.held(s.look, s.n_draft);
s.n_draft = 0;
ngram = false;
}
}
}
if (!ngram && s.n_draft > cap) s.n_draft = cap;
s.draft_ngram = ngram;
@@ -2103,12 +2245,6 @@ const RadBatch* Scheduler::step_next_locked() {
* that cannot rewind decides at commit whether the next step may speculate; one that can,
* or no grammar, keeps its answer. */
if (!e.produces_token || !r.speculation_fixed) return nullptr;
/* A SCORED row (Request::token_scores, a /v1/systemone question) that the step in flight
* brings to max_tokens gets no step issued ahead: it would run a whole forward pass for a
* request that finishes at the commit, and max_tokens=1 pays a decode step after its
* prefill. Scored rows only -- every other decode keeps the step of overlap it had. */
if (r.token_scores && r.max_tokens > 0 &&
(int64_t)r.output.size() + 1 >= r.max_tokens) return nullptr;
/* The furthest context the row can reach: every position this step verifies committed,
* counted from the last commit rather than this step's own bound (which would add a
@@ -2352,6 +2488,15 @@ int Scheduler::commit(StepResult& res) {
}
if (res.token) emit(res.token[i], a);
r.n_computed = valid;
/* The gate's pending proposal against what this step emitted (lookup.h). */
if (lookup_gate_on_ && s.look.started) {
TokenSeq ts;
ts.a = r.prompt.data();
ts.na = (int64_t)r.prompt.size();
ts.b = r.output.data();
ts.nb = (int64_t)r.output.size();
lookup_.advance(s.look, ts);
}
step_decode_tok_ += 1 + a;
if (e.n_spec > 0) {
@@ -2359,7 +2504,7 @@ int Scheduler::commit(StepResult& res) {
/* And this sequence's own estimate under the adaptive window, only when the head
* drafted: a prompt-lookup proposal is accepted on different text by a different
* rule. A move takes effect at the next draft; this step's drafter already ran. */
if (draft_.adaptive() && !e.ngram) {
if (draft_.adaptive() && (!e.ngram || lookup_adapt_)) {
const int was = s.adapt.depth;
const int now = draft_.adapt_observe(s.adapt, e.n_spec, a);
if (now != was && was > 0 && (int)log_level() >= (int)Log::Debug) {
@@ -2496,6 +2641,13 @@ void Scheduler::step_finished() {
if (m_.prefill_tps < 0.05f) m_.prefill_tps = 0.0f;
if (m_.decode_tps < 0.05f) m_.decode_tps = 0.0f;
}
/* The step's cost by query rows, for the prompt-lookup gate (lookup.h): every pure decode
* step, drafting or not, since the plain step is what a verify is weighed against. */
if (lookup_gate_on_ && plan_.phase == RAD_PHASE_DECODE && wall > 0) {
int64_t rows = 0;
for (const StepEntry& e : plan_.e) rows += e.is_prefill ? 0 : e.n_tokens;
if (rows > 0) lookup_.step_time(rows, wall * 1e3);
}
/* The step's cost at its depth, for the adaptive window (draft.h): the same wall time,
* including the draft rounds after it, with a step issued ahead counted once. Decode steps
* only. Keyed by the deepest window the step verified, which is the head depth it ran at
@@ -3130,6 +3282,10 @@ SchedMetrics Scheduler::metrics_locked() const {
m.draft_pos_reached [j] = draft_.reached(j);
m.draft_pos_accepted[j] = draft_.hits(j);
}
m.lookup_proposals = lookup_.proposed();
m.lookup_held = lookup_.held_count();
m.lookup_held_drafts = lookup_.held_drafts();
m.lookup_held_hits = lookup_.held_hits();
if (draft_.adaptive()) {
m.draft_adaptive = true;
/* Over the running sequences whose estimate has started; one that has not drafted yet