A transformer block has two parts. Attention mixes information across tokens, and the feed-forward layer mixes the channels within a token, the 768 numbers that describe it. The feed-forward layers hold most of the parameters in a transformer, and on a small device every parameter has to be read from memory for every token. Needle replaces the feed-forward layer with a Hadamard MLP, a mixer with 25.6K parameters per layer instead of 4.7M. Below is the construction and what it costs.
The cost of a feed-forward layer
A few words first. A token is a word or a piece of a word, and inside the model it is carried as 768 numbers. A parameter is one learned number the model stores, and a weight is a parameter that sits in a matrix. A nonlinearity is a fixed function applied to each number on its own, such as setting negatives to zero, and it is what lets two matrices in a row do more than one matrix could. A multiply-add is one multiplication followed by one addition, and it is the unit of arithmetic here. A GPU is the graphics processor that does that arithmetic in training and in most serving.
A token is a vector with . The standard feed-forward layer is two matrices with a nonlinearity between them:
It stores numbers per layer, and it spends multiply-adds on every token.
On a GPU the multiply-adds are the cost. On a phone, a watch or a microcontroller, the cost is moving the weights. Every weight travels from memory into the processor for every token, and memory is slow. A token is not done until the last weight has arrived.
Our attention-only study measured what this layer contributes. Its parameter count matters. Its exact shape matters much less. What the block needs is a mixer that connects every channel to every other, plus a nonlinearity, with as few stored numbers as possible.
A mixer with no weights
A token inside the model is a list of 768 numbers, and each of the 768 slots is called a channel. A mixer is a matrix that turns those 768 numbers into 768 new ones, where each new number depends on all of the old ones. A matrix with 768 by 768 stored entries can do this any way it likes, but every one of those entries has to be stored and fetched. The question of this section is how to mix every channel with every other while storing nothing.
The answer is a rotation. The token is a point in a space with 768 axes, one axis per channel. A rotation turns the point about the origin, so its distance from the origin does not change. The matrices that do this are called orthogonal, and multiplying an orthogonal matrix by its own transpose gives the identity, which is what in the list below says. If the new axes point equally at every old axis, each channel's value gets spread over all of the other channels, and no output channel is a copy of any single input channel any more.
Two of those words need unpacking. The transpose of a matrix is the same matrix with its rows written as columns, so the entry in row two, column five moves to row five, column two. The identity is the matrix that leaves every vector as it was, with ones down the diagonal and zeros everywhere else. A matrix times its transpose giving the identity means that applying the matrix and then its transpose undoes it, the way turning left and then right by the same angle leaves you facing where you started.
The Walsh-Hadamard matrix is that rotation. Every entry is or , and each row is a different pattern of signs, a square wave that flips at one of rates. The whole matrix is scaled by so that each row has length one. Each output is then a signed sum of every input with the same weight on each, and any two rows are at right angles to each other, the way the coordinate axes are.
The sign in row and column is . Here is the bitwise AND of the two position numbers written in binary, and popcount counts how many ones are left. If the count is even the entry is , and if it is odd the entry is . The entry is a short calculation on the position, which is why the matrix needs no storage at any size.
The Walsh-Hadamard matrix connects every channel to every other and stores nothing. One rule defines it at every size:
Tile the matrix four times, flip the sign of the bottom-right copy, scale by .
Three properties make it a good mixer.
- Every entry is or . Nothing is stored; the rule is the matrix.
- It is orthogonal, . It rotates a vector without stretching it, so values do not grow or vanish as they pass through.
- Applying it is cheap. The fast transform below does it in stages. Each stage pairs channels and replaces the pair by , which is additions in total.
After the last stage every output depends on every input. Needle 2 used this transform as its mixer, with a learned scale on each channel before and after it. Needle 3 keeps the idea and makes the rotation itself learnable, at the same cost. The tool for that is the Kronecker product, in the form of a Monarch matrix.
Folding a vector into a tile
This section uses the Kronecker product, so here is what it is. Take two matrices and , both . Their Kronecker product is the matrix you get by replacing each entry of with the block , a copy of scaled by that entry. Written out it is a large matrix, but it never has to be written out.
The reason is a change of view. A vector with channels can be laid out as an tile, so that each channel sits at a row and a column. Then acts on the row index with one factor and on the column index with the other, and the two actions do not interfere. Mixing the rows of the tile is one small matrix product, mixing the columns is another, and doing both one after the other is the whole map.
A general matrix on channels can send any channel to any other with its own weight. A Kronecker product can only move a channel along its own row and its own column of the tile, and it uses the same for every row and the same for every column. That constraint is where the saving comes from. One such product is what the figures below call a Monarch stage, and the section after this one is about what the constraint costs and how a second stage pays for it.
Pad the 768 channels to and write them as a tile , one row after another. Take two small matrices . Mix the rows of the tile with and the columns with :
Flatten the tile back into a vector. The result is the same as multiplying the vector by one matrix, the Kronecker product :
A dense matrix stores numbers and costs as many multiply-adds. The two factors store numbers and cost multiply-adds. That is 512 times fewer parameters and 16 times less work.
The Walsh matrix is itself a Kronecker product, . So at initialisation makes the stage an exact Hadamard transform on the first step of training, and every later step is free to move and .
The identity is what makes the initialisation exact rather than approximate, and it follows from the doubling rule. That rule says is the sign pattern with each entry replaced by a copy of , with the included in . Replacing each entry of one matrix by a scaled copy of another is the Kronecker product from the start of this section, so . Unrolling ten doublings from gives
The Kronecker product is associative, meaning the factors can be grouped any way without changing the result. So the ten factors can be grouped as five and five, and five doublings from is .
The tile picture says the same thing. A Walsh transform on 1,024 channels is a Walsh transform on each row of the tile followed by one on each column. That is how the fast transform of the last section would run if its ten stages were split into two groups of five.
Why one stage is not enough
One stage mixes each channel with its own row and its own column of the tile. Two channels in different rows and different columns never meet. Of the 1,024 channels, one stage lets a channel reach .
The fix is a fixed shuffle between stages. After the first stage, a random permutation moves the 63 reached channels to new places in the tile. The second stage spreads each of them along its new row and column, and after that every channel has a path to every other.
A product of block-structured matrices with permutations between them is a Monarch matrix, the same idea as learned butterfly factorisations. Two stages give full reach. Needle uses three, because the third stage adds capacity, and each stage is still two small matrix products.
A short word on where those two ideas come from. The fast Fourier transform, and the fast Walsh transform above, are built from butterfly stages. One stage pairs each channel with a partner a fixed distance away and mixes only the pair. As a matrix, after reordering the channels, that is block-diagonal with blocks, meaning it is zero everywhere except in small squares along its diagonal.
No single stage reaches far. But stages at distances give every channel a path to every other, and the fixed wiring between the stages is what carries information from one block to the next. A learned butterfly factorisation keeps that wiring and lets the numbers inside the blocks be trained.
A Monarch matrix makes the same bargain with fewer, fatter stages. It replaces the thin stages with two, each block-diagonal with blocks of channels, joined by one fixed permutation. Two fat stages reach as far as thin ones, because a block of channels, shuffled and then mixed again in blocks of , covers all .
A Kronecker product is itself a product of this kind. Mixing the rows of the tile and then the columns is two block-structured factors with a fixed transpose of the tile between them. So each of Needle's stages is a Monarch matrix on its own, and the shuffles between the stages are what let the three of them act as one.
The whole layer
are the three mixes, the fixed shuffles, learned per-channel scales, a bias, and the nonlinearity:
Reading from the inside out: scale, mix, shuffle, scale, nonlinearity, mix, shuffle, scale, mix, scale. The middle scale is multiplied by a gain that depends on the token:
The token is projected to 8 numbers, softmax turns them into weights over 8 patterns, and each pattern is a vector of channel gains. starts at zero, so at initialisation. The gain costs 14K multiply-adds and gives each token some control over how its own channels are scaled, which a wide dense layer gets from its width. starts at 0.02, so a new layer writes gently into the residual stream, the same convention as the attention output.
Three terms there need unpacking. Softmax takes a list of numbers and turns them into positive weights that add up to one, with the largest input getting the largest weight. Rank 8 means the gain is built from 8 patterns, so however the token is scaled, the scaling is some blend of those 8. The residual stream is the running sum that carries a token through the network. Each layer reads it, adds its own contribution and passes it on, so a layer that writes at 0.02 of its natural size barely disturbs what the earlier layers have put there.
The parameter count per layer:
| piece | count |
|---|---|
| three Kronecker stages, | 6,144 |
| four diagonal scales and a bias, | 5,120 |
| gain, : | 6,144 |
| gain, : | 8,192 |
| total | 25,600 |
Per token, the three mixes cost multiply-adds, the scales about 5K and the gain 14K, roughly in total.
The cost, compared
Per layer the Hadamard MLP is 180 times smaller than the dense layer and 22 times cheaper to run. Over the 20 blocks of Needle 3 the difference is 94M parameters, which would take the model from 121M to 215M, and 196 MFLOPs per token, which would take it from 100 to 296.
A dense feed-forward layer is also where a transformer stores facts. Needle stores them in the engram, a conditional memory of hashed n-gram tables that holds 70.8M of its 121M parameters. A table is read by gather: a few rows per token, selected by hashing the last few tokens, with no arithmetic. So the parameter budget went from a matrix that every token reads in full to a memory that is only touched where it is needed.
That paragraph leans on three words. An n-gram is a run of n tokens in a row, so the last two or three tokens before the current one. Hashing turns that run into a row number by a fixed arithmetic rule, so no index has to be stored to find the row. A gather is the memory operation that reads a handful of rows by their numbers and nothing else. The engram is the name for this memory, and its own post is on this site.
Results
The question is what the swap loses. We trained the same 12-layer, decoder on 800M tokens of Nemotron QA text on 8 H100s, once per mixer, with the same optimiser, schedule and seed, and changed only the feed-forward block. At this width a dense feed-forward layer is 3.28M parameters and the Hadamard MLP is 24K; at Needle 3's the same two numbers are 4.7M and 25.6K. Validation cross-entropy is in nats per token. The removed columns are for the whole model, since that is what a device stores, and the change column is against the dense GELU row.
Some of the table's words need unpacking. Cross-entropy is the training loss. At each position it is how surprised the model was by the true next token, the negative logarithm of the probability it gave that token, and the table reports the average over positions. A nat is the unit when the logarithm is the natural one. Validation means it is measured on text the model did not train on. The seed is the number that fixes every random choice in a run, so two runs with the same seed start from the same weights and see the data in the same order. GELU and SwiGLU are two choices of nonlinearity, and SwiGLU adds a third matrix that gates the hidden layer, which is why its row has slightly more parameters.
| mixer, same model shape | mixer per layer | model | removed | MACs removed | validation CE | loss change |
|---|---|---|---|---|---|---|
| dense FFN, GELU, hidden | 3.28M | 61.6M | 2.281 | |||
| dense FFN, SwiGLU | 3.32M | 62.1M | 0% | 0% | 2.422 | +6.2% |
| Hadamard MLP | 24K | 22.6M | 63% | 40% | 2.695 | +18.1% |
| Kronecker stages without the gain | 11K | 22.4M | 64% | 40% | 2.695 | +18.1% |
| Monarch, normal init | 65K | 23.1M | 62% | 41% | 2.699 | +18.3% |
| low-rank FFN, same size | 24K | 22.6M | 63% | 42% | 2.930 | +28.5% |
What we lose relative to what we remove: per layer the mixer goes from 3.28M parameters to 24K, 99.3% of it; the whole model gives up 63% of its parameters and 40% of its multiply-adds, because the embeddings and attention stay, and pays 18% more loss on general QA text. A low-rank feed-forward layer that removes the same 63% pays 28%. Monarch without the Walsh start pays the same as the Hadamard MLP. SwiGLU removes nothing and pays 6%. The token-dependent gain costs nothing and gains nothing measurable here; across three seeds the Hadamard MLP and the same stages without the gain differ by 0.0%, 1.0% and 1.4% of the loss, in alternating directions.
Removing one piece at a time, at a shorter 573-step screen on an 8-layer, model where the dense layer scores 5.29:
| ablation | validation CE | loss change |
|---|---|---|
| full Hadamard MLP | 5.84 | |
| one Kronecker stage instead of three | 5.95 | +1.9% |
| normal initialisation instead of Walsh | 5.95 | +1.9% |
| no permutations between stages | 5.90 | +1.0% |
| rotation fixed at Walsh, only scales learn | 5.86 | +0.3% |
| no diagonal scales | 5.45 | −6.8% |
Removing the diagonal scales is the one change that helps at 573 steps and hurts at the full 3,000, where it costs 2.3% of the loss, 2.756 against 2.695.
The 18% is the price on text where the feed-forward layer's storage role matters, the same query-token deficit the attention-only study measured. On tool-shaped data, where the answer is in the context, the price was not where the loss lived, and the engram carries the facts. All rows in the table are one seed; the Hadamard MLP and the gain-free stages were run at two more seeds, which is where the three paired differences come from.
On the device
Each Kronecker stage runs as the two products of the diagram, on a tile that fits in vector registers. A layer's mixer is a few hundred fused multiply-adds per lane with no weight matrix to fetch. The three factor pairs, the scales and the gain are a little over a hundred kilobytes per layer at full precision, and much less in the shipped 2-bit archive, so the mixer stays in cache for the whole session. Decode on a Raspberry Pi 5 runs at 400 to 4,000 tokens per second across the depth ladder, and the Hadamard MLP is a large part of that.
The hardware words there are these. Registers are the few dozen storage slots inside the processor itself, and cache is the small fast memory beside it. Anything held in either is reached far quicker than anything in main memory, and the whole mixer fits. The 2-bit archive is the shipped copy of the weights with each one stored in two bits rather than the usual sixteen or thirty-two. Decode is the stage where the model writes its reply one token at a time, and the depth ladder is the way Needle can be run with only its first few blocks, so the same model is served at several depths.
What it gives up
- A dense layer can represent any linear map on its hidden width. Three Kronecker mixes with shuffles cannot represent all of them.
- The rank-8 gain is a small substitute for the token-dependence a wide hidden layer has.
- On the reasoning-dense, tool-shaped data Needle trains on, the block's job is to route and transform what is already in context. That expressivity was not where the loss lived, and the engram took the storage role.
- Whether the same trade holds for knowledge-heavy general text at larger scale is open, and it is the same question the attention-only study left open.
- Within Needle's regime the numbers above are the argument: a 121M model that beats models ten times its size on mobile tool calls, at a third of the compute a transformer of its shape would spend.
References
- Vaswani et al., Attention Is All You Need, 2017.
- Walsh, A Closed Set of Normal Orthogonal Functions, American Journal of Mathematics, 1923.
- Fino and Algazi, Unified Matrix Treatment of the Fast Walsh-Hadamard Transform, IEEE Transactions on Computers, 1976.
- Dao et al., Monarch: Expressive Structured Matrices for Efficient and Accurate Training, 2022.
- Dao et al., Learning Fast Algorithms for Linear Transforms Using Butterfly Factorizations, 2019.
- Hendrycks and Gimpel, Gaussian Error Linear Units, 2016.
- Cheng et al., Conditional Memory via Scalable Lookup: A New Axis of Sparsity for Large Language Models, 2026.
- Ndubuaku et al., A Controlled Study of Attention-Only Transformers, 2026.