Expand description
The Qwen-family recognizers — the ops beyond matmul / elementwise / fused-row / swiglu that an authored
transformer forward is made of, each lifted from the EXACT statement shape its parser sugar desugars to
(parser/mod.rs, the desugar_tensor_* family) into a typed spec.
Shared the way super is: the CUDA orchestrator’s stage walk adapts these specs into its tuples, and
the spatial front end’s whole-program walk composes them. Recognition is structural — no markers, no
naming conventions — and every recognizer here is keyed on its own sugar’s body, so a composite block is
never claimed as one of its parts and a neighbouring sugar with the same skeleton (silu / gelu, rope /
transpose) is told apart by the sub-expression that differs. Each answers None for anything else.
Structs§
- ArgMax
Spec t = argmax of X— the 1-based index of the first maximal element ofX’snelements, an Int.- Attention
Spec ctx = attention(q over kc, vc)— single-query multi-head GQA attention:qis1 × (h·hd),kcisl × (kv·hd)andvcl × (kv·vd)(the cached keys/values), query headgattends to kv headg / (h/kv)and writesvdvalues.posis the position bound of the maskedup to Pform (the KV cache as carried state);Noneis the unmasked form that attends to every cached row.- Causal
Conv Spec Y = causal conv(X, WC, CS)— the depthwise causal conv1d overchanchannels with ataps-deep decode ringCS(shifted up, appended withX, reduced againstWC).CSis read AND written: carried state.- Decay
Spec A = decay(Ar, Dt, Alog)— Gated DeltaNet’s per-head forget gate(1 + e^(Ar+Dt))^Alogovernheads.- Full
Attention Spec C = full attention of Q over K and V with H heads— bidirectional multi-head self-attention overtpositions;Q/K/V/Caret × (h·hd). Every query row attends to every position, unmasked.- Gated
Delta Spec O = gated delta(Q, K, V, Z, B, A, S, GN)— the Gated DeltaNet step overhvvalue heads readinghkkey heads at head dimd:u = S·k,S ← α·S + β·(v − α·u)·kᵀ,o = S·q, RMSNorm’d, scaled byGN, gated bysilu(z), the norm taking the program’s epsilon.S(st) is read AND written: the carried memory.- Gelu
Spec Y = gelu(X)— ggml’s tanh approximation in its one-exponential formx / (1 + e^-2z),z = √(2/π)·(x + 0.044715·x³), overnentries.- Head
Slice Spec P = the first K of every N of X/the last K of every N of X— one part of every group ofeveryconsecutive elements ofX’sn: thewidthelements from offsetfromof each group, in order (P[g·width + i] = X[g·every + from + i]). A head’s part of a row of heads, or a part of every cached row.- IntClamp
Spec Y = X clamped between LO and HI— saturation,min(max(x, LO), HI)overnInt entries,LO ≤ HI.- IntRequant
Spec Y = X times M shifted right by S— an int8 model’s requant(x · M) >> SovernInt entries, an arithmetic shift of the exact product.- Join
Spec J = A joined with B [across H heads]—rowsrows ofheadsheads, each head’s part ofAthen its part ofB:Aiswawide andBwbwide per row, a head’s partswa / headsandwb / headswide. One head joins the rows.- L2Norm
Spec Y = l2norm(X)— per-rowX / sqrt(Σ X² + eps)over arows × colsview (theacross H headsform isrows == H,cols == |X| / H), with the program’s epsilon.- LutAttention
Spec Let C be lut attention of Q over K and V with H heads KV groups [up to P] with table E and scale M shifted right by S— single-query attention over Int tensors whose softmax is an exp table: per head, scoresq · k_j, their maximum, weightsE[clamp(((max − s_j) · M) >> S, 0, table_len − 1)]and the context⌊Σ w_j · v_j / Σ w_j⌋. The shapes andposareAttentionSpec’s.- MoeGroups
- A mixture’s experts in
countgroups ofne / count, each scored by the sum of its two best selection scores (its one where a group holds one expert), the experts chosen only among the topkeptgroups. - MoeSpec
- A recognized Mixture-of-Experts FFN —
Let out be moe of x with router wr gate wg up wu down wd choosing K of E. Lifted from the desugared block the SAME wayrecognize_matmul/recognize_ropelift theirs — purely structural, no markers, no naming conventions: the OPERAND tensors are the indexed reads NOTLet-bound inside the block (the temps are), and the dims fall straight out of the weight index arithmetic (wr[(t-1)·E+e]yields E;wg[((e-1)·dff+(o-1))·d+t]yields d and dff);outis the tensor the weighted accumulation writes. - Relu
Spec Y = relu of X—max(x, 0)overnentries, inX’s element kind (int).- Rope
Spec Qr = rope(Q, cos, sin)— RoPE-NEOX overhheads ofhddims, rotating each head’s leading2·halfdims as(i, i+half)pairs and passing the remaininghd − 2·halfthrough (Qwen3.8’s partial rotary;hd == 2·halfis the classic full form).hdis read off the pass-through loop’s bound.- RowGather
Spec x = row i of T— the embedding lookup: rowi(1-based) of arows × colstable, as1 × cols.- Rows
Gather Spec X = rows I of T— the block gather: rowkofXis rowI[k](1-based) of arows × colstable, for each ofI’scountindices.- Scale
Spec Y = X scaled by C—x·Covernentries.- SetRow
Spec Set row i of T to S— the KV-cache append: rowi(1-based) of arows × colstable overwritten by thecolselements ofS.Tis read AND written: carried state.- Sigmoid
Spec Y = sigmoid(X)—1 / (1 + e^-x)overnentries.- Silu
Spec Y = silu(X)—x / (1 + e^-x)overnentries.- Softcap
Spec Y = X capped at C—C·tanh(x/C)overnentries (Gemma 2’s logits).- Transpose
Spec Kt = transpose(X)— anrows × colstensor becomescols × rows(Kt[j·rows + i] = X[i·cols + j]).- Unary
MapSpec - The one-loop unary map
Y[i] = f(X[i])overnentries, withfclassified exactly.
Enums§
- Activation
- The activation a unary elementwise map applies: the three sugars that share the one-loop
Push f(X[i])skeleton and differ only inf. - GateAct
- The activation of a Gated DeltaNet’s output gate.
- MoeAct
- A mixture’s gated activation: SwiGLU, or gpt-oss’s SwiGLU with the gate clamped to at most
limit, the up to[−limit, limit], and the sigmoid sharpened byslope(both kept as their bits, so the spec staysEq). - MoeScore
- How a mixture scores its experts.
- RowIndex
- Where a row sugar’s row comes from: a NAMED scalar (the data-indexed case — a token id, a cache position, an arg-max) or a literal. An arbitrary expression is not lifted; the walk lists the statement instead.
- Window
- The positions of the
nan attention query attends that it keeps.
Functions§
- recognize_
argmax - Recognize
Let t be argmax of X. - recognize_
attention - Recognize the single-query GQA
attentionblock. The desugar is aSplicethat: Lets the outputSeq(firstNew), zero-fills it, then a head-loop (While≤ H whose body Lets ascoresNew(Seq)) computesscores = Q·K / √HD, a softmax (Pow(E, …)), and the weighted sum ofV,SetIndexing the output. It does NOT mask with the-1e30sentinel (that’smoe). Dims come from the loop bounds (H = head-loop bound, L = score-loop bound, HD = score-inner bound) and thekvhdivisor (group = H/KV); the three externally-indexed operands, in program order, areQ, K, V. The maskedup to Pform setspos. - recognize_
causal_ attention - Recognize
Let C be causal attention of Q over K and V with H heads KV groups from P— a block ofTquery rows: theLet from be Pand its block checkfrom ≥ 1 and from + (T − 1) ≤ Lat the top, the output’s zero fill, then a row loop (toT) whose body binds the row’s lengthfrom + row − 1and runs the single-query head loop over it. - recognize_
causal_ conv - Recognize
Let Y be causal conv of X with weight WC state CS over T taps— the three loops: the ring shiftCS[(t−1)·chan+c] ← CS[t·chan+c], the append ofX, and the tap reduce againstWC. - recognize_
decay - Recognize
Let A be decay of Ar with bias Dt rate Alog—Push (1 + E^(Ar[i]+Dt[i]))^Alog[i]. - recognize_
full_ attention - Recognize
Let C be full attention of Q over K and V with H heads. - recognize_
gated_ delta - Recognize
Let O be gated delta of Q over K and V with gate Z beta B decay A state S norm GN with HV heads HK groups— theheadloop opened by the(head−1) mod HKkey-head binding, the in-placeSupdate, and the gated norm that writes the head. - recognize_
gelu - Recognize
Let Y be gelu of X—Push X[i] / (1 + E^(0 − 2z))with the 0.044715 cubic inz. - recognize_
head_ slice - Recognize
Let P be the first K of every N of X/the last K of every N of X— a loop over the groups, each pushing itsKelementsX[(g·N + i) + from]. - recognize_
int_ clamp - Recognize
Let Y be X clamped between LO and HI—Push min(max(X[p], LO), HI)in one counted loop. - recognize_
int_ requant - Recognize
Let Y be X times M shifted right by S—Push (X[p] * M) shifted right by Sin one counted loop. - recognize_
join - Recognize
Let J be A joined with B [across H heads]— a loop over the rows and in it a loop over the heads, each head pushing its part ofA(A[(r·wa + h·ha) + i]) then its part ofB. - recognize_
l2norm - Recognize
Let Y be l2norm of X [across H heads]— the per-rowsqrtdenominator bound in aLetand theX[base+q] / denomnormalize loop. - recognize_
lut_ attention - Recognize the
lut attentionblock: aSpliceholding the LUT weight read and a floor-division write, its head loop and cache bound the single-query attention’s (attention_heads,masked_length). Its operands, the tensors it reads by index and does not bind, areQ, K, E, Vin program order; the table is the one the LUT read names. - recognize_
moe - Recognize a MoE block anywhere in the statement list (through
Splice).Noneif not themoeshape. - recognize_
relu - Recognize
Let Y be relu of X—Push max(X[p], 0)in one counted loop, over Int or Real. - recognize_
rope - Recognize
Let Qr be rope of Q with cos RC and sin RS [from position P] [across H heads]— per head, the two rotation loops overhalfand the pass-through loop up tohd, after the position’s check when there is one. - recognize_
row_ gather - Recognize
Let x be row i of T. - recognize_
rows_ gather - Recognize
Let X be rows I of T. - recognize_
scale - Recognize
Let Y be X scaled by Canywhere in the statement list. - recognize_
set_ row - Recognize
Set row i of T to S. - recognize_
sigmoid - Recognize
Let Y be sigmoid of X—Push 1 / (1 + E^(0 − X[i]))in one counted loop. - recognize_
silu - Recognize
Let Y be silu of X—Push X[i] / (1 + E^(0 − X[i]))in one counted loop. - recognize_
softcap - Recognize
Let Y be X capped at Canywhere in the statement list. - recognize_
transpose - Recognize
Let Kt be transpose of X— thecols-outer /rows-inner loop nest pushingX[ti·cols+tj+1]. - recognize_
unary_ map - Recognize the one-loop unary map
Let Y be silu/sigmoid/gelu of Xanywhere in the statement list.