Self-consistency draws many answers and votes. Self-refinement keeps one draft and asks the model to improve it. The loop needs a score, otherwise “improve” is just another sample and you have no reason to keep the new draft instead of the old one.
Two scores are developed here. A rule score is a heuristic on the text. A log-probability score reads the model’s own token probabilities. Neither score is the verifier from Module 3. They are available when you do not want to call the grader inside the loop, and the log-probability score is the same quantity Module 6 will differentiate.
A revision loop
Figure 5.1 runs for a fixed number of rounds, usually a handful. The prompt for a revision contains the original question, the current draft, and an instruction to correct it and box the answer again. The model does not see a gradient. It sees text.
Keep every draft, not only the last one. The last round is not reliably the best. Return the draft with the highest score, and record the score trajectory. A trajectory that never rises means the revision prompt is not giving the model anything to push against.
A rule score
Definition 5.1 (Rule score).A rule score is a scalar computed from the decoded string by a fixed function, with no call to the model and no call to the grader. It may reward the presence of a boxed answer and penalize length. It does not test correctness.
Definition 5.1 exists so the idea of “rank two completions” is concrete before probabilities show up. It is a weak ranker. Two correct solutions can receive different scores because one is shorter. Two incorrect solutions can outrank a correct one. Use it as a baseline, then compare it to the log-probability score on the same pairs.
A minimal version:
def rule_score(text: str) -> float:
score = 1.0 if last_boxed(text) is not None else 0.0
score -= 0.001 * len(text)
return scoreThe length penalty is a tie-break, not a model of quality. The coefficient should be too small to overturn the presence of a box.
From logits to a sequence score
At step
The second form is the numerically stable one. Subtract the max logit before the exp-sum, and do not compute the softmax and then take a log.
Definition 5.2 (Length-normalized log probability).The confidence score of a completion
is the mean token log probability
Definition 5.2 is a property of the generated tokens under the current model. It is not a probability that the answer is correct. A model can be nearly certain and wrong. What the score does measure is how peaked the model was along this particular string.
The mean, rather than the sum, is the right reduction for ranking. The sum
In practice, compute
Using the score inside the loop
At round
- Decode the current draft and record
. - Build a revision prompt that quotes the draft.
- Sample a new completion. Greedy revision tends to echo the draft. A moderate temperature gives the model a chance to change the answer.
- Score the new completion. If it is higher, replace the incumbent.
Stop early when the boxed answer has been stable for two rounds and the score gain is below a small threshold. Otherwise you pay for tokens that restate the same solution.
The same score breaks ties in self-consistency. If two distinct answers each receive
What this does not do
The loop does not know whether the boxed value matches the reference. Plugging the verifier in as the score turns the procedure into a search with an external oracle: generate, grade, keep the correct one if any sample hit it. That is a strong inference-time method, and it is closer to best-of-
Exercises: Homework 5.
Code
- Notebook: ch05_main.ipynb
- Best-of-
and refinement on MATH-500: ch05/02_math500-more-inference-scaling-scripts