Expand description
Backend-neutral tensor-op recognition (LOGOS AST → typed specs), shared by every codegen backend. Backend-agnostic tensor-op recognition — LOGOS AST -> typed op specs, shared by every codegen backend.
The recognizers here read an ordinary (desugared) LOGOS program and lift it into a typed spec — a matmul, an
elementwise op, a reduction / fused-row normalization, or a SwiGLU map — or REFUSE (typed). They are
backend-neutral: the CUDA (crate::codegen_cuda), Tenstorrent (crate::codegen_tt), and RISC-V
(crate::codegen_riscv) tiers all consume the SAME spec and each emit their own kernel from it. The
per-backend EMITTERS (lower_*_to_tensix, the CUDA emit_* fns, …) live in their backend module; only the
recognition + spec types are shared, and they live HERE rather than under any one backend.
Modules§
- qwen
- 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, thedesugar_tensor_*family) into a typed spec.
Structs§
- DotSpec
- A recognized dot product
acc = Σ_{t<n} A[t]·B[t]: one counted loop accumulating the product of two declaredSeq of Intarrays read at the same unit-stride index. - Elementwise
Spec - The recognized shape of an elementwise tensor op: two source tensors
a,b, a destinationc, the operator, and the shared element countn. - Epsilon
- A norm’s epsilon — the
Eofsqrt(… + E)— kept as its bits, so a spec holding one staysEqand hashable. - Factor
- A constant factor an op multiplies by, kept as its bits, so a spec holding one stays
Eqand hashable. - Fills
- Every tensor’s fill in a statement list at once (
recognize_fillof each), from one walk of the list: for each tensor the first loop pushing onto it — a loop’s own pushes met before the loops nested in it, asrecognize_fillmeets them — and its top-level literal writes, in order. - Fused
RowSpec - A recognized fused row op — a per-row reduction over a
rows × colstensor: one output row per input row, each normalized independently over itscolsentries. This is the transformer-normalization shape (RMSNorm / softmax / LayerNorm over the hidden dim, batched across rows). The flat single-vector reductionReductionSpecis exactly therows == 1special case; this spec is what the one-block-per-row CUDA emitter consumes, and it growsweight/residualas the fused variants land — the durable foundation. - Head
Matmul Spec - A recognized per-head matmul,
Y = X matmul W across H heads: each head’s slice of every row ofXtimes its own block ofW’s rows.Xism × (heads·a),W(heads·a) × b,Ym × (heads·b), andY[t][h·b + r] = Σ_e X[t][h·a + e]·W[h·a + e][r]— a block-diagonal product, onea × bblock per head. - Kmeans
Spec - A recognized k-means clustering (Lloyd’s algorithm):
npoints offinteger features, row-major inpoints, aroundkcentroids, row-major incentroids, which start as the firstkpoints. Each ofitersepochs assigns every point to its nearest centroid by the distanceΣ_d term(point[d], centroid[d])(ties astiessays), sums each centroid’s points and counts them, then sets every centroid to the integer mean of its points — 0 for a centroid with none. - Matmul
Spec - A recognized matmul:
C(M×N) = A(M×K) · B(K×N), row-major, over the namedSeq of Intarrays. - Outer
Spec - A recognized outer product
C[i·n + j] = A[i]·B[j]overi < m,j < n: a counted nest storing (not accumulating) the product of twoSeq of Intarrays into a row-majorm × narray. - Program
- What a loop’s term may draw on beyond the loop: the program’s pure functions (inlined where they are called), the
builtin
abs, and the element type of every array it declares (a new Seq of Int,Seq of Real,Seq of Bool). - Reduction
Spec - The recognized shape of a reduction: source tensor
x, destinationy, element countn, and (an rmsnorm’s) the divisor its normalize step divides by, whose assignment holds its epsilon. - Scatter
Spec - A recognized scatter-matrix accumulation
S = Σ_{r<R} d_r d_rᵀ(C × C, row-major, from zero): each row’s vectord_r[c] = termcomputed into a scratch array by an elementwise loop over the row, then added toSas a rank-1 update of itself — the covariance step of Gaussian discriminant analysis. - SwiGlu
Spec - A recognized SwiGLU map:
Y[i] = silu(gate[i]) * up[i],silu(g) = g / (1 + e^-g), overtotalelements — the gated MLP activation (not a row-reduction; a pure two-input elementwise map). - TermDag
- A term as a DAG: each distinct subterm once, every node after its operands — how a large term (a function inlined at each call) is evaluated per element and printed with each shared subterm bound once.
- ZipMap
- A recognized zip map
out[t] = f(A₀[t], A₁[t], …)fort < n: one counted loop storing a term of several arrays into another, every one read at the index it stores at — Black-Scholes over its option columns, any elementwise map over aligned streams. The term’s type is the output’s element type,IntorReal. - ZipSum
- A recognized zip sum
acc = Σ_{t<n} f(A₀[t], A₁[t], …): one counted loop accumulating an integer term of severalSeq of Intarrays, every one read at the same unit-stride index — a dot product, TPC-H Q6’s filtered revenue, any map-reduce over aligned streams.
Enums§
- Axis
- How a scatter row’s term reads an array: at the row (
y[r]), at the column (mu[c]), or at both, row-major (x[r·C + c]). - DagNode
- A node of a
TermDag: aZipTermnode whose operands are nodes, by index. - DotReject
- Why a program is not a recognizable dot product — a typed refusal, never a miscompile. Ordered from the least to the most advanced, so a search reports the refusal of the loop that came closest: what a loop’s body computes ranks before whether its counter is provable, so a dot product with an unprovable counter outranks a loop that accumulates nothing.
- Elementwise
Op - An elementwise tensor op —
C[p] = A[p] ⊕ B[p]over a shared flat length. - Elementwise
Reject - Why a program is not a recognizable elementwise op.
- Fill
- How an array is FILLED by its build loop — recognized from
While iv is less than N: Push <expr> to xs, so a backend host can reproduce the source’s data and a numerics claim can bound the operand range. - Kmeans
Reject - Why a program is not a recognizable k-means clustering, least to most advanced.
- Matmul
Reject - Why a program is not a recognizable tile-matmul — a typed refusal, never a miscompile.
- Outer
Reject - Why a program is not a recognizable outer product, least to most advanced.
- Reduction
Kind - A float reduction tensor op —
rmsnorm(unit-RMS rescale) orsoftmax(probability distribution). - Reduction
Reject - Why a program is not a recognizable reduction op.
- RowReduce
Kind - The reduction performed per row in a fused row op — the extensible core of the fused-row family (LayerNorm / fused-add-RMSNorm join here as they land). Each is a shared-memory reduction over one row.
- Scalar
- A scalar a
ZipTermtakes, as the VM holds it. - Scatter
Reject - Why a program is not a recognizable scatter accumulation, least to most advanced.
- Stop
- Why
ZipTerm::valuehas no value: anIntleft the range asked for, aRealis not finite or left its bound, a divisor is zero, an exponent negative, or an operator met an operand it does not take. - Ties
- Which of several equally near centroids a point joins.
- Ty
- The type of a
ZipTerm, or of an array element it reads: LOGOS’s 64-bitInt, itsReal(a double) andBool. - ZipMap
Reject - Why a program is not a recognizable zip map, least to most advanced.
- ZipOp
- An operator of a
ZipTerm: arithmetic, a comparison (a boolean), or a boolean connective. On twoInts arithmetic isInt(/truncating toward zero,**exact); with aRealoperand it isReal— the VM’s rules. - ZipReject
- Why a program is not a recognizable zip sum, least to most advanced (what a loop’s body computes ranks before whether its counter is provable).
- ZipTerm
- The term a zip loop computes from the elements it reads at the counter’s index: arithmetic, comparisons and
selects over array elements and constants. A guarded accumulation (
If P: Set s to s + E) is the selectP ? E : 0; a call of one of the program’s pure functions is its body, inlined.
Functions§
- epsilon_
of - The epsilon of the norm whose divisor is
denom: theEof thesqrt(… + E)the norm’s desugar assigns to it. - hoist_
common_ operand termwith every select whose two arms apply one operator to a common operand rewritten to apply it once, to the select of what differs —c ? x − a : x − bbecomesx − (c ? a : b), and the same for+,×and a common right operand. Exact for every value: both forms apply the same operator to the same operands of the arm taken.- recognize_
dot - Recognize a dot product anywhere in a LOGOS statement list:
- recognize_
elementwise - Recognize an elementwise idiom
Push (item p of A) ⊕ (item p of B) to Cinside a counted loop — the exact shape theplus/hadamardsugar desugars to — anywhere in the statement list (throughSplice). - recognize_
fill - Recognize the fill of
tensoranywhere in the statement list: the FIRST loop whose body pushes onto it. - recognize_
fused_ row - Recognize the fused row-op idiom. First it lifts the PER-ROW WEIGHTED RMSNorm shape (a row loop whose
normalize body
Push (X[idx] / denom) * W[col]givesx,weight,y, and whose enclosing loop bounds giverows/cols); failing that it falls back to the weightless flatrmsnorm/softmaxsugar (rows == 1,cols == n) viarecognize_reduction. Backend-agnostic (CUDA + Tenstorrent). - recognize_
head_ matmul - Recognize
Let Y be X matmul W across H heads— the matmul’s nest with a head loop between the row loop and the contraction:Y[t·(H·b) + h·b + r + 1] += X[t·(H·a) + h·a + e + 1] · W[(h·a + e)·b + r + 1], the loopst,h,e,roverm,H,a,b. - recognize_
kmeans - Recognize a k-means clustering anywhere in a LOGOS statement list — the epoch loop of Lloyd’s algorithm:
- recognize_
matmul - Recognize a row-major GEMM idiom anywhere in a LOGOS statement list and extract its
MatmulSpec. - recognize_
outer - Recognize an outer product anywhere in a LOGOS statement list:
- recognize_
reduction - Recognize the
rmsnorm/softmaxnormalize-loop idiom the sugar desugars to — a counted loop thatPushesX[p] / denom(rmsnorm) ore^(X[p]-m) / s(softmax) intoY— anywhere (throughSplice). - recognize_
scatter - Recognize a scatter accumulation anywhere in a LOGOS statement list:
- recognize_
swiglu - Recognize the SwiGLU idiom the sugar desugars to —
Push (gate[iv] / (1 + E^(0 - gate[iv]))) * up[iv]in a counted loop. Backend-agnostic (CUDA + Tenstorrent). - recognize_
zip_ map - Recognize a zip map anywhere in a LOGOS statement list:
- recognize_
zip_ map_ where recognize_zip_mapfor the first loop whose zip mapwantedaccepts; one it turns away counts asZipMapReject::UnsupportedTerm(its term is not the one asked for).- recognize_
zip_ sum - Recognize a zip sum anywhere in a LOGOS statement list:
- recognize_
zip_ sum_ where recognize_zip_sumfor the first loop whose zip sumwantedaccepts; a zip sum it turns away counts asZipReject::UnsupportedTerm(its term is not the one asked for).