模型简介
模型特点
模型能力
使用案例
许可证:gemma 库名称:transformers 流水线标签:图像文本到文本 额外授权标题:在Hugging Face上访问Gemma 额外授权提示:要在Hugging Face上访问Gemma,您需要审阅并同意Google的使用许可。为此,请确保您已登录Hugging Face并点击下方按钮。请求将立即处理。 额外授权按钮内容:确认许可 基础模型:google/gemma-3-4b-it
Gemma 3 4B 指令调优 INT4
这是从Kaggle获取的QAT INT4 Flax检查点,已转换为HF+AWQ格式以便使用。AWQ未用于量化。您可以在本模型仓库中找到转换脚本convert_flax.py
。
注意:这与官方发布的QAT INT4 GGUFs不同,后者位于https://huggingface.co/collections/google/gemma-3-qat-67ee61ccacbf2be4195c265b
以下是来自https://huggingface.co/google/gemma-3-4b-it的原始模型卡片。
Gemma 3 模型卡片
模型页面: Gemma
资源与技术文档:
- [Gemma 3 技术报告][g3-tech-report]
- [负责任生成AI工具包][rai-toolkit]
- [Kaggle上的Gemma][kaggle-gemma]
- [Vertex Model Garden上的Gemma][vertex-mg-gemma3]
使用条款: [条款][terms]
作者: Google DeepMind
模型信息
简要描述模型及其输入输出的定义。
描述
Gemma是Google推出的一系列轻量级、先进的开放模型,基于与创建Gemini模型相同的研究和技术构建。Gemma 3模型是多模态的,能够处理文本和图像输入并生成文本输出,提供预训练和指令调优两种变体的开放权重。Gemma 3拥有128K的大上下文窗口,支持超过140种语言,并且比之前的版本提供更多尺寸选择。Gemma 3模型适用于多种文本生成和图像理解任务,包括问答、摘要和推理。其相对较小的尺寸使其能够在资源有限的环境中部署,如笔记本电脑、台式机或您自己的云基础设施,从而普及先进AI模型的访问,促进创新。
输入和输出
-
输入:
- 文本字符串,如问题、提示或待摘要的文档
- 图像,归一化为896 x 896分辨率并编码为每个256个token
- 4B、12B和27B尺寸的总输入上下文为128K token,1B尺寸为32K token
-
输出:
- 根据输入生成的文本,如问题的答案、图像内容的分析或文档的摘要
- 总输出上下文为8192 token
使用
以下是一些快速开始运行模型的代码片段。首先,安装专为Gemma 3设计的Transformers库版本:
$ pip install git+https://github.com/huggingface/transformers@v4.49.0-Gemma-3
然后,复制与您的用例相关的代码片段。
使用pipeline
API运行
您可以通过pipeline
初始化模型和处理器进行推理:
from transformers import pipeline
import torch
pipe = pipeline(
"image-text-to-text",
model="google/gemma-3-4b-it",
device="cuda",
torch_dtype=torch.bfloat16
)
对于指令调优模型,您需要先使用聊天模板处理输入,然后将其传递给pipeline:
messages = [
{
"role": "system",
"content": [{"type": "text", "text": "You are a helpful assistant."}]
},
{
"role": "user",
"content": [
{"type": "image", "url": "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/p-blog/candy.JPG"},
{"type": "text", "text": "What animal is on the candy?"}
]
}
]
output = pipe(text=messages, max_new_tokens=200)
print(output[0][0]["generated_text"][-1]["content"])
# Okay, let's take a look!
# Based on the image, the animal on the candy is a **turtle**.
# You can see the shell shape and the head and legs.
在单/多GPU上运行模型
# pip install accelerate
from transformers import AutoProcessor, Gemma3ForConditionalGeneration
from PIL import Image
import requests
import torch
model_id = "google/gemma-3-4b-it"
model = Gemma3ForConditionalGeneration.from_pretrained(
model_id, device_map="auto"
).eval()
processor = AutoProcessor.from_pretrained(model_id)
messages = [
{
"role": "system",
"content": [{"type": "text", "text": "You are a helpful assistant."}]
},
{
"role": "user",
"content": [
{"type": "image", "image": "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/bee.jpg"},
{"type": "text", "text": "Describe this image in detail."}
]
}
]
inputs = processor.apply_chat_template(
messages, add_generation_prompt=True, tokenize=True,
return_dict=True, return_tensors="pt"
).to(model.device, dtype=torch.bfloat16)
input_len = inputs["input_ids"].shape[-1]
with torch.inference_mode():
generation = model.generate(**inputs, max_new_tokens=100, do_sample=False)
generation = generation[0][input_len:]
decoded = processor.decode(generation, skip_special_tokens=True)
print(decoded)
# **Overall Impression:** The image is a close-up shot of a vibrant garden scene,
# focusing on a cluster of pink cosmos flowers and a busy bumblebee.
# It has a slightly soft, natural feel, likely captured in daylight.
引用
@article{gemma_2025,
title={Gemma 3},
url={https://goo.gle/Gemma3Report},
publisher={Kaggle},
author={Gemma Team},
year={2025}
}
模型数据
用于模型训练的数据及其处理方式。
训练数据集
这些模型在包含多种来源的文本数据集上训练。27B模型训练使用了14万亿token,12B模型使用了12万亿token,4B模型使用了4万亿token,1B模型使用了2万亿token。以下是关键组成部分:
- 网络文档:多样化的网络文本确保模型接触到广泛的文体、主题和词汇。训练数据集包含超过140种语言的内容。
- 代码:让模型接触代码有助于学习编程语言的语法和模式,提高生成代码和理解代码相关问题的能力。
- 数学:数学文本训练帮助模型学习逻辑推理、符号表示和解决数学查询。
- 图像:广泛的图像使模型能够执行图像分析和视觉数据提取任务。
这些多样数据源的组合对于训练一个能够处理各种任务和数据格式的强大多模态模型至关重要。
数据预处理
以下是应用于训练数据的关键数据清理和过滤方法:
- CSAM过滤:在数据准备过程的多个阶段应用严格的CSAM(儿童性虐待材料)过滤,确保排除有害和非法内容。
- 敏感数据过滤:作为使Gemma预训练模型安全可靠的一部分,使用自动化技术过滤掉训练集中的某些个人信息和其他敏感数据。
- 其他方法:根据[我们的政策][safety-policies]基于内容质量和安全性进行过滤。
实现信息
关于模型内部的详细信息。
硬件
Gemma使用[Tensor Processing Unit (TPU)][tpu]硬件(TPUv4p、TPUv5p和TPUv5e)训练。训练视觉语言模型(VLMs)需要大量计算资源。TPU专为机器学习中常见的矩阵操作设计,在此领域具有以下优势:
- 性能:TPU专为处理训练VLM涉及的大规模计算而设计,相比CPU可显著加速训练。
- 内存:TPU通常配备大量高带宽内存,可在训练期间处理大型模型和批量大小,从而提高模型质量。
- 可扩展性:TPU Pods(大型TPU集群)为处理大型基础模型日益增长的复杂性提供了可扩展解决方案。您可以跨多个TPU设备分发训练以实现更快、更高效的处理。
- 成本效益:在许多场景中,TPU相比基于CPU的基础设施可以提供更具成本效益的解决方案,特别是在考虑因更快训练而节省的时间和资源时。
- 这些优势与[Google的可持续发展承诺][sustainability]一致。
软件
训练使用[JAX][jax]和[ML Pathways][ml-pathways]完成。
JAX允许研究人员利用包括TPU在内的最新硬件,更快、更高效地训练大型模型。ML Pathways是Google最新努力,旨在构建能够跨多个任务泛化的人工智能系统。这特别适合基础模型,包括这些大型语言模型。
如[关于Gemini模型家族的论文][gemini-2-paper]所述,JAX和ML Pathways一起使用:“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 |









