google/gemma-4-E4B-it-assistant
摘要
Google DeepMind 发布了 Gemma 4 E4B 指令微调助手模型,该模型具备多模态能力、推理改进以及针对低延迟端侧应用优化的投机解码功能。
查看缓存全文
缓存时间: 2026/05/10 06:33
google/gemma-4-E4B-it-assistant · Hugging Face 来源: https://huggingface.co/google/gemma-4-E4B-it-assistant Hugging Face (https://huggingface.co/collections/google/gemma-4) | GitHub (https://github.com/google-gemma) | MTP 文档 (https://ai.google.dev/gemma/docs/mtp/mtp) 许可证: Apache 2.0 (https://ai.google.dev/gemma/docs/gemma_4_license) | 作者: Google DeepMind (https://deepmind.google/models/gemma/)
此模型卡片适用于 Gemma 4 模型的多令牌预测 (MTP) 草稿模型。MTP 是通过在基础模型上扩展一个更小、更快的草稿模型来实现的。在投机解码(Speculative Decoding)流水线中使用时,草稿模型会预测多个后续令牌,目标模型随后并行验证这些令牌。这带来了显著的解码速度提升(高达 2 倍),同时保证与标准生成完全相同的质量,使得这些检查点非常适合低延迟和端侧应用。
Gemma 是由 Google DeepMind 构建的一系列开源模型。Gemma 4 模型是多模态的,支持文本和图像输入(小模型支持音频),并生成文本输出。此次发布包括预训练和指令微调两种变体的开源权重模型。Gemma 4 的上下文窗口长达 256K 令牌,并维持对 140 多种语言的多语言支持。Gemma 4 采用稠密(Dense)和混合专家(MoE)两种架构,非常适合文本生成、编码和推理等任务。模型提供四种不同尺寸:E2B、E4B、26B A4B 和 31B。它们多样的尺寸使其可部署在从高端手机、笔记本电脑到服务器等各种环境中,从而普及最先进的 AI 技术。
Gemma 4 引入了关键的能力和架构 advancement:
- 推理 – 家族中的所有模型都设计为具备强大推理能力的模型,支持可配置的思考模式。
- 扩展的多模态能力 – 处理文本、图像(支持可变宽高比和分辨率,所有模型均支持)、视频以及音频(E2B 和 E4B 模型原生支持)。
- 多样化且高效的架构 – 提供不同尺寸的稠密和混合专家(MoE)变体,以支持可扩展的部署。
- 针对端侧优化 – 较小模型专门设计用于在笔记本电脑和移动设备上高效本地执行。
- 增大的上下文窗口 – 小模型具备 128K 上下文窗口,而中等模型支持 256K。
- 增强的编码与智能体能力 – 在编码基准测试中取得显著改进,同时原生支持函数调用,赋能高度自主的智能体。
- 原生系统提示支持 – Gemma 4 引入了对
system角色的原生支持,从而实现更结构化、可控的对话。
https://huggingface.co/google/gemma-4-E4B-it-assistant#models-overview模型概览
Gemma 4 模型旨在在每个尺寸下提供前沿水平的性能,目标部署场景涵盖从移动端和边缘设备(E2B, E4B)到消费级 GPU 和工作站(26B A4B, 31B)。它们非常适合推理、智能体工作流、编码和多模态理解。
模型采用混合注意力机制,将局部滑动窗口注意力与全局注意力交错,确保最后一层始终为全局注意力。这种混合设计在保持轻量级模型的处理速度和低内存占用的同时,不牺牲复杂长上下文任务所需的深度感知能力。为了优化长上下文的内存,全局层采用统一的键和值,并应用比例旋转位置编码(p-RoPE)。
https://huggingface.co/google/gemma-4-E4B-it-assistant#dense-models稠密模型
| 属性 | E2B | E4B | 31B 稠密 |
|---|---|---|---|
| 总参数量 | 2.3B 有效(含嵌入层 5.1B) | 4.5B 有效(含嵌入层 8B) | 30.7B |
| 层数 | 35 | 42 | 60 |
| 滑动窗口 | 512 令牌 | 512 令牌 | 1024 令牌 |
| 上下文长度 | 128K 令牌 | 128K 令牌 | 256K 令牌 |
| 词汇表大小 | 262K | 262K | 262K |
| 支持模态 | 文本、图像、音频 | 文本、图像、音频 | 文本、图像 |
| 视觉编码器参数 | ~150M | ~150M | ~550M |
| 音频编码器参数 | ~300M | ~300M | 无音频 |
E2B 和 E4B 中的 “E” 代表“有效(effective)”参数。较小模型采用了逐层嵌入(Per-Layer Embeddings, PLE)以最大化端侧部署中的参数效率。PLE 不是通过增加模型层数或参数量,而是为每个解码器层的每个令牌提供独立的小型嵌入表。这些嵌入表虽然庞大,但仅用于快速查找,因此有效参数量远小于总参数量。
https://huggingface.co/google/gemma-4-E4B-it-assistant#mixture-of-experts-moe-model混合专家 (MoE) 模型
| 属性 | 26B A4B MoE |
|---|---|
| 总参数量 | 25.2B |
| 活跃参数量 | 3.8B |
| 层数 | 30 |
| 滑动窗口 | 1024 令牌 |
| 上下文长度 | 256K 令牌 |
| 词汇表大小 | 262K |
| 专家数量 | 8 个活跃 / 128 个总计及 1 个共享 |
| 支持模态 | 文本、图像 |
| 视觉编码器参数 | ~550M |
26B A4B 中的 “A” 代表“活跃参数(active parameters)”,区别于模型的总参数量。通过在推理期间仅激活 4B 子集参数,混合专家模型比其 26B 的总量所暗示的要快得多。与稠密的 31B 模型相比,这是一个极佳的快速推理选择,因为它运行的速度几乎与 4B 参数模型相当。
https://huggingface.co/google/gemma-4-E4B-it-assistant#benchmark-results基准测试结果
这些模型针对大量不同的数据集和指标进行了评估,以涵盖文本生成的不同方面。表中标记的评估结果适用于指令微调模型。
| 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 no tools | 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 (average over 3) | 76.9% | 68.2% | 42.2% | 24.5% | 16.2% |
| HLE no tools | 19.5% | 8.7% | - | - | - |
| HLE with search | 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% |
https://huggingface.co/google/gemma-4-E4B-it-assistant#core-capabilities核心能力
Gemma 4 模型处理涵盖文本、视觉和音频的广泛任务。关键能力包括:
- 思考 – 内置推理模式,让模型在回答前进行逐步思考。
- 长上下文 – 上下文窗口高达 128K 令牌(E2B/E4B)和 256K 令牌(26B A4B/31B)。
- 图像理解 – 对象检测、文档/PDF 解析、屏幕和 UI 理解、图表理解、OCR(包括多语言)、手写识别和指代。图像可以以可变宽高比和分辨率处理。
- 视频理解 – 通过处理帧序列来分析视频。
- 交错多模态输入 – 在单个提示中自由混合文本和图像,顺序不限。
- 函数调用 – 原生支持结构化工具使用,赋能智能体工作流。
- 编码 – 代码生成、补全和纠错。
- 多语言 – 开箱即用支持 35+ 种语言,并在 140+ 种语言上进行预训练。
- 音频(仅限 E2B 和 E4B) – 跨多种语言的自动语音识别 (ASR) 和语音到翻译文本的转换。
https://huggingface.co/google/gemma-4-E4B-it-assistant#getting-started入门指南
您可以使用最新版本的 Transformers 库使用所有 Gemma 4 模型。要开始使用,请在您的环境中安装必要的依赖项:
pip install -U transformers torch accelerate
安装完成后,您可以使用以下代码加载目标模型和辅助(草稿)模型:
from transformers import AutoProcessor, AutoModelForCausalLM
TARGET_MODEL_ID = "google/gemma-4-E4B-it"
ASSISTANT_MODEL_ID = "google/gemma-4-E4B-it-assistant"
# 目标模型
processor = AutoProcessor.from_pretrained(TARGET_MODEL_ID)
target_model = AutoModelForCausalLM.from_pretrained(
TARGET_MODEL_ID,
dtype="auto",
device_map="auto",
)
# 辅助模型 (草稿模型)
assistant_model = AutoModelForCausalLM.from_pretrained(
ASSISTANT_MODEL_ID,
dtype="auto",
device_map="auto",
)
加载模型后,您可以开始生成输出:
# 提示词
messages = [
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "Write a short joke about saving RAM."},
]
# 处理输入
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]
# 生成输出
outputs = target_model.generate(
**inputs,
assistant_model=assistant_model,
max_new_tokens=256,
)
response = processor.decode(outputs[0][input_len:], skip_special_tokens=False)
# 解析输出
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-E4B-it"
ASSISTANT_MODEL_ID = "google/gemma-4-E4B-it-assistant"
# 目标模型
processor = AutoProcessor.from_pretrained(TARGET_MODEL_ID)
target_model = AutoModelForMultimodalLM.from_pretrained(
TARGET_MODEL_ID,
torch_dtype=torch.bfloat16,
device_map="auto",
)
# 辅助模型 (草稿模型)
assistant_model = AutoModelForCausalLM.from_pretrained(
ASSISTANT_MODEL_ID,
torch_dtype=torch.bfloat16,
device_map="auto",
)
加载模型后,通过直接在提示词中引用音频 URL 来开始生成输出:
# 提示词 - 在文本前添加音频
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."},
]
}
]
# 处理输入
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]
# 生成输出
outputs = target_model.generate(
**inputs,
assistant_model=assistant_model,
max_new_tokens=256,
)
response = processor.decode(outputs[0][input_len:], skip_special_tokens=False)
# 解析输出
processor.parse_response(response)
处理图像的代码
可以使用 AutoModelForMultimodalLM 代替 AutoModelForCausalLM 来处理图像。使用前,请确保安装以下包:
pip install -U transformers torch torchvision accelerate
安装完成后,使用以下代码加载目标模型和辅助模型:
import torch
from transformers import AutoProcessor, AutoModelForCausalLM, AutoModelForMultimodalLM
TARGET_MODEL_ID = "google/gemma-4-E4B-it"
ASSISTANT_MODEL_ID = "google/gemma-4-E4B-it-assistant"
# 目标模型
processor = AutoProcessor.from_pretrained(TARGET_MODEL_ID)
target_model = AutoModelForMultimodalLM.from_pretrained(
TARGET_MODEL_ID,
torch_dtype=torch.bfloat16,
device_map="auto",
)
# 辅助模型 (草稿模型)
assistant_model = AutoModelForCausalLM.from_pretrained(
ASSISTANT_MODEL_ID,
torch_dtype=torch.bfloat16,
device_map="auto",
)
加载模型后,通过直接在提示词中引用图像 URL 来开始生成输出:
# 提示词 - 在文本前添加图像
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?"}
]
}
]
# 处理输入
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]
# 生成输出
outputs = target_model.generate(**inputs, max_new_tokens=512)
response = processor.decode(outputs[0][input_len:], skip_special_tokens=False)
# 解析输出
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-E4B-it"
ASSISTANT_MODEL_ID = "google/gemma-4-E4B-it-assistant"
# 目标模型
processor = AutoProcessor.from_pretrained(TARGET_MODEL_ID)
target_model = AutoModelForMultimodalLM.from_pretrained(
TARGET_MODEL_ID,
torch_dtype=torch.bfloat16,
device_map="auto",
)
# 辅助模型 (草稿模型)
assistant_model = AutoModelForCausalLM.from_pretrained(
ASSISTANT_MODEL_ID,
torch_dtype=torch.bfloat16,
device_map="auto",
)
加载模型后,通过直接在提示词中引用视频 URL 来开始生成输出:
# 提示词 - 在文本前添加视频
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.'}
]
}
]
# 处理输入
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]
# 生成输出
outputs = target_model.generate(**inputs, max_new_tokens=512)
response = processor.decode(outputs[0][input_len:], skip_special_tokens=False)
# 解析输出
processor.parse_response(response)
https://huggingface.co/google/gemma-4-E4B-it-assistant#best-practices最佳实践
为了获得最佳性能,请使用以下配置和最佳实践:
https://huggingface.co/google/gemma-4-E4B-it-assistant#1-sampling-parameters1. 采样参数
在所有用例中使用以下标准化采样配置:
temperature=1.0top_p=0.95top_k=64
https://huggingface.co/google/gemma-4-E4B-it-assistant#2-thinking-mode-configuration2. 思考模式配置
与 Gemma 3 相比,模型使用标准的 system、assistant 和 user 角色。为了正确管理思考过程,请使用以下控制令牌:
- **触发思考:**通过在开头包含
<|think|>令牌来启用思考。
相似文章
google/gemma-4-31B-it-assistant
Google DeepMind 发布了 Gemma 4,这是一个开源权重的多模态模型家族,支持文本、图像、视频和音频,具备增强的推理和编码能力,并通过多令牌预测(MTP)实现高达 2 倍的解码速度提升。
google/gemma-4-26B-A4B-it-assistant
Google DeepMind 发布了 Gemma 4 MTP 草稿模型(drafter),适用于 Gemma 4 系列模型,通过推测解码(speculative decoding)实现显著的解码加速,同时保持完全一致的生成质量,适用于低延迟应用场景。
google/gemma-4-26B-A4B-it
Google DeepMind 发布 Gemma 4,一系列开放权重的多模态模型,参数量从2.3B到31B,支持文本、图像、视频和音频输入。模型具有256K上下文窗口,MoE和密集架构,增强的推理能力,并针对从移动设备到服务器的部署进行优化。
Gemma 4 发布:前沿多模态智能,端侧可用
Google DeepMind 发布 Gemma 4,这是一系列前沿多模态模型,已在 Hugging Face 上以 Apache 2 协议开源,针对端侧部署进行了优化,并支持多种推理框架。
Gemma 4:同等参数规模下能力最强的开源模型
Google DeepMind 发布 Gemma 4,这是其迄今为止能力最强的开源模型系列,专为高级推理和智能体工作流设计,在多种参数规模下均实现了极高的智能密度。