prefill: take a lone spanned prefill's tail snapshot inside its last chunk
A lone spanned prefill now runs to the prompt's end and takes the prefix-resume snapshot mid-step at the same position, instead of ending its last chunk at the snapshot and running the last few dozen tokens as a step of their own (on Flash-Next that step reads nearly every routed expert for those rows). Every stateful part (delta-net recurrence + conv slot, QSA key ring, PLE windows) computes the rows before and after the cut as two pieces, each what a chunk of those rows computes; rad_ckpt_snap copies each layer's state to a stage on the card between them (57.1 MiB a rank on Flash-Next at TP2, carved from the KV pool only where prefill-span > 1 and the prefix cache is on), then one contiguous copy goes to the host slot. The connection reads and the routed passes restart at the cut, and moe_gemm_q gains an optional `split` operand so a step-wide routed pass gives each expert's rows before the cut a run of their own (the 1-2-row dot-path remainder depends on the run length). ABI 43: RadBatch::ckpt_cut/ckpt_cu (appended), RadArchProbe:: ckpt_snap_ok (in the tail padding). Snapshots and follow-up turns are bit-identical to the cut schedule's (24 checkpoints of 9 prompts, 3.9-30.6K tokens; resumed turns identical). TTFT of 11 documentation prompts (4.3-21K tokens, span 4 at 8192, span-first on): -22.8%..+2.4% a prompt, -2.3% in total; BetterBench filler (narrow routing, a cheap tail step) -0.5%; decode unchanged. Off: RADIANCE_CKPT_INSIDE=0 (keeps the cut) / RADIANCE_CKPT_STAGE=0 (no stage either); RADIANCE_CKPT_HASH=1 logs a digest of every snapshot for byte comparisons. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
917d5e4b93
commit
3deabdaeca
39 files changed
+798
-81
No files matched your search
+1
-1
@@ -19,7 +19,7 @@ extern "C" {
|
||||
* version is refused at load with both versions printed. Each also exports the layout's fingerprint
|
||||
* (rad_abi_layout.h), so two layouts given one number are refused as well. A new optional export
|
||||
* (e.g. rad_plugin_reference) is not a change of shape: a plugin without it gets the default. */
|
||||
#define RAD_ABI_VERSION 42u
|
||||
#define RAD_ABI_VERSION 43u
|
||||
|
||||
/* ================================================================== plugin identity */
|
||||
/* One value per plugin, not flags (QUANT is 3). RAD_PLUGIN_QUANT is a quantiser plugin:
|
||||
|
||||
@@ -27,6 +27,7 @@ ABI_FIELD(RadArchProbe, tree_pays)
|
||||
ABI_FIELD(RadArchProbe, gen_max_pixels)
|
||||
ABI_FIELD(RadArchProbe, gen_max_refs)
|
||||
ABI_FIELD(RadArchProbe, gen_shape)
|
||||
ABI_FIELD(RadArchProbe, ckpt_snap_ok)
|
||||
ABI_STRUCT(RadArgs)
|
||||
ABI_FIELD(RadArgs, t)
|
||||
ABI_FIELD(RadArgs, n_t)
|
||||
@@ -105,6 +106,8 @@ ABI_FIELD(RadBatch, bidi_n)
|
||||
ABI_FIELD(RadBatch, bidi_seq)
|
||||
ABI_FIELD(RadBatch, bidi_used)
|
||||
ABI_FIELD(RadBatch, anchor_rows)
|
||||
ABI_FIELD(RadBatch, ckpt_cut)
|
||||
ABI_FIELD(RadBatch, ckpt_cu)
|
||||
ABI_STRUCT(RadBufDecl)
|
||||
ABI_FIELD(RadBufDecl, dtype)
|
||||
ABI_FIELD(RadBufDecl, rank)
|
||||
|
||||
@@ -1735,6 +1735,14 @@ typedef struct RadArchProbe {
|
||||
* `gen_rows` (its largest pass: a step's two branches, or a query pass's points). It keeps no
|
||||
* KV. */
|
||||
int32_t gen_shape;
|
||||
|
||||
/* ---- appended in ABI 43 (a checkpoint inside the step), in the tail padding behind gen_shape */
|
||||
/* Nonzero when the plugin serves RadBatch::ckpt_cut: a step of one prefill sequence whose
|
||||
* checkpoint lies inside it runs its recurrence in two pieces and snapshots between them
|
||||
* (rad_ckpt_snap). The scheduler then takes a prompt's tail snapshot inside its last chunk
|
||||
* rather than ending the chunk there and running the rows past it as a step of their own.
|
||||
* Zero keeps that step. */
|
||||
int32_t ckpt_snap_ok;
|
||||
} RadArchProbe;
|
||||
|
||||
int rad_arch_probe(const RadModelMeta* meta, RadArchProbe* out);
|
||||
|
||||
@@ -363,6 +363,18 @@ typedef struct RadBatch {
|
||||
* context state at the anchor (LiLiCorr) gathers it there; the query rows themselves sit past
|
||||
* that position. Null on every other pass. */
|
||||
const int32_t* anchor_rows;
|
||||
/* ---- appended in ABI 43 (a checkpoint inside the step) */
|
||||
/* A step of one prefill sequence (n_seq 1, no decode rows) whose linear-state checkpoint lies
|
||||
* inside it rather than at its end: host, the rows before the checkpoint (its position less
|
||||
* the sequence's context), 0 on every other step. Set only for a plugin that asked
|
||||
* (RadArchProbe::ckpt_snap_ok). Such a plugin runs its recurrence over [0, ckpt_cut) and
|
||||
* [ckpt_cut, n_tok) as two pieces -- each what a chunk of those rows computes -- and calls
|
||||
* rad_ckpt_snap for each stateful layer between them, so the snapshot is the state a chunk
|
||||
* ending at the checkpoint leaves, without a step of its own for the rows past it.
|
||||
* `ckpt_cu` is [3] device: {0, ckpt_cut, n_tok}, a piece's cu_seqlens at +0 and +1; null when
|
||||
* ckpt_cut is 0. */
|
||||
int32_t ckpt_cut;
|
||||
const int32_t* ckpt_cu;
|
||||
} RadBatch;
|
||||
|
||||
/* What a generator pass carries beside the step batch's own fields, which keep their meanings:
|
||||
@@ -925,6 +937,13 @@ int rad_lane_join(RadCtx* c, int from, int to);
|
||||
* the status it set, so a helper can `return rad_step_fail(...)`. */
|
||||
int rad_step_fail(RadCtx* c, const char* what);
|
||||
|
||||
/* A step with a checkpoint inside it (RadBatch::ckpt_cut): copy layer `layer`'s state of the
|
||||
* stateful KV group `group` -- the sequence's committed slot as the issue stands -- into the
|
||||
* engine's checkpoint stage, on the current lane's stream. Called between the two pieces of the
|
||||
* recurrence, once for every stateful group and layer the plugin holds. A no-op returning RAD_OK
|
||||
* on a step with no checkpoint inside it, so a caller need not test. */
|
||||
int rad_ckpt_snap(RadCtx* c, rad_kvgroup group, int layer);
|
||||
|
||||
/* ================================================================== run-phase queries */
|
||||
const RadBatch* rad_batch(RadCtx* c);
|
||||
/* The position of model layer `layer` among `group`'s bound layers, or -1 when this rank has no
|
||||
|
||||
@@ -557,7 +557,43 @@ inline void GdnFP8::step(RadCtx* c, const RadBatch* batch, bool own_ar,
|
||||
* gated norm, quantiser and out projection are per row, so a pure prefill step can finish them
|
||||
* a piece at a time and hand off each piece's reduction behind it. */
|
||||
const bool cut = !own_ar && pieces && pieces->cuts(T) && D == 0 && P > 0 && !op_add;
|
||||
/* A checkpoint inside the step (RadBatch::ckpt_cut): the convolution, solve and scan run over
|
||||
* [0, cut) and [cut, T) as two pieces, each the chunk of those rows (the state handed on
|
||||
* through the cache, the second piece's context its first row's position, as ArPieces' pieces
|
||||
* do), and the state and convolution slots go to the checkpoint stage between them. The first
|
||||
* piece is issued at the band a chunk of its rows takes, so the snapshot holds the bytes a
|
||||
* step ending at the checkpoint leaves. */
|
||||
const int64_t ck = batch->ckpt_cut;
|
||||
const bool ck_split = P == 1 && D == 0 && ck > 0 && ck < T && batch->ckpt_cu;
|
||||
if (ck_split) {
|
||||
for (int64_t p = 0; p < 2; ++p) {
|
||||
const int64_t n = p == 0 ? ck : T - ck;
|
||||
const int32_t* cu = batch->ckpt_cu + p;
|
||||
RAD_ISSUE_N(c, op_conv_prep, n,
|
||||
bcol(w.in.x, 0, conv_dim, T), RAD_W(w_conv), RAD_NONE, RAD_W(w_a_log),
|
||||
RAD_W(w_dt_bias), kv_cache(kv_conv, layer), praw(cu, RAD_I32, 2),
|
||||
bcol(w.ab, 0, H, T), bcol(w.ab, H, H, T),
|
||||
praw(cv_idx, RAD_I32, 1),
|
||||
praw(p == 0 ? batch->ctx_lens : batch->positions + ck, RAD_I32, 1),
|
||||
brows(w.q, T), brows(w.k, T), brows(w.v, T),
|
||||
brows(w.gdec, T), brows(w.beta, T));
|
||||
RAD_ISSUE_N(c, op_kkt, n,
|
||||
brows(w.k, T), brows(w.beta, T), brows(w.gdec, T),
|
||||
praw(cu, RAD_I32, 2), brows(w.kkt, T));
|
||||
RAD_ISSUE_N(c, op_scan, n,
|
||||
brows(w.q, T), brows(w.k, T), brows(w.v, T), brows(w.kkt, T),
|
||||
brows(w.gdec, T), brows(w.beta, T),
|
||||
kv_cache(kv_state, layer), praw(cu, RAD_I32, 2),
|
||||
brows(w.o.x, T), kv_cache(kv_state, layer),
|
||||
praw2(st_idx, RAD_I32, 1, st_w));
|
||||
if (p == 0) {
|
||||
rad_ckpt_snap(c, kv_state, layer);
|
||||
rad_ckpt_snap(c, kv_conv, layer);
|
||||
}
|
||||
}
|
||||
}
|
||||
if (P > 0) {
|
||||
if (!ck_split) {
|
||||
RAD_ISSUE(c, op_conv_prep,
|
||||
bcol(w.in.x, 0, conv_dim, T), RAD_W(w_conv), RAD_NONE, RAD_W(w_a_log), RAD_W(w_dt_bias),
|
||||
kv_cache(kv_conv, layer), praw(batch->cu_seqlens + D, RAD_I32, P + 1),
|
||||
@@ -575,6 +611,7 @@ inline void GdnFP8::step(RadCtx* c, const RadBatch* batch, bool own_ar,
|
||||
kv_cache(kv_state, layer), praw(batch->cu_seqlens + D, RAD_I32, P + 1),
|
||||
brows(w.o.x, T), kv_cache(kv_state, layer),
|
||||
praw2(st_idx ? st_idx + D * st_w : nullptr, RAD_I32, P, st_w));
|
||||
}
|
||||
if (cut) {
|
||||
for (int64_t r0 = 0; r0 < T; r0 += pieces->rows) {
|
||||
const int64_t n = T - r0 < pieces->rows ? T - r0 : pieces->rows;
|
||||
|
||||
@@ -170,10 +170,21 @@ struct HyperConn {
|
||||
* (hc_down_kchunk in libr4d), so one read over twice the rows sums in a different order than
|
||||
* two reads over half of them; reading a wide step in pieces computes what the narrower steps
|
||||
* did. */
|
||||
void read_pieces(RadCtx* c_, int64_t T) const {
|
||||
void read_pieces(RadCtx* c_, int64_t T) const { read_span(c_, T, 0, T); }
|
||||
/* The same with a boundary at `cut` (a checkpoint inside the step, RadBatch::ckpt_cut): rows
|
||||
* [0, cut) are read as a chunk of `cut` rows reads them, then [cut, T) on their own; 0 is
|
||||
* none. */
|
||||
void read_pieces(RadCtx* c_, int64_t T, int64_t cut) const {
|
||||
if (cut <= 0 || cut >= T) { read_span(c_, T, 0, T); return; }
|
||||
read_span(c_, T, 0, cut);
|
||||
read_span(c_, T, cut, T);
|
||||
}
|
||||
/* Rows [a, b) in pieces of Config::read_rows from `a`; one read where they fit in one. */
|
||||
void read_span(RadCtx* c_, int64_t T, int64_t a, int64_t b) const {
|
||||
const int64_t piece = c.read_rows;
|
||||
if (piece <= 0 || T <= piece) { read(c_, T, 0, T); return; }
|
||||
for (int64_t r0 = 0; r0 < T; r0 += piece) read(c_, T, r0, T - r0 < piece ? T - r0 : piece);
|
||||
auto one = [&](int64_t r0, int64_t n) { read(c_, T, r0, n); };
|
||||
if (piece <= 0 || b - a <= piece) { one(a, b - a); return; }
|
||||
for (int64_t r0 = a; r0 < b; r0 += piece) one(r0, b - r0 < piece ? b - r0 : piece);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@@ -528,6 +528,11 @@ struct MoeFP8 {
|
||||
const Wire& wire, const Src& src, bool fold = false, bool add_out = true,
|
||||
int ar_in = kArNone, bool ar_out_ = false);
|
||||
void step(RadCtx* c, const RadBatch* batch) const;
|
||||
/* A checkpoint inside the step (RadBatch::ckpt_cut): its token on the card, for the routed
|
||||
* GEMMs' `split` operand, while step() issues a step that is one pass; null otherwise. Each
|
||||
* expert's rows before the checkpoint are then a run of their own, as a step ending there
|
||||
* gives them (moe_gemm_iqr's packed map), so they take the same paths. */
|
||||
mutable const int32_t* split_tok = nullptr;
|
||||
/* The block over the first `T` rows of its buffers, for passes that need fewer rows than the
|
||||
* batch (the MTP head's history pass reads its output only at drafting rows). */
|
||||
void step_rows(RadCtx* c, int64_t T) const;
|
||||
@@ -1914,6 +1919,9 @@ inline void MoeFP8::pass(RadCtx* c_, int64_t T, int64_t r0, int64_t rows, bool o
|
||||
* in turn. Flash-Next's 16- and 32-row verify passes on two R9700: 22.1 -> 21.9 ms and
|
||||
* 31.6 -> 30.95 ms. Below, the arm fits inside the routed GEMM's time and stays beside it. */
|
||||
const bool sh_first = shared && gu_trellis && rows * k >= kSharedFirstRows;
|
||||
/* The routed GEMMs' `split` (split_tok), on a pass that is the whole step. */
|
||||
const bool split_on = split_tok && r0 == 0 && rows == T;
|
||||
const RadOperand sp = split_on ? praw(split_tok, RAD_I32, 1) : RAD_NONE;
|
||||
if (sh_first)
|
||||
shared_up(c_, T, r0, rows,
|
||||
(rfused && router_fused_gate) || (sfused && sigmoid_scatter_gate));
|
||||
@@ -1924,24 +1932,29 @@ inline void MoeFP8::pass(RadCtx* c_, int64_t T, int64_t r0, int64_t rows, bool o
|
||||
RAD_ISSUE_N(c_, op_gu, rows, brow_slice(w.h.x, r0, rows, g.n_embd),
|
||||
RAD_WTAB(w_gu[0], ne), brows(w.sorted, rows * k), brows(w.eoff, ne + 1),
|
||||
brows(w.egu, rows * k));
|
||||
else if (ncls == 4)
|
||||
RAD_ISSUE_N(c_, op_gu, rows,
|
||||
brow_slice(gin_q, r0, rows, g.n_embd),
|
||||
brow_slice(gin_s, r0, rows, g.n_embd / gin_g),
|
||||
RAD_WTAB(w_gu[0], n4[0]), RAD_WTAB(w_gus[0], n4[0]),
|
||||
brows(w.sorted, rows * k), brows(w.eoff, n_reg + 1),
|
||||
RAD_WTAB(w_gu[1], n4[1]), RAD_WTAB(w_gus[1], n4[1]),
|
||||
RAD_WTAB(w_gu[2], n4[2]), RAD_WTAB(w_gus[2], n4[2]),
|
||||
RAD_WTAB(w_gu[3], n4[3]), RAD_WTAB(w_gus[3], n4[3]),
|
||||
brows(w.egu, rows * k));
|
||||
else
|
||||
RAD_ISSUE_N(c_, op_gu, rows,
|
||||
brow_slice(gin_q, r0, rows, g.n_embd),
|
||||
brow_slice(gin_s, r0, rows, g.n_embd / gin_g),
|
||||
RAD_WTAB(w_gu[0], n_reg / 2), RAD_WTAB(w_gus[0], n_reg / 2),
|
||||
brows(w.sorted, rows * k), brows(w.eoff, n_reg + 1),
|
||||
RAD_WTAB(w_gu[1], n_reg / 2), RAD_WTAB(w_gus[1], n_reg / 2),
|
||||
brows(w.egu, rows * k));
|
||||
else if (ncls == 4) {
|
||||
/* `split` only where set: every other step's list is the one it always was. */
|
||||
const RadOperand o_[] = {
|
||||
brow_slice(gin_q, r0, rows, g.n_embd),
|
||||
brow_slice(gin_s, r0, rows, g.n_embd / gin_g),
|
||||
RAD_WTAB(w_gu[0], n4[0]), RAD_WTAB(w_gus[0], n4[0]),
|
||||
brows(w.sorted, rows * k), brows(w.eoff, n_reg + 1),
|
||||
RAD_WTAB(w_gu[1], n4[1]), RAD_WTAB(w_gus[1], n4[1]),
|
||||
RAD_WTAB(w_gu[2], n4[2]), RAD_WTAB(w_gus[2], n4[2]),
|
||||
RAD_WTAB(w_gu[3], n4[3]), RAD_WTAB(w_gus[3], n4[3]),
|
||||
brows(w.egu, rows * k), sp };
|
||||
rad_issue(c_, op_gu, o_, (int)(sizeof o_ / sizeof *o_) - (split_on ? 0 : 1), rows);
|
||||
} else {
|
||||
/* `split` only where set: every other step's list is the one it always was. */
|
||||
const RadOperand o_[] = {
|
||||
brow_slice(gin_q, r0, rows, g.n_embd),
|
||||
brow_slice(gin_s, r0, rows, g.n_embd / gin_g),
|
||||
RAD_WTAB(w_gu[0], n_reg / 2), RAD_WTAB(w_gus[0], n_reg / 2),
|
||||
brows(w.sorted, rows * k), brows(w.eoff, n_reg + 1),
|
||||
RAD_WTAB(w_gu[1], n_reg / 2), RAD_WTAB(w_gus[1], n_reg / 2),
|
||||
brows(w.egu, rows * k), sp };
|
||||
rad_issue(c_, op_gu, o_, (int)(sizeof o_ / sizeof *o_) - (split_on ? 0 : 1), rows);
|
||||
}
|
||||
if (shared && !sh_first)
|
||||
shared_up(c_, T, r0, rows,
|
||||
(rfused && router_fused_gate) || (sfused && sigmoid_scatter_gate));
|
||||
@@ -1963,22 +1976,27 @@ inline void MoeFP8::pass(RadCtx* c_, int64_t T, int64_t r0, int64_t rows, bool o
|
||||
if (c.expert_bf16)
|
||||
RAD_ISSUE_N(c_, op_dn, rows, brows(w.eff.x, rows * k), RAD_WTAB(w_dn[0], ne),
|
||||
brows(w.sorted, rows * k), brows(w.eoff, ne + 1), brows(w.edn, rows * k));
|
||||
else if (ncls == 4)
|
||||
RAD_ISSUE_N(c_, op_dn, rows,
|
||||
brows(gq_exp.a.cq(), rows * k), brows(gq_exp.a.cs(), rows * k),
|
||||
RAD_WTAB(w_dn[0], n4[0]), RAD_WTAB(w_dns[0], n4[0]),
|
||||
brows(w.sorted, rows * k), brows(w.eoff, n_reg + 1),
|
||||
RAD_WTAB(w_dn[1], n4[1]), RAD_WTAB(w_dns[1], n4[1]),
|
||||
RAD_WTAB(w_dn[2], n4[2]), RAD_WTAB(w_dns[2], n4[2]),
|
||||
RAD_WTAB(w_dn[3], n4[3]), RAD_WTAB(w_dns[3], n4[3]),
|
||||
brows(w.edn, rows * k));
|
||||
else
|
||||
RAD_ISSUE_N(c_, op_dn, rows,
|
||||
brows(gq_exp.a.cq(), rows * k), brows(gq_exp.a.cs(), rows * k),
|
||||
RAD_WTAB(w_dn[0], n_reg / 2), RAD_WTAB(w_dns[0], n_reg / 2),
|
||||
brows(w.sorted, rows * k), brows(w.eoff, n_reg + 1),
|
||||
RAD_WTAB(w_dn[1], n_reg / 2), RAD_WTAB(w_dns[1], n_reg / 2),
|
||||
brows(w.edn, rows * k));
|
||||
else if (ncls == 4) {
|
||||
/* `split` only where set: every other step's list is the one it always was. */
|
||||
const RadOperand o_[] = {
|
||||
brows(gq_exp.a.cq(), rows * k), brows(gq_exp.a.cs(), rows * k),
|
||||
RAD_WTAB(w_dn[0], n4[0]), RAD_WTAB(w_dns[0], n4[0]),
|
||||
brows(w.sorted, rows * k), brows(w.eoff, n_reg + 1),
|
||||
RAD_WTAB(w_dn[1], n4[1]), RAD_WTAB(w_dns[1], n4[1]),
|
||||
RAD_WTAB(w_dn[2], n4[2]), RAD_WTAB(w_dns[2], n4[2]),
|
||||
RAD_WTAB(w_dn[3], n4[3]), RAD_WTAB(w_dns[3], n4[3]),
|
||||
brows(w.edn, rows * k), sp };
|
||||
rad_issue(c_, op_dn, o_, (int)(sizeof o_ / sizeof *o_) - (split_on ? 0 : 1), rows);
|
||||
} else {
|
||||
/* `split` only where set: every other step's list is the one it always was. */
|
||||
const RadOperand o_[] = {
|
||||
brows(gq_exp.a.cq(), rows * k), brows(gq_exp.a.cs(), rows * k),
|
||||
RAD_WTAB(w_dn[0], n_reg / 2), RAD_WTAB(w_dns[0], n_reg / 2),
|
||||
brows(w.sorted, rows * k), brows(w.eoff, n_reg + 1),
|
||||
RAD_WTAB(w_dn[1], n_reg / 2), RAD_WTAB(w_dns[1], n_reg / 2),
|
||||
brows(w.edn, rows * k), sp };
|
||||
rad_issue(c_, op_dn, o_, (int)(sizeof o_ / sizeof *o_) - (split_on ? 0 : 1), rows);
|
||||
}
|
||||
/* This layer's streamed experts are consumed; the next layer's copies may start. */
|
||||
(void)rad_expert_prefetch_mark(c_);
|
||||
/* The protected experts, over the trailing offsets: the quantised down GEMM zeroed their
|
||||
@@ -2115,7 +2133,25 @@ inline void MoeFP8::calib_experts_pass(RadCtx* c_, int64_t slots) const {
|
||||
}
|
||||
|
||||
inline void MoeFP8::step(RadCtx* c_, const RadBatch* batch) const {
|
||||
step_rows(c_, batch->n_tok);
|
||||
const int64_t T = batch->n_tok, ck = batch->ckpt_cut;
|
||||
const bool cut = ck > 0 && ck < T && batch->ckpt_cu;
|
||||
if (cut && T > c.rows) {
|
||||
/* A checkpoint inside a step wider than a pass: the passes start again at the checkpoint
|
||||
* (as HyperConn::read_pieces does), so the rows before it are passed as a chunk ending
|
||||
* there passes them, and the rows after it as the next chunk would. */
|
||||
for (int64_t a = 0; a < T; a = ck > a ? ck : T) {
|
||||
const int64_t b = ck > a ? ck : T;
|
||||
for (int64_t r0 = a; r0 < b; r0 += c.rows)
|
||||
pass(c_, T, r0, b - r0 < c.rows ? b - r0 : c.rows);
|
||||
}
|
||||
route_report(c_);
|
||||
return;
|
||||
}
|
||||
/* One pass over the whole step: `split` gives the rows before the checkpoint runs of their
|
||||
* own (split_tok). */
|
||||
split_tok = cut ? batch->ckpt_cu + 1 : nullptr;
|
||||
step_rows(c_, T);
|
||||
split_tok = nullptr;
|
||||
}
|
||||
|
||||
inline void MoeFP8::step_rows(RadCtx* c_, int64_t T) const {
|
||||
|
||||
@@ -501,7 +501,24 @@ inline void PleLayer::ids(RadCtx* c_, const RadBatch* batch) const {
|
||||
* after the step's (`tok` reaches A tokens past the step); the window left behind is the
|
||||
* step's alone. */
|
||||
const int64_t A = P > 0 ? ahead_rows(batch) : 0;
|
||||
if (P > 0)
|
||||
/* A checkpoint inside the step (RadBatch::ckpt_cut): the rows before it, the window's
|
||||
* snapshot, then the rest with the ahead rows, each as a chunk of those rows hashes them. */
|
||||
const int64_t ck = batch->ckpt_cut;
|
||||
if (P == 1 && D == 0 && ck > 0 && ck < T && batch->ckpt_cu) {
|
||||
for (int64_t p = 0; p < 2; ++p) {
|
||||
const int64_t r0 = p == 0 ? 0 : ck, n = p == 0 ? ck : T - ck;
|
||||
RAD_ISSUE_N(c_, op_ids, n,
|
||||
praw(batch->token_ids, RAD_I32, T + A), kv_cache(kv_tok, layer_),
|
||||
praw(batch->ckpt_cu + p, RAD_I32, 2),
|
||||
RAD_W(w_mult), RAD_W(w_vsz), RAD_W(w_off),
|
||||
praw(tk_idx, RAD_I32, 1),
|
||||
praw(p == 0 ? batch->ctx_lens : batch->positions + r0, RAD_I32, 1),
|
||||
RAD_NONE,
|
||||
brows(b_ids, T),
|
||||
p == 1 && A > 0 ? brow_slice(b_ids, T, A, c.heads) : RAD_NONE);
|
||||
if (p == 0) rad_ckpt_snap(c_, kv_tok, layer_);
|
||||
}
|
||||
} else if (P > 0)
|
||||
RAD_ISSUE_N(c_, op_ids, T,
|
||||
praw(batch->token_ids, RAD_I32, T + A), kv_cache(kv_tok, layer_),
|
||||
praw(batch->cu_seqlens + D, RAD_I32, P + 1),
|
||||
@@ -577,7 +594,21 @@ inline void PleLayer::step(RadCtx* c_, const RadBatch* batch) const {
|
||||
praw(batch->ctx_lens, RAD_I32, D),
|
||||
praw(batch->num_accepted, RAD_I32, D),
|
||||
brows(b_out, DT));
|
||||
if (P > 0)
|
||||
const int64_t ck = batch->ckpt_cut;
|
||||
if (P == 1 && D == 0 && ck > 0 && ck < T && batch->ckpt_cu) {
|
||||
/* A checkpoint inside the step: the window over the rows before it, its snapshot, then
|
||||
* the rest (as ids()). */
|
||||
for (int64_t p = 0; p < 2; ++p) {
|
||||
RAD_ISSUE_N(c_, op_conv, p == 0 ? ck : T - ck,
|
||||
brows(b_gvn, T), RAD_W(w_conv), kv_cache(kv_conv, layer_),
|
||||
praw(batch->ckpt_cu + p, RAD_I32, 2), brows(b_gv, T),
|
||||
praw(cv_idx, RAD_I32, 1),
|
||||
praw(p == 0 ? batch->ctx_lens : batch->positions + ck, RAD_I32, 1),
|
||||
RAD_NONE,
|
||||
brows(b_out, T));
|
||||
if (p == 0) rad_ckpt_snap(c_, kv_conv, layer_);
|
||||
}
|
||||
} else if (P > 0)
|
||||
RAD_ISSUE_N(c_, op_conv, T,
|
||||
brows(b_gvn, T), RAD_W(w_conv), kv_cache(kv_conv, layer_),
|
||||
praw(batch->cu_seqlens + D, RAD_I32, P + 1), brows(b_gv, T),
|
||||
|
||||
+21
-6
@@ -338,12 +338,27 @@ inline bool QsaIndexer::store_keys(RadCtx* c_, const RadBatch* batch, int part)
|
||||
brows(w.page, work), bpos,
|
||||
kv_cache(kv_bk, layer));
|
||||
if (part == 2) return true;
|
||||
RAD_ISSUE_N(c_, op_tail, T,
|
||||
bcol_at(w.qk, 0, nw, koff, c.head_dim, T),
|
||||
praw(batch->positions, RAD_I32, T),
|
||||
praw(batch->cu_seqlens, RAD_I32, S + 1),
|
||||
kv_cache(c.kv_tail, layer),
|
||||
praw2(tlb->state_index, RAD_I32, S, tw));
|
||||
if (S == 1 && batch->ckpt_cut > 0 && batch->ckpt_cut < T && batch->ckpt_cu) {
|
||||
/* A checkpoint inside the step (RadBatch::ckpt_cut): the ring over the rows before it,
|
||||
* the snapshot, then the rest -- the ring a chunk ending there leaves, and after both
|
||||
* the ring the whole step leaves (a row writes its own position's slot). */
|
||||
const int64_t ck = batch->ckpt_cut;
|
||||
for (int64_t p = 0; p < 2; ++p) {
|
||||
RAD_ISSUE_N(c_, op_tail, p == 0 ? ck : T - ck,
|
||||
bcol_at(w.qk, 0, nw, koff, c.head_dim, T),
|
||||
praw(batch->positions, RAD_I32, T),
|
||||
praw(batch->ckpt_cu + p, RAD_I32, 2),
|
||||
kv_cache(c.kv_tail, layer),
|
||||
praw2(tlb->state_index, RAD_I32, S, tw));
|
||||
if (p == 0) rad_ckpt_snap(c_, c.kv_tail, layer);
|
||||
}
|
||||
} else
|
||||
RAD_ISSUE_N(c_, op_tail, T,
|
||||
bcol_at(w.qk, 0, nw, koff, c.head_dim, T),
|
||||
praw(batch->positions, RAD_I32, T),
|
||||
praw(batch->cu_seqlens, RAD_I32, S + 1),
|
||||
kv_cache(c.kv_tail, layer),
|
||||
praw2(tlb->state_index, RAD_I32, S, tw));
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
@@ -1777,7 +1777,8 @@ static void step_pipelined(RadCtx* c, const RadBatch* batch, Model& m, const int
|
||||
|
||||
/* The delta net runs a piece at a time over the bounds (ArPieces::cu) in a step of one
|
||||
* prefill sequence in two pieces. */
|
||||
const int32_t* gcu = pcu && K == 2 && batch->n_seq == 1 && batch->phase == RAD_PHASE_PREFILL
|
||||
const int32_t* gcu = pcu && K == 2 && batch->n_seq == 1 && batch->phase == RAD_PHASE_PREFILL &&
|
||||
batch->ckpt_cut <= 0
|
||||
? pcu : nullptr;
|
||||
|
||||
const int64_t n = (int64_t)m.layers.size();
|
||||
@@ -1884,7 +1885,8 @@ static void step_serial(RadCtx* c, const RadBatch* batch, Model& m, const int32_
|
||||
const Layer& l = m.layers[(size_t)li];
|
||||
/* Before the attention connection reads the stream, which is where the reference puts it. */
|
||||
if (li == m.ple_layer) m.ple.step(c, batch);
|
||||
l.hc_mix.read_pieces(c, T);
|
||||
/* A checkpoint inside the step (RadBatch::ckpt_cut) cuts the reads' pieces there. */
|
||||
l.hc_mix.read_pieces(c, T, batch->ckpt_cut);
|
||||
/* The calibration taps read the codes the mixer's GEMMs read: its input before it runs,
|
||||
* its output projection's input before the feed-forward's connection reuses the pair. */
|
||||
if (l.cs_in >= 0) m.calib.tap(c, (size_t)l.cs_in, T);
|
||||
@@ -1919,7 +1921,7 @@ static void step_serial(RadCtx* c, const RadBatch* batch, Model& m, const int32_
|
||||
l.mlp.w_router, T);
|
||||
l.hc_mix.write(c, T, 0, T);
|
||||
dbg_resid(c, (int)li, "mix", g.n_embd, m.b_h, m.a_x.x);
|
||||
l.hc_ffn.read_pieces(c, T);
|
||||
l.hc_ffn.read_pieces(c, T, batch->ckpt_cut);
|
||||
l.mlp.step(c, batch);
|
||||
l.hc_ffn.write(c, T, 0, T);
|
||||
/* The request's control vector, on the stream the write just finished. */
|
||||
@@ -2069,6 +2071,11 @@ static int probe(const RadModelMeta* meta, RadArchProbe* out) {
|
||||
* have chained forms: on two R9700 a full pass under the draft head went from 13.9 to 12.5 ms
|
||||
* and the step without the head from 9.98 to 9.17 ms at one sequence. */
|
||||
out->chain_pays = RAD_CHAIN_ALWAYS;
|
||||
/* A step of one prefill sequence with its checkpoint inside it (RadBatch::ckpt_cut): the delta
|
||||
* net runs its recurrence in two pieces and snapshots between them (rad_block_gdn_fp8.h), and
|
||||
* the connection reads cut their pieces at the checkpoint (HyperConn::read_pieces), so the
|
||||
* rows before it read what a chunk ending there reads. */
|
||||
out->ckpt_snap_ok = 1;
|
||||
return RAD_OK;
|
||||
}
|
||||
|
||||
|
||||
+5
-3
@@ -188,9 +188,11 @@ static const Flag g_flags[] = {
|
||||
" piece passes at tie level. Needs --prefill-span above 1" },
|
||||
{ "--prefill-tail-snapshot", "MODE", "on (default): a prompt's last chunk ends where the\n"
|
||||
" next turn's linear-state snapshot goes (prefix resume\n"
|
||||
" nearest), leaving its last tokens a step of their own; off:\n"
|
||||
" the last chunk runs to the prompt's end, one step fewer, and\n"
|
||||
" a next turn resumes up to an interval earlier" },
|
||||
" nearest), leaving its last tokens a step of their own (a\n"
|
||||
" lone spanned prefill on a model that snapshots inside a step\n"
|
||||
" takes it inside the chunk, no step more); off: the last chunk\n"
|
||||
" runs to the prompt's end, one step fewer, and a next turn\n"
|
||||
" resumes up to an interval earlier" },
|
||||
{ "--prefill-span-first", "MODE", "off (default) | on -- whether a lone prefill may span from\n"
|
||||
" its first token; on: the first interval rides the span (its\n"
|
||||
" rows read QSA's selection, the same keys as the dense pass\n"
|
||||
|
||||
+89
-6
@@ -1216,7 +1216,8 @@ int Engine::prepare_release(const RadBatch* b) {
|
||||
* parked, so no allocation races the read. */
|
||||
kvstream_step_runs(ranks_[0]->kv, b, sched_.step_seqs(b), &stage_runs_);
|
||||
for (auto& rk : ranks_) kvstream_bound_live(&rk->ctx, stage_runs_);
|
||||
return RAD_OK;
|
||||
/* A checkpoint inside the step: every rank's Ctx copies into its stage (Ctx::set_ckpt_snap). */
|
||||
return ckpt_stage_arm(b);
|
||||
}
|
||||
|
||||
int Engine::issue_ahead(const RadBatch** out) {
|
||||
@@ -1912,6 +1913,49 @@ int Engine::collect_pooled(const RadBatch* b, StepOut& so) {
|
||||
return RAD_OK;
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------ the checkpoint stage */
|
||||
/* After the KV carve, the snapshot store and Scheduler::init. Every rank carved a stage or none
|
||||
* did (KVManager::plan_groups decides on the same config and plans). */
|
||||
int Engine::ckpt_stage_open() {
|
||||
bool staged = !ranks_.empty() && ck_store_.enabled();
|
||||
for (auto& rk : ranks_) staged = staged && rk->kv.ckpt_stage() != nullptr;
|
||||
if (!staged) return RAD_OK;
|
||||
ranks_[0]->kv.ckpt_stage_layout(&ck_stage_off_, &ck_stage_lb_);
|
||||
auto env_off = [](const char* k) {
|
||||
const char* e = std::getenv(k);
|
||||
return e && e[0] == '0';
|
||||
};
|
||||
ckpt_inside_ = ckpt_snap_ok_ && !env_off("RADIANCE_CKPT_INSIDE");
|
||||
sched_.set_ckpt_inside(ckpt_inside_);
|
||||
RAD_INFO("checkpoint stage: %s a rank on the card; %s",
|
||||
humanb(ranks_[0]->kv.ckpt_stage_bytes()).c_str(),
|
||||
ckpt_inside_ ? "a prompt's tail snapshot is taken inside its last chunk"
|
||||
: "every snapshot at a chunk's end");
|
||||
return RAD_OK;
|
||||
}
|
||||
|
||||
int Engine::ckpt_stage_arm(const RadBatch* b) {
|
||||
if (ck_stage_off_.empty()) return RAD_OK;
|
||||
const StepEntry* in = nullptr;
|
||||
if (b && b->ckpt_cut > 0)
|
||||
for (const StepEntry& e : sched_.plan().e)
|
||||
if (e.ckpt_slot >= 0 && e.ckpt_tok >= 0 && e.ckpt_tok + 1 < e.n_tokens) in = &e;
|
||||
if (!in) {
|
||||
for (auto& rk : ranks_) rk->ctx.set_ckpt_snap(nullptr, ckpt_slots_, ck_stage_off_, ck_stage_lb_);
|
||||
return RAD_OK;
|
||||
}
|
||||
if (in->ckpt_tok + 1 != b->ckpt_cut || !state_slots_of(in->seq, &ckpt_slots_)) {
|
||||
RAD_ERR("step %d: sequence %llu's checkpoint inside the step (row %d) is not the batch's "
|
||||
"(%d), or its state slots are gone", b->step, (unsigned long long)in->seq,
|
||||
(int)in->ckpt_tok, (int)b->ckpt_cut);
|
||||
return RAD_E_STATE;
|
||||
}
|
||||
/* The step writes the stage; the last snapshot left it on the same stream (save_checkpoints). */
|
||||
for (auto& rk : ranks_)
|
||||
rk->ctx.set_ckpt_snap(rk->kv.ckpt_stage(), ckpt_slots_, ck_stage_off_, ck_stage_lb_);
|
||||
return RAD_OK;
|
||||
}
|
||||
|
||||
/* Snapshot after the step that produced the state. Linear state is what the chunk's last token
|
||||
* leaves, so the chunker ended the chunk at the checkpoint and copy 0 holds exactly that state; a
|
||||
* window snapshot is the window's rows behind the checkpoint, which may lie inside the chunk
|
||||
@@ -1940,12 +1984,26 @@ int Engine::save_checkpoints() {
|
||||
(unsigned long long)e.seq, (int)(e.ctx_len + e.ckpt_tok + 1));
|
||||
return RAD_E_STATE;
|
||||
}
|
||||
for (auto& rk : ranks_) {
|
||||
/* A checkpoint inside the step is in the stage already (rad_ckpt_snap) and goes
|
||||
* to the host slot from there in one copy; one at the chunk's end is copied from
|
||||
* the live slots as before. */
|
||||
const bool inside = e.ckpt_tok + 1 < e.n_tokens && !ck_stage_off_.empty();
|
||||
for (size_t ri = 0; ri < ranks_.size(); ++ri) {
|
||||
auto& rk = ranks_[ri];
|
||||
if (ranks_.size() > 1) (void)rad_dev_set(rk->device);
|
||||
const int s = rk->kv.checkpoint_save(ckpt_slots_.data(),
|
||||
(int)ckpt_slots_.size(),
|
||||
e.ckpt_slot, rk->ctx.stream(),
|
||||
&ckpt_win_);
|
||||
int s = RAD_OK;
|
||||
if (!inside) {
|
||||
s = rk->kv.checkpoint_save(ckpt_slots_.data(), (int)ckpt_slots_.size(),
|
||||
e.ckpt_slot, rk->ctx.stream(), &ckpt_win_);
|
||||
} else {
|
||||
/* The layers the step did not snapshot (rad_ckpt_snap) are the ones it did
|
||||
* not change: a drafter head's state, which moves only in the drafter's
|
||||
* passes behind this copy. */
|
||||
s = rk->kv.checkpoint_stage_rest(
|
||||
ckpt_slots_.data(), (int)ckpt_slots_.size(), rk->ctx.stream(),
|
||||
[&](size_t g, int64_t li) { return rk->ctx.ckpt_snapped(g, li); });
|
||||
if (s >= 0) s = rk->kv.checkpoint_stage_out(e.ckpt_slot, rk->ctx.stream());
|
||||
}
|
||||
if (s < 0) {
|
||||
RAD_ERR("rank %d could not snapshot sequence %llu into checkpoint slot "
|
||||
"%d: %s", rk->index, (unsigned long long)e.seq,
|
||||
@@ -1955,6 +2013,30 @@ int Engine::save_checkpoints() {
|
||||
}
|
||||
}
|
||||
if (ranks_.size() > 1) (void)rad_dev_set(ranks_[0]->device);
|
||||
/* RADIANCE_CKPT_HASH=1: wait for the snapshot and log a digest of each rank's
|
||||
* shard, to compare two builds' snapshots byte for byte. A debugging aid: it
|
||||
* drains the streams. */
|
||||
static const bool hash_on = [] {
|
||||
const char* h = std::getenv("RADIANCE_CKPT_HASH");
|
||||
return h && h[0] == '1';
|
||||
}();
|
||||
if (hash_on) {
|
||||
for (size_t ri = 0; ri < ranks_.size(); ++ri) {
|
||||
auto& rk = ranks_[ri];
|
||||
if (ranks_.size() > 1) (void)rad_dev_set(rk->device);
|
||||
(void)rad_stream_sync(rk->ctx.stream());
|
||||
const unsigned char* p = (const unsigned char*)rk->kv.checkpoint_ptr(e.ckpt_slot);
|
||||
uint64_t h = 0xcbf29ce484222325ull;
|
||||
const int64_t nb = rk->kv.checkpoint_bytes();
|
||||
for (int64_t j = 0; p && j < nb; ++j) { h ^= p[j]; h *= 0x100000001b3ull; }
|
||||
RAD_INFO("ckpt-hash seq=%llu pos=%lld rank=%zu inside=%d bytes=%lld %016llx",
|
||||
(unsigned long long)e.seq,
|
||||
(long long)(e.ctx_len + e.ckpt_tok + 1), ri,
|
||||
(int)(e.ckpt_tok + 1 < e.n_tokens), (long long)nb,
|
||||
(unsigned long long)h);
|
||||
}
|
||||
if (ranks_.size() > 1) (void)rad_dev_set(ranks_[0]->device);
|
||||
}
|
||||
}
|
||||
return RAD_OK;
|
||||
});
|
||||
@@ -2851,6 +2933,7 @@ int Engine::serve(std::unique_ptr<StallWatch> watch) {
|
||||
* spanned step (--prefill-span-pass step) the span pays from the second interval on. */
|
||||
sched_.set_span_min(cfg_.prefill_span_min >= 0 ? cfg_.prefill_span_min : span_auto);
|
||||
}
|
||||
RAD_TRY(ckpt_stage_open());
|
||||
|
||||
for (auto& r : ranks_) r->sampler.attach_vocab(vocab_view_.get());
|
||||
|
||||
|
||||
@@ -787,6 +787,20 @@ private:
|
||||
* the checkpoint save waits for its context pass (save_checkpoints). */
|
||||
bool drafter_snap_ = false;
|
||||
int save_checkpoints();
|
||||
/* Checkpoints through the stage (KVManager::ckpt_stage).
|
||||
*
|
||||
* ckpt_snap_ok_: the plugin snapshots inside a step (RadArchProbe::ckpt_snap_ok); with a
|
||||
* stage on every rank the scheduler then takes a prompt's tail snapshot inside its last chunk
|
||||
* (Scheduler::set_ckpt_inside; RADIANCE_CKPT_INSIDE=0 keeps the chunk's own step). Such a
|
||||
* snapshot is the stage's one contiguous copy to the host slot rather than a pitched copy a
|
||||
* group. */
|
||||
bool ckpt_snap_ok_ = false;
|
||||
bool ckpt_inside_ = false;
|
||||
std::vector<int64_t> ck_stage_off_, ck_stage_lb_;
|
||||
int ckpt_stage_open();
|
||||
/* Before a step: a step with a checkpoint inside it arms every rank's Ctx
|
||||
* (Ctx::set_ckpt_snap); every other step disarms them. */
|
||||
int ckpt_stage_arm(const RadBatch* b);
|
||||
/* A tree drafter (rad_drafter_tree), driven on the card only: ADV_BTAKE takes its parents,
|
||||
* ADV_COMPACT moves the accepted path's K and V after the acceptance, and ADV_CTXTREE stages
|
||||
* the context pass by that path. */
|
||||
|
||||
@@ -1303,6 +1303,12 @@ int Engine::declare() {
|
||||
/* A tree is verified only behind a step issued ahead (step_loop). The KL mode, which issues
|
||||
* none, drafts nothing either (n_spec is 0 there by now), so this never runs under it. */
|
||||
draft_tree_ = cfg_.n_spec > 0 && pb.tree_pays != 0;
|
||||
ckpt_snap_ok_ = pb.ckpt_snap_ok != 0;
|
||||
/* The checkpoint stage (KVManager::ckpt_stage), planned before any KV plan is made. */
|
||||
{
|
||||
const char* e = std::getenv("RADIANCE_CKPT_STAGE");
|
||||
cfg_.ckpt_stage = !(e && e[0] == '0');
|
||||
}
|
||||
if (draft_tree_) {
|
||||
const char* why = "the model's plugin measured a gain with them";
|
||||
RAD_INFO("draft trees: asked -- %s", why);
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
|
||||
#include "kvstream/carve.h"
|
||||
#include "kvstream/plan.h"
|
||||
#include "device/device.h"
|
||||
|
||||
#include <algorithm>
|
||||
#include <cmath>
|
||||
@@ -303,6 +304,21 @@ int KVManager::plan_groups(const std::vector<KVGroupInfo>& groups, const Config&
|
||||
if (p.runtime_win && !p.zero)
|
||||
fixed += align_up(max_seqs_ * p.ring_blocks_max() * p.bytes_per_block, RAD_ALIGN_UNIT);
|
||||
|
||||
/* The checkpoint stage (ckpt_stage): one snapshot of the linear state, where a lone spanned
|
||||
* prefill may take its tail snapshot inside its last chunk (--prefill-span above 1,
|
||||
* --prefill-tail-snapshot on), the engine snapshots into a prefix cache and the slot carries
|
||||
* no window. */
|
||||
stage_bytes_ = 0;
|
||||
{
|
||||
int64_t lin = 0;
|
||||
for (const auto& p : plans_) if (p.stateful() && !p.zero) lin += p.bytes_per_state;
|
||||
if (cfg.ckpt_stage && cfg.prefill_span > 1 && cfg.prefill_tail_snapshot &&
|
||||
cfg.prefix_cache && cfg.checkpoint_slots != 0 && window_ck_bytes_ == 0 && lin > 0 &&
|
||||
lin == checkpoint_bytes_)
|
||||
stage_bytes_ = lin;
|
||||
}
|
||||
if (stage_bytes_ > 0) fixed += align_up(stage_bytes_, RAD_ALIGN_UNIT);
|
||||
|
||||
fixed_ = fixed;
|
||||
ckpts_ = budget_ckpts;
|
||||
return RAD_OK;
|
||||
@@ -418,6 +434,8 @@ void KVManager::forget() {
|
||||
live_.clear(); free_usable_.clear(); bucket_blocks_.clear(); held_hist_.clear();
|
||||
chunk_blocks_.clear(); short_peak_.clear();
|
||||
pool_ = nullptr;
|
||||
stage_ = nullptr;
|
||||
stage_off_ = 0;
|
||||
clears_.clear();
|
||||
seqs_.clear(); pending_.clear();
|
||||
configured_ = false;
|
||||
@@ -589,6 +607,15 @@ int KVManager::carve_into(Pool& kv_pool) {
|
||||
p.pool_offset = off;
|
||||
p.pool_bytes = bytes;
|
||||
}
|
||||
/* The checkpoint stage, after the groups (its bytes are in fixed_). */
|
||||
if (stage_bytes_ > 0) {
|
||||
stage_ = kv_pool.reserve("checkpoint stage", stage_bytes_, RAD_ALIGN_UNIT, &stage_off_);
|
||||
if (!stage_) {
|
||||
RAD_ERR("the checkpoint stage (%s) did not fit the KV pool",
|
||||
humanb(stage_bytes_).c_str());
|
||||
return RAD_E_NOMEM;
|
||||
}
|
||||
}
|
||||
/* --checkpoint-slots does nothing on a model with no recurrent state; say so. */
|
||||
if (ckpts_ > 0 && checkpoint_bytes_ <= 0)
|
||||
RAD_WARN("--checkpoint-slots %lld ignored: this model declares no linear-attention state, "
|
||||
@@ -672,6 +699,10 @@ int KVManager::carve_into(Pool& kv_pool) {
|
||||
const int rc = lendable(p.index) ? 0 : pool_->commit(p.pool_offset, p.pool_bytes);
|
||||
if (rc < 0) return rc;
|
||||
}
|
||||
if (stage_) {
|
||||
const int rc = pool_->commit(stage_off_, stage_bytes_);
|
||||
if (rc < 0) return rc;
|
||||
}
|
||||
if (const int rc = flex_span(flex_floor_loan_, carved + 1, true); rc < 0) {
|
||||
RAD_ERR("KV pool: the card would not back the cache's floor of %s",
|
||||
humanb(flex_backed_).c_str());
|
||||
@@ -1817,6 +1848,50 @@ int KVManager::checkpoint_restore(const int32_t* slots, int n, int32_t ck, RadSt
|
||||
return checkpoint_copy(slots, n, ck, /*save=*/false, s, win);
|
||||
}
|
||||
|
||||
void KVManager::ckpt_stage_layout(std::vector<int64_t>* off, std::vector<int64_t>* lb) const {
|
||||
off->assign(plans_.size(), -1);
|
||||
lb->assign(plans_.size(), 0);
|
||||
int64_t at = 0;
|
||||
for (size_t g = 0; g < plans_.size(); ++g) {
|
||||
const KVGroupPlan& p = plans_[g];
|
||||
if (!p.stateful()) continue;
|
||||
(*off)[g] = at;
|
||||
(*lb)[g] = p.layer_bytes_per_state();
|
||||
at += p.layer_bytes_per_state() * p.n_layers;
|
||||
}
|
||||
}
|
||||
|
||||
int KVManager::checkpoint_stage_rest(const int32_t* slots, int n, RadStream s,
|
||||
const std::function<bool(size_t, int64_t)>& skip) {
|
||||
if (!stage_) return RAD_E_STATE;
|
||||
if (!slots || n < (int)plans_.size()) return RAD_E_INVAL;
|
||||
int64_t off = 0;
|
||||
for (size_t g = 0; g < plans_.size(); ++g) {
|
||||
const KVGroupPlan& p = plans_[g];
|
||||
if (!p.stateful()) continue;
|
||||
const int64_t nbytes = p.layer_bytes_per_state();
|
||||
for (int64_t l = 0; l < p.n_layers; ++l) {
|
||||
if (skip(g, l)) continue;
|
||||
void* st = slots[g] >= 0 ? state_ptr((int32_t)g, (int32_t)l, slots[g]) : nullptr;
|
||||
if (!st) return RAD_E_STATE;
|
||||
RAD_TRY(rad_memcpy_async((char*)stage_ + off + l * nbytes, st, nbytes, s));
|
||||
}
|
||||
off += nbytes * p.n_layers;
|
||||
}
|
||||
return RAD_OK;
|
||||
}
|
||||
|
||||
int KVManager::checkpoint_stage_out(int32_t ck, RadStream s) {
|
||||
if (!stage_) return RAD_E_STATE;
|
||||
if (!ck_store_ || !ck_store_->in_use(ck)) {
|
||||
RAD_ERR("checkpoint stage: the store does not hold slot %d", (int)ck);
|
||||
return RAD_E_INVAL;
|
||||
}
|
||||
char* dst = (char*)checkpoint_ptr(ck);
|
||||
if (!dst) return RAD_E_INVAL;
|
||||
return rad_memcpy_async(dst, stage_, stage_bytes_, s);
|
||||
}
|
||||
|
||||
bool KVManager::window_rows(uint64_t id, int64_t pos, std::vector<int32_t>* out) const {
|
||||
out->clear();
|
||||
const Seq* s = find(id);
|
||||
|
||||
@@ -627,6 +627,31 @@ public:
|
||||
int checkpoint_restore(const int32_t* state_slots, int n_slots, int32_t ck_slot, RadStream s,
|
||||
const std::vector<int32_t>* win = nullptr);
|
||||
|
||||
/* ---------------- the checkpoint stage ----------------
|
||||
*
|
||||
* One snapshot of the linear state on the card, laid out as a checkpoint slot's linear section
|
||||
* (checkpoint_copy's walk: group by group in plans() order, a layer after another), carved
|
||||
* from the KV pool as a fixed cost. A checkpoint inside a step (RadBatch::ckpt_cut): the
|
||||
* plugin copies each layer's state there between the recurrence's two pieces (rad_ckpt_snap,
|
||||
* Ctx::set_ckpt_snap), since the live slot has moved on by the step's end; the engine then
|
||||
* copies the layers the step left alone in (checkpoint_stage_rest) and the stage to the host
|
||||
* slot in one copy (checkpoint_stage_out, Engine::save_checkpoints).
|
||||
*
|
||||
* Planned (plan_groups) where the engine snapshots linear state into a prefix cache and the
|
||||
* slot has no window section; 0 bytes and a null stage otherwise. RADIANCE_CKPT_STAGE=0 plans
|
||||
* none, which keeps every checkpoint at a chunk's end. */
|
||||
int64_t ckpt_stage_bytes() const { return stage_bytes_; }
|
||||
void* ckpt_stage() const { return stage_; }
|
||||
/* Per group (by index): the group's offset in the stage and its bytes a layer, -1 / 0 for a
|
||||
* group with no state. */
|
||||
void ckpt_stage_layout(std::vector<int64_t>* off, std::vector<int64_t>* lb) const;
|
||||
/* The live slots' state of every stateful (group, layer) `skip` does not name into the stage,
|
||||
* on `s`: the layers a step with a checkpoint inside it left alone. */
|
||||
int checkpoint_stage_rest(const int32_t* state_slots, int n_slots, RadStream s,
|
||||
const std::function<bool(size_t, int64_t)>& skip);
|
||||
/* The stage into checkpoint slot `ck`, as one copy on `s`. */
|
||||
int checkpoint_stage_out(int32_t ck, RadStream s);
|
||||
|
||||
/* ---------------- window snapshots (the sliding-window half of a prefix hit) ----------------
|
||||
* A WINDOW group keeps only the blocks its window can still read, so the prefix cache's block
|
||||
* lists do not describe it. A prompt extending a cached prefix at position P needs blocks
|
||||
@@ -720,6 +745,9 @@ private:
|
||||
int64_t ck_rank_off_ = 0;
|
||||
int64_t checkpoint_bytes_ = 0;
|
||||
int64_t window_ck_bytes_ = 0; /* the window section of checkpoint_bytes_ */
|
||||
int64_t stage_bytes_ = 0; /* ckpt_stage_bytes */
|
||||
void* stage_ = nullptr;
|
||||
int64_t stage_off_ = 0; /* in the pool */
|
||||
std::vector<StateClear> clears_; /* grabbed slots the engine must zero */
|
||||
void* pool_base_ = nullptr;
|
||||
BundleComparison bundling_;
|
||||
|
||||
@@ -1002,6 +1002,10 @@ struct Config {
|
||||
* store is opened (checkpoint_slots_auto in core/mem/prefix.h). */
|
||||
int64_t checkpoint_slots = -1;
|
||||
bool prefix_cache = true;
|
||||
/* Engine-internal, not a flag: plan a checkpoint stage on the card (KVManager::ckpt_stage).
|
||||
* The engine sets it at startup unless RADIANCE_CKPT_STAGE=0; a KVManager planned without it
|
||||
* (a test, a tool) carves none. */
|
||||
bool ckpt_stage = false;
|
||||
/* --prefix-resume exact|nearest.
|
||||
* - exact (the default): a prefix hit resumes only on the grid an idle server's cold prefill
|
||||
* chunks the same prompt on (ChunkGeometry::grid), and a prompt's tail snapshot is taken
|
||||
|
||||
@@ -998,8 +998,9 @@ int Ctx::lane_join(int from, int to) {
|
||||
* size here. The ABI-35 host mirrors are used by issue-time split selection; unsupported dynamic
|
||||
* geometries fail closed via the pass-key contract, while Qwen keys its full-group maximum in the
|
||||
* architecture callback. `agen_b`, `vgen_b` and `sgen_b` are set only on generator passes, which are
|
||||
* never recorded (below). */
|
||||
static_assert(sizeof(void*) != 8 || sizeof(RadBatch) == 480,
|
||||
* never recorded (below). `ckpt_cut` and `ckpt_cu`: a pass carrying them is never recorded.
|
||||
*/
|
||||
static_assert(sizeof(void*) != 8 || sizeof(RadBatch) == 496,
|
||||
"RadBatch changed: check that Ctx::pass_key reads every new field a pass depends on");
|
||||
static_assert(sizeof(void*) != 8 || sizeof(RadKVGroupBatch) == 88,
|
||||
"RadKVGroupBatch changed: check that Ctx::pass_key reads every new field");
|
||||
@@ -1026,6 +1027,9 @@ uint64_t Ctx::pass_key(const RadBatch* b) const {
|
||||
/* A generator pass is shaped by what RadBatch::gen_b, agen_b, vgen_b or sgen_b carries, which
|
||||
* no key below reads. */
|
||||
if (b->gen) return 0;
|
||||
/* A step with a checkpoint inside it copies from addresses the engine armed for it
|
||||
* (Ctx::set_ckpt_snap), which no recording could replay. */
|
||||
if (b->ckpt_cut > 0) return 0;
|
||||
uint64_t h = 0x9e3779b97f4a7c15ull;
|
||||
auto mix = [&](uint64_t v) {
|
||||
h ^= v + 0x9e3779b97f4a7c15ull + (h << 6) + (h >> 2);
|
||||
|
||||
@@ -290,6 +290,32 @@ public:
|
||||
return g < kv_.size() ? &kv_[(size_t)g] : nullptr;
|
||||
}
|
||||
|
||||
/* ---------------------------------------------------------- a checkpoint inside the step
|
||||
* (RadBatch::ckpt_cut, rad_ckpt_snap). The engine arms the rank before it releases such a
|
||||
* step: `stage` is this rank's checkpoint stage (KVManager::ckpt_stage), laid out as a
|
||||
* checkpoint slot; per KV group, the sequence's committed state slot (-1: not stateful), the
|
||||
* group's offset in the stage and its bytes a layer. Disarmed (stage null) for every other
|
||||
* step. */
|
||||
void set_ckpt_snap(void* stage, const std::vector<int32_t>& slot,
|
||||
const std::vector<int64_t>& off, const std::vector<int64_t>& lb) {
|
||||
snap_stage_ = stage;
|
||||
snap_slot_ = slot;
|
||||
snap_off_ = off;
|
||||
snap_lb_ = lb;
|
||||
snap_done_.resize(kv_.size());
|
||||
for (size_t g = 0; g < kv_.size(); ++g) snap_done_[g].assign(kv_[g].layer_slot.size(), 0);
|
||||
}
|
||||
int ckpt_snap(rad_kvgroup g, int layer);
|
||||
/* Whether the armed step snapshotted group `g`'s pool layer `li` (a group-local index, as the
|
||||
* stage lays it out); a layer the step left alone holds the snapshot's state at the step's end
|
||||
* and the engine copies it in (Engine::save_checkpoints). */
|
||||
bool ckpt_snapped(size_t g, int64_t li) const {
|
||||
if (g >= snap_done_.size()) return false;
|
||||
for (size_t i = 0; i < kv_[g].layer_slot.size(); ++i)
|
||||
if (kv_[g].layer_slot[i] == li && snap_done_[g][i]) return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
/* ---------------------------------------------------------- routing (spec §5.5) */
|
||||
int route_report(int layer, const RadRouting* r);
|
||||
/* Layer `layer`'s row of the card-side histogram, when it holds `n_expert` counts; null
|
||||
@@ -691,6 +717,11 @@ private:
|
||||
Program* program_ = nullptr;
|
||||
Registry* registry_ = nullptr; /* CtxDesc::registry */
|
||||
std::vector<KVPoolBinding> kv_; /* by rad_kvgroup; RAD_OPK_KV resolves through it */
|
||||
/* set_ckpt_snap */
|
||||
void* snap_stage_ = nullptr;
|
||||
std::vector<int32_t> snap_slot_;
|
||||
std::vector<int64_t> snap_off_, snap_lb_;
|
||||
std::vector<std::vector<uint8_t>> snap_done_; /* [group][model layer] snapshotted */
|
||||
ResidencyTable* residency_ = nullptr;
|
||||
OpStager* stager_ = nullptr;
|
||||
bool staging_ = false; /* the stager armed for the pass being issued */
|
||||
|
||||
@@ -1466,6 +1466,42 @@ int64_t rad::Ctx::kv_layer_index(rad_kvgroup g, int layer) const {
|
||||
return ls[(size_t)layer];
|
||||
}
|
||||
|
||||
/* rad_ckpt_snap: one layer of a stateful group, the sequence's slot to the stage's row for that
|
||||
* layer, on the current lane. The stage is laid out as KVManager::checkpoint_copy lays a slot out
|
||||
* (group by group in plan order, a layer's state after another), so the engine moves it to the
|
||||
* host slot in one copy (Engine::save_checkpoints). */
|
||||
int rad::Ctx::ckpt_snap(rad_kvgroup g, int layer) {
|
||||
if (!snap_stage_ || !batch_ || batch_->ckpt_cut <= 0) return RAD_OK;
|
||||
if (g >= kv_.size() || g >= snap_slot_.size() || snap_slot_[g] < 0 || snap_off_[g] < 0 ||
|
||||
!kv_[g].base) {
|
||||
step_fail_ = fmt("rank %d: rad_ckpt_snap names KV group %u, which holds no state the "
|
||||
"checkpoint stage carries", rank_, (unsigned)g);
|
||||
step_status_ = RAD_E_STATE;
|
||||
return RAD_E_STATE;
|
||||
}
|
||||
const KVPoolBinding& b = kv_[g];
|
||||
const int64_t li = kv_layer_index(g, layer);
|
||||
const int64_t lb = snap_lb_[g];
|
||||
if (li < 0 || lb <= 0) {
|
||||
step_fail_ = fmt("rank %d: rad_ckpt_snap: KV group %u holds no layer %d", rank_,
|
||||
(unsigned)g, layer);
|
||||
step_status_ = RAD_E_STATE;
|
||||
return RAD_E_STATE;
|
||||
}
|
||||
const char* src = static_cast<const char*>(b.base) + kv_binding_offset(b, li) +
|
||||
(int64_t)snap_slot_[g] * lb;
|
||||
char* dst = static_cast<char*>(snap_stage_) + snap_off_[g] + li * lb;
|
||||
const int rc = rad_memcpy_async(dst, src, lb, cur_stream());
|
||||
if (rc < 0 && step_status_ >= 0) step_status_ = rc;
|
||||
if (rc >= 0 && g < snap_done_.size() && (size_t)layer < snap_done_[g].size())
|
||||
snap_done_[g][(size_t)layer] = 1;
|
||||
return rc;
|
||||
}
|
||||
|
||||
int rad_ckpt_snap(RadCtx* c, rad_kvgroup group, int layer) {
|
||||
return c ? static_cast<rad::Ctx*>(c)->ckpt_snap(group, layer) : RAD_E_INVAL;
|
||||
}
|
||||
|
||||
int rad_kv_layer_index(RadCtx* c, rad_kvgroup group, int layer) {
|
||||
return c ? (int)static_cast<rad::Ctx*>(c)->kv_layer_index(group, layer) : -1;
|
||||
}
|
||||
|
||||
@@ -118,6 +118,7 @@ void BatchBuilder::carve(Set& s, char* h) {
|
||||
pair(&s.h_acc, &s.d_acc, S);
|
||||
pair(&s.h_cktok, &s.d_cktok, S);
|
||||
pair(&s.h_ckslot, &s.d_ckslot, S);
|
||||
pair(&s.h_ckcu, &s.d_ckcu, 4);
|
||||
pair(&s.h_out, &s.d_out, O);
|
||||
pair(&s.h_dout, &s.d_dout, S);
|
||||
pair(&s.h_rows, &s.d_rows, 4 * S);
|
||||
@@ -974,6 +975,7 @@ const RadBatch* BatchBuilder::build(const StepPlan& plan, const IKVManager& kv,
|
||||
* band for separately issued decode rows. */
|
||||
int32_t max_q_dec = 0, max_q_pf = 0;
|
||||
int n_ck = 0;
|
||||
int32_t ck_cut = 0; /* RadBatch::ckpt_cut */
|
||||
int64_t n_out = 0, n_dout = 0;
|
||||
int32_t rope_mixed = 0;
|
||||
int64_t n_mm = 0;
|
||||
@@ -1109,6 +1111,13 @@ const RadBatch* BatchBuilder::build(const StepPlan& plan, const IKVManager& kv,
|
||||
++n_bidi;
|
||||
}
|
||||
}
|
||||
if (e.ckpt_tok >= 0 && e.ckpt_slot >= 0 && e.ckpt_tok + 1 < e.n_tokens &&
|
||||
plan.draft_pass == 0) {
|
||||
/* A checkpoint inside the chunk (RadBatch::ckpt_cut), where the step is the one shape
|
||||
* a plugin snapshots inside: one prefill alone (the scheduler plans a linear-state
|
||||
* one nowhere else). A window snapshot inside a chunk needs no cut. */
|
||||
if (ckpt_cut_on_ && n_seq == 1 && e.is_prefill) ck_cut = e.ckpt_tok + 1;
|
||||
}
|
||||
if (e.ckpt_tok >= 0 && e.ckpt_slot >= 0) {
|
||||
/* checkpoint_tok indexes this step's tokens: the sequence's base plus the offset the
|
||||
* scheduler chose, the last token of a checkpoint interval (§7.3). */
|
||||
@@ -1176,6 +1185,11 @@ const RadBatch* BatchBuilder::build(const StepPlan& plan, const IKVManager& kv,
|
||||
(long long)n_tok, (long long)k);
|
||||
return nullptr;
|
||||
}
|
||||
/* The two pieces' bounds of a checkpoint inside the step. */
|
||||
s.h_ckcu[0] = 0;
|
||||
s.h_ckcu[1] = ck_cut;
|
||||
s.h_ckcu[2] = (int32_t)n_tok;
|
||||
s.h_ckcu[3] = 0;
|
||||
|
||||
/* Scored rows (Request::score): one per token of a scoring entry's prefill chunk, in entry
|
||||
* order (the order Engine::score_rows maps back). Appended after the sampled rows so the
|
||||
@@ -1358,6 +1372,8 @@ const RadBatch* BatchBuilder::build(const StepPlan& plan, const IKVManager& kv,
|
||||
B.n_checkpoints = n_ck;
|
||||
B.checkpoint_tok = n_ck ? on_rank(base, b0, s.d_cktok) : nullptr;
|
||||
B.checkpoint_slot = n_ck ? on_rank(base, b0, s.d_ckslot) : nullptr;
|
||||
B.ckpt_cut = ck_cut;
|
||||
B.ckpt_cu = ck_cut > 0 ? on_rank(base, b0, s.d_ckcu) : nullptr;
|
||||
B.n_ahead = n_ahead;
|
||||
}
|
||||
/* Record the flight behind everything reading this span: the upload and the derivation
|
||||
|
||||
@@ -143,6 +143,9 @@ public:
|
||||
* it -- the build stage refuses an over-cap sequence, and a refused build restages forever,
|
||||
* so a request admitted past the cap stalls every queue behind it. */
|
||||
int64_t plugin_words_cap() const { return pw_cap_; }
|
||||
/* Whether a checkpoint inside a lone prefill's chunk is handed to the plugin as
|
||||
* RadBatch::ckpt_cut (Scheduler::set_ckpt_inside). Off: ckpt_cut is always 0. */
|
||||
void set_ckpt_cut(bool on) { ckpt_cut_on_ = on; }
|
||||
|
||||
/* The last build's slab and offsets on one rank, for the advance. The caller supplies mode,
|
||||
* round, slot state and inputs (advance.h). */
|
||||
@@ -211,6 +214,7 @@ private:
|
||||
int32_t *h_tok = nullptr, *h_pos = nullptr, *h_cu = nullptr, *h_sid = nullptr;
|
||||
int32_t *h_q = nullptr, *h_ctx = nullptr, *h_acc = nullptr, *h_parent = nullptr;
|
||||
int32_t *h_cktok = nullptr, *h_ckslot = nullptr;
|
||||
int32_t *h_ckcu = nullptr, *d_ckcu = nullptr; /* [3] RadBatch::ckpt_cu */
|
||||
int32_t *h_out = nullptr; /* [n_out] which of this step's tokens want logits */
|
||||
int32_t *h_dout = nullptr; /* [n_draft_out] a history pass's round-1 rows */
|
||||
int32_t *d_tok = nullptr, *d_pos = nullptr, *d_cu = nullptr, *d_sid = nullptr;
|
||||
@@ -363,6 +367,7 @@ private:
|
||||
* (`mm_ev_`). */
|
||||
bool mm_on_ = false;
|
||||
bool bidi_on_ = false; /* RadEncoderDecl::bidirectional */
|
||||
bool ckpt_cut_on_ = false; /* set_ckpt_cut */
|
||||
/* The engine serves runtime LoRA adapters (Config::lora): the batch carries each row's slot. */
|
||||
bool lora_on_ = false;
|
||||
/* Program::plugin_words: the words a sequence may carry, 0 for none. */
|
||||
|
||||
@@ -1211,7 +1211,18 @@ int64_t Scheduler::plan_chunk(int64_t n_computed, int64_t n_target, int64_t budg
|
||||
* (under a quantum) runs as the next step's chunk (tail_checkpoint decides when that
|
||||
* is worth it). A window's tail snapshot is taken above, inside its chunk. */
|
||||
const int64_t tail = geo_.window_ckpt ? -1 : tail_checkpoint(n_target);
|
||||
if (!cfg_.prefill_tail_snapshot) {
|
||||
/* A prefill alone in its spanned step whose plugin snapshots inside the step
|
||||
* (set_ckpt_inside) takes either snapshot below inside its last chunk, at the same
|
||||
* position, and runs to the prompt's end: the rows past it would otherwise be a step
|
||||
* of their own, which on a routed model reads nearly every expert for a few dozen
|
||||
* rows. */
|
||||
const bool inside = ckpt_inside_ && cfg_.prefill_tail_snapshot && geo_.span > 1 &&
|
||||
span_now_ && cfg_.prefill_span > 1 && !geo_.window_ckpt;
|
||||
if (inside && tail > n_computed) {
|
||||
if (checkpoint_pos) *checkpoint_pos = tail;
|
||||
} else if (inside && n_computed + want > next) {
|
||||
if (checkpoint_pos) *checkpoint_pos = (n_computed + want) / I * I;
|
||||
} else if (!cfg_.prefill_tail_snapshot) {
|
||||
/* --prefill-tail-snapshot off: neither cut below, so the last chunk runs to the
|
||||
* prompt's end; its snapshot is kept where that end is the tail (no cut). */
|
||||
if (tail == n_target && tail > n_computed && checkpoint_pos) *checkpoint_pos = tail;
|
||||
|
||||
@@ -531,6 +531,10 @@ public:
|
||||
* step takes is room the experts lose for it, which on a short prompt costs more than the span
|
||||
* saves (Engine::serve). */
|
||||
void set_span_min(int64_t tokens);
|
||||
/* Whether a checkpoint may lie inside a chunk (RadBatch::ckpt_cut): the plugin snapshots
|
||||
* inside the step (RadArchProbe::ckpt_snap_ok) and the engine holds a stage for it. Set once
|
||||
* by the engine after init; false keeps every checkpoint at a chunk's end. */
|
||||
void set_ckpt_inside(bool on) { ckpt_inside_ = on; builder_.set_ckpt_cut(on); }
|
||||
/* Whether the step just planned leaves a prompt with at least the stager's smallest pass still
|
||||
* to prefill, so the next step most likely carries a chunk the stager arms for. */
|
||||
bool prefill_continues() const;
|
||||
@@ -572,6 +576,8 @@ private:
|
||||
int64_t stage_rows_ = 0;
|
||||
/* set_span_min: the shortest prefill a span is taken for, 0 for any. */
|
||||
int64_t span_min_ = 0;
|
||||
/* set_ckpt_inside */
|
||||
bool ckpt_inside_ = false;
|
||||
/* Set by admit_one when it held the admitted chunk back: what it would have taken. */
|
||||
int64_t admit_full_ = 0;
|
||||
bool admit_next_step_ = false; /* admit_one's refusal is a wait for the next step */
|
||||
|
||||
+16
-1
@@ -1485,6 +1485,21 @@ nothing); a next turn then resumes from the deepest snapshot a chunk end left, t
|
||||
span boundary, and re-prefills up to an interval more. For single-shot prompts (benchmarks, one
|
||||
question per document) off is the faster schedule; for conversations that come back, on.
|
||||
|
||||
Where the model's plugin can snapshot inside a step (Flash-Next, `qwen4exp_fp8`), a prefill alone
|
||||
in its spanned step (`prefill-span` above 1) gets both: its last chunk runs to the prompt's end
|
||||
and the tail snapshot is taken inside it, at the same position. Everything that carries state
|
||||
across the snapshot -- the delta net's recurrence and convolution, the indexer's key ring, the PLE
|
||||
windows -- runs over the rows before it and the rows after it as two pieces, each what a chunk of
|
||||
those rows computes, and copies its slot to a stage on the card between them; the connection reads
|
||||
are cut there too, and the routed GEMMs give each expert's rows before it a run of their own. The
|
||||
snapshot holds the bytes the cut schedule's step leaves (a next turn resumes and answers exactly as
|
||||
before), and the step of the last tokens is gone. The stage is one snapshot a rank on the card
|
||||
(57.1 MiB a rank on Flash-Next at TP2), taken from the KV pool only where `prefill-span` is above 1
|
||||
and the prefix cache is on; such a snapshot goes to its host slot from there in one copy. `RADIANCE_CKPT_INSIDE=0` keeps the cut, `RADIANCE_CKPT_STAGE=0` the
|
||||
stage and the cut. A step shared with other work keeps the cut. Prompts of documentation text (whose
|
||||
last tokens read most experts) at 4.8-6.6K tokens: 843 -> 785 ms, 896 -> 850, 1110 -> 1007; filler
|
||||
text 6-24K: about -1%.
|
||||
|
||||
Under `exact` an interval larger than the chunk must be a multiple of it; otherwise it is rounded
|
||||
up, and startup says so:
|
||||
|
||||
@@ -2644,7 +2659,7 @@ switch is set with `true`. The fleet's own keys (a model's `aliases`, `priority`
|
||||
| `prefill-span-first: MODE` | off | `on`: a lone prefill may span from its first token, the first interval no step of its own (its rows read QSA's selection, the dense pass's keys below the exactness bound, summed in another order); `off`: from its second interval on (§4.5) |
|
||||
| `checkpoint-slots: N` | auto | linear-state (hybrid) or sliding-window snapshots the server may hold at once, in total, not per sequence. They live in pinned host memory, allocated at startup: one slot is a snapshot of every rank's shard (113 MB on Flash-Next at `tp: 2`, 144 MiB on Qwen3.8-27B; on Gemma 4 31B the 1024-token window of every sliding layer, 406 MiB, and with its DFlash2 drafter that drafter's 2048-token window too, 487 MiB). `auto` gives each of `max-num-seqs` what `checkpoint-policy` keeps for a `max-model-len` session plus one (10 at 200K tokens under geometric backoff), within an eighth of host memory and what the host can pin; a floor of 8 (or what the policy wants, if fewer) outranks `memory.host-headroom-mib`, which gives back exactly the shortfall down to 1 GiB rather than take the store to none, and the startup line says so. The startup line and the report say what it chose and why (§4.8). A full store retires its least recently used snapshot. A count is taken as given: a store the host memory cannot hold or pin stops the start, and a table placed in host memory leaves room for it; `0` serves with none, every prefix hit then prefilling from the start |
|
||||
| `checkpoint-policy: NAME` | geometric-backoff | also `tip-only`, `keep-all` |
|
||||
| `prefill-tail-snapshot: MODE` | on | `on`: under `prefix-resume: nearest` a prompt's last chunk is cut at its tail snapshot (last quantum boundary, or a spanned chunk's last interval boundary), its last tokens a step of their own; `off`: the last chunk runs to the prompt's end, one step fewer, and a next turn resumes up to an interval earlier (§4.5) |
|
||||
| `prefill-tail-snapshot: MODE` | on | `on`: under `prefix-resume: nearest` a prompt's last chunk is cut at its tail snapshot (last quantum boundary, or a spanned chunk's last interval boundary), its last tokens a step of their own -- or, a prefill alone in its spanned step on a plugin that snapshots inside a step (Flash-Next), the snapshot taken inside the last chunk and no extra step; `off`: the last chunk runs to the prompt's end, one step fewer, and a next turn resumes up to an interval earlier (§4.5) |
|
||||
| `prefix-resume: MODE` | exact | `exact`: a prefix hit resumes only where an idle server's cold prefill of the prompt starts a chunk, and a prompt's tail snapshot is taken where its last chunk starts, so a hit answers bit-identically to a cold run; an interval past the chunk is rounded up to a multiple of it. `nearest`: the deepest snapshot or block, up to a chunk less re-prefilled a turn |
|
||||
| `no-prefix-cache: true` | prefix cache on | disable prefix caching |
|
||||
|
||||
|
||||
@@ -531,7 +531,8 @@ static const RadParamSpec pMoeGemmQ[] = { P_INT("M"), P_INT("N"), P_INT("K"),
|
||||
P_INTO("parts"), P_INTO("a_group") };
|
||||
static const RadOperandSpec oMoeGemmQ[] = { OPD("a"), OPD_O("a_scale"), WTAB("w"), WTAB("w_scale"),
|
||||
OPD("sorted_tok"), OPD("expert_offset"),
|
||||
WTAB("w_odd"), WTAB("w_odd_scale"), OUT("y") };
|
||||
WTAB("w_odd"), WTAB("w_odd_scale"), OUT("y"),
|
||||
OPD_O("split") };
|
||||
/* The same over four classes by expert number mod 4, `w_q` class q at [N_q, K_q], as in libref
|
||||
* and libr4d. */
|
||||
static const RadParamSpec pMoeGemmQMod4[] = { P_INT("M"), P_INT("N"), P_INT("K"),
|
||||
@@ -544,7 +545,8 @@ static const RadOperandSpec oMoeGemmQMod4[] = { OPD("a"), OPD_O("a_scale"), WTAB
|
||||
WTAB("w_scale"), OPD("sorted_tok"),
|
||||
OPD("expert_offset"), WTAB("w_1"),
|
||||
WTAB("w_1_scale"), WTAB("w_2"), WTAB("w_2_scale"),
|
||||
WTAB("w_3"), WTAB("w_3_scale"), OUT("y") };
|
||||
WTAB("w_3"), WTAB("w_3_scale"), OUT("y"),
|
||||
OPD_O("split") };
|
||||
|
||||
/* With `shared` and `shared_gate`, the shared expert is folded in: y = narrow(shared * act(gate))
|
||||
* + narrow(gathered), the unfused pair's roundings. `act` is sigmoid or none, as in scale_rows,
|
||||
|
||||
@@ -1035,6 +1035,7 @@ static const RadOperandSpec oMoeGemmQ[] = {
|
||||
{ "w_odd", RAD_OPD_WTAB, 0 },
|
||||
{ "w_odd_scale", RAD_OPD_WTAB, 0 },
|
||||
{ "y", RAD_OPD_OUT, 0 },
|
||||
{ "split", RAD_OPD_IN, 1 },
|
||||
};
|
||||
/* The same GEMM over four classes by expert number mod 4: `w`/`w_scale` hold class 0 at [N, K],
|
||||
* `w_q`/`w_q_scale` class q at [N_q, K_q], expert e at entry e / 4 of class e % 4. Four ranks
|
||||
@@ -1061,6 +1062,7 @@ static const RadOperandSpec oMoeGemmQMod4[] = {
|
||||
{ "w_3", RAD_OPD_WTAB, 0 },
|
||||
{ "w_3_scale", RAD_OPD_WTAB, 0 },
|
||||
{ "y", RAD_OPD_OUT, 0 },
|
||||
{ "split", RAD_OPD_IN, 1 },
|
||||
};
|
||||
static const RadParamSpec pMoeGather[] = {
|
||||
P_INT("M"), P_INT("n"), P_INT("top_k"), P_STR("dtype"),
|
||||
|
||||
@@ -1602,6 +1602,7 @@ extern "C" int halo_shape_moe_gemm_q(const RadParam* p, int n_p, int operand, Ra
|
||||
opd((uint32_t)RAD_F8E4M3, ne1, N1, K1), /* w_odd -- the odd experts */
|
||||
opd((uint32_t)RAD_BF16, ne1, (N1 + g - 1) / g, (K1 + g - 1) / g), /* w_odd_scale */
|
||||
opd(D, M * k, N), /* y, in sorted order */
|
||||
opd_none(), /* split -- optional, none here */
|
||||
};
|
||||
HALO_PICK(l);
|
||||
}
|
||||
@@ -1645,6 +1646,7 @@ extern "C" int halo_shape_moe_gemm_q_mod4(const RadParam* p, int n_p, int operan
|
||||
opd_idx(ne + 1, 0, M * k + 1, -1), /* expert_offset */
|
||||
wq(1), sq(1), wq(2), sq(2), wq(3), sq(3), /* classes 1, 2, 3 */
|
||||
opd(D, M * k, N), /* y -- in SORTED order */
|
||||
opd_none(), /* split -- optional, none here */
|
||||
};
|
||||
HALO_PICK(l);
|
||||
}
|
||||
|
||||
+4
-2
@@ -374,7 +374,8 @@ static const RadOperandSpec oMoeGemmQ[] = { { "a", RAD_OPD_IN, 0 },
|
||||
{ "expert_offset", RAD_OPD_IN, 0 },
|
||||
{ "w_odd", RAD_OPD_WTAB, 0 },
|
||||
{ "w_odd_scale", RAD_OPD_WTAB, 0 },
|
||||
{ "y", RAD_OPD_OUT, 0 } };
|
||||
{ "y", RAD_OPD_OUT, 0 },
|
||||
{ "split", RAD_OPD_IN, 1 } };
|
||||
/* `a_group` is an int8 activation's scale group, which no row here reads: the dtypes this library
|
||||
* serves take E4M3 or int4 activations, and the op's schema is one for every library. */
|
||||
static const RadParamSpec pMoeGemmQ[] = { P_INT("M"), P_INT("N"), P_INT("K"), P_INT("n_expert"),
|
||||
@@ -647,7 +648,8 @@ static const RadOperandSpec oMoeGemmQMod4[] = { { "a", RAD_OPD_IN, 0 }, { "a_sca
|
||||
{ "w_1", RAD_OPD_WTAB, 0 }, { "w_1_scale", RAD_OPD_WTAB, 0 },
|
||||
{ "w_2", RAD_OPD_WTAB, 0 }, { "w_2_scale", RAD_OPD_WTAB, 0 },
|
||||
{ "w_3", RAD_OPD_WTAB, 0 }, { "w_3_scale", RAD_OPD_WTAB, 0 },
|
||||
{ "y", RAD_OPD_OUT, 0 } };
|
||||
{ "y", RAD_OPD_OUT, 0 },
|
||||
{ "split", RAD_OPD_IN, 1 } };
|
||||
|
||||
static const RadOpSchema kSchemas[] = {
|
||||
SCHEMA("rmsnorm", pRmsNorm, oRmsNorm,
|
||||
|
||||
+55
-8
@@ -581,13 +581,20 @@ void moe_iqr_kernel(R4D_MOEIQR_PARAMS) {
|
||||
* rows. Offsets are clamped to T, where the kernel clamps a run's end. */
|
||||
__global__ __launch_bounds__(256) void moe_iqr_packmap_kernel(
|
||||
const int32_t* __restrict__ off, uint32_t n_expert, uint32_t T, uint32_t FM, uint32_t CAP,
|
||||
uint32_t cap, uint32_t* __restrict__ map) {
|
||||
uint32_t cap, uint32_t* __restrict__ map, const int32_t* __restrict__ sorted,
|
||||
const int32_t* __restrict__ split, uint32_t top_k) {
|
||||
constexpr uint32_t NT = 256, NW = NT / kWave, kTiles = (kMaxExpert + NT) / NT;
|
||||
__shared__ uint32_t s_cnt[kMaxExpert + 1];
|
||||
__shared__ int32_t s_wt[kTiles][NW];
|
||||
const uint32_t tid = threadIdx.x, lane = tid % kWave, wave = tid / kWave;
|
||||
/* a thread's experts (tid, tid + NT, ..): first row, rows, and the dot path's remainder */
|
||||
uint32_t lo[kTiles], n[kTiles], dr[kTiles];
|
||||
/* `split` (a checkpoint inside the step): the rows of tokens below *split, which lead each
|
||||
* expert's run in the stable sort, are a run of their own and the rest another, each cut as a
|
||||
* whole run is -- so those rows take the entries, and the paths, a pass of them alone gives
|
||||
* them. Null: one run an expert. */
|
||||
const uint32_t cslot = split ? (uint32_t)split[0] * top_k : 0u;
|
||||
/* a thread's experts (tid, tid + NT, ..): first row, the first run's rows and the dot path's
|
||||
* remainder, then the second run's (none without a split) */
|
||||
uint32_t lo[kTiles], n[kTiles], dr[kTiles], n2[kTiles], dr2[kTiles];
|
||||
#pragma unroll
|
||||
for (uint32_t i = 0; i < kTiles; ++i) {
|
||||
const uint32_t e = tid + i * NT;
|
||||
@@ -596,15 +603,31 @@ __global__ __launch_bounds__(256) void moe_iqr_packmap_kernel(
|
||||
lo[i] = a < T ? a : T;
|
||||
const uint32_t hi = b < T ? b : T;
|
||||
n[i] = hi > lo[i] ? hi - lo[i] : 0u;
|
||||
n2[i] = 0u;
|
||||
if (split && n[i] > 0u) {
|
||||
/* the first row at or past the split: rows hold slots, ascending within a run */
|
||||
uint32_t l = lo[i], h = lo[i] + n[i];
|
||||
while (l < h) {
|
||||
const uint32_t m = (l + h) >> 1;
|
||||
if ((uint32_t)sorted[m] < cslot) l = m + 1u;
|
||||
else h = m;
|
||||
}
|
||||
n2[i] = lo[i] + n[i] - l;
|
||||
n[i] = l - lo[i];
|
||||
}
|
||||
const uint32_t rem = n[i] % FM;
|
||||
dr[i] = rem >= 1u && rem <= kDotRows ? rem : 0u;
|
||||
const uint32_t rem2 = n2[i] % FM;
|
||||
dr2[i] = rem2 >= 1u && rem2 <= kDotRows ? rem2 : 0u;
|
||||
}
|
||||
const uint32_t live0 = (uint32_t)off[n_expert];
|
||||
const uint32_t live = live0 < T ? live0 : T;
|
||||
#pragma unroll
|
||||
for (uint32_t i = 0; i < kTiles; ++i) {
|
||||
const uint32_t e = tid + i * NT;
|
||||
if (e < n_expert) s_cnt[e] = (n[i] - dr[i] + CAP - 1) / CAP + (dr[i] ? 1u : 0u);
|
||||
if (e < n_expert)
|
||||
s_cnt[e] = (n[i] - dr[i] + CAP - 1) / CAP + (dr[i] ? 1u : 0u) +
|
||||
(n2[i] - dr2[i] + CAP - 1) / CAP + (dr2[i] ? 1u : 0u);
|
||||
}
|
||||
/* the tail of dropped slots, carried as one more "expert" so the scan places it too */
|
||||
if (tid == 0) s_cnt[n_expert] = T > live ? (T - live + CAP - 1) / CAP : 0u;
|
||||
@@ -643,6 +666,18 @@ __global__ __launch_bounds__(256) void moe_iqr_packmap_kernel(
|
||||
if (dr[i] && o < cap) {
|
||||
map[1 + 2 * o + 0] = e | dr[i] << 16;
|
||||
map[1 + 2 * o + 1] = lo[i] + m;
|
||||
++o;
|
||||
}
|
||||
/* the second run, after the first */
|
||||
const uint32_t lo2 = lo[i] + n[i], m2 = n2[i] - dr2[i];
|
||||
for (uint32_t r = 0; r < m2; r += CAP, ++o) {
|
||||
if (o >= cap) break;
|
||||
map[1 + 2 * o + 0] = e | (m2 - r < CAP ? m2 - r : CAP) << 16;
|
||||
map[1 + 2 * o + 1] = lo2 + r;
|
||||
}
|
||||
if (dr2[i] && o < cap) {
|
||||
map[1 + 2 * o + 0] = e | dr2[i] << 16;
|
||||
map[1 + 2 * o + 1] = lo2 + m2;
|
||||
}
|
||||
}
|
||||
if (tid == 0) {
|
||||
@@ -729,6 +764,11 @@ struct Launch {
|
||||
static inline long long pack_cap(long long T, long long n_expert) {
|
||||
return 2 * n_expert + T / (long long)(kRows * kPackTiles) + 2;
|
||||
}
|
||||
/* ...and with a split (moe_iqr_packmap_kernel): two runs an expert, one more partial entry and
|
||||
* dot remainder each. */
|
||||
static inline long long pack_cap_split(long long T, long long n_expert) {
|
||||
return 4 * n_expert + T / (long long)(kRows * kPackTiles) + 2;
|
||||
}
|
||||
|
||||
template <class F>
|
||||
int launch_form(const Launch& g, RadStream stream) {
|
||||
@@ -785,6 +825,9 @@ int moe_iqr_launch(const RadArgs* a, RadStream stream, int ncls) {
|
||||
ncls == 4 ? r4d_opt(a, 9) : nullptr,
|
||||
ncls == 4 ? r4d_opt(a, 11) : nullptr };
|
||||
const RadTensor* Y = r4d_opt(a, ncls == 4 ? 12 : 8);
|
||||
/* `split` (optional, after y): a checkpoint inside the step -- the token at which each expert's
|
||||
* run is cut in two for the map (moe_iqr_packmap_kernel). */
|
||||
const RadTensor* split = r4d_opt(a, ncls == 4 ? 13 : 9);
|
||||
if (!Aq || !As || !sorted || !off || !Y) return R4D_NO(RAD_E_INVAL);
|
||||
if (!Aq->data || !As->data || !sorted->data || !off->data || !Y->data)
|
||||
return R4D_NO(RAD_E_INVAL);
|
||||
@@ -908,9 +951,12 @@ int moe_iqr_launch(const RadArgs* a, RadStream stream, int ncls) {
|
||||
* all of the activation and the scratch holds its entries (an expert at most one per
|
||||
* 128 rows begun and one for its dot remainder, and the tail). */
|
||||
const uint32_t pin = map_tiles_pin();
|
||||
const long long capp = pack_cap(T, n_expert);
|
||||
/* A split takes the packed map whatever the rows: its entries carry their row counts, and
|
||||
* its bytes are the run map's. */
|
||||
const bool split_on = split && split->data && split->dtype == RAD_I32;
|
||||
const long long capp = split_on ? pack_cap_split(T, n_expert) : pack_cap(T, n_expert);
|
||||
g.packed = fm.bits == 4 && fm.L == 16 &&
|
||||
(pin ? pin == kPackTiles : T >= kPackMinRowsPerExpert * n_expert) &&
|
||||
(split_on || (pin ? pin == kPackTiles : T >= kPackMinRowsPerExpert * n_expert)) &&
|
||||
(unsigned long long)M * (unsigned long long)a_ld + (unsigned long long)K <
|
||||
(1ull << 32) &&
|
||||
(unsigned long long)Ms * (unsigned long long)as_ld < (1ull << 32) &&
|
||||
@@ -919,7 +965,8 @@ int moe_iqr_launch(const RadArgs* a, RadStream stream, int ncls) {
|
||||
if (g.packed) {
|
||||
cap = capp;
|
||||
rc = R4D_EMIT(stream, moe_iqr_packmap_kernel, dim3(1), dim3(256), 0, g.off, g.n_expert,
|
||||
g.T, kRows * kMapTiles, kRows * kPackTiles, (uint32_t)cap, g.map);
|
||||
g.T, kRows * kMapTiles, kRows * kPackTiles, (uint32_t)cap, g.map, g.sorted,
|
||||
split_on ? (const int32_t*)split->data : nullptr, (uint32_t)top_k);
|
||||
} else {
|
||||
rc = r4d_moe_blockmap_emit(stream, g.off, g.n_expert, g.T, kRows * kMapTiles,
|
||||
(uint32_t)cap, g.map);
|
||||
@@ -948,7 +995,7 @@ int64_t moe_iqr_scratch(const RadArgs* a) {
|
||||
if (r4d_moe_in_decode_band(T, n_expert)) return 0;
|
||||
/* the run map's entries or the packed map's, whichever are more */
|
||||
return r4d_moe_blockmap_bytes(std::max(r4d_moe_blockmap_cap(T, n_expert, kRows * kMapTiles),
|
||||
pack_cap(T, n_expert)));
|
||||
pack_cap_split(T, n_expert)));
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
@@ -1139,6 +1139,7 @@ static const RadOperandSpec oMoeGemmQ[] = {
|
||||
{ "w_odd", RAD_OPD_WTAB, 0 },
|
||||
{ "w_odd_scale", RAD_OPD_WTAB, 0 },
|
||||
{ "y", RAD_OPD_OUT, 0 },
|
||||
{ "split", RAD_OPD_IN, 1 },
|
||||
};
|
||||
/* The same GEMM over four classes by expert number mod 4: `w`/`w_scale` hold class 0 at [N, K],
|
||||
* `w_q`/`w_q_scale` class q at [N_q, K_q], expert e at entry e / 4 of class e % 4. Four ranks
|
||||
@@ -1165,6 +1166,7 @@ static const RadOperandSpec oMoeGemmQMod4[] = {
|
||||
{ "w_3", RAD_OPD_WTAB, 0 },
|
||||
{ "w_3_scale", RAD_OPD_WTAB, 0 },
|
||||
{ "y", RAD_OPD_OUT, 0 },
|
||||
{ "split", RAD_OPD_IN, 1 },
|
||||
};
|
||||
static const RadParamSpec pMoeGather[] = {
|
||||
P_INT("M"), P_INT("n"), P_INT("top_k"), P_STR("dtype"),
|
||||
|
||||
@@ -8106,6 +8106,71 @@ static void moe_iqr_pack_case(const Plugin& pl, int64_t Mtok, bool down) {
|
||||
std::to_string(first / N) + " column " + std::to_string(first % N)
|
||||
: "the run map's bytes");
|
||||
}
|
||||
/* `split` (a checkpoint inside the step): with each expert's slots ascending, as the router's
|
||||
* stable sort leaves them, the rows of tokens before the split are a run of their own, so they
|
||||
* hold the bytes a pass of those tokens alone gives them -- whatever the rows after them. The
|
||||
* head pass is the same routing cut to those rows. */
|
||||
if (!down) {
|
||||
const int64_t cut = Mtok * 3 / 4 + 1; /* not on a tile, so the runs' remainders move */
|
||||
std::vector<int32_t> so2 = sorted, oh((size_t)E + 1, 0), soh;
|
||||
for (int64_t e = 0; e < E; ++e) {
|
||||
auto b = so2.begin() + off[(size_t)e], en = so2.begin() + off[(size_t)e + 1];
|
||||
/* live slots ascending, dropped ones (-1) last within the run */
|
||||
std::sort(b, en, [](int32_t x, int32_t y2) {
|
||||
return (x < 0) != (y2 < 0) ? y2 < 0 : x < y2; });
|
||||
oh[(size_t)e] = (int32_t)soh.size();
|
||||
for (auto it = b; it != en; ++it)
|
||||
if (*it >= 0 && *it < cut * top_k) soh.push_back(*it);
|
||||
}
|
||||
oh[(size_t)E] = (int32_t)soh.size();
|
||||
/* the dropped slots inside runs are not part of this check: drop them from the split run */
|
||||
std::vector<int32_t> so3, o3((size_t)E + 1, 0);
|
||||
for (int64_t e = 0; e < E; ++e) {
|
||||
o3[(size_t)e] = (int32_t)so3.size();
|
||||
for (int32_t i = off[(size_t)e]; i < off[(size_t)e + 1]; ++i)
|
||||
if (so2[(size_t)i] >= 0) so3.push_back(so2[(size_t)i]);
|
||||
}
|
||||
o3[(size_t)E] = (int32_t)so3.size();
|
||||
const int64_t live = (int64_t)so3.size();
|
||||
so3.resize((size_t)Tr, -1);
|
||||
soh.resize((size_t)Tr, -1);
|
||||
const int32_t cut32 = (int32_t)cut;
|
||||
void* d_cut = dalloc(4);
|
||||
st_memcpy(d_cut, &cut32, 4, hipMemcpyHostToDevice);
|
||||
auto launch = [&](const std::vector<int32_t>& so, const std::vector<int32_t>& of, bool sp,
|
||||
std::vector<uint16_t>& out) {
|
||||
st_memcpy(d_so, so.data(), (size_t)Tr * 4, hipMemcpyHostToDevice);
|
||||
st_memcpy(d_of, of.data(), (size_t)(E + 1) * 4, hipMemcpyHostToDevice);
|
||||
st_memcpy(d_y, poison.data(), poison.size() * 2, hipMemcpyHostToDevice);
|
||||
std::vector<RadTensor> t2 = ts;
|
||||
if (sp) t2.push_back(T(d_cut, RAD_I32, { 1 }));
|
||||
RadArgs a2 = a;
|
||||
a2.t = t2.data(); a2.n_t = (int)t2.size();
|
||||
const int rc = r->launch(&a2, (RadStream) nullptr);
|
||||
const bool fine = rc == RAD_OK && st_stream_sync(nullptr) == hipSuccess;
|
||||
if (fine) st_memcpy(out.data(), d_y, out.size() * 2, hipMemcpyDeviceToHost);
|
||||
return fine;
|
||||
};
|
||||
std::vector<uint16_t> ys((size_t)(Tr * N)), yh((size_t)(Tr * N));
|
||||
const std::string what = std::string(nm) + ": split at token " + std::to_string(cut) +
|
||||
", the rows before it are a pass of those tokens alone";
|
||||
if (!launch(so3, o3, true, ys) || !launch(soh, oh, false, yh)) {
|
||||
ok(what.c_str(), false, "launch failed");
|
||||
} else {
|
||||
int64_t diff = 0, rows = 0;
|
||||
for (int64_t e = 0; e < E; ++e)
|
||||
for (int32_t j = 0; j < oh[(size_t)e + 1] - oh[(size_t)e]; ++j) {
|
||||
const int64_t rs = o3[(size_t)e] + j, rh = oh[(size_t)e] + j;
|
||||
++rows;
|
||||
if (std::memcmp(&ys[(size_t)(rs * N)], &yh[(size_t)(rh * N)], (size_t)N * 2))
|
||||
++diff;
|
||||
}
|
||||
ok(what.c_str(), diff == 0 && rows > 0 && live > rows,
|
||||
std::to_string(diff) + " of " + std::to_string(rows) + " rows differ");
|
||||
}
|
||||
st_memcpy(d_so, sorted.data(), (size_t)Tr * 4, hipMemcpyHostToDevice);
|
||||
st_memcpy(d_of, off.data(), (size_t)(E + 1) * 4, hipMemcpyHostToDevice);
|
||||
}
|
||||
dfree_all();
|
||||
}
|
||||
|
||||
|
||||
@@ -1628,6 +1628,7 @@ static int moe_gemm_q_shape(const RadParam* p, int n_p, int operand, RadOpdDesc*
|
||||
opd(C, ne1, N1, K1), /* w_odd -- the odd experts */
|
||||
opd((uint32_t)RAD_BF16, ne1, srows(N1), (K1 + g - 1) / g), /* w_odd_scale */
|
||||
opd(D, M * k, N), /* y, in sorted order */
|
||||
opd_none(), /* split -- optional */
|
||||
};
|
||||
R4D_PICK(l);
|
||||
}
|
||||
@@ -1678,6 +1679,7 @@ static int moe_gemm_q_mod4_shape(const RadParam* p, int n_p, int operand, RadOpd
|
||||
opd_idx(ne + 1, 0, M * k + 1, -1), /* expert_offset */
|
||||
wq(1), sq(1), wq(2), sq(2), wq(3), sq(3), /* classes 1, 2, 3 */
|
||||
opd(D, M * k, N), /* y -- in SORTED order */
|
||||
opd_none(), /* split -- optional */
|
||||
};
|
||||
R4D_PICK(l);
|
||||
}
|
||||
|
||||
@@ -654,7 +654,8 @@ static const RadParamSpec pMoeGemmQ[] = { P_INT("M"), P_INT("N"), P_INT("K"), P_
|
||||
P_INTO("parts"), P_INTO("a_group") };
|
||||
static const RadOperandSpec oMoeGemmQ[] = { OPD("a"), OPD_O("a_scale"), WTAB("w"), WTAB("w_scale"),
|
||||
OPD("sorted_tok"), OPD("expert_offset"),
|
||||
WTAB("w_odd"), WTAB("w_odd_scale"), OUT("y") };
|
||||
WTAB("w_odd"), WTAB("w_odd_scale"), OUT("y"),
|
||||
OPD_O("split") };
|
||||
/* The same op over four classes by expert number mod 4: `w`/`w_scale` class 0 at [N, K],
|
||||
* `w_q`/`w_q_scale` class q at [N_q, K_q], expert e at entry e / 4 of class e % 4; how four ranks
|
||||
* share the extra block of a five-block expert (as in libr4d's schema). */
|
||||
@@ -667,7 +668,8 @@ static const RadOperandSpec oMoeGemmQMod4[] = { OPD("a"), OPD_O("a_scale"), WTAB
|
||||
WTAB("w_scale"), OPD("sorted_tok"),
|
||||
OPD("expert_offset"), WTAB("w_1"),
|
||||
WTAB("w_1_scale"), WTAB("w_2"), WTAB("w_2_scale"),
|
||||
WTAB("w_3"), WTAB("w_3_scale"), OUT("y") };
|
||||
WTAB("w_3"), WTAB("w_3_scale"), OUT("y"),
|
||||
OPD_O("split") };
|
||||
|
||||
/* With `shared` and `shared_gate`, the shared expert is folded in: y = bf16(shared * act(gate)) +
|
||||
* bf16(gathered), scale_rows' fused gate-and-add. `act` (sigmoid or none) applies to the scalar. */
|
||||
|
||||
@@ -2183,6 +2183,7 @@ int ref_moe_gemm_q_shape(const RadParam* p, int n_p, int operand, RadOpdDesc* ou
|
||||
if (mx) o.t_idx_dt(RAD_U8, { ne1, N1, (K1 + 31) / 32 }, 0, 127, 0); /* w_odd_scale */
|
||||
else o.t(SD, { ne1, (N1 + sg.rows - 1) / sg.rows, (K1 + sg.k - 1) / sg.k });
|
||||
o.t(D, { M * k, N }); /* y -- in sorted order */
|
||||
o.skip(); /* split -- optional */
|
||||
return o.finish("moe_gemm_q", operand, out);
|
||||
}
|
||||
|
||||
@@ -2228,6 +2229,7 @@ int ref_moe_gemm_q_mod4_shape(const RadParam* p, int n_p, int operand, RadOpdDes
|
||||
cls(2);
|
||||
cls(3);
|
||||
o.t(D, { M * k, N }); /* y -- in sorted order */
|
||||
o.skip(); /* split -- optional */
|
||||
return o.finish("moe_gemm_q_mod4", operand, out);
|
||||
}
|
||||
|
||||
@@ -2256,6 +2258,7 @@ int ref_moe_gemm_w4a8_shape(const RadParam* p, int n_p, int operand, RadOpdDesc*
|
||||
o.t((uint32_t)RAD_I4, { ne1, N1, K1 }); /* w_odd -- odd experts */
|
||||
o.t((uint32_t)RAD_BF16, { ne1, N1, K1 / 128 }); /* w_odd_scale */
|
||||
o.t((uint32_t)RAD_BF16, { M * k, N }); /* y -- in sorted order */
|
||||
o.skip(); /* split -- optional */
|
||||
return o.finish("moe_gemm_q", operand, out);
|
||||
}
|
||||
|
||||
@@ -2283,6 +2286,7 @@ int ref_moe_gemm_w4a4_shape(const RadParam* p, int n_p, int operand, RadOpdDesc*
|
||||
o.t((uint32_t)RAD_I4, { ne1, N1, K1 }); /* w_odd -- odd experts */
|
||||
o.t((uint32_t)RAD_BF16, { ne1, N1, K1 / 128 }); /* w_odd_scale */
|
||||
o.t((uint32_t)RAD_BF16, { M * k, N }); /* y -- in sorted order */
|
||||
o.skip(); /* split -- optional */
|
||||
return o.finish("moe_gemm_q", operand, out);
|
||||
}
|
||||
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
/* abi_layout_table.h -- every abi/ struct's size and field offsets at RAD_ABI_VERSION 42 and
|
||||
/* abi_layout_table.h -- every abi/ struct's size and field offsets at RAD_ABI_VERSION 43 and
|
||||
* RAD_FORMAT_VER 2, as { type, field, offset, size } rows ("" is the struct itself). A snapshot
|
||||
* of the headers on x86-64 Linux, which abi_layout_test.cpp holds the layout to; regenerate it
|
||||
* from the test's printout only together with a version bump. */
|
||||
@@ -23,6 +23,7 @@
|
||||
{ "RadArchProbe", "gen_max_pixels", 104, 8 },
|
||||
{ "RadArchProbe", "gen_max_refs", 112, 8 },
|
||||
{ "RadArchProbe", "gen_shape", 120, 4 },
|
||||
{ "RadArchProbe", "ckpt_snap_ok", 124, 4 },
|
||||
{ "RadArgs", "", 0, 72 },
|
||||
{ "RadArgs", "t", 0, 8 },
|
||||
{ "RadArgs", "n_t", 8, 4 },
|
||||
@@ -35,7 +36,7 @@
|
||||
{ "RadArgs", "world_size", 60, 4 },
|
||||
{ "RadArgs", "kv_used_max_h", 64, 4 },
|
||||
{ "RadArgs", "wgp_count", 68, 4 },
|
||||
{ "RadBatch", "", 0, 480 },
|
||||
{ "RadBatch", "", 0, 496 },
|
||||
{ "RadBatch", "phase", 0, 4 },
|
||||
{ "RadBatch", "step", 4, 4 },
|
||||
{ "RadBatch", "n_tok", 8, 8 },
|
||||
@@ -101,6 +102,8 @@
|
||||
{ "RadBatch", "bidi_seq", 456, 8 },
|
||||
{ "RadBatch", "bidi_used", 464, 8 },
|
||||
{ "RadBatch", "anchor_rows", 472, 8 },
|
||||
{ "RadBatch", "ckpt_cut", 480, 4 },
|
||||
{ "RadBatch", "ckpt_cu", 488, 8 },
|
||||
{ "RadBufDecl", "", 0, 80 },
|
||||
{ "RadBufDecl", "dtype", 0, 4 },
|
||||
{ "RadBufDecl", "rank", 4, 4 },
|
||||
|
||||
@@ -58,8 +58,8 @@ std::vector<Row> cpp_rows() {
|
||||
};
|
||||
}
|
||||
|
||||
/* The layout at RAD_ABI_VERSION 42 / RAD_FORMAT_VER 2 (see the file header). */
|
||||
constexpr uint32_t kTableAbi = 42, kTableFormat = 2;
|
||||
/* The layout at RAD_ABI_VERSION 43 / RAD_FORMAT_VER 2 (see the file header). */
|
||||
constexpr uint32_t kTableAbi = 43, kTableFormat = 2;
|
||||
const AbiRow kTable[] = {
|
||||
#include "abi_layout_table.h"
|
||||
};
|
||||
@@ -225,4 +225,14 @@ TEST(the_span_pass_field_is_appended_past_the_build_contexts_end) {
|
||||
CHECK_EQ(sizeof(RadBuildCtx), (size_t)208);
|
||||
}
|
||||
|
||||
/* ABI 43: a checkpoint inside the step. The probe's flag takes the tail padding behind gen_shape
|
||||
* (the probe keeps its 128 bytes); the batch's cut and bounds sit past its old end. */
|
||||
TEST(the_checkpoint_cut_fields_are_appended_past_the_old_ends) {
|
||||
CHECK_EQ(offsetof(RadArchProbe, ckpt_snap_ok), (size_t)124);
|
||||
CHECK_EQ(sizeof(RadArchProbe), (size_t)128);
|
||||
CHECK_EQ(offsetof(RadBatch, ckpt_cut), (size_t)480);
|
||||
CHECK_EQ(offsetof(RadBatch, ckpt_cu), (size_t)488);
|
||||
CHECK_EQ(sizeof(RadBatch), (size_t)496);
|
||||
}
|
||||
|
||||
RAD_TEST_MAIN()
|
||||
@@ -158,6 +158,11 @@ struct RadBuilder {
|
||||
const RecOp* op_of(rad_op h) const { return h ? &ops[h - 1] : nullptr; }
|
||||
};
|
||||
|
||||
struct SnapRec {
|
||||
size_t at; /* issues before the call */
|
||||
int group;
|
||||
int layer;
|
||||
};
|
||||
struct StageRec {
|
||||
size_t issue = 0; /* the issue count when the stage copy was issued */
|
||||
int layer = -1;
|
||||
@@ -181,6 +186,7 @@ struct RadCtx {
|
||||
bool stage_on = false;
|
||||
int stage_last = -1;
|
||||
std::vector<StageRec> stages;
|
||||
std::vector<SnapRec> snaps; /* rad_ckpt_snap calls */
|
||||
/* kv_stage_next: after which issue index the next layer's copy was handed over. */
|
||||
std::vector<StageRec> stages_next;
|
||||
};
|
||||
@@ -503,6 +509,13 @@ extern "C" int rad_lane_join(RadCtx* c, int from, int to) {
|
||||
c->joins.push_back({ c->issues.size(), from, to });
|
||||
return RAD_OK;
|
||||
}
|
||||
/* A checkpoint inside the step (RadBatch::ckpt_cut): recorded at its place in the issue stream,
|
||||
* so a test can find the snapshot between the recurrence's two pieces. */
|
||||
extern "C" int rad_ckpt_snap(RadCtx* c, rad_kvgroup g, int layer) {
|
||||
if (!c) return RAD_E_INVAL;
|
||||
c->snaps.push_back(SnapRec{ c->issues.size(), (int)g, layer });
|
||||
return RAD_OK;
|
||||
}
|
||||
/* No pool binding here, so a layer is at the index it names: the arch test uses layer 0 where it
|
||||
* checks the mirror offset. */
|
||||
extern "C" int rad_kv_layer_index(RadCtx* c, rad_kvgroup g, int layer) {
|
||||
|
||||
@@ -701,6 +701,64 @@ TEST(prefill_chunks_span_intervals_on_the_same_pieces) {
|
||||
CHECK_EQ(ck, -1);
|
||||
}
|
||||
|
||||
/* A plugin that snapshots inside a step (Scheduler::set_ckpt_inside): a lone spanned prefill's
|
||||
* last chunk runs to the prompt's end and carries the tail snapshot -- or, with none, the last
|
||||
* boundary it crosses -- inside it, at the position the cut would have put it. Not for a step
|
||||
* planned at the shared budget, and not under --prefill-tail-snapshot off. */
|
||||
TEST(a_lone_spanned_prefill_takes_its_tail_snapshot_inside_its_last_chunk) {
|
||||
Fixture f;
|
||||
f.build(); /* interval 256, budget 512, quantum 64 */
|
||||
f.cfg.prefill_span = 2;
|
||||
CHECK_OK(f.start());
|
||||
f.sch.set_ckpt_inside(true);
|
||||
int64_t ck = -1;
|
||||
/* The tail: [512, 1000) with the snapshot at 960, where the cut ended the chunk. */
|
||||
CHECK_EQ(f.sch.plan_chunk(512, 1000, 512, &ck), 488);
|
||||
CHECK_EQ(ck, 960);
|
||||
/* No tail snapshot: the last boundary crossed, 768, inside [512, 830). */
|
||||
CHECK_EQ(f.sch.plan_chunk(512, 830, 512, &ck), 318);
|
||||
CHECK_EQ(ck, 768);
|
||||
/* Mid-prompt chunks and a prompt ending on its tail are as before. */
|
||||
CHECK_EQ(f.sch.plan_chunk(256, 2000, 512, &ck), 512);
|
||||
CHECK_EQ(ck, 768);
|
||||
CHECK_EQ(f.sch.plan_chunk(768, 960, 512, &ck), 192);
|
||||
CHECK_EQ(ck, 960);
|
||||
/* Every position matches the cut schedule's. */
|
||||
Fixture g;
|
||||
g.build();
|
||||
g.cfg.prefill_span = 2;
|
||||
CHECK_OK(g.start());
|
||||
int bad = 0;
|
||||
for (int64_t target = 300; target <= 2600; target += 7) {
|
||||
std::vector<int64_t> a, b;
|
||||
for (int64_t n = 0; n < target; ) {
|
||||
int64_t c1 = -1;
|
||||
const int64_t w = g.sch.plan_chunk(n, target, 512, &c1);
|
||||
if (w <= 0) break;
|
||||
n += w;
|
||||
if (c1 >= 0) a.push_back(c1);
|
||||
}
|
||||
for (int64_t n = 0; n < target; ) {
|
||||
int64_t c1 = -1;
|
||||
const int64_t w = f.sch.plan_chunk(n, target, 512, &c1);
|
||||
if (w <= 0) break;
|
||||
n += w;
|
||||
if (c1 >= 0) b.push_back(c1);
|
||||
}
|
||||
bad += a != b;
|
||||
}
|
||||
CHECK_EQ(bad, 0);
|
||||
|
||||
Fixture o;
|
||||
o.build();
|
||||
o.cfg.prefill_span = 2;
|
||||
o.cfg.prefill_tail_snapshot = false;
|
||||
CHECK_OK(o.start());
|
||||
o.sch.set_ckpt_inside(true);
|
||||
CHECK_EQ(o.sch.plan_chunk(512, 1000, 512, &ck), 488);
|
||||
CHECK_EQ(ck, -1);
|
||||
}
|
||||
|
||||
/* A span moves no checkpoint and no piece, from a prompt's start or from any checkpoint a turn
|
||||
* resumes from: every chunk end and checkpoint it makes is one a span of 1 makes, and the deepest
|
||||
* checkpoint (where the next turn resumes) is the same. */
|
||||
|
||||
Reference in new issue
Block a user