TL;DR
The authors introduce a novel framework for mechanistic interpretability by reverse-engineering transformer attention heads into executable Python code. Remarkably, up to 25% of these neural attention heads can be hot-swapped with their synthesized symbolic code counterparts during a model's forward pass, incurring only a minor performance penalty while maintaining downstream task accuracy.
What Problem Does This Paper Address?
A primary goal of mechanistic interpretability is to transform the "black box" of deep neural networks into human-readable, symbolic logic. Traditional approaches generally fall into two camps: top-down methods (like training probes to detect human-defined concepts) and bottom-up methods (like generating natural language summaries of inputs that activate specific features).
However, natural language descriptions are often ambiguous and cannot be formally verified. Crucially, you cannot plug a text description back into a neural network to verify its causal accuracy. This paper addresses the need for formal, executable, and causally verifiable explanations of neural computation by targeting attention heads in transformer language models.
Methods
The authors develop a four-step pipeline to translate attention mechanisms into code:
- Attention Map Extraction: The target model processes a corpus of text, and the ground-truth attention matrices are recorded. The pipeline filters for the top 2.5% of attention weights by magnitude to isolate the most salient token-pair interactions.
- Program Synthesis: An auxiliary large language model (Claude 4 Sonnet) acts as a synthesis agent. Prompted with text-based summaries of the extracted attention patterns, it generates candidate Python functions (utilizing libraries like NumPy, spaCy, and NLTK) designed to predict the attention matrix using only the raw input text.
- Evaluation and Refinement: Candidate programs are scored against real attention maps using Jensen-Shannon Distance (JSD). The top-performing candidate undergoes an automated round of feedback-conditioned refinement based on its best and worst-scoring examples.
- Causal Head Replacement: To validate the causal faithfulness of the code, the authors perform interchange interventions. They literally replace the neural attention matrix with the synthesized program's output during the model's forward pass to measure its impact on performance.
Datasets
- TinyStories: A synthetic, highly structured dataset used to extract attention maps and evaluate attention alignment. Its relative simplicity helps isolate specific head behaviors without the stochastic noise of complex corpora.
- Downstream Benchmarks: To evaluate out-of-distribution generalization after replacing neural heads with code, the authors use a suite of six multiple-choice reasoning benchmarks: HellaSwag (commonsense inference), PIQA (physical reasoning), SciQ (science understanding), ARC-Easy (elementary science), Social IQA (social interaction), and COPA (causal reasoning).
Key Insights and Ideas
- Code as a Causal Proxy: Executable programs occupy a unique middle ground between billion-parameter weights and natural language. Because they can be directly substituted back into the model, they allow for rigorous causal validation of inferred explanations.
- Scale Breeds Symbolic Tractability: Counterintuitively, program alignment scores improved as the target model scaled up (Llama-3B outperformed GPT-2). The authors hypothesize that larger models exhibit greater "functional specialization," meaning individual heads serve narrower, cleaner, and more symbolically tractable roles.
- Autoregressive vs. Bidirectional Constraints: Causal (autoregressive) decoder models are significantly easier to fit with symbolic code than bidirectional encoder models like BERT. BERT's masked language modeling objective yields highly complex, bidirectional attention distributions that resist simple algorithmic descriptions.
Key Results
- High Alignment Accuracy: The synthesized programs achieved high mean Intersection-over-Union (IoU) scores when compared to true attention patterns: 69% for GPT-2-Small, 74% for TinyLlama-1.1B, and 79% for Llama-3B.
- Low Substitution Cost: Across the decoder models, replacing 25% of the neural attention heads with programmatic surrogates caused only a modest 16% average increase in perplexity.
- Downstream Resilience: Models maintained robust performance on multiple-choice reasoning tasks even when up to 30–40% of their attention heads were replaced by synthesized Python code, remaining non-trivially better than random guessing.
Limitations and Future Work
- The Coverage Gap: A substantial fraction of attention heads still scored below 40% IoU, indicating that the current program library lacks the diversity or complexity to fully characterize them.
- Pruning Mimicry: The authors note that the minimal performance degradation observed at low replacement levels might partially resemble a pruning effect rather than a perfect functional replication of the head.
- Future Directions: To close the coverage gap, future work will focus on richer synthesis strategies, such as multi-round refinement with stronger feedback signals, and expanding the framework to better capture the bidirectional logic of encoder models like BERT.
Summary updated