Archived
spec: --draft-depth adaptive stops the MTP chain inside the step
A coupled serial chain stops as soon as the product of the head's own probabilities along it falls under --draft-depth-stop (default 0.85; top or path statistic, at most --draft-depth-max rounds, at least --draft-depth-min), and the next verify carries exactly the drafts made. The decision trails the chain by --draft-depth-lag rounds (default 1), so the host's readback of one state row and its issue of the next round hide behind the running round. The rule reads the draft's laws and the tokens drawn before the one a decision admits, never the target and never the admitted token, so the row-wise and the traversal verify stay exact; mtp_rrs_test checks it by chi-square with two rules that must fail (one reads the admitted draft's own mass, one the target). It excludes --draft-window adaptive, which picks the next step's window from earlier acceptance and step cost and so is a second controller on the same chain. The recorded-pass cache holds 48 passes instead of 24: every row count is its own pass shape and the adaptive chain makes more of them than 24 hold. On Flash-Next IQR-V4 (TP2) on our branch, against chain 3 with coupling and traversal: +3.0% tok/s [+1.0, +5.0] over 117 paired requests; the 48-entry cache is +1.7% [+1.1, +2.3] of that for this mode. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
d0fd4aa1bb
commit
e78d484e60
13 files changed
+1054
-4
No files matched your search
@@ -223,6 +223,21 @@ static const Flag g_flags[] = {
|
||||
" root-to-leaf path bottom-up (Traversal Verification, arXiv\n"
|
||||
" 2505.12398; needs --draft-coupling on). The same output law in\n"
|
||||
" every mode, more drafts accepted per step in block and traversal" },
|
||||
{ "--draft-depth", "MODE", "fixed (default) | adaptive -- a coupled serial chain stops within\n"
|
||||
" the step once the product of the head's probabilities along it falls\n"
|
||||
" below --draft-depth-stop, and the next verify carries only the drafts\n"
|
||||
" made (1 .. --draft-depth-max). Same output law (the rule reads the\n"
|
||||
" draft's own laws only); needs --draft-coupling on and --draft-window\n"
|
||||
" fixed (both choose the depth, so they are not served together)" },
|
||||
{ "--draft-depth-max", "N", "--draft-depth adaptive: the deepest chain (default 0 =\n"
|
||||
"num-speculative-tokens)" },
|
||||
{ "--draft-depth-min", "N", "--draft-depth adaptive: rounds that always run (default 1)" },
|
||||
{ "--draft-depth-stop", "P", "--draft-depth adaptive: the chain goes on while its confidence\n"
|
||||
"is at least P (default 0.85)" },
|
||||
{ "--draft-depth-lag", "K", "--draft-depth adaptive: rounds the decision trails the chain,\n"
|
||||
"0 or 1 (default 1: the host decides while the next round runs)" },
|
||||
{ "--draft-depth-stat", "S", "--draft-depth adaptive: top (default, each round's largest\n"
|
||||
"probability) | path (the probability of the token it drew)" },
|
||||
{ "--draft-model", "PATH", "NOT IMPLEMENTED -- merge the drafter at convert time" },
|
||||
|
||||
{ "--host", "ADDR", "listen address" },
|
||||
@@ -924,6 +939,37 @@ int config_parse(int argc, char** argv, Config* c) {
|
||||
return RAD_E_INVAL;
|
||||
}
|
||||
}
|
||||
else if (S("--draft-depth")) {
|
||||
if (!(v = need(i))) return RAD_E_INVAL;
|
||||
if (!std::strcmp(v, "fixed")) c->draft_depth_adaptive = false;
|
||||
else if (!std::strcmp(v, "adaptive")) c->draft_depth_adaptive = true;
|
||||
else {
|
||||
RAD_ERR("--draft-depth takes fixed or adaptive, not '%s'", v);
|
||||
return RAD_E_INVAL;
|
||||
}
|
||||
}
|
||||
else if (S("--draft-depth-max")) { if (!num(0, 16, &c->draft_depth_max)) return RAD_E_INVAL; }
|
||||
else if (S("--draft-depth-min")) { if (!num(1, 16, &c->draft_depth_min)) return RAD_E_INVAL; }
|
||||
else if (S("--draft-depth-lag")) { if (!num(0, 1, &c->draft_depth_lag)) return RAD_E_INVAL; }
|
||||
else if (S("--draft-depth-stop")) {
|
||||
if (!(v = need(i))) return RAD_E_INVAL;
|
||||
double d = 0;
|
||||
if (!parse_num(a.c_str(), v, &d)) return RAD_E_INVAL;
|
||||
if (!(d >= 0.0 && d <= 1.0)) {
|
||||
RAD_ERR("--draft-depth-stop must be a probability in [0, 1], not '%s'", v);
|
||||
return RAD_E_INVAL;
|
||||
}
|
||||
c->draft_depth_stop = d;
|
||||
}
|
||||
else if (S("--draft-depth-stat")) {
|
||||
if (!(v = need(i))) return RAD_E_INVAL;
|
||||
if (!std::strcmp(v, "top")) c->draft_depth_stat = 0;
|
||||
else if (!std::strcmp(v, "path")) c->draft_depth_stat = 1;
|
||||
else {
|
||||
RAD_ERR("--draft-depth-stat takes top or path, not '%s'", v);
|
||||
return RAD_E_INVAL;
|
||||
}
|
||||
}
|
||||
else if (S("--draft-model")) {
|
||||
/* Refused, not ignored: separate draft models are not supported (spec §10), and the
|
||||
* drafter comes from the container (rad-convert --draft-model merges one in; an MTP
|
||||
@@ -1431,6 +1477,29 @@ int config_parse(int argc, char** argv, Config* c) {
|
||||
RAD_ERR("--video-min-pixels is above --video-max-pixels"); return RAD_E_INVAL; }
|
||||
if (c->video_min_frames && c->video_max_frames && *c->video_min_frames > *c->video_max_frames) {
|
||||
RAD_ERR("--video-min-frames is above --video-max-frames"); return RAD_E_INVAL; }
|
||||
/* --draft-depth adaptive reads the coupled chain's laws and chooses the chain's depth itself. */
|
||||
if (c->draft_depth_adaptive) {
|
||||
if (c->given.count("--draft-coupling") && !c->draft_coupling) {
|
||||
RAD_ERR("--draft-depth adaptive needs the coupled drafting (--draft-coupling on): the "
|
||||
"stop rule reads the law each draft was drawn from");
|
||||
return RAD_E_INVAL;
|
||||
}
|
||||
if (c->draft_window == DraftWindow::Adaptive) {
|
||||
RAD_ERR("--draft-depth adaptive and --draft-window adaptive both choose the chain's "
|
||||
"depth; serve one of them");
|
||||
return RAD_E_INVAL;
|
||||
}
|
||||
if (c->draft_verify_block) {
|
||||
RAD_ERR("--draft-depth adaptive is verified token by token or by traversal "
|
||||
"(--draft-verify token | traversal), not as a fixed block");
|
||||
return RAD_E_INVAL;
|
||||
}
|
||||
if (c->draft_depth_min > 1 && c->draft_depth_max > 0 && c->draft_depth_min > c->draft_depth_max) {
|
||||
RAD_ERR("--draft-depth-min is above --draft-depth-max");
|
||||
return RAD_E_INVAL;
|
||||
}
|
||||
c->draft_coupling = true;
|
||||
}
|
||||
/* --draft-verify block verifies a linear chain with the coupled drafting's states, so it
|
||||
* switches the coupling on; traversal needs it and says so rather than switching it on. */
|
||||
if (c->draft_verify_block) {
|
||||
@@ -1573,6 +1642,13 @@ const ShowRow kShow[] = {
|
||||
{ "--draft-coupling", [](const Config& c) { return onoff(c.draft_coupling); } },
|
||||
{ "--draft-verify", [](const Config& c) {
|
||||
return std::string(c.draft_verify_block ? "block" : c.draft_verify_traversal ? "traversal" : "token"); } },
|
||||
{ "--draft-depth", [](const Config& c) { return std::string(c.draft_depth_adaptive ? "adaptive" : "fixed"); } },
|
||||
{ "--draft-depth-max", [](const Config& c) {
|
||||
return c.draft_depth_max > 0 ? num(c.draft_depth_max) : std::string("auto"); } },
|
||||
{ "--draft-depth-min", [](const Config& c) { return num(c.draft_depth_min); } },
|
||||
{ "--draft-depth-stop", [](const Config& c) { return fnum(c.draft_depth_stop); } },
|
||||
{ "--draft-depth-lag", [](const Config& c) { return num(c.draft_depth_lag); } },
|
||||
{ "--draft-depth-stat", [](const Config& c) { return std::string(c.draft_depth_stat == 1 ? "path" : "top"); } },
|
||||
{ "--draft-model", [](const Config&) { return std::string("not implemented (the container's own drafter)"); } },
|
||||
{ "--host", [](const Config& c) { return c.host; } },
|
||||
{ "--port", [](const Config& c) { return num(c.port); } },
|
||||
|
||||
+4
-1
@@ -757,6 +757,7 @@ Engine::Readback::~Readback() {
|
||||
}
|
||||
|
||||
Engine::~Engine() {
|
||||
depth_log_stats("final");
|
||||
/* The control vector's per-rank direction copies. Uploaded at declare, outside any arena, so
|
||||
* nothing else frees them; each rank's copy is freed with that rank's device bound, since the
|
||||
* allocator is per device. */
|
||||
@@ -1225,7 +1226,9 @@ int Engine::issue_ahead(const RadBatch** out) {
|
||||
if (flex_pending_.load(std::memory_order_acquire) || quiet_pending_.load(std::memory_order_acquire))
|
||||
return RAD_OK;
|
||||
for (int i = 0; i < serial_rows_ && serial_done_ > 0; ++i)
|
||||
sched_.expect_draft(serial_row_[(size_t)i].seq, serial_done_);
|
||||
/* --draft-depth adaptive: the row's own chain (Engine::depth_row_nodes); else the rounds
|
||||
* every row ran. */
|
||||
sched_.expect_draft(serial_row_[(size_t)i].seq, depth_row_nodes(i));
|
||||
for (int i = 0; i < block_rows_; ++i)
|
||||
sched_.expect_draft(block_row_[(size_t)i].seq, block_steps_);
|
||||
const RadBatch* nb = sched_.step_next();
|
||||
|
||||
@@ -29,6 +29,7 @@
|
||||
#include "runtime/ctx.h" /* KVStageRun */
|
||||
#include "sched/mem_iface.h"
|
||||
#include "sched/scheduler.h"
|
||||
#include "sched/draft_depth.h" /* --draft-depth adaptive: the stop rule */
|
||||
#include "sample/vocab_view.h"
|
||||
#include "runtime/rank_barrier.h"
|
||||
#include "text/tokenizer.h"
|
||||
@@ -677,6 +678,42 @@ private:
|
||||
bool couple_evidence_ = false; /* the completed-verify line was written (once a process) */
|
||||
bool couple_host_note_ = false; /* the host-domain counterpart was written (once) */
|
||||
bool couple_malformed_note_ = false; /* a status -1 round was reported (once) */
|
||||
int64_t couple_planes_ = 0; /* the rounds the state plane holds */
|
||||
/* --draft-depth adaptive (sched/draft_depth.h, engine_draft.cpp): the chain of a step stops by
|
||||
* the head's own laws, so its rounds are issued one decision at a time. After the pass of
|
||||
* round r the engine copies that round's state rows to depth_rb_ and records depth_ev_[r - 1];
|
||||
* the driver waits for the event of the round a decision needs, reads the statistic and issues
|
||||
* the next round or ends the chain. depth_of_[i] is drafting row i's final depth, which
|
||||
* expect_draft, set_draft and the coupling's bind all take in place of the rounds that ran. */
|
||||
DraftDepthCfg depth_cfg_;
|
||||
RadEvent depth_ev_[RAD_MTP_COUPLE_MAX_ROUNDS] = {};
|
||||
bool depth_issued_[RAD_MTP_COUPLE_MAX_ROUNDS] = {};
|
||||
RadMtpCoupleState* depth_rb_ = nullptr; /* pinned [rounds, couple_rows_] */
|
||||
std::vector<DraftDepthRow> depth_row_;
|
||||
std::vector<int32_t> depth_of_;
|
||||
int depth_seen_ = 0; /* rounds observed this step */
|
||||
int64_t depth_spin_us_ = 3000; /* RADIANCE_DEPTH_SPIN_US: poll this long, then block */
|
||||
struct DepthStats {
|
||||
uint64_t steps = 0, rows = 0, rounds = 0, drafts = 0, lawless = 0, waits = 0, blocked = 0;
|
||||
uint64_t hist[RAD_MTP_COUPLE_MAX_ROUNDS + 1] = {};
|
||||
double wait_us = 0;
|
||||
uint64_t late = 0;
|
||||
/* By the drafts a step made (d): the card's cycle from that step's first statistic to the
|
||||
* next step's (its later rounds, the verify of d + 1 rows, the sampler, the next history
|
||||
* pass), and what the verify of d drafts accepted. */
|
||||
double cyc_us[RAD_MTP_COUPLE_MAX_ROUNDS + 1] = {};
|
||||
uint64_t cyc_n[RAD_MTP_COUPLE_MAX_ROUNDS + 1] = {};
|
||||
uint64_t acc[RAD_MTP_COUPLE_MAX_ROUNDS + 1] = {}, acc_n[RAD_MTP_COUPLE_MAX_ROUNDS + 1] = {};
|
||||
} depth_stats_;
|
||||
std::chrono::steady_clock::time_point depth_e1_{}; /* the last step's first statistic */
|
||||
uint64_t depth_logged_ = 0; /* depth_stats_.rows at the last line */
|
||||
int depth_last_ = 0; /* and the deepest chain that step made */
|
||||
/* Drafting row i's depth after issue_serial_drafter: its own under the adaptive rule, else the
|
||||
* rounds every row ran. */
|
||||
int depth_row_nodes(int i) const;
|
||||
int depth_readback_issue(int round, int n);
|
||||
int depth_observe_upto(int round, int n, const StepOut& so);
|
||||
void depth_log_stats(const char* when);
|
||||
/* `rows` are serial_row_ or block_row_ (Scheduler::SerialRow / BlockRow). */
|
||||
template <class Row> int stage_couple_draws(const Row* rows, int n);
|
||||
int bind_couple(uint64_t seq, int i, int done, bool drafted, int32_t card);
|
||||
|
||||
+249
-2
@@ -100,6 +100,7 @@ int Engine::step_state_init() {
|
||||
return RAD_E_STATE;
|
||||
}
|
||||
couple_rows_ = sd.shape[1];
|
||||
couple_planes_ = sd.shape[0];
|
||||
/* One entry per card slot, indexed by StepEntry::card as slots_host_ is. */
|
||||
couple_by_card_.assign((size_t)n_slot, RadMtpCoupleDraw{});
|
||||
couple_draw_stage_ = static_cast<RadMtpCoupleDraw*>(
|
||||
@@ -117,6 +118,49 @@ int Engine::step_state_init() {
|
||||
drafter_.depth);
|
||||
}
|
||||
|
||||
/* --draft-depth adaptive: the rule, the per-round readback of the chain's states and one host
|
||||
* event a round. Refused wherever the chain it stops does not exist, rather than served fixed. */
|
||||
depth_cfg_ = DraftDepthCfg{};
|
||||
if (cfg_.draft_depth_adaptive) {
|
||||
if (!mtp_couple_ || drafter_.kind != RAD_DRAFT_SERIAL) {
|
||||
RAD_ERR("--draft-depth adaptive needs a coupled serial draft chain (--draft-coupling on, a "
|
||||
"serial head); this deployment's drafter is '%s'",
|
||||
drafter_.name ? drafter_.name : "(none)");
|
||||
return RAD_E_INVAL;
|
||||
}
|
||||
/* --draft-window adaptive picks a window from the acceptance and the cost of the steps
|
||||
* before (sched/draft.h); this rule picks the depth inside the step from the head's laws.
|
||||
* Two controllers on one chain would fight over it. */
|
||||
if (cfg_.draft_window == DraftWindow::Adaptive) {
|
||||
RAD_ERR("--draft-depth adaptive and --draft-window adaptive both choose the chain's "
|
||||
"depth; serve one of them");
|
||||
return RAD_E_INVAL;
|
||||
}
|
||||
depth_cfg_.on = true;
|
||||
depth_cfg_.max = (int)std::min<int64_t>(drafter_.depth, couple_planes_);
|
||||
if (cfg_.draft_depth_max > 0) depth_cfg_.max = std::min(depth_cfg_.max, cfg_.draft_depth_max);
|
||||
depth_cfg_.max = std::min(depth_cfg_.max, (int)RAD_MTP_COUPLE_MAX_ROUNDS);
|
||||
depth_cfg_.min = std::min(std::max(cfg_.draft_depth_min, 1), depth_cfg_.max);
|
||||
depth_cfg_.lag = cfg_.draft_depth_lag;
|
||||
depth_cfg_.stat = cfg_.draft_depth_stat;
|
||||
depth_cfg_.stop = cfg_.draft_depth_stop;
|
||||
depth_cfg_.lawless = std::min(3, depth_cfg_.max);
|
||||
depth_rb_ = static_cast<RadMtpCoupleState*>(rad_dev_alloc(
|
||||
(int64_t)RAD_MTP_COUPLE_MAX_ROUNDS * couple_rows_ * (int64_t)sizeof(RadMtpCoupleState),
|
||||
RAD_MEM_HOST_PINNED));
|
||||
if (!depth_rb_) return RAD_E_NOMEM;
|
||||
for (RadEvent& e : depth_ev_)
|
||||
RAD_TRY(rad_event_create_as(&e, RAD_EVENT_LOCAL | RAD_EVENT_HOST_WAIT));
|
||||
depth_row_.assign((size_t)n_slot, DraftDepthRow{});
|
||||
depth_of_.assign((size_t)n_slot, 0);
|
||||
if (const char* e = std::getenv("RADIANCE_DEPTH_SPIN_US")) depth_spin_us_ = std::atoll(e);
|
||||
RAD_INFO("draft depth adaptive: 1..%d draft(s) a step, the chain goes on while the product of "
|
||||
"the head's %s probabilities is >= %.3f (decided %d round(s) behind, %d round(s) "
|
||||
"always run); a row with no law drafts %d",
|
||||
depth_cfg_.max, depth_cfg_.stat == DRAFT_DEPTH_PATH ? "drawn-token" : "top",
|
||||
depth_cfg_.stop, depth_cfg_.lag, depth_cfg_.min, depth_cfg_.lawless);
|
||||
}
|
||||
|
||||
RAD_TRY(rad_event_create_as(&step_end_, RAD_EVENT_LOCAL | RAD_EVENT_HOST_WAIT));
|
||||
RAD_TRY(rad_event_create_as(&tok_end_, RAD_EVENT_LOCAL | RAD_EVENT_HOST_WAIT));
|
||||
return RAD_OK;
|
||||
@@ -136,6 +180,9 @@ void Engine::step_state_fini() {
|
||||
if (couple_readback_) rad_dev_free(couple_readback_, RAD_MEM_HOST_PINNED);
|
||||
couple_draw_stage_ = nullptr;
|
||||
couple_readback_ = nullptr;
|
||||
if (depth_rb_) rad_dev_free(depth_rb_, RAD_MEM_HOST_PINNED);
|
||||
depth_rb_ = nullptr;
|
||||
for (RadEvent& e : depth_ev_) { if (e) rad_event_destroy(e); e = nullptr; }
|
||||
if (step_end_) rad_event_destroy(step_end_);
|
||||
step_end_ = nullptr;
|
||||
if (tok_end_) rad_event_destroy(tok_end_);
|
||||
@@ -244,6 +291,144 @@ int Engine::tree_compact(int rows) {
|
||||
return RAD_OK;
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------ --draft-depth adaptive
|
||||
*
|
||||
* The chain's depth is decided while it is drafted (sched/draft_depth.h), so the rounds cannot all
|
||||
* be queued behind the step as a fixed chain's are: round j is issued once the statistic of round
|
||||
* j - 1 - lag is known. That costs the host one tiny readback a round (the round's state rows of
|
||||
* the drafting sequences, rank 0's copy; every rank holds the same) and a wait for it. With lag 1
|
||||
* the card is running round j - 1 meanwhile, so nothing idles as long as the host answers within a
|
||||
* round (about 0.5 ms; reading the row and issuing a recorded pass takes about 0.1 ms). The next
|
||||
* verify is issued the same way, right after the chain's last decision, with exactly the rows the
|
||||
* chain has (Engine::issue_ahead): each row count is its own recorded pass.
|
||||
*
|
||||
* The commit of the step in flight moves behind those decisions (the step loop collects after
|
||||
* issue_ahead), so its tokens are streamed up to the chain's rounds later than with a fixed chain;
|
||||
* the card is not held up by that. */
|
||||
int Engine::depth_row_nodes(int i) const {
|
||||
if (!depth_cfg_.on) return serial_done_;
|
||||
if (i < 0 || (size_t)i >= depth_of_.size()) return 0;
|
||||
return depth_of_[(size_t)i];
|
||||
}
|
||||
|
||||
/* Behind the pass of round `round`: its state rows of the `n` drafting sequences to the host, and
|
||||
* the event the driver waits on. */
|
||||
int Engine::depth_readback_issue(int round, int n) {
|
||||
if (round < 1 || round > (int)RAD_MTP_COUPLE_MAX_ROUNDS || round > (int)couple_planes_ || n <= 0)
|
||||
return RAD_OK;
|
||||
Rank& r0 = *ranks_[0];
|
||||
const char* state = static_cast<const char*>(r0.ctx.buf_ptr(couple_state_buf_));
|
||||
if (!state) return RAD_E_STATE;
|
||||
const int64_t plane = couple_rows_ * (int64_t)sizeof(RadMtpCoupleState);
|
||||
RAD_TRY(rad_memcpy_async(depth_rb_ + (int64_t)(round - 1) * couple_rows_,
|
||||
state + (int64_t)(round - 1) * plane,
|
||||
(int64_t)n * (int64_t)sizeof(RadMtpCoupleState), r0.ctx.stream()));
|
||||
RAD_TRY(rad_event_record(depth_ev_[round - 1], r0.ctx.stream()));
|
||||
depth_issued_[round - 1] = true;
|
||||
return RAD_OK;
|
||||
}
|
||||
|
||||
/* Waits for the statistics of every round up to `round` and feeds them to the rows. A round whose
|
||||
* readback was never issued (its pass did not run) leaves the rows without a law. */
|
||||
int Engine::depth_observe_upto(int round, int n, const StepOut& so) {
|
||||
while (depth_seen_ < round) {
|
||||
const int r = depth_seen_ + 1;
|
||||
const bool have = r <= (int)RAD_MTP_COUPLE_MAX_ROUNDS && depth_issued_[r - 1];
|
||||
if (have) {
|
||||
RadEvent ev = depth_ev_[r - 1];
|
||||
const auto t0 = std::chrono::steady_clock::now();
|
||||
int q = rad_event_query(ev);
|
||||
if (q < 0) return q;
|
||||
if (q == 0) {
|
||||
/* The ids' copy sits ahead of every draft pass: block for it (the trunk's whole
|
||||
* forward pass may be in front), then poll, which answers within a microsecond of
|
||||
* the card where an interrupt takes tens. */
|
||||
if (so.early && depth_seen_ == 0) RAD_TRY(rad_event_sync(tok_end_));
|
||||
const auto t1 = std::chrono::steady_clock::now();
|
||||
bool blocked = false;
|
||||
for (uint32_t spin = 0;; ++spin) {
|
||||
q = rad_event_query(ev);
|
||||
if (q != 0) break;
|
||||
if ((spin & 255u) == 255u &&
|
||||
std::chrono::duration<double, std::micro>(std::chrono::steady_clock::now() - t1)
|
||||
.count() > (double)depth_spin_us_) {
|
||||
blocked = true;
|
||||
break;
|
||||
}
|
||||
#if defined(__x86_64__) || defined(__i386__)
|
||||
__builtin_ia32_pause();
|
||||
#endif
|
||||
}
|
||||
if (q < 0) return q;
|
||||
if (blocked) ++depth_stats_.blocked;
|
||||
++depth_stats_.waits;
|
||||
} else {
|
||||
/* Already there: the host was behind the card by the time it asked. */
|
||||
++depth_stats_.late;
|
||||
}
|
||||
/* The wait proper: what a host wait does to the fences of the dispatches that follow
|
||||
* (the copy wrote host memory), and the block when the poll gave up. */
|
||||
RAD_TRY(rad_event_sync(ev));
|
||||
const auto t2 = std::chrono::steady_clock::now();
|
||||
depth_stats_.wait_us += std::chrono::duration<double, std::micro>(t2 - t0).count();
|
||||
/* The card's cycle by the last step's depth: the host waits for every step's first
|
||||
* statistic, so two of them in a row are one cycle apart. A gap of 30 ms or more is
|
||||
* no decode cycle (an idle engine, or the next request's prefill, in between). */
|
||||
if (r == 1) {
|
||||
const double us = std::chrono::duration<double, std::micro>(t2 - depth_e1_).count();
|
||||
if (q == 1 && depth_last_ >= 1 && depth_last_ <= (int)RAD_MTP_COUPLE_MAX_ROUNDS &&
|
||||
us > 0 && us < 30000.0) {
|
||||
depth_stats_.cyc_us[depth_last_] += us;
|
||||
++depth_stats_.cyc_n[depth_last_];
|
||||
}
|
||||
depth_e1_ = t2;
|
||||
}
|
||||
}
|
||||
for (int i = 0; i < n; ++i) {
|
||||
DraftDepthRow& row = depth_row_[(size_t)i];
|
||||
double st = -1.0;
|
||||
if (have && !row.lawless) {
|
||||
const RadMtpCoupleState& cs = depth_rb_[(int64_t)(r - 1) * couple_rows_ + i];
|
||||
/* this step's draw for this row, or a stale plane: no law */
|
||||
if (!std::memcmp(&cs.draw, &couple_draw_stage_[(size_t)i], sizeof cs.draw))
|
||||
st = draft_depth_stat(cs, r, depth_cfg_.stat);
|
||||
}
|
||||
draft_depth_observe(&row, r, st);
|
||||
}
|
||||
depth_seen_ = r;
|
||||
}
|
||||
return RAD_OK;
|
||||
}
|
||||
|
||||
/* The adaptive chain's tuning line: the shares of each depth and what the host waits cost, then
|
||||
* by the drafts a step made, the card's cycle and the drafts its verify accepted. Logged every
|
||||
* 4096 drafting rows and at shutdown. */
|
||||
void Engine::depth_log_stats(const char* when) {
|
||||
const DepthStats& ds = depth_stats_;
|
||||
if (!depth_cfg_.on || ds.rows == 0 || (depth_logged_ == ds.rows && !std::strcmp(when, "final"))) return;
|
||||
depth_logged_ = ds.rows;
|
||||
std::string h;
|
||||
for (int d2 = 1; d2 <= depth_cfg_.max; ++d2)
|
||||
h += fmt("%s%d:%.3f", d2 > 1 ? " " : "", d2, (double)ds.hist[d2] / (double)ds.rows);
|
||||
RAD_INFO("draft depth adaptive (%s): %llu drafting step(s), drafts/row %.3f, rounds/step %.3f, "
|
||||
"depth shares [%s], lawless rows %llu; host waits %llu (%.1f us each, %llu ended "
|
||||
"blocked), statistics already there %llu",
|
||||
when, (unsigned long long)ds.steps, (double)ds.drafts / (double)ds.rows,
|
||||
(double)ds.rounds / (double)ds.steps, h.c_str(), (unsigned long long)ds.lawless,
|
||||
(unsigned long long)ds.waits,
|
||||
ds.waits + ds.late ? ds.wait_us / (double)(ds.waits + ds.late) : 0.0,
|
||||
(unsigned long long)ds.blocked, (unsigned long long)ds.late);
|
||||
std::string by;
|
||||
for (int d2 = 1; d2 <= depth_cfg_.max; ++d2)
|
||||
by += fmt("%s%d: %.0f us (%llu), %.3f accepted (%llu)", d2 > 1 ? "; " : "", d2,
|
||||
ds.cyc_n[d2] ? ds.cyc_us[d2] / (double)ds.cyc_n[d2] : 0.0,
|
||||
(unsigned long long)ds.cyc_n[d2],
|
||||
ds.acc_n[d2] ? (double)ds.acc[d2] / (double)ds.acc_n[d2] : 0.0,
|
||||
(unsigned long long)ds.acc_n[d2]);
|
||||
RAD_INFO("draft depth adaptive (%s): by drafts made, the card's cycle and the drafts the "
|
||||
"verify accepted -- %s", when, by.c_str());
|
||||
}
|
||||
|
||||
/* Issue the step's draft passes behind it, without waiting. After the trunk's ranks join: the
|
||||
* acceptance on every rank, the sampled-id readback (before a round can overwrite them), the
|
||||
* history pass and every round, then the final proposal into the slots and their readback.
|
||||
@@ -263,6 +448,8 @@ int Engine::issue_serial_drafter(int step, StepOut& so) {
|
||||
RAD_TRY(collect_issue(so, /*fence=*/true));
|
||||
|
||||
int rounds = drafter_.depth;
|
||||
/* --draft-depth-max below the head's declared depth: no round past it is ever asked for. */
|
||||
if (depth_cfg_.on && rounds > depth_cfg_.max) rounds = depth_cfg_.max;
|
||||
serial_row_.resize((size_t)cfg_.max_seqs);
|
||||
/* `n` counts rows that can draft. It can be zero while the history pass still has work (a
|
||||
* mid-prompt sequence has hidden states the head needs but no token to propose from). */
|
||||
@@ -280,9 +467,30 @@ int Engine::issue_serial_drafter(int step, StepOut& so) {
|
||||
static double a_batch = 0, a_samp = 0, a_rel = 0, a_iss = 0;
|
||||
static long n_pass = 0;
|
||||
|
||||
/* --draft-depth adaptive: every row starts a chain; a greedy row (no draw) has no law. */
|
||||
const bool adepth = depth_cfg_.on && n > 0;
|
||||
if (adepth) {
|
||||
depth_seen_ = 0;
|
||||
for (bool& b : depth_issued_) b = false;
|
||||
for (int i = 0; i < n; ++i) {
|
||||
draft_depth_begin(depth_cfg_, &depth_row_[(size_t)i], couple_draw_stage_[(size_t)i].mode != 1);
|
||||
depth_of_[(size_t)i] = 0;
|
||||
}
|
||||
}
|
||||
|
||||
/* Pass 0, then rounds 2..rounds: round 1 has no pass of its own. */
|
||||
for (int rd = 0; rd <= rounds; rd += rd == 0 ? 2 : 1) {
|
||||
if (rd > 0 && n <= 0) break;
|
||||
/* The adaptive chain: round rd runs only for a row whose confidence up to round
|
||||
* rd - 1 - lag holds (sched/draft_depth.h); the pass runs while any row goes on. */
|
||||
if (adepth && rd >= 2) {
|
||||
const int need = draft_depth_need(depth_cfg_, rd);
|
||||
if (need >= 1) RAD_TRY(depth_observe_upto(need, n, so));
|
||||
bool any = false;
|
||||
for (int i = 0; i < n; ++i)
|
||||
any = draft_depth_go(depth_cfg_, &depth_row_[(size_t)i], rd) || any;
|
||||
if (!any) break;
|
||||
}
|
||||
const auto tA = std::chrono::steady_clock::now();
|
||||
const RadBatch* db = sched_.serial_batch(rd, step);
|
||||
if (!db) { if (rd == 0) continue; break; }
|
||||
@@ -335,8 +543,31 @@ int Engine::issue_serial_drafter(int step, StepOut& so) {
|
||||
}
|
||||
if (rd > 0) serial_done_ = rd;
|
||||
else if (drafted > 0) serial_done_ = 1;
|
||||
/* This round's states to the host, when a later round's decision reads them. */
|
||||
if (adepth && serial_done_ == (rd > 0 ? rd : 1) &&
|
||||
serial_done_ <= draft_depth_need(depth_cfg_, rounds))
|
||||
RAD_TRY(depth_readback_issue(serial_done_, n));
|
||||
}
|
||||
if (serial_done_ <= 0 || n <= 0) return RAD_OK;
|
||||
if (adepth) {
|
||||
DepthStats& ds = depth_stats_;
|
||||
++ds.steps;
|
||||
ds.rounds += (uint64_t)serial_done_;
|
||||
for (int i = 0; i < n; ++i) {
|
||||
const DraftDepthRow& row = depth_row_[(size_t)i];
|
||||
const int d = std::max(1, std::min(row.depth, serial_done_));
|
||||
depth_of_[(size_t)i] = d;
|
||||
++ds.rows;
|
||||
ds.drafts += (uint64_t)d;
|
||||
ds.lawless += row.lawless;
|
||||
++ds.hist[std::min(d, (int)RAD_MTP_COUPLE_MAX_ROUNDS)];
|
||||
}
|
||||
depth_last_ = 0;
|
||||
for (int i = 0; i < n; ++i) depth_last_ = std::max(depth_last_, (int)depth_of_[(size_t)i]);
|
||||
if (ds.rows >= depth_logged_ + 4096) depth_log_stats("running");
|
||||
} else {
|
||||
depth_last_ = 0;
|
||||
}
|
||||
|
||||
/* The last round's proposal into the slots, then rank 0's slots back to the host. Only the
|
||||
* drafting rows: at depth 1 the last pass is the history pass, whose later rows only carried
|
||||
@@ -377,6 +608,19 @@ int Engine::finish_serial_drafter(const StepOut& so) {
|
||||
}
|
||||
}
|
||||
}
|
||||
/* --draft-depth adaptive: what the verify of d drafts accepted, by d (the tuning line). */
|
||||
if (depth_cfg_.on) {
|
||||
for (size_t i = 0; i < plan.e.size(); ++i) {
|
||||
const StepEntry& e = plan.e[i];
|
||||
if (so.first_row[i] < 0 || e.n_spec <= 0 || e.n_spec > (int)RAD_MTP_COUPLE_MAX_ROUNDS ||
|
||||
e.card < 0 || (size_t)e.card >= couple_by_card_.size())
|
||||
continue;
|
||||
const RadMtpCoupleDraw& d = couple_by_card_[(size_t)e.card];
|
||||
if (d.seq != e.seq || d.mode != 1) continue;
|
||||
depth_stats_.acc[e.n_spec] += (uint64_t)std::max(0, so.accepted[i]);
|
||||
++depth_stats_.acc_n[e.n_spec];
|
||||
}
|
||||
}
|
||||
sched_.serial_end(serial_done_);
|
||||
if (serial_done_ <= 0 || serial_rows_ <= 0) return RAD_OK;
|
||||
|
||||
@@ -393,9 +637,12 @@ int Engine::finish_serial_drafter(const StepOut& so) {
|
||||
* cannot snapshot its state); the sampler would refuse the next step otherwise. */
|
||||
if (!r0.sampler.allows_speculation(serial_row_[(size_t)i].seq)) continue;
|
||||
const uint64_t seq = serial_row_[(size_t)i].seq;
|
||||
const int rc = sched_.set_draft(seq, &draft_out_[(size_t)i * (size_t)rounds], done);
|
||||
/* Under --draft-depth adaptive the row's own chain: the drafts the step issued ahead was
|
||||
* planned with (issue_ahead's expect_draft), a prefix of the rounds that ran. */
|
||||
const int row_nodes = depth_cfg_.on ? depth_row_nodes(i) : done;
|
||||
const int rc = sched_.set_draft(seq, &draft_out_[(size_t)i * (size_t)rounds], row_nodes);
|
||||
if (mtp_couple_)
|
||||
RAD_TRY(bind_couple(seq, i, done, rc == RAD_OK, serial_row_[(size_t)i].card));
|
||||
RAD_TRY(bind_couple(seq, i, row_nodes, rc == RAD_OK, serial_row_[(size_t)i].card));
|
||||
}
|
||||
return RAD_OK;
|
||||
}
|
||||
|
||||
@@ -1047,6 +1047,21 @@ struct Config {
|
||||
* tokens than token-wise, and it needs --draft-coupling on. */
|
||||
bool draft_verify_block = false;
|
||||
bool draft_verify_traversal = false;
|
||||
/* --draft-depth fixed|adaptive (core/sched/draft_depth.h): adaptive stops a coupled serial
|
||||
* chain within the step as soon as the head's own laws make the next draft unlikely, so a step
|
||||
* drafts 1 .. draft_depth_max tokens (0: num-speculative-tokens) and its verify carries that
|
||||
* many rows. The chain goes on while the product of the rounds' statistic (stat 0: the largest
|
||||
* mass of each round's law, 1: the mass of the token it drew) stays >= draft_depth_stop; the
|
||||
* decision trails the chain by draft_depth_lag rounds (0 or 1; 1 hides the host's readback
|
||||
* behind a running round), draft_depth_min rounds always run. Needs --draft-coupling on, a
|
||||
* serial head's chain and --draft-window fixed. Lossless: the rule reads draft-side quantities
|
||||
* only. Off by default, which is the fixed chain bit for bit. */
|
||||
bool draft_depth_adaptive = false;
|
||||
int draft_depth_max = 0;
|
||||
int draft_depth_min = 1;
|
||||
int draft_depth_lag = 1;
|
||||
int draft_depth_stat = 0;
|
||||
double draft_depth_stop = 0.85;
|
||||
|
||||
/* ---------------- KL DIVERGENCE AGAINST A REFERENCE (core/kld.h) ----------------
|
||||
*
|
||||
|
||||
+1
-1
@@ -568,7 +568,7 @@ private:
|
||||
int rec1);
|
||||
int haz_mark(PassTape& t);
|
||||
|
||||
static constexpr size_t kTapes = 24; /* recorded passes kept, least recently used out */
|
||||
static constexpr size_t kTapes = 48; /* recorded passes kept, least recently used out */
|
||||
static constexpr int64_t kAuditPlays = 1024;
|
||||
|
||||
std::vector<PassTape> tapes_;
|
||||
|
||||
@@ -0,0 +1,131 @@
|
||||
/* draft_depth.h -- same-step adaptive depth of a coupled serial draft chain (--draft-depth adaptive).
|
||||
*
|
||||
* A fixed chain drafts N tokens every step and its verify pays for N + 1 rows whatever the head
|
||||
* thought of them. Here the chain of a step stops as soon as the head's own laws say the next draft
|
||||
* is unlikely to be accepted: round j (j >= 2) runs only while the chain's confidence stays at or
|
||||
* above the threshold,
|
||||
*
|
||||
* round j runs iff j <= max and ( j <= min or conf(j - 1 - lag) >= stop ),
|
||||
* conf(m) = prod_{i = 1..m} s_i, conf(m <= 0) = 1,
|
||||
*
|
||||
* where s_i is read from round i's coupled state (abi/rad_spechub.h): the largest mass of the law
|
||||
* Q_i the round drew from (`top`), or the mass of the token it drew (`path`, the probability of
|
||||
* the drafted path). The first round that does not run ends the chain; its depth D is the number
|
||||
* of rounds that ran, and the next verify carries D drafts (D + 1 rows).
|
||||
*
|
||||
* `lag` is how many rounds the decision trails the chain: with lag 0 round j is decided by every
|
||||
* round before it, so the card waits for the host between two rounds; with lag 1 round j is decided
|
||||
* when round j - 2 has finished, while round j - 1 runs, so the host's decision (a readback of one
|
||||
* state row, then the next round's or the next verify's issue) hides behind a running round.
|
||||
*
|
||||
* Why this keeps the verify exact. The decision "x_j is drafted" reads Q_1 .. Q_{j-1-lag} and, for
|
||||
* `path`, x_1 .. x_{j-1-lag}: draft-side quantities of the chain's ANCESTORS of x_j only, never a
|
||||
* target output and never x_j itself. For a row-wise verify any such stopping time is exact (each
|
||||
* verified position is exact on its own). For the traversal walk (abi/rad_mtp_traversal.h) the step
|
||||
* of its induction at a node v needs v's number of children to be independent of the tokens drawn
|
||||
* AT v; the shape of a child's subtree may depend on the child's own token, because the induction
|
||||
* hypothesis is applied per child value. Here node v (the prefix x_1 .. x_{j-1}) has a child iff
|
||||
* the rule above holds, a function of v's own path. A rule that read x_j's own mass to admit x_j
|
||||
* would not be exact (tests/mtp_rrs_test.cpp shows the statistic that catches it, and one that
|
||||
* peeks at the target).
|
||||
*
|
||||
* Plain code, no engine types beyond the wire row: the engine's driver, the scheduler's tests and
|
||||
* the exactness test share it.
|
||||
*/
|
||||
#pragma once
|
||||
|
||||
#include <cstdint>
|
||||
|
||||
#include "rad_spechub.h"
|
||||
|
||||
namespace rad {
|
||||
|
||||
enum DraftDepthStat : int32_t {
|
||||
DRAFT_DEPTH_TOP = 0, /* s_i = max_x Q_i(x) */
|
||||
DRAFT_DEPTH_PATH = 1, /* s_i = Q_i(x_i), the drawn token's mass */
|
||||
};
|
||||
|
||||
struct DraftDepthCfg {
|
||||
bool on = false;
|
||||
int max = 0; /* the deepest chain: rounds 1 .. max (the head's declared depth caps it) */
|
||||
int min = 1; /* rounds that always run */
|
||||
int lag = 1; /* rounds the decision trails the chain, 0 or more */
|
||||
int stat = DRAFT_DEPTH_TOP;
|
||||
double stop = 0.85; /* the chain goes on while conf >= stop */
|
||||
int lawless = 3; /* the depth of a row with no law (a greedy request, a malformed round) */
|
||||
};
|
||||
|
||||
/* One drafting row's chain while its step is drafted. */
|
||||
struct DraftDepthRow {
|
||||
double conf = 1.0; /* conf(seen) */
|
||||
int seen = 0; /* rounds whose state went into conf */
|
||||
int depth = 1; /* rounds decided to run so far (round 1 always does) */
|
||||
bool done = false; /* a round was refused: depth is final */
|
||||
bool lawless = false; /* no law: the fixed fallback depth */
|
||||
};
|
||||
|
||||
/* Round `round`'s statistic from its state, or a negative number when the state carries no law
|
||||
* (status != 1: a greedy row or a shortlist the proposer could not turn into Q). */
|
||||
inline double draft_depth_stat(const RadMtpCoupleState& s, int round, int stat) {
|
||||
if (s.status != 1 || s.round != round || s.count < 1 || s.count > 8) return -1.0;
|
||||
double top = 0.0, own = -1.0;
|
||||
for (int i = 0; i < s.count; ++i) {
|
||||
const double q = s.q[i];
|
||||
if (!(q > 0.0) || q > 1.0) return -1.0;
|
||||
if (q > top) top = q;
|
||||
if (s.ids[i] == s.x) own = q;
|
||||
}
|
||||
return stat == DRAFT_DEPTH_PATH ? own : top;
|
||||
}
|
||||
|
||||
/* The round whose statistic decides round `j` (>= 2); <= 0 when nothing does (conf = 1). */
|
||||
inline int draft_depth_need(const DraftDepthCfg& c, int j) { return j - 1 - (c.lag > 0 ? c.lag : 0); }
|
||||
|
||||
/* The start of a step's chain for one row. `lawless`: the row draws nothing (a greedy request). */
|
||||
inline void draft_depth_begin(const DraftDepthCfg& c, DraftDepthRow* r, bool lawless) {
|
||||
*r = DraftDepthRow{};
|
||||
r->lawless = lawless;
|
||||
(void)c;
|
||||
}
|
||||
|
||||
/* Feed round `round`'s statistic (rounds in order, each once). A negative one makes the row lawless. */
|
||||
inline void draft_depth_observe(DraftDepthRow* r, int round, double s) {
|
||||
if (round != r->seen + 1) return;
|
||||
r->seen = round;
|
||||
if (s < 0.0) { r->lawless = true; return; }
|
||||
r->conf *= s;
|
||||
}
|
||||
|
||||
/* Whether round `j` (>= 2) runs for this row, given every statistic up to round
|
||||
* draft_depth_need(c, j) was observed. Rounds are asked in order; once one is refused the chain has
|
||||
* ended. Records the answer in r->depth / r->done. */
|
||||
inline bool draft_depth_go(const DraftDepthCfg& c, DraftDepthRow* r, int j) {
|
||||
if (r->done || j != r->depth + 1) return false;
|
||||
bool go;
|
||||
if (j > c.max) go = false;
|
||||
else if (r->lawless) go = j <= (c.lawless > 0 ? c.lawless : 1);
|
||||
else if (j <= c.min) go = true;
|
||||
else {
|
||||
/* conf(need): the row has seen exactly `need` rounds when asked in order with lag >= 0, or
|
||||
* more when the caller observed ahead; recompute is not possible, so the caller observes
|
||||
* no further than draft_depth_need before asking (the driver does). */
|
||||
go = r->conf >= c.stop;
|
||||
}
|
||||
if (go) r->depth = j; else r->done = true;
|
||||
return go;
|
||||
}
|
||||
|
||||
/* The whole rule at once, for a chain whose statistics are all known (tests, offline): s[i] is
|
||||
* round i + 1's, n of them (n >= max - 1 - lag is enough). Returns the depth D in [1, max]. */
|
||||
inline int draft_depth_of(const DraftDepthCfg& c, const double* s, int n) {
|
||||
DraftDepthRow r;
|
||||
draft_depth_begin(c, &r, false);
|
||||
for (int j = 2; j <= c.max; ++j) {
|
||||
const int need = draft_depth_need(c, j);
|
||||
while (r.seen < need && r.seen < n) draft_depth_observe(&r, r.seen + 1, s[r.seen]);
|
||||
if (!draft_depth_go(c, &r, j)) break;
|
||||
}
|
||||
return r.depth < 1 ? 1 : r.depth;
|
||||
}
|
||||
|
||||
} /* namespace rad */
|
||||
Reference in new issue
Block a user