Search

Search papers and lessons

PAL: Program-aided Language Models

Luyu Gao, Aman Madaan, Shuyan Zhou et al. · 2022

PAL has a language model write Python programs for word problems and hands the arithmetic to a Python interpreter, beating much larger models that reason in plain text.

On this page

  • Colour = topic mix
  • Neural network basics
  • Calculus
  • Discrete math
  • Linear algebra
  • Probability
  • Architectures
  • ReadReady to readNot yet

TL;DR

Language models are good at breaking a word problem into steps, but they often get the arithmetic wrong, even when the steps are right. PAL, short for Program-Aided Language models, keeps the first job and offloads the second. The model writes each step as Python code, with comments for the words, and a Python interpreter runs the program to get the answer. On 13 reasoning tasks, PAL with the Codex model beat much larger models that reason in plain text, and it set a new few-shot result on the GSM8K math benchmark.

Builds on: Chain-of-Thought Prompting, Program-Aided Language Model, Language Model · Leads to: Toolformer and later tool-using reasoning systems

Why it mattered

PAL showed that a small change to what a model writes, from prose steps to runnable code, can lift accuracy more than a much larger model. The gains were largest on problems with big numbers, where plain-text reasoning broke down. Program-based reasoning spread into later work on math questions, and it is one of the early examples of a language model calling an outside tool during reasoning. Its ideas recur in later work on tool use and on checking answers by execution.

The problem

By 2022, prompting a language model to reason step by step, as in chain of thought, was the standard way to get better answers on word problems. The model writes a short explanation, then the answer. That helps with the reading and planning, but the arithmetic is still done by the model, token by token, with no guarantee of correctness.

The paper names the gap precisely. A chain of thought can have every step in the right order and still produce a wrong number. On GSM8K, the grade-school math benchmark from Cobbe and colleagues, chain of thought with the 540 billion parameter PaLM model reached 56.9 percent, and the paper's analysis points to wrong calculations rather than wrong plans as the usual failure. Larger numbers made things worse. The authors also checked the bigger picture: in 16 of 25 cases they examined on a version with large numbers, the reasoning text was nearly identical to the easy version, so the failure was in the arithmetic.

How it works

PAL is a prompting method. It does not change the model. Three parts make up each query.

  1. Build the prompt. Take three to six worked examples. Each has a question and a reasoning block, but no final answer. The reasoning block is Python code, with comments for the plain-language steps. Check: the prompt shows the model how to write a program, not how to give a number.
  2. Append the test question. The new question goes after the examples, in the same format. The model continues the pattern and writes its own program for the new question. Check: the model decides the decomposition, and it does so in code.
  3. Run the program. The generated code is passed to a Python interpreter. The last value the program computes is the answer. Check: the arithmetic is done by Python, so the answer is correct if the program is.

A small example from the paper makes this concrete. The question is about a player who starts with 5 tennis balls and buys 2 cans of 3 balls each. Chain of thought writes the sentence "2 cans of 3 tennis balls each is 6." PAL writes the same steps as code: tennis_balls = 5, then bought_balls = 2 * 3, then answer = tennis_balls + bought_balls. The interpreter returns 11. Check: the program reads like the sentence, and the number comes from Python, not from the model.

flowchart LR
    Q[word problem] --> M[language model writes Python with comments]
    M --> P[program]
    P --> I[Python interpreter runs it]
    I --> A[answer]

The choice that matters most is the use of meaningful variable names, such as tennis_balls, so the model can link each variable to a thing in the question. The ablations below test this choice.

The math

PAL does not add a formula, but its sampling is standard. The paper uses greedy decoding, meaning the model picks the most likely token at each step, with temperature zero. The answer is the output of a deterministic function applied to the program pp:

y^=run(p),p=LM(x1,t1,…,xk,tk,xtest).\hat y = \mathrm{run}(p), \qquad p = \mathrm{LM}(x_1, t_1, \ldots, x_k, t_k, x_{\text{test}}).

Here xix_i are the example questions, tit_i their programs, xtestx_{\text{test}} the new question, and run\mathrm{run} is the interpreter. The only learned part is the language model. The interpreter is exact, so all the error is in the program the model writes. That is the paper's central claim: decomposing the problem is the only learning task left.

Majority voting adds a second formula. The paper samples k=40k = 40 programs at temperature 0.7, using nucleus sampling with p=0.95p = 0.95, and takes the most common answer:

y^maj=arg⁡max⁡y∑j=1401[ run(pj)=y ].\hat y_{\text{maj}} = \arg\max_{y} \sum_{j=1}^{40} \mathbf{1}[\, \mathrm{run}(p_j) = y \,].

This is the same majority-vote idea used for chain of thought, applied to programs. Each program is cheap to run, so the vote costs little beyond sampling.

Results

Table 1 reports solve rates on the math datasets. PAL with Codex is best on every task.

DatasetDirect, CodexCoT, CodexCoT, PaLM-540BPAL, Codex
GSM8K19.765.656.972.0
GSM-Hard5.023.1not reported61.2
SVAMP69.974.879.079.4
ASDiv74.076.973.979.6
MAWPS MultiArith44.095.994.799.2

GSM-Hard is the paper's harder version of GSM8K, where the numbers in each question are replaced with much larger values (the paper's example changes 23 dollars to 15,687 dollars). The answers were recomputed with the help of PAL itself, using the programs PAL had already written correctly. On it, chain of thought falls from 65.6 to 23.1, while PAL falls from 72.0 to 61.2. This is the clearest sign of robustness: the program does not care how large the numbers are.

Majority voting over 40 samples raises PAL on GSM8K from 72.0 to 80.4, which the paper reports as 1.9 points above Minerva 540B with the same number of samples.

On the symbolic and algorithmic tasks from BIG-Bench Hard, PAL with Codex gives these results against chain of thought with Codex:

TaskCoT, CodexPAL, Codex
Colored Objects86.395.1
Penguins79.293.3
Date Understanding64.876.2
Object Counting73.096.7
Repeat Copy68.890.6

The ablations explain where the gain comes from. A variant that writes a program and then has the model state the answer itself, without an interpreter, scores 23.2 on GSM8K, close to direct prompting. So the program alone is not enough; the interpreter does the work. Removing the variable names drops PAL below chain of thought on some tasks, so meaningful names matter for the model. Scores also depend on the model: PAL beat chain of thought on each code model the authors tried, but on a text-only model the gain appeared only once the model was strong enough, and the weakest text model did worse with PAL than with chain of thought.

The limits are real. The main results use Codex, a closed model, and the paper reports results from a single prompt set per task. The gsm-hard numbers come from a construction that relies partly on PAL's own outputs. And the method needs a model that writes working code; the weakest text model in the paper did worse with PAL than with chain of thought.

Reading guide

The paper is short, and each section leans on a prerequisite lesson.

Check yourself

QuestionRecall: what does the language model produce in PAL, and what does the interpreter produce?Show answer

The language model produces a Python program, with comments for the plain-language steps. The interpreter runs that program and produces the answer, so the arithmetic is done by the interpreter rather than by the model.

QuestionCompute: a PAL program sets bought_balls = 2 * 3 and tennis_balls = 5, then answer = tennis_balls + bought_balls. What does the interpreter return?Show answer

The interpreter returns 5+6=115 + 6 = 11. Each line is an exact operation, so the answer is only as wrong as the program's logic.

QuestionWhy: why does PAL keep its gains on GSM-Hard, while chain of thought falls sharply?Show answer

In chain of thought the model does the arithmetic token by token, and it makes more mistakes as the numbers get larger. In PAL the model writes the formula, and the interpreter evaluates it exactly for any size of number. So the failure point moves from the arithmetic to the program, which is much less sensitive to the size of the numbers.

QuestionApplied: you want a model with no code training to use PAL on a new task. What does the paper suggest you check first, and why?Show answer

The paper found that PAL helped text-only models only once they were strong enough at code, and the weakest text model did worse with PAL than with chain of thought. So you should first test whether the model writes correct, runnable programs on a few examples of the task, and compare its results with chain of thought before committing to PAL.

Sources

Read the full paper

Luyu Gao, Aman Madaan, Shuyan Zhou et al., 2022. arXiv:2211.10435