微调 Nova 2.0 - 亚马逊 SageMaker AI

本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。

微调 Nova 2.0

先决条件

在开始训练作业之前,请注意具备以下内容:

  • Amazon S3 存储桶,用于存储您的输入数据和训练作业的输出。您可以为这两者使用一个存储桶,也可以为每种类型的数据使用不同的存储桶。确保您的存储桶与您创建所有其他训练资源AWS 区域的地方相同。有关更多信息,请参阅创建通用存储桶

  • 具有运行训练作业权限的 IAM 角色。请务必为 IAM 策略附加 AmazonSageMakerFullAccess。有关更多信息,请参阅如何使用 SageMaker 执行角色

  • 基本 Amazon Nova 配方,请参阅获取 Amazon Nova 配方

什么是 SFT?

监督微调 (SFT) 使用带标签的输入输出对训练语言模型。该模型从由提示和响应组成的演示示例中学习,完善其功能以适应特定的任务、指令或所需行为。

数据准备

概述

Nova 2.0 SFT 数据使用与 Nova 1.0 相同的 Converse API 格式,但增加了可选的推理内容字段。有关完整的格式规范,请参阅:

支持的功能

  • 输入类型-用户内容块中的文本、图像或视频

  • 助手内容 — 纯文字回复和推理内容

  • 数据集构成-必须是同质的。选择如下选项之一:

    • 仅限文字的转弯

    • 文字 + 图像变身

    • 文字+视频转弯(支持文档理解)

重要

您不能在同一个数据集中或在不同的回合中混合图像和视频。

目前的局限性

  • 多模态推理内容 — 尽管 Converse 格式支持基于图像的推理内容,但 Nova 2.0 SFT 在 ReasingText 字段中仅支持基于文本的推理内容。

  • 验证集 — 在 Nova 2.0 中,您无法为 SFT 提供验证数据集。如果您提供了验证数据集,则在训练期间会忽略该数据集。此限制适用于基于用户界面的作业提交和编程作业提交。

支持的媒体格式

  • 图片 — PNG、JPEG、GIF

  • 视频 — MOV、MKV MP4

数据格式示例

Text-only (Nova 1.0 compatible)
{ "schemaVersion": "bedrock-conversation-2024", "system": [ { "text": "You are a digital assistant with a friendly personality" } ], "messages": [ { "role": "user", "content": [ { "text": "What country is right next to Australia?" } ] }, { "role": "assistant", "content": [ { "text": "The closest country is New Zealand" } ] } ] }
Text with reasoning (Nova 2.0)
{ "schemaVersion": "bedrock-conversation-2024", "system": [ { "text": "You are a digital assistant with a friendly personality" } ], "messages": [ { "role": "user", "content": [ { "text": "What country is right next to Australia?" } ] }, { "role": "assistant", "content": [ { "reasoningContent": { "reasoningText": { "text": "I need to use my world knowledge of geography to answer this question" } } }, { "text": "The closest country to Australia is New Zealand, located to the southeast across the Tasman Sea." } ] } ] }
Image + text input
{ "schemaVersion": "bedrock-conversation-2024", "system": [ { "text": "You are a helpful assistant." } ], "messages": [ { "role": "user", "content": [ { "image": { "format": "jpeg", "source": { "s3Location": { "uri": "s3://your-bucket/your-path/your-image.jpg", "bucketOwner": "your-aws-account-id" } } } }, { "text": "Which country is highlighted in the image?" } ] }, { "role": "assistant", "content": [ { "reasoningContent": { "reasoningText": { "text": "I will determine the highlighted country by examining its location on the map and using my geographical knowledge" } } }, { "text": "The highlighted country is New Zealand" } ] } ] }
Video + text input
{ "schemaVersion": "bedrock-conversation-2024", "system": [ { "text": "You are a helpful assistant." } ], "messages": [ { "role": "user", "content": [ { "video": { "format": "mp4", "source": { "s3Location": { "uri": "s3://your-bucket/your-path/your-video.mp4", "bucketOwner": "your-aws-account-id" } } } }, { "text": "What is shown in this video?" } ] }, { "role": "assistant", "content": [ { "reasoningContent": { "reasoningText": { "text": "I will analyze the video content to identify key elements" } } }, { "text": "The video shows a map with New Zealand highlighted" } ] } ] }

工具调用

Nova 2.0 SFT 支持根据工具调用模式训练模型,使您的模型能够学习何时以及如何调用外部工具或函数。

调用工具的数据格式

工具调用训练数据包括定义可用工具的toolConfig部分,以及演示工具使用模式的对话回合。

样本输入

{ "schemaVersion": "bedrock-conversation-2024", "system": [ { "text": "You are an expert in composing function calls." } ], "toolConfig": { "tools": [ { "toolSpec": { "name": "getItemCost", "description": "Retrieve the cost of an item from the catalog", "inputSchema": { "json": { "type": "object", "properties": { "item_name": { "type": "string", "description": "The name of the item to retrieve cost for" }, "item_id": { "type": "string", "description": "The ASIN of item to retrieve cost for" } }, "required": [ "item_id" ] } } } }, { "toolSpec": { "name": "getItemAvailability", "description": "Retrieve whether an item is available in a given location", "inputSchema": { "json": { "type": "object", "properties": { "zipcode": { "type": "string", "description": "The zipcode of the location to check in" }, "quantity": { "type": "integer", "description": "The number of items to check availability for" }, "item_id": { "type": "string", "description": "The ASIN of item to check availability for" } }, "required": [ "item_id", "zipcode" ] } } } } ] }, "messages": [ { "role": "user", "content": [ { "text": "I need to check whether there are twenty pieces of the following item available. Here is the item ASIN on Amazon: id-123. Please check for the zipcode 94086" } ] }, { "role": "assistant", "content": [ { "reasoningContent": { "reasoningText": { "text": "The user wants to check how many pieces of the item with ASIN id-123 are available in the zipcode 94086" } } }, { "toolUse": { "toolUseId": "getItemAvailability_0", "name": "getItemAvailability", "input": { "zipcode": "94086", "quantity": 20, "item_id": "id-123" } } } ] }, { "role": "user", "content": [ { "toolResult": { "toolUseId": "getItemAvailability_0", "content": [ { "text": "[{\"name\": \"getItemAvailability\", \"results\": {\"availability\": true}}]" } ] } } ] }, { "role": "assistant", "content": [ { "text": "Yes, there are twenty pieces of item id-123 available at 94086. Would you like to place an order or know the total cost?" } ] } ] }

工具调用要求

创建调用训练数据的工具时,请遵循以下要求:

要求 说明
ToolUse 放置 ToolUse 只能出现在助手回合中
ToolResult 放置 ToolResult 只能出现在用户回合中
ToolResult 格式 ToolResult 应该仅为文本或 JSON。Nova 型号不支持其他模式
InputSchema 格式 ToolSpec 中的 InputSchema 必须是有效的 JSON 架构对象
toolUseId 匹配 每个人都 ToolResult 必须引用前一位助手 toolUseId 的有效信息 ToolUse,每个助手每次对话只能 toolUseId 使用一次

重要提示

  • 确保您的工具定义在所有训练样本中保持一致

  • 该模型从您提供的演示中学习工具调用模式

  • 包括各种示例,说明何时使用每种工具以及何时不使用工具

文档理解

Nova 2.0 SFT 支持基于文档的任务训练,使您的模型能够学习如何分析和回答有关 PDF 文档的问题。

用于理解文档的数据格式

文档理解训练数据包括用户内容块中的文档引用,模型学习提取和推理而不是文档内容。

样本输入

{ "schemaVersion": "bedrock-conversation-2024", "messages": [ { "role": "user", "content": [ { "text": "What are the ways in which a customer can experience issues during checkout on Amazon?" }, { "document": { "format": "pdf", "source": { "s3Location": { "uri": "s3://my-bucket-name/path/to/documents/customer_service_debugging.pdf", "bucketOwner": "123456789012" } } } } ] }, { "role": "assistant", "content": [ { "text": "Customers can experience issues with 1. Data entry, 2. Payment methods, 3. Connectivity while placing the order. Which one would you like to dive into?" } ], "reasoning_content": [ { "text": "I need to find the relevant section in the document to answer the question.", "type": "text" } ] } ] }

文档理解局限性

限制 Details
支持的格式 仅限 PDF 文件
最大文档大小 10 MB
模态混音 样本可以包含文档和文本,但不能将文档与其他形式(图像、视频)混合在一起

理解文档的最佳实践

  • 确保文档格式清晰且文本可提取

  • 提供涵盖不同文档类型和问题格式的不同示例

  • 包括推理内容以帮助模型学习文档分析模式

视频理解

Nova 2.0 SFT 支持基于视频的任务训练,使您的模型能够学习如何分析和回答有关视频内容的问题。

用于视频理解的数据格式

视频理解训练数据包括用户内容块中的视频参考,模型学习提取视频内容的信息和推理。

样本输入

{ "schemaVersion": "bedrock-conversation-2024", "messages": [ { "role": "user", "content": [ { "text": "What are the ways in which a customer can experience issues during checkout on Amazon?" }, { "video": { "format": "mp4", "source": { "s3Location": { "uri": "s3://my-bucket-name/path/to/videos/customer_service_debugging.mp4", "bucketOwner": "123456789012" } } } } ] }, { "role": "assistant", "content": [ { "text": "Customers can experience issues with 1. Data entry, 2. Payment methods, 3. Connectivity while placing the order. Which one would you like to dive into?" } ], "reasoning_content": [ { "text": "I need to find the relevant section in the video to answer the question.", "type": "text" } ] } ] }

视频理解限制

限制 Details
最大视频大小 50 MB
最大视频时长 15 分钟
每个样本的视频 每个样本只允许一个视频。不支持同一样本中的多个视频
模态混音 样本可以包含视频和文本,但不能将视频与其他形式(图像、文档)结合使用

支持的视频格式

  • MOV

  • MKV

  • MP4

视频理解的最佳实践

  • 保持视频简洁明了,重点放在与你的任务相关的内容上

  • 确保视频质量足以让模型提取有意义的信息

  • 提供明确的问题,提及视频内容的特定方面

  • 包括涵盖不同视频类型和问题格式的不同示例

推理模式与非推理模式

了解推理内容

推理内容(也称为 chain-of-thought)在生成最终答案之前捕捉模型的中间思维步骤。assistant反过来,使用该reasoningContent字段来包含这些推理痕迹。

损失是如何计算的

  • 包含推理内容 — 训练损失包括推理代币和最终输出标记

  • 不含推理内容 — 训练损失仅根据最终输出代币计算

你可以在多回合对话中加入reasoningContent多个助手回合。

格式指南

  • 使用纯文本作为推理内容

  • 除非您的任务特别要求,</thinking>否则请避免使用<thinking>和之类的标记标记

  • 确保推理内容清晰且与问题解决过程相关

何时启用推理模式

在以下reasoning_enabled: true情况下在训练配置中进行设置:

  • 你的训练数据有推理标记

  • 你希望模型在生成最终输出之前生成思考标记

  • 你需要提高复杂推理任务的性能

允许使用reasoning_enabled = true非推理数据集训练 Nova。但是,这样做可能会导致模型失去其推理能力,因为Nova主要是在不应用推理的情况下学习生成数据中呈现的响应。如果你想在非推理数据集上训练 Nova,但仍然期望在推理期间进行推理,则可以在训练期间禁用推理 (reasoning_enabled = false),但将其启用以进行推理。虽然这种方法允许在推理时使用推理,但与不进行推理的推理相比,它并不能保证性能得到改善。通常,在使用推理数据集时,可以同时启用训练和推理的推理,而在使用非推理数据集时,可以同时为训练和推理启用推理。

设置reasoning_enabled: false时间:

  • 您的训练数据没有推理标记

  • 你正在训练简单易懂的任务,这些任务并不能从明确的推理步骤中受益

  • 你想优化速度并减少代币使用量

生成推理数据

如果你的数据集缺少推理轨迹,你可以使用像 Nova Premier 这样的具有推理功能的模型来创建推理轨迹。向模型提供您的输入输出对,并捕获其推理过程以构建推理增强数据集。

使用推理代币进行训练

在启用推理模式的情况下训练时,模型会学会将内部推理与最终答案分开。培训过程:

  • 将数据组织为三元组:输入、推理和答案

  • 使用推理和答案令牌中的标准下一代币预测损失进行优化

  • 鼓励模型在生成回应之前进行内部推理

有效的推理内容

高质量的推理内容应包括:

  • 中间思考和分析

  • 逻辑推断和推理步骤

  • Step-by-step 解决问题的方法

  • 步骤和结论之间的明确联系

这有助于模型培养 “在回答之前先思考” 的能力。

数据集准备指南

尺寸和质量

  • 建议大小 — 2,000-10,000 个样本

  • 最少样本 — 200

  • 优先级-质量胜于数量。确保示例准确且注释良好

  • 应用程序协调 — 数据集应密切反映您的生产用例

多元化

包括以下各种示例:

  • 涵盖预期输入的全部范围

  • 代表不同的难度等级

  • 包括边缘案例和变体

  • 防止过度拟合到狭窄的图案

输出格式

在助手响应中明确指定所需的输出格式:

  • JSON 结构

  • CSV 格式

  • 特定于您的应用程序的自定义格式

多回合对话

对于多回合数据集,请记住:

  • 损失仅在助手回合中计算,不计算用户回合

  • 每个助手回复的格式都应正确

  • 在对话回合中保持一致性

质量清单

  • 足够的数据集大小(2K-10K 样本)

  • 涵盖所有用例的多样化示例

  • 清晰、一致的输出格式

  • 准确的标签和注释

  • 生产场景的代表

  • 没有矛盾或模棱两可之处

上传数据

数据集应上传到 SageMaker 训练作业可以访问的存储桶。有关设置正确权限的信息,请参阅先决条件

开始训练作业

选择超参数并更新配方

Nova 2.0 的设置与 Nova 1.0 的设置基本相同。将输入数据上传到 S3 后,使用 Fine tuning 文件夹下的 SageMaker Hyperpod Recipes 中的配方。对于 Nova 2.0,以下是一些可以根据用例更新的关键超参数。以下是 Nova 2.0 SFT PEFT 配方的示例。

请使用 SageMaker pySDK 的 v2.254.1,以便与 Nova 训练完全兼容。将 SDK 升级到 v3.0 版本将导致重大更改。对 SageMaker pySDK v3 的支持即将推出。

示例输入

!pip install sagemaker==2.254.1
run: name: {peft_recipe_job_name} model_type: amazon.nova-2-lite-v1:0:256k model_name_or_path: {peft_model_name_or_path} data_s3_path: {train_dataset_s3_path} # SageMaker Hyperpod (SMHP) only and not compatible with SageMaker Training jobs. Note replace my-bucket-name with your real bucket name for SMHP job replicas: 4 # Number of compute instances for training, allowed values are 4, 8, 16, 32 output_s3_path: "" # Output artifact path (Hyperpod job-specific; not compatible with standard SageMaker Training jobs). Note replace my-bucket-name with your real bucket name for SMHP job training_config: max_steps: 10 # Maximum training steps. Minimal is 4. save_steps: 10 # How many training steps the checkpoint will be saved. Should be less than or equal to max_steps save_top_k: 1 # Keep top K best checkpoints. Note supported only for SageMaker HyperPod jobs. Minimal is 1. max_length: 32768 # Sequence length (options: 8192, 16384, 32768 [default], 65536) global_batch_size: 32 # Global batch size (options: 32, 64, 128) reasoning_enabled: true # If data has reasoningContent, set to true; otherwise False lr_scheduler: warmup_steps: 15 # Learning rate warmup steps. Recommend 15% of max_steps min_lr: 1e-6 # Minimum learning rate, must be between 0.0 and 1.0 optim_config: # Optimizer settings lr: 1e-5 # Learning rate, must be between 0.0 and 1.0 weight_decay: 0.0 # L2 regularization strength, must be between 0.0 and 1.0 adam_beta1: 0.9 # Exponential decay rate for first-moment estimates, must be between 0.0 and 1.0 adam_beta2: 0.95 # Exponential decay rate for second-moment estimates, must be between 0.0 and 1.0 peft: # Parameter-efficient fine-tuning (LoRA) peft_scheme: "lora" # Enable LoRA for PEFT lora_tuning: alpha: 64 # Scaling factor for LoRA weights ( options: 32, 64, 96, 128, 160, 192), lora_plus_lr_ratio: 64.0

该配方还包含与 Nova 1.0 基本相同的超参数。值得注意的超参数有:

  • max_steps— 您要运行作业的步骤数。通常,对于一个时期(一个周期遍历整个数据集),步骤数 = 数据样本数/全局批次大小。步骤数越大,全局批量大小越小,作业运行所需的时间就越长。

  • reasoning_enabled— 控制数据集的推理模式。选项:

    • true: 启用推理模式(相当于高推理)

    • false: 禁用推理模式

    注意:对于 SFT,无法精细控制推理工作量。设置reasoning_enabled: true启用全面的推理能力。

  • peft.peft_scheme— 将其设置为 “lora” 可启用基于 PEFT 的微调。将其设置为 null(不带引号)可以进行完整等级微调。

开始训练作业

from sagemaker.pytorch import PyTorch # define OutputDataConfig path if default_prefix: output_path = f"s3://{bucket_name}/{default_prefix}/{sm_training_job_name}" else: output_path = f"s3://{bucket_name}/{sm_training_job_name}" recipe_overrides = { "run": { "replicas": instance_count, # Required "output_s3_path": output_path }, } estimator = PyTorch( output_path=output_path, base_job_name=sm_training_job_name, role=role, disable_profiler=True, debugger_hook_config=False, instance_count=instance_count, instance_type=instance_type, training_recipe=training_recipe, recipe_overrides=recipe_overrides, max_run=432000, sagemaker_session=sagemaker_session, image_uri=image_uri, tags=[ {'Key': 'model_name_or_path', 'Value': model_name_or_path}, ] ) print(f"\nsm_training_job_name:\n{sm_training_job_name}\n") print(f"output_path:\n{output_path}")
from sagemaker.inputs import TrainingInput train_input = TrainingInput( s3_data=train_dataset_s3_path, distribution="FullyReplicated", s3_data_type="Converse", ) estimator.fit(inputs={"validation": val_input}, wait=False)
注意

Nova 2.0 的监督微调不支持通过验证数据集。

要开始这项工作,请执行以下操作:

  • 使用您的数据集路径和超参数更新配方

  • 执行笔记本中指定的单元格以提交训练作业

笔记本处理作业提交并提供状态跟踪。