Archived
radiance 1.7.0
- every model serves without a GPU: libavx covers every op, so `--kernels libavx` alone serves every shipped container - Qwen3-ASR (zivius): speech to text on /v1/audio/transcriptions and live over /v1/realtime - /v1/messages works with Claude Code as it ships (gearwave00001) - a long agent conversation keeps its cached history when the client rewrites its last turn (manueloverride) - a stream's first event goes out with the first token, so llama-benchy measures prefill right - opt-in --prefill-interleave (Smellyyyy, baKon) and --prefill-span (keptn42) - four-rank TP releases the link early; four-rank selftests and docs/TP4.md (goodman-b) - NVFP4 and FP8 compressed-tensors imports (devmilan); tools/rad-export for finetunes and LoRA adapters (baKon) - many fixes, among them lost tokens on stop strings under concurrency, keep-alive, MiniCPM5 tool arguments and the CPU's vision and audio encoders - plugin ABI 17 and changed op schemas: out-of-tree plugins must be rebuilt
This commit is contained in:
1 parent
b068190326
commit
1e968f6e1d
739 files changed
+128447
-84632
No files matched your search
@@ -1,37 +1,26 @@
|
||||
/* rad_block_attn_gated_fp8.h -- the gated GQA block with block-scaled FP8 projections.
|
||||
/* rad_block_attn_gated_fp8.h -- gated GQA attention with block-scaled FP8 projections.
|
||||
*
|
||||
* THE ARCHITECTURE IS rad_block_attn_gated.h's AND IS DOCUMENTED THERE: the interleaved query/gate
|
||||
* projection and why it is declared rank 3, the partial mrope and what is not expressible about it,
|
||||
* why k and v are not fused, why the fused qk_norm_rope_gate probe is withdrawn, and why the gate
|
||||
* is a sigmoid. None of that changes with the weight format and none of it is repeated here.
|
||||
* The architecture (interleaved q|gate projection, partial M-RoPE, separate k and v, sigmoid
|
||||
* gate) is documented in rad_block_attn_gated.h. What differs here:
|
||||
*
|
||||
* What differs:
|
||||
* - q|gate, k, v and o are LinearFP8: an E4M3 weight with a bf16 [N/128][K/128] scale plane,
|
||||
* through `gemm_nt_q` at dtype fp8a8.
|
||||
* - One input quantise feeds the three input projections (they all read the post-norm hidden
|
||||
* state); the gate pass quantises the out-projection's input.
|
||||
* - Per-head norms, rope, kv_store and attn_paged stay bf16.
|
||||
* - The KV cache dtype is `g.kv_dtype`, not `g.dtype`: it is a deployment choice that selects
|
||||
* libr4d's fp8kv attention family. The attention op and kv_store both take it, so they agree.
|
||||
* - Optional fused prologue, QSA sparse attention and an attention-zero mode (see declare).
|
||||
*
|
||||
* - q|gate, k, v and o are LinearFP8 -- an E4M3 weight with a bf16 [N/128][K/128] scale plane
|
||||
* declared as a weight of its own, through `gemm_nt_q` at dtype fp8a8.
|
||||
* - two `quant_act_fp8` launches a layer, not four: the three input projections all read the
|
||||
* post-norm hidden state, so one quantise feeds all three, and the output projection gets its
|
||||
* own because it reads the gated attention output.
|
||||
* - everything between the GEMMs -- the per-head norms, rope, kv_store, attn_paged, the sigmoid
|
||||
* and the gate multiply -- is UNCHANGED and still bf16.
|
||||
* - THE KV CACHE'S WIDTH IS `g.kv_dtype` AND NOT `g.dtype`. `dtype` follows the WEIGHTS;
|
||||
* "the cache is bf16 because the weights are fp8" is true of this checkpoint and is not a
|
||||
* rule. `kv_dtype` follows the CACHE, which is a deployment choice: the fp8kv attention family
|
||||
* and its descale operands are in libr4d, and the cache dtype is what selects them.
|
||||
* Both the attention op and kv_store take it, so the two cannot disagree about the width of
|
||||
* the thing one writes and the other reads.
|
||||
*
|
||||
* The q|gate projection is [2*n_head*head_dim, n_embd] = [12288, 5120] at 27B, and its scale plane
|
||||
* is [96, 40]. Row-sharding by head keeps whole 128-row scale blocks together only because
|
||||
* 2*head_dim is 512, a multiple of 128 -- so a rank's slice of the head range is a whole number of
|
||||
* scale rows. LinearFP8 checks that rather than trusting it.
|
||||
* The q|gate projection is [2*n_head*head_dim, n_embd] = [12288, 5120] at 27B, scale plane
|
||||
* [96, 40]. Row-sharding by head keeps whole 128-row scale blocks only because 2*head_dim = 512 is
|
||||
* a multiple of 128; LinearFP8 checks this.
|
||||
*/
|
||||
#ifndef RAD_BLOCK_ATTN_GATED_FP8_H
|
||||
#define RAD_BLOCK_ATTN_GATED_FP8_H
|
||||
|
||||
#include "rad_fp8.h"
|
||||
|
||||
#include <cstdlib>
|
||||
#include <cstring>
|
||||
|
||||
namespace rad {
|
||||
@@ -45,14 +34,13 @@ struct AttnGatedFP8 {
|
||||
rad_buf k = 0; /* [max_tok, kv_dim] */
|
||||
rad_buf v = 0; /* [max_tok, kv_dim] */
|
||||
rad_buf q = 0; /* [max_tok, q_dim] de-interleaved, normed, rotated */
|
||||
/* [rope_table_rows, rot] f32, the model's, shared by every layer and filled once a step.
|
||||
* Zero means the plugin has not been taught to build one, and then the fused prologue is
|
||||
* not asked for at all. */
|
||||
/* [rope_table_rows, rot] f32 cos/sin table, shared by every layer, filled once a step. Zero
|
||||
* if the plugin builds none; the fused prologue is then not requested. */
|
||||
rad_buf cos_sin = 0;
|
||||
ActFP8 attn{}; /* [max_tok, q_dim] attention output, then gated in place */
|
||||
/* [max_tok, q_dim] the de-interleaved sigmoid gate, in a model whose linears are bf16
|
||||
* (Geom::w_bf16) only: there the gate is `sigmoid` then `mul`, rad_block_attn_gated.h's
|
||||
* pair, and the fp8 path's fused gate-and-quantiser has no form that skips its codes. */
|
||||
/* [max_tok, q_dim] de-interleaved sigmoid gate, used only with bf16 linears
|
||||
* (Geom::w_bf16), where the gate is `sigmoid` then `mul`; the fused gate quantiser always
|
||||
* writes codes. */
|
||||
rad_buf gate = 0;
|
||||
};
|
||||
|
||||
@@ -71,66 +59,47 @@ struct AttnGatedFP8 {
|
||||
int layer = 0;
|
||||
rad_kvgroup kv = 0;
|
||||
int64_t n_rot = 0;
|
||||
/* THE KV PAGE, IN TOKENS. 0 means RAD_KV_BLOCK, which is 16 and is what every geometry in
|
||||
* this tree has wanted. It is a field rather than that constant because Qwen4-Exp's QSA
|
||||
* selects 4-TOKEN BLOCKS, and a page of 4 makes the selected set a paged block table instead
|
||||
* of a new attention kernel -- see docs/QSA.md. The attention kernel's own rows must carry a
|
||||
* matching C_EQ("block_size", ...) or nothing resolves, which is the check that keeps this
|
||||
* from being a number anyone can set. */
|
||||
/* The KV page in tokens; 0 means RAD_KV_BLOCK (16). Qwen4-Exp's QSA selects 4-token blocks,
|
||||
* and a page of 4 turns the selected set into a paged block table, so no new attention kernel
|
||||
* is needed (docs/QSA.md). The attention kernel's rows must carry a matching
|
||||
* C_EQ("block_size", ...) or nothing resolves. */
|
||||
int64_t kv_block = 0;
|
||||
/* THE QSA SELECTION, when this layer has an indexer. Set, and on a step where one query a
|
||||
* sequence makes the selection per-SEQUENCE, the attention is issued with these in place of
|
||||
* the sequence's own block table and context length -- which is the whole of what makes it
|
||||
* sparse. Both are ordinary operands (the block table has always been one), so this is the
|
||||
* SAME op and the SAME kernel reading a different list.
|
||||
*
|
||||
* `sel` is `topk + 1` wide and the kernel takes its block-table pitch from the operand, so
|
||||
* nothing else has to be told. See docs/QSA.md; arch/common/rad_qsa.h produces them. */
|
||||
/* The QSA selection, when this layer has an indexer (produced by arch/common/rad_qsa.h; see
|
||||
* docs/QSA.md). On a sparse step the attention is issued with these in place of each row's
|
||||
* block table and context length: same op, same kernel, different list. `sel` is `topk + 1`
|
||||
* wide and the kernel takes its block-table pitch from the operand. */
|
||||
rad_buf qsa_sel = 0;
|
||||
rad_buf qsa_sequ = 0;
|
||||
/* THE CONTEXT UP TO WHICH THE DENSE PATH IS THE SAME ANSWER, `budget + ratio - 1`. Below it
|
||||
* every complete block is selected and the two paths agree to the bit, and the dense one is
|
||||
* cheaper at prefill -- it reads each key once a query TILE where the sparse one reads a
|
||||
* table a query. Above it the dense path is a different model. Zero means "always sparse",
|
||||
* which is what a caller that has not been told the bound should get. */
|
||||
/* Context length up to which dense attention gives the same result, `budget + ratio - 1`.
|
||||
* Below it every complete block is selected, the paths agree to the bit, and dense is cheaper
|
||||
* at prefill (it reads each key once per query tile, sparse reads a table per query). Zero
|
||||
* means always sparse. */
|
||||
int64_t qsa_exact_to = 0;
|
||||
/* `sel`'s row pitch, `topk + 1`. The extra slot is the partial page and is not spare --
|
||||
* r4d_qsa_select_bf16.hip says why. */
|
||||
/* `sel`'s row pitch is `topk + 1`; the extra slot is the partial page
|
||||
* (r4d_qsa_select_bf16.hip). */
|
||||
int64_t qsa_topk = 0;
|
||||
/* HOW MANY QUERY ROWS ONE SPARSE ATTENTION LAUNCH CARRIES, and it is a scratch budget rather
|
||||
* than a preference. The decode kernel's split-KV partials are sized AT DECLARE from the
|
||||
* `max_seqs` and `q_len` parameters, so the only way to issue more rows than `max_seqs` is to
|
||||
* declare a larger one -- which grows the scratch arena by `rows * q_len_band * n_head *
|
||||
* (head_dim + 2) * 4`, about 32 MiB at 256 rows on this model, ONCE for the whole program
|
||||
* because the arena is the maximum over ops.
|
||||
*
|
||||
* It matters because the alternative is `max_seqs` itself. At `--max-num-seqs 4` a 2048-token
|
||||
* prefill chunk would be 512 launches a layer, and the dispatch cost of that dominates what
|
||||
* the sparse path saves. 256 makes it 8.
|
||||
*
|
||||
* Raising it further is pure scratch: 512 rows is 63 MiB, 2048 is 253. 256 is the point at
|
||||
* which the launch cost has stopped mattering. */
|
||||
/* Query rows per sparse attention launch, a scratch budget. The decode kernel's split-KV
|
||||
* partials are sized at declare from `max_seqs` and `q_len`, so issuing more rows than
|
||||
* `max_seqs` means declaring a larger value, which grows the scratch arena (the maximum over
|
||||
* ops) by `rows * q_len_band * n_head * (head_dim + 2) * 4` bytes: about 32 MiB at 256 rows on
|
||||
* this model, 63 MiB at 512, 253 MiB at 2048. With max_seqs alone, `--max-num-seqs 4` would
|
||||
* split a 2048-token prefill chunk into 512 launches per layer; 256 makes it 8, past which
|
||||
* launch cost no longer matters. */
|
||||
static constexpr int64_t kQsaRows = 256;
|
||||
int64_t qsa_rows() const { return g.max_seqs > kQsaRows ? g.max_seqs : kQsaRows; }
|
||||
/* THE GATED FORM TAKES THE WHOLE STEP IN ONE LAUNCH, because its band is q_len 1 and so its
|
||||
* partials are only `rows * n_head * (2 * head_dim + 8)` bytes -- 12.8 MiB at a 2048-row step
|
||||
* on this model, where the ungated op's q_len band makes the same rows cost the figures
|
||||
* above. Fewer launches still pay here: one 2048-row launch takes about 2.16 ms a layer
|
||||
* against about 3.0 ms for eight 256-row ones, each of which is 256 one-wave workgroups -- two
|
||||
* waves a SIMD -- with its own tail. */
|
||||
/* The gated form takes the whole step in one launch: its band is q_len 1, so partials are only
|
||||
* `rows * n_head * (2 * head_dim + 8)` bytes (12.8 MiB for 2048 rows on this model). One
|
||||
* 2048-row launch is about 2.16 ms per layer versus 3.0 ms for eight 256-row launches, each
|
||||
* with its own tail. */
|
||||
int64_t qsa_gq_rows() const { return g.max_tok > qsa_rows() ? g.max_tok : qsa_rows(); }
|
||||
/* THE ROTATION'S PAIRING, a question the checkpoint cannot answer by inspection and that no
|
||||
* per-op check can reach, because both readings -- halves (NeoX, what HF's rotate_half does
|
||||
* over the partial rotary width) and adjacent pairs (GPT-J) -- are self-consistent and libref
|
||||
* shares whichever one the graph picks. Either answer is a DIFFERENT MODEL. "mrope" is what the
|
||||
* model does, and over one position component a token it is NeoX (rad_block_attn_gated.h).
|
||||
/* The rotation's pairing. Halves (NeoX, HF's rotate_half over the partial rotary width) and
|
||||
* adjacent pairs (GPT-J) are both self-consistent and pass every per-op check, so it must be
|
||||
* stated. "mrope" over one position component per token is NeoX (rad_block_attn_gated.h).
|
||||
*
|
||||
* THE q|gate ORDER IS FIXED TOO. q is the FIRST half of each head's pair and the gate the
|
||||
* second, which is how vLLM chunks q_gate, and every consumer of the projection has to agree
|
||||
* on it: the unfused q norm reads column 0 of each head, the gate quantiser column head_dim,
|
||||
* and the fused prologue splits the pair the same way inside its kernel. A swap would be
|
||||
* arithmetically silent -- q_norm renormalises either half and the sigmoid accepts either --
|
||||
* so an order that only some of them followed would be fluent and wrong. */
|
||||
* The q|gate order is fixed as well: q is the first half of each head's pair, the gate the
|
||||
* second (as vLLM chunks q_gate). The unfused q norm reads column 0 of each head, the gate
|
||||
* quantiser column head_dim, and the fused prologue splits the same way. A swap would pass
|
||||
* silently, since q_norm and the sigmoid accept either half. */
|
||||
const char* mode = "mrope";
|
||||
|
||||
rad_weight w_norm = 0, w_q_norm = 0, w_k_norm = 0;
|
||||
@@ -140,39 +109,54 @@ struct AttnGatedFP8 {
|
||||
NormQuantFP8 nq_h{};
|
||||
|
||||
rad_op op_fused = 0; /* qk_norm_rope_gate, if it resolves */
|
||||
rad_op op_fused_kv = 0; /* ...with kv_store folded on, likewise */
|
||||
int64_t fused_rows = 0; /* ...and the widest T it is issued at */
|
||||
rad_op op_fused_kv = 0; /* the same with kv_store folded in */
|
||||
int64_t fused_rows = 0; /* widest T the fused ops take */
|
||||
rad_op op_q_norm = 0, op_k_norm = 0, op_rope_q = 0, op_rope_k = 0;
|
||||
rad_op op_attn = 0, op_kv_store = 0, op_gq = 0;
|
||||
rad_op op_attn_gq = 0; /* attn_paged + gate_quant_fp8 */
|
||||
rad_op op_sig = 0, op_mul = 0; /* the bf16 gate, Geom::w_bf16 */
|
||||
/* The output gate as `sigmoid` then `mul`: the bf16-linear form (Geom::w_bf16), and with fp8
|
||||
* linears the chain gate_quant_fp8 fuses, where it is not served. `gate` is the sigmoid's
|
||||
* output (the wire's with bf16 linears, the block's own otherwise) and `q_gate` the chain's
|
||||
* quantiser (no op with bf16 linears). */
|
||||
rad_op op_sig = 0, op_mul = 0;
|
||||
rad_buf gate = 0;
|
||||
QuantFP8 q_gate{};
|
||||
rad_op op_ar = 0, op_add = 0;
|
||||
rad_op op_fill = 0; /* the attention-zero rank: see declare */
|
||||
/* ...AND WHETHER THE COLLECTIVE AT THE END OF THIS BLOCK IS THE NEXT BLOCK'S FIRST PASS.
|
||||
* `ar_in` says this block's own norm absorbs its PREDECESSOR's all-reduce; `ar_out` says the
|
||||
* block after it absorbs THIS one's, so this one must not issue it. They are the residual
|
||||
* fold's two flags again, one boundary later, and they are separate for the same reason: a
|
||||
* block is declared without knowing what follows it. On EITHER wire -- the fused kernel
|
||||
* carries both payloads -- and both sides ask ar_fused_ok(g, T), so the two cannot disagree
|
||||
* about whether the collective has already been issued. */
|
||||
/* The next block absorbs this block's closing all-reduce, so this one must not issue it.
|
||||
* Counterpart of `ar_in` (this block's norm absorbs the predecessor's); separate because a
|
||||
* block is declared without knowing what follows. Both sides ask ar_fused_ok(g, T), so they
|
||||
* agree on whether the collective was issued. */
|
||||
bool ar_out = false;
|
||||
/* ...and WHICH WIRES that consumer takes (ArOut, rad_fp8.h): the norm's by default, and a gated
|
||||
* residual's write says its own (HyperConn::ar_take). Set before declare; both sides then ask
|
||||
/* Which wires that consumer takes (ArOut, rad_fp8.h): the norm's by default, or a gated
|
||||
* residual's own (HyperConn::ar_take). Set before declare; both sides then ask
|
||||
* ar_taken(g, T, ar_out, ar_out_take). */
|
||||
int ar_out_take = kArOutNorm;
|
||||
|
||||
/* `fold` says the PREVIOUS block left its delta in `w.h.x` for this norm to add; `add_out`
|
||||
* says the NEXT one will not, so this block issues the trailing add itself. See rad_fp8.h. */
|
||||
/* `fold`: the previous block left its delta in `w.h.x` for this norm to add. `add_out`: the
|
||||
* next block will not add, so this one issues the trailing add. See rad_fp8.h. */
|
||||
int declare(RadBuilder* b, Names& nm, const Geom& geom, int l, rad_kvgroup group,
|
||||
int64_t rotary_dim, 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 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. */
|
||||
/* Stores the pass's K and V exactly as step() does, with no query, attention or output. Used
|
||||
* by passes that only leave layer state behind, such as the MTP head's history pass over a
|
||||
* prompt chunk with no drafting row. */
|
||||
void store_kv(RadCtx* c, const RadBatch* batch) const;
|
||||
|
||||
/* The block's tail, declared last by every rank: the all-reduce and the trailing add. */
|
||||
int declare_out(RadBuilder* b, bool add_out);
|
||||
/* `own_ar` false: the caller issues the all-reduce itself (GdnFP8::step says when). */
|
||||
void step(RadCtx* c, const RadBatch* batch, bool own_ar = true,
|
||||
ArPieces* pieces = nullptr) const;
|
||||
bool issues_ar(int64_t T) const { return op_ar && !ar_taken(g, T, ar_out, ar_out_take); }
|
||||
void issue_ar(RadCtx* c, int64_t r0, int64_t rows) const {
|
||||
RAD_ISSUE_N(c, op_ar_ov ? op_ar_ov : op_ar, rows * g.n_embd,
|
||||
brow_slice(w.h.x, r0, rows, g.n_embd), RAD_NONE);
|
||||
}
|
||||
/* Set before declare: the `overlap` issue_ar's collective is declared at, 0 for none. */
|
||||
int ar_overlap = 0;
|
||||
rad_op op_ar_ov = 0;
|
||||
};
|
||||
|
||||
inline int AttnGatedFP8::declare(RadBuilder* b, Names& nm, const Geom& geom, int l,
|
||||
@@ -186,23 +170,17 @@ inline int AttnGatedFP8::declare(RadBuilder* b, Names& nm, const Geom& geom, int
|
||||
const int64_t kv_dim = g.kv_dim();
|
||||
const int64_t qg_dim = 2 * q_dim;
|
||||
|
||||
/* THE KV ROW SPAN, AND IT ONLY EXISTS ABOVE THE KV HEAD COUNT.
|
||||
* A world size larger than `n_head_kv_all` cannot divide the KV heads, so rad_arch.h's
|
||||
* `geom_from` replicates them in PAIRS: two ranks serve the same head, `g.n_head_kv` is 1 on
|
||||
* each, and the checkpoint entry they both read is still `n_head_kv_all * head_dim` rows -- 512
|
||||
* for 2 KV heads at head_dim 256. This rank wants ONE head's row-span of that entry, which the
|
||||
* even shard would cut up instead (at 4 ranks it would hand rank 2 rows [256, 384), half of one
|
||||
* head), so the span `[g.kv_off, g.kv_off + kv_dim)` is declared in the CONTAINER's unsharded
|
||||
* units -- [0, 256) on ranks 0 and 1, [256, 512) on ranks 2 and 3 -- and the scale plane takes
|
||||
* the same span in 128-blocks. `kv_off` is a multiple of head_dim, so it is whole blocks and
|
||||
* never cuts one, and share.cpp's `shard_hi <= cpart` holds with cpart = 512 / 1 part.
|
||||
* AT WORLD <= n_head_kv_all the ordinary even shard already selects exactly these rows, so the
|
||||
* span stays 0/0 and the default path runs as before -- TP2 declares identically. */
|
||||
/* AND UNDER A SMALLER ATTENTION WORLD (rad_arch.h Geom::attn_world: three ranks over two KV
|
||||
* heads) the even shard over `world` is the wrong cut for every projection: q|gate and the
|
||||
* output take this rank's heads [attn_rank * n_head, +n_head) as a span too, and k/v the
|
||||
* kv_off span. An attention-zero rank declares none of them: its block is one `fill` of the
|
||||
* output wire (the branch below), so the spans here are only ever cut for a rank with heads. */
|
||||
/* Row spans, in the container's unsharded rows, used where the even shard is the wrong cut.
|
||||
*
|
||||
* Above the KV head count geom_from replicates KV heads (rad_arch.h): with 2 KV heads at
|
||||
* head_dim 256 and 4 ranks, the k/v entry is 512 rows and ranks 0-1 take [0, 256), ranks 2-3
|
||||
* [256, 512), where the even shard would hand rank 2 half a head. The scale plane takes the
|
||||
* same span in 128-blocks; kv_off is a multiple of head_dim, so it never cuts a block. At
|
||||
* world <= n_head_kv_all the span stays 0/0 and the even shard applies.
|
||||
*
|
||||
* Under a smaller attention world (Geom::attn_world) q|gate and the output also take this
|
||||
* rank's heads [attn_rank * n_head, +n_head) as a span. An attention-zero rank declares none
|
||||
* of them (see the `fill` below). */
|
||||
const bool a_spanned = g.attn_world != g.world;
|
||||
const bool kv_spanned = g.world > g.n_head_kv_all || a_spanned;
|
||||
const int64_t kv_span_lo = kv_spanned ? g.kv_off : 0;
|
||||
@@ -218,18 +196,13 @@ inline int AttnGatedFP8::declare(RadBuilder* b, Names& nm, const Geom& geom, int
|
||||
return RAD_E_INVAL;
|
||||
}
|
||||
|
||||
/* THE INPUT MAY BE PREPARED BY THE ARCHITECTURE, and `src.norm == nullptr` is how it says so.
|
||||
/* `src.norm == nullptr`: the architecture prepares the input. A pre-norm block owns its norm
|
||||
* (rad_fp8.h's NormQuantFP8: add the previous delta, normalise, quantise). In a gated-residual
|
||||
* model, rad_block_hc.h's `hc_read` produces the block input from a 4x wider stream, so this
|
||||
* block takes `wire.h` as already normed and quantised and declares no norm weight.
|
||||
*
|
||||
* A pre-norm model's block owns its own norm: one op that adds the previous block's delta into
|
||||
* the residual stream, normalises it and quantises it (rad_fp8.h's NormQuantFP8). A GATED
|
||||
* RESIDUAL model's does not -- arch/common/rad_block_hc.h's `hc_read` produces the block input
|
||||
* from a stream four times as wide, with a data-dependent mix the block has no way to compute,
|
||||
* and there is nothing left for a norm here to do. So this block takes `wire.h` as ALREADY
|
||||
* normed and quantised and declares no norm weight at all.
|
||||
*
|
||||
* `ar_in` is refused in that mode rather than ignored: the fused all-reduce lives INSIDE the
|
||||
* norm, so with no norm there is nowhere to fold it and a caller that asked would silently get
|
||||
* an unreduced input. It emits a standalone collective instead (`add_out`/`ar_out`). */
|
||||
* `ar_in` is refused in that mode: the fused all-reduce lives inside the norm, so it would be
|
||||
* silently dropped. The caller emits a standalone collective instead (`add_out`/`ar_out`). */
|
||||
ext_in = (src.norm == nullptr);
|
||||
if (ext_in) {
|
||||
if (!w.h.x || (!g.w_bf16 && (!w.h.q || !w.h.s)) || ar_in != kArNone) return RAD_E_INVAL;
|
||||
@@ -246,27 +219,23 @@ inline int AttnGatedFP8::declare(RadBuilder* b, Names& nm, const Geom& geom, int
|
||||
w_k_norm = decl_w(b, nm.f("blk.%d.attn_k_norm.weight", l), RAD_F32, {g.head_dim},
|
||||
RAD_ACCESS_PER_TOKEN, RAD_SHARD_NONE, grp_layer(l));
|
||||
|
||||
/* One norm, one quantise, three consumers -- and one op, see rad_fp8.h's NormQuantFP8. */
|
||||
/* One norm+quantise op feeds all three input projections (rad_fp8.h: NormQuantFP8). */
|
||||
if (!ext_in)
|
||||
RAD_ARCH_TRY(nq_h.declare(b, g, w_norm, fold ? w.h.x : w.x, w.h, g.n_embd,
|
||||
fold ? w.x : 0, 0, ar_in));
|
||||
|
||||
/* AN ATTENTION-ZERO RANK COMPUTES NO ATTENTION AT ALL. Its share of every projection is zero,
|
||||
* so its q, k and v are zero, its attention is zero and its output projection adds exactly
|
||||
* nothing to the reduce -- which is what the ranks that DO hold heads need from it and all
|
||||
* they need. Declaring the projections and running them on zero weights spent the same
|
||||
* dispatches and bytes as a real rank and a slot for every zero (and a KV pool nothing read,
|
||||
* see rad_kv_group_zero). Here the block's output wire is written to zero by one `fill`, and
|
||||
* the all-reduce and the residual below see what they always saw. The norm above stays: a
|
||||
* pre-norm block's `fold` and `ar_in` live in it. Nothing in this branch runs for a rank with
|
||||
* heads, so a world the heads divide (every TP2 and TP4 deployment) declares as it always did. */
|
||||
/* An attention-zero rank holds no heads, so its output projection contributes zero to the
|
||||
* reduce. Its output wire is zeroed by one `fill` instead of running projections on zero
|
||||
* weights (and it keeps no KV pool, rad_kv_group_zero). The all-reduce and residual add run as
|
||||
* usual, and the norm above stays because a pre-norm block's `fold` and `ar_in` live in it. */
|
||||
if (g.attn_zero) {
|
||||
op_fill = rw(b, RAD_OP(b, "fill",
|
||||
RAD_PARAMS(RAD_RANGE("M", 1, g.max_tok), RAD_INT("n", g.n_embd),
|
||||
RAD_STR("dtype", g.dtype)),
|
||||
RAD_NOWEIGHTS),
|
||||
{}, {w.h.x});
|
||||
} else {
|
||||
return declare_out(b, add_out);
|
||||
}
|
||||
RAD_ARCH_TRY(qg.declare(b, nm, g, nm.f("blk.%d.attn_qg", l), qg_dim, g.n_embd,
|
||||
RAD_SHARD_ROW, grp_layer(l), { src.qg }, w.h, w.qg,
|
||||
0, {}, qg_span_lo, qg_span_hi));
|
||||
@@ -277,33 +246,25 @@ inline int AttnGatedFP8::declare(RadBuilder* b, Names& nm, const Geom& geom, int
|
||||
RAD_SHARD_ROW, grp_layer(l), { src.v }, w.h, w.v,
|
||||
0, {}, kv_span_lo, kv_span_hi));
|
||||
|
||||
/* THE FUSED PROLOGUE, AND IT IS A PROBE: the four ops below are declared either way and are
|
||||
* what runs when nothing serves this one. rad_block_attn_gated.h does not make the probe,
|
||||
* because the schema takes a PRECOMPUTED cos/sin table rather than `theta` and that block has
|
||||
* none; `rope_table` builds one here, once a step for every layer, and `w.cos_sin` is where
|
||||
* the model puts it. A block handed no table cannot ask for the fusion, which is what keeps a
|
||||
* plugin that does not build the table on the unfused path.
|
||||
/* The fused prologue (qk norms + rope + q|gate split) is a probe; the four unfused ops below
|
||||
* are declared either way and run when it does not resolve. Its schema takes a precomputed
|
||||
* cos/sin table instead of `theta`: `rope_table` fills `w.cos_sin` once a step for every
|
||||
* layer, and without a table the fusion is not requested.
|
||||
*
|
||||
* NEOX ONLY. The fused kernel rotates NeoX pairs and has no `mode`, so the probe is made only
|
||||
* where mrope reduces to neox -- one position component a token, which is every text batch and
|
||||
* is argued in rad_block_attn_gated.h. A vision span would need the sections first.
|
||||
* The kernel rotates NeoX pairs and has no `mode`. M-RoPE qualifies because the table is
|
||||
* filled at one component per token, which is exact when the components agree; step() takes
|
||||
* the fused path only then.
|
||||
*
|
||||
* NO `gate_out`. `gate_quant_fp8` below already reads the gate at its column offset inside the
|
||||
* interleaved projection, so a contiguous copy would be q_dim of bf16 a layer written for
|
||||
* nothing. The operand is optional and this passes none.
|
||||
* No `gate_out` is passed: `gate_quant_fp8` reads the gate in place in the interleaved
|
||||
* projection.
|
||||
*
|
||||
* IT IS BIT-IDENTICAL TO THE FOUR, checked in r4d_selftest (`qk_norm_rope_gate ... matches the
|
||||
* unfused pair`) rather than argued: the kernel rounds its norm output to bf16 before rotating
|
||||
* because that is what a norm that stores a tensor and a rope that reads it back do. It
|
||||
* reproduces `r4d_rmsnorm_bf16`'s sum-of-squares fold element by element rather than folding
|
||||
* eight elements a lane, and r4d_selftest asserts byte equality. A merely RELATIVE bound is
|
||||
* not enough here: a fold order that agrees to a ULP and not to the last bit moves the
|
||||
* engine's transcript within a few dozen steps. */
|
||||
/* M-RoPE qualifies too: the table is filled at one component a token, which is exact on every
|
||||
* pass whose tokens' components agree, and the step takes the fused path only then. */
|
||||
* Bit-identical to the unfused ops, asserted in r4d_selftest: the kernel rounds the norm output
|
||||
* to bf16 before rotating and reproduces `r4d_rmsnorm_bf16`'s sum-of-squares order element by
|
||||
* element. A ULP-level difference would shift the engine's transcript within a few dozen
|
||||
* steps. */
|
||||
const bool rope_neox = !std::strcmp(mode, "neox") || !std::strcmp(mode, "mrope") || g.rope_mc;
|
||||
fused_rows = qk_fuse_rows(g);
|
||||
if (w.cos_sin && rope_neox && fused_rows > 0)
|
||||
if (w.cos_sin && rope_neox && fused_rows > 0 && rad_op_declared(b, "qk_norm_rope_gate"))
|
||||
op_fused = rw(b, RAD_OP(b, "qk_norm_rope_gate",
|
||||
RAD_PARAMS(RAD_RANGE("M", 1, fused_rows), RAD_INT("head_dim", g.head_dim),
|
||||
RAD_INT("n_head", g.n_head),
|
||||
@@ -330,33 +291,25 @@ inline int AttnGatedFP8::declare(RadBuilder* b, Names& nm, const Geom& geom, int
|
||||
op_rope_k = rw(b, decl_rope(b, g, g.max_tok, g.head_dim, g.n_head_kv, 0, n_rot, mode),
|
||||
{w.k}, {w.k});
|
||||
|
||||
/* Declared before kv_store because block_size is RAD_DERIVED and kv_store reads it back --
|
||||
* rad_block_attn_gated.h explains why the declared order differs from the issue order. */
|
||||
/* Declared before kv_store, which reads back the resolved `block_size`
|
||||
* (rad_block_attn_gated.h). */
|
||||
op_attn = RAD_OP(b, "attn_paged",
|
||||
RAD_PARAMS(RAD_RANGE("q_len", 1, g.max_tok), RAD_INT("head_dim", g.head_dim),
|
||||
RAD_INT("gqa", g.gqa()), RAD_INT("causal", 1),
|
||||
RAD_INT("window", 0),
|
||||
RAD_INT("n_head", g.n_head),
|
||||
RAD_INT("block_size", kv_block > 0 ? kv_block : (int64_t)RAD_KV_BLOCK),
|
||||
/* The sparse path issues one row a QUERY, so the bound the split-KV
|
||||
* scratch is sized from is the widest such launch and not the
|
||||
* sequence count -- see kQsaRows. */
|
||||
/* The sparse path issues one row per query, so the split-KV
|
||||
* scratch is sized for the widest launch (kQsaRows). */
|
||||
RAD_INT("max_seqs", (qsa_sel && qsa_sequ) ? qsa_rows()
|
||||
: g.max_seqs),
|
||||
RAD_INT("max_ctx", g.max_ctx),
|
||||
RAD_STR("q_dtype", g.dtype), RAD_STR("kv_dtype", g.kv_dtype)),
|
||||
RAD_NOWEIGHTS);
|
||||
/* THE QSA SELECTION IS A READ OF THIS OP AND HAS TO BE DECLARED AS ONE. The block table has
|
||||
* always been a raw batch pointer, so `attn_paged`'s read set was just the query -- and
|
||||
* `sel`/`seqused` are BUFFERS, whose lifetimes the Builder plans from exactly these lists. Left
|
||||
* out, their last use is `qsa_select` and the arena is free to lay another buffer over them;
|
||||
* anything written between the selection and the attention then arrives as a page id, and a
|
||||
* garbage page id is an address far outside the cache -- an aperture violation at the first
|
||||
* step whose plan happens to overlap them, which need not be a step that ran before. Spelled
|
||||
* with rad_op_reads rather than rw() because the list is conditional -- a layer with no
|
||||
* indexer has no such read.
|
||||
*
|
||||
* THE DECLARATION ORDER IS THE BUFFER PLAN, and this is one of the shapes that rule takes. */
|
||||
/* The QSA selection buffers must be declared as reads of this op. Otherwise their last use is
|
||||
* `qsa_select`, the arena may overlay another buffer, and the attention reads garbage page
|
||||
* ids: an aperture violation outside the cache. rad_op_reads rather than rw() because the list
|
||||
* depends on whether the layer has an indexer. */
|
||||
if (op_attn) {
|
||||
const rad_buf rd[3] = { w.q, qsa_sel, qsa_sequ };
|
||||
rad_op_reads(b, op_attn, rd, (qsa_sel && qsa_sequ) ? 3 : 1);
|
||||
@@ -371,12 +324,11 @@ inline int AttnGatedFP8::declare(RadBuilder* b, Names& nm, const Geom& geom, int
|
||||
RAD_NOWEIGHTS),
|
||||
{w.k, w.v}, {});
|
||||
|
||||
/* THE PROLOGUE AND THE CACHE STORE AS ONE LAUNCH, wherever the fused prologue is taken: a k
|
||||
* head's block already holds its token's rotated k, one element a lane, so it writes that and
|
||||
* the head's v into the cache too, byte-for-byte what kv_store would (the fold's row says why).
|
||||
* Declared here and not beside op_fused because the cache's block size is read back from the
|
||||
* attention op above. It resolves for an E4M3 and a bf16 cache alike. */
|
||||
if (op_fused)
|
||||
/* The fused prologue with the cache store folded in: a k head's block already holds its
|
||||
* token's rotated k, so it also writes k and v into the cache, byte-identical to kv_store.
|
||||
* Declared here because the cache block size is read back from the attention op above.
|
||||
* Resolves for E4M3 and bf16 caches. */
|
||||
if (op_fused && rad_op_declared(b, "qk_norm_rope_gate_kv_store"))
|
||||
op_fused_kv = rw(b, RAD_OP(b, "qk_norm_rope_gate_kv_store",
|
||||
RAD_PARAMS(RAD_RANGE("M", 1, fused_rows),
|
||||
RAD_INT("head_dim", g.head_dim), RAD_INT("n_head", g.n_head),
|
||||
@@ -388,48 +340,62 @@ inline int AttnGatedFP8::declare(RadBuilder* b, Names& nm, const Geom& geom, int
|
||||
RAD_WEIGHTS(w_q_norm, w_k_norm)),
|
||||
{w.qg, w.k, w.cos_sin, w.v}, {w.q, w.k});
|
||||
|
||||
/* THE OUTPUT GATE, THE MULTIPLY AND THE QUANTISER ARE ONE PASS. Unfused they are three --
|
||||
* sigmoid into a de-interleaved buffer, a multiply in place, then quant_act_fp8 -- and at
|
||||
* decode each is small enough that the dispatch gap between them is comparable to the work
|
||||
* itself, once per layer. THE ROW IS A (token, head) PAIR, which is what makes the fusion
|
||||
* expressible: at that shape the gate is a fixed stride inside the [q|gate] projection, the
|
||||
* attention output is contiguous, and the fp8 scale plane of [T * n_head, head_dim / 128] is
|
||||
* byte-for-byte the [T, q_dim / 128] the out projection reads. No separate gate buffer is
|
||||
* declared, because nothing reads one.
|
||||
/* Output gate, multiply and quantiser in one pass (`gate_quant_fp8`); at decode the dispatch
|
||||
* gaps between three small ops are comparable to the work. Each row is a (token, head) pair:
|
||||
* the gate is a fixed stride inside [q|gate], the attention output is contiguous, and the
|
||||
* [T * n_head, head_dim / 128] scale plane is byte-identical to the [T, q_dim / 128] the out
|
||||
* projection reads.
|
||||
*
|
||||
* The bf16 product is still written, because that is what the unfused pair left in `w.attn.x`
|
||||
* and the codes are taken over it -- see r4d_fused_quant_fp8.hip on why that is a requirement.
|
||||
* The codes are the int8 pair when the model feeds the out projection int8 (ActFP8::q8_fed),
|
||||
* and the same op writes them: the codes operand's dtype picks the quantiser.
|
||||
* PREFILL AND DECODE BOTH TAKE IT: unlike the gdn fold this one has no reduction across the
|
||||
* row, so there is no shape it cannot serve. */
|
||||
if (g.w_bf16) {
|
||||
* The bf16 product is still written to `w.attn.x` and the codes are taken over it
|
||||
* (r4d_fused_quant_fp8.hip says why). The codes operand's dtype picks the quantiser, int8 when
|
||||
* the out projection is fed int8 (ActFP8::q8_fed). Serves prefill and decode: there is no
|
||||
* cross-row reduction.
|
||||
*
|
||||
* With bf16 linears (Geom::w_bf16) the gate is the plain `sigmoid` + `mul` pair instead.
|
||||
*
|
||||
* Where no loaded library declares gate_quant_fp8, or it does not resolve, the fp8 block runs
|
||||
* the chain it fuses: the gate into a buffer of its own, the multiply, then the activation
|
||||
* quantiser, which write what the fused op writes. A head of whole 128-column blocks makes
|
||||
* the quantiser's [T, q_dim / 128] scale plane the fused op's [T * n_head, head_dim / 128]. */
|
||||
auto gate_pair = [&](rad_buf out) {
|
||||
gate = out;
|
||||
op_sig = rw(b, RAD_OP(b, "sigmoid",
|
||||
RAD_PARAMS(RAD_RANGE("M", 1, g.max_tok * g.n_head),
|
||||
RAD_INT("n", g.head_dim), RAD_STR("dtype", g.dtype)),
|
||||
RAD_NOWEIGHTS),
|
||||
{w.qg}, {w.gate});
|
||||
{w.qg}, {gate});
|
||||
op_mul = rw(b, RAD_OP(b, "mul",
|
||||
RAD_PARAMS(RAD_RANGE("M", 1, g.max_tok), RAD_INT("n", q_dim),
|
||||
RAD_STR("dtype", g.dtype)),
|
||||
RAD_NOWEIGHTS),
|
||||
{w.attn.x, w.gate}, {w.attn.x});
|
||||
if (!op_sig || !op_mul) return RAD_E_INVAL;
|
||||
} else
|
||||
op_gq = rw(b, RAD_OP(b, "gate_quant_fp8",
|
||||
RAD_PARAMS(RAD_RANGE("M", 1, g.max_tok * g.n_head),
|
||||
RAD_INT("n", g.head_dim), RAD_INT("group", RAD_FP8_BLOCK),
|
||||
RAD_STR("dtype", g.dtype), RAD_STR("act", "sigmoid")),
|
||||
RAD_NOWEIGHTS),
|
||||
{w.qg, w.attn.x}, {w.attn.cq(), w.attn.cs(), w.attn.x});
|
||||
{w.attn.x, gate}, {w.attn.x});
|
||||
return op_sig && op_mul ? RAD_OK : RAD_E_INVAL;
|
||||
};
|
||||
if (g.w_bf16) {
|
||||
RAD_ARCH_TRY(gate_pair(w.gate));
|
||||
} else {
|
||||
if (rad_op_declared(b, "gate_quant_fp8"))
|
||||
op_gq = rw(b, RAD_OP(b, "gate_quant_fp8",
|
||||
RAD_PARAMS(RAD_RANGE("M", 1, g.max_tok * g.n_head),
|
||||
RAD_INT("n", g.head_dim), RAD_INT("group", RAD_FP8_BLOCK),
|
||||
RAD_STR("dtype", g.dtype), RAD_STR("act", "sigmoid")),
|
||||
RAD_NOWEIGHTS),
|
||||
{w.qg, w.attn.x}, {w.attn.cq(), w.attn.cs(), w.attn.x});
|
||||
if (!op_gq && g.head_dim % RAD_FP8_BLOCK == 0) {
|
||||
RAD_ARCH_TRY(gate_pair(decl_b(b, nm.f("blk.%d.attn_gate", l), g.act_dtype,
|
||||
{g.max_tok, q_dim})));
|
||||
RAD_ARCH_TRY(q_gate.declare(b, g, w.attn, q_dim));
|
||||
if (!q_gate.op) return RAD_E_INVAL;
|
||||
}
|
||||
}
|
||||
|
||||
/* AND ON THE SPARSE PATH THE GATE RIDES IN THE ATTENTION'S OWN MERGE. There every launch is
|
||||
* one query row a (token, head) pair at q_len 1, and the split-KV merge that closes each row
|
||||
* holds exactly the 256 values the gate and the quantiser read next -- so the fused op merges,
|
||||
* gates, rounds and quantises the row where it is, byte-for-byte the pair (its row says how),
|
||||
* and the launch between them is gone. Declared only beside a selection: the dense path's
|
||||
* query rows are the band the prefill kernel serves as well, and that kernel has no merge. */
|
||||
if (op_attn && qsa_sel && qsa_sequ && !g.w_bf16) {
|
||||
/* On the sparse path the gate is applied inside the attention's split-KV merge: each launch
|
||||
* row is one (token, head) at q_len 1, and the merge already holds the head_dim values the gate
|
||||
* and quantiser read, so one op merges, gates, rounds and quantises (byte-identical to the
|
||||
* pair). Declared only with a selection: the dense path also uses the prefill kernel, which
|
||||
* has no merge. */
|
||||
if (op_attn && qsa_sel && qsa_sequ && !g.w_bf16 &&
|
||||
rad_op_declared(b, "attn_paged_gate_quant")) {
|
||||
op_attn_gq = RAD_OP(b, "attn_paged_gate_quant",
|
||||
RAD_PARAMS(RAD_INT("q_len", 1), RAD_INT("head_dim", g.head_dim),
|
||||
RAD_INT("gqa", g.gqa()), RAD_INT("causal", 1),
|
||||
@@ -453,8 +419,10 @@ inline int AttnGatedFP8::declare(RadBuilder* b, Names& nm, const Geom& geom, int
|
||||
RAD_ARCH_TRY(o.declare(b, nm, g, nm.f("blk.%d.attn_output", l), g.n_embd, q_dim,
|
||||
RAD_SHARD_COL, grp_layer(l), { src.o }, w.attn, w.h.x,
|
||||
0, {}, o_span_lo, o_span_hi));
|
||||
} /* !g.attn_zero */
|
||||
return declare_out(b, add_out);
|
||||
}
|
||||
|
||||
inline int AttnGatedFP8::declare_out(RadBuilder* b, bool add_out) {
|
||||
if (g.world > 1)
|
||||
op_ar = rw(b, RAD_OP(b, "all_reduce",
|
||||
RAD_PARAMS(RAD_INT("world_size", g.world),
|
||||
@@ -463,8 +431,19 @@ inline int AttnGatedFP8::declare(RadBuilder* b, Names& nm, const Geom& geom, int
|
||||
RAD_INT("min_bytes", g.wire_min_bytes)),
|
||||
RAD_NOWEIGHTS),
|
||||
{w.h.x}, {w.h.x});
|
||||
/* The same collective for a caller that runs it beside other work (issue_ar, a pipelined
|
||||
* prefill): at `overlap` 1 it gets the rows that hold fewer CUs while waiting on the link. */
|
||||
if (g.world > 1 && ar_overlap > 0)
|
||||
op_ar_ov = rw(b, RAD_OP(b, "all_reduce",
|
||||
RAD_PARAMS(RAD_INT("world_size", g.world),
|
||||
RAD_RANGE("numel", g.n_embd, g.max_tok * g.n_embd),
|
||||
RAD_STR("dtype", g.dtype), RAD_INT("exact", g.wire_exact),
|
||||
RAD_INT("min_bytes", g.wire_min_bytes),
|
||||
RAD_INT("overlap", ar_overlap)),
|
||||
RAD_NOWEIGHTS),
|
||||
{w.h.x}, {w.h.x});
|
||||
|
||||
/* Only where the NEXT block's norm will not do it; see rad_fp8.h's NormQuantFP8. */
|
||||
/* Only when the next block's norm will not add (rad_fp8.h: NormQuantFP8). */
|
||||
if (add_out)
|
||||
op_add = rw(b, RAD_OP(b, "add",
|
||||
RAD_PARAMS(RAD_RANGE("M", 1, g.max_tok), RAD_INT("n", g.n_embd),
|
||||
@@ -489,7 +468,8 @@ inline void AttnGatedFP8::store_kv(RadCtx* c, const RadBatch* batch) const {
|
||||
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 {
|
||||
inline void AttnGatedFP8::step(RadCtx* c, const RadBatch* batch, bool own_ar,
|
||||
ArPieces* pieces) const {
|
||||
const RadKVGroupBatch* kvb = kv_batch(batch, kv);
|
||||
const int32_t* slot = kvb ? kvb->slot_mapping : nullptr;
|
||||
const int32_t* table = kvb ? kvb->block_table : nullptr;
|
||||
@@ -499,9 +479,9 @@ inline void AttnGatedFP8::step(RadCtx* c, const RadBatch* batch) const {
|
||||
|
||||
if (!ext_in) nq_h.step(c, T);
|
||||
if (op_fill) {
|
||||
/* The attention-zero rank: the output wire is zero, then the reduce and the residual. */
|
||||
/* Attention-zero rank: zero output, then the reduce and the residual. */
|
||||
RAD_ISSUE_N(c, op_fill, T, brows(w.h.x, T));
|
||||
if (op_ar && !ar_taken(g, T, ar_out, ar_out_take))
|
||||
if (own_ar && op_ar && !ar_taken(g, T, ar_out, ar_out_take))
|
||||
RAD_ISSUE_N(c, op_ar, T * g.n_embd, brows(w.h.x, T), RAD_NONE);
|
||||
if (op_add) RAD_ISSUE(c, op_add, brows(w.x, T), brows(w.h.x, T), brows(w.x, T));
|
||||
return;
|
||||
@@ -510,11 +490,10 @@ inline void AttnGatedFP8::step(RadCtx* c, const RadBatch* batch) const {
|
||||
kp.step(c, w.h, w.k, T);
|
||||
vp.step(c, w.h, w.v, T);
|
||||
|
||||
/* Four launches or one. The fused kernel takes the [q|gate] projection WHOLE and splits it
|
||||
* itself -- so `w.qg` goes in as it stands and not as the column slice the unfused norm needs
|
||||
* -- and it rotates k in place, which is what the unfused pair does too. */
|
||||
/* THE FUSED PATH READS ONE COMPONENT A TOKEN, so a pass carrying media tokens -- whose
|
||||
* components differ -- takes the unfused pair with the whole [3, T] instead. */
|
||||
/* Fused or unfused prologue. The fused kernel takes the whole [q|gate] projection and splits it
|
||||
* itself, and rotates k in place like the unfused ops. It reads one rotary component per
|
||||
* token, so a pass with media tokens (differing components) takes the unfused path with the
|
||||
* full [3, T] positions. */
|
||||
const bool fuse_ok = T <= fused_rows && !rope_mixed(batch);
|
||||
const bool fused_kv = op_fused_kv && fuse_ok;
|
||||
if (fused_kv) {
|
||||
@@ -541,26 +520,49 @@ inline void AttnGatedFP8::step(RadCtx* c, const RadBatch* batch) const {
|
||||
RAD_ISSUE(c, op_kv_store,
|
||||
brows(w.k, T), brows(w.v, T), praw(slot, RAD_I32, T), kv_cache(kv, layer));
|
||||
|
||||
/* ONE ROW A QUERY IS WHAT MAKES THE SPARSE PATH WORK AT PREFILL, and it needs no new kernel
|
||||
* for the same reason the decode path needed none: `attn_paged` indexes its block table and
|
||||
* its length by the ROW, and nothing says a row has to be a sequence. A chunk of T queries
|
||||
* issued as T rows of q_len 1, each with its own selection and its own `seqused`, is exactly
|
||||
* per-query sparse attention -- and at decode, where T is the sequence count, it is the same
|
||||
* issue it has always been.
|
||||
/* Sparse attention issues one row per query. `attn_paged` indexes its block table and length
|
||||
* by row, and a row need not be a sequence, so T queries issued as T rows of q_len 1, each with
|
||||
* its own selection and `seqused`, is per-query sparse attention; at decode T is the sequence
|
||||
* count.
|
||||
*
|
||||
* DENSE BELOW THE BOUND, because there the two are the same answer and dense is cheaper: it
|
||||
* reads each key once a query TILE where this reads a table a query. `max_ctx_len` is the
|
||||
* host-side bound the batch already carries, so the test costs nothing.
|
||||
* Below `qsa_exact_to` the dense path gives the same result more cheaply (see the field);
|
||||
* `max_ctx_len` is a host-side bound the batch already carries.
|
||||
*
|
||||
* CHUNKED AT max_seqs, and that is not tidiness. The decode kernel's split-KV scratch is
|
||||
* sized AT DECLARE from the `max_seqs` and `q_len` parameters -- max_seqs * q_len_max * heads
|
||||
* rows -- and handing it T rows past that is refused as RAD_E_SCRATCH at issue. max_seqs rows
|
||||
* of q_len 1 is strictly inside what q_len_max >= 1 already bought, so the bound holds
|
||||
* whatever the kernel's band turns out to be. */
|
||||
* Launches are chunked because the split-KV scratch is sized at declare from `max_seqs` and
|
||||
* `q_len` (rows = max_seqs * q_len_max * heads); more rows are refused as RAD_E_SCRATCH at
|
||||
* issue. Rows of q_len 1 up to the declared max_seqs always fit. */
|
||||
const int64_t reach = (int64_t)batch->max_ctx_len + batch->max_q_len;
|
||||
const bool sparse = qsa_sel && qsa_sequ &&
|
||||
(T == batch->n_seq || qsa_exact_to <= 0 || reach > qsa_exact_to);
|
||||
const bool gated = sparse && op_attn_gq;
|
||||
/* A piece at a time where the caller takes the all-reduce that way (ArPieces). On the gated
|
||||
* sparse path every query row is its own (q_len 1) and the gate rides in the merge, so the
|
||||
* attention and out projection can finish a piece's rows and hand off its reduction while the
|
||||
* next piece's attention runs. Each piece is launched as a step of the piece's width would
|
||||
* launch it. */
|
||||
if (gated && !own_ar && pieces && pieces->cuts(T) && !op_add) {
|
||||
const int64_t chunk = qsa_gq_rows();
|
||||
const int64_t qw = g.q_dim(), hd = g.head_dim;
|
||||
for (int64_t p0 = 0; p0 < T; p0 += pieces->rows) {
|
||||
const int64_t pn = T - p0 < pieces->rows ? T - p0 : pieces->rows;
|
||||
for (int64_t off = p0; off < p0 + pn; off += chunk) {
|
||||
const int64_t rows = (p0 + pn - off) < chunk ? (p0 + pn - off) : chunk;
|
||||
RAD_ISSUE_N(c, op_attn_gq, 1,
|
||||
brow_slice(w.q, off, rows, qw),
|
||||
kv_cache(kv, layer),
|
||||
brow_slice(qsa_sel, off, rows, qsa_topk + 1),
|
||||
brow_slice(qsa_sequ, off, rows, 1),
|
||||
RAD_NONE, RAD_NONE, RAD_NONE,
|
||||
brow_slice(w.attn.x, off, rows, qw),
|
||||
bcol_at(w.qg, off, g.n_head * 2 * hd, hd, hd, rows),
|
||||
brow_slice(w.attn.cq(), off, rows, qw),
|
||||
brow_slice(w.attn.cs(), off, rows, fp8_blocks(qw)));
|
||||
}
|
||||
o.step(c, w.attn, w.h.x, T, p0, pn);
|
||||
if (p0 + pn < T) pieces->hand(p0, pn);
|
||||
}
|
||||
return;
|
||||
}
|
||||
if (gated) {
|
||||
const int64_t chunk = qsa_gq_rows();
|
||||
const int64_t qw = g.q_dim(), hd = g.head_dim;
|
||||
@@ -597,25 +599,27 @@ inline void AttnGatedFP8::step(RadCtx* c, const RadBatch* batch) const {
|
||||
kvb ? kvb->block_table_pitch : 0),
|
||||
praw(used, RAD_I32, batch->n_seq),
|
||||
RAD_NONE, RAD_NONE,
|
||||
/* cu_seqlens. The batch may be ragged -- one prefill chunk beside other
|
||||
* sequences one decode row each -- and without it the kernel has to
|
||||
* assume a single q_len for the whole step, which it cannot. */
|
||||
/* cu_seqlens: the batch may be ragged (a prefill chunk beside decode rows). */
|
||||
praw(batch->cu_seqlens, RAD_I32, batch->n_seq + 1),
|
||||
brows(w.attn.x, T));
|
||||
}
|
||||
|
||||
if (op_sig) {
|
||||
if (gated) {
|
||||
/* the sparse attention gated and quantised in its merge */
|
||||
} else if (op_sig) {
|
||||
/* The gate is the strided second half of each head's [q|gate] pair. */
|
||||
RAD_ISSUE_N(c, op_sig, T * g.n_head,
|
||||
bcol(w.qg, g.head_dim, g.head_dim, T), brows(w.gate, T));
|
||||
RAD_ISSUE(c, op_mul, brows(w.attn.x, T), brows(w.gate, T), brows(w.attn.x, T));
|
||||
} else if (!gated)
|
||||
bcol(w.qg, g.head_dim, g.head_dim, T), brows(gate, T));
|
||||
RAD_ISSUE(c, op_mul, brows(w.attn.x, T), brows(gate, T), brows(w.attn.x, T));
|
||||
q_gate.step(c, T);
|
||||
} else {
|
||||
RAD_ISSUE_N(c, op_gq, T * g.n_head,
|
||||
bcol(w.qg, g.head_dim, g.head_dim, T), brows(w.attn.x, T),
|
||||
brows(w.attn.cq(), T), brows(w.attn.cs(), T), brows(w.attn.x, T));
|
||||
}
|
||||
|
||||
o.step(c, w.attn, w.h.x, T);
|
||||
if (op_ar && !ar_taken(g, T, ar_out, ar_out_take))
|
||||
if (own_ar && op_ar && !ar_taken(g, T, ar_out, ar_out_take))
|
||||
RAD_ISSUE_N(c, op_ar, T * g.n_embd, brows(w.h.x, T), RAD_NONE);
|
||||
if (op_add) RAD_ISSUE(c, op_add, brows(w.x, T), brows(w.h.x, T), brows(w.x, T));
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user