Introduction
Large Language Models (LLMs) exhibit a remarkable ability for in-context learning (ICL), yet the internal mechanisms driving this capability remain partially obscured. While previous research has explored how Transformers learn Fixed-order Markov Chains (FOMC)—where the next token depends on a static context length—natural language is more accurately characterized by Variable-order Markov Chains (VOMC), or Context Trees. In a VOMC, the relevant "memory" length fluctuates based on the specific sequence of preceding tokens. This paper investigates how Transformers adapt to these complex structures, effectively mimicking optimal Bayesian compression algorithms.
Understanding Variable-order Markov Chains
Learning VOMCs in-context is significantly more challenging than fixed-order tasks. The model must dynamically identify the relevant suffix length while simultaneously tracking token occurrences to estimate transition probabilities.
As shown in the figure above, the probability distribution for the next token $x_{n+1}$ is determined by suffixes of the history. For instance, after a specific sequence, a short suffix might suffice, whereas other contexts require a deeper look-back.
Methods and Architecture
The researchers framed language modeling as a data compression task, using average log-loss to compare Transformers against gold-standard algorithms like Context-Tree Weighting (CTW)—a Bayesian optimal approach—and Prediction by Partial Matching (PPM).
The study utilized shallow Transformers (1–6 layers) trained on synthetic sequences generated from random context trees. By using small alphabets (typically ternary), the focus remained on structural learning rather than vocabulary memorization. The researchers also performed attention map analysis and theoretical constructions to prove that a Transformer with $D+2$ layers (where $D$ is the max tree depth) can mathematically mimic the CTW algorithm.
Key Insights: Suffix Matching and Probability Blending
The investigation revealed that Transformers develop specific internal "circuitry" for VOMC tasks:
- The Two-Layer Sweet Spot: Performance gains largely saturate at two layers. A 2-layer model tracks the optimal CTW algorithm closely, whereas 1-layer models lack the capacity for the necessary "match-then-blend" logic.
- Suffix Matching via Attention: Two distinct attention patterns were identified: "suffix formers," which copy previous tokens to the current position, and "suffix matchers," which locate prior occurrences of the current context.
- Counting and Blending: Feed-Forward Networks (FFNs) likely handle the complex task of "blending" counts from different suffix lengths into a final probability estimate.
Results and Performance
On a task with a maximum order of $D=5$, a 2-layer Transformer achieved a compression rate of 0.7569, remarkably close to the optimal CTW's 0.7400. Notably, Transformers trained on specific tree distributions can actually outperform the "optimal" CTW by learning the underlying meta-prior of the data. Furthermore, Transformers demonstrated superior robustness compared to heuristic algorithms like PPM, which often suffer from oversimplification bias in short-context scenarios.
Limitations and Future Directions
While this work provides a foundational understanding of how Transformers handle variable-order dependencies, several avenues for future research remain. The experiments were limited to small alphabets and a maximum context depth of $D=5$. Real-world language involves much larger vocabularies and deeper dependencies. Additionally, while the authors constructed a Transformer that mimics CTW, further mechanistic interpretability is required to confirm if standard trained models converge on this exact logic.
Summary updated