Archived
radiance 1.2.2
- Static YaRN for the Qwen rotary paths (#37): a container whose config names YaRN gets it in attention, the QSA index keys and a DFlash2 drafter; one reader for every rotary type, and a LLaMA container whose type is not served (llama3, longrope, dynamic) is refused by name. - --rope-scaling yarn:FACTOR[:ORIG] (#38) writes YaRN over a container's own rotary keys at load, no reconvert; the persisted prefix cache's fingerprint carries the resolved rotary. - An uneven world's cheaper rank (#36) pages the common token count and gives the rest of its VRAM to its experts; a plan and pool that disagree refuse to start. - MoE prefill (#35): wide gather, parallel scan and run-map prefix, 256-column down tile, early weight loads -- by rule, bit-identical. - MoE decode (#34): the sorted operand's own down kernel, its column tiles by routed rows. - --draft-window fixed|adaptive (#24): a per-sequence draft window priced by measured step time, one head depth per step over the batch; fixed stays the default. New metric radiance:draft_position_accepted_total and Grafana panels. - Out-of-tree plugin builds compile to the tree's device code from any build directory.
This commit is contained in:
1 parent
6fa7707dff
commit
ac79f4dc8a
63 files changed
+2744
-8759
No files matched your search
@@ -73,6 +73,10 @@ struct StepEntry {
|
||||
bool draft_out = false;
|
||||
bool is_prefill = false;
|
||||
bool produces_token = false; /* this entry's last token is the sequence's last */
|
||||
/* ITS DRAFTS CAME FROM PROMPT LOOKUP, not from the declared drafter. Their outcome is counted
|
||||
* in the reported acceptance like any other, and kept out of the adaptive window's estimate,
|
||||
* which describes the draft head (DraftController, draft.h). */
|
||||
bool ngram = false;
|
||||
/* A DEVICE ROW: the card fills its tokens, positions, rollback count and slots from the slot's
|
||||
* StepSlot before the pass runs (advance.h), and what the host staged for them is replaced.
|
||||
* `ctx_len` is then an upper bound, which is what the host-side ceilings read. The other three
|
||||
|
||||
+189
-197
@@ -1,23 +1,35 @@
|
||||
/* draft.cpp -- the static draft-depth policy, and the adaptive one RADIANCE_DRAFT_ADAPT turns on.
|
||||
* See draft.h for why the first is the default and what the second changes.
|
||||
/* draft.cpp -- the static draft-depth policy, and the adaptive one `--draft-window adaptive` turns
|
||||
* on. See draft.h for why the first is the default and what the second changes.
|
||||
*/
|
||||
#include "draft.h"
|
||||
|
||||
#include <cmath>
|
||||
#include <cstdint>
|
||||
#include <cstdlib>
|
||||
#include <cstring>
|
||||
#include <string>
|
||||
|
||||
namespace rad {
|
||||
|
||||
namespace {
|
||||
|
||||
/* THE COST MODEL WHEN NO TABLE IS GIVEN: every draft adds six percent to a step -- one round of
|
||||
* the head (about 0.4 ms profiled on QFN at tp4) and one more verify row, against a step of about
|
||||
* 12 ms. A guess at a shape, not a measurement of any deployment: one that has measured its own
|
||||
* steps by depth gives them as RADIANCE_DRAFT_COST. */
|
||||
/* THE STEP-COST PRIOR, a shape and not a measurement: every draft adds six percent to a step --
|
||||
* one round of a serial head and one more verify row against a weight-bound step. It prices a
|
||||
* depth nothing has measured yet, and only its RATIOS are read once anything has been measured:
|
||||
* an unmeasured depth is the nearest measured one scaled by prior(d) / prior(d'). */
|
||||
constexpr double kCostSlope = 0.06;
|
||||
double cost_prior(int d) { return 1.0 + kCostSlope * (double)d; }
|
||||
|
||||
/* A STEP TIME'S MEAN IS A RUNNING MEAN FOR ITS FIRST STEPS AND THEN A MOVING AVERAGE WITH A
|
||||
* HALF-LIFE OF 32 STEPS. Long enough that the scatter of one step against the next -- a tier
|
||||
* copy, a recording, a prefix-cache trim -- averages out; short enough to follow a context that
|
||||
* grows by a few thousand tokens over a reply. */
|
||||
constexpr double kCostHalfLife = 32.0;
|
||||
/* AND ONE STEP COUNTS FOR AT MOST FOUR TIMES THE KEY'S MEAN, once the mean is over four steps. A
|
||||
* step that waited on something other than its own work -- a first recording, an allocation, a
|
||||
* host stall -- says nothing about what its depth costs, and one such step would otherwise move a
|
||||
* short mean by more than any depth. A mean over fewer steps is not yet a reference to clip
|
||||
* against: its first step may itself be the odd one. */
|
||||
constexpr double kCostOutlier = 4.0;
|
||||
constexpr int64_t kCostClipAfter = 4;
|
||||
|
||||
/* THE PRIOR WEIGHS AS MUCH AS THIS MANY VERIFIES. The (n+1)-th real one moves an estimate by
|
||||
* 1 / (n + k + 1) -- a running mean over the prior and the evidence -- until that step falls below
|
||||
@@ -26,169 +38,37 @@ constexpr double kCostSlope = 0.06;
|
||||
* a short answer. */
|
||||
constexpr double kPriorWeight = 4.0;
|
||||
|
||||
/* A POSITION THE DEPLOYMENT HAS EVALUATED FEWER TIMES THAN THIS starts a new sequence from the
|
||||
* fixed prior rather than from the global count: a rate over a handful of verifies is noise, and a
|
||||
* position nobody has drafted to is no rate at all. */
|
||||
constexpr int64_t kGlobalMinReach = 64;
|
||||
|
||||
/* RADIANCE_DRAFT_COST="1:10.6,2:11.3,3:12.0": depth:cost pairs, comma separated. Returns how many
|
||||
* it read, or -1 when the string is not that. */
|
||||
int parse_cost(const char* s, float* cost) {
|
||||
int n = 0;
|
||||
const char* p = s;
|
||||
for (;;) {
|
||||
while (*p == ' ') ++p;
|
||||
if (!*p) return n;
|
||||
char* end = nullptr;
|
||||
const long d = std::strtol(p, &end, 10);
|
||||
if (end == p || *end != ':') return -1;
|
||||
p = end + 1;
|
||||
const double v = std::strtod(p, &end);
|
||||
if (end == p) return -1;
|
||||
if (d < 1 || d > RAD_SCHED_MAX_SPEC || !std::isfinite(v) || !(v > 0.0)) return -1;
|
||||
cost[d] = (float)v;
|
||||
++n;
|
||||
p = end;
|
||||
while (*p == ' ') ++p;
|
||||
if (*p == ',') ++p;
|
||||
else if (*p) return -1;
|
||||
}
|
||||
}
|
||||
|
||||
/* One knob, or the default with a warning when it does not parse. A knob that is silently not
|
||||
* read is the one that gets reported as "made no difference". */
|
||||
void env_real(const char* name, double lo, double hi, float* out) {
|
||||
const char* v = std::getenv(name);
|
||||
if (!v || !*v) return;
|
||||
char* end = nullptr;
|
||||
const double x = std::strtod(v, &end);
|
||||
if (end == v || *end || !(x >= lo && x <= hi)) {
|
||||
RAD_WARN("%s=%s is not a number in [%g, %g]; keeping %g", name, v, lo, hi, (double)*out);
|
||||
return;
|
||||
}
|
||||
*out = (float)x;
|
||||
}
|
||||
|
||||
void env_int(const char* name, long lo, long hi, int* out) {
|
||||
const char* v = std::getenv(name);
|
||||
if (!v || !*v) return;
|
||||
char* end = nullptr;
|
||||
const long x = std::strtol(v, &end, 10);
|
||||
if (end == v || *end || x < lo || x > hi) {
|
||||
RAD_WARN("%s=%s is not an integer in [%ld, %ld]; keeping %d", name, v, lo, hi, *out);
|
||||
return;
|
||||
}
|
||||
*out = (int)x;
|
||||
}
|
||||
|
||||
} /* namespace */
|
||||
|
||||
void DraftController::from_env(Params* p) {
|
||||
static const Params env = [] {
|
||||
Params e;
|
||||
const char* on = std::getenv("RADIANCE_DRAFT_ADAPT");
|
||||
e.adapt = on && *on && *on != '0';
|
||||
if (const char* c = std::getenv("RADIANCE_DRAFT_COST"); c && *c) {
|
||||
const int n = parse_cost(c, e.cost);
|
||||
if (n < 2) {
|
||||
RAD_WARN("RADIANCE_DRAFT_COST=\"%s\" %s; the adaptive draft depth prices a step at "
|
||||
"1 + %.2f per draft instead", c,
|
||||
n < 0 ? "is not a list of depth:cost pairs"
|
||||
: "has fewer than two depth:cost pairs", kCostSlope);
|
||||
std::memset(e.cost, 0, sizeof e.cost);
|
||||
}
|
||||
}
|
||||
env_real("RADIANCE_DRAFT_ADAPT_HYST", 0.0, 10.0, &e.hyst);
|
||||
env_int ("RADIANCE_DRAFT_ADAPT_HOLD", 0, 1000000, &e.hold);
|
||||
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;
|
||||
std::memcpy(p->cost, env.cost, sizeof p->cost);
|
||||
p->hyst = env.hyst;
|
||||
p->hold = env.hold;
|
||||
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) {
|
||||
if (p.max_depth < 0 || p.max_depth > RAD_SCHED_MAX_SPEC) {
|
||||
RAD_ERR("draft depth %d is outside 0..%d -- the verify kernel band is %d query rows",
|
||||
p.max_depth, RAD_SCHED_MAX_SPEC, RAD_SCHED_MAX_SPEC);
|
||||
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 || 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;
|
||||
tot_drafted_ = tot_accepted_ = 0;
|
||||
for (int j = 0; j < RAD_SCHED_MAX_SPEC; ++j) { reach_[j] = 0; hit_[j] = 0; }
|
||||
seen_ = 0;
|
||||
|
||||
adapt_ = p.adapt && p.max_depth > 0;
|
||||
alpha_ = 0.0;
|
||||
for (int d = 0; d <= RAD_SCHED_MAX_SPEC; ++d) { cost_[d] = 0.0; by_depth_[d] = 0; }
|
||||
/* SAID, because a switch that does nothing reads exactly like a switch that made no
|
||||
* difference. */
|
||||
if (p.adapt && !adapt_)
|
||||
RAD_WARN("RADIANCE_DRAFT_ADAPT is set and this deployment does not speculate "
|
||||
"(--num-speculative-tokens 0): there is no window to adapt");
|
||||
if (!adapt_) return RAD_OK;
|
||||
if (p_.min_depth > p_.max_depth) p_.min_depth = p_.max_depth;
|
||||
alpha_ = 1.0 - std::exp2(-1.0 / (double)p_.half_life);
|
||||
|
||||
/* THE COST TABLE, resolved for every window the policy may choose. A point that was measured
|
||||
* is taken as it is; between two points the line between them; past the ends the line through
|
||||
* the nearest two -- a table for depths 1..6 still prices depth 8 the way its own last two
|
||||
* steps say. A table that extrapolates to a cost at or below zero is a table with the wrong
|
||||
* sign in it, and the default is the better guess. */
|
||||
int pd[RAD_SCHED_MAX_SPEC];
|
||||
double pv[RAD_SCHED_MAX_SPEC];
|
||||
int np = 0;
|
||||
for (int d = 1; d <= RAD_SCHED_MAX_SPEC; ++d)
|
||||
if (std::isfinite(p.cost[d]) && p.cost[d] > 0.0f) {
|
||||
pd[np] = d;
|
||||
pv[np] = (double)p.cost[d];
|
||||
++np;
|
||||
}
|
||||
bool table = np >= 2;
|
||||
for (int d = 1; table && d <= p_.max_depth; ++d) {
|
||||
int i = 0;
|
||||
while (i + 2 < np && pd[i + 1] < d) ++i;
|
||||
const double t = (double)(d - pd[i]) / (double)(pd[i + 1] - pd[i]);
|
||||
cost_[d] = pv[i] + t * (pv[i + 1] - pv[i]);
|
||||
if (!(cost_[d] > 0.0)) {
|
||||
RAD_WARN("adaptive draft depth: the cost table extrapolates to %g at depth %d; pricing "
|
||||
"a step at 1 + %.2f per draft instead", cost_[d], d, kCostSlope);
|
||||
table = false;
|
||||
}
|
||||
alpha_ = 1.0 - std::exp2(-1.0 / kHalfLife);
|
||||
step_d_ = 0;
|
||||
step_w_ = 0;
|
||||
step_held_ = 0;
|
||||
prior_n_ = 0;
|
||||
for (int j = 0; j < RAD_SCHED_MAX_SPEC; ++j) prior_[j] = 0.0f;
|
||||
for (int d = 0; d <= RAD_SCHED_MAX_SPEC; ++d) {
|
||||
by_depth_[d] = 0;
|
||||
for (int c = 0; c < kCostClasses; ++c) { cost_ms_[d][c] = 0.0; cost_n_[d][c] = 0; }
|
||||
}
|
||||
if (!table)
|
||||
for (int d = 1; d <= p_.max_depth; ++d) cost_[d] = 1.0 + kCostSlope * (double)d;
|
||||
|
||||
std::string t;
|
||||
for (int d = 1; d <= p_.max_depth; ++d) t += fmt("%s%d:%.3g", d > 1 ? " " : "", d, cost_[d]);
|
||||
RAD_INFO("draft depth: ADAPTIVE per sequence (RADIANCE_DRAFT_ADAPT), %d..%d -- the window "
|
||||
"maximising E(d)/T(d) for T = [%s]%s; moves at +%.1f%% and at most every %d verifies; "
|
||||
"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);
|
||||
if (adapt_)
|
||||
RAD_INFO("draft depth: ADAPTIVE (--draft-window adaptive), %d..%d -- the step's head depth "
|
||||
"maximises the tokens its sequences are expected to emit over the step's measured "
|
||||
"cost, and a sequence stops at the first draft emitted less than %.0f%% of the "
|
||||
"time; moves at +%.0f%% and at most every %d steps",
|
||||
kMinDepth, p_.max_depth, 100.0 * kDraftMinChance, 100.0 * kHyst, kHold);
|
||||
return RAD_OK;
|
||||
}
|
||||
|
||||
@@ -212,49 +92,115 @@ void DraftController::observe(int drafted, int accepted) {
|
||||
if (adapt_) ++by_depth_[drafted];
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------ the adaptive policy */
|
||||
int DraftController::best_depth(const float* a, int at, double* best, double* at_gain) const {
|
||||
double e = 1.0, run = 1.0, bg = -1.0, ag = 0.0;
|
||||
int arg = p_.min_depth;
|
||||
for (int d = 1; d <= p_.max_depth; ++d) {
|
||||
run *= (double)a[d - 1];
|
||||
e += run;
|
||||
const double g = e / cost_[d];
|
||||
if (d == at) ag = g;
|
||||
if (d >= p_.min_depth && g > bg) { bg = g; arg = d; }
|
||||
/* ------------------------------------------------------------------ the step's cost */
|
||||
int DraftController::cost_class(int n_rows) {
|
||||
int c = 0;
|
||||
while (c + 1 < kCostClasses && n_rows >= (2 << c)) ++c;
|
||||
return c;
|
||||
}
|
||||
|
||||
void DraftController::step_time(int depth, int n_rows, double ms) {
|
||||
if (!adapt_ || depth < 1 || depth > p_.max_depth || n_rows < 1 || !(ms > 0.0) ||
|
||||
!std::isfinite(ms))
|
||||
return;
|
||||
const int c = cost_class(n_rows);
|
||||
double& m = cost_ms_[depth][c];
|
||||
int64_t& n = cost_n_[depth][c];
|
||||
if (n >= kCostClipAfter && ms > kCostOutlier * m) ms = kCostOutlier * m;
|
||||
const double w = std::fmax(1.0 - std::exp2(-1.0 / kCostHalfLife), 1.0 / (double)(n + 1));
|
||||
m = n ? m + w * (ms - m) : ms;
|
||||
++n;
|
||||
}
|
||||
|
||||
int64_t DraftController::cost_samples(int depth, int n_rows) const {
|
||||
if (depth < 1 || depth > RAD_SCHED_MAX_SPEC || n_rows < 1) return 0;
|
||||
return cost_n_[depth][cost_class(n_rows)];
|
||||
}
|
||||
|
||||
double DraftController::step_cost(int depth, int n_rows) const {
|
||||
if (depth < 1 || depth > RAD_SCHED_MAX_SPEC) return 0.0;
|
||||
const int c = n_rows >= 1 ? cost_class(n_rows) : 0;
|
||||
if (cost_n_[depth][c] > 0) return cost_ms_[depth][c];
|
||||
/* THE NEAREST MEASURED KEY: in this class first, the nearest depth and the shallower on a
|
||||
* tie; then the nearest class. Only its ratio to the prior is borrowed, so a class whose steps
|
||||
* are twice as dear prices every depth twice as dear, and the comparison D makes -- depths at
|
||||
* ONE class -- reads the prior's shape until its own depths are measured. */
|
||||
for (int dc = 0; dc < kCostClasses; ++dc) {
|
||||
for (int sgn = 0; sgn < 2; ++sgn) {
|
||||
const int k = sgn ? c + dc : c - dc;
|
||||
if (k < 0 || k >= kCostClasses || (dc == 0 && sgn)) continue;
|
||||
int best = -1;
|
||||
for (int d = 1; d <= p_.max_depth; ++d) {
|
||||
if (cost_n_[d][k] <= 0) continue;
|
||||
if (best < 0 || std::abs(d - depth) < std::abs(best - depth)) best = d;
|
||||
}
|
||||
if (best > 0) return cost_ms_[best][k] * cost_prior(depth) / cost_prior(best);
|
||||
}
|
||||
}
|
||||
if (best) *best = bg;
|
||||
if (at_gain) *at_gain = ag;
|
||||
return arg;
|
||||
return cost_prior(depth);
|
||||
}
|
||||
|
||||
double DraftController::gain(const DraftAdapt& s, int d) const {
|
||||
if (!adapt_ || d < 1 || d > p_.max_depth) return 0.0;
|
||||
double e = 1.0, run = 1.0;
|
||||
for (int k = 1; k <= d; ++k) { run *= (double)s.a[k - 1]; e += run; }
|
||||
return e / cost_[d];
|
||||
/* ------------------------------------------------------------------ one sequence */
|
||||
int DraftController::stop_of(const float* a) const {
|
||||
double p = 1.0;
|
||||
int d = 0;
|
||||
while (d < p_.max_depth) {
|
||||
p *= (double)a[d];
|
||||
if (p < kDraftMinChance) break;
|
||||
++d;
|
||||
}
|
||||
return d < kMinDepth ? kMinDepth : d;
|
||||
}
|
||||
|
||||
int DraftController::depth_for(DraftAdapt& s) const {
|
||||
if (!adapt_) return p_.max_depth;
|
||||
if (s.depth > 0) return s.depth;
|
||||
/* NOT STARTED. Each position from what the deployment has measured there, where it has
|
||||
* measured enough; otherwise from the fixed prior, which is optimistic on purpose: a window
|
||||
* that starts deep evaluates every position and learns all of them, and one that starts
|
||||
* shallow learns nothing about the positions it never drafts to. */
|
||||
double DraftController::prior_at(int j) const {
|
||||
if (j < 0 || j >= RAD_SCHED_MAX_SPEC) return 0.0;
|
||||
return prior_n_ >= kPriorMinSeqs ? (double)prior_[j] : kPrior;
|
||||
}
|
||||
|
||||
void DraftController::start(DraftAdapt& s) const {
|
||||
/* Each position from the sequences that finished before it once there are enough of them
|
||||
* (adapt_retire); otherwise from the fixed prior, which is optimistic on purpose (kPrior). */
|
||||
for (int j = 0; j < p_.max_depth; ++j) {
|
||||
s.a[j] = reach_[j] >= kGlobalMinReach ? (float)((double)hit_[j] / (double)reach_[j])
|
||||
: p_.prior;
|
||||
s.a[j] = (float)prior_at(j);
|
||||
s.n[j] = 0;
|
||||
}
|
||||
s.depth = best_depth(s.a, 0, nullptr, nullptr);
|
||||
s.depth = stop_of(s.a);
|
||||
s.held = 0;
|
||||
return s.depth;
|
||||
}
|
||||
|
||||
void DraftController::adapt_retire(const DraftAdapt& s) {
|
||||
if (!adapt_ || s.depth <= 0 || (double)s.n[0] < kHalfLife) return;
|
||||
const double w = std::fmax(1.0 - std::exp2(-1.0 / kPriorHalfLifeSeqs),
|
||||
1.0 / (double)(prior_n_ + 1));
|
||||
for (int j = 0; j < p_.max_depth; ++j)
|
||||
prior_[j] = prior_n_ ? (float)((double)prior_[j] + w * ((double)s.a[j] - (double)prior_[j]))
|
||||
: s.a[j];
|
||||
++prior_n_;
|
||||
}
|
||||
|
||||
double DraftController::yield(const DraftAdapt& s, int d) const {
|
||||
if (d > p_.max_depth) d = p_.max_depth;
|
||||
double e = 1.0, run = 1.0;
|
||||
for (int k = 1; k <= d; ++k) { run *= (double)s.a[k - 1]; e += run; }
|
||||
return e;
|
||||
}
|
||||
|
||||
int DraftController::window_of(const DraftAdapt& s) const {
|
||||
if (!adapt_) return p_.max_depth;
|
||||
if (s.depth <= 0) return 0;
|
||||
const int D = step_d_ > 0 ? step_d_ : p_.max_depth;
|
||||
return s.depth < D ? s.depth : D;
|
||||
}
|
||||
|
||||
int DraftController::window_for(DraftAdapt& s) const {
|
||||
if (!adapt_) return p_.max_depth;
|
||||
if (s.depth <= 0) start(s);
|
||||
return window_of(s);
|
||||
}
|
||||
|
||||
int DraftController::adapt_observe(DraftAdapt& s, int drafted, int accepted) const {
|
||||
if (!adapt_) return p_.max_depth;
|
||||
if (s.depth <= 0) (void)depth_for(s);
|
||||
if (s.depth <= 0) start(s);
|
||||
if (drafted <= 0) return s.depth;
|
||||
const int D = p_.max_depth;
|
||||
if (drafted > D) drafted = D;
|
||||
@@ -275,27 +221,73 @@ int DraftController::adapt_observe(DraftAdapt& s, int drafted, int accepted) con
|
||||
* learned about them while the window is shorter -- and a sequence held at three by prose that
|
||||
* turns into code would otherwise keep pricing positions four and up at whatever they read
|
||||
* before, and never draft deep enough to find out. The guess is that acceptance decays with
|
||||
* position rather than falling off a cliff, which is what QFN's head measured, so the window's
|
||||
* edge stands in for what lies past it. Where that is wrong it costs a move out, the verifies
|
||||
* that refute it, and a move back. */
|
||||
* position rather than falling off a cliff, so the window's edge stands in for what lies past
|
||||
* it. Where that is wrong it costs a move out, the verifies that refute it, and a move back. */
|
||||
const double edge = (double)s.a[drafted - 1];
|
||||
for (int j = drafted; j < D; ++j)
|
||||
s.a[j] = (float)((double)s.a[j] + alpha_ * (edge - (double)s.a[j]));
|
||||
|
||||
/* THE MOVE, rate-limited twice. A window changes the shape of the verify pass and of the
|
||||
* head's history pass, and a shape is a new recording before it plays (runtime/ctx.h); a
|
||||
* window that follows every flicker of the estimate pays that for nothing. So it moves only
|
||||
* when the window it would take is worth `hyst` more than the one it has, and not again within
|
||||
* `hold` verifies of the last move. */
|
||||
/* THE STOP MOVES ONLY PAST THE HOLD: it is this sequence's share of the step's shape. */
|
||||
if (s.held < INT32_MAX) ++s.held;
|
||||
if (s.held < p_.hold) return s.depth;
|
||||
double best = 0.0, cur = 0.0;
|
||||
const int d = best_depth(s.a, s.depth, &best, &cur);
|
||||
if (d != s.depth && best > cur * (1.0 + (double)p_.hyst)) {
|
||||
if (s.held < kHold) return s.depth;
|
||||
const int d = stop_of(s.a);
|
||||
if (d != s.depth) {
|
||||
s.depth = d;
|
||||
s.held = 0;
|
||||
}
|
||||
return s.depth;
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------ the step */
|
||||
int DraftController::plan_step(DraftAdapt* const* seq, int n_seq, int n_rows) {
|
||||
if (!adapt_ || n_seq <= 0) return step_d_;
|
||||
if (n_rows < n_seq) n_rows = n_seq;
|
||||
const int M = p_.max_depth;
|
||||
|
||||
/* THE STEP'S EXPECTED TOKENS AT EVERY HEAD DEPTH: sum_i E_i(min(D, stop_i)), plus one for
|
||||
* every row that does not draft. A depth past every stop is the deepest stop's step -- no
|
||||
* sequence drafts further and the head runs no further -- so the candidates end there. */
|
||||
double tok[RAD_SCHED_MAX_SPEC + 1] = {};
|
||||
int top = kMinDepth;
|
||||
for (int i = 0; i < n_seq; ++i) {
|
||||
DraftAdapt& s = *seq[i];
|
||||
if (s.depth <= 0) start(s);
|
||||
if (s.depth > top) top = s.depth;
|
||||
double e = 1.0, run = 1.0;
|
||||
for (int d = 1; d <= M; ++d) {
|
||||
if (d <= s.depth) { run *= (double)s.a[d - 1]; e += run; }
|
||||
tok[d] += e;
|
||||
}
|
||||
}
|
||||
const double plain = (double)(n_rows - n_seq);
|
||||
if (top > M) top = M;
|
||||
|
||||
int best = kMinDepth;
|
||||
double best_g = -1.0;
|
||||
for (int d = kMinDepth; d <= top; ++d) {
|
||||
const double g = (tok[d] + plain) / step_cost(d, n_rows);
|
||||
if (g > best_g) { best_g = g; best = d; }
|
||||
}
|
||||
|
||||
/* THE MOVE, rate-limited twice: not within kHold steps of the last one, and only for a depth
|
||||
* worth kHyst more than the one in force. The first step that drafts takes the best outright. */
|
||||
if (step_d_ <= 0) {
|
||||
step_d_ = best;
|
||||
step_held_ = 0;
|
||||
} else {
|
||||
if (step_held_ < INT32_MAX) ++step_held_;
|
||||
const int cur = step_d_ < top ? step_d_ : top;
|
||||
const double cur_g = (tok[cur] + plain) / step_cost(cur, n_rows);
|
||||
if (best != cur && step_held_ >= kHold && best_g > cur_g * (1.0 + kHyst)) {
|
||||
RAD_DEBUG("draft window: head depth %d -> %d over %d drafting of %d rows (%.3f -> "
|
||||
"%.3f tokens per unit of step cost)", step_d_, best, n_seq, n_rows, cur_g,
|
||||
best_g);
|
||||
step_d_ = best;
|
||||
step_held_ = 0;
|
||||
}
|
||||
}
|
||||
step_w_ = step_d_ < top ? step_d_ : top;
|
||||
return step_d_;
|
||||
}
|
||||
|
||||
} /* namespace rad */
|
||||
+154
-69
@@ -1,8 +1,8 @@
|
||||
/* draft.h -- the speculative draft-depth controller.
|
||||
*
|
||||
* IT IS STATIC. The depth is `--num-speculative-tokens` and nothing moves it.
|
||||
* BY DEFAULT IT IS STATIC. The depth is `--num-speculative-tokens` and nothing moves it.
|
||||
*
|
||||
* No dynamic policy sits here, and the reason is structural rather than a tuning failure. The
|
||||
* No dynamic policy sits there, and the reason is structural rather than a tuning failure. The
|
||||
* dynamic policy this engine is otherwise modelled on -- a confidence-product gate against a
|
||||
* decayed per-position acceptance estimate, plus a batch-size ceiling schedule -- gates on the
|
||||
* DRAFTER's own top-1 confidences, per slot, on device, after the drafter has run. The scheduler
|
||||
@@ -17,21 +17,34 @@
|
||||
* What survives is the accounting, because the acceptance rate is a reported metric (spec §14)
|
||||
* and because a verify that accepts nothing has to be visible.
|
||||
*
|
||||
* UNLESS RADIANCE_DRAFT_ADAPT=1, and then the premise above is what does not hold. On a model
|
||||
* whose weights stream -- a MoE that reads experts per verify row, a head that runs one pass a
|
||||
* UNDER `--draft-window adaptive` THE PREMISE ABOVE IS WHAT DOES NOT HOLD. On a model whose
|
||||
* weights stream -- a MoE that reads experts per verify row, a serial head that runs one pass a
|
||||
* round -- a step is NOT flat in depth: each draft is one more round of the head and one more
|
||||
* verify row, and on QFN at tp4 a step measured 12.0 ms at depth 3 and 15.0 ms at depth 5. And
|
||||
* the best depth is not a property of the model pair but of the TEXT: per-position acceptance
|
||||
* holds at ~0.9 to position five on code and JSON, sits near 0.6 on prose and falls to 0.2-0.4
|
||||
* past the third position on a summary. No constant is right for all of them, and the one that is
|
||||
* right for one costs the others either rounds nobody accepts or tokens nobody drafted.
|
||||
* verify row. And the best depth is not a property of the model pair but of the TEXT:
|
||||
* per-position acceptance stays high deep into the window on code and JSON and falls off after a
|
||||
* position or two on prose and summaries. No constant is right for all of them, and the one that
|
||||
* is right for one costs the others either rounds nobody accepts or tokens nobody drafted.
|
||||
*
|
||||
* So the switch adds a policy, per sequence and on the host: a smoothed estimate of each
|
||||
* position's conditional acceptance, fed by the same verify outcomes observe() counts (DraftAdapt),
|
||||
* and before every draft the window d that maximises E(d) / T(d) -- the tokens a step of that
|
||||
* window is expected to emit, 1 + sum_k prod_{j<=k} a_j, over what such a step costs. It moves
|
||||
* only when the gain is worth a re-recorded pass (Params::hyst, Params::hold). `max_depth` stays
|
||||
* the ceiling, and everything sized for it stays sized for it.
|
||||
* So the adaptive window is a policy on the host, in two halves:
|
||||
*
|
||||
* PER SEQUENCE (DraftAdapt): a smoothed estimate of each draft position's conditional
|
||||
* acceptance, fed by the same verify outcomes observe() counts, and the sequence's STOP -- the
|
||||
* deepest draft whose chance of being emitted still pays for the verify row it adds
|
||||
* (kDraftMinChance).
|
||||
*
|
||||
* PER STEP (plan_step): the HEAD DEPTH D. A serial head runs as many rounds as the deepest
|
||||
* window in the step, so depth is a property of the step and not of one sequence: D maximises
|
||||
* the tokens the whole step is expected to emit, sum_i E_i(min(D, stop_i)), over what a step
|
||||
* at that depth and that many sequences costs, T(D, n) -- with E(d) = 1 + sum_k prod_{j<=k} a_j
|
||||
* and T measured online (step_time). Every sequence then drafts min(D, stop_i).
|
||||
*
|
||||
* Both move only past a hold, and D also past a hysteresis, because a window is a pass shape and
|
||||
* a new shape is a new recording before it plays (runtime/ctx.h). `max_depth` stays the ceiling,
|
||||
* and everything sized for it stays sized for it.
|
||||
*
|
||||
* IT IS THE SERIAL DRAFTER'S POLICY. A block drafter proposes its whole block in one pass, and
|
||||
* prompt lookup runs no pass at all, so neither pays the rounds the policy prices; the scheduler
|
||||
* turns it on only for a serial drafter (Scheduler::init) and says so when it does not.
|
||||
*
|
||||
* IT CHANGES HOW MANY DRAFTS A STEP VERIFIES AND NOTHING ELSE. Acceptance is the rule in
|
||||
* core/sample/accept.h, so what a step emits is the target's own token at every position whatever
|
||||
@@ -50,14 +63,14 @@ namespace rad {
|
||||
enum { RAD_SCHED_MAX_SPEC = 64 };
|
||||
|
||||
/* ONE SEQUENCE'S HALF OF THE ADAPTIVE POLICY: what the drafter has done on this sequence's own
|
||||
* text, and the window that buys. It lives in the scheduler's per-request state and the controller
|
||||
* does the arithmetic, so neither has to look the other up.
|
||||
* text, and how deep that makes it worth drafting. It lives in the scheduler's per-request state
|
||||
* and the controller does the arithmetic, so neither has to look the other up.
|
||||
*
|
||||
* `depth` 0 is "not started": the first ask fills the estimate from the prior and picks a window.
|
||||
* `depth` 0 is "not started": the first ask fills the estimate from the prior and sets the stop.
|
||||
* It belongs to the REQUEST, not to the cache it holds -- a preemption keeps it, because the text
|
||||
* it describes is still the text the request is writing -- and a recycled slot starts it over. */
|
||||
struct DraftAdapt {
|
||||
int32_t depth = 0; /* the window the sequence gets now; 0 = not started */
|
||||
int32_t depth = 0; /* the sequence's stop; 0 = not started */
|
||||
int32_t held = 0; /* verifies since `depth` last moved */
|
||||
float a[RAD_SCHED_MAX_SPEC] = {}; /* conditional acceptance at each draft position */
|
||||
uint16_t n[RAD_SCHED_MAX_SPEC] = {}; /* verifies that evaluated each one, saturating */
|
||||
@@ -66,63 +79,125 @@ struct DraftAdapt {
|
||||
class DraftController {
|
||||
public:
|
||||
struct Params {
|
||||
int max_depth = 0; /* the deployment's window; 0 disables speculation entirely */
|
||||
|
||||
/* THE ADAPTIVE POLICY (see the top of this file), off unless RADIANCE_DRAFT_ADAPT=1 --
|
||||
* from_env() is where the switch and the knobs below are read, once per process. With it
|
||||
* off every field below is ignored and the depth is max_depth, as it always was. */
|
||||
bool adapt = false;
|
||||
/* T(d), what a step verifying d drafts costs, by d; any unit, only ratios are read. An
|
||||
* entry above zero is a measured point, the rest are interpolated between the points and
|
||||
* extrapolated past them along the nearest two. Fewer than two points is no table, and
|
||||
* then T(d) = 1 + 0.06 d. Index 0 is unused. */
|
||||
float cost[RAD_SCHED_MAX_SPEC + 1] = {};
|
||||
float hyst = 0.02f; /* a move must buy this much more E/T than the window it leaves */
|
||||
int hold = 8; /* and come at least this many verifies after the last move */
|
||||
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 max_depth = 0; /* the deployment's window; 0 disables speculation entirely */
|
||||
/* THE ADAPTIVE POLICY (see the top of this file): `--draft-window adaptive` with a serial
|
||||
* drafter, which Scheduler::init decides. Off, the depth is max_depth, as it always was. */
|
||||
bool adapt = false;
|
||||
};
|
||||
|
||||
/* ---------------------------------------------------------- the policy's constants
|
||||
* Named rather than configurable: each is a property of how the policy works, not of a
|
||||
* deployment, and the one property that IS a deployment's -- what a step costs -- is
|
||||
* measured (step_time). */
|
||||
|
||||
/* A WINDOW MOVES ONLY FOR TWO PERCENT MORE YIELD. One draft more or less moves E/T by several
|
||||
* percent on any drafter worth speculating with, while a smoothed estimate wobbles by about
|
||||
* a percent verify to verify; below two percent a move would flip D between two near-equal
|
||||
* neighbours and pay a recording each time. */
|
||||
static constexpr double kHyst = 0.02;
|
||||
/* AND NOT WITHIN EIGHT VERIFIES (steps, for D) OF THE LAST MOVE. A pass shape is recorded the
|
||||
* second time its key is seen and played from the third (runtime/ctx.h), so a shape held for
|
||||
* eight steps plays from the tape for most of them; and eight verifies at the half-life below
|
||||
* move an estimate by about a third of the way to a new rate, which is a trend rather than
|
||||
* one rejection. */
|
||||
static constexpr int kHold = 8;
|
||||
/* EACH POSITION'S ESTIMATE HAS A HALF-LIFE OF SIXTEEN VERIFIES THAT EVALUATE IT. At a few
|
||||
* tokens a verify that is some fifty tokens of text: short enough to follow a reply from prose
|
||||
* into a code block and back, long enough that a single rejection moves an estimate by four
|
||||
* percent rather than resetting it. */
|
||||
static constexpr double kHalfLife = 16.0;
|
||||
/* A POSITION NOTHING HAS MEASURED STARTS AT 0.8, optimistic on purpose: a window that starts
|
||||
* deep evaluates every position and learns all of them, one that starts shallow learns nothing
|
||||
* about the positions it never drafts to. 0.8 sits between what draft heads measure on prose
|
||||
* and on code at the first positions, so a wrong start is corrected within a hold. */
|
||||
static constexpr double kPrior = 0.8;
|
||||
/* ONCE FOUR SEQUENCES HAVE FINISHED, A NEW ONE STARTS FROM WHAT THEY ENDED ON (adapt_retire):
|
||||
* the mean of their estimates, a running mean and then a moving average with a half-life of 32
|
||||
* sequences, so a deployment that moves from one kind of traffic to another follows it within
|
||||
* a few dozen requests. Four, because a mean over fewer is one text's. A sequence counts only
|
||||
* once it has had a half-life of verifies: before that its estimate is mostly the prior it
|
||||
* started from, and folding it back would feed the prior to itself. */
|
||||
static constexpr int kPriorMinSeqs = 4;
|
||||
static constexpr double kPriorHalfLifeSeqs = 32.0;
|
||||
/* NEVER FEWER THAN ONE DRAFT. A sequence at zero would evaluate no position, so nothing could
|
||||
* ever move its estimate off zero again. */
|
||||
static constexpr int kMinDepth = 1;
|
||||
/* A SEQUENCE STOPS AT THE FIRST DRAFT WHOSE CHANCE OF BEING EMITTED -- every draft before it
|
||||
* and itself accepted, prod_{j<=d} a_j -- IS UNDER ONE IN TEN. The draft adds a verify row to
|
||||
* the step whatever D is, and that row is the only cost a sequence adds once the head runs D
|
||||
* rounds for its deepest neighbour. At one sequence the step-cost prior (1 + 0.06 d) already
|
||||
* breaks even near that chance -- one more draft is worth it while p_d / E exceeds the 5-6 %
|
||||
* it adds to the step, with E around two -- and a verify row alone costs less than a round
|
||||
* plus a row, so the threshold is never stricter than D's own trade. A constant rather than a
|
||||
* measurement: a row's share of the step is not separable from the head's rounds in step
|
||||
* times keyed by depth, and the measured T(D, n) already prices the rounds. */
|
||||
static constexpr double kDraftMinChance = 0.1;
|
||||
/* STEP TIMES ARE KEPT IN THESE RUNNING-SEQUENCE CLASSES: 1, 2-3, 4-7, 8-15, 16 and up. A
|
||||
* step's cost moves with how many rows share its weight reads, and a class a power of two
|
||||
* wide keeps every key fed often enough to mean something. */
|
||||
static constexpr int kCostClasses = 5;
|
||||
|
||||
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, _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);
|
||||
|
||||
/* The draft depth for the next step. 0 means "do not speculate". Constant by construction:
|
||||
* under the adaptive policy it is the CEILING, and depth_for() is the window. */
|
||||
* under the adaptive policy it is the CEILING, and window_for() is the window. */
|
||||
int next_depth() const { return p_.max_depth; }
|
||||
|
||||
/* ---------------------------------------------------------- the adaptive policy */
|
||||
bool adaptive() const { return adapt_; }
|
||||
/* The window this sequence may draft into: its policy depth, starting the estimate on the
|
||||
* first ask; max_depth when the policy is off. */
|
||||
int depth_for(DraftAdapt& s) const;
|
||||
/* The window this sequence may draft into: min(D, its stop), starting its estimate on the
|
||||
* first ask; max_depth when the policy is off. Before any step has chosen D, D is the
|
||||
* ceiling. */
|
||||
int window_for(DraftAdapt& s) const;
|
||||
/* The same for a sequence whose estimate has started, without starting one: 0 when it has
|
||||
* not. For reporting. */
|
||||
int window_of(const DraftAdapt& s) const;
|
||||
/* 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. */
|
||||
* estimate, and its stop past the hold. Returns the stop 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). */
|
||||
double cost(int d) const {
|
||||
return (d >= 1 && d <= RAD_SCHED_MAX_SPEC) ? cost_[d] : 0.0;
|
||||
}
|
||||
/* A SEQUENCE THAT HAS FINISHED: its estimate joins the prior the next sequences start from.
|
||||
*
|
||||
* PER SEQUENCE, NOT PER VERIFY. The deployment's own counts -- accepted(j) / reached(j) over
|
||||
* every verify -- are what a fixed window would want, and under this policy they are biased:
|
||||
* only a sequence whose drafts keep being accepted is given a window that reaches the deep
|
||||
* positions, so those positions are measured almost only on the texts that predict well, and
|
||||
* a new sequence would start as if every text did. Each finished sequence counts once here, at
|
||||
* every position, and at a position it never reached its estimate is the extrapolation from
|
||||
* its own window's edge (adapt_observe) -- a shallow text pulls the deep positions down as
|
||||
* much as a deep one holds them up. */
|
||||
void adapt_retire(const DraftAdapt& s);
|
||||
/* The prior a sequence starting now is given at position `j`. For tests and logs. */
|
||||
double prior_at(int j) const;
|
||||
|
||||
/* D FOR THE STEP THAT IS OPENING. `seq` are the estimates of the `n_seq` sequences that will
|
||||
* draft in it, `n_rows` the decode rows it carries in all (the rest emit one token each and
|
||||
* draft nothing). Started here if they have not been. Moves D past the hold and the
|
||||
* hysteresis; with no sequence to draft, D stays. Returns D. */
|
||||
int plan_step(DraftAdapt* const* seq, int n_seq, int n_rows);
|
||||
/* The step depth in force: 0 before the first step chose one. */
|
||||
int step_depth() const { return step_d_; }
|
||||
/* The deepest window a step at that depth carries: D, but no deeper than the deepest stop it
|
||||
* was chosen over, since no head-drafting sequence drafts past its stop; the ceiling before
|
||||
* any step chose one, and with the policy off. What a proposal from outside the head (prompt
|
||||
* lookup) is held to, so it adds no deeper pass shape than the head's own. */
|
||||
int step_window() const { return adapt_ && step_w_ > 0 ? step_w_ : p_.max_depth; }
|
||||
|
||||
/* T(depth, n): a decode step whose deepest window was `depth`, with `n_rows` decode rows,
|
||||
* took `ms` from the end of the step before it. Fed by Scheduler::step_finished; only the
|
||||
* adaptive policy reads it. */
|
||||
void step_time(int depth, int n_rows, double ms);
|
||||
/* What a step at `depth` with `n_rows` rows is expected to cost, in ms when anything has
|
||||
* been measured: the key's own mean when it has samples; the nearest measured key scaled by
|
||||
* the prior's ratio (1 + 0.06 d) when it does not; the prior itself when nothing has been
|
||||
* measured. */
|
||||
double step_cost(int depth, int n_rows) const;
|
||||
/* Samples behind a key: for tests and logs. */
|
||||
int64_t cost_samples(int depth, int n_rows) const;
|
||||
|
||||
/* E(d) for `s`'s estimate as it stands: the tokens a verify of d drafts is expected to emit,
|
||||
* the verified token included. For tests and logs. */
|
||||
double yield(const DraftAdapt& s, int d) const;
|
||||
/* Verifies by the window they carried, counted only under the policy: the histogram of the
|
||||
* depths it actually chose, which the mean gauge on its own cannot show. */
|
||||
int64_t verified_at(int d) const {
|
||||
@@ -170,13 +245,23 @@ private:
|
||||
|
||||
/* The adaptive policy as configure() resolved it. */
|
||||
bool adapt_ = false;
|
||||
double cost_[RAD_SCHED_MAX_SPEC + 1] = {}; /* T(d), resolved for 1..max_depth */
|
||||
double alpha_ = 0.0; /* the smoothing step from half_life */
|
||||
double alpha_ = 0.0; /* the smoothing step from kHalfLife */
|
||||
int64_t by_depth_[RAD_SCHED_MAX_SPEC + 1] = {};
|
||||
int32_t step_d_ = 0; /* D; 0 until a step chose one */
|
||||
int32_t step_w_ = 0; /* min(D, the deepest stop it saw) */
|
||||
int32_t step_held_ = 0; /* steps since D last moved */
|
||||
/* The prior from finished sequences (adapt_retire), and how many are behind it. */
|
||||
float prior_[RAD_SCHED_MAX_SPEC] = {};
|
||||
int64_t prior_n_ = 0;
|
||||
/* T(d, class): the mean step time of each key, and the steps behind it. */
|
||||
double cost_ms_[RAD_SCHED_MAX_SPEC + 1][kCostClasses] = {};
|
||||
int64_t cost_n_ [RAD_SCHED_MAX_SPEC + 1][kCostClasses] = {};
|
||||
|
||||
/* The window in [min_depth, max_depth] that maximises E/T for estimate `a`, the smaller on a
|
||||
* tie; its E/T in *best, and the E/T of window `at` in *at_gain. */
|
||||
int best_depth(const float* a, int at, double* best, double* at_gain) const;
|
||||
/* Fill a sequence's estimate from the prior and set its first stop. */
|
||||
void start(DraftAdapt& s) const;
|
||||
/* The deepest draft whose chance of being emitted is at least kDraftMinChance. */
|
||||
int stop_of(const float* a) const;
|
||||
static int cost_class(int n_rows);
|
||||
};
|
||||
|
||||
} /* namespace rad */
|
||||
@@ -137,7 +137,7 @@ struct SchedMetrics {
|
||||
int draft_depth_seen = 0;
|
||||
int64_t draft_pos_reached [RAD_METRICS_MAX_SPEC] = {};
|
||||
int64_t draft_pos_accepted[RAD_METRICS_MAX_SPEC] = {};
|
||||
/* THE ADAPTIVE WINDOW (RADIANCE_DRAFT_ADAPT, draft.h), all zero while it is off. The mean
|
||||
/* THE ADAPTIVE WINDOW (--draft-window adaptive, draft.h), all zero while it is off. The mean
|
||||
* window it gives the sequences running now -- at one sequence, that sequence's window -- and
|
||||
* the verifies by the window they carried, index = drafts verified, cumulative: the gauge says
|
||||
* where the policy stands, the histogram where it has been. */
|
||||
|
||||
+81
-36
@@ -97,10 +97,29 @@ int Scheduler::init(Program& prog, const Config& cfg, IKVManager* kv, IPrefixCac
|
||||
|
||||
DraftController::Params dp;
|
||||
dp.max_depth = cfg.n_spec;
|
||||
/* The adaptive window's switch and knobs, read once per process (draft.h). Off unless set. */
|
||||
DraftController::from_env(&dp);
|
||||
/* THE ADAPTIVE WINDOW IS THE SERIAL DRAFTER'S POLICY (draft.h): it prices a draft as one more
|
||||
* round of the head. A block drafter proposes its block in one pass and prompt lookup runs no
|
||||
* pass at all, so for either the window stays fixed -- decided here, once, before any step
|
||||
* has counted an acceptance, and said, because a flag that changes nothing reads exactly
|
||||
* like a flag that made no difference. */
|
||||
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.depth <= 0)
|
||||
RAD_WARN("--draft-window adaptive: the window is fixed at %d with %s -- the adaptive "
|
||||
"window prices the rounds of a serial draft head, which this deployment does "
|
||||
"not run", cfg.n_spec,
|
||||
dd.kind == RAD_DRAFT_BLOCK ? "a block drafter, which proposes its block in one "
|
||||
"pass"
|
||||
: "prompt lookup only");
|
||||
else
|
||||
dp.adapt = true;
|
||||
}
|
||||
RAD_TRY(draft_.configure(dp));
|
||||
draft_busy_cap_ = 0;
|
||||
draft_est_.clear();
|
||||
draft_est_.reserve((size_t)cfg.max_seqs);
|
||||
|
||||
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
|
||||
@@ -366,6 +385,12 @@ void Scheduler::close_request(SchedReq& s, ReqState st, const char* reason, bool
|
||||
}
|
||||
|
||||
if (cfg_.prefix_cache && pc_) pc_->release(*s.req);
|
||||
/* What the draft head did on this text is now part of what the next text starts from --
|
||||
* once: the estimate is marked unstarted, so a second close of the same request adds nothing. */
|
||||
if (draft_.adaptive()) {
|
||||
draft_.adapt_retire(s.adapt);
|
||||
s.adapt.depth = 0;
|
||||
}
|
||||
|
||||
if (defer_free) {
|
||||
if (!s.pending_free) ++n_deferred_;
|
||||
@@ -937,7 +962,7 @@ 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_). */
|
||||
/* A cycle opens here, so its head depth is chosen here (scheduler.h, draft_cycle). */
|
||||
draft_cycle();
|
||||
|
||||
/* The drafter's snapshots describe the step before this one and nothing reads them from here
|
||||
@@ -997,12 +1022,19 @@ const RadBatch* Scheduler::step() {
|
||||
* none. Request::may_speculate says why a request can be ineligible.
|
||||
*
|
||||
* 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. */
|
||||
* policy is on -- then it is what this sequence's acceptance of the HEAD's drafts pays
|
||||
* for. The n-gram fallback is not the head and that estimate says nothing about it, so
|
||||
* it is held to the step's window instead: as deep as the head drafts this step and no
|
||||
* deeper, which adds no pass shape the head's own windows do not. */
|
||||
const int cap = draft_.adaptive() ? draft_cap(s) : depth;
|
||||
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, cap);
|
||||
if (s.n_draft > cap) s.n_draft = cap;
|
||||
else if (s.n_draft == 0) {
|
||||
s.n_draft = (int32_t)draft_ngram(s, draft_.step_window());
|
||||
ngram = s.n_draft > 0;
|
||||
}
|
||||
if (!ngram && s.n_draft > cap) s.n_draft = cap;
|
||||
s.draft_ngram = ngram;
|
||||
/* NO VERIFIED POSITION AT OR PAST THE CONTEXT. The step computes positions n_computed
|
||||
* .. n_computed + n_draft, and nothing past ctx_limit_ - 1 has a rotary row or a
|
||||
* block-table column. commit() finishes a sequence before its length reaches the
|
||||
@@ -1089,6 +1121,7 @@ const RadBatch* Scheduler::step() {
|
||||
e.draft = s.draft;
|
||||
e.n_draft = s.n_draft;
|
||||
e.is_prefill = prefill;
|
||||
e.ngram = !prefill && s.draft_ngram && s.n_draft > 0;
|
||||
e.produces_token = (r.n_computed + want >= target);
|
||||
/* The snapshot a hit resumes from, consumed once: the state is in the slot for the whole
|
||||
* sequence after this, and restoring it again on a later chunk would rewind it. */
|
||||
@@ -1355,24 +1388,23 @@ void Scheduler::take_encodes(std::vector<std::shared_ptr<mm::Item>>* out) {
|
||||
out->swap(enc_want_);
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------ the load cap */
|
||||
void Scheduler::draft_adapt_off(const char* why) {
|
||||
std::lock_guard<std::mutex> lk(mu_);
|
||||
if (!draft_.adaptive()) return;
|
||||
DraftController::Params p = draft_.params();
|
||||
p.adapt = false;
|
||||
if (draft_.configure(p) == RAD_OK)
|
||||
RAD_WARN("RADIANCE_DRAFT_ADAPT is set and the drafter is %s: the adaptive window is the serial "
|
||||
"drafter's policy, so this run keeps the configured depth %d", why, p.max_depth);
|
||||
draft_busy_cap_ = 0;
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------ the step's head depth */
|
||||
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;
|
||||
if (!draft_.adaptive()) return;
|
||||
/* THE ROWS THE OPENING STEP WILL DECODE: every running sequence past its prompt. Those that
|
||||
* may speculate draft, and their estimates choose D; the rest emit one token each, which is
|
||||
* part of what the step yields and of what it costs. A sequence still prefilling is neither. */
|
||||
draft_est_.clear();
|
||||
int n_rows = 0;
|
||||
for (int32_t slot : running_) {
|
||||
SchedReq& s = *reqs_[(size_t)slot];
|
||||
if (!s.req || !s.running) continue;
|
||||
const Request& r = *s.req;
|
||||
if (r.n_computed < (int64_t)r.prompt.size() || r.n_computed + 1 < s.seq_len()) continue;
|
||||
++n_rows;
|
||||
if (r.may_speculate) draft_est_.push_back(&s.adapt);
|
||||
}
|
||||
(void)draft_.plan_step(draft_est_.data(), (int)draft_est_.size(), n_rows);
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------ the next step */
|
||||
@@ -1383,7 +1415,7 @@ 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: nothing commits between the two, and the load cap is the cycle's (draft_busy_cap_). */
|
||||
* since: nothing commits between the two, and D is the cycle's (draft_cycle). */
|
||||
if (draft_.adaptive() && n > 0) {
|
||||
const int cap = draft_cap(s);
|
||||
if (n > cap) n = cap;
|
||||
@@ -1473,7 +1505,7 @@ 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
|
||||
* called. Its proposals were counted under the last cycle's D; the next ones are drafted
|
||||
* under this one. */
|
||||
draft_cycle();
|
||||
std::swap(plan_, plan_next_);
|
||||
@@ -1634,9 +1666,12 @@ int Scheduler::commit(const StepResult& res) {
|
||||
if (e.n_spec > 0) {
|
||||
draft_.observe(e.n_spec, a);
|
||||
/* AND THIS SEQUENCE'S OWN ESTIMATE, from the same outcome, under the adaptive
|
||||
* window. A move takes effect at the next draft: this step's drafter has already
|
||||
* run, sized from the estimate as it stood before this commit. */
|
||||
if (draft_.adaptive()) {
|
||||
* window -- when the head drafted it. A prompt-lookup proposal is accepted on
|
||||
* different text by a different rule, and folding it in would price the head's
|
||||
* next window on what the lookup found. A move takes effect at the next draft:
|
||||
* this step's drafter has already run, sized from the estimate as it stood before
|
||||
* this commit. */
|
||||
if (draft_.adaptive() && !e.ngram) {
|
||||
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) {
|
||||
@@ -1646,9 +1681,10 @@ int Scheduler::commit(const StepResult& res) {
|
||||
o += std::snprintf(est + o, sizeof est - (size_t)o, "%s%.2f",
|
||||
j ? " " : "", (double)s.adapt.a[j]);
|
||||
est[o] = 0;
|
||||
RAD_DEBUG("draft window: request %llu %d -> %d (E/T %.4f -> %.4f; "
|
||||
"acceptance by position %s)", (unsigned long long)r.id, was,
|
||||
now, draft_.gain(s.adapt, was), draft_.gain(s.adapt, now), est);
|
||||
RAD_DEBUG("draft window: request %llu stops at %d, was %d (expected "
|
||||
"tokens a verify %.3f -> %.3f; acceptance by position %s)",
|
||||
(unsigned long long)r.id, now, was, draft_.yield(s.adapt, was),
|
||||
draft_.yield(s.adapt, now), est);
|
||||
}
|
||||
}
|
||||
m_.draft_tokens += e.n_spec;
|
||||
@@ -1777,6 +1813,16 @@ 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;
|
||||
}
|
||||
/* AND WHAT THE STEP COST AT ITS DEPTH, for the adaptive window (draft.h): the same wall time,
|
||||
* which is the step's share of the loop -- the draft rounds that followed it included, and a
|
||||
* step issued ahead counted from the end of the one before rather than twice. A decode step
|
||||
* only: a prefill chunk's time is the chunk's, not the window's. Keyed by the deepest window
|
||||
* the step verified, which is the head depth it ran at whenever D held from the step before. */
|
||||
if (draft_.adaptive() && plan_.phase == RAD_PHASE_DECODE && plan_.n_spec > 0 && wall > 0) {
|
||||
int rows = 0;
|
||||
for (const StepEntry& e : plan_.e) rows += (e.produces_token && !e.is_prefill) ? 1 : 0;
|
||||
draft_.step_time((int)plan_.n_spec, rows, wall * 1e3);
|
||||
}
|
||||
last_step_at_ = step_end;
|
||||
/* So a second call, or one on a loop iteration that ran no step, charges nothing. */
|
||||
step_start_ = {};
|
||||
@@ -2392,9 +2438,8 @@ SchedMetrics Scheduler::metrics_locked() const {
|
||||
for (int32_t slot : running_) {
|
||||
const SchedReq& s = *reqs_[(size_t)slot];
|
||||
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;
|
||||
/* The window it drafts into: its stop under the step's head depth. */
|
||||
sum += draft_.window_of(s.adapt);
|
||||
++n;
|
||||
}
|
||||
m.draft_depth_adaptive = n ? (float)((double)sum / (double)n) : 0.0f;
|
||||
|
||||
+20
-26
@@ -109,11 +109,13 @@ struct SchedReq {
|
||||
draft_hi = 0;
|
||||
}
|
||||
|
||||
/* THE ADAPTIVE DRAFT WINDOW'S ESTIMATE FOR THIS REQUEST (RADIANCE_DRAFT_ADAPT, draft.h), and
|
||||
/* THE ADAPTIVE DRAFT WINDOW'S ESTIMATE FOR THIS REQUEST (--draft-window adaptive, draft.h), and
|
||||
* deliberately NOT in drafter_reset(): it describes the text the request is writing, which a
|
||||
* preemption does not change, rather than any cache the request holds. A recycled slot starts
|
||||
* it over in add_locked. Untouched while the policy is off. */
|
||||
DraftAdapt adapt;
|
||||
/* The proposal the step being planned carries came from prompt lookup (StepEntry::ngram). */
|
||||
bool draft_ngram = false;
|
||||
|
||||
int32_t sched_tokens = 0; /* tokens given to this request in the current step */
|
||||
int32_t entry = -1; /* its index in the current StepPlan, or -1 */
|
||||
@@ -290,13 +292,6 @@ public:
|
||||
* (draft_cap): the controller's depth, or what the adaptive policy gives the sequence. */
|
||||
int set_draft(uint64_t id, const int32_t* tokens, int n);
|
||||
|
||||
/* RADIANCE_DRAFT_ADAPT is the SERIAL drafter's policy: every draft there is one more round of
|
||||
* the head, which is what its cost table prices. A block drafter proposes its whole block in
|
||||
* one pass, so a shorter window only saves verify rows -- a different trade with a policy of its
|
||||
* own. The engine calls this once, before the first step, when the drafter is a block drafter;
|
||||
* the switch then changes nothing about that run. Says so when it was on. */
|
||||
void draft_adapt_off(const char* why);
|
||||
|
||||
/* ---------------------------------------------------------- media the encoder owes
|
||||
* A REQUEST CARRYING MEDIA IS ADMITTED ONLY ONCE EVERY ITEM IS ENCODED. Until then it keeps
|
||||
* its place in the queue, the requests behind it are admitted around it, and its items wait
|
||||
@@ -506,28 +501,27 @@ private:
|
||||
return hit_holes_draft_ ? ctx_len : 0;
|
||||
}
|
||||
|
||||
/* 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), 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. */
|
||||
/* THE WINDOW A SEQUENCE MAY DRAFT INTO: the configured depth, or under the adaptive window
|
||||
* min(D, its stop) (DraftController::window_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. */
|
||||
int draft_cap(SchedReq& s) const {
|
||||
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;
|
||||
return draft_.adaptive() ? draft_.window_for(s.adapt) : draft_.next_depth();
|
||||
}
|
||||
|
||||
/* THE LOAD CAP OF THE CYCLE IN FLIGHT (RADIANCE_DRAFT_ADAPT_BUSY, draft.h), 0 for none.
|
||||
/* THE STEP'S HEAD DEPTH D IS CHOSEN 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 D read live could move between them, because a commit or a client hanging
|
||||
* up changes the running set mid-cycle; held, it moves only where a cycle starts, and a
|
||||
* proposal drafted under one D and planned under the next is only ever cut shorter.
|
||||
*
|
||||
* 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;
|
||||
* `draft_est_` is the list plan_step reads, reserved at init so the step path does not
|
||||
* allocate. */
|
||||
std::vector<DraftAdapt*> draft_est_;
|
||||
void draft_cycle();
|
||||
|
||||
/* The built-in prompt-lookup drafter (scheduler.cpp). Fills s.draft and returns how many
|
||||
|
||||
Reference in new issue
Block a user