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:
Deadcode committed 2026-10-16 18:25:18 +02:00
1 parent b068190326
commit 1e968f6e1d
739 files changed
+128447 -84632

No files matched your search

+99 -170
View File
@@ -1,35 +1,26 @@
/* rad_block_attn_gated.h -- one GATED grouped-query attention block, bf16 operands, paged KV.
/* rad_block_attn_gated.h -- gated grouped-query attention, bf16 operands, paged KV.
*
* rmsnorm -> q|gate gemm, k gemm, v gemm -> qk-norm + rope + gate extraction
* -> kv_store -> paged attention -> attn * sigmoid(gate) -> o gemm -> [all_reduce] -> residual
* rmsnorm -> q|gate gemm, k gemm, v gemm -> qk-norm + rope -> kv_store -> paged attention
* -> attn * sigmoid(gate) -> o gemm -> [all_reduce] -> residual
*
* This is Qwen3.5's attention and not Qwen3's, and the difference is not a flag:
* Qwen3.5's attention, which differs from Qwen3's in three ways:
*
* * `q_proj` emits the query AND a per-head output gate INTERLEAVED at a pitch of 2*head_dim.
* Head h's query is at h*2*head_dim and its gate at h*2*head_dim + head_dim, so neither is a
* contiguous half of the projection and both are strided views of one buffer. libr4d's
* `qk_norm_rope_gate` splits [q|gate] per head and takes the stride off the `qg` OPERAND's own
* row pitch, so the interleaved layout is the one to declare rather than to convert away at
* load. There is no `qg_pitch` PARAMETER to pass -- see pQkNormRopeGate in
* libr4d/r4d_rows.cpp -- the stride comes from the operand.
* * The gate multiplies the attention OUTPUT -- `out = attn * sigmoid(gate)` -- so it is
* extracted before attention and consumed after it. `output_gate_type: "swish"` in the config
* names the GDN block's gate, not this one; llama.cpp uses sigmoid here and it is ground truth
* (../llama.cpp/src/models/qwen35.cpp, build_layer_attn).
* * RoPE is PARTIAL -- `partial_rotary_factor` 0.25 rotates the first 64 of 256 head dims -- and
* it is MROPE. See the note on `mode` in declare(): with one position component per token,
* which is all RadBatch carries, mrope and neox are the same arithmetic, and that equality is
* what makes this declaration honest for text and wrong for an image span.
* * `q_proj` emits the query and a per-head output gate interleaved at a pitch of 2*head_dim:
* head h's query at h*2*head_dim, its gate at h*2*head_dim + head_dim. Both are strided views
* of one buffer. libr4d's `qk_norm_rope_gate` takes the stride from the `qg` operand's row
* pitch (there is no `qg_pitch` parameter; pQkNormRopeGate in libr4d/r4d_rows.cpp), so the
* interleaved layout is declared as is.
* * The gate multiplies the attention output, `out = attn * sigmoid(gate)`. The config's `output_gate_type: "swish"` refers to the
* GDN block's gate; this one is sigmoid, as in llama.cpp (src/models/qwen35.cpp,
* build_layer_attn).
* * RoPE is partial (`partial_rotary_factor` 0.25 rotates the first 64 of 256 head dims) and is
* M-RoPE; see `mode` in declare().
*
* K AND V ARE NOT FUSED INTO ONE PROJECTION, and that is a decision with a cost: two GEMM launches
* a layer instead of one, and two movement units instead of one. It buys every consumer a
* CONTIGUOUS operand. A fused [k|v] row makes the per-head k norm a rank-2 column slice whose last
* dimension is kv_dim while the norm's width is head_dim, and a kernel that reads extents off its
* operands -- libref does, by rule -- then walks it as if it were contiguous and normalises
* across the k/v seam. The fused form is expressible the moment the slice carries the head
* dimension in its own right; until then this is correct and slightly slow rather than fast and
* silently wrong. libr4d's kernel takes a `k_pitch` for the fused form, so the w8a8 sibling can
* make the other choice without changing this block's shape.
* k and v are separate projections (two GEMMs, two movement units) so every consumer gets a
* contiguous operand. With a fused [k|v] row the per-head k norm would be a column slice whose
* last dimension is kv_dim, not head_dim, and a kernel reading extents from its operands (libref
* does) would normalise across the k/v seam. libr4d's kernel takes a `k_pitch` for the fused
* form, so a quantised sibling can fuse without changing this block's shape.
*/
#ifndef RAD_BLOCK_ATTN_GATED_H
#define RAD_BLOCK_ATTN_GATED_H
@@ -40,9 +31,8 @@ namespace rad {
namespace arch {
struct AttnGatedBF16 {
/* The activation buffers this block reads and writes. Declared once by the plugin and shared
* by every layer of this kind: layers that share a geometry share the buffer (spec §8), and
* the liveness analysis over the declared read and write sets is what makes that safe. */
/* Activation buffers, declared once by the plugin and shared by every layer of this kind
* (spec §3.1); liveness over the declared read/write sets makes the sharing safe. */
struct Wire {
rad_buf x = 0; /* [max_tok, n_embd] the residual stream */
rad_buf h = 0; /* [max_tok, n_embd] normed input, then the o output */
@@ -50,14 +40,12 @@ struct AttnGatedBF16 {
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 */
rad_buf gate = 0; /* [max_tok, q_dim] de-interleaved, then sigmoid in place */
rad_buf gate = 0; /* [max_tok, q_dim] sigmoid of the de-interleaved gate */
rad_buf attn = 0; /* [max_tok, q_dim] attention output, then gated in place */
};
/* The checkpoint-side names for one layer. The plugin owns both sides of the map and is the
* only component that knows both (spec §2.4), so the block takes the spelling rather than
* inventing one -- this architecture arrives as `model.language_model.layers.N.*` from the
* safetensors checkpoint and as `blk.N.*` from a GGUF. */
/* Checkpoint-side names for one layer, supplied by the plugin, which owns the map (spec §2.4):
* `model.language_model.layers.N.*` from safetensors, `blk.N.*` from GGUF. */
struct Src {
const char* norm = nullptr; /* input_layernorm.weight */
const char* qg = nullptr; /* self_attn.q_proj.weight -- query AND gate */
@@ -78,8 +66,7 @@ struct AttnGatedBF16 {
rad_weight w_norm = 0, w_qg = 0, w_k = 0, w_v = 0, w_q_norm = 0, w_k_norm = 0, w_o = 0;
rad_op op_norm = 0, op_qg = 0, op_kp = 0, op_vp = 0;
rad_op op_fused = 0; /* qk_norm_rope_gate, if it resolves */
rad_op op_q_norm = 0, op_k_norm = 0, op_rope_q = 0, op_rope_k = 0; /* the fallback */
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_sigmoid = 0, op_gate = 0;
rad_op op_o = 0, op_ar = 0, op_add = 0;
@@ -103,13 +90,10 @@ inline int AttnGatedBF16::declare(RadBuilder* b, Names& nm, const Geom& geom, in
return RAD_E_INVAL;
}
/* KV-HEAD REPLICATION IS NOT PLUMBED HERE. `geom_from` answers a world size above the KV head
* count by giving every rank ONE kv head and a `kv_off` row span into a projection that stays
* `n_head_kv_all * head_dim` rows in the container -- and this block declares its k and v with
* the plain even row-shard, which at four ranks would hand a rank half of one KV head. The
* shapes would declare, the numbers would be wrong, and the text would be fluent. Refuse by
* name instead: rad_block_attn_gated_fp8.h is the block that passes the span, and a bf16 model
* that needs TP4 plumbs it the same way. */
/* KV-head replication is not supported here. Above the KV head count geom_from gives each rank
* one KV head and a `kv_off` row span, but this block declares k and v with the plain even
* row shard, which would hand a rank part of one head and compute wrong values. The span is
* passed in rad_block_attn_gated_fp8.h. */
if (g.world > g.n_head_kv_all) {
fprintf(stderr, "radiance: %s declares no KV row span and cannot serve world_size = %d "
"with n_head_kv = %lld; the KV-head replication in geom_from needs the "
@@ -118,13 +102,12 @@ inline int AttnGatedBF16::declare(RadBuilder* b, Names& nm, const Geom& geom, in
return RAD_E_UNSUPPORTED;
}
/* ---- weights. Shapes are [out, in]: gemm_nt is A[M,K] times B transposed [N,K], the layout
* every transformer weight already has, so nothing is transposed at load.
/* ---- weights. Shapes are [out, in]: gemm_nt multiplies A[M,K] by B[N,K] transposed, the
* layout checkpoints already use.
*
* The q|gate projection row-shards by HEAD, and that works only because each head's query and
* gate are adjacent: rows [h*2*head_dim, (h+1)*2*head_dim) are one head's pair, so a rank's
* slice of the head range is a contiguous row range. A projection that put all queries before
* all gates would not shard this way. */
* The q|gate projection row-shards by head because each head's query and gate are adjacent:
* rows [h*2*head_dim, (h+1)*2*head_dim) are one head's pair, so a rank's heads form a
* contiguous row range. */
RAD_ARCH_TRY(map_copy(b, nm.f("blk.%d.attn_norm.weight", l), src.norm));
RAD_ARCH_TRY(map_copy(b, nm.f("blk.%d.attn_qg.weight", l), src.qg));
RAD_ARCH_TRY(map_copy(b, nm.f("blk.%d.attn_k.weight", l), src.k));
@@ -141,8 +124,7 @@ inline int AttnGatedBF16::declare(RadBuilder* b, Names& nm, const Geom& geom, in
RAD_ACCESS_PER_TOKEN, RAD_SHARD_ROW, grp_layer(l));
w_v = decl_w(b, nm.f("blk.%d.attn_v.weight", l), RAD_BF16, {kv_dim, g.n_embd},
RAD_ACCESS_PER_TOKEN, RAD_SHARD_ROW, grp_layer(l));
/* One gain vector shared by every head, so it is replicated across ranks even though the heads
* are not. */
/* One gain vector shared by every head, so replicated across ranks. */
w_q_norm = decl_w(b, nm.f("blk.%d.attn_q_norm.weight", l), RAD_F32, {g.head_dim},
RAD_ACCESS_PER_TOKEN, RAD_SHARD_NONE, grp_layer(l));
w_k_norm = decl_w(b, nm.f("blk.%d.attn_k_norm.weight", l), RAD_F32, {g.head_dim},
@@ -150,10 +132,8 @@ inline int AttnGatedBF16::declare(RadBuilder* b, Names& nm, const Geom& geom, in
w_o = decl_w(b, nm.f("blk.%d.attn_output.weight", l), RAD_BF16, {g.n_embd, q_dim},
RAD_ACCESS_PER_TOKEN, RAD_SHARD_COL, grp_layer(l));
/* ---- ops, in the order they run, with the one exception noted at attn_paged. Each states the
* buffers it reads and writes, because that is what the buffer planner computes liveness
* from -- and a gated attention layer has six activation buffers where a plain one has three,
* so an undeclared use here costs an arena, not a rounding error. */
/* ---- ops, in run order except attn_paged (see there). Each states its read and write sets,
* from which the buffer planner computes liveness. */
op_norm = rw(b, RAD_OP(b, "rmsnorm",
RAD_PARAMS(RAD_RANGE("M", 1, g.max_tok), RAD_INT("n", g.n_embd),
RAD_F64("eps", g.eps), RAD_F64("wadd", g.wadd), RAD_STR("dtype", g.dtype)),
@@ -176,89 +156,59 @@ inline int AttnGatedBF16::declare(RadBuilder* b, Names& nm, const Geom& geom, in
RAD_WEIGHTS(w_v)),
{w.h}, {w.v});
/* Fusion is a selection, not a compiler pass (spec §2.3): ask for the fused op, and emit the
* sequence it stands for when nothing in the hierarchy serves it.
/* The fused qk_norm_rope_gate is not declared: libr4d's version (libr4d/r4d_rows.cpp,
* pQkNormRopeGate) takes a precomputed cos/sin table operand, and this block has no PERSIST
* [max_ctx, rot] table to give it. The unfused sequence below runs everywhere, including
* without libr4d.
*
* THE PROBE IS NOT MADE, because this block cannot fill the fused op's schema. libr4d's
* r4d_qk_norm_rope_gate (libr4d/r4d_rows.cpp, pQkNormRopeGate) names the rotary width `rot`,
* takes `q_dtype`, and -- the part that matters -- takes a PRECOMPUTED COS/SIN TABLE as a
* required input operand instead of `theta`. Nothing in arch/ or core/ builds such a table.
* Declaring the op with parameters the schema does not have is a STRUCTURAL error and fails
* declare outright; it is not a miss that falls through to the sequence below, because an
* unknown parameter is a caller bug and the builder is right to say so.
* `mode` is "mrope" (mrope_section [11,11,10], interleaved). Each rotary dimension has its own
* frequency and the section only chooses which position component feeds it, so with equal
* components (text) mrope is bit-identical to neox.
*
* What making the probe needs, precisely: a PERSIST buffer of [max_ctx, rot] filled once from
* theta, and a way for a plugin to fill a declared buffer at load -- which the ABI has no hook
* for, since buffers are arena storage and weights come from the container. Both are real work
* and neither belongs in a probe. Until then this model runs the unfused sequence, which is
* correct everywhere and is what makes it run on an install with no libr4d at all.
*
* `mode` would be "mrope" because that is what the model does: mrope_section [11,11,10] over
* an interleaved layout. TWO THINGS ABOUT THAT ARE NOT EXPRESSIBLE and both stay true of the
* unfused path. The sections have no parameter in any op schema, and RadBatch carries ONE
* position component per token, so all three sections would receive the same position. For
* text that is not an approximation: every rotary dimension has its own frequency and the
* section only decides which position component feeds it, so mrope over equal components is
* bit-identical to neox. It stops being identical the moment an image span gives the
* components different values, which is why the vision tower needs both a sections parameter
* and a 3-row position array before it can be correct. */
op_fused = RAD_NULL_HANDLE;
{
* Per-head RMS norm is a rmsnorm with M = tokens x heads, n = head_dim. The q norm reads the
* strided view of the interleaved projection and writes the contiguous q buffer, so it
* de-interleaves in the same pass. The k norm runs in place, which is safe because a row's
* scale is computed before the row is written. */
op_q_norm = rw(b, RAD_OP(b, "rmsnorm",
RAD_PARAMS(RAD_RANGE("M", 1, g.max_tok * g.n_head),
RAD_INT("n", g.head_dim),
RAD_F64("eps", g.eps), RAD_F64("wadd", g.wadd_qk), RAD_STR("dtype", g.dtype)),
RAD_WEIGHTS(w_q_norm)),
{w.qg}, {w.q});
op_k_norm = rw(b, RAD_OP(b, "rmsnorm",
RAD_PARAMS(RAD_RANGE("M", 1, g.max_tok * g.n_head_kv),
RAD_INT("n", g.head_dim),
RAD_F64("eps", g.eps), RAD_F64("wadd", g.wadd_qk), RAD_STR("dtype", g.dtype)),
RAD_WEIGHTS(w_k_norm)),
{w.k}, {w.k});
/* Two rope calls because q and k are separate buffers. `rope` describes a fused [q|k|v] row by
* n_head and n_head_kv; n_head_kv 0 makes it a q-only (or k-only) row. */
op_rope_q = rw(b, decl_rope(b, g, g.max_tok, g.head_dim, g.n_head, 0, n_rot, mode),
{w.q}, {w.q});
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});
/* Per-head RMS norm is an ordinary rmsnorm with M = tokens x heads and n = head_dim. The q
* one reads the STRIDED view of the interleaved projection and writes the contiguous q
* buffer, so it de-interleaves and normalises in one pass: the copy llama.cpp spends a
* ggml_cont on is this op's output operand. The k one runs in place, which is safe for any
* implementation that computes a row's scale before writing the row -- and none can do
* otherwise. */
op_q_norm = rw(b, RAD_OP(b, "rmsnorm",
RAD_PARAMS(RAD_RANGE("M", 1, g.max_tok * g.n_head),
RAD_INT("n", g.head_dim),
RAD_F64("eps", g.eps), RAD_F64("wadd", g.wadd_qk), RAD_STR("dtype", g.dtype)),
RAD_WEIGHTS(w_q_norm)),
{w.qg}, {w.q});
op_k_norm = rw(b, RAD_OP(b, "rmsnorm",
RAD_PARAMS(RAD_RANGE("M", 1, g.max_tok * g.n_head_kv),
RAD_INT("n", g.head_dim),
RAD_F64("eps", g.eps), RAD_F64("wadd", g.wadd_qk), RAD_STR("dtype", g.dtype)),
RAD_WEIGHTS(w_k_norm)),
{w.k}, {w.k});
/* Two rope calls rather than one, because q and k are two buffers here. `rope` describes a
* fused [q|k|v] row through n_head and n_head_kv; a call that names only q heads is that
* row with an empty k and v, which is what a separate q buffer is. */
op_rope_q = rw(b, decl_rope(b, g, g.max_tok, g.head_dim, g.n_head, 0, n_rot, mode),
{w.q}, {w.q});
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});
}
/* attn_paged is declared BEFORE kv_store even though it runs after it. block_size is
* RAD_DERIVED -- the KV block size is a property of the resolved attention kernel and not a
* number anyone gets to choose (spec §7.2) -- so it is OMITTED here, the selector skips
* constraints on it while matching, writes the winner's value into the resolved geometry, and
* kv_store reads it back with rad_kv_block_size(). The two ops touch disjoint buffers, so the
* declared order differing from the issue order costs the buffer planner nothing.
/* attn_paged is declared before kv_store although it runs after it. `block_size` is
* RAD_DERIVED, a property of the resolved attention kernel (spec §7.2): the selector writes the
* winner's value into the resolved geometry and kv_store reads it with rad_kv_block_size().
* The ops touch disjoint buffers, so the order mismatch does not affect buffer planning.
*
* The ranged parameter is q_len, not M: one query row at decode, up to n_spec+1 at a
* speculative verify, thousands in a prefill chunk, and libr4d has a different kernel for each
* band. `scale` is left off: 1/sqrt(head_dim) is the default and is what this model uses. */
* The ranged parameter is q_len, not M: 1 at decode, up to n_spec+1 at a verify, thousands in
* a prefill chunk, with a libr4d kernel per band. `scale` defaults to 1/sqrt(head_dim). */
op_attn = rw(b, 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),
/* n_head, max_seqs and max_ctx are what the resolved kernel's SCRATCH HOOK
* needs at declare: libr4d sizes a split-KV partial buffer of
* num_seqs * q_len * q_heads * splits rows, and gqa gives the head
* ratio rather than the count. All three are bounds, which is the
* right kind of number for an arena sized once for the worst step. */
/* Bounds for the kernel's scratch hook: libr4d sizes a split-KV
* partial buffer of num_seqs * q_len * q_heads * splits rows. */
RAD_INT("n_head", g.n_head), RAD_INT("block_size", RAD_KV_BLOCK), RAD_INT("max_seqs", 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),
{w.q}, {w.attn});
/* kv_store writes the group's pool, which is not a declared buffer -- so its write set is
* empty and its read set is the two projections it stores. */
/* kv_store writes the group's pool, which is not a declared buffer, so its write set is
* empty. */
op_kv_store = rw(b, RAD_OP(b, "kv_store",
RAD_PARAMS(RAD_RANGE("M", 1, g.max_tok), RAD_INT("head_dim", g.head_dim),
RAD_INT("n_head_kv", g.n_head_kv),
@@ -267,19 +217,14 @@ inline int AttnGatedBF16::declare(RadBuilder* b, Names& nm, const Geom& geom, in
RAD_NOWEIGHTS),
{w.k, w.v}, {});
/* The gate. Two ops rather than one fused `mul(attn, sigmoid(gate))`, because the vocabulary
* has no gated multiply for a bf16 output -- `gated_had_quant_i8` is the same arithmetic with
* an int8 epilogue and is what the w8a8 sibling will use instead of this pair.
*
* The sigmoid's INPUT differs between the two paths and its output does not: the fused kernel
* has already written a contiguous gate, and the fallback reads the strided view of the
* interleaved projection. Same op, same geometry, one operand spelled two ways -- which is why
* there is no second declaration and no branch after this point. */
/* The gate, as two ops: the op vocabulary has no gated multiply with a bf16 output
* (`gated_had_quant_i8` is the same arithmetic with an int8 epilogue). The sigmoid reads the
* strided gate view of the interleaved projection into the contiguous gate buffer. */
op_sigmoid = 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.gate});
{w.qg}, {w.gate});
op_gate = rw(b, RAD_OP(b, "mul",
RAD_PARAMS(RAD_RANGE("M", 1, g.max_tok), RAD_INT("n", q_dim),
@@ -293,12 +238,10 @@ inline int AttnGatedBF16::declare(RadBuilder* b, Names& nm, const Geom& geom, in
RAD_WEIGHTS(w_o)),
{w.attn}, {w.h});
/* The output projection is column-sharded, so each rank holds a partial sum of the residual
* update and the ranks must agree before the add. The core inserts nothing: this collective is
* here because the plugin put it here, and --debug-graph shows it exactly here (spec §9).
* `exact` is stated rather than defaulted -- a caller that does not say it will accept a
* quantised wire gets the exact kernel or no kernel. `hops` is not stated: one-shot against
* two-shot is the kernel library's choice, by message size. */
/* The column-sharded output projection leaves a partial sum on each rank. The plugin emits the
* collective itself, so --debug-graph shows it here (spec §9). `exact` is always stated, so a
* deployment that has not accepted a lossy wire gets the exact kernel. `hops` (one-shot vs
* two-shot) is left to the kernel library, by message size. */
if (g.world > 1)
op_ar = rw(b, RAD_OP(b, "all_reduce",
RAD_PARAMS(RAD_INT("world_size", g.world),
@@ -318,9 +261,8 @@ inline int AttnGatedBF16::declare(RadBuilder* b, Names& nm, const Geom& geom, in
}
inline void AttnGatedBF16::step(RadCtx* c, const RadBatch* batch) const {
/* rad_issue records its own failure and the core aborts the step and fails the requests in it,
* naming the kernel and the geometry (spec §17). There is nothing a plugin can usefully do
* with the status, and ignoring it is what keeps the run phase a flat readable list. */
/* Issue statuses are ignored: rad_issue records a failure and the core aborts the step and
* fails its requests, naming the kernel and geometry (spec §17). */
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;
@@ -333,44 +275,31 @@ inline void AttnGatedBF16::step(RadCtx* c, const RadBatch* batch) const {
RAD_ISSUE(c, op_kp, brows(w.h, T), RAD_W(w_k), brows(w.k, T));
RAD_ISSUE(c, op_vp, brows(w.h, T), RAD_W(w_v), brows(w.v, T));
if (op_fused) {
RAD_ISSUE(c, op_fused,
brows(w.qg, T), brows(w.k, T), praw(batch->positions, RAD_I32, T),
RAD_W(w_q_norm), RAD_W(w_k_norm),
brows(w.q, T), brows(w.gate, T));
} else {
/* The q view of the interleaved projection: head_dim columns out of every 2*head_dim, the
* declared width kept as the stride. */
RAD_ISSUE_N(c, op_q_norm, T * g.n_head,
bcol(w.qg, 0, g.head_dim, T), RAD_W(w_q_norm), brows(w.q, 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_q, brows(w.q, T), rope_posmc(g, batch, T));
RAD_ISSUE(c, op_rope_k, brows(w.k, T), rope_posmc(g, batch, T));
}
/* The q view of the interleaved projection: head_dim columns of every 2*head_dim. */
RAD_ISSUE_N(c, op_q_norm, T * g.n_head,
bcol(w.qg, 0, g.head_dim, T), RAD_W(w_q_norm), brows(w.q, 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_q, brows(w.q, T), rope_posmc(g, batch, 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));
/* The actual query length, not the batch's token count: it is what selects the attention band,
* and a verify step and a prefill chunk can carry the same token count at very different
* q_len. */
/* The longest query length, not the token count, selects the attention band: a verify step and
* a prefill chunk can have the same token count at very different q_len. */
RAD_ISSUE_N(c, op_attn, batch->max_q_len,
brows(w.q, T), kv_cache(kv, layer),
praw2(table, RAD_I32, batch->n_seq, 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, T));
/* out = attn * sigmoid(gate). The fused kernel wrote the gate contiguously; the fallback left
* it interleaved and this is where it is read out. */
if (op_fused) RAD_ISSUE_N(c, op_sigmoid, T * g.n_head, brows(w.gate, T), brows(w.gate, T));
else RAD_ISSUE_N(c, op_sigmoid, T * g.n_head,
bcol(w.qg, g.head_dim, g.head_dim, T), brows(w.gate, T));
/* out = attn * sigmoid(gate), the gate read out of the interleaved projection. */
RAD_ISSUE_N(c, op_sigmoid, T * g.n_head,
bcol(w.qg, g.head_dim, g.head_dim, T), brows(w.gate, T));
RAD_ISSUE(c, op_gate, brows(w.attn, T), brows(w.gate, T), brows(w.attn, T));
RAD_ISSUE(c, op_o, brows(w.attn, T), RAD_W(w_o), brows(w.h, T));