Lune

ICML2021Top-tier venue

Thinking Like Transformers

Gail Weiss, Yoav Goldberg, Eran Yahav

Abstract

What is the computational model behind a Transformer? Where recurrent neural networks have direct parallels in finite state machines, allowing clear discussion and thought around architecture variants or trained models, Transformers have no such familiar parallel. In this paper we aim to change that, proposing a computational model for the transformer-encoder in the form of a programming language. We map the basic components of a transformer-encoder -- attention and feed-forward computation -- into simple primitives, around which we form a programming language: the Restricted Access Sequence Processing Language (RASP). We show how RASP can be used to program solutions to tasks that could conceivably be learned by a Transformer, and how a Transformer can be trained to mimic a RASP solution. In particular, we provide RASP programs for histograms, sorting, and Dyck-languages. We further use our model to relate their difficulty in terms of the number of required layers and attention heads: analyzing a RASP program implies a maximum number of heads and layers necessary to encode a task in a transformer. Finally, we see how insights gained from our abstraction might be used to explain phenomena seen in recent works.

Ask Lune

Know this paper in minutes, not an afternoon.

Lune can read every page of it and the top-tier papers that cite it, and gives you its claims, its evidence and its limits, each one cited to the paper it comes from. No skimming, no AI slop.

105 top-tier papers have cited it since. Know what they found before your reviewers do.

Questions to start from

Lune can draw on

the full paper · the top-tier papers that cite it · related work

Get the answer

Free to start. No credit card required.

Lineage

  • Big Bird: Transformers for Longer Sequences cites Are Transformers universal approximators of sequence-to-sequence functions?
  • Transformers as Statisticians: Provable In-Context Learning with In-Context Algorithm Selection cites On the Ability and Limitations of Transformers to Recognize Formal Languages
  • Transformers as Statisticians: Provable In-Context Learning with In-Context Algorithm Selection cites Are Transformers universal approximators of sequence-to-sequence functions?
  • Transformers as Statisticians: Provable In-Context Learning with In-Context Algorithm Selection cites What Can Transformers Learn In-Context? A Case Study of Simple Function Classes
  • Chain of Thought Empowers Transformers to Solve Inherently Serial Problems cites Towards Revealing the Mystery behind Chain of Thought: A Theoretical Perspective
  • On the Ability and Limitations of Transformers to Recognize Formal Languages cites Are Transformers universal approximators of sequence-to-sequence functions?
  • Towards Revealing the Mystery behind Chain of Thought: A Theoretical Perspective cites Transformers as Statisticians: Provable In-Context Learning with In-Context Algorithm Selection
  • Towards Revealing the Mystery behind Chain of Thought: A Theoretical Perspective cites On the Ability and Limitations of Transformers to Recognize Formal Languages
  • Towards Revealing the Mystery behind Chain of Thought: A Theoretical Perspective cites Are Transformers universal approximators of sequence-to-sequence functions?
  • Towards Revealing the Mystery behind Chain of Thought: A Theoretical Perspective cites What Can Transformers Learn In-Context? A Case Study of Simple Function Classes
  • What Can Transformers Learn In-Context? A Case Study of Simple Function Classes cites On the Ability and Limitations of Transformers to Recognize Formal Languages
  • What Can Transformers Learn In-Context? A Case Study of Simple Function Classes cites Are Transformers universal approximators of sequence-to-sequence functions?
Builds onCited byLeft to right by year; the more cited, the higher.

Cited by top-tier papers105

  1. What Can Transformers Learn In-Context? A Case Study of Simple Function ClassesShivam Garg, Dimitris Tsipras, Percy Liang, Gregory ValiantNeurIPS 2022 · 883 citations
  2. Towards Revealing the Mystery behind Chain of Thought: A Theoretical PerspectiveGuhao Feng, Bohang Zhang, Yuntian Gu, Haotian Ye et al.NeurIPS 2023 · 470 citations
  3. The Impact of Positional Encoding on Length Generalization in TransformersAmirhossein Kazemnejad, Inkit Padhi, Karthikeyan Natesan Ramamurthy, Payel Das et al.NeurIPS 2023 · 444 citations
  4. Transformers as Statisticians: Provable In-Context Learning with In-Context Algorithm SelectionYu Bai, Fan Chen, Huan Wang, Caiming Xiong et al.NeurIPS 2023 · 356 citations
  5. Chain of Thought Empowers Transformers to Solve Inherently Serial ProblemsZhiyuan Liu, Hong Liu, Denny Zhou, Tengyu MaICLR 2024 · 259 citations

Builds on8

  1. Big Bird: Transformers for Longer SequencesManzil Zaheer, Guru Guruganesh, Kumar Avinava Dubey, Joshua Ainslie et al.NeurIPS 2020 · 3,159 citations
  2. Are Transformers universal approximators of sequence-to-sequence functions?Chulhee Yun, Srinadh Bhojanapalli, Ankit Singh Rawat, Sashank J. Reddi et al.ICLR 2020 · 481 citations
  3. How Can Self-Attention Networks Recognize Dyck-n Languages?Javid Ebrahimi, Dhruv Gelda, Wei ZhangEMNLP Findings 2020 · 13 citations
  4. On the Ability and Limitations of Transformers to Recognize Formal LanguagesSatwik Bhattamishra, Kabir Ahuja, Navin GoyalEMNLP 2020 · 7 citations
  5. Improving Transformer Models by Reordering their SublayersOfir Press, Noah A. Smith, Omer LevyACL 2020 · 6 citations

Related papers

  1. What Algorithms can Transformers Learn? A Study in Length GeneralizationHattie Zhou, Arwen Bradley, Etai Littwin, Noam Razin et al.ICLR 2024 · 189 citations
  2. Discovering Interpretable Algorithms by Decompiling Transformers to RASPXinting Huang, Aleksandra Bakalova, Satwik Bhattamishra, William Merrill et al.ICML 2026 · 3 citations
  3. Learning Transformer ProgramsDan Friedman, Alexander Wettig, Danqi ChenNeurIPS 2023 · 59 citations
  4. Length Generalization Bounds for TransformersAndy Yang, Pascal Bergsträßer, Georg Zetzsche, David Chiang et al.ICML 2026
  5. Adaptive Transformer Programs: Bridging the Gap Between Performance and Interpretability in TransformersQuoc-Vinh Lai-Dang, Taemin Kang, Seungah SonICLR 2025
Dusk over the sea between two cliffs drawn in fine vertical lines