Archived
radiance 1.4.0
- Qwen3.8-Flash-Next on one card: the heat engine moves 64 experts a step (was 8), its copies and the prefill's expert staging run on the card's DMA engines instead of as copy kernels, and at decode the next layer's likeliest experts are copied onto the card ahead of its GEMMs. Against 1.3.0 at --tp 1: prefill +17-20% from 8K tokens up, throughput under load +16-20% - deploy/compose/flashnext-tp1.yaml: Qwen3.8-Flash-Next on one card and 64 GB of host RAM (4096-token prefill steps, 4 sequences, a 301K-token cache) - #61: --busy-prefill-chunk (default auto): a long prefill leaves the rest of the step to the requests decoding beside it and to new arrivals, instead of taking all of it (baKon) - LiquidAI's LFM2 and LFM2-VL, and d1's /v1/systemone (after zivius's PR #50) - a startup report: every option with its value and where it came from, the hardware, and what on it costs speed with the fix; /diagnostics serves it (behind the API key; without one, only to the same machine) - #69: a GPU memory fault names the buffer and kernel argument behind it and ends the server at once (exit status 70), instead of hanging or writing a core of tens of gigabytes - #64: a card without Resizable BAR is refused at startup, naming what to turn on, instead of crashing at the first launch - the image is built with ROCm 10.1 (the host's amdgpu driver must support it), with ROCm's libLLVM rebuilt so the same source compiles to the same kernels every time; a source build takes ROCm 10.1 or 7.2 - #71: the image ships curl, jq and a CA bundle, so --model <HF repo id> works in the container, and hfget.sh runs under its busybox (baKon) - --profile-ops shows how long each op's lane sat idle before it; cardclock.py skips an integrated GPU instead of stopping
This commit is contained in:
1 parent
d5b65cddc6
commit
dc339b4cd2
153 files changed
+12956
-1067
No files matched your search
@@ -168,6 +168,11 @@ struct AttnGatedFP8 {
|
||||
bool fold = false, bool add_out = true,
|
||||
int ar_in = kArNone, bool ar_out_ = false);
|
||||
void step(RadCtx* c, const RadBatch* batch) const;
|
||||
/* The pass's K and V into the cache and nothing else: no query, no attention, no output. What a
|
||||
* pass that only leaves the layer's state behind asks for -- the MTP head's history pass over
|
||||
* a prompt chunk with no row that drafts. The keys are the ones step() stores: the same
|
||||
* projection, norm, rotation and store. */
|
||||
void store_kv(RadCtx* c, const RadBatch* batch) const;
|
||||
};
|
||||
|
||||
inline int AttnGatedFP8::declare(RadBuilder* b, Names& nm, const Geom& geom, int l,
|
||||
@@ -470,6 +475,20 @@ inline int AttnGatedFP8::declare(RadBuilder* b, Names& nm, const Geom& geom, int
|
||||
return RAD_OK;
|
||||
}
|
||||
|
||||
inline void AttnGatedFP8::store_kv(RadCtx* c, const RadBatch* batch) const {
|
||||
const int64_t T = batch->n_tok;
|
||||
if (!ext_in) nq_h.step(c, T);
|
||||
if (op_fill) return; /* the attention-zero rank holds no heads to store */
|
||||
const RadKVGroupBatch* kvb = kv_batch(batch, kv);
|
||||
const int32_t* slot = kvb ? kvb->slot_mapping : nullptr;
|
||||
kp.step(c, w.h, w.k, T);
|
||||
vp.step(c, w.h, w.v, T);
|
||||
RAD_ISSUE_N(c, op_k_norm, T * g.n_head_kv, brows(w.k, T), RAD_W(w_k_norm), brows(w.k, T));
|
||||
RAD_ISSUE(c, op_rope_k, brows(w.k, T), rope_posmc(g, batch, T));
|
||||
RAD_ISSUE(c, op_kv_store,
|
||||
brows(w.k, T), brows(w.v, T), praw(slot, RAD_I32, T), kv_cache(kv, layer));
|
||||
}
|
||||
|
||||
inline void AttnGatedFP8::step(RadCtx* c, const RadBatch* batch) const {
|
||||
const RadKVGroupBatch* kvb = kv_batch(batch, kv);
|
||||
const int32_t* slot = kvb ? kvb->slot_mapping : nullptr;
|
||||
|
||||
@@ -90,6 +90,8 @@ struct MlpFP8 {
|
||||
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;
|
||||
/* The block over the first `T` rows of its buffers (MoeFP8::step_rows says who asks). */
|
||||
void step_rows(RadCtx* c, int64_t T) const { slice(c, T, 0, T); }
|
||||
/* One row slice of the whole block; `rows <= 0` means the rest of the step. */
|
||||
void slice(RadCtx* c, int64_t T, int64_t r0, int64_t rows) const;
|
||||
};
|
||||
|
||||
@@ -387,6 +387,16 @@ struct MoeFP8 {
|
||||
* `pff`, and down into the same `edn` rows the quantised down left zero. */
|
||||
std::vector<int64_t> prot;
|
||||
const char* prot_list = nullptr;
|
||||
/* THE NEXT-LAYER EXPERT PREFETCH (rad_expert_prefetch). `xp_next` is the next routed layer, which
|
||||
* this one predicts for at its router input -- the architecture links them once every layer is
|
||||
* declared. `xp` is what this layer is to the one before it: its router, and each of the model's
|
||||
* experts' unit -- the four handles its place's tables hold, and its place in the routing counts.
|
||||
* Quantised experts only: a bf16 layer and the protected experts are not described, and a layer
|
||||
* with no description is predicted for by nobody. */
|
||||
const MoeFP8* xp_next = nullptr;
|
||||
RadPrefetchNext xp{};
|
||||
std::vector<rad_weight> xp_parts;
|
||||
std::vector<int32_t> xp_count_at;
|
||||
int64_t n_reg = 0;
|
||||
/* How many weight-table classes the quantised GEMMs take: 2 by parity (`moe_gemm_q`), or 4 by
|
||||
* number mod 4 (`moe_gemm_q_mod4`, Config::ff_lo4). */
|
||||
@@ -461,6 +471,10 @@ 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;
|
||||
/* The block over the first `T` rows of its buffers, which a pass that runs it for fewer rows
|
||||
* than its batch has asks for: the MTP head's history pass, which reads the head's output only
|
||||
* at its drafting rows. */
|
||||
void step_rows(RadCtx* c, int64_t T) const;
|
||||
/* One pass of at most `c.rows` tokens. */
|
||||
void pass(RadCtx* c, int64_t T, int64_t r0, int64_t rows) const;
|
||||
void shared_up(RadCtx* c, int64_t T, int64_t r0, int64_t rows, bool gate_done = false) const;
|
||||
@@ -1469,6 +1483,29 @@ inline int MoeFP8::declare(RadBuilder* b, Names& nm, const Geom& geom, int l,
|
||||
RAD_STR("dtype", g.dtype)),
|
||||
RAD_NOWEIGHTS),
|
||||
{w.x, w.h.x}, {w.x});
|
||||
|
||||
/* The layer as the prefetch sees it, by the model's expert number -- what the router ranks. */
|
||||
if (!c.expert_bf16 && c.n_expert_all > 0 && n_reg > 0) {
|
||||
const int64_t na = c.n_expert_all;
|
||||
xp_parts.assign((size_t)(na * 4), 0);
|
||||
xp_count_at.assign((size_t)na, -1);
|
||||
for (int64_t s = 0; s < n_reg; ++s) {
|
||||
const int64_t e = c.expert_base + reg[(size_t)s];
|
||||
if (e < 0 || e >= na) continue;
|
||||
xp_parts[(size_t)(e * 4 + 0)] = w_gu[(size_t)s];
|
||||
xp_parts[(size_t)(e * 4 + 1)] = w_gus[(size_t)s];
|
||||
xp_parts[(size_t)(e * 4 + 2)] = w_dn[(size_t)s];
|
||||
xp_parts[(size_t)(e * 4 + 3)] = w_dns[(size_t)s];
|
||||
xp_count_at[(size_t)e] = (int32_t)s;
|
||||
}
|
||||
xp.layer = layer;
|
||||
xp.router = w_router;
|
||||
xp.n_expert = na;
|
||||
xp.top_k = c.top_k;
|
||||
xp.n_parts = 4;
|
||||
xp.parts = xp_parts.data();
|
||||
xp.count_at = xp_count_at.data();
|
||||
}
|
||||
return RAD_OK;
|
||||
}
|
||||
|
||||
@@ -1516,6 +1553,10 @@ inline void MoeFP8::pass(RadCtx* c_, int64_t T, int64_t r0, int64_t rows) const
|
||||
* sharing pool (see declare), so issuing it ahead of its declared position moves no byte. */
|
||||
|
||||
/* THE ROUTED ARM. */
|
||||
/* THE NEXT ROUTED LAYER'S PREDICTION, from this router's input, read in place behind the router
|
||||
* on the compute stream (rad_expert_prefetch). A failure there costs the prediction, nothing else. */
|
||||
if (xp_next && xp_next->xp.n_parts > 0)
|
||||
(void)rad_expert_prefetch(c_, &xp_next->xp, w.h.x, r0, rows, g.n_embd);
|
||||
/* THE COUNTS GO STRAIGHT INTO THE CORE'S ROW for this layer when it keeps one, so the
|
||||
* report at the end of step() hands them over with no copy -- otherwise a card-local blit
|
||||
* a routed layer, a whole dependent dispatch for a kilobyte. */
|
||||
@@ -1564,6 +1605,8 @@ inline void MoeFP8::pass(RadCtx* c_, int64_t T, int64_t r0, int64_t rows) const
|
||||
* 2 * i + q, and each parity is its own pair of table operands; at four classes expert
|
||||
* 4 * i + q, class q holding ceil((n_reg - q) / 4). */
|
||||
const int64_t n4[4] = { (n_reg + 3) / 4, (n_reg + 2) / 4, (n_reg + 1) / 4, n_reg / 4 };
|
||||
/* The layer before predicted for this one: its copies go into the tables here, where they landed. */
|
||||
(void)rad_expert_prefetch_step(c_, layer);
|
||||
if (c.expert_bf16)
|
||||
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),
|
||||
@@ -1608,6 +1651,8 @@ inline void MoeFP8::pass(RadCtx* c_, int64_t T, int64_t r0, int64_t rows) const
|
||||
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));
|
||||
/* This layer's streamed experts are read: the next one's copies cross the link from here. */
|
||||
(void)rad_expert_prefetch_mark(c_);
|
||||
/* THE PROTECTED EXPERTS, over the trailing run of the offsets: their sorted rows are the
|
||||
* ones the quantised down GEMM above zeroed as past its live count, and the down GEMM
|
||||
* here writes them -- after it, which the shared `edn` orders. */
|
||||
@@ -1733,7 +1778,10 @@ inline void MoeFP8::calib_experts_pass(RadCtx* c_, int64_t slots) const {
|
||||
}
|
||||
|
||||
inline void MoeFP8::step(RadCtx* c_, const RadBatch* batch) const {
|
||||
const int64_t T = batch->n_tok;
|
||||
step_rows(c_, batch->n_tok);
|
||||
}
|
||||
|
||||
inline void MoeFP8::step_rows(RadCtx* c_, int64_t T) const {
|
||||
for (int64_t r0 = 0; r0 < T; r0 += c.rows) {
|
||||
const int64_t n = (T - r0 < c.rows) ? (T - r0) : c.rows;
|
||||
pass(c_, T, r0, n);
|
||||
|
||||
@@ -288,6 +288,24 @@ inline int MtpBlockFP8<Ffn>::declare(RadBuilder* b, Names& nm, const Geom& geom,
|
||||
{w.c}, {w.x});
|
||||
|
||||
RAD_ARCH_TRY(attn.declare(b, nm, g, l, kv, rotary_dim, w.attn, src.attn));
|
||||
|
||||
/* DRAFT ROUND 1'S ROWS OUT OF A HISTORY PASS (RadBatch::draft_out_ids), one a drafting
|
||||
* sequence in round order, to the front of the residual stream `x` -- before the feed-forward,
|
||||
* which then runs over those rows alone. Through `hout`, which is dead from the fc's read to
|
||||
* the norm's write, because a gather in place could overwrite a row another sequence has yet to
|
||||
* read. */
|
||||
op_dsel = rw(b, RAD_OP(b, "gather_rows",
|
||||
RAD_PARAMS(RAD_RANGE("M", 1, g.max_logit_rows), RAD_INT("n", g.n_embd),
|
||||
RAD_STR("dtype", g.dtype)),
|
||||
RAD_NOWEIGHTS),
|
||||
{w.x}, {w.hout});
|
||||
op_dback = rw(b, RAD_OP(b, "cast",
|
||||
RAD_PARAMS(RAD_RANGE("M", 1, g.max_logit_rows), RAD_INT("n", g.n_embd),
|
||||
RAD_STR("from", g.dtype), RAD_STR("to", g.dtype)),
|
||||
RAD_NOWEIGHTS),
|
||||
{w.hout}, {w.x});
|
||||
if (!op_dsel || !op_dback) return RAD_E_INVAL;
|
||||
|
||||
RAD_ARCH_TRY(mlp.declare(b, nm, g, l, w.mlp, src.mlp));
|
||||
|
||||
op_norm = rw(b, RAD_OP(b, "rmsnorm",
|
||||
@@ -297,22 +315,6 @@ inline int MtpBlockFP8<Ffn>::declare(RadBuilder* b, Names& nm, const Geom& geom,
|
||||
RAD_WEIGHTS(w_norm)),
|
||||
{w.x}, {w.hout});
|
||||
|
||||
/* DRAFT ROUND 1'S ROWS OUT OF A HISTORY PASS (RadBatch::draft_out_ids), one a drafting
|
||||
* sequence in round order, to the front of `hout` -- where the lm_head reads a round's rows
|
||||
* and round 2 reads its input. Through `x`, which is dead once the norm has read it, because a
|
||||
* gather in place could overwrite a row another sequence has yet to read. */
|
||||
op_dsel = rw(b, RAD_OP(b, "gather_rows",
|
||||
RAD_PARAMS(RAD_RANGE("M", 1, g.max_logit_rows), RAD_INT("n", g.n_embd),
|
||||
RAD_STR("dtype", g.dtype)),
|
||||
RAD_NOWEIGHTS),
|
||||
{w.hout}, {w.x});
|
||||
op_dback = rw(b, RAD_OP(b, "cast",
|
||||
RAD_PARAMS(RAD_RANGE("M", 1, g.max_logit_rows), RAD_INT("n", g.n_embd),
|
||||
RAD_STR("from", g.dtype), RAD_STR("to", g.dtype)),
|
||||
RAD_NOWEIGHTS),
|
||||
{w.x}, {w.hout});
|
||||
if (!op_dsel || !op_dback) return RAD_E_INVAL;
|
||||
|
||||
/* The trunk's lm_head, over the head's hidden state. Banded at the logits buffer's own extent
|
||||
* rather than at `rows`: it runs over one row a drafting sequence. */
|
||||
op_logits = rw(b, RAD_OP(b, "logits_gemm",
|
||||
@@ -435,27 +437,32 @@ inline void MtpBlockFP8<Ffn>::step(RadCtx* c, const RadBatch* batch) const {
|
||||
|
||||
RAD_ISSUE(c, op_fc, brows(w.c, T), RAD_W(w_fc), brows(w.x, T));
|
||||
|
||||
/* A HISTORY PASS THAT DRAFTS NOTHING ONLY LEAVES K AND V BEHIND, and they come from the
|
||||
* block's input. The query, the attention, its output, the feed-forward, the norm and the
|
||||
* lm_head compute the head's output, which only a drafting row reads -- and every prefill
|
||||
* chunk but a prompt's last drafts nothing. `n_draft_out` is the batch's and the same on every
|
||||
* rank, so a collective skipped here is skipped on all of them. */
|
||||
if (r < 0 && batch->n_draft_out <= 0) {
|
||||
attn.store_kv(c, batch);
|
||||
return;
|
||||
}
|
||||
attn.step(c, batch);
|
||||
|
||||
/* A HISTORY PASS THAT DRAFTS NOTHING ENDS HERE. It exists to leave K and V behind, and the
|
||||
* attention above has stored them. The feed-forward, the norm and the lm_head compute the
|
||||
* head's output, which only a drafting row reads -- and every prefill chunk but a prompt's
|
||||
* last drafts nothing. `n_draft_out` is the batch's and the same on every rank, so a
|
||||
* collective the feed-forward would run is skipped on all of them. */
|
||||
if (r < 0 && batch->n_draft_out <= 0) return;
|
||||
|
||||
mlp.step(c, batch);
|
||||
|
||||
RAD_ISSUE(c, op_norm, brows(w.x, T), RAD_W(w_norm), brows(w.hout, T));
|
||||
|
||||
/* A history pass that drafts brings round 1's rows to the front first. */
|
||||
int64_t L = T; /* the rows the lm_head runs over */
|
||||
/* A HISTORY PASS THAT DRAFTS GOES ON WITH ITS DRAFTING ROWS ALONE. They are round 1, and
|
||||
* nothing reads the head's output at any other row: the feed-forward, the norm and the lm_head
|
||||
* would run over a whole prompt chunk for a handful of rows. So round 1's rows come to the
|
||||
* front of `x` first, and everything after runs as a pass of those rows. */
|
||||
int64_t L = T; /* the rows the stream goes on with */
|
||||
if (r < 0) {
|
||||
L = batch->n_draft_out;
|
||||
RAD_ISSUE_N(c, op_dsel, L, RAD_B(w.hout), praw(batch->draft_out_ids, RAD_I32, L),
|
||||
brows(w.x, L));
|
||||
RAD_ISSUE_N(c, op_dback, L, brows(w.x, L), brows(w.hout, L));
|
||||
RAD_ISSUE_N(c, op_dsel, L, RAD_B(w.x), praw(batch->draft_out_ids, RAD_I32, L),
|
||||
brows(w.hout, L));
|
||||
RAD_ISSUE_N(c, op_dback, L, brows(w.hout, L), brows(w.x, L));
|
||||
}
|
||||
|
||||
mlp.step_rows(c, L);
|
||||
|
||||
RAD_ISSUE(c, op_norm, brows(w.x, L), RAD_W(w_norm), brows(w.hout, L));
|
||||
if (q2) {
|
||||
/* `dsum` is GROUP-major [n_embd/group][ceil(L/16)*16] and not row-major, so it is passed
|
||||
* whole: there is no row slice of it that means anything. */
|
||||
|
||||
@@ -292,6 +292,22 @@ inline int MtpHcBlock::declare(RadBuilder* b, Names& nm, const Geom& geom, int l
|
||||
false, false, kArNone, hc_attn.op_ar_write != 0));
|
||||
if (src.qsa.qk_proj)
|
||||
RAD_ARCH_TRY(qsa.declare(b, nm, g, l, src.qsa, w.qsa, w.attn.h, qsa_bk));
|
||||
/* ---- draft round 1's rows out of a history pass (RadBatch::draft_out_ids), before the
|
||||
* feed-forward, which then runs over them alone: gathered into `hin`, one row a drafting
|
||||
* sequence in round order, and copied back to the front of `hw`. Two ops and not one because a
|
||||
* gather in place could overwrite a row another sequence has yet to read. */
|
||||
op_dsel = rw(b, RAD_OP(b, "gather_rows",
|
||||
RAD_PARAMS(RAD_RANGE("M", 1, g.max_logit_rows), RAD_INT("n", HN),
|
||||
RAD_STR("dtype", g.dtype)),
|
||||
RAD_NOWEIGHTS),
|
||||
{w.hw}, {w.hin});
|
||||
op_dback = rw(b, RAD_OP(b, "cast",
|
||||
RAD_PARAMS(RAD_RANGE("M", 1, g.max_logit_rows), RAD_INT("n", HN),
|
||||
RAD_STR("from", g.dtype), RAD_STR("to", g.dtype)),
|
||||
RAD_NOWEIGHTS),
|
||||
{w.hin}, {w.hw});
|
||||
if (!op_dsel || !op_dback) return RAD_E_INVAL;
|
||||
|
||||
/* The rotated expert input rides in this read, as it does on the trunk. */
|
||||
HyperConn::Config mcfg = hc;
|
||||
HyperConn::Wire mwire{w.hw, w.x, w.inj};
|
||||
@@ -321,21 +337,6 @@ inline int MtpHcBlock::declare(RadBuilder* b, Names& nm, const Geom& geom, int l
|
||||
RAD_STR("from", g.dtype), RAD_STR("to", g.dtype)),
|
||||
RAD_NOWEIGHTS),
|
||||
{w.hw}, {w.hin});
|
||||
/* ---- draft round 1's rows out of a history pass (RadBatch::draft_out_ids): gathered into the
|
||||
* carry, one row a drafting sequence in round order, and copied back to the front of `hw` for
|
||||
* the mixer. Two ops and not one because a gather in place could overwrite a row another
|
||||
* sequence has yet to read. */
|
||||
op_dsel = rw(b, RAD_OP(b, "gather_rows",
|
||||
RAD_PARAMS(RAD_RANGE("M", 1, g.max_logit_rows), RAD_INT("n", HN),
|
||||
RAD_STR("dtype", g.dtype)),
|
||||
RAD_NOWEIGHTS),
|
||||
{w.hw}, {w.hin});
|
||||
op_dback = rw(b, RAD_OP(b, "cast",
|
||||
RAD_PARAMS(RAD_RANGE("M", 1, g.max_logit_rows), RAD_INT("n", HN),
|
||||
RAD_STR("from", g.dtype), RAD_STR("to", g.dtype)),
|
||||
RAD_NOWEIGHTS),
|
||||
{w.hin}, {w.hw});
|
||||
if (!op_dsel || !op_dback) return RAD_E_INVAL;
|
||||
|
||||
/* ---- the head's own mixer: the connection with no write half, which collapses its wide
|
||||
* stream to the one hidden state the lm_head reads. No fp8 twin -- that input is bf16. */
|
||||
@@ -523,40 +524,51 @@ inline void MtpHcBlock::step(RadCtx* c, const RadBatch* batch) const {
|
||||
RAD_W(w_fc_h), RAD_W(w_fc_e), brows(w.hw, T));
|
||||
|
||||
hc_attn.read(c, T, 0, T);
|
||||
|
||||
/* A HISTORY PASS THAT DRAFTS NOTHING ONLY LEAVES THE HEAD'S STATE BEHIND: the K and V of its
|
||||
* rows and the indexer's block keys and tail, all of which come from the block's input. The
|
||||
* query, the indexer's scores and selection, the attention, its output and everything after
|
||||
* compute the head's stream, which only a drafting row reads. Every prefill chunk but a
|
||||
* prompt's last is such a pass. `n_draft_out` is the batch's and the same on every rank, so
|
||||
* the collectives skipped here are skipped on all of them. */
|
||||
if (r < 0 && batch->n_draft_out <= 0) {
|
||||
qsa.store(c, attn.w.h, batch);
|
||||
attn.store_kv(c, batch);
|
||||
return;
|
||||
}
|
||||
|
||||
/* THE INDEXER RUNS FIRST, because the attention reads its selection. It can: this block's
|
||||
* input is normed and quantised by the connection above, so `w.h` is already what it will be
|
||||
* when the attention runs. */
|
||||
qsa.step(c, attn.w.h, batch);
|
||||
attn.step(c, batch);
|
||||
|
||||
/* A HISTORY PASS THAT DRAFTS NOTHING ENDS HERE. It exists to leave the head's state behind --
|
||||
* the K and V the attention just stored and the indexer's block keys and tail -- and both are
|
||||
* written above. Everything below computes the head's stream, which only a drafting row
|
||||
* reads: the experts, the two connections around them and the lm_head. Every prefill chunk
|
||||
* but a prompt's last is such a pass, so on those the head costs its attention alone.
|
||||
* `n_draft_out` is the batch's and the same on every rank, so the collectives skipped here
|
||||
* are skipped on all of them. */
|
||||
if (r < 0 && batch->n_draft_out <= 0) return;
|
||||
|
||||
hc_attn.write(c, T, 0, T);
|
||||
|
||||
hc_mlp.read(c, T, 0, T);
|
||||
mlp.step(c, batch);
|
||||
hc_mlp.write(c, T, 0, T);
|
||||
|
||||
/* THE CARRY, and only where a round after this one will read it. The last round of a group
|
||||
* is followed by a trunk step. A history pass that drafts takes round 1's rows out of its own
|
||||
* -- into the carry, and back to the front of `hw` for the mixer below. */
|
||||
int64_t L = T; /* the rows the lm_head runs over */
|
||||
/* A HISTORY PASS THAT DRAFTS GOES ON WITH ITS DRAFTING ROWS ALONE. They are round 1, and
|
||||
* nothing reads the head's stream at any other row: the experts, their connection and the
|
||||
* lm_head would run over a whole prompt chunk for a handful of rows. So round 1's rows come out
|
||||
* of `hw` first -- gathered into `hin` and copied back to its front, two ops because a gather
|
||||
* in place could overwrite a row another sequence has yet to read -- and everything after runs
|
||||
* as a pass of those rows. */
|
||||
int64_t L = T; /* the rows the stream goes on with */
|
||||
if (r < 0) {
|
||||
L = batch->n_draft_out;
|
||||
RAD_ISSUE_N(c, op_dsel, L, RAD_B(w.hw), praw(batch->draft_out_ids, RAD_I32, L),
|
||||
brows(w.hin, L));
|
||||
RAD_ISSUE_N(c, op_dback, L, brows(w.hin, L), brows(w.hw, L));
|
||||
} else if (r < depth) {
|
||||
RAD_ISSUE_N(c, op_carry, T, brows(w.hw, T), brows(w.hin, T));
|
||||
}
|
||||
|
||||
hc_mlp.read(c, L, 0, L);
|
||||
mlp.step_rows(c, L);
|
||||
hc_mlp.write(c, L, 0, L);
|
||||
|
||||
/* THE CARRY, and only where a round after this one will read it. The last round of a group
|
||||
* is followed by a trunk step; a history pass is round 1, followed by round 2 when the
|
||||
* deployment drafts deeper. */
|
||||
if (r < 0 ? depth > 1 : r < depth)
|
||||
RAD_ISSUE_N(c, op_carry, L, brows(w.hw, L), brows(w.hin, L));
|
||||
|
||||
mixer.read(c, L, 0, L);
|
||||
if (q2) {
|
||||
/* `dsum` is GROUP-major [n_embd/group][ceil(L/16)*16] and not row-major, so it is passed
|
||||
|
||||
+51
-28
@@ -154,6 +154,11 @@ struct QsaIndexer {
|
||||
int declare(RadBuilder* b, Names& nm, const Geom& geom, int l, const Config& cfg,
|
||||
const Wire& wire, const ActFP8& in, rad_kvgroup* bk);
|
||||
void step(RadCtx* c, const ActFP8& in, const RadBatch* batch) const;
|
||||
/* The block keys and the tail of the pass's rows and nothing else: no query, no score, no
|
||||
* selection. What a pass that only leaves the layer's state behind asks for (see
|
||||
* AttnGatedFP8::store_kv); the stores are the ones step() makes. */
|
||||
void store(RadCtx* c, const ActFP8& in, const RadBatch* batch) const;
|
||||
bool store_keys(RadCtx* c, const RadBatch* batch) const;
|
||||
};
|
||||
|
||||
inline int QsaIndexer::declare(RadBuilder* b, Names& nm, const Geom& geom, int l,
|
||||
@@ -314,38 +319,16 @@ inline int QsaIndexer::declare(RadBuilder* b, Names& nm, const Geom& geom, int l
|
||||
return RAD_OK;
|
||||
}
|
||||
|
||||
inline void QsaIndexer::step(RadCtx* c_, const ActFP8& in, const RadBatch* batch) const {
|
||||
if (!on()) return;
|
||||
/* THE KEYS A PASS LEAVES BEHIND: the block keys of the blocks its rows complete and the tail of
|
||||
* raw keys after them, out of the projection's raw-key head. False when the step carries no batch
|
||||
* for the indexer's groups. */
|
||||
inline bool QsaIndexer::store_keys(RadCtx* c_, const RadBatch* batch) const {
|
||||
const int64_t T = batch->n_tok, S = batch->n_seq;
|
||||
const int64_t qw = c.heads * c.head_dim;
|
||||
const int64_t nw = (c.heads + 1) * c.head_dim;
|
||||
const int64_t koff = qw; /* the raw-key head, after the query heads */
|
||||
|
||||
proj.step(c_, in, w.qk, T);
|
||||
/* ONE LAUNCH OR `heads` + 1. The fused form takes the projection WHOLE -- the kernel picks
|
||||
* head `h` out of it at the operand's own row pitch, so the strided view the unfused norm
|
||||
* needs is not taken at all -- and it writes the query heads contiguous and rotated, which is
|
||||
* what the pair below leaves behind. `n_head` 0 is the k-only form: no q_gate, no q_w, no
|
||||
* q_out, and the ninth operand (`gate_out`) is absent as it is everywhere else.
|
||||
*
|
||||
* `T <= qprep_rows` IS THE TABLE'S BOUND and declare() says why it is correctness. */
|
||||
if (op_qprep && T <= qprep_rows && !rope_mixed(batch)) {
|
||||
RAD_ISSUE_N(c_, op_qprep, T,
|
||||
RAD_NONE, brows(w.qk, T), RAD_B(w.cos_sin), rope_pos1(batch, T),
|
||||
RAD_NONE, RAD_W(w_qn),
|
||||
RAD_NONE, brows(w.q, T), RAD_NONE);
|
||||
} else {
|
||||
for (int64_t h = 0; h < c.heads; ++h)
|
||||
RAD_ISSUE_N(c_, op_norm, T,
|
||||
bcol_at(w.qk, 0, nw, h * c.head_dim, c.head_dim, T), RAD_W(w_qn),
|
||||
bcol_at(w.q, 0, qw, h * c.head_dim, c.head_dim, T));
|
||||
RAD_ISSUE_N(c_, op_rope, T, brows(w.q, T), rope_posmc(g, batch, T));
|
||||
}
|
||||
|
||||
const int64_t koff = c.heads * c.head_dim; /* the raw-key head, after the query heads */
|
||||
const RadKVGroupBatch* bkb = kv_batch(batch, kv_bk);
|
||||
const RadKVGroupBatch* atb = kv_batch(batch, c.kv_attn);
|
||||
const RadKVGroupBatch* tlb = kv_batch(batch, c.kv_tail);
|
||||
if (!bkb || !atb || !tlb) return;
|
||||
if (!bkb || !tlb) return false;
|
||||
/* COLUMN 0 OF THE STATE ROW IS THE COMMITTED SLOT. The row is wider than one whenever the
|
||||
* deployment speculates -- the manager gives every linear group a second copy then, because
|
||||
* the kernel that needed one asked for it -- and this op wants the committed slot in every
|
||||
@@ -389,6 +372,46 @@ inline void QsaIndexer::step(RadCtx* c_, const ActFP8& in, const RadBatch* batch
|
||||
praw(batch->cu_seqlens, RAD_I32, S + 1),
|
||||
kv_cache(c.kv_tail, layer),
|
||||
praw2(tlb->state_index, RAD_I32, S, tw));
|
||||
return true;
|
||||
}
|
||||
|
||||
inline void QsaIndexer::store(RadCtx* c_, const ActFP8& in, const RadBatch* batch) const {
|
||||
if (!on()) return;
|
||||
proj.step(c_, in, w.qk, batch->n_tok);
|
||||
(void)store_keys(c_, batch);
|
||||
}
|
||||
|
||||
inline void QsaIndexer::step(RadCtx* c_, const ActFP8& in, const RadBatch* batch) const {
|
||||
if (!on()) return;
|
||||
const int64_t T = batch->n_tok, S = batch->n_seq;
|
||||
const int64_t qw = c.heads * c.head_dim;
|
||||
const int64_t nw = (c.heads + 1) * c.head_dim;
|
||||
|
||||
proj.step(c_, in, w.qk, T);
|
||||
/* ONE LAUNCH OR `heads` + 1. The fused form takes the projection WHOLE -- the kernel picks
|
||||
* head `h` out of it at the operand's own row pitch, so the strided view the unfused norm
|
||||
* needs is not taken at all -- and it writes the query heads contiguous and rotated, which is
|
||||
* what the pair below leaves behind. `n_head` 0 is the k-only form: no q_gate, no q_w, no
|
||||
* q_out, and the ninth operand (`gate_out`) is absent as it is everywhere else.
|
||||
*
|
||||
* `T <= qprep_rows` IS THE TABLE'S BOUND and declare() says why it is correctness. */
|
||||
if (op_qprep && T <= qprep_rows && !rope_mixed(batch)) {
|
||||
RAD_ISSUE_N(c_, op_qprep, T,
|
||||
RAD_NONE, brows(w.qk, T), RAD_B(w.cos_sin), rope_pos1(batch, T),
|
||||
RAD_NONE, RAD_W(w_qn),
|
||||
RAD_NONE, brows(w.q, T), RAD_NONE);
|
||||
} else {
|
||||
for (int64_t h = 0; h < c.heads; ++h)
|
||||
RAD_ISSUE_N(c_, op_norm, T,
|
||||
bcol_at(w.qk, 0, nw, h * c.head_dim, c.head_dim, T), RAD_W(w_qn),
|
||||
bcol_at(w.q, 0, qw, h * c.head_dim, c.head_dim, T));
|
||||
RAD_ISSUE_N(c_, op_rope, T, brows(w.q, T), rope_posmc(g, batch, T));
|
||||
}
|
||||
|
||||
/* The keys first: the selection below reads the blocks they complete. */
|
||||
const RadKVGroupBatch* bkb = kv_batch(batch, kv_bk);
|
||||
const RadKVGroupBatch* atb = kv_batch(batch, c.kv_attn);
|
||||
if (!atb || !store_keys(c_, batch)) return;
|
||||
|
||||
/* SCORE AND SELECT OVER EVERY QUERY IN THE STEP. A selection does not have to be per-SEQUENCE
|
||||
* even though a block table is: both ops take `cu`, the block table stays the sequence's, and
|
||||
|
||||
Reference in new issue
Block a user