Finite State Automata Inside Transformers with Chain-of-Thought: A Mechanistic Study on State Tracking

Yifan Zhang, Wenyu Du, Dongming Jin, Jie Fu, Zhi Jin
2/27/2025
cs.CLcs.LG

Official code: IvanChangPKU/FSA (6 stars, last commit 2025-05-31).

Abstract

Chain-of-thought (CoT) significantly enhances the performance of large language models (LLMs) across a wide range of tasks, and prior research shows that CoT can theoretically increase expressiveness. However, there is limited mechanistic understanding of the algorithms that Transformer+CoT can learn. Our key contributions are: (1) We evaluate the state tracking capabilities of Transformer+CoT and its variants, confirming the effectiveness of CoT. (2) Next, we identify the circuit (a subset of model components, responsible for tracking the world state), indicating that late-layer MLP neurons play a key role. We propose two metrics, compression and distinction, and show that the neuron sets for each state achieve nearly 100% accuracy, providing evidence of an implicit finite state automaton (FSA) embedded within the model. (3) Additionally, we explore three challenging settings: skipping intermediate steps, introducing data noises, and testing length generalization. Our results demonstrate that Transformer+CoT learns robust algorithms (FSAs), highlighting its resilience in challenging scenarios. Our code is available at https://github.com/IvanChangPKU/FSA.

View on arXivView PDF

Code Implementations(1)

IvanChangPKU/FSAOfficial100%
60Shell, PythonMay 27, 20251 years ago

Similar Papers

Cite this paper

@article{zhang2025finite,
  title  = {Finite State Automata Inside Transformers with Chain-of-Thought: A Mechanistic Study on State Tracking},
  author = {Yifan Zhang and Wenyu Du and Dongming Jin and Jie Fu and Zhi Jin},
  year   = {2025},
  eprint = {2502.20129},
  archivePrefix = {arXiv},
  url    = {http://arxiv.org/abs/2502.20129v3}
}

Add a badge to your README

Shows whether official code is linked for this paper.

Code of Paper badge
[![Code of Paper](https://codeofpaper.com/badge/2502.20129.svg)](https://codeofpaper.com/paper/2502.20129)

Discussion