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,中型模型则支持 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 个 token | 512 个 token | 1024 个 token |
| 上下文长度 | 128K token | 128K token | 256K token |
| 词汇表大小 | 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 个 token |
| 上下文长度 | 256K token |
| 词汇表大小 | 262K |
| 专家数量 | 8 个激活 / 128 个总数及 1 个共享 |
| 支持的模态 | 文本、图像 |
| 视觉编码器参数 | ~5.5 亿 |
26B A4B 中的“A”代表“激活”(active)参数,与模型包含的总参数数量相对。通过在推理过程中仅激活 40 亿参数的子集,混合专家模型的运行速度比其 260 亿总参数所暗示的要快得多。这使其成为与 310 亿密集型模型相比进行快速推理的理想选择,因为它的运行速度几乎与 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-E4B-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 torchvision librosa accelerate
然后可通过以下代码加载模型:
from transformers import AutoProcessor, AutoModelForMultimodalLM
MODEL_ID = "google/gemma-4-E4B-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)若要处理图像,您可以使用 AutoModelForMultimodalLM 而非 AutoModelForCausalLM。使用前,请确保已安装以下软件包:
pip install -U transformers torch torchvision accelerate
然后您可以通过以下代码加载模型:
from transformers import AutoProcessor, AutoModelForMultimodalLM
MODEL_ID = "google/gemma-4-E4B-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 librosa accelerate
然后,您可以通过以下代码加载模型:
from transformers import AutoProcessor, AutoModelForMultimodalLM
MODEL_ID = "google/gemma-4-E4B-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 原则以及安全政策,旨在防止我们的生成式 AI 模型生成有害内容,包括:
在所有安全测试领域,我们看到所有类别的内容安全相较于之前的 Gemma 模型都有重大改进。总体而言,Gemma 4 模型在提升安全性方面显著优于 Gemma 3 和 3n 模型,同时保持较低的不当拒绝率。所有测试均在无安全过滤器的情况下进行,以评估模型的能力和行为。无论是文本到文本还是图像到文本,在所有模型尺寸上,该模型产生的政策违规行为都极少,并且相较于之前的 Gemma 模型表现出显著改进。
这些模型存在某些用户应注意的限制。
多模态模型(能够处理视觉、语言和/或音频)在各个行业和领域都有广泛的应用。以下潜在用途列表并不全面。此列表旨在提供模型创建者在模型训练和开发过程中考虑的可能用例的背景信息。
视觉语言模型(VLM)的发展引发了若干伦理问题。在开发开放模型时,我们仔细考虑了以下方面:
已识别的风险及缓解措施:
在发布之时,与同等规模的模型相比,该系列模型提供了高性能的开放式视觉语言模型实现,这些实现是为负责任的 AI 开发从头设计的。