模型简介
模型特点
模型能力
使用案例
🚀 Gemma 3模型卡片
Gemma 3是谷歌推出的轻量级、最先进的开放模型家族,基于与Gemini模型相同的研究和技术构建。它支持多模态,能处理文本和图像输入并生成文本输出,适用于多种文本生成和图像理解任务。
🚀 快速开始
要在Hugging Face上访问Gemma,你需要查看并同意Google的使用许可。请确保你已登录Hugging Face,然后点击下方按钮,请求将立即处理。
按钮内容:确认许可
✨ 主要特性
- 多模态处理:能够处理文本和图像输入,并生成文本输出。
- 大上下文窗口:拥有128K的大上下文窗口,支持超过140种语言。
- 资源友好:相对较小的模型规模,可在笔记本电脑、台式机或自有云基础设施等资源有限的环境中部署。
📦 安装指南
首先,安装Transformers库。Gemma 3从transformers 4.50.0版本开始支持。
$ pip install -U transformers
💻 使用示例
基础用法
from transformers import pipeline
import torch
pipe = pipeline(
"image-text-to-text",
model="google/gemma-3-4b-pt",
device="cuda",
torch_dtype=torch.bfloat16
)
output = pipe(
"https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/bee.jpg",
text="<start_of_image> in this image, there is"
)
print(output)
# [{'input_text': '<start_of_image> in this image, there is',
# 'generated_text': '<start_of_image> in this image, there is a bumblebee on a pink flower.\n\n'}]
高级用法
# pip install accelerate
from transformers import AutoProcessor, Gemma3ForConditionalGeneration
from PIL import Image
import requests
import torch
model_id = "google/gemma-3-4b-pt"
url = "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/bee.jpg"
image = Image.open(requests.get(url, stream=True).raw)
model = Gemma3ForConditionalGeneration.from_pretrained(model_id).eval()
processor = AutoProcessor.from_pretrained(model_id)
prompt = "<start_of_image> in this image, there is"
model_inputs = processor(text=prompt, images=image, return_tensors="pt")
input_len = model_inputs["input_ids"].shape[-1]
with torch.inference_mode():
generation = model.generate(**model_inputs, max_new_tokens=100, do_sample=False)
generation = generation[0][input_len:]
decoded = processor.decode(generation, skip_special_tokens=True)
print(decoded)
📚 详细文档
- 模型页面:Gemma
- 资源和技术文档:
- [Gemma 3技术报告][g3-tech-report]
- [负责任的生成式AI工具包][rai-toolkit]
- [Kaggle上的Gemma][kaggle-gemma]
- [Vertex模型花园中的Gemma 3][vertex-mg-gemma3]
- 使用条款:[条款][terms]
- 作者:Google DeepMind
🔧 技术细节
硬件
Gemma使用[张量处理单元(TPU)][tpu]硬件(TPUv4p、TPUv5p和TPUv5e)进行训练。TPU专为机器学习中常见的矩阵运算而设计,在训练视觉语言模型(VLM)方面具有以下优势:
- 性能:能够处理VLM训练中的大量计算,相比CPU可显著加速训练过程。
- 内存:通常配备大量高带宽内存,可在训练过程中处理大型模型和批量数据,有助于提高模型质量。
- 可扩展性:TPU Pods(大型TPU集群)为处理大型基础模型的日益复杂性提供了可扩展的解决方案,可在多个TPU设备上分布式训练,实现更快、更高效的处理。
- 成本效益:在许多情况下,与基于CPU的基础设施相比,TPU在训练大型模型时具有更高的成本效益,特别是考虑到更快的训练速度所节省的时间和资源。
- 可持续性:这些优势符合[Google的可持续运营承诺][sustainability]。
软件
训练使用了[JAX][jax]和[ML Pathways][ml-pathways]。 JAX使研究人员能够利用最新一代的硬件(包括TPU),实现大型模型的更快、更高效训练。ML Pathways是Google构建能够跨多个任务进行泛化的人工智能系统的最新成果,特别适用于基础模型,如这些大型语言模型。 JAX和ML Pathways的结合使用在[关于Gemini模型家族的论文][gemini-2-paper]中有详细描述:“Jax和Pathways的‘单控制器’编程模型允许单个Python进程编排整个训练过程,大大简化了开发工作流程。”
🔍 评估
基准测试结果
这些模型针对大量不同的数据集和指标进行了评估,以涵盖文本生成的不同方面:
基准测试 | 指标 | Gemma 3 PT 1B | Gemma 3 PT 4B | Gemma 3 PT 12B | Gemma 3 PT 27B |
---|---|---|---|---|---|
[HellaSwag][hellaswag] | 10-shot | 62.3 | 77.2 | 84.2 | 85.6 |
[BoolQ][boolq] | 0-shot | 63.2 | 72.3 | 78.8 | 82.4 |
[PIQA][piqa] | 0-shot | 73.8 | 79.6 | 81.8 | 83.3 |
[SocialIQA][socialiqa] | 0-shot | 48.9 | 51.9 | 53.4 | 54.9 |
[TriviaQA][triviaqa] | 5-shot | 39.8 | 65.8 | 78.2 | 85.5 |
[Natural Questions][naturalq] | 5-shot | 9.48 | 20.0 | 31.4 | 36.1 |
[ARC-c][arc] | 25-shot | 38.4 | 56.2 | 68.9 | 70.6 |
[ARC-e][arc] | 0-shot | 73.0 | 82.4 | 88.3 | 89.0 |
[WinoGrande][winogrande] | 5-shot | 58.2 | 64.7 | 74.3 | 78.8 |
[BIG-Bench Hard][bbh] | few-shot | 28.4 | 50.9 | 72.6 | 77.7 |
[DROP][drop] | 1-shot | 42.4 | 60.1 | 72.2 | 77.2 |
STEM和代码
基准测试 | 指标 | Gemma 3 PT 4B | Gemma 3 PT 12B | Gemma 3 PT 27B |
---|---|---|---|---|
[MMLU][mmlu] | 5-shot | 59.6 | 74.5 | 78.6 |
[MMLU][mmlu] (Pro COT) | 5-shot | 29.2 | 45.3 | 52.2 |
[AGIEval][agieval] | 3 - 5-shot | 42.1 | 57.4 | 66.2 |
[MATH][math] | 4-shot | 24.2 | 43.3 | 50.0 |
[GSM8K][gsm8k] | 8-shot | 38.4 | 71.0 | 82.6 |
[GPQA][gpqa] | 5-shot | 15.0 | 25.4 | 24.3 |
[MBPP][mbpp] | 3-shot | 46.0 | 60.4 | 65.6 |
[HumanEval][humaneval] | 0-shot | 36.0 | 45.7 | 48.8 |
多语言
基准测试 | Gemma 3 PT 1B | Gemma 3 PT 4B | Gemma 3 PT 12B | Gemma 3 PT 27B |
---|---|---|---|---|
[MGSM][mgsm] | 2.04 | 34.7 | 64.3 | 74.3 |
[Global-MMLU-Lite][global-mmlu-lite] | 24.9 | 57.0 | 69.4 | 75.7 |
[WMT24++][wmt24pp] (ChrF) | 36.7 | 48.4 | 53.9 | 55.7 |
[FloRes][flores] | 29.5 | 39.2 | 46.0 | 48.8 |
[XQuAD][xquad] (all) | 43.9 | 68.0 | 74.5 | 76.8 |
[ECLeKTic][eclektic] | 4.69 | 11.0 | 17.2 | 24.4 |
[IndicGenBench][indicgenbench] | 41.4 | 57.2 | 61.7 | 63.4 |
多模态
基准测试 | Gemma 3 PT 4B | Gemma 3 PT 12B | Gemma 3 PT 27B |
---|---|---|---|
[COCOcap][coco-cap] | 102 | 111 | 116 |
[DocVQA][docvqa] (val) | 72.8 | 82.3 | 85.6 |
[InfoVQA][info-vqa] (val) | 44.1 | 54.8 | 59.4 |
[MMMU][mmmu] (pt) | 39.2 | 50.3 | 56.1 |
[TextVQA][textvqa] (val) | 58.9 | 66.5 | 68.6 |
[RealWorldQA][realworldqa] | 45.5 | 52.2 | 53.9 |
[ReMI][remi] | 27.3 | 38.5 | 44.8 |
[AI2D][ai2d] | 63.2 | 75.2 | 79.0 |
[ChartQA][chartqa] | 63.6 | 74.7 | 76.3 |
[VQAv2][vqav2] | 63.9 | 71.2 | 72.9 |
[BLINK][blinkvqa] | 38.0 | 35.9 | 39.6 |
[OKVQA][okvqa] | 51.0 | 58.7 | 60.2 |
[TallyQA][tallyqa] | 42.5 | 51.8 | 54.3 |
[SpatialSense VQA][ss-vqa] | 50.9 | 60.0 | 59.4 |
[CountBenchQA][countbenchqa] | 26.1 | 17.8 | 68.0 |
📄 许可证
Gemma的许可证为gemma。
🔖 引用
@article{gemma_2025,
title={Gemma 3},
url={https://goo.gle/Gemma3Report},
publisher={Kaggle},
author={Gemma Team},
year={2025}
}
⚠️ 重要提示
本评估仅使用了英语提示,存在一定局限性。
💡 使用建议
在使用这些模型时,用户应注意其局限性,如训练数据的质量和多样性可能影响模型的响应能力,复杂任务和模糊语言可能对模型性能产生挑战。同时,模型的性能可能受到上下文长度的影响,适当提供更长的上下文通常有助于获得更好的输出,但也存在一定的上限。








