On Limitation of Transformer for Learning HMMs

Jiachen Hu, Qinghua Liu, Chi Jin · Submission to ICLR 2025 · Rejected

TL;DR

While Transformers dominate modern AI, this paper reveals that they struggle significantly compared to simple Recurrent Neural Networks (RNNs) when learning Hidden Markov Models (HMMs). Across both theory and synthetic experiments, the authors show that Transformers require depth logarithmic in sequence length ($\lceil \log_2 T \rceil$) to track state transitions, fail entirely on non-mixing HMMs lacking intermediate supervision, and can be partly rescued using a compute-efficient "block Chain-of-Thought" recurrence mechanism.

What Problem Does This Paper Address?

HMMs are foundational models for sequential data with latent structure (underpinning speech, language tagging, time series, and Partially Observable Markov Decision Processes in RL). While Transformers excel at parallel sequence processing, their computational depth does not scale with sequence length, whereas recurrent architectures naturally step through time recursively.

This paper investigates:

  1. Can standard decoder-only Transformers effectively learn HMMs and their variants?
  2. How do Transformers compare to single-layer RNNs in convergence, data efficiency, and final accuracy?
  3. How many attention layers are theoretically and practically required to model a sequence of length $T$?

Methods

  • Neural Architectures: Evaluated standard decoder-only pre-LN Transformers (depth $L \in \{1, \dots, 7\}$, 8 attention heads, hidden dimension 512) against a basic single-layer ReLU RNN (hidden dimension 64).
  • Tasks:
  • Belief State Inference: Predicting the posterior distribution over hidden states $b_t = \Pr(s_t \mid o_1, \dots, o_t)$, where true belief states serve as intermediate supervision during training.
  • Next-Observation Prediction: Autoregressively predicting $\Pr(o_{t+1} \mid o_1, \dots, o_t)$ without access to latent states.
  • Curriculum Training: Implemented a doubling curriculum schedule starting at sequence length $2^L$ and doubling across $8 - L$ stages to stabilize Transformer training.
  • Block Chain-of-Thought (Block CoT): To bridge the depth limitation without incurring the massive slowdown of token-by-token CoT, the authors introduce a variant that feeds model predictions back into the input every $b$ tokens (with stop-gradient). This cuts the autoregressive overhead by a factor of roughly $1/b$.
  • Theoretical Expressiveness: Proved constructive upper bounds on finite-precision Transformers showing that an $L$-layer Transformer can express HMM belief updates up to sequence length $2^L$ via divide-and-conquer matrix multiplications.

Datasets

The authors design synthetic benchmarks representing distinct dynamics and mixing times ($T_{\text{mix}}$):

  • RanHMM & RanLDS: Random discrete HMM and linear dynamical systems. Both are fast-mixing ($T_{\text{mix}} \approx 1.6$ and $3.12$).
  • Cyclic-DET: Deterministic cyclic MDPs converted into aperiodic, non-mixing HMMs.
  • Cyclic-RND: Stochastic cyclic HMMs with parameterized backward transition probability $\epsilon$, allowing controlled mixing times from 14.2 to 120.
  • Cyclic-HARD: A multi-stage non-mixing HMM designed for next-observation prediction where the network must retain state memory over arbitrary intervals without intermediate belief supervision.
  • MatMul: Sequential product of random orthogonal matrices, serving as an algebraic generalization.

Each dataset consists of $5 \times 10^6$ rollouts of length $T = 120$.

Key Insights and Ideas

  • Inductive Bias Dominates: A single-layer RNN outperformed Transformers of all depths across every task. The recurrent inductive bias directly mirrors the Markov property ($b_t \propto O_t P b_{t-1}$), enabling RNNs to converge orders of magnitude faster.
  • Three Regimes of Transformer Depth:
  • Constant depth: Fast-mixing models (RanHMM, RanLDS) can be solved with shallow Transformers ($L=2$), because predictions depend only on the immediate past lookback window.
  • Logarithmic scaling ($\sim \log_2 T$): For structured/non-mixing models (Cyclic-DET, MatMul), the minimum layer depth required scales logarithmically with sequence length ($2^L$), matching the theoretical divide-and-conquer matrix product tree.
  • Hard failure: When intermediate supervision is removed and mixing time is long (Cyclic-HARD), Transformers fail to fit even constant sequence lengths, whereas RNNs succeed effortlessly.
  • Intermediate Supervision is Critical: Transformers struggle primarily with credit assignment across long latent trajectories. When belief supervision is absent and mixing times are long, attention mechanisms fail to learn the implicit recurrence.

Key Results

  • Optimization & Accuracy: On Cyclic-DET, RNN reached a training loss of $2.27 \times 10^{-7}$, whereas 5-layer and 6-layer Transformers plateaued around $0.67$–$0.77$. RNN achieved a $0.05$-fit length of $T=120$ across all tested tasks.
  • Depth Scaling: For Cyclic-DET and MatMul, empirical fit lengths doubled roughly with each additional layer ($2^L$), validating the theoretical bound that $L = \lceil \log_2 T \rceil$ layers are needed.
  • Block CoT: Setting $b = 8$ or $12$ enabled a 3-layer Transformer on Cyclic-DET to achieve low evaluation loss at length 60 (where it previously failed completely), while running $\approx 8\times$–$12\times$ faster per epoch than standard single-step CoT (390s vs. 4838s).

Limitations and Future Work

  • Dependence on Observability: Block CoT assumes that output predictions carry sufficient information about the hidden state. In models with poor observability (like Cyclic-HARD), block CoT cannot recover the missing latent state.
  • Optimization Fragilities: Deeper Transformers suffered from severe training instability; for example, the 7-layer Transformer failed to converge on MatMul even after 200 epochs and extensive curriculum tuning.
  • Scope: The study evaluates synthetic benchmarks with small discrete state spaces ($n=5$); extending these findings to complex POMDPs with control inputs and real-world high-dimensional time series remains open.

Summary updated

BibTeX
@misc{hu2024limitation,
  author = {Jiachen Hu and Qinghua Liu and Chi Jin},
  title = {{On Limitation of Transformer for Learning HMMs}},
  year = {2024},
  eprint = {2406.04089},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2406.04089},
}

Loading related content...

Figure