RRHF (Rank Responses to align language models with Human Feedback) is an offline alignment method that takes several responses to the same prompt, from any source, scores each with a reward, and trains the model so that its length-normalised log-likelihood ranks the responses in the same order, while also doing supervised fine-tuning on the best one. The loss is a margin-free pairwise hinge over every ordered pair plus cross-entropy. There is no value model, no reference model and, if responses and rewards are precomputed, no generation during training. RRHF belongs to the first wave of "RLHF without RL" methods, alongside SLiC-HF and before DPO, and in practice it behaves as a best-of-n learner: the trained model's reward ends up close to the reward of the best response it was shown. It sits after or in place of SFT, and is a close cousin of rejection-sampling fine-tuning.
Where RRHF came from#
RRHF was proposed in April 2023 by Zheng Yuan, Hongyi Yuan, Chuanqi Tan, Wei Wang, Songfang Huang and Fei Huang of Alibaba DAMO Academy and Tsinghua University (Yuan et al., 2023), and published at NeurIPS 2023. The arXiv title adds "without tears", which sums up the motivation. PPO-based RLHF, as in InstructGPT, keeps four models in memory (policy, value, reward, reference), is sensitive to many hyperparameters, and only learns from the policy's own samples. The authors wanted something that adds about 30 lines to an SFT script.
The idea came from summarisation, where BRIO (Liu et al., 2022) and the original SLiC had used ranking losses over sequence probabilities to calibrate generation. RRHF replaces the summarisation metric with a reward: a reward model, human labels, or another LLM acting as judge. And it removes a restriction that PPO has: responses can come from the model itself, from other models (the paper uses ChatGPT, text-davinci-003, LLaMA and Alpaca), or from humans.
On Anthropic's Helpful and Harmless data, with a 6B GPT-J reward model as the proxy judge, RRHF on Alpaca-7B reached an average reward of (top-p sampling) and (diverse beam search), against for PPO on the same model, for untrained Alpaca and for the dataset's human-preferred responses. In human evaluation of the diverse-beam variant against PPO, 27 wins, 48 ties and 25 losses: comparable, from a far simpler training loop.
The objective#
For a query , take responses sampled by any policies , and score each with a reward function, . The model being trained scores each response by its length-normalised conditional log-probability:
The ranking loss penalises every pair whose model scores disagree with their rewards:
The fine-tuning loss is cross-entropy on the highest-reward response with :
Four details are deliberate. The sum is unweighted: the authors tried weights of 10 and 100 on the ranking term, as BRIO had suggested, and found them worse. There is no margin: BRIO's margins were dropped because results were good without them and tuning was expensive. Scores are length-normalised, unlike SLiC-HF's summed log-likelihoods. And there is no KL term, because samples are drawn before training from fixed policies rather than from the moving .
What the gradient does
For a violated pair ( but ), the hinge's gradient is , pushing the worse response's average token log-probability down and the better one's up, each with unit weight on the length-normalised score, so per token. A pair already in the right order contributes nothing, even if the scores are tied to the third decimal. Meanwhile contributes a full SFT gradient, summed over the tokens of the best response, at every step. Two consequences follow. With no margin, the ranking term is satisfied the moment the order is right, so it is a corrector, not a driver. And because the cross-entropy is summed over tokens while each ranking term is an average, dominates the gradient for responses of any realistic length. RRHF is best understood as SFT on the best of , plus a term that actively pushes down responses the model likes more than better ones.
RRHF next to its neighbours
| Method | Responses per prompt | Score compared | Loss | Reference model |
|---|---|---|---|---|
| Best-of-n SFT / RAFT | n, keep best | none | CE on best | no |
| RRHF | k, any source | mean token log-prob | margin-free hinge over all ordered pairs + CE on best | no |
| SLiC-HF | pairs | summed log-prob | hinge with margin + CE regulariser | no |
| PRO | k, ranked | length-normalised log-prob | listwise softmax (Plackett-Luce style) + CE | no |
| DPO | pairs | log-ratio to reference | logistic | yes |
| SimPO | pairs | mean token log-prob | logistic with target margin | no |
The comparison with SimPO is instructive: SimPO, a year later, uses the same length-normalised score as RRHF but with a logistic loss and a target reward margin, and without the SFT term. PRO (Song et al., 2023) generalises RRHF's pairwise hinge to a listwise objective over a full ranking.
Worked example: four responses#
A prompt has four responses with rewards and model log-likelihoods:
| Response | Reward | Sum of log-probs | Tokens | Score |
|---|---|---|---|---|
| a | 0.9 | 50 | ||
| b | 0.2 | 30 | ||
| c | 100 | |||
| d | 0.4 | 40 |
The reward order is a, d, b, c. The model's order by is b, c, d, a, almost exactly backwards. Go through the six ordered pairs with : (d, a) gives ; (b, a) gives 0.4; (c, a) gives 0.3; (b, d) gives 0.3; (c, d) gives 0.2; and (c, b) gives , because the model already scores b above c. So . The fine-tuning term is the negative log-likelihood of a, the best response: . The total loss is 61.3, of which the ranking term is about 2%.
Length normalisation changes the picture. Ranked by summed log-probability, the model would order the responses b (), d (), a (), c (), so the 100-token response c would look worst for no reason other than its length. With normalisation, c's per-token score of is better than a's , and the hinge correctly flags that c is over-scored relative to a. Without normalisation, ranking losses on sequence probabilities systematically favour short responses, since every extra token adds a negative log-probability.
The algorithm#
# Offline: build candidates and rewards (done once, or once per iteration)
for x in prompts:
cands[x] = (beam_search(policy_init, x, beams=4) # or diverse beam / top-p
+ dataset_responses(x)) # e.g. HH chosen + rejected
rewards[x] = [reward_model(x, y) for y in cands[x]]
# Training: an SFT loop with one extra term
for batch in loader(prompts): # 1 query per GPU, grad accum 8
loss = 0
for x in batch:
ys, r = cands[x], torch.tensor(rewards[x])
p = torch.stack([policy.mean_token_logprob(x, y) for y in ys]) # (k,)
worse = r[:, None] < r[None, :] # r_i < r_j
rank = torch.relu(p[:, None] - p[None, :])[worse].sum()
ft = -policy.sum_token_logprob(x, ys[int(r.argmax())])
loss = loss + rank + ft
(loss / len(batch)).backward(); opt.step(); opt.zero_grad()
Sampling and training are separate, so the training job needs only the policy in memory. The paper's sampling policies are worth knowing because they mattered more than anything else. Each query got four model samples plus the dataset's two human responses: vanilla beam search (BP), diverse beam search with four groups and diversity penalty 1.0 at temperature 0.8 (DP), or top-p sampling at temperature 0.8 (SP). Training only on the two dataset responses (setting P) gave the same reward, , from all three starting models, which the authors took as evidence that sample quality, not the starting checkpoint, determines the result. Removing made results worse ( versus with beam sampling), so the ranking term earns its place despite its small share of the loss.
Iterated and online variants
Iterating helps: resample candidates from the trained model and train again (IP-n). Rewards improved from (IP-1) to (IP-2) and (IP-3), and in human evaluation IP-2 beat the single-round model in 10 of 100 comparisons, with 90 ties and no losses. Fully online sampling (resampling from every 32 steps) pushed the proxy reward to 0.34, but perplexity jumped from about 14 to 63.78 and the outputs were reward hacking: "very friendly but meaningless" responses, including fake dialogue turns in which a "Human" praises the answer. Adding a KL penalty of 0.01 to the reward fixed it (reward , perplexity 19.76), at the cost of a reference model, which the authors note is contrary to the method's purpose.
Data
Prompts, responses per prompt, and a scalar reward or a ranking per response. The paper used the HH dataset's prompts with (four samples plus two dataset responses), truncated to 192 tokens, and for the Wombat model, Alpaca's prompts with responses from four models scored by ChatGPT. Since RRHF only uses the order of the rewards, any judge that can rank works: human labels, a reward model, or an LLM judge.
Hyperparameters#
| Parameter | Paper value | Notes |
|---|---|---|
| Learning rate | 2e-5, warm-up then linear decay to 0 | SFT-like; RRHF "converges well under the same hyper-parameter setting as SFT". |
| Epochs | 3, no early stopping | Losses and rewards peaked in the third epoch (2,400 to 3,600 steps). |
| Batch | 1 query per GPU, 8 accumulation steps, 8 GPUs: 64 queries | Each query carries all responses. |
| (responses per query) | 6 (4 sampled + 2 provided) | Quality of the best response matters more than . |
| Max length | 192 tokens (query + response) | Memory scales with times length. |
| Ranking weight | 1 (unweighted sum) | 10 and 100 were worse. |
| Margin | none | Consider adding one if responses are near-duplicates. |
The only real tuning decision is how to generate candidates. Use the strongest sampling setup you can afford, include high-quality external responses if you have them, and iterate. If you add anything, add a margin: with none, pairs that are ordered correctly by a hair stop contributing, which matters when candidates are near-duplicates from beam search.
Compute and memory#
Training memory is one model with its optimiser state, about 16 bytes per parameter for full fine-tuning with Adam in mixed precision, the same as SFT; there is no reference or value model. Activations are times larger than SFT's for the same sequence length, since each step runs responses per query through the model (the paper's 1 query per GPU with at 192 tokens is a direct consequence). Per step:
against DPO's two responses plus a reference forward pass. Sampling is a separate, embarrassingly parallel job: the paper reports 4 to 6 hours on 8 A100 80GB GPUs to sample with beam, diverse beam or top-p, and 4 to 6 hours to train without online sampling, versus about 30 hours with online sampling. The PPO comparison is the paper's headline: one or two models instead of four.
Toy run: RRHF as a best-of-n learner#
We checked the best-of-n claim in a tabular model where we know every reward. 300 prompts, each with a pool of 12 possible responses with hidden rewards drawn from . The policy is a softmax over the pool, initialised randomly (expected reward 0.019). For each prompt, the candidate set has 4 samples from the policy plus two "provided" responses (the pool's second-best and third-worst, playing the role of the dataset's chosen and rejected). The mean reward of a candidate is 0.069; the mean reward of the best candidate per set is 1.299. In a tabular model the score is just . We trained with gradient steps at learning rate 0.1 and compared RRHF, cross-entropy on the best candidate alone, and the ranking loss alone.
| Steps | RRHF (rank + CE) | CE on best only | Rank only |
|---|---|---|---|
| 10 | 0.398 | 0.170 | 0.223 |
| 30 | 0.769 | 0.568 | 0.223 |
| 100 | 1.146 | 1.120 | 0.223 |
| 300 | 1.254 | 1.250 | 0.223 |
The results line up with the paper's observations. The trained policy's reward converges to the best-of-set reward (1.254 against 1.299 after 300 steps), which is the paper's "best-of-n learner" statement in its simplest form; in a separate run trained to convergence (1,500 steps at learning rate 1.0, a different candidate draw) it matched the set best to three decimals, 1.292 against 1.292. The ranking term matters early: after 30 steps RRHF is at 0.769 against 0.568 for cross-entropy alone, because it actively pushes probability off responses the model over-scores. And ranking alone stalls at 0.223 after 10 steps. Without a margin, it stops the instant every candidate set is ordered correctly, which shows why RRHF needs the cross-entropy term to pull the model towards the best response.
Iterating, with 30 steps per round and fresh samples from the current policy, raised the trained policy's reward from 0.761 to 1.045, 1.159 and 1.218 over four rounds, while the mean candidate reward rose from 0.049 to 0.856. That is the IP-n result: each round's candidates are better, so each round's best-of-n target is better.
Failure modes#
Ceiling set by the candidates. RRHF can only learn what is in its candidate sets. If the best of is mediocre, so is the model. Track the mean and maximum candidate reward per round; if the trained model's reward is already near the maximum, more epochs will not help, but better or more diverse candidates will.
Reward hacking with online sampling. As soon as samples come from the model being trained, a learned reward model is exploitable, exactly as with PPO. The paper's online run found degenerate praise loops within the proxy reward's blind spots. Watch perplexity on a held-out set and read samples; add a KL penalty if you sample online.
Base models with poor samples. Starting from untuned LLaMA gave the worst results ( with diverse beam and perplexity 67), because its own samples were poor (). Start from an instruction-tuned model or include strong external responses.
Ties and near-duplicates. Beam-search candidates are often near-identical. With no margin and tied rewards, those pairs contribute nothing and is effectively smaller. Deduplicate and prefer diverse beam search or sampling.
Long responses under-weighted in ranking. Length normalisation removes the short-response bias of summed log-probabilities, but it also means a long response's per-token gradient from the ranking term is small. If a long, bad response is over-scored, it is corrected slowly.
When to use RRHF#
| Situation | Use | Why |
|---|---|---|
| Several scored responses per prompt, including strong external ones; SFT-sized budget | RRHF | Uses all responses and any source; one model in memory. |
| Only the best response matters and you have a good judge | Rejection-sampling fine-tuning | Nearly the same fixed point, simpler. |
| Pairwise preferences only | DPO, SimPO | Better studied for pairs; SimPO shares RRHF's length-normalised score. |
| Want to distil the best-of-n distribution, not just its argmax | BOND | Matches the full best-of-n distribution. |
| Want to improve beyond the best sample you can generate | PPO, GRPO, online DPO | On-policy exploration; RRHF reorders what it sees. |
| Ranked lists, want a listwise loss | PRO | Plackett-Luce style objective over the full ranking. |
Minimal implementation#
import torch
def token_logprobs(model, input_ids, attention_mask, labels_mask):
logits = model(input_ids=input_ids, attention_mask=attention_mask).logits[:, :-1]
lp = torch.gather(logits.log_softmax(-1), 2, input_ids[:, 1:, None]).squeeze(-1)
m = labels_mask[:, 1:].float()
return (lp * m).sum(-1), m.sum(-1).clamp(min=1) # summed log-prob, length
def rrhf_loss(model, batch, rank_weight=1.0):
"""batch: k responses for ONE query, stacked: input_ids (k, T), attention_mask, labels_mask; rewards (k,)."""
summed, length = token_logprobs(model, batch["input_ids"], batch["attention_mask"], batch["labels_mask"])
p = summed / length # length-normalised scores
r = batch["rewards"]
worse = (r[:, None] < r[None, :]).float() # pair (i, j) with r_i < r_j
rank = (torch.relu(p[:, None] - p[None, :]) * worse).sum()
ft = -summed[r.argmax()] # CE on the best response
return rank_weight * rank + ft, {"rank": rank.item(), "ft": ft.item()}
The authors' reference implementation, a modified Alpaca training script, is in the GanjinZero/RRHF repository. Most preference libraries do not ship RRHF as a named loss, but the function above drops into any SFT trainer that can batch responses per prompt. In POFE, an RRHF-style run is a preference recipe with a candidate-generation stage, and the run report shows candidate best-of- reward next to the trained model's, so the ceiling is visible before you spend more epochs.
Further reading#
Yuan et al. (2023) is the primary source; its ablation table on sampling policies is the most useful part. Liu et al. (2022, BRIO) is the summarisation work whose ranking loss RRHF adapted; Zhao et al. (2022, SLiC) and Zhao et al. (2023, SLiC-HF) are the parallel line from Google. Song et al. (2023, PRO) extends the pairwise hinge to a listwise objective. Dong et al. (2023, RAFT), contemporary with RRHF, fine-tunes on the best-reward samples only; the RRHF paper's ranking-loss ablation is the direct comparison. Ouyang et al. (2022) is the InstructGPT pipeline RRHF set out to simplify.
Frequently asked questions#
What is the RRHF loss?
The sum over all response pairs with r_i < r_j of max(0, p_i - p_j), where p_i is the response's mean token log-probability under the model, plus the cross-entropy of the highest-reward response. The two terms are added without weighting and there is no margin.
Why does RRHF length-normalise the log-probabilities?
Summed log-probabilities fall with every extra token, so a ranking loss on sums systematically favours short responses. Dividing by length compares responses on per-token likelihood, which is the same choice SimPO later made.
Is RRHF the same as best-of-n fine-tuning?
Close. The paper found the trained model's reward ends up near the average best-of-n reward of its training candidates, and calls RRHF a best-of-n learner. The ranking term adds active pressure against responses the model over-scores; removing it lowered the HH reward from -1.03 to -1.14.
Does RRHF need a reference model?
No. Candidates are sampled before training from fixed policies, so there is no KL term and only the policy is in memory. If you sample online from the model being trained, the paper found reward hacking and needed a KL penalty, which brings the reference model back.
Where can the candidate responses come from?
Anywhere: the model's own beam or sampled outputs, other LLMs, or human-written chosen and rejected responses. The paper's best settings combined four model samples with the dataset's two human responses, and sample quality mattered more than the starting checkpoint.
References#
- Zheng Yuan, Hongyi Yuan, Chuanqi Tan, Wei Wang, Songfang Huang, Fei Huang (2023). RRHF: Rank Responses to Align Language Models with Human Feedback without tears. arxiv.org/abs/2304.05302
- Yixin Liu, Pengfei Liu, Dragomir Radev, Graham Neubig (2022). BRIO: Bringing Order to Abstractive Summarization. arxiv.org/abs/2203.16804
- Yao Zhao, Misha Khalman, Rishabh Joshi, Shashi Narayan, Mohammad Saleh, Peter J. Liu (2022). Calibrating Sequence likelihood Improves Conditional Language Generation. arxiv.org/abs/2210.00045
- Yao Zhao, Rishabh Joshi, Tianqi Liu, Misha Khalman, Mohammad Saleh, Peter J. Liu (2023). SLiC-HF: Sequence Likelihood Calibration with Human Feedback. arxiv.org/abs/2305.10425
- Feifan Song, Bowen Yu, Minghao Li, Haiyang Yu, Fei Huang, Yongbin Li, Houfeng Wang (2023). Preference Ranking Optimization for Human Alignment. arxiv.org/abs/2306.17492
- Hanze Dong, Wei Xiong, Deepanshu Goyal, Yihan Zhang, Winnie Chow, Rui Pan, Shizhe Diao, Jipeng Zhang, Kashun Shum, Tong Zhang (2023). RAFT: Reward rAnked FineTuning for Generative Foundation Model Alignment. arxiv.org/abs/2304.06767
- Long Ouyang et al. (2022). Training language models to follow instructions with human feedback. arxiv.org/abs/2203.02155
- GanjinZero (2023). RRHF code repository. github.com/GanjinZero/RRHF