Gemma 3
作者提出了Gemma3系列大模型 (Gemma Team, 2025),包括 1B, 4B, 12B, 27B 四个 size. 4B, 12B, 27B 三个 size 均支持多模态,128K 的上下文长度以及 140 种语言。
数据格式
[BOS]<start_of_turn>user
Who are you?<end_of_turn>
<start_of_turn>model
My name is Gemma!<end_of_turn>
<start_of_turn>user
What is 2+2?<end_of_turn>
<start_of_turn>model
2+2=4.<end_of_turn>
pre-train 和 SFT 的区别在于,pre-train 时模型输出以 <eos> 结束,SFT 时模型输出以 <end_of_turn> 结束。
图片处理
输入的图片都会被 resize 到 , 如果图片精度过大或者不是正方形,则会通过 Pan & Scan 技巧裁剪为多个子图,然后每个子图分别进行 resize. 核心处理代码为:
crop_size_w = int(math.ceil(width / num_crops_w))
crop_size_h = int(math.ceil(height / num_crops_h))
# Don't apply PaS if crop size is too small.
if min(crop_size_w, crop_size_h) < pan_and_scan_min_crop_size:
return []
crop_positions_w = [crop_size_w * i for i in range(num_crops_w)]
crop_positions_h = [crop_size_h * i for i in range(num_crops_h)]
image_crops = [
image[pos_h : pos_h + crop_size_h, pos_w : pos_w + crop_size_w]
for pos_h, pos_w in itertools.product(crop_positions_h, crop_positions_w)
]
模型架构
模型包括 4 个 size, 分别是 1B, 4B, 12B, 27B. 1B 的模型是单模态的,4B, 12B, 27B 的模型是多模态的。
对于多模态模型来说:
- Vision encoder: Siglip-400M
- Projection layer: linear layer
- LLM: Gemma 3
模型参数如下表琐事
| Model | Vision Encoder | Embedding Parameters | Non-embedding Parameters | context length | multilingual | training data |
|---|---|---|---|---|---|---|
| 1B | 0 | 302M | 698M | 32K | English | 2T tokens |
| 4B | 417M | 675M | 3,209M | 128K | 140+ languages | 4T tokens |
| 12B | 417M | 1,012M | 10,759M | 128K | 140+ languages | 12T tokens |
| 27B | 417M | 1,416M | 25,600M | 128K | 140+ languages | 14T tokens |
为了提高效率,作者将部分 layer 的 self-attention 替换为 sliding window attention. 论文里是将 6 layers 为一组,替换一组中最后一层为 sliding window attention, 其余层为 self attention. 判断某 layer 是否为 sliding window attention 的代码为:
# config.sliding_window_pattern = 6
self.is_sliding = bool((layer_idx + 1) % config.sliding_window_pattern)
作者将 global self-attention 的 RoPE 的 base frequency 从 10K 提升到了 1M, 对于 sliding window attention, RoPE的 base frequency 保持 10k 不变。
预训练
- 数据在模型参数里进行了汇总
- tokenizer 使用的是 Gemini2.0 的 tokenizer, 基于 SentencePiece, vocab 大小为 262K
- 使用知识蒸馏的方法进行训练,每个 token 按照教师模型的概率采样 256 个 logits, 然后使用 cross-entropy loss 进行训练
- 4B, 12B, 27B 的模型先在 32K 的 context length 上进行训练,然后在 128K 的 context length 上进行训练
作者提供了 quantized 版本的模型,模型基于预训练好的模型使用 QAT 方法进行 SFT 5000 steps 左右。
最后是各个模型的内存占用情况
| Raw (GB) | Quantized (GB) | |||
|---|---|---|---|---|
| Model | bf16 | Int4 | Int4(blocks=32) | SFP8 |
| 1B | 2.0 | 0.5 | 0.7 | 1.0 |
| +KV | 2.9 | 1.4 | 1.6 | 1.9 |
| 4B | 8.0 | 2.6 | 2.9 | 4.4 |
| +KV | 12.7 | 7.3 | 7.6 | 9.1 |
| 12B | 24.0 | 6.6 | 7.1 | 12.4 |
| +KV | 38.9 | 21.5 | 22.0 | 27.3 |
| 27B | 54.0 | 14.1 | 15.3 | 27.4 |
| +KV | 72.7 | 32.8 | 34.0 | 46.1 |
后训练
数据过滤:筛选掉包含 PII, 不安全的或者有毒的输出等。留下上下文依赖高,幻觉小的数据
训练包括升级版的知识蒸馏和基于 RL 的 fine tuning, 其中 RL 的 reward 来自于 weight averaged reward models, code execution feedback 和 ground-truth rewards
表现
Gemma3 的表现如下图所示
与 PaliGemma 2 的表现对比

消融实验
Local attention layers
sliding_window_pattern对模型的表现影响不大- sliding window size 对模型的表现也不是很大,如下图
- 使用 sliding window attention 可以降低 KV cache 的内存占用
Long context ablation
结论为 long context 会降低模型的性能
知识蒸馏
训练 token 个数比较少的时候,使用小的教师模型效果更好;训练 token 个数比较多的时候,使用大的教师模型效果更好。
Pan & Scan
使用图片原始的aspect ratio训练的模型效果更好。
memorization
memorization指模型输出的文本与训练数据中文本的重复率。结果发现Gemma3的memorization要更低一些。
- Gemma Team. (2025). Gemma 3 Technical Report. https://arxiv.org/abs/2503.19786
Gemma 4
| Feature | E2B | E4B | 26B 4A | 31B |
|---|---|---|---|---|
| Architecture | Dense | Dense | MoE (4B Active) | Dense |
| Context Window | 128K | 128K | 256K | 256K |
| Modalities | Text, Vision, Audio | Text, Vision, Audio | Text, Vision | Text, Vision |
| Target Hardware | Pixel & Qualcomm | Pixel & Qualcomm | MacBook / Cloud | V100 32GB |
Gemma 4 31B:
- architecture: wide dense model with 60 layers
- benchmark: #3 spot on the global Arena AI leaderboard
- context: 256k context length, 1024 sliding window size, 5:1 local global layer ratio
- agentic foundation: thinking mode, function calling, structured json output
Gemma 4 26B A4B
- expert mix: 128 total experts, 1 shared expert, 8 active experts
- active parameters: 3.8b parameters
- context: 256k context length, 1024 sliding window size, 5:1 local global layer ratio
- kv cache reuse: shared kv cache across 8 layers
Gemma 4 E4B
- PLE integration: per layer embedding
- multimodal native: audio, vision, Textcontext: 128k context length, 512 sliding window size
architecture
- 5:1 ratio of local to global layers (4:1 for E2B models)
- sliding window attention
multimodal
- 550M ViT for 31B/26B models
- 150M ViT for E2B/E4B models
variable aspect ratios and variable resolution in images: 70, 140, 280, 560, 1120 soft token budgets
audio
- 305M audio encoder: audio conformer