TL;DR
Standard transformers face a brutal trade-off: exact memory over long contexts costs quadratic time, while faster alternatives suffer from amnesia by compressing the past. By restricting individual attention heads to just two dimensions—while keeping overall model width and depth unconstrained—this paper achieves both polylogarithmic speed per token and universal Turing completeness. To prove it isn't just theoretical, the authors built a compiler that turns C code directly into transformer weights, allowing a vanilla transformer to execute millions of steps of deterministic algorithms like a literal CPU.
What Problem Does This Paper Address?
Transformers owe their magic to self-attention, where every new token looks back at every previous token. The catch is math: generating a sequence of length $N$ takes $\Theta(N^2)$ time. If you want a model to run a deterministic calculation spanning millions of steps, standard autoregressive generation grinds to a halt.
Existing workarounds—like sliding windows, state-space models (e.g., Mamba), or linear attention—buy speed by trading away expressivity, squashing the past into a fixed-size summary. Meanwhile, prior attempts to approximate exact softmax attention in near-linear time either restricted the sizes (norms) of keys and queries—accidentally reducing the model back to fixed-capacity linear attention—or still paid heavy polynomial overheads per step. The authors ask: Is this trade-off between speed and universal expressivity actually fundamental?
Methods
The authors find a geometric "Goldilocks zone" by setting the per-head dimension to $d_h = 2$ (you can still stack as many heads and layers as you want).
- Fast Online 2D Attention: Softmax attention is dominated by keys with the largest inner products with the query. Prior work found these "active" keys using 2D half-plane range searches, costing $\widetilde{O}(\sqrt{N})$ per query. Because we are on a 2D plane, the authors map keys relative to their upper convex hull, flattening the hull to the x-axis. This transforms the expensive diagonal half-plane search into a crisp, axis-aligned 2D rectangle search using standard range trees, paired with localized Taylor polynomials and a binary-counter "doubling trick" to handle streaming tokens in amortized $\text{polylog}(N)$ time.
- Append-only Lookup Machines (ALMs): To prove 2D heads can compute anything, the authors design an abstract machine (ALM) using only two memory reads: exact point lookup and cumulative sum. Each maps cleanly to a 2D attention head. Point lookup uses a clever parabolic trick: mapping integer key $k \mapsto (2k, -k^2)$ and query $q \mapsto (q, 1)$ makes the inner product $2qk - k^2 = -(k-q)^2 + q^2$, which peaks uniquely at $k = q$!
- C-to-Weights Compiler: They introduce CALM (a language for ALMs), write a 32-bit integer (
i32) WebAssembly (WASM) interpreter in it, and use Mixed-Integer Linear Programming (MILP) to pack the logic into minimal transformer width. Using a classic compiler trick (the first Futamura projection), they bake the WASM program directly into the feed-forward weights.
Datasets
Because the weights are analytically compiled rather than trained, there are no training datasets. For evaluation and verification, the authors test on:
- A test corpus of C/WASM programs exercising arithmetic, branches, memory access, and recursion (including a ~44k-token Collatz sequence trace).
- Published "hardest Sudoku" instances, specifically Arto Inkala's 2012 puzzle and AI Escargot (2006), alongside context from Sudoku-Bench.
Key Insights and Ideas
- Two dimensions is the sweet spot: 1D can't isolate arbitrary past tokens; higher dimensions blow up exponentially and hit computational complexity walls (SETH lower bounds) above $\log N$ dimensions. 2D gives just enough planar geometry for fast range trees and exact memory addressing via parabolas.
- Chain-of-thought as a CPU trace: With just four ALM memories (tracking depths and contents of two stacks), a single fixed 2D transformer is Turing complete, reconstructing machine state entirely from its own emitted tokens.
- Programs are weights: You don't have to hope a model learns an algorithm; C code compiled via
clangto WASM can be mathematically folded straight into standard causal transformer matrices.
Key Results
- Exponentially faster exact-ish attention: Achieves $\widetilde{O}(N)$ total time ($\text{polylog}(NV/\varepsilon)$ amortized per token) for online 2D softmax attention with unbounded norms.
- Efficient RAM simulation: Simulates $T$ steps of an $O(\log n)$-bit word-RAM machine in $\text{polylog}(n+T)$ amortized time per step.
- 100% accuracy on brutal Sudokus: Compiled with a C backtracking solver, the PyTorch transformer solves Inkala's 2012 puzzle (~5.94M tokens in 150.7s) and AI Escargot (~5.36M tokens in 139.4s) on a single CPU thread—tasks where frontier reasoning LLMs score ~0% without tools.
- Massive cache speedup: On the ~44k-token Collatz trace, their convex-hull KV cache finishes in 0.99 seconds versus 229.2 seconds for a standard linear-scan KV cache (~230× speedup).
Limitations and Future Work
- Precision grows with runtime: Simulating $T$ steps requires $O(\log(n+T))$ bits of numerical precision (standard
float64comfortably handles millions of steps, not infinity). - Finite-horizon softmax bridge: While exact compilation uses hardmax/ReGLU, approximating it with standard softmax/SwiGLU only guarantees exactness over finite runs with run-dependent scaling parameters.
- Constructed vs. learned weights: All experiments use analytically compiled weights. Training large-scale 2D-head transformers from data—and accelerating their inference with the new range-tree algorithm—remains an exciting next step.
Summary updated