Skip to main content

Module from_qwen_ops

Module from_qwen_ops 

Source
Expand description

from_qwen_ops — every op of an authored transformer forward, lowered as a COMPOSITION of the closed compute algebra: chains of ComputeOp::Generic and ComputeOp::MatmulTiles, and nothing else.

★ NOT ONE NEW ComputeOp. Rope, silu, sigmoid, gelu, decay, l2norm, transpose, a causal conv ring, a Gated-DeltaNet step and GQA attention all lower through this file, and spatial_algebra_lock’s frozen per-printer arm counts do not move for any of them. That is the campaign’s whole claim, and it holds because B-P3 made the pieces general: a reduction is an ITERATOR KIND, a broadcast is an ABSENT TERM, a transform is an SfpuKind, a piecewise region is a Select on an AXIS, and an access map is a signed FLAT affine offset rather than one term per axis.

── The layout contract of a composed program ────────────────────────────────────────────────────────

★ EVERY ACTIVATION IS FLAT: 1 × n. This is not a convenience, it is what makes composition possible at all. An ElemAccess is a flat offset into a page read as tile.rows × (page_tiles · tile.cols), so element p of a 1 × n tensor is at offset p — the SAME number the language’s flat item p uses. Give the same tensor a 2 × 4 shape in one section and a 1 × 8 shape in the next and the two sections disagree about where element 4 lives, with no affine map able to reconcile them (the conversion needs p / cols, which is not affine in p). Flat is the one layout every op can both produce and consume.

★ A 2-D VIEW IS A BAND, NOT A RESHAPE. Where an op is per-head — rope’s rotations, l2norm across heads, a GDN head’s memory — the heads are contiguous RANGES of the flat column axis, and a range is a Pred::AxisLt on a CONSTANT. So one Generic computes every head by nesting one Select per head (banded), and inside head h’s band an operand that is indexed per-head is read through that head’s own base (c - h·width), while a per-head SCALAR is read through a CONSTANT map: the reduction that produced it broadcast it across the whole of its tile row, and inside one band the row is fixed. No divisibility, no reshape, no gather.

Functions§

attention_reads_dense
Whether an attention of h query heads over kv K/V heads, keys hd and values vd wide, reads its K and V as DENSE tiles on target — the flash form’s cache layout. A table an attention reads dense is written dense too, so this is the one place the two agree.
blocked_rms_norm
compute_shapes
The compute shapes a program uses, named — for a ledger row or a refusal.
from_argmax
t = argmax(X) + 1 — an ArgMax fold (first maximum, ties to the lowest index: the VM’s > scan) onto the integer datapath, then LOGOS’s 1-based count.
from_attention
GQA attention of a block of query rows over a cache of l rows: score[j] = (q·k_j)/√hd, row-softmax, then the weighted V sum — THREE sections, because a softmax cannot be one Generic (its maximum and its denominator are reductions whose results the next stage broadcasts back). One row is decode’s single query; a causal block of T rows is a prefill chunk’s queries, row r attending one position past row r − 1.
from_causal_conv
Y[c] = Σ_t CS'[t, c]·WC[t, c] where CS' = shift(CS) ← X — the depthwise causal conv1d over a taps-deep decode ring, as TWO sections.
from_decay
y[i] = (1 + e^(ar[i] + dt[i]))^(alog[i]) — Gated DeltaNet’s forget gate.
from_full_attention
C = full attention(Q, K, V) over t positions and h heads — t query rows of h heads, each reading its own K/V head and attending over all t positions. Unmasked, as the vision tower’s block is: every row sees every position.
from_gated_delta
The Gated DeltaNet step over hv value heads reading hk key heads at head dim d: u = S·k, S ← α·S + β·(v − α·u)·kᵀ, o = S·q / √d, RMSNorm’d, scaled by GN, gated by silu(z).
from_gelu
y = gelu(x) — ggml’s tanh approximation in its one-exponential form x / (1 + e^-2z) with z = √(2/π)·(x + 0.044715·x³), exactly as the parser desugars it (same arrangement, same tails).
from_head_matmul
Y = X matmul W across H heads — the weighted-sum shape: the folded axis is the ROW (e over a head’s a inputs), the flat output column c = u·b + r for unit u (row u / H, head h = u mod H), and each unit’s band reads X[u·a + e] and W[(h·a + e)·b + c − u·b].
from_head_slice
P = the first|last K of every N of X — group g’s K elements from from land at [g·K, (g+1)·K): one band per group, each an affine read X[c + g·(N − K) + from].
from_int_clamp
y = min(max(x, LO), HI) — saturation into [LO, HI].
from_int_requant
y = (x · M) >> S — an int8 model’s requant: the exact product, then an arithmetic shift.
from_join
J = A joined with B across H heads — unit u (row u / H, head u mod H) holds its ha elements of A then its hb of B at [u·(ha + hb), (u+1)·(ha + hb)): two bands per unit, A[c − u·hb] then B[c − (u+1)·ha].
from_l2norm
y[r, i] = x[r, i] / sqrt(Σ_i x[r, i]² + 1e-6) over rows heads of cols — per-head L2 normalization on a FLAT 1 × rows·cols tensor.
from_lut_attention
C = lut attention(Q, K, V) — an int8 model’s single-query attention, integer throughout, in THREE sections: the scores s = q·k_j over the cache; per head, the weights w_j = E[clamp(((max − s_j)·M) >> S, 0, n − 1)] for the positions attended and 0 past them; and the context ⌊Σ_j w_j·v_j / Σ_j w_j⌋. A masked position is fenced off only where it could change an answer — the maximum and the weights — and the sums need no mask: a weight of 0 adds nothing. Every value the masked step computes is one the unmasked step could: a masked position folds into the maximum as the head’s first score, which every P ≥ 1 attends, so no sentinel widens what the maximum holds.
from_moe
The Mixture-of-Experts FFN — route x to its top-k of ne experts and combine their SwiGLUs by the renormalized router weight:
from_relu
y = max(x, 0), in the tensor’s element kind.
from_rope
RoPE-NEOX over h heads of hd dims, rotating each head’s leading 2·half dims as (i, i+half) pairs and passing the remaining hd − 2·half through — Qwen3.8’s partial rotary.
from_row_gather
x[j] = T[lo + j] — the embedding lookup: row index (1-based) of a flat rows·cols table.
from_row_norm_flat
The COMPOSITION-LAYOUT twin of super::from_tensor_op::from_fused_row’s RMSNorm: the same chain (square → fold → rsqrt-once → rescale → per-column weight), over a FLAT 1 × rows·cols tensor instead of a rows × cols one.
from_rows_gather
X = rows I of T — the block gather: row k of X is row I[k] (1-based) of a flat rows·cols table, for every index of I at once, in ONE section; X is written as flat rows (the layout pass bridges it once where the program lays X out as a matrix).
from_scale
y = x·C — one multiply by the program’s constant.
from_set_row
Set row index of T to S — the KV-cache append, as ONE rewrite of the carried table: T'[c] = (lo ≤ c < lo + cols) ? S[c − lo] : T[c].
from_sigmoid
y = sigmoid(x) — 1/(1 + e^-x), the reciprocal of the silu denominator. No new SFPU kind: Exp and Recip already exist and the composition of the two IS the sigmoid.
from_silu
y = silu(x) — x / (1 + e^-x), one fused SFPU kind.
from_softcap
y = C·tanh(x/C) — each element soft-capped (capped’s form: one exponential and one reciprocal).
from_transpose
Kt[j·rows + i] = X[i·cols + j] — a TRANSPOSE IS A MAP, not an operator.
full_attention_reads_dense
attention_reads_dense for a full attention, whose query heads read K/V heads one to one.
is_closed_algebra
Every compute stage of a composition is one of the CLOSED algebra’s two shapes — the property each_composition_is_a_chain_of_the_closed_algebra asserts, exposed so a lock and a ledger read the same predicate rather than two copies of it.
row_needs_blocking
How many tiles of one row a single site may hold resident. A flat section keeps its whole row in local memory, which is free while the row is a handful of tiles and impossible once it is not: at Qwen3.8’s real d = 5120 a row is 160 tiles against an accumulator budget of eight, and the whole-row form is refused as AccTilesExceeded long before local memory is consulted.