Hugging Face |
GitHub |
MTP 文档
许可证: Apache 2.0 | 作者: Google DeepMind
[!Note] 本模型卡片适用于 Gemma 4 模型的多令牌预测(MTP)草稿模型。MTP 通过在基础模型上扩展一个更小、更快的草稿模型来实现。在推测解码(Speculative Decoding)流程中使用时,草稿模型会提前预测多个令牌,然后目标模型并行验证这些令牌。这能显著提升解码速度(最高可达 3 倍),同时保证与标准生成完全一致的质量,使这些检查点非常适合低延迟和设备端应用。
Gemma 是由 Google DeepMind 构建的一系列开放模型。Gemma 4 模型是多模态的,可处理文本和图像输入(小型模型支持音频)并生成文本输出。此次发布包括开放权重模型,既有预训练版本,也有指令调优版本。Gemma 4 拥有高达 256K 令牌的上下文窗口,并保持对超过 140 种语言的多语言支持。
Gemma 4 同时具备密集型(Dense)和混合专家(Mixture-of-Experts, MoE)架构,非常适合文本生成、编码和推理等任务。该模型系列提供四种不同尺寸:E2B、E4B、26B A4B 和 31B。其多样化的尺寸使其能够部署在从高端手机到笔记本电脑再到服务器的各种环境中,让尖端 AI 技术的获取更加普及。
Gemma 4 带来了关键的能力与架构方面的进步:
推理能力——该系列中的所有模型都被设计为高性能的推理器,具备可配置的思维模式。
扩展的多模态——处理文本、支持可变宽高比和分辨率的图像(所有模型)、视频以及音频(E2B 和 E4B 模型原生支持)。
多样化且高效的架构——提供不同尺寸的密集型和混合专家(MoE)变体,以实现可扩展部署。
针对设备端优化——小型模型专门设计用于在笔记本电脑和移动设备上进行高效的本地执行。
增大的上下文窗口——小型模型具备 128K 的上下文窗口,而中型模型则支持 256K。
增强的编码与智能体能力——在编码基准测试中取得显著进步,同时原生支持函数调用,为高性能自主智能体提供动力。
原生系统提示支持——Gemma 4 引入了对 system 角色的原生支持,实现更结构化和可控的对话。
Gemma 4 模型旨在为不同规模的模型提供前沿级性能,其部署场景覆盖从移动设备和边缘设备(E2B、E4B)到消费级 GPU 和工作站(26B A4B、31B)。这些模型非常适合推理、智能体工作流、编码以及多模态理解任务。
模型采用混合注意力机制,将局部滑动窗口注意力与全局注意力交错使用,并确保最后一层始终为全局注意力。这种混合设计在保证轻量级模型处理速度和低内存占用的同时,不会牺牲复杂长上下文任务所需的深度理解能力。为优化长上下文的内存使用,全局层采用统一的 Keys 和 Values,并应用 Proportional RoPE(p-RoPE)。
| 属性 | E2B | E4B | 31B Dense |
|---|---|---|---|
| 总参数 | 23 亿有效参数(含嵌入层为 51 亿) | 45 亿有效参数(含嵌入层为 80 亿) | 307 亿 |
| 层数 | 35 | 42 | 60 |
| 滑动窗口 | 512 tokens | 512 tokens | 1024 tokens |
| 上下文长度 | 128K tokens | 128K tokens | 256K tokens |
| 词汇表大小 | 262K | 262K | 262K |
| 支持的模态 | 文本、图像、音频 | 文本、图像、音频 | 文本、图像 |
| 视觉编码器参数 | 约 1.5 亿 | 约 1.5 亿 | 约 5.5 亿 |
| 音频编码器参数 | 约 3 亿 | 约 3 亿 | 无音频 |
E2B 和 E4B 中的“E”代表“有效”参数。较小的模型采用每层嵌入(Per-Layer Embeddings, PLE),以在设备端部署中最大化参数效率。PLE 并非通过增加模型的层数或参数,而是为每个解码器层中的每个 token 提供自己的小型嵌入。这些嵌入表虽然较大,但仅用于快速查找,因此有效参数数量远小于总参数数量。
| 属性 | 26B A4B MoE |
|---|---|
| 总参数 | 252 亿 |
| 激活参数 | 38 亿 |
| 层数 | 30 |
| 滑动窗口 | 1024 tokens |
| 上下文长度 | 256K tokens |
| 词汇表大小 | 262K |
| 专家数量 | 8 个激活专家 / 128 个总专家和 1 个共享专家 |
| 支持的模态 | 文本、图像 |
| 视觉编码器参数 | 约 5.5 亿 |
26B A4B 中的“A”代表“激活参数”,与模型包含的总参数数量形成对比。通过在推理过程中仅激活 40 亿参数的子集,混合专家模型的运行速度比其 260 亿总参数所暗示的要快得多。这使其成为与 31B 稠密模型相比进行快速推理的理想选择,因为它的运行速度几乎与 40 亿参数的模型相当。
这些模型通过大量不同的数据集和指标进行了评估,以全面覆盖文本生成的各个方面。表格中标注的评估结果均针对指令微调模型。
| Gemma 4 31B | Gemma 4 26B A4B | Gemma 4 E4B | Gemma 4 E2B | Gemma 3 27B (no think) | |
|---|---|---|---|---|---|
| MMLU Pro | 85.2% | 82.6% | 69.4% | 60.0% | 67.6% |
| AIME 2026 无工具 | 89.2% | 88.3% | 42.5% | 37.5% | 20.8% |
| LiveCodeBench v6 | 80.0% | 77.1% | 52.0% | 44.0% | 29.1% |
| Codeforces ELO | 2150 | 1718 | 940 | 633 | 110 |
| GPQA Diamond | 84.3% | 82.3% | 58.6% | 43.4% | 42.4% |
| Tau2(3次平均) | 76.9% | 68.2% | 42.2% | 24.5% | 16.2% |
| HLE 无工具 | 19.5% | 8.7% | - | - | - |
| HLE 带搜索 | 26.5% | 17.2% | - | - | - |
| BigBench Extra Hard | 74.4% | 64.8% | 33.1% | 21.9% | 19.3% |
| MMMLU | 88.4% | 86.3% | 76.6% | 67.4% | 70.7% |
| 视觉 | |||||
| MMMU Pro | 76.9% | 73.8% | 52.6% | 44.2% | 49.7% |
| OmniDocBench 1.5(平均编辑距离,越低越好) | 0.131 | 0.149 | 0.181 | 0.290 | 0.365 |
| MATH-Vision | 85.6% | 82.4% | 59.5% | 52.4% | 46.0% |
| MedXPertQA MM | 61.3% | 58.1% | 28.7% | 23.5% | - |
| 音频 | |||||
| CoVoST | - | - | 35.54 | 33.47 | - |
| FLEURS(越低越好) | - | - | 0.08 | 0.09 | - |
| 长上下文 | |||||
| MRCR v2 8 needle 128k(平均) | 66.4% | 44.1% | 25.4% | 19.1% | 13.5% |
Gemma 4模型可处理文本、视觉和音频等广泛任务。主要能力包括:
您可以通过最新版本的Transformers使用所有Gemma 4模型。要开始使用,请在您的环境中安装必要的依赖项:
pip install -U transformers torch accelerate
安装完成后,您可以使用以下代码加载目标模型和助手模型:
from transformers import AutoProcessor, AutoModelForCausalLM
TARGET_MODEL_ID = "google/gemma-4-26B-A4B-it"
ASSISTANT_MODEL_ID = "google/gemma-4-26B-A4B-it-assistant"
# Target Model
processor = AutoProcessor.from_pretrained(TARGET_MODEL_ID)
target_model = AutoModelForCausalLM.from_pretrained(
TARGET_MODEL_ID,
dtype="auto",
device_map="auto",
)
# Assistant Model (the drafter)
assistant_model = AutoModelForCausalLM.from_pretrained(
ASSISTANT_MODEL_ID,
dtype="auto",
device_map="auto",
)模型加载完成后,即可开始生成输出:
# Prompt
messages = [
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "Write a short joke about saving RAM."},
]
# Process input
text = processor.apply_chat_template(
messages,
tokenize=False,
add_generation_prompt=True,
)
inputs = processor(text=text, return_tensors="pt").to(target_model.device)
input_len = inputs["input_ids"].shape[-1]
# Generate output
outputs = target_model.generate(
**inputs,
assistant_model=assistant_model,
max_new_tokens=256,
)
response = processor.decode(outputs[0][input_len:], skip_special_tokens=False)
# Parse output
processor.parse_response(response)要启用推理功能,请设置 enable_thinking=True,parse_response 函数将负责解析推理输出。
以下是处理音频(仅限 E2B 和 E4B)、图像以及视频与文本结合的代码片段:
若要处理音频,可使用 AutoModelForMultimodalLM 替代 AutoModelForCausalLM。使用前,请确保安装以下软件包:
pip install -U transformers torch torchvision librosa accelerate
完成所有安装后,可使用以下代码加载目标模型和助手模型:
import torch
from transformers import AutoProcessor, AutoModelForCausalLM, AutoModelForMultimodalLM
TARGET_MODEL_ID = "google/gemma-4-26B-A4B-it"
ASSISTANT_MODEL_ID = "google/gemma-4-26B-A4B-it-assistant"
# Target Model
processor = AutoProcessor.from_pretrained(TARGET_MODEL_ID)
target_model = AutoModelForMultimodalLM.from_pretrained(
TARGET_MODEL_ID,
torch_dtype=torch.bfloat16,
device_map="auto",
)
# Assistant Model (the drafter)
assistant_model = AutoModelForCausalLM.from_pretrained(
ASSISTANT_MODEL_ID,
torch_dtype=torch.bfloat16,
device_map="auto",
)模型加载完成后,您可以在提示词中直接引用音频 URL 来开始生成输出:
# Prompt - add audio before text
messages = [
{
"role": "user",
"content": [
{"type": "audio", "audio": "https://github.com/google-gemma/cookbook/raw/refs/heads/main/apps/sample-data/journal1.wav"},
{"type": "text", "text": "Transcribe the following speech segment in its original language. Follow these specific instructions for formatting the answer:\n* Only output the transcription, with no newlines.\n* When transcribing numbers, write the digits, i.e. write 1.7 and not one point seven, and write 3 instead of three."},
]
}
]
# Process input
text = processor.apply_chat_template(
messages,
tokenize=False,
add_generation_prompt=True,
)
inputs = processor(text=text, return_tensors="pt").to(target_model.device)
input_len = inputs["input_ids"].shape[-1]
# Generate output
outputs = target_model.generate(
**inputs,
assistant_model=assistant_model,
max_new_tokens=256,
)
response = processor.decode(outputs[0][input_len:], skip_special_tokens=False)
# Parse output
processor.parse_response(response)无需使用AutoModelForCausalLM,您可以通过AutoModelForMultimodalLM来处理图像。使用前,请确保已安装以下软件包:
pip install -U transformers torch torchvision accelerate
完成所有安装后,您可以通过以下代码加载目标模型和助手模型:
import torch
from transformers import AutoProcessor, AutoModelForCausalLM, AutoModelForMultimodalLM
TARGET_MODEL_ID = "google/gemma-4-26B-A4B-it"
ASSISTANT_MODEL_ID = "google/gemma-4-26B-A4B-it-assistant"
# Target Model
processor = AutoProcessor.from_pretrained(TARGET_MODEL_ID)
target_model = AutoModelForMultimodalLM.from_pretrained(
TARGET_MODEL_ID,
torch_dtype=torch.bfloat16,
device_map="auto",
)
# Assistant Model (the drafter)
assistant_model = AutoModelForCausalLM.from_pretrained(
ASSISTANT_MODEL_ID,
torch_dtype=torch.bfloat16,
device_map="auto",
)模型加载完成后,您可以在提示词中直接引用图片 URL 来开始生成输出:
# Prompt - add image before text
messages = [
{
"role": "user", "content": [
{"type": "image", "url": "https://raw.githubusercontent.com/google-gemma/cookbook/refs/heads/main/apps/sample-data/GoldenGate.png"},
{"type": "text", "text": "What is shown in this image?"}
]
}
]
# Process input
inputs = processor.apply_chat_template(
messages,
tokenize=True,
return_dict=True,
return_tensors="pt",
add_generation_prompt=True,
).to(target_model.device)
input_len = inputs["input_ids"].shape[-1]
# Generate output
outputs = target_model.generate(**inputs, max_new_tokens=512)
response = processor.decode(outputs[0][input_len:], skip_special_tokens=False)
# Parse output
processor.parse_response(response)您可以使用 AutoModelForMultimodalLM 来处理视频,而非 AutoModelForCausalLM。使用前,请确保安装以下软件包:
pip install -U transformers torch torchvision librosa accelerate
完成所有安装后,您可以通过以下代码加载目标模型和助手模型:
import torch
from transformers import AutoProcessor, AutoModelForCausalLM, AutoModelForMultimodalLM
TARGET_MODEL_ID = "google/gemma-4-26B-A4B-it"
ASSISTANT_MODEL_ID = "google/gemma-4-26B-A4B-it-assistant"
# Target Model
processor = AutoProcessor.from_pretrained(TARGET_MODEL_ID)
target_model = AutoModelForMultimodalLM.from_pretrained(
TARGET_MODEL_ID,
torch_dtype=torch.bfloat16,
device_map="auto",
)
# Assistant Model (the drafter)
assistant_model = AutoModelForCausalLM.from_pretrained(
ASSISTANT_MODEL_ID,
torch_dtype=torch.bfloat16,
device_map="auto",
)模型加载完成后,您可以在提示词中直接引用视频 URL 来开始生成输出:
# Prompt - add video before text
messages = [
{
'role': 'user',
'content': [
{"type": "video", "video": "https://github.com/bebechien/gemma/raw/refs/heads/main/videos/ForBiggerBlazes.mp4"},
{'type': 'text', 'text': 'Describe this video.'}
]
}
]
# Process input
inputs = processor.apply_chat_template(
messages,
tokenize=True,
return_dict=True,
return_tensors="pt",
add_generation_prompt=True,
).to(target_model.device)
input_len = inputs["input_ids"].shape[-1]
# Generate output
outputs = target_model.generate(**inputs, max_new_tokens=512)
response = processor.decode(outputs[0][input_len:], skip_special_tokens=False)
# Parse output
processor.parse_response(response)为获得最佳性能,请使用以下配置和最佳实践:
在所有使用场景中采用以下标准化采样配置:
temperature=1.0top_p=0.95top_k=64与 Gemma 3 相比,模型使用标准的 system、assistant 和 user 角色。为妥善管理思维过程,请使用以下控制令牌:
<|think|> 令牌即可启用思维功能。若要禁用思维,移除该令牌即可。<|channel>thought\n[内部推理内容]<channel|><|channel>thought\n<channel|>[最终答案][!Note] 请注意,许多库(如 Transformers 和 llama.cpp)会为您处理聊天模板的复杂细节。
除了可变宽高比外,Gemma 4 还通过可配置的视觉令牌预算支持可变图像分辨率,该预算控制用于表示图像的令牌数量。较高的令牌预算会保留更多视觉细节,但会增加计算成本;而较低的预算则适用于不需要细粒度理解的任务,可实现更快的推理。
使用以下提示结构进行音频处理:
Transcribe the following speech segment in {LANGUAGE} into {LANGUAGE} text.
Follow these specific instructions for formatting the answer:
* Only output the transcription, with no newlines.
* When transcribing numbers, write the digits, i.e. write 1.7 and not one point seven, and write 3 instead of three.Transcribe the following speech segment in {SOURCE_LANGUAGE}, then translate it into {TARGET_LANGUAGE}.
When formatting the answer, first output the transcription in {SOURCE_LANGUAGE}, then one newline, then output the string '{TARGET_LANGUAGE}: ', then the translation in {TARGET_LANGUAGE}.所有模型均支持图像输入,并能将视频按帧处理,而 E2B 和 E4B 模型还支持音频输入。音频支持的最大长度为 30 秒。若按每秒一帧的速度处理图像,视频支持的最大时长为 60 秒。
用于模型训练的数据及其处理方式。
我们的预训练数据集是一个大规模、多样化的数据集集合,涵盖广泛的领域和模态,包括网络文档、代码、图像、音频,数据截止日期为 2025 年 1 月。以下是关键组成部分:
这些多样化数据源的组合对于训练一个能够处理各种不同任务和数据格式的强大多模态模型至关重要。
以下是应用于训练数据的关键数据清洗和过滤方法:
随着开放模型成为企业基础设施的核心,溯源性和安全性变得至关重要。Gemma 4 由 Google DeepMind 开发,与我们的专有 Gemini 模型一样,经过了严格的安全评估。
Gemma 4 模型是与内部安全和负责任 AI 团队合作开发的。我们通过一系列自动化和人工评估来提升模型安全性,这些评估符合 Google 人工智能原则 及安全政策,旨在防止生成式 AI 模型生成有害内容,包括:
在所有安全测试领域,Gemma 4 模型在内容安全的各个类别中均较前代 Gemma 模型有显著改进。总体而言,Gemma 4 模型在提升安全性的同时,能保持较低的无正当理由拒绝率,显著优于 Gemma 3 和 3n 模型。所有测试均在无安全过滤器的情况下进行,以评估模型的能力和行为。无论是文本到文本、图像到文本,还是在所有模型尺寸中,该模型的政策违规率极低,且较前代 Gemma 模型表现出显著进步。
这些模型存在一些用户应注意的局限性。
多模态模型(能够处理视觉、语言和/或音频)在各行业和领域拥有广泛应用。以下潜在用途列表并非详尽无遗,其目的是提供模型创建者在模型训练和开发过程中考虑的可能用例的背景信息。
视觉语言模型(VLM)的发展引发了若干伦理问题。在开发开放模型时,我们已仔细考虑以下方面:
已识别的风险及缓解措施:
在发布时,与同等规模的模型相比,该系列模型提供了高性能的开放式视觉语言模型实现,这些实现从根本上为负责任的AI开发而设计。