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
Figure 1: Trained-model predictions on two held-out rules. Top row: Wolfram Rule 110, a Turing-complete deterministic ECA. Bottom row: Stavskaya, a stochastic CA with p=0.29 ; predictions are colored by the probability the model assigns to state 1. Teacher-forced predictions (middle column) condition on the ground-truth history at every step; autoregressive predictions (right column) are rolled out from the context alone, conditioning on the sample from the previous timestep.
Figure 2: Layer 1 gathers the routing cells; Layer 2 retrieves matching precedents. Attention weights from the shared-head model on 1D V=2,k=3 (elementary cellular automata), Wolfram Rule 110. The query cell at z predicts the target cell at z+u^d (marked “?”). Layer 1 (left) concentrates on the four routing cells in the previous timestep, at offsets U=DK∪DQ relative to z , where DQ=DK+u^d gives the target cell’s parent offsets. Layer 2 (right) concentrates on matching precedents: earlier cells whose parent configurations (at offsets DK ) match the query configuration (at offsets DQ ). Their states are used to predict the target cell.
Deterministic
Stochastic
Task
Arch.
TF cell acc
AR cell acc
AR cell acc ( k -gram)
AR KL
1D V=2,k=3
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 V=3,k=3
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 V=2,k=5
Shared-head
1.0000
1.0000
0.5201 (0.5197)
0.0013
Table 1: Test-set prediction accuracy on held-out rules. Deterministic metrics use the ground-truth history (teacher forcing, TF) or greedy autoregressive (AR) rollout. Stochastic AR cell accuracy is reported for the model and the k -gram sampling baseline. Stochastic AR KL is DKL(qkgram∥qmodel) between the k -gram and model predictive distributions on the model’s rollout.
Figure 3: Attention alignment and ablation. Left: Mean attention mass on the positions predicted by each construction: L1 routing cells at offsets U=DK∪DQ , and L2 matching precedents, earlier cells whose parent configurations match the query configuration. Right: Cell accuracy when attention to the L1 routing cells or L2 matching precedents is masked at evaluation time, compared with no ablation and controls that mask attention to the same number of randomly selected cells.
Figure 4: Linear probe results. Higher is better. The configuration probe measures classification accuracy for the k -cell neighborhood; the k-gram predictive probe measures 1−DKL(qkgram∥qprobe)/DKL(qkgram∥Uniform) , which the axis label abbreviates as 1−KLprobe/KLuniform . Solid lines: deterministic; dashed: stochastic. Vertical lines mark the stage at which the theoretical construction places each target.
Figure 5: t-SNE visualization of frozen representations from the Shared-head model on deterministic 1D V=2,k=3 . Top row (colored by neighborhood configuration): configurations cluster after the L1 MLP. Bottom row (colored by output value): outputs cluster after L2 attention.
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
Appendix
Table 2: Dependencies between construction stages.
symbol
meaning
fixed at
Grid and dual group
d
number of spatial dimensions
Def. 1
Wj
side length of axis j
Def. 1
Wmax
largest side length, maxjWj
Lem. E.3
G , ∣G∣
the torus [W1]×⋯×[Wd] ; its volume ∏jWj
Def. 1
z , zj , δ
a cell of G ; its j -th coordinate; an offset
Def. 1
Appendix
Table 3: Symbols, and where each is fixed.
1D, V=2
1D, V=3
2D von Neumann
G
Z16
Z32
Z62
d , Wmax , ∣G∣
1 , 16 , 16
1 , 32 , 32
2 , 6 , 36
V , T
2 , 10
3 , 12
2 , 12
DK , k
{−1,0,1} , 3
{−1,0,1} , 3
von Neumann, 5
u^d , ∣U∣
1 , 4
1 , 4
(0,1) , 8
tokens per step, N
17 , 170
33 , 396
42 , 504
Appendix
Table 4: The three experimental settings as tasks.
token
predicts
spatial target
cell (t,z) , zd<Wd−1
cell (t,z+u^d)
z+u^d
cell (t,z) , zd=Wd−1
a separator
none
separator
first cell of the next row
κ(n)+u^d
Appendix
Table 5: Prediction duties under Definition C.1 . For a non-row-final cell and for a separator, the predicted token is a cell and its spatial location is expressed through the constant successor offset u^d . A row-final cell instead predicts a separator whose identity is fixed by position.
1D, V=2
1D, V=3
2D von Neumann
∣(U−U)∖{0}∣
6
6
21
degree bound on M⋆
7
7
22
subgroup K
4Z16
4Z32
⟨(2,2)⟩
colors M=MK
4
4
12
Appendix
Table 6: The coloring Construction II uses in each experimental setting. Every one is admissible by Lemma F.2 , and every one is far below the degree bound on the same line.
neighborhood
grid
∣U∣
least periodic MK
degree bound
∣G∣
elementary
Z5
4
5
7
5
elementary
Z16
4
4
7
16
elementary
Z32
4
4
7
32
von Neumann
Z52
8
25
23
25
von Neumann
Z62
8
12
23
36
von Neumann
Z72
8
49
23
49
Appendix
Table 7: Colorings across grids, for reference. MK is the least number of colors over periodic admissible colorings and is arithmetic in the side length. The degree bound ∣(U−U)∖{0}∣+1 of Lemma F.3 is evaluated with the differences taken in Zd , so it is the same for every grid and bounds M⋆ from above; the exact torus count can be smaller, as Remark F.4 shows on Z62 . The bound never degenerates and MK sometimes does: on Z52 and Z72 every nontrivial subgroup has a nonzero intersection with U−U , so MK=∣G∣ and every cell takes a color of its own.
1D, V=2
1D, V=3
2D von Neumann
Construction I (Thm. G.9 )
heads, dMLP
4 , 0
4 , 0
8 , 0
dS=2d , dp=dS+3
2 , 5
2 , 5
4 , 7
width of B3=∣U∣V
8
12
16
residual dmodel
20
26
30
block-error threshold 1/(2k+2)
1/8
1/8
1/12
Appendix
Table 8: What the two constructions require on the three experimental settings, at the colorings of Table 6 .
Dataset type
Train
Val-in (no filter)
Val-in (filter)
Val-out / Val
Test
Deterministic
No
No
Yes
Yes
Yes
Stochastic
No
—
—
Yes
Yes
Appendix
Table 9: Trajectory coverage filter by split. “Yes” means the split rejection-samples trajectories until all Vk neighborhood configurations appear in the context; “No” means no filter. Deterministic runs use five splits (train, val-in without filter, val-in with filter, val-out, test); stochastic runs use three (train, validation, test). The main-text deterministic validation refers to val-out; val-in is diagnostic only and not used for checkpoint selection.
Task
Rule type
Grid G
T
T0
Vk configs
1D V=2,k=3
det / stoch
16
10 / 20
4 / 10
8
1D V=3,k=3
det / stoch
32 / 16
12 / 20
8 / 10
27
2D VN V=2,k=5
det / stoch
6×6
12 / 20
8 / 10
32
Appendix
Table 10: Grid and trajectory parameters for each experimental setting. Values shown as “det / stoch” when they differ between the two regimes.
Task
Arch.
heads
mlp
dmodel
dffn
lr
wd
β2
ep
1D V=2,k=3
Shared-head
[1,1]
[1,1]
128
512
10−3
0.2
0.999
3
Dedicated-heads
[4,1]
[0,1]
128
512
10−3
0.2
0.999
3
1D V=3,k=3
Shared-head
[1,1]
[1,1]
256
1024
5×10−4
0.2
0.95
8
Dedicated-heads
[4,1]
[0,1]
256
1024
5×10−4
0.2
0.95
8
2D VN V=2,k=5
Shared-head
[1,1]
[1,1]
1024
4096
10−3
0.2
0.95
5
Dedicated-heads
[8,1]
[0,1]
1024
4096
10−3
0.2
0.95
5
Appendix
Table 11: Architecture and training hyperparameters: deterministic settings.
Task
Arch.
heads
mlp
dmodel
dffn
lr
wd
β2
ep
1D V=2,k=3
Shared-head
[1,1]
[1,1]
256
1024
10−3
0.2
0.999
15
Dedicated-heads
[4,1]
[0,1]
128
512
10−3
0.2
0.999
8
1D V=3,k=3
Shared-head
[1,1]
[1,1]
256
1024
5×10−4
0.2
0.95
8
Dedicated-heads
[4,1]
[0,1]
256
1024
5×10−4
0.2
0.95
8
2D VN V=2,k=5
Shared-head
[1,1]
[1,1]
1024
4096
5×10−4
0.2
0.95
10
Dedicated-heads
[8,1]
[0,1]
512
2048
10−3
0.2
0.95
5
Appendix
Table 12: Architecture and training hyperparameters: stochastic settings.
Task
Arch.
TF seq acc
TF cell acc
AR cell acc
1D V=2,k=3
Shared-head
0.9990
1.0000
0.9999
Dedicated-heads
0.9994
1.0000
1.0000
1D V=3,k=3
Shared-head
0.9994
1.0000
1.0000
Dedicated-heads
0.9989
1.0000
1.0000
2D VN V=2,k=5
Shared-head
1.0000
1.0000
1.0000
Dedicated-heads
0.9989
1.0000
0.9999
Appendix
Table 13: Deterministic evaluation: full metrics on held-out test rules.
TF KL
TF cell acc
AR
Task
Arch.
kg ∥ m
true ∥ m
(kgram)
cell acc (kgram)
KL kg ∥ m
1D V=2,k=3
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 V=3,k=3
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 V=2,k=5
Shared-head
0.0014
0.0300
0.7345 (0.7351)
0.5201 (0.5197)
0.0013
Appendix
Table 14: Stochastic evaluation: full metrics on held-out test trajectories. TF KL columns report teacher-forced divergence against the k -gram and true-rule baselines.
Figure 6: t-SNE of frozen residual-stream representations from the Dedicated-heads model on deterministic 1D V=2,k=3 . Top row: colored by neighborhood configuration. Bottom row: colored by the ground-truth output value. Under the Dedicated-heads construction, the configuration should become accessible after L1 attention rather than after the L1 MLP.
Figure 7: t-SNE of frozen representations from the Shared-head model on stochastic 1D V=2,k=3 . Top row: colored by neighborhood configuration. Bottom row: colored by the model’s predicted P(y=1) at the corresponding query position. Because rules are drawn from a continuous Dirichlet prior, the output value at a fixed configuration is a sample from the true conditional distribution, so the model’s predictive distribution varies continuously across queries with the same configuration.
Figure 8: t-SNE of frozen representations from the Shared-head model on deterministic 2D von Neumann V=2,k=5 . Top row: colored by neighborhood configuration. Bottom row: colored by the ground-truth output value. The larger neighborhood ( k=5 , Vk=32 configurations) is harder to display in two dimensions than the ECA case; per-configuration clusters are correspondingly less separated.
Deterministic (Rule 110)
Stochastic (Stavskaya)
Task
1D V=2,k=3
1D V=2,k=3
Training ∣G∣,T,T0
64,64,12
64,64,12
Plotted ∣G∣,T,T0
64,64,12
64,64,12
heads / mlp
[4,1]/[0,1]
[1,1]/[1,1]
dmodel / dffn
512/2048
512/2048
lr, wd, epochs
5×10−4 , 0.2, 10
10−3 , 0.2, 8
Appendix
Table 15: Configuration of the visualization checkpoints used for Figure 1 . The deterministic panel uses greedy decoding; the stochastic panel uses temperature-1 sampling, and the displayed values are per-cell probabilities of state 1.