# The conventional op vocabulary Op names are free strings (`spec.md` §2.3) and the core never learns one. But `libref` cannot implement an op it has never heard of, so **ref's op list is the conventional vocabulary**: an architecture plugin that stays inside it is guaranteed to run somewhere, and one that invents an op has no fallback for it (`spec.md` §17). This file is that list. `libref/` holds the authoritative `RadOpSchema` rows; this is what they say, in a form an architecture plugin author reads first. **AND IT IS CHECKED.** `tests/CMakeLists.txt` registers `ops_doc`, which dumps every plugin's schemas with `rad-schemas` and holds every table row below to them — op for op, parameter for parameter, operand for operand, including the optional markers and the `w` that makes an operand a weight. Run it by hand as: tools/ops_doc_check.sh docs/OPS.md build/bin/rad-schemas build/radiance_home/kernels/*.so A row that is a PROPOSAL rather than a description carries a FOURTH COLUMN saying so and is exempt from the check. That is how an op the vocabulary has agreed on but no plugin declares yet stays in this document without being a lie, and it is the only way one may. An op that a kernel library serves and this document omits gets written against a GUESS at its schema, and no fallback absorbs a wrong guess — `qk_norm_rope_gate` and `qk_norm_rope` differ in exactly the way a guess gets wrong, one taking a `theta` and the other a precomputed cos/sin table. **A document that is WRONG is worse than one that is missing**, because it is read instead of the source. **A ROW HERE DOES NOT MEAN THERE IS A HOST KERNEL.** A table marked **device-only** is declared by libr4d and by nothing else, so its ops have no oracle for `rad-kbench` and no host fallback: an architecture that reaches for one is choosing to run on this library or not at all. That is a legitimate choice — every fusion in this document is one — but it is a choice, and it should be made knowingly. ## Conventions - Operands are **positional**, in the order given. `w` marks a weight operand, `→` the outputs. - A tensor's leading dimension is tokens unless stated. Everything is contiguous unless an op says otherwise; strides are the kernel's business, not the schema's. - `dtype` is always present and always a string. It names the **operand combination**, which is why a weight format and an activation format share one key: `bf16`, `f16`/`fp16`, `f32`/`fp32`, `fp8_e4m3`, `fp8a8`, `fp8a16`, `w4`, `w4a8`, `w4a8_asym`, `w4a8h`, `w8a8`, `w4a16`, `w8a16`, `w2a8`, `mxfp4a8`. `w4a8h` is `w4a8` with a `group`-wide Walsh–Hadamard along K baked into the stored weight (`docs/MOE-W4.md`), `w4a8_asym` the unsigned-nibble-minus-zero-point grid rather than the symmetric one, and `fp8a8`/`fp8a16` say whether a block-scaled E4M3 weight is met by an fp8 activation or a bf16 one. `q_dtype` and `kv_dtype` are the same key split in two where attention reads two formats at once. - `M` is the ranged parameter wherever a token count appears. **At most one ranged parameter per op** — that is what keeps `rad_issue` a single integer on the hot path. - Every op below returns `RAD_OK` or a negative `RAD_E_*`. There is no partial success. - In a row, `?` marks an optional operand, `/opt` an optional parameter and `/der` a derived one, and none of the three is decoration: `/der` says the KERNEL supplies the value rather than the caller, an omitted optional parameter is one a kernel will ignore when a caller passes it, and a constraint on an `:f64` key never matches, because a tolerance is not a predicate. ## How a schema is written `libref` and `libr4d` implement the same ops from opposite ends, and a schema they do not agree on cannot be loaded at all — the loader refuses the whole plugin on any one conflict, which makes every other row of it unreachable and `rad-kbench` unrunnable. Four rules settle every case, and they are why `gdn_conv_prep` and `gdn_recurrent_update` carry the long operand lists they do. ### The four rules that resolve every case 1. **Operands are the UNION, and anything only one implementation needs is `optional`.** A schema that lacks an operand cannot express the call at all, so the narrower reading always loses. An implementation ignores an optional operand it does not use; it cannot invent one it needs. 2. **Parameters are the UNION, and anything only one implementation needs is `optional`.** Same argument. A constraint on an absent key simply does not hold, which is the correct answer: a row that needs a parameter the caller did not state is not a match. 3. **A float-valued parameter is `RAD_P_F64`.** `eps`, `scale`, `theta`, `softplus_thr`, `l2_eps`, `wadd`. A `"%.9g"` string round-trips an IEEE single exactly and is unreadable in a graph dump, which is the whole of the argument against spelling them as strings. **Constraints never match on a float** — a tolerance is not a predicate, and a kernel that cares about `eps` to that precision has a bug. 4. **An enumerated parameter is `RAD_P_STR` with named values.** `act` is `"silu"` or `"sigmoid"`, not `0` or `1`. A graph dump reading `act=1` is precisely the "unclear what actually runs" complaint this engine exists to answer; a plugin's launch adapter maps the name to whatever integer its kernel wants. The union rule has exactly two deliberate exceptions, both on the gated delta net and both argued where those ops are described: `cu` and `num_accepted` are REQUIRED, because an op that lets the caller omit either is an op whose contract is optional. ### Parameters the kernel supplies `attn_paged.block_size`, `gdn_chunk_scan.chunk`, `gdn_kkt_solve.chunk` and `gdn_conv_prep.chunk` are supplied by the **kernel**, not the caller (`spec.md` §7.2, and `RAD_DERIVED` in `rad_abi.h`). Constraints on them are skipped during matching and the winning kernel's `RAD_C_EQ` value is written into the resolved geometry afterwards. Declaring them `RAD_REQUIRED` makes them unselectable: a constraint whose key the geometry lacks does not hold, so a kernel compiled for `block_size == 16` could never be reached by the query whose answer was to be read off it. `gdn_conv_prep.chunk` has a cost the other three do not: nothing in conv_prep's operands carries the chunk length, so `libref`, which declares no `RAD_C_EQ` to answer with, returns `RAD_E_SHAPE` by name when a caller omits it rather than quietly treating the whole sequence as one chunk. libr4d supplies 64. `all_reduce.hops` is `RAD_OPTIONAL` — a one-shot/two-shot distinction the caller may state and usually should not. `all_reduce.exact` stays `RAD_REQUIRED`: a caller that does not say it will accept a lossy wire gets no kernel rather than the quantised one. ### Weight operands, and the weight TABLE - **`RAD_OPD_WEIGHT`** — the issue names one weight handle, and the kernel receives that weight's bytes, its dtype and its declared shape. - **`RAD_OPD_WTAB`** — the issue names the FIRST handle of a run and the run's length, and the kernel receives a device array of `n` pointers, one per entry. `RAD_WTAB(h, n)` in `abi/rad_runtime.h` is the issue-side spelling, and `rad_decl_weight` handing out consecutive indices is what makes a declaration loop over experts produce such a run. **Why it is a table and not a base plus a stride.** A layer's experts are separate movement units (`spec.md` §4.1): after placement one is in VRAM, the next is streamed over the link and the third is still on disk. `base + e * stride` describes exactly one of those cases — the one where nothing was offloaded — and expert offload is the reason the placement engine exists. So the core resolves every handle in the run the way it resolves any weight (residency table, mover event, use note) and uploads a pointer array, rebuilding it only when a pointer actually moved. **What the kernel sees.** `RadTensor::data` points at the pointer array; `shape[0]` is the entry count and **`stride[0]` is 0**, which is the marker. `dtype` and the trailing extents and strides are ONE entry's, and every entry of a run must agree about them — the core refuses the issue by name if they do not, because a table whose entries have different shapes is a table the kernel cannot walk. A kernel may also accept the STACKED form (an ordinary rank-3 tensor with a real leading stride): `rad-kbench` replaying a fixture has nowhere to put a pointer array, and both libref and libr4d read either, so a described case runs against the same bytes the engine would hand it. **How the declaration divides.** An op may carry several table operands — `moe_gemm_q` has four, the codes and the scale planes of its even experts and of its odd ones. Whatever is left after the fixed weight positions have taken one each is split EVENLY across the table positions, in declaration order, and where it does not come out whole the first positions take one more each: `moe_gemm_q_mod4`'s first `n_expert % 4` classes hold one expert more than the rest, and their codes and scales are its first table positions. A list too short to give every table an entry is refused at declare. A table whose declared run is not contiguous is refused when the tables are bound, before the first step; one whose entries disagree about their shape, or an issue naming a run other than the declared one, is refused at issue. That is exactly right for the case tables exist for: every table of one MoE op takes one weight per expert of its class. ## What the loader enforces **A kernel plugin must declare a `RadOpSchema` for every op it has a `RadKernelInfo` row for.** The loader checks this after the whole hierarchy is open — deferred to the end because an override plugin may legitimately rely on the schema of the plugin below it — and refuses the load naming the plugin and the op. Without the rule, a typo and an unimplemented op are the same diagnostic. The first plugin in hierarchy order to declare an op **fixes its schema**. A later plugin declaring the same op name with a *different* schema is refused at load with both plugins named; declaring it identically is fine and is what every plugin implementing a shared op does. Since arguments are positional, two disagreeing schemas would make the same call mean different things depending on which plugin won selection — silent numerical garbage rather than a diagnosable failure. `build/bin/rad-schemas a.so b.so` prints and diffs what plugins actually declare. Run it before adding a kernel plugin; "refused at load" names one op and leaves you to find the rest. --- ## Elementwise and norms | op | params | operands | |---|---|---| | `rmsnorm` | `M`(range) `n` `eps`:f64 `dtype` `wadd`:f64/opt | `x`, `w`w → `y` | | `rmsnorm_add` | `M`(range) `n` `eps`:f64 `dtype` `wadd`:f64/opt | `x`, `residual`, `w`w → `y`, `residual_out` | | `hc_enter` | `M`(range) `n` `hc` `dtype` | `x` → `h` | | `hc_read` | `M`(range) `n` `hc` `lowrank` `eps`:f64 `dtype` `wadd`:f64/opt `inject`/opt `group`/opt `rotate`/opt `mix`/opt | `h`, `w`w, `mix_down`w, `mix_up`w, `inject_w`w? → `x`, `inj`?, `q`?, `scale`?, `rq`?, `rscale`? | | `hc_write` | `M`(range) `n` `hc` `dtype` | `y`, `inj`, `h`:inout | | `layernorm` | `M`(range) `n` `eps`:f64 `dtype` | `x`, `w`w, `b`w? → `y` | | `grid_embed` | `M`(range) `n` `side` `dtype` | `x`:inout, `table`w, `coord` | | `add` | `M`(range) `n` `dtype` | `a`, `b` → `y` | | `mul` | `M`(range) `n` `dtype` | `a`, `b` → `y` | | `silu_mul` | `M`(range) `n` `dtype` | `gate_up` → `y` (splits `[M,2n]` into gate·up) | | `gelu` / `silu` | `M`(range) `n` `dtype` | `x` → `y` | | `cast` | `M`(range) `n` `from` `to` | `x` → `y` | | `softmax` | `M`(range) `n` `dtype` | `x` → `y` | | `sigmoid` | `M`(range) `n` `dtype` | `x` → `y` | | `silu` | `M` `n` `dtype` | `x` → `y` | `gelu` and `silu` are two ops with one schema, and `silu` carries a row of its own because a row names one op. `rmsnorm_add` exists because the residual add and the next norm are one pass in every real implementation. A plugin asks for it first and falls back to `add` + `rmsnorm` (`spec.md` §2.3). `hc_read` / `hc_write` are the GATED RESIDUAL pair, which is what a hyper-connection model has instead of a pre-norm and a residual add. The stream is `hc` times the model width and every block reads a mix of the `hc` sub-streams and writes back into all of them, both gated by data: hc_read N = grouped_rmsnorm(h) one rsqrt per n-wide stream, one hc*n gain g = sigmoid(mix_up @ silu(mix_down @ N / hc)) x = mean over the hc streams of g * N inj[s] = 2 * sigmoid(inject_w[s] . N / hc) hc_write h[s] += inj[s] * y Three facts that are not derivable from the others and that a second implementation has to match: the mix is a MEAN over the streams and not a sum; there is a separate `/ hc` inside each of the two gate arguments; and **`hc_write` adds into the UNNORMED `h`** -- the normalisation is consumed by the block input and the gates and never enters the residual. Writing into `N` instead re-normalises the stream 96 times over and still produces fluent text. `inject_w` absent is the final mixer, which collapses the wide stream to one and has nothing to write back; `group` absent is a read whose consumer wants bf16. Otherwise the fp8 form of `x` comes out of the same pass, because a model with 96 connections cannot afford a separate quantise launch at each of them -- and it is quantised from the NARROWED `x`, not from the accumulator, so a fused kernel and the reference agree bit for bit. `rotate` adds `rq`/`rscale`: `had_quant_act_fp8` of the same stored `x`, each group rotated by an unnormalised Hadamard before its absmax, for a consumer whose weight plane carries the rotation (the w4a8h experts) -- which then needs no quantise launch of its own either. `mix` is the mixing matrices' STORED format: `bf16`, or `e4m3` -- rows of E4M3 codes with f32 scales in each row's own tail, a 128-column group for `mix_down` and a quarter row for `mix_up`. It changes the bytes a step reads, not the arithmetic above; the two matrices are replicated on every rank and read once a connection, so at 96 connections the format is worth half of 1.26 GB a step on Qwen3.8-Flash-Next. `mul`'s `b` operand may have extent 1 on any dimension where `a` does not, and is broadcast along it. Every other elementwise op keeps the strict same-shape rule. `sigmoid`, with a broadcast-capable `mul`, is what a gated shared expert needs (`ffn_gate_inp_shexp`). Without it the shared expert runs ungated, which is a different model. ## Quantisation and rotation | op | params | operands | |---|---|---| | `quant_act_i8` | `M`(range) `n` `group` `dtype` | `x` → `q`, `scale`, `asum`? | | `had_quant_act_i8` | `M`(range) `n` `group` `dtype` | `x` → `q`, `scale` | | `rmsnorm_had_quant_i8` | `M`(range) `n` `eps`:f64 `group` `dtype` `wadd`:f64/opt | `x`, `residual`:inout?, `w`w → `q`, `scale`, `out_bf16`? | | `gated_had_quant_i8` | `M`(range) `n` `group` `dtype` `act`:str/opt `mode`/opt | `a`, `b`? → `q`, `scale` | | `dequant` | `M`(range) `n` `group` `dtype` | `q`, `scale` → `y` | | `gram_accum` | `M`(range) `n` `group` `dtype` | `a`, `a_scale`?, `h`:inout | `gram_accum` is the odd one here and belongs in this table anyway, because it characterises a quantiser's output: `h[k1][k2] += sum over rows of dequant(a)[m][k1] * dequant(a)[m][k2]`, in f32. That is the Hessian a GPTQ packer needs, over exactly the operand the consuming GEMM contracts. **`h` is `[n, n]` and INOUT** -- a caller zeroes it once and drives a whole calibration corpus through the graph, so each launch adds its own block; an implementation that stored instead of added would agree on one call and disagree on a corpus. It is **not** `gemm_nt` with a transposed operand: that op contracts the LAST axis of both sides and this one contracts the FIRST, so expressing it as a GEMM would need `x` materialised transposed. It is also the one op in this document that **no forward pass issues** -- it runs under a calibration flag and its result is read at convert time, in another process, through a file. **`a_scale` is optional, and `dtype` says which reading `a` takes.** `fp8a8` is E4M3 codes with an f32 dequant scale per (row, `group` columns): the tap of a model whose expert GEMM contracts a quantised activation. `bf16` is a plain activation read as it stands, with no `a_scale` and no grouping: the tap of a model served at bf16, whose expert GEMM contracts exactly that. **Two activation widths are in use, and which one a linear takes is the WEIGHT's question rather than a preference.** Where the checkpoint already ships E4M3 weights and a block scale plane, the activation follows the weight: `fp8a8` is the shipped path (`arch/common/rad_fp8.h`), because the `fp8a16` form that would read a bf16 activation directly and skip the quantise launch has a kernel only at `M <= 1` — a single-sequence decode and nothing else. Where the engine quantises the activation itself, it is **rotated int8**: the rotation is a per-group Walsh–Hadamard, and it is what makes a single scale per row viable at all, which is what an int8 row buys over a row of floats each carrying its own exponent. **`group` MEANS TWO DIFFERENT THINGS IN THIS TABLE**, and a kernel that reads it the other way is wrong rather than slow. `libref/ref_quant.cpp` is the definition; this is what it says. - **`quant_act_i8`, `dequant`** — `group` is the number of **contiguous elements sharing one f32 scale**, so `scale` is `[M, ceil(n/group)]`. `group <= 0` or `group >= n` means one scale per ROW. For `dequant` the same reading, inverted: the scale stream's extent says how many groups there are and `group` says how wide each one is. - **every `had_*` INT8 form** — `group` is the **Walsh–Hadamard rotation width** (libr4d spells it `had`), and the int8 scale is per **ROW**, carrying the `1/sqrt(group)` normalisation. That is the reading the prose above forces: a per-group rotation is what makes a single scale per row viable, so the rotation width and the scale granularity cannot be the same number. - **the `had_*` FP8 forms** (`had_quant_act_fp8`, `gated_had_quant_fp8`) — `group` is **both**, and that is not an inconsistency but a different bargain. They exist for a four-bit MoE expert whose scale grid is already one bf16 per (row, 128 of K); making the rotation the same 128 lines the two up, so the scale is per group here and shaped `[M, n/group]` exactly as `quant_act_fp8`'s is. They also do **no normalisation at all**: ` = group * `, and the whole residual factor is folded into the **stored weight scale** at convert, where dividing a bf16 by a power of two is exact. Splitting it as the int8 forms do would round `amax/7/sqrt(group)` into bf16 and land a ~0.2% multiplicative error on every group. `n` must be a multiple of `group`: a rotation block cannot straddle the end of a row. - **`quant_act_i8g`, `had_quant_act_i8g`** — `quant_act_fp8`'s grouping and scale shape at INT8 codes: one f32 scale per `group` of a row, `amax / 127` (1 for a zero group), each code the value over it rounded to nearest even and clamped to +-127. They are the activation side of the int8 W8A8 trunk (`gemm_nt_q` at `dtype=i8a8`), which is why they are not `quant_act_i8`: that op's scale is per row unless asked otherwise, and an int8 GEMM folding a scale per K block needs the block. The `had_` form rotates first, as `had_quant_act_fp8` does. The rotation is unnormalised butterflies at ascending strides `1, 2, 4, ... group/2`, pairing `i` with `i + h` where `(i & h) == 0`. The `1/sqrt(group)` is folded into the scale rather than applied to the values -- dividing every element by a constant cannot change which int8 code it rounds to, and folding it costs nothing. A rotation block may not cross a tensor-parallel shard boundary, so the width has to divide the PER-RANK `n`. `gated_had_quant_i8` serves two readings of its input: the gate and up as two operands, which is libr4d's, and one fused `gate_up` `[M, 2n]` with the gate first, which is what a fused gate_up projection produces. Two operands with the second optional expresses both — a caller with a fused buffer passes it as `a` and omits `b` — and `mode` is optional because it is libr4d's integer spelling of `act`, not the vocabulary's. ### The fp8 fusions | op | params | operands | |---|---|---| | `quant_act_fp8` | `M` `n` `group` `dtype` | `x` → `q`, `scale` | | `had_quant_act_fp8` | `M` `n` `group` `dtype` | `x` → `q`, `scale` | | `rmsnorm_quant_fp8` | `M` `n` `eps`:f64 `group` `dtype` `wadd`:f64/opt | `x`, `residual`:inout?, `w`w → `q`, `scale`, `out_bf16`? | | `gated_quant_fp8` | `M` `n` `group` `dtype` `act`:str/opt | `gate_up` → `q`, `scale`, `out_bf16`? | | `gemm_nt_q_gated` | `M` `N` `K` `group` `dtype` `act` | `a`, `a_scale`, `b`w, `b_scale`w → `q`, `scale`, `out_bf16`? | ### The int8 and gated fp8 fusions — device-only | op | params | operands | |---|---|---| | `quant_act_i8g` | `M` `n` `group` `dtype` | `x` → `q`, `scale` | | `had_quant_act_i8g` | `M` `n` `group` `dtype` | `x` → `q`, `scale` | | `gated_had_quant_fp8` | `M` `n` `group` `dtype` `act`:str/opt | `gate_up` → `q`, `scale`, `out_bf16`? | | `gate_quant_fp8` | `M` `n` `group` `dtype` `act`:str/opt | `gate`, `x` → `q`, `scale`, `out_bf16`? | `gemm_nt_q_gated` is the row above pulled into the projection that feeds it: `gemm_nt_q` over a `[N, K]` gate_up plane — `N` is the WEIGHT's rows, twice the fused width, gate half first — with `gated_quant_fp8`'s pass in the epilogue, so the `[M, N]` bf16 tensor between them is never written. It is a separate schema and not a flag on `gemm_nt_q` because it does not produce that op's `y`, and an architecture that resolved it into `gemm_nt_q`'s operand list would hand the GEMM a buffer of half the width it expects. `act` is REQUIRED here, unlike the quantiser's: an implementation is free to compile one gate and an optional parameter would let it resolve for another. `out_bf16` is not an optimisation: the unfused pair leaves a bf16 tensor behind and the codes are taken over *that*, so a layer with a bf16 consumer of the post-norm value must ask for it or the fusion is a different function. `residual` present is the `fused_add_rms_norm` form. ### `dequant_w4` — device-only `libref` declares `dequant` and nothing else declares `dequant_w4`, so this op is not negotiated between plugins the way the shared ones are: it is libr4d's own, spelled libr4d's way. | op | params | operands | |---|---|---| | `dequant_w4` | `N` `K` `group` `dtype` `twos` | `wq`w, `ws`w → `y` | `twos` selects the symmetric two's-complement nibble over the asymmetric unsigned-nibble-minus- zero-point grid. The two differ by bit 3, so the wrong one is not a refusal, it is wrong output. **`wq` and `ws` are WEIGHT operands**, and the `w` marker is the load-bearing part of that. Spelt as plain inputs they name the same bytes, and the core stops resolving their handles and stops waiting on the mover's event — a read of weight bytes that may not have arrived yet. ## GEMM | op | params | operands | |---|---|---| | `gemm_nt` | `M`(range) `N` `K` `dtype` | `a`, `b`w → `y` | | `gemm_nt_bias` | `M`(range) `N` `K` `dtype` `act`/opt | `a`, `b`w, `bias`w, `res`? → `y` | | `gemm_nt_q` | `M`(range) `N` `K` `group` `dtype` | `a`, `a_scale`?, `b`w, `b_scale`w → `y`; `a_sum`?, `b_ref`w? | | `gemm_nt_q_bias` | `M`(range) `N` `K` `group` `dtype` | `a`, `a_scale`, `b`w, `b_scale`w, `bias`w → `y` | `nt` is A row-major `[M,K]` times B **transposed** `[N,K]`, giving `[M,N]` — the layout every weight in a transformer already has. `dtype` distinguishes `bf16` from `w4a8`, so one op name covers the family and the selector picks by the quant string. That is deliberate: an architecture plugin that switches quant changes one string, not its op names. `gemm_nt_q` takes activation scales as an operand because the quantiser produced them; a kernel that folds dequantisation into its epilogue still receives them. ## Attention | op | params | operands | |---|---|---| | `attn_paged` | `q_len`(range) `head_dim` `gqa` `block_size`/der `causal` `window` `q_dtype` `kv_dtype` `scale`:f64/opt `max_ctx`/opt `n_head`/opt `max_seqs`/opt | `q`, `kv_cache`, `block_table`, `seqused`, `k_descale`?, `v_descale`?, `cu_seqlens`? → `out` | | `attn_dense` | `M`(range) `head_dim` `gqa` `causal` `dtype` `scale`:f64/opt `max_seqlen`/opt | `q`, `k`, `v`, `cu_seqlens` → `out` | | `kv_store` | `M`(range) `head_dim` `n_head_kv` `block_size` `kv_dtype` `k_scale`:f64/opt `v_scale`:f64/opt | `k`, `v`, `slot_mapping`, `kv_cache`:inout | | `rope` | `M`(range) `head_dim` `n_head` `n_head_kv` `theta`:f64 `scale`:f64 `mode` `rotary_dim`/opt `sections`/opt | `qkv`:inout, `positions` | | `rope_table` | `M`(range) `rot` `theta`:f64 `scale`:f64 `dtype` | `positions` → `cos_sin` | | `qk_norm_rope` | `M`(range) `head_dim` `n_head` `n_head_kv` `theta`:f64 `eps`:f64 `scale`:f64/opt `mode`/opt `rotary_dim`/opt `wadd`:f64/opt | `qkv`:inout, `positions`, `q_w`w, `k_w`w | `attn_paged` is the ranged op whose band is `q_len`, not `M`: one query row at decode, up to 64 at a speculative verify, thousands in a prefill chunk, and libr4d has a different kernel for each band. `block_size` is **read off the resolved kernel**, not chosen — declare the group, declare the op, then ask `rad_kv_block_size()` (`spec.md` §7.2). `cu_seqlens` is optional and makes the batch **ragged**: `[n_seq+1]` i32, sequence `s` owning query rows `[cu[s], cu[s+1])` of the dense `q` and `out` blocks. Absent, every sequence must present the same query length and `total_q` must divide `n_seq`. Pass it: a mixed step — one sequence's prefill chunk beside other sequences' decode rows, which carry their own verify windows and so are not one row each — is what the scheduler builds whenever prefill and decode coexist, and it does not satisfy the uniform form. `q_len` is then the batch MAXIMUM, which is what selects the band; the per-sequence lengths come from `cu_seqlens` alone. `attn_dense` is the vision tower's: non-causal, nothing paged, `cu_seqlens` bounding one image per entry so a whole batch is one launch. `q`, `k` and `v` may each be `[tokens, heads, head_dim]` or `[tokens, heads * head_dim]`, and the second form may be a column slice of a fused projection read at that projection's row pitch -- which is how a tower's `[q|k|v]` row reaches it without a copy. **`rope`'s multi-component modes.** `mrope`, `imrope` and `axial` rotate with NeoX pairing and let several position components drive different frequencies. `positions` is then `[C, M]`, component-major, and `sections` names how many of the `rotary_dim / 2` frequencies each component owns: `mrope` gives contiguous runs of them to components 0, 1, ... (Qwen2-VL); `imrope` interleaves -- component 1 takes frequency `i` where `i % 3 == 1` and `i < 3 * s1`, component 2 where `i % 3 == 2` and `i < 3 * s2`, component 0 the rest (Qwen3-VL); `axial` gives contiguous runs AND restarts the frequency ladder in each, `theta^(-2j / (2 s_c))` for the run's `j`-th frequency (a vision tower's 2-D rotary over row and column). With `[M]` positions every component is that one position, so `mrope` and `imrope` over a text batch are `neox` exactly. **`gemm_nt_bias`'s epilogue** is a linear layer's: the biased product rounded to `y`'s dtype, then `act` of it (`none`, `gelu` exact, `gelu_tanh`) rounded, then `res` added and rounded -- each step what a separate tensor would hold, so the fused form is the unfused chain to the bit. **`grid_embed`** adds a learned `side x side` position table resampled to each row's grid: `coord` is `[4, M]` (row, column, grid height, grid width), sampled bilinearly with the grid's corners on the table's (align_corners), edge taps clamped, the four taps summed in f32 and rounded to `x`'s dtype before the add. | op | params | operands | |---|---|---| | `attn_paged_gate_quant` | `q_len`(range) `head_dim` `gqa` `block_size` `causal` `window` `q_dtype` `kv_dtype` `scale`:f64/opt `max_ctx`/opt `n_head`/opt `max_seqs`/opt:capacity `group` `act` | `q`, `kv_cache`, `block_table`, `seqused`, `k_descale`?, `v_descale`?, `cu_seqlens`? → `out`; `gate` → `q8`, `q8_scale` | **`attn_paged_gate_quant` is `attn_paged` followed by `gate_quant_fp8` over its output**, the second op finished inside the first's split-KV merge. A merge row is one (token, head) pair of `head_dim` columns, which is exactly a `gate_quant_fp8` row, so the merged values are gated (`bf16(sigmoid(gate))`, rounded before the multiply), rounded, written to `out` and quantised to E4M3 with one scale per 128 columns while they are still in registers. It is byte-for-byte the pair, checked by `r4d_selftest` at both merge forms and over a bf16 and an E4M3 cache. `block_size` is required rather than derived because the cache's page size is `attn_paged`'s to answer; the caller passes the value it read. The gated attention block takes it on the QSA sparse path, where every launch is one query row a sequence and the decode kernel serves every chunk -- the whole step in one launch, which is why `max_seqs` is a capacity: the block declares it at the step size, and the activation arena's smaller declares bound it lower. ### The KV cache is an operand `attn_paged` and `kv_store` take the group's cache as operand 1, passed as `RAD_KV(group, layer)`. `RadKVGroupBatch` carries the slot mapping, the block table and the sequence lengths, but the pool base belongs to the block manager and not to the batch. ### `qk_norm_rope_gate` — device-only | op | params | operands | |---|---|---| | `qk_norm_rope_gate` | `M` `head_dim` `n_head` `n_head_kv` `rot` `q_dtype` `eps`:f64/opt `wadd`:f64/opt `cs_f32`/opt `pos_i64`/opt | `q_gate`?, `k`, `cos_sin`, `positions`, `q_w`w?, `k_w`w → `q_out`?, `k_out`, `gate_out`? | It splits an interleaved `[q|gate]` projection per head, RMS-norms q and k over the head, applies partial NeoX rope and copies the gate — four launches a layer in one. **It is NOT `qk_norm_rope`** in the attention table above, which takes `theta`: this one takes a PRECOMPUTED `cos_sin` table (`rope_table` builds it) and a different operand list, which is why it carries its own name. `rot` is the rotary width, `cs_f32` and `pos_i64` state the widths of the table and the position vector, and `gate_out` is optional — a caller that already reads the gate at its column offset inside the projection passes none. **`n_head` 0 IS THE K-ONLY FORM**, and it is the half of this op that has nothing to do with gating. The `k` side is the plain reader — one head per `head_dim` columns, taken at the OPERAND's own row pitch, written contiguously to `k_out` — and the only thing the q side adds is the `[q|gate]` split. So an input that is already plain asks for `n_head` 0 and passes no `q_gate`, no `q_w` and no `q_out`. The QSA indexer's query heads are that shape: `heads` of them inside a `heads + 1` projection, which is a stride no rank-2 view can express and would otherwise be `heads` separate `rmsnorm` launches plus a `rope`. At 4 heads over 12 trunk layers and 4 MTP passes that is 80 launches a step for ~7 µs of work each; as one op it is 16. | op | params | operands | |---|---|---| | `qk_norm_rope_gate_kv_store` | `M` `head_dim` `n_head` `n_head_kv` `rot` `q_dtype` `eps`:f64/opt `wadd`:f64/opt `cs_f32`/opt `pos_i64`/opt `block_size` `kv_dtype` `k_scale`:f64/opt `v_scale`:f64/opt | `q_gate`?, `k`, `cos_sin`, `positions`, `q_w`w?, `k_w`w → `q_out`?, `k_out`, `gate_out`?; `v`, `slot_mapping`, `kv_cache`:inout | **`qk_norm_rope_gate_kv_store` is the prologue followed by `kv_store`**, in one launch. A k head's block already holds its token's rotated k one element a lane, so it writes that element and the head's v element into the cache at the token's slot as well. The `k` the store reads is `k_out` and is not an operand of its own, which is why the tail adds only `v`, `slot_mapping` and `kv_cache`; every parameter is one of the two ops' own. It is byte-for-byte the pair at either cache width: the store converts the rotated k exactly as `k_out` stores it, a bf16 cache takes the two values as they are, and the E4M3 conversion rounds each element on its own. **It IS byte-identical to the kernels it replaces**, and the check is in `r4d_selftest` rather than in an argument — two rows, one for the `[q|gate]` form and one for the K-only form on a strided projection. Byte equality and not a tolerance, and the difference between the two is the whole reason: fold the sum of squares over eight consecutive elements a lane where `rmsnorm` folds it over one and the two agree to a ULP, which a RELATIVE bound calls ok — but a ULP in the mean square is a ULP in the normed value, and a rotated element is a difference of two products, so where those nearly cancel the ULP comes out as a few percent and the transcript moves after a few dozen steps. The kernel reproduces the reference's fold exactly: the widths above the head dim contribute zeros and adding 0.0f is exact, so a block sum over 8 wave partials and over 32 are the same number. ## Mixture of experts | op | params | operands | |---|---|---| | `router_topk` | `M`(range) `n_expert` `top_k` `norm` `dtype` | `logits` → `expert_ids`, `expert_w` | | `moe_scatter` | `M`(range) `n_expert` `top_k` `expert_base`/opt `protect`:str/opt | `expert_ids` → `sorted_tok`, `expert_offset`, `expert_count` | | `router_topk_scatter` | `M`(range) `n_expert` `n_local`/opt `top_k` `norm` `dtype` `expert_base`/opt `protect`:str/opt | `logits` → `expert_ids`, `expert_w`, `sorted_tok`, `expert_offset`, `expert_count` | | `router_gemm_topk_scatter` | `M`(range) `K` `n_expert` `n_local`/opt `top_k` `norm` `dtype` `expert_base`/opt `protect`:str/opt | `a`, `b`w, `gate_b`w? → `logits`, `expert_ids`, `expert_w`, `sorted_tok`, `expert_offset`, `expert_count`, `gate`? | | `moe_gemm` | `M`(range) `N` `K` `n_expert` `top_k` `a_order` `dtype` | `a`, `w`wt, `sorted_tok`, `expert_offset` → `y` | | `moe_gather` | `M`(range) `n` `top_k` `dtype` `act`:str/opt | `y_expert`, `expert_w`, `sorted_tok`, `shared`?, `shared_gate`? → `y` | | `row_topk` | `M`(range) `N` `R` `vocab_off`/opt `dtype` | `x` → `idx`, `val`, `pairs`? | | `row_topk_merge` | `M` `R` `world_size` `dtype` | `gathered` → `idx`, `val` | | `logit_rerank` | `M`(range) `R` `n_vocab` `n_embd` `vocab_off`/opt `dtype` | `x`, `head`w, `idx_in` → `idx`, `val`, `pairs`? | `row_topk` and `row_topk_merge` are a pair, and the second only means anything with the first's optional half. A vocab-sharded head produces logits for its own slice and nothing else, so its top-R is over that slice: `vocab_off` shifts the columns reported into GLOBAL ids, and `pairs` writes the same R entries as `[M, R, 2]` (an int32 id in the first float slot, its value in the second) -- the plane `core/sample/sampler.h` holds as `buf_pairs_`, and the one the sampler's own vocab-parallel path gathers. An `all_gather` with `row = R*2` concatenates those into `[M, world_size * R, 2]`, rank r's at `[r*R, (r+1)*R)`, and `row_topk_merge` reduces it to the global top-R. **The merge is exact, not approximate**: a candidate in the global top-R is in its own shard's top-R, and both stages break ties on the lower id, so the result equals a top-R over the unsharded plane element for element. A slot a shard could not fill carries id -1 in `pairs` and is dropped -- a sentinel rather than a -inf value, because a masked logit takes that value legitimately. `logit_rerank` is the second half of a COARSE top-R. A speculative step can afford a cheap draft head -- the target verifies every proposal, so a worse draft costs acceptance and never a token -- but a head quantised hard enough to be cheap also picks a different top-1 often enough to matter. So the coarse head proposes R and this op scores those R, and only those R, against the full-precision head the trunk already carries: a few hundred KB a row against a few hundred MB, for an answer that agrees with the exact argmax on almost every row. `idx_in` carries GLOBAL ids as `row_topk` reports them and `vocab_off` is this rank's first column, so under vocab-parallel drafting each rank rescores the candidates it owns and the existing `all_gather` + `row_topk_merge` still finds the global winner -- it needs no collective of its own. Ties break on the LOWER id, the rule the other two use, so a tie inside a shard and a tie between shards resolve alike. **`router_topk_scatter` is those two in one launch**, and it exists because neither of them is work. At a decode shape each is a SINGLE 256-thread workgroup ranking about a kilobyte — the selection is a wave a row and four rows is one workgroup; the sort is one workgroup by construction — so each costs the one-workgroup dispatch floor and almost nothing else. At 52 of each per step that is ~1.0 ms, 3.6% of the decode step, for two dispatches. The chain is closed: the sort reads `expert_ids` and nothing else, and `expert_ids` is exactly what the selection writes, so a launch between them buys only a barrier and `__syncthreads()` is that barrier. **Two expert counts, and they are not the same number.** `n_expert` is the ROUTER's — it is replicated and every rank picks from every expert — while `n_local` is THIS RANK's slice, which is what the sort runs over and what sizes `expert_offset` and `expert_count` — half of the router's on Qwen3.8-Flash-Next when its experts are divided between two ranks, as a calibration run divides them. `n_local` absent means the slice is the router, which is every deployment that gives each rank a slice of every expert instead. **Bounded, and it refuses rather than falling back.** Only `moe_scatter`'s single-workgroup form is fusible (`M * top_k <= 1024`); above it the sort is three launches, which exist to keep it stable without an atomic deciding the order. That bound is the decode band by construction — eight sequences at depth 3 and top_k 10 is 320 slots — and a prefill chunk is past it, which is also where the pair is amortised over real work and a fusion would buy nothing. A caller past the bound gets a refusal, not a silent change of cost. **`router_gemm_topk_scatter` puts the router's GEMM in front of that pair**, in the same launch: `gemm_nt` of `a` [M, K] against the router `b` [n_expert, K] into `logits`, then exactly `router_topk_scatter` over those logits. With `gate_b` [1, K] and `gate` [M, 1] present it is also the shared expert's one-column `gemm_nt` over the same `a`; the two come together or not at all. The selection needs every logit of a row, so it runs in the workgroup that finishes the GEMM last (an arrival counter, self-resetting, one a stream and logits buffer). It is byte-identical to the launches it stands in for: an element of the skinny bf16 GEMM depends only on its split-K count, which this op takes from the same law at the same N, and the selection and sort are the same code. Decode only, `M <= 64` -- past that the router is the tiled GEMM -- and bounded like the pair. The logits stay an output. `moe_scatter`'s output is what `rad_route_report` hands the heat engine. The histogram is read back asynchronously and consumed on the next step; nothing here blocks the compute stream (`spec.md` §5.5). **`protect` sorts a few experts last.** A layer that keeps some experts at a higher precision than the rest serves them with a second grouped GEMM, and a grouped GEMM takes one contiguous run of the offsets. So `protect` lists local experts (ascending, at most eight) that the sort places after every other expert, in the order listed, with the rest closed up: `expert_offset` and `expert_count` are then in that order, the regular experts are `[0, n - p)` of the offsets and the protected ones `[n - p, n)`. A `moe_gemm` handed the trailing run (offsets that begin above zero) serves those rows and leaves the ones below untouched. **`moe_gather` folds the shared expert in when `shared` and `shared_gate` are given**, computing `y = bf16(shared * act(shared_gate)) + bf16(gathered)` in one pass -- which is `scale_rows`' own fused gate-and-add form, taken one op further back so the bf16 plane between the two never exists. BOTH NARROWS ARE PART OF THE CONTRACT: the unfused pair writes the gather to a bf16 buffer and narrows the product before the sum, so an implementation that carried either in f32 would move the last bit of every channel. `act` is "sigmoid" or "none" and applies to the SCALAR, the same spelling `scale_rows` uses. It is worth a pair of operands because a shared arm that cannot be width-sharded runs on ONE RANK, so the op it replaces is a dispatch a layer that every other rank waits out inside the block's all-reduce. ### The quantised and gated arms | op | params | operands | |---|---|---| | `moe_gemm_q` | `M`(range) `N` `K` `n_expert` `top_k` `group` `a_order` `dtype` `N_odd`/opt `K_odd`/opt `parts`/opt | `a`, `a_scale`?, `w`wt, `w_scale`wt, `sorted_tok`, `expert_offset`, `w_odd`wt, `w_odd_scale`wt → `y` | | `moe_gemm_q_mod4` | `M`(range) `N` `K` `n_expert` `top_k` `group` `a_order` `dtype` `N_1`/opt `K_1`/opt `N_2`/opt `K_2`/opt `N_3`/opt `K_3`/opt `parts`/opt | `a`, `a_scale`?, `w`wt, `w_scale`wt, `sorted_tok`, `expert_offset`, `w_1`wt, `w_1_scale`wt, `w_2`wt, `w_2_scale`wt, `w_3`wt, `w_3_scale`wt → `y` | | `scale_rows` | `M`(range) `n` `act` `dtype` | `x`, `s`, `add`? → `y` | The `wt` suffix is the table form and `w` alone is an ordinary weight operand; `moe_gemm`'s row further up carries the same suffix, because its weight list is a per-expert pointer table. The arithmetic is unchanged by that — a kernel may also be handed the stacked reading, an ordinary rank-3 tensor with a real leading stride, and computes the same function from it. **`moe_gemm_q`'s experts are two tables by parity.** `w`/`w_scale` hold the even-numbered experts at `[N, K]` and `w_odd`/`w_odd_scale` the odd ones at `[N_odd, K_odd]` (defaulting to `N`, `K`); expert `e` is entry `e / 2` of its class. Every entry of one table shares a geometry, and a rank that holds an uneven slice of every expert's width — three 128-column blocks of one parity's experts and two of the other's, which is how a five-block expert splits across two ranks evenly on average — has two. `parts` stacked parts run along both the weight rows and the output columns (two for a fused gate/up); the output is `max(N, N_odd)` wide, and a narrower class fills the leading columns of each output part and writes zeros after them, so the gated quantiser behind it reads one layout for every row. Under the equal-division rule above the op's weight list is the even class's codes, its scales, then the odd class's codes and scales. **`moe_gemm_q_mod4` is the same GEMM over four classes, by the expert's number mod 4.** `w`/`w_scale` hold class 0 at `[N, K]` and `w_q`/`w_q_scale` class `q` at `[N_q, K_q]` (defaulting to `N`, `K`); expert `e` is entry `e / 4` of class `e % 4`, so class `q` holds `ceil((n_expert - q) / 4)` of them. Four ranks splitting a five-block expert cannot share the extra block evenly over two classes -- whichever two ranks take it carry three blocks of every even/odd pair against the other two's two -- and over four they can: rank `r` takes two blocks of class `r` and one of each other, five of every four experts on every rank. It is a separate op and not a parameter on `moe_gemm_q` because the declaration divides a table op's weights over its table operands by count alone: the operand list is the class count. Everything else -- `parts`, the zero fill of a narrower class, `a_order` -- is `moe_gemm_q`'s, and the weight list is class 0's codes and scales, then class 1's, 2's and 3's. ### `a_order`: which rows a grouped GEMM's activation has A routed block runs two `moe_gemm_q` ops and **they disagree about what a row of `a` is**, which is why this is a parameter and not a convention. - The **gate_up** projection reads the block's normed input: one row per **token**. A sorted row `i` belongs to the pair `sorted_tok[i] = token * top_k + slot`, so it must gather `a[sorted_tok[i] / top_k]`. That is `a_order = "token"`, the default. - The **down** projection reads gate_up's output through the gated quantiser, which is already one row per **(token, slot)** in the scatter's own order. Row `i` is row `i`. That is `a_order = "sorted"`. `a_scale` has a row for every row `a` has, either way. **Why it is said and not derived.** The row count gives it away — `M` against `M * top_k` — and deriving it is the tempting shortcut. It is also the shortcut no harness catches: a kernel and a reference that *both* gather agree to 1e-4 on every case, and `rad-kbench` builds each operand from the op's own shape hook, so a hook describing `a` at the extent the gather wants builds a case that is self-consistent and wrong. Served, the down projection hands every slot of a token the *first* slot's intermediate — at decode, where a step carries one token, all `top_k` experts read slot 0 — and the model runs, and answers fluently, and gets facts wrong. An operand whose meaning changes with its extent cannot be checked by a harness that sizes the operand from the same reading. **`moe_gemm_q` is a separate op and not a `dtype` on `moe_gemm`**, for the same reason `gemm_nt_q` is separate from `gemm_nt`: the operand LIST differs, operands are positional, and an op whose count depends on a string means different things to different kernels. The group sub-sum is accumulated from the raw codes and scaled once per group, exactly as `gemm_nt_q` does — the two references have to agree in the last bits or `rad-kbench` blames a kernel for a choice made in `libref`. **`moe_gemm` itself is what a model served at bf16 runs**, and libr4d serves it at `dtype` bf16: one weight table of the checkpoint's own row-major `[N, K]` planes, read with no layout hook, so a container stores an expert as the safetensors file holds it. It has no parity classes and no `parts` — those exist because a quantised expert's width must fall on its scale blocks, and an unscaled plane has none — and its rows go through the same run grouping and the same three forms as `moe_gemm_q`'s kernels, byte-identical to each other. **`scale_rows` is the per-token scalar gate**, and it exists because `mul`'s broadcast is the wrong one. `mul` lets `b` carry a single ROW broadcast down the rows; a shared-expert gate is a single COLUMN broadcast across them — `y[m, i] = x[m, i] * act(s[m])` — and adding a second broadcast rule to `mul` would make the two ambiguous exactly when `rows == n`. `act` is `"sigmoid"` or `"none"`. Qwen3.5-MoE's `shared_expert_gate` is a `[1, n_embd]` projection whose one output per token gates the shared expert's contribution, and this is that multiply. ## Linear attention (gated delta net) | op | params | operands | |---|---|---| | `gdn_conv_prep` | `M`(range) `head_k` `head_v` `chunk`/der `conv_width` `l2_eps`:f64/opt `softplus_thr`:f64/opt | `x`, `w`w, `b`w?, `A_log`w, `dt_bias`w, `conv_state`:inout, `cu`, `a`?, `b_gate`?, `cache_idx`?, `has_init`? → `q`,`k`,`v`,`g`,`beta` | | `gdn_conv_update` | `q_len`(range) `head_k` `head_v` `conv_width` `max_query_len`/opt | `x`, `w`w, `b`w?, `conv_state`:inout, `state_idx`, `num_accepted`, `cu` → `q`,`k`,`v` | | `gdn_kkt_solve` | `M`(range) `head_k` `chunk`/der | `k`, `beta`, `g`, `cu` → `A` | | `gdn_chunk_scan` | `M`(range) `head_k` `head_v` `chunk`/der `scale`:f64/opt `state_fp16`/opt | `q`,`k`,`v`,`A`,`g`,`beta`,`h0`?,`cu` → `o`, `ht`?; `state_idx`? | | `gdn_recurrent_update` | `q_len`(range) `head_k` `head_v` `n_head_v`/opt `scale`:f64/opt `eps`:f64/opt `l2_eps`:f64/opt `softplus_thr`:f64/opt `act`:str/opt `state_fp16`/opt `state_form`:str/opt | `q`,`k`,`v`,`a`,`b`,`A_log`w,`dt_bias`w,`state`:inout,`state_idx`,`num_accepted`,`cu`,`z`?,`norm_w`w? → `o`,`o_q`?,`o_scale`? | | `gdn_conv_recurrent_update` | `q_len`(range) `head_k` `head_v` `conv_width` `n_head_v`/opt `scale`:f64/opt `eps`:f64/opt `l2_eps`:f64/opt `softplus_thr`:f64/opt `act`:str/opt `state_fp16`/opt `state_form`:str/opt | `x`, `conv_w`w, `conv_b`w?, `conv_state`:inout, `conv_idx`, `a`,`b`,`A_log`w,`dt_bias`w,`state`:inout,`state_idx`,`num_accepted`,`cu`,`z`,`norm_w`w → `o`,`o_q`?,`o_scale`? | | `gdn_gated_rmsnorm` | `M`(range) `channels` `eps`:f64 `act`:str | `x`, `z`, `w`w → `o` | **A linear-attention kernel pair must support speculative rollback.** The recurrent state and the conv window have already absorbed rejected tokens, so `num_accepted` is a required operand and the state cache is a rolling window of `conv_width-1 + n_spec` entries read at the slot the last accepted token left. A rejection is a change of read offset, not a recompute. This is part of the schema, not an assumption (`spec.md` §10). **`num_accepted` is ONE-BASED.** It counts the tokens the previous step committed: the one it was given, plus however many of its drafts survived. So 1 is an ordinary decode step that drafted nothing, which is exactly what these kernels assume when the operand is absent, and 0 is not a value either of them can read -- `gdn_conv_update` reads its window at `num_accepted - 1`. **`state_form` says what the state cache MEANS, and there are two answers.** `per_candidate` -- the default, and what this table's operand description above says -- writes one full state per candidate token and needs `1 + n_spec` distinct slots in `state_idx`. `anchor` keeps ONE committed state a sequence and replays the accepted prefix onto it out of a scratch slot, which is two slots whatever the draft depth. The two produce the SAME `o` from a COMPLETELY DIFFERENT `state`, so an implementation that serves one and is handed the other computes a correct answer to the other question and disagrees with a checker on every element of the cache while agreeing on every element of the output. That is why it is declared and not inferred: the index row's width does not say which, and neither does anything else a kernel can see. A caller declares it; an implementation that serves only one form REFUSES the other by name. libref serves `per_candidate`. The engine picks the form from whether the deployment speculates, which is the same condition that sizes the state pool. It is also REQUIRED rather than optional, which is one of the two places the union rule is deliberately not applied: an op that lets the caller omit `num_accepted` is an op whose rollback contract is optional. A non-speculative decode passes all ones. **`state_idx` is `[n_seq, W]`, and W is the group's, not the op's.** One column is the sequence's state. A kernel that needs somewhere to put what it cannot commit yet may ask for more, and the columns after the first are ITS scratch: the caller stages the row, zeroes every slot it names when the sequence is admitted, zeroes the scratch again after any step that ran a different path over the same sequence (a chunked prefill commits the state directly and writes no scratch), and otherwise does not look inside. `gdn_chunk_scan` takes the same row and reads column 0. The point of that freedom is what libr4d does with it. One state per candidate token is the plain reading of "the next step reads the slot the last accepted token left", and on Qwen3.8-27B it is 1.3 GiB a sequence. Instead libr4d keeps ONE committed state and, beside it, the few hundred floats a head that each token's rank-1 update needs; the next step replays the accepted prefix onto the state out of that block and starts from a state carrying exactly the tokens that survived. Two slots a sequence instead of `1 + n_spec`, and less write traffic than the per-candidate form. Two consequences worth naming. A zeroed scratch block replays as the identity, which is what makes "zero it and pass `num_accepted` 1" the correct handover out of a prefill. And **libref is not a meaningful oracle for `gdn_recurrent_update` while that layout is in use** -- ref implements the per-candidate reading, so it would decode another plugin's scratch as its own. The end-to-end check that does hold is stronger anyway: greedy speculation must emit the same bytes as greedy without it. **Every gdn kernel takes `cu`, decode included, and that is the other place the union rule is not applied.** `gdn_conv_update` and `gdn_recurrent_update` run off the one cumulative-lengths tensor on the one step, and both open by reading `cu[n]` and `cu[n+1]` before any null test — the number of sequences is `cu`'s length minus one and nothing else in the argument list carries it. A caller working from a schema that made `cu` optional passes a null and faults on the dereference rather than being refused. **`gdn_conv_recurrent_update` is the decode pair as one launch**: `gdn_conv_update` followed by `gdn_recurrent_update`, with the convolution computed where the recurrence reads it, so `q`, `k` and `v` are never written. The operands are the convolution's inputs -- `conv_idx` is its `state_idx` -- and then the recurrence's without those three; the parameters are the union of the two ops' under their own names, and both ops' rollback and state contracts hold unchanged. `z` and `norm_w` are required, because the fold serves the decode shape, where the fused row norm already puts one workgroup on each (sequence, value head). It is byte-identical to the pair. libr4d rewrites a key head's conv state once all the value heads reading it have read it, by an arrival count, so nothing waits on another workgroup. **The output gate `z` may be a COLUMN SLICE, on both `gdn_recurrent_update` and `gdn_gated_rmsnorm`.** The fp8 delta-net block projects `[q|k|v|z]` in one GEMM (`arch/common/rad_block_gdn_fp8.h`), so the gate is the right-hand `n_head_v * head_v` columns of a wider row and its `(token, head)` rows are NOT evenly spaced: heads are `head_v` apart inside a token and the next token is the slice's own stride away. An implementation that reads one pitch off `z` walks into the next token after `n_head_v` rows and is wrong without being out of bounds. The three spellings a caller may hand over are `[tokens, heads, head_v]`, a flat `[tokens, heads*head_v]` buffer of its own, and that slice; the first two are the same arithmetic and the third is what needs the token stride carried separately. ## Collectives | op | params | operands | |---|---|---| | `all_reduce` | `world_size` `numel`(range) `dtype` `exact` `min_bytes`/opt `hops`/opt | `x`:inout, `y`:out? | | `all_gather` | `world_size` `numel` `dtype` `row`/opt | `x` → `y` | `exact` separates the exact sum from the quantised wire, and a caller has to say which it wants: a request that does not declare it will accept a lossy wire gets no kernel rather than the quantised one. That is libr4d's rule and it is right. libr4d separates one-shot from two-shot, at a width that has both, by message size: its two-shot rows start above 524288 elements, which makes that a band edge of the ranged `numel`, so a caller states nothing for it. `hops` is a caller's request for one or the other (1 or 2); libref accepts and ignores it, and no libr4d row reads it. Every all-reduce kernel serves `y` absent, in place. libr4d serves these at **2, 3, 4 and 8 ranks**. Two ranks have their own kernels (one flag word a block, the rotated 6-bit wire, the fused norm-and-reduce forms). The wide family is one template on the world size; 4 and 8 have both hops, 3 has the one-shot only -- the two-shot shards the message `world` ways and a residual width such as 2560 does not divide by 3 -- and `all_gather` at width rides the same instance as the wide `all_reduce` (`ar_gather_3rank`, `_4rank`, `_8rank`). The fused forms are declared only at two ranks by the arch blocks, which fall back to the plain pair at any other width. `min_bytes` is the byte floor under which a LOSSY row must serve the message exactly anyway, and it is a parameter rather than a constant in the library because it is a property of the link and not of the kernel. Compression only pays once the transfer is bandwidth-bound: a decode step all-reduces a handful of residual rows and the exact kernel already moves that at most of what the interconnect delivers, so the rotate, the pack and the unpack cost more than the bytes they remove. A row selected for `exact=0` therefore decides the ROW once, at declare, and the WIRE per launch. Absent, the plugin uses its own measured default. `all_gather.row` is one row of a rank's contribution, in elements. Absent or `0`, `y` is the plain concatenation — rank `r`'s slice at `r * numel`. Present, the placement is row-interleaved: `x` is `[rows][row]` and `y` is `[rows][world_size][row]`, which is what a vocab-parallel candidate merge reads, because it takes one row of gathered candidates per sampled position at a single row stride. **`-ffp-contract=off` is mandatory for any quantised cross-rank op.** Two ranks see the two dequantised products in the opposite order, and contracting either into an FMA makes them disagree by ~1 ULP, which breaks the replicated-state invariant. The build sets it globally for device code. ### The collectives that carry a norm — device-only | op | params | operands | |---|---|---| | `ar_rmsnorm_quant_fp8` | `M` `n` `eps`:f64 `group` `world_size` `dtype` `wadd`:f64/opt `wire`/opt | `x`:inout, `residual`:inout?, `w`w → `q`, `scale`, `out_bf16`? | | `ar_hc_write` | `M`(range) `n` `hc` `world_size` `dtype` `wire`/opt | `y`:inout, `inj`, `h`:inout | | `ar_gather_hc_write` | `M`(range) `n` `hc` `top_k` `world_size` `dtype` `act`/opt `wire`/opt | `y_expert`, `expert_w`, `sorted_tok`, `shared`?, `shared_gate`?, `y`:inout, `inj`, `h`:inout | | `ar_ln_had_quant_i8` | `M` `n` `eps`:f64 `group` `world_size` `dtype` `wadd`:f64/opt `wire`/opt | `x`, `residual`:inout?, `w`w → `q`, `scale`, `out_bf16`? | | `gdn_gated_norm_had_quant_i8` | `M` `n` `head_dim` `group` `eps`:f64 `dtype` `zgrp`/opt `zblk`/opt | `x`, `z`, `w`w → `q`, `scale` | `wire` is the payload width on the link: 0 is exact bf16, 6 the rotated six-bit form. It is a property of the LINK and not of the kernel, which is why it is a parameter — see `all_reduce`'s `min_bytes` for the same argument. `x` is INOUT on `ar_rmsnorm_quant_fp8`: it is both the message and the input, and the reduced sum lands in it. `ar_hc_write` is `all_reduce` of `y` followed by `hc_write` of the reduced `y`, for a block whose output feeds a gated residual rather than a norm. `y` is INOUT for the same reason `x` is above. It serves both wires, one op a wire: a caller declares the rotated form beside the exact one and issues whichever the step's message goes out on, and a `wire` of 6 is 6 at every size. At four ranks only the exact form exists. A caller states `wire` 0 on the exact form rather than leaving it out: a row that serves one wire only says so with a constraint on `wire`, and a constraint on an absent key does not hold. `ar_gather_hc_write` is `moe_gather` followed by `ar_hc_write`: the gather's output IS the message, so `y` is written by the gather half before it is reduced in place, and the five gather operands and `act` mean exactly what they mean on `moe_gather`. `wire` is `ar_hc_write`'s. On either wire, byte-identical to the three kernels it stands in for. At four ranks only the exact form exists, as for `ar_hc_write`. ## Vocabulary edges | op | params | operands | |---|---|---| | `embed_lookup` | `M`(range) `n_embd` `n_vocab` `dtype` `vocab_offset`/opt `wscale`:f64/opt | `tokens`, `wte`w → `x` | | `embed_lookup_q` | `M`(range) `n_embd` `n_vocab` `dtype` `vocab_offset`/opt `wscale`:f64/opt | `tokens`, `wte`w, `scale`w? → `x`, `ahead`? | | `logits_gemm` | `M`(range) `n_vocab` `n_embd` `dtype` | `x`, `lm_head`w → `logits` | | `ngram_ids` | `M`(range) `heads` `ngram` `eos` | `tok`, `state`:inout, `cu`, `mult`w, `vocab_sizes`w, `offsets`w, `cache_idx`?, `has_init`?, `num_accepted`? → `ids`, `ahead_ids`? | | `ple_gate` | `M`(range) `n` `hc` `eps`:f64 `dtype` `wadd`:f64/opt `gate_eps`:f64/opt | `k`, `q`, `v`, `w_key`w, `w_query`w, `w_conv`w → `gv`, `gvn` | | `ple_conv` | `M`(range) `n` `width` `dilation` `dtype` | `x`, `w`w, `conv_state`:inout, `cu`, `resid`?, `cache_idx`?, `has_init`?, `num_accepted`? → `y` | Separate from `gemm_nt` because `lm_head` is its own access class (`RAD_ACCESS_VOCAB`) and because the sampler wants to fuse against it: at a 151K vocab and batch 256 the logits are ~77 MiB a step, and the distributed top-k means a rank never materialises a full row (`spec.md` §9, §13). ### `gather_rows` | op | params | operands | |---|---|---| | `gather_rows` | `M`(range) `n` `dtype` | `x`, `idx` → `y` | `gather_rows` is not a convenience. Without it a plugin computes logits for **every** token in a prefill chunk instead of the last one per sequence — `n_tok / n_seq` times too much work, and at `max_tok` 8192 against a 248K vocab the logits buffer alone is 8 GiB. It is the op that turns "correct" into "affordable". ### `scatter_rows` | op | params | operands | |---|---|---| | `scatter_rows` | `M`(range) `n` `dtype` | `v`, `idx`, `x`:inout | `x[idx[i], :] = v[i, :]`, the mirror of `gather_rows`, and `x` is **inout** because the write is partial: rows no index names keep what they held. That is what makes it usable against a pool other sequences are also in — an op that returned a fresh `x` would have to copy everything it was not asked to change. Two rules differ from the gather, and both follow from the destination being live memory rather than a fresh buffer: - **A negative index writes nothing.** `gather_rows` reads a zero row for one, which is the padding convention a short prefill chunk leaves behind. On this side a zero fill would be data loss. - **Indices must be distinct**, declared with `RAD_OPD_F_IDX_UNIQUE` rather than left to prose. Two rows landing on one destination has no defined winner on a parallel machine, so permitting it would make this an op whose reference implementation and whose real kernel are allowed to disagree — which is exactly what the oracle exists to rule out. It exists for the same reason `gather_rows` does: the alternative is `M` separate transfers, and where this op is used a row is a few hundred bytes, so that is dispatch cost with the data as a rounding error. The caller is the KV tier traffic (`core/mem/kvtier.h`). A paged pool is **layer-major**, so within one layer a group's blocks are dense rows of `layer_bytes_per_block()` — which makes packing an arbitrary set of them exactly a gather and restoring them exactly a scatter, one launch per (group, layer) however many blocks are moving. **At `dtype` u8 the rows are opaque bytes**, and libr4d supplies that width as its own kernel pair rather than widening the bf16 one. What moves is whatever the attention kernel wrote; calling it bf16 would matter, because `libref` is the oracle for every op here and its bf16 path loads through float, which is not guaranteed to return a NaN payload unchanged. At u8 every value is 0..255, float carries it exactly, and oracle and kernel agree bit for bit on any payload at all. ### `embed_lookup`'s `vocab_offset` `vocab_offset` (optional, default 0) is the first vocabulary row this rank holds. With it, `n_vocab` is THIS RANK'S row count -- the meaning it already carries for `logits_gemm` -- and the table covers global ids `[vocab_offset, vocab_offset + n_vocab)`. **Token ids stay GLOBAL**: a rank subtracts the offset and writes a zero row for an id belonging to another rank, exactly as it does for the negative padding id, so summing the ranks reconstructs `x`. Every token lands on exactly one rank, which is why the `all_reduce` that follows is declared `exact` rather than taking the model's wire setting: it sums disjoint rows, so a lossy wire would be loss on a value that is already the answer. Without it the embedding must be REPLICATED while `lm_head` is row-sharded, and asymmetric sharding of the two ends of the same vocabulary is not a design, it is an omission. The table is bf16 even in the fp8 checkpoint, so replicating it costs 248320 x 5120 x 2 = 2.43 GiB on every card -- memory the split hands to the KV cache instead. One thing is given up and it is worth naming: under a split this kernel can no longer tell a garbage id from another rank's token, so the "an id at or above `n_vocab` fails the launch" check holds only where the rank holds the WHOLE vocabulary -- `world_size` 1, not merely `vocab_offset` 0. **Rank 0 of a vocab-parallel split has offset 0** and legitimately sees ids past its own slice on most steps, so a check keyed on the offset alone refuses correct calls. Bounding ids is the caller's, and the tokeniser and the sampler both already are. ### `embed_lookup_q`, the n-gram gather **A gather from a table quantised with ONE scale for all of it**: `x[m] = wte[id] * scale[0]`. Padding and bounds behave exactly as `embed_lookup`'s — a negative id writes a zero row, an id past the table fails the launch in the host kernels and writes a zero row in the device one, which cannot read its ids before it runs (libr4d's divergence note). **Why one scale is enough**, measured on the bf16 original: E4M3 carries its own exponent, a row's dynamic range in this table is only 2.3×, and the scale sits on a **three-octave plateau** where 2⁻¹³ through 2⁻¹⁰ all give rel_l2 0.02662 to five digits. A per-row scale buys 2.9%. Qwen3.8-Flash-Next-FP8 ships exactly this: 128 E4M3 shards and a single bf16 `weight_scale` of 1.9932e-4. **A separate op and not a `dtype` on `embed_lookup`.** The operand list differs — there is a scale — and operands are positional, so an op whose count depends on a string means different things to different kernels. It is `gemm_nt_q`'s argument against being a `dtype` on `gemm_nt`. Adding the scale to `embed_lookup` as an optional operand would also have moved `x` from slot 2 to slot 3 for every caller in the tree. **Where it runs is where `--ngram-placement` puts the table**, because the planner runs an op on the host when **every weight operand of it** is `Site::Host` and takes that from the table's placement. The table is 51.2 GB and is never staged a unit at a time — moving a whole unit to use 5 KB is the wrong shape — so it is read where it lies: | placement | the table | the gather | |---|---|---| | `disk` | the container's mapping | libavx's host kernel | | `ram`, libr4d loaded | a pinned copy every card maps at its host address | libr4d's device kernel, over the link | | `ram`, no device kernel | a locked copy (`core/format/table_copy.h`) | libavx's host kernel | | `vram` | the card, every rank holding all of it | libr4d's device kernel | `hipHostRegister` on the container's mapping is not one of them: it would force all of the file resident. **The host gather keeps its own row cache.** The container is mmapped, so the table is at a host address on open. libavx keeps the rows a gather has touched in a row cache of its own and reads a miss straight from the file with O_DIRECT (`libavx/avx_ngram.cpp`): the page cache would map a 4 KiB page per 160-byte row. The reads keep the alignment the filesystem reports for direct I/O, or its block size where it reports none, as btrfs does; one read of the table proves it before the first row is served. A filesystem with no direct I/O at all cannot hold a container with this table on `disk`. **The device gather is libr4d's `embed_lookup_q_bf16`**: four entries a thread, each E4M3 code decoded exactly, multiplied by the scale in f32 and rounded to nearest even — the value libavx's lookup table holds, to the bit. It reads the ids and writes `e` in the host arena, where the architecture declares them for the host gather, so the step has no host op left in it. The table has to be a weight operand at all for the host site to be reachable: `gather_rows` takes a plain `IN` operand and would never be placed there. And the host kernel has to live in a production plugin — libavx, the host kernel library — because `rad_gate_reference_kernels` refuses to serve on libref by design. ### `ngram_ids`, and the gather that is not an op **Qwen4-Exp's Per-Layer Embedding gives one layer a second embedding table addressed by a HASH of the last `ngram` tokens.** Qwen3.8-Flash-Next's is 320,001,536 rows of 160 — 102 GB, 51 of the model's 180 billion parameters — read sixteen rows a token. `ngram_ids` computes those row ids: mixed(k) = t0*mult[0] ^ t1*mult[1] ^ ... ^ tk*mult[k] ids[h] = remainder(mixed(b+1), vocab_sizes[h]) + offsets[h] where `heads` is `ngram - 1` equal blocks and block `b` uses the `(b+2)`-gram. `t_s` is the token `s` positions back, **or the EOS token if the shift would cross one** — and once a shift falls back to EOS every deeper shift does too. `mult`, `vocab_sizes` and `offsets` are weights because the checkpoint carries them; the reference builds them from a seed and a prime search at construction time, and re-deriving sixteen numbers the container already holds would be a second source for them. **`state` is a rolling window of committed ids**, `ngram - 1 + n_spec` deep, read at `num_accepted - 1` and rewritten in the layout `ple_conv` describes below — operand for operand, including the two layouts `num_accepted`'s presence chooses between. **A cold window is EOS and not zero**, which is the one place it differs from a convolution window: zero is a real token id. **The ids are a STATE and not a plain input**, and the reason is that the contract is already paid for. A state needs a rollback contract and an input does not, which reads like an argument for the input — but the dilated convolution downstream carries a 9-timestep window with exactly this contract, so PLE pays for `num_accepted`, `cache_idx` and `has_init` regardless, and against that a plain input costs *more*. Nothing in the engine derives "the ids before this step": `DeriveInput` carries positions and cu_seqlens and no token ids at all, so an input would mean a new core derivation and a widened `DeriveFn` signature to serve one op. `RAD_KV_CONV` is already "per-sequence, a rolling window of width-1 + num_spec entries", and `conv_state_index` already derives the read cursor for it. **The gather is `embed_lookup_q`, not `gather_rows`.** The table has to be a WEIGHT operand or `Site::Host` is unreachable, because the planner puts an op on the host only when every weight operand of it is `Site::Host`, and `gather_rows` takes a plain `IN` operand. **`ahead_ids` and `ahead`: the next chunk's rows, a step early.** A prefill chunk gathers `heads` rows a token and, with the table on `disk` and text the row cache has not seen, most of them are random reads of the container file — ~30 ms a 2048-token chunk on an NVMe, with the card idle behind the host-site op. The next chunk of the prompt is already known (`RadBatch::n_ahead`, which stores it in `token_ids` after the step's own tokens), so: - `ngram_ids` with `ahead_ids` present reads `tok` that many tokens past the step's, as a continuation of the LAST sequence, and writes their ids there — exactly the ids the step that runs those tokens will compute, since it hashes the same history. The window it writes back is the step's own and never sees them. - `embed_lookup_q` with `ahead` present may start reading those rows once its own are gathered. No output depends on it, so a kernel may ignore it; libavx's row hands them to a reader thread that fills its row cache while the step runs, and the next call finds them there. ### `ple_gate`, and what the convolution branch does not see PLE's mixing coefficient is a **dot product between the n-gram embedding and the residual stream** — one scalar per hyper-connection stream, per token: K[c] = grouped_rmsnorm(k)[c] * (w_key + wadd) k is key_proj(embedding) Q[c] = grouped_rmsnorm(q)[c] * (w_query + wadd) q is the INCOMING hc stream s[c] = sum_d K[c][d] * Q[c][d] / sqrt(n) s[c] = sign(s[c]) * sqrt(max(|s[c]|, gate_eps)) gv[c] = sigmoid(s[c]) * v v is value_proj(embedding), ONE row gvn = grouped_rmsnorm(gv) * (w_conv + wadd) **The signed square root keeps the sign**, so a stream whose embedding opposes the residual gets a gate below a half rather than a gate of zero. The clamp is on the **magnitude** and the sign comes from the **original**, which matters at exactly one value: torch's `sign(0)` is 0, so a dot product of exactly zero comes out as zero and not as `sqrt(gate_eps)`. Clamping first and re-signing the clamped value would give 1e-3 there. **Both outputs leave the op.** The dilated convolution that follows reads the *normed* copy and the residual add reads the *un-normed* one — two different tensors, and recomputing either outside would be a second definition of the norm. **AND THE CONVOLUTION BRANCH DOES NOT SEE THE GATE.** `gv[c]` is a scalar times one shared `v` row, and `norm_conv`'s rms is taken per `n`-wide stream — so the gate appears in both the numerator and the rms and divides straight back out. `gvn[c][d]` is `v[d]/rms(v)` times its gain, whatever the gate did. This is a sharp invariant to check a kernel against: a fused implementation whose `gvn` varies with the gate has folded something it should not have. It is *not* a shortcut to take — `gv` is narrowed to its dtype before the third norm reads it, so computing `gvn` from `v` directly would differ in the last bits. **One op and not six.** Three grouped norms, a reduction, a scalar nonlinearity and a broadcast multiply over tensors that are all `[M, hc*n]` — 10240 wide here — would be six launches against a ~4.5 µs per-kernel floor, every one of them memory-bound. `hc_read` is the same argument at the same width. ### `ple_conv`: a dilated convolution cache is not a conv cache y[t] = resid[t] + silu( sum_j w[c][j] * x[t - (width-1-j)*dilation] ) **The dilation changes how deep the history is, and that is the whole trap.** At Qwen4-Exp's width 4 and dilation 3 the taps are **9, 6, 3 and 0** timesteps back, so a sequence carries `(width-1)*dilation = 9` timesteps — not `width-1 = 3`. A cache sized for the undilated width keeps only the three most recent values and reads zeros where the deepest tap belongs, which is a model that generates fluently from a slightly wrong context. The state is `[n_slots, n, state_len]` with slot `i` holding the value at relative position `i - (width-1)*dilation`, which is `gdn_conv_prep`'s convention with the dilation folded in. `has_init` 0 means that window is **zeros** rather than whatever the slot last held — without it a fresh sequence landing on a recycled slot convolves the previous sequence's tail into its first nine tokens. **`num_accepted` shifts the READ offset, and its presence picks what the step leaves behind.** The manager sizes the state `(width-1)*dilation + n_spec` deep so a speculative step can read at an offset inside it. What the next step's offset must find is the history ending at the last token this step *committed*, and which token that is depends on the kind of step: - **Present — a decode step**, any of whose drafts a verify may reject. The window is rewritten the way `gdn_conv_update` rewrites its own: the history read, shifted down by one, then every input of the step, so slot `i` holds position `i + 1` of (window read ++ step). A next step that kept `k` of these tokens reads at `k - 1` and finds the history ending at the `k`-th. It needs `(width-1)*dilation - 1` slots plus one a token, which a step of `1 + n_spec` tokens gets. - **Absent — every token commits**, which is the prefill chunk. The read is at offset zero and the last `(width-1)*dilation` inputs land at offset zero, where the next step's `num_accepted` of 1 reads them. The two agree when the step has one token, and no single layout can serve both at several: at offset zero one must hold the history ending at the step's first token and the other the history ending at its last. An architecture therefore issues the op once over its decode rows and once over its prefill rows, as it already does the two `gdn` convolutions. **One op where the gated delta net has two.** `gdn_conv_prep` and `gdn_conv_update` are separate because the work *around* their convolutions differs — one splits a fused projection and takes an l2 norm, the other does not. PLE's convolution is the same arithmetic at prefill and at decode, so it takes the union of the two contracts instead: `has_init` is prefill's question and `num_accepted` is decode's. The residual add is folded in because the reference is `gv + silu(conv(gvn))` and the two tensors are both `[M, hc*n]` — 10240 wide here. `resid` absent leaves the plain convolution. ## Sampling Every sampler is a kernel, not a host lift. `llama.cpp`'s chain is kept as a **host reference** that `rad-kbench` compares the device samplers against (`spec.md` §13). **EVERY PER-REQUEST SCALAR IS AN OPERAND, NOT A PARAMETER.** `params` is `[M]` rows of `RadSampleParams` (`abi/rad_sample.h`), uploaded once a step, and it carries `temp`, `top_k`, `top_p`, `min_p`, `typical_p`, the three penalties, the DRY and XTC settings, the seed and position, the history window and the grammar's `mask_row`. Not one of them is in a params cell below, and the reason is that a parameter is GEOMETRY: the selector matches on it and freezes it at declare (`spec.md` §2.2), so two requests in one batch with different `top_p` would be two resolutions of one op — which is not something a batch can contain. | op | params | operands | |---|---|---| | `sample_penalties` | `M`(range) `n_vocab` `vocab_off`/opt | `logits`:inout, `params`, `history` | | `sample_temp` | `M`(range) `n_vocab` | `logits`:inout, `params` | | `sample_topk` | `M`(range) `n_vocab` `vocab_off`/opt | `logits`, `params` → `cand_idx`, `cand_val`, `pairs`? | | `sample_topp` | `M`(range) `n_cand` | `cand_idx`:inout, `cand_val`:inout, `params` | | `sample_minp` | `M`(range) `n_cand` | `cand_idx`:inout, `cand_val`:inout, `params` | | `sample_mask` | `M`(range) `n_vocab` | `logits`:inout, `params`, `bitmask` | | `sample_pick` | `M`(range) `n_cand` | `cand_idx`, `cand_val`, `params` → `token` | | `sample_argmax` | `M`(range) `n_vocab` | `logits`, `params` → `token` | | `sample_merge_topk` | `M`(range) `n_cand` | `gathered`, `params` → `cand_idx`, `cand_val` | `sample_mask` is where a GBNF grammar lands: the state machine stays on host and what crosses is a token bitmask per sequence, 19 KiB at a 151K vocab, cheap enough to upload every step. ### Stages the decomposed chain does not carry | op | params | operands | |---|---|---| | `sample_dry` | `M` `n_vocab` `vocab_off`/opt | `logits`:inout, `params`, `history`, `breakers`? | | `sample_typical` | `M` `n_cand` | `cand_idx`:inout, `cand_val`:inout, `params` | | `sample_xtc` | `M` `n_cand` | `cand_idx`:inout, `cand_val`:inout, `params` | | `sample_chain` | `M` `n_vocab` `temp`:f64/opt `p`:f64/opt `min_p`:f64/opt `k`/opt | `logits`, `query`?, `u` → `draw` | `sample_dry` penalises each token that would extend a repeated suffix of the history ONCE, by its longest such repeat — llama.cpp's DRY. The sequence breakers are strings, so they are resolved against the vocabulary on the host: the row's `dry_rep_limit` carries the repeat limit the most recent breaker imposes, and the optional `breakers` plane, one byte beside every history token, marks the tokens that are whole breakers and are never penalised. Absent, no token is exempt. `sample_penalties` and `sample_dry` read GLOBAL token ids from `history` and write this rank's logits, so under a split vocabulary they take `vocab_off`, as `sample_topk` does, and a token outside the shard is another rank's to penalise. Their `penalty_last_n` / `dry_penalty_last_n` window is llama.cpp's: 0 turns the stage off for the row, and a negative value takes the whole history. `sample_typical` is locally-typical sampling and is NOT a prefix of the descending candidate order — that order and the probability order disagree, which is why it narrows in place rather than truncating. `sample_chain` is the whole chain in one kernel for the speculative verify: `query` is the drafted token per position (absent at the last one), `u` is TWO uniforms a position, and `draw` is `{u32 tok, u32 tok_excl, f32 p_query}` — the rejection test's three values. It stands beside the decomposed stages rather than replacing them; both are declared and the band decides. ### The vocabulary-parallel chain When `lm_head` is row-sharded, a rank's logits row covers only its own slice of the vocabulary, and two things follow. `sample_argmax` becomes unusable — its answer is a LOCAL column index, which is a different token — so a greedy request is staged at `top_k = 1` and takes the chain instead. And `sample_topk` writes an optional third output, `pairs`: its candidates as (u32 GLOBAL id, f32 logit), with the rank's `vocab_off` already added, which is what `all_gather` moves and `sample_merge_topk` reads. `sample_merge_topk` **re-applies the row's `top_k`**, and that is why it takes `params`. Every rank ran its own top-k before the gather, so the merged set holds up to `world_size * k` candidates where the request asked for `k`, and no stage after it narrows again. At `k = 1` the difference is not a slightly wider support: `sample_pick` is a DRAW, so a greedy row with two live candidates returns whichever one the request's seed happens to select. **`gathered` is `world_size` SORTED RUNS, and the merge relies on it.** Each rank's `sample_topk` writes its `n_cand` pairs in descending (logit, lower id first) order with its holes at the tail, and the all-gather lays a row's runs side by side. libr4d's merge therefore runs only the merge stages of its sorting network when the runs are a power of two long and fill the row -- 11 passes over two runs of 1024 where a full sort is 66 -- and the whole network otherwise. A producer that hands it unsorted runs gets a wrong order, not a slow one. ## Sparse attention (QSA) — device-only | op | params | operands | |---|---|---| | `qsa_block_key` | `M`(range) `n` `ratio` `rotary_dim` `pos0`/opt `block_size`/opt `eps`:f64 `theta`:f64 `scale`:f64/opt `wadd`:f64/opt `dtype` `mode`/opt `sections`/opt | `k`, `w`w, `page`?, `pos`? → `out` | | `qsa_score` | `M`(range) `n` `heads` `blocks` `seqs` `dtype` | `q`, `bk`, `bt`, `cu`, `nc` → `score` | | `qsa_select` | `M`(range) `blocks` `topk` `ratio` `seqs` | `score`, `bt`, `nc`, `pos`, `cu` → `sel`, `nsel`, `seqused` | | `qsa_tail_store` | `M`(range) `n` `ratio` `ring` `seqs` `dtype` | `k`, `pos`, `cu`, `state`:inout, `sidx` | | `qsa_work` | `M`(range) `n` `ratio` `ring` `seqs` `work` `dtype` | `k`, `pos`, `cu`, `state`, `sidx`, `bt`, `rope`? → `stage`, `page`, `bpos`, `nc` | Five ops decompose one thing: an indexer that scores blocks of key history against the query and hands `attn_paged` a shorter block table. `docs/QSA.md` is that decomposition; these are the schemas, and `arch/common/rad_qsa.h` issues all five. `qsa_block_key` builds the **compressed key**: one vector per `ratio` tokens of key history, which is what the indexer's block scores are taken against. ``` out[b] = rope( rmsnorm( mean over j