Google Gemma 3 开源模型本地部署教程:1B到27B全覆盖
Gemma 3 是 Google 推出的第三代开源大模型,提供 1B/4B/12B/27B 四个版本,支持 128K 上下文和多模态输入(4B+版本)。它是 Gemini 技术的开源版。
📊 版本选择
| 版本 | 参数 | 显存 | 多模态 | 适用场景 |
|---|---|---|---|---|
| 1B | 10亿 | 2GB | ❌ | 手机/嵌入式 |
| 4B | 40亿 | 4GB | ✅ | 轻量应用 |
| 12B | 120亿 | 10GB | ✅ | 均衡之选 |
| 27B | 270亿 | 20GB | ✅ | 高性能需求 |
🚀 快速上手
# 用 Ollama 运行
ollama run gemma3:4b
# 用 Transformers
from transformers import AutoModelForCausalLM, AutoTokenizer
model = AutoModelForCausalLM.from_pretrained("google/gemma-3-4b")
tokenizer = AutoTokenizer.from_pretrained("google/gemma-3-4b")
inputs = tokenizer("用Python写一个Hello World", return_tensors="pt")
outputs = model.generate(**inputs, max_new_tokens=256)
print(tokenizer.decode(outputs[0]))
🖼️ 多模态图片理解(4B+)
from transformers import Gemma3ForConditionalGeneration, AutoProcessor
model = Gemma3ForConditionalGeneration.from_pretrained("google/gemma-3-4b-it")
processor = AutoProcessor.from_pretrained("google/gemma-3-4b-it")
messages = [{
"role": "user",
"content": [
{"type": "image", "image": "photo.jpg"},
{"type": "text", "text": "描述这张图片"}
]
}]
inputs = processor.apply_chat_template(messages, add_generation_prompt=True)
🎯 总结
Gemma 3 是 Google 对开源社区的重磅贡献。4B版本只需4GB显存就能跑多模态,27B版本在多项基准上超过同级竞品。配合 Ollama 使用极其方便。

发表回复