[Paper Review] Learning Transformer Programs
This paper trains Transformers constrained to be mechanistically interpretable by design, then converts them into human-readable programs (Python/RASP-inspired) without sacrificing much performance. It demonstrates results on in-context learning, algorithmic tasks, and NLP, and provides interpretable circuits via code-level debugging.
Recent research in mechanistic interpretability has attempted to reverse-engineer Transformer models by carefully inspecting network weights and activations. However, these approaches require considerable manual effort and still fall short of providing complete, faithful descriptions of the underlying algorithms. In this work, we introduce a procedure for training Transformers that are mechanistically interpretable by design. We build on RASP [Weiss et al., 2021], a programming language that can be compiled into Transformer weights. Instead of compiling human-written programs into Transformers, we design a modified Transformer that can be trained using gradient-based optimization and then automatically converted into a discrete, human-readable program. We refer to these models as Transformer Programs. To validate our approach, we learn Transformer Programs for a variety of problems, including an in-context learning task, a suite of algorithmic problems (e.g. sorting, recognizing Dyck languages), and NLP tasks including named entity recognition and text classification. The Transformer Programs can automatically find reasonable solutions, performing on par with standard Transformers of comparable size; and, more importantly, they are easy to interpret. To demonstrate these advantages, we convert Transformers into Python programs and use off-the-shelf code analysis tools to debug model errors and identify the "circuits" used to solve different sub-problems. We hope that Transformer Programs open a new path toward the goal of intrinsically interpretable machine learning.
Motivation & Objective
- Motivate the need for intrinsically interpretable Transformer models for auditing and debugging in high-stakes tasks.
- Propose a framework to train Transformers under constraints that guarantee a deterministic mapping to human-readable programs.
- Showcase that Transformer Programs can solve various algorithmic and NLP tasks with competitive performance.
- Enable automated extraction of executable Python/RASP-like programs from trained models for circuit-level debugging.
Proposed method
- Introduce a disentangled residual stream constraint so each module reads a fixed set of variables and writes to a dedicated orthogonal subspace.
- Define and train using discrete, interpretable modules (categorical attention heads) with hard attention, relaxed via Gumbel-Softmax during optimization.
- Map each attention head to a RASP-like predicate-aggregate primitive; learn distributions over discrete weights (πK, πQ, πV, Wpredicate) and sample with Gumbel reparameterization.
- After training, deterministically extract a Python program by maxing discrete weights and converting attention heads into predicate functions with a select_closest primitive.
- Extend the framework to include word embeddings, numerical attention, and feed-forward/lookup-like layers to broaden the program repertoire.
- Provide extensions and details on training and mapping to interpretable programs, including example code and debugging workflows.
Experimental results
Research questions
- RQ1Can Transformer models be trained under constraints that guarantee a deterministic mapping to interpretable programs?
- RQ2To what extent can such Transformer Programs solve in-context learning, RASP-style algorithmic tasks, and NLP benchmarks while remaining interpretable?
- RQ3What is the qualitative structure of the learned programs and circuits when converted to readable Python/RASP-like code?
- RQ4How do Transformer Programs compare to standard Transformers in accuracy and interpretability across tasks of varying difficulty?
Key findings
- Transformer Programs achieve reasonable performance close to standard Transformers of similar size on multiple tasks.
- On RASP-style tasks, they exceed 99% accuracy on several tasks (with some exceptions at longer inputs).
- In an in-context learning toy task, the model learns to compose heads to reproduce induction-head behavior and achieves perfect test accuracy.
- On CoNLL-2003 NER, Transformer Programs reach F1 similar to standard Transformers and surpass a unigram baseline.
- The extracted Python/RASP-like programs expose interpretable circuits and feature weights, aiding debugging and circuit analysis.
- There is a trade-off: standard Transformers generally outperform Transformer Programs on longer sequences or larger vocabularies, highlighting scaling challenges.
Read this paper
Open the full text in the Nubint reader and ask about any passage as you read.
Read this paper on NubintThis review was generated by AI from the original paper. Please check the paper before citing it.