Archived
draft: an adaptive draft window per sequence (RADIANCE_DRAFT_ADAPT)
One depth for every request is too shallow for code and JSON and too deep
for prose. With RADIANCE_DRAFT_ADAPT=1, --num-speculative-tokens becomes
the ceiling and each sequence drafts into the window d that maximises
E(d)/T(d): the tokens a step is expected to emit from the sequence's own
smoothed acceptance at each position, over the step's cost
(RADIANCE_DRAFT_COST, depth:ms pairs). The serial head runs as many rounds
as the deepest window in the step. A window moves only past a hysteresis
and a hold, since a new window is a new pass shape. Acceptance is
unchanged. /metrics gains radiance:draft_depth_adaptive and
radiance:draft_verify_depth_total{depth} while it is on. Unset, nothing
changes. Greedy at up to 6 against fixed 3: code +7.7 %, JSON +16.3 %.
This commit is contained in:
1 parent
73691a1189
commit
82da8eab69
11 files changed
+830
-10
No files matched your search
+254
-1
@@ -1,19 +1,185 @@
|
||||
/* draft.cpp -- the static draft-depth policy. See draft.h for why it is static.
|
||||
/* 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.
|
||||
*/
|
||||
#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. */
|
||||
constexpr double kCostSlope = 0.06;
|
||||
|
||||
/* 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
|
||||
* the half-life's, and from then on it is the moving average it is meant to be. Without it a
|
||||
* sequence would spend its first ~16 verifies at whatever window the prior chose, which is most of
|
||||
* 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);
|
||||
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;
|
||||
}
|
||||
|
||||
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)) {
|
||||
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);
|
||||
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;
|
||||
}
|
||||
}
|
||||
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);
|
||||
return RAD_OK;
|
||||
}
|
||||
|
||||
@@ -34,6 +200,93 @@ void DraftController::observe(int drafted, int accepted) {
|
||||
if (j < accepted) ++hit_[j];
|
||||
}
|
||||
if (drafted > seen_) seen_ = drafted;
|
||||
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; }
|
||||
}
|
||||
if (best) *best = bg;
|
||||
if (at_gain) *at_gain = ag;
|
||||
return arg;
|
||||
}
|
||||
|
||||
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];
|
||||
}
|
||||
|
||||
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. */
|
||||
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.n[j] = 0;
|
||||
}
|
||||
s.depth = best_depth(s.a, 0, nullptr, nullptr);
|
||||
s.held = 0;
|
||||
return s.depth;
|
||||
}
|
||||
|
||||
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 (drafted <= 0) return s.depth;
|
||||
const int D = p_.max_depth;
|
||||
if (drafted > D) drafted = D;
|
||||
if (accepted < 0) accepted = 0;
|
||||
if (accepted > drafted) accepted = drafted;
|
||||
|
||||
/* THE POSITIONS THIS VERIFY EVALUATED, by observe()'s rule: every one before the first
|
||||
* rejection, taken, and the rejected one, refused. A position behind the rejection was drafted
|
||||
* and never looked at, and says nothing either way. */
|
||||
const int last = accepted < drafted ? accepted : drafted - 1;
|
||||
for (int j = 0; j <= last; ++j) {
|
||||
const double w = std::fmax(alpha_, 1.0 / ((double)s.n[j] + kPriorWeight + 1.0));
|
||||
const double x = j < accepted ? 1.0 : 0.0;
|
||||
s.a[j] = (float)((double)s.a[j] + w * (x - (double)s.a[j]));
|
||||
if (s.n[j] < UINT16_MAX) ++s.n[j];
|
||||
}
|
||||
/* AND THE POSITIONS THE WINDOW DID NOT REACH DRIFT TOWARD THE DEEPEST ONE IT DID. Nothing is
|
||||
* 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. */
|
||||
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. */
|
||||
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)) {
|
||||
s.depth = d;
|
||||
s.held = 0;
|
||||
}
|
||||
return s.depth;
|
||||
}
|
||||
|
||||
} /* namespace rad */
|
||||
+90
-1
@@ -16,6 +16,29 @@
|
||||
*
|
||||
* 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
|
||||
* 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.
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* 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
|
||||
* window it verified. At temperature 0 that is the same text at every window, to exactly the
|
||||
* degree a speculative run matches a plain one at all: exact given the same logits, and a verify
|
||||
* row's logits are computed at an M that now moves with the window (accept.h says what that does
|
||||
* and does not change).
|
||||
*/
|
||||
#pragma once
|
||||
#include "rad_core.h"
|
||||
@@ -26,18 +49,74 @@ namespace rad {
|
||||
* band (spec §10), so 64 is the largest window any batch can carry. */
|
||||
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.
|
||||
*
|
||||
* `depth` 0 is "not started": the first ask fills the estimate from the prior and picks a window.
|
||||
* 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 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 */
|
||||
};
|
||||
|
||||
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 */
|
||||
};
|
||||
|
||||
int configure(const Params& p);
|
||||
const Params& params() const { return p_; }
|
||||
|
||||
/* The draft depth for the next step. 0 means "do not speculate". Constant by construction. */
|
||||
/* 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`;
|
||||
* 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. */
|
||||
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;
|
||||
/* 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;
|
||||
/* 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;
|
||||
}
|
||||
/* 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 {
|
||||
return (d >= 0 && d <= RAD_SCHED_MAX_SPEC) ? by_depth_[d] : 0;
|
||||
}
|
||||
|
||||
/* One verify's outcome: `drafted` tokens were proposed and a prefix of `accepted` of them was
|
||||
* taken. Feeds the reported metrics and nothing else. */
|
||||
void observe(int drafted, int accepted);
|
||||
@@ -76,6 +155,16 @@ private:
|
||||
int64_t reach_[RAD_SCHED_MAX_SPEC] = {};
|
||||
int64_t hit_ [RAD_SCHED_MAX_SPEC] = {};
|
||||
int seen_ = 0;
|
||||
|
||||
/* 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 */
|
||||
int64_t by_depth_[RAD_SCHED_MAX_SPEC + 1] = {};
|
||||
|
||||
/* 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;
|
||||
};
|
||||
|
||||
} /* namespace rad */
|
||||
@@ -137,6 +137,13 @@ 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
|
||||
* 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. */
|
||||
bool draft_adaptive = false;
|
||||
float draft_depth_adaptive = 0.0f;
|
||||
int64_t draft_verify_depth[RAD_METRICS_MAX_SPEC + 1] = {};
|
||||
};
|
||||
|
||||
} /* namespace rad */
|
||||
@@ -97,6 +97,8 @@ 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);
|
||||
RAD_TRY(draft_.configure(dp));
|
||||
|
||||
RAD_TRY(builder_.init(prog, cfg, geo_, kvgeom_, ranks));
|
||||
@@ -178,6 +180,9 @@ int Scheduler::add_locked(std::unique_ptr<Request> r) {
|
||||
s.card = -1;
|
||||
s.running = false;
|
||||
s.drafter_reset();
|
||||
/* A NEW REQUEST'S TEXT, so a new estimate: the slot's last tenant was writing something else. */
|
||||
s.adapt.depth = 0;
|
||||
s.adapt.held = 0;
|
||||
s.sched_tokens = 0;
|
||||
s.entry = -1;
|
||||
s.entry_next = -1;
|
||||
@@ -221,8 +226,15 @@ int Scheduler::set_draft(uint64_t id, const int32_t* tokens, int n) {
|
||||
|
||||
/* THE NEXT STEP ALREADY VERIFIES THESE. It was planned with the count the drafter issued, and
|
||||
* the card already put the tokens in its batch; the host copy is what that step's commit
|
||||
* emits the accepted ones from, so it takes exactly those, unclamped. */
|
||||
* emits the accepted ones from, so it takes exactly those, unclamped.
|
||||
*
|
||||
* UNDER THE ADAPTIVE WINDOW THE DRAFTER RAN AS MANY ROUNDS AS THE DEEPEST SEQUENCE WANTED,
|
||||
* and expect_draft() already cut this one's count to its own window -- the card verifies the
|
||||
* FIRST draft_next of the proposals, which are these. So a longer proposal is cut to that
|
||||
* count here, against the number the step was planned with: the window itself may have moved
|
||||
* at the commit in between, and the planned step is what these tokens are for. */
|
||||
if (next_open_ && s.entry_next >= 0) {
|
||||
if (draft_.adaptive() && n > s.draft_next) n = s.draft_next;
|
||||
if (n != s.draft_next) {
|
||||
RAD_ERR("request %llu: %d draft(s) published for a step planned to verify %d",
|
||||
(unsigned long long)id, n, s.draft_next);
|
||||
@@ -244,7 +256,7 @@ int Scheduler::set_draft(uint64_t id, const int32_t* tokens, int n) {
|
||||
return RAD_OK;
|
||||
}
|
||||
|
||||
int cap = draft_.next_depth();
|
||||
int cap = draft_cap(s);
|
||||
if (n > cap) n = cap;
|
||||
if (n > RAD_SCHED_MAX_SPEC) n = RAD_SCHED_MAX_SPEC;
|
||||
for (int i = 0; i < n; ++i) s.draft[i] = tokens[i];
|
||||
@@ -979,10 +991,15 @@ const RadBatch* Scheduler::step() {
|
||||
/* THE LAST GATE, and it has to be here rather than only at set_draft, because the
|
||||
* n-gram fallback below fills any proposal a drafter left empty -- so refusing a
|
||||
* draft upstream and stopping there hands the request a different draft instead of
|
||||
* none. Request::may_speculate says why a request can be ineligible. */
|
||||
* 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. */
|
||||
const int cap = draft_.adaptive() ? draft_.depth_for(s.adapt) : depth;
|
||||
if (!r.may_speculate) s.n_draft = 0;
|
||||
else if (s.n_draft == 0) s.n_draft = (int32_t)draft_ngram(s, depth);
|
||||
if (s.n_draft > depth) s.n_draft = depth;
|
||||
else if (s.n_draft == 0) s.n_draft = (int32_t)draft_ngram(s, cap);
|
||||
if (s.n_draft > cap) s.n_draft = cap;
|
||||
/* 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
|
||||
@@ -1340,7 +1357,15 @@ void Scheduler::expect_draft(uint64_t id, int n) {
|
||||
std::lock_guard<std::mutex> lk(mu_);
|
||||
auto it = by_id_.find(id);
|
||||
if (it == by_id_.end()) return;
|
||||
reqs_[(size_t)it->second]->draft_next = n > 0 ? n : 0;
|
||||
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. */
|
||||
if (draft_.adaptive() && n > 0) {
|
||||
const int cap = draft_.depth_for(s.adapt);
|
||||
if (n > cap) n = cap;
|
||||
}
|
||||
s.draft_next = n > 0 ? n : 0;
|
||||
}
|
||||
|
||||
const RadBatch* Scheduler::step_next() {
|
||||
@@ -1581,6 +1606,24 @@ 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()) {
|
||||
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) {
|
||||
char est[8 * RAD_SCHED_MAX_SPEC];
|
||||
int o = 0;
|
||||
for (int j = 0; j < draft_.next_depth() && o < (int)sizeof est - 8; ++j)
|
||||
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);
|
||||
}
|
||||
}
|
||||
m_.draft_tokens += e.n_spec;
|
||||
m_.draft_accepted += a;
|
||||
server::sink_drafts(r.sink, e.n_spec, a);
|
||||
@@ -1740,6 +1783,27 @@ int Scheduler::serial_begin(int* rounds, SerialRow* out, int max) {
|
||||
if (depth > 0 && *rounds > depth) *rounds = depth;
|
||||
if (*rounds > RAD_SCHED_MAX_SPEC) *rounds = RAD_SCHED_MAX_SPEC;
|
||||
|
||||
/* UNDER THE ADAPTIVE WINDOW, NO MORE ROUNDS THAN THE DEEPEST SEQUENCE WANTS. A round is one
|
||||
* pass of the head over every drafting row, so the step runs as many as its deepest window,
|
||||
* and a sequence that wants fewer verifies the first of them (expect_draft, set_draft); a
|
||||
* round past the deepest window is a pass whose proposal nobody verifies. Only a row that will
|
||||
* draft and be given its drafts counts. When no row does, the rounds stay as they are: round 0
|
||||
* is the history pass, and that still has to run. The reservation below is then made for the
|
||||
* rounds that will actually run, which is what it is for. */
|
||||
if (may_draft && draft_.adaptive()) {
|
||||
int want = 0;
|
||||
for (const StepEntry& e : plan_.e) {
|
||||
if (!e.produces_token || e.slot < 0 || (size_t)e.slot >= reqs_.size()) continue;
|
||||
SchedReq& s = *reqs_[(size_t)e.slot];
|
||||
if (!s.req || s.req->state != ReqState::Running || !s.running ||
|
||||
!s.req->may_speculate)
|
||||
continue;
|
||||
const int c = draft_.depth_for(s.adapt);
|
||||
if (c > want) want = c;
|
||||
}
|
||||
if (want > 0 && *rounds > want) *rounds = want;
|
||||
}
|
||||
|
||||
int n_draft_rows = 0;
|
||||
int64_t cu = 0; /* this entry's first token in the step just run */
|
||||
for (size_t i = 0; i < plan_.e.size(); ++i) {
|
||||
@@ -2293,6 +2357,19 @@ SchedMetrics Scheduler::metrics_locked() const {
|
||||
m.draft_pos_reached [j] = draft_.reached(j);
|
||||
m.draft_pos_accepted[j] = draft_.hits(j);
|
||||
}
|
||||
if (draft_.adaptive()) {
|
||||
m.draft_adaptive = true;
|
||||
/* Over the running sequences whose estimate has started; one that has not drafted yet
|
||||
* has no window to report. */
|
||||
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; }
|
||||
}
|
||||
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]);
|
||||
for (int d = 0; d < kDepth; ++d) m.draft_verify_depth[d] = draft_.verified_at(d);
|
||||
}
|
||||
return m;
|
||||
}
|
||||
|
||||
|
||||
+19
-2
@@ -109,6 +109,12 @@ struct SchedReq {
|
||||
draft_hi = 0;
|
||||
}
|
||||
|
||||
/* THE ADAPTIVE DRAFT WINDOW'S ESTIMATE FOR THIS REQUEST (RADIANCE_DRAFT_ADAPT, 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;
|
||||
|
||||
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 */
|
||||
/* Its index in the NEXT step, already issued while this one runs (step_next), or -1; and the
|
||||
@@ -280,7 +286,8 @@ public:
|
||||
* the one that is stuck. Returns how many were written. */
|
||||
int requests(ReqStat* out, int max) const;
|
||||
|
||||
/* The drafter's proposal for a request's next step. n is clamped to the controller's depth. */
|
||||
/* The drafter's proposal for a request's next step. n is clamped to the sequence's window
|
||||
* (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);
|
||||
|
||||
/* ---------------------------------------------------------- media the encoder owes
|
||||
@@ -318,7 +325,8 @@ public:
|
||||
* from one to the other.
|
||||
*
|
||||
* expect_draft after the drafter is issued: how many drafts it proposes for a sequence,
|
||||
* which is how many the next step verifies.
|
||||
* which is how many the next step verifies -- capped at the sequence's window,
|
||||
* which under the adaptive policy can be fewer than the rounds that ran.
|
||||
* step_next plans and builds that step: every row of this one, as a device row, at
|
||||
* the furthest context it can reach. Null -- and the engine plans with step()
|
||||
* after this one commits -- whenever the step needs a decision only a
|
||||
@@ -491,6 +499,15 @@ 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). 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();
|
||||
}
|
||||
|
||||
/* 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. */
|
||||
|
||||
@@ -280,6 +280,13 @@ public:
|
||||
o.draft_pos_reached [j] = m.draft_pos_reached [j];
|
||||
o.draft_pos_accepted[j] = m.draft_pos_accepted[j];
|
||||
}
|
||||
o.draft_adaptive = m.draft_adaptive;
|
||||
o.draft_depth_adaptive = m.draft_depth_adaptive;
|
||||
{
|
||||
const int ka = (int)(sizeof m.draft_verify_depth / sizeof m.draft_verify_depth[0]);
|
||||
const int kb = (int)(sizeof o.draft_verify_depth / sizeof o.draft_verify_depth[0]);
|
||||
for (int d = 0; d < ka && d < kb; ++d) o.draft_verify_depth[d] = m.draft_verify_depth[d];
|
||||
}
|
||||
o.preemptions = m.preemptions;
|
||||
o.total_requests = m.admitted;
|
||||
o.steps = m.steps;
|
||||
|
||||
@@ -114,6 +114,11 @@ struct SchedMetrics {
|
||||
int draft_depth_seen = 0;
|
||||
int64_t draft_pos_reached [MAX_DRAFT_POS] = {};
|
||||
int64_t draft_pos_accepted[MAX_DRAFT_POS] = {};
|
||||
/* THE ADAPTIVE DRAFT WINDOW (RADIANCE_DRAFT_ADAPT), all zero while it is off: the mean window
|
||||
* of the running sequences, and the verifies by the window they carried (index = drafts). */
|
||||
bool draft_adaptive = false;
|
||||
double draft_depth_adaptive = 0.0;
|
||||
int64_t draft_verify_depth[MAX_DRAFT_POS + 1] = {};
|
||||
int64_t preemptions = 0;
|
||||
int64_t total_requests = 0;
|
||||
/* ENGINE STEPS. One forward pass of the model plus whatever draft rounds followed it, which
|
||||
|
||||
@@ -145,6 +145,17 @@ void Metrics::sample(const SchedMetrics& m) {
|
||||
/* Published after the gauges it bounds, so a concurrent render never lists a position whose
|
||||
* gauge has not been written yet. */
|
||||
draft_pos_seen_.store(seen, std::memory_order_release);
|
||||
if (m.draft_adaptive) {
|
||||
g_draft_depth_adapt_.set(m.draft_depth_adaptive);
|
||||
int top = 0;
|
||||
for (int d = 1; d <= (int)MAX_DRAFT_POS; ++d) {
|
||||
g_draft_vdepth_[d].set((double)m.draft_verify_depth[d]);
|
||||
if (m.draft_verify_depth[d] > 0) top = d;
|
||||
}
|
||||
/* After the gauges, for the reason above. */
|
||||
draft_vdepth_top_.store(top, std::memory_order_release);
|
||||
draft_adapt_on_.store(true, std::memory_order_release);
|
||||
}
|
||||
c_preemptions_.inc(std::max(0.0, (double)m.preemptions - c_preemptions_.get()));
|
||||
c_steps_.inc(std::max(0.0, (double)m.steps - c_steps_.get()));
|
||||
c_steps_prefill_.inc(std::max(0.0, (double)m.steps_prefill - c_steps_prefill_.get()));
|
||||
@@ -398,6 +409,24 @@ std::string Metrics::render() const {
|
||||
w.out += "radiance:draft_position_reached_total" + base + ",position=\"" +
|
||||
std::to_string(j) + "\"} " + num(g_draft_pos_reached_[j].get()) + '\n';
|
||||
}
|
||||
/* THE ADAPTIVE DRAFT WINDOW, only while RADIANCE_DRAFT_ADAPT is on: where the policy stands --
|
||||
* at one sequence, that sequence's window -- and where it has been, as verifies by the
|
||||
* number of drafts they carried. */
|
||||
if (draft_adapt_on_.load(std::memory_order_acquire)) {
|
||||
w.gauge("radiance:draft_depth_adaptive",
|
||||
"Mean draft window the adaptive policy gives the running sequences.",
|
||||
L, g_draft_depth_adapt_.get());
|
||||
const int top = draft_vdepth_top_.load(std::memory_order_acquire);
|
||||
if (top > 0) {
|
||||
const std::string base = L.substr(0, L.size() - 1);
|
||||
w.help("radiance:draft_verify_depth_total",
|
||||
"Verifies by the number of drafts they carried, under the adaptive window.",
|
||||
"counter");
|
||||
for (int d = 1; d <= top; ++d)
|
||||
w.out += "radiance:draft_verify_depth_total" + base + ",depth=\"" +
|
||||
std::to_string(d) + "\"} " + num(g_draft_vdepth_[d].get()) + '\n';
|
||||
}
|
||||
}
|
||||
w.gauge("radiance:num_requests_preempted",
|
||||
"Requests currently preempted and awaiting recompute.", L, g_preempted_.get());
|
||||
w.gauge("radiance:http_queue_depth",
|
||||
|
||||
@@ -122,6 +122,13 @@ private:
|
||||
Gauge g_draft_pos_[MAX_DRAFT_POS];
|
||||
Gauge g_draft_pos_reached_[MAX_DRAFT_POS];
|
||||
std::atomic<int> draft_pos_seen_{0};
|
||||
/* THE ADAPTIVE DRAFT WINDOW (RADIANCE_DRAFT_ADAPT): the mean window of the running sequences,
|
||||
* and the verifies by the window they carried, index = drafts. Rendered only while the policy
|
||||
* is on, so a deployment without it exposes exactly what it always did. */
|
||||
Gauge g_draft_depth_adapt_;
|
||||
Gauge g_draft_vdepth_[MAX_DRAFT_POS + 1];
|
||||
std::atomic<bool> draft_adapt_on_{false};
|
||||
std::atomic<int> draft_vdepth_top_{0}; /* the deepest window with a verify counted */
|
||||
|
||||
/* sample() turns the scheduler's cumulative totals into counter increments by reading each
|
||||
* counter and adding the difference. Two scrapes doing that at once both read the old value
|
||||
|
||||
@@ -1604,6 +1604,303 @@ TEST(the_draft_depth_is_the_configured_one_whatever_the_acceptance_trace) {
|
||||
CHECK_EQ(d3.next_depth(), 0);
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------ the adaptive window
|
||||
* RADIANCE_DRAFT_ADAPT (draft.h). The switch is read from the environment once per process, so
|
||||
* these cases turn the policy on through Params instead -- the controller cannot tell the two
|
||||
* apart, and the tests do not depend on what the suite was started with. */
|
||||
|
||||
/* A small deterministic coin, the one the static case above uses. */
|
||||
namespace {
|
||||
struct Coin {
|
||||
uint64_t s = 12345;
|
||||
bool operator()(double pr) {
|
||||
s = s * 6364136223846793005ull + 1442695040888963407ull;
|
||||
return ((double)((s >> 33) & 0xFFFFFF) / (double)0x1000000) < pr;
|
||||
}
|
||||
};
|
||||
} /* namespace */
|
||||
|
||||
TEST(the_adaptive_window_follows_each_sequences_own_acceptance) {
|
||||
DraftController c;
|
||||
DraftController::Params p;
|
||||
p.max_depth = 6;
|
||||
p.adapt = true;
|
||||
CHECK_OK(c.configure(p));
|
||||
CHECK(c.adaptive());
|
||||
CHECK_EQ(c.next_depth(), 6); /* the ceiling is still the flag */
|
||||
|
||||
/* Nothing measured yet: both start from the prior, which on the default cost wants it all. */
|
||||
DraftAdapt code, summary;
|
||||
CHECK_EQ(c.depth_for(code), 6);
|
||||
CHECK_EQ(c.depth_for(summary), 6);
|
||||
|
||||
/* Two texts through one controller. The drafter almost always agrees on one; on the other it
|
||||
* agrees at the first position and rarely after it. Each window follows its own text. */
|
||||
Coin coin;
|
||||
for (int t = 0; t < 600; ++t) {
|
||||
int d = c.depth_for(code), a = 0;
|
||||
while (a < d && coin(0.95)) ++a;
|
||||
c.observe(d, a);
|
||||
c.adapt_observe(code, d, a);
|
||||
|
||||
d = c.depth_for(summary);
|
||||
a = 0;
|
||||
while (a < d && coin(a == 0 ? 0.6 : 0.2)) ++a;
|
||||
c.observe(d, a);
|
||||
c.adapt_observe(summary, d, a);
|
||||
}
|
||||
CHECK_EQ(c.depth_for(code), 6);
|
||||
CHECK(c.depth_for(summary) >= 1);
|
||||
CHECK(c.depth_for(summary) <= 2);
|
||||
CHECK(code.a[0] > 0.85f);
|
||||
CHECK_NEAR(summary.a[0], 0.6, 0.2);
|
||||
/* The estimate is the conditional rate, and the global hazard is still counted. */
|
||||
CHECK(c.reached(0) == 1200);
|
||||
/* The windows the verifies carried are counted while the policy is on: 6 throughout for one,
|
||||
* and 6 until the other moved off it. */
|
||||
CHECK(c.verified_at(6) >= 600);
|
||||
CHECK(c.verified_at(1) + c.verified_at(2) > 0);
|
||||
CHECK_EQ(c.verified_at(1) + c.verified_at(2) + c.verified_at(3) + c.verified_at(4) +
|
||||
c.verified_at(5) + c.verified_at(6), (int64_t)1200);
|
||||
|
||||
/* And a window that went shallow comes back when the text changes: the positions it stopped
|
||||
* drafting drift toward the deepest it still drafts, so a text the drafter predicts again is
|
||||
* found without anyone drafting deep first. */
|
||||
for (int t = 0; t < 600; ++t) {
|
||||
const int d = c.depth_for(summary);
|
||||
int a = 0;
|
||||
while (a < d && coin(0.95)) ++a;
|
||||
c.adapt_observe(summary, d, a);
|
||||
}
|
||||
CHECK_EQ(c.depth_for(summary), 6);
|
||||
}
|
||||
|
||||
/* A MOVE IS RATE-LIMITED: not within `hold` verifies of the last one, and only for a window worth
|
||||
* `hyst` more than the one it leaves. A window that followed every flicker of its estimate would
|
||||
* re-record its passes for nothing (runtime/ctx.h). */
|
||||
TEST(the_adaptive_window_moves_only_past_its_hold_and_its_hysteresis) {
|
||||
DraftController c;
|
||||
DraftController::Params p;
|
||||
p.max_depth = 6;
|
||||
p.adapt = true;
|
||||
p.hold = 50;
|
||||
CHECK_OK(c.configure(p));
|
||||
DraftAdapt s;
|
||||
CHECK_EQ(c.depth_for(s), 6);
|
||||
for (int t = 0; t < 49; ++t) CHECK_EQ(c.adapt_observe(s, 6, 0), 6); /* held */
|
||||
CHECK(c.adapt_observe(s, 6, 0) < 6); /* the 50th moves */
|
||||
|
||||
/* The same trace with a hysteresis no estimate can clear never moves at all. */
|
||||
DraftController h;
|
||||
p.hold = 0;
|
||||
p.hyst = 1000.0f;
|
||||
CHECK_OK(h.configure(p));
|
||||
DraftAdapt u;
|
||||
CHECK_EQ(h.depth_for(u), 6);
|
||||
for (int t = 0; t < 200; ++t) h.adapt_observe(u, 6, 0);
|
||||
CHECK_EQ(u.depth, 6);
|
||||
|
||||
/* The floor holds whatever the estimate says. */
|
||||
DraftController m;
|
||||
p.hyst = 0.0f;
|
||||
p.min_depth = 2;
|
||||
CHECK_OK(m.configure(p));
|
||||
DraftAdapt v;
|
||||
for (int t = 0; t < 200; ++t) m.adapt_observe(v, m.depth_for(v), 0);
|
||||
CHECK_EQ(m.depth_for(v), 2);
|
||||
|
||||
/* Off, the window is the flag and an observation is not taken. */
|
||||
DraftController off;
|
||||
DraftController::Params q;
|
||||
q.max_depth = 5;
|
||||
CHECK_OK(off.configure(q));
|
||||
CHECK(!off.adaptive());
|
||||
DraftAdapt w;
|
||||
CHECK_EQ(off.depth_for(w), 5);
|
||||
CHECK_EQ(off.adapt_observe(w, 5, 0), 5);
|
||||
CHECK_EQ(w.depth, 0);
|
||||
off.observe(5, 2);
|
||||
CHECK_EQ(off.verified_at(5), (int64_t)0);
|
||||
|
||||
/* Nonsense knobs are refused rather than run. */
|
||||
DraftController bad;
|
||||
DraftController::Params r;
|
||||
r.max_depth = 4;
|
||||
r.adapt = true;
|
||||
r.half_life = 0.0f;
|
||||
CHECK(bad.configure(r) < 0);
|
||||
}
|
||||
|
||||
/* THE COST TABLE: measured points as they are, lines between them, the nearest two past the ends;
|
||||
* and with fewer than two points, the default 1 + 0.06 d. A dear depth is a shallower window at
|
||||
* the same estimate. */
|
||||
TEST(the_adaptive_window_prices_a_step_from_the_cost_table) {
|
||||
DraftController c;
|
||||
DraftController::Params p;
|
||||
p.max_depth = 6;
|
||||
p.adapt = true;
|
||||
p.cost[2] = 11.3f;
|
||||
p.cost[3] = 12.0f;
|
||||
CHECK_OK(c.configure(p));
|
||||
CHECK_NEAR(c.cost(2), 11.3, 1e-4);
|
||||
CHECK_NEAR(c.cost(3), 12.0, 1e-4);
|
||||
CHECK_NEAR(c.cost(1), 10.6, 1e-4); /* extrapolated below */
|
||||
CHECK_NEAR(c.cost(6), 14.1, 1e-4); /* and above */
|
||||
|
||||
DraftController d;
|
||||
DraftController::Params q;
|
||||
q.max_depth = 6;
|
||||
q.adapt = true;
|
||||
q.cost[1] = 1.0f;
|
||||
q.cost[2] = 10.0f; /* a second draft costs nine times the first */
|
||||
CHECK_OK(d.configure(q));
|
||||
DraftAdapt s;
|
||||
CHECK_EQ(d.depth_for(s), 1);
|
||||
|
||||
DraftController e;
|
||||
DraftController::Params r;
|
||||
r.max_depth = 4;
|
||||
r.adapt = true;
|
||||
r.cost[3] = 5.0f; /* one point is no table */
|
||||
CHECK_OK(e.configure(r));
|
||||
CHECK_NEAR(e.cost(1), 1.06, 1e-9);
|
||||
CHECK_NEAR(e.cost(4), 1.24, 1e-9);
|
||||
|
||||
/* A table whose line runs below zero is the wrong sign, and the default is used. */
|
||||
DraftController g;
|
||||
DraftController::Params t;
|
||||
t.max_depth = 6;
|
||||
t.adapt = true;
|
||||
t.cost[1] = 10.0f;
|
||||
t.cost[2] = 4.0f;
|
||||
CHECK_OK(g.configure(t));
|
||||
CHECK_NEAR(g.cost(6), 1.36, 1e-9);
|
||||
}
|
||||
|
||||
/* THE SCHEDULER'S HALF: the cap on a proposal, and the rounds the head runs. One sequence whose
|
||||
* prior pays for two drafts of a ceiling of four verifies two, and the head runs two rounds. */
|
||||
TEST(the_adaptive_window_caps_the_proposal_and_the_rounds) {
|
||||
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.prior = 0.3f; /* on the default cost: two */
|
||||
CHECK_OK(f.sch.draft().configure(p));
|
||||
|
||||
const uint64_t a = f.submit(160, 64);
|
||||
CHECK(f.sch.step() != nullptr);
|
||||
f.commit_plain(11);
|
||||
|
||||
int32_t draft[4] = { 21, 22, 23, 24 };
|
||||
CHECK_OK(f.sch.set_draft(a, draft, 4));
|
||||
CHECK_EQ(f.sch.find(a)->n_draft, 2);
|
||||
const RadBatch* b = f.sch.step();
|
||||
REQUIRE(b != nullptr);
|
||||
CHECK_EQ(b->n_spec, 2);
|
||||
CHECK_EQ(b->n_tok, 3);
|
||||
CHECK_EQ(f.sch.plan().e[0].n_spec, 2);
|
||||
|
||||
int rounds = 4;
|
||||
Scheduler::SerialRow rows[4];
|
||||
CHECK_EQ(f.sch.serial_begin(&rounds, rows, 4), 1);
|
||||
CHECK_EQ(rounds, 2);
|
||||
const RadBatch* hb = f.sch.serial_batch(0, 1);
|
||||
REQUIRE(hb != nullptr);
|
||||
CHECK_EQ(hb->n_tok, 3); /* every verified position, as ever */
|
||||
CHECK(f.sch.serial_batch(2, 1) != nullptr);
|
||||
f.sch.serial_end(2);
|
||||
|
||||
const SchedMetrics m = f.sch.metrics();
|
||||
CHECK(m.draft_adaptive);
|
||||
CHECK_NEAR(m.draft_depth_adaptive, 2.0, 1e-6);
|
||||
CHECK_EQ(m.draft_depth, 4); /* the ceiling, as before */
|
||||
}
|
||||
|
||||
/* EACH SEQUENCE ITS OWN WINDOW, AND THE ROUNDS FOLLOW THE DEEPEST. The step issued ahead of a
|
||||
* commit verifies, for each sequence, the first of those rounds up to its own window -- and the
|
||||
* drafter's longer proposal is taken cut to that count rather than refused as a mismatch. */
|
||||
TEST(the_adaptive_window_is_per_sequence_and_the_rounds_follow_the_deepest) {
|
||||
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.hold = 0;
|
||||
p.hyst = 0.0f;
|
||||
CHECK_OK(f.sch.draft().configure(p));
|
||||
|
||||
const uint64_t a = f.submit(160, 2000);
|
||||
const uint64_t b = f.submit(160, 2000);
|
||||
CHECK(f.sch.step() != nullptr);
|
||||
f.commit_plain(11);
|
||||
|
||||
/* `a`'s drafts are never taken and `b`'s always are. */
|
||||
int32_t draft[4] = { 21, 22, 23, 24 };
|
||||
auto commit_ab = [&]() {
|
||||
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].seq == b ? pl.e[i].n_spec : 0;
|
||||
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));
|
||||
};
|
||||
for (int t = 0; t < 64; ++t) {
|
||||
CHECK_OK(f.sch.set_draft(a, draft, 4));
|
||||
CHECK_OK(f.sch.set_draft(b, draft, 4));
|
||||
REQUIRE(f.sch.step() != nullptr);
|
||||
commit_ab();
|
||||
}
|
||||
|
||||
CHECK_OK(f.sch.set_draft(a, draft, 4));
|
||||
CHECK_OK(f.sch.set_draft(b, draft, 4));
|
||||
const RadBatch* s = f.sch.step();
|
||||
REQUIRE(s != nullptr);
|
||||
CHECK_EQ(s->n_spec, 4);
|
||||
CHECK_EQ(s->n_tok, 2 + 5);
|
||||
for (const StepEntry& e : f.sch.plan().e) CHECK_EQ(e.n_spec, e.seq == a ? 1 : 4);
|
||||
CHECK_NEAR(f.sch.metrics().draft_depth_adaptive, 2.5, 1e-6);
|
||||
|
||||
/* The head runs for the deepest. */
|
||||
int rounds = 4;
|
||||
Scheduler::SerialRow rows[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);
|
||||
|
||||
/* The step issued ahead: `a` verifies one of the four rounds, `b` all four. */
|
||||
f.sch.find(a)->speculation_fixed = true;
|
||||
f.sch.find(b)->speculation_fixed = true;
|
||||
f.sch.expect_draft(a, rounds);
|
||||
f.sch.expect_draft(b, rounds);
|
||||
const RadBatch* nb = f.sch.step_next();
|
||||
REQUIRE(nb != nullptr);
|
||||
CHECK_EQ(nb->n_spec, 4);
|
||||
CHECK_EQ(nb->n_tok, 2 + 5);
|
||||
|
||||
/* Its commit, the watermark, and the proposals published for the step already on the card:
|
||||
* four rounds' worth to both, and each keeps what its step was planned to verify. */
|
||||
commit_ab();
|
||||
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, 1);
|
||||
CHECK_EQ(f.sch.find(b)->n_draft, 4);
|
||||
f.sch.promote_next();
|
||||
for (const StepEntry& e : f.sch.plan().e) CHECK_EQ(e.n_spec, e.seq == a ? 1 : 4);
|
||||
CHECK(f.sch.metrics().draft_verify_depth[1] > 0);
|
||||
CHECK(f.sch.metrics().draft_verify_depth[4] > 0);
|
||||
}
|
||||
|
||||
/* ================================================================== the step path */
|
||||
TEST(the_step_path_does_not_allocate) {
|
||||
Fixture f;
|
||||
|
||||
@@ -2444,6 +2444,38 @@ TEST(concurrent_scrapes_count_each_total_once) {
|
||||
CHECK_EQ(value("radiance:prefill_tokens_total"), 5000.0);
|
||||
}
|
||||
|
||||
/* THE ADAPTIVE DRAFT WINDOW (RADIANCE_DRAFT_ADAPT) IS EXPOSED ONLY WHILE IT IS ON, so a deployment
|
||||
* without it scrapes exactly what it always did -- and when it is on, the gauge and the histogram
|
||||
* are well-formed families with the labels every other sample carries. */
|
||||
TEST(the_adaptive_draft_window_is_exposed_only_while_it_is_on) {
|
||||
Metrics m("m", 1024);
|
||||
server::SchedMetrics s;
|
||||
m.sample(s);
|
||||
std::string body = m.render();
|
||||
CHECK(body.find("radiance:draft_depth_adaptive") == std::string::npos);
|
||||
CHECK(body.find("radiance:draft_verify_depth_total") == std::string::npos);
|
||||
|
||||
s.draft_adaptive = true;
|
||||
s.draft_depth_adaptive = 2.5;
|
||||
s.draft_verify_depth[2] = 7;
|
||||
s.draft_verify_depth[5] = 3;
|
||||
m.sample(s);
|
||||
PromDoc d = parse_prom(m.render());
|
||||
CHECK_EQ((int)d.errors.size(), 0);
|
||||
CHECK_EQ(d.type["radiance:draft_depth_adaptive"], std::string("gauge"));
|
||||
CHECK_EQ(d.type["radiance:draft_verify_depth_total"], std::string("counter"));
|
||||
auto value = [&d](const std::string& name) {
|
||||
auto it = d.samples.find(name);
|
||||
return it == d.samples.end() ? -1.0 : it->second;
|
||||
};
|
||||
const std::string L = "{model_name=\"m\",engine=\"0\"";
|
||||
CHECK_NEAR(value("radiance:draft_depth_adaptive" + L + "}"), 2.5, 1e-9);
|
||||
CHECK_NEAR(value("radiance:draft_verify_depth_total" + L + ",depth=\"1\"}"), 0.0, 1e-9);
|
||||
CHECK_NEAR(value("radiance:draft_verify_depth_total" + L + ",depth=\"2\"}"), 7.0, 1e-9);
|
||||
CHECK_NEAR(value("radiance:draft_verify_depth_total" + L + ",depth=\"5\"}"), 3.0, 1e-9);
|
||||
CHECK_NEAR(value("radiance:draft_verify_depth_total" + L + ",depth=\"6\"}"), -1.0, 1e-9);
|
||||
}
|
||||
|
||||
TEST(models_and_health_answer) {
|
||||
Fixture f;
|
||||
HttpResponse r = f.get("/v1/models");
|
||||
|
||||
Reference in new issue
Block a user