
Gemma 3 行動端:微調與裝置端部署
如何將 Google 的 Gemma 3 模型用於裝置端行動 AI。模型選擇、LoRA 微調、GGUF 匯出,以及透過 llama.cpp 在 iOS 和 Android 上部署。
Google 的 Gemma 3 相比 Gemma 2 是一次重大的進步。1B 模型適合行動端的分類任務,而 4B 模型提供的推理能力可以與其他系列的更大模型競爭。
對於已經在 Google 生態系統中的行動開發者(Android、Firebase、Google Cloud),Gemma 是具有良好工具支援的自然選擇。
Gemma 3 行動端模型陣容
| 模型 | 參數量 | GGUF Q4 大小 | 所需 RAM | 行動可行性 |
|---|---|---|---|---|
| Gemma 3 1B | 1B | 約 600MB | 約 800MB | 優秀(4GB+ 裝置) |
| Gemma 3 4B | 4B | 約 2.3GB | 約 3GB | 良好(8GB+ 裝置) |
| Gemma 3 12B | 12B | 約 7GB | 約 9GB | 不適合行動端 |
| Gemma 3 27B | 27B | 約 15GB | 約 18GB | 不適合行動端 |
1B 和 4B 模型是與行動端相關的尺寸。4B 比典型的 3B 目標略大,但在 8GB 裝置上可以在預算內運行。
Gemma 3 對比 Gemma 2
| 改進項目 | Gemma 2 | Gemma 3 |
|---|---|---|
| 指令遵循(IFEval) | 51.2(2B) | 54.2(1B) |
| 通用知識(MMLU) | 51.3(2B) | 46.8(1B)、67.2(4B) |
| 多語言支援 | 20 種語言 | 35+ 種語言 |
| 上下文視窗(1B) | 8K | 32K |
| 上下文視窗(4B) | 8K | 128K |
Gemma 3 的 4B 模型表現突出。它接近 Llama 3.2 8B(不適合行動端)的能力,同時能在旗艦行動裝置上運行。
何時 Gemma 3 是正確的選擇
Google 生態系統整合: 如果你已經使用 Firebase、Android Studio 和 Google Cloud,Gemma 擁有最順暢的工具鏈路徑。Google 提供 Keras 整合、Vertex AI 微調和 Android 專屬文件。
旗艦裝置上的 4B 品質: 如果你的應用程式針對旗艦裝置,且需要比 3B 模型更強的推理能力,Gemma 3 4B 填補了這個空缺。它介於典 型的 3B 和 7B 類別之間。
多語言需求: Gemma 3 的 35+ 語言支援比 Llama 3.2 更廣泛(雖然不如 Qwen)。對於歐洲和南亞語言的應用程式,Gemma 是一個強有力的選擇。
微調 Gemma 3
訓練資料格式
Gemma 使用帶有 <start_of_turn> 和 <end_of_turn> token 的特定聊天範本:
<start_of_turn>user
What's the return policy for electronics?<end_of_turn>
<start_of_turn>model
Electronics purchased within the last 30 days can be returned with receipt for a full refund. Items must be in original packaging.<end_of_turn>
進行微調時,按照此範本將你的資料組織為對話格式。大多數訓練框架(Hugging Face、Axolotl、Unsloth)在指定 Gemma 作為模型類型時會自動處理範本格式化。
LoRA 設定
| 參數 | 1B | 4B |
|---|---|---|
| LoRA 秩(r) | 16-32 | 16-64 |
| LoRA alpha | 32-64 | 32-128 |
| 學習率 | 2e-4 | 1e-4 |
| 訓練輪數 | 3-5 | 2-4 |
| 目標模組 | q_proj, v_proj, k_proj, o_proj | 相同 |
| 適配器大小 | 30-80MB | 50-150MB |
訓練資料需求
與其他模型系列相同的準則適用:
| 任務 | 最少範例數 | 建議範例數 |
|---|---|---|
| 分類 | 200 | 500-1,000 |
| 問答 | 300 | 1,000-2,000 |
| 聊天 | 500 | 2,000-5,000 |
微調後的品質
Gemma 3 對微調反應良好。1B 模型只需 500 個範例就能從通用的平庸表現躍升到領域專屬的專業水準。4B 模型微調後在狹窄任務上的品質可與提示式 GPT-4o 匹敵。
預期準確率範圍(領域特定分類):
- 1B 基礎:65-72%
- 1B 微調後(500 個範例):88-92%
- 4B 基礎:75-80%
- 4B 微調後(500 個範例):92-96%
GGUF 匯出
Gemma 3 模型使用標準的 llama.cpp 轉換工具轉換為 GGUF 格式。流程如下:
- 使用 LoRA 微調
- 將 LoRA 適配器合併到基礎權重中
- 使用
convert_hf_to_gguf.py轉換為 GGUF - 使用
llama-quantize量化至 Q4_K_M
像 Ertas 這樣的平台自動化了這個流程:選擇 Gemma 3 作為基礎模型、上傳訓練資料、訓練,並以你想要的量化等級直接匯出為 GGUF。
在 iOS 和 Android 上部署
Gemma 3 GGUF 模型在 llama.cpp 上的運行方式與 Llama 或任何其他 GGUF 模型完全相同。部署流程一樣:
iOS: 透過帶有 Metal 加速的 llama.cpp 載入 GGUF。無需 Gemma 特定的設定。
Android: 透過帶有 Vulkan GPU 加速的 llama.android 載入。API 與任何其他 GGUF 模型相同。
GGUF 作為通用格式的優勢在於,你的部署基礎設施可以與任何模型系列一起使用。從 Llama 切換到 Gemma(或反之)只需要替換模型檔案。
行動裝置上的效能
Gemma 3 1B(Q4_K_M,約 600MB)
| 裝置 | Tokens/秒 | 記憶體 |
|---|---|---|
| iPhone 16 Pro | 38-48 | 約 800MB |
| iPhone 15 | 26-34 | 約 800MB |
| Galaxy S24(Vulkan) | 38-48 | 約 800MB |
| 中階 Android | 18-25 | 約 800MB |
Gemma 3 4B(Q4_K_M,約 2.3GB)
| 裝置 | Tokens/秒 | 記憶體 |
|---|---|---|
| iPhone 16 Pro | 16-22 | 約 3.0GB |
| iPhone 15 Pro | 14-20 | 約 3.0GB |
| Galaxy S24(Vulkan) | 18-24 | 約 3.0GB |
| Galaxy S25(Vulkan) | 20-28 | 約 3.0GB |
4B 模型比 3B 模型略慢,但差異很小。在旗艦裝置上,它仍然遠超 10 tok/s 的可用性門檻。
Gemma 對比 Gemini Nano
Google 提供 Gemma(用於自行部署的開放模型)和 Gemini Nano(透過 Android AICore 的裝置端模型)。它們服務於不同的目的:
| 因素 | Gemma 3(GGUF) | Gemini Nano |
|---|---|---|
| 自訂微調 | 是 | 否 |
| 裝置覆蓋率 | 任何 4GB+ 裝置 | 僅 Pixel 8+、Galaxy S24+ |
| 模型控制 | 完全 | 無 |
| 任務 | 任何文字生成 | 有限的預定義任務 |
| 平台 | iOS 和 Android | 僅 Android |
| 成本 | 免費(裝置端) | 免費(裝置端) |
如果你需要自訂 AI 行為、領域特定知識或跨平台部署,透過 GGUF 使用 Gemma 是正確的路徑。Gemini Nano 僅適用於在有限裝置集上的預定義任務。
授權條款
Gemma 3 使用 Gemma 使用條款:
- 商業使用:允許
- 微調和修改:允許
- 散布:允許
- 無月活躍用戶門檻(不同於 Llama 的 7 億限制)
- 不得使用輸出來訓練與 Gemini 競爭的模型
該授權對大多數行動應用使用場景是實用的。關於競爭性模型訓練的限制不太可能影響行動開發者。
Ship AI that runs on your users' devices.
Early bird pricing starts at $14.50/mo — locked in for life. Plans for builders and agencies.
Ship AI that runs on your users' devices.
Early bird pricing starts at $14.50/mo — locked in for life. Plans for builders and agencies.
Keep reading

On-Device AI Model Size Guide: 1B vs 3B vs 7B for Mobile
How to choose the right model size for your mobile app. Capability breakdown, device requirements, quality benchmarks, and the fine-tuning factor that changes the math.

Llama 3.2 for Mobile Apps: Fine-Tuning and On-Device Deployment
A complete guide to using Meta's Llama 3.2 1B and 3B models in mobile apps. Fine-tuning with LoRA, exporting to GGUF, and deploying on iOS and Android via llama.cpp.

How to Add AI to Your Mobile App: A Developer's Decision Guide
A comprehensive guide covering every approach to adding AI features to iOS and Android apps. Cloud APIs, on-device models, and hybrid architectures compared with real cost and performance data.