Expand description
The decode engine’s arithmetic, bit for bit.
Every number the engine produces is defined here as a sequence of exact integer operations and f32 operations, so the hardware is held to these functions exactly and these functions are held to the model’s own arithmetic within a measured bar.
★ THE ENGINE NEVER ROUNDS A WEIGHT. A block format stores integer codes and a few scales; every weight is a
scale times an integer. Activations are quantized under scales m·2^e, the codes the exact nearest integers of
x / scale, in one of two ways (ActScaling):
• per block of 32 under a 4-bit significand (m in 8..15): a matrix row times an activation row is, within a
group of 256 elements, a sum of products of a few small integers and a binary16 significand with integer dot
products, taken EXACTLY, aligned to the group’s least exponent in a wide integer, the group rounded to f32 once;
• per weight block (the matrix format’s own block, 32 or 256 elements) under an 8-bit significand (m in
128..255), llama.cpp’s own blocking: each weight block’s two terms — its scale times the block’s integer sum,
its second scale times the offset terms’ — summed exactly and rounded to f32 once, which is what the array’s
block-scale lanes compute at the array’s rate.
Either way the rounded pieces of a row are added in order.
f32 arithmetic is IEEE binary32, round to nearest even, with denormal operands and results flushed to zero
keeping their sign (ftz); multiply and add round separately. Dot products and sums over a vector run in
LANES lanes (element i in lane i mod LANES, in index order) folded by a balanced pairwise tree. The
transcendentals are fixed sequences of those operations over fixed tables.
Structs§
- ActRow
- An activation row as the array reads it: a whole-number code per element, worth
code·m·2^eof its block ofblockelements. - ActScale
- An activation block’s scale
m·2^e:min 8..15 perActScaling::Sub32block, in 128..255 perActScaling::PerBlockblock. - Coded
Row - A row of a block-format matrix as the engine reads it: every weight is
a·coef + b·offsof its segment. - Logged
- One logged token as a lazily flushed head reads it: its key group’s key, the head’s values, the exponent of its decay and its update gate, as the log stores them.
- Scale
- A scale stored in a block:
sig·2^exp, exactly. - Segment
- A run of a row’s elements under one pair of scales: the elements’ coefficients under the first scale (codes times their sub-block scales) and under the second (the offset terms), as stored, exactly.
- Unreadable
- A block a format stores that the engine cannot read: an infinite or NaN scale.
Enums§
- ActScaling
- How an activation row is scaled, and so how a matrix row’s dot with it rounds.
- FpUnit
- The multiplier an f32 product is computed by. Its add is the engine’s either way: the card’s block adds as
fadddoes. - Words
- Words an activation crosses the array in.
Constants§
- ACT_
BLOCK - Elements that share one activation scale: a ggml sub-block, so a block’s scale meets a weight sub-block’s.
- COEF_
BOUND - The largest magnitude a coded row’s coefficient or offset takes in any format the array reads: Q6_K’s
−128·−32. - GROUP
- Elements whose products round to f32 together.
- INV_127
1/127, the int8 cache’s scale per unit of its row’s largest magnitude.- LANES
- Lanes a dot product or a sum accumulates in before its tree.
- NAN
- The one NaN the engine produces: a quiet NaN of no payload and no sign.
- SUM_
WINDOW - Binary places an exact sum keeps below its largest term’s least place.
- VALUE_
BLOCK - Positions a block of the array’s weighted sum spans.
- WIDE_
BITS - Significand bits of a per-weight-block activation scale.
Functions§
- absmax
- The largest magnitude in
x, a denormal read as a zero, folded from+0; a NaN is passed over. - argmax
- The index of
x’s largest element, the first of equals (the two zeros equal), a NaN passed over;0when there is none. Lanes each keeping their first largest and folded to the larger, the lower index of equals, agree with it in any order. - attend_
int8 - One query head’s attention over its cached int8 rows on the array: the query quantized as
wordsandscalingname, each position’sint8_scoretimesscale, thesoftmax, theweighted_int8sum. - attend_
vector - One query head’s attention over its cached rows on the vector units: each position’s score the head’s dot with
the key times
scale, thesoftmaxof the scores, and each value element the probabilities’ dot (fdot, lanes by position) with that element across the positions. - block_
scale - The least scale
m·2^e(min 8..15) with127·m·2^e ≥ amax, for a finite positiveamax. - cache_
row - A cache row stored as int8 codes under one f16 scale
amax/127(absmax): the scale’s bits and the codes under its reciprocal (int8_codes), all zero when the scale rounds to zero. - conv
- A Gated-DeltaNet block’s causal convolution through SiLU, channel by channel: the ring’s rows, oldest first,
against the channel’s first taps, then the token’s row
xagainst its last, the products summed in tap order from+0.kernelholds each channel’stapsweights together. - dot
- The f32 the engine produces for
row · act. - exact_
sum Σ v·2^eoverterms, rounded once to f32: exact while every term lies withinSUM_WINDOWplaces of the largest term’s least place, a term past the window floored at it, however far past; no terms is +0.- exp
e^x:x = n·ln2/64 + rwithnthe nearest multiple,e^rby its cubic, times2^((n mod 64)/64)from the table, times2^(n div 64).- f32_
to_ f16 xrounded to binary16, to nearest with ties to even, subnormals kept, past the top infinite.- fadd
- fdot
Σ a_i·b_iinLANESlanes, then the tree.- fmax
- The largest of
x, folded from −∞; a NaN is passed over, and a largest value that is a zero is+0whichever zeros it was folded from, so lanes folding in any order agree. - fmul
- fsub
- fsum
Σ a_iinLANESlanes, then the tree.- ftz
xwith a denormal flushed to a zero of its sign.- gdn_
flush - A lazily flushed state row’s flush: every logged update replayed over the row in order (
gdn_updatewith valuerof each token), exactly as the token-by-token memory makes them, and the row stored once throughstore. - gdn_
fold - A query folded back through a lazily flushed head’s log, newest token first: per token, its gate times the key’s
dot with the folded query; that coefficient times the token’s values collected into
widthsums; the query decayed past the token’s update,y ← decay·(y − coef·k). Returns the folded query and the collected values. - gdn_
fold_ trailing gdn_foldover a log whose oldestoldertokens a trailing flush is replaying, in one walk: first through the newer tokens alone, the fold a row the flush has replayed reads, then on through the older ones, the fold every other row reads. The walk is newest first, so the first is where the second has reached after the newer tokens: each isgdn_foldover its own tokens, bit for bit.- gdn_
read - A lazily flushed state row’s read-out through its head’s folded query:
(row·y + carried)·scale. - gdn_row
- One value row of a Gated-DeltaNet memory through one token, in one pass over the row: its dots with the token’s
key and query, the rank-one update (
gdn_update), and the row’s read-out(decay·(row·q) + coef·(k·q))·scaleof the row as it was.kqis the head’sk·q. - gdn_
update - One value row of a Gated-DeltaNet memory through one token’s rank-one update:
coef = beta·(v − decay·(row·k)), each elementdecay·m + coef·kwritten back throughstore; returnscoef. - hadamard
- The Walsh–Hadamard transform of
xin place, unnormalized: for each stridehfrom 1 doubling tolen/2, every pair(x[j], x[j+h])of each2h-block becomes(x[j] + x[j+h], x[j] − x[j+h]), in index order. Applied twice it islentimes the identity, solena power of two makes the inverse exact: the transform, then2^-log2 len. - int8_
codes - The int8 codes of
xunder the inverse scaleinv: eachx·invto the nearest integer, ties to even, within ±127, a NaN product zero; every code zero wheninvis infinite, the reciprocal of a scale that rounded to zero. - int8_
score - A score as the array computes it against a cached int8 key row (its f16 scale’s bits and its codes): per activation
block of the query, its scale’s
mtimes the integer dot of its codes and the key’s codes, times the key’s scale, summed exactly and rounded once. - l2norm
x / √(Σx² + ε).- ldexp
x·2^n, exact while the result is a normal f32: infinite past the top, a zero of its sign below the bottom.- ln
ln xforx > 0:x = m·2^Ewithmin[3/4, 3/2), so a value near 1 keepsE = 0;magainst the nearest table pointc_j, andln(m/c_j)by its quartic.- ln1p
ln(1 + t)fort ≥ 0:ln uofu = 1 + trounded, corrected by the rounding’s own error(t − (u − 1))/u, which is exact.- quantize
x(a whole number of groups) quantized per block ofACT_BLOCK.- quantize_
blocks x(a whole number ofblocks) quantized per weight block ofblockelements.- quantize_
head - A query head as the array reads it: per 32 elements (the head padded with zeros to a whole group), or the whole head one block.
- reads
- Whether the engine’s array reads matrices of
ty. - recip
1/x: the seed from the top seven mantissa bits, then two Newton stepsy·(2 − x·y).- rmsnorm
- RMSNorm:
x_i · rsqrt(Σx²/n + ε) · w_i, the product in that order. - rne
xrounded to the nearest whole number, ties to even, as an integer.- rne_div
- The nearest integer to
x / (m·2^e), ties to even, exactly. - rope
- A head turned by a rotary table: pair
iof the first2·cos.len()elements —(i, i + cos.len())under NEOX,(2i, 2i + 1)otherwise — becomes(x₀·cos_i − x₁·sin_i, x₀·sin_i + x₁·cos_i); the rest pass through. - rsqrt
1/√xforx ≥ 0: the seed from the exponent’s parity and the top six mantissa bits, then two Newton stepsy·(1.5 − ½·((x·y)·y)), every intermediate near 1 so none flushes at the ends of the range.- scaled_
to_ f32 v·2^exprounded once to f32, to nearest with ties to even, a denormal flushed to a zero of its sign.- sigmoid
- silu
- softmax
- The softmax: the maximum (
fmax), eachexp(s − max), their sum in lanes (fsum), one reciprocal, the products. - softplus
ln(1 + e^u):upast 16 addse^-u.- tree
- Folds lane partials by a balanced pairwise tree: adjacent pairs, then pairs of pairs.
- weighted_
int8 - The weighted sum on the array over cached int8 value rows (each its f16 scale’s bits and its codes): each position’s
probability times its row’s scale, quantized per block of
VALUE_BLOCKpositions to 8-bit codes at a power-of-two scale; per value element the block’s exact integer sum against the value codes, rounded once a block; the blocks added in order. - wide_
scale - The least scale
m·2^e(min 128..255) with127·m·2^e ≥ amax, for a finite positiveamax.