Gemma 4 发布:前沿多模态智能,端侧可用

Hugging Face Blog 模型

摘要

Google DeepMind 发布 Gemma 4,这是一系列前沿多模态模型,已在 Hugging Face 上以 Apache 2 协议开源,针对端侧部署进行了优化,并支持多种推理框架。

暂无内容
查看原文
查看缓存全文

缓存时间: 2026/05/08 09:09

欢迎 Gemma 4:设备端的前沿多模态智能 来源:https://huggingface.co/blog/gemma4 返回文章 (https://huggingface.co/blog) - 目录 (https://huggingface.co/blog/gemma4#table-of-contents) - 能力与架构概览 (https://huggingface.co/blog/gemma4#overview-of-capabilities-and-architecture) - 多模态能力 (https://huggingface.co/blog/gemma4#multimodal-capabilities)- 目标检测与指向 (https://huggingface.co/blog/gemma4#object-detection-and-pointing) - GUI 检测 (https://huggingface.co/blog/gemma4#gui-detection) - 目标检测 (https://huggingface.co/blog/gemma4#object-detection) - 多模态推理与函数调用 (https://huggingface.co/blog/gemma4#multimodal-thinking-and-function-calling) - 视频理解 (https://huggingface.co/blog/gemma4#video-understanding) - 图像描述 (https://huggingface.co/blog/gemma4#captioning) - 音频问答 (https://huggingface.co/blog/gemma4#audio-question-answering) - 多模态函数调用 (https://huggingface.co/blog/gemma4#multimodal-function-calling) - transformers (https://huggingface.co/blog/gemma4#transformers) - Llama.cpp (https://huggingface.co/blog/gemma4#llamacpp) - 接入你的本地智能体 (https://huggingface.co/blog/gemma4#plug-in-your-local-agent) - transformers.js (https://huggingface.co/blog/gemma4#transformersjs) - MLX (https://huggingface.co/blog/gemma4#mlx)- Mistral.rs (https://huggingface.co/blog/gemma4#mistralrs) - 多 Token 预测草稿器 (https://huggingface.co/blog/gemma4#multi-token-prediction-drafters) - 面向所有人的微调 (https://huggingface.co/blog/gemma4#fine-tuning-for-all) - 使用 TRL 微调 (https://huggingface.co/blog/gemma4#fine-tuning-with-trl)- 在 Vertex AI 上使用 TRL 微调 (https://huggingface.co/blog/gemma4#fine-tuning-with-trl-on-vertex-ai) - 使用 Unsloth Studio 微调 (https://huggingface.co/blog/gemma4#fine-tuning-with-unsloth-studio) - 体验 Gemma 4 (https://huggingface.co/blog/gemma4#try-gemma-4) - 基准测试结果 (https://huggingface.co/blog/gemma4#benchmark-results) - 致谢 (https://huggingface.co/blog/gemma4#acknowledgements) Google DeepMind 推出的 Gemma 4 多模态模型系列现已在 Hugging Face 上线,支持您喜爱的智能体、推理引擎和微调库 🤗 这些模型来真的:真正开源的 Apache 2 许可证、高质量的帕累托前沿竞技场分数、包含音频的多模态能力,以及可以在任何地方使用的尺寸——包括设备端。 Gemma 4 建立在先前系列的技术进步之上,并将它们融会贯通。我们在预发布版本的测试中对其能力印象深刻,以至于我们很难找到好的微调示例,因为它们开箱即用就太出色了。 我们与 Google 及社区合作,让它们随处可用:transformers、llama.cpp、MLX、WebGPU、Rust;应有尽有。这篇博客将向你展示如何使用你最喜欢的工具进行构建 (https://huggingface.co/collections/google/gemma-4),期待你的反馈! ## https://huggingface.co/blog/gemma4#table-of-contents 目录 - Gemma 4 有什么新特性? (https://huggingface.co/blog/gemma4#what-is-new-with-gemma-4) - 能力与架构概览 (https://huggingface.co/blog/gemma4#overview-of-capabilities-and-architecture)- 架构一览 (https://huggingface.co/blog/gemma4#architecture-at-a-glance) - 逐层嵌入 (PLE) (https://huggingface.co/blog/gemma4#per-layer-embeddings-ple) - 共享 KV 缓存 (https://huggingface.co/blog/gemma4#shared-kv-cache) - 多模态能力 (https://huggingface.co/blog/gemma4#multimodal-capabilities) - 随处部署 (https://huggingface.co/blog/gemma4#deploy-anywhere)- transformers (https://huggingface.co/blog/gemma4#transformers) - Llama.cpp (https://huggingface.co/blog/gemma4#llamacpp) - 接入你的本地智能体 (https://huggingface.co/blog/gemma4#Plug-in-your-local-agent) - transformers.js (https://huggingface.co/blog/gemma4#transformersjs) - MLX (https://huggingface.co/blog/gemma4#mlx) - Mistral.rs (https://huggingface.co/blog/gemma4#mistralrs) - 多 Token 预测草稿器 (https://huggingface.co/blog/gemma4#multi-token-prediction-drafters) - 微调与演示 (https://huggingface.co/blog/gemma4#fine-tuning–demos)- 使用 TRL 微调 (https://huggingface.co/blog/gemma4#fine-tuning-with-trl)- 在 Vertex AI 上使用 TRL 微调 (https://huggingface.co/blog/gemma4#fine-tuning-with-trl-on-vertex-ai) - 使用 Unsloth Studio 微调 (https://huggingface.co/blog/gemma4#fine-tuning-with-unsloth-studio) - 体验 Gemma 4 (https://huggingface.co/blog/gemma4#try-gemma-4) - 基准测试结果 (https://huggingface.co/blog/gemma4#benchmark-results) - 致谢 (https://huggingface.co/blog/gemma4#acknowledgements) ## https://huggingface.co/blog/gemma4#what-is-new-with-gemma-4 Gemma 4 有什么新特性? 与 Gemma-3n 类似,Gemma 4 支持图像、文本和音频输入,并生成文本回复。文本解码器基于 Gemma 模型,支持长上下文窗口。图像编码器与 Gemma 3 类似,但有关键的两项改进:可变宽高比,以及可配置的图像 token 输入数量,以在速度、内存和质量之间找到最佳平衡点。所有模型都支持图像(或视频)和文本输入,而较小的变体(E2B 和 E4B)还支持音频。 Gemma 4 有四种尺寸,均提供基础版和指令微调版: 模型参数规模上下文窗口检查点Gemma 4 E2B2.3B 有效参数,含嵌入层 5.1B128kbase (https://huggingface.co/google/gemma-4-E2B),IT (https://huggingface.co/google/gemma-4-E2B-it)Gemma 4 E4B4.5B 有效参数,含嵌入层 8B128kbase (https://huggingface.co/google/gemma-4-E4B),IT (https://huggingface.co/google/gemma-4-E4B-it)Gemma 4 31B31B 稠密模型256Kbase (https://huggingface.co/google/gemma-4-31B),IT (https://huggingface.co/google/gemma-4-31B-it)Gemma 4 26B A4B4B 激活 / 26B 总参数的混合专家模型256Kbase (https://huggingface.co/google/gemma-4-26B-A4B),IT (https://huggingface.co/google/gemma-4-26B-A4B-it) ## https://huggingface.co/blog/gemma4#overview-of-capabilities-and-architecture 能力与架构概览 Gemma 4 采用了此前 Gemma 版本和其他开源模型中的多个架构组件,并去除了 Altup 等复杂或结论尚不明确的特性。这种组合旨在实现跨库和跨设备的高度兼容性,能够高效支持长上下文和智能体用例,同时非常适合量化。 如上所示的基准测试表明,这种特性组合(结合训练数据和配方)使 31B 稠密模型在纯文本 LMArena 评分中达到估计 1452 分,而 26B MoE 仅以 4B 激活参数就达到 1441 分 🤯。正如我们将看到的,多模态操作与文本生成相比同样出色,至少在非正式和主观测试中如此。 Gemma 4 的主要架构特点包括: - 交替使用局部滑动窗口全局全上下文注意力层。较小的稠密模型使用 512 token 的滑动窗口,而较大的模型使用 1024 token。 - 双 RoPE配置:滑动层使用标准 RoPE,全局层使用裁剪 RoPE,以支持更长的上下文。 - 逐层嵌入(PLE):第二个嵌入表,为每个解码器层提供小型残差信号。 - 共享 KV 缓存:模型的最后 N 层重用前面层的键值状态,消除冗余的 KV 投影。 - 视觉编码器:使用学习的二维位置编码和多维 RoPE。保留原始宽高比,可将图像编码为不同的 token 预算(70、140、280、560、1120)。 - 音频编码器:USM 风格的 conformer,与 Gemma-3n 中的基础架构相同。 #### https://huggingface.co/blog/gemma4#per-layer-embeddings-ple 逐层嵌入(PLE) 较小 Gemma 4 模型中最独特的特性之一是逐层嵌入(PLE),此前已在 Gemma-3n 中引入。 在标准 transformer 中,每个 token 在输入时获得单一嵌入向量,相同的初始表示作为残差流在所有层中构建,这迫使嵌入层预先加载模型可能需要的一切信息。 PLE 在主残差流旁边添加了一条并行的低维调节路径。对于每个 token,它通过组合两个信号为每一层生成专用的小向量:token 身份分量(来自嵌入查找)和上下文感知分量(来自主嵌入的学习投影)。每个解码器层然后使用对应的向量,通过轻量级残差块在注意力和前馈之后调节隐藏状态。 这为每一层提供了自己的通道,仅在相关信息变得重要时接收 token 特定信息,而非要求所有信息都打包到单一的初始嵌入中。由于 PLE 维度远小于主隐藏大小,这以适度的参数成本实现了有意义的逐层特化。 对于多模态输入(图像、音频、视频),PLE 在软 token 合并到嵌入序列之前计算——因为 PLE 依赖多模态特征替换占位符后就会丢失的 token ID。多模态位置使用 pad token ID,有效接收中性的逐层信号。 #### https://huggingface.co/blog/gemma4#shared-kv-cache 共享 KV 缓存 共享 KV 缓存是一种效率优化,可减少推理期间的计算量和内存占用。最后 num_kv_shared_layers 层不计算自己的键和值投影。相反,它们重用同类型注意力(滑动或全上下文)中最后一个非共享层的 K 和 V 张量。 实践中,这对质量影响极小,同时对于长上下文生成和设备端使用而言,在内存和计算方面都高效得多。 ## https://huggingface.co/blog/gemma4#multimodal-capabilities 多模态能力 我们在测试中看到,Gemma 4 开箱即用地支持全面的多模态能力。我们不知道具体的训练混合比例,但我们成功将其用于 OCR、语音转文本、目标检测或指向等任务。它还支持纯文本和多模态函数调用、推理、代码补全和修正。 这里,我们展示不同模型尺寸下的几个推理示例。你可以方便地使用此笔记本 (https://github.com/huggingface/huggingface-gemma-recipes/blob/main/notebooks/Gemma4_(E2B)-Multimodal.ipynb) 运行它们。我们鼓励你尝试这些演示并在博客下方分享! ### https://huggingface.co/blog/gemma4#object-detection-and-pointing 目标检测与指向 ### https://huggingface.co/blog/gemma4#gui-detection GUI 检测 我们在不同尺寸下测试了 Gemma 4 的 GUI 元素检测和指向能力,使用以下图像和文本提示: “图片中 ‘view recipe’ 元素的边界框是什么?” 图像 (https://huggingface.co/datasets/merve/vlm_test_images/resolve/main/food_resized.png) 对此提示,模型原生以 JSON 格式回复检测到的边界框——无需特定指令或语法约束生成。我们发现坐标参照 1000x1000 的图像尺寸,相对于输入尺寸。我们在下方为你可视化输出。我们从返回的 JSON 解析边界框:json\n[\n {"box_2d": [171, 75, 245, 308], "label": "view recipe element"}\n]\n ### https://huggingface.co/blog/gemma4#object-detection 目标检测 我们测试模型检测日常物体,这里要求它们检测自行车并比较不同模型的输出。与前面的情况类似,我们从 json 解析边界框并转换为图像空间坐标。 ### https://huggingface.co/blog/gemma4#multimodal-thinking-and-function-calling 多模态推理与函数调用 我们让 Gemma 4 编写 HTML 代码来重建我们用 Gemini 3 制作的页面。你可以在下方找到执行此操作的代码,我们启用推理并让每个模型生成最多 4000 个新 token,以确保万无一失。 推理代码 messages = [ { "role": "user", "content": [ { "type": "image", "image": "https://huggingface.co/datasets/merve/vlm_test_images/resolve/main/landing_page.png", }, {"type": "text", "text": "Write HTML code for this page."}, ], } ] inputs = processor.apply_chat_template( messages, tokenize=True, return_dict=True, return_tensors="pt", add_generation_prompt=True, enable_thinking=True, ).to(model.device) output = model.generate(**inputs, max_new_tokens=4000) input_len = inputs.input_ids.shape[-1] generated_text_ids = output[0][input_len:] generated_text = processor.decode(generated_text_ids, skip_special_tokens=True) result = processor.parse_response(generated_text) print(result["content"]) ### https://huggingface.co/blog/gemma4#video-understanding 视频理解 较小的 Gemma 4 模型可以接收带音频的视频,而较大的模型可以接收不带音频的视频。虽然模型没有专门针对视频进行后训练,但它们能理解带音频和不带音频的视频。该模型在音频方面特别出色。 推理代码 messages = [ { "role": "user", "content": [ {"type": "video", "url": "https://huggingface.co/datasets/merve/vlm_test_images/resolve/main/concert.mp4"}, {"type": "text", "text": "What is happening in the video? What is the song about?"}, ], }, ] inputs = processor.apply_chat_template( messages, tokenize=True, return_dict=True, return_tensors="pt", add_generation_prompt=True, load_audio_from_video=True, # 对较大模型禁用此项 ).to(model.device) output = model.generate(**inputs, max_new_tokens=200) input_len = inputs.input_ids.shape[-1] generated_text_ids = output[0][input_len:] generated_text = processor.decode(generated_text_ids, skip_special_tokens=True) print(result["content"]) 模型输出E2B(音频出现幻觉)Based on the video provided, here is an analysis:**What is happening in this video?**The video captures a live musical performance, likely at a large outdoor festival or concert. ***Setting:**The scene is a large stage set up outdoors, indicated by the extensive lighting rig, large speakers, and the audience visible in the foreground. ***Performers:**There are several musicians on stage. The focal point seems to be a lead singer/guitarist in a blue shirt and white pants who is actively performing, playing an acoustic or electric guitar. There are also other musicians, including drummers and bassists, visible in the background. ***Atmosphere:**The atmosphere is energetic and dramatic, characterized by bright stage lights, intense blue and white spotlights, and a significant amount of stage fog/smoke, which adds to the concert mood. ***Audience:**The foreground is filled with the backs of audience members, showing they are close to the stage and engaged in watching the performance.**What is the song about?**The lyrics provided below the video are: > “I don’t care ’bout street > From what I cherish > Could it’s time to protestE4B ✅The video captures a live performance of a band on a stage with bright lights and smoke effects, surrounded by an audience. The band performs and sings to the crowd. The song appears to be about the struggles and disillusionment of modern life, specifically the feeling of being stuck and the difficulties in finding one’s place in a chaotic and overwhelming world. Lyrics mention themes of the “concrete street,” “broken promises,” “storm of the day,” and the difficulty of finding oneself amidst societal pressures.26/A4B

相似文章

google/gemma-4-31B-it-assistant

Hugging Face Models Trending

Google DeepMind 发布了 Gemma 4,这是一个开源权重的多模态模型家族,支持文本、图像、视频和音频,具备增强的推理和编码能力,并通过多令牌预测(MTP)实现高达 2 倍的解码速度提升。

google/gemma-4-26B-A4B-it

Hugging Face Models Trending

Google DeepMind 发布 Gemma 4,一系列开放权重的多模态模型,参数量从2.3B到31B,支持文本、图像、视频和音频输入。模型具有256K上下文窗口,MoE和密集架构,增强的推理能力,并针对从移动设备到服务器的部署进行优化。

google/gemma-4-E4B-it-assistant

Hugging Face Models Trending

Google DeepMind 发布了 Gemma 4 E4B 指令微调助手模型,该模型具备多模态能力、推理改进以及针对低延迟端侧应用优化的投机解码功能。