模型概述
模型特點
模型能力
使用案例
🚀 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}
}
⚠️ 重要提示
本評估僅使用了英語提示,存在一定侷限性。
💡 使用建議
在使用這些模型時,用戶應注意其侷限性,如訓練數據的質量和多樣性可能影響模型的響應能力,複雜任務和模糊語言可能對模型性能產生挑戰。同時,模型的性能可能受到上下文長度的影響,適當提供更長的上下文通常有助於獲得更好的輸出,但也存在一定的上限。








