Abstract
Training large language models (LLMs) on source code significantly enhances their general-purpose reasoning abilities, but the mechanisms underlying this generalisation are poorly understood. In this paper, we propose Programming by Backprop (PBB) as a potential driver of this effect - teaching a model to evaluate a program for inputs by training on its source code alone, without ever seeing I/O examples. To explore this idea, we finetune LLMs on two sets of programs representing simple maths problems and algorithms: one with source code and I/O examples (w/ IO), the other with source code only (w/o IO). We find evidence that LLMs have some ability to evaluate w/o IO programs for inputs in a range of experimental settings, and make several observations. Firstly, PBB works significantly better when programs are provided as code rather than semantically equivalent language descriptions. Secondly, LLMs can produce outputs for w/o IO programs directly, by implicitly evaluating the program within the forward pass, and more reliably when stepping through the program in-context via chain-of-thought. We further show that PBB leads to more robust evaluation of programs across inputs than training on I/O pairs drawn from a distribution that mirrors naturally occurring data. Our findings suggest a mechanism for enhanced reasoning through code training: it allows LLMs to internalise reusable algorithmic abstractions.
Introduction: The Knowing-Doing Gap
Most Large Language Model (LLM) training relies on the principle of "show, don't just tell." Supervised Finetuning (SFT) typically uses demonstrations, while Reinforcement Learning (RL) relies on direct experience. However, a vast amount of human knowledge is declarative—it describes what to do (e.g., a recipe, a math formula, or Python source code) without necessarily providing every possible input-output pair.
While models can follow these instructions "in-context" via prompting, this is computationally expensive and limited by context windows. Ideally, models should internalize these rules into their weights. The challenge is the "knowing-doing gap": training a model to predict the text of an instruction (declarative loss) does not automatically grant it the ability to execute that instruction (procedural knowledge). This research introduces Programming by Backprop (PBB) to bridge this gap, allowing models to "compile" declarative instructions into procedural behaviors within their parameters.
Methodology: Programming by Backprop (PBB)
PBB treats training on instructions as a way to program specific behaviors into model weights. The researchers introduce two primary curricula to achieve this internalization:
- Proactive PBB: A meta-learning approach where the model first learns a general correspondence between instructions and behaviors using a training set of tasks (Stage 1). In Stage 2, it is exposed to new "evaluation" instructions without any examples. The gradient update from reading the instruction text alone is intended to induce the correct behavior.
- Retroactive PBB: The model first learns all instructions declaratively (Stage 1). In Stage 2, it receives execution supervision for only a subset of those tasks. This "activates" the latent knowledge of the remaining instructions, enabling the model to execute tasks it has only ever "read" about.
Experimental Benchmarks
The study evaluates PBB across several diverse benchmarks to test different facets of procedural learning:
- Random Arithmetic: 1,000 synthetically generated Python functions involving complex loops and conditionals.
- Leetcode: 702 real-world competitive programming problems to test PBB on naturalistic code.
- Ciphers: Custom variations of Caesar and Vigenère ciphers to test out-of-distribution generalization and bias mitigation.
- Formal Grammars: 200 context-free grammars (CFGs) to demonstrate that PBB works for symbolic systems beyond standard programming languages.
Results and Key Insights
The 100x Efficiency Lever
The most striking finding is the massive sample efficiency gain provided by PBB. On the Random Arithmetic task, a single instruction in the training data was found to substitute for approximately 100 demonstration examples. This suggests that declarative data is a significantly more efficient lever for finetuning than raw input-output pairs.
Code as a Superior Scaffold
Algorithmic behaviors are learned much more effectively when instructions are written in code rather than semantically equivalent natural language. The unambiguous, regular structure of code likely provides a better "inductive bias," allowing the model to more easily map the description to a procedural execution strategy. Furthermore, while models can evaluate programs implicitly, they are significantly more reliable when using Chain-of-Thought (CoT) to step through the program in-context.
Latent Learning and RL Activation
In the retroactive regime, the model acquires "latent" knowledge of a procedure just by predicting its description. This knowledge remains dormant until a specific training curriculum activates it. Interestingly, Reinforcement Learning (specifically using the GRPO algorithm) proved more effective than SFT at "activating" this declarative knowledge without causing the model to over-memorize specific examples.
Conclusion
Programming by Backprop demonstrates that LLMs can internalize complex procedural logic from source code alone. While internalized execution can still be "noisier" than in-context prompting, the 100x efficiency gain suggests a powerful new path for data-efficient training. Future work will explore how to elicit these effects during large-scale pretraining and how PBB might be used for safety alignment by "programming" models to follow formal constitutional principles.
Summary updated