scheduler: --prefill-interleave auto sizes the decode-only steps from measured step times

Cause: while prompts prefill, a sequence that is already decoding gets one token group per
step that carries a chunk. On Qwen3.8-Flash-Next at TP2 such a step takes a 2048-token
chunk's time (~0.4 s, more on the device under load), so decoders crawl at a few tokens/s
until the queue of prompts drains. --prefill-interleave N inserts N decode-only steps after
each chunk, but a fixed N costs the prefill whatever N decode steps cost, which grows with
the number of decoders, and is too few where decode steps are cheap.

Change: --prefill-interleave auto (default stays 0). After a chunk that ran beside decoding
sequences with its prompt continuing, as many decode-only steps follow as fit in a quarter
of a chunk step's time at a decode step's (running means of both, fed by step_finished from
the step wall times; at most 16; 1 until a decode step has been timed; 0 where a decode step
costs more than that quarter). The prefill gives up at most a fifth of its rate however
many sequences decode. The hold mechanics (no admission while held, steps issued ahead
count, the stager not armed for a held chunk) are --prefill-interleave N's, unchanged.
Under auto which steps carry a chunk follows the device's timing; a number keeps a
reproducible schedule. A chunk's cut and every row's computation are unchanged.

Evidence: sched_test interleave_auto_* (the count from given costs, the step shapes it
produces, nothing held for a lone prefill or when decode steps are too dear); config_test
parses auto. Host simulation of the scheduler (next commit), 32 x 128K prompts sent at
once, 512 tokens each, MTP 3, step costs fitted to a lone 2048-token prefill and to decode
alone: decode of the sequences decoding while prompts wait 6.5 -> 31 tokens/s each, last
first token 826 -> 875 s (+6 %), TTFT p50 453 -> 480 s; fixed N=2 gives 18 tokens/s at
the same cost. Device numbers: the commit that makes auto:1 the served default.
This commit is contained in:
Code000 committed 2026-11-05 20:20:13 +01:00
1 parent f713f449c2
commit f87d03d304
7 files changed
+144 -9

No files matched your search

+40 -3
View File
@@ -138,7 +138,14 @@ int Scheduler::init(Program& prog, const Config& cfg, IKVManager* kv, IPrefixCac
"prompt unfinished while other sequences decode, the next %lld steps carry "
"decode rows only and admit nothing", (long long)cfg.prefill_interleave,
(long long)cfg.prefill_interleave);
else if (cfg.prefill_interleave < 0)
RAD_INFO("scheduler: --prefill-interleave auto: after a prefill chunk that leaves its "
"prompt unfinished while other sequences decode, decode-only steps follow for up "
"to a quarter of the chunk step's measured time (at most %lld steps)",
(long long)kInterleaveAutoMax);
interleave_left_ = 0;
ema_chunk_s_ = 0.0;
ema_decode_s_ = 0.0;
held_prefill_ = 0;
draft_refill_ = 0;
if (cfg.n_spec > 0 && prog.drafter.kind == RAD_DRAFT_BLOCK && prog.drafter.window > 0 &&
@@ -1270,6 +1277,33 @@ int64_t Scheduler::busy_cap(int64_t n_computed, int64_t n_target, int64_t budget
return std::min(std::max(cap, Q), budget);
}
/* ------------------------------------------------------------------ decode-only steps */
/* How many decode-only steps follow a chunk that ran beside decoding sequences
* (--prefill-interleave). N as given; under auto, as many as fit in kInterleaveShare of a chunk
* step's measured time at a decode step's, so the prefill gives up at most a fifth of its rate
* (a quarter of a chunk step) however many sequences decode, and the decoders get the steps that
* share buys: several where decode steps are cheap beside the chunk, none where one costs more
* than a quarter of it (they still ride every chunk). One until both costs are measured, which
* takes the first decode-only step. Both are wall times read off step_finished, so under auto
* which steps carry a chunk follows the device's timing and differs from run to run; a number
* schedules the same steps every time. */
void Scheduler::step_cost_locked(bool chunk, double seconds) {
double& ema = chunk ? ema_chunk_s_ : ema_decode_s_;
ema = ema > 0.0 ? ema + kInterleaveEma * (seconds - ema) : seconds;
}
void Scheduler::note_step_cost(bool chunk, double seconds) {
std::lock_guard<std::mutex> lk(mu_);
if (seconds > 0) step_cost_locked(chunk, seconds);
}
int64_t Scheduler::interleave_steps() const {
if (cfg_.prefill_interleave >= 0) return cfg_.prefill_interleave;
if (ema_chunk_s_ <= 0.0 || ema_decode_s_ <= 0.0) return 1;
const double n = kInterleaveShare * ema_chunk_s_ / ema_decode_s_;
return n >= (double)kInterleaveAutoMax ? kInterleaveAutoMax : (int64_t)n;
}
/* ------------------------------------------------------------------ preemption */
bool Scheduler::preempt_back() {
int32_t slot = running_.back();
@@ -1772,7 +1806,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) {
for (int32_t slot : running_) {
const SchedReq& s = *reqs_[(size_t)slot];
if (!s.req || !s.running) continue;
@@ -2223,13 +2257,13 @@ const RadBatch* Scheduler::step() {
case RAD_PHASE_MIXED: ++m_.steps_mixed; break;
default: ++m_.steps_decode; break;
}
if (cfg_.prefill_interleave > 0) {
if (cfg_.prefill_interleave != 0) {
/* Armed only while a prompt continues past this step: after a last chunk nothing is left
* to interleave, and holding would only keep the next request out. */
bool continues = false;
for (const StepEntry& e : plan_.e)
if (e.is_prefill && !e.produces_token) continues = true;
if (any_pf) interleave_left_ = any_decoding && continues ? cfg_.prefill_interleave : 0;
if (any_pf) interleave_left_ = any_decoding && continues ? interleave_steps() : 0;
else if (interleave_left_ > 0) --interleave_left_;
if (held_prefill_ > 0) ++m_.steps_held;
}
@@ -2831,6 +2865,9 @@ void Scheduler::step_finished() {
/* The window ages by all elapsed time, idle included; only the busy part is added as time the
* tokens took. */
const double aged = std::chrono::duration<double>(step_end - last_step_at_).count();
/* --prefill-interleave auto's two step costs (interleave_steps). */
if (wall > 0 && cfg_.prefill_interleave < 0)
step_cost_locked(plan_.phase != RAD_PHASE_DECODE, wall);
if (wall > 0) {
constexpr double kWindowS = 1.0;
const double f = std::exp(-aged / kWindowS);