在 Gemini 模型的監督式微調中使用檢查點

查核點是模型在微調過程中,某個時間點的狀態快照。您可以在 Gemini 模型微調中使用中繼檢查點,執行下列操作:

  • 儲存調整進度。
  • 比較中繼檢查點的效能。
  • 請先選取成效最佳的檢查點,再將其設為預設檢查點,避免過度訓練。

如果微調工作少於 10 個訓練週期,系統大約會在每個訓練週期後儲存一個檢查點。如果微調工作超過 10 個訓練週期,系統會平均儲存約 10 個檢查點,但最後一個檢查點除外,因為系統會在所有訓練週期完成後立即儲存。

中繼查核點會依序部署至新端點,並隨著調整進度更新。微調模型端點代表預設檢查點的端點,微調模型檢查點則包含所有檢查點和對應的端點。

支援的模型

下列 Gemini 模型支援檢查點:

  • gemini-2.0-flash-001
  • gemini-2.0-flash-lite-001
  • gemini-2.5-flash

如要進一步瞭解 Gemini 模型版本,請參閱「Google 模型」和「模型版本和生命週期」。

建立會匯出檢查點的微調工作

您可以使用 Google Gen AI SDK 或 Google Cloud 控制台,建立會匯出檢查點的監督式微調工作。

主控台

如要建立會匯出檢查點的調整工作,請前往 Vertex AI Studio 頁面,然後選取「調整」分頁標籤。詳情請參閱微調模型

Google Gen AI SDK

import time

from google import genai
from google.genai.types import HttpOptions, CreateTuningJobConfig, TuningDataset

client = genai.Client(http_options=HttpOptions(api_version="v1"))

training_dataset = TuningDataset(
    gcs_uri="gs://cloud-samples-data/ai-platform/generative_ai/gemini/text/sft_train_data.jsonl",
)

tuning_job = client.tunings.tune(
    base_model="gemini-2.5-flash",
    training_dataset=training_dataset,
    config=CreateTuningJobConfig(
        tuned_model_display_name="Example tuning job",
        # Set to True to disable tuning intermediate checkpoints. Default is False.
        export_last_checkpoint_only=False,
    ),
)

running_states = set([
    "JOB_STATE_PENDING",
    "JOB_STATE_RUNNING",
])

while tuning_job.state in running_states:
    print(tuning_job.state)
    tuning_job = client.tunings.get(name=tuning_job.name)
    time.sleep(60)

print(tuning_job.tuned_model.model)
print(tuning_job.tuned_model.endpoint)
print(tuning_job.experiment)
# Example response:
# projects/123456789012/locations/us-central1/models/1234567890@1
# projects/123456789012/locations/us-central1/endpoints/123456789012345
# projects/123456789012/locations/us-central1/metadataStores/default/contexts/tuning-experiment-2025010112345678

if tuning_job.tuned_model.checkpoints:
    for i, checkpoint in enumerate(tuning_job.tuned_model.checkpoints):
        print(f"Checkpoint {i + 1}: ", checkpoint)
    # Example response:
    # Checkpoint 1:  checkpoint_id='1' epoch=1 step=10 endpoint='projects/123456789012/locations/us-central1/endpoints/123456789000000'
    # Checkpoint 2:  checkpoint_id='2' epoch=2 step=20 endpoint='projects/123456789012/locations/us-central1/endpoints/123456789012345'

列出調整工作的檢查點

您可以在Google Cloud 控制台中查看已完成微調作業的檢查點,也可以使用 Google Gen AI SDK 列出檢查點。

如果停用中繼檢查點,系統只會顯示或傳回最終檢查點。

主控台

  1. 如要在 Google Cloud 控制台中找到微調模型,請前往「Vertex AI Studio」頁面。

    前往 Vertex AI Studio

  2. 在「微調」分頁中找到模型,然後點選「監控」

    畫面上會顯示模型的調整指標和檢查點。在每個指標圖表中,檢查點編號會顯示為註解,如下所示:

    • 每個訓練週期都會顯示步驟編號和訓練週期編號。
    • 步數是儲存檢查點時的確切步數。
    • 檢查點所屬的訓練週期數為預估值,但已完成調整作業的最終檢查點除外,因為這類檢查點的訓練週期數為確切值。

Google Gen AI SDK

from google import genai
from google.genai.types import HttpOptions

client = genai.Client(http_options=HttpOptions(api_version="v1"))

# Get the tuning job and the tuned model.
# Eg. tuning_job_name = "projects/123456789012/locations/us-central1/tuningJobs/123456789012345"
tuning_job = client.tunings.get(name=tuning_job_name)

if tuning_job.tuned_model.checkpoints:
    for i, checkpoint in enumerate(tuning_job.tuned_model.checkpoints):
        print(f"Checkpoint {i + 1}: ", checkpoint)
# Example response:
# Checkpoint 1:  checkpoint_id='1' epoch=1 step=10 endpoint='projects/123456789012/locations/us-central1/endpoints/123456789000000'
# Checkpoint 2:  checkpoint_id='2' epoch=2 step=20 endpoint='projects/123456789012/locations/us-central1/endpoints/123456789012345'

查看模型詳細資料和檢查點

您可以在 Google Cloud 控制台中查看微調模型,也可以使用 Google Gen AI SDK 取得模型詳細資料,包括端點和檢查點。

模型中的 Endpoint 欄位會更新如下:

  • 這個端點會根據預設查核點更新,並代表微調作業為更新後的預設查核點建立的端點。
  • 如果沒有模型,或微調工作無法取得模型,Endpoint 值會是空白。
  • 如果預設檢查點尚未部署 (因為微調仍在進行中,或部署失敗),Endpoint 值會是空白。

主控台

您可以在「Online prediction」端點頁面中,查看 Vertex AI Model Registry 內的微調模型。

  1. 前往 Google Cloud 控制台的 Vertex AI 專區,然後前往「Model Registry」頁面。

    前往「Model Registry」頁面

  2. 按一下模型名稱。

    系統會顯示模型的預設版本。

  3. 按一下「版本詳細資料」分頁標籤,查看模型版本相關資訊。

    請注意,「目標」Large model,「模型類型」Foundation,「來源」Vertex AI Studio tuning

  4. 按一下「Deploy & test」(部署及測試) 分頁標籤,即可查看模型部署的端點。

  5. 按一下端點名稱前往「端點」頁面,查看部署至端點的檢查點清單。每個檢查點都會顯示模型版本 ID 和檢查點 ID。

或者,您也可以在「Tuning Job Details」頁面中查看檢查點。如要查看這個頁面,請前往「調整」頁面,然後按一下其中一項調整工作。

前往「微調」頁面

Google Gen AI SDK

from google import genai
from google.genai.types import HttpOptions

client = genai.Client(http_options=HttpOptions(api_version="v1"))

# Get the tuning job and the tuned model.
# Eg. tuning_job_name = "projects/123456789012/locations/us-central1/tuningJobs/123456789012345"
tuning_job = client.tunings.get(name=tuning_job_name)
tuned_model = client.models.get(model=tuning_job.tuned_model.model)
print(tuned_model)
# Example response:
# Model(name='projects/123456789012/locations/us-central1/models/1234567890@1', ...)

print(f"Default checkpoint: {tuned_model.default_checkpoint_id}")
# Example response:
# Default checkpoint: 2

if tuned_model.checkpoints:
    for _, checkpoint in enumerate(tuned_model.checkpoints):
        print(f"Checkpoint {checkpoint.checkpoint_id}: ", checkpoint)
# Example response:
# Checkpoint 1:  checkpoint_id='1' epoch=1 step=10
# Checkpoint 2:  checkpoint_id='2' epoch=2 step=20

測試查核點

您可以在 Vertex AI Model Registry 中查看檢查點清單,並測試每個檢查點。您也可以使用 Google Gen AI SDK 列出及測試檢查點。

主控台

  1. 如要在 Google Cloud 控制台中找到微調模型,請前往「Vertex AI Studio」頁面。

    前往 Vertex AI Studio

  2. 在「微調」分頁中找到模型,然後點選「監控」

  3. 在「Monitor」窗格的檢查點表格中,按一下所需檢查點旁的「Test」連結。

Google Gen AI SDK

from google import genai
from google.genai.types import HttpOptions

client = genai.Client(http_options=HttpOptions(api_version="v1"))

# Get the tuning job and the tuned model.
# Eg. tuning_job_name = "projects/123456789012/locations/us-central1/tuningJobs/123456789012345"
tuning_job = client.tunings.get(name=tuning_job_name)

contents = "Why is the sky blue?"

# Predicts with the default checkpoint.
response = client.models.generate_content(
    model=tuning_job.tuned_model.endpoint,
    contents=contents,
)
print(response.text)
# Example response:
# The sky is blue because ...

# Predicts with Checkpoint 1.
checkpoint1_response = client.models.generate_content(
    model=tuning_job.tuned_model.checkpoints[0].endpoint,
    contents=contents,
)
print(checkpoint1_response.text)
# Example response:
# The sky is blue because ...

# Predicts with Checkpoint 2.
checkpoint2_response = client.models.generate_content(
    model=tuning_job.tuned_model.checkpoints[1].endpoint,
    contents=contents,
)
print(checkpoint2_response.text)
# Example response:
# The sky is blue because ...

選取新的預設檢查點

您可以使用預設檢查點,代表成效最佳的檢查點。 根據預設,預設檢查點是微調作業的最終檢查點。

使用檢查點部署模型時,系統會部署預設檢查點。

複製含有查核點的模型時,目的地模型會與來源模型具有相同的預設查核點 ID。系統會複製所有查核點,因此您可以為目標模型選取新的預設查核點。

更新預設檢查點後,微調作業端點也會隨之更新,您可以使用新端點進行預測。

主控台

  1. 如要在 Google Cloud 控制台中找到微調模型,請前往「Vertex AI Studio」頁面。

    前往 Vertex AI Studio

  2. 在「微調」分頁中找到模型,然後點選「監控」

  3. 在「監控」窗格的檢查點表格中,按一下所需檢查點旁的 「動作」,然後選取「設為預設」

  4. 按一下「確認」。

    指標圖表和檢查點表格會更新,顯示新的預設檢查點。調整作業詳細資料頁面中的端點會更新,顯示新預設檢查點的端點。

Google Gen AI SDK

from google import genai
from google.genai.types import HttpOptions, UpdateModelConfig

client = genai.Client(http_options=HttpOptions(api_version="v1"))

# Get the tuning job and the tuned model.
# Eg. tuning_job_name = "projects/123456789012/locations/us-central1/tuningJobs/123456789012345"
tuning_job = client.tunings.get(name=tuning_job_name)
tuned_model = client.models.get(model=tuning_job.tuned_model.model)

print(f"Default checkpoint: {tuned_model.default_checkpoint_id}")
print(f"Tuned model endpoint: {tuning_job.tuned_model.endpoint}")
# Example response:
# Default checkpoint: 2
# projects/123456789012/locations/us-central1/endpoints/123456789012345

# Set a new default checkpoint.
# Eg. checkpoint_id = "1"
tuned_model = client.models.update(
    model=tuned_model.name,
    config=UpdateModelConfig(default_checkpoint_id=checkpoint_id),
)

print(f"Default checkpoint: {tuned_model.default_checkpoint_id}")
print(f"Tuned model endpoint: {tuning_job.tuned_model.endpoint}")
# Example response:
# Default checkpoint: 1
# projects/123456789012/locations/us-central1/endpoints/123456789000000

後續步驟