Learning Transformer Programs

Dan Friedman, Alexander Wettig, Danqi Chen

TL;DR

Instead of trying to "reverse-engineer" black-box AI, this paper introduces Transformer Programs: a method to train Transformers that are interpretable by design. By constraining the architecture to map directly to a programming language (RASP), the authors can automatically decompile a trained model into human-readable Python code that can be inspected, audited, and even stepped through with a standard debugger.

The Interpretability Problem

Transformers are the "black boxes" powering modern AI, from large language models like ChatGPT to specialized scientific tools. While they are incredibly powerful, we don't truly understand how they arrive at specific outputs. The field of mechanistic interpretability tries to solve this by manually inspecting weights and activations to find "circuits"—the internal logic of the network.

However, this manual reverse-engineering is labor-intensive and often results in incomplete or unfaithful descriptions of the model's logic. The authors argue that instead of trying to explain a messy, continuous model after the fact, we should build models that are forced to learn discrete, symbolic algorithms from the start.

Methods: Bridging Neural Networks and Code

The authors bridge the gap between neural networks and symbolic code using three key innovations:

  1. Disentangled Residual Stream: In a standard Transformer, information is "smeared" across the hidden state. Here, the model is forced to use a "disentangled" stream where specific variables (like token value or position) live in dedicated, orthogonal subspaces.
  2. RASP-Inspired Constraints: They build on RASP (Restricted Access Sequence Processing), a language designed to mirror Transformer operations. They restrict attention heads to perform discrete "select" and "aggregate" operations and turn feed-forward layers into discrete lookup tables (MLPs).
  3. Differentiable "Decompilation": To make this trainable, they use the Gumbel-Softmax trick. This allows the model to remain continuous and "trainable" via standard gradient descent, but gradually "harden" into discrete choices that can be mapped directly to Python if/else statements and loops.
The Transformer Program pipeline: from task data to a relaxed continuous model, sampled into a discrete model, and finally decompiled into readable Python code.

Evaluation and Datasets

The researchers evaluated Transformer Programs across three distinct categories:

  • Algorithmic Tasks (RASP suite): Classic computer science problems like sorting a list, reversing a string, and recognizing Dyck languages (balanced parentheses).
  • In-Context Learning: A toy task designed to see if the model can learn to associate letters with numbers based on examples provided in the prompt.
  • Real-world NLP: Standard benchmarks including CoNLL-2003 for Named Entity Recognition (NER) and several text classification datasets (TREC, MR, Subj, AG News).

Key Insights and Results

  • Neural Networks as Code: The most striking insight is that a trained Transformer Program is a program. You can literally open the model in an IDE, set a breakpoint, and see exactly which token the model is "attending" to and why.
  • Debugging the "Black Box": The authors demonstrate using a debugger to find why a model failed. For a sorting task, they could trace the error to a specific "brittle heuristic" the model learned for certain sequence positions, rather than a general sorting algorithm.
  • Induction Heads Emerge Naturally: The model automatically learns "induction heads"—a known pattern in LLMs where the model looks at previous occurrences of a token to predict what comes next. In Transformer Programs, this logic is explicitly written out in the decompiled Python code.
  • Competitive Performance: Surprisingly, for many tasks, you don't have to sacrifice much accuracy for this level of transparency.
  • The models achieved >99% accuracy on 5 out of 7 RASP algorithmic tasks.
  • On the CoNLL-2003 NER task, the Transformer Program achieved an F1 score of 76.1, beating a standard Transformer (70.5) and a unigram baseline (66.2).
  • On text classification datasets like AG News and TREC, the programs performed within 1-3% of standard black-box Transformers.

Limitations and Future Work

Despite the promise, the authors note several hurdles:

  • Optimization Challenges: Training discrete structures is hard. The models sometimes get stuck in "local minima," learning a messy heuristic instead of the elegant algorithm the researchers knew was possible.
  • Scaling: As the vocabulary size and sequence length increase, the "lookup table" approach for MLPs and the memory requirements for the disentangled stream become much more expensive.
  • Expressivity: By forcing the model to be "human-readable," you inherently limit its "creativity." Standard Transformers can use complex, high-dimensional interactions that are difficult to capture in simple Python if statements.

The authors suggest that future work should focus on more sophisticated discrete optimization techniques and expanding the library of "interpretable modules" to handle even more complex tasks.

Summary updated

BibTeX
@inproceedings{friedman2023learning,
  author = {Dan Friedman and Alexander Wettig and Danqi Chen},
  title = {{Learning Transformer Programs}},
  booktitle = {Advances in Neural Information Processing Systems},
  year = {2023},
  url = {https://openreview.net/forum?id=Pe9WxkN8Ff},
}
Figure