draft: cap every adaptive window under load (RADIANCE_DRAFT_ADAPT_BUSY)

The cost table prices a step at one sequence. Under load every row a step
adds is expert bandwidth its neighbours wait on, and the adaptive window
lost 4 % at 8 concurrent requests. With RADIANCE_DRAFT_ADAPT_BUSY=N and
more than N sequences running, every window is capped at
RADIANCE_DRAFT_ADAPT_BUSY_DEPTH (default 3); the estimates keep learning.
The count is taken once a cycle and held to its end, so the plan, the
rounds, expect_draft and set_draft see one cap. Unset, nothing changes.
This commit is contained in:
keptn42 authored and StillDeadcode committed 2026-10-07 11:10:19 +02:00
1 parent 82da8eab69
commit 76727fa050
5 files changed
+215 -14

No files matched your search

+13 -4
View File
@@ -103,6 +103,8 @@ void DraftController::from_env(Params* p) {
env_real("RADIANCE_DRAFT_ADAPT_HALFLIFE", 0.5, 1e6, &e.half_life);
env_real("RADIANCE_DRAFT_ADAPT_PRIOR", 0.0, 1.0, &e.prior);
env_int ("RADIANCE_DRAFT_ADAPT_MIN", 1, RAD_SCHED_MAX_SPEC, &e.min_depth);
env_int ("RADIANCE_DRAFT_ADAPT_BUSY", 0, 1000000, &e.busy);
env_int ("RADIANCE_DRAFT_ADAPT_BUSY_DEPTH", 1, RAD_SCHED_MAX_SPEC, &e.busy_depth);
return e;
}();
p->adapt = env.adapt;
@@ -112,6 +114,8 @@ void DraftController::from_env(Params* p) {
p->half_life = env.half_life;
p->prior = env.prior;
p->min_depth = env.min_depth;
p->busy = env.busy;
p->busy_depth = env.busy_depth;
}
int DraftController::configure(const Params& p) {
@@ -121,10 +125,12 @@ int DraftController::configure(const Params& p) {
return RAD_E_INVAL;
}
if (p.adapt && (!(p.hyst >= 0.0f) || p.hold < 0 || !(p.half_life > 0.0f) ||
!(p.prior >= 0.0f && p.prior <= 1.0f) || p.min_depth < 1)) {
RAD_ERR("adaptive draft depth: hysteresis %g, hold %d, half-life %g, prior %g, minimum %d "
"-- they want >= 0, >= 0, > 0, 0..1 and >= 1", (double)p.hyst, p.hold,
(double)p.half_life, (double)p.prior, p.min_depth);
!(p.prior >= 0.0f && p.prior <= 1.0f) || p.min_depth < 1 || p.busy < 0 ||
p.busy_depth < 1)) {
RAD_ERR("adaptive draft depth: hysteresis %g, hold %d, half-life %g, prior %g, minimum %d, "
"busy %d, busy depth %d -- they want >= 0, >= 0, > 0, 0..1, >= 1, >= 0 and >= 1",
(double)p.hyst, p.hold, (double)p.half_life, (double)p.prior, p.min_depth, p.busy,
p.busy_depth);
return RAD_E_INVAL;
}
p_ = p;
@@ -180,6 +186,9 @@ int DraftController::configure(const Params& p) {
"acceptance half-life %.1f verifies, prior %.2f",
p_.min_depth, p_.max_depth, t.c_str(), table ? "" : " (the default model)",
100.0 * (double)p_.hyst, p_.hold, (double)p_.half_life, (double)p_.prior);
if (p_.busy > 0)
RAD_INFO("draft depth: above %d running sequences every window is capped at %d "
"(RADIANCE_DRAFT_ADAPT_BUSY)", p_.busy, p_.busy_depth);
return RAD_OK;
}
+13 -1
View File
@@ -82,13 +82,20 @@ public:
float half_life = 16.0f; /* of each position's estimate, in verifies that evaluate it */
float prior = 0.8f; /* for a position too few verifies have evaluated to say */
int min_depth = 1; /* the policy never drafts fewer */
/* THE LOAD CAP. The cost table is a step's cost at ONE sequence, and a deeper window costs
* more under load than it says -- every row a step adds is expert bandwidth all its
* neighbours wait on. So with more than `busy` sequences running, every window is capped
* at `busy_depth`; the estimates go on learning underneath and the windows come back when
* the load does. 0 is no cap. */
int busy = 0;
int busy_depth = 3;
};
int configure(const Params& p);
const Params& params() const { return p_; }
/* RADIANCE_DRAFT_ADAPT and its companions (RADIANCE_DRAFT_COST, _HYST, _HOLD, _HALFLIFE,
* _PRIOR, _MIN), parsed ONCE per process and copied into the adaptive fields of `p`;
* _PRIOR, _MIN, _BUSY, _BUSY_DEPTH), parsed ONCE per process and copied into the adaptive fields of `p`;
* max_depth is left alone. Once, so that every step of a run is decided by the same rule --
* a pass recorded under one setting and played under another is a pass the audit fails. */
static void from_env(Params* p);
@@ -105,6 +112,11 @@ public:
/* One verify of this sequence: `drafted` proposed, a prefix of `accepted` taken. Moves its
* estimate, and its depth when that clears the hysteresis. Returns the depth after. */
int adapt_observe(DraftAdapt& s, int drafted, int accepted) const;
/* The load cap for a cycle with `n_running` sequences running: busy_depth past the threshold,
* 0 for none. The scheduler asks once a cycle and holds the answer (Scheduler::draft_cycle). */
int busy_cap(int n_running) const {
return (adapt_ && p_.busy > 0 && n_running > p_.busy) ? p_.busy_depth : 0;
}
/* What the policy maximises, E(d) / T(d), for `s`'s estimate as it stands. For tests and logs. */
double gain(const DraftAdapt& s, int d) const;
/* The cost table as configured, T(1..max_depth). */
+25 -5
View File
@@ -100,6 +100,7 @@ int Scheduler::init(Program& prog, const Config& cfg, IKVManager* kv, IPrefixCac
/* The adaptive window's switch and knobs, read once per process (draft.h). Off unless set. */
DraftController::from_env(&dp);
RAD_TRY(draft_.configure(dp));
draft_busy_cap_ = 0;
RAD_TRY(builder_.init(prog, cfg, geo_, kvgeom_, ranks));
/* The builder's bound and not a second derivation of it: it is what the block tables were
@@ -936,6 +937,8 @@ void Scheduler::undo_hit(SchedReq& s) {
/* ------------------------------------------------------------------ the step */
const RadBatch* Scheduler::step() {
std::lock_guard<std::mutex> lk(mu_);
/* A cycle opens here, so its load cap is counted here (scheduler.h, draft_busy_cap_). */
draft_cycle();
/* The drafter's snapshots describe the step before this one and nothing reads them from here
* on. block_end() and serial_end() clear them already; this covers a drafter that stopped
@@ -996,7 +999,7 @@ const RadBatch* Scheduler::step() {
* AND THE CAP IS THE SEQUENCE'S OWN WINDOW, which is `depth` unless the adaptive
* policy is on -- then it is what this sequence's acceptance pays for, and the n-gram
* fallback is held to it like any other proposal. */
const int cap = draft_.adaptive() ? draft_.depth_for(s.adapt) : depth;
const int cap = draft_.adaptive() ? draft_cap(s) : depth;
if (!r.may_speculate) s.n_draft = 0;
else if (s.n_draft == 0) s.n_draft = (int32_t)draft_ngram(s, cap);
if (s.n_draft > cap) s.n_draft = cap;
@@ -1352,6 +1355,15 @@ void Scheduler::take_encodes(std::vector<std::shared_ptr<mm::Item>>* out) {
out->swap(enc_want_);
}
/* ------------------------------------------------------------------ the load cap */
void Scheduler::draft_cycle() {
const int32_t cap = (int32_t)draft_.busy_cap((int)running_.size());
if (cap != draft_busy_cap_)
RAD_DEBUG("draft window: %zu running, %s", running_.size(),
cap > 0 ? "every window capped at the busy depth" : "the cap is lifted");
draft_busy_cap_ = cap;
}
/* ------------------------------------------------------------------ the next step */
void Scheduler::expect_draft(uint64_t id, int n) {
std::lock_guard<std::mutex> lk(mu_);
@@ -1360,9 +1372,9 @@ void Scheduler::expect_draft(uint64_t id, int n) {
SchedReq& s = *reqs_[(size_t)it->second];
/* UNDER THE ADAPTIVE WINDOW THE ROUNDS RAN FOR THE DEEPEST SEQUENCE, and this one verifies the
* first of them up to its own window -- the cap serial_begin sized the rounds from, unchanged
* since, because nothing commits between the two. */
* since: nothing commits between the two, and the load cap is the cycle's (draft_busy_cap_). */
if (draft_.adaptive() && n > 0) {
const int cap = draft_.depth_for(s.adapt);
const int cap = draft_cap(s);
if (n > cap) n = cap;
}
s.draft_next = n > 0 ? n : 0;
@@ -1449,6 +1461,10 @@ const RadBatch* Scheduler::step_next_locked() {
void Scheduler::promote_next() {
std::lock_guard<std::mutex> lk(mu_);
if (!next_open_) return;
/* The other place a cycle opens: the step issued ahead becomes this one, and step() is not
* called. Its proposals were counted under the last cycle's cap; the next ones are drafted
* under this one. */
draft_cycle();
std::swap(plan_, plan_next_);
plan_next_.reset();
step_prefill_tok_ = 0;
@@ -1798,7 +1814,7 @@ int Scheduler::serial_begin(int* rounds, SerialRow* out, int max) {
if (!s.req || s.req->state != ReqState::Running || !s.running ||
!s.req->may_speculate)
continue;
const int c = draft_.depth_for(s.adapt);
const int c = draft_cap(s);
if (c > want) want = c;
}
if (want > 0 && *rounds > want) *rounds = want;
@@ -2364,7 +2380,11 @@ SchedMetrics Scheduler::metrics_locked() const {
int64_t sum = 0, n = 0;
for (int32_t slot : running_) {
const SchedReq& s = *reqs_[(size_t)slot];
if (s.adapt.depth > 0) { sum += s.adapt.depth; ++n; }
if (s.adapt.depth <= 0) continue;
/* The window it drafts into, which under load is the cap and not its estimate. */
sum += (draft_busy_cap_ > 0 && s.adapt.depth > draft_busy_cap_) ? draft_busy_cap_
: s.adapt.depth;
++n;
}
m.draft_depth_adaptive = n ? (float)((double)sum / (double)n) : 0.0f;
const int kDepth = (int)(sizeof m.draft_verify_depth / sizeof m.draft_verify_depth[0]);
+19 -4
View File
@@ -501,13 +501,28 @@ private:
/* THE WINDOW A SEQUENCE MAY DRAFT INTO: the configured depth, or under RADIANCE_DRAFT_ADAPT
* the one its own acceptance pays for (DraftController::depth_for, which starts the estimate
* on the first ask). It is the rule every site that caps a proposal applies -- step(),
* set_draft(), expect_draft() and the rounds in serial_begin() -- and with the policy off it
* is next_depth(), exactly as before. */
* on the first ask), under this cycle's load cap. It is the rule every site that caps a
* proposal applies -- step(), set_draft(), expect_draft() and the rounds in serial_begin() --
* and with the policy off it is next_depth(), exactly as before. */
int draft_cap(SchedReq& s) const {
return draft_.adaptive() ? draft_.depth_for(s.adapt) : draft_.next_depth();
if (!draft_.adaptive()) return draft_.next_depth();
const int d = draft_.depth_for(s.adapt);
return (draft_busy_cap_ > 0 && d > draft_busy_cap_) ? draft_busy_cap_ : d;
}
/* THE LOAD CAP OF THE CYCLE IN FLIGHT (RADIANCE_DRAFT_ADAPT_BUSY, draft.h), 0 for none.
*
* COUNTED ONCE A CYCLE AND HELD, by draft_cycle(), which is the first thing step() and
* promote_next() do -- every cycle opens with exactly one of the two. A cycle's proposal
* passes through four sites -- the plan that verifies it, the rounds that draft the next one,
* expect_draft() and set_draft() -- and the step issued ahead holds them to agree: it was
* built with the count expect_draft() gave it, and set_draft() refuses a different one. A
* cap read live could move between them, because a commit or a client hanging up takes a
* sequence out of running_ mid-cycle; held, it moves only where a cycle starts, and a
* proposal drafted under one cap and planned under the next is only ever cut shorter. */
int32_t draft_busy_cap_ = 0;
void draft_cycle();
/* The built-in prompt-lookup drafter (scheduler.cpp). Fills s.draft and returns how many
* tokens it proposed; 0 when the sequence has no repeated pattern to extend. The pattern
* lengths it tries, longest first. */
+145
View File
@@ -1901,6 +1901,151 @@ TEST(the_adaptive_window_is_per_sequence_and_the_rounds_follow_the_deepest) {
CHECK(f.sch.metrics().draft_verify_depth[4] > 0);
}
/* THE LOAD CAP (RADIANCE_DRAFT_ADAPT_BUSY) IS COUNTED ONCE A CYCLE AND HELD FOR IT. The load moves
* between cycles -- a sequence admitted, a client hanging up -- and the cap follows it from the
* next cycle on; within one, the plan, the rounds, the step issued ahead and the proposal
* published after the commit all see the same cap, so a count that changes mid-cycle is never a
* mismatch. The estimates learn on underneath: the windows come back with the load. */
TEST(the_busy_cap_is_counted_once_a_cycle_and_follows_the_load_between_cycles) {
Fixture f;
f.build(/*blocks=*/4096, /*states=*/64);
f.cfg.n_spec = 4;
CHECK_OK(f.start());
DraftController::Params p;
p.max_depth = 4;
p.adapt = true;
p.busy = 2; /* more than two running is busy */
p.busy_depth = 2;
CHECK_OK(f.sch.draft().configure(p));
CHECK_EQ(f.sch.draft().busy_cap(2), 0);
CHECK_EQ(f.sch.draft().busy_cap(3), 2);
int32_t draft[4] = { 21, 22, 23, 24 };
/* Every draft taken, so every estimate stays at the whole window. */
auto commit_all = [&]() {
const StepPlan& pl = f.sch.plan();
std::vector<int32_t> acc(pl.e.size(), 0), tok(pl.e.size(), 9);
std::vector<uint8_t> eos(pl.e.size(), 0);
for (size_t i = 0; i < pl.e.size(); ++i) acc[i] = pl.e[i].is_prefill ? 0 : pl.e[i].n_spec;
Scheduler::StepResult r;
r.n_seq = (int)pl.e.size();
r.n_accepted = acc.data();
r.token = tok.data();
r.eos = eos.data();
CHECK_OK(f.sch.commit(r));
};
auto spec_of = [&](uint64_t id) {
for (const StepEntry& e : f.sch.plan().e) if (e.seq == id) return (int)e.n_spec;
return -1;
};
const uint64_t a = f.submit(160, 2000);
const uint64_t b = f.submit(160, 2000);
REQUIRE(f.sch.step() != nullptr); /* both prompts */
commit_all();
/* Two running is not busy: each takes its own window. */
CHECK_OK(f.sch.set_draft(a, draft, 4));
CHECK_OK(f.sch.set_draft(b, draft, 4));
REQUIRE(f.sch.step() != nullptr);
CHECK_EQ(spec_of(a), 4);
CHECK_EQ(spec_of(b), 4);
commit_all();
/* A third arrives. The plan that admits it counted two when its cycle opened, so that cycle
* is not busy yet... */
const uint64_t c = f.submit(160, 2000);
CHECK_OK(f.sch.set_draft(a, draft, 4));
CHECK_OK(f.sch.set_draft(b, draft, 4));
REQUIRE(f.sch.step() != nullptr);
CHECK_EQ(spec_of(a), 4);
CHECK_EQ(spec_of(c), 0); /* its prompt */
commit_all();
/* ...and the next one is: three running, every window capped, no estimate moved. The
* proposals were published under the last cycle, four long; this plan cuts them. */
CHECK_OK(f.sch.set_draft(a, draft, 4));
CHECK_OK(f.sch.set_draft(b, draft, 4));
REQUIRE(f.sch.step() != nullptr);
CHECK_EQ(spec_of(a), 2);
CHECK_EQ(spec_of(b), 2);
CHECK(spec_of(c) <= 2);
CHECK_NEAR(f.sch.metrics().draft_depth_adaptive, 2.0, 1e-6);
int rounds = 4;
Scheduler::SerialRow rows[4];
CHECK_EQ(f.sch.serial_begin(&rounds, rows, 4), 3);
CHECK_EQ(rounds, 2);
REQUIRE(f.sch.serial_batch(0, 1) != nullptr);
REQUIRE(f.sch.serial_batch(2, 1) != nullptr);
/* The step issued ahead verifies the cycle's count... */
for (uint64_t id : { a, b, c }) f.sch.find(id)->speculation_fixed = true;
for (uint64_t id : { a, b, c }) f.sch.expect_draft(id, rounds);
const RadBatch* nb = f.sch.step_next();
REQUIRE(nb != nullptr);
CHECK_EQ(nb->n_spec, 2);
/* ...and a client hangs up before the commit. Two running would not be busy, and the cycle
* keeps its cap anyway: the proposal published after the commit matches the step that is
* already on the card, so set_draft has nothing to refuse. */
CHECK_OK(f.sch.cancel(c));
CHECK_NEAR(f.sch.metrics().draft_depth_adaptive, 2.0, 1e-6);
commit_all();
f.sch.serial_end(rounds);
CHECK_OK(f.sch.set_draft(a, draft, rounds));
CHECK_OK(f.sch.set_draft(b, draft, rounds));
CHECK_EQ(f.sch.find(a)->n_draft, 2);
/* The next cycle opens on the load as it now is: two running, the cap lifted, and the
* windows are the whole window again at once -- the estimates never stopped learning. */
f.sch.promote_next();
CHECK_EQ(spec_of(a), 2); /* planned last cycle */
rounds = 4;
CHECK_EQ(f.sch.serial_begin(&rounds, rows, 4), 2);
CHECK_EQ(rounds, 4);
REQUIRE(f.sch.serial_batch(0, 1) != nullptr);
for (int rd = 2; rd <= rounds; ++rd) REQUIRE(f.sch.serial_batch(rd, 1) != nullptr);
commit_all();
f.sch.serial_end(rounds);
CHECK_OK(f.sch.set_draft(a, draft, rounds));
CHECK_OK(f.sch.set_draft(b, draft, rounds));
REQUIRE(f.sch.step() != nullptr);
CHECK_EQ(spec_of(a), 4);
CHECK_EQ(spec_of(b), 4);
CHECK_NEAR(f.sch.metrics().draft_depth_adaptive, 4.0, 1e-6);
commit_all();
/* THE SAME HANG-UP WITH NO STEP ISSUED AHEAD, where the held cap is what the proposal gets: a
* busy cycle stays busy to its end, and the next plan is the first to see the load fall. */
const uint64_t e = f.submit(160, 2000);
CHECK_OK(f.sch.set_draft(a, draft, 4));
CHECK_OK(f.sch.set_draft(b, draft, 4));
REQUIRE(f.sch.step() != nullptr); /* admits it; counted two */
commit_all();
CHECK_OK(f.sch.set_draft(a, draft, 4));
CHECK_OK(f.sch.set_draft(b, draft, 4));
REQUIRE(f.sch.step() != nullptr); /* counted three: busy */
CHECK_EQ(spec_of(a), 2);
CHECK_OK(f.sch.cancel(e));
commit_all();
CHECK_OK(f.sch.set_draft(a, draft, 4));
CHECK_EQ(f.sch.find(a)->n_draft, 2); /* the cycle's cap, not the live count's */
REQUIRE(f.sch.step() != nullptr); /* counted two: lifted */
CHECK_EQ(spec_of(a), 2); /* drafted under the busy cycle */
rounds = 4;
CHECK_EQ(f.sch.serial_begin(&rounds, rows, 4), 2);
CHECK_EQ(rounds, 4);
f.sch.serial_end(0);
/* No threshold set: no cap, whatever the load. */
DraftController::Params q;
q.max_depth = 4;
q.adapt = true;
DraftController d;
CHECK_OK(d.configure(q));
CHECK_EQ(d.busy_cap(1000), 0);
}
/* ================================================================== the step path */
TEST(the_step_path_does_not_allocate) {
Fixture f;