Paper · 2023-02-27 · cs.CL
LLaMA: Open and Efficient Foundation Language Models
Decoder-only transformers from 7B to 65B parameters, trained on 1.0T to 1.4T tokens of publicly available text. Pre-norm RMSNorm, SwiGLU and rotary embeddings, no absolute positions. Author-reported: LLaMA-13B beats GPT-3 (175B) on most common-sense benchmarks; LLaMA-65B is close to Chinchilla-70B and PaLM-540B.
Language model pretrainingPositional encodingLLM evaluationLLM ethicspretrainingtransformeropen-weightsscaling-laws
arXivPDFCodeMarkdown17 min read
AI-compiled by claude-haiku-5-5 on 2026-10-19, not yet reviewed by a human. Every claim cites its source; check it when a detail matters.
In plain words
Large language models are usually built by making the network as big as possible. This paper asks a different question: for a model that will be run many times after training, is it better to train a smaller model for longer? The authors train a family of models from 7 to 65 billion parameters on about a trillion tokens of text. They use only publicly available data.
The model itself is a standard text-predicting transformer with a few well-known tweaks: each block normalises its inputs rather than its outputs, the activation function is a gated one called SwiGLU, and word positions are encoded by rotating the query and key vectors instead of adding a position vector. The result is that the 13-billion-parameter model is better than GPT-3, which is more than ten times larger, on most common-sense tests, and the largest model comes close to the best closed models of its time.
The paper is mainly a recipe. The authors also list what the training cost in energy and carbon, and what biased or toxic output they measured.
TL;DR
- Decoder-only transformers with 6.7B to 65B parameters in the paper's labels (called 7B to 65B in the text), trained on 1.0T tokens (the two smaller models) or 1.4T tokens (the two larger ones) tab. 2 fig. 1.
- Pre-norm RMSNorm, SwiGLU feed-forward layers and rotary position embeddings; no absolute position embeddings §2.2.
- Training data is public web text, C4, GitHub under permissive licences, Wikipedia (20 languages), books, arXiv and Stack Exchange §2.1 tab. 1.
- Author-reported: LLaMA-13B beats GPT-3 (175B) on most common-sense benchmarks; LLaMA-65B is competitive with Chinchilla-70B and PaLM-540B §3.1 tab. 3.
- Released code covers inference only. The official repository now implements Llama 2, not this paper's exact release (see Paper vs code).
Problem
Scaling-law work fixes a training compute budget and asks which model size and data size give the lowest loss for it. That objective ignores the cost of serving the model afterwards. If a model will serve many requests, a smaller model trained longer can be cheaper to run at the same quality §1. The authors take that view and train past the point where the scaling-law recommendation stops, reporting that a 7B model keeps improving beyond 1T tokens §1.
The paper's second constraint is data: it uses only publicly available sources and avoids proprietary or undocumented corpora §1 §2.1.
Method
The method is a recipe, not a new algorithm. It has five parts: a data mix §2.1, a transformer architecture with three named modifications §2.2, an AdamW training schedule §2.3, systems optimisations for training §2.4, and an evaluation protocol that uses log-likelihood scoring for multiple-choice tasks and exact match for generation §3.
Data §2.1 tab. 1. Text is tokenised with byte-pair encoding through SentencePiece. All digits are split into single tokens, and unknown UTF-8 characters fall back to bytes §2.1, Tokenizer. Sources are sampled at fixed proportions: English CommonCrawl 67%, C4 15%, GitHub 4.5%, Wikipedia 4.5%, Books 4.5%, arXiv 2.5% and Stack Exchange 2%. English CommonCrawl is five dumps (2017 to 2020) processed by the CCNet pipeline, with a linear classifier that keeps pages resembling Wikipedia references §2.1. GitHub is restricted to Apache, BSD and MIT projects §2.1. arXiv LaTeX has everything before the first section and the bibliography removed, comments stripped, and user macros expanded §2.1. Stack Exchange uses the 28 largest sites, with answers sorted by score §2.1. About 1.4T tokens result, and most sources are seen once, while Wikipedia and Books are seen about twice §2.1.
Architecture §2.2 tab. 2. The network is a stack of identical blocks. Each block has two residual sub-layers. Both normalise their input (not their output) with RMSNorm §2.2. The attention sub-layer is causal multi-head self-attention in which rotary embeddings are applied to queries and keys at every layer §2.2. The feed-forward sub-layer is SwiGLU, whose hidden width is 2/3 of 4 times the model width, not 4 times as in PaLM §2.2. A final RMSNorm precedes the output projection to the vocabulary (the vocabulary size is not stated in the paper) model.py model.py. The paper does not state dropout, bias terms or weight tying §2.2.
# LLaMA decoder block, pre-norm with residuals [§2.2]
# Input and output: hidden (B, T, d)
def decoder_block(hidden, rope_angles, causal_mask):
# Normalise the input of the attention sub-layer [§2.2]
normed = rms_norm(hidden, attention_gain) # (B, T, d)
# Causal self-attention, residual add
# [§2.2] [code: llama/model.py#L406-L408]
hidden = hidden + attention(normed, rope_angles, causal_mask)
# Normalise the input of the feed-forward sub-layer [§2.2]
normed = rms_norm(hidden, ffn_gain) # (B, T, d)
# SwiGLU feed-forward, residual add [§2.2] [code: llama/model.py#L409]
hidden = hidden + swiglu_ffn(normed)
return hidden# RMSNorm: scale by the root mean square, no mean subtraction [§2.2]
# Epsilon is not stated in the paper; code uses 1e-5
# [code: llama/model.py#L28]
def rms_norm(x, gain):
# Compute the inverse root mean square over the last dimension [§2.2]
inv_rms = rsqrt(mean(x * x, over last dim) + 1e-5) # (B, T, 1)
# Multiply by the learned per-channel gain [code: llama/model.py#L63-L77]
return x * inv_rms * gain# Causal multi-head attention with rotary embeddings [§2.2]
# Shapes: x (B, T, d); heads H, head size d_head = d / H [tab. 2]
def attention(x, rope_angles, causal_mask):
# Project to queries, keys and values [code: llama/model.py#L273-L278]
queries = linear(x, w_q).reshape(B, T, H, d_head)
keys = linear(x, w_k).reshape(B, T, H, d_head)
values = linear(x, w_v).reshape(B, T, H, d_head)
# Rotate query and key channel pairs by position-dependent angles [§2.2]
queries, keys = apply_rope(queries, keys, rope_angles)
# Scaled dot products, with future positions masked out [§2.2]
scores = queries @ transpose(keys) / sqrt(d_head) + causal_mask
# Softmax is computed in float32 [code: llama/model.py#L298-L301]
weights = softmax(scores, in float32)
return linear(concat_heads(weights @ values), w_o)# Rotary position embedding [§2.2]
# theta = 10000 comes from the code; the paper does not state it.
def apply_rope(queries, keys, rope_angles):
# Rotate channel pairs (2i, 2i+1), not the two halves of the head
# [code: llama/model.py#L156-L161]
queries = rotate_adjacent_pairs(queries, rope_angles)
keys = rotate_adjacent_pairs(keys, rope_angles)
return queries, keys# SwiGLU feed-forward [§2.2] [code: llama/model.py#L331-L348]
# Hidden width: 2/3 * 4d, rounded up to a multiple of 256 in the code.
def swiglu_ffn(x):
# Gate and value branches, both projected from the input [§2.2]
gate = silu(linear(x, w1)) # (B, T, hidden)
value = linear(x, w3) # (B, T, hidden)
# Elementwise product, then project back to model width [§2.2]
return linear(gate * value, w2) # (B, T, d)Optimisation §2.3. AdamW with beta1 = 0.9 and beta2 = 0.95. The learning rate follows a cosine schedule that decays to 10% of its peak, with 2,000 warmup steps. Weight decay is 0.1 and gradient clipping is 1.0. Peak learning rate and batch size vary with model size tab. 2. The training code is not in the public repository, so these values cannot be checked against code.
Efficiency §2.4. The authors use a causal attention kernel from the xformers library, which does not store the masked attention weights and skips masked score computations, and they hand-write the backward pass of the transformer layers to save activation memory. Reducing memory further needs model and sequence parallelism, and the authors overlap computation with the network communication §2.4. Reported throughput for 65B is about 380 tokens per second per GPU on 2,048 A100-80GB GPUs, roughly 21 days for 1.4T tokens §2.4.
Evaluation protocol §3. Zero-shot and few-shot evaluation on 20 benchmarks. For multiple-choice tasks the model scores each option by its likelihood and picks the best. The likelihood is normalised by the number of characters for most datasets. For OpenBookQA and BoolQ it is divided by the likelihood of the option after an "Answer:" prompt, following GPT-3 §3. Question answering uses exact match after normalising the answer app. A.
Hyperparameters
Model rows from tab. 2; the paper labels these models 6.7B, 13.0B, 32.5B and 65.2B in the table and 7B, 13B, 33B and 65B in the text. Use the table values for implementation.
| Name | Value | Where | Notes |
|---|---|---|---|
| model sizes | 6.7B / 13.0B / 32.5B / 65.2B | tab. 2 | text rounds to 7B / 13B / 33B / 65B |
| layers | 32 / 40 / 60 / 80 | tab. 2 | |
| model width d | 4096 / 5120 / 6656 / 8192 | tab. 2 | |
| attention heads | 32 / 40 / 52 / 64 | tab. 2 | head size is 128 for all four (derived: d / heads) |
| peak learning rate | 3.0e-4 (6.7B, 13.0B); 1.5e-4 (32.5B, 65.2B) | tab. 2 | |
| batch size | 4M tokens | tab. 2 fig. 1 | |
| training tokens | 1.0T (6.7B, 13.0B); 1.4T (32.5B, 65.2B) | tab. 2 fig. 1 | |
| AdamW betas | beta1 0.9, beta2 0.95 | §2.3 | epsilon not stated |
| LR schedule | cosine to 10% of peak | §2.3 | |
| warmup | 2,000 steps | §2.3 | same for all sizes as written |
| weight decay | 0.1 | §2.3 | |
| gradient clipping | 1.0 | §2.3 | norm type not stated |
| dropout | not stated | ||
| context length | not stated | code default 2048 model.py | |
| vocabulary size | not stated | comes from the SentencePiece model file | |
| RMSNorm epsilon | not stated | code uses 1e-5 model.py | |
| RoPE base theta | not stated | code uses 10000.0 model.py | |
| FFN hidden width | 2/3 * 4d | §2.2 | code rounds up to a multiple of 256 model.py; for d = 4096 this gives 11008 (derived from code) |
| numeric precision | not stated | code runs RMSNorm and softmax in float32 model.py model.py | |
| code pass@1 sampling | temperature 0.1 | §3.5 tab. 8 | pass@100 and pass@80 use temperature 0.8 |
| QA decoding | greedy | app. A | output cut at first line break, final dot or comma |
| pretraining hardware (65B) | 2,048 A100-80GB | §2.4 | |
| inference hardware (13B) | one V100 GPU | §3.2 | stated for inference; no memory figures given |
Data mixture and epoch counts tab. 1. Epochs refer to the 1.4T-token runs; the 1.0T runs use the same proportions tab. 1.
| Dataset | Sampling prop. | Epochs | Disk size |
|---|---|---|---|
| CommonCrawl | 67.0% | 1.10 | 3.3 TB |
| C4 | 15.0% | 1.06 | 783 GB |
| Github | 4.5% | 0.64 | 328 GB |
| Wikipedia | 4.5% | 2.45 | 83 GB |
| Books | 4.5% | 2.23 | 85 GB |
| ArXiv | 2.5% | 1.06 | 92 GB |
| StackExchange | 2.0% | 1.03 | 78 GB |
Architecture and optimisation table tab. 2, full:
| params | dimension | n heads | n layers | learning rate | batch size | n tokens |
|---|---|---|---|---|---|---|
| 6.7B | 4096 | 32 | 32 | 3.0e-4 | 4M | 1.0T |
| 13.0B | 5120 | 40 | 40 | 3.0e-4 | 4M | 1.0T |
| 32.5B | 6656 | 52 | 60 | 1.5e-4 | 4M | 1.4T |
| 65.2B | 8192 | 64 | 80 | 1.5e-4 | 4M | 1.4T |
Results
Author-reported. Setup: zero-shot and few-shot evaluation on 20 benchmarks §3. Baseline numbers are mostly copied from the original papers of the other models, not re-run by the authors, and the paper says so for the common-sense and instruction tables §3.1 §4. Model names follow the text (7B to 65B).
Headline claims: LLaMA-13B outperforms GPT-3 on most common-sense benchmarks despite being about 10 times smaller §3.1. LLaMA-65B beats Chinchilla-70B on all reported common-sense benchmarks except BoolQ, and beats PaLM-540B everywhere except BoolQ and WinoGrande §3.1. On TriviaQA, LLaMA-65B is reported as state-of-the-art in zero-shot and few-shot settings §3.2 tab. 5.
Common-sense reasoning, zero-shot tab. 3. Bold marks the best value in the paper's table.
| Model | Params | BoolQ | PIQA | SIQA | HellaSwag | WinoGrande | ARC-e | ARC-c | OBQA |
|---|---|---|---|---|---|---|---|---|---|
| GPT-3 | 175B | 60.5 | 81.0 | - | 78.9 | 70.2 | 68.8 | 51.4 | 57.6 |
| Gopher | 280B | 79.3 | 81.8 | 50.6 | 79.2 | 70.1 | - | - | - |
| Chinchilla | 70B | 83.7 | 81.8 | 51.3 | 80.8 | 74.9 | - | - | - |
| PaLM | 62B | 84.8 | 80.5 | - | 79.7 | 77.0 | 75.2 | 52.5 | 50.4 |
| PaLM-cont | 62B | 83.9 | 81.4 | - | 80.6 | 77.0 | - | - | - |
| PaLM | 540B | 88.0 | 82.3 | - | 83.4 | 81.1 | 76.6 | 53.0 | 53.4 |
| LLaMA | 7B | 76.5 | 79.8 | 48.9 | 76.1 | 70.1 | 72.8 | 47.6 | 57.2 |
| LLaMA | 13B | 78.1 | 80.1 | 50.4 | 79.2 | 73.0 | 74.8 | 52.7 | 56.4 |
| LLaMA | 33B | 83.1 | 82.3 | 50.4 | 82.8 | 76.0 | 80.0 | 57.8 | 58.6 |
| LLaMA | 65B | 85.3 | 82.8 | 52.3 | 84.2 | 77.0 | 78.9 | 56.0 | 60.2 |
Natural Questions, exact match tab. 4. Test split used for open-domain QA with 3,610 questions app. A.
| Model | Params | 0-shot | 1-shot | 5-shot | 64-shot |
|---|---|---|---|---|---|
| GPT-3 | 175B | 14.6 | 23.0 | - | 29.9 |
| Gopher | 280B | 10.1 | - | 24.5 | 28.2 |
| Chinchilla | 70B | 16.6 | - | 31.5 | 35.5 |
| PaLM | 8B | 8.4 | 10.6 | - | 14.6 |
| PaLM | 62B | 18.1 | 26.5 | - | 27.6 |
| PaLM | 540B | 21.2 | 29.3 | - | 39.6 |
| LLaMA | 7B | 16.8 | 18.7 | 22.0 | 26.1 |
| LLaMA | 13B | 20.1 | 23.4 | 28.1 | 31.9 |
| LLaMA | 33B | 24.9 | 28.3 | 32.9 | 36.0 |
| LLaMA | 65B | 23.8 | 31.0 | 35.0 | 39.9 |
TriviaQA, exact match on the filtered dev set tab. 5. Not comparable to GPT-3 and PaLM, which report on the unfiltered test set app. A.
| Model | Params | 0-shot | 1-shot | 5-shot | 64-shot |
|---|---|---|---|---|---|
| Gopher | 280B | 43.5 | - | 57.0 | 57.2 |
| Chinchilla | 70B | 55.4 | - | 64.1 | 64.6 |
| LLaMA | 7B | 50.0 | 53.4 | 56.3 | 57.6 |
| LLaMA | 13B | 56.6 | 60.5 | 63.1 | 64.0 |
| LLaMA | 33B | 65.1 | 67.9 | 69.9 | 70.4 |
| LLaMA | 65B | 68.2 | 71.6 | 72.6 | 73.0 |
Reading comprehension, zero-shot accuracy tab. 6.
| Model | Params | RACE-middle | RACE-high |
|---|---|---|---|
| GPT-3 | 175B | 58.4 | 45.5 |
| PaLM | 8B | 57.9 | 42.3 |
| PaLM | 62B | 64.3 | 47.5 |
| PaLM | 540B | 68.1 | 49.1 |
| LLaMA | 7B | 61.1 | 46.9 |
| LLaMA | 13B | 61.6 | 47.2 |
| LLaMA | 33B | 64.1 | 48.3 |
| LLaMA | 65B | 67.9 | 51.6 |
Mathematical reasoning tab. 7. maj1@k is majority voting over k samples: k = 256 for MATH and k = 100 for GSM8k. Minerva is PaLM fine-tuned on mathematical text; LLaMA is not fine-tuned on mathematical data §3.4.
| Model | Params | MATH | MATH maj1@k | GSM8k | GSM8k maj1@k |
|---|---|---|---|---|---|
| PaLM | 8B | 1.5 | - | 4.1 | - |
| PaLM | 62B | 4.4 | - | 33.0 | - |
| PaLM | 540B | 8.8 | - | 56.5 | - |
| Minerva | 8B | 14.1 | 25.4 | 16.2 | 28.4 |
| Minerva | 62B | 27.6 | 43.4 | 52.4 | 68.5 |
| Minerva | 540B | 33.6 | 50.3 | 68.5 | 78.5 |
| LLaMA | 7B | 2.9 | 6.9 | 11.0 | 18.1 |
| LLaMA | 13B | 3.9 | 8.8 | 17.8 | 29.3 |
| LLaMA | 33B | 7.1 | 15.2 | 35.6 | 53.1 |
| LLaMA | 65B | 10.6 | 20.5 | 50.9 | 69.7 |
Code generation tab. 8. HumanEval is zero-shot. MBPP uses 3-shot prompts. pass@1 is sampled at temperature 0.1; pass@100 and pass@80 at 0.8 §3.5. Values marked with an asterisk are read from a figure in the PaLM paper.
| Model | Params | HumanEval @1 | HumanEval @100 | MBPP @1 | MBPP @80 |
|---|---|---|---|---|---|
| LaMDA | 137B | 14.0 | 47.3 | 14.8 | 62.4 |
| PaLM | 8B | 3.6* | 18.7* | 5.0* | 35.7* |
| PaLM | 62B | 15.9 | 46.3* | 21.4 | 63.2* |
| PaLM-cont | 62B | 23.7 | - | 31.2 | - |
| PaLM | 540B | 26.2 | 76.2 | 36.8 | 75.0 |
| LLaMA | 7B | 10.5 | 36.5 | 17.7 | 56.2 |
| LLaMA | 13B | 15.8 | 52.5 | 22.0 | 64.0 |
| LLaMA | 33B | 21.7 | 70.7 | 30.2 | 73.4 |
| LLaMA | 65B | 23.7 | 79.3 | 37.7 | 76.8 |
MMLU, 5-shot accuracy, by domain tab. 9. The per-subject table for the 57 tasks is tab. 16 in the appendix; its rows are not reproduced here.
| Model | Params | Humanities | STEM | Social Sciences | Other | Average |
|---|---|---|---|---|---|---|
| GPT-NeoX | 20B | 29.8 | 34.9 | 33.7 | 37.7 | 33.6 |
| GPT-3 | 175B | 40.8 | 36.7 | 50.4 | 48.8 | 43.9 |
| Gopher | 280B | 56.2 | 47.4 | 71.9 | 66.1 | 60.0 |
| Chinchilla | 70B | 63.6 | 54.9 | 79.3 | 73.9 | 67.5 |
| PaLM | 8B | 25.6 | 23.8 | 24.1 | 27.8 | 25.4 |
| PaLM | 62B | 59.5 | 41.9 | 62.7 | 55.8 | 53.7 |
| PaLM | 540B | 77.0 | 55.6 | 81.0 | 69.6 | 69.3 |
| LLaMA | 7B | 34.0 | 30.5 | 38.3 | 38.1 | 35.1 |
| LLaMA | 13B | 45.0 | 35.8 | 53.8 | 53.3 | 46.9 |
| LLaMA | 33B | 55.8 | 46.0 | 66.7 | 63.4 | 57.8 |
| LLaMA | 65B | 61.8 | 51.7 | 72.9 | 67.4 | 63.4 |
Instruction tuning, MMLU 5-shot tab. 10. One short fine-tuning run on instruction data following Chung et al.; the authors say this is not the focus of the paper §4.
| Model | Params | MMLU |
|---|---|---|
| OPT | 30B | 26.1 |
| GLM | 120B | 44.8 |
| PaLM | 62B | 55.1 |
| PaLM-cont | 62B | 62.8 |
| Chinchilla | 70B | 67.5 |
| LLaMA | 65B | 63.4 |
| OPT-IML-Max | 30B | 43.2 |
| Flan-T5-XXL | 11B | 55.1 |
| Flan-PaLM | 62B | 59.6 |
| Flan-PaLM-cont | 62B | 66.1 |
| LLaMA-I | 65B | 68.9 |
Bias, toxicity and truthfulness §5. Table 11 is RealToxicityPrompts with greedy decoding over 100k prompts, scored by a third-party API; higher is more toxic. Table 12 is CrowS-Pairs in zero-shot; higher means the model prefers the stereotype more often. Table 13 is WinoGender co-reference accuracy. Table 14 is TruthfulQA tab. 11 tab. 12 tab. 13 tab. 14.
| Model | Basic | Respectful |
|---|---|---|
| LLaMA 7B | 0.106 | 0.081 |
| LLaMA 13B | 0.104 | 0.095 |
| LLaMA 33B | 0.107 | 0.087 |
| LLaMA 65B | 0.128 | 0.141 |
| Category | LLaMA-65B | GPT-3 | OPT-175B |
|---|---|---|---|
| Gender | 70.6 | 62.6 | 65.7 |
| Religion | 79.0 | 73.3 | 68.6 |
| Race/Color | 57.0 | 64.7 | 68.6 |
| Sexual orientation | 81.0 | 76.2 | 78.6 |
| Age | 70.1 | 64.4 | 67.8 |
| Nationality | 64.2 | 61.6 | 62.9 |
| Disability | 66.7 | 76.7 | 76.7 |
| Physical appearance | 77.8 | 74.6 | 76.2 |
| Socioeconomic status | 71.5 | 73.8 | 76.2 |
| Average | 66.6 | 67.2 | 69.5 |
| Pronoun set | 7B | 13B | 33B | 65B |
|---|---|---|---|---|
| All | 66.0 | 64.7 | 69.0 | 77.5 |
| her/her/she | 65.0 | 66.7 | 66.7 | 78.8 |
| his/him/he | 60.8 | 62.5 | 62.1 | 72.1 |
| their/them/someone | 72.1 | 65.0 | 78.3 | 81.7 |
| her/her/she (gotcha) | 64.2 | 65.8 | 61.7 | 75.0 |
| his/him/he (gotcha) | 55.0 | 55.8 | 55.8 | 63.3 |
| Model | Truthful | Truthful*Informative |
|---|---|---|
| GPT-3 1.3B | 0.31 | 0.19 |
| GPT-3 6B | 0.22 | 0.19 |
| GPT-3 175B | 0.28 | 0.25 |
| LLaMA 7B | 0.33 | 0.29 |
| LLaMA 13B | 0.47 | 0.41 |
| LLaMA 33B | 0.52 | 0.48 |
| LLaMA 65B | 0.57 | 0.53 |
Carbon footprint §6 tab. 15. Estimated, not measured. The formula is energy = GPU-hours × 400 W × PUE 1.1, and emissions = MWh × 0.385 kg CO2e per kWh (US average), the same assumptions applied to OPT and BLOOM. The total for developing the models is estimated at about 2,638 MWh and 1,015 tCO2eq §6. Equations in the paper are unnumbered, so they are cited by section.
| Model | GPU | GPU-hours | Energy | tCO2eq |
|---|---|---|---|---|
| OPT-175B | A100-80GB | 809,472 | 356 MWh | 137 |
| BLOOM-175B | A100-80GB | 1,082,880 | 475 MWh | 183 |
| LLaMA-7B | A100-80GB | 82,432 | ~36 MWh | ~14 |
| LLaMA-13B | A100-80GB | 135,168 | ~59 MWh | ~23 |
| LLaMA-33B | A100-80GB | 530,432 | 233 MWh | ~90 |
| LLaMA-65B | A100-80GB | 1,022,362 | 449 MWh | 173 |
Figures are described in words, as the wiki does not carry images yet. fig. 1 plots training loss over tokens for the four models; the 33B and 65B runs use 1.4T tokens and the smaller two use 1.0T, all at 4M-token batches fig. 1. fig. 2 tracks question answering and common-sense scores during training: most rise steadily and track training perplexity, the exceptions are SIQA, which varies a lot between checkpoints, and WinoGrande, where 33B and 65B perform alike during training §3.7. fig. 3 shows the 1-shot prompt format used for NaturalQuestions and TriviaQA, with a fixed "Answer these questions:" prefix app. A.
Paper vs code
The official repository is https://github.com/facebookresearch/llama at
689c7f261b9c5514636ecc3c5fefefcbb3e6eed7. It is linked from the paper's
abstract. Its README marks the repository as deprecated and says it targets
Llama 2 README.md. The commit therefore does not match the release
described here. Only inference code is present: llama/model.py,
llama/generation.py and llama/tokenizer.py. Training code, data pipeline
and the paper's LLaMA v1 weights are not in the repository. Comparisons
below cover the architecture only.
- Training hyperparameters (AdamW betas, warmup, clipping, schedule) cannot be checked; no training code is in the repository §2.3.
- Efficient attention: the paper names an xformers causal-attention kernel §2.4. The repository computes attention with a plain matrix product, a softmax and an explicit mask model.py. The kernel is not in the repository.
- RoPE pairing: the code rotates adjacent channel pairs model.py. The paper says only "rotary embeddings" §2.2.
- RoPE base: the code uses theta = 10000 model.py. The paper does not state it.
- FFN width: the paper gives 2/3 of 4d §2.2. The code rounds up to a multiple of 256 model.py. For the 6.7B model this gives 11008 (derived from code).
- Grouped key/value heads: the code accepts a separate number of key/value heads and defaults to the number of query heads model.py model.py. The paper does not describe this option, and its Table 2 shows no separate key/value count tab. 2.
- Output projection: the code keeps a separate output layer and has no bias terms in any linear layer model.py [code: llama/model.py#L446-L448]. The paper does not say whether the output and embedding weights are tied, or whether biases exist.
- Context length: the code default is 2048 tokens model.py, and the RoPE table covers twice that, with a comment about a 4096-token limit for Llama 2 model.py. The paper does not state a context length.
- Sampling defaults: the code uses temperature 0.6 and top-p 0.9 [code: llama/generation.py#L134-L135]. The paper evaluates QA with greedy decoding app. A and code pass@1 at temperature 0.1 §3.5. The code defaults are not the paper's evaluation settings.
- Tokenizer: the code loads a SentencePiece model file and reads the vocabulary size from it tokenizer.py. The model file is not in the repository, so the vocabulary size stays unknown here.
License of the code. The repository's LICENSE is the Llama 2 Community License Agreement, which is custom and not MIT, Apache-2.0 or BSD. It includes additional commercial terms above 700 million monthly active users [LICENSE §2]. Under our rules no verbatim code is quoted from this repository; the pseudocode above is written from the code with citations only.
The causal mask, the pre-norm placement and the residual layout match the paper's description §2.2 model.py [code: llama/model.py#L457-L495].
Pitfalls
- Model names differ between the table and the text: 6.7B, 13.0B, 32.5B and 65.2B in tab. 2, and 7B, 13B, 33B and 65B in the text. The table values (width, layers, heads, learning rate, tokens) are the ones to implement.
- The 33B and 65B learning rate is half the 6.7B and 13B rate, and the training budget is 1.4T rather than 1.0T tokens tab. 2. Using one value for all sizes is a common mistake.
- Epoch counts in tab. 1 refer to the 1.4T-token runs. Wikipedia and Books are seen about twice, and the others about once §2.1.
- RMSNorm epsilon, RoPE base, numeric precision, dropout and vocabulary size are not in the paper. The code gives values for epsilon, theta and precision model.py model.py [code: llama/model.py#L76]. Vocabulary size is not given in the repository.
- The SwiGLU hidden width is 2/3 of 4d before rounding §2.2. The code then rounds up to a multiple of 256; for d = 4096 that moves 10922 to 11008 (derived from code). Match the code if reproducing the released shapes model.py.
- Rotary embeddings in the code rotate adjacent pairs, not split halves model.py. Other codebases use the split-half convention, which changes the attention scores relative to the code.
- The KV cache is allocated up front for the maximum batch size and sequence
length, on the GPU, for every layer model.py. Memory
is reserved even for short prompts. The code calls
.cuda()directly, so it does not run on a CPU-only machine as written. - TriviaQA is evaluated on the filtered dev set, not the unfiltered test set that GPT-3 and PaLM use, so the numbers are not directly comparable app. A.
- Multiple-choice scoring uses different normalisation for OpenBookQA and BoolQ than for the rest §3. Implement both or the scores will not match the table.
- Most baseline numbers are copied from other papers, under their own protocols §3.1 §4. Some PaLM code numbers are read from a figure tab. 8. Do not treat the table as a single controlled comparison.
- LLaMA-I comes from a single instruction-tuning experiment, which the authors describe as not the focus of the paper §4. Treat its MMLU number as a single data point.
- Toxicity scores rely on a third-party API the authors do not control, so they are hard to compare with other papers §5.1.
- The authors say their bias and toxicity evaluations are not sufficient to show the risks of the model §5. They single out religion as the category where the model is most biased against OPT-175B, followed by age and gender §5.2 tab. 12.
- Carbon numbers are estimates from an assumed power draw and grid intensity, not measured energy §6.
- Training throughput (380 tokens per second per GPU on 2,048 A100s) implies about 21 days for 1.4T tokens §2.4. Plan compute from this, not from the parameter count alone.
Lineage
- Builds on: GPT-3 style pre-training §2.1 §2.2; pre-normalisation, with the GPT-3 and GPTNeo references the paper cites §2.2; RMSNorm §2.2; SwiGLU §2.2; rotary embeddings §2.2; the causal attention approach the paper credits to rabe2021self, and the backward pass from dao2022flashattention §2.4.
- Scaling-law basis: Chinchilla's training-compute-optimal recipe, which the paper deliberately departs from to optimise for inference cost §1 §7.
- Compared against: GPT-3, Gopher, Chinchilla, PaLM, OPT, GPT-J, GPT-Neo, GPT-NeoX, GLM, OPT-IML, Flan-PaLM, Minerva, LaMDA §3 tab. 3 tab. 9.
- Superseded by: none recorded on this wiki yet.
Source and license
Adapted from “LLaMA: Open and Efficient Foundation Language Models” by Hugo Touvron, Thibaut Lavril, Gautier Izacard, Xavier Martinet, Marie-Anne Lachaux, Timothée Lacroix, Baptiste Rozière, Naman Goyal, Eric Hambro, Faisal Azhar, Aurelien Rodriguez, Armand Joulin, Edouard Grave, Guillaume Lample (arXiv:2302.13971), used under CC BY 4.0. Changes: condensed and restructured for implementation, with tables, equations and algorithms taken from the paper and its code, each cited to its source. Not affiliated with or endorsed by the authors. Code citations refer to https://github.com/facebookresearch/llama at commit 689c7f261b9c (Llama 2 Community License Agreement (custom, not MIT/Apache-2.0/BSD; see Paper vs code)). Any quoted code is shown under that license.