Linear-Time Sequence Modeling via MLP-Mixer on Latent Memory Slots

A sequence model takes a list of tokens and processes it. A token needs unpacking. Before a model sees any text, a fixed program cuts the text into pieces drawn from a vocabulary of a few tens of thousands of pieces. A common word is one piece and a rare word is several, so unbelievable might become un, believ and able. Each piece is a token, and the model only ever sees the list of tokens.

Inside the model each token is carried as a list of numbers, and the length of that list is the model width. Attention is the step where every token looks at the other tokens and takes a weighted mix of their numbers, with the weights worked out from the content. It is what lets the model relate one part of the text to another.

Most of the work on making this linear in the sequence length, meaning the cost grows in proportion to the number of tokens rather than faster, has gone into the decoder. A decoder generates text one token at a time, and its attention only looks backwards, so a running summary that is updated after each token can carry what came before. Encoders are a different problem. An encoder reads a whole input and wants every token to see every other token in both directions, with weights that depend on the content. The linear-time options for that are either one-directional models with a forward and a backward pass bolted on, or heavy.

On a phone this matters twice over. Models that are only a decoder run two stages through the same attention. Prefill, where the whole prompt is read at once, and decode, where tokens come out one at a time. The two stages want different kernels. A kernel is a small program written for one chip that does one piece of arithmetic, such as multiplying two matrices, and a kernel tuned for one stage slows the other. And attention's cost of O(N2d)O(N^{2} d), the square of the sequence length times the model width, makes even modest contexts slow on a small chip.

The first problem pushes toward encoder-decoder models, where a separate encoder reads the input and the decoder writes the output, and these have been shown to beat decoder-only models below a billion parameters. A parameter is one learned weight, and the count of them is the usual measure of a model's size. The second problem asks for an encoder whose cost is linear in the sequence.

MemoryMixer is that encoder block. It keeps a fixed number of memory slots alongside the sequence. A slot is a list of numbers the same length as a token's, learned during training and not tied to any position in the text, so there are as many slots for ten tokens as for ten thousand. The slots read the sequence through cross-attention, which is attention where one set of vectors does the looking and a different set is looked at. Here the slots look and the tokens are looked at.

It then processes the slots with an MLP-Mixer, a small network I describe below, and updates each token with its own MLP. It never unpacks the slots back into token space.

We ran a controlled sweep against several other linear-time blocks and against a transformer with the same width and depth. The simplest variant had the lowest validation loss, where the loss is how badly the model predicts text it was not trained on, lower being better. It beat the attention baseline at finding one planted sentence in 4,096 tokens of filler, and it ran up to 14 times faster at 4,096 tokens.

The pack step

A little algebra follows, which is the price of being precise about the cost. Let X∈RN×dX \in \mathbb{R}^{N \times d} be the token states, one vector of the model width for each token, and S∈RM×dS \in \mathbb{R}^{M \times d} the slot states, one vector of the same width for each slot. LN is layer normalisation, which rescales each vector to a standard size before it is used. The first line of the block is

S←S+Attn ⁣(Q=LN(S), K=LN(X), V=LN(X)). S \leftarrow S + \mathrm{Attn}\!\left(Q = \mathrm{LN}(S),\ K = \mathrm{LN}(X),\ V = \mathrm{LN}(X)\right).

Attention works with three kinds of vector. A query is what a reader is looking for, a key is what each item offers to be found by, and a value is what the reader gets back. Here the slots are the only queries, and the tokens supply the keys and values.

Take one slot with query vector qmq_m. It scores every token's key knk_n by a dot product, which multiplies the two vectors entry by entry and adds up the products, so the score is large when the two point the same way. The softmax then turns those NN scores into NN weights that are positive and sum to one, by exponentiating each score and dividing by the total. The slot's update is the average of the token value vectors under those weights,

αmn=exp⁡ ⁣(qm⊤kn/d)∑n′exp⁡ ⁣(qm⊤kn′/d),Δsm=∑n=1Nαmn vn, \alpha_{mn} = \frac{\exp\!\left(q_m^{\top} k_n / \sqrt{d}\right)}{\sum_{n'} \exp\!\left(q_m^{\top} k_{n'} / \sqrt{d}\right)}, \qquad \Delta s_m = \sum_{n=1}^{N} \alpha_{mn}\, v_n,

which is a point somewhere between the NN value vectors, inside the shape they span. The slot chooses where in that shape to sit by what it is asking for, which is what makes the operation depend on the content. Every token is a candidate, which is what makes it global.

MM slots give MM such points, each a different weighted summary of the whole sequence. The cost is MM queries against NN keys, O(NMd)O(NMd), and it is one-way. Slots read from tokens. Tokens never read from slots.

The mix step

S←S+Mixer ⁣(LN(S)). S \leftarrow S + \mathrm{Mixer}\!\left(\mathrm{LN}(S)\right).

The slots now have to talk to each other, and the tool for that is the MLP-Mixer. An MLP is the plainest kind of neural network. It multiplies its input by a matrix of weights, passes each result through a nonlinearity, a fixed curve such as one that sets negative values to zero, and multiplies by a second matrix. Without the curve in the middle the two multiplies would collapse into one, so the curve is what lets the network do more than a straight line. The MLP-Mixer, from the paper of that name, applies MLPs to a table along each of its two axes in turn.

SS is an M×dM \times d table, one row per slot, and each column is one channel, meaning one of the entries that every vector has. A slot-mixing MLP acts down each column of the table, across the MM slots with the same weights for every channel. A channel-mixing MLP acts along each row, across the dd channels with the same weights for every slot. Each has a residual connection, meaning its output is added to its input rather than replacing it, and a layer norm,

S←S+W2 σ ⁣(W1 LN(S)),S←S+σ ⁣(LN(S) U1)U2, S \leftarrow S + W_2\, \sigma\!\left(W_1\, \mathrm{LN}(S)\right), \qquad S \leftarrow S + \sigma\!\left(\mathrm{LN}(S)\, U_1\right) U_2,

with W1∈Rh×MW_1 \in \mathbb{R}^{h \times M} and W2∈RM×hW_2 \in \mathbb{R}^{M \times h} mixing slots and U1∈Rd×h′U_1 \in \mathbb{R}^{d \times h'} and U2∈Rh′×dU_2 \in \mathbb{R}^{h' \times d} mixing channels.

Compare the slot-mixing half to self-attention among the slots, which is attention where the same set of vectors both looks and is looked at. Self-attention would compute S←S+AVS \leftarrow S + A V with an M×MM \times M matrix A=softmax(QK⊤)A = \mathrm{softmax}(QK^{\top}) that depends on the content. The Mixer replaces that content-dependent AA with a fixed learned map on the slot axis, the same for every input. That is why the MLP-Mixer paper describes it as a static approximation of self-attention.

That is also why it cannot be used on the tokens. W1W_1 has MM columns, so it needs the number of things it mixes to be fixed, and a sequence of varying NN cannot go through it. The slots always number MM, so it fits them. The dependence on content that the encoder needs has already been spent in the pack step, so the mixing can afford to be fixed. It is all dense matrix multiplies on fixed shapes, with no NN anywhere in the cost.

The token update

X←X+MLP ⁣(LN(X)). X \leftarrow X + \mathrm{MLP}\!\left(\mathrm{LN}(X)\right).

Each token gets a small MLP of its own, applied to that token alone, O(Nd2)O(Nd^{2}) in total, and that is the only thing the tokens do in a block.

The designs this grows out of, Luna, Perceiver and the Set Transformer, pack into a latent, a small set of vectors like the slots here, and then unpack back into token space with a second cross-attention, so the tokens can read what the latent found. MemoryMixer does not. Token states only matter as the keys and values the next block's pack step reads, and the sweep below shows that letting them read the slots did not help.

The encoder output is the final SS, MM vectors regardless of NN. The decoder is an ordinary transformer whose cross-attention reads those MM vectors instead of NN token states. Stack LL of these blocks and the whole encoder is linear in NN.

one MemoryMixer blockN tokens, M slots · tokens never read from the slots, slots read from the tokenspack · S ← S + Attn(Q=LN S, K=LN X, V=LN X)X · N tokensS · M slotscost O(N M d) · every slot reads every tokenmix · S ← S + Mixer(LN S)X · N tokensS · M slotscost O(M² d + M d²) · learned fixed mixing, no N anywhereupdate · X ← X + MLP(LN X)X · N tokensS · M slotscost O(N d²) · each token on its ownrepeat for L blocks, then decode from SX · N tokensS · M slotsthe encoder output is the final S, M vectors
The three steps of one block, with the tokens on the left and the memory slots on the right. First every slot reads from every token through attention. Then the slots mix among themselves with a fixed learned map. Then each token is updated by its own small network. Each step carries its cost, and none of the costs has the number of tokens squared in it, so the block is linear in the sequence length once the number of slots is fixed.

The cost

Add the three terms. Pack costs O(NMd)O(NMd), the per-token MLP costs O(Nd2)O(Nd^{2}), and the mixer does not see NN at all. For a fixed number of slots M≪NM \ll N the block is linear in NN, against O(N2d)O(N^{2}d) for self-attention.

Below a few hundred tokens the slot bookkeeping costs more than attention saves, and the measured wall-clock crossover is near 384 tokens. From 512 tokens every variant with fewer than 256 slots takes less time and fewer arithmetic operations than the attention block, and at 4,096 tokens the speed-up reaches 14 times.

That is more than the operation count predicts. The work is dense matrix multiplies on fixed shapes, which use the hardware better than attention does, and it needs none of the custom kernels that linear attention and state-space models, the other linear-time families, rely on. The operation count matters on its own on edge devices, meaning phones and other small machines that run the model locally, where the energy the processor uses tracks it.

mixing cost per layer against sequence length, d = 256, M = 64attention O(N² d) against MemoryMixer O(N M d + N d²) · log-log128256512102420484096819210^810^910^10attentionMemoryMixerthe lines cross near N = 320 · measured wall clock crosses at about 384 and reaches 14× at 4,096
The number of arithmetic operations one layer needs, for attention and for MemoryMixer, as the sequence gets longer. Both axes are logarithmic, so a straight line is a power law and a steeper line is a higher power. Below a few hundred tokens the slot bookkeeping costs more than attention saves. Above that the attention line climbs as the square of the length while the MemoryMixer line climbs in proportion to it, and the gap opens. The measured speed-up is larger than this count predicts, because the work is dense matrix multiplies on fixed shapes, which the hardware runs well.

The sweep

The experiments use a few named things, so here is what each one is.

namewhat it is
T5Gemma 2an open encoder-decoder architecture from Google that we borrowed the layout of
UL2a training objective where spans of the input are blanked out and the model has to fill them in
FineWebEdua large set of educational web pages used as training text
RULER needle testa short fact is hidden in a long block of filler and the model is scored on how well it recalls it
WMTa standard set of translation data, here English to German
GLUEa standard set of nine sentence-level language understanding tasks

We trained six MemoryMixer variants and a transformer on the T5Gemma 2 layout at width 256, with four encoder and four decoder blocks, on 4,096-token sequences, with the UL2 blank-filling objective on 1.5B tokens of FineWebEdu, and with 16, 64 and 256 slots each.

The variants add machinery to the minimal block. One adds an unpack step back into token space. One swaps the token MLP for a ConvMLP, which adds a convolution, a layer that lets each token see a few neighbours on either side with the same weights at every position. One feeds summary statistics of the whole sequence into the slots through a bilinear map, a layer that multiplies two inputs together through a learned matrix, before unpacking.

The minimal block, pack, mix and local update, with 64 slots was best, at 4.2137 validation loss against 4.3168 for the transformer, and seventeen of the eighteen slot and block combinations also beat the transformer. More machinery did not help.

blockslotseval loss
pack · mix · MLP644.2137
pack · mix · MLP2564.2246
pack · mix · unpack · ConvMLP2564.2258
pack · mix · ConvMLP2564.2277
pack · unpack · MLP2564.3109
transformer–4.3168

Two things can be read off the table. The block without a mixer, pack and unpack only, is the worst of the family and barely beats the transformer, so the dense mixing among slots is doing real work. And the unpack step is not, since the best block has none.

Scaling width from 256 through 512 to 768 lowered the loss the way it does for the transformer, so the slot bottleneck is not eating capacity as the model grows.

Retrieval and probes

Compressing context into fixed slots sounds like it should hurt retrieval. We tested this with the RULER needle task at a context of 4,096 tokens, which the models never saw during training. The score is the log-probability the model gives the hidden fact, meaning the logarithm of the probability it assigns to the right answer, higher being better. It is tracked over training, with three attention runs and five runs per slot count.

It did not hurt. Every slot count beat the matched attention baseline over the whole of training, with 64 slots a clear step up from 16 and little gained beyond that. Our reading is that keeping global mixing and local processing apart lets the slots preserve token identities rather than blur them.

Linear probes on the attention maps say what the slots are doing. A linear probe is a simple classifier trained on a model's internal numbers to see what information they contain. We passed FineWebEdu through spaCy, an off-the-shelf language toolkit, to label each word with its grammatical role and similar categories. We then trained linear classifiers on the pack attention distributions and on the decoder's cross-attention distributions to predict those labels.

A probe on the first encoder layer's pack attention predicts fifteen categories, part-of-speech tags among them, which say whether a word is a noun, a verb and so on, at 0.463 accuracy, and one on the last decoder layer's cross-attention at 0.512. Attention is run several times side by side with different weights, and each copy is a head. The most predictive encoder heads are in the first layer and the most predictive decoder heads are in the last.

We read that as an ingest phase, where grammatical and word-level information is sorted into slots on the way in, and an emit phase, where it is read back out for generation. So the slots are not used uniformly. They specialise, and that is our reading of why retrieval held up.

Nested slot counts and feed-forward widths

Matryoshka representation learning trains one model so that the first few coordinates of its output are themselves a working output, like the nested dolls. The first coordinates then have to carry what matters most, and one trained model can be cut to several sizes.

The block has two axes that nest this way without any change to the architecture. The slots nest because masking all but the first M′≤MM' \le M of them leaves a valid smaller block. The feed-forward layer, the token MLP, nests too. Its hidden width is the number of units in its middle layer. An MLP with hidden width dffd_{\text{ff}} is a sum of dffd_{\text{ff}} terms, one per hidden unit, and keeping the first dff′≤dffd'_{\text{ff}} \le d_{\text{ff}} units is a partial sum. A checkpoint is a saved set of trained weights. Train so that every prefix works and one checkpoint can be served at any of them. The figures below score each cut by perplexity, which measures how surprised the model is by held-out text, so lower is better.

one checkpoint, sliced at a prefix of either axisthe first slots, or the first FFN units, are shared by every budget · summed elastic modelslot axis, d_ff fixed at 1,024M′ = 64 · ppl 67.79M′ = 128 · ppl 63.85M′ = 256 · ppl 59.95FFN axis, M fixed at 64d′_ff = 128 · ppl 64.38d′_ff = 256 · ppl 62.37d′_ff = 512 · ppl 61.29d′_ff = 1,024 · ppl 61.28dark is the prefix every budget shares · dedicated slot models sit at 61.27, 62.32 and 63.37
One trained model sliced at different sizes. Each bar is one deployment size, and the dark part at the left is the part that every size shares. On the left the model is cut to its first 64, 128 or 256 memory slots. On the right it is cut to the first 128 to 1,024 hidden units of its feed-forward layer. The number next to each bar is the validation perplexity at that cut, lower being better. These are the numbers for the model trained with the losses of all sizes summed, at width 256 with 8 encoder and 4 decoder layers.

There are two ways to train it. One is to sum the losses of every prefix at every step, which is the original Matryoshka recipe. The other is to sample one prefix per step and update the weights from that prefix's loss alone, which is the MatFormer recipe and is cheaper. We trained one checkpoint each way for each axis, with a UL2 objective on 900K samples for two epochs at d=256d = 256, with 8 encoder and 4 decoder layers and a full feed-forward width of dff=1024d_{\text{ff}} = 1024. We compared against dedicated single-budget models trained under the same recipe.

Summing won at every width on both axes. On the slot axis the summed model reaches 59.95 perplexity at 256 slots against 63.37 for a dedicated 256-slot model, and is 6.5 behind the dedicated model at 64.

On the feed-forward axis, with M=64M = 64, the summed model matches the dedicated baseline at 1,024, 61.28 against 61.27, and is 3.1 worse at 128, which is an eight-fold narrower layer. The sampled model's perplexity grows four-fold at 512 and more than fifty-fold at 128.

one checkpoint, deployed at three slot counts · validation perplexitysummed elastic loss against dedicated single-budget models · M = 64, 128, 256M=64 · dedicated61.27M=64 · elastic67.79M=128 · dedicated62.32M=128 · elastic63.85M=256 · dedicated63.37M=256 · elastic59.95
Validation perplexity, lower being better, for one elastic model run at 64, 128 or 256 slots, next to three separate models that were each trained for only one of those slot counts. The elastic model was trained with the losses at every slot count added together. At 256 slots it beats the model trained only for 256 slots. At 64 slots it is 6.5 perplexity behind the model trained only for 64. Training by sampling one slot count per step instead of summing was worse at every width and fell apart on the feed-forward axis.

What it does not say

What stands is that encoding a sequence does not need every token to interact with every other token. A fixed latent memory mixed by dense matrices keeps the transformer's scaling while running in linear time on hardware that already knows how to multiply matrices. And the same checkpoint can serve several budgets.

References

  1. Tolstikhin et al., MLP-Mixer: an all-MLP architecture for vision, 2021.
  2. Jaegle et al., Perceiver IO: a general architecture for structured inputs and outputs, ICLR, 2022.
  3. Lee et al., Set Transformer: a framework for attention-based permutation-invariant neural networks, ICML, 2019.
  4. Ma et al., Luna: linear unified nested attention, 2021.
  5. Kusupati et al., Matryoshka representation learning, NeurIPS, 2022.
  6. Devvrit et al., MatFormer: nested transformer for elastic inference, 2023.
  7. Tay et al., UL2: unifying language learning paradigms, 2022.
  8. Zhang et al., T5Gemma 2: seeing, reading, and understanding longer, 2025.
  9. Gu and Dao, Mamba: linear-time sequence modeling with selective state spaces, 2024.
  10. Hooker, The hardware lottery, 2020.