Paper · 2024-07-31 · cs.CL, cs.AI
Gemma 2: Improving Open Language Models at a Practical Size
Gemma 2 is a family of 2B, 9B and 27B decoder-only models that alternate local sliding-window and global attention, soft-cap logits and use GQA. The 2B and 9B models are trained by distilling a larger teacher. Author-reported: 27B is best in its size class on the HF suite and 9B averages 70.2 on eight benchmarks.
Knowledge distillationGrouped-query attention (GQA)Attention mechanismLocal-global attentionlanguage-modelsopen-weightsknowledge-distillationsliding-window-attentiongrouped-query-attentionlogit-soft-cappingpretraining
arXivPDFCodeMarkdown12 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
Gemma 2 is a family of open language models in three sizes: about 2, 9 and 27 billion parameters. The authors keep the basic Transformer design but change a few parts of it, and they train the two smaller models differently. Instead of learning only from the single correct next word, the small models learn from the full set of word probabilities produced by a larger model. That gives each training step richer feedback.
The architecture changes are small. Attention alternates between a local layer that looks only at nearby text and a global layer that looks at the whole context. Attention scores are squashed by a smooth cap so they cannot grow without bound, and several query heads share one set of key and value heads to save memory when generating text. The authors report that the models beat other open models of similar size and sometimes compete with models about twice as large. They release the weights.
TL;DR
Gemma 2 (2B, 9B, 27B) is a decoder-only Transformer with alternating local sliding-window (4096) and global attention layers, logit soft-capping and grouped-query attention with two query heads per key/value head. The 2B and 9B models are trained by distilling a larger teacher's token distributions; the 27B model is trained from scratch. Author-reported results place the 27B model near LLaMA-3 70B on the HuggingFace suite and the 9B model at 70.2 average on eight benchmarks. Use this card when you need the architecture details or want to know where the paper is thin on training recipe.
Problem
Small open models were improving mainly by training on more tokens, and the authors note that this gain scales only logarithmically with data size §1. They ask whether small models can do better per training step by receiving a richer training signal, and whether a few known architecture changes help at this size §1. The distillation ablations use a 2B model and smaller models (200M to 1B parameters) §5 tab. 6 tab. 7; the architecture ablations use 9B §5 tab. 8 tab. 9 tab. 10.
Method
Architecture §2 tab. 1. Decoder-only Transformer. The main changes from Gemma 1 are:
- Local and global attention alternate, one layer each, with the local sliding window set to 4096 tokens and the global span set to 8192 §2.
- Logit soft-capping in every attention layer and in the final output layer,
with
soft_cap = 50.0for attention and30.0for the final layer §2. - RMSNorm applied to the input and output of both the attention and the feed-forward sub-layers (pre- and post-norm) §2.
- Grouped-query attention with
num_groups = 2, chosen from ablations that showed faster inference with no clear loss §2 §5 tab. 8. - Rotary position embeddings and the approximated GeGLU activation, both carried over from Gemma 1 §2.
- A 256k-entry SentencePiece vocabulary shared by input and output embeddings (tied embeddings) §3.1 tab. 1.
# Gemma 2 attention for one layer [§2]
# Input: queries (B, T, H, d); keys, values (B, S, K, d), K = H / 2
# Output: attended values (B, T, H, d)
def attention_layer(queries, keys, values, is_local):
# Scale queries by a constant; the 27B value differs (see Pitfalls) [§2]
queries = queries * query_scale
# Every two query heads share one key/value head [§2] [tab. 1]
logits = grouped_scores(queries, keys) # (B, T, H, S)
# Soft-cap attention logits to (-50, 50) [§2]
logits = 50.0 * tanh(logits / 50.0)
# Causal mask; local layers also drop keys beyond 4096 tokens [§2]
mask = causal_mask(T, S)
if is_local:
mask = mask and within_window(T, S, 4096)
logits = where(mask, logits, -inf)
# Normalize over keys and mix the values [§2]
weights = softmax(logits, axis=-1)
return grouped_mix(weights, values) # (B, T, H, d)# Layer schedule [§2] [code: gemma/gm/nn/_gemma.py#L60-L64]
# The paper says local and global layers alternate; the code starts local.
def layer_types(num_layers):
# Even-indexed layers local, odd-indexed layers global [code]
return ["local" if i % 2 == 0 else "global" for i in range(num_layers)]Knowledge distillation §3.2. For the 2B and 9B models the student is trained to match the teacher's distribution over the next token given the same context, not the one-hot label. The objective is the cross-entropy between the teacher and student distributions, summed over the vocabulary §3.2.
# Distillation loss for one next-token position [§3.2]
# Input: teacher_logits (V,), student_logits (V,); V = 256128
# Output: scalar loss
def distillation_loss(teacher_logits, student_logits):
# Teacher probability of every vocabulary token [§3.2]
teacher_probs = softmax(teacher_logits)
# Student log-probability of every vocabulary token [§3.2]
student_log_probs = log_softmax(student_logits)
# Cross-entropy: minimize -sum_x P_T(x) log P_S(x) [§3.2]
return -sum(teacher_probs * student_log_probs)Pre-training data and budget §3.1 §3.3 tab. 3. Tokens: 13T for 27B, 8T for 9B, 2T for 2B, mostly English web, code and science text §3.1. Data mixture was chosen by ablations similar to Gemini 1.0 §3.1. Data filtering matches Gemma 1 and includes decontamination of evaluation sets §3.1. Training used TPUv5e (2B), TPUv4 (9B) and TPUv5p (27B) with the sharding given in tab. 3. The optimizer state is sharded in a way the authors describe as similar to ZeRO-3 §3.3. The paper reports 1247.61 tCO2eq for pre-training §3.4.
Post-training §4. Supervised fine-tuning on synthetic and human prompt
and response pairs, with some distillation from the teacher on the student's
own outputs; then RLHF with a reward model that is "an order of magnitude
larger than the policy"; then averaging of models produced by several
hyperparameter settings §4. Data filtering removes personal information,
unsafe or toxic outputs, self-identification mistakes and duplicates §4.
Control tokens are the Gemma 1 set, with a new turn format: the model ends
each turn with <end_of_turn> then <eos> §4 tab. 4 tab. 5.
Hyperparameters
| Name | Value | Where | Notes |
|---|---|---|---|
| Layers (2B / 9B / 27B) | 26 / 42 / 46 | tab. 1 | |
| d_model (2B / 9B / 27B) | 2304 / 3584 / 4608 | tab. 1 | |
| Query heads (2B / 9B / 27B) | 8 / 16 / 32 | tab. 1 | |
| KV heads (2B / 9B / 27B) | 4 / 8 / 16 | tab. 1 §2 | two query heads per KV head |
| Head size (2B / 9B / 27B) | 256 / 256 / 128 | tab. 1 | |
| Feed-forward width (2B / 9B / 27B) | 18432 / 28672 / 73728 | tab. 1 | paper's number is the fused gate+up width, see Pitfalls |
| Activation | GeGLU (approximate) | §2 tab. 1 | |
| Position encoding | RoPE | §2 | base and scaling not stated |
| Context length | 8192 tokens | §2 | |
| Local sliding window | 4096 tokens | §2 tab. 1 | |
| Global attention span | 8192 tokens | tab. 1 §2 | |
| Attention logit soft-cap | 50.0 | §2 | |
| Final logit soft-cap | 30.0 | §2 | |
| Norm | RMSNorm, pre and post each sub-layer | §2 | epsilon not stated |
| Vocabulary | 256128 entries, tied embeddings | tab. 1 tab. 2 §3.1 | |
| Embedding parameters (2B / 9B / 27B) | 590,118,912 / 917,962,752 / 1,180,237,824 | tab. 2 | |
| Non-embedding parameters (2B / 9B / 27B) | 2,024,517,888 / 8,324,201,984 / 26,047,480,320 | tab. 2 | |
| Pre-training tokens (2B / 9B / 27B) | 2T / 8T / 13T | §3.1 | |
| Teacher for 9B distillation | 27B | §5 | stated as the ratio target in the ablation |
| Teacher for 2B distillation | not stated | §3.2 | ablation uses a 7B teacher §5 tab. 6 |
| Distillation: temperature, top-k, token budget per step | not stated | §3.2 | |
| Optimizer, learning rate, batch size, warmup, schedule | not stated | §3 | |
| Post-training hyperparameters | not stated ("tuned") | §4 | |
| Reward model size | "an order of magnitude larger than the policy" | §4 | exact size not stated |
| Hardware (2B / 9B / 27B) | TPUv5e 512 / TPUv4 4096 / TPUv5p 6144 chips | tab. 3 §3.3 | |
| Sharding (data / model), 2B / 9B / 27B | 512 / 1 ; 1024 / 4 ; 768 / 8 | tab. 3 §3.3 | |
| Sliding window at inference (9B ablation) | 4096, 2048, 1024 (changeable) | tab. 10 |
Results
Author-reported. Benchmark setups are listed in the tables cited. Baselines come from the authors' own evaluations, the HuggingFace leaderboard or the LLaMA-3 report, as noted in the paper tab. 12 tab. 13.
| Setting | Metric | Baseline | This paper | Source |
|---|---|---|---|---|
| 27B vs LLaMA-3 70B and Qwen1.5 32B, HF suite | MMLU | 79.2 (LLaMA-3 70B), 74.3 (Qwen1.5 32B) | 75.2 | tab. 12 |
| same | GSM8K | 76.9, 61.1 | 74.0 | tab. 12 |
| same | ARC-c | 68.8, 63.6 | 71.4 | tab. 12 |
| same | HellaSwag | 88.0, 85.0 | 86.4 | tab. 12 |
| same | Winogrande | 85.3, 81.5 | 83.7 | tab. 12 |
| 2B / 9B / 27B, average of 8 benchmarks | average (8) | LLaMA-3 8B 61.9; Mistral 7B 61.0; Gemma 1 7B 62.4 | 50.0 (2B), 70.2 (9B), 74.4 (27B) | tab. 13 |
| 2B / 9B / 27B, average of all benchmarks | average (all) | Gemma 1 7B 57.9; Mistral 7B 55.6 | 48.7 (2B), 64.9 (9B), 69.4 (27B) | tab. 13 |
| 9B | MMLU, 5-shot | Gemma 1 7B 64.4; LLaMA-3 8B 66.6 | 71.3 | tab. 13 |
| 9B | MATH, 4-shot | Gemma 1 7B 24.3 | 36.6 | tab. 13 |
| 27B | MATH, 4-shot | not compared | 42.3 | tab. 13 |
| 2B vs Gemma 1 2B | HumanEval, pass@1 | 22.0 (Gemma 1 2B) | 20.1 | tab. 13 |
| 2B vs Gemma 1 2B | MMLU, 5-shot | 42.3 | 52.2 | tab. 13 |
| Chatbot Arena, instruction-tuned | Elo | Llama 3 70B 1206 | 27B 1218 (CI +4/-3); 9B 1187 (+3/-5); 2B 1126 (+10/-10) | tab. 14 |
| Human side-by-side, safety | win-loss vs GPT-4o | Gemma 1.1 7B: 42.8% | 2B 57.5%, 9B 57.8%, 27B 55% | tab. 15 |
| Human side-by-side, instruction following | % prompts fully followed | Gemma 1.1 7B: 24.3% | 2B 26.5%, 9B 34.1%, 27B 37.7% | tab. 15 |
| 500 multi-turn scenarios | user satisfaction (1-5) | Gemma 1.1 7B: 3.32 | 2B 3.64, 9B 4.04, 27B 4.20 | tab. 16 |
| Distillation vs scratch, 2B, 500B tokens | average (3 benchmarks) | 60.3 (from scratch) | 67.7 (distilled from 7B) | tab. 6 |
| Distillation vs scratch, validation perplexity | 200M / 400M / 1B params | 23 / 19 / 17 (scratch) | 21 / 17 / 15 (distilled) | tab. 7 |
| GQA vs MHA, 9B | average (4 benchmarks) | 50.3 (MHA) | 50.8 (GQA) | tab. 8 |
| Deep vs wide, 9B | average (4 benchmarks) | 50.8 (wide) | 52.0 (deep) | tab. 9 |
| Memorization, exact match | verbatim memorization rate | prior models of similar size (values in fig. 1, log scale) | below 0.1% | §7 fig. 1 |
Selected extra numbers. Pre-trained vs instruction-tuned (MMLU): 2B 52.2 to 56.1, 9B 71.3 to 72.3, 27B 75.2 to 76.2 tab. 17. MBPP: 30.2 to 36.6, 52.4 to 59.2, 62.6 to 67.4 tab. 17. Safety benchmarks (lower is better for RealToxicity, CrowS-Pairs, Toxigen; higher is better for others) are in tab. 18. Offensive-security CTF: 27B solves 34 of 76 InterCode-CTF tasks, 1 of 13 internal CTFs and 0 of 13 Hack the Box tasks tab. 19. None of this is independently verified.
Paper vs code
Searched: the paper links only google-deepmind/dangerous-capability-evaluations
§8.4 and a Gemma repository is not linked. A GitHub search of the
google-deepmind organization found google-deepmind/gemma, the official
Gemma library (Apache-2.0). It is the JAX inference and model code at commit
9e1f74131ba9d45e5fe6ae7bb3941d9f2abb39c3, read below. The repository has
since gained Gemma 3 and Gemma 4 code, which we do not use here. The repository
contains model definitions and sampling only. No training, distillation or
RLHF code is present, so those parts can be checked only against the paper.
Model parameters match tab. 1 for head counts, KV heads, head size, softcaps and window: Gemma 2 2B uses 8 heads, 4 KV heads, head size 256 [code: gemma/gm/nn/_gemma.py#L52-L70]; 9B uses 16/8/256 [code: gemma/gm/nn/_gemma.py#L81-L100]; 27B uses 32/16/128 [code: gemma/gm/nn/_gemma.py#L111-L130]. Layer counts are 26, 42 and 46 [code: gemma/gm/nn/_gemma.py#L29-L31].
Differences and conventions, each with both citations:
- Feed-forward width: paper gives 18432 / 28672 / 73728 tab. 1; code sets
hidden_dimto 9216 / 14336 / 36864 _gemma.py _gemma.py _gemma.py. The gating weight has a leading dimension of 2 _modules.py, so the paper's figure is twice the per-branch width. Both describe the same model. - 27B query scaling: paper does not say. Code uses
(embed_dim / num_heads)^-0.5for 27B, i.e. 144^-0.5, nothead_dim^-0.5used for 2B and 9B _gemma.py _config.py. The 27B head size is 128 tab. 1, so the two choices differ. - Layer order: paper says "every other layer" with no starting point §2. Code alternates local first, then global _gemma.py.
- Attention soft-cap and mask order: code applies the cap to the raw scores before masking _modules.py. The paper does not state an order.
- Sliding-window mask: code's
create_sliding_maskuses two bounds, keys with position greater thanpos - Wand less thanpos + W_modules.py. The paper describes only a window of 4096. Under a causal mask the upper bound adds no restriction (inference, not checked by running the code). - Final soft-cap: paper states 30.0 §2; code divides by 30, applies tanh and multiplies back _transformer.py.
- Attention soft-cap in code is 50.0 for all three sizes [code: gemma/gm/nn/_gemma.py#L68] _gemma.py _gemma.py, matching §2.
- Text vs table: §6.1 says "Qwen1.5 34B" §6.1 while the table caption says 32B tab. 12. §6.2 writes "Gemma 2.6B" for the 2B model §6.2. Use the table values and treat these as typos.
Pitfalls
- Feed-forward width is fused. Copy the paper's width as the gate+up width, so the per-branch hidden size is half of it. Doubling it by mistake doubles the feed-forward parameters.
- 27B query scale. Use
1/sqrt(d_model / num_heads)for 27B and1/sqrt(head_dim)for 2B and 9B, as the code does. The paper does not state this, so an implementer following only the paper would pick the 2B/9B rule. - Layer ordering is unstated in the paper. Code puts a local layer first. Weights are indexed by layer, so the ordering decides which parameters apply to which layer. Follow the code, not the paper's wording.
- Distillation details are missing. The paper gives only the cross-entropy over the full vocabulary §3.2. Temperature, top-k truncation, whether teacher logits are capped, and what the student's training data is are not stated.
- 2B teacher is unnamed. The only teacher size given is 7B in the ablation §5 and 27B for 9B by ratio §5. Do not assume the teacher for 2B.
- Training hyperparameters are absent. Learning rate, batch size, optimizer settings, warmup, schedule and sequence packing are not stated anywhere in the paper. The appendix is empty in the source. There is no training code in the repository, so these cannot be recovered.
- Post-training values are absent. SFT, RLHF and model-averaging settings are described as tuned but no values are given §4.
- GQA naming. The paper calls the setting
num_groups = 2§2. In code the KV head count is half the query head count in all three models _gemma.py, which means two query heads per KV head. - Caps are not ablated. The paper sets the attention cap to 50 and the final cap to 30 §2 but does not report an ablation that removes or changes either one, so their effect on quality is not stated.
- Window at inference. The window can be shrunk at inference with small perplexity change, but this was measured only on the 9B model and on one validation set tab. 10. Results for other sizes and tasks are not stated.
- Baseline protocols differ. Several baselines are copied from the HuggingFace leaderboard or from LLaMA-3's report, and the paper notes a +3% to +4% change in some baseline scores under LLaMA-3's evaluation protocol tab. 13 (footnotes). Cross-model comparisons mix protocols.
- Not uniform gains. 2B HumanEval is lower than Gemma 1 2B (20.1 vs 22.0) tab. 13. Gains are not on every benchmark.
- Arena and human studies. Elo numbers are from the Chatbot Arena and the side-by-side studies use the authors' own prompt sets tab. 14 tab. 15. Treat them as author-reported.
- Memorization. The paper uses a narrow definition: a model can be prompted to generate near-copies of training examples §7. The test is 50-token memorization given a 50-token prompt, with exact and approximate (10% edit distance) matching §7. The personal-data check uses an automated tool that the authors say produces false positives, so the rates may be overestimates §7.
- CBRN. The CBRN section reports only qualitative findings ("knowledge in these domains is low") and gives no numbers §8.4. Do not cite a CBRN number from this card.
Lineage
- Builds on: Gemma 1 (cited as gemmateam2024gemma in the paper, not in this wiki); local sliding-window attention and global attention, both cited to Beltagy et al., 2020 (Longformer) §2; grouped-query attention, Ainslie et al., 2023 §2; knowledge distillation, Hinton et al., 2015 §3.2; RMSNorm §2; RoPE §2; GeGLU §2; logit capping, Bello et al., 2016 §2.
- Compared against: Qwen1.5 32B and LLaMA-3 70B (HF suite) tab. 12; LLaMA-3 8B, Mistral 7B and Gemma 1 2B/7B tab. 13; GPT-4o and other chat models in the Arena tab. 14 and in human studies tab. 15.
- Superseded by: not stated in the wiki.
Source and license
Adapted from “Gemma 2: Improving Open Language Models at a Practical Size” by Gemma Team, Morgane Riviere, Shreya Pathak, Pier Giuseppe Sessa, Cassidy Hardin, Surya Bhupatiraju, Léonard Hussenot, Thomas Mesnard, Bobak Shahriari, Alexandre Ramé, Johan Ferret, Peter Liu, Pouya Tafti, Abe Friesen, Michelle Casbon, Sabela Ramos, Ravin Kumar, Charline Le Lan, Sammy Jerome, Anton Tsitsulin, Nino Vieillard, Piotr Stanczyk, Sertan Girgin, Nikola Momchev, Matt Hoffman, Shantanu Thakoor, Jean-Bastien Grill, Behnam Neyshabur, Olivier Bachem, Alanna Walton, Aliaksei Severyn, Alicia Parrish, Aliya Ahmad, Allen Hutchison, Alvin Abdagic, Amanda Carl, Amy Shen, Andy Brock, Andy Coenen, Anthony Laforge, Antonia Paterson, Ben Bastian, Bilal Piot, Bo Wu, Brandon Royal, Charlie Chen, Chintu Kumar, Chris Perry, Chris Welty, Christopher A. Choquette-Choo, Danila Sinopalnikov, David Weinberger, Dimple Vijaykumar, Dominika Rogozińska, Dustin Herbison, Elisa Bandy, Emma Wang, Eric Noland, Erica Moreira, Evan Senter, Evgenii Eltyshev, Francesco Visin, Gabriel Rasskin, Gary Wei, Glenn Cameron, Gus Martins, Hadi Hashemi, Hanna Klimczak-Plucińska, Harleen Batra, Harsh Dhand, Ivan Nardini, Jacinda Mein, Jack Zhou, James Svensson, Jeff Stanway, Jetha Chan, Jin Peng Zhou, Joana Carrasqueira, Joana Iljazi, Jocelyn Becker, Joe Fernandez, Joost van Amersfoort, Josh Gordon, Josh Lipschultz, Josh Newlan, Ju-yeong Ji, Kareem Mohamed, Kartikeya Badola, Kat Black, Katie Millican, Keelin McDonell, Kelvin Nguyen, Kiranbir Sodhia, Kish Greene, Lars Lowe Sjoesund, Lauren Usui, Laurent Sifre, Lena Heuermann, Leticia Lago, Lilly McNealus, Livio Baldini Soares, Logan Kilpatrick, Lucas Dixon, Luciano Martins, Machel Reid, Manvinder Singh, Mark Iverson, Martin Görner, Mat Velloso, Mateo Wirth, Matt Davidow, Matt Miller, Matthew Rahtz, Matthew Watson, Meg Risdal, Mehran Kazemi, Michael Moynihan, Ming Zhang, Minsuk Kahng, Minwoo Park, Mofi Rahman, Mohit Khatwani, Natalie Dao, Nenshad Bardoliwalla, Nesh Devanathan, Neta Dumai, Nilay Chauhan, Oscar Wahltinez, Pankil Botarda, Parker Barnes, Paul Barham, Paul Michel, Pengchong Jin, Petko Georgiev, Phil Culliton, Pradeep Kuppala, Ramona Comanescu, Ramona Merhej, Reena Jana, Reza Ardeshir Rokni, Rishabh Agarwal, Ryan Mullins, Samaneh Saadat, Sara Mc Carthy, Sarah Cogan, Sarah Perrin, Sébastien M. R. Arnold, Sebastian Krause, Shengyang Dai, Shruti Garg, Shruti Sheth, Sue Ronstrom, Susan Chan, Timothy Jordan, Ting Yu, Tom Eccles, Tom Hennigan, Tomas Kocisky, Tulsee Doshi, Vihan Jain, Vikas Yadav, Vilobh Meshram, Vishal Dharmadhikari, Warren Barkley, Wei Wei, Wenming Ye, Woohyun Han, Woosuk Kwon, Xiang Xu, Zhe Shen, Zhitao Gong, Zichuan Wei, Victor Cotruta, Phoebe Kirk, Anand Rao, Minh Giang, Ludovic Peran, Tris Warkentin, Eli Collins, Joelle Barral, Zoubin Ghahramani, Raia Hadsell, D. Sculley, Jeanine Banks, Anca Dragan, Slav Petrov, Oriol Vinyals, Jeff Dean, Demis Hassabis, Koray Kavukcuoglu, Clement Farabet, Elena Buchatskaya, Sebastian Borgeaud, Noah Fiedel, Armand Joulin, Kathleen Kenealy, Robert Dadashi, Alek Andreev (arXiv:2408.00118), 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/google-deepmind/gemma at commit 9e1f74131ba9 (Apache-2.0). Any quoted code is shown under that license.