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

+9 -3
View File
@@ -76,7 +76,8 @@ static const Flag g_flags[] = {
"(default) leaves decode rows and new requests their room,\n"
"off takes the whole step, N holds the chunk to N tokens" },
{ "--prefill-interleave", "N", "decode-only steps after each prefill chunk while other\n"
"sequences decode, at most 64; 0 (default) lets every step\n"
"sequences decode, at most 64; auto fits them in a quarter\n"
"of the chunk step's time; 0 (default) lets every step\n"
"carry a chunk" },
{ "--preempt-wait-ms", "N", "how long a preempted prefill may wait for the whole-\n"
"recompute gate before reclaiming cache blocks and\n"
@@ -571,7 +572,11 @@ int config_parse(int argc, char** argv, Config* c) {
else if (!std::strcmp(v, "off")) c->busy_prefill_chunk = 0;
else if (!parse_int(a.c_str(), v, 1, INT64_MAX, &c->busy_prefill_chunk)) return RAD_E_INVAL;
}
else if (S("--prefill-interleave")) { if (!num(0, 64, &c->prefill_interleave)) return RAD_E_INVAL; }
else if (S("--prefill-interleave")) {
if (!(v = need(i))) return RAD_E_INVAL;
if (!std::strcmp(v, "auto")) c->prefill_interleave = -1;
else if (!parse_int(a.c_str(), v, 0, 64, &c->prefill_interleave)) return RAD_E_INVAL;
}
else if (S("--preempt-wait-ms")) { if (!num(0, INT64_MAX, &c->preempt_wait_ms)) return RAD_E_INVAL; }
else if (S("--max-num-seqs")) { if (!num(1, INT64_MAX, &c->max_seqs)) return RAD_E_INVAL; }
else if (S("--max-model-len")) { if (!num(0, INT64_MAX, &c->max_ctx)) return RAD_E_INVAL; }
@@ -1346,7 +1351,8 @@ const ShowRow kShow[] = {
return c.busy_prefill_chunk < 0 ? std::string("auto")
: c.busy_prefill_chunk == 0 ? std::string("off") : num(c.busy_prefill_chunk); } },
{ "--prefill-interleave", [](const Config& c) {
return c.prefill_interleave > 0 ? num(c.prefill_interleave) : std::string("off"); } },
return c.prefill_interleave > 0 ? num(c.prefill_interleave)
: c.prefill_interleave < 0 ? std::string("auto") : std::string("off"); } },
{ "--preempt-wait-ms", [](const Config& c) { return num(c.preempt_wait_ms); } },
{ "--max-num-seqs", [](const Config& c) { return num(c.max_seqs); } },
{ "--max-model-len", [](const Config& c) {
+2 -1
View File
@@ -839,7 +839,8 @@ struct Config {
* prefill stager arms for. 0 = off: the chunk takes every token the step has left. N > 0: cap
* the chunk at N tokens on such a step. See Scheduler::step, docs/GUIDE.md. */
int64_t busy_prefill_chunk = -1;
int64_t prefill_interleave = 0; /* --prefill-interleave: decode-only steps between chunks */
int64_t prefill_interleave = 0; /* --prefill-interleave: decode-only steps between chunks;
* -1 auto (Scheduler::interleave_steps) */
/* --preempt-wait-ms: how long a preempted prefill may wait for the whole-recompute gate
* before the scheduler evicts cache blocks and preempts a lower-priority running request to
* make room for it. 0 = fall back on the next step; default 5000 ms. */
+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);
+14
View File
@@ -504,6 +504,12 @@ public:
* multi-token chunk past the floor would overwrite keys its own earlier rows still read, while
* one token a step attends as its decode step did. Otherwise `n_target`. */
int64_t chunk_end(const Request& r, int64_t n_target) const;
/* --prefill-interleave: the decode-only steps that follow a chunk run beside decoding
* sequences. N as configured; under auto, from the measured step costs (see the definition).
* Visible for tests, as is note_step_cost, which feeds auto a step's wall time as
* step_finished does: `chunk` for a step that carried a prefill chunk, else a decode step. */
int64_t interleave_steps() const;
void note_step_cost(bool chunk, double seconds);
private:
#ifdef RAD_ENABLE_KV_ROLLBACK_TEST_HOOK
@@ -774,6 +780,14 @@ private:
* the hold is spent, or the chunk would wait as long as the decoders keep stepping ahead. */
int64_t interleave_left_ = 0;
int64_t held_prefill_ = 0;
/* --prefill-interleave auto: the decode-only steps after a chunk (interleave_steps), sized by
* the running means of a step that carried a chunk and of a decode step, in seconds, fed by
* step_finished. */
static constexpr double kInterleaveShare = 0.25;
static constexpr double kInterleaveEma = 0.25;
static constexpr int64_t kInterleaveAutoMax = 16;
double ema_chunk_s_ = 0.0, ema_decode_s_ = 0.0;
void step_cost_locked(bool chunk, double seconds);
/* 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;
+13 -1
View File
@@ -1155,6 +1155,18 @@ decoders keep their cadence. A lone prefill is not held, nor is the step after a
chunk. `0` (the default) schedules as before. `/stats` counts the decode-only steps the hold
inserted as `steps_held`, `/metrics` as `radiance:engine_steps_held_total`.
`--prefill-interleave auto` sizes the hold from measured step times instead: after each such chunk,
as many decode-only steps as fit in a quarter of a chunk step's time at a decode step's (running
means of both, at most 16 steps; one until a decode step has been timed). The prefill gives up at
most a fifth of its rate however many sequences decode, and the decoders get several steps where a
decode step is cheap beside a chunk, none where one costs more than that quarter (they still ride
every chunk). In the scheduler's host simulation of 32 prompts of 128K tokens sent at once, each
then decoding 512 tokens (2048-token chunks, MTP 3, step costs fitted to a lone prefill and to
decode alone), auto raised the decode rate of the sequences decoding while prompts still waited
from 6.5 to 31 tokens/s each and delayed the last first token by 6 %. Because the hold follows the
device's timing, which steps carry a chunk differs from run to run; a number schedules the same
steps every time.
**A prefill alone in its step can carry several intervals** (`--prefill-span N`, default 1). A
chunk ends on every checkpoint interval, so a prompt is otherwise prefilled one interval a step.
With
@@ -2070,7 +2082,7 @@ by 0.002-0.003 nats.
| flag | default | |
|---|---|---|
| `--max-num-batched-tokens N` | 2048 | largest token count one step may carry |
| `--prefill-interleave N` | 0 (off) | decode-only steps after each prefill chunk while other sequences decode (§4.5), at most 64 |
| `--prefill-interleave N` | 0 (off) | decode-only steps after each prefill chunk while other sequences decode (§4.5), at most 64; `auto` fits them in a quarter of the chunk step's measured time |
| `--preempt-wait-ms N` | 5000 | how long a preempted prefill may wait for its whole-recompute gate before reclaiming cache blocks and preempting lowest-priority running requests until one overdue request can prefill; `0` falls back on the next step. A preempted request publishes what it computed to the prefix cache, as a finished one does, so its recompute adopts its prompt instead of computing it again; under pressure the cache gives those blocks back, the newest last, before another request is preempted |
| `--busy-prefill-chunk MODE` | auto | a prefill chunk's share of a step that has other work: `auto` leaves decode rows and waiting requests their room (§4.5), `off` takes the whole step, `N` holds it to N tokens |
| `--max-num-seqs N` | 256 | largest number of sequences one step may carry |
+5
View File
@@ -460,6 +460,11 @@ TEST(prefill_interleave_takes_a_step_count_up_to_64) {
CHECK_EQ(c.prefill_interleave, 4);
CHECK_EQ(parse({ "--prefill-interleave", "65" }, &c), RAD_E_INVAL);
CHECK_EQ(parse({ "--prefill-interleave", "-1" }, &c), RAD_E_INVAL);
/* auto sizes the hold from measured step costs (Scheduler::interleave_steps) */
Config a;
CHECK_EQ(parse({ "--prefill-interleave", "auto" }, &a), RAD_OK);
CHECK_EQ(a.prefill_interleave, -1);
CHECK_EQ(parse({ "--prefill-interleave", "automatic" }, &c), RAD_E_INVAL);
}
/* ================================================================== the table, the parser and the help agree
+61 -1
View File
@@ -3194,12 +3194,17 @@ struct Interleaved {
int64_t steps_held = 0;
};
static Interleaved interleave_run(int64_t n, bool decoder, int steps) {
/* `chunk_s` / `decode_s`: step costs fed to --prefill-interleave auto before the run (none when
* 0), standing in for step_finished's wall times. */
static Interleaved interleave_run(int64_t n, bool decoder, int steps, double chunk_s = 0,
double decode_s = 0) {
Fixture f;
f.build();
f.cfg.prefill_interleave = n;
CHECK_OK(f.start());
f.sch.set_stage_rows(64);
if (chunk_s > 0) f.sch.note_step_cost(true, chunk_s);
if (decode_s > 0) f.sch.note_step_cost(false, decode_s);
if (decoder) {
f.submit(100, 512);
CHECK(f.sch.step() != nullptr);
@@ -3259,6 +3264,61 @@ TEST(interleave_leaves_a_lone_prefill_alone) {
CHECK_EQ(r.steps_held, 0);
}
/* auto: as many decode-only steps as fit in a quarter of a chunk step's measured time at a decode
* step's (rounded down, at most 16), one until both are measured, none where a decode step costs
* more than that quarter. */
TEST(interleave_auto_fits_decode_steps_in_a_quarter_of_a_chunk_step) {
auto steps = [](double chunk_s, double decode_s) {
Fixture f;
f.build();
f.cfg.prefill_interleave = -1;
CHECK_OK(f.start());
if (chunk_s > 0) f.sch.note_step_cost(true, chunk_s);
if (decode_s > 0) f.sch.note_step_cost(false, decode_s);
return f.sch.interleave_steps();
};
CHECK_EQ(steps(0, 0), 1);
CHECK_EQ(steps(0.4, 0), 1);
CHECK_EQ(steps(0, 0.03), 1);
CHECK_EQ(steps(0.4, 0.03), 3); /* 0.1 s of 0.03 s steps */
CHECK_EQ(steps(0.4, 0.05), 2);
CHECK_EQ(steps(0.4, 0.2), 0); /* a decode step dearer than the quarter */
CHECK_EQ(steps(1.0, 0.001), 16);
/* A number is used as given, whatever was measured. */
Fixture g;
g.build();
g.cfg.prefill_interleave = 5;
CHECK_OK(g.start());
g.sch.note_step_cost(true, 1.0);
g.sch.note_step_cost(false, 0.001);
CHECK_EQ(g.sch.interleave_steps(), 5);
/* The costs are running means: a chunk step of 0.4 s, then one of 0.8 s, reads 0.5 s. */
Fixture h;
h.build();
h.cfg.prefill_interleave = -1;
CHECK_OK(h.start());
h.sch.note_step_cost(true, 0.4);
h.sch.note_step_cost(true, 0.8);
h.sch.note_step_cost(false, 0.05);
CHECK_EQ(h.sch.interleave_steps(), 2); /* 0.125 / 0.05 */
}
/* auto schedules the measured count after every chunk beside a decoder, holds nothing for a lone
* prefill, and with decode steps too dear for the quarter puts a chunk in every step. */
TEST(interleave_auto_holds_each_chunk_for_the_steps_its_costs_allow) {
const Interleaved r = interleave_run(-1, /*decoder=*/true, 10, 0.4, 0.05);
CHECK_EQ(r.shape, std::string("CDDCDDCDDC"));
CHECK_EQ(r.steps_held, 6);
const Interleaved first = interleave_run(-1, /*decoder=*/true, 6); /* nothing measured */
CHECK_EQ(first.shape, std::string("CDCDCD"));
const Interleaved dear = interleave_run(-1, /*decoder=*/true, 8, 0.4, 0.2);
CHECK_EQ(dear.shape, std::string("CCCCCCCC"));
CHECK_EQ(dear.steps_held, 0);
const Interleaved lone = interleave_run(-1, /*decoder=*/false, 8, 0.4, 0.01);
CHECK_EQ(lone.shape, std::string("CCCCCCCC"));
CHECK_EQ(lone.steps_held, 0);
}
/* After a prompt's last chunk there is nothing to interleave: the next request is admitted in the
* next step. */
TEST(interleave_does_not_hold_after_a_prompts_last_chunk) {