Skip to main content
QUICK REVIEW

[Paper Review] Can Transformers Learn to Solve Problems Recursively?

Shizhuo Dylan Zhang, Curt Tigges|arXiv (Cornell University)|May 24, 2023
Adversarial Robustness in Machine LearningComputer Science5 citations
TL;DR

This paper investigates whether small transformer models can learn to emulate structurally recursive functions—key to program synthesis and formal verification—by training them on input-output examples. Using mechanistic interpretability, the authors reconstruct 'shortcut' algorithms the models learn, predicting up to 91% of failure cases, revealing that transformers approximate recursion through flawed, depth-specific heuristics rather than true semantic generalization.

ABSTRACT

Neural networks have in recent years shown promise for helping software engineers write programs and even formally verify them. While semantic information plays a crucial part in these processes, it remains unclear to what degree popular neural architectures like transformers are capable of modeling that information. This paper examines the behavior of neural networks learning algorithms relevant to programs and formal verification proofs through the lens of mechanistic interpretability, focusing in particular on structural recursion. Structural recursion is at the heart of tasks on which symbolic tools currently outperform neural models, like inferring semantic relations between datatypes and emulating program behavior. We evaluate the ability of transformer models to learn to emulate the behavior of structurally recursive functions from input-output examples. Our evaluation includes empirical and conceptual analyses of the limitations and capabilities of transformer models in approximating these functions, as well as reconstructions of the ``shortcut" algorithms the model learns. By reconstructing these algorithms, we are able to correctly predict 91 percent of failure cases for one of the approximated functions. Our work provides a new foundation for understanding the behavior of neural networks that fail to solve the very tasks they are trained for.

Motivation & Objective

  • To investigate whether transformer models can learn to emulate structurally recursive functions, a core class of programs in program synthesis and formal verification.
  • To analyze how and why transformer models fail on tasks they are explicitly trained for, particularly in modeling recursive semantics.
  • To reverse-engineer the 'shortcut' algorithms learned by transformers using mechanistic interpretability techniques.
  • To understand the role of attention mechanisms and model depth in shaping these learned, suboptimal behaviors.
  • To provide a foundation for improving training, prompting, and evaluation of neural models on recursive and semantic reasoning tasks.

Proposed method

  • Training small transformer models from scratch on synthetic tasks involving structural recursion, including binary successor and tree traversal.
  • Using abstract-state machines (ASMs) as a formal framework to model and analyze the computational behavior of the transformers.
  • Applying mechanistic interpretability techniques such as counterfactual patching, circuit tracing, and attention head analysis to reverse-engineer learned algorithms.
  • Tracking attention patterns across layers and heads to identify how models handle recursive substructures, bracket signals, and node copying.
  • Using UNROLL and EMPTY tokens as symbolic markers to trace model behavior during recursive reductions and subtree copying.
  • Reconstructing the 'shortcut' algorithms by analyzing attention dependencies and behavior shifts at critical points like bracket closures.
(a) Decoder self-attention. Reverse Order.
(a) Decoder self-attention. Reverse Order.

Experimental results

Research questions

  • RQ1Can small transformer models learn to emulate structurally recursive functions from input-output examples, despite lacking explicit supervision on recursive structure?
  • RQ2What kinds of 'shortcut' algorithms do transformers learn instead of true recursive generalization, and how do they fail?
  • RQ3How do attention mechanisms in transformers encode and manipulate recursive substructures during inference?
  • RQ4To what extent can mechanistic interpretability techniques predict failure cases in transformer models trained on recursive tasks?
  • RQ5How do model depth and hyperparameter choices influence the emergence of depth-specific heuristics in recursive reasoning?

Key findings

  • The models successfully approximate structurally recursive functions but do so via learned shortcuts rather than true recursive generalization.
  • Up to 91% of failure cases for one function could be correctly predicted by reconstructing the shortcut algorithm the model learned.
  • Attention mechanisms in early layers show task-specific patterns: one head tracks forward symbols like parentheses, while another attends linearly to encoded tokens.
  • For deeper trees, models rely on decoder self-attention to track parent nodes and use UNROLL symbols as signals for recursive composition.
  • The models learn depth-specific tricks—such as copying the root node after subtree unrolling—which break down on more complex or deeper recursive structures.
  • Different hyperparameter settings lead to distinct learned algorithms, indicating that model behavior is sensitive to training setup even on simple recursive tasks.
(b) Encoder self-attention. Reverse Order
(b) Encoder self-attention. Reverse Order

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.