TL;DR
Llama 2 is a family of language models from 7 to 70 billion parameters, pretrained on 2 trillion tokens. Its chat variant, Llama 2-Chat, is made by supervised fine-tuning on demonstrations, then several rounds of reinforcement learning from human feedback, using separate reward models for helpfulness and safety. The largest chat model wins 36% of human comparisons against ChatGPT and ties 31.5%.
Where you'll see it: as a base for open-weight chat assistants, and as a worked example of combining supervised fine-tuning, reward models and RLHF in one published pipeline.
The problem
The paper states its aim as releasing pretrained and fine-tuned models so the community can build on the work, with a detailed account of the fine-tuning and safety steps. It positions its chat models against open-source chat models, which it says it outperforms on most benchmarks tested, and against closed-source assistants, which it says its models may substitute for on helpfulness and safety. Dialogue also raises a specific problem: a model can be helpful and unsafe at the same time, and the two goals can pull against each other, so the paper treats them as separate objectives.
Key idea
The work has two stages. The first is a better base model: more data, a longer context, and grouped-query attention for the large sizes. The second is an alignment pipeline that turns the base into a chat model. Supervised fine-tuning gives the model its format and first behaviours. Then preference data, collected from humans each week, trains reward models, and the policy is improved against them over several iterations.
flowchart LR A["Pretraining, 2T tokens"] --> B["Supervised fine-tuning"] B --> C["Human preference data, weekly"] C --> D["Helpfulness and safety reward models"] D --> E["Rejection sampling, then PPO"] E --> C E --> F["Llama 2-Chat"]
The method
Pretraining
The corpus is a new mix of publicly available data, excluding Meta's product data. The paper trains on 2 trillion tokens, which it describes as a good trade-off between performance and cost, and up-samples the most factual sources to help with knowledge and hallucination. The paper reports 40% more tokens than Llama 1, a context length doubled from 2048 to 4096 tokens, and more robust data cleaning.
The architecture follows Llama 1. It is a standard auto-regressive transformer with pre-normalisation using RMSNorm, the SwiGLU activation, and rotary positional embeddings. The 34B and 70B models use grouped-query attention, which the paper adopts for inference scalability. The optimiser is AdamW with , and . The learning rate follows a cosine schedule with 2000 warmup steps and decays to 10% of its peak. Weight decay is 0.1 and gradient clipping is 1.0. Every model uses a global batch of 4 million tokens. The peak learning rate is for 7B and 13B, and for 34B and 70B. The tokeniser is byte-pair encoding with a 32k vocabulary, and numbers are split into single digits.
The paper's ablations support two of the changes. In a 150-billion-token comparison with the 2k context, the 4k context raises NarrativeQA F1 from 0.21 to 17.26, and it does not hurt SQuAD. For grouped-query attention, a 30B ablation trained on 150 billion tokens finds GQA comparable to multi-head attention on most tasks and better than multi-query attention on average. The whole pretraining effort is reported at 3.3 million A100-80GB GPU hours.
Supervised fine-tuning
The first alignment step is supervised fine-tuning on annotated demonstrations. The paper reports a cosine schedule with a peak learning rate of , weight decay 0.1, batch size 64 and sequence length 4096. Annotators write both the prompt and the answer, covering helpful and safe behaviour. This is the same idea as Instruction Tuning, with Fine-Tuning as the general mechanism.
Reward modelling
The paper collects binary preferences. An annotator writes a prompt, sees two responses from different model variants sampled at different temperatures, picks one, and rates the strength of the preference on a four-point scale. Helpfulness and safety are collected under separate guidelines. Table 6 reports 2,919,326 comparisons in total, of which 1,418,091 are the paper's own Meta helpfulness and safety data, combined with open-source preference sets.
Each reward model is a language model with a scalar output head, initialised from a pretrained chat checkpoint so that it shares the policy's knowledge. It is trained with a ranking loss , where is the chosen response and the rejected one. The paper adds a margin that grows with the rating, so that clearly different pairs get more separated scores. It trains two reward models, one for helpfulness and one for safety, because the two objectives can trade off. Training runs for one epoch with a maximum learning rate of for 70B and otherwise, and an effective batch of 512 pairs, which is 1024 rows because each pair contains two responses.
Iterative RLHF
The paper runs five RLHF versions, RLHF-V1 to V5. Each uses new preference data gathered from the latest chat model, because the reward model loses accuracy on samples it was not trained on. Two algorithms are used. Rejection sampling draws outputs per prompt and keeps the one the reward model scores highest, and the paper uses it with the 70B model and then distils the result into smaller models. Proximal policy optimisation, or Proximal Policy Optimization (PPO), is applied after V4, on top of the rejection-sampled checkpoint.
The PPO reward combines the reward model score with a penalty for drifting from the starting policy:
where is the whitened, logit-transformed reward, and is 0.01 for 7B and 13B and 0.005 for 34B and 70B. The KL term is the Kullback–Leibler Divergence between the current policy and the original one. The paper says it stabilises training and reduces reward hacking. For prompts flagged as potentially unsafe, the safety reward decides the score. Otherwise the helpfulness reward does, unless the safety score is below 0.15, a threshold that gives precision 0.89 and recall 0.55 on the paper's safety test set. PPO uses a constant learning rate of , batch size 512, clip threshold 0.2, and runs for 200 to 400 iterations, with early stopping on held-out prompts. A 70B iteration takes about 330 seconds.
The paper also introduces Ghost Attention, which the paper says helps control dialogue flow over multiple turns, described in its Section 3.3. Its safety work covers safety pretraining, safety fine-tuning and red teaming in Section 4, which this lesson does not summarise.
Results
The main evaluations are human comparisons on about 4,000 single-turn and multi-turn helpfulness prompts, with three raters per prompt. The 7B chat model beats MPT-7B-chat on 60% of prompts. The 34B model has a win rate above 75% against Vicuna-33B and Falcon 40B. The 70B model wins 36% and ties 31.5% against ChatGPT, and beats PaLM-bison chat by a large margin.
Automatic judges are reported as well. Across iterations, the win rate on the paper's in-house reward models rises, and the paper says that after RLHF-V3 the models exceed ChatGPT on its in-house safety and helpfulness reward models. With GPT-4 as the judge the margin is smaller, and the paper reports a win rate above 60% for the latest chat model on its validation prompts, which are 1586 safety and 584 helpfulness prompts.
Agreement among raters is moderate. Gwet's AC2 ranges from 0.37 to 0.55 on the seven-point helpfulness scale, and it is lowest for comparisons with similar win rates. The paper treats this as expected for subjective tasks.
The paper also reports base-model results on academic benchmarks against Llama 1 and open base models such as MPT and Falcon. It says the 70B base model is on par with or better than PaLM (540B) on almost all of the benchmarks it compares.
Why it mattered
The paper gives the community open weights at sizes from 7B to 70B, a full description of the pretraining changes, and a detailed alignment recipe. It also documents its failure modes and open issues in some detail, including the limits of reward-model judging and of its human evaluation. The paper states that the models "may be a suitable substitute for closed-source models" for helpfulness and safety, based on its human evaluations.
Reading guide
Read the paper in this order. Each section needs the prerequisites listed.
- 1 Introduction. Needs Language Model and Reinforcement Learning from Human Feedback (RLHF) for the overall aim.
- 2.1 and 2.2 Pretraining. Needs Transformer, Layer Normalization, Rotary Position Embedding, Subword Tokenization and Adam Optimizer. For the grouped-query attention choice, read Grouped-Query Attention and GQA: Training Generalized Multi-Query Transformer Models first.
- 3.1 Supervised fine-tuning. Needs Fine-Tuning and Instruction Tuning.
- 3.2.1 and 3.2.2 Reward modelling. Needs Reward Model and the ranking loss in Reinforcement Learning from Human Feedback (RLHF).
- 3.2.3 Iterative fine-tuning. Needs Rejection Sampling, Proximal Policy Optimization (PPO) and Kullback–Leibler Divergence.
- 3.4 Results. Needs Training Language Models to Follow Instructions with Human Feedback for the earlier RLHF pipeline the paper builds on, to compare recipes.
- 4 Safety. Read on its own. The safety categories and red teaming need no prerequisite lessons in this graph.
Check yourself
QuestionWhich Llama 2 sizes use grouped-query attention, and why did the paper adopt it?Show answer
The 34B and 70B models use grouped-query attention. The paper adopts it for inference scalability, since the key-value cache grows with context length and batch size, and large models are the ones where that cache becomes a bottleneck.
QuestionSuppose a PPO sample has whitened reward and KL divergence 3 nats from the starting policy, and the model is 7B. What is the total reward ?Show answer
With for 7B, the penalty is , so . The numbers are illustrative, chosen to show how the penalty scales. Doubling the divergence to 6 nats would subtract 0.06 instead, which lowers the reward a little more for the same response.
QuestionWhy does each reward-model batch contain 1024 rows when the paper calls it 512 pairs?Show answer
Each preference comparison has a chosen response and a rejected response, and each response is passed through the reward model as a separate row. So 512 comparisons give 1024 rows. The distinction matters when counting compute or memory.
QuestionA team has a small budget for safety and wants the model to refuse unsafe requests without hurting helpfulness on ordinary prompts. Using the paper's design, what should they change, and what could go wrong?Show answer
The paper's design routes safety-tagged prompts to a separate safety reward model, and uses a threshold on that score for other prompts, so ordinary prompts are scored mainly by the helpfulness model. The team could keep separate safety and helpfulness reward models and mix their data as the paper does. What could go wrong is over-refusal, where the safety score is too strict and blocks harmless answers. The paper's own threshold of 0.15 was chosen against its safety test set, so the team would need to recalibrate it on their own data.