Hugging Face |
GitHub |
发布博客 |
文档
许可证: Apache 2.0 | 作者: Google DeepMind
Gemma 是由 Google DeepMind 构建的一系列开源模型。Gemma 4 模型具备多模态能力,可处理文本和图像输入(小型模型支持音频)并生成文本输出。此次发布包括开源权重模型,既有预训练版本,也有指令调优版本。Gemma 4 的上下文窗口容量高达 256K tokens,并保持对超过 140 种语言的多语言支持。
Gemma 4 同时采用密集型(Dense)和混合专家(Mixture-of-Experts, MoE)架构,非常适合文本生成、编码和推理等任务。该模型系列提供四种不同规格:E2B、E4B、26B A4B 和 31B。其多样化的规格使其能够部署在从高端手机到笔记本电脑再到服务器的各种环境中,让尖端 AI 技术的获取更加普及。
Gemma 4 在能力与架构方面带来了多项关键进步:
推理能力——该系列所有模型均设计为高性能推理器,具备可配置的思维模式。
扩展的多模态支持——可处理文本、图像(所有模型均支持可变宽高比和分辨率)、视频以及音频(E2B 和 E4B 模型原生支持)。
多样化且高效的架构——提供不同规格的密集型和混合专家(MoE)变体,以支持可扩展部署。
针对设备端优化——小型模型经过专门设计,可在笔记本电脑和移动设备上高效本地运行。
更大的上下文窗口——小型模型支持 128K tokens 的上下文窗口,中型模型则支持 256K tokens。
增强的编码与智能体能力——在编码基准测试中取得显著提升,同时原生支持函数调用,为高性能自主智能体提供动力。
原生系统提示支持——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”代表“有效(effective)”参数。较小的模型集成了每层嵌入(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”代表“激活(active)”参数,以区别于模型包含的总参数数量。通过在推理过程中仅激活 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
MODEL_ID = "google/gemma-4-31B-it"
# Load model
processor = AutoProcessor.from_pretrained(MODEL_ID)
model = AutoModelForCausalLM.from_pretrained(
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,
enable_thinking=False
)
inputs = processor(text=text, return_tensors="pt").to(model.device)
input_len = inputs["input_ids"].shape[-1]
# Generate output
outputs = model.generate(**inputs, max_new_tokens=1024)
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 librosa accelerate
然后,您可以通过以下代码加载模型:
from transformers import AutoProcessor, AutoModelForMultimodalLM
MODEL_ID = "google/gemma-4-E2B-it"
# Load model
processor = AutoProcessor.from_pretrained(MODEL_ID)
model = AutoModelForMultimodalLM.from_pretrained(
MODEL_ID,
dtype="auto",
device_map="auto"
)模型加载完成后,您可以在提示词中直接引用音频 URL 来开始生成输出:
# Prompt - add audio before text
messages = [
{
"role": "user",
"content": [
{"type": "audio", "audio": "https://raw.githubusercontent.com/google-gemma/cookbook/refs/heads/main/Demos/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
inputs = processor.apply_chat_template(
messages,
tokenize=True,
return_dict=True,
return_tensors="pt",
add_generation_prompt=True,
).to(model.device)
input_len = inputs["input_ids"].shape[-1]
# Generate output
outputs = model.generate(**inputs, max_new_tokens=512)
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
随后,您可以通过以下代码加载模型:
from transformers import AutoProcessor, AutoModelForMultimodalLM
MODEL_ID = "google/gemma-4-31B-it"
# Load model
processor = AutoProcessor.from_pretrained(MODEL_ID)
model = AutoModelForMultimodalLM.from_pretrained(
MODEL_ID,
dtype="auto",
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/Demos/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(model.device)
input_len = inputs["input_ids"].shape[-1]
# Generate output
outputs = model.generate(**inputs, max_new_tokens=512)
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 torchcodec librosa accelerate
然后,您可以通过以下代码加载模型:
from transformers import AutoProcessor, AutoModelForMultimodalLM
MODEL_ID = "google/gemma-4-31B-it"
# Load model
processor = AutoProcessor.from_pretrained(MODEL_ID)
model = AutoModelForMultimodalLM.from_pretrained(
MODEL_ID,
dtype="auto",
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(model.device)
input_len = inputs["input_ids"].shape[-1]
# Generate output
outputs = 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 模型相比,Gemma 4 在所有内容安全类别上都有显著改进。总体而言,Gemma 4 模型在提升安全性方面显著优于 Gemma 3 和 3n 模型,同时保持较低的不当拒绝率。所有测试均在不使用安全过滤器的情况下进行,以评估模型的能力和行为。无论是文本到文本还是图像到文本任务,在所有模型尺寸上,该模型产生的政策违规内容极少,并且与之前的 Gemma 模型相比表现出显著改进。
这些模型存在一些用户应注意的局限性。
多模态模型(能够处理视觉、语言和/或音频)在各个行业和领域都有广泛的应用。以下潜在用途列表并非详尽无遗。此列表旨在提供模型创建者在模型训练和开发过程中考虑的可能用例的背景信息。
视觉语言模型(VLM)的发展引发了若干伦理问题。在开发开放模型时,我们已仔细考虑以下方面:
已识别的风险及缓解措施:
在发布之时,与同等规模的模型相比,该系列模型提供了高性能的开放式视觉语言模型实现,这些实现从根本上为负责任的AI开发而设计。