Skip to main content
QUICK REVIEW

[Paper Review] Thinking Like Transformers

Gail Garfinkel Weiss, Yoav Goldberg|arXiv (Cornell University)|Jun 13, 2021
Advanced Neural Network ApplicationsComputer Science11 citations
TL;DR

This paper introduces RASP (Restricted Access Sequence Processing Language), a symbolic programming language that models the computational behavior of Transformer encoders by abstracting attention and feed-forward operations into first-class primitives. It enables formal reasoning about Transformer capabilities, showing that RASP programs can predict the minimum number of layers and heads needed to solve tasks like sorting, histogram computation, and Dyck-language recognition, with trained Transformers achieving over 99% accuracy when matching RASP-derived attention patterns.

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.

Motivation & Objective

  • To provide a formal computational model for Transformer encoders, analogous to finite automata for RNNs.
  • To enable higher-level reasoning about Transformer capabilities by expressing tasks as symbolic programs in a domain-specific language.
  • To predict the minimum number of layers and attention heads required to implement specific sequence processing tasks in a Transformer.
  • To validate that trained Transformers can learn attention patterns matching RASP-derived solutions, linking symbolic reasoning to neural network behavior.
  • To explain empirical phenomena in Transformer variants and limitations of efficient architectures using the RASP abstraction.

Proposed method

  • Design RASP as a minimal programming language with primitives for selection, filtering, and aggregation over sequences, capturing attention and feed-forward constraints.
  • Compile RASP programs into equivalent attention and MLP operations that define the required attention patterns and layer structure in a Transformer.
  • Train Transformers with dual supervision: standard cross-entropy loss on output and MSE loss on attention patterns to match the RASP solution.
  • Use RASP programs to derive theoretical lower bounds on the number of layers and heads needed for a given task.
  • Analyze the feasibility of RASP programs by training Transformers at predicted minimal sizes and measuring accuracy drops when reducing heads or layers.
  • Apply RASP to re-express and improve prior constructions for Dyck-k language recognition, proving full recognition capability for all k.

Experimental results

Research questions

  • RQ1Can a symbolic programming language be constructed to model the computational behavior of Transformer encoders in a way analogous to finite automata for RNNs?
  • RQ2What is the minimal number of layers and attention heads required to implement a given sequence processing task in a Transformer, as predicted by a symbolic program?
  • RQ3Can trained Transformers learn attention patterns that exactly match those derived from a RASP program for a given task?
  • RQ4How do reductions in model size (layers or heads) affect performance, and can RASP predict these failure points?
  • RQ5Can RASP be used to explain or predict empirical behaviors in Transformer variants, such as performance drops in efficient architectures?

Key findings

  • RASP programs for double-histogram, sorting, and most-frequent token tasks were successfully compiled into attention patterns that trained Transformers replicated with over 99% test accuracy.
  • Transformers trained with both output and attention supervision achieved 99.9% accuracy on double-histogram and 99.8% on sorting, closely matching the RASP-specified attention patterns.
  • When model size was reduced below the RASP-predicted minimum, accuracy dropped significantly—e.g., 41.2% on reverse with one fewer layer—indicating RASP's predictive power for architectural limits.
  • The RASP abstraction enabled a more concise and correct construction for Dyck-k language recognition than prior work, proving that Transformers can fully recognize Dyck-k for all k.
  • For sorting, a single-layer Transformer with one head achieved near-perfect accuracy due to uniform attention patterns, suggesting it effectively implemented a form of bucket sort.
  • The study revealed that 'efficient' Transformers with restricted attention mechanisms may fail on tasks requiring complex information flow, as predicted by RASP analysis.

Read this paper

Open the full text in the Nubint reader and ask about any passage as you read.

Read this paper on Nubint

This review was generated by AI from the original paper. Please check the paper before citing it.