google/gemma-4-31B-it-assistant
摘要
Google DeepMind 发布了 Gemma 4,这是一个开源权重的多模态模型家族,支持文本、图像、视频和音频,具备增强的推理和编码能力,并通过多令牌预测(MTP)实现高达 2 倍的解码速度提升。
任务:任意到任意
标签:transformers, safetensors, gemma4_assistant, text-generation, any-to-any, license:apache-2.0, endpoints_compatible, region:us
查看缓存全文
缓存时间: 2026/05/08 08:54
google/gemma-4-31B-it-assistant · Hugging Face Source: https://huggingface.co/google/gemma-4-31B-it-assistant Hugging Face (https://huggingface.co/collections/google/gemma-4)|GitHub (https://github.com/google-gemma)|MTP Documentation (https://ai.google.dev/gemma/docs/mtp/mtp) License:Apache 2.0 (https://ai.google.dev/gemma/docs/gemma_4_license)|Authors:Google DeepMind (https://deepmind.google/models/gemma/) > 本模型卡适用于 Gemma 4 模型的 Multi-Token Prediction (MTP) 草稿模型。MTP 通过扩展基础模型,加入一个更小、更快的草稿模型来实现。在投机解码(Speculative Decoding)管道中使用时,草稿模型会预测前方的多个 token,随后目标模型并行验证这些预测。这带来了显著的解码加速(最高可达 2 倍),同时保证与标准生成完全相同的质量,使得这些检查点非常适合低延迟和端侧应用。 Gemma 是由 Google DeepMind 构建的一系列开放模型。Gemma 4 模型是多模态模型,支持文本和图像输入(小模型支持音频),并生成文本输出。本次发布包含预训练和指令微调两种变体的开放权重模型。Gemma 4 具备高达 256K token 的上下文窗口,并在 140 多种语言中保持多语言支持。Gemma 4 采用密集(Dense)和混合专家(Mixture-of-Experts, MoE)架构,非常适合文本生成、编程和推理等任务。该模型提供四种不同的尺寸:E2B、E4B、26B A4B 和 31B。多样化的尺寸使其能够部署在从高端手机到笔记本电脑和服务器的各种环境中,从而让最先进的人工智能技术更加普及。 Gemma 4 引入了关键的能力和架构改进: - 推理能力 – 系列中的所有模型都设计为具备强大的推理能力,支持可配置的思考模式。 - 扩展的多模态能力 – 处理文本、图像(所有模型支持可变宽高比和分辨率)、视频以及音频(E2B 和 E4B 模型原生支持)。 - 多样且高效的架构 – 提供不同尺寸的密集和混合专家(MoE)变体,以实现可扩展部署。 - 优化端侧部署 – 小型模型专为在笔记本电脑和移动设备上高效本地运行而设计。 - 增强的上下文窗口 – 小型模型具备 128K 上下文窗口,中型模型支持 256K。 - 增强的编程与智能体能力 – 在编程基准测试中取得显著进步,同时原生支持函数调用,赋能强大的自主智能体。 - 原生系统提示支持 – Gemma 4 引入了对 system 角色的原生支持,使对话更具结构化和可控性。 ## https://huggingface.co/google/gemma-4-31B-it-assistant#models-overview模型概览 Gemma 4 模型旨在为每种尺寸提供前沿水平的性能,针对从移动和边缘设备(E2B、E4B)到消费级 GPU 和工作站(26B A4B、31B)的部署场景。它们非常适合推理、智能体工作流、编程和多模态理解。这些模型采用混合注意力机制,将局部滑动窗口注意力与全全局注意力交错,确保最后一层始终为全局注意力。这种混合设计在保持轻量级模型的处理速度和低内存占用优势的同时,不牺牲复杂长上下文任务所需的深度感知能力。为了优化长上下文的内存使用,全局层采用统一的 Keys 和 Values,并应用比例 RoPE (p-RoPE)。 ### https://huggingface.co/google/gemma-4-31B-it-assistant#dense-models密集模型 PropertyE2BE4B31B Dense总参数量2.3B 有效参数(含嵌入层为 5.1B)4.5B 有效参数(含嵌入层为 8B)30.7B层数354260滑动窗口512 tokens512 tokens1024 tokens上下文长度128K tokens128K tokens256K tokens词表大小262K262K262K支持的模态文本、图像、音频文本、图像、音频文本、图像视觉编码器参数量*~150M**~150M**~550M音频编码器参数量~300M**~300M无音频 E2B 和 E4B 中的“E”代表“有效(effective)”参数。小型模型采用逐层嵌入(Per-Layer Embeddings, PLE),以最大化端侧部署中的参数效率。PLE 不为模型增加更多层或参数,而是为每个解码器层提供针对每个 token 的小型嵌入。虽然这些嵌入表体积较大,但仅用于快速查找,因此有效参数量远小于总参数量。 ### https://huggingface.co/google/gemma-4-31B-it-assistant#mixture-of-experts-moe-model混合专家(MoE)模型 Property26B A4B MoE总参数量25.2B活跃参数量3.8B层数30滑动窗口1024 tokens上下文长度256K tokens词表大小262K专家数量8 个活跃 / 128 个总计 + 1 个共享支持的模态文本、图像视觉编码器参数量~550M* 26B A4B 中的“A”代表“活跃参数(active parameters)”,与模型包含的总参数量相对。由于在推理时仅激活 4B 的参数子集,混合专家模型的运行速度远快于其 26B 的总参数量所暗示的速度。与密集的 31B 模型相比,它几乎与 4B 参数模型一样快,因此是快速推理的绝佳选择。 ## https://huggingface.co/google/gemma-4-31B-it-assistant#benchmark-results基准测试结果 这些模型在大量不同的数据集和指标上进行了评估,以覆盖文本生成的各个方面。表中标记的评估结果对应指令微调模型。 Gemma 4 31BGemma 4 26B A4BGemma 4 E4BGemma 4 E2BGemma 3 27B (no think)MMLU Pro85.2%82.6%69.4%60.0%67.6%AIME 2026 no tools89.2%88.3%42.5%37.5%20.8%LiveCodeBench v680.0%77.1%52.0%44.0%29.1%Codeforces ELO21501718940633110GPQA Diamond84.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 tools19.5%8.7%—HLE with search26.5%17.2%—BigBench Extra Hard74.4%64.8%33.1%21.9%19.3%MMMLU88.4%86.3%76.6%67.4%70.7%视觉MMMU Pro76.9%73.8%52.6%44.2%49.7%OmniDocBench 1.5 (average edit distance, lower is better)0.1310.1490.1810.2900.365MATH-Vision85.6%82.4%59.5%52.4%46.0%MedXPertQA MM61.3%58.1%28.7%23.5%-音频CoVoST–35.5433.47-FLEURS (lower is better)–0.080.09-长上下文MRCR v2 8 needle 128k (average)66.4%44.1%25.4%19.1%13.5% ## https://huggingface.co/google/gemma-4-31B-it-assistant#core-capabilities核心能力 Gemma 4 模型处理文本、视觉和音频方面的广泛任务。关键能力包括: - 思考模式 – 内置的推理模式,让模型在回答前进行逐步思考。 - 长上下文 – 上下文窗口高达 128K token(E2B/E4B)和 256K token(26B A4B/31B)。 - 图像理解 – 物体检测、文档/PDF 解析、屏幕和 UI 理解、图表理解、OCR(包括多语言)、手写识别和指针定位。图像可以以可变宽高比和分辨率处理。 - 视频理解 – 通过处理帧序列来分析视频。 - 交错多模态输入 – 在单个提示中自由混合任意顺序的文本和图像。 - 函数调用 – 原生支持结构化工具使用,赋能智能体工作流。 - 编程 – 代码生成、补全和纠错。 - 多语言 – 开箱即用支持 35+ 种语言,基于 140+ 种语言进行预训练。 - 音频(仅限 E2B 和 E4B) – 跨多种语言的自动语音识别(ASR)和语音到翻译文本的转换。 ## https://huggingface.co/google/gemma-4-31B-it-assistant#getting-started快速入门 您可以使用最新版本的 Transformers 库运行所有 Gemma 4 模型。要开始使用,请在您的环境中安装必要的依赖项: pip install -U transformers torch accelerate 安装完成后,您可以使用以下代码加载目标模型和助手模型(草稿模型): from transformers import AutoProcessor, AutoModelForCausalLM TARGET_MODEL_ID = "google/gemma-4-31B-it" ASSISTANT_MODEL_ID = "google/gemma-4-31B-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)、图像和视频的代码片段: 处理音频的代码除了使用 AutoModelForCausalLM,您还可以使用 AutoModelForMultimodalLM 来处理音频。要使用它,请确保安装以下包: pip install -U transformers torch torchvision librosa accelerate 安装完成后,您可以使用以下代码加载目标模型和助手模型: import torch from transformers import AutoProcessor, AutoModelForCausalLM, AutoModelForMultimodalLM TARGET_MODEL_ID = "google/gemma-4-31B-it" ASSISTANT_MODEL_ID = "google/gemma-4-31B-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) 处理图像的代码除了使用 AutoModelForCausalLM,您还可以使用 AutoModelForMultimodalLM 来处理图像。要使用它,请确保安装以下包: pip install -U transformers torch torchvision accelerate 安装完成后,您可以使用以下代码加载目标模型和助手模型: import torch from transformers import AutoProcessor, AutoModelForCausalLM, AutoModelForMultimodalLM TARGET_MODEL_ID = "google/gemma-4-31B-it" ASSISTANT_MODEL_ID = "google/gemma-4-31B-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) 处理视频的代码除了使用 AutoModelForCausalLM,您还可以使用 AutoModelForMultimodalLM 来处理视频。要使用它,请确保安装以下包: pip install -U transformers torch torchvision librosa accelerate 安装完成后,您可以使用以下代码加载目标模型和助手模型: import torch from transformers import AutoProcessor, AutoModelForCausalLM, AutoModelForMultimodalLM TARGET_MODEL_ID = "google/gemma-4-31B-it" ASSISTANT_MODEL_ID = "google/gemma-4-31B-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-31B-it-assistant#best-practices最佳实践 为了获得最佳性能,请使用以下配置和最佳实践: ### https://huggingface.co/google/gemma-4-31B-it-assistant#1-sampling-parameters1. 采样参数 在所有用例中使用以下标准化采样配置: - temperature=1.0 - top_p=0.95 - top_k=64 ### https://huggingface.co/google/gemma-4-31B-it-assistant#2-thinking-mode-configuration2. 思考模式配置 与 Gemma 3 相比,这些模型使用标准的 system、assistant 和 user 角色。为了正确管理思考过程,请使用以下控制令牌: - 触发思考: 通过在起始位置包含 <|think|> 令牌来启用思考。
相似文章
google/gemma-4-26B-A4B-it
Google DeepMind 发布 Gemma 4,一系列开放权重的多模态模型,参数量从2.3B到31B,支持文本、图像、视频和音频输入。模型具有256K上下文窗口,MoE和密集架构,增强的推理能力,并针对从移动设备到服务器的部署进行优化。
google/gemma-4-E4B-it-assistant
Google DeepMind 发布了 Gemma 4 E4B 指令微调助手模型,该模型具备多模态能力、推理改进以及针对低延迟端侧应用优化的投机解码功能。
google/gemma-4-26B-A4B-it-assistant
Google DeepMind 发布了 Gemma 4 MTP 草稿模型(drafter),适用于 Gemma 4 系列模型,通过推测解码(speculative decoding)实现显著的解码加速,同时保持完全一致的生成质量,适用于低延迟应用场景。
Gemma 4:同等参数规模下能力最强的开源模型
Google DeepMind 发布 Gemma 4,这是其迄今为止能力最强的开源模型系列,专为高级推理和智能体工作流设计,在多种参数规模下均实现了极高的智能密度。
推出 Gemma 4 12B:一个统一的、无编码器的多模态模型
Google DeepMind 宣布推出 Gemma 4 12B,一种新颖的无编码器多模态 AI 模型,将视觉和音频直接集成到 LLM 主干中,在配备 16GB RAM 的笔记本电脑上提供高级推理和代理能力,以 Apache 2.0 许可证发布。