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. */