Files
Deadcode 3b13fd3266 radiance 2.3.0
New models. Thinking Machines' Inkling 2 Mini by gostar (#584-#586): its text backbone and loop, the
release's NVFP4 and MXFP8 checkpoint read by the converter, an int8 trunk, the vision tower, tensor
parallelism, tool calls (with Warzuponus) and an image processor that matches transformers bit for
bit (millaguie). Inkling's routed experts are served in IQR-V4's four-bit trellis (moe_gemm_iqr),
fitted by GPTQ against Hessians the plugin now records (`--calib-dir`), its token embedding in int8
(read in place by embed_lookup), and incoai's DFlash2 drafter in four bits in place of the release's
DSpark. That container, 14.4 GiB with the vision tower, decodes 190 / 427 / 658 tokens a second at 1
/ 4 / 8 streams at tp 2 on two R9700 with `draft-window: adaptive` (code 240 / 550 / 790,
step-by-step arithmetic 358 / 811 / 1116): 19-43% ahead of the same target with the release's DSpark
drafter and 45-83% ahead of the all-int8 container (26.1 GiB, 124 / 263 / 386), at a mean KL of
0.0066 against the release's weights (int8: 0.0042). DeepSeek-V4.1-Flash on 2.x by baKon (#583),
with its vision tower, its IQ1_S and IQ2_XXS containers and linears and experts served in
llama.cpp's own blocks; parts ported from mikoskinen/r9700-exact-serving by Dad_Vibes' fork.
GLM-5.3-Flash on 2.x with its IQR / IQ8R container, an int8 latent cache and four-card serving by
AnimusNull (#566). Gemma 4 dense on EXL3-V3 by keptn42 (#537), on the EXL3 dense rows and
qwen35_exl3 tensor parallelism by Hero422. NVIDIA's 2-bit Nemotron-3.5-Bolt-74B-A9B on one card by
keptn42 (#477). Wan 2.2 TI2V-5B text-to-video and first-frame video by rkcth (#535). LFM3-1.7B on
RDNA3 iGPUs by DokterSmol (#578). Qwen3.8-27B in bf16 with the DFlash2 drafter, and bf16x12, bf16
weights packed losslessly in 12.25 bits, by eddie168 (#577). Int8 experts for drama-3 by morevgalex
(#598).

Speculative decoding. Draft trees for Qwen3.8-27B's DFlash2 drafts by Kezi (#450, opt-in): one card,
+17% at one stream and +14% at two. --draft-max-lanes and --draft-min-accept, two gates on a block
drafter, and --draft-window-ckpt, its window KV kept in the checkpoint slots, by rkcth (#600-#602).
`draft-window: adaptive` verifies each sequence's drafts up to the first one unlikely to be kept. A
verify's rows share each K and V tile in Inkling's attention by gostar (#608): 93 to 118 tokens a
second at a 70k-token prompt on one card.

Generation. Krea 2 instruction edits with one or two references and registered outfill, LoRA on the
image generators with bias deltas and row-mixer adapters applied per request, and MiniMax-H3's
model-declared sparse attention profiles, by rkcth (#534, #542, #543, #605-#609). MiniMax-H3's audio
reference guide pinned from a frame by 1beb (#595). LTX-2's chunk ceiling raised to 40 by Itoash
(#570).

Fleets and memory. memory.queue-reserve, the hardware queues a card keeps ready, by kevinjd (#589).
memory.pinned-numa-node by Foundry (#580). memory.card-headroom-mib on single-model tensor-parallel
budgets by zihaomu (#624). Parked weights rebuilt on the CPU, memory ownership kept across a failed
handoff and quiescent callbacks serialised, by rkcth (#557-#561). A warm load may unload a parked
model (#592); the first card listed chooses the architecture (#458).

Kernels. libr3d's packed dense IQR GEMM and routed IQR expert rows on gfx1100, and libhalo's GEMV
decode for Qwen3.8-Flash-Next's MoE down, by zihaomu (#562, #620, #622). GGML IQ2_XXS and IQ3_XXS
packed on the card by baKon.

Dashboard. KV cache in tokens (in use, capacity now and with all flexible space), live figures
through long prefill steps, step / decode / prefill / acceptance lines without holes, and expert hit
rates only on tiered models. Seven tile and history defects fixed by Noblistic (#597), vision
encoder and per-card hardware figures by davetha (#574), per-LoRA attribution by dowjames (#552).

Fixed. return_tokens / return_token_ids on the OpenAI endpoints (#571). reasoning_effort "none" and
"minimal" reach a template that takes them as levels, by millaguie (#608). Misaligned loads in the
windowed paged attention over an int8 cache (#540). A retired 27B head refused by name (#541).
rad-convert's GPU trellis jobs kept fed, and --reuse-only, by gostar (#575). jinja's tojson takes
sort_keys by Warzuponus (#582). hfget measures file sizes with stat, by kepler_62f (#623). The
tested C++ compiler is stated in the build guide by lowell80 (#551). The codebase was cleaned up:
shared helpers instead of repeated blocks, and the int8 linear, activation and GEMM code no longer
named after the retired fp8 formats.

Inkling 2 Mini is served in one form: IQR-V4 experts with the DFlash2 drafter
(StillDeadcode/inkling-2-mini-iq4r-v4 on Hugging Face, or
data/recipes/inkling2-mini-iq4r-v4.recipe). A container with the release's DSpark drafter, or with
NVFP4, int8 or 4-bit experts, is refused by name and needs converting again.

Plugin ABI 44: out-of-tree plugins need to be rebuilt.

Thanks to @gostar, @baKon, @Dad_Vibes, @AnimusNull, @keptn42, @Hero422, @rkcth, @DokterSmol,
@eddie168, @morevgalex, @Kezi, @1beb, @Itoash, @kevinjd, @Foundry, @zihaomu, @Noblistic, @davetha,
@dowjames, @Warzuponus, @millaguie, @kepler_62f, @lowell80 and @Code000.
2026-11-26 15:28:37 +01:00

3519 lines
247 KiB
C++

/* ref_registry.cpp -- libref's op schemas and kernel rows.
*
* The schemas are the authoritative copy of the conventional op vocabulary; docs/OPS.md is their
* prose form (spec §17). The core never learns op names, but ref can only implement ops it knows,
* so an architecture plugin using only these ops is guaranteed a fallback and an oracle. Adding an
* op means a row here plus a host implementation; no core file changes.
*
* The first plugin in hierarchy order to declare an op fixes its schema, and ref sits last, so a
* faster plugin's schema wins and ref's must agree. Arguments are positional, so disagreeing
* schemas would silently give the same call different meanings (spec §2.3). Operand orders follow
* docs/OPS.md; deviations are marked in the deviating row's doc string.
*
* The kernel rows are deliberately unconstrained: every extent is read off the operands, and ref
* must match anything. The exception is kv_store, which must be told the paged block size and so
* carries `block_size >= 1`; that admits every value and refuses only a query that omits the key
* (an absent key fails a constraint, rad_util.cpp's constraint_holds). attn_paged carries the same
* constraint, but there `block_size` is RAD_DERIVED, so it is skipped during matching and ref reads
* the block size off the cache tensor.
*
* spec §7.2 has the block manager read the KV block size from the resolved attention kernel's
* constraints. libr4d answers `block_size == 16`; ref answers "any", so a core resolving attention
* to ref alone must pick, and 16 is the value the project is built around.
*
* Everything is domain HOST at priority 0; a device-side reference would be a separate row.
*/
#include "ref_common.h"
#include "ref_ops.h"
#include "ref_planes.h"
#include "rad_abi_layout.h"
/* ================================================================== parameter specs */
#define P_INT(k) { k, RAD_P_INT, RAD_REQUIRED }
#define P_INTO(k) { k, RAD_P_INT, RAD_OPTIONAL }
#define P_STR(k) { k, RAD_P_STR, RAD_REQUIRED }
#define P_STRO(k) { k, RAD_P_STR, RAD_OPTIONAL }
/* Supplied by the kernel, not the caller (rad_abi.h, spec §7.2): constraints on it are skipped
* during matching and the winner's RAD_C_EQ value is written into the resolved geometry.
* RAD_REQUIRED would make it unselectable, since the caller cannot know it before resolution. */
#define P_INTD(k) { k, RAD_P_INT, RAD_DERIVED }
/* The linear-state tile length. The scheduler cuts its steps on a multiple of it (rad_abi.h,
* RAD_PROLE_SEQ_CHUNK); nothing else in the core reads a parameter by meaning. */
#define P_CHUNK(k) { k, RAD_P_INT, RAD_DERIVED, RAD_PROLE_SEQ_CHUNK }
/* A count the kernel is handed as a bound and never writes past its operands for (rad_abi.h,
* RAD_PROLE_CAPACITY). */
#define P_CAP(k) { k, RAD_P_INT, RAD_REQUIRED, RAD_PROLE_CAPACITY }
/* Real-valued parameters are RAD_P_F64 (docs/OPS.md, "The four rules"); the core refuses a string
* spelling for these keys, so plugins cannot drift apart over formatting. Constraints never match
* on a float. */
#define P_F64(k) { k, RAD_P_F64, RAD_REQUIRED }
#define P_F64O(k) { k, RAD_P_F64, RAD_OPTIONAL }
#define OPD(n) { n, RAD_OPD_IN, 0 }
#define OPD_O(n) { n, RAD_OPD_IN, 1 }
#define OUT(n) { n, RAD_OPD_OUT, 0 }
#define OUT_O(n) { n, RAD_OPD_OUT, 1 }
#define INOUT(n) { n, RAD_OPD_INOUT, 0 }
#define INOUT_O(n) { n, RAD_OPD_INOUT, 1 }
#define WGT(n) { n, RAD_OPD_WEIGHT, 0 }
/* A weight table: the issue names a run of declared weights and the kernel gets a device array of
* pointers, one per expert (RAD_WTAB in abi/rad_runtime.h). */
#define WTAB(n) { n, RAD_OPD_WTAB, 0 }
#define WGT_O(n) { n, RAD_OPD_WEIGHT, 1 }
#define ARR(a) (a), (int)(sizeof(a) / sizeof((a)[0]))
/* ---- elementwise and norms ---------------------------------------------------------------- */
/* `mult` scales the normed row before its one rounding (default 1). rmsnorm_add has none: nothing
* fuses a scaled branch into the add. */
static const RadParamSpec pRmsnormAdd[] = { P_INT("M"), P_INT("n"), P_F64("eps"), P_STR("dtype"),
P_F64O("wadd") };
static const RadParamSpec pRmsnorm[] = { P_INT("M"), P_INT("n"), P_F64("eps"), P_STR("dtype"),
P_F64O("wadd"), P_F64O("mult") };
/* `w` is optional: absent, the gain is one (a norm with no scale, such as Gemma 4's value norm and
* its multimodal embedder's pre-projection norm). */
static const RadOperandSpec oRmsnorm[] = { OPD("x"), WGT_O("w"), OUT("y") };
/* The gated residual pair. The operand list is banded on `inject` and `group`: `inject` 0 is the
* final mixer (no per-stream gate weight or output), `group` 0 a read whose consumer wants bf16
* instead of block-scaled fp8. Both default on; only two sites in the model turn them off. */
static const RadParamSpec pHcRead[] = { P_INT("M"), P_INT("n"), P_INT("hc"), P_INT("lowrank"),
P_F64("eps"), P_STR("dtype"), P_F64O("wadd"),
P_INTO("inject"), P_INTO("group"), P_INTO("rotate"),
P_STRO("mix") };
static const RadOperandSpec oHcRead[] = { OPD("h"), WGT("w"), WGT("mix_down"), WGT("mix_up"),
WGT_O("inject_w"), OUT("x"), OUT_O("inj"), OUT_O("q"),
OUT_O("scale"), OUT_O("rq"), OUT_O("rscale") };
/* ---- PLE / n-gram ---------------------------------------------------------------------
*
* `state` is a rolling window of committed ids with RAD_KV_CONV's shape and ple_conv's contract,
* read at `num_accepted - 1`. Its cold value is eos, not zero (a real token); see ref_ngram.cpp.
* `eos` is a parameter, not an operand: the shift rule is a model property and can be
* constrained on. */
static const RadParamSpec pNgramIds[] = { P_INT("M"), P_INT("heads"), P_INT("ngram"),
P_INT("eos") };
static const RadOperandSpec oNgramIds[] = { OPD("tok"), INOUT("state"), OPD("cu"), WGT("mult"),
WGT("vocab_sizes"), WGT("offsets"),
OPD_O("cache_idx"), OPD_O("has_init"),
OPD_O("num_accepted"), OUT("ids"),
OUT_O("ahead_ids") };
/* PLE's gate. `k` and `q` arrive un-normed; the op owns all three norms, since the third is over
* bytes the op itself wrote. */
static const RadParamSpec pPleGate[] = { P_INT("M"), P_INT("n"), P_INT("hc"), P_F64("eps"),
P_STR("dtype"), P_F64O("wadd"), P_F64O("gate_eps") };
static const RadOperandSpec oPleGate[] = { OPD("k"), OPD("q"), OPD("v"), WGT("w_key"),
WGT("w_query"), WGT("w_conv"),
OUT("gv"), OUT("gvn") };
/* The dilated convolution that finishes the layer: one op for prefill and decode, taking both
* contracts (`has_init` for prefill, `num_accepted` for decode; see ref_ple.cpp). */
static const RadParamSpec pPleConv[] = { P_INT("M"), P_INT("n"), P_INT("width"),
P_INT("dilation"), P_STR("dtype") };
static const RadOperandSpec oPleConv[] = { OPD("x"), WGT("w"), INOUT("conv_state"), OPD("cu"),
OPD_O("resid"), OPD_O("cache_idx"), OPD_O("has_init"),
OPD_O("num_accepted"), OUT("y") };
/* ple_conv's operands with `b`, a per-channel bias inside the silu, before `y`. */
static const RadOperandSpec oPleConvBias[] = { OPD("x"), WGT("w"), INOUT("conv_state"), OPD("cu"),
OPD_O("resid"), OPD_O("cache_idx"), OPD_O("has_init"),
OPD_O("num_accepted"), WGT("b"), OUT("y") };
static const RadParamSpec pCausalConv[] = { P_INT("M"), P_INT("n"), P_INT("width"),
P_STR("dtype") };
static const RadOperandSpec oCausalConv[] = { OPD("x"), WGT("w"), INOUT("conv_state"), OPD("cu"),
OPD_O("cache_idx"), OPD_O("has_init"),
OPD_O("num_accepted"), OUT("y") };
/* Inkling 2's short convolution with its residual: causal_conv1d's operands, with the optional
* input-dependent taps `dyn` before the state's optionals; `heads` 0 means there is no `dyn`. */
static const RadParamSpec pSconvRes[] = { P_INT("M"), P_INT("n"), P_INT("width"), P_INT("heads"),
P_STR("dtype") };
static const RadOperandSpec oSconvRes[] = { OPD("x"), WGT("w"), INOUT("conv_state"), OPD("cu"),
OPD_O("dyn"), OPD_O("cache_idx"), OPD_O("has_init"),
OPD_O("num_accepted"), OUT("y") };
static const RadParamSpec pHcWrite[] = { P_INT("M"), P_INT("n"), P_INT("hc"), P_STR("dtype") };
/* The control vector's projection (libref/ref_hc.cpp, core/cvec.h): one unit direction per layer
* and a per-row switch. `v` is f32 whatever the stream's dtype is, because the engine uploads
* the table in f32; `on` is an int32 row mask (core derive.cpp's `projection_on_rows`). */
static const RadParamSpec pHcProject[] = { P_INT("M"), P_INT("n"), P_INT("hc"), P_F64("scale"),
P_STR("dtype") };
static const RadOperandSpec oHcProject[] = { INOUT("h"), OPD("v"), OPD("on") };
static const RadOperandSpec oHcEnter[] = { OPD("x"), OUT("h") };
static const RadOperandSpec oHcWrite[] = { OPD("y"), OPD("inj"), INOUT("h") };
static const RadParamSpec pMhcPre[] = { P_INT("M"), P_INT("n"), P_INT("hc"),
P_INT("iterations"), P_F64("eps"), P_F64("hc_eps"), P_STR("dtype") };
static const RadOperandSpec oMhcPre[] = { OPD("h"), WGT("fn"), WGT("scale"), WGT("base"),
OUT("x"), OUT("post"), OUT("comb") };
static const RadParamSpec pMhcPrePartial[] = { P_INT("M"), P_INT("n"), P_INT("hc"),
P_INT("chunk"), P_INT("first"), P_INT("chunks"), P_STR("dtype") };
static const RadOperandSpec oMhcPrePartial[] = { OPD("h"), WGT("fn"), OUT("part") };
static const RadParamSpec pMhcPreFinish[] = { P_INT("M"), P_INT("n"), P_INT("hc"),
P_INT("iterations"), P_F64("eps"), P_F64("hc_eps"), P_INT("chunk"), P_STR("dtype") };
static const RadOperandSpec oMhcPreFinish[] = { OPD("part"), OPD("h"), WGT("scale"), WGT("base"),
OUT("x"), OUT("post"), OUT("comb") };
static const RadOperandSpec oMhcPost[] = { OPD("x"), OPD("h"), OPD("post"), OPD("comb"),
OUT("out") };
static const RadOperandSpec oMhcContract[] = { OPD("h"), OUT("x") };
static const RadOperandSpec oMhcMix[] = { OPD("h"), WGT("fn"), WGT("scale"), WGT("base"),
OUT("pre"), OUT("post"), OUT("comb") };
static const RadOperandSpec oMhcCollapse[] = { OPD("h"), OPD("pre"), OUT("x") };
static const RadParamSpec pKdaUpdate[] = { P_INT("M"), P_INT("heads"), P_INT("head_k"),
P_INT("head_v"), P_F64("lower_bound"), P_F64("scale"), P_F64("l2_eps"),
P_STR("mode"), P_STR("dtype"), P_STRO("state_form") };
static const RadOperandSpec oKdaUpdate[] = { OPD("q"), OPD("k"), OPD("v"), OPD("a"), OPD("b"),
WGT("A_log"), WGT("dt_bias"), INOUT("state"), OPD("state_idx"), OPD("cu"),
OPD_O("num_accepted"), OPD_O("has_init"), OUT("o") };
/* Mamba-2's selective state update: kda_update's rollback contract with a scalar decay per head. */
static const RadParamSpec pSsdUpdate[] = { P_INT("M"), P_INT("heads"), P_INT("head_dim"),
P_INT("d_state"), P_INT("groups"), P_STR("mode"), P_STR("dtype"), P_STRO("state_form") };
static const RadOperandSpec oSsdUpdate[] = { OPD("x"), OPD("B"), OPD("C"), OPD("dt"),
WGT("A_log"), WGT("dt_bias"), WGT("D"), INOUT("state"), OPD("state_idx"), OPD("cu"),
OPD_O("num_accepted"), OPD_O("has_init"), OUT("o") };
/* The MTP head's entry for a wide residual stream. `ngroup` is hc for a grouped norm over the
* sub-streams, 1 for a flat one over the whole row (see ref_hc.cpp). */
static const RadParamSpec pMtpEnter[] = { P_INT("M"), P_INT("n"), P_INT("hc"),
P_F64("eps"), P_STR("dtype"), P_F64O("wadd"),
P_INTO("ngroup"), P_STRO("fc") };
static const RadOperandSpec oMtpEnter[] = { OPD("h"), OPD("e"), WGT("w_h"), WGT("w_e"),
WGT("fc_h"), WGT("fc_e"), OUT("x") };
static const RadOperandSpec oRmsnormAdd[] = { OPD("x"), OPD("residual"), WGT("w"),
OUT("y"), OUT("residual_out") };
static const RadParamSpec pLayernorm[] = { P_INT("M"), P_INT("n"), P_F64("eps"), P_STR("dtype") };
static const RadOperandSpec oLayernorm[] = { OPD("x"), WGT("w"), WGT_O("b"), OUT("y") };
/* A learned position table resampled to each row's grid and added in (a vision tower's). */
static const RadParamSpec pGridEmbed[] = { P_INT("M"), P_INT("n"), P_INT("side"), P_STR("dtype") };
/* posemb_resample (ref_elementwise.cpp): a position table resampled to a grid, as a table. */
static const RadParamSpec pPosembResample[] = { P_INT("S_h"), P_INT("S_w"), P_INT("T_h"),
P_INT("T_w"),
P_INT("n"), P_STR("dtype") };
static const RadOperandSpec oPosembResample[] = { WGT("table"), OUT("y") };
static const RadOperandSpec oGridEmbed[] = { INOUT("x"), WGT("table"), OPD("coord") };
/* window_order: the window permutation of one pass, from the coordinate planes alone. */
static const RadParamSpec pWindowOrder[] = { P_INT("M"), P_INT("window") };
static const RadOperandSpec oWindowOrder[] = { OPD("crd"), OPD_O("cu"), OUT("idx"), OUT("inv"),
OUT("pos"), OUT("cuw") };
/* grid_embed_cpe adds the rows a segment carries ahead of its patches (n_prefix, `prefix`). */
static const RadParamSpec pGridEmbedCpe[] = { P_INT("M"), P_INT("n"), P_INT("side"), P_STR("dtype"),
P_INTO("n_prefix") };
static const RadOperandSpec oGridEmbedCpe[] = { INOUT("x"), WGT("table"), OPD("coord"),
WGT_O("prefix") };
/* SiTU's gated product over a fused gate_up (ref_elementwise.cpp). A cap of 0 is no softcap. */
static const RadParamSpec pSituMul[] = { P_INT("M"), P_INT("n"), P_STR("dtype"), P_F64O("gate_cap"),
P_F64O("up_cap") };
/* Block attention residuals (ref_attn_res.cpp). states may be absent at n_state 0. */
static const RadParamSpec pAttnResMix[] = { P_INT("M"), P_INT("n"), P_INT("n_state"),
P_INT("n_query"), P_STR("dtype"), P_F64("eps") };
static const RadOperandSpec oAttnResMix[] = { OPD_O("states"), OPD("cur"), OPD_O("addend"),
WGT("query"), WGT_O("w"), OUT("y") };
/* LiLiCorr's correlator (ref_lili.cpp). */
static const RadParamSpec pLiliCand[] = { P_INT("M"), P_INT("k"), P_INT("world"), P_INT("steps"),
P_INT("n"), P_STR("dtype"), P_F64O("eps"),
P_INTO("row_k") };
static const RadOperandSpec oLiliCand[] = { OPD("stats"), OPD("pas"), WGT("ln_w"), WGT("ln_b"),
WGT("up_w"), WGT("up_b"), WGT("slot"), WGT("rank"),
OUT("cand"), OUT("feat"), OUT("base") };
static const RadParamSpec pLiliAttn[] = { P_INT("M"), P_INT("steps"), P_INT("k"), P_INT("n_head"),
P_INT("head_dim"), P_STR("dtype"), P_F64O("scale") };
static const RadOperandSpec oLiliAttn[] = { OPD("qkv"), WGT("rel"), WGT("same"), OUT("out") };
static const RadParamSpec pLiliFactor[] = { P_INT("M"), P_INT("L"), P_INT("n"), P_STR("dtype") };
static const RadOperandSpec oLiliFactor[] = { OPD("h"), OPD("a"), OUT("y") };
static const RadParamSpec pLiliWalk[] = { P_INT("M"), P_INT("steps"), P_INT("k"), P_INT("n"),
P_STR("dtype"), P_F64O("eps"), P_F64O("scale") };
static const RadOperandSpec oLiliWalk[] = { OPD("edges"), OPD("aout"), OPD("cand"), OUT("tokens"),
OUT_O("scores") };
static const RadParamSpec pSwigluClamped[] = { P_INT("M"), P_INT("n"), P_F64("limit"),
P_STR("dtype") };
static const RadParamSpec pDsaPool[] = { P_INT("M"), P_INT("n"), P_INT("ratio"), P_STR("dtype"),
P_INTO("block_size") };
/* `kv_dtype`: the latent cache's dtype when it is not the query's (fp8_e4m3, at descale 1). */
static const RadParamSpec pMlaAttn[] = { P_INT("M"), P_INT("latent_dim"), P_INT("heads"),
P_INT("blocks"), P_INT("block_size"), P_F64("scale"), P_STR("dtype"), P_STRO("kv_dtype") };
static const RadParamSpec pMlaProject[] = { P_INT("M"), P_INT("heads"), P_INT("head_dim"),
P_INT("latent_dim"), P_STR("mode"), P_STR("dtype") };
static const RadOperandSpec oMlaAttn[] = { OPD("q"), OPD("kv"), OPD("bt"), OPD("length"),
OUT("out") };
static const RadParamSpec pMlaAttnPaged[] = { P_INT("M"), P_INT("heads"), P_INT("latent_dim"),
P_INT("rope_dim"), P_INT("block_size"), P_F64("scale"), P_STR("dtype") };
static const RadOperandSpec oMlaAttnPaged[] = { OPD("q"), OPD_O("q_rope"), OPD("kv"),
OPD("block_table"), OPD("seqused"), OPD("cu_seqlens"), OPD_O("gate"), OUT("out") };
static const RadParamSpec pDsaScore[] = { P_INT("M"), P_INT("n"), P_INT("heads"),
P_INT("blocks"), P_INT("seqs"), P_STR("dtype"), P_INTO("row_part") };
static const RadOperandSpec oDsaScore[] = { OPD("q"), OPD("q_scale"), OPD("gate"), OPD("k"),
OPD_O("k_scale"), OPD("bt"), OPD("cu"), OPD("nc"), OUT("score"), OUT_O("cu_part") };
static const RadOperandSpec oDsaPool[] = { OPD("kg"), WGT("ape"), OUT("q"), OUT("scale"),
OPD_O("page"), INOUT_O("cache") };
static const RadParamSpec pRouterSigmoid[] = { P_INT("M"), P_INT("n_expert"), P_INT("top_k"),
P_INT("norm"), P_F64("route_scale"), P_STR("dtype") };
static const RadOperandSpec oRouterSigmoid[] = { OPD("logits"), WGT("bias"), OUT("ids"),
OUT("weights") };
static const RadParamSpec pRouterScored[] = { P_INT("M"), P_INT("n_expert"), P_INT("top_k"),
P_INT("norm"), P_F64("route_scale"), P_STR("score"), P_STR("dtype") };
/* router_scored_topk_vl (ref_glm.cpp): the same gate with a second correction bias, swapped in per
* row by `mask` -- HF's MoEGate keeps bias_vl for image-span tokens. Selection moves, weights
* don't. */
static const RadOperandSpec oRouterScoredVL[] = { OPD("logits"), WGT("bias"), WGT("bias_vl"),
OPD("mask"), OUT("ids"), OUT("weights") };
static const RadParamSpec pRouterSigmoidGroup[] = { P_INT("M"), P_INT("n_expert"), P_INT("top_k"),
P_INT("n_group"), P_INT("topk_group"), P_INT("norm"), P_F64("route_scale"), P_STR("dtype") };
static const RadParamSpec pMnD[] = { P_INT("M"), P_INT("n"), P_STR("dtype") };
/* `div` divides the rounded sum, rounded again (default 1, the plain sum): a mean of branches
* summed pairwise, as a vocoder averages its residual blocks. */
static const RadParamSpec pAdd[] = { P_INT("M"), P_INT("n"), P_STR("dtype"), P_F64O("div") };
/* `scalar` 1 says `w` is one element rather than one row; kernels read that off the operand, but a
* shape description needs the parameter. */
static const RadParamSpec pMulWeight[] = { P_INT("M"), P_INT("n"), P_STR("dtype"),
P_INTO("scalar") };
/* `cap` is required: the one model that issues this op (Gemma 4) has a value for it. */
static const RadParamSpec pMnCap[] = { P_INT("M"), P_INT("n"), P_F64("cap"), P_STR("dtype") };
static const RadOperandSpec oBinary[] = { OPD("a"), OPD("b"), OUT("y") };
static const RadOperandSpec oGateUp[] = { OPD("gate_up"), OUT("y") };
static const RadOperandSpec oMulWeight[] = { OPD("x"), WGT("w"), OUT("y") };
static const RadParamSpec pActMul[] = { P_INT("M"), P_INT("n"), P_STR("act"), P_STR("dtype") };
/* `M` is the step's token count and bands the op; the segment count is cu_seqlens' length. */
static const RadParamSpec pSegMean[] = { P_INT("M"), P_INT("n"), P_STR("dtype") };
static const RadOperandSpec oSegMean[] = { OPD("x"), OPD("cu_seqlens"), OUT("y") };
static const RadParamSpec pSegPool[] = { P_INT("M"), P_INT("n"), P_STR("dtype") };
static const RadOperandSpec oSegPool[] = { OPD("score"), OPD("v"), OPD("cu_seqlens"), OUT("y") };
/* `n` is the point's values; y has n (2 freqs + 1) columns. */
static const RadParamSpec pPointFourier[] = { P_INT("M"), P_INT("n"), P_INT("freqs"), P_INTO("pi"),
P_STR("dtype") };
/* `M` is the input row count, a multiple of `k`; y has M / k rows. */
static const RadParamSpec pPoolRows[] = { P_INT("M"), P_INT("n"), P_INT("k"), P_F64O("mult"),
P_STR("dtype") };
static const RadOperandSpec oUnary[] = { OPD("x"), OUT("y") };
/* The audio (USM conformer) tower's operators. */
static const RadOperandSpec oClamp[] = { OPD("x"), WGT("lo"), WGT("hi"), OUT("y") };
/* `M` is the input time rows. */
static const RadParamSpec pConv2d[] = { P_INT("M"), P_INT("width"), P_INT("cin"), P_INT("cout"),
P_INT("kh"), P_INT("kw"), P_INT("stride"), P_INT("pad"),
P_INTO("cu_div"), P_INTO("valid_stride"),
P_STR("dtype"), P_INTO("t_last"), P_STRO("act") };
static const RadOperandSpec oConv2d[] = { OPD("x"), WGT("w"), OPD("cu_seqlens"), OPD_O("valid"),
OUT("y"), WGT_O("b") };
/* `M` counts input time rows; y is [output rows * Wo, cin * kh * kw]. */
static const RadParamSpec pConv2dUnfold[] = { P_INT("M"), P_INT("width"), P_INT("cin"),
P_INT("kh"), P_INT("kw"), P_INT("stride"),
P_INT("pad"), P_INTO("cu_div"),
P_INTO("valid_stride"), P_INTO("t_last"),
P_STR("dtype") };
static const RadOperandSpec oConv2dUnfold[] = { OPD("x"), OPD("cu_seqlens"), OPD_O("valid"),
OUT("y") };
static const RadParamSpec pConv1dDw[] = { P_INT("M"), P_INT("n"), P_INT("k"), P_INTO("cu_div"),
P_STR("dtype") };
static const RadOperandSpec oConv1dDw[] = { OPD("x"), WGT("w"), OPD("cu_seqlens"), OUT("y"),
WGT_O("b") };
/* `M` is the table's rows, one per distance. */
static const RadParamSpec pRelSin[] = { P_INT("M"), P_INT("n"), P_F64O("max_timescale"),
P_STR("dtype") };
static const RadOperandSpec oRelSin[] = { OUT("y") };
/* `M` is the output rows; `period` the rows a position cycle takes. */
static const RadParamSpec pSegSin[] = { P_INT("M"), P_INT("n"), P_INT("period"),
P_F64O("max_timescale"), P_INTO("cu_div"),
P_STR("dtype") };
static const RadOperandSpec oSegSin[] = { OPD("cu_seqlens"), OUT("y") };
static const RadParamSpec pAttnLocalRel[] = { P_INT("M"), P_INT("head_dim"), P_INT("n_head"),
P_INT("left"), P_INT("right"), P_F64("softcap"),
P_INTO("cu_div"), P_STR("dtype") };
static const RadOperandSpec oAttnLocalRel[] = { OPD("q"), OPD("k"), OPD("v"), OPD("relk"),
WGT("pds"), OPD("cu_seqlens"), OUT("out") };
/* `fill` has no input; it writes `value`. */
static const RadParamSpec pFill[] = { P_INT("M"), P_INT("n"), P_STR("dtype"), P_F64O("value") };
static const RadOperandSpec oFill[] = { OUT("y") };
static const RadOperandSpec oScale[] = { OPD("x"), WGT("s"), OUT("y") };
static const RadOperandSpec oGather[] = { OPD("x"), OPD("idx"), OUT("y") };
static const RadOperandSpec oScatterRows[] = { OPD("v"), OPD("idx"), INOUT("x") };
static const RadParamSpec pCast[] = { P_INT("M"), P_INT("n"), P_STR("from"), P_STR("to") };
/* ---- quantisation and rotation ------------------------------------------------------------ */
static const RadParamSpec pQuant[] = { P_INT("M"), P_INT("n"), P_INT("group"), P_STR("dtype") };
/* gram_accum's: `upper` 1 accumulates only k2 >= k1, the half a reader of a symmetric H needs. */
static const RadParamSpec pGram[] = { P_INT("M"), P_INT("n"), P_INT("group"), P_STR("dtype"),
P_INTO("upper") };
/* `asum`, quant_act_i8's optional third output: group-major per-row code sums an asymmetric
* weight grid subtracts to remove the stored zero point. */
static const RadOperandSpec oQuant[] = { OPD("x"), OUT("q"), OUT("scale"), OUT_O("asum") };
static const RadOperandSpec oHadQuant[] = { OPD("x"), OUT("q"), OUT("scale") };
/* No `asum`: it exists for asymmetric integer grids and fp8 is symmetric (as libr4d/r4d_rows.cpp
* notes). */
static const RadOperandSpec oQuantFp8[] = { OPD("x"), OUT("q"), OUT("scale") };
static const RadParamSpec pQuantCols[] = { P_INT("M"), P_INT("n"), P_STR("dtype") };
static const RadOperandSpec oQuantCols[] = { OPD("x"), OUT("y"), OUT("scale") };
/* ParoQuant's rotation, one per part, to f16 for the 16-bit GEMMs. The tables are the "givens"
* encoding's (abi/rad_encoding.h) and are weight operands, so they are placed with their weight. */
static const RadParamSpec pGivensRotate[] = { P_INT("M"), P_INT("n"), P_INT("group"),
P_STR("dtype"),
P_INT("stages"), P_INT("parts") };
static const RadParamSpec pGivensDense[] = { P_INT("M"), P_INT("n"), P_INT("group"), P_STR("dtype"),
P_INT("parts"), P_STR("out") };
static const RadOperandSpec oGivensDense[] = { OPD("x"), WGT("tdense"), WGT("rscale"), OUT("q"),
OUT("scale") };
static const RadOperandSpec oGivensRotate[] = { OPD("x"), WGT("pairs"), WGT("angle"), WGT("rscale"),
OUT("y") };
/* ---- the fp8 fusions ----------------------------------------------------------------------
* A fused op's operands are the unfused pair's minus the intermediate; optional `out_bf16` restores
* it (the codes are taken over its value) so the fused form can be compared against the pair. */
static const RadParamSpec pRmsQuantFp8[] = { P_INT("M"), P_INT("n"), P_F64("eps"),
P_INT("group"), P_STR("dtype"), P_F64O("wadd") };
static const RadOperandSpec oRmsQuantFp8[] = { OPD("x"), INOUT_O("residual"), WGT("w"),
OUT("q"), OUT("scale"), OUT_O("out_bf16") };
static const RadParamSpec pGatedQuantFp8[] = { P_INT("M"), P_INT("n"), P_INT("group"),
P_STR("dtype"), P_STRO("act") };
static const RadOperandSpec oGatedQuantFp8[] = { OPD("gate_up"), OUT("q"), OUT("scale"),
OUT_O("out_bf16") };
/* `act` is required here, unlike on the quantiser (docs/OPS.md; see ref_gemm_nt_q_gated). `N` is
* the weight's rows, twice the fused width, so the output is [M, N/2]. */
static const RadParamSpec pGemmQGated[] = { P_INT("M"), P_INT("N"), P_INT("K"),
P_INT("group"), P_STR("dtype"), P_STR("act") };
static const RadOperandSpec oGemmQGated[] = { OPD("a"), OPD("a_scale"), WGT("b"),
WGT("b_scale"), OUT("q"), OUT("scale"),
OUT_O("out_bf16") };
static const RadParamSpec pDflashSelect[] = { P_INT("M"), P_INT("steps"), P_INT("top_k"),
P_INT("rank"), P_INT("n_vocab"),
P_INTO("anchor_stride"), P_STR("dtype") };
static const RadParamSpec pDflashMarkov[] = { P_INT("M"), P_INT("steps"), P_INT("rank"),
P_INT("n_vocab"), P_INTO("anchor_stride"),
P_STR("dtype") };
static const RadOperandSpec oDflashSelect[] = { OPD("cand"), OPD("unary"), OPD_O("hp"),
OPD("anchor"), WGT("pred"), WGT("succ"),
OUT("tokens") };
static const RadOperandSpec oDflashMarkov[] = { OPD("logits"), OPD("anchor"), WGT("pred"),
WGT("succ"), OUT("tile_scores"),
OUT("tile_tokens"), OUT("tokens") };
static const RadParamSpec pDflashTree[] = { P_INT("M"), P_INT("steps"), P_INT("top_k"),
P_INT("rank"), P_INT("n_vocab"),
P_INTO("anchor_stride"), P_INT("nodes"),
P_F64O("temp"), P_STR("dtype") };
static const RadOperandSpec oDflashTree[] = { OPD("cand"), OPD("unary"), OPD_O("hp"),
OPD("anchor"), WGT("pred"), WGT("succ"),
OUT("tokens"), OUT("parents") };
static const RadParamSpec pRmsHadQuant[] = { P_INT("M"), P_INT("n"), P_F64("eps"), P_INT("group"),
P_STR("dtype"), P_F64O("wadd") };
static const RadOperandSpec oRmsHadQuant[] = { OPD("x"), INOUT_O("residual"), WGT("w"),
OUT("q"), OUT("scale"), OUT_O("out_bf16") };
/* Callers pass gate and up as two operands, or one fused [M, 2n] `gate_up` as `a` with `b`
* omitted. `mode` is libr4d's optional integer spelling of `act`. */
static const RadParamSpec pGatedQuant[] = { P_INT("M"), P_INT("n"), P_INT("group"), P_STR("dtype"),
P_STRO("act"), P_INTO("mode") };
static const RadOperandSpec oGatedQuant[] = { OPD("a"), OPD_O("b"), OUT("q"), OUT("scale") };
/* The SiTU caps are optional: absent or 0 is no softcap on that side. */
static const RadParamSpec pGatedQuantCap[] = { P_INT("M"), P_INT("n"), P_INT("group"),
P_STR("dtype"),
P_STRO("act"), P_INTO("mode"), P_F64O("gate_cap"),
P_F64O("up_cap") };
/* `h` is INOUT: the caller zeroes it once and each launch over the calibration corpus adds its
* block. `a`/`a_scale` are gemm_nt_q's first two operands, the activation this op characterises;
* `a_scale` is absent for a plain bf16 activation. */
static const RadOperandSpec oGram[] = { OPD("a"), OPD_O("a_scale"), INOUT("h") };
static const RadParamSpec pDequant[] = { P_INT("M"), P_INT("n"), P_INT("group"), P_STR("dtype") };
static const RadOperandSpec oDequant[] = { OPD("q"), OPD("scale"), OUT("y") };
/* ---- gemm --------------------------------------------------------------------------------- */
static const RadParamSpec pGemm[] = { P_INT("M"), P_INT("N"), P_INT("K"), P_STR("dtype") };
static const RadOperandSpec oGemm[] = { OPD("a"), WGT("b"), OUT("y") };
/* The bias form carries an optional epilogue: an activation of the biased product, then a residual
* add, each rounded to the output dtype as separate tensors would be. */
static const RadParamSpec pGemmBias[] = { P_INT("M"), P_INT("N"), P_INT("K"), P_STR("dtype"),
P_STRO("act") };
static const RadOperandSpec oGemmBias[] = { OPD("a"), WGT("b"), WGT("bias"), OPD_O("res"),
OUT("y") };
/* `split_n` and `w_rb` are libr4d's (docs/OPS.md): ref never splits K and reads the per-row
* scale plane as it is, so it accepts and ignores both. */
static const RadParamSpec pGemmQ[] = { P_INT("M"), P_INT("N"), P_INT("K"), P_INT("group"),
P_STR("dtype"), P_INTO("pb1"), P_INTO("pb2"),
P_INTO("split_n"), P_INTO("w_rb") };
/* The bias form has no parts (`pb1`/`pb2`); only the plain product supports them. */
static const RadParamSpec pGemmQBias[] = { P_INT("M"), P_INT("N"), P_INT("K"), P_INT("group"),
P_STR("dtype") };
/* `a_sum` and `b_ref` are libr4d's, appended after `y` so docs/OPS.md's list stays a positional
* prefix: asymmetric 4- and 2-bit grids subtract the activation's per-group code sums, and mxfp4
* folds a per-output-row reference exponent. Neither fits a weight aux stream (RadLayout can
* declare one but RadArgs cannot deliver it). Ref reads no zero point, so it refuses `a_sum`;
* `b_ref` is derived from `b_scale`, so ref accepts it beside an e2m1 weight and ignores it. */
static const RadOperandSpec oGemmQ[] = { OPD("a"), OPD_O("a_scale"), WGT("b"), WGT("b_scale"),
OUT("y"), OPD_O("a_sum"), WGT_O("b_ref") };
static const RadOperandSpec oGemmQBias[] = { OPD("a"), OPD("a_scale"), WGT("b"), WGT("b_scale"),
WGT("bias"), OUT("y") };
/* Runtime LoRA, the two halves of y += scale * (B . (A . x)) at a trunk GEMM, per row: `slot` names
* each row's adapter and a row outside [0, S) is a base row neither op touches. `a`/`a_scale` are
* the GEMM's own first two operands, read as gemm_nt_q reads them, and `y` is its output. The pools
* (`lora_a` [S, R, K], `lora_b` [S, N, R], `scale` [S, N]) are inputs, not weights: the engine owns
* them as program buffers and copies an adapter into a slot between steps. `group` is optional, 0
* meaning no scale stream; `dtype` is the tensor dtype name (rad_dtype_name) of `a` for shrink and
* of `y` for expand. */
static const RadParamSpec pLoraShrink[] = { P_INT("M"), P_INT("K"), P_INT("R"), P_INT("S"),
P_INTO("group"), P_STR("dtype") };
static const RadOperandSpec oLoraShrink[] = { OPD("a"), OPD_O("a_scale"), OPD("slot"),
OPD("lora_a"), OUT("t") };
static const RadParamSpec pLoraExpand[] = { P_INT("M"), P_INT("N"), P_INT("R"), P_INT("S"),
P_STR("dtype") };
static const RadOperandSpec oLoraExpand[] = { OPD("t"), OPD("slot"), OPD("lora_b"), OPD("scale"),
INOUT("y") };
/* ---- attention ---------------------------------------------------------------------------- */
static const RadParamSpec pAttnPaged[] = {
P_INT("q_len"), P_INT("head_dim"), P_INT("gqa"), P_INTD("block_size"), P_INT("causal"),
P_INT("window"), P_STR("q_dtype"), P_STR("kv_dtype"), P_F64O("scale"), P_INTO("max_ctx"),
/* For libr4d's scratch hook, which sizes its split-KV partial buffer at declare
* (num_seqs * q_len * q_heads * splits rows; gqa is only the head ratio). Ref ignores them;
* they are here because both plugins must agree on the parameter list. */
P_INTO("n_head"), P_INTO("max_seqs"),
/* The e4m3 products a caller accepts (docs/OPS.md). A permission, not a request: ref computes
* the exact form whatever it says. */
P_INTO("prefill_fp8")
};
static const RadOperandSpec oAttnPaged[] = {
OPD("q"), OPD("kv_cache"), OPD("block_table"), OPD("seqused"),
OPD_O("k_descale"), OPD_O("v_descale"), OPD_O("cu_seqlens"), OUT("out")
};
/* attn_paged with the verify block's tree (RadBatch::spec_parent, rad_drafter_tree). */
static const RadOperandSpec oAttnPagedTree[] = {
OPD("q"), OPD("kv_cache"), OPD("block_table"), OPD("seqused"),
OPD_O("k_descale"), OPD_O("v_descale"), OPD_O("cu_seqlens"), OUT("out"), OPD("spec_parent")
};
/* attn_paged with a learned sink logit a query head (gpt-oss, LiLiCorr's drafter). A name of its
* own, so no attn_paged kernel that does not know the operand can be chosen for it. */
static const RadOperandSpec oAttnPagedSink[] = {
OPD("q"), OPD("kv_cache"), OPD("block_table"), OPD("seqused"),
OPD_O("k_descale"), OPD_O("v_descale"), OPD_O("cu_seqlens"), WGT("sinks"), OUT("out")
};
/* Inkling 2's attention: attn_paged's parameters, the bias's two widths. */
static const RadParamSpec pAttnPagedRel[] = {
P_INT("q_len"), P_INT("head_dim"), P_INT("gqa"), P_INTD("block_size"), P_INT("causal"),
P_INT("window"), P_STR("q_dtype"), P_STR("kv_dtype"), P_F64O("scale"), P_INTO("max_ctx"),
P_INTO("n_head"), P_INTO("max_seqs"), P_INTO("prefill_fp8"),
P_INT("d_rel"), P_INT("extent")
};
/* attn_paged with the relative-position bias r . rel_proj[:, d] on each score and one sink logit a
* query row and head carries (Inkling 2's attention; ref_attn.cpp's ref_attn_paged_rel). */
static const RadOperandSpec oAttnPagedRel[] = {
OPD("q"), OPD("kv_cache"), OPD("block_table"), OPD("seqused"),
OPD_O("k_descale"), OPD_O("v_descale"), OPD_O("cu_seqlens"),
OPD("r"), WGT("rel_proj"), OPD("sink"), OUT("out")
};
/* Effective on kv_store, where `block_size` is RAD_REQUIRED: admits every value and refuses only a
* query that omits it. On attn_paged the key is RAD_DERIVED and the constraint is skipped
* (core/plugin/select.cpp). See the file header for the spec §7.2 consequence. */
static const RadConstraint cPagedBlock[] = { RAD_CGE("block_size", 1) };
static const RadParamSpec pAttnDense[] = { P_INT("M"), P_INT("head_dim"), P_INT("gqa"),
P_INT("causal"), P_STR("dtype"), P_F64O("scale"),
P_INTO("max_seqlen"), P_INTO("window"),
P_INTO("cu_div"), P_INTO("block"),
P_INTO("window_right") };
static const RadOperandSpec oAttnDense[] = { OPD("q"), OPD("k"), OPD("v"), OPD("cu_seqlens"),
OUT("out") };
static const RadParamSpec pAttnUmt5[] = { P_INT("M"), P_INT("heads"), P_STR("dtype") };
static const RadOperandSpec oAttnUmt5[] = {
OPD("q"), OPD("k"), OPD("v"), OPD("cu_seqlens"), WGT("bias"), OUT("out"),
};
static const RadParamSpec pAttnCross[] = { P_INT("M"), P_INT("head_dim"), P_INT("gqa"),
P_INT("causal"), P_STR("dtype"), P_F64O("scale") };
static const RadOperandSpec oAttnCross[] = { OPD("q"), OPD("k"), OPD("v"), OPD("cu_seqlens"),
OPD("cu_k"), OUT("out") };
static const RadParamSpec pAttnNa3d[] = { P_INT("M"), P_INT("head_dim"), P_INT("kt"), P_INT("kh"),
P_INT("kw"), P_STR("dtype"), P_F64O("scale") };
static const RadOperandSpec oAttnNa3d[] = { OPD("q"), OPD("k"), OPD("v"), OPD("geom"), OUT("out") };
/* The SAM tower's attention. `q`, `k` and `v` are the three column slices of one fused projection,
* so each is read at its own row pitch. The two rel_pos tables are weights: [2*L-1, head_dim] with
* L the window (or the grid for a global layer), resampled along dim 0 when a geometry needs it.
* `grid_h` x `grid_w` rows a segment, `window` 0 for a whole-segment tile. */
static const RadParamSpec pSamAttn[] = { P_INT("M"), P_INT("head_dim"), P_INT("n_head"),
P_INT("grid_h"), P_INT("grid_w"), P_INT("window"),
P_F64("scale"), P_STR("dtype") };
static const RadOperandSpec oSamAttn[] = { OPD("q"), OPD("k"), OPD("v"), OPD("cu_seqlens"),
OUT("out"), WGT("rel_pos_h"), WGT("rel_pos_w") };
/* `k_scale` and `v_scale` are the descales an fp8 cache is read back with, so a store divides by
* them; optional because a bf16 cache has none. */
static const RadParamSpec pKvStore[] = { P_INT("M"), P_INT("head_dim"), P_INT("n_head_kv"),
P_INT("block_size"), P_STR("kv_dtype"),
P_F64O("k_scale"), P_F64O("v_scale") };
static const RadOperandSpec oKvStore[] = { OPD("k"), OPD("v"), OPD("slot_mapping"),
INOUT("kv_cache") };
/* The streamed store: kv_store's parameters, plus the authoritative mirror `host` (always
* written) and the residency map `res` (read, never written). */
static const RadParamSpec pKvStoreStreamed[] = {
P_INT("M"), P_INT("head_dim"), P_INT("n_head_kv"), P_INT("block_size"), P_STR("kv_dtype"),
P_F64O("k_scale"), P_F64O("v_scale")
};
static const RadOperandSpec oKvStoreStreamed[] = { OPD("k"), OPD("v"), OPD("slot_mapping"),
INOUT("kv_cache"), OPD("res"),
INOUT("host") };
static const RadParamSpec pRope[] = { P_INT("M"), P_INT("head_dim"), P_INT("n_head"),
P_INT("n_head_kv"), P_F64("theta"), P_F64("scale"),
P_STR("mode"), P_INTO("rotary_dim"), P_STRO("sections"),
P_F64O("yarn_factor"), P_F64O("yarn_low"),
P_F64O("yarn_high"), P_F64O("yarn_magnitude"),
P_STRO("ladder"), P_F64O("llama3_factor"),
P_F64O("llama3_low"), P_F64O("llama3_high"),
P_F64O("llama3_orig") };
static const RadOperandSpec oRope[] = { INOUT("qkv"), OPD("positions") };
static const RadParamSpec pPositionScale[] = { P_INT("M"), P_INT("n"), P_INT("original"),
P_F64("beta"), P_STR("dtype") };
static const RadOperandSpec oPositionScale[] = { INOUT("x"), OPD("positions") };
static const RadParamSpec pEmbedAttn[] = { P_INT("M"), P_INT("head_dim"), P_INT("gqa"),
P_INT("causal"), P_INTO("max_seqlen"), P_STR("dtype"), P_F64O("scale") };
static const RadOperandSpec oEmbedAttn[] = { OPD("q"), OPD("k"), OPD("v"), OPD("cu_seqlens"),
OUT("out") };
static const RadParamSpec pMinistralRope[] = { P_INT("M"), P_INT("head_dim"), P_INT("n_head"),
P_INT("n_head_kv"), P_F64("theta"), P_STR("dtype"), P_F64O("yarn_factor"),
P_F64O("yarn_low"), P_F64O("yarn_high"), P_F64O("yarn_magnitude") };
static const RadOperandSpec oMinistralRope[] = { INOUT("qkv"), OPD("pos") };
static const RadParamSpec pPixtralRope[] = { P_INT("M"), P_INT("head_dim"), P_INT("n_head"),
P_F64("theta"), P_STR("dtype") };
static const RadOperandSpec oPixtralRope[] = { INOUT("qkv"), OPD("coord") };
static const RadParamSpec pPixtralMerge[] = { P_INT("M"), P_INT("n"), P_INT("merge"),
P_STR("dtype") };
static const RadOperandSpec oPixtralMerge[] = { OPD("x"), OPD("coord"), OUT("y") };
/* DeepSeek-V4.1's own tower geometry (ref_pixtral.cpp): a rotary where BOTH axes ride one ladder,
* and the aligner's zero-padded 3x3 pack. Neither is pixtral_rope/pixtral_merge: the frequencies
* differ and the pack is 3x3 zero-padded over a raster grid, not channel-major 2x2 blocks. */
static const RadParamSpec pDs41VitRope[] = { P_INT("M"), P_INT("head_dim"), P_INT("n_head"),
P_F64("theta"), P_STR("dtype") };
static const RadOperandSpec oDs41VitRope[] = { INOUT("qkv"), OPD("coord") };
static const RadParamSpec pDs41Pack[] = { P_INT("M"), P_INT("n"), P_INT("merge"), P_STR("dtype") };
static const RadOperandSpec oDs41Pack[] = { OPD("x"), OPD("coord"), OUT("y") };
/* ds41_span_rows (ref_pixtral.cpp): the learned rows an image span's typed positions take. Row 0
* start, 2 newline, 3 end; row 1 is the encoder's own rows and stays untouched. A copy, no math. */
static const RadParamSpec pDs41SpanRows[] = { P_INT("n"), P_STR("dtype") };
static const RadOperandSpec oDs41SpanRows[] = { WGT("start"), WGT("nl"), WGT("end"),
OUT("span_rows") };
/* The cos/sin plane a fused prologue reads instead of theta, at each position's own row of a
* [max_ctx, rot] table. `rot`, `theta` and `scale` are spelled as on `rope`, which must compute the
* same angles. */
static const RadParamSpec pRopeTable[] = { P_INT("M"), P_INT("rot"), P_F64("theta"),
P_F64("scale"), P_STR("dtype"),
P_F64O("yarn_factor"), P_F64O("yarn_low"),
P_F64O("yarn_high"), P_F64O("yarn_magnitude"),
P_INTO("head_dim"), P_STRO("ladder") };
static const RadOperandSpec oRopeTable[] = { OPD("positions"), OUT("cos_sin") };
static const RadParamSpec pRopeRows[] = { P_INT("M"), P_INT("n_head"), P_INT("head_dim"),
P_INT("rot"), P_INT("off"), P_STR("mode"), P_INT("inverse"), P_STR("dtype") };
static const RadOperandSpec oRopeRows[] = { INOUT("x"), OPD("cos_sin"), OPD("positions") };
static const RadParamSpec pQuantRoundtrip[] = { P_INT("M"), P_INT("n"), P_INT("block"),
P_STR("format"), P_STR("scale"), P_STR("dtype") };
static const RadOperandSpec oQuantRoundtrip[] = { INOUT("x") };
static const RadParamSpec pSoftmaxPool[] = { P_INT("M"), P_INT("n"), P_INT("ratio"),
P_STR("dtype") };
static const RadOperandSpec oSoftmaxPool[] = { OPD("stage"), OUT("out"), OPD_O("page") };
static const RadParamSpec pAttnSparseSink[] = { P_INT("M"), P_INT("heads"), P_INT("dim"),
P_INT("window"), P_INT("ratio"), P_INT("topk"), P_F64("scale"), P_STR("dtype"),
P_STRO("latent_format") };
static const RadOperandSpec oAttnSparseSink[] = { OPD("q"), OPD("window_cache"), OPD("window_bt"),
OPD("window_used"), OPD_O("comp_cache"), OPD_O("comp_bt"), OPD_O("sel"), OPD("positions"),
OPD("cu_seqlens"),
WGT("sinks"), OUT("out") };
static const RadParamSpec pIndexScore[] = { P_INT("M"), P_INT("heads"), P_INT("dim"),
P_INT("ratio"), P_INT("width"), P_F64("wscale"), P_STR("dtype"), P_INTO("row_part"),
P_STRO("key_format") };
static const RadOperandSpec oIndexScore[] = { OPD("q"), OPD("w"), OPD("index_cache"), OPD("bt"),
OPD("positions"), OPD("cu_seqlens"), OUT("score") };
static const RadParamSpec pIndexCand[] = { P_INT("M"), P_INT("width"), P_INT("block"),
P_INT("topk_blocks"), P_INT("ratio") };
static const RadOperandSpec oIndexCand[] = { OPD("score"), OPD("positions"), OUT("keep") };
static const RadParamSpec pTokenMap[] = { P_INT("M"), P_INT("n_vocab") };
static const RadOperandSpec oTokenMap[] = { OPD("ids"), WGT("table"), OUT("mapped") };
static const RadParamSpec pStreamGate[] = { P_INT("M"), P_INT("n"), P_INT("hc"), P_F64("eps"),
P_F64("gate_eps"), P_STR("dtype") };
static const RadOperandSpec oStreamGate[] = { OPD("h"), OPD("k"), OPD("v"), WGT("w"), OUT("out"),
OPD_O("mask") };
static const RadParamSpec pEntryStore[] = { P_INT("M"), P_INT("n"), P_INT("block_size"),
P_STR("dtype"),
P_STRO("format") };
static const RadOperandSpec oEntryStore[] = { OPD("x"), OPD("page"), INOUT("cache") };
static const RadParamSpec pIndexTopk[] = { P_INT("M"), P_INT("width"), P_INT("topk"),
P_INT("ratio"),
P_INTO("block") };
static const RadOperandSpec oIndexTopk[] = { OPD("score"), OPD_O("keep"), OPD("positions"),
OUT("sel") };
/* QSA's compressed key, libr4d's schema exactly (libr4d/r4d_rows.cpp): `block_size` is `ratio`
* spelled for the block-key store's KV group; `page` and `pos` are optional. */
static const RadParamSpec pQsaBlockKey[] = { P_INT("M"), P_INT("n"), P_INT("ratio"),
P_INT("rotary_dim"), P_INTO("pos0"),
P_INTO("block_size"), P_F64("eps"), P_F64("theta"),
P_F64O("scale"), P_F64O("wadd"), P_STR("dtype"),
P_STRO("mode"), P_STRO("sections"),
P_F64O("yarn_factor"), P_F64O("yarn_low"),
P_F64O("yarn_high"), P_F64O("yarn_magnitude") };
static const RadOperandSpec oQsaBlockKey[] = { OPD("k"), WGT("w"), OPD_O("page"), OPD_O("pos"),
OUT("out") };
/* The rest of the QSA indexer, libr4d's schemas exactly (libr4d/r4d_rows.cpp). `M` counts query
* rows (score, select) or tokens (tail store, work list); `cu` maps a row to its sequence, whose
* count is cu's length less one (`seqs` is the declared height). */
static const RadParamSpec pQsaScore[] = { P_INT("M"), P_INT("n"), P_INT("heads"), P_INT("blocks"),
P_INT("seqs"), P_STR("dtype") };
static const RadOperandSpec oQsaScore[] = { OPD("q"), OPD("bk"), OPD("bt"), OPD("cu"), OPD("nc"),
OUT("score") };
static const RadParamSpec pQsaSelect[] = { P_INT("M"), P_INT("blocks"), P_INT("topk"),
P_INT("ratio"), P_INT("seqs"), P_INTO("signed") };
static const RadOperandSpec oQsaSelect[] = { OPD("score"), OPD("bt"), OPD("nc"), OPD("pos"),
OPD("cu"), OUT("sel"), OUT("nsel"), OUT("seqused") };
static const RadParamSpec pQsaTailStore[] = { P_INT("M"), P_INT("n"), P_INT("ratio"), P_INT("ring"),
P_INT("seqs"), P_STR("dtype") };
static const RadOperandSpec oQsaTailStore[] = { OPD("k"), OPD("pos"), OPD("cu"), INOUT("state"),
OPD("sidx") };
/* `work` bounds the blocks that can complete; slots past the real count get page -1. */
static const RadParamSpec pQsaWork[] = { P_INT("M"), P_INT("n"), P_INT("ratio"), P_INT("ring"),
P_INT("seqs"), P_CAP("work"), P_STR("dtype") };
static const RadOperandSpec oQsaWork[] = { OPD("k"), OPD("pos"), OPD("cu"), OPD("state"),
OPD("sidx"), OPD("bt"), OPD_O("rope"), OUT("stage"),
OUT("page"), OUT("bpos"), OUT("nc") };
/* The residency map of a streamed group (ref_kvstream.cpp says why it takes no `block_size`). */
static const RadParamSpec pKvResolve[] = { P_INT("M"), P_INT("blocks"), P_INT("slots"),
P_INT("sel_pitch"), P_INT("block_bytes") };
static const RadOperandSpec oKvResolve[] = { INOUT("sel"), OPD("nsel"), INOUT("res"),
INOUT("slot_block"), INOUT("slot_state"),
INOUT("ctl"), OPD("host"), INOUT("kv_cache") };
/* DFlash2's grouped convolution, libr4d's schema exactly. */
static const RadParamSpec pDflashConv[] = { P_INT("T"), P_INT("hidden_size"), P_INT("taps"),
P_INT("group_size"), P_INT("NG"), P_INT("block_size"),
P_STR("dtype") };
static const RadOperandSpec oDflashConv[] = { OPD("x"), OPD("delta"), WGT("base"), OUT("out") };
/* `mode` "blocks" rotates each of `sections` (pair counts) as its own neox block of channels,
* reading the position component `comps` names for it; positions are then [C, M]. */
static const RadParamSpec pQkNormRope[] = { P_INT("M"), P_INT("head_dim"), P_INT("n_head"),
P_INT("n_head_kv"), P_F64("theta"), P_F64("eps"),
P_F64O("scale"), P_STRO("mode"), P_INTO("rotary_dim"),
P_F64O("wadd"), P_INTO("v_norm"), P_STRO("sections"),
P_STRO("comps") };
/* `positions` may be absent at `rotary_dim` 0 (the norm alone), and an absent gain is a unit
* one. */
static const RadOperandSpec oQkNormRope[] = { INOUT("qkv"), OPD_O("positions"), WGT_O("q_w"),
WGT_O("k_w") };
/* ---- mixture of experts ------------------------------------------------------------------- */
static const RadParamSpec pScaleRows[] = { P_INT("M"), P_INT("n"), P_STR("act"),
P_STR("dtype") };
static const RadOperandSpec oScaleRows[] = { OPD("x"), OPD("s"), OPD_O("add"), OUT("y") };
static const RadParamSpec pL2Clip[] = { P_INT("M"), P_INT("n"), P_INT("group"), P_F64("threshold"),
P_STR("dtype"), P_F64O("floor"), P_F64O("alpha") };
static const RadOperandSpec oL2Clip[] = { OPD("x"), WGT_O("w"), OPD_O("positions"), OUT("y") };
static const RadParamSpec pRouter[] = { P_INT("M"), P_INT("n_expert"), P_INT("top_k"),
P_INT("norm"), P_STR("dtype") };
static const RadOperandSpec oRouter[] = { OPD("logits"), OUT("expert_ids"), OUT("expert_w") };
/* `expert_base` is this rank's first expert id, making expert parallelism a parameter rather than
* a second op; absent (0) is the unsharded case. */
static const RadParamSpec pScatter[] = { P_INT("M"), P_INT("n_expert"), P_INT("top_k"),
P_INTO("expert_base"),
P_STRO("protect") };
static const RadOperandSpec oScatter[] = { OPD("expert_ids"), OUT("sorted_tok"),
OUT("expert_offset"), OUT("expert_count") };
/* The pair as one op: the union of the two parameter lists, and their operands with the shared
* `expert_ids` named once, as an output. Same schema as libr4d's fused row. */
static const RadParamSpec pRouterTopkScatter[] = { P_INT("M"), P_INT("n_expert"),
P_INTO("n_local"), P_INT("top_k"),
P_INT("norm"), P_STR("dtype"),
P_INTO("expert_base"),
P_STRO("protect") };
static const RadOperandSpec oRouterTopkScatter[] = { OPD("logits"), OUT("expert_ids"),
OUT("expert_w"), OUT("sorted_tok"),
OUT("expert_offset"),
OUT("expert_count") };
/* The router GEMM in front of the pair, and the gate's one-column GEMM beside it: the union of
* the three operand lists with `logits` named once, as the GEMM's output. */
static const RadParamSpec pRouterGemmTopkScatter[] = { P_INT("M"), P_INT("K"),
P_INT("n_expert"), P_INTO("n_local"),
P_INT("top_k"), P_INT("norm"),
P_STR("dtype"), P_INTO("expert_base"),
P_STRO("protect") };
static const RadOperandSpec oRouterGemmTopkScatter[] = {
OPD("a"), WGT("b"), WGT_O("gate_b"), OUT("logits"), OUT("expert_ids"), OUT("expert_w"),
OUT("sorted_tok"), OUT("expert_offset"), OUT("expert_count"), OUT_O("gate") };
/* `a_order` says which rows the activation has; a routed block's two grouped GEMMs differ. gate_up
* reads the block input, one row per token, so sorted row i gathers `a[sorted_tok[i] / top_k]`.
* down reads gate_up's output, already one row per (token, slot) in scatter order, so row i is row
* i.
*
* It is stated rather than derived from the row count (M vs M*top_k): an operand whose meaning
* changes with its extent is invisible to a checker, since a harness sizing it for one reading
* never exercises the other. */
static const RadParamSpec pMoeGemm[] = { P_INT("M"), P_INT("N"), P_INT("K"), P_INT("n_expert"),
P_INT("top_k"), P_STR("a_order"), P_STR("dtype") };
static const RadOperandSpec oMoeGemm[] = { OPD("a"), WTAB("w"), OPD("sorted_tok"),
OPD("expert_offset"), OUT("y") };
/* The quantised arm is a separate op, like gemm_nt_q, because it adds operands (an activation
* scale stream and a per-expert weight scale plane) and positional operands cannot vary with a
* dtype string. `group` is the block edge, 128 for DeepSeek-style fp8 checkpoints. Both weight
* operands are tables with one entry per expert.
*
* Experts are two tables by parity: `w`/`w_scale` the even ones at [N, K], `w_odd`/`w_odd_scale`
* the odd ones at [N_odd, K_odd], expert e at entry e / 2. A table's entries share one geometry,
* and a rank holding an uneven slice of each expert's width has two. `parts` stacked parts run
* along both weight rows and output columns; the output is the wider class's width, and a narrower
* class fills each part's leading columns and zeros the rest (as in libr4d's schema). */
static const RadParamSpec pMoeGemmQ[] = { P_INT("M"), P_INT("N"), P_INT("K"), P_INT("n_expert"),
P_INT("top_k"), P_INT("group"), P_STR("a_order"),
P_STR("dtype"), P_INTO("N_odd"), P_INTO("K_odd"),
P_INTO("parts"), P_INTO("a_group") };
static const RadOperandSpec oMoeGemmQ[] = { OPD("a"), OPD_O("a_scale"), WTAB("w"), WTAB("w_scale"),
OPD("sorted_tok"), OPD("expert_offset"),
WTAB("w_odd"), WTAB("w_odd_scale"), OUT("y"),
OPD_O("split") };
/* The same op over four classes by expert number mod 4: `w`/`w_scale` class 0 at [N, K],
* `w_q`/`w_q_scale` class q at [N_q, K_q], expert e at entry e / 4 of class e % 4; how four ranks
* share the extra block of a five-block expert (as in libr4d's schema). */
static const RadParamSpec pMoeGemmQMod4[] = { P_INT("M"), P_INT("N"), P_INT("K"), P_INT("n_expert"),
P_INT("top_k"), P_INT("group"), P_STR("a_order"),
P_STR("dtype"), P_INTO("N_1"), P_INTO("K_1"),
P_INTO("N_2"), P_INTO("K_2"), P_INTO("N_3"),
P_INTO("K_3"), P_INTO("parts"), P_INTO("a_group") };
static const RadOperandSpec oMoeGemmQMod4[] = { OPD("a"), OPD_O("a_scale"), WTAB("w"),
WTAB("w_scale"), OPD("sorted_tok"),
OPD("expert_offset"), WTAB("w_1"),
WTAB("w_1_scale"), WTAB("w_2"), WTAB("w_2_scale"),
WTAB("w_3"), WTAB("w_3_scale"), OUT("y"),
OPD_O("split") };
/* With `shared` and `shared_gate`, the shared expert is folded in: y = bf16(shared * act(gate)) +
* bf16(gathered), scale_rows' fused gate-and-add. `act` (sigmoid or none) applies to the scalar. */
static const RadParamSpec pMoeGather[] = { P_INT("M"), P_INT("n"), P_INT("top_k"),
P_STR("dtype"), P_STRO("act") };
static const RadOperandSpec oMoeGather[] = { OPD("y_expert"), OPD("expert_w"), OPD("sorted_tok"),
OPD_O("shared"), OPD_O("shared_gate"), OUT("y") };
/* `vocab_off` and `pairs` are the vocab-parallel form: `x` is one rank's slice, `idx` carries
* global columns, and `pairs` is the [M, R, 2] (int32 id, float value) plane an all_gather moves.
* Must match libr4d's schema, or the loader refuses ref and the model fails to load. */
static const RadParamSpec pRowTopk[] = { P_INT("M"), P_INT("N"), P_INT("R"), P_INTO("vocab_off"),
P_STR("dtype") };
static const RadOperandSpec oRowTopk[] = { OPD("x"), OUT("idx"), OUT("val"), OUT_O("pairs") };
static const RadParamSpec pRowTopkMerge[] = { P_INT("M"), P_INT("R"), P_INT("world_size"),
P_STR("dtype") };
static const RadOperandSpec oRowTopkMerge[] = { OPD("gathered"), OUT("idx"), OUT("val") };
static const RadParamSpec pDequantW4[] = { P_INT("N"), P_INT("K"), P_INT("group"), P_STR("dtype"),
P_INT("twos") };
static const RadOperandSpec oDequantW4[] = { WGT("wq"), WGT("ws"), OUT("y") };
static const RadParamSpec pPairArgmax[] = { P_INT("M"), P_INT("n"), P_STR("dtype") };
static const RadOperandSpec oPairArgmax[] = { OPD("pairs"), OUT("idx"), OUT_O("val") };
static const RadParamSpec pLogitRerank[] = { P_INT("M"), P_INT("R"), P_INT("n_vocab"),
P_INT("n_embd"), P_INTO("vocab_off"), P_STR("dtype") };
static const RadOperandSpec oLogitRerank[] = { OPD("x"), WGT("head"), OPD("idx_in"),
OUT("idx"), OUT("val"), OUT_O("pairs") };
/* ---- gated delta net ----------------------------------------------------------------------
*
* `cu` is an operand of every GDN op, decode included: the fast decode kernels read
* `bos = cu[n]; T = cu[n+1] - bos` before any null test. `num_accepted` is required on both decode
* ops (spec §10): the state has absorbed the rejected tokens, so a rejection is a change of read
* offset. A non-speculative decode passes all ones. */
static const RadParamSpec pConvPrep[] = { P_INT("M"), P_INT("head_k"), P_INT("head_v"),
P_CHUNK("chunk"), P_INT("conv_width"),
P_F64O("l2_eps"), P_F64O("softplus_thr") };
/* Inputs first, outputs last, as docs/OPS.md's resolved table draws. `a` and `b_gate` are separate
* gate and beta projections; absent, they are the last 2*head_v_count columns of `x`. `cache_idx`
* is the conv state slot per sequence (a paged cache is not indexed by batch position), and
* `has_init` says whether that slot holds history. */
static const RadOperandSpec oConvPrep[] = {
OPD("x"), WGT("w"), WGT_O("b"), WGT("A_log"), WGT("dt_bias"), INOUT("conv_state"), OPD("cu"),
OPD_O("a"), OPD_O("b_gate"), OPD_O("cache_idx"), OPD_O("has_init"),
OUT("q"), OUT("k"), OUT("v"), OUT("g"), OUT("beta")
};
static const RadParamSpec pConvUpdate[] = { P_INT("q_len"), P_INT("head_k"), P_INT("head_v"),
P_INT("conv_width"), P_INTO("max_query_len") };
static const RadOperandSpec oConvUpdate[] = {
OPD("x"), WGT("w"), WGT_O("b"), INOUT("conv_state"), OPD("state_idx"), OPD("num_accepted"),
OPD("cu"), OUT("q"), OUT("k"), OUT("v")
};
static const RadParamSpec pKkt[] = { P_INT("M"), P_INT("head_k"), P_CHUNK("chunk") };
static const RadOperandSpec oKkt[] = { OPD("k"), OPD("beta"), OPD("g"), OPD("cu"), OUT("A") };
static const RadParamSpec pChunkScan[] = { P_INT("M"), P_INT("head_k"), P_INT("head_v"),
P_CHUNK("chunk"), P_F64O("scale"),
P_INTO("state_fp16") };
static const RadOperandSpec oChunkScan[] = { OPD("q"), OPD("k"), OPD("v"), OPD("A"), OPD("g"),
OPD("beta"), OPD_O("h0"), OPD("cu"),
OUT("o"), OUT_O("ht"), OPD_O("state_idx") };
static const RadParamSpec pRecurrent[] = { P_INT("q_len"), P_INT("head_k"), P_INT("head_v"),
P_INTO("n_head_v"), P_F64O("scale"), P_F64O("eps"),
P_F64O("l2_eps"), P_F64O("softplus_thr"),
P_STRO("act"), P_INTO("state_fp16"),
P_STRO("state_form") };
static const RadOperandSpec oRecurrent[] = {
OPD("q"), OPD("k"), OPD("v"), OPD("a"), OPD("b"), WGT("A_log"), WGT("dt_bias"),
INOUT("state"), OPD("state_idx"), OPD("num_accepted"), OPD("cu"), OPD_O("z"),
WGT_O("norm_w"), OUT("o"), OUT_O("o_q"), OUT_O("o_scale")
};
/* gdn_conv_update folded into gdn_recurrent_update: the convolution's inputs, then the
* recurrence's minus the intermediate q, k and v. Parameters are the union of both ops'. */
static const RadParamSpec pConvRecurrent[] = { P_INT("q_len"), P_INT("head_k"), P_INT("head_v"),
P_INT("conv_width"), P_INTO("n_head_v"),
P_F64O("scale"), P_F64O("eps"), P_F64O("l2_eps"),
P_F64O("softplus_thr"), P_STRO("act"),
P_INTO("state_fp16"), P_STRO("state_form") };
static const RadOperandSpec oConvRecurrent[] = {
OPD("x"), WGT("conv_w"), WGT_O("conv_b"), INOUT("conv_state"), OPD("conv_idx"), OPD("a"),
OPD("b"), WGT("A_log"), WGT("dt_bias"), INOUT("state"), OPD("state_idx"),
OPD("num_accepted"), OPD("cu"), OPD("z"), WGT("norm_w"), OUT("o"), OUT_O("o_q"),
OUT_O("o_scale")
};
/* The decode trio over a tree verify (rad_drafter_tree): the chain op's operands, then the step's
* parents (RadBatch::spec_parent). */
static const RadOperandSpec oConvUpdateTree[] = {
OPD("x"), WGT("w"), WGT_O("b"), INOUT("conv_state"), OPD("state_idx"), OPD("num_accepted"),
OPD("cu"), OUT("q"), OUT("k"), OUT("v"), OPD("spec_parent")
};
static const RadOperandSpec oRecurrentTree[] = {
OPD("q"), OPD("k"), OPD("v"), OPD("a"), OPD("b"), WGT("A_log"), WGT("dt_bias"),
INOUT("state"), OPD("state_idx"), OPD("num_accepted"), OPD("cu"), OPD_O("z"),
WGT_O("norm_w"), OUT("o"), OUT_O("o_q"), OUT_O("o_scale"), OPD("spec_parent")
};
static const RadOperandSpec oConvRecurrentTree[] = {
OPD("x"), WGT("conv_w"), WGT_O("conv_b"), INOUT("conv_state"), OPD("conv_idx"), OPD("a"),
OPD("b"), WGT("A_log"), WGT("dt_bias"), INOUT("state"), OPD("state_idx"),
OPD("num_accepted"), OPD("cu"), OPD("z"), WGT("norm_w"), OUT("o"), OUT_O("o_q"),
OUT_O("o_scale"), OPD("spec_parent")
};
static const RadParamSpec pGatedNorm[] = { P_INT("M"), P_INT("channels"), P_F64("eps"),
P_STR("act") };
static const RadOperandSpec oGatedNorm[] = { OPD("x"), OPD("z"), WGT("w"), OUT("o") };
/* ---- collectives -------------------------------------------------------------------------- */
static const RadParamSpec pAllReduce[] = { P_INT("world_size"), P_INT("numel"), P_STR("dtype"),
P_INT("exact"), P_INTO("min_bytes"),
P_INTO("hops"), P_INTO("overlap") };
/* `y` is optional; absent means in place. libr4d's two-shot rows need it (see ref_all_reduce). */
static const RadOperandSpec oAllReduce[] = { INOUT("x"), OUT_O("y") };
static const RadParamSpec pAllGather[] = { P_INT("world_size"), P_INT("numel"), P_STR("dtype"),
P_INTO("row") };
static const RadOperandSpec oAllGather[] = { OPD("x"), OUT("y") };
/* ---- vocabulary edges --------------------------------------------------------------------- */
/* `vocab_offset` makes the embedding vocab-parallel, like lm_head. Absent (or 0 with an unsharded
* table) it is the plain gather.
*
* Present, `n_vocab` is this rank's row count (as for logits_gemm) and the table covers global ids
* [vocab_offset, vocab_offset + n_vocab). Ids stay global; an id outside the slice writes a zero
* row, as the negative padding id does. Each token lands on exactly one rank, so the following
* all_reduce reconstructs x exactly.
*
* Without it the table is replicated per card: 248320 x 5120 bf16 is 2.43 GiB (bf16 even in the
* fp8 checkpoint), memory taken from the KV cache. */
static const RadParamSpec pEmbed[] = { P_INT("M"), P_INT("n_embd"), P_INT("n_vocab"),
P_STR("dtype"), P_INTO("vocab_offset"),
/* embed_lookup_q's: the scale its layout quantises with;
* a parameter because a global scale needs the whole
* table and the transform sees one block. */
P_F64O("wscale") };
/* embed_lookup's list: pEmbed plus `mult`, the factor every gathered row is multiplied by (Gemma's
* sqrt(hidden)). embed_lookup_q has its table scale instead. */
static const RadParamSpec pEmbedM[] = { P_INT("M"), P_INT("n_embd"), P_INT("n_vocab"),
P_STR("dtype"), P_INTO("vocab_offset"),
P_F64O("wscale"), P_F64O("mult") };
static const RadOperandSpec oEmbed[] = { OPD("tokens"), WGT("wte"), OUT("x") };
/* A table's affine planes, read in place (ref_gemm.cpp): codes, a scale per group, a codebook. */
static const RadParamSpec pEmbedPlanes[] = { P_INT("M"), P_INT("n_embd"), P_INT("n_vocab"),
P_INT("group"), P_STR("dtype"), P_INTO("vocab_offset"),
P_F64O("mult") };
static const RadOperandSpec oEmbedPlanes[] = { OPD("tokens"), WGT("codes"), WGT("scale"),
WGT_O("table"), OUT("x") };
/* The quantised table, one scale for all of it; a separate op since it adds an operand. */
static const RadOperandSpec oEmbedQ[] = { OPD("tokens"), WGT("wte"), WGT_O("scale"), OUT("x"),
OPD_O("ahead") };
static const RadParamSpec pLogits[] = { P_INT("M"), P_INT("n_vocab"), P_INT("n_embd"),
P_STR("dtype") };
static const RadOperandSpec oLogits[] = { OPD("x"), WGT("lm_head"), OUT("logits") };
/* ---- sampling -------------------------------------------------------------------------------
*
* Every sampling scalar is per row and arrives as an operand. Parameters are geometry, matched and
* frozen at declare (spec §2.2), so `top_p` as a parameter would allow one value per deployment.
* `params` is [M] rows of RadSampleParams (abi/rad_sample.h), uploaded once per step.
*
* This is the schema core/sample/sampler.h issues against. It supersedes the one docs/OPS.md
* prints (`rep`/`freq`/`pres`/`k`/`p` as parameters, `seed`/`offset` as extra sample_pick
* operands); a plugin declaring that form cannot start, since declare refuses per-request values
* as missing geometry. */
static const RadParamSpec pMVocab[] = { P_INT("M"), P_INT("n_vocab") };
static const RadParamSpec pMCand[] = { P_INT("M"), P_INT("n_cand") };
/* The history stages read global token ids and write this rank's columns, so under a split
* vocabulary they take the rank's first row as `vocab_off` (as sample_topk does). Default 0. */
static const RadParamSpec pPenal[] = { P_INT("M"), P_INT("n_vocab"), P_INTO("vocab_off") };
/* `history` is [M, max_hist] int32 of global token ids; row m's window is addressed by row m's
* hist_off / hist_len within row m. */
static const RadOperandSpec oPenalties[] = { INOUT("logits"), OPD("params"), OPD("history") };
/* `breakers` is optional: [M, max_hist] u8 beside `history`, nonzero where that position's token
* is a whole DRY sequence breaker and never penalised. Absent, no token is exempt. */
static const RadOperandSpec oDry[] = { INOUT("logits"), OPD("params"), OPD("history"),
OPD_O("breakers") };
/* The windowed n-gram ban: penalties' operands plus a {n, window} pair a row.
* `whitelist` is optional: a bitmask plane [M, ceil(n_vocab/32)] like sample_mask's, a SET bit
* marking a token exempt from the ban (the reference's whitelist, removed from the banned set
* before the -inf writes). Absent, nothing is exempt. Rows are indexed by the sampled position,
* not by mask_row: every row the op bans reads its own words. */
static const RadOperandSpec oNgramBan[] = { INOUT("logits"), OPD("params"), OPD("history"),
OPD("ngram"), OPD_O("whitelist") };
static const RadOperandSpec oTemp[] = { INOUT("logits"), OPD("params") };
/* `bitmask` is [rows, ceil(n_vocab/32)] uint32, addressed by the row's mask_row -- which is -1 for
* an unconstrained sequence, so the plane is shared and shorter than M. */
static const RadOperandSpec oMask[] = { INOUT("logits"), OPD("params"), OPD("bitmask") };
/* `pairs` is the optional vocab-parallel output: [M, 2 * n_cand] u32 of this rank's candidates as
* (u32 global id, f32 logit), which sample_merge_topk reads after the all-gather. The kernel adds
* `vocab_off`, so the gather needs no shard layout. */
static const RadParamSpec pTopk[] = { P_INT("M"), P_INT("n_vocab"), P_INTO("vocab_off") };
static const RadOperandSpec oTopk[] = { OPD("logits"), OPD("params"),
OUT("cand_idx"), OUT("cand_val"), OUT_O("pairs") };
static const RadOperandSpec oArgmax[] = { OPD("logits"), OPD("params"), OUT("token") };
/* `stats` is [M, 2 + 2k]: max, sum of exp(l - max), then k (id, logit) pairs. `k` is geometry
* (the sampler declares one width every request's top_logprobs fits); `vocab_off` is as on
* sample_topk. */
static const RadParamSpec pLogprobs[] = { P_INT("M"), P_INT("n_vocab"), P_INT("k"),
P_INTO("vocab_off") };
static const RadOperandSpec oLogprobs[] = { OPD("logits"), OPD_O("params"), OUT("stats") };
static const RadOperandSpec oNarrow[] = { INOUT("cand_idx"), INOUT("cand_val"), OPD("params") };
static const RadOperandSpec oPick[] = { OPD("cand_idx"), OPD("cand_val"), OPD("params"),
OUT("token") };
/* `gathered` is [M, world_size * n_cand] (u32 id, f32 logit) pairs with global token ids, each rank
* having added its vocab offset before the gather. */
static const RadOperandSpec oMerge[] = { OPD("gathered"), OPD("params"),
OUT("cand_idx"), OUT("cand_val") };
/* ---- image generation (ref_gen.cpp) --------------------------------------------------------- */
static const RadParamSpec pLnMod[] = { P_INT("M"), P_INT("n"), P_F64("eps"), P_STR("dtype") };
static const RadOperandSpec oLnMod[] = { OPD("x"), OPD("s"), OPD("cu_seqlens"), OUT("y") };
/* `act` is the gate's activation, "tanh" (the default) or "none". */
static const RadParamSpec pTableAdd[] = { P_INT("M"), P_INT("n"), P_INT("L"), P_STR("dtype") };
static const RadOperandSpec oTableAdd[] = { OPD("x"), WGT("t"), OUT("y") };
static const RadParamSpec pMixRows[] = { P_INT("M"), P_INT("n"), P_INT("k"), P_STR("dtype") };
static const RadParamSpec pChanStats[] = { P_INT("M"), P_INT("n"), P_STR("mode"), P_INTO("var"),
P_F64O("eps"), P_STR("dtype") };
static const RadOperandSpec oChanStats[] = { OPD("x"), WGT("mean"), WGT("s"), OUT("y") };
static const RadOperandSpec oMixRows[] = { OPD("x"), WGT("w"), OUT("y") };
static const RadParamSpec pGateAdd[] = { P_INT("M"), P_INT("n"), P_STR("dtype"), P_STRO("act") };
static const RadOperandSpec oGateAdd[] = { OPD("y"), OPD("g"), OPD("cu_seqlens"), INOUT("x") };
static const RadParamSpec pLnModQ[] = { P_INT("M"), P_INT("n"), P_F64("eps"), P_INT("group"),
P_INT("gated"), P_STR("dtype") };
static const RadOperandSpec oLnModQ[] = { OPD_O("y"), OPD_O("g"), OPD("s"), OPD("cu_seqlens"),
INOUT("x"), OUT("q"), OUT("scale"), OUT_O("z") };
/* `s` is [n_seq, >= 2n], [shift | scale] in each row. */
static const RadParamSpec pRmsMod[] = { P_INT("M"), P_INT("n"), P_F64("eps"), P_INTO("gated"),
P_STR("dtype"), P_F64O("wadd"),
P_INTO("norm_once"), P_STRO("norm") };
static const RadOperandSpec oRmsMod[] = { OPD_O("y"), OPD_O("g"), OPD("s"), WGT_O("w"),
OPD("cu_seqlens"), INOUT("x"), OUT("z"),
OPD_O("colmap") };
static const RadParamSpec pRmsModQ[] = { P_INT("M"), P_INT("n"), P_F64("eps"), P_INT("group"),
P_INT("gated"), P_STR("dtype"), P_F64O("wadd"),
P_INTO("norm_once"), P_STRO("norm") };
static const RadOperandSpec oRmsModQ[] = { OPD_O("y"), OPD_O("g"), OPD("s"), WGT_O("w"),
OPD("cu_seqlens"), INOUT("x"), OUT("q"), OUT("scale"),
OUT_O("z"), OPD_O("colmap") };
/* `t`, `sigma` and `sigma_next` are one f32 value each. */
static const RadOperandSpec oFlowX0[] = { OPD("v"), OPD("t"), OPD("sigma"), OPD("sigma_next"),
INOUT("x") };
static const RadOperandSpec oFlowRM[] = { OPD("v"), INOUT("x0"), OPD("sigma"), OPD("r"),
OPD("hb1"), OPD("hb2"), OPD("order"), INOUT("x") };
static const RadParamSpec pTimestep[] = { P_INT("M"), P_INT("n"), P_F64("scale"),
P_F64("max_period"),
P_INTO("round"), P_F64O("clamp_min"), P_INTO("curve"),
P_STR("dtype") };
static const RadOperandSpec oTimestep[] = { OPD("t"), OUT("y"), WGT_O("table") };
static const RadOperandSpec oGather2[] = { OPD("a"), OPD("b"), OPD("idx"), OUT("y") };
static const RadParamSpec pLane[] = { P_INT("M"), P_INT("n"), P_INT("stride"), P_STR("dtype") };
static const RadOperandSpec oLaneGather[] = { OPD("src"), OPD("lane"), OPD("cu_seqlens"),
OUT("y") };
static const RadOperandSpec oLaneStore[] = { OPD("src"), OPD("lane"), INOUT("dst") };
static const RadOperandSpec oRandnLanes[] = { OPD("flags"), OPD("seed"), OPD("lane"),
OPD("cu_seqlens"), INOUT("dst") };
static const RadOperandSpec oFlowEuler[] = { OPD("v"), OPD("sigma"), OPD("sigma_next"), OPD("cfg"),
OPD("pair"), OPD("lane"), OPD("flags"), OPD("seed"),
OPD("cu_seqlens"), OPD_O("inp_lat"),
OPD_O("inp_mask"),
INOUT("state"), INOUT_O("preview") };
/* The k-diffusion sampler's input and step: `pred` "eps" or "v", the lanes f32. */
static const RadParamSpec pKdStep[] = { P_INT("M"), P_INT("n"), P_INT("stride"), P_STR("pred"),
P_STR("dtype") };
static const RadOperandSpec oKdInput[] = { OPD("sigma"), OPD("pair"), OPD("lane"), OPD("flags"),
OPD("seed"), OPD("cu_seqlens"), INOUT("state"),
OUT("y") };
static const RadOperandSpec oKdStep[] = { OPD("out"), OPD("sigma"), OPD("sigma_next"),
OPD("sigma_prev"), OPD("cfg"), OPD("pair"), OPD("lane"),
OPD("flags"), OPD("seed"), OPD("sampler"), OPD("step"),
OPD("cu_seqlens"), INOUT("state"), INOUT("hist"),
INOUT_O("preview") };
/* `round` 0 keeps the lists and the arithmetic in f32 (default 1, bf16 lists and roundings). */
static const RadParamSpec pChanAffine[] = { P_INT("M"), P_INT("n"), P_STR("mul"), P_STR("add"),
P_STR("mode"), P_STR("dtype"), P_INTO("round") };
static const RadParamSpec pConvNhwc[] = { P_INT("M"), P_INT("cin"), P_INT("cout"), P_INT("k"),
P_INT("stride"), P_STR("pad"), P_INT("up"),
P_STR("dtype") };
static const RadOperandSpec oConvNhwc[] = { OPD("x"), WGT("w"), WGT("b"), OUT("y") };
static const RadParamSpec pIm2col[] = { P_INT("M"), P_INT("cin"), P_INT("k"), P_INT("stride"),
P_STR("pad"), P_INT("up"), P_STR("dtype") };
static const RadParamSpec pL2Norm[] = {
P_INT("M"), P_INT("n"), P_STR("act"), P_STR("dtype"), P_INTO("round_length"),
};
static const RadOperandSpec oL2Norm[] = { OPD("x"), WGT("gamma"), OUT("y") };
static const RadParamSpec pSpaceDepth[] = { P_INT("M"), P_INT("cin"), P_INT("cout"), P_INT("ft"),
P_STR("dtype") };
static const RadOperandSpec oSpaceDepth[] = { OPD("x"), INOUT("y") };
static const RadParamSpec pAttnPS[] = { P_INT("q_len"), P_INT("head_dim"), P_INT("gqa"),
P_INTD("block_size"), P_INT("causal"), P_STR("q_dtype"),
P_STR("kv_dtype"), P_F64O("scale"), P_INTO("max_ctx"),
P_INTO("n_head"), P_INTO("max_seqs"), P_STRO("qk"),
P_STRO("pv") };
static const RadOperandSpec oAttnPS[] = { OPD("q"), OPD("k"), OPD("v"), OPD_O("kv_cache"),
OPD_O("block_table"), OPD_O("seqused"), OPD("cu_seqlens"),
OPD_O("k_descale"), OPD_O("v_descale"), OPD_O("k_i8"),
OPD_O("k_scale"), OUT("out"), OPD_O("v_scale") };
/* ---- video generation (ref_video.cpp). `geom` is the request's latent geometry, i32 on the
* device: (F, Hp, Wp) for patch_unfold, (frames, H, W) for vtile_gather and (frames, H, W, first,
* last) for vtile_stitch. */
static const RadParamSpec pPatchUnfold[] = { P_INT("M"), P_INT("C"), P_INT("pt"), P_INT("ph"),
P_INT("pw"), P_STR("mul"), P_STR("add"),
P_STR("dtype") };
static const RadOperandSpec oPatchUnfold[] = { OPD("x"), OPD("geom"), OUT("y") };
static const RadParamSpec pVtileGather[] = { P_INT("M"), P_INT("n"), P_INT("tile"),
P_INT("overlap"), P_INT("frames"), P_INT("period"),
P_INT("n_reg"), P_STR("dtype") };
static const RadOperandSpec oVtileGather[] = { OPD("x"), WGT("reg"), OPD("geom"), OUT("y") };
static const RadParamSpec pVtileStitch[] = { P_INT("M"), P_INT("tile"), P_INT("overlap"),
P_INT("scale"), P_INT("pt"), P_INT("skip"),
P_INT("keep"), P_INT("blend"), P_INT("period"),
P_STR("mean"), P_STR("std") };
static const RadOperandSpec oVtileStitch[] = { OPD("x"), OPD("geom"), INOUT("prev"), OUT("y") };
/* The tiled encoder (ref_video.cpp): the plan, the cut and the blend. `geom` is the request's
* (clip, H, W); plan and crop take it in pixels, join in latent units. */
static const RadParamSpec pVtilePlan[] = { P_INT("M"), P_INT("tile"), P_INT("overlap"),
P_INT("ratio") };
static const RadOperandSpec oVtilePlan[] = { OPD("geom"), OUT("y") };
static const RadParamSpec pVtileCrop[] = { P_INT("M"), P_INT("tile"), P_INT("overlap"),
P_INT("ratio"), P_STR("dtype") };
static const RadOperandSpec oVtileCrop[] = { OPD("x"), OPD("geom"), OUT("y") };
static const RadParamSpec pVtileJoin[] = { P_INT("M"), P_INT("tile"), P_INT("overlap"),
P_INT("n"), P_STR("dtype") };
static const RadOperandSpec oVtileJoin[] = { OPD("x"), OPD("geom"), OUT("y") };
/* `y` is [3, >= M], component-major at its own row pitch. */
static const RadParamSpec pGridPos3[] = { P_INT("M"), P_INT("T"), P_INT("H"), P_INT("W"),
P_INT("period"), P_F64("scale") };
static const RadOperandSpec oGridPos3[] = { OUT("y") };
/* ---- video sparse attention (ref_vsa.cpp). `segs` cuts the rows into dense segments and the
* video grid, last; `geom` is the request's RadVideoGeom on the device (RAD_VGEOM_*). The two ops
* share their parameters: attn_vsa recomputes k from `sparsity`. `v` of vsa_select is read for
* `coarse` alone, and attn_vsa's `gate` comes with its `coarse`. */
static const RadParamSpec pVsa[] = { P_INT("M"), P_INT("head_dim"), P_INT("tile_t"),
P_INT("tile_h"), P_INT("tile_w"), P_INT("patch"),
P_F64("sparsity"), P_F64O("scale") };
static const RadOperandSpec oVsaSelect[] = { OPD("q"), OPD("k"), OPD_O("v"), OPD("segs"),
OPD("geom"), OUT("sel"), OUT_O("coarse") };
/* attn_vsa adds attn_prefix_self's int8 Q.K form at the end: `qk` "i8" with `k_i8` and `k_scale`,
* quant_act_i8g's codes and scales of k at group head_dim. */
static const RadParamSpec pAttnVsa[] = { P_INT("M"), P_INT("head_dim"), P_INT("tile_t"),
P_INT("tile_h"), P_INT("tile_w"), P_INT("patch"),
P_F64("sparsity"), P_F64O("scale"), P_STRO("qk"),
P_STRO("pv") };
static const RadOperandSpec oAttnVsa[] = { OPD("q"), OPD("k"), OPD("v"), OPD("segs"),
OPD("geom"), OPD("sel"), OPD_O("coarse"),
OPD_O("gate"), OUT("out"), OPD_O("k_i8"),
OPD_O("k_scale"), OPD_O("v_scale") };
/* ---- the latent upscaler (ref_video.cpp). `geom` is the request's volume, i32 on the device:
* (T, H, W) for conv3d_ndhwc and tconv_dw, (T, h, w, H, W) for resize_bilinear and (F, Hp, Wp)
* for patch_fold, which shares patch_unfold's parameters and operands. */
/* `stride`/`stride_t`/`causal`/`pad`/`reflect`: the video encoder's CausalConv3d forms; the
* defaults are the stride-1 `same` zero padding of everything shipped before them. */
static const RadParamSpec pConv3d[] = { P_INT("M"), P_INT("cin"), P_INT("cout"), P_INT("k"),
P_INT("kt"), P_STR("dtype"), P_INTO("stride"),
P_INTO("stride_t"), P_INTO("causal"), P_STRO("pad"),
P_INTO("reflect"), P_STRO("tpad"), P_INTO("tpre") };
static const RadOperandSpec oConv3d[] = { OPD("x"), WGT("w"), WGT("b"), OPD("geom"),
OPD_O("res"), OUT("y") };
/* `mod` is one row [scale | shift]; `stats` the per-group mean and rstd, then scratch. */
static const RadParamSpec pGroupNorm[] = { P_INT("M"), P_INT("n"), P_INT("groups"), P_F64("eps"),
P_STR("act"), P_STR("dtype") };
static const RadOperandSpec oGroupNorm[] = { OPD("x"), WGT("w"), WGT("b"), OPD_O("mod"),
INOUT("stats"), OUT("y") };
static const RadParamSpec pTconvDw[] = { P_INT("M"), P_INT("n"), P_INT("k"), P_STR("dtype") };
static const RadOperandSpec oTconvDw[] = { OPD("x"), WGT("w"), WGT("b"), OPD("geom"), OUT("y") };
static const RadOperandSpec oResize[] = { OPD("x"), OPD("geom"), OUT("y") };
/* `sigma` is one f32 value, `seed` two u32 words, low first. */
static const RadParamSpec pFlowRenoise[] = { P_INT("M"), P_INT("n"), P_INT("stream"),
P_STR("dtype") };
static const RadOperandSpec oFlowRenoise[] = { OPD("sigma"), OPD("seed"), INOUT("x") };
/* `aug` is the condition's kept fraction (VISUAL_COND_TIMESTEP, 0.999), a declared constant
* where `flow_renoise` stages its weight. */
static const RadParamSpec pCondAug[] = { P_INT("M"), P_INT("n"), P_INT("stream"), P_F64("aug"),
P_STR("dtype") };
static const RadOperandSpec oCondAug[] = { OPD("seed"), INOUT("x") };
/* ---- LTX-2 (ref_ltx.cpp) */
static const RadParamSpec pRopeSplit[] = { P_INT("M"), P_INT("n_head"), P_INT("head_dim"),
P_INT("dim"), P_INT("n_dims"), P_F64("theta"),
P_STR("max"), P_INTO("head0"), P_STR("dtype") };
static const RadOperandSpec oRopeSplit[] = { INOUT("x"), OPD("positions") };
static const RadParamSpec pHeadGate[] = { P_INT("M"), P_INT("n_head"), P_INT("head_dim"),
P_STR("dtype") };
static const RadOperandSpec oHeadGate[] = { OPD("g"), INOUT("x") };
static const RadParamSpec pLtxGuide[] = { P_INT("M"), P_INT("n"), P_STR("dtype") };
static const RadOperandSpec oLtxGuide[] = { OPD("vc"), OPD_O("vu"), OPD_O("vs"), OPD_O("vm"),
OPD("scales"), OPD("sched"), INOUT("stats"),
INOUT("x") };
static const RadParamSpec pPixelNorm[] = { P_INT("M"), P_INT("n"), P_F64("eps"), P_STR("form"),
P_STRO("act"), P_INTO("ileave"), P_STR("dtype"),
P_F64O("mult") };
static const RadOperandSpec oPixelNorm[] = { OPD("x"), OUT("y") };
static const RadParamSpec pD2s3d[] = { P_INT("M"), P_INT("C"), P_INT("st"), P_INT("sh"),
P_INT("sw"),
P_INTO("drop"), P_STR("dtype") };
static const RadOperandSpec oD2s3d[] = { OPD("x"), OPD("geom"), OUT("y") };
static const RadParamSpec pS2d3d[] = { P_INT("M"), P_INT("C"), P_INT("st"), P_INT("sh"),
P_INT("sw"),
P_INTO("tpre"), P_INTO("group"), P_STR("mode"),
P_INTO("add"), P_STR("dtype") };
static const RadOperandSpec oS2d3d[] = { OPD("x"), OPD("geom"), INOUT("y") };
static const RadParamSpec pPixPatch[] = { P_INT("M"), P_INT("p"), P_INT("C"), P_STRO("order"),
P_STR("dtype") };
static const RadOperandSpec oPixPatch[] = { OPD("x"), OUT("y") };
static const RadParamSpec pVolIndex[] = { P_INT("M") };
static const RadOperandSpec oVolIndex[] = { OPD("geom"), OUT("y") };
static const RadParamSpec pTileGrid[] = { P_INT("M"), P_INT("tile"), P_INT("stride"),
P_STRO("rule"),
P_INTO("min"), P_INTO("tile_t"), P_INTO("stride_t"),
P_INTO("min_t"), P_INTO("tail") };
static const RadOperandSpec oTileGrid[] = { OPD("geom"), OUT("y") };
static const RadParamSpec pTileCrop[] = { P_INT("M"), P_INT("C"), P_STR("dtype") };
static const RadOperandSpec oTileCrop[] = { OPD("x"), OPD("geom"), OPD("box"), OUT("y") };
static const RadParamSpec pTileStitch[] = { P_INT("M"), P_INT("tile"), P_INT("stride"),
P_STRO("rule"), P_INTO("min"), P_INTO("tile_t"),
P_INTO("stride_t"), P_INTO("min_t"), P_INT("scale"),
P_INTO("scale_t"), P_INT("p"), P_INT("C"),
P_STRO("order"), P_STR("dtype") };
static const RadOperandSpec oTileStitch[] = { OPD("x"), OPD("geom"), OPD("first"), OUT("y") };
static const RadParamSpec pStftMel[] = { P_INT("M"), P_INT("hop"), P_INT("nfft"), P_INT("n_mel"),
P_INT("ch"), P_STR("dtype") };
static const RadOperandSpec oStftMel[] = { OPD("x"), WGT("basis"), WGT("mel"), OUT("y") };
static const RadParamSpec pBweUp[] = { P_INT("M"), P_INT("ratio"), P_INT("ch"), P_STR("dtype") };
static const RadOperandSpec oBweUp[] = { OPD("x"), OPD("res"), OUT("y") };
/* ---- audio generation (ref_audio.cpp) */
static const RadParamSpec pConv1d[] = { P_INT("M"), P_INT("cin"), P_INT("cout"), P_INT("k"),
P_INT("dil"), P_STRO("act"), P_STR("dtype"),
P_INTO("causal"), P_INTO("stride"), P_INTO("pad"),
P_INTO("cu_div") };
static const RadOperandSpec oConv1d[] = { OPD("x"), WGT("w"), OPD("cu_seqlens"), OPD_O("res"),
OUT("y"), WGT_O("b") };
static const RadParamSpec pConvT1d[] = { P_INT("M"), P_INT("cin"), P_INT("cout"), P_INT("stride"),
P_STR("dtype"), P_INTO("pad"), P_INTO("k") };
static const RadOperandSpec oConvT1d[] = { OPD("x"), WGT("w"), WGT("b"), OPD("cu_seqlens"),
OUT("y") };
static const RadParamSpec pSnake[] = { P_INT("M"), P_INT("n"), P_STR("dtype"), P_INTO("log") };
static const RadOperandSpec oSnake[] = { OPD("x"), WGT("alpha"), OUT("y"), WGT_O("beta") };
static const RadParamSpec pSnakeAa[] = { P_INT("M"), P_INT("n"), P_INT("ratio"), P_INT("k"),
P_INTO("log"), P_STR("dtype") };
static const RadOperandSpec oSnakeAa[] = { OPD("x"), WGT("alpha"), WGT("beta"), WGT("f_up"),
WGT("f_down"), OPD("cu_seqlens"), OUT("y") };
static const RadParamSpec pCfgTopk[] = { P_INT("M"), P_INT("n_vocab"), P_INT("k"), P_INT("rep"),
P_INTO("cond_k"), P_F64("cfg"), P_F64O("temp"),
P_INT("stream"), P_STR("dtype"), P_INTO("sup_lo"),
P_INTO("sup_hi"), P_F64O("penalty"),
P_F64O("top_p") };
static const RadOperandSpec oCfgTopk[] = { OPD("logits"), OPD("seed"), OPD("frame"), OUT("ids"),
OPD_O("cfg"), OPD_O("lane"), INOUT_O("hist") };
static const RadParamSpec pRvqEmbed[] = { P_INT("M"), P_INT("n_embd"), P_INT("n_codes"),
P_INT("vocab"), P_INT("off0"), P_F64("scale"),
P_STR("dtype"), P_INTO("round"), P_INTO("clamp") };
static const RadOperandSpec oRvqEmbed[] = { OPD("codes"), WGT("t0"), WGT("t1"), OUT("y") };
static const RadParamSpec pVqEncode[] = { P_INT("M"), P_INT("n"), P_INT("dim"), P_INT("bins"),
P_STR("dtype") };
static const RadOperandSpec oVqEncode[] = { INOUT("x"), WGT("in_w"), WGT("in_b"), WGT("cb"),
WGT("out_w"), WGT("out_b"), OUT("codes") };
static const RadParamSpec pVqEuclidean[] = { P_INT("M"), P_INT("n"), P_INT("bins"),
P_STR("dtype") };
static const RadOperandSpec oVqEuclidean[] = { INOUT("x"), WGT("cb"), OUT("codes") };
static const RadParamSpec pFrameMix[] = { P_INT("M"), P_INT("n_embd"), P_INT("L"), P_INT("stride"),
P_INTO("rep"), P_STR("dtype") };
static const RadOperandSpec oFrameMix[] = { OPD("hid"), WGT("logits"), WGT("scale"), OPD("lane"),
OPD("frame"), OPD("status"), INOUT("dst") };
static const RadParamSpec pEndStatus[] = { P_INT("M"), P_INT("end"), P_INT("rep") };
static const RadOperandSpec oEndStatus[] = { OPD("ids"), OPD("lane"), INOUT("status") };
static const RadParamSpec pRasPick[] = { P_INT("M"), P_INT("win"), P_INT("lo"), P_INT("hi"),
P_INTO("rep"), P_INTO("clear") };
static const RadOperandSpec oRasPick[] = { OPD("first"), OPD("alt"), OPD("lane"), INOUT("hist"),
OUT("ids") };
static const RadParamSpec pFrameStore[] = { P_INT("M"), P_INT("n_codes"), P_INT("stride"),
P_INTO("rep"), P_INTO("skip") };
static const RadOperandSpec oFrameStore[] = { OPD("codes"), OPD("lane"), OPD("frame"),
OPD("status"), INOUT("dst") };
static const RadParamSpec pFourier[] = { P_INT("M"), P_INT("n"), P_INTO("round"), P_STR("dtype") };
static const RadOperandSpec oFourier[] = { OPD("t"), WGT("w"), OUT("y") };
static const RadParamSpec pWinInput[] = { P_INT("M"), P_INT("n_lat"), P_INT("n_zero"),
P_INT("n_cond"), P_INT("stride"), P_STR("dtype") };
static const RadOperandSpec oWinInput[] = { OPD("lat"), OPD("cond"), OPD("lat_cu"), OPD("win_lane"),
OPD("pair"), OUT("y") };
static const RadParamSpec pWinPrep[] = { P_INT("M"), P_INT("n"), P_INT("stride"), P_INT("ov"),
P_STR("dtype") };
static const RadOperandSpec oWinPrep[] = { OPD("flags"), OPD("t"), OPD("seed"), OPD("win_index"),
OPD("win_lane"), OPD("pair"), OPD("lat_cu"),
OPD("prev"), INOUT("noise"), INOUT("lat") };
static const RadParamSpec pWinFinish[] = { P_INT("M"), P_INT("n"), P_INT("n_cond"), P_INT("stride"),
P_INT("ov"), P_STR("dtype") };
static const RadOperandSpec oWinFinish[] = { OPD("flags"), OPD("pair"), OPD("win_lane"),
OPD("lat_cu"), OPD("cond"), INOUT("lat"),
INOUT("prev"), INOUT("prev_cond") };
static const RadParamSpec pChanSplit[] = { P_INT("M"), P_INT("n"), P_INT("parts"), P_STR("dtype") };
static const RadParamSpec pInterleave[] = { P_INT("M"), P_STR("dtype") };
static const RadOperandSpec oInterleave2[] = { OPD("a"), OPD("b"), OUT("y") };
static const RadParamSpec pIota[] = { P_INT("M"), P_INT("start"), P_INT("step"),
P_INTO("period") };
static const RadOperandSpec oIota[] = { OUT("y") };
static const RadParamSpec pCuRepeat[] = { P_INT("M"), P_INT("mult") };
static const RadParamSpec pIntAffine[] = { P_INT("M"), P_STR("src"), P_STR("mul"), P_STR("add") };
static const RadOperandSpec oIntAffine[] = { OPD("x"), OUT("y") };
static const RadParamSpec pSubI32[] = { P_INT("M") };
static const RadOperandSpec oSubI32[] = { OPD("x"), OPD("s"), OUT("y") };
static const RadOperandSpec oCuRepeat[] = { OPD("len"), OUT("y") };
/* ================================================================== the schema table */
static const RadParamSpec pWanTable[] = { P_INT("M"), P_STR("dtype") };
static const RadParamSpec pWanRope[] = { P_INT("M"), P_INT("heads"), P_STR("dtype") };
static const RadOperandSpec oWanTable[] = { OPD("geom"), OUT("table") };
static const RadOperandSpec oWanRope[] = { INOUT("q"), INOUT("k"), OPD("table") };
static const RadParamSpec pWanModNorm[] = { P_INT("M"), P_INT("n"), P_INT("group"),
P_F64O("eps"), P_INTO("round_norm"), P_STR("dtype") };
static const RadParamSpec pWanGate[] = { P_INT("M"), P_INT("n"), P_INT("gate"), P_STR("dtype") };
static const RadOperandSpec oWanModNorm[] = {
OPD("x"), OPD("time"), WGT("table"), OPD_O("split"), OUT("y"),
};
static const RadOperandSpec oWanGate[] = {
INOUT("x"), OPD("update"), OPD_O("time"), WGT_O("table"), OPD_O("split"),
};
static const RadOperandSpec oWanUnpatch[] = { OPD("patches"), OPD("geom"), OUT("velocity") };
static const RadParamSpec pWanVaeFrameCu[] = { P_INT("M") };
static const RadOperandSpec oWanVaeFrameCu[] = { OPD("geom"), OUT("cu") };
static const RadParamSpec pWanVaeHistory[] = {
P_INT("M"), P_INT("temporal"), P_INT("reset"), P_STR("dtype"),
};
static const RadParamSpec pWanVaeExpand[] = {
P_INT("M"), P_INT("cin"), P_INT("cout"), P_INT("st"), P_INT("s"), P_INT("drop"),
P_INT("temporal"), P_STR("dtype"),
};
static const RadOperandSpec oWanVaeHistory[] = { OPD("x"), INOUT("history"), OUT("y") };
static const RadOperandSpec oWanVaeExpand[] = { OPD("x"), OPD("geom"), OUT("y") };
static const RadParamSpec pTimestepF64[] = { P_INT("M"), P_INT("n"), P_F64("max_period"),
P_STR("dtype") };
static const RadOperandSpec oTimestepF64[] = { OPD("t"), OUT("y") };
static const RadOperandSpec oFlowUniPC[] = { OPD("v"), OPD("coefficients"),
INOUT("history"), INOUT("x"), OPD_O("negative"),
OPD_O("guidance") };
/* GEMMs over llama.cpp's native blocks (ref_ggml.cpp). */
static const RadParamSpec pGemmGgml[] = { P_INT("M"), P_INT("N"), P_INT("K"), P_STR("type"),
P_STR("dtype") };
static const RadOperandSpec oGemmGgml[] = { OPD("a"), WGT("w"), OUT("y") };
static const RadParamSpec pMoeGemmGgml[] = { P_INT("M"), P_INT("N"), P_INT("K"), P_INT("n_expert"),
P_INT("top_k"), P_STR("a_order"), P_STR("type"), P_STR("dtype") };
static const RadOperandSpec oMoeGemmGgml[] = { OPD("a"), WTAB("w"), OPD("sorted_tok"),
OPD("expert_offset"), OUT("y") };
static const RadParamSpec pLogitsGgml[] = { P_INT("M"), P_INT("n_vocab"), P_INT("n_embd"),
P_STR("type"),
P_STR("dtype") };
static const RadOperandSpec oLogitsGgml[] = { OPD("x"), WGT("lm_head"), OUT("logits") };
static const RadParamSpec pEmbedGgml[] = { P_INT("M"), P_INT("n_embd"), P_INT("n_vocab"),
P_STR("type"),
P_STR("dtype"), P_INTO("vocab_offset"), P_F64O("mult") };
static const RadOperandSpec oEmbedGgml[] = { OPD("tokens"), WGT("wte"), OUT("x") };
static const RadParamSpec pAttnDenseRef[] = {
P_INT("M"), P_INT("head_dim"), P_INT("gqa"), P_INT("causal"), P_STR("dtype"),
P_F64O("scale"),
};
static const RadOperandSpec oAttnDenseRef[] = {
{ "q", RAD_OPD_IN, 0 }, { "k", RAD_OPD_IN, 0 }, { "v", RAD_OPD_IN, 0 },
{ "cu_seqlens", RAD_OPD_IN, 0 }, { "out", RAD_OPD_OUT, 0 }, { "mode_cu", RAD_OPD_IN, 0 },
{ "ref_log_bias", RAD_OPD_IN, 0 },
};
static const RadOpSchema kSchemas[] = {
{ "flow_unipc_step", ARR(pMnD), ARR(oFlowUniPC),
"Flow-prediction UniPC BH2 corrector and predictor. F32 history per row is "
"[last corrected sample | last clean estimate | older clean estimate]." },
{ "timestep_embed_f64", ARR(pTimestepF64), ARR(oTimestepF64),
"sinusoidal embedding of unscaled timesteps, with f64 frequencies and "
"trigonometry, f32 output." },
{ "wan_vae_frame_cu", ARR(pWanVaeFrameCu), ARR(oWanVaeFrameCu),
"Wan VAE per-frame attention offsets." },
{ "wan_vae_history", ARR(pWanVaeHistory), ARR(oWanVaeHistory),
"Wan VAE causal history and temporal taps." },
{ "wan_vae_expand", ARR(pWanVaeExpand), ARR(oWanVaeExpand),
"Wan VAE temporal shuffle and shortcut upsample." },
{ "wan_unpatch", ARR(pMnD), ARR(oWanUnpatch),
"Expand Wan head patches into F32 NDHWC velocity rows." },
{ "wan_mod_norm", ARR(pWanModNorm), ARR(oWanModNorm),
"Wan's timestep-modulated layer normalization." },
{ "wan_gated_add", ARR(pWanGate), ARR(oWanGate),
"Wan's FP32 residual update with an optional gate." },
{ "wan_rope_table", ARR(pWanTable), ARR(oWanTable), "Wan's three-axis rotary coefficients." },
{ "wan_rope", ARR(pWanRope), ARR(oWanRope), "Apply Wan rotary coefficients to queries and keys." },
{ "attn_dense_ref", ARR(pAttnDenseRef), ARR(oAttnDenseRef),
"Dense attention with a logit bias on target queries to reference keys. mode_cu gives "
"each sequence's text, reference and target starts and the last sequence's end. The natural-log "
"bias is finite in [-16, 16], or negative infinity for internal reference isolation." },
{ "rmsnorm", ARR(pRmsnorm), ARR(oRmsnorm),
"y = x * rsqrt(mean(x^2) + eps) * (w + wadd) * mult. `wadd` is not in docs/OPS.md's "
"elementwise table: "
"it is GemmaRMSNorm's `1 +`, which Qwen3.5, Qwen3-Next and Gemma all need, and it defaults to 0. "
"Spelled `wadd` and not `w_add` because that is what libr4d's fused norms call it and one "
"vocabulary cannot have two spellings of one parameter. `mult` (default 1) scales the row "
"before its one rounding." },
{ "rmsnorm_p", ARR(pLayernorm), ARR(oUnary),
"y = x * rsqrt(mean(x^2) + eps); an rmsnorm with NO gain tensor at all. wadd is 1 so the result "
"is a plain normalisation. Gemma's attention normalises the value with a bare RMS (a weightless "
"ggml_rms_norm) and the key with the k_norm gain in the same layer, which one op with a required "
"gain cannot express." },
{ "rmsnorm_add", ARR(pRmsnormAdd), ARR(oRmsnormAdd),
"residual_out = x + residual, then y = rmsnorm(residual_out) * w. The variance is taken from "
"the unrounded f32 sum; the multiply reads residual_out back AFTER it has been narrowed, which "
"is what every fused implementation does." },
{ "hc_enter", ARR(pHcWrite), ARR(oHcEnter),
"h[s] = x for each of the hc streams -- the wide residual stream's first value. Shares "
"`hc_write`'s parameter list." },
{ "mhc_pre", ARR(pMhcPre), ARR(oMhcPre),
"Manifold hyper-connection read: RMS-scaled projection of the whole hc*n residual, sigmoid "
"pre/post gates and Sinkhorn mixing. x has dtype, post and comb are f32. comb is indexed "
"[source,destination]. First pass is row softmax + hc_eps then column normalization; later "
"passes add hc_eps to both normalization denominators. hc is 2..8; iterations is 1..256." },
{ "mhc_pre_partial", ARR(pMhcPrePartial), ARR(oMhcPrePartial),
"mhc_pre's projection split by residual columns. Chunk c covers columns [c*chunk, (c+1)*chunk) "
"of the hc*n row; for chunks [first, first+chunks) part[m, (c-first)*S + j] is the sum over the "
"chunk of h*fn[j] for j < hc*(hc+2) and of h*h for j = hc*(hc+2), with S = hc*(hc+2)+1 rounded "
"up to a multiple of 4 and the slots past the last zero. chunk divides hc*n. Tensor-parallel "
"ranks each compute their chunks and an all_gather (row = chunks*S) joins them." },
{ "mhc_pre_finish", ARR(pMhcPreFinish), ARR(oMhcPreFinish),
"mhc_pre from mhc_pre_partial's matrix over all hc*n/chunk chunks: each coefficient and the sum "
"of squares is the sum of its chunks' partials (the order is the implementation's), then "
"mhc_pre's gates, Sinkhorn mixing and collapse of h. part and mhc_pre_partial over every chunk "
"together are mhc_pre." },
{ "mhc_post", ARR(pHcWrite), ARR(oMhcPost),
"out[dst,i] = post[dst]*x[i] + sum_src comb[src,dst]*h[src,i]. post and comb are f32; "
"out must not alias h. No normalization of the residual stream." },
{ "mhc_contract", ARR(pHcWrite), ARR(oMhcContract),
"x[i] = mean over hc streams h[stream,i], rounded once to dtype." },
{ "mhc_mix", ARR(pMhcPre), ARR(oMhcMix),
"mhc_pre without the collapse: the same RMS-scaled projection, gates and Sinkhorn mixing, with "
"the pre gates written to `pre` (f32 [M, hc]) instead of collapsing h. For models that collapse "
"a sublayer's input with gates an earlier sublayer computed (DeepSeek-V4.1); mhc_collapse "
"applies them." },
{ "mhc_collapse", ARR(pHcWrite), ARR(oMhcCollapse),
"x[i] = sum over hc streams of pre[stream] * h[stream, i], accumulated in f32 and rounded once "
"to dtype. pre is f32 [M, hc]; with every pre 1/hc it is mhc_contract." },
{ "kda_update", ARR(pKdaUpdate), ARR(oKdaUpdate),
"L2-normalized KDA: per-channel decay exp(lower_bound*sigmoid(exp(A_log)*(a+dt_bias))), "
"beta=sigmoid(b), S*=decay; S+=beta*(v-S*k)*k; o=scale*S*q. State is f32 [slot,heads,V,K]. "
"verify reads state_idx[seq,num_accepted-1] and writes each candidate state_idx[seq,t]; "
"prefill reads/writes column zero, committing only the final state. cu offsets are absolute; "
"has_init=0 starts from zero and a negative initial slot emits zero output. No output norm." },
{ "ssd_update", ARR(pSsdUpdate), ARR(oSsdUpdate),
"Mamba-2 selective state update: dt'=softplus(dt+dt_bias) (identity above 20, no clamp), "
"S*=exp(-exp(A_log)*dt'); S+=dt'*x*B^T; o=S*C+D*x, B and C shared by heads/groups heads. "
"State is f32 [slot,heads,head_dim,d_state]. verify reads state_idx[seq,num_accepted-1] and "
"writes each candidate state_idx[seq,t]; prefill reads/writes column zero, committing only the "
"final state. cu offsets are absolute; has_init=0 starts from zero and a negative initial slot "
"emits zero output. state_form anchor: one committed state (column 0) and a scratch slot "
"(column width/2) of per-token records (decay, dt' x, B) replayed up to num_accepted. No output "
"norm." },
{ "hc_read", ARR(pHcRead), ARR(oHcRead),
"The read half of a gated residual. N = grouped_rmsnorm(h) -- one rsqrt per `n`-wide stream, "
"one gain of hc*n over all of them, `w + wadd`; g = sigmoid(mix_up @ silu(mix_down @ N / hc)); "
"x = mean over the hc streams of g * N. With `inject_w`, inj[s] = "
"2*sigmoid(inject_w[s] . N / hc) "
"-- the per-branch gains hc_write consumes. With `q`, x is also emitted block-scaled fp8, "
"quantised from the NARROWED x and not from the accumulator. With `rotate` (and `group`), `rq` "
"and `rscale` carry had_quant_act_fp8 of the same stored x: each scale group rotated by an "
"unnormalised Hadamard before its absmax, for a consumer whose weight carries the rotation. "
"An int8 `q` or `rq` takes int8 codes instead (scale absmax / 127), quant_act_i8g's and "
"had_quant_act_i8g's. "
"`mix` names the mixing matrices' STORED format, \"bf16\" or \"e4m3\"; the arithmetic is the "
"same and an oracle reads either as the dense tensor its kernel's inverse returns." },
{ "hc_write", ARR(pHcWrite), ARR(oHcWrite),
"The write half: h[s] += inj[s] * y, for each of the hc streams of h. In place, and `h` is the "
"UNNORMED stream the paired hc_read was handed -- the normalisation never enters the residual." },
{ "hc_project", ARR(pHcProject), ARR(oHcProject),
"The control vector's projection (llama.cpp's `--cvec-mode project`): for each of the hc "
"streams of a row whose `on` flag is 1, h[s] -= scale * (h[s] . v) * v, with v that layer's unit "
"direction and scale the norm the file stored. The dot is per stream, not on the stream mean. A "
"row whose flag is 0 is not read and not written, so an unprojected request is byte-identical to "
"a deployment with no vector loaded." },
{ "mtp_enter", ARR(pMtpEnter), ARR(oMtpEnter),
"The MTP head's entry for a wide residual stream: x[k*n+i] = fc_h[i] . grouped_norm(h)[k] "
"+ fc_e[i] . norm(e), for each of the hc sub-streams k. `ngroup` == hc is one rsqrt per "
"sub-stream (what every gated residual in this architecture does) and 1 is a single rsqrt "
"over the whole wide row; the checkpoint fits both and the acceptance rate decides. `fc` names "
"the two matrices' STORED format, \"bf16\" or \"e4m3\"." },
{ "layernorm", ARR(pLayernorm), ARR(oLayernorm),
"y = (x - mean) * rsqrt(var + eps) * w + b, biased variance. `b` is optional." },
{ "grid_embed", ARR(pGridEmbed), ARR(oGridEmbed),
"x[m] += table resampled at row m's grid position: `table` is a side x side grid of rows, "
"`coord` is [4, M] -- row, column, grid height, grid width -- and the position is sampled "
"bilinearly with the grid's corners on the table's corners (align_corners), edge taps clamped. "
"The taps are summed in f32 and rounded to x's dtype, then added to x and rounded again." },
{ "grid_embed_pad", ARR(pGridEmbed), ARR(oGridEmbed),
"x[m] += the table resampled at row m's grid position in the half-pixel-centre form "
"(align_corners=False) with grid_sample's padding_mode=\"zeros\": per axis taps at floor(src) "
"and floor(src) + 1 of src = (i + 0.5) * side / size - 0.5, each carrying the hat weight "
"(1 - |src - tap|)+, and a tap whose UNCLAMPED index falls outside the table contributes zero "
"rather than reading an edge. Same operands and tap order as `grid_embed`; equal to it whenever "
"the grid fits the table, different only when a grid upsamples past it (Muse's case)." },
{ "window_order", ARR(pWindowOrder), ARR(oWindowOrder),
"The window-attention permutation of one encoder pass (HF's get_vision_window_index) computed "
"from the coordinates alone: `crd` [4, P] i32 is row, column, segment height, segment width per "
"patch and `cu` [S+1] i32 the per-frame segment boundaries. Windows of `window` patches "
"partition each segment, window-row-major, ragged at the segment's right and bottom edges, "
"patches raster within a window and windows never across segments. Outputs i32: `idx` [P], "
"slot -> source patch (the gather index); `inv` [P], the inverse; `pos` [2, P], each slot's rope "
"positions (col + 1, row + 1), component-major like the planes; `cuw` [P+1], the cumulative "
"window boundaries in the permuted order, n_windows + 1 entries used. `M` bands the pass; slots "
"past M are the identity. `window` <= 0 is the identity permutation, cuw = cu." },
{ "grid_embed_aa", ARR(pGridEmbed), ARR(oGridEmbed),
"x += the side x side table resampled as torch's F.interpolate(bilinear, align_corners=False, "
"antialias=True) resamples it to the row's grid -- SigLIP2's position embedding: per axis a "
"triangle filter on half-pixel centres, widened by the scale when shrinking, its in-table taps "
"normalised. Separable, f32, rounded once before the add; coordinates clamp into their grid." },
{ "grid_embed_cpe", ARR(pGridEmbedCpe), ARR(oGridEmbedCpe),
"x += the side x side table resampled as torch's F.interpolate(bilinear, align_corners=False) -- "
"no antialias -- onto a square of side max(grid height, grid width), of which the grid keeps the "
"top-left part: RADIO's cropped position embedding (CPE). Per axis two taps at (i "
"+ 0.5) * side / "
"d - 0.5, clamped at 0 and into the table, blended along the row first; f32, rounded once before "
"the add. A row whose coordinate row is negative is the k-th of `n_prefix` learned rows ahead "
"of its segment (RADIO's class and register tokens), k its column: it gets `prefix[k]` "
"instead, rounded the same way." },
{ "grid_embed_hw", ARR(pGridEmbedCpe), ARR(oGridEmbedCpe),
"x += the side x side table resampled as torch's F.interpolate(size=(grid height, grid width), "
"bilinear, align_corners=False) -- no antialias -- each axis onto its own extent: Inkling 2's "
"position embedding. Per axis two taps at (i + 0.5) * side / d - 0.5, d that axis's grid "
"extent, clamped at 0 and into the table, blended along the row first; f32, rounded once "
"before the add. A row whose coordinate row is negative is the k-th of `n_prefix` learned rows "
"ahead of its segment, k its column: it gets `prefix[k]` instead, rounded the same way." },
{ "posemb_resample", ARR(pPosembResample), ARR(oPosembResample),
"y [T_h * T_w, n] = the learned S_h x S_w table (`table` [S_h * S_w, n], row-major) resampled "
"bilinearly to a T_h x T_w grid, row i_h * T_w + i_w: per axis coord = S * (i + 0.5) / T - 0.5 "
"clamped to [0, S - 1], taps floor(coord) and the next one clamped at S - 1, linear weights. "
"align_corners=False with edge taps clamped. The four taps are summed in f32 (top-left, "
"top-right, bottom-left, bottom-right), each weighted by its row and column weights, and y is "
"rounded once to its dtype. Added to x by the caller, or read as a table of its own." },
{ "situ_mul", ARR(pSituMul), ARR(oGateUp),
"SiTU, Inkling 2's expert activation, over a fused gate_up with the gate in the first half: "
"y = gcap * tanh(g / gcap) * sigmoid(g) * ucap * tanh(u / ucap), the sigmoid on the raw gate. "
"A cap of 0 leaves that side uncapped." },
{ "attn_res_mix", ARR(pAttnResMix), ARR(oAttnResMix),
"Block attention residuals: a softmax over depth. Row t's values are the n_state planes "
"states[i][t] and cur[t] (+ addend[t]), each rms-normalised without a gain; query q scores each "
"as (v . query[q]) / sqrt(n), the softmax over the depth weights them, and y[q][t] is that mix, "
"rms-normalised and gained by w[q] when w is passed. y is [n_query, M, n]; f32 throughout, "
"rounded once." },
{ "lili_cand", ARR(pLiliCand), ARR(oLiliCand),
"LiLiCorr's candidates. `stats` [M, world * (2 + 2k)] f32 is each rank's sample_logprobs row, "
"row-interleaved; the ranks merge to the log-normaliser and the k largest pairs by "
"value, ties to "
"the lower id. cand [M, k] i32 = those ids. For candidate j, lp_j = logit_j - lse: "
"feat row m*k+j "
"= silu(up_w . layernorm([lp_j, exp lp_j, lp_j - lp_0, j/(k-1), j == 0]; ln_w, ln_b, eps) + "
"up_b); base row m*k+j = pas[m] + slot[m % steps] + rank[j]. f32, rounded once to the outputs." },
{ "lili_attn", ARR(pLiliAttn), ARR(oLiliAttn),
"Attention over each sequence's lattice of L = steps * k rows (row i at slot i / k): qkv [M, 3 * "
"n_head * head_dim] as q | k | v; score(i, j) = q_i . k_j * scale + rel[h][s_i - s_j + steps] + "
"(s_i == s_j ? same[h] : 0), softmax over the lattice's rows, out [M, n_head * head_dim]. No "
"mask." },
{ "lili_factor", ARR(pLiliFactor), ARR(oLiliFactor),
"y [M, 3n] = [h_i | a_s | h_i * a_s] with s = i / L: each lattice row beside its "
"sequence's anchor "
"and their product, the factor projection's input." },
{ "lili_walk", ARR(pLiliWalk), ARR(oLiliWalk),
"LiLiCorr's walk. edges [M * steps * k, 2n] each candidate's outgoing | incoming vector, aout "
"[M, n] the anchor's; each normalised as v / max(|v|, eps). start[j] = scale * aout . in[0][j], "
"pair[s][i][j] = scale * out[s][i] . in[s+1][j]; greedy p_0 = argmax start, p_s = argmax "
"pair[s-1][p_{s-1}], first index on ties; tokens [M, steps] i32 = cand[m * steps + s][p_s]. "
"`scores` [M, steps, k, k], when passed: row 0 start for every i, row s pair[s - 1]." },
{ "add", ARR(pAdd), ARR(oBinary),
"y = a + b. An operand carrying exactly n elements against a taller output broadcasts along "
"rows; nothing else broadcasts. With `div`, y = rd(rd(a + b) / div), rd y's dtype: the last "
"sum of a mean taken pairwise." },
{ "mul", ARR(pMnD), ARR(oBinary), "y = a * b, with the same one broadcast rule as `add`." },
{ "table_add", ARR(pTableAdd), ARR(oTableAdd),
"y[r, l n + c] = x[r, c] + t[l, c]: a shared modulation row plus each of L learned rows, f32 sum "
"rounded once; `M` is y's rows." },
{ "chan_stats", ARR(pChanStats), ARR(oChanStats),
"a latent through per-channel statistics held as weights: `denorm` y = rd(rd(x std) + mean), "
"`norm` y = rd(rd(x - mean) / std); std is `s`, or sqrt(s + eps) with `var` 1, each statistic "
"rounded to y's dtype." },
{ "mix_rows", ARR(pMixRows), ARR(oMixRows),
"y[r, c] = sum_i w[i] x[r k + i, c], i < k: a Linear(k, 1) across k consecutive rows, f32 sum in "
"ascending i rounded once; `M` is y's rows." },
{ "mul_weight", ARR(pMulWeight), ARR(oMulWeight),
"y = x * w, `w` a WEIGHT of one element (a scalar for the whole output) or of one row "
"(broadcast down the rows): a learned gain on an activation. f32 product, rounded once." },
{ "act_mul", ARR(pActMul), ARR(oBinary),
"y = act(a) * b, `act` \"silu\", \"gelu\" (exact erf), \"gelu_tanh\", \"sigmoid\", "
"\"quick_gelu\" or "
"\"relu\" (relu(x)^2 with one tensor in both operands). The activation is rounded to y's dtype "
"before the product, which is the unfused pair. `a` and `b` may be column slices of one fused "
"row." },
{ "seg_mean_bf16_sum", ARR(pSegMean), ARR(oSegMean),
"BF16 masked mean: f32 segment sum rounded to BF16 before division, then BF16 output." },
{ "seg_mean", ARR(pSegMean), ARR(oSegMean),
"y[s] = mean of x's rows [cu[s], cu[s+1]): one row a segment, summed in double and rounded "
"once to y's dtype. An empty segment is a zero row." },
{ "seg_softmax_pool", ARR(pSegPool), ARR(oSegPool),
"y[s] = v's rows [cu[s], cu[s+1]) summed with the softmax of their scores (one a row, f32, the "
"maximum subtracted) as weights, in f32 and rounded once to y's dtype. An empty segment is a "
"zero row." },
{ "pool_rows", ARR(pPoolRows), ARR(oUnary),
"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` (default 1) and rounded again. M, the input rows, is a multiple of k." },
{ "pool_cols", ARR(pPoolRows), ARR(oUnary),
"Column c of row m of y is the mean of x[m]'s columns [c*k, c*k + k), summed in f32 and rounded "
"to y's dtype, then times `mult` (default 1) and rounded again. n, the input width, is a "
"multiple of k." },
{ "point_fourier", ARR(pPointFourier), ARR(oUnary),
"A point's Fourier features: row m of y is [x | sin(x_i f_j) | cos(x_i f_j)] over x's n values, "
"sines then cosines each i-major then j, f_j = 2^j for j < freqs (times pi with `pi` 1); the "
"products f32, the sine and cosine f32 rounded once to y's dtype. y is [M, n (2 freqs + 1)]." },
{ "relu", ARR(pMnD), ARR(oUnary), "y = max(x, 0), each operand at its own row pitch." },
{ "clamp", ARR(pMnD), ARR(oClamp),
"y = min(max(x, lo), hi), `lo` and `hi` WEIGHTS of one element each: a clipped linear's range." },
{ "conv2d", ARR(pConv2d), ARR(oConv2d),
"A 2-D convolution over (time, width), channels innermost: x[t, w*cin + c] -> y[t, w*cout + c], "
"w [cout, cin, kh, kw] ([cout, cin, kw, kh] with t_last). Each cu_seqlens segment (rows "
"cu/cu_div) is convolved on its own with zero padding `pad` at its edges; its output rows start "
"at cu/(cu_div*stride). With `valid`, input row t reads as zeros unless valid[t * valid_stride] "
"!= 0. f32 sum plus the optional bias `b`, one rounding; then `act` (none, gelu) rounded." },
{ "conv2d_unfold", ARR(pConv2dUnfold), ARR(oConv2dUnfold),
"conv2d's receptive fields as rows: row (to, wo) holds, per input channel and kernel tap, what "
"conv2d multiplies that tap's weight by (zero in the padding and on masked rows), its columns "
"in the weight's order -- (ci, i, j), or (ci, j, i) with t_last -- so a GEMM against the weight "
"as [cout, cin*kh*kw] is the convolution. Segments, cu_div and valid as conv2d's." },
{ "conv1d_dw", ARR(pConv1dDw), ARR(oConv1dDw),
"Causal depthwise convolution over time: y[t, c] = sum_j w[c, 0, j] * x[t - (k-1) + j, c], rows "
"before the segment's start (cu/cu_div) read as zeros, plus the optional bias b[c]. f32 sum, "
"one rounding." },
{ "rel_sinusoid", ARR(pRelSin), ARR(oRelSin),
"Row d: sin(d * inv[i]) then cos(d * inv[i]) over n/2 timescales, inv[i] = exp(i * "
"-log(max_timescale) / max(n/2 - 1, 1)), each step in f32, rounded once." },
{ "seg_sinusoid", ARR(pSegSin), ARR(oSegSin),
"Row r of segment s (rows cu/cu_div) takes position p = (r - cu[s]/cu_div) mod period: sin(p * "
"inv[i]) then cos(p * inv[i]) over n/2 timescales, inv as rel_sinusoid's, each step in f32, "
"rounded once. Rows no segment covers are left alone." },
{ "attn_local_rel", ARR(pAttnLocalRel), ARR(oAttnLocalRel),
"Local attention with a relative-position term (Gemma 4's audio): query i of a segment sees "
"keys j with 0 <= i - j <= left (`right` 0). qf = q * head_dim^-0.5/ln2 * softplus(pds); logit "
"= qf.k * ln(1+e)/ln2 + qf.relk[i-j]; softcap * tanh(logit / softcap); softmax; .v. f32, one "
"rounding." },
{ "mla_attn", ARR(pMlaAttn), ARR(oMlaAttn),
"Softmax over selected latent vectors used as both key and value; per-query page tables and "
"visible lengths." },
{ "mla_attn_paged", ARR(pMlaAttnPaged), ARR(oMlaAttnPaged),
"Dense causal latent attention over a paged cache whose token row is the latent then the rotated "
"key part: score = scale * (q . latent + q_rope . rope), value = the latent; sequences as in "
"attn_paged. With `gate`, out = bf16(bf16(out) * bf16(sigmoid(gate[head])))." },
{ "mla_project", ARR(pMlaProject), ARR(oGemm),
"Per-head query absorption or value expansion through the shared "
"[head,(K|V),latent] BF16 matrix." },
{ "dsa_score", ARR(pDsaScore), ARR(oDsaScore),
"Per-head ReLU of FP8 dot products, then signed head gates and scales; invalid "
"history is -inf." },
{ "dsa_query", ARR(pMnD), ARR(oQuantFp8),
"Normalized Hadamard, BF16 boundary and power-of-two E4M3 quantization per query head." },
{ "dsa_pool", ARR(pDsaPool), ARR(oDsaPool),
"Channel-wise softmax pooling with learned position bias, BF16, normalized Hadamard, BF16, "
"power-of-two FP8." },
{ "swiglu_step", ARR(pSwigluClamped), ARR(oGateUp),
"min(silu(gate),limit)*clamp(up,-limit,limit); gate is the first half." },
{ "swiglu_clamped", ARR(pSwigluClamped), ARR(oGateUp),
"silu(min(gate,limit))*clamp(up,-limit,limit); gate is the first half." },
{ "silu_mul", ARR(pMnD), ARR(oGateUp),
"y[i] = silu(gate_up[i]) * gate_up[n + i]. The GATE IS THE FIRST HALF, which is the order "
"rad-convert fuses gate and up into one weight in." },
{ "gelu_mul", ARR(pMnD), ARR(oGateUp),
"y[i] = gelu_tanh(gate_up[i]) * gate_up[n + i], the tanh approximation (gemma's "
"gelu_pytorch_tanh), and the GATE IS THE FIRST HALF as in silu_mul. Its own op, "
"not gelu, because "
"gated feed-forwards are trained with the tanh form and the unary gelu is the exact erf one." },
{ "gelu", ARR(pMnD), ARR(oUnary),
"y = 0.5x(1 + erf(x/sqrt(2))). The exact erf form, not the tanh approximation." },
{ "silu", ARR(pMnD), ARR(oUnary), "y = x * sigmoid(x)." },
{ "sigmoid", ARR(pMnD), ARR(oUnary), "y = 1 / (1 + exp(-x))." },
{ "fill", ARR(pFill), ARR(oFill),
"y = value, every element of an [M, n] extent (default 0), rounded once to y's dtype. The op a "
"rank issues where it computes nothing and a reader downstream -- an all-reduce, a residual -- "
"still expects a defined buffer." },
{ "logit_softcap", ARR(pMnCap), ARR(oUnary), "y = cap * tanh(x / cap); the final-logit softcap." },
{ "scale", ARR(pMnD), ARR(oScale),
"y = x * s, s a [1] weight broadcast over every element; the per-tensor scalar "
"(per-layer scalar, "
"`mul` broadcasts a row, `scale_rows` a column-per-row)." },
{ "ple_gate", ARR(pPleGate), ARR(oPleGate),
"Per-Layer Embedding's gate. K = grouped_rmsnorm(k) * (w_key + wadd) and Q likewise over `q`, "
"one rsqrt per `n`-wide stream and one gain across all hc*n; s[c] = sum_d K[c][d]*Q[c][d] / "
"sqrt(n); then the SIGNED SQUARE ROOT s = sign(s) * sqrt(max(|s|, gate_eps)), where the sign is "
"the ORIGINAL's so that a dot of exactly zero stays zero; then gv[c] = sigmoid(s[c]) * v, one v "
"row gated differently per stream. `gvn` is grouped_rmsnorm(gv) * (w_conv + wadd), taken over "
"the bytes gv was narrowed to. BOTH outputs leave: the convolution that follows reads the normed "
"copy and the residual add reads the un-normed one." },
{ "ple_conv", ARR(pPleConv), ARR(oPleConv),
"y[t] = resid[t] + silu(sum_j w[c][j] * x[t - (width-1-j)*dilation]), a DEPTHWISE CAUSAL "
"convolution whose taps are `dilation` apart -- at width 4 and dilation 3 they are 9, 6, 3 and 0 "
"timesteps back, so the history a sequence carries is (width-1)*dilation and not width-1. The "
"state is [n_slots, n, state_len] with slot i holding the value at relative position "
"i - (width-1)*dilation, and `has_init` 0 means that window is ZEROS rather than whatever the "
"slot last held. `num_accepted` shifts the READ offset within a deeper window and marks a "
"decode step: the window is then rewritten as gdn_conv_update rewrites its own -- the history "
"read shifted down by one, then every input of the step -- so a next step that kept k of this "
"step's 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. Absent, every token commits: the read is at "
"offset zero and the last (width-1)*dilation inputs land at offset zero, which is the prefill "
"chunk. `resid` absent drops the add and leaves the plain convolution." },
{ "ple_conv_bias", ARR(pPleConv), ARR(oPleConvBias),
"ple_conv with `b`, a per-channel bias added inside the silu: y[t] = resid[t] + silu(b[c] + "
"sum_j w[c][j] * x[t - (width-1-j)*dilation]). At dilation one with no `resid` it is Mamba-2's "
"conv1d. The window, `has_init`, `cache_idx` and `num_accepted` are ple_conv's." },
{ "causal_conv1d", ARR(pCausalConv), ARR(oCausalConv),
"y[t] = sum_j w[c][j] * x[t - (width-1-j)], a DEPTHWISE CAUSAL convolution with no activation "
"and no residual (LFM2's short convolution, whose gates the caller applies). The state, "
"`has_init`, `cache_idx` and `num_accepted` are ple_conv's at dilation one: a window of width-1 "
"values, read at `num_accepted - 1` and rewritten shifted by one with every input of a decode "
"step behind it, or left as the chunk's last width-1 inputs when `num_accepted` is absent." },
{ "sconv_res", ARR(pSconvRes), ARR(oSconvRes),
"y[t][c] = (x[t][c] + sum_j (w[c][j] + dyn[t][c/(n/heads)][k-1-j]) * x[t-(k-1-j)][c]) / "
"sqrt(m*k + 1), a DEPTHWISE CAUSAL convolution (k = width) whose residual is the input itself, "
"m = 2 when `dyn` is present and 1 without it (heads 0). w[c][k-1] multiplies the current "
"input, as causal_conv1d's does. The state, `has_init`, `cache_idx` and `num_accepted` are "
"causal_conv1d's: a window of width-1 values per channel, so a stream of another channel count "
"takes its own state tensor." },
{ "ngram_ids", ARR(pNgramIds), ARR(oNgramIds),
"ids[m][h] = remainder(hash of the n-gram ending at m, vocab_sizes[h]) + offsets[h], where "
"hash = t0*mult[0] ^ t1*mult[1] ^ ... over as many shifted tokens as head h's block needs -- "
"heads are `ngram - 1` equal blocks, block b using the (b+2)-gram. A shift that would cross an "
"EOS reads the EOS instead, and so does every deeper shift. `state` is a rolling window of the "
"committed ids in front of the step -- ple_conv's contract exactly, read at `num_accepted - 1` "
"and rewritten in the decode layout when `num_accepted` is present and the prefill layout when "
"it is not -- except that a cold window is EOS and not zero, because zero is a real token. The "
"gather that follows is `embed_lookup_q` over M * heads indices." },
{ "gather_rows", ARR(pMnD), ARR(oGather),
"y[i, :] = x[idx[i], :]. `M` is a RANGE, so the rows gathered are `y`'s extent and not the "
"declared worst case. A negative index writes a zero row, as embed_lookup's padding does; an "
"index at or past x's row count is RAD_E_INVAL, because reading it would be reading another "
"sequence's hidden state." },
{ "scatter_rows", ARR(pMnD), ARR(oScatterRows),
"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, which is what makes it usable against a pool "
"other sequences are in. A negative index writes nothing, mirroring the zero row `gather_rows` "
"reads for one. Indices MUST be distinct -- the schema says so with RAD_OPD_F_IDX_UNIQUE -- "
"because two rows landing on one destination has no defined winner on a parallel machine, and "
"an op whose oracle and whose kernel may legitimately disagree is not checkable." },
{ "cast", ARR(pCast), ARR(oUnary),
"y = x, converted. `from` and `to` are for the selector; the bytes are described by the "
"tensors' own dtypes and those are what ref reads." },
{ "softmax", ARR(pMnD), ARR(oUnary), "Row-wise, f32, with the maximum subtracted." },
{ "quant_act_i8", ARR(pQuant), ARR(oQuant),
"Symmetric int8 with ONE SCALE PER ROW, max(amax, 1e-8) / 127, and codes x / scale rounded to "
"nearest even, stored in the STEP ORDER the int8 WMMA reads (the middle two dwords of every "
"16-byte step exchanged; n a multiple of 16). The activation of the w4a8, w8a8 and w2a8 GEMMs. "
"`asum`, if passed, gets each row's code sums per `group` columns, group-major "
"[n/group][ceil(M/16)*16]; rows past M are not written." },
{ "quant_act_i8g", ARR(pQuant), ARR(oQuantFp8),
"quant_act_fp8 at int8 codes: per `group` contiguous elements a scale amax / 127 (1 for an "
"all-zero group) and codes rounded to nearest even, row-major, scale [M, ceil(n/group)]. The "
"activation of the i8a8 GEMMs." },
/* Not one of docs/OPS.md's original ops: ref adopts the schema libr4d/r4d_rows.cpp fixed, and
* implements it so the production fp8 activation format has an oracle. */
{ "quant_act_fp8", ARR(pQuant), ARR(oQuantFp8),
"Symmetric E4M3 per group of `group` contiguous elements: scale = amax / 448 (a DEQUANT "
"multiplier, the same sense as a checkpoint's weight_scale_inv), q = clamp(x / scale, +-448), "
"scale shaped [M, ceil(n/group)] in f32. An all-zero group gets scale 1, not scale 0: the GEMM "
"multiplies this scale by the weight's block scale, and a zero here would erase that one. Rows "
"are dim 0, so a rank-3 activation is one row per token and not one per head." },
{ "quant_cols_fp8", ARR(pQuantCols), ARR(oQuantCols),
"E4M3 codes with one scale per column over all M rows: scale[c] = amax_r |x[r, c]| / 448 (1 for "
"an all-zero column), y = clamp(x / scale, +-448). An attention's values for its fp8 P.V, where "
"a column's scale multiplies out of the sum over keys." },
{ "rmsnorm_quant_fp8", ARR(pRmsQuantFp8), ARR(oRmsQuantFp8),
"RMS norm with an optional residual add, then the E4M3 quantiser, in one pass. The residual "
"add is STORED BEFORE THE VARIANCE READS IT, so the sum the norm is taken over is the bf16 "
"one and not the f32 one -- that is what makes this the `add` + `rmsnorm` pair and not "
"`rmsnorm_add`, which normalises the unrounded sum. `wadd` is added to every gain in f32. "
"`out_bf16` holds exactly the value the codes were taken over." },
{ "gated_quant_fp8", ARR(pGatedQuantFp8), ARR(oGatedQuantFp8),
"act(gate) * up over a packed [M, 2n] plane, gate half first, then the E4M3 quantiser. "
"`act` is silu (down_proj's input) or sigmoid (a gated attention o_proj's) and defaults to "
"silu, which is what docs/OPS.md's `silu_mul` names." },
{ "gemm_nt_q_gated", ARR(pGemmQGated), ARR(oGemmQGated),
"gemm_nt_q over a [N, K] gate_up plane with gated_quant_fp8's pass in the epilogue, so the "
"[M, N] bf16 tensor between them is never written. `N` is the WEIGHT's rows, twice the fused "
"width; the output is [M, N/2]." },
{ "dflash_select", ARR(pDflashSelect), ARR(oDflashSelect),
"A greedy walk over a candidate lattice: score[l, c] = unary[l, c] + sum_r pred[pid(l)][r] * "
"hp[l][r] * succ[cand[l, c]][r], where pid(0) is the anchor token and pid(l) the walk's own "
"previous pick. `hp` absent is a plane of ones, which drops the form to a plain low-rank "
"bigram. Serial in l by construction -- the predecessor is not known until the previous step "
"has been decided." },
{ "dflash_markov_greedy", ARR(pDflashMarkov), ARR(oDflashMarkov),
"The DSpark reference walk: score all vocabulary ids at each position as draft_logit[token] + "
"sum_r pred[previous_token][r] * succ[token][r], select the greatest score (lower id breaks "
"ties), then use that token as the next predecessor." },
{ "dflash_markov_greedy_f32", ARR(pDflashMarkov), ARR(oDflashMarkov),
"Full-vocabulary greedy Markov walk with F32 base logits and BF16 codebooks." },
{ "dflash_tree", ARR(pDflashTree), ARR(oDflashTree),
"dflash_select's lattice and edge scores grown best-first into a tree of `nodes` nodes: node 0 "
"is the anchor, each selected node's children are the valid candidates at the next position "
"with log probability parent + log_softmax(score / temp) over them, and each round selects the "
"unselected child with the largest (ties to the first pushed). tokens[j-1] and parents[j-1] are "
"the j-th selected node and its parent's index (0 the anchor)." },
/* The rotated fp8 twin. Unlike had_quant_act_i8, the group is both rotation width and scale
* granularity, and the factor of `group` is left entirely to the paired w4a8h weight plane, where
* the division is an exact exponent shift on a bf16 (libref/ref_quant.cpp). */
/* Its int8 twin, the activation of the i8a8h GEMMs; the factor of `group` is the weight's here
* too. */
{ "had_quant_act_i8g", ARR(pQuant), ARR(oQuantFp8),
"quant_act_i8g with a Walsh-Hadamard rotation of width `group` along n first: scale = "
"amax(rotated) / 127 per group, [M, n/group] in f32, n a multiple of `group`. Pairs with an i8 "
"weight rotated the same way, its scale carrying the factor of `group` (dtype i8a8h). A `group` "
"above 128 is whole 128-wide rotations under one scale (IQ8R's 256), the weight's scale "
"carrying the factor of 128." },
/* The calibration op, the only one no forward pass issues: it runs under a calibration flag and
* its output is read at convert time, in another process, through a file. `dtype` is the input's:
* fp8a8 where the tap is a quantiser's output, bf16 without `a_scale` for a plain activation. */
{ "gram_accum", ARR(pGram), ARR(oGram),
"h[k1][k2] += sum over rows m of dequant(a)[m][k1] * dequant(a)[m][k2], in f32. The second "
"moment of a layer's real input over a calibration corpus -- the Hessian a GPTQ packer needs. "
"dequant(a) is a * a_scale for fp8a8 and `a` itself for bf16, which passes no a_scale. "
"`h` is [n, n] and INOUT: it is READ and added to, not stored. NOT gemm_nt with a transposed "
"operand; that op contracts the last axis of both sides and this one contracts the first." },
{ "had_quant_act_i8", ARR(pQuant), ARR(oHadQuant),
"A Walsh-Hadamard rotation of width `group` along n, then symmetric int8 with ONE SCALE PER ROW "
"carrying the 1/sqrt(group) normalisation. Here `group` is the rotation width, not the scale "
"granularity -- see libref/ref_quant.cpp." },
{ "rmsnorm_had_quant_i8", ARR(pRmsHadQuant), ARR(oRmsHadQuant),
"residual add (if `residual` is present), rmsnorm, the width-`group` rotation, then per-row "
"int8. The fused form a w4a8 layer asks for before falling back to the unfused ops. `residual` "
"is INOUT and receives x + residual; `out_bf16` is the post-norm tensor for a layer that has a "
"bf16 consumer of it as well." },
{ "gated_had_quant_i8", ARR(pGatedQuantCap), ARR(oGatedQuant),
"act(a) * b, then the rotation and the int8 quantiser. `b` ABSENT MEANS THE PACKED FORM: `a` is "
"[M, 2n] and b is its second half, which is what the MLP actually produces. `act` is \"silu\" "
"(down_proj's input), \"sigmoid\" (a gated-attention o_proj's), or \"situ\" (Inkling 2's expert "
"activation, with gate_cap/up_cap; 0 is no cap). `mode` is libr4d's integer spelling of the "
"first two -- 0 silu, 1 sigmoid. `act` wins where both are given." },
{ "dequant", ARR(pDequant), ARR(oDequant),
"y = q * scale, the inverse of quant_act_i8g. It does NOT undo a rotation." },
{ "gemm_nt", ARR(pGemm), ARR(oGemm), "y[M,N] = a[M,K] @ b[N,K]^T, f32 accumulate." },
{ "gemm_nt_f32", ARR(pGemm), ARR(oGemm),
"BF16 activations times F32 weights, F32 accumulation and output; preserves routing and index "
"gate precision." },
{ "gemm_nt_bias", ARR(pGemmBias), ARR(oGemmBias),
"gemm_nt with a per-column bias added in f32, rounded to y's dtype; then, optionally, `act` of "
"that (\"gelu\" exact, \"gelu_tanh\" the tanh approximation, \"none\"), rounded; then `res` "
"added, rounded. Each step is what a separate tensor would hold." },
{ "gemm_nt_q", ARR(pGemmQ), ARR(oGemmQ),
"gemm_nt with quantised operands. The group sub-sum is accumulated from the raw codes and "
"scaled once per group, which is what an int8 or fp8 WMMA kernel does. Scale streams are "
"[rows, groups] or [rows]; an mxfp4 weight's u8 scale stream is read as OCP e8m0." },
{ "gemm_nt_q_bias", ARR(pGemmQBias), ARR(oGemmQBias), "gemm_nt_q with a per-column bias." },
{ "lora_shrink", ARR(pLoraShrink), ARR(oLoraShrink),
"t[m, j] = sum_k x(m, k) * lora_a[s, j, k] for each row m whose s = slot[m] is in [0, S), f32 "
"accumulate, k ascending; x(m, k) is the value gemm_nt_q uses for that element of `a` (a code "
"times its group's scale from `a_scale`, a float as it is). A row outside [0, S) is a base row "
"and its `t` row is not written. A row's bytes do not depend on M or on any other row." },
{ "lora_expand", ARR(pLoraExpand), ARR(oLoraExpand),
"y[m, n] = y[m, n] + scale[s, n] * sum_j t[m, j] * lora_b[s, n, j] for each row m whose s = "
"slot[m] is in [0, S), f32 accumulate, j ascending, one rounding to y's dtype. A base row's `y` "
"is neither read nor written, so it stays bit for bit what the GEMM wrote (not y + 0: that "
"turns -0 into +0)." },
{ "lora_expand_mm", ARR(pLoraExpand), ARR(oLoraExpand),
"lora_expand with t rounded to bf16 and the sum over j in any order: y[m, n] = y[m, n] + "
"scale[s, n] * sum_j bf16(t[m, j]) * lora_b[s, n, j], f32 accumulate, one rounding to y's dtype. "
"A base row's `y` is neither read nor written. A row's bytes may depend on M: the generators' "
"expand, whose passes are many rows of one adapter." },
{ "attn_paged", ARR(pAttnPaged), ARR(oAttnPaged),
"Paged attention over kv_cache[num_blocks, kv_heads, block_size, 2*head_dim] (K then V per "
"slot), f32 softmax with the max subtracted, causal and sliding-window masking, q_len >= 1 so "
"one kernel serves decode, speculative verify and prefill. seqused is the TOTAL key count "
"including this step's query rows. k_descale / v_descale are optional and apply to an fp8 "
"cache. `scale` is not in docs/OPS.md and defaults to 1/sqrt(head_dim)." },
{ "attn_paged_anchor_window", ARR(pAttnPaged), ARR(oAttnPaged),
"Paged attention with one shared window anchor at the first query position in each sequence. "
"Every row uses anchor - key < window as its lower-bound predicate; causal attention also masks "
"keys after the anchor. This operation has a distinct name because ordinary attn_paged measures "
"visibility from each query row." },
{ "attn_paged_tree", ARR(pAttnPaged), ARR(oAttnPagedTree),
"attn_paged over a tree verify (rad_drafter_tree): spec_parent[row] is -2 for an ordinary "
"causal row, else the row's parent's index within its sequence's rows (-1 for the block's "
"first row). Inside the block such a row sees itself and its ancestors only, and its window is "
"measured from the block's first position plus its depth." },
{ "attn_paged_sink", ARR(pAttnPaged), ARR(oAttnPagedSink),
"attn_paged with `sinks` [n_head] f32, one logit a query head in the scaled-score domain: it "
"joins the softmax's maximum and denominator, contributes no value, and so only shrinks the "
"keys' weights (out = sum_j p_j v_j with p_j = exp(s_j) / (sum_k exp(s_k) + exp(sink)))." },
{ "attn_paged_rel", ARR(pAttnPagedRel), ARR(oAttnPagedRel),
"attn_paged with a relative-position bias and a sink logit per query row and head. The bias of "
"key j is r[i,h] . rel_proj[:, d] at d = pos_i - pos_j for 0 <= d < extent, else 0; it joins "
"the score unscaled (logit = scale * q.k + bias). The sink joins the softmax's maximum and "
"denominator with no value, and a row with no visible key is zero." },
{ "attn_dense", ARR(pAttnDense), ARR(oAttnDense),
"Dense varlen attention over cu_seqlens segments (rows cu/cu_div). Nothing paged. `window` is "
"attn_paged's rule over a segment's positions: causal, i - j < window; non-causal, |i - j| < "
"window, or i - j < window and j - i < window_right with `window_right`; 0 none. `block` cuts "
"a segment into blocks of that many rows from its start, each attending within itself; 0 "
"none." },
{ "attn_umt5", ARR(pAttnUmt5), ARR(oAttnUmt5),
"UMT5 BF16 bidirectional attention with an unscaled query-key dot and learned relative bias. "
"Each packed sequence uses its own key range and 32 signed distance buckets." },
{ "attn_cross", ARR(pAttnCross), ARR(oAttnCross),
"attn_dense with each entry's keys a run of their own: queries rows [cu[s], cu[s+1]), keys and "
"values rows [cu_k[s], cu_k[s+1]); with `causal` a query sees key j of its run when j <= its own "
"index, both counted from their run's start. A query that sees no key is zero." },
{ "attn_na3d", ARR(pAttnNa3d), ARR(oAttnNa3d),
"3-D neighborhood attention (NATTEN's na3d) over the voxels of geom = (T, H, W): a "
"query sees the "
"kt x kh x kw window centred on it, shifted inward at the border, each extent "
"at most its axis." },
{ "sam_attn", ARR(pSamAttn), ARR(oSamAttn),
"Unlimited-OCR's SAM tower attention: per cu segment of grid_h*grid_w rows in raster grid order, "
"pad to multiples of `window` with ZERO rows (window_partition, after norm1), cut into window x "
"window tiles and attend each tile to all of its rows; `window` 0 is one tile over the segment. "
"logits = scale*q.k + q.Rh[i_h,j_h] + q.Rw[i_w,j_w], dotted over the head's whole head_dim, "
"softmax in f32 over the tile's keys (pad keys included), out = sum p*v, written back in segment "
"row order with pad rows dropped. The tables are [2*L-1, head_dim], linearly resampled along "
"dim 0 when a geometry's axes need it (get_rel_pos)." },
{ "kv_store", ARR(pKvStore), ARR(oKvStore),
"Writes k and v into the paged cache at slot_mapping; a negative slot is skipped. The cache is "
"INOUT rather than the OUT docs/OPS.md draws, because the op touches only the mapped slots." },
{ "kv_store_streamed", ARR(pKvStoreStreamed), ARR(oKvStoreStreamed),
"kv_store for a streamed group: writes the token's k and v into the group's host mirror ALWAYS, "
"at the block's identity row, and into the ring slot only when the residency map says the block "
"is resident. A negative slot is skipped. The mirror is the truth; a block absent from the ring "
"is copied in whole by kv_resolve." },
{ "quant_roundtrip", ARR(pQuantRoundtrip), ARR(oQuantRoundtrip),
"Block-scaled quantise then dequantise in place: per `block` values of a row, amax, a scale "
"(fp8_e4m3: amax floored at 1e-4, ue8m0 = power of two at or above amax*f32(1/448), f32 = "
"amax*f32(1/448); fp4_e2m1: ue8m0 from amax floored at 6*2^-126 times f32(1/6), e4m3 = "
"fp8(max(amax, 6*2^-9)/6)), codes of clamp(x/scale) (fp8 RNE saturating, e2m1 ties to the "
"even code), times scale in f32, rounded once to dtype." },
{ "softmax_pool", ARR(pSoftmaxPool), ARR(oSoftmaxPool),
"Channel-wise softmax pooling of `ratio` rows: stage [M*ratio, 2n] f32 holds each entry's "
"values then scores; out[w,c] = sum_i softmax_i(score[i,c]) value[i,c] in f32, rounded once. "
"An entry whose optional page is negative is not written." },
{ "attn_sparse_sink", ARR(pAttnSparseSink), ARR(oAttnSparseSink),
"Latent attention (key = value, dim wide) with a per-head sink over the window cache's keys "
"at [pos-window+1, pos] (the window table rebased at first = pos(last row of s) + 1 - "
"window_used[s]: block wbt[s][(j-first)/bs], cache [blocks,1,bs,dim]) and, with ratio > 0, "
"the selected compressed entries c (tokens [c ratio, (c+1) ratio), block cbt[s][c ratio/cbs], "
"cache [blocks,1,cbs,dim/ratio]). f32 scores q.k*scale, max floored at -1e30, "
"out = sum p k / (sum p + exp(sink - max)), rounded once; a row with no key is zero. "
"latent_format e4m3_e4m3_16 / e4m3_ue8m0_32: the compressed cache holds block-scaled E4M3 "
"entries (rad_kv_fp8.h; E4M3 [blocks,1,cbs,bytes/ratio]), a value code x scale in f32." },
{ "index_score", ARR(pIndexScore), ARR(oIndexScore),
"Per row and compressed position j: sum_h relu(q_h . k_j) w_h with k_j entry j of the paged "
"index cache and w_h = round(w * wscale); the dot, relu(dot) w_h and the f32 head sum each "
"rounded once to dtype. "
"j >= (pos+1)/ratio or an unmapped block scores -inf. score is f32 [M, width]. With row_part "
"p, score row i is step row p M + i (q, w, positions the step's; past the step -inf). "
"key_format e4m3_e4m3_16 / e4m3_ue8m0_32: the index cache holds block-scaled E4M3 entries "
"(rad_kv_fp8.h), a value code x scale in f32." },
{ "index_candidates", ARR(pIndexCand), ARR(oIndexCand),
"Blocks of `block` positions scored by their maximum, the newest reachable block pinned to "
"+inf, the topk_blocks highest (ties to the lower block) kept unless -inf; keep is a bit a "
"block, i32 [M, ceil(ceil(width/block)/32)], bit b%32 of word b/32 set when block b is kept." },
{ "index_topk", ARR(pIndexTopk), ARR(oIndexTopk),
"The topk highest-scoring passed positions (j < (pos+1)/ratio), inside keep when given "
"(index_candidates' bits over blocks of `block`), never -inf, ties to the lower position, in "
"ascending order, then -1." },
{ "entry_store", ARR(pEntryStore), ARR(oEntryStore),
"x[w] (n values) into the whole of block page[w] of a paged cache [blocks, heads, block_size, "
"slot], heads*block_size*slot == n, rounded once to the cache dtype; a negative page writes "
"nothing. Declared right after its group, its block_size sets the group's. format "
"e4m3_e4m3_16 / e4m3_ue8m0_32: each entry packed as block-scaled E4M3 (rad_kv_fp8.h) into an "
"E4M3 cache, heads*block_size*slot its bytes; exact for quant_roundtrip's output." },
{ "token_map", ARR(pTokenMap), ARR(oTokenMap),
"mapped[m] = table[ids[m]], -1 for an id outside [0, n_vocab): ids through an id table." },
{ "stream_gate_add", ARR(pStreamGate), ARR(oStreamGate),
"Per row and stream s: dot = sum h w k * rsqrt(mean h^2 + eps) rsqrt(mean k^2 + eps) / sqrt(n), "
"gate = sigmoid(copysign(sqrt(max(|dot|, gate_eps)), dot)), out = h + gate v; f32, rounded "
"once. v is one [n] row a token shared by the hc streams; w is f32 [hc*n]." },
{ "ds41_pack3x3", ARR(pDs41Pack), ARR(oDs41Pack),
"Pack each complete raster patch grid into 3x3 zero-padded blocks: the grid is padded to a "
"multiple of merge on both axes with zeros, then each cell of the padded grid becomes one row "
"of merge*merge patch rows in (dy, dx) order; a cell under the padded edge is zero." },
{ "ds41_span_rows", ARR(pDs41SpanRows), ARR(oDs41SpanRows),
"Copy three learned rows into a span table: span_rows[0] = start, [2] = nl, [3] = end; row 1 "
"(the encoder's own rows) is not written. bf16 rows of n." },
{ "ds41_rope_rows", ARR(pRopeRows), ARR(oRopeRows),
"rope from a rope_table plane over columns [off, off+rot) of each head_dim-wide head of x, in "
"place: gptj pairs adjacent columns, neox i with i+rot/2; (a,b)(c+is) in f32, rounded once; "
"inverse 1 conjugates the rotation. A negative or out-of-table position leaves its row." },
{ "ds41_vit_rope2d", ARR(pDs41VitRope), ARR(oDs41VitRope),
"DeepSeek-V4.1 tower rotary: rotate-half on q and k of qkv [M, 3*H*D], both axes on ONE "
"theta^(-2j/(D/2)) ladder -- half the rotary width tracks coord[0] and half coord[1]; v is "
"unchanged; f32 products rounded once to the stored dtype." },
{ "rope_table", ARR(pRopeTable), ARR(oRopeTable),
"cos/sin for the positions a step names, at each position's own row of a [max_ctx, rot] plane: "
"rot values a row, cos for the first half and sin for the second. The angles are `rope`'s "
"exactly. A row no position names is not written; a negative position writes nothing." },
{ "embed_attn", ARR(pEmbedAttn), ARR(oEmbedAttn),
"Whole noncausal segments with QK, scaled scores and probabilities rounded to "
"activation dtype." },
{ "ministral_rope", ARR(pMinistralRope), ARR(oMinistralRope),
"Ministral NeoX rotary with BF16 cos/sin and separately rounded products; V is unchanged." },
{ "pixtral_rope", ARR(pPixtralRope), ARR(oPixtralRope),
"Pixtral axial rotary with interleaved height/width frequencies and BF16 product rounding." },
{ "pixtral_merge", ARR(pPixtralMerge), ARR(oPixtralMerge),
"Pack full raster patch grids into channel-major spatial merge blocks, without mixing images." },
{ "position_scale", ARR(pPositionScale), ARR(oPositionScale),
"Scale query row p by 1 + beta * log(1 + floor(position[p] / original)), rounding the scale "
"to x's dtype before multiplication. Positions are nonnegative i32." },
{ "qsa_block_key", ARR(pQsaBlockKey), ARR(oQsaBlockKey),
"QSA's compressed key, one vector per `ratio` rows of k: the block's mean accumulated in f32 and "
"rounded to k's dtype, RMS-normed with gain (wadd + w), then NeoX-rotated over `rotary_dim` at "
"the block's position -- `pos[b]` (with `mode` mrope/imrope and `sections`, a [>= M, 3] list's "
"three components) or `pos0 + b*ratio` -- with YaRN's blend and magnitude when given. `page[b]`, "
"when given, is the output row; a negative entry writes nothing." },
{ "qsa_score", ARR(pQsaScore), ARR(oQsaScore),
"score[m][b] = (sum over heads of relu(q[m][h] . bk[bt[seq(m)][b]])) / sqrt(n) for "
"the sequence's "
"first min(nc, width) blocks; the relu is per (block, head) and the sum over heads, "
"and a negative "
"page id scores -inf. Columns past the bound are not written." },
{ "qsa_select", ARR(pQsaSelect), ARR(oQsaSelect),
"The topk highest-scoring complete blocks a query has passed, as page ids: ties to "
"the lower block "
"index, the blocks above the threshold first and the tied ones after, then the partial page when "
"(pos + 1) % ratio != 0, then -1 to topk + 1. nsel counts the entries and seqused is the "
"attention's length over them." },
{ "qsa_tail_store", ARR(pQsaTailStore), ARR(oQsaTailStore),
"state[sidx[s][0]][pos[t] % ring] = k[t] for the last `ring` tokens of each sequence; the other "
"rows of the tail keep what they held." },
{ "qsa_work", ARR(pQsaWork), ARR(oQsaWork),
"The blocks completing in this batch: per block its `ratio` raw keys (this step's rows, or the "
"tail's at q % ring) staged contiguously, its block-key page and its first-token position; slots "
"past the count get page -1. nc is each sequence's complete-block count." },
{ "kv_resolve", ARR(pKvResolve), ARR(oKvResolve),
"Makes every block the selections name resident and rewrites them as ring slots: an epoch bump, "
"a hit or a single claim per distinct block, a free slot before any resident one and otherwise "
"one CLOCK (second-chance) victim per miss - never a slot this call uses - the two maps "
"re-pointed, each missed block copied from the host mirror "
"into its slot, then every valid sel entry replaced by the slot holding its block. nsel, and so "
"the attention's length, are unchanged. The ring must hold the call's distinct blocks; a call "
"that cannot place every miss latches ctl[3] and leaves those entries naming blocks, never a "
"fabricated slot." },
{ "dflash_conv", ARR(pDflashConv), ARR(oDflashConv),
"DFlash2's grouped dynamic depthwise convolution: out[t][h] = sum over taps j, j == 0 or "
"t % block_size >= j, of (base[j][h] + delta[t][j][h / group_size]) * x[t - j][h]." },
{ "rope", ARR(pRope), ARR(oRope),
"Rotary embedding in place over the q and k heads of a fused [q|k|v] row; the v heads are left "
"alone. `mode` is \"neox\" or \"gptj\", or a multi-component form: \"mrope\" (contiguous "
"`sections` of frequencies, one ladder), \"imrope\" (Qwen3-VL's interleaved sections) or "
"\"axial\" (contiguous sections, each on its own ladder -- a 2-D rotary). `positions` is [M], "
"or [C, M] component-major for a multi-component mode; with [M] every component is that one. "
"An f32 `positions` holds the positions as they are (fractional coordinates), an integer one "
"integers. "
"`scale` divides the position; `rotary_dim` is optional and defaults to head_dim. `llama3_*` "
"applies Llama 3's frequency scaling (factor, low and high frequency factors, the original "
"length)." },
{ "qk_norm_rope", ARR(pQkNormRope), ARR(oQkNormRope),
"Per-head RMS norm of q and k against their own gains, then rope. THE NORM OUTPUT IS ROUNDED "
"TO THE TENSOR'S DTYPE BEFORE THE ROTATION, which is what the unfused pair does. `v_norm` 1 "
"also RMS-norms each v head with a unit gain, unrotated. An absent gain is a unit gain "
"without `wadd`; `rotary_dim` 0 is the norm alone, `positions` then optional. `mode` "
"\"axial\" is rope's axial (its section ladders also over rank-1 positions), and f32 "
"positions are read as rope reads them." },
{ "scale_rows", ARR(pScaleRows), ARR(oScaleRows),
"y[m, i] = x[m, i] * act(s[m]): one scalar per ROW, broadcast ACROSS the row. `mul` broadcasts "
"the other way -- one row down the rows -- and giving it both rules would make them ambiguous "
"when rows == n. `act` is \"sigmoid\" or \"none\", and it applies to the SCALAR. With `add` "
"present the form is y = bf16(x * act(s)) + add -- the product NARROWED before the sum, which "
"is what makes the fused form bit-identical to the pair it replaces." },
{ "l2_clip", ARR(pL2Clip), ARR(oL2Clip),
"y[r,i] = x[r,i] * w[i] * min(1, threshold / (||x*w over i's group|| + 1e-6)) * tau[r]; group "
"is the norm's width (n / group groups per row), w the optional per-column scale, and tau = "
"1 + alpha * ln(max(1, (positions[r] + 1) / floor)) with positions, else 1. floor 1 and alpha 0 "
"by default." },
{ "router_sigmoid_group_topk", ARR(pRouterSigmoidGroup), ARR(oRouterSigmoid),
"Group-limited sigmoid routing: groups rank by the sum of their two best p + bias, selection "
"runs over the topk_group best groups; correction bias selects, original scores weight." },
{ "router_sigmoid_topk", ARR(pRouterSigmoid), ARR(oRouterSigmoid),
"Single-group sigmoid routing; correction bias selects experts, original scores weight them." },
{ "router_scored_topk", ARR(pRouterScored), ARR(oRouterSigmoid),
"router_sigmoid_topk with the score function a required name: sigmoid, sqrtsoftplus "
"(sqrt(softplus(logit)), softplus linear above 20) or softmax over all n_expert logits." },
{ "router_scored_topk_vl", ARR(pRouterScored), ARR(oRouterScoredVL),
"router_scored_topk with two correction biases: rows with mask 1 (an image span) select by "
"score + bias_vl, other rows by score + bias; weights are the unbiased scores either way." },
{ "router_topk", ARR(pRouter), ARR(oRouter),
"Softmax over ALL n_expert logits, then the top_k by probability with ties to the lower expert "
"id, then -- if `norm` -- renormalise the kept weights to sum to one." },
{ "moe_scatter", ARR(pScatter), ARR(oScatter),
"Stable counting sort of the (token, slot) pairs by expert. sorted_tok holds the FLATTENED "
"index token*top_k + slot, not the bare token id; a slot whose expert is outside the range this "
"call owns is dropped and the tail of sorted_tok is filled with -1. THE RANGE IS "
"[expert_base, expert_base + n_expert), which is how EXPERT PARALLELISM is expressed: under it "
"the router stays replicated and picks from every expert in the model, each rank declares and "
"sorts only its own slice, and the slots that went elsewhere simply contribute nothing to this "
"rank's partial -- which the block's end-of-block all-reduce then sums. `expert_base` absent "
"or 0 with n_expert the model's count is the whole-model case." },
{ "router_topk_scatter", ARR(pRouterTopkScatter), ARR(oRouterTopkScatter),
"`router_topk` then `moe_scatter`, in one launch: exactly those two and in that order, over "
"the operands they share. The reference IS the pair -- the fused device kernel claims to be "
"byte-identical to these two launches, and this is what it is measured against. The sort's "
"outputs are sized by `n_local`, this rank's slice; absent it is the router's count." },
{ "router_gemm_topk_scatter", ARR(pRouterGemmTopkScatter), ARR(oRouterGemmTopkScatter),
"`gemm_nt` of `a` against the router `b`, then `router_topk_scatter` over those logits, and "
"with `gate_b`/`gate` present the shared expert's `gemm_nt` at N = 1 over the same `a`. As "
"with the pair, the reference IS the launches: the gate weight and output come together or not "
"at all, and the operand count says which." },
{ "moe_gemm", ARR(pMoeGemm), ARR(oMoeGemm),
"y[i] = a[row(i)] @ w[e]^T in SORTED order, e being the expert whose offset range contains i. "
"`a_order` picks row(i): \"token\" gathers a[sorted_tok[i] / top_k] from an activation with one "
"row per token, \"sorted\" reads row i of one that is already in the scatter's order. `w` is a "
"WEIGHT TABLE of n_expert entries, each [N, K] -- a layer's experts are separate movement "
"units, so base-plus-stride describes only the case where none was offloaded." },
{ "moe_gemm_q", ARR(pMoeGemmQ), ARR(oMoeGemmQ),
"moe_gemm with quantised operands, `a_order` included: an activation scale stream per row per "
"group of K -- with a row for every row `a` has -- and one "
"weight scale plane per expert shaped [ceil(N/group), ceil(K/group)]. The group sub-sum is "
"accumulated from the raw codes and scaled once, exactly as `gemm_nt_q` does. The experts are "
"two tables by parity: `w` the even ones at [N, K], `w_odd` the odd ones at [N_odd, K_odd], "
"expert e at entry e/2. The output is max(N, N_odd) wide in `parts` stacked parts, and a "
"narrower class fills each part's leading columns and zeros the rest." },
{ "moe_gemm_q_mod4", ARR(pMoeGemmQMod4), ARR(oMoeGemmQMod4),
"moe_gemm_q over FOUR weight-table classes by the expert's number mod 4: `w` class 0 at [N, K], "
"`w_q` class q at [N_q, K_q], expert e at entry e/4 of class e%4. The output is as wide as the "
"widest class in `parts` stacked parts, and a narrower class fills each part's leading columns "
"and zeros the rest." },
{ "moe_gather", ARR(pMoeGather), ARR(oMoeGather),
"Scatters the sorted expert output back and weights it. The sum over a token's slots runs in "
"ASCENDING SLOT order, which is what makes it reproducible." },
{ "row_topk", ARR(pRowTopk), ARR(oRowTopk),
"Exact per-row top-R, descending, ties to the lower column. `vocab_off` shifts every column it "
"reports, so a rank's slice reports GLOBAL ids; `pairs` is the [M, R, 2] (int32 id, float value) "
"plane an all_gather moves, with id -1 for a slot the row could not fill." },
{ "row_topk_merge", ARR(pRowTopkMerge), ARR(oRowTopkMerge),
"The R largest of world_size all-gathered per-rank candidate sets, in the same descending "
"(idx, val) form the single-rank top-R produces. `gathered` is [M, world_size * R, 2] pairs, "
"rank r's at [r*R, (r+1)*R); id -1 is a pad and is dropped. Ties break on the LOWER id, which is "
"what makes the merged answer agree with a top-R over the unsharded plane." },
{ "dequant_w4", ARR(pDequantW4), ARR(oDequantW4),
"Packed int4 weight to row-major bf16. Each 32-bit word holds 8 codes; the scale is "
"f16 in the low 16 bits, the zero point -(1024+zero) as f16 in the high 16 bits." },
{ "pair_argmax", ARR(pPairArgmax), ARR(oPairArgmax),
"idx[m] = the id of the largest value among pairs[m, 0..n), each pair an int32 id in the first "
"f32 slot and its value in the second (row_topk's `pairs`, all-gathered): ties to the lower "
"id, a negative id skipped, -1 and -inf where nothing is valid. row_topk_merge at R = 1, "
"written one element a row at the output's own pitch." },
{ "logit_rerank", ARR(pLogitRerank), ARR(oLogitRerank),
"Rescore R candidate ids a row EXACTLY against the full-precision head and re-sort on those "
"values: the second half of a coarse top-R. `idx_in` carries GLOBAL ids as row_topk reports "
"them, `vocab_off` is this rank's first column, and an id outside its rows becomes a pad. Ties "
"break on the LOWER id, the same rule row_topk and row_topk_merge use, so a tie inside a shard "
"and a tie between shards resolve alike." },
{ "gdn_conv_prep", ARR(pConvPrep), ARR(oConvPrep),
"The gated-delta-net prefill preamble: depthwise causal conv with silu and its state cache, the "
"q/k/v split, the l2 norm over each q and k head, the gate with its per-chunk cumsum, and beta. "
"`a` and `b_gate` carry the gate and beta projections; absent, they are the last 2*head_v_count "
"columns of the fused `x` row [q|k|v|a|b]. `cache_idx` names the conv-state slot per sequence "
"-- without it the state is indexed by the batch's sequence number, which a paged conv cache "
"cannot be -- and a zero entry in `has_init` means that slot holds no history, so the window in "
"front of the sequence's first token is zeros rather than whatever the slot last held." },
{ "gdn_conv_update", ARR(pConvUpdate), ARR(oConvUpdate),
"The same convolution for a decode step against the rolling window: reads at num_accepted-1 and "
"rewrites the buffer shifted down by one, so a speculative rejection is a change of read offset "
"rather than a recompute. `num_accepted` is REQUIRED, because an op that lets the caller omit it "
"is an op whose rollback contract is optional (spec §10); a non-speculative "
"decode passes ones." },
{ "gdn_kkt_solve", ARR(pKkt), ARR(oKkt),
"A = (I + strict_lower(diag(beta) K K^T e^{g_i-g_j}))^-1 per chunk, by forward substitution in "
"f32. The decay is applied per element, which cannot overflow at any gate span." },
{ "gdn_chunk_scan", ARR(pChunkScan), ARR(oChunkScan),
"The chunked scan: W = beta(V - e^g K S^T), V' = A W, O = scale e^g Q S^T + sA V', "
"S = e^{g_last} S + V'^T(e^{g_last-g} K). h0 and ht are [N, heads, head_v, head_k] and both are "
"optional-ish: a null h0 is a zero initial state." },
{ "gdn_recurrent_update", ARR(pRecurrent), ARR(oRecurrent),
"The decode recurrence against the paged state cache: the initial state comes from "
"state_idx[s, num_accepted-1] and every candidate token writes its own slot. The gated RMS norm "
"is folded into the epilogue, and so is the fp8 out-projection quantiser where o_q and o_scale "
"are present: one group per (token, value head), because head_v is the fp8 block width. It "
"quantises the bf16 `o` it just wrote and not the f32 behind it, which is what makes it agree "
"to the byte with a standalone quant_act_fp8 over the same output. "
"`state_form` says WHAT THE STATE CACHE MEANS and defaults to \"per_candidate\", which is what "
"this description and ref describe: the initial state comes from state_idx[s, num_accepted-1] "
"and every candidate token writes its own slot, so a step drafting n needs 1 + n distinct ones. "
"\"anchor\" is a second contract the same op serves -- one committed state a sequence with the "
"accepted prefix replayed onto it out of a scratch slot, two slots whatever the depth -- and it "
"produces the SAME `o` from a COMPLETELY DIFFERENT `state`. A reference that implements one and "
"is handed the other computes a correct answer to the other question, which is why the form is "
"declared rather than inferred from the index row's width. ref serves per_candidate and "
"DECLINES anchor; libr4d serves both. "
"`cu` bounds the sequences exactly as it does on the sibling "
"gdn_conv_update, which runs off the same tensor on the same step." },
{ "gdn_conv_recurrent_update", ARR(pConvRecurrent), ARR(oConvRecurrent),
"gdn_conv_update followed by gdn_recurrent_update, with q, k and v never written: the operands "
"are the convolution's (`conv_idx` is its state_idx) and then the recurrence's without those "
"three. The rolling-window and state contracts are the two ops' own." },
{ "gdn_conv_update_tree", ARR(pConvUpdate), ARR(oConvUpdateTree),
"gdn_conv_update over a tree verify (rad_drafter_tree): spec_parent[row] is the row's parent "
"within its sequence's rows, -1 for the block's first row and -2 for an ordinary causal row, "
"which continues the row before it. A row's taps are its ancestors' inputs, newest first, and "
"above the block's first row the committed history. The window is rewritten by row as the "
"chain's is; the core then moves the accepted path's entries to the front." },
{ "gdn_recurrent_update_tree", ARR(pRecurrent), ARR(oRecurrentTree),
"gdn_recurrent_update over a tree verify: a row's state is its parent's with the row applied, "
"the committed state's for the block's first row. Under `anchor` the factor records are written "
"by row and a row is applied from its stored record, so the core's move of the accepted path's "
"records to the front leaves the next step the replay it has after a chain." },
{ "gdn_conv_recurrent_update_tree", ARR(pConvRecurrent), ARR(oConvRecurrentTree),
"gdn_conv_update_tree followed by gdn_recurrent_update_tree, with q, k and v never written." },
{ "gdn_gated_rmsnorm", ARR(pGatedNorm), ARR(oGatedNorm),
"o = x * rsqrt(mean_channels(x^2) + eps) * w * act(z). `act` is \"silu\" or \"sigmoid\"." },
{ "all_reduce", ARR(pAllReduce), ARR(oAllReduce),
"Sum across ranks, f32, IN ASCENDING RANK ORDER so every rank gets the same bits. world_size 1 "
"is a no-op. On the host backend the ranks are threads in one address space, so this is a "
"rendezvous rather than a transfer. exact=0 is served exactly, so `min_bytes` -- the byte "
"floor under which a lossy row must serve the message exactly anyway -- is accepted and "
"ignored, as is `hops`, and so is `overlap` (the caller issues it beside compute). "
"`y` absent is the in-place form; present, the sum lands in y and x is left alone." },
{ "all_gather", ARR(pAllGather), ARR(oAllGather),
"y is [world_size * numel] with rank r's contribution at offset r * numel. `row` -- one row of "
"a rank's contribution, in elements -- switches to the ROW-INTERLEAVED placement instead: x is "
"[rows][row] and y is [rows][world_size][row], so one row of y carries every rank's words for "
"that row. The vocab-parallel candidate merge reads exactly that." },
{ "embed_lookup", ARR(pEmbedM), ARR(oEmbed),
"A gather from wte[n_vocab, n_embd]. A negative token id writes a zero row (padding); an id at "
"or above n_vocab fails the launch. `mult`, when given, multiplies every gathered row and the "
"product is rounded once to x's dtype (Gemma's sqrt(hidden)); absent it is a plain copy." },
{ "embed_lookup_q", ARR(pEmbed), ARR(oEmbedQ),
"The same gather from a table quantised with ONE scale for all of it: x[m] = wte[id] * scale[0]. "
"Qwen3.8-Flash-Next-FP8's 320-million-row n-gram embedding is stored that way -- "
"E4M3 codes and a "
"single bf16 `weight_scale` -- and it can be, because E4M3 carries its own exponent and a row's "
"dynamic range is 2.3x. Padding and bounds behave exactly as embed_lookup's." },
{ "embed_lookup_planes", ARR(pEmbedPlanes), ARR(oEmbedPlanes),
"A gather from a table stored as single-level affine planes, read in place: x[m, i] = v * "
"scale[id, i / group] * mult, v = table[code] when a table is given, else the "
"code. Codes are i8, "
"u8, or 4-bit elements of a bit stream along the row, lowest bits first; a table "
"is indexed by an "
"unsigned code and holds every value. Padding, bounds and vocab_offset as embed_lookup." },
{ "logits_gemm", ARR(pLogits), ARR(oLogits),
"logits[M, n_vocab] = x[M, n_embd] @ lm_head[n_vocab, n_embd]^T. Separate from gemm_nt because "
"lm_head is its own access class and the sampler wants to fuse against it." },
{ "sample_penalties", ARR(pPenal), ARR(oPenalties),
"Repetition (multiplicative, sign-dependent), frequency and presence penalties over the row's "
"history. Applied once per DISTINCT token, with the count carrying the multiplicity. "
"penalty_last_n windows the TAIL of the history; 0 turns the stage off for the row. History ids "
"are global and a token's column is its id less vocab_off." },
{ "sample_dry", ARR(pPenal), ARR(oDry),
"DRY: each token that would extend a suffix of the window which has occurred before is "
"penalised ONCE, by multiplier * base^(L - allowed_length) for the longest such repeat L, "
"repeats capped at the row's dry_rep_limit. Only lengths at or above allowed_length count, and "
"a token the optional `breakers` plane marks is never penalised. dry_penalty_last_n windows the "
"TAIL of the history; 0 turns the stage off for the row. Columns are ids less vocab_off." },
{ "sample_ngram_ban", ARR(pPenal), ARR(oNgramBan),
"The windowed no-repeat-ngram ban (Unlimited-OCR's SlidingWindowNoRepeatNgramProcessor): bans, "
"by setting -infinity, every token that would complete an n-gram the last `window` tokens of "
"the row's history already contain. `ngram` is one i32 {n, window} pair a row; the window "
"bounds the n-gram's START index, as in the reference, window <= 0 searches the whole span and "
"n 0 is off. The optional `whitelist` bitmask (a SET bit, word w bit b covering column 32w + b, "
"the row's own plane row) exempts a token from the ban, as the reference's "
"`banned.difference_update(whitelist)` does; absent, nothing is exempt. Columns are ids less "
"vocab_off." },
{ "sample_temp", ARR(pMVocab), ARR(oTemp),
"logits /= temp, per row. temp <= 0 leaves the row untouched -- greedy decoding is "
"sample_argmax, and a temperature of zero silently meaning something else is how a sampler "
"chain becomes unreadable." },
{ "sample_topk", ARR(pTopk), ARR(oTopk),
"The row's top_k largest logits, descending, ties to the lower token id. cand_val carries "
"LOGITS. top_k <= 0 or wider than the buffer means the whole candidate width; a narrower one "
"leaves index -1 / -infinity holes, which every later stage skips." },
{ "sample_topp", ARR(pMCand), ARR(oNarrow),
"Narrows the candidate set in place to the shortest prefix whose cumulative probability reaches "
"top_p, always keeping at least one. A dropped candidate is index -1, value -infinity. The "
"candidates are assumed to arrive descending, as sample_topk leaves them." },
{ "sample_minp", ARR(pMCand), ARR(oNarrow),
"Keeps candidates whose probability is at least min_p times the most likely one's." },
{ "sample_typical", ARR(pMCand), ARR(oNarrow),
"Locally typical: keeps the candidates whose surprisal is closest to the distribution's entropy "
"until their mass reaches typical_p. NOT a prefix -- the candidate order is by probability and "
"this order is by |surprisal - H| -- so it is a threshold on the distance, ties kept." },
{ "sample_xtc", ARR(pMCand), ARR(oNarrow),
"Exclude top choices: with probability xtc_probability, DROP every candidate at or above "
"xtc_threshold except the least likely of them. The opposite of every other stage, and the "
"point. A no-op unless at least two candidates clear the threshold." },
{ "sample_mask", ARR(pMVocab), ARR(oMask),
"A SET BIT MEANS ALLOWED. Word w bit b covers token 32w + b; a cleared bit sets -infinity. The "
"row's mask_row selects the plane row, or -1 for an unconstrained sequence." },
{ "sample_pick", ARR(pMCand), ARR(oPick),
"Inverse-CDF draw over the candidate set. The uniform is splitmix64 over "
"(seed * 0x9E3779B97F4A7C15 + pos), taking the top 24 bits as a float in [0,1) -- specified "
"rather than left to a library, because the same stream has to come out of a host reference, a "
"device kernel and a rerun." },
{ "sample_merge_topk", ARR(pMCand), ARR(oMerge),
"The vocab-parallel merge: the k largest of the all-gathered per-rank candidate sets, in the "
"same descending form the single-rank path produces. Token ids in `gathered` are already "
"global." },
{ "sample_argmax", ARR(pMVocab), ARR(oArgmax), "Greedy, ties to the lower token id." },
{ "sample_logprobs", ARR(pLogprobs), ARR(oLogprobs),
"For a row with RAD_SP_LOGPROBS: the raw logits' max, sum of exp(l - max) and k largest as (id, "
"logit) pairs, descending, ties to the lower id. Other rows are not written." },
/* ---- image generation -------------------------------------------------------------------- */
{ "ln_mod", ARR(pLnMod), ARR(oLnMod),
"y = bf16(bf16(LN(x)) * bf16(1 + s[seq])): a layer norm with no gain or bias, then the row's "
"sequence's modulation row of `s` ([n_seq, >= n] at its own pitch). A diffusion transformer's "
"adaptive norm without a shift." },
{ "gate_add", ARR(pGateAdd), ARR(oGateAdd),
"x = bf16(x + bf16(bf16(tanh(g[seq])) * y)): a residual added through its sequence's gate row; "
"with `act` none the gate is g itself, x = bf16(x + bf16(g[seq] * y))." },
{ "timestep_embed", ARR(pTimestep), ARR(oTimestep),
"the sinusoidal timestep embedding, [cos | sin] of t' * exp(-ln(max_period) * i / (n/2)) with "
"t' = scale * t; `round` 1 takes t through bf16 at scale and back first, as diffusers' "
"pipeline hands it to its transformer. With `curve` > 0 and a `table` [curve, n] f32 operand: "
"y[m] = table sampled at clamp01(max(t[m], clamp_min)) * (curve - 1), lerped between the two "
"rows around that position -- a precomputed adaln time-embedding curve." },
{ "gather_rows2", ARR(pMnD), ARR(oGather2),
"y[i] = idx[i] >= 0 ? a[idx[i]] : b[-1 - idx[i]]: a sequence assembled from two sources." },
{ "lane_gather", ARR(pLane), ARR(oLaneGather),
"y[cu[e] + k] = src[lane[e] * stride + k]: each entry's rows out of its lane of a per-image "
"state." },
{ "lane_store", ARR(pLane), ARR(oLaneStore),
"dst[l * stride + k] = src[k] for every lane l listed: one source's rows into several lanes." },
{ "randn_lanes", ARR(pLane), ARR(oRandnLanes),
"an image's first step: its lane's rows become its seed's standard-normal stream "
"(abi/rad_philox.h), element (k, c) the stream's k * n + c; entries without flag 1 are left." },
{ "flow_euler", ARR(pLane), ARR(oFlowEuler),
"one Euler step of a flow-matching sampler per entry that moves a lane: guidance against the "
"paired entry, x' = bf16(x + bf16(bf16(sigma_next - sigma) * v)), a preview of the clean "
"latent with flag 4, the kept region of a repaint restored with flag 2 (docs/OPS.md)." },
{ "kd_input", ARR(pLane), ARR(oKdInput),
"a k-diffusion sampler's model input per entry that moves a lane: on a first step the lane "
"becomes its seed's normal stream times sqrt(1 + sigma^2); y = rd(x / sqrt(sigma^2 + 1)) at the "
"entry's rows and at its pair's (docs/OPS.md)." },
{ "kd_step", ARR(pKdStep), ARR(oKdStep),
"one k-diffusion sampler step over a discrete-time denoiser per entry that moves a lane: the "
"clean estimate D from an eps or v output, guided against the pair, then Euler, Euler ancestral "
"or DPM++ 2M by the entry's sampler, the lane f32; flag 4 previews D, `hist` keeps it "
"(docs/OPS.md)." },
{ "chan_affine", ARR(pChanAffine), ARR(oUnary),
"a per-channel affine from two parameter lists of n values (rounded to bf16): `fma` is "
"bf16(bf16(x * mul) + add), `inv` bf16(bf16(x - add) / mul). `round` 0 keeps the lists and "
"the arithmetic in f32, rounded once into y: rd(f32(x * mul) + add), rd(f32(x - add) / mul)." },
{ "conv2d_nhwc", ARR(pConvNhwc), ARR(oConvNhwc),
"an image convolution, channels innermost: x [Hin, Win * cin] and y [Hout, Wout * cout] dense, "
"the sizes read off them; PyTorch's [cout, cin, k, k] weight and a bias; `up` 2 reads the input "
"upsampled nearest-neighbour; `pad` same (k/2 a side) or down (0 above and left, 1 below and "
"right)." },
{ "im2col_nhwc", ARR(pIm2col), ARR(oUnary),
"conv2d_nhwc's receptive fields as rows: x [Hin, Win * cin], y [Hout * Wout, cin * k * k], row "
"oy * Wout + ox and column (ci * k + i) * k + j the value conv2d_nhwc multiplies weight[o, ci, "
"i, j] by (0 in the padding), so y @ w.reshape(cout, cin * k * k)^T + b is the convolution; "
"`up`, `pad` and `stride` as conv2d_nhwc reads them." },
{ "l2norm_chan", ARR(pL2Norm), ARR(oL2Norm),
"each row normalised to unit L2 (eps 1e-12), times sqrt(n), times gamma -- three bf16 roundings "
"-- then `act` none or silu: an image decoder's channel norm." },
{ "d2s_add", ARR(pSpaceDepth), ARR(oSpaceDepth),
"y [2H, 2W * cout] += x [H, W * cin] repeated and dealt into 2x2 blocks: an upsampling "
"shortcut (Wan's DupUp3D on one frame; ft 2 keeps the second frame)." },
{ "s2d_avg_add", ARR(pSpaceDepth), ARR(oSpaceDepth),
"y [H, W * cout] += the 2x2 blocks of x [2H, 2W * cin] dealt into channels and averaged in "
"groups: a downsampling shortcut (Wan's AvgDown3D on one frame; ft 2 counts the zero frame)." },
{ "to_rgba8", ARR(pMnD), ARR(oUnary),
"decoded pixels in [-1, 1] to bytes as diffusers' postprocess makes them: "
"round(clamp(bf16(bf16(clamp(x) / 2) + 0.5), 0, 1) * 255), u8 out." },
{ "gelu_umt5", ARR(pMnD), ARR(oUnary), "Wan UMT5's tanh GELU with BF16 intermediates." },
{ "gelu_tanh", ARR(pMnD), ARR(oUnary), "the tanh approximation of gelu." },
{ "gelu_quick", ARR(pMnD), ARR(oUnary),
"CLIP's quick_gelu, three roundings: a = rd(1.702 x), s = rd(sigmoid(a)), y = rd(x s)." },
{ "scatter_add_rows", ARR(pMnD), ARR(oScatterRows),
"x[idx[i]] += v[i], rounded to x's dtype; a negative index adds nothing." },
{ "attn_prefix_self", ARR(pAttnPS), ARR(oAttnPS),
"attention over two key sources: `seqused[s]` prefix positions in the paged cache and the "
"step's own rows `k`/`v` beside `q`; self row i sees every prefix key and its own rows j <= i "
"with `causal`, all of them without. `qk` \"i8\" with `k_i8` [tokens, kv_heads * head_dim] and "
"`k_scale` [tokens, kv_heads] selects the int8 Q.K form: they are the own keys as quant_act_i8g "
"writes them at group head_dim (scale amax/127, 1 for an all-zero row; code "
"rint(clamp(x * (1/scale), +-127))), the query and each prefix key are quantised by the same "
"rule per (row, head), and a score is the exact integer dot product times both scales. `k` is "
"then not read for the own rows." },
/* ---- video generation -------------------------------------------------------------------- */
{ "rms_mod", ARR(pRmsMod), ARR(oRmsMod),
"a DiT's adaptive RMS norm with a shift: with `gated` 1 first x = bf16(x + bf16(g[seq] * y)) "
"(no tanh), stored; then n1 = bf16(x * rsqrt(mean(x^2) + eps)), n2 = bf16(n1 * w), "
"m = bf16(n2 * bf16(1 + scale[seq])), z = bf16(m + shift[seq]), `s` [n_seq, >= 2n] holding "
"[shift | scale] at its own pitch." },
{ "flow_x0_step", ARR(pMnD), ARR(oFlowX0),
"a sampler step toward a data-ward velocity's clean estimate, in f32: st = 1 - t, "
"d = x + st v, r = sigma_next / sigma, x = r x + (1 - r) d, each product and sum rounded "
"once; t, sigma and sigma_next one value each (MiniMax-H3's scheduler)." },
{ "flow_res_multistep", ARR(pMnD), ARR(oFlowRM),
"a second-order sampler step on the clean estimate: d = x + sigma v, then "
"x = r x + hb1 d + hb2 x0 with r = sigma_next / sigma, x0 the previous step's estimate (read, "
"then d written back for the next). order<2 makes it Euler (x = r x + (1 - r) d), the first "
"step, a schedule restart and the last step; there hb1 + hb2 = 1 - r. sigma, r, hb1, hb2 and "
"order one f32 each; x and x0 f32 (MiniMax-H3's DPM-Solver++2M / res_multistep)." },
{ "patch_unfold", ARR(pPatchUnfold), ARR(oPatchUnfold),
"patch rows [F Hp Wp, C pt ph pw] (geom = (F, Hp, Wp)) unfolded to voxel rows "
"[(F pt) (Hp ph) (Wp pw), C] and denormalised, f32(f32(x * mul[c]) + add[c]) with `mul` and "
"`add` lists of C f32 values." },
{ "vtile_gather", ARR(pVtileGather), ARR(oVtileGather),
"a video decoder's tiles out of voxel rows [frames H W, n] (geom = (frames, H, W)): each axis "
"split into tile-wide tiles at least `overlap` apart (diffusers' _split_tiles), tile k at rows "
"[k period, (k + 1) period) of y as its frames tile tile voxels, then n_reg rows of `reg`, then "
"zeros." },
{ "vtile_stitch", ARR(pVtileStitch), ARR(oVtileStitch),
"a decoded chunk's tiles stitched into frames (each tile's kept range, blended with the tile "
"above and then the tile to the left, both as decoded), the first `blend` kept frames blended "
"with `prev` unless the chunk is first, prev refilled from the chunk's tail (also emitted "
"when it is last), then bytes round(255 clamp(v std + mean, 0, 1)); geom = (frames, H, W, "
"first, last), u8 out." },
{ "vtile_plan", ARR(pVtilePlan), ARR(oVtilePlan),
"the tiled encoder's plan for one frame of geom = (clip, H, W) pixels: split_tiles_axis over "
"the spatial axes in latent units (tile, overlap, pixels per latent ratio), tiles row-major; "
"row t of the i32 [M, 22] y is the tile's first pixel (y0, x0), its pixel extent (th, tw) and "
"level L's volume (1, th / 2^min(L, 4), tw / 2^min(L, 4)) for L = 0..5; M is the tile count." },
{ "vtile_crop", ARR(pVtileCrop), ARR(oVtileCrop),
"one frame's pixel plane [>= H W, 3] (geom = (clip, H, W) pixels) cut into vtile_plan's tiles: "
"y [M, 3] the tiles' pixels tile-major, M the tiles' pixel count (sum of th tw)." },
{ "vtile_join", ARR(pVtileJoin), ARR(oVtileJoin),
"the tiled encoder's per-tile moments (vtile_crop's row layout at latent extent) blended into "
"the canvas y [M, n], M = geom's (clip, lat_h, lat_w) lat_h lat_w: a canvas position's tile "
"kept range, blended with the tile above and then the tile to the left, both RAW (never "
"already blended), the products and sums rounding to x's dtype as the reference's bf16 blends "
"do." },
{ "grid_pos3", ARR(pGridPos3), ARR(oGridPos3),
"a video decoder's rotary positions over tiles of `period` rows: token (t, i, j) of a tile's "
"T x H x W gets f32(scale) * (2 (c + 0.5) / n - 1) on each axis, in f32 as torch makes it, "
"the rest of the period 0; y f32 [3, >= M] component-major at its own row pitch." },
{ "vsa_select", ARR(pVsa), ARR(oVsaSelect),
"video sparse attention's tile selection. Tiles: each dense segment of `segs` in chunks of "
"tile_t tile_h tile_w rows (P prefix tiles), then tile_t x tile_h x tile_w boxes over the "
"F x Hp x Wp video grid (geom's frames, height / patch, width / patch), box (a, b, c) tile "
"P + (a nb + b) nc + c (V of them). S = f32 tile means of q and k, dotted, times scale; a video "
"query tile's sel[h][i][0, k) is its k = max(1, min(ceil((1 - sparsity) V), V)) (f64) video "
"tiles of largest S, ties to the lower, ascending. With `coarse` (and `v`): softmax over all "
"tiles of S times the tile means of v, rounded once." },
{ "attn_vsa", ARR(pAttnVsa), ARR(oAttnVsa),
"video sparse attention: a prefix query tile sees every tile, a video one the prefix tiles and "
"its k tiles of `sel` (k ascending video tiles, RAD_E_INVAL otherwise); f32 softmax with the "
"max subtracted. With `coarse` and `gate`: out = rd(rd(o) + coarse[h][tile] gate), rd the "
"output's dtype." },
{ "conv3d_ndhwc", ARR(pConv3d), ARR(oConv3d),
"a 3-D convolution over voxel rows of geom = (T, H, W) -> [rows out, cout]: "
"PyTorch's Conv3d weight [cout, cin, kt, k, k] and bias, f32 accumulation, rd(acc + b). "
"Defaults are stride 1 with `same` zero padding (kt/2 frames, k/2 rows and columns a side; "
"k and kt odd). `stride` (spatial) and `stride_t` > 1 pad then stride (3x3x3 only); `causal` "
"1 pads the time axis front-only with kt - 1 zeros and none behind (so one frame in gives "
"one frame out at any stride, the reference's T==1 tap truncation); `pad` `down` pads none "
"above and left and one below and right; `reflect` 1 mirrors the spatial padding (torch "
"mode `reflect`, a mirror with no repeated edge, so the padded dimension must exceed the "
"pad). With `res`, rd(rd(acc + b) + res), `res` possibly y itself." },
{ "group_norm", ARR(pGroupNorm), ARR(oGroupNorm),
"PyTorch's GroupNorm over all M rows: group g is channels [g n/groups, (g+1) n/groups), its "
"biased mean and variance in f64, rstd = 1 / sqrt(var + eps) in f32; v = (x - mean) rstd w + b, "
"with `mod` (one row [scale | shift]) v (1 + scale) + shift, `act` none or silu, one rounding. "
"stats[2g], stats[2g+1] get the mean and rstd; stats holds groups (2 + 3 1024) f32, the rest "
"scratch. y may be x." },
{ "tconv_dw", ARR(pTconvDw), ARR(oTconvDw),
"a depthwise convolution along the frames of voxel rows (geom = (T, H, W)): PyTorch's "
"[n, 1, k, 1, 1] weight and bias, k odd, zero frames outside [0, T), the sum f32 in tap order, "
"rd(acc + b)." },
{ "resize_bilinear", ARR(pMnD), ARR(oResize),
"each frame of voxel rows [T h w, n] resized to [T H W, n] (geom = (T, h, w, H, W)) as torch's "
"bilinear with align_corners=False at a size: src = max(f32(h / H) (i + 0.5) - 0.5, 0), its "
"floor and the next row clamped, weights in f32, rounded once." },
{ "patch_fold", ARR(pPatchUnfold), ARR(oPatchUnfold),
"patch_unfold's inverse: voxel rows [(F pt) (Hp ph) (Wp pw), C] folded to patch rows "
"[F Hp Wp, C pt ph pw] (geom = (F, Hp, Wp)) and normalised, f32(f32(x * mul[c]) + add[c]) with "
"`mul` and `add` lists of C f32 values." },
{ "flow_renoise", ARR(pFlowRenoise), ARR(oFlowRenoise),
"a clean latent re-noised to sigma s: x = f32(f32(f32(1 - s) x) + f32(s z)), z element "
"k n + c of the normal stream (seed, `stream`) (abi/rad_philox.h)." },
{ "cond_aug", ARR(pCondAug), ARR(oCondAug),
"a visual condition's rows blended with fresh noise, the reference's condition augmentation: "
"x = f32(f32(a x) + f32(oma z)) with a = `aug` and oma the f32 of the f64 1 - aug, z element "
"k n + c of the normal stream (seed, `stream`); the caller issues one media item's rows so "
"every item restarts the same stream, as the reference does." },
{ "rope_split", ARR(pRopeSplit), ARR(oRopeSplit),
"LTX-2's rotary over fractional coordinates: dim/2 pairs over all heads, the first `pad` "
"unrotated, then freq[k] = f32(theta^linspace(0,1,steps)[k] pi/2) with the n_dims coordinates "
"interleaved; angle f32(f32(2 f32(c / max)) - 1) freq; each head's halves rotated against each "
"other in f32, rounded to x's dtype. `head0` is the operand's first head." },
{ "head_gate", ARR(pHeadGate), ARR(oHeadGate),
"x = rd(x rd(2 rd(sigmoid(g)))) per head: LTX-2's gated attention output." },
{ "pixel_norm", ARR(pPixelNorm), ARR(oPixelNorm),
"an RMS norm over each row's n values as bf16 tensors compute it, every step rounded to y's "
"dtype: m = rd(sum rd(x^2) / n), then x rd(rsqrt(rd(m + eps))) (`form` rsqrt) or "
"x / rd(sqrt(rd(m + eps))) (div), optionally silu; `ileave` L writes x's row t L + l to "
"columns c L + l of y's row t." },
{ "d2s3d", ARR(pD2s3d), ARR(oD2s3d),
"depth to space over voxel rows: packed channel ((c st + a) sh + b) sw + d of x's row (t, i, j) "
"to y's (t st + a - (st - 1), i sh + b, j sw + d), the first st - 1 frames dropped." },
{ "s2d3d", ARR(pS2d3d), ARR(oS2d3d),
"space to depth over voxel rows, `tpre` leading frames prepended: each st x sh x sw box a packed "
"row (`mode` copy), or each run of `group` packed channels averaged (mean); `add` adds into y." },
{ "pix_patch", ARR(pPixPatch), ARR(oPixPatch),
"an image [H, W C] to patch rows [H/p, (W/p) C p p], channel c's (ph, pw) at c p p + pw p + ph "
"(order wh) or c p p + ph p + pw (hw)." },
{ "vol_index", ARR(pVolIndex), ARR(oVolIndex),
"a volume's voxel coordinates (t, i, j), component-major: attention's rotary positions." },
{ "tile_grid", ARR(pTileGrid), ARR(oTileGrid),
"the tiles of a grid as diffusers cuts them (`rule` stride: tiled_decode's, a tile every "
"`stride` cells; intervals: _tile_intervals', a short last remnant merged), in space and "
"optionally time, one row (t, h, w, t0, y0, x0, k, count) a tile." },
{ "tile_crop", ARR(pTileCrop), ARR(oTileCrop),
"one tile's voxels out of a volume: y[(a, i, j)] = x[(t0 + a, y0 + i, x0 + j)], box "
"(t, h, w, t0, y0, x0)." },
{ "tile_stitch", ARR(pTileStitch), ARR(oTileStitch),
"decoded tiles to RGB bytes: diffusers' in-place blend_v then blend_h of each tile with the one "
"above and the one to its left, then blend_t of each time band with the one before, each kept to "
"its stride, then to_rgba8's bytes." },
{ "stft_logmel", ARR(pStftMel), ARR(oStftMel),
"each channel's causal log-mel spectrogram from a windowed DFT basis and a mel filterbank, every "
"stored tensor rounded to y's dtype: frames S / hop, columns h n_mel + m." },
{ "bwe_up", ARR(pBweUp), ARR(oBweUp),
"y = clamp(rd(res + up(x)), -1, 1): x raised `ratio` times by a Hann-windowed sinc resampler "
"(width ceil(6 / 0.99), taps in x's dtype, edges replicated)." },
{ "ltx_guide", ARR(pLtxGuide), ARR(oLtxGuide),
"LTX-2's sampler step: each pass's clean estimate x - v sigma, the guidance deltas (CFG, STG, "
"modality) added in f32, an optional rescale by the deviations' ratio, the velocity back and "
"an Euler step of x, all f32. `scales` f32[4] (gs, stg, ms, rescale), `sched` f32[2]." },
{ "conv1d", ARR(pConv1d), ARR(oConv1d),
"a 1-D convolution over the sequences `cu_seqlens` bounds, channels innermost: x [rows, cin], "
"y [rows, cout], PyTorch's [cout, cin, k] weight and an optional bias, dilation "
"`dil` and padding "
"dil (k - 1) / 2 a side (k odd), or dil (k - 1) on the left with `causal` 1, zeros outside each "
"sequence; `res` added after the bias, then `act` none, tanh or clamp (to [-1, 1]). With "
"`stride` > 1 and an explicit `pad` (bf16 only): one sequence [0, rows of x], any "
"k, output row m "
"reading x[m * stride + tap * dil - pad] — PyTorch's zero-padded strided conv exactly." },
{ "conv_t1d", ARR(pConvT1d), ARR(oConvT1d),
"a transposed 1-D convolution of kernel `k` >= stride (2 stride by default), padding `pad` "
"((k - stride) / 2 by default, 0 the causal form): each sequence of x [rows, cin] grows "
"`stride` times into y [rows * stride, cout], output row o reading the input rows i with "
"0 <= o + pad - i stride < k; PyTorch's [cin, cout, k] weight and a bias." },
{ "snake", ARR(pSnake), ARR(oSnake),
"the snake activation per channel: x + sin(alpha x)^2 / (alpha + 1e-9), or with `beta` "
"x + (1 / (beta + 1e-9)) sin(alpha x)^2; `log` 1 stores both as logarithms." },
{ "snake_aa", ARR(pSnakeAa), ARR(oSnakeAa),
"BigVGAN's alias-free SnakeBeta over each sequence, f32: replicate-padded, upsampled `ratio` "
"times by the transposed `f_up` filter, SnakeBeta, replicate-padded and filtered by `f_down` "
"at stride `ratio` (docs/OPS.md has the paddings and crops); `log` 1 reads alpha and beta "
"as logarithms." },
{ "cfg_topk_sample", ARR(pCfgTopk), ARR(oCfgTopk),
"one pick a lane from `rep` branch rows of logits (1 or 2, conditional first): guidance "
"u + cfg (c - u) (`cfg` [n] per lane when bound), a repetition `penalty` over the lane's "
"history `hist` (cleared at frame 0, the pick recorded), ids in [sup_lo, sup_hi) never "
"candidates, with `cond_k` > 0 only the entries where c reaches its cond_k-th largest, the k "
"largest kept (ties to the lower index), over `temp`, softmax, drawn by inverse CDF with "
"rad_philox_uniform_s(seed, 2^32 + frame, stream); written to every one of the lane's slots." },
{ "rvq_embed", ARR(pRvqEmbed), ARR(oRvqEmbed),
"a frame's embedding from code-major residual codes [n_codes, rows]: t0[c0 + off0] plus the sum "
"of t1[c_k + (k - 1) vocab], times `scale`, rounded as bf16(bf16(t0 + bf16(sum)) * scale), or "
"with `round` 1 one f32 sum in codebook order times scale, rounded once." },
{ "vq_encode", ARR(pVqEncode), ARR(oVqEncode),
"one residual vector quantiser step: e = in_w x + in_b, the code the row of cb of greatest "
"cosine with e (the lower index on a tie), then x -= out_w cb[code] + out_b; f32, x rounded "
"once." },
{ "vq_euclidean", ARR(pVqEuclidean), ARR(oVqEuclidean),
"nearest Euclidean centroid (lower index on ties), codes written and centroid "
"subtracted from x." },
{ "elu", ARR(pMnD), ARR(oUnary), "y = x >= 0 ? x : expm1(x), alpha 1." },
{ "frame_mix_store", ARR(pFrameMix), ARR(oFrameMix),
"a frame's conditioning row: the softmax(logits)-weighted sum of hid's L planes times scale, "
"stored at dst[lane * stride + frame - 1] for frame >= 1 and status[lane] >= 0." },
{ "end_status", ARR(pEndStatus), ARR(oEndStatus),
"status[lane[i]] = ids[rep i] == end ? -1 : ids[rep i]." },
{ "ras_pick", ARR(pRasPick), ARR(oRasPick),
"repetition-aware sampling: ids[rep i] = alt[rep i] where first[rep i] is in [lo, hi) and in "
"the lane's window of its last `win` picks (hist [lanes, win], -1 empty, cleared first with "
"`clear` 1), else first[rep i]; the pick then enters the window." },
{ "frame_store", ARR(pFrameStore), ARR(oFrameStore),
"a frame's codes into its lane's history: dst[(lane n_codes + k) stride + frame - skip] = "
"codes[k, rep i] when frame >= skip and status[lane] >= 0." },
{ "resample_nearest", ARR(pMnD), ARR(oUnary),
"rows resampled nearest-neighbour: output row j reads input row min(floor(j * in / out), "
"in - 1) in f32, the row counts read off the operands." },
{ "fourier_embed", ARR(pFourier), ARR(oFourier),
"[cos(2 pi t w) | sin(2 pi t w)] of t [M] f32 and a trained w [n / 2]; `round` 1 rounds each "
"tensor a bf16 reference holds (t, 2 pi t, the product, each cosine and sine)." },
{ "win_input", ARR(pWinInput), ARR(oWinInput),
"a denoiser's input for its window entries: [lat | zeros | cond] from each entry's lane of the "
"latent and conditioning states, zeros for the conditioning of an unconditional entry "
"(pair -2)." },
{ "win_prep", ARR(pWinPrep), ARR(oWinPrep),
"a window step's start for each entry that moves its lane: flag 1 draws the window's noise "
"(rad_philox_normal_s(seed, 1 + win_index, .)) into the lane, keeping its first `ov` rows with "
"flag 16; flag 16 binds the first rows to the previous window's end, "
"bf16(bf16(1 - bf16((1 - 1e-6) t)) noise + bf16(t prev))." },
{ "win_finish", ARR(pWinFinish), ARR(oWinFinish),
"a window's end (flag 8): with flag 16 its first rows become the previous window's end "
"exactly; rows [max(0, n - 2 ov), max(.., n - ov)) of the latent and the conditioning are kept "
"as the next window's prev and prev_cond." },
{ "chan_split", ARR(pChanSplit), ARR(oUnary),
"x [rows, parts * n] to y [parts * rows, n]: each channel group a sequence of its own." },
{ "interleave2", ARR(pInterleave), ARR(oInterleave2),
"two streams interleaved, y [rows, 2] = (a[r], b[r])." },
{ "iota", ARR(pIota), ARR(oIota),
"y[i] = start + i * step over y's elements, or start + (i % period) * step with `period`." },
{ "cu_repeat", ARR(pCuRepeat), ARR(oCuRepeat),
"bounds of equal sequences, y[j] = j * len[0] * mult over y's elements." },
{ "int_affine", ARR(pIntAffine), ARR(oIntAffine),
"y[j] = (src[j] < 0 ? 0 : x[src[j]]) * mul[j] + add[j] over three integer lists: shapes and "
"bounds derived from a pass's device geometry." },
{ "sub_i32", ARR(pSubI32), ARR(oSubI32),
"y[i] = x[i] - s[0], int32: a device length less a device offset." },
{ "gemm_nt_ggml", ARR(pGemmGgml), ARR(oGemmGgml),
"y = a . w^T with w llama.cpp's own blocks of GGUF type `type` (\"iq1_s\", \"q6_K\", ...), "
"relayouted from the type's planes at load: each row dequantised as llama.cpp does, f32 dot in "
"index order, rounded once to dtype. A float [N, K] w is the logical form." },
{ "moe_gemm_ggml", ARR(pMoeGemmGgml), ARR(oMoeGemmGgml),
"moe_gemm with each expert's weight in llama.cpp's blocks of GGUF type `type`: "
"y[i] = a[row(i)] . "
"w[e]^T in sorted order, row(i) per `a_order` as moe_gemm, a dropped slot a row of "
"zeros. `w` is a "
"WEIGHT TABLE of n_expert entries, each read as gemm_nt_ggml reads its weight (native blocks, or "
"the float [N, K] logical form), so each output row equals gemm_nt_ggml's for its row and "
"expert." },
{ "logits_gemm_ggml", ARR(pLogitsGgml), ARR(oLogitsGgml),
"logits_gemm with lm_head in llama.cpp's blocks of GGUF type `type`, read as "
"gemm_nt_ggml reads its "
"weight: logits[M, n_vocab] (f32) = x[M, n_embd] . lm_head^T." },
{ "embed_lookup_ggml", ARR(pEmbedGgml), ARR(oEmbedGgml),
"embed_lookup from a table in llama.cpp's blocks of GGUF type `type`: the id's row "
"dequantised as "
"llama.cpp does, times `mult` when given, rounded once. Padding, bounds and "
"vocab_offset behave as "
"embed_lookup's. A float [n_vocab, n_embd] wte is the logical form." },
/* Row forms libr4d declares under names of their own, so a model can ask for the form: the same
* op, the same arithmetic and the same schema as the op named in each string. */
{ "rmsnorm_rows", ARR(pRmsnorm), ARR(oRmsnorm),
"rmsnorm, for rows a model declares in thousands (the head norms over head_dim)." },
{ "rope_rows", ARR(pRope), ARR(oRope),
"rope, for a model that wants the per-(token, pair) angles form at prefill row counts." },
};
/* ================================================================== the kernel table */
/* One row per op. `shape` is the same on every row, since ref refuses no geometry; --debug-graph
* prints it beside the warning that an op fell through to ref. */
#define SHAPE_ANY "any: every extent is read off the operands, strides included"
/* The operand-description hook (`RadKernelInfo::opd_shape`, rad_abi.h) is pasted from the launch
* name as `fn ## _shape` (see ref_ops.h), so a row without one fails to link. The implementations
* are in libref/ref_shapes.cpp.
*
* Every launch first passes ref_check_dtypes (below), so an operand libref cannot read or store
* is refused rather than read as zeros.
*
* No tunables on any row: the oracle has one implementation per op, so its answer never depends on
* a variant. The three nulls are tunables, n_tunables and tune_valid. */
static bool ref_readable(uint32_t dt) {
switch (dt) {
case RAD_F32: case RAD_F16: case RAD_BF16: case RAD_F8E4M3: case RAD_F8E5M2:
case RAD_I8: case RAD_U8: case RAD_BOOL: case RAD_I16: case RAD_U16: case RAD_I32:
case RAD_U32: case RAD_I64: case RAD_I4: case RAD_I2: case RAD_FP4E2M1: case RAD_E8M0:
return true;
default:
return false;
}
}
static bool ref_storable(uint32_t dt) {
switch (dt) {
case RAD_F32: case RAD_F16: case RAD_BF16: case RAD_F8E4M3: case RAD_F8E5M2:
case RAD_I8: case RAD_U8: case RAD_BOOL: case RAD_I16: case RAD_U16: case RAD_I32:
case RAD_U32: case RAD_I64: case RAD_I4: case RAD_I2: case RAD_FP4E2M1:
return true;
default:
return false;
}
}
/* A weight operand stored in the joined form of abi/rad_planes.h, or a table whose first entry is:
* its dtype is the first plane's, and the kernel reads each plane by the dtype its entry names. */
static bool ref_joined_weight(const RadTensor& t, int role) {
if (role != RAD_OPD_WEIGHT && role != RAD_OPD_WTAB) return false;
const void* p = t.data;
if (role == RAD_OPD_WTAB && t.rank >= 1 && t.stride[0] == 0)
p = t.shape[0] > 0 ? ((void* const*)t.data)[0] : nullptr;
return rad_planes_header(p) != nullptr;
}
/* RAD_E_DTYPE for a present operand whose dtype ref_common.h's ld_dt cannot read (an input) or
* st_dt cannot store (an output): those return zero and drop the store, which would make a call
* on an unsupported format return RAD_OK with a wrong answer. A joined weight is left to its
* kernel, which refuses the planes it does not compute. */
static int ref_check_dtypes(const char* op, const RadArgs* a) {
if (!a) return RAD_E_INVAL;
const RadOpSchema* sc = nullptr;
for (const RadOpSchema& s : kSchemas)
if (std::strcmp(s.op, op) == 0) { sc = &s; break; }
if (!sc) return RAD_E_NOKERNEL;
const int n = a->n_t < sc->n_operands ? a->n_t : sc->n_operands;
for (int i = 0; i < n; ++i) {
const RadTensor& t = a->t[i];
if (!t.data) continue;
const int role = sc->operands[i].role;
if (role != RAD_OPD_OUT && !ref_readable(t.dtype) && !ref_joined_weight(t, role))
return RAD_E_DTYPE;
if ((role == RAD_OPD_OUT || role == RAD_OPD_INOUT) && !ref_storable(t.dtype))
return RAD_E_DTYPE;
}
return RAD_OK;
}
#define ROW(nm, opname, fam, what, dts, cons, ncons, init, fini, fn, scr) \
{ nm, opname, fam, what, SHAPE_ANY, dts, RAD_DOMAIN_HOST, 0, \
cons, ncons, nullptr, 0, nullptr, init, fini, \
[](const RadArgs* a, RadStream s) -> int { \
const int rc = ref_check_dtypes(opname, a); \
return rc < 0 ? rc : fn(a, s); \
}, \
scr, nullptr, nullptr, fn##_shape }
#define SIMPLE(nm, opname, fam, what, dts, fn) \
ROW(nm, opname, fam, what, dts, nullptr, 0, nullptr, nullptr, fn, nullptr)
/* A grouped GEMM row with the codebook experts' layout hooks (ref_moe.cpp). */
#define ROW_CB(nm, opname, fam, what, dts, fn) \
{ nm, opname, fam, what, SHAPE_ANY, dts, RAD_DOMAIN_HOST, 0, \
nullptr, 0, nullptr, 0, nullptr, nullptr, nullptr, \
[](const RadArgs* a, RadStream s) -> int { \
const int rc = ref_check_dtypes(opname, a); \
return rc < 0 ? rc : fn(a, s); \
}, \
nullptr, ref_moe_cb_layout, ref_moe_cb_relayout, fn##_shape, ref_moe_cb_unrelayout }
/* gemm_nt_q's zero-point grids read their codes as `b`: u2, u4 or u5, which ld_dt does not read
* and every other op refuses. The grid's own path reads them (ref_gemm.cpp: gemm_zp), so they pass
* the check there and only there. */
static int ref_check_gemm_q(const char* op, const RadArgs* a) {
const ref::ZpGrid* zg =
a ? ref::zp_grid(rad_param_gets(a->p, a->n_p, "dtype", nullptr)) : nullptr;
if (!zg || a->n_t < 3 || !a->t[2].data || a->t[2].dtype != zg->codes)
return ref_check_dtypes(op, a);
RadArgs b = *a;
RadTensor t[16];
if (a->n_t > 16) return RAD_E_INVAL;
for (int i = 0; i < a->n_t; ++i) t[i] = a->t[i];
t[2].data = nullptr;
b.t = t;
return ref_check_dtypes(op, &b);
}
/* A row whose weight operands may be joined over several planes (ref_planes.h): its layout hooks,
* and the dtype check its launch passes first. */
#define ROWL(nm, opname, fam, what, dts, check, fn, lay) \
{ nm, opname, fam, what, SHAPE_ANY, dts, RAD_DOMAIN_HOST, 0, \
nullptr, 0, nullptr, 0, nullptr, nullptr, nullptr, \
[](const RadArgs* a, RadStream s) -> int { \
const int rc = check(opname, a); \
return rc < 0 ? rc : fn(a, s); \
}, \
nullptr, lay, ref::ref_relayout_planes<lay>, fn##_shape, ref::ref_unrelayout_planes<lay> }
/* gemm_nt_q's row: the hooks above with dtype m2xfp4 answered first (ref_m2xfp4.cpp). */
int ref_gemm_nt_q_m2(const RadArgs*, RadStream);
REF_SHAPE_FN(ref_gemm_nt_q_m2);
int ref_layout_gemm_q_m2(const RadParam*, int, int, const RadEncoding*, const int*,
const RadTensor*, int, RadLayout*);
int ref_relayout_gemm_q_m2(const RadParam*, int, int, const RadEncoding*, const int*,
const RadTensor*, int, void*, int64_t);
int ref_unrelayout_gemm_q_m2(const RadParam*, int, int, const RadEncoding*, const int*,
const void*, int64_t, const RadTensor*, int);
#define ROWL_GQ(nm, opname, fam, what, dts, check, fn, lay, rel, unrel) \
{ nm, opname, fam, what, SHAPE_ANY, dts, RAD_DOMAIN_HOST, 0, \
nullptr, 0, nullptr, 0, nullptr, nullptr, nullptr, \
[](const RadArgs* a, RadStream s) -> int { \
const int rc = check(opname, a); \
return rc < 0 ? rc : fn(a, s); \
}, \
nullptr, lay, rel, fn##_shape, unrel }
/* A row whose kernel checks its own operands' dtypes (codes the generic reader does not read, such
* as u4) and reads its weights as stored: no layout hooks, so a table on the file tier is read in
* place. */
#define ROW_OWN(nm, opname, fam, what, dts, fn) \
{ nm, opname, fam, what, SHAPE_ANY, dts, RAD_DOMAIN_HOST, 0, \
nullptr, 0, nullptr, 0, nullptr, nullptr, nullptr, fn, \
nullptr, nullptr, nullptr, fn##_shape, nullptr }
#define DT_FLOAT "bf16 f16 f32 fp8_e4m3 fp8_e5m2 i8 i32, chosen per operand from its own dtype"
#define DT_ANY "any dtype rad_types.h names, per operand"
#define DT_FLOAT_CB \
DT_FLOAT "; or u4 / u5 / u6 codes into an f32 table, joined (w4nl* / w5nl* / w6nl*)"
#ifdef REF_HAVE_GGML
extern "C" int ref_ggml_layout(const RadParam*, int, int, const RadEncoding*, const int*,
const RadTensor*, int, RadLayout*);
extern "C" int ref_ggml_relayout(const RadParam*, int, int, const RadEncoding*, const int*,
const RadTensor*, int, void*, int64_t);
extern "C" int ref_ggml_unrelayout(const RadParam*, int, int, const RadEncoding*, const int*,
const void*, int64_t, const RadTensor*, int);
/* A GEMM over llama.cpp's native blocks (ref_ggml.cpp): the hooks relayout the type's planes into
* its blocks, and the kernel checks its own operands (the weight is u8 blocks or a float logical
* form, which the generic dtype check does not describe). */
#define ROW_GGML(nm, opname, fam, what, dts, fn) \
{ nm, opname, fam, what, SHAPE_ANY, dts, RAD_DOMAIN_HOST, 0, \
nullptr, 0, nullptr, 0, nullptr, nullptr, nullptr, fn, \
nullptr, ref_ggml_layout, ref_ggml_relayout, fn##_shape, ref_ggml_unrelayout }
#endif
static const RadKernelInfo kKernels[] = {
SIMPLE("ref_flow_unipc_step", "flow_unipc_step", "elem",
"flow-prediction UniPC BH2 corrector and predictor", DT_FLOAT, ref_flow_unipc_step),
SIMPLE("ref_timestep_embed_f64", "timestep_embed_f64", "elem", "FP64 sinusoidal timestep embedding",
"f32", ref_timestep_embed_f64),
SIMPLE("ref_wan_vae_frame_cu", "wan_vae_frame_cu", "elem",
"Wan VAE per-frame attention offsets.", "i32", ref_wan_vae_frame_cu),
SIMPLE("ref_wan_vae_history", "wan_vae_history", "elem", "Wan VAE causal history", "bf16",
ref_wan_vae_history),
SIMPLE("ref_wan_vae_expand", "wan_vae_expand", "elem", "Wan VAE temporal shuffle and shortcut",
"bf16", ref_wan_vae_expand),
SIMPLE("ref_wan_unpatch", "wan_unpatch", "elem", "Wan patch velocity to voxel velocity", "bf16/f32",
ref_wan_unpatch),
SIMPLE("ref_wan_mod_norm", "wan_mod_norm", "norm", "Wan's timestep-modulated layer normalization",
"f32 input, f32 or bf16 output", ref_wan_mod_norm),
SIMPLE("ref_wan_gated_add", "wan_gated_add", "elem", "Wan's FP32 residual update",
"f32 state and bf16 update", ref_wan_gated_add),
SIMPLE("ref_wan_rope_table", "wan_rope_table", "elem", "Wan rotary coefficients", "f32",
ref_wan_rope_table),
SIMPLE("ref_wan_rope", "wan_rope", "elem", "Wan rotary on queries and keys", "bf16/f32",
ref_wan_rope),
SIMPLE("ref_attn_dense_ref", "attn_dense_ref", "attn", "dense attention with reference key bias",
DT_FLOAT, ref_attn_dense_ref),
SIMPLE("ref_rmsnorm", "rmsnorm", "norm", "root-mean-square norm with a gain", DT_FLOAT,
ref_rmsnorm),
SIMPLE("ref_rmsnorm_p", "rmsnorm_p", "norm", "weightless RMS norm (no gain, plain x/rms)", DT_FLOAT,
ref_rmsnorm_p),
SIMPLE("ref_rmsnorm_add", "rmsnorm_add", "norm", "residual add then RMS norm", DT_FLOAT,
ref_rmsnorm_add),
SIMPLE("ref_hc_enter", "hc_enter", "norm", "gated residual entry: hc copies of x", DT_FLOAT,
ref_hc_enter),
SIMPLE("ref_mhc_pre", "mhc_pre", "norm", "manifold residual gates and Sinkhorn mixing", DT_FLOAT,
ref_mhc_pre),
SIMPLE("ref_mhc_pre_partial", "mhc_pre_partial", "norm", "manifold residual projection partials",
DT_FLOAT,
ref_mhc_pre_partial),
SIMPLE("ref_mhc_pre_finish", "mhc_pre_finish", "norm",
"manifold residual gates from projection partials", DT_FLOAT,
ref_mhc_pre_finish),
SIMPLE("ref_mhc_post", "mhc_post", "norm", "manifold residual source-to-destination update",
DT_FLOAT,
ref_mhc_post),
SIMPLE("ref_mhc_contract", "mhc_contract", "norm", "mean of manifold residual streams", DT_FLOAT,
ref_mhc_contract),
SIMPLE("ref_mhc_mix", "mhc_mix", "norm",
"manifold residual gates and Sinkhorn mixing, pre gates out", DT_FLOAT,
ref_mhc_mix),
SIMPLE("ref_mhc_collapse", "mhc_collapse", "norm",
"manifold residual streams collapsed by given gates", DT_FLOAT,
ref_mhc_collapse),
SIMPLE("ref_kda_update", "kda_update", "gdn",
"bounded per-channel delta recurrence and candidate rollback", DT_FLOAT,
ref_kda_update),
SIMPLE("ref_ssd_update", "ssd_update", "gdn",
"Mamba-2 scalar-decay recurrence and candidate rollback", DT_FLOAT,
ref_ssd_update),
ROWL("ref_hc_read", "hc_read", "norm",
"gated residual read: grouped norm, low-rank gate, stream mix",
DT_FLOAT ", or mixing matrices of E4M3 / int8 codes with a scale plane, joined",
ref_check_dtypes, ref_hc_read, ref::ref_layout_hc),
ROWL("ref_mtp_enter", "mtp_enter", "norm",
"MTP head entry: two norms, a per-sub-stream fc and a broadcast fc",
DT_FLOAT ", or fc matrices of E4M3 / int8 codes with a scale plane, joined",
ref_check_dtypes, ref_mtp_enter, ref::ref_layout_mtp),
SIMPLE("ref_hc_write", "hc_write", "norm", "gated residual write: h[s] += inj[s] * y",
DT_FLOAT, ref_hc_write),
SIMPLE("ref_hc_project", "hc_project", "norm",
"control vector: h[s] -= scale * (h[s] . v) * v where the row's flag is on",
DT_FLOAT, ref_hc_project),
SIMPLE("ref_layernorm", "layernorm", "norm", "layer norm with gain and bias", DT_FLOAT,
ref_layernorm),
SIMPLE("ref_grid_embed", "grid_embed", "elem",
"bilinear resample of a learned position grid, added in", DT_FLOAT, ref_grid_embed),
SIMPLE("ref_grid_embed_pad", "grid_embed_pad", "elem",
"half-pixel bilinear resample of a position grid, zero-padded taps, added in", DT_FLOAT,
ref_grid_embed_pad),
SIMPLE("ref_window_order", "window_order", "elem",
"the window-attention permutation of one pass, from its coordinates", DT_ANY,
ref_window_order),
SIMPLE("ref_grid_embed_aa", "grid_embed_aa", "elem",
"antialiased half-pixel bilinear resample of a position grid, added in", DT_FLOAT,
ref_grid_embed_aa),
SIMPLE("ref_grid_embed_cpe", "grid_embed_cpe", "elem",
"half-pixel bilinear resample of a position grid onto a square, cropped, added in", DT_FLOAT,
ref_grid_embed_cpe),
SIMPLE("ref_grid_embed_hw", "grid_embed_hw", "elem",
"half-pixel bilinear resample of a position grid onto the grid's own height and width, "
"added in", DT_FLOAT, ref_grid_embed_hw),
SIMPLE("ref_posemb_resample", "posemb_resample", "elem",
"bilinear resample of a position table to a T_h x T_w grid, as a table", DT_FLOAT,
ref_posemb_resample),
SIMPLE("ref_situ_mul", "situ_mul", "elem", "SiTU gated product over a fused gate_up", DT_FLOAT,
ref_situ_mul),
SIMPLE("ref_attn_res_mix", "attn_res_mix", "norm",
"block attention residuals: a depth softmax over rms-normed states", DT_ANY,
ref_attn_res_mix),
SIMPLE("ref_lili_cand", "lili_cand", "draft", "LiLiCorr candidates and their features",
DT_FLOAT, ref_lili_cand),
SIMPLE("ref_lili_attn", "lili_attn", "draft", "LiLiCorr lattice attention with slot biases",
DT_FLOAT, ref_lili_attn),
SIMPLE("ref_lili_factor", "lili_factor", "draft", "LiLiCorr factor input [h | a | h * a]",
DT_FLOAT, ref_lili_factor),
SIMPLE("ref_lili_walk", "lili_walk", "draft", "LiLiCorr edge scores and greedy walk", DT_FLOAT,
ref_lili_walk),
SIMPLE("ref_add", "add", "elem", "elementwise sum", DT_FLOAT, ref_add),
SIMPLE("ref_mul", "mul", "elem", "elementwise product", DT_FLOAT, ref_mul),
SIMPLE("ref_mla_attn", "mla_attn", "attn", "absorbed latent MQA attention", DT_FLOAT,
ref_mla_attn),
SIMPLE("ref_dsa_score", "dsa_score", "attn", "weighted FP8 index scoring", DT_FLOAT,
ref_dsa_score),
SIMPLE("ref_dsa_pool", "dsa_pool", "attn", "learned compressed FP8 index keys", DT_FLOAT,
ref_dsa_pool),
SIMPLE("ref_dsa_query", "dsa_query", "attn", "normalized FP8 index queries", DT_FLOAT,
ref_dsa_query),
SIMPLE("ref_gemm_nt_f32", "gemm_nt_f32", "gemm", "F32 routing projection", DT_FLOAT,
ref_gemm_nt_f32),
SIMPLE("ref_mla_attn_paged", "mla_attn_paged", "attn",
"dense paged latent attention with a rotary part",
DT_FLOAT, ref_mla_attn_paged),
SIMPLE("ref_swiglu_step", "swiglu_step", "elem", "SwiGLU clipped after the SiLU", DT_FLOAT,
ref_swiglu_step),
SIMPLE("ref_mla_project", "mla_project", "attn", "absorbed MLA projections", DT_FLOAT,
ref_mla_project),
SIMPLE("ref_swiglu_clamped", "swiglu_clamped", "elem", "asymmetrically clipped SwiGLU", DT_FLOAT,
ref_swiglu_clamped),
SIMPLE("ref_silu_mul", "silu_mul", "elem", "silu-gated product over a fused gate_up", DT_FLOAT,
ref_silu_mul),
SIMPLE("ref_act_mul", "act_mul", "elem", "act(a) * b over two operands", DT_FLOAT, ref_act_mul),
SIMPLE("ref_mul_weight", "mul_weight", "elem", "x * a scalar or one-row weight", DT_FLOAT,
ref_mul_weight),
SIMPLE("ref_table_add", "table_add", "elem", "a row plus each of L table rows", DT_FLOAT,
ref_table_add),
SIMPLE("ref_mix_rows", "mix_rows", "elem", "a weighted sum of k consecutive rows", DT_FLOAT,
ref_mix_rows),
SIMPLE("ref_chan_stats", "chan_stats", "elem", "per-channel statistics held as weights",
DT_FLOAT, ref_chan_stats),
SIMPLE("ref_seg_mean_bf16_sum", "seg_mean_bf16_sum", "elem", "BF16 sum/division mean pooling",
"bf16 in and out; f32 sum, BF16 sum and quotient", ref_seg_mean_bf16_sum),
SIMPLE("ref_seg_mean", "seg_mean", "elem", "mean of each cu_seqlens segment's rows", DT_FLOAT,
ref_seg_mean),
SIMPLE("ref_seg_softmax_pool", "seg_softmax_pool", "elem",
"each cu_seqlens segment's rows summed with the softmax of their scores", DT_FLOAT,
ref_seg_softmax_pool),
SIMPLE("ref_pool_rows", "pool_rows", "elem", "mean of every k consecutive rows, then a multiplier",
DT_FLOAT, ref_pool_rows),
SIMPLE("ref_pool_cols", "pool_cols", "elem", "mean of every k consecutive columns, then a "
"multiplier", DT_FLOAT, ref_pool_cols),
SIMPLE("ref_point_fourier", "point_fourier", "elem", "a point's sines and cosines at octaves",
DT_FLOAT, ref_point_fourier),
SIMPLE("ref_relu", "relu", "elem", "max(x, 0)", DT_FLOAT, ref_relu),
SIMPLE("ref_clamp", "clamp", "elem", "x clamped to two weight scalars", DT_FLOAT, ref_clamp),
SIMPLE("ref_conv2d", "conv2d", "elem", "segment-wise 2-D convolution, channels innermost",
DT_FLOAT, ref_conv2d),
SIMPLE("ref_conv2d_unfold", "conv2d_unfold", "elem",
"a convolution's receptive fields as rows, for a GEMM against its weight", DT_FLOAT,
ref_conv2d_unfold),
SIMPLE("ref_conv1d_dw", "conv1d_dw", "elem", "segment-wise causal depthwise convolution",
DT_FLOAT, ref_conv1d_dw),
SIMPLE("ref_rel_sinusoid", "rel_sinusoid", "elem", "sinusoidal relative-position table", DT_FLOAT,
ref_rel_sinusoid),
SIMPLE("ref_seg_sinusoid", "seg_sinusoid", "elem",
"sinusoidal absolute positions, restarting every `period` rows of a segment", DT_FLOAT,
ref_seg_sinusoid),
SIMPLE("ref_attn_local_rel", "attn_local_rel", "attn",
"local attention with a relative-position term and a softcap", DT_FLOAT,
ref_attn_local_rel),
SIMPLE("ref_gelu", "gelu", "elem", "exact erf gelu", DT_FLOAT, ref_gelu),
SIMPLE("ref_silu", "silu", "elem", "silu", DT_FLOAT, ref_silu),
SIMPLE("ref_sigmoid", "sigmoid", "elem", "logistic sigmoid", DT_FLOAT, ref_sigmoid),
SIMPLE("ref_fill", "fill", "elem", "constant fill", DT_FLOAT, ref_fill),
SIMPLE("ref_gelu_mul", "gelu_mul", "elem",
"tanh-gelu-gated product over a fused gate_up (gemma's gelu_pytorch_tanh)", DT_FLOAT,
ref_gelu_mul),
SIMPLE("ref_logit_softcap", "logit_softcap", "elem", "final-logit softcap: cap * tanh(x / cap)",
DT_FLOAT, ref_logit_softcap),
SIMPLE("ref_scale", "scale", "elem", "per-tensor scalar scale: y = x * s (s a [1] weight)",
DT_FLOAT, ref_scale),
SIMPLE("ref_ngram_ids", "ngram_ids", "vocab", "hashed n-gram row ids for PLE", DT_ANY,
ref_ngram_ids),
SIMPLE("ref_ple_gate", "ple_gate", "norm", "PLE gate: three grouped norms, a dot, a signed sqrt",
DT_FLOAT, ref_ple_gate),
SIMPLE("ref_ple_conv", "ple_conv", "norm", "PLE's dilated depthwise conv, silu and residual add",
DT_FLOAT, ref_ple_conv),
SIMPLE("ref_ple_conv_bias", "ple_conv_bias", "norm",
"ple_conv with a per-channel bias inside the silu (Mamba-2's conv1d)", DT_FLOAT,
ref_ple_conv_bias),
SIMPLE("ref_causal_conv1d", "causal_conv1d", "conv", "depthwise causal conv, no activation",
DT_FLOAT, ref_causal_conv1d),
SIMPLE("ref_sconv_res", "sconv_res", "conv",
"depthwise causal conv with its residual and optional input-dependent taps", DT_FLOAT,
ref_sconv_res),
SIMPLE("ref_gather_rows", "gather_rows", "elem", "y[i,:] = x[idx[i],:]", DT_ANY, ref_gather_rows),
SIMPLE("ref_scatter_rows", "scatter_rows", "elem", "x[idx[i],:] = v[i,:]", DT_ANY,
ref_scatter_rows),
SIMPLE("ref_cast", "cast", "elem", "dtype conversion", DT_ANY, ref_cast),
SIMPLE("ref_softmax", "softmax", "elem", "row-wise softmax", DT_FLOAT, ref_softmax),
SIMPLE("ref_quant_act_i8", "quant_act_i8", "quant",
"symmetric int8 activation quantiser, one scale a row, codes in the WMMA step order",
"float in, i8 out, f32 scale", ref_quant_act_i8),
SIMPLE("ref_quant_act_i8g", "quant_act_i8g", "quant",
"symmetric int8 activation quantiser, one scale per group, row-major",
"float in, i8 out, f32 scale", ref_quant_act_i8g),
SIMPLE("ref_quant_act_fp8", "quant_act_fp8", "quant", "symmetric block-scaled E4M3 quantiser",
"float in, fp8_e4m3 out, f32 scale", ref_quant_act_fp8),
SIMPLE("ref_quant_cols_fp8", "quant_cols_fp8", "quant", "E4M3 with one scale per column",
"float in, fp8_e4m3 out, f32 scale", ref_quant_cols_fp8),
/* The fused fp8 forms, so a host-backend run can start on an fp8 model (see ref_quant.cpp). */
SIMPLE("ref_rmsnorm_quant_fp8", "rmsnorm_quant_fp8", "quant",
"RMS norm, optional residual add, and the E4M3 quantiser in one pass",
"bf16 in, fp8_e4m3 out, f32 scale", ref_rmsnorm_quant_fp8),
SIMPLE("ref_gated_quant_fp8", "gated_quant_fp8", "quant",
"act(gate) * up over a packed [M, 2n] plane, then the E4M3 quantiser",
"bf16 in, fp8_e4m3 out, f32 scale", ref_gated_quant_fp8),
SIMPLE("ref_gemm_nt_q_gated", "gemm_nt_q_gated", "gemm",
"gemm_nt_q over a gate_up plane with the gated quantiser in the epilogue",
"fp8_e4m3 operands, fp8_e4m3 out, f32 scale", ref_gemm_nt_q_gated),
SIMPLE("ref_dflash_select", "dflash_select", "draft",
"DFlash2's greedy walk over the candidate lattice",
"i32 candidates, bf16 codebooks, i32 tokens out", ref_dflash_select),
SIMPLE("ref_dflash_markov_greedy", "dflash_markov_greedy", "draft",
"DSpark's full-vocabulary greedy Markov walk",
"bf16 logits and codebooks, f32/i32 tile winners, i32 tokens out",
ref_dflash_markov_greedy),
SIMPLE("ref_dflash_markov_greedy_f32", "dflash_markov_greedy_f32", "draft",
"Full-vocabulary greedy Markov walk with F32 base logits",
"f32 logits, bf16 codebooks, f32/i32 tile winners, i32 tokens out",
ref_dflash_markov_greedy_f32),
SIMPLE("ref_dflash_tree", "dflash_tree", "draft",
"DFlash2's candidate lattice grown best-first into a tree",
"i32 candidates, bf16 codebooks, i32 tokens and parents out", ref_dflash_tree),
SIMPLE("ref_had_quant_act_i8g", "had_quant_act_i8g", "quant",
"width-`group` Hadamard (128-wide rotations where `group` is wider), then the symmetric "
"int8 quantiser, one scale per group",
"float in, i8 out, f32 scale per group; n a multiple of group",
ref_had_quant_act_i8g),
SIMPLE("ref_gram_accum", "gram_accum", "quant",
"h += x^T x over a dequantised E4M3 activation, or a plain bf16 one -- the Gram matrix "
"GPTQ calibrates on",
"fp8_e4m3 plus an f32 scale per group, or bf16 with no scale, in; an f32 [n][n] "
"accumulator READ and added to",
ref_gram_accum),
SIMPLE("ref_had_quant_act_i8", "had_quant_act_i8", "quant",
"Walsh-Hadamard rotation then symmetric int8", "float in, i8 out, f32 scale",
ref_had_quant_act_i8),
SIMPLE("ref_rmsnorm_had_quant_i8", "rmsnorm_had_quant_i8", "quant",
"RMS norm, rotation and int8 in one pass", "float in, i8 out, f32 scale",
ref_rmsnorm_had_quant_i8),
SIMPLE("ref_gated_had_quant_i8", "gated_had_quant_i8", "quant",
"gated product, rotation and int8 in one pass", "float in, i8 out, f32 scale",
ref_gated_had_quant_i8),
SIMPLE("ref_dequant", "dequant", "quant", "group-scaled dequantisation", DT_ANY, ref_dequant),
SIMPLE("ref_dequant_w4", "dequant_w4", "quant", "packed int4 weight to row-major bf16", DT_ANY,
ref_dequant_w4),
SIMPLE("ref_gemm_nt", "gemm_nt", "gemm", "C = A B^T, f32 accumulate", DT_FLOAT, ref_gemm_nt),
SIMPLE("ref_gemm_nt_bias", "gemm_nt_bias", "gemm", "C = A B^T + bias", DT_FLOAT,
ref_gemm_nt_bias),
ROWL_GQ("ref_gemm_nt_q", "gemm_nt_q", "gemm", "C = A B^T with group-scaled quantised operands",
"i8 / fp8 / float A; w4, w2, mxfp4, m2xfp4, i8 or float B; u2 / u4 / u5 codes with an f16 "
"scale and integer zero per group, joined; f32 accumulate",
ref_check_gemm_q, ref_gemm_nt_q_m2, ref_layout_gemm_q_m2, ref_relayout_gemm_q_m2,
ref_unrelayout_gemm_q_m2),
SIMPLE("ref_gemm_nt_q_bias", "gemm_nt_q_bias", "gemm", "quantised C = A B^T + bias",
"i8 / fp8 / float A; w4, w2, mxfp4, i8 or float B; f32 accumulate", ref_gemm_nt_q_bias),
SIMPLE("ref_lora_shrink", "lora_shrink", "gemm", "t = x A[slot]^T per row, base rows untouched",
"bf16 / f16 / f32 a, or fp8_e4m3 / fp8_e5m2 / i8 a with its f32 scale stream; bf16 lora_a; "
"i32 slot; f32 t", ref_lora_shrink),
SIMPLE("ref_lora_expand", "lora_expand", "gemm",
"y += scale * (t B[slot]^T) per row, base rows untouched",
"f32 t and scale; bf16 lora_b; i32 slot; bf16 / f16 / f32 y", ref_lora_expand),
SIMPLE("ref_lora_expand_mm", "lora_expand_mm", "gemm",
"y += scale * (bf16(t) B[slot]^T) per row, base rows untouched",
"f32 t and scale; bf16 lora_b; i32 slot; bf16 / f16 / f32 y", ref_lora_expand_mm),
ROW("ref_attn_paged", "attn_paged", "attn",
"paged causal / sliding-window attention, any query length",
"any q and out dtype; bf16 or fp8 kv cache with optional per-(seq,head) descales",
cPagedBlock, (int)(sizeof(cPagedBlock) / sizeof(cPagedBlock[0])),
nullptr, nullptr, ref_attn_paged, nullptr),
ROW("ref_attn_paged_anchor_window", "attn_paged_anchor_window", "attn",
"paged attention with an exact shared anchor-aligned sliding window",
"any q and out dtype; bf16 or fp8 kv cache with optional per-(seq,head) descales",
cPagedBlock, (int)(sizeof(cPagedBlock) / sizeof(cPagedBlock[0])),
nullptr, nullptr, ref_attn_paged_anchor_window, nullptr),
ROW("ref_attn_paged_tree", "attn_paged_tree", "attn",
"paged causal / sliding-window attention with a tree verify block (spec_parent)",
"any q and out dtype; bf16 or fp8 kv cache with optional per-(seq,head) descales; i32 parents",
cPagedBlock, (int)(sizeof(cPagedBlock) / sizeof(cPagedBlock[0])),
nullptr, nullptr, ref_attn_paged_tree, nullptr),
ROW("ref_attn_paged_sink", "attn_paged_sink", "attn",
"paged causal / sliding-window attention with a sink logit a head, any query length",
"any q and out dtype; bf16 or fp8 kv cache with optional per-(seq,head) descales; f32 sinks",
cPagedBlock, (int)(sizeof(cPagedBlock) / sizeof(cPagedBlock[0])),
nullptr, nullptr, ref_attn_paged_sink, nullptr),
ROW("ref_attn_paged_rel", "attn_paged_rel", "attn",
"paged causal / sliding-window attention with a relative-position bias and a "
"sink logit per row",
"any q and out dtype; bf16, f32 or int8 kv cache with optional per-(seq,head) descales; f32 "
"rel_proj, r and sinks at any float dtype",
cPagedBlock, (int)(sizeof(cPagedBlock) / sizeof(cPagedBlock[0])),
nullptr, nullptr, ref_attn_paged_rel, nullptr),
SIMPLE("ref_attn_dense", "attn_dense", "attn", "dense varlen attention over cu_seqlens", DT_FLOAT,
ref_attn_dense),
SIMPLE("ref_attn_umt5", "attn_umt5", "attn", "UMT5 signed relative-position attention",
"bf16, h64", ref_attn_umt5),
SIMPLE("ref_attn_cross", "attn_cross", "attn",
"dense varlen attention, keys bounded apart from the queries (cu_k)", DT_FLOAT,
ref_attn_cross),
SIMPLE("ref_attn_na3d", "attn_na3d", "attn", "3-D neighborhood attention over a volume", DT_FLOAT,
ref_attn_na3d),
SIMPLE("ref_sam_attn", "sam_attn", "attn",
"SAM tower attention: windowed tiles with decomposed relative position", DT_FLOAT,
ref_sam_attn),
ROW("ref_kv_store", "kv_store", "attn", "scatter k and v into the paged cache", DT_ANY,
cPagedBlock, (int)(sizeof(cPagedBlock) / sizeof(cPagedBlock[0])),
nullptr, nullptr, ref_kv_store, nullptr),
ROW("ref_kv_store_streamed", "kv_store_streamed", "attn",
"scatter k and v into the host mirror always and into the resident ring slot", DT_ANY,
nullptr, 0, nullptr, nullptr, ref_kv_store_streamed, nullptr),
SIMPLE("ref_rope", "rope", "attn", "rotary embedding in place", DT_FLOAT, ref_rope),
SIMPLE("ref_embed_attn", "embed_attn", "attn", "BF16 materialized encoder attention",
"bf16/f32, i32 cu", ref_embed_attn),
SIMPLE("ref_ministral_rope", "ministral_rope", "attn", "Ministral BF16 rotary",
"bf16/f32 inout, i32 positions", ref_ministral_rope),
SIMPLE("ref_pixtral_rope", "pixtral_rope", "attn", "Pixtral interleaved axial rotary",
"bf16/f32 inout, i32 coordinates", ref_pixtral_rope),
SIMPLE("ref_pixtral_merge", "pixtral_merge", "vision", "channel-major patch merger packing",
"bf16/f32, i32 coordinates", ref_pixtral_merge),
SIMPLE("ref_ds41_pack3x3", "ds41_pack3x3", "vision", "3x3 zero-padded raster patch packing",
"bf16/f32, i32 coordinates", ref_ds41_pack3x3),
SIMPLE("ref_ds41_span_rows", "ds41_span_rows", "vision",
"learned span rows into the typed-row table", "bf16 weights and table", ref_ds41_span_rows),
SIMPLE("ref_ds41_vit_rope2d", "ds41_vit_rope2d", "attn",
"DeepSeek-V4.1 tower shared-ladder 2-D rotary", "bf16/f32 inout, i32 coordinates",
ref_ds41_vit_rope2d),
SIMPLE("ref_position_scale", "position_scale", "attn", "Ministral position-dependent query scale",
"bf16/f32 inout, i32 positions", ref_position_scale),
SIMPLE("ref_qk_norm_rope", "qk_norm_rope", "attn", "per-head QK norm then rotary embedding",
DT_FLOAT, ref_qk_norm_rope),
SIMPLE("ref_scale_rows", "scale_rows", "elem", "one scalar per row, broadcast across the row",
DT_FLOAT, ref_scale_rows),
SIMPLE("ref_l2_clip", "l2_clip", "elem",
"per-group L2 clip with an optional scale and a position tau",
DT_FLOAT, ref_l2_clip),
SIMPLE("ref_router_sigmoid_group_topk", "router_sigmoid_group_topk", "moe",
"group-limited sigmoid correction-bias routing", DT_FLOAT, ref_router_sigmoid_group_topk),
SIMPLE("ref_router_sigmoid_topk", "router_sigmoid_topk", "moe", "sigmoid correction-bias routing",
DT_FLOAT,
ref_router_sigmoid_topk),
SIMPLE("ref_router_scored_topk", "router_scored_topk", "moe",
"correction-bias routing over named scores", DT_FLOAT,
ref_router_scored_topk),
SIMPLE("ref_router_scored_topk_vl", "router_scored_topk_vl", "moe",
"correction-bias routing with a second bias for image-span rows", DT_FLOAT,
ref_router_scored_topk_vl),
SIMPLE("ref_router_topk", "router_topk", "moe", "softmax then exact top-k routing", DT_FLOAT,
ref_router_topk),
SIMPLE("ref_moe_scatter", "moe_scatter", "moe", "stable counting sort of routing slots by expert",
"i32 indices", ref_moe_scatter),
SIMPLE("ref_router_topk_scatter", "router_topk_scatter", "moe",
"the selection and the sort in one op, run as exactly the pair", DT_FLOAT,
ref_router_topk_scatter),
SIMPLE("ref_router_gemm_topk_scatter", "router_gemm_topk_scatter", "moe",
"the router GEMM, the selection and the sort in one op, run as exactly the launches",
DT_FLOAT, ref_router_gemm_topk_scatter),
SIMPLE("ref_moe_gemm", "moe_gemm", "moe", "grouped GEMM over the expert-sorted rows", DT_FLOAT,
ref_moe_gemm),
ROW_CB("ref_moe_gemm_q", "moe_gemm_q", "moe",
"grouped GEMM over the expert-sorted rows, quantised operands", DT_FLOAT_CB,
ref_moe_gemm_q),
ROW_CB("ref_moe_gemm_q_mod4", "moe_gemm_q_mod4", "moe",
"grouped GEMM over the expert-sorted rows, quantised operands, four classes by number mod 4",
DT_FLOAT_CB, ref_moe_gemm_q_mod4),
SIMPLE("ref_moe_gather", "moe_gather", "moe", "weighted scatter back to token order", DT_FLOAT,
ref_moe_gather),
SIMPLE("ref_rope_table", "rope_table", "attn", "cos/sin at each position's own row", DT_FLOAT,
ref_rope_table),
SIMPLE("ref_ds41_rope_rows", "ds41_rope_rows", "attn",
"rope from a cos/sin plane over a window of each head", DT_FLOAT, ref_rope_rows),
SIMPLE("ref_quant_roundtrip", "quant_roundtrip", "quant",
"block-scaled fp8/fp4 quantise-dequantise in place", DT_FLOAT, ref_quant_roundtrip),
SIMPLE("ref_softmax_pool", "softmax_pool", "attn", "channel-wise softmax pooling of ratio rows",
DT_FLOAT, ref_softmax_pool),
SIMPLE("ref_token_map", "token_map", "embed", "ids through an id table", DT_ANY, ref_token_map),
SIMPLE("ref_stream_gate_add", "stream_gate_add", "norm",
"normalized-dot gated write into hc streams", DT_FLOAT, ref_stream_gate_add),
SIMPLE("ref_entry_store", "entry_store", "attn", "one entry a block into a paged cache", DT_FLOAT,
ref_entry_store),
SIMPLE("ref_index_score", "index_score", "attn",
"indexer relu-gated head scores over a paged index cache", DT_FLOAT, ref_index_score),
SIMPLE("ref_index_candidates", "index_candidates", "attn", "two-level top-k: kept candidate blocks",
DT_ANY, ref_index_candidates),
SIMPLE("ref_index_topk", "index_topk", "attn", "top-k passed positions in position order", DT_ANY,
ref_index_topk),
SIMPLE("ref_attn_sparse_sink", "attn_sparse_sink", "attn",
"window plus selected compressed latent attention with sinks", DT_FLOAT,
ref_attn_sparse_sink),
SIMPLE("ref_qsa_block_key", "qsa_block_key", "attn",
"QSA compressed key: pooled, normed and rotated at the block's position", DT_FLOAT,
ref_qsa_block_key),
SIMPLE("ref_qsa_score", "qsa_score", "attn",
"QSA block scores: per-head relu dots summed over the heads", DT_FLOAT, ref_qsa_score),
SIMPLE("ref_qsa_select", "qsa_select", "attn",
"QSA selection: the top-k blocks as a page-id table, partial page last",
"f32 scores, i32 tables", ref_qsa_select),
SIMPLE("ref_qsa_tail_store", "qsa_tail_store", "attn",
"QSA tail: the raw keys of open blocks into a per-sequence ring", DT_FLOAT,
ref_qsa_tail_store),
SIMPLE("ref_qsa_work", "qsa_work", "attn",
"QSA work list: completing blocks' keys, pages and positions", DT_FLOAT, ref_qsa_work),
SIMPLE("ref_kv_resolve", "kv_resolve", "attn",
"streamed KV residency map: selected block ids become ring slots, misses copied in",
"i32 maps, u8 block planes", ref_kv_resolve),
SIMPLE("ref_dflash_conv", "dflash_conv", "draft",
"DFlash2 grouped dynamic depthwise convolution", DT_FLOAT, ref_dflash_conv),
SIMPLE("ref_row_topk", "row_topk", "moe", "exact per-row top-R", DT_FLOAT, ref_row_topk),
SIMPLE("ref_row_topk_merge", "row_topk_merge", "moe",
"the global top-R of the gathered per-rank candidate sets", DT_FLOAT, ref_row_topk_merge),
SIMPLE("ref_pair_argmax", "pair_argmax", "moe",
"the global top-1 of a row of gathered (id, value) pairs, one id a row", DT_FLOAT,
ref_pair_argmax),
ROWL("ref_logit_rerank", "logit_rerank", "moe",
"R candidates a row rescored exactly against the full-precision head",
DT_FLOAT ", or a head of E4M3 / int8 codes with a scale plane, joined",
ref_check_dtypes, ref_logit_rerank, ref::ref_layout_head),
SIMPLE("ref_gdn_conv_prep", "gdn_conv_prep", "gdn",
"gdn prefill preamble: conv, split, l2 norm, gate cumsum, beta", DT_FLOAT,
ref_gdn_conv_prep),
SIMPLE("ref_gdn_conv_update", "gdn_conv_update", "gdn",
"gdn decode conv over the rolling speculative window", DT_FLOAT, ref_gdn_conv_update),
SIMPLE("ref_gdn_kkt_solve", "gdn_kkt_solve", "gdn", "gdn chunk preamble: the triangular inverse",
DT_FLOAT, ref_gdn_kkt_solve),
SIMPLE("ref_gdn_chunk_scan", "gdn_chunk_scan", "gdn", "gdn chunked scan", DT_FLOAT,
ref_gdn_chunk_scan),
SIMPLE("ref_gdn_recurrent_update", "gdn_recurrent_update", "gdn",
"gdn decode recurrence against the paged state cache, per-candidate or anchor state",
DT_FLOAT, ref_gdn_recurrent_update),
SIMPLE("ref_gdn_conv_recurrent_update", "gdn_conv_recurrent_update", "gdn",
"gdn_conv_update then gdn_recurrent_update, composed from those two references", DT_FLOAT,
ref_gdn_conv_recurrent_update),
SIMPLE("ref_gdn_conv_update_tree", "gdn_conv_update_tree", "gdn",
"gdn decode conv over a tree verify's rows (spec_parent)", DT_FLOAT,
ref_gdn_conv_update_tree),
SIMPLE("ref_gdn_recurrent_update_tree", "gdn_recurrent_update_tree", "gdn",
"gdn decode recurrence over a tree verify's rows (spec_parent), per-candidate or anchor "
"state", DT_FLOAT, ref_gdn_recurrent_update_tree),
SIMPLE("ref_gdn_conv_recurrent_update_tree", "gdn_conv_recurrent_update_tree", "gdn",
"gdn_conv_update_tree then gdn_recurrent_update_tree, composed from those two references",
DT_FLOAT, ref_gdn_conv_recurrent_update_tree),
SIMPLE("ref_gdn_gated_rmsnorm", "gdn_gated_rmsnorm", "gdn", "gated RMS norm over a head", DT_FLOAT,
ref_gdn_gated_rmsnorm),
ROW("ref_all_reduce", "all_reduce", "ar", "sum across ranks, f32, ascending rank order", DT_FLOAT,
nullptr, 0, ref_collective_init, ref_collective_fini, ref_all_reduce, ref_all_reduce_scratch),
ROW("ref_all_gather", "all_gather", "ar", "gather every rank's shard", DT_FLOAT,
nullptr, 0, ref_collective_init, ref_collective_fini, ref_all_gather, nullptr),
SIMPLE("ref_embed_lookup", "embed_lookup", "vocab", "embedding gather", DT_FLOAT,
ref_embed_lookup),
SIMPLE("ref_embed_lookup_q", "embed_lookup_q", "vocab", "embedding gather, one scale for the table",
DT_ANY, ref_embed_lookup_q),
ROW_OWN("ref_embed_lookup_planes", "embed_lookup_planes", "vocab",
"embedding gather from a table's affine planes in place",
"i8 u8 i4 u4 codes, any float scale and table, per operand", ref_embed_lookup_planes),
ROWL("ref_logits_gemm", "logits_gemm", "vocab", "the lm_head projection",
DT_FLOAT ", or a head of E4M3 / int8 codes with a scale plane, joined",
ref_check_dtypes, ref_logits_gemm, ref::ref_layout_head),
SIMPLE("ref_sample_penalties", "sample_penalties", "sample",
"repetition, frequency and presence penalties", DT_FLOAT, ref_sample_penalties),
SIMPLE("ref_sample_temp", "sample_temp", "sample", "per-request temperature", DT_FLOAT,
ref_sample_temp),
SIMPLE("ref_sample_topk", "sample_topk", "sample", "exact top-k candidate set", DT_FLOAT,
ref_sample_topk),
SIMPLE("ref_sample_topp", "sample_topp", "sample", "nucleus narrowing in place", DT_FLOAT,
ref_sample_topp),
SIMPLE("ref_sample_minp", "sample_minp", "sample", "min-p narrowing in place", DT_FLOAT,
ref_sample_minp),
SIMPLE("ref_sample_mask", "sample_mask", "sample", "grammar bitmask application", DT_FLOAT,
ref_sample_mask),
SIMPLE("ref_sample_pick", "sample_pick", "sample", "seeded inverse-CDF draw", DT_FLOAT,
ref_sample_pick),
SIMPLE("ref_sample_argmax", "sample_argmax", "sample", "greedy pick", DT_FLOAT, ref_sample_argmax),
SIMPLE("ref_sample_logprobs", "sample_logprobs", "sample", "the raw distribution's summary",
DT_FLOAT, ref_sample_logprobs),
SIMPLE("ref_sample_dry", "sample_dry", "sample", "DRY repetition-suffix penalty", DT_FLOAT,
ref_sample_dry),
SIMPLE("ref_sample_ngram_ban", "sample_ngram_ban", "sample",
"windowed no-repeat-ngram ban", DT_FLOAT, ref_sample_ngram_ban),
SIMPLE("ref_sample_typical", "sample_typical", "sample", "locally typical narrowing in place",
DT_FLOAT, ref_sample_typical),
SIMPLE("ref_sample_xtc", "sample_xtc", "sample", "exclude-top-choices narrowing in place",
DT_FLOAT, ref_sample_xtc),
SIMPLE("ref_sample_merge_topk", "sample_merge_topk", "sample",
"vocab-parallel candidate merge", DT_FLOAT, ref_sample_merge_topk),
SIMPLE("ref_ln_mod", "ln_mod", "norm", "gainless layer norm times (1 + a sequence's scale row)",
DT_FLOAT, ref_ln_mod),
SIMPLE("ref_gate_add", "gate_add", "elem", "x += act(gate row) * y, act tanh or none", DT_FLOAT,
ref_gate_add),
SIMPLE("ref_timestep_embed", "timestep_embed", "elem", "sinusoidal timestep embedding",
DT_FLOAT, ref_timestep_embed),
SIMPLE("ref_gather_rows2", "gather_rows2", "elem", "rows from two sources by a signed index",
DT_ANY, ref_gather_rows2),
SIMPLE("ref_lane_gather", "lane_gather", "elem", "each entry's rows out of its lane", DT_ANY,
ref_lane_gather),
SIMPLE("ref_lane_store", "lane_store", "elem", "one source's rows into several lanes", DT_ANY,
ref_lane_store),
SIMPLE("ref_randn_lanes", "randn_lanes", "elem", "a lane's rows from its seed's normal stream",
DT_FLOAT, ref_randn_lanes),
SIMPLE("ref_flow_euler", "flow_euler", "elem", "a flow-matching Euler step with guidance",
DT_FLOAT, ref_flow_euler),
SIMPLE("ref_kd_input", "kd_input", "elem", "a k-diffusion sampler's model input", DT_FLOAT,
ref_kd_input),
SIMPLE("ref_kd_step", "kd_step", "elem", "a k-diffusion sampler step with guidance", DT_FLOAT,
ref_kd_step),
SIMPLE("ref_chan_affine", "chan_affine", "elem", "a per-channel affine from parameter lists",
DT_FLOAT, ref_chan_affine),
SIMPLE("ref_conv2d_nhwc", "conv2d_nhwc", "elem", "an image convolution, channels innermost",
DT_FLOAT, ref_conv2d_nhwc),
SIMPLE("ref_im2col_nhwc", "im2col_nhwc", "elem", "a convolution's receptive fields as rows",
DT_FLOAT, ref_im2col_nhwc),
SIMPLE("ref_l2norm_chan", "l2norm_chan", "norm", "unit-L2 channel norm, scaled, optional silu",
DT_FLOAT, ref_l2norm_chan),
SIMPLE("ref_d2s_add", "d2s_add", "elem", "an upsampling shortcut added in", DT_FLOAT,
ref_d2s_add),
SIMPLE("ref_s2d_avg_add", "s2d_avg_add", "elem", "a downsampling shortcut added in", DT_FLOAT,
ref_s2d_avg_add),
SIMPLE("ref_to_rgba8", "to_rgba8", "elem", "decoded pixels to bytes", DT_FLOAT, ref_to_rgba8),
SIMPLE("ref_gelu_umt5", "gelu_umt5", "elem", "Wan UMT5 tanh GELU", "bf16", ref_gelu_umt5),
SIMPLE("ref_gelu_tanh", "gelu_tanh", "elem", "tanh gelu", DT_FLOAT, ref_gelu_tanh),
SIMPLE("ref_gelu_quick", "gelu_quick", "elem", "quick gelu", DT_FLOAT, ref_gelu_quick),
SIMPLE("ref_scatter_add_rows", "scatter_add_rows", "elem", "x[idx[i]] += v[i]", DT_FLOAT,
ref_scatter_add_rows),
SIMPLE("ref_attn_prefix_self", "attn_prefix_self", "attn",
"attention over a paged prefix and the step's own rows", DT_FLOAT, ref_attn_prefix_self),
SIMPLE("ref_rms_mod", "rms_mod", "norm",
"gated residual, RMS norm with a gain times (1 + scale row) plus a shift row", DT_FLOAT,
ref_rms_mod),
SIMPLE("ref_flow_x0_step", "flow_x0_step", "elem", "a sampler step toward the clean estimate",
DT_FLOAT, ref_flow_x0_step),
SIMPLE("ref_flow_res_multistep", "flow_res_multistep", "elem",
"a second-order sampler step on the clean estimate", DT_FLOAT, ref_flow_res_multistep),
SIMPLE("ref_patch_unfold", "patch_unfold", "elem", "patch rows to denormalised voxel rows",
DT_FLOAT, ref_patch_unfold),
SIMPLE("ref_vtile_gather", "vtile_gather", "elem", "a video decoder's tiles out of the latent",
DT_FLOAT, ref_vtile_gather),
SIMPLE("ref_vtile_stitch", "vtile_stitch", "elem",
"decoded tiles blended into frames, the chunk overlap, bytes out", DT_FLOAT,
ref_vtile_stitch),
SIMPLE("ref_vtile_plan", "vtile_plan", "elem", "the tiled encoder's per-tile geometry table",
DT_ANY, ref_vtile_plan),
SIMPLE("ref_vtile_crop", "vtile_crop", "elem", "a frame's pixel plane cut into encode tiles",
DT_FLOAT, ref_vtile_crop),
SIMPLE("ref_vtile_join", "vtile_join", "elem",
"per-tile encoder moments blended into the latent canvas", DT_FLOAT, ref_vtile_join),
SIMPLE("ref_grid_pos3", "grid_pos3", "elem", "a video decoder's rotary positions over its tiles",
DT_FLOAT, ref_grid_pos3),
SIMPLE("ref_vsa_select", "vsa_select", "attn",
"video sparse attention's top-k tiles by pooled score, and the coarse branch",
"float q, k, v and coarse; i32 segs, geom and sel", ref_vsa_select),
SIMPLE("ref_attn_vsa", "attn_vsa", "attn",
"block-sparse attention over the prefix and selected video tiles, the gated coarse term",
"float q, k, v, coarse, gate and out; i32 segs, geom and sel", ref_attn_vsa),
SIMPLE("ref_conv3d_ndhwc", "conv3d_ndhwc", "conv",
"a 3-D convolution over voxel rows, channels innermost", DT_FLOAT, ref_conv3d_ndhwc),
SIMPLE("ref_group_norm", "group_norm", "norm",
"a group norm over all rows, optionally modulated, then an activation", DT_FLOAT,
ref_group_norm),
SIMPLE("ref_tconv_dw", "tconv_dw", "conv", "a depthwise convolution along a volume's frames",
DT_FLOAT, ref_tconv_dw),
SIMPLE("ref_resize_bilinear", "resize_bilinear", "elem",
"each frame of a volume resized bilinearly", DT_FLOAT, ref_resize_bilinear),
SIMPLE("ref_patch_fold", "patch_fold", "elem", "voxel rows to normalised patch rows", DT_FLOAT,
ref_patch_fold),
SIMPLE("ref_flow_renoise", "flow_renoise", "elem", "a clean latent re-noised to a sigma", DT_FLOAT,
ref_flow_renoise),
SIMPLE("ref_cond_aug", "cond_aug", "elem", "condition rows blended with fresh noise at a fixed aug",
DT_FLOAT, ref_cond_aug),
SIMPLE("ref_rope_split", "rope_split", "elem", "LTX-2's split rotary over coordinates", DT_FLOAT,
ref_rope_split),
SIMPLE("ref_head_gate", "head_gate", "elem", "each head times 2 sigmoid of its logit", DT_FLOAT,
ref_head_gate),
SIMPLE("ref_ltx_guide", "ltx_guide", "elem", "LTX-2's guided sampler step", DT_FLOAT,
ref_ltx_guide),
SIMPLE("ref_pixel_norm", "pixel_norm", "norm", "an RMS norm as bf16 tensors compute it", DT_FLOAT,
ref_pixel_norm),
SIMPLE("ref_d2s3d", "d2s3d", "elem", "depth to space over voxel rows", DT_FLOAT, ref_d2s3d),
SIMPLE("ref_s2d3d", "s2d3d", "elem", "space to depth over voxel rows", DT_FLOAT, ref_s2d3d),
SIMPLE("ref_pix_patch", "pix_patch", "elem", "an image to patch rows", DT_FLOAT, ref_pix_patch),
SIMPLE("ref_vol_index", "vol_index", "elem", "a volume's voxel coordinates", DT_ANY, ref_vol_index),
SIMPLE("ref_tile_grid", "tile_grid", "elem", "a tiled decode's boxes", DT_ANY, ref_tile_grid),
SIMPLE("ref_tile_crop", "tile_crop", "elem", "a tile out of a volume", DT_FLOAT, ref_tile_crop),
SIMPLE("ref_tile_stitch", "tile_stitch", "elem", "decoded tiles blended to RGB bytes", DT_FLOAT,
ref_tile_stitch),
SIMPLE("ref_stft_logmel", "stft_logmel", "elem", "a causal log-mel spectrogram", DT_FLOAT,
ref_stft_logmel),
SIMPLE("ref_bwe_up", "bwe_up", "elem", "a sinc-resampled waveform plus a residual, clamped",
DT_FLOAT, ref_bwe_up),
SIMPLE("ref_conv1d", "conv1d", "conv", "a 1-D convolution over sequences, channels innermost",
DT_FLOAT, ref_conv1d),
SIMPLE("ref_conv_t1d", "conv_t1d", "conv", "a transposed 1-D convolution growing each sequence",
DT_FLOAT, ref_conv_t1d),
SIMPLE("ref_snake", "snake", "elem", "the snake activation", DT_FLOAT, ref_snake),
SIMPLE("ref_snake_aa", "snake_aa", "elem", "the alias-free SnakeBeta: upsample, snake, downsample",
DT_FLOAT, ref_snake_aa),
SIMPLE("ref_cfg_topk_sample", "cfg_topk_sample", "sample", "a guided top-k pick a lane",
DT_FLOAT, ref_cfg_topk_sample),
SIMPLE("ref_rvq_embed", "rvq_embed", "elem", "a frame's embedding from its residual codes",
DT_FLOAT, ref_rvq_embed),
SIMPLE("ref_vq_encode", "vq_encode", "elem",
"a residual quantiser's step: the nearest code by cosine", DT_FLOAT, ref_vq_encode),
SIMPLE("ref_vq_euclidean", "vq_euclidean", "elem", "Euclidean residual quantiser", DT_FLOAT,
ref_vq_euclidean),
SIMPLE("ref_elu", "elu", "elem", "ELU, alpha 1", DT_FLOAT, ref_elu),
SIMPLE("ref_frame_mix_store", "frame_mix_store", "elem", "a frame's conditioning row into its lane",
DT_FLOAT, ref_frame_mix_store),
SIMPLE("ref_end_status", "end_status", "elem", "a frame's status from its first pick", DT_ANY,
ref_end_status),
SIMPLE("ref_ras_pick", "ras_pick", "elem", "a repetition-aware choice between two draws a lane",
DT_ANY, ref_ras_pick),
SIMPLE("ref_frame_store", "frame_store", "elem", "a frame's codes into its lane's history", DT_ANY,
ref_frame_store),
SIMPLE("ref_resample_nearest", "resample_nearest", "elem", "rows resampled nearest-neighbour",
DT_ANY, ref_resample_nearest),
SIMPLE("ref_fourier_embed", "fourier_embed", "elem", "random Fourier features of a time",
DT_FLOAT, ref_fourier_embed),
SIMPLE("ref_win_input", "win_input", "elem", "a denoiser's input from the window states",
DT_FLOAT, ref_win_input),
SIMPLE("ref_win_prep", "win_prep", "elem", "a window step's noise and overlap", DT_FLOAT,
ref_win_prep),
SIMPLE("ref_win_finish", "win_finish", "elem", "a window's end and the overlap it hands on",
DT_FLOAT, ref_win_finish),
SIMPLE("ref_chan_split", "chan_split", "elem", "channel groups to sequences", DT_ANY,
ref_chan_split),
SIMPLE("ref_interleave2", "interleave2", "elem", "two streams interleaved", DT_ANY,
ref_interleave2),
SIMPLE("ref_iota", "iota", "elem", "an arithmetic index sequence", DT_ANY, ref_iota),
SIMPLE("ref_cu_repeat", "cu_repeat", "elem", "bounds of equal sequences", DT_ANY, ref_cu_repeat),
SIMPLE("ref_int_affine", "int_affine", "elem", "integers derived from device geometry", DT_ANY,
ref_int_affine),
SIMPLE("ref_sub_i32", "sub_i32", "elem", "a device int32 vector less a device scalar", DT_ANY,
ref_sub_i32),
#ifdef REF_HAVE_GGML
ROW_GGML("ref_gemm_nt_ggml", "gemm_nt_ggml", "gemm", "llama.cpp native-block linear", DT_FLOAT,
ref_gemm_nt_ggml),
ROW_GGML("ref_moe_gemm_ggml", "moe_gemm_ggml", "moe",
"grouped GEMM over llama.cpp native-block experts", DT_FLOAT, ref_moe_gemm_ggml),
ROW_GGML("ref_logits_gemm_ggml", "logits_gemm_ggml", "vocab",
"lm_head over llama.cpp native blocks", DT_FLOAT, ref_logits_gemm_ggml),
ROW_GGML("ref_embed_lookup_ggml", "embed_lookup_ggml", "vocab",
"embedding gather from llama.cpp native blocks", DT_FLOAT, ref_embed_lookup_ggml),
#endif
/* The row-form names (see the schema table): each the base op's reference row. */
SIMPLE("ref_rmsnorm_rows", "rmsnorm_rows", "norm", "rmsnorm under its row-form name", DT_FLOAT,
ref_rmsnorm),
SIMPLE("ref_rope_rows", "rope_rows", "attn", "rope under its row-form name", DT_FLOAT, ref_rope),
};
/* ================================================================== plugin exports */
static const RadPluginInfo kInfo = {
RAD_PLUGIN_KERNEL,
"libref",
"0.1.0",
"every op in the conventional vocabulary, naively and generically: the fallback that makes an "
"unsupported model run at reduced speed instead of not at all, and the oracle rad-kbench runs "
"the resolved kernels against (spec.md §17)",
"host"
};
extern "C" uint32_t rad_plugin_abi_version(void) { return RAD_ABI_VERSION; }
RAD_PLUGIN_ABI_LAYOUT_EXPORT
extern "C" const RadPluginInfo* rad_plugin_info(void) { return &kInfo; }
/* Declares this plugin the reference through the ABI (RadPluginReferenceFn); the engine knows no
* library by name. */
extern "C" int rad_plugin_reference(void) { return 1; }
extern "C" int rad_kernel_schema_count(void) {
return (int)(sizeof(kSchemas) / sizeof(kSchemas[0]));
}
extern "C" const RadOpSchema* rad_kernel_schema_at(int i) {
if (i < 0 || i >= rad_kernel_schema_count()) return nullptr;
return &kSchemas[i];
}
extern "C" int rad_kernel_count(void) {
return (int)(sizeof(kKernels) / sizeof(kKernels[0]));
}
extern "C" const RadKernelInfo* rad_kernel_at(int i) {
if (i < 0 || i >= rad_kernel_count()) return nullptr;
return &kKernels[i];
}
/* The rows that join the other ranks (ref_collective.cpp), via RadKernelCollectiveFn; covers the
* all-gather as well as the all-reduce. */
extern "C" int rad_kernel_collective(int i) {
const RadKernelInfo* k = rad_kernel_at(i);
return k && k->init == ref_collective_init;
}