Abstract
This study investigates how Transformers master sequence modeling by analyzing their performance on Hidden Markov Models (HMMs). We reveal a clear layerwise hierarchy: lower layers focus on local token relationships, while upper layers "decouple" these features into time-disentangled representations to perform task-level reasoning. Our findings suggest that Transformers function as internal optimizers, identifying hidden states before task identities.
Introduction
Despite the massive success of Transformer-based models in in-context learning (ICL) and multi-tasking, the internal mechanics of how they represent and generalize across different tasks are often treated as a "black box." This research aims to bridge the gap between empirical observations and theoretical understanding by using HMMs as a fundamental testbed. HMMs provide a classic framework where visible observations depend on unobserved hidden states, allowing for a rigorous study of how Transformers learn to predict sequential outputs.
Methodology
The researchers employed a dual approach combining empirical probing with theoretical construction.
Empirical Probing
A 16-layer Transformer was trained on a synthetic dataset mimicking a large-scale pre-training corpus, consisting of 8,192 distinct HMMs. Each HMM represented a specific "task" with 128 hidden states and a 16-token vocabulary. The model was evaluated on its ability to perform in-context learning—predicting the next token of a new HMM sequence given demonstrative examples.
Theoretical Construction
The authors provided a formal proof showing that a Transformer can approximate a low-rank HMM. They constructed an architecture where lower layers extract local history features, middle layers decouple these features from specific time steps, and upper layers implement an internal version of gradient descent to identify the HMM task and make predictions.
Layerwise Hierarchy and Probing
Probing results reveal a distinct "local-to-global" shift in how information is processed. Lower layers are essentially "short-sighted," focusing on immediate neighbors to build local representations. As information ascends, the model begins to recognize broader task identities.
As shown in the probing results, the Transformer identifies the hidden state of the HMM in earlier layers than it identifies the task ID. This suggests a hierarchy where the model first determines the immediate context ("what is happening now") before identifying the overarching system ("what kind of system am I in"). Additionally, the accuracy of recognizing previous tokens follows a "rising-then-falling" trend; accuracy for nearby tokens peaks in lower layers, while distant token recognition peaks later before falling off as features become decoupled.
Feature Decoupling and Attention
A striking finding is the development of "decoupled" features in the upper layers. In these stages, the attention mechanism becomes less dependent on the exact position of a token and more focused on the abstract information the token carries for the current task.
This "time disentanglement" supports the hypothesis that Transformers act as internal optimizers. The theoretical analysis reinforces this, showing that multi-head attention can be mathematically configured to perform gradient descent steps internally to solve the linear regression problem required for HMM prediction.
Conclusion
While HMMs are simpler than the non-Markovian structures of natural language, this study provides a vital framework for understanding Transformer behavior. The transition from local feature extraction to global task reasoning through feature decoupling offers a roadmap for future research into more complex sequence models. Future work will explore "weakly revealing" cases where the hidden state space is significantly larger than the observation space, a common challenge in real-world natural language processing.
Summary updated