本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。
微調 Nova 2.0
先決條件
開始訓練任務之前,請注意以下事項:
-
Amazon S3 儲存貯體,用於存放訓練任務的輸入資料和輸出。您可以為兩種類型的資料使用一個儲存貯體,也可以為每一種類型的資料使用個別的儲存貯體。請確定您的儲存貯體位於您建立所有其他資源以進行訓練AWS 區域的相同位置。如需詳細資訊,請參閱建立一般用儲存貯體。
-
具有執行訓練任務許可的 IAM 角色。請務必使用
AmazonSageMakerFullAccess連接 IAM 政策。如需詳細資訊,請參閱如何使用 SageMaker 執行角色。 -
基本 Amazon Nova 配方,請參閱取得 Amazon Nova 配方。
什麼是 SFT?
受監督的微調 (SFT) 會使用標記的輸入輸出對來訓練語言模型。此模型會從包含提示和回應的示範範例中學習,改善其功能以符合特定任務、指示或所需行為。
資料準備
概觀
Nova 2.0 SFT 資料使用與 Nova 1.0 相同的 Converse API 格式,並新增選用的推理內容欄位。如需完整的格式規格,請參閱:
-
Converse API 結構描述:Converse API
-
資料集限制條件:資料集限制條件
支援的功能
-
輸入類型 – 使用者內容區塊中的文字、影像或影片
-
助理內容 – 純文字回應和推理內容
-
資料集合成 – 必須是同質的。選擇以下一項:
-
純文字轉彎
-
文字 + 影像轉彎
-
文字 + 影片轉場 (支援文件理解)
-
重要
您無法在相同資料集內或不同轉彎之間混合影像和影片。
目前限制
-
多模態推理內容 – 雖然 Converse 格式支援以影像為基礎的推理內容,但 Nova 2.0 SFT 在 reasoningText 欄位中僅支援以文字為基礎的推理內容。
-
驗證集 – 您無法為具有 Nova 2.0 的 SFT 提供驗證資料集。如果您提供驗證資料集,則會在訓練期間予以忽略。此限制適用於 UI 型和程式設計任務提交。
支援的媒體格式
-
影像 – PNG、JPEG、GIF
-
影片 – MOV、MKV、MP4
資料格式範例
工具呼叫
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?" } ] } ] }
工具呼叫需求
建立工具呼叫訓練資料時,請遵循下列要求:
| 需求 | Description |
|---|---|
| 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" } ] } ] }
文件理解限制
| 限制 | 詳細資訊 |
|---|---|
| 支援的格式 | 僅限 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" } ] } ] }
影片理解限制
| 限制 | 詳細資訊 |
|---|---|
| 影片大小上限 | 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 format (CSV 格式)
-
您應用程式特有的自訂格式
多回合對話
對於多迴轉資料集,請記住:
-
只有在助理輪換時才會計算損失,而非使用者輪換時
-
每個助理回應都應正確格式化
-
在對話回合之間保持一致性
品質檢查清單
-
足夠的資料集大小 (2K-10K 範例)
-
涵蓋所有使用案例的多樣化範例
-
清晰、一致的輸出格式
-
準確的標籤和註釋
-
生產案例的代表
-
沒有矛盾或模棱兩可之處
上傳資料
資料集應上傳至 SageMaker 訓練任務可存取的儲存貯體。如需設定正確許可的詳細資訊,請參閱先決條件。
開始訓練任務
選取超參數並更新配方
Nova 2.0 的設定大致上與 Nova 1.0 的設定相同。輸入資料上傳到 S3 後,請使用微調資料夾下的 SageMaker Hyperpod 配方
請使用 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– 您要執行任務的步驟數目。一般而言,對於一個 epoch (一個執行整個資料集),步驟數目 = 資料樣本數目/全域批次大小。步驟數目越大,全域批次大小越小,任務執行的時間就越長。 -
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 的微調。
若要開始任務:
-
使用資料集路徑和超參數更新配方
-
在筆記本中執行指定的儲存格以提交訓練任務
筆記本會處理任務提交並提供狀態追蹤。