在 Gemini 模型的监督式微调中使用检查点

检查点是指模型在微调过程中特定时间点的状态快照。您可以在 Gemini 模型微调中使用中间检查点来执行以下操作:

  • 保存调优进度。
  • 比较中间检查点的性能。
  • 选择出现过拟合之前性能最佳的检查点作为默认检查点。

对于周期数少于 10 个的调优作业,系统会大约在每个周期完成后保存一个检查点。对于周期数超过 10 个的调优作业,系统会以均匀分布的方式保存大约 10 个检查点,但最终检查点除外,该检查点会在所有周期训练完毕后立即保存。

中间检查点会随着调优进展按顺序部署到新端点。经过调优的模型端点表示默认检查点的端点,而经过调优的模型检查点包括所有检查点及其对应的端点。

支持的模型

以下 Gemini 模型支持检查点:

如需详细了解 Gemini 模型版本,请参阅 Google 模型以及模型版本和生命周期

创建导出检查点的调优作业

您可以使用 Google Gen AI SDK 或 Google Cloud 控制台创建导出检查点的监督式微调作业。

控制台

如需创建导出检查点的调优作业,请前往 Vertex AI Studio 页面,然后选择调优标签页。如需了解详情,请参阅对模型调优

Google Gen AI SDK

(预览版)您可以配置 Gen AI Evaluation Service,以在每个检查点后自动运行评估。此评估配置适用于 us-central1 区域。

import time

from google import genai
from google.genai.types import HttpOptions, CreateTuningJobConfig, TuningDataset, EvaluationConfig, OutputConfig, GcsDestination, Metric

# TODO(developer): Update and un-comment below line
# output_gcs_uri = "gs://your-bucket/your-prefix"

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

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

evaluation_config = EvaluationConfig(
    metrics=[
        Metric(
            name="FLUENCY",
            prompt_template="""Evaluate this {prediction}"""
        )
    ],
    output_config=OutputConfig(
        gcs_destination=GcsDestination(
            output_uri_prefix=output_gcs_uri,
        )
    ),
)

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,
        validation_dataset=validation_dataset,
        evaluation_config=evaluation_config,
    ),
)

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 值为空。

控制台

您可以在 Vertex AI Model Registry 的“在线预测”端点页面中查看经过调优的模型。

  1. 在 Google Cloud 控制台中,从 Vertex AI 部分进入 Model Registry 页面。

    进入 Model Registry 页面

  2. 点击模型的名称。

    系统随即显示模型的默认版本。

  3. 点击版本详情标签页以查看模型版本的相关信息。

    请注意,目标Large model模型类型Foundation来源Vertex AI Studio tuning

  4. 点击部署和测试标签页,查看在其中部署模型的端点。

  5. 点击端点名称以前往端点页面,查看部署到该端点的检查点列表。对于每个检查点,系统都会显示模型版本 ID 和检查点 ID。

或者,您也可以在调优作业详情页面中查看检查点。如需查看此页面,请前往调优页面,然后点击一个调优作业。

前往“调优”页面

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

如果您已将 Gen AI Evaluation Service 配置为在每个检查点后运行评估,请查看您为评估结果配置的 Cloud Storage 存储桶。

测试检查点

您可以在 Vertex AI Model Registry 中查看检查点列表,并测试每个检查点。您也可以使用 Google Gen AI SDK 列出并测试您的检查点。

控制台

  1. 如需在 Google Cloud 控制台中找到经过调优的模型,请前往 Vertex AI Studio 页面。

    进入 Vertex AI Studio

  2. 调优标签页中,找到您的模型,然后点击监控

  3. 监控窗格中的检查点表中,点击所需检查点旁边的测试链接。

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

后续步骤