Archived
Radiance becomes a multi-model engine: one process serves several models behind one port, loads each when a request names it, parks it in host memory or restores it from its container, and shares the cards between them (docs/MULTIMODEL.md). The configuration is one YAML file; the command line flags are gone (`radiance --config FILE`, hot reload on SIGHUP). Formats. IQ8R (rotated int8) and IQR-V4 replace FP8 and MXFP4 as whole-model formats, with an int8 KV cache (`kv-cache-dtype: i8`) in place of the fp8 one. Containers whose weights are block FP8, MXFP4, M2XFP4, NVFP4 or ParoQuant are refused at startup by the name of the first such weight. Not served until they have int8 containers: GLM-5.3-Flash, Ornith-1.5, Sage-A1.5, Nemotron-3.5-Bolt-74B, Ling-3.0-flash, drama-3 and Anima's FP8 container; Nemotron-3.5-Bolt-E4B, the Gemma 4 31B DFlash containers and MiniCPM5-2B bf16 serve without their drafter. 1.17.0 serves all of them. Speed. Chained launches for Qwen3.8-27B, Gemma 4, Gemma 3 and the small bf16 models, a new Qwen3.8-Flash-Next container (IQR-V4 experts, int8 trunk) with its decode step and prefill reworked, and a reworked adaptive draft window. Deployment. deploy/compose has two fleet files, tp1.yaml and tp2.yaml, each with its configuration beside it, in place of the per-model files. `host-pool-mib` is a model's host pool in all, split among its ranks. The dashboard is rebuilt: an hour of history kept by the server, each accelerator's memory by model, model states over time. Merged since 1.17.0: Nemotron-3.5-Bolt-74B-A9B by keptn42 (#339); GLM-5.3-Flash on four cards by AnimusNull (#397); chained launches for Flash-Next's decode step (#396) and preempted requests keeping their prefix and place (#405) by mici81; --prefill-interleave auto by Code000 (#410); eight more libr3d kernels and the w4a4 reference by RikshaDriver (#403, #407, #408); libr3d large-BAR and E4M3 decode fixes by neuromanMD (#438, #439); int4/int5 decode tiles by Kezi (#426); attention for Nanbeige 4.3 33B by everplays (#417); expert_stats_report.py by baKon (#431). Fixed: #398, #402, #419, #437 (dschmitz's patch); added the metrics of #392, and multi-model serving as asked for in #367 and #109. Plugin ABI 39: out-of-tree plugins need to be rebuilt. Thanks to @keptn42, @AnimusNull, @mici81, @Code000, @RikshaDriver, @neuromanMD, @Kezi, @everplays, @baKon and @dschmitz.
237 lines
8.8 KiB
C++
237 lines
8.8 KiB
C++
/* draft.cpp -- the static draft-depth policy and the adaptive one (`--draft-window adaptive`).
|
|
* See draft.h.
|
|
*/
|
|
#include "draft.h"
|
|
|
|
#include <cmath>
|
|
#include <cstdint>
|
|
#include <cstdlib>
|
|
|
|
namespace rad {
|
|
|
|
namespace {
|
|
|
|
/* The prior weighs as much as this many verifies. The (n+1)-th verify moves an estimate by
|
|
* 1 / (n + k + 1), a running mean over prior and evidence, until that falls below the half-life's
|
|
* step and the moving average takes over. Without it a sequence would spend its first ~16
|
|
* verifies at the prior's window, most of a short answer. */
|
|
constexpr double kPriorWeight = 4.0;
|
|
|
|
/* The weight of the (n+1)-th sample in an estimate that is a running mean until 1 / (n + 1)
|
|
* falls below a moving average's step at half-life `h` samples, and that average after. */
|
|
double settle_weight(double h, int64_t n) {
|
|
return std::fmax(1.0 - std::exp2(-1.0 / h), 1.0 / (double)(n + 1));
|
|
}
|
|
|
|
} /* namespace */
|
|
|
|
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;
|
|
}
|
|
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_ = 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;
|
|
cost_.reset(kRowCost);
|
|
if (adapt_)
|
|
RAD_INFO("draft depth: adaptive, %d..%d; a sequence stops at the first draft emitted less "
|
|
"than %.0f%% of the time, and the depth moves at +%.0f%% at most every %d steps",
|
|
kMinDepth, p_.max_depth, 100.0 * kDraftMinChance, 100.0 * kHyst, kHold);
|
|
return RAD_OK;
|
|
}
|
|
|
|
void DraftController::observe(int drafted, int accepted) {
|
|
if (drafted <= 0) return;
|
|
if (drafted > RAD_SCHED_MAX_SPEC) drafted = RAD_SCHED_MAX_SPEC;
|
|
if (accepted < 0) accepted = 0;
|
|
if (accepted > drafted) accepted = drafted;
|
|
tot_drafted_ += drafted;
|
|
tot_accepted_ += accepted;
|
|
|
|
/* Position j is evaluated only if every earlier position was accepted: 0..accepted-1 are
|
|
* evaluated and accepted, position `accepted` (if drafted) evaluated and rejected, and nothing
|
|
* past it is reached. */
|
|
const int last = accepted < drafted ? accepted : drafted - 1;
|
|
for (int j = 0; j <= last; ++j) {
|
|
++reach_[j];
|
|
if (j < accepted) ++hit_[j];
|
|
}
|
|
if (drafted > seen_) seen_ = drafted;
|
|
if (adapt_) ++by_depth_[drafted];
|
|
}
|
|
|
|
/* ------------------------------------------------------------------ the step's cost */
|
|
void DraftController::step_time(int q_rows, double ms) {
|
|
if (!adapt_) return;
|
|
cost_.sample(q_rows, ms);
|
|
}
|
|
|
|
int64_t DraftController::cost_samples(int q_rows) const {
|
|
return q_rows >= 1 ? cost_.samples(q_rows) : 0;
|
|
}
|
|
|
|
double DraftController::step_cost(int q_rows) const { return cost_.cost(q_rows); }
|
|
|
|
/* ------------------------------------------------------------------ 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;
|
|
}
|
|
|
|
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 {
|
|
/* From finished sequences once there are enough (adapt_retire), else the fixed kPrior. */
|
|
for (int j = 0; j < p_.max_depth; ++j) {
|
|
s.a[j] = (float)prior_at(j);
|
|
s.n[j] = 0;
|
|
}
|
|
s.depth = stop_of(s.a);
|
|
s.held = 0;
|
|
}
|
|
|
|
void DraftController::adapt_retire(const DraftAdapt& s) {
|
|
if (!adapt_ || s.depth <= 0 || (double)s.n[0] < kHalfLife) return;
|
|
const double w = settle_weight(kPriorHalfLifeSeqs, prior_n_);
|
|
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) start(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. Positions past the first
|
|
* rejection were never evaluated and are not updated here. */
|
|
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];
|
|
}
|
|
/* Positions past the window drift toward its deepest one. Otherwise a sequence held shallow
|
|
* by prose that turns into code would keep its stale deep estimates and never draft deep
|
|
* enough to update them. Assumes acceptance decays smoothly with position; when wrong it costs
|
|
* a move out and 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 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 < 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;
|
|
|
|
/* Expected tokens at each head depth, sum_i E_i(min(D, stop_i)) plus one per non-drafting
|
|
* row, and the drafts the step would then verify, sum_i min(D, stop_i). Candidates end at the
|
|
* deepest stop, since a deeper D changes nothing. */
|
|
double tok[RAD_SCHED_MAX_SPEC + 1] = {};
|
|
int drafts[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;
|
|
drafts[d] += d <= s.depth ? d : s.depth;
|
|
}
|
|
}
|
|
const double plain = (double)(n_rows - n_seq);
|
|
if (top > M) top = M;
|
|
|
|
/* A step's query rows at depth d: a row a decode sequence and its drafts. */
|
|
auto gain = [&](int d) { return (tok[d] + plain) / step_cost(n_rows + drafts[d]); };
|
|
int best = kMinDepth;
|
|
double best_g = -1.0;
|
|
for (int d = kMinDepth; d <= top; ++d) {
|
|
const double g = gain(d);
|
|
if (g > best_g) { best_g = g; best = d; }
|
|
}
|
|
|
|
/* Move only after kHold steps and only for kHyst more yield than the current depth. The
|
|
* first drafting step 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 = gain(cur);
|
|
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 */
|