Universal and Efficient Computation with 2D Attention

Christos Tzamos, Guoqing Zheng, Athul Paul JacobNeurIPS 2026

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).

  1. 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.
Geometric backbone of 2D attention: flattening the convex hull turns a costly halfspace search (middle) into a fast 2D rectangle range search (right).
  1. 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$!
  2. 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 clang to 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.
Table 1: Comparison with prior work for approximate 2D attention with N tokens
  • 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 float64 comfortably 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

Abstract

Transformers are powerful models but are inefficient with large context due to the quadratic complexity of the attention mechanism. Limiting their context window addresses the efficiency concern but at a cost: they cannot remember everything from the past, which severely limits their computational power. We show that we can simultaneously achieve efficiency and universal computation if we restrict attention heads to be 2-dimensional. First, we give the first algorithm for standard 2D attention that serves every insertion and every query in amortized polylogarithmic time per token. Prior work either restricted key/query magnitudes or paid polynomial overhead per step. Second, we show that the class of 2D-attention models is universal and efficient: a vanilla 2D transformer efficiently simulates an arbitrary RAM machine via chain-of-thought decoding so that, combined with our first result, $T$ steps of a word-RAM machine can be simulated in $\widetilde O(T)$ time. In contrast, earlier universality results focused on computability rather than per-step efficient simulation. Third, as an application, we turn a transformer into a computer. We provide a compiler that supports the full $\texttt{i32}$ WebAssembly subset with integer computations (reachable from C via $\texttt{clang}$) and turns such programs into the weights of a transformer, which then executes them over millions of steps autoregressively. We illustrate how this can be used to solve combinatorial problems, tasks at which even large reasoning LLMs struggle.
BibTeX
@inproceedings{tzamos2026universal,
  author = {Christos Tzamos and Guoqing Zheng and Athul Paul Jacob},
  title = {{Universal and Efficient Computation with 2D Attention}},
  booktitle = {Advances in Neural Information Processing Systems},
  year = {2026},
  url = {https://openreview.net/forum?id=4ZHuwBppLm},
}
Figure