Looped Transformers: More Depth Without More Parameters

9 min read

A normal transformer gets deeper by stacking more layers, and every new layer brings its own weights. A looped transformer takes one block and runs it again and again on its own output. The depth goes up, the parameter count stays the same.

That sounds like a trick that should not work. So we tested it on a laptop-sized problem: one transformer block run 12 times against a stack of 12 different blocks. The looped model got close to the stack with one twelfth of the parameters, and it also showed us where the idea breaks.

A normal stack of 12 blocks next to one looped block, and our test error as the number of loops changes

What looping means

Take a transformer block f: attention plus an MLP, with residual connections. A normal model with 12 layers computes f12(...f2(f1(x))), where each f has its own weights. A looped model computes f(f(f(...f(x)))) with one f and repeats it K times.

The idea is older than the recent papers. Universal Transformers (Dehghani, Gouws, Vinyals, Uszkoreit and Kaiser, ICLR 2019) applied the same block repeatedly across depth and added a per-position halting rule, so each token could decide how many steps it needed. The authors showed that under certain assumptions the model is Turing-complete, and reported a 0.9 BLEU gain over a standard Transformer on WMT14 English-German.

Most recent looped models add one more detail, called input injection. Instead of feeding only the last output back in, they add the embedded prompt again before every loop:

py
def forward(self, tok, loops=1, all_steps=False):
    p = self.read_in(tok)                    # the prompt, embedded once
    h = torch.zeros_like(p)
    outs = []
    for _ in range(loops):
        h = h + p                            # input injection: Y_{t+1} = M(Y_t + P)
        for b in self.blocks:
            h = b(h)
        outs.append(self.read_out(h[:, -1]).squeeze(-1))
    return outs if all_steps else outs[-1]

That is the forward pass from our lab. With one block in self.blocks and loops=12, it is a looped model. With 12 blocks and loops=1, it is a normal stack. The h + p line comes from Leiay/looped_transformer, the official code for Yang et al., whose TransformerModelLooped calls its backbone on output + embeds in every loop. Without it, the prompt has to survive many passes through the same block, and small errors pile up.

Why reuse gives compute for free (in parameters)

Each loop does the full work of one layer: attention over every token, then the MLP. So 12 loops cost about the same compute as 12 layers. What you save is memory for weights. The model file is the size of one block, but the computation is as deep as you choose to run it.

This matters because many problems seem to need depth more than they need lots of different weights. An iterative algorithm, like gradient descent or a search that refines a guess, does the same step many times. A looped block is a natural fit for it: learn the step once, then repeat it.

Giannou et al. (Giannou, Rajput, Sohn, Kangwook Lee, Jason D. Lee and Papailiopoulos, ICML 2023) pushed this to the limit. In "Looped Transformers as Programmable Computers" they hand-set the weights of a looped, 13-layer transformer so that it emulates a small instruction-set computer. The input acts like a punchcard holding instructions and memory, and the same looped network then runs a basic calculator, a basic linear algebra library and in-context learning with backpropagation. Those weights are constructed by hand, not trained, but they show that a fixed-size looped block can, in principle, run general programs.

Our small run: in-context linear regression

Yang, Lee, Nowak and Papailiopoulos asked whether training finds this kind of solution. In "Looped Transformers are Better at Learning Learning Algorithms" (ICLR 2024) they trained looped transformers on in-context data-fitting tasks and report performance comparable to a standard transformer while using less than 10% of the parameter count.

We copied their setup at toy scale. Each prompt holds 10 pairs (x, y) where y = w.x for a fresh random w in 5 dimensions, then a query x. The model must predict the query's y from the examples alone, so it has to learn a fitting algorithm, not a fixed answer. We trained three models on CPU with the same width (64), the same seed and 2500 Adam steps:

  • one_block: 1 block, run once
  • looped: the same kind of block, run 12 times with input injection
  • stack_12: 12 different blocks, run once

The looped model is trained like the paper's: the loss is averaged over the predictions from the later loops (loops 7 to 12 here), so the model learns to keep improving its answer instead of only getting it right at one exact step.

bash
python looped.py
output
task: in-context linear regression, d=5, 10 examples per prompt, 2500 training steps, batch 128
error = mean squared error on 2000 test prompts / d (lower is better)
always predict 0      error 0.946
least squares         error 0.000
one_block  params  50,497  loops  1  error 0.201  train 49s
looped     params  50,497  loops 12  error 0.011  train 506s
stack_12   params 600,321  loops  1  error 0.006  train 654s
looped model, trained with 12 loops, tested with more or fewer loops:
  loops  1  error 0.556
  loops  2  error 0.357
  loops  4  error 0.067
  loops  6  error 0.018
  loops  8  error 0.010
  loops 12  error 0.011
  loops 16  error 0.026
  loops 24  error 0.147
  loops 32  error 0.454

Three things stand out.

First, the same 50,497 weights do very different jobs depending on how often you run them. Run once, the block gets 0.201. Run 12 times, it gets 0.011, close to the 12-block stack at 0.006, which has 600,321 parameters. Exact least squares gets 0.000 here because there are more examples than dimensions and no noise, so it is the floor.

Second, looping did not save compute. The looped model took 506s to train against 654s for the stack on our machine. Both do 12 block passes per prompt. You save weights, not FLOPs.

Third, look at the sweep. Error keeps falling as you add loops up to 8 and 12, which is the depth the model was trained for. Past that, it gets worse: 0.026 at 16 loops and 0.454 at 32, nearly as bad as one loop. Our block learned a process that is good for about 12 steps, not one that settles on an answer and stays there.

That last point is where our toy and the paper differ. Yang et al. show a looped model trained for 30 loop iterations that reaches a stable fixed-point solution when you keep running it past the trained depth. Their training also grows the number of loops on a schedule while the loss is taken over a truncated window of recent loops. We used a fixed 12 loops and 2500 steps. Getting the fixed-point behaviour seems to need that extra training design, it does not come for free with weight sharing.

Looping as latent reasoning

Chain-of-thought lets a model think longer by writing more tokens. Looping lets it think longer without writing anything: the extra steps happen inside the hidden state.

Saunshi, Dikkala, Li, Kumar and Reddi made this link formal in "Reasoning with Latent Thoughts: On the Power of Looped Transformers" (ICLR 2025). On synthetic reasoning tasks such as addition, p-hop induction and math problems, a k-layer transformer looped L times nearly matches a kL-layer model, and does much better than the k-layer model alone. They prove that looped models can simulate T steps of chain-of-thought with T loops, and they find that both looped and non-looped models scale with effective depth, much like chain-of-thought scales with more generated steps. They also report a split between reasoning and memorization, and propose a looping-based regularizer that helps on both.

Our toy shows the small version of that claim. The 1-block model has the same parameters as the looped one. The only difference is depth, and depth took the error from 0.201 to 0.011.

Test-time compute: turn the loop count up

If the number of loops is a knob, you can turn it at inference time. Hard input, more loops. Easy input, fewer.

Geiping et al. (NeurIPS 2025) built a language model around this idea in "Scaling up Test-Time Compute with Latent Reasoning: A Recurrent Depth Approach". Their model has a recurrent block that is run for a randomly sampled number of iterations during training, so it learns to work with many different depths. They scaled it to 3.5 billion parameters and 800 billion tokens, and report that it improves on reasoning benchmarks, sometimes dramatically, up to a compute load equivalent to 50 billion parameters. The model is public as tomg-group-umd/huginn-0125 on Hugging Face, with code at seal-rg/recurrent-pretraining.

The random depth during training is the key difference from our run. We trained at one depth, and our model fell apart past it. Training across many depths is what lets a recurrent model take a larger loop count at test time and use it.

Ouro (Zhu, Wang and others, a team that includes Yoshua Bengio and Jason Eshraghian) takes looping into pretraining. "Scaling Latent Reasoning via Looped Language Models" trains looped language models on 7.7T tokens with an entropy-regularized objective that learns how much depth to use, and reports that the 1.4B and 2.6B models match 4B and 8B standard transformers on most benchmarks. The authors argue the gain comes from better knowledge manipulation, not from storing more knowledge.

Limits and open questions

  • Looping saves parameters, not compute. Our looped model was cheaper to store but not much cheaper to train. Latency grows with every loop.
  • Running past the trained depth can hurt. Our model went from 0.011 at 12 loops to 0.454 at 32. Fixed-point training, random-depth training or learned halting are the known fixes, and none is free.
  • Memorization is a weak spot. Saunshi et al. describe the reasoning-memorization split, and Ouro credits its gains to knowledge manipulation, not knowledge capacity. A small looped model still has a small store of facts.
  • Knowing when to stop is unsolved in general. Universal Transformers used a halting rule, Ouro learns depth allocation, and many models still use a fixed count.
  • Our run is tiny: one task, one seed, 5 dimensions, a short CPU run. Treat the numbers as an illustration of the paper results, not evidence on their own.

Try this

The lab is in NafiGit/blogllm-labs/looped-transformers. It needs PyTorch (CPU is fine) and matplotlib for the figure.

bash
git clone https://github.com/NafiGit/blogllm-labs
cd blogllm-labs/looped-transformers
pip install torch matplotlib
python looped.py --quick   # smoke test
python looped.py           # full run, writes run_log.txt and results.json
python figure.py           # draws loops.png

Things worth changing in looped.py:

  • Set TRAIN_LOOPS to 24 and see whether the error at 32 loops stops blowing up.
  • Delete the h = h + p line inside the loop (inject the prompt only once) and compare the sweep.
  • Sample a random loop count for each training step, the way Geiping et al. do, and test past the trained depth again.

Sources