開放模型的監督式和蒸餾微調

本頁面說明如何對 Llama 3.1 等開放原始碼模型執行監督式和知識蒸餾微調。除非另有說明,否則本頁的操作說明適用於監督式微調和知識蒸餾微調。精煉功能可讓您使用較大教師模型的輸出內容,調整較小的學生模型。

支援的調整模式

  • 監督式微調:
    • 全量微調
    • 低秩調整 (LoRA):LoRA 是一種參數高效調整模式,只會調整部分參數。與全量微調相比,這種方法更具成本效益,且需要的訓練資料較少。另一方面,全量微調會調整所有參數,因此品質潛力較高。
  • 知識蒸餾微調:知識蒸餾微調會使用 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
形式 文字

事前準備

  1. 登入 Google Cloud 帳戶。如果您未曾使用過 Google Cloud,歡迎 建立帳戶,親自體驗實際使用我們產品的成效。新客戶還能獲得價值 $300 美元的免費抵免額,用於執行、測試及部署工作負載。
  2. In the Google Cloud console, on the project selector page, select or create a Google Cloud 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 the resourcemanager.projects.create permission. Learn how to grant roles.

    Go to project selector

  3. Verify that billing is enabled for your Google Cloud project.

  4. 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.enable permission. 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.

    Enable the APIs

  5. In the Google Cloud console, on the project selector page, select or create a Google Cloud 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 the resourcemanager.projects.create permission. Learn how to grant roles.

    Go to project selector

  6. Verify that billing is enabled for your Google Cloud project.

  7. 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.enable permission. 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.

    Enable the APIs

安裝 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.iam.gserviceaccount.com
  • service-PROJECT_NUMBER@gcp-sa-aiplatform.iam.gserviceaccount.com

建立調整工作

您可以從下列位置進行調整:

  • 支援的基礎模型,例如 Llama 3.1
  • 架構與其中一個支援的基礎模型相同。這可以是 Hugging Face 等存放區中的自訂模型檢查點,或是先前透過 Gemini Enterprise Agent Platform 調整工作調整的模型。這樣就能繼續調整已調整過的模型。

Cloud 控制台 (受監控)

  1. 您可以透過下列方式啟動微調:

    • 前往模型資訊卡,按一下「Fine tune」(微調),然後選擇「Managed tuning」(代管微調)。

    前往 Llama 3.1 模型資訊卡

    或

    • 前往「調整」頁面,然後點選「建立調整後的模型」。

    前往「調整」

  2. 填寫參數,然後按一下「Start tuning」(開始調整)。

這會啟動調整工作,您可以在「調整」頁面的「代管調整」分頁中查看。

調整工作完成後,您可以在「Details」(詳細資料) 分頁中查看調整後模型的相關資訊。

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}",
)

GenAI 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

  1. 前往「Model Garden」頁面,然後按一下「Deploy model with custom weights」(使用自訂權重部署模型)。

    前往 Model Garden

  2. 填寫參數,然後點選「部署」。

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 代管模型 (MaaS)」。

定價

系統會根據模型調整定價,向您收取調整費用。訓練詞元數量是訓練資料集中的詞元數量乘以訓練週期數。如果是精煉調整,系統也會根據代管模型的定價,向您收取對教師模型發出 API 呼叫以生成回覆的費用。

您也必須支付相關服務的費用,例如 Cloud Storage 和 Gemini Enterprise Agent Platform Prediction。

瞭解 Gemini Enterprise Agent Platform 定價和 Cloud Storage 定價,並使用 Pricing Calculator,根據預測用量估算費用。

後續步驟