Spatial Induction Heads: In-Context Learning of Multidimensional Cellular Automata
Organizations: Cornell University
Abstract
Induction heads provide a mechanistic account of in-context learning in sequential data, but existing theory largely assumes that the context relevant to a prediction forms a contiguous block. In multidimensional data, serialization breaks this assumption by scattering spatial neighbors across distant positions in the token sequence. We study how transformers overcome this routing problem in multidimensional stochastic and deterministic cellular automata, where each trajectory is generated by an unknown local rule and presented as a flattened sequence without an explicit coordinate-based spatial inductive bias. We introduce spatial induction heads, two-layer gather-and-match circuits in which the first layer reconstructs the relevant spatial neighborhood and the second matches the resulting configuration against earlier occurrences. We give two explicit realizations of the gather and show that the positional dimension required for spatial routing depends only on the local neighborhood and spatial dimension, not on grid volume or trajectory horizon. We further construct a matching layer which implements Bayesian counting. The end-to-end circuit can approximate the Bayesian posterior arbitrarily closely for stochastic rules and can predict exactly for deterministic rules. Empirically, trained two-layer transformers generalize to unseen rules in one and two dimensional settings, achieving near-perfect deterministic rollouts and less than 0.005 nats KL from the Bayes-optimal predictor on stochastic rules. Attention patterns and layerwise probes align with the predicted gather-and-match computation, providing mechanistic evidence for spatial induction in trained transformers.
Figures & tables
| Deterministic | Stochastic | ||||
|---|---|---|---|---|---|
| Task | Arch. | TF cell acc | AR cell acc | AR cell acc ( -gram) | AR KL |
| 1D | Shared-head | 1.0000 | 0.9999 | 0.5496 (0.5485) | 0.0019 |
| Dedicated-heads | 1.0000 | 1.0000 | 0.5488 (0.5485) | 0.0044 | |
| 1D | Shared-head | 1.0000 | 1.0000 | 0.3476 (0.3475) | 0.0026 |
| Dedicated-heads | 1.0000 | 1.0000 | 0.3475 (0.3475) | 0.0004 | |
| 2D VN | Shared-head | 1.0000 | 1.0000 | 0.5201 (0.5197) | 0.0013 |
Appendix figures & tables17 assets
Supplementary material from the paper’s appendix.
Appendix
| Stage result | Downstream usage |
|---|---|
| Corollary E.6 | Positional features for offset selection in previous time steps |
| Corollary F.5 | Distinct color slots and storage costs (Construction II) |
| Corollary G.12 | State blocks (approximate or exact) per routed offset |
| Theorem H.2 | Equal-weight matching for empirical counts and Dirichlet smoothing |
| Lemmas H.3 , H.5 , H.6 | Bounds on separation, dispersion, and predictive cost |
| Theorem H.9 | Finite-parameter prediction guarantee |
| symbol | meaning | fixed at |
|---|---|---|
| Grid and dual group | ||
| number of spatial dimensions | Def. 1 | |
| side length of axis | Def. 1 | |
| largest side length, | Lem. E.3 | |
| , | the torus ; its volume | Def. 1 |
| , , | a cell of ; its -th coordinate; an offset | Def. 1 |
| 1D, | 1D, | 2D von Neumann | |
|---|---|---|---|
| , , | , , | , , | , , |
| , | , | , | , |
| , | , | , | von Neumann, |
| , | , | , | , |
| tokens per step, | , | , | , |
| token | predicts | spatial target |
|---|---|---|
| cell , | cell | |
| cell , | a separator | none |
| separator | first cell of the next row |
| 1D, | 1D, | 2D von Neumann | |
|---|---|---|---|
| degree bound on | |||
| subgroup | |||
| colors |
| neighborhood | grid | least periodic | degree bound | ||
|---|---|---|---|---|---|
| elementary | 4 | 5 | 7 | 5 | |
| elementary | 4 | 4 | 7 | 16 | |
| elementary | 4 | 4 | 7 | 32 | |
| von Neumann | 8 | 25 | 23 | 25 | |
| von Neumann | 8 | 12 | 23 | 36 | |
| von Neumann | 8 | 49 | 23 | 49 |
| 1D, | 1D, | 2D von Neumann | |
| Construction I (Thm. G.9 ) | |||
| heads, | , | , | , |
| , | , | , | , |
| width of | |||
| residual | |||
| block-error threshold | |||
| Dataset type | Train | Val-in (no filter) | Val-in (filter) | Val-out / Val | Test |
|---|---|---|---|---|---|
| Deterministic | No | No | Yes | Yes | Yes |
| Stochastic | No | — | — | Yes | Yes |
| Task | Rule type | Grid | configs | ||
|---|---|---|---|---|---|
| 1D | det / stoch | 16 | 10 / 20 | 4 / 10 | 8 |
| 1D | det / stoch | 32 / 16 | 12 / 20 | 8 / 10 | 27 |
| 2D VN | det / stoch | 12 / 20 | 8 / 10 | 32 |
| Task | Arch. | heads | mlp | lr | wd | ep | |||
|---|---|---|---|---|---|---|---|---|---|
| 1D | Shared-head | 128 | 512 | 0.2 | 0.999 | 3 | |||
| Dedicated-heads | 128 | 512 | 0.2 | 0.999 | 3 | ||||
| 1D | Shared-head | 256 | 1024 | 0.2 | 0.95 | 8 | |||
| Dedicated-heads | 256 | 1024 | 0.2 | 0.95 | 8 | ||||
| 2D VN | Shared-head | 1024 | 4096 | 0.2 | 0.95 | 5 | |||
| Dedicated-heads | 1024 | 4096 | 0.2 | 0.95 | 5 |
| Task | Arch. | heads | mlp | lr | wd | ep | |||
|---|---|---|---|---|---|---|---|---|---|
| 1D | Shared-head | 256 | 1024 | 0.2 | 0.999 | 15 | |||
| Dedicated-heads | 128 | 512 | 0.2 | 0.999 | 8 | ||||
| 1D | Shared-head | 256 | 1024 | 0.2 | 0.95 | 8 | |||
| Dedicated-heads | 256 | 1024 | 0.2 | 0.95 | 8 | ||||
| 2D VN | Shared-head | 1024 | 4096 | 0.2 | 0.95 | 10 | |||
| Dedicated-heads | 512 | 2048 | 0.2 | 0.95 | 5 |
| Task | Arch. | TF seq acc | TF cell acc | AR cell acc |
|---|---|---|---|---|
| 1D | Shared-head | 0.9990 | 1.0000 | 0.9999 |
| Dedicated-heads | 0.9994 | 1.0000 | 1.0000 | |
| 1D | Shared-head | 0.9994 | 1.0000 | 1.0000 |
| Dedicated-heads | 0.9989 | 1.0000 | 1.0000 | |
| 2D VN | Shared-head | 1.0000 | 1.0000 | 1.0000 |
| Dedicated-heads | 0.9989 | 1.0000 | 0.9999 |
| TF KL | TF cell acc | AR | ||||
|---|---|---|---|---|---|---|
| Task | Arch. | kg m | true m | (kgram) | cell acc (kgram) | KL kg m |
| 1D | Shared-head | 0.0020 | 0.0185 | 0.7491 (0.7499) | 0.5496 (0.5485) | 0.0019 |
| Dedicated-heads | 0.0046 | 0.0211 | 0.7469 (0.7499) | 0.5488 (0.5485) | 0.0044 | |
| 1D | Shared-head | 0.0026 | 0.0881 | 0.5669 (0.5674) | 0.3476 (0.3475) | 0.0026 |
| Dedicated-heads | 0.0004 | 0.0859 | 0.5675 (0.5674) | 0.3475 (0.3475) | 0.0004 | |
| 2D VN | Shared-head | 0.0014 | 0.0300 | 0.7345 (0.7351) | 0.5201 (0.5197) | 0.0013 |
| Deterministic (Rule 110) | Stochastic (Stavskaya) | |
|---|---|---|
| Task | 1D | 1D |
| Training | ||
| Plotted | ||
| heads / mlp | ||
| / | ||
| lr, wd, epochs | , 0.2, 10 | , 0.2, 8 |