Marek Oerlemans

Logo

Find out about my work

View My GitHub Profile

Thinking longer: scaling test-time compute with a looped transformer

By Marek Oerlemans December 2026

A normal neural network spends the same amount of compute on every input: an easy question and a hard one both go through the same stack of layers once. A looped transformer works differently. It applies the same small block of layers over and over, so you can decide at test time how long it gets to think. Spending more compute on harder problems, scaling test-time compute, is also the idea behind the newer chatbots that think before they answer. I got curious about looping when Thinking Machines released Inkling 2 Mini, a language model that runs its middle layers several times for every token. In this post I’ll tell you about my exploration of looped transformers, and how I taught one to solve mazes.

Mazes are just the playground here. What I wanted to show is that a looped transformer gets better the more passes it makes, and that this lets it solve problems far outside what it was trained on. So I trained a small model, fewer than a million parameters, on mazes of at most 12×12 with 30 passes through its loop, and then gave it 33×33 mazes. At those 30 passes it solves none of them. Let it loop for 1500 passes and it solves 62%.

A 33×33 maze goes into the looped network; the predicted path grows with every pass

Above you see the looped network working on a 33×33 maze, much bigger than any maze it was trained on. The counter in the middle shows how many passes it has made, in steps of 10. On the right is its answer after that many passes: cells it marks as path are filled in green if they really are on the path and blue if not, and the prediction of every single pass in between shows up in faint blue. The thin green line with arrows is the true path, from the green start to the red goal. With every pass the answer gets better, until at pass 90 the whole path of 374 cells is right, three times as many passes as the model was trained with.

Looped transformers

A standard transformer is a fixed stack of layers, each with its own weights, and the input goes through every layer exactly once. A looped transformer reuses its layers. Mine has three parts, named after music by Geiping et al. (2025), where a prelude opens a piece and a coda closes it:

At every pass the encoded input is fed back into the core, so the model never loses sight of the problem while it’s working on it.

This has two nice properties. The first is that it saves parameters. My model stores 0.88M parameters. At 30 passes it computes as deep as a 62-block network, which would need about 14M parameters if every block had its own weights; at 1500 passes, that’s 678M. The second is more interesting: the amount of compute is no longer fixed by the architecture. You can run a trained model for more passes than it was trained with. If it has learned an iterative procedure rather than a fixed function, extra passes should let it solve harder problems. It can think longer.

The loop and how it is trained

Figure 1. Top: the looped network and the same network unrolled, with what it stores versus how much compute it does. Bottom: the two parts of the training loss, explained in the next section.

Making the loop learn

The obvious way to train a loop is to run it 30 times, compute the loss, and backpropagate through all 30 passes. That didn’t work well for me: the gradient gets multiplied through 30 copies of the same layers, and training was unstable, with huge gradient spikes, or even collapsed to predicting the same thing for every input. Two tricks fixed it.

Limited backprop. The main loss is still taken after 30 passes, but only the last 5 passes get a gradient. The first 25 run without gradient tracking, and their output is treated as a fixed starting point: a stop-gradient. The model still learns what to do at pass 30, but the gradient only travels through 5 copies of the core instead of 30.

Progressive loss (Bansal et al., 2022). Next to the 30-pass loss, with equal weight, every training step adds a second loss with a random length. First the model runs a random number of passes without gradient (0 to 29), then a random number of passes with gradient from wherever it ended up (1 up to the passes that are left), and then it’s scored. So the model can’t memorise “what to do at pass 17”. It has to learn a step that improves any intermediate state, which is exactly what you need if extra passes at test time should help rather than hurt. This term can still backprop through up to 30 passes, but because its length is random, it’s 8 passes on average.

Limited backprop is what made training stable; the progressive loss is what makes extra passes useful.

The task: mazes

In this project I train a network to predict the solution of a maze. Others have used looped and recurrent transformers for language (Geiping et al., 2025; Inkling 2 Mini) or for puzzles such as Sudoku and mazes of a fixed size (Wang et al., 2025; Jolicoeur-Martineau, 2025). I chose mazes because they have a dial for difficulty: make the maze bigger and the path gets longer, so it takes more steps to trace. That makes “thinking longer” easy to test. Recurrent convolutional networks are known to extrapolate to bigger mazes (Schwarzschild et al., 2021; Bansal et al., 2022), and looped transformers to longer inputs on other tasks (Fan et al., 2025). My question was: can a small looped transformer, trained only on small mazes, keep improving with extra passes and solve mazes much bigger than any it has seen?

Every maze is an $n \times n$ grid made with a randomised depth-first search, so there is exactly one route between any two cells. Each cell is one token with six input features: its four walls, and whether it’s the start or the goal. For every cell, the model predicts whether it’s on the path from start to goal. A maze only counts as solved if every cell is right.

From small mazes to big ones

I started out training only on 9×9 mazes. That worked well: the model solved nearly every new 9×9 maze, building up the path over several passes. But it didn’t carry over to other sizes. On bigger mazes it failed, however long it looped. Extra passes can’t help if a single pass already breaks on a bigger grid. So I made the following fixes. The training mazes stay small (5×5 to 12×12), so everything from 13×13 to 33×33 is still out of sample.

Making the network indifferent to the maze size. Two changes make attention behave the same on a small and a large grid.

Multiple training sizes. Instead of only 9×9, the training mazes are spread evenly over sizes 5 to 12, one size per batch. The model can no longer tune itself to one grid size.

Harder training mazes. With a random start and goal, most paths in small mazes are short, while paths in big mazes are long. An earlier model solved big mazes almost only when the path happened to be short. So for half of the training mazes I put the goal among the cells farthest from the start.

Results

This made it work. The final model was trained for 60 epochs with 30 passes. I test it on 200 new mazes per size and vary the number of passes at test time.

Percentage of mazes solved vs number of passes, per maze size

Figure 2. Percentage of test mazes solved exactly, against the number of passes at test time (log scale). The dashed line marks the 30 passes the model was trained with; everything to the right of it is thinking longer than it was trained to.

Bigger mazes need more passes, and the model uses them. At its training budget of 30 passes, the model solves 2% of the 25×25 mazes and none of the 33×33 ones. Just letting it run longer brings that to 89.5% and 61.5% at 1500 passes, and 33×33 is still going up. Nearly all of the out-of-sample performance comes after the number of passes it was trained with.

It solves long paths, not just lucky short ones. At 33×33 with 1500 passes, paths of more than 300 cells are solved 69% of the time (25 of the 36 such mazes). That’s more than two and a half times the longest path in the training data (113 cells).

Thinking longer isn’t free, of course: 1500 passes on one 33×33 maze take about 30 to 40 seconds on my laptop GPU, against under a second for 30 passes.

Extra passes rarely hurt. Once a maze is solved, it almost always stays solved. Of the mazes solved at some point, only a few were lost again by pass 1500: 2 of 198 at 19×19, 7 of 186 at 25×25, and 5 of 128 at 33×33.

So what?

I set out to see whether a looped transformer can think longer, and it can. A model with under a million parameters learned to solve mazes, and because it’s a loop, how much compute it spends is a choice you make at test time, not something fixed by the architecture. That choice matters: on 25×25 and 33×33 mazes, far bigger than anything it was trained on, it goes from almost nothing at its training budget of 30 passes to solving most of them when it gets hundreds of passes. The loop learned a procedure that keeps improving its answer for hundreds of passes beyond what it was trained for. That makes looped transformers a very promising direction for problems where you want to spend more compute on harder inputs.

There is a big caveat, though. Mazes are a friendly setting. I could generate as much training data as I wanted, and when the model failed on long paths, I could simply make the training mazes harder. In for example healthcare, you can do neither. You can’t generate extra patients, every label costs a doctor’s time, and the hard cases, such as rare diseases or unusual scans, are exactly the ones you have the fewest examples of.

All code and the trained model are on GitHub: github.com/moerlemans/looped-transformers-maze. You can run the model on a maze of your own choice in about half a minute.

References