Abstract
The emergence of seemingly similar representations across tasks and neural architectures suggests that convergent properties may underlie sophisticated behavior. One form of representation that seems particularly fundamental to reasoning in many artificial (and perhaps natural) networks is the formation of world models, which decompose observed task structures into re-usable perceptual primitives and task-relevant relations. In this work, we show that auto-regressive transformers tasked with solving mazes learn to linearly represent the structure of mazes, and that the formation of these representations coincides with a sharp increase in generalization performance. Furthermore, we find preliminary evidence for Adjacency Heads which may play a role in computing valid paths through mazes.
Introduction
How does a Transformer "think"? While these models are ubiquitous in modern AI, their internal logic often remains opaque. Mechanistic interpretability seeks to reverse-engineer these networks to understand if they develop a true "understanding" of a task's underlying structure—a world model—or if they are merely high-powered statistical predictors. To investigate this, researchers trained small decoder-only Transformers to solve mazes, discovering that these models spontaneously build sophisticated, linear representations of the maze's geometry.
Experimental Setup and Datasets
The study utilized the maze-dataset library to generate thousands of $n \times n$ lattice mazes. These mazes were serialized into text strings containing an adjacency list of connections, origin and target coordinates, and the solution path.
Two primary models were analyzed: hallway (1.2M parameters, simple paths) and jirpy (9.6M parameters, complex mazes). The models were tested on various maze types, including forkless corridors and complex structures with cycles (pRDFS).
Linear World Models
A key finding is the emergence of a "single-token map." By the second layer of the Transformer, a linear probe can reconstruct the entire 2D maze layout from the activation vector of the <PATH_START> token with over 90% accuracy. This suggests the model compresses the global environment into a single high-dimensional vector early in its processing.
Furthermore, the model organizes coordinate tokens in its embedding space based on Manhattan distance. Even though coordinates are provided as arbitrary text, the model learns to place physically adjacent locations closer together in its internal representation.
Mechanistic Insights
The researchers identified specific "Adjacency Heads" (e.g., Layer 5, Head 3) that attend to tokens exactly one step away from the current position. These heads appear to verify the maze's topology to ensure valid pathfinding.
Using the "Tuned Lens" technique—which maps intermediate hidden states back to the model's vocabulary—the team observed that the model's internal "guesses" for the next token refine as information passes through layers. By the middle layers, the model narrows its focus from any adjacent coordinate to only those validly connected in the specific maze instance.
Grokking and Performance
The models exhibited a "grokking" phenomenon: a sudden, sharp increase in generalization performance. Critically, this jump coincided with the point at which internal representations became linearly decodable. This suggests that the formation of a structured, linear world model is a prerequisite for the model to successfully generalize to unseen maze configurations. The jirpy model eventually achieved an 82.4% exactly correct rollout rate on complex mazes.
Conclusion
This work provides strong evidence that Transformers can spontaneously develop structured internal representations of their environment. While the causal link between these representations and final output requires further study, the correlation between linear decodability and generalization performance is a significant step toward understanding how these models solve spatial reasoning tasks. Future research will focus on mapping the complete circuitry of information flow between attention heads.
Summary updated