This repository has been archived on 2026-11-24. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
radiance-sycl/docs/OPS.md
T
AshlynandClaude Opus 5.5 833173cc04 gemma4k: Gemma 4's kernels beyond libr4d -- paged attention, pair_argmax, the sandwich joins
A kernel library beside libr4d, gfx12 only, and AHEAD of it for Gemma 4:
`--kernels gemma4k,libr4d,...`. Its own library rather than rows in libr4d: a smaller change,
its own gfx12 gating, and no churn in libr4d's row table; folding it in can follow.

- attn_paged at head_dim 256 and 512, gqa 1/2/4/8, bf16 or fp8 cache, any window, causal or not.
  One scalar kernel serves decode, verify and prefill, with a split-KV path whose merge is one
  wave a row (live pieces found by ballot, four at a time; a piece that saw no key writes only
  its max and sum); a WMMA prefill kernel takes a query of at least 64 rows and 32 a sequence on
  average -- Q and K bf16, P and V f16 (bf16 P measured 0.25% of the V range in error), the
  denominator summed from the rounded P. RADIANCE_G4K_WMMA=0 keeps every launch on the scalar
  kernel.
- pair_argmax, device and host, described as libref describes it so rad-kbench checks both.
- norm_add_norm and join_add_scale, ops of its own (docs/OPS.md, a new subsection): each one
  kernel for a post-norm / residual add / pre-norm chain, rounding where the chain rounds. libref
  has no row for them on purpose -- a reference row would resolve them where gemma4k is not
  loaded and the architecture would never fall back to the chain -- and no row declares
  `replaces`, since rad-kbench assembles a replaced chain from the fusing library's own rows.

WHY FIRST. libr4d on this tree also has rows at h256/gqa2 and h512/gqa8, Gemma 4's own
geometries, written for the dense 31B (which has no drafter) and not measured on the 26B-A4B or
under a DFlash verify; the hierarchy is absolute, so the first library takes them. These rows are
the ones every Gemma 4 number below was measured with. No other row overlaps libr4d's (the h256
gqa4/gqa8 rows take a window only), so putting this library first takes nothing else from it. The
softcap is libr4d's and libavx's `logit_softcap`.

Measured on 4x R9700, gemma-4-26B-A4B-it + z-lab DFlash, TP4: decode attention 3.234 -> 0.104 ms
a step on the full layers and 1.906 -> 0.722 on the sliding ones (the split-KV merge); a
20,489-token prefill 4.01 -> 2.83 s (the WMMA kernel); rad-kbench against libref 176/176. Not yet
run on the cards: the joins.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-12 14:40:04 -05:00

117 KiB
Raw Blame History

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 mult:f64/opt x, ww? → y (no gain operand: a gain of one)
rmsnorm_p M(range) n eps:f64 dtype x → y
rmsnorm_add M(range) n eps:f64 dtype wadd:f64/opt x, residual, ww → 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, ww, mix_downw, mix_upw, inject_ww? → 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, ww, bw? → y
grid_embed M(range) n side dtype x:inout, tablew, 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)
mul_weight M(range) n dtype scalar/opt x, ww → y (w one element or one row)
act_mul M(range) n act dtype a, b → y (act(a)·b, two operands)
seg_mean M(range) n dtype x, cu_seqlens → y (one mean row a segment)
pool_rows M(range) n k mult:f64/opt dtype x → y (one mean row a window of k rows)
relu M(range) n dtype x → y
clamp M(range) n dtype x, low, hiw → y
conv2d M(range) width cin cout kh kw stride pad cu_div/opt valid_stride/opt dtype x, ww, cu_seqlens, valid? → y
conv1d_dw M(range) n k cu_div/opt dtype x, ww, cu_seqlens → y
rel_sinusoid M(range) n max_timescale:f64/opt dtype → y
gelu_mul M(range) n dtype gate_up → y (splits [M,2n] into gelu·up, tanh approx)
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
logit_softcap M(range) n cap:f64 dtype x → y
scale M(range) n dtype x, sw → y
silu M n dtype x → y
fill M(range) n dtype value:f64/opt → 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).

rmsnorm_p is rmsnorm with the gain removed: x * rsqrt(mean(x^2) + eps) and no w at all. Gemma's attention normalises the value with a bare RMS (a weightless norm) while normalising the key with the k_norm gain in the same layer, and there is no checkpoint plane to feed the value through a gain -- rmsnorm would need a phantom all-zero weight to say the same thing.

gelu_mul is silu_mul with the tanh GELU instead of silu -- Gemma's gated FFN is trained with gelu_pytorch_tanh, and gated feed-forwards ask for the tanh form rather than the unary gelu's exact erf one. Both split the fused gate_up with the gate in the FIRST half.

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.

fill has no input: y = value over the extent, value 0 when absent. It is the op a rank issues where it computes nothing and a reader downstream still expects a defined buffer -- an attention-zero rank's block (docs/TP3.md) is one fill of its output wire before the all-reduce. A wire shared across layers cannot be zeroed once a step, and a memset knows neither the row pitch nor the dtype; this does.

mul_weight is a learned gain on an activation: w is a WEIGHT of one element, a scalar for the whole output -- Gemma 4's per-layer layer_scalar on the residual stream -- or of one row, broadcast down the rows (a per-channel layer scale). Its own op rather than a form of mul because an operand is a weight or an activation by its schema, and mul's b is an activation. scalar 1 states the one-element form; kernels read the form off the operand, and the parameter is what lets a shape description (rad-kbench's) build the right weight.

act_mul is silu_mul with its two halves in two operands, so the multiplier can be any tensor -- the two column slices of a fused gate_up, or a gate and a separate per-layer input (EmbeddingGemma 2's per-layer-embedding block multiplies gelu_tanh(gate(x)) by one). act is silu, gelu (exact erf), gelu_tanh or sigmoid (a GLU's gate: a the gate half, b the value half); the activation is rounded to y's dtype before the product, which is what the unfused pair stores.

seg_mean is a sentence embedding's mean pooling over a ragged batch: row s of y is the mean of x's rows [cu[s], cu[s+1]), summed in double and rounded once. M is the step's token count and bands the op; the segment count is cu_seqlens' length less one. An empty segment is a zero row.

pool_rows is a vision pooler over windows its processor laid out consecutively: row r of y is the mean of x's rows [r*k, r*k + k), summed in f32 and rounded to y's dtype, then times mult and rounded again (the unfused pair stores the mean). M is the input row count and a multiple of k.

rmsnorm's mult scales the normed row before its one rounding: a residual branch weighted by a constant -- Gemma 4's audio feed-forward adds half of its output -- with no second pass. For a power of two it is exactly the reference's norm-then-scale, since scaling by one commutes with the rounding.

The audio tower's operators (Gemma 4's USM conformer):

  • relu is max(x, 0) at each operand's own row pitch.
  • clamp is min(max(x, lo), hi) with lo and hi WEIGHTS of one element each -- a clipped linear's input or output range, which the checkpoint states per projection.
  • conv2d convolves over (time, width) with channels innermost: element (t, w, c) of x is at row t, column w * cin + c, and w is PyTorch's [cout, cin, kh, kw]. Each cu_seqlens segment -- rows [cu[s] / cu_div, cu[s+1] / cu_div) -- is convolved on its own with zero padding pad at its edges on both axes, so two clips in one pass never see each other, and its output rows start at cu[s] / (cu_div * stride) (a segment whose start does not divide is refused). With the optional valid, input row t reads as zeros unless valid[t * valid_stride] != 0: the frame mask the subsampler multiplies in before each convolution. f32 sum, one rounding.
  • conv1d_dw is a causal depthwise convolution over time, y[t, c] = sum_j w[c, 0, j] * x[t - (k - 1) + j, c], with the rows before the segment's start reading as zeros.
  • rel_sinusoid writes the sinusoidal relative-position table, row d for distance d: sin(d * inv[i]) over the first half and cos(d * inv[i]) over the second, inv[i] = exp(i * -log(max_timescale) / max(n/2 - 1, 1)), each step in f32 as the reference's torch expressions are, then rounded.

The sandwich-norm joins — no reference row

op params operands
norm_add_norm M(range) n eps:f64 dtype a, w_aw, x:inout, w_yw → y
join_add_scale M(range) n eps:f64 dtype d, w_dw, r, w_rw, w_jw, x:inout, sw

Gemma's sandwich norms, each one kernel in place of a chain of rmsnorm, add and mul_weight. norm_add_norm is t = rmsnorm(a) * w_a; x += t; y = rmsnorm(x) * w_y -- a post-norm, the residual add, the next pre-norm. join_add_scale is x = (x + rmsnorm(rmsnorm(d) * w_d + rmsnorm(r) * w_r) * w_j) * s -- two arms' post-norms, their sum, its norm, the residual add and a one-element layer scalar (Gemma 4's MoE feed-forward). Every value the chain stores is rounded to the activation dtype at the same point, so the two differ from the chain only in the order a sum of squares is added in: with the same order they are bit-identical to it, and against a chain summed in another block width a norm factor can differ in its last bit.

libref has no row for either, on purpose. An architecture asks for a join and declares the chain where it does not resolve (rad_op_resolved), and a reference row would resolve it everywhere -- so a model loaded without the library that serves the join would resolve it to the reference and be refused at serve, instead of running the chain on libr4d or libavx. The gemma4k kernel library declares both (device and host rows); with no oracle, its kernels are held to the chain bit for bit by tests/g4k_emu, and no row declares replaces, because rad-kbench assembles a replaced chain from the fusing library's own rows and gemma4k has none of the chain's ops.

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?, ww → 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: <Hw, Ha> = group * <w, a>, 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?, ww → 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, bw, b_scalew → 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 wqw, wsw → 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, bw → y
gemm_nt_bias M(range) N K dtype act/opt a, bw, biasw, res? → y
gemm_nt_q M(range) N K group dtype a, a_scale?, bw, b_scalew → y; a_sum?, b_refw?
gemm_nt_q_bias M(range) N K group dtype a, a_scale, bw, b_scalew, biasw → 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 window/opt q, k, v, cu_seqlens → out
attn_local_rel M(range) head_dim n_head left right softcap:f64 cu_div/opt dtype q, k, v, relk, pdsw, 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 yarn_factor:f64/opt yarn_low:f64/opt yarn_high:f64/opt yarn_magnitude:f64/opt ladder/opt qkv:inout, positions
rope_table M(range) rot theta:f64 scale:f64 dtype yarn_factor:f64/opt yarn_low:f64/opt yarn_high:f64/opt yarn_magnitude:f64/opt head_dim/opt ladder/opt 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 v_norm/opt sections/opt comps/opt qkv:inout, positions, q_ww, k_ww

attn_local_rel is Gemma 4's audio attention: local, with a relative-position term and a softcap. Query row i of a segment sees the key rows j of the same segment with 0 <= i - j <= left (right must be 0). Per head, qf = q * (head_dim^-0.5 / ln 2) * softplus(pds) -- softplus rounded to pds's dtype, as the reference evaluates it on the parameter -- and logit = qf . k * (ln(1 + e) / ln 2) + qf . relk[i - j], then softcap * tanh(logit / softcap), a softmax over the visible keys and the weighted sum of v, all in f32 and rounded once. relk is [left + 1, n_head * head_dim], row d the projected position for distance d. The reference computes this in 12-query blocks over 24-key windows with a Transformer-XL shift; every key outside the window carries a -1e9 logit there, so the sliding form is the same function.

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 and an encoder's: nothing paged, cu_seqlens bounding one image or one input per entry so a whole batch is one launch. window is attn_paged's rule over a segment's own positions -- causal, key j is visible to query i when i - j < window; non-causal, when |i - j| < window -- and 0 is no window. A bidirectional encoder's sliding layer of radius r (|i - j| <= r) is window = r + 1. 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.

qk_norm_rope's v_norm set to 1 also RMS-norms every v head of the fused row with a UNIT gain and does not rotate it: Gemma 4's value norm, which has no weight to name.

qk_norm_rope's blocks mode rotates each section as its own block of channels: section c is sections[c] pairs over the next 2 * sections[c] channels, paired (start + j, start + sections[c] + j) -- NeoX inside the block -- and turned by position component comps[c] (default c) at theta^(-j / sections[c]). The sections fill rotary_dim exactly and positions is [C, M]. It is Gemma 4's vision rotary: x over the first half of each head and y over the second, each half a complete rotary of its own; mode axial of rope pairs across the halves instead.

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.

ladder names the width the base frequency ladder is measured in. Absent, or rotary, the exponent divides by rotary_dim: a partial rotary is a complete rotation of its own width, and every existing model is bit-for-bit unchanged. head divides by head_dim instead -- the proportional variant, where the rotary_dim / 2 frequencies are a prefix of the full-width table rather than a table rescaled to the rotated width: at head_dim 512, rotary_dim 128, frequency i = 1 wants theta^(-1 / 256), not theta^(-1 / 64). Only the base ladder moves (axial keeps its per-section ladders), and the tail of the head past rotary_dim is untouched either way. rope_table carries a head_dim param for the same reason: its angles must stay rope's to the last operation.

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_ww?, k_ww → 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_ww?, k_ww → 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, bw, gate_bw? → 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, wwt, 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
pair_argmax M(range) n dtype pairs → idx, val?
logit_rerank M(range) R n_vocab n_embd vocab_off/opt dtype x, headw, 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.

pair_argmax is the merge at R = 1, for a vocab-sharded draft head that wants each position's winner as a proposal: idx gets the id of the largest value among a row's n gathered pairs, ties to the lower id, a negative id skipped, -1 (and -inf in val) when nothing is valid -- written one element a row at the output's own pitch, which is what a block drafter's contiguous proposal needs. It is not row_topk_merge with R = 1 because a rank still sends R >= 2 pairs: a row-interleaved all_gather moves whole 16-byte words and one pair is 8 bytes, so n is world_size * R and the op takes the top-1 of all of them.

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 an op instance: libr4d takes it in the row's init, so it is in the VRAM budget, and refuses a launch without one). 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.

The router GEMM inside it is NOTUNE. It always takes the split the untuned law picks, which is what makes it byte-identical to the unfused GEMM as an untuned install runs it. A tune-cache entry for the unfused router's gemm_nt does not reach this op, so on a tuned install the two forms may differ in the last bit; no shipped configuration tunes these GEMMs. An architecture asks for this op wherever it declares the pair and issues the router and the pair itself where it does not resolve or a pass is past its rows.

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?, wwt, w_scalewt, sorted_tok, expert_offset, w_oddwt, w_odd_scalewt → 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?, wwt, w_scalewt, sorted_tok, expert_offset, w_1wt, w_1_scalewt, w_2wt, w_2_scalewt, w_3wt, w_3_scalewt → 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, ww, bw?, A_logw, dt_biasw, 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, ww, bw?, 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_logw,dt_biasw,state:inout,state_idx,num_accepted,cu,z?,norm_ww? → 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_ww, conv_bw?, conv_state:inout, conv_idx, a,b,A_logw,dt_biasw,state:inout,state_idx,num_accepted,cu,z,norm_ww → o,o_q?,o_scale?
gdn_gated_rmsnorm M(range) channels eps:f64 act:str x, z, ww → 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?, ww → 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?, ww → q, scale, out_bf16?
gdn_gated_norm_had_quant_i8 M n head_dim group eps:f64 dtype zgrp/opt zblk/opt x, z, ww → 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 mult:f64/opt tokens, wtew → x
embed_lookup_q M(range) n_embd n_vocab dtype vocab_offset/opt wscale:f64/opt tokens, wtew, scalew? → x, ahead?
logits_gemm M(range) n_vocab n_embd dtype x, lm_headw → logits
ngram_ids M(range) heads ngram eos tok, state:inout, cu, multw, vocab_sizesw, offsetsw, 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_keyw, w_queryw, w_convw → gv, gvn
ple_conv M(range) n width dilation dtype x, ww, 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 mult

Every gathered row multiplied by mult and rounded once to x's dtype; absent, the gather is a plain copy. Gemma scales its embedding by sqrt(hidden) held in a bf16 buffer, so the plugin passes that value rounded to bf16 (22.625 at a hidden of 512, not 22.6274) and the product is the reference's to the bit. wscale is not this: it is the scale embed_lookup_q's layout quantises with, and embed_lookup reads none.

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's runtime mult

mult (optional f64, default 1) multiplies the rows that are FOUND: x[m] = wte[id] * mult. A missed row — negative padding or another rank's token — writes zero, not zero times the scale. Absent it is the plain gather the op has always been, which is what keeps every other caller in the tree bit-identical.

Not wscale. wscale is the scale embed_lookup_q's LAYOUT quantises with — a property of the stored table, read back as the dequant. mult is applied AFTER the gather, to the rows that come out of it; embed_lookup_q takes no mult at all, because its rows arrive already scaled by the table's own dequant.

Why it exists: Gemma 4 scales its embedding by sqrt(n_embd) at the lookup — the reference's build_inp_embd and HF's Gemma4TextScaledWordEmbedding both do it there, and the checkpoint's table is plain. Baking it into the table instead would untie it from the head, which the model ties and which the checkpoint stores twice; scaling the lookup leaves the table and the head the same tensor the weights loader already maps.

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_logprobs M(range) n_vocab k vocab_off/opt logits, params → stats

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.

sample_logprobs is what an OpenAI logprobs field or llama-server's n_probs reads. It runs on the RAW logits, before any stage has touched them, and only for a row whose flags carry RAD_SP_LOGPROBS; other rows are not written. stats is [M, 2 + 2k] f32: the row's maximum, the sum of exp(l - max) over it, and its k largest as (id, logit) pairs, descending, ties to the lower id, the id written as a float (exact below 2^24) and GLOBAL under a split vocabulary (vocab_off added). A slot past the vocabulary is id -1, logit -infinity. k is geometry: the one width every request's top_logprobs fits in. The engine declares 21 -- OpenAI's bound of 20, plus one so that a row is 44 floats, whole 16-byte words, which the vocab-parallel all_gather moves. A token's logprob is l - max - log(sum); under a split vocabulary the host merges the ranks' rows (the maxima and sums by the usual rescale, the pairs by value).

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)

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 yarn_factor:f64/opt yarn_low:f64/opt yarn_high:f64/opt yarn_magnitude:f64/opt k, ww, 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 has a host kernel in libref and libavx -- the oracle for libr4d's, YaRN included -- and the other four are device-only.

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<ratio of k[b*ratio + j] ),  at position pos0 + b*ratio )

The rotation is rope's NeoX ladder over the first rotary_dim lanes, with rope's YaRN coefficients (yarn_factor, yarn_low, yarn_high, yarn_magnitude) when given -- the attention's own rotary, so a compressed key and the keys it summarises are rotated alike. With YaRN, scale must be 1.

M is BLOCKS and k is ratio times taller. Every other op in this document has one output row per input row. A caller that sized k at the block count would have every block pool three rows of the next one — plausible numbers and a different model — so libr4d's entry point checks it.

The mean is accumulated in f32 and rounded to the activation dtype BEFORE the norm. That is the reference's order (key_groups.float().mean(dim=1).to(raw_keys.dtype)), and carrying f32 through into the norm is a different number: small, and systematic on every block of every layer.

block_size is the same number as ratio and exists to size a KV group. Builder::find_kv_consumer binds a group to the first op declared after it that carries the literal key block_size, so an op that wants to decide a group's page size has to spell it that way -- and the block-key store's page is the compress block. libr4d refuses a disagreement rather than picking one.

out may be a KV cache, and is then rank four. The block-key store's geometry is a container and not an attention shape -- kv_heads 1, block_size the compress ratio, head_dim n/(2*ratio) -- so a page holds exactly n contiguous values and the manager allocates one per ratio tokens. Read that way it is [pages, n], and libr4d checks the product rather than the rank, because the rank is the engine's business and the width is the op's. qsa_score's bk is the same object and takes the same reading.

page is optional and changes where the answer goes, not what it is. The block-key store is the paged KV group described in docs/QSA.md, so block b of a sequence lives at whatever page the manager gave it and consecutive blocks are not consecutive pages. Absent, the write is contiguous — a caller with its own scratch, or a test. Present, M comes off page and not off out, because out is then as tall as the pool and reading the block count from it would write the whole store on every call. A -1 entry is a block with no page and is skipped rather than written somewhere arbitrary.

pos turns a launch from a RANGE into a LIST, and pos0 is the range. Blocks tile the token stream, so within one sequence block i's first token is at pos0 + i*ratio and the parameter is enough — which is what a prefill chunk looks like. It is not what a decode step looks like: the sequences in a batch are at different positions, so the blocks completing in one launch belong to different sequences and are not a run at all. With pos given, a launch is a set of work items and one of them a layer a step serves any mix.

A block key is query-independent and permanent once its ratio tokens exist. The reference recomputes it inside its per-query loop for clarity; an engine computes it when the last token of the block arrives and caches it. That is the difference between the indexer costing 64 bytes a token a layer and costing a pass over the whole history every step.

qsa_score is the indexer's vote:

score[m][b] = ( sum over heads h of relu( q[m][h] . bk[bt[seq(m)][b]] ) ) / sqrt(n)

m is a QUERY and bt is the SEQUENCE's, and cu is the join. At decode the two are the same list — one query a sequence — but a prefill chunk is many queries over one history and each of them selects its own set. cu_seqlens is the only operand in a batch that says which sequence a token belongs to, so it is what turns a query row into a table row.

nc bounds the block loop and is required. blocks is a declared width, sized for max_ctx / ratio, so a scorer that trusted it would read 65536 padding entries a query on a 200-token prompt. nc[seq] is the sequence's live complete-block count — an upper bound for every query in the chunk, and within T / ratio of each one's own. qsa_select narrows it to the exact per-query bound, which it can because it has pos.

And score is narrowed by the CALLER to the live reach, because a grid cannot read nc. Both ops size their launch from the declared width -- the live count is device data and the grid is a host-side number -- so an op issued at the full blocks launches for 200K of context whatever the prompt is. The architecture narrows the score operand's column count to (max_ctx_len + n_tok + n_spec + ratio) / ratio + 1, which the batch already carries on the host, and the pitch stays the declaration's. Every term of that is slack on purpose: an UNDER-estimate drops blocks from the selection and nothing anywhere would say so, and the + 1 is what keeps n < blocks, which is what makes qsa_select's partial page -- bt[n] -- readable at all.

The relu is per (block, head) and the sum is over heads, in that order. Four independent votes, each of which can only ever argue for a block. Summing first and clamping after would let one head's large negative cancel another's positive — a different model that still ranks blocks plausibly, which is the worst kind of difference.

bt holds PAGE IDS, not block indices. The attention KV group is paged at the compress ratio, so a block and a page are the same thing: the sequence's existing block table is already the list to score, and the top-k over the output is already the block table the attention reads. That identity is the design, and an op that took block indices would put a translation on both sides of it.

A negative table entry scores -inf, not zero. A block table is padded to its declared width and a real score can be zero — every head vetoed the block — so zero would let padding outrank a block the model deliberately ignored.

qsa_select turns those scores into the table the attention reads: the topk highest-scoring blocks as page ids, with the partial page appended.

nc is the complete-block count and is a separate operand from the table's width, because the two differ by exactly the thing the op treats specially. bt[n], when it is not -1, is the partial page — the tokens after the last complete block, which the reference attends unconditionally and which the query itself lives in. It is appended after the selection and is never a candidate for it, so sel is topk + 1 wide and the last slot is not spare: sized at topk it would drop the newest tokens on three queries in four.

The bound n is the QUERY's, not the sequence's: n = min(nc[seq(m)], (pos[m] + 1) / ratio), because block b is entirely in this query's past exactly when b*ratio + ratio - 1 <= pos. At decode the two agree by construction. cu is the join, as in qsa_score.

qsa_select takes OPTIONAL scratch, and it is what lets the selection use the device. One workgroup a query is the natural grid -- a selection is a reduction over the whole block list -- and at decode that is 1 + n_spec workgroups on a 64-CU part, 6% of it, walking 50000 blocks five times. Given an arena the kernel instead cuts the list into chunks, takes each chunk's own top-k, and merges: exact, because a chunk's local top-k contains every global top-k member of that chunk, and bit-identical in the OUTPUT LAYOUT too -- the attention sums the selected pages in table order and float addition is not associative, so "the same set" is not the same text. Without the arena the op runs the single-workgroup kernel, which is what a caller with no scratch, or a launch wider than a decode step, gets.

The partial page exists exactly when (pos + 1) % ratio != 0, and that is a causality rule. A decode query on a block boundary has n == nc and bt[nc] is the page after the sequence's last, which the KV manager leaves -1 — so testing the page instead of the position would be right for the only case decode presents, and wrong mid-chunk, where the same query has later tokens of the same chunk already stored in bt[n]: a live page entirely in its future, which seqused would then cover in full. A model that attends to its own future is not a wrong ranking; it is a model that cheats, and it would show up as an implausibly good prefill and nothing else.

Last is load-bearing. Every selected complete page is entirely in the query's past, so the only causal masking left is inside the final page — and putting it at the end of the table makes the attention's own kk <= q bound do exactly that, with no change to the attention kernel. The effective query position the caller passes is then (nsel - 1) * ratio + pos % ratio.

Ties break to the lower block index, through a prefix count. A block score is a sum of four relus over a shared grid, so equal scores are ordinary rather than pathological, and a top-k placed by whichever atomic landed first would not reproduce across runs — which this tree checks by comparing generated text byte for byte.

seqused is why QSA needs no new attention kernel. attn_paged takes its block table and its per-sequence length as ordinary operands — the architecture reads them off the KV group batch and passes them — so the sparse attention is that same op issued with sel in place of the sequence's table and this number in place of its length. The flat KV it then walks is nsel pages of ratio with the partial page last, so the query sits at (nsel-1)*ratio + pos%ratio and the length is one past it. One formula covers both cases: with a tail the query is inside that last page, and without one pos%ratio is ratio-1 and it collapses to nsel*ratio, which is every selected page fully visible — correct, because a complete page is entirely in the query's past. It is emitted here rather than by an op of its own because it is one store off a number this kernel already has.

qsa_tail_store parks the raw index keys of the tokens that have not completed a block yet: state[sidx[s][0]][pos[t] % ring] = k[t]. A block key is the mean of ratio consecutive raw keys, so at decode three of the four are from earlier steps and something has to carry them.

The tail is a LINEAR KV STATE, not a plane indexed by the batch row. It is read on a later step than the one that wrote it, which makes it state and not scratch, and state has two axes a buffer cannot express: the LAYER (the keys are index_qk_proj's output, and that weight is per layer) and the SEQUENCE. The batch row is step-local — the scheduler builds a step from the running set in priority order and stable-sorts decode first — so a request that outlives an earlier one shifts down a row and inherits its keys. A linear group gives a slot that is the sequence's for its life, zeroes it when it hands one out, and saves and restores it with the rest of the linear state when the prefix cache checkpoints. state is the group's cache for one bound layer, [slots, 1, ring, n]; sidx is that group's state_index row and column 0 is the committed slot.

cu is how a token finds its ROW, and it is the only per-token index in the batch that says so — slot_mapping is a flat KV slot and carries no sequence at all. With a handful of sequences a linear scan of the boundary list costs nothing beside the copy it guards. Which STATE that row belongs to is sidx, and the two are different questions.

The ring is ratio + n_spec, and speculation is the whole of the difference. A verify step runs 1 + n_spec positions and the next step restarts at the first REJECTED one, so a position this step has already overwritten is owed to the next. At ratio 4 and depth 3 the step covering positions 9..12 writes rows 1, 2, 3, 0 — and row 0 is the one position 8 is still owed, because the block holding 8..11 is rebuilt as soon as the following step starts at 10. The widest window a step can still owe is [p0 - ratio + 1, p0 + n_spec], so a ring that long means a row is only ever overwritten by a position past the end of any step that could ask for it. With n_spec 0 the ring is the ratio.

Only the last ring tokens of a sequence's chunk write, and that is not an optimisation. Positions p and p + ring land on the same row, so a launch that wrote every token of a prefill chunk would have several workgroups racing for one row and the winner would depend on the schedule. Restricting the write makes every row the target of at most one token — their positions differ by less than ring, so their rows differ — and the result is what a serial last-writer-wins loop leaves. Removing the restriction makes the case report rows DIFFER and two runs identical NO, which is the race saying so out loud.

state is INOUT and rows nothing targets keep what they held. That is the whole point across steps: at decode a handful of rows are written and the rest are what the next block key needs. The selftest counts the untouched rows of the slots the batch does NOT name separately, because that is the count a batch-row index destroys.

Under a multi-component rotary (M-RoPE) a block key rotates at its first token's three components, not at its index. qsa_work takes the step's rope planes (RadBatch::rope_pos, [3, T]) and then writes bpos as [work, 3], a block's three components side by side (rows first, so a smaller step is fewer rows of the same buffer): a first token in the step reads its own components, and one committed in an earlier step -- always text, since a prefill chunk ends on a whole block and a prompt ends in text -- takes its index plus the shift the step's first row of that sequence carries. qsa_block_key with mode (mrope, imrope) and sections then reads the component each frequency's section names, as rope does.

qsa_work is the gather that turns a batch into the flat block list qsa_block_key reads. Nothing in a batch hands it that list: the keys of a block completing at decode are three rows of the tail state and one of this step, the keys of a block completing inside a prefill chunk are four rows of the chunk, and a block straddling the chunk's start is some of each — while the blocks themselves belong to different sequences at different positions.

So this op walks the sequences, works out which blocks became complete, copies each one's ratio raw keys into consecutive rows of stage, and writes its destination page and first-token position beside it. The block-key kernel never learns that two sources existed.

M is tokens and work is blocks, and they are different numbers: one block per ratio tokens plus at most one straddling block per sequence. work is a HOST bound on how many blocks can complete, not a count of how many did — the count is device data, and slots past it get page = -1, which is the sentinel qsa_block_key already skips. So the bound costs empty workgroups that exit on their first load and never a wrong answer.

bt is the BLOCK-KEY group's table, not the attention group's — see docs/QSA.md on why there are two and which op takes which.

state and sidx are the tail, read at row q % ring of slot sidx[s][0] — see qsa_tail_store just above for why it is a KV state and not a plane. A sequence with no slot (sidx[s][0] < 0) gets page = -1 on its blocks rather than a page built out of whatever the pointer arithmetic reached.

nc is complete blocks per sequence, which the scorer and the selection both need and which this op has already computed on its way to the work list. It is (last position + 1) / ratio and not the number of blocks that completed this step — the two are the same only on a sequence that starts at zero. A separate op for one division per sequence would be a launch a layer a step for four instructions.

The copy is a kilobyte per completing block: 512 KiB for a full 2048-token prefill chunk a layer, four kilobytes for a decode step. The alternative is a block-key kernel that takes two sources, a per-key source selector and a tail layout — three more things to get wrong inside the kernel that does the arithmetic.

Draft heads

A drafter is an ordinary graph over ordinary ops except at its two ends: what proposes the candidates, and what enters the head from the trunk. Those two ends are what these ops are.

DFlash2

op params operands
dflash_select M steps top_k rank n_vocab anchor_stride/opt dtype cand, unary, hp?, anchor, predw, succw → tokens

dflash_conv is the drafter's grouped convolution, and device-only:

op params operands
dflash_conv T hidden_size taps group_size NG block_size dtype x, delta, basew → out

dflash_select is the greedy walk over the candidate lattice: cand/unary are row_topk's two output planes at one row per draft position, hp the selector's rank-256 query, and anchor the step's own token ids — anchor_stride is the block width, because a draft pass carries block ids a sequence and the anchor is the first of them. tokens is THE DRAFT, and the engine reads it by name rather than through the sampler.

hp IS OPTIONAL, AND ABSENT IT IS A PLANE OF ONES — which turns the edge score unary + Σ_r pred[prev][r]·hp[l][r]·succ[cand][r] into the plain low-rank BIGRAM ⟨pred[prev], succ[cand]⟩. That is exactly DSpark's Markov head: a rank-256 learned bigram bias added to the draft logits, decoded greedily, with the predecessor read after the previous step's argmax inside one workgroup. Written literally it is steps full-vocabulary [rank, n_vocab] matrix products — 466 MiB a step to use seven rows — and through this operand it is 8 KiB of codebook gather. The only approximation is the top-K candidate set, which can cost acceptance and nothing else.

mtp_enter: the MTP head's entry, when the residual stream is wide

op params operands
mtp_enter M(range) n hc eps:f64 dtype wadd:f64/opt ngroup/opt fc/opt h, e, w_hw, w_ew, fc_hw, fc_ew → x
ne[j]    = e[j] * rsqrt(mean(e^2) + eps) * (w_e[j] + wadd)
fe[i]    = fc_e[i] . ne
rs[k]    = rsqrt(mean(h[k*n .. k*n+n)^2) + eps)
nh[k][j] = h[k*n+j] * rs[k] * (w_h[k*n+j] + wadd)
x[k*n+i] = fc_h[i] . nh[k] + fe[i]                         for each of the hc sub-streams

A pre-norm MTP head (Qwen3.5, Qwen3-Next, DeepSeek-V3) joins the trunk's hidden state and the next token's embedding with one fc over their concatenation, and arch/common/rad_block_mtp_fp8.h runs that as two rmsnorms into the two halves of one buffer and a single gemm_nt. Qwen4-Exp's head cannot: its hidden state is the wide stream and its checkpoint carries mtp.fc_hidden and mtp.fc_embedding as two square [n, n] matrices beside a [hc*n] gain and an [n] one. So the hidden half is the same matrix applied to each sub-stream — the reference's own .unflatten(-1, (hc_count, hidden_size)) idiom, which is how every other 10240-wide tensor in that architecture is handled — and the embedding half is one n-wide product added into all hc of them, the same broadcast the trunk enters the stream with (hidden_states.repeat(1, 1, hc_count)).

One op and not six, and here that is a statement about the ABI rather than about launch count. Every intermediate above is a different view of the same bytes: nh is [M*hc, n] to the GEMM and [M, hc*n] to the norm. core/runtime/issue.cpp narrows dim 0 or the last dimension with every stride left alone, which is the right rule and makes a row-pitch change inexpressible — so the unfused spelling cannot be written at all, not merely at a cost. It is also six launches off a draft round, and a draft round is the latency the head exists to hide.

fc is the two square matrices' STORED format: bf16, or e4m3 rows with f32 128-column group scales in each row's own tail, the geometry hc_read's mix_down uses. Only the drafts see it -- the head proposes and the trunk verifies -- so it can cost acceptance and never a token.

ngroup is the one thing the checkpoint does not say. A [hc*n] gain fits both a grouped norm (hc rsqrts, one per sub-stream — what Qwen4ExpTextGatedResidual does at every one of its 97 connections) and a flat one (a single rsqrt over the whole wide row). Both readings are self-consistent, libref shares whichever the graph picks, and no per-op oracle can reach it — exactly the shape of question rad_block_mtp_fp8.h's concat order is. It has the same cheap empirical answer: a head wired the right way round accepts most of what it proposes, and one wired the wrong way round proposes noise while the model's own output stays perfectly correct. The head declares the grouped reading (ngroup = hc), and ngroup carries it as a parameter so a flat reading (ngroup = 1) is one declaration away rather than a different kernel.


Adding an op

Add the schema row to libref/, write the naive host implementation beside it, and it exists. No core file changes. If you add an op without a ref implementation, say so in your commit: you have just created something with no fallback and no oracle, and that is a decision, not an oversight.