# Reasoning language model

Reasoning language models are artificial intelligence systems that combine natural language processing with structured reasoning capabilities. These models are usually constructed by prompting, supervised finetuning (SFT), and reinforcement learning (RL) initialized with pretrained language models.

## Prompting

A language model is a generative model of a training dataset of texts. Prompting means constructing a text prompt, such that, conditional on the text prompt, the language model generates a solution to the task. Prompting can be applied to a pretrained model ("base model"), a base model that has undergone SFT, or RL, or both.

### Chain of thought

Chain of Thought prompting (CoT) prompts the model to answer a question by first generating a "chain of thought", i.e. steps of reasoning that mimic a train of thought. It was published in 2022 by the Brain team of Google on the PaLM-540B model. In CoT prompting, the prompt is of the form "<Input> Let's think step by step", and the model would respond with a chain of reasoning steps, ended with an answer:${\text{Input}}\rightarrow \underbrace {{\text{Step}}_{1}\rightarrow {\text{Step}}_{2}\rightarrow \cdots \rightarrow {\text{Step}}_{n}} _{\text{Reasoning chain}}\rightarrow {\text{Answer}}$Similarly, Tree of Thought prompting generalizes CoT by prompting the model to generate one or more "possible next steps", and then running the model on each of the possible next steps by breadth-first, beam, or some other method of tree search. Similarly, Graph of Thought generalizes CoT so that the reasoning steps form a directed acyclic graph.
Self-consistency decoding performs several chain-of-thought rollouts, then selects the most commonly reached conclusion out of all the rollouts. If the rollouts disagree by a lot, a human can be queried for the correct chain of thought.

### Retrieval-augmented generation

A language model may answer a query by first querying a database of documents using the query. The document retrieval can be via a vector database, summary index, tree index, or keyword table index. Following document retrieval, the LLM generates an output that incorporates information from both the query and the retrieved documents.

### Tool use

Language models can perform long reasoning steps by calling external methods, such as numerical recipes, program interpreters, API calls, and so on. This can be prompt-engineered by describing the external methods in-context (an example of in-context learning) or finetuned into the model.

## Supervised finetuning

A base model can be finetuned on a dataset of reasoning tasks with example solutions and reasoning traces. The finetuned model would then be able to generate reasoning traces for a given problem.
As it is expensive to get humans to write reasoning traces for a SFT dataset, researchers have proposed ways to automatically construct SFT datasets. In rejection sampling finetuning (RFT), new reasoning traces are collected via a loop:
* Sample a task prompt
* Generate many reasoning traces for the prompt.
* Use a verifier to remove reasoning traces with the wrong final answer.
* For each remaining trace, extract the set of equations appearing in it. Deduplicate the traces so that each one has a different set of equations. Add those to the dataset.

## Reinforcement learning

A pretrained language model can be further trained by RL. In the RL formalism, a generative language model is a policy $\pi$. A prompt specifying a task to solve is an environmental state $x$, and the response of the language model to the prompt is an action $y$. The probability that the language model responds $x$ with $y$ is $\pi (y|x)$.
Training a reasoning language model by RL then consists of constructing a reward model $r(x,y)$ to guide the RL process. Intuitively, a reward model describes how desirable/appropriate/good the response is for the prompt. For reasoning language model, the prompt describes a reasoning task, and the reward would be high if the response solves the task, and low if the response fails to solve the task.
For reasoning language models, the model's response $y$ may be broken down into multiple steps, in which case it is written as $y_{1},y_{2},\dots ,y_{n}$.

### Outcome Reward Model

Outcome reward model, or outcome-supervised RM (ORM), is a reward model that computes the reward of a step $r(x,y_{1},\dots ,y_{i})$ determined by the final answer: $r(x,y_{1},\dots ,y_{i})=r(x,y_{n})$. They are also called "verifiers".
For tasks with an answer that is easy to verify, such as word problems in math, the outcome reward can simply be binary: 1 if the final answer is correct, and 0 otherwise. If the answer is not easy to verify programmatically, humans can manually label the answers as correct or not, then the labels can be used to finetune a base model that predicts the human label. For other kinds of tasks, such as creative writing, where task performance is not binary true/false, one can train a reward model by finetuning a base model on human ranked preference data, such as used in reinforcement learning from human feedback. A base model can also be finetuned to predict, given a partial thinking trace $x,y_{1},\dots ,y_{m}$, whether the final answer would be correct or not. This can then be used as a binary reward signal.
The ORM is usually trained via logistic regression, i.e. minimizing cross-entropy loss.
Given a PRM, an ORM can be constructed by multiplying the total process reward during the reasoning trace, or by taking the minimum, or some other method to aggregate the process rewards. DeepSeek used a simple ORM for training the R1 model.

### Process Reward Model

Process reward model, or process-supervised RM (PRM), is a reward model that computes the reward of a step $r(x,y_{1},\dots ,y_{i})$ determined by the steps so far: $(x,y_{1},\dots ,y_{i})$.
Given a partial thinking trace $x,y_{1},\dots ,y_{m}$, a human can be queried as to whether the steps so far are correct, regardless of whether the ultimate answer would be correct. This can then be used as a binary reward signal. As human labels are expensive, a base model can then be finetuned to predict the human labels. The PRM is usually trained by logistic regression on the human labels, i.e. by minimizing the cross-entropy loss between the true labels and the predicted labels.
As an example, in a 2023 OpenAI paper, 800K process labels were collected for 75K solution traces. A labeler would be presented with a solution trace, and keep labelling "positive" if the step progresses towards the solution, "neutral" if it is not wrong, but does not progress towards solution, and "negative" if it is a mistake. As soon as a "negative" label is entered, the labeler stops labeling that thinking trace, and begins labeling another one. The idea was that, while labelling subsequent reasoning steps can provide even richer supervision signals, simply labeling up to the first error was sufficient for training a competent PRM.
As human labels are expensive, researchers have proposed methods to create PRM without human labels on the processes. Inspired by Monte Carlo tree search (MCTS), the Math-Shepherd method samples multiple continuations until the end, starting at each reasoning step $y_{i}$, and set the reward at that step to be either ${\frac {\#{\text{(correct answers)}}}{\#{\text{(total answers)}}}}$ in the case of "soft estimation", or ${\begin{cases}1&{\text{if one of the answers is correct}}\\0&{\text{else}}\end{cases}}$ in the case of "hard estimation". This creates process reward using only an ORM, which is usually easier or cheaper to construct. After creating these process reward labels, a PRM can be trained on them. Some have tried a fully MCTS approach.
One can also use an ORM to implicitly construct a PRM, similar to direct preference optimization.

### Guided sampling

A trained ORM can be used to select the best response. The policy would rollout multiple responses, and a trained ORM would select the best response. This allows a simple form of test time compute scaling ("best-of-N").
A trained PRM can also be used to guide reasoning by greedy tree search. That is, the policy model generates several possible next reasoning steps, and the PRM selects the best one, and the process repeats. This is similar to how a trained ORM can be used to select the best response. Beam search perform better than greedy search.
Lookahead search is another tree search method, where the policy model generates several possible next reasoning steps, then make a (partial) rollout for each. If a solution endpoint is reached during the forward simulation, the process halts early. Otherwise, the PRM is used to calculate the total reward for each rollout. The step with the highest rollout is selected.
Self-consistency can be combined with an ORM. The model would be used to generate multiple answers, and the answers would be clustered, so that each cluster has the same answer. The ORM is used to compute the reward for each answer, and the rewards within each cluster is summed. The answer corresponding to the cluster with the highest summed reward is output.

## Applications

Prompt engineering was discovered in GPT-3 as "few-shot learning", which began a period of research into "eliciting" capacities of pretrained language models. It was then found that a model could be prompted to perform CoT reasoning, which improves its performance on reasoning tasks.

## Benchmark

The reasoning ability of language models are usually tested on problems with unambiguous solutions that can be cheaply checked, and requires reasoning when solved by a human. Such problems are usually in mathematics and competitive programming. The answer is usually an array of integers, a multiple choice letter, or a program that passes unit tests within a limited runtime. Some common ones include:
* GSM8K (Grade School Math): 8.5K linguistically diverse elementary school math word problems that require 2 to 8 basic arithmetic operations to solve.
* MMLU (Measuring Massive Multitask Language Understanding): 16,000 multiple-choice questions spanning 57 academic subjects including mathematics, philosophy, law, and medicine.
* GPQA (Google-Proof Q&A): 448 multiple-choice questions written by domain experts in biology, physics, and chemistry, and requires PhD-level experts to solve.
* HumanEval: Programming problems where the solution is always a python function, often just a few lines long.
The benchmark scores are of the following kinds:
* pass@n: The model is given $n$ attempts to solve each problem. If any attempt is correct, the model earns a point. The pass@n score is the model's average score over all problems.
* cons@n: The model is given $n$ attempts to solve each problem. If the most common answer is correct, the model earns a point. The cons@n score is the model's average score over all problems. Here "cons" stands for "consensus" or "majority voting".
The pass@n score can be estimated more accurately by making $N>n$ attempts, and use the unbiased estimator $1-{\frac {\binom {N-c}{n}}{\binom {N}{n}}}$, where $c$ is the number of correct attempts.