Gemma 3n 全面接入主流開源工具庫,推出 E2B、E4B 尺寸模型
Gemma 3n fully available in the open-source ecosystem!
Gemma 3n 已接入多個主流開源工具庫,並推出 E2B、E4B 兩種尺寸的 base 與 instruct 版本。支援工具包括 Transformers、timm、MLX、llama.cpp、Transformers.js 和 Ollama;兩款模型實際參數量為 5B 和 8B,GPU 記憶體佔用約相當於 2B 和 4B 模型。
文章並列模型的實際參數量、GPU 記憶體需求和多個工具庫的接入方式,方便衡量 Gemma 3n 本地多模態部署時的硬件門檻與工具選擇。
Gemma 3n 在 Google I/O 期間以預覽版形式公佈。裝置端社羣非常興奮,因為這款模型從設計之初便以在你的硬件上本機執行為目標。除此之外,它原生支援多模態,可接收圖像、文字、音訊和影片輸入 🤯
今天,Gemma 3n 終於登陸最常用的開源函式庫,包括 transformers & timm、MLX、llama.cpp(文字輸入)、transformers.js、ollama、Google AI Edge 等。
本文會簡介一些實用程式碼片段,示範如何透過這些函式庫使用模型,以及如何輕鬆微調模型以應用於其他領域。
今日發布的模型
以下是 Gemma 3n 發布系列
今天共發布兩種模型尺寸,各有兩個變體(base 和 instruct)。模型名稱採用非標準命名方式,分別為 gemma-3n-E2B 和 gemma-3n-E4B。參數量前的 E 代表 Effective。兩者實際參數量分別為 5B 和 8B,但得益於記憶體效率改進,只需 2B 和 4B VRAM(GPU 記憶體)即可執行。
因此,從硬件支援來看,這些模型的需求相當於 2B 和 4B,但品質卻勝過 2B/4B 模型。E2B 模型只需 2GB GPU RAM 即可執行,E4B 則只需 3GB GPU RAM。
| 尺寸 | Base | Instruct |
|---|---|---|
| 2B | google/gemma-3n-e2b | google/gemma-3n-e2b-it |
| 4B | google/gemma-3n-e4b | google/gemma-3n-e4b-it |
模型詳情
Gemma 3n 除了語言解碼器,亦使用音訊編碼器和視覺編碼器。以下介紹兩者的主要功能,並説明如何將它們加入 transformers 和 timm;這兩個函式庫是其他實作的參考標準。
- 視覺編碼器(MobileNet-V5)。 Gemma 3n 使用新版 MobileNet:MobileNet-v5-300,已加入新版的
timm今日發布。- 具備 300M 個參數。
- 支援
256x256、512x512和768x768解析度。 - 在 Google Pixel 上達到 60 FPS,參數數量比 ViT Giant 少 3 倍,效能更佳。
- 音訊編碼器:
- 以 Universal Speech Model(USM)為基礎。
- 以
160ms為單位處理音訊。 - 提供語音轉文字和翻譯功能(例如英語翻譯成西班牙語/法語)。
- Gemma 3n 架構與語言模型。架構已加入今日發布的新版本
transformers。此實作會把圖像編碼交由timm處理,因此 MobileNet 架構只有一個參考實作。
架構重點
- MatFormer 架構:
- 類似 Matryoshka embedding 的巢狀 transformer 設計,可抽取不同層子集,並視為獨立模型。
- E2B 和 E4B 一同訓練,並將 E2B 設定為 E4B 的子模型。
- 用戶可按硬件特性和記憶體預算,自由「混搭」各層。
- 逐層嵌入(PLE):把嵌入移至 CPU,減少加速器記憶體用量。這就是 E2B 模型雖有 5B 實際參數,卻只佔用相當於 2B 參數模型 GPU 記憶體的原因。
- KV 快取共享:加快音訊和影片長上下文處理;相較 Gemma 3 4B,prefill 速度提升 2 倍。
效能和基準測試:
- LMArena 分數:E4B 是首個分數達 1300+ 的 10B 以下模型。
- MMLU 分數:Gemma 3n 在不同尺寸(E4B、E2B 和多種 Mix-n-Match 配置)均有競爭力。
- 多語言支援:支援 140 種文字語言,以及 35 種多模態互動語言。
示範 Space
要快速體驗模型,最簡單的方法是使用專為它建立的 Hugging Face Space。你可以在這裡嘗試不同模態的各種提示詞。
使用 transformers 推論
執行推論或微調模型前,請先安裝最新版 timm(視覺編碼器所需)和 transformers。
pip install -U -q timm
pip install -U -q transformers
使用 pipeline 推論
使用 Gemma 3n 最簡單的方法,是透過 transformers 中的 pipeline 抽象層:
import torch
from transformers import pipeline
pipe = pipeline(
"image-text-to-text",
model="google/gemma-3n-E4B-it", # "google/gemma-3n-E4B-it"
device="cuda",
torch_dtype=torch.bfloat16
)
messages = [
{
"role": "user",
"content": [
{"type": "image", "url": "https://huggingface.co/datasets/ariG23498/demo-data/resolve/main/airplane.jpg"},
{"type": "text", "text": "Describe this image"}
]
}
]
output = pipe(text=messages, max_new_tokens=32)
print(output[0]["generated_text"][-1]["content"])
輸出:
The image shows a futuristic, sleek aircraft soaring through the sky. It's designed with a distinctive, almost alien aesthetic, featuring a wide body and large
使用 transformers 詳細推論
從 Hub 初始化模型和處理器,並撰寫 model_generation 函數,負責處理提示詞和執行模型推論。
from transformers import AutoProcessor, AutoModelForImageTextToText
import torch
model_id = "google/gemma-3n-e4b-it" # google/gemma-3n-e2b-it
processor = AutoProcessor.from_pretrained(model_id)
model = AutoModelForImageTextToText.from_pretrained(model_id).to(device)
def model_generation(model, messages):
inputs = processor.apply_chat_template(
messages,
add_generation_prompt=True,
tokenize=True,
return_dict=True,
return_tensors="pt",
)
input_len = inputs["input_ids"].shape[-1]
inputs = inputs.to(model.device, dtype=model.dtype)
with torch.inference_mode():
generation = model.generate(**inputs, max_new_tokens=32, disable_compile=False)
generation = generation[:, input_len:]
decoded = processor.batch_decode(generation, skip_special_tokens=True)
print(decoded[0])
由於模型支援所有模態作為輸入,以下簡單説明如何透過 transformers 使用它們。
純文字
# Text Only
messages = [
{
"role": "user",
"content": [
{"type": "text", "text": "What is the capital of France?"}
]
}
]
model_generation(model, messages)
輸出:
The capital of France is **Paris**.
交錯使用音訊
# Interleaved with Audio
messages = [
{
"role": "user",
"content": [
{"type": "text", "text": "Transcribe the following speech segment in English:"},
{"type": "audio", "audio": "https://huggingface.co/datasets/ariG23498/demo-data/resolve/main/speech.wav"},
]
}
]
model_generation(model, messages)
輸出:
Send a text to Mike. I'll be home late tomorrow.
交錯使用圖像/影片
影片支援方式是將多個圖像影格組成一組。
# Interleaved with Image
messages = [
{
"role": "user",
"content": [
{"type": "image", "image": "https://huggingface.co/datasets/ariG23498/demo-data/resolve/main/airplane.jpg"},
{"type": "text", "text": "Describe this image."}
]
}
]
model_generation(model, messages)
輸出:
The image shows a futuristic, sleek, white airplane against a backdrop of a clear blue sky transitioning into a cloudy, hazy landscape below. The airplane is tilted at
使用 MLX 推論
Gemma 3n 在發布當日便全面支援 MLX 的 3 種模態。請確保升級 mlx-vlm 安裝版本。
pip install -u mlx-vlm
開始使用視覺功能:
python -m mlx_vlm.generate --model google/gemma-3n-E4B-it --max-tokens 100 --temperature 0.5 --prompt "Describe this image in detail." --image https://huggingface.co/datasets/ariG23498/demo-data/resolve/main/airplane.jpg
以及音訊:
python -m mlx_vlm.generate --model google/gemma-3n-E4B-it --max-tokens 100 --temperature 0.0 --prompt "Transcribe the following speech segment in English:" --audio https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/blog/audio-samples/jfk.wav
使用 llama.cpp 推論
除了 MLX,Gemma 3n(僅文字)亦可直接使用 llama.cpp。請確保從原始碼安裝 llama.cpp/Ollama。
請參閲 llama.cpp 的安裝説明:https://github.com/ggml-org/llama.cpp/blob/master/docs/install.md
可按以下方式執行:
llama-server -hf ggml-org/gemma-3n-E4B-it-GGUF:Q8_0
使用 Transformers.js 和 ONNXRuntime 推論
最後,我們亦發布了 gemma-3n-E2B-it 模型變體的 ONNX 權重,方便在不同執行環境和平台靈活部署。對 JavaScript 開發者而言,Gemma 3n 已整合至 Transformers.js,並自版本 3.6.0 起提供。
如需瞭解如何透過這些函式庫執行模型,請參閲 模型卡中的使用説明。
在免費 Google Colab 微調
考慮到模型大小,按不同模態的下游任務進行微調相當方便。為了令微調更簡單,我們建立了一份簡易筆記本,讓你可在免費的 Google Colab 上試驗!
我們亦提供專用的音訊任務微調筆記本,讓你輕鬆使用自己的語音數據集和基準測試調整模型!
Hugging Face Gemma Recipes
隨著這次發布,我們亦推出 Hugging Face Gemma Recipes 儲存庫,當中有可用來執行和微調模型的 notebooks 和 scripts。
我們很希望你使用 Gemma 模型系列,並為它新增更多 recipes!歡迎在儲存庫提出 Issues 和建立 Pull Requests。
總結
我們一直很高興能與 Google 合作,推出 Gemma 模型系列。希望社羣能一起善用這些模型。多模態、尺寸小而能力強,是一次出色的模型發布!
若想深入討論這些模型,歡迎在本文下方發起討論。我們很樂意提供協助!
衷心感謝 Arthur、Cyril、Raushan、Lysandre,以及 Hugging Face 所有負責整合並讓社羣可以使用這些模型的人!
來源:Hugging Face Blog · huggingface.co
