本頁面說明如何對 Llama 3.1 等開放模型執行監督式和蒸餾微調。除非另有說明,否則本頁的操作說明適用於監督式微調和蒸餾微調。透過蒸餾,您可以運用較大學員模型的輸出內容,調整較小的學員模型。
支援的調整模式
- 監督式微調:
- 蒸餾微調:蒸餾微調會使用 GenAI SDK,指定教師模型生成回覆,然後用來微調較小的學生模型。
建議的微調用途
如果老師模型在目標工作上的能力遠勝於學生模型,蒸餾微調的效果最佳。建議用於將複雜的多步驟推理能力從較大的老師模型轉移至較小的學生模型,包括:
- 數學和量化推論
- 需要逐步推理的科學、醫療和其他特定領域問答
- 其他工作:強大的教師模型具有「思考」或連鎖思考行為,可持續生成比學生模型品質更高的回覆。
如果學生模型已接近老師的表現,或在簡短的檢索任務中,老師的推論追蹤記錄沒有增加價值,則蒸餾技術的效益較小。
支援的模型
支援監督式微調的模型
- Gemma 4 E2B IT (
google/gemma4@gemma-4-e2b-it) - Gemma 4 E4B IT (
google/gemma4@gemma-4-e4b-it) - Gemma 4 26B A4B IT (
google/gemma4@gemma-4-26b-a4b-it) - Gemma 4 31B IT (
google/gemma4@gemma-4-31b-it) - Gemma 3 1B IT (
google/gemma3@gemma-3-1b-it) - Gemma 3 4B IT (
google/gemma3@gemma-3-4b-it) - Gemma 3 12B IT (
google/gemma3@gemma-3-12b-it) - Gemma 3 27B IT (
google/gemma3@gemma-3-27b-it) - Medgemma 1.5 4B IT (
google/medgemma@medgemma-1.5-4b-it) - Qwen 3.6 27B (
qwen/qwen3-6@qwen3.6-27b) - Qwen 3.6 35B A3B (
qwen/qwen3-6@qwen3.6-35b-a3b) - Qwen 3.5 9B (
qwen/qwen3-5@qwen3.5-9b) - Qwen 3 4B (
qwen/qwen3@qwen3-4b) - Qwen 3 8B (
qwen/qwen3@qwen3-8b) - Qwen 3 14B (
qwen/qwen3@qwen3-14b) - Qwen 3 32B (
qwen/qwen3@qwen3-32b) - Llama 3.1 8B (
meta/llama3_1@llama-3.1-8b) - Llama 3.1 8B Instruct (
meta/llama3_1@llama-3.1-8b-instruct) - Llama 3.2 1B Instruct (
meta/llama3-2@llama-3.2-1b-instruct) - Llama 3.2 3B Instruct (
meta/llama3-2@llama-3.2-3b-instruct) - Llama 3.3 70B Instruct (
meta/llama3-3@llama-3.3-70b-instruct) - Llama 4 Scout 17B 16E Instruct (
meta/llama4@llama-4-scout-17b-16e-instruct) - GLM 4.7 Flash (
zai-org/glm-4.7-flash@glm-4.7-flash)
支援蒸餾調整的模型
支援的教師模型:
- DeepSeek R1 0528 MaaS (
deepseek-ai/deepseek-r1-0528-maas) - DeepSeek V3.2 MaaS (
deepseek-ai/deepseek-v3.2-maas) - GLM 5.2 MaaS (
zai-org/glm-5.2-maas) - Kimi K2 Thinking MaaS (
moonshotai/kimi-k2-thinking-maas) - Qwen 3 Next 80B A3B Thinking MaaS (
qwen/qwen3-next-80b-a3b-thinking-maas)
支援的學生模型:
- Qwen 3 4B (
qwen/qwen3@qwen3-4b) - Qwen 3 8B (
qwen/qwen3@qwen3-8b) - Qwen 3 14B (
qwen/qwen3@qwen3-14b) - Qwen 3 32B (
qwen/qwen3@qwen3-32b) - Gemma 3 1B IT (
google/gemma3@gemma-3-1b-it) - Gemma 3 4B IT (
google/gemma3@gemma-3-4b-it) - Gemma 3 12B IT (
google/gemma3@gemma-3-12b-it) - Gemma 3 27B IT (
google/gemma3@gemma-3-27b-it)
支援的地區
強烈建議使用全域端點 (global) 啟動微調工作,系統會選取有可用容量的支援區域,並啟動微調工作。從全域端點啟動工作時,請注意下列事項:
- 建立全域微調作業時,系統會建立對應的區域微調作業 (位於所選區域)。系統只會針對單一調整工作收取配額費用。
- 子資源 (例如 TensorBoard 實驗或模型) 會在所選目標區域中建立。您可以從完整資源名稱中擷取位置,並使用 Vertex AI SDK 查詢。
對於有進階需求的使用者 (例如將所有資源限定在單一區域),我們仍提供區域專屬的微調工作:
- 愛荷華州 (
us-central1) - 荷蘭 (
europe-west4) - 奧勒岡州 (
us-west1) - 哥倫布 (
us-east5) - 新加坡 (
asia-southeast1) - 雅加達 (
asia-southeast2)
限制
| 模型 | 規格 | 值 |
|---|---|---|
| Gemma 4 E2B IT | 調整模式 | 高效參數微調 |
| 序列長度上限 | 8192 | |
| 形式 | 文字 | |
| Gemma 4 E4B IT | 調整模式 | 高效參數微調 |
| 序列長度上限 | 8192 | |
| 形式 | 文字 | |
| Gemma 4 26B A4B IT | 調整模式 | 高效參數微調 |
| 序列長度上限 | 8192 | |
| 形式 | 文字 | |
| Gemma 4 31B IT | 調整模式 | 高效參數微調 |
| 序列長度上限 | 8192 | |
| 形式 | 文字 | |
| Gemma 3 1B IT | 調整模式 | 完整微調 |
| 序列長度上限 | 8192 | |
| 形式 | 文字 | |
| Gemma 3 4B IT | 調整模式 | 完整微調 |
| 序列長度上限 | 8192 | |
| 形式 | 文字 | |
| Gemma 3 12B IT | 調整模式 | 完整微調 |
| 序列長度上限 | 8192 | |
| 形式 | 文字 | |
| Gemma 3 27B IT | 調整模式 | 高效參數微調 完整微調 |
| 序列長度上限 | 8192 | |
| 形式 | 文字 | |
| Medgemma 1.5 4B IT | 調整模式 | 完整微調 |
| 序列長度上限 | 8192 | |
| 形式 | 文字 | |
| Qwen 3.6 27B | 調整模式 | 高效參數微調 |
| 序列長度上限 | 12288 | |
| 形式 | 文字 | |
| Qwen 3.6 35B A3B | 調整模式 | 高效參數微調 |
| 序列長度上限 | 12288 | |
| 形式 | 文字 | |
| Qwen 3.5 9B | 調整模式 | 完整微調 |
| 序列長度上限 | 8192 | |
| 形式 | 文字 | |
| Qwen 3 4B | 調整模式 | 完整微調 |
| 序列長度上限 | 8192 | |
| 形式 | 文字 | |
| Qwen 3 8B | 調整模式 | 完整微調 |
| 序列長度上限 | 8192 | |
| 形式 | 文字 | |
| Qwen 3 14B | 調整模式 | 完整微調 |
| 序列長度上限 | 8192 | |
| 形式 | 文字 | |
| Qwen 3 32B | 調整模式 | 高效參數微調 完整微調 |
| 序列長度上限 | 8192 | |
| 形式 | 文字 | |
| Llama 3.1 8B | 調整模式 | 高效參數微調 完整微調 |
| 序列長度上限 | 8192 | |
| 形式 | 文字 | |
| Llama 3.1 8B Instruct | 調整模式 | 高效參數微調 完整微調 |
| 序列長度上限 | 8192 | |
| 形式 | 文字 | |
| Llama 3.2 1B Instruct | 調整模式 | 完整微調 |
| 序列長度上限 | 8192 | |
| 形式 | 文字 | |
| Llama 3.2 3B Instruct | 調整模式 | 完整微調 |
| 序列長度上限 | 8192 | |
| 形式 | 文字 | |
| Llama 3.3 70B Instruct | 調整模式 | 高效參數微調 完整微調 |
| 序列長度上限 | 8192 | |
| 形式 | 文字 | |
| Llama 4 Scout 17B 16E Instruct | 調整模式 | 高效參數微調 |
| 序列長度上限 | 2048 | |
| 形式 | 文字 圖片* *不支援純文字和圖片範例的混合資料集。如果資料集中至少有一個圖片範例,系統就會篩除所有僅含文字的範例。 |
|
| GLM 4.7 Flash | 調整模式 | 高效參數微調 |
| 序列長度上限 | 8192 | |
| 形式 | 文字 |
事前準備
-
In the Cloud de Confiance console, on the project selector page, select or create a Cloud de Confiance project.
Roles required to select or create a project
- Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
-
Create a project: To create a project, you need the Project Creator role
(
roles/resourcemanager.projectCreator), which contains theresourcemanager.projects.createpermission. Learn how to grant roles.
-
Verify that billing is enabled for your Cloud de Confiance project.
Enable the Gemini Enterprise Agent Platform and Cloud Storage APIs, if any are not already enabled.
Roles required to enable APIs
To enable APIs, you need the
serviceusage.services.enablepermission. If you created the project, then you likely already have this permission through the Owner role (roles/owner). Otherwise, you can get this permission through the Service Usage Admin role (roles/serviceusage.serviceUsageAdmin). Learn how to grant roles.
安裝 SDK,並匯入微調方法適用的程式庫。
監督式微調
安裝並初始化 Vertex AI SDK for Python,然後匯入下列程式庫:
import os
import time
import uuid
import vertexai
vertexai.init(project=PROJECT_ID, location=REGION)
from google.cloud import aiplatform
from vertexai.tuning import sft, SourceModel
蒸餾調整
安裝下列 SDK:
pip install google-genai
然後匯入下列程式庫:
import os
import time
import uuid
from google import genai
from google.genai import types
client = genai.Client(vertexai=True, project=PROJECT_ID, location=REGION)
準備調整用資料集
如要微調模型,必須提供訓練資料集。建議您準備選用的驗證資料集,評估調整後模型的成效。
資料集必須採用下列其中一種支援的 JSON Lines (JSONL) 格式,其中每一行都包含一個微調範例。
將 JSONL 檔案上傳至 Cloud Storage。
純文字資料集
提示詞完成
{"prompt": "<prompt text>", "completion": "<ideal generated text>"}
回合制即時通訊格式
{"messages": [
{"content": "You are a chatbot that helps with scientific literature and generates state-of-the-art abstracts from articles.",
"role": "system"},
{"content": "Summarize the paper in one paragraph.",
"role": "user"},
{"content": " Here is a one paragraph summary of the paper:\n\nThe paper describes PaLM, ...",
"role": "assistant"}
]}
GenerateContent
{
"systemInstruction": {
"parts": [{ "text": "You are a chatbot that helps with scientific literature and generates state-of-the-art abstracts from articles." }]},
"contents": [
{"role": "user",
"parts": [{ "text": "Summarize the paper in one paragraph." }]},
{"role": "assistant",
"parts": [{ "text": "Here is a one paragraph summary of the paper:\n\nThe paper describes PaLM, ..." }]}
]}
多模態資料集
回合制即時通訊格式
{"messages": [
{"role": "user", "content": [
{"type": "text", "text": "You are a chatbot that helps with scientific literature and generates state-of-the-art abstracts from articles."},
{"type": "image_url", "image_url": {
"url": "gs://your-gcs-bucket/your-image.jpeg",
"detail": "low"}}]
},
{"role": "assistant", "content": [
{"type": "text", "text": "Here is a one paragraph summary of the paper:\n\nThe paper describes PaLM, ..."}]
},
{"role": "user", "content": [
{"type": "text", "text": "You are a chatbot that helps with scientific literature and generates state-of-the-art abstracts from articles."},
{"type": "image_url", "image_url": {
"url": "data:image/jpeg;base64,<base64 image>",
"detail": "low"}}]
},
{"role": "assistant", "content": [
{"type": "text", "text": "Here is a one paragraph summary of the paper:\n\nThe paper describes PaLM, ..."}]
},
]}
GenerateContent
{
"systemInstruction": {
"parts": [{ "text": "You are a chatbot that helps with scientific literature and generates state-of-the-art abstracts from articles." }]},
"contents": [
{"role": "user",
"parts": [
{"text": "You are a chatbot that helps with scientific literature and generates state-of-the-art abstracts from articles." },
{"file_data": {
"mime_type": "image/jpeg", "file_uri": "gs://your-gcs-bucket/your-image.jpeg"}}]
},
{"role": "assistant",
"parts": [{ "text": "Here is a one paragraph summary of the paper:\n\nThe paper describes PaLM, ..." }]}
]}
支援的格式包括 JPEG、PNG、WEBP 和 Base64 編碼圖片。
請注意,如果圖片儲存在與 JSONL 檔案不同的 Cloud Storage bucket 中,請務必在這兩個 bucket 中,為下列兩個服務帳戶授予 Storage 物件使用者 (roles/storage.objectUser) IAM 角色:
service-PROJECT_NUMBER@gcp-sa-vertex-moss-ft.s3ns-system.iam.gserviceaccount.comservice-PROJECT_NUMBER@gcp-sa-aiplatform.s3ns-system.iam.gserviceaccount.com
建立調整工作
你可以從以下來源調整:
- 支援的基礎模型,例如 Llama 3.1
模型架構與其中一個支援的基礎模型相同。 這可以是來自 Hugging Face 等存放區的自訂模型檢查點,也可以是先前透過 Gemini Enterprise Agent Platform 微調作業微調的模型。您可以繼續調整先前調整過的模型。
Cloud 控制台 (受監管)
您可以透過下列方式啟動微調:
- 前往模型資訊卡,按一下「微調」,然後選擇「受管理微調」。
或
- 前往「調整」頁面,然後按一下「建立調整後的模型」。
填寫參數,然後按一下「開始調整」。
這會啟動微調作業,您可以在「受管理微調」分頁的「微調」頁面中查看。
調整工作完成後,您可以在「詳細資料」分頁中查看調整後模型的相關資訊。
Agent Platform SDK (受監督)
將參數值替換成您自己的值,然後執行下列程式碼,建立微調作業:
sft_tuning_job = sft.train(
source_model=SourceModel(
base_model="meta/llama3_1@llama-3.1-8b",
# Optional, folder that is either a custom model checkpoint or previously tuned model
custom_base_model="gs://{STORAGE-URI}",
),
tuning_mode="FULL", # FULL or PEFT_ADAPTER
epochs=3,
train_dataset="gs://{STORAGE-URI}", # JSONL file
validation_dataset="gs://{STORAGE-URI}", # JSONL file
output_uri="gs://{STORAGE-URI}",
)
生成式 AI SDK (蒸餾)
將參數值替換為您自己的值,然後執行下列程式碼,建立模型壓縮微調作業:
tuning_job = client.tunings.tune(
base_model="qwen/qwen3@qwen3-4b",
training_dataset=types.TuningDataset(
gcs_uri="gs://{STORAGE-URI}"
),
config=types.CreateTuningJobConfig(
method="DISTILLATION",
base_teacher_model="qwen/qwen3-next-80b-a3b-thinking-maas",
epoch_count=3,
validation_dataset=types.TuningValidationDataset(
gcs_uri="gs://{STORAGE-URI}"
),
output_uri="gs://{STORAGE-URI}",
),
)
調整後模型構件
調整工作完成後,調整後模型的模型構件會儲存在 Cloud Storage 輸出目錄中。
gs://<output_dir>/
# (Distillation tuning only) The labeled dataset from teacher model's inference
-> distillation_labelled_dataset.jsonl
gs://<output_dir>/postprocess/node-0/checkpoints/
# Final checkpoint
-> final/
-> model-00001-of-000xx.safetensors
-> model-000yy-of-000xx.safetensors
# Intermediate checkpoints
-> checkpoint-M/
-> model-00001-of-000xx.safetensors
-> model-000yy-of-000xx.safetensors
…
-> checkpoint-N/
-> model-00001-of-000xx.safetensors
-> model-000yy-of-000xx.safetensors
- 最多可儲存 10 個檢查點。
- 如果歷元數 (E) 小於 10,則會儲存 E 個檢查點 (每個歷元各一個)。
- 範圍 M 到 N 的中繼檢查點會依序排列。請注意,中間檢查點不一定會連續編號。舉例來說,檢查點的編號可能是 1、3、5、10,而不是 1、2、3、4。
部署經過微調的模型
您可以將微調模型部署至 Gemini Enterprise Agent Platform 端點。您也可以從 Cloud Storage 匯出微調模型,並部署到其他位置。
如要將微調模型部署至 Gemini Enterprise Agent Platform 端點,請按照下列步驟操作:
Cloud Console
前往「Model Garden」頁面,然後按一下「Deploy model with custom weights」(使用自訂權重部署模型)。
填寫參數,然後按一下「部署」。
Agent Platform SDK for Python
使用預先建構的容器部署 G2 machine:
from vertexai.preview import model_garden
MODEL_ARTIFACTS_STORAGE_URI = "gs://{STORAGE-URI}/postprocess/node-0/checkpoints/final"
model = model_garden.CustomModel(
gcs_uri=MODEL_ARTIFACTS_STORAGE_URI,
)
# deploy the model to an endpoint using GPUs. Cost will incur for the deployment
endpoint = model.deploy(
machine_type="g2-standard-12",
accelerator_type="NVIDIA_L4",
accelerator_count=1,
)
取得推論結果
部署成功後,您就能使用文字提示將要求傳送至端點。請注意,前幾個提示詞的執行時間會比較長。
# Loads the deployed endpoint
endpoint = aiplatform.Endpoint("projects/{PROJECT_ID}/locations/{REGION}/endpoints/{endpoint_name}")
prompt = "Summarize the following article. Article: Preparing a perfect risotto requires patience and attention to detail. Begin by heating butter in a large, heavy-bottomed pot over medium heat. Add finely chopped onions and minced garlic to the pot, and cook until they're soft and translucent, about 5 minutes. Next, add Arborio rice to the pot and cook, stirring constantly, until the grains are coated with the butter and begin to toast slightly. Pour in a splash of white wine and cook until it's absorbed. From there, gradually add hot chicken or vegetable broth to the rice, stirring frequently, until the risotto is creamy and the rice is tender with a slight bite.. Summary:"
# Define input to the prediction call
instances = [
{
"prompt": "What is a car?",
"max_tokens": 200,
"temperature": 1.0,
"top_p": 1.0,
"top_k": 1,
"raw_response": True,
},
]
# Request the prediction
response = endpoint.predict(
instances=instances
)
for prediction in response.predictions:
print(prediction)
如要進一步瞭解如何從已部署的模型取得推論結果,請參閱「取得線上推論結果」。
請注意,受管理開放式模型會使用 chat.completions 方法,而不是已部署模型使用的 predict 方法。如要進一步瞭解如何從受管理模型取得推論結果,請參閱「呼叫 Llama 模型」。
限制與配額
系統會對並行微調工作數量強制執行配額。每個專案都有預設配額,至少可執行一項微調作業。這是全域配額,適用於所有可用區域和支援的模型。如要同時執行
更多工作,請
申請更多
Global concurrent managed OSS model fine-tuning jobs per project 配額。
除了調整工作配額外,蒸餾微調作業也會使用老師模型,因此專案必須有足夠的指定老師模型配額。以服務形式提供的開放式模型 (MaaS) 使用動態共用配額。調整作業呼叫老師模型時,會耗用專案的該模型共用配額。如要進一步瞭解代管開放模型的配額,請參閱「Gemini Enterprise Agent Platform managed models for MaaS」。
定價
系統會根據模型微調定價,向您收取微調費用。訓練詞元數量的計算方式為:訓練資料集中的詞元數量乘以訓練週期數。如果是精煉調整作業,系統也會根據受管理模型的定價,針對向老師模型發出的 API 呼叫收費,因為這些呼叫會產生回應。
您也需要支付相關服務的費用,例如 Cloud Storage 和 Gemini Enterprise Agent Platform Prediction。
瞭解 Gemini Enterprise Agent Platform 定價和 Cloud Storage 定價,並使用 Pricing Calculator 根據預測用量估算費用。