

本文為英文版的機器翻譯版本，如內容有任何歧義或不一致之處，概以英文版為準。

# 新版本 2.0 上的 SFT
<a name="nova-sft-2-fine-tune"></a>

Amazon Nova Lite 2.0 為監督式微調提供了增強功能，包括進階推理模式、改善多模態理解和延伸內容處理。在 Nova 2.0 上的 SFT 可讓您將這些強大的功能適應您的特定使用案例，同時在複雜的任務上維持模型的卓越效能。

SFT on Nova 2.0 的主要功能包括：
+ **推理模式支援**：訓練模型在增強型分析功能的最終答案之前產生明確的推理追蹤。
+ **進階多模式訓練**：微調文件理解 (PDF)、影片理解和影像型任務，以提高準確性。
+ **工具呼叫功能**：訓練模型，以有效地使用外部工具和函數呼叫進行複雜的工作流程。
+ **延伸內容支援**：利用較長的內容時段，為文件密集型應用程式提供更好的穩定性和準確性。

**注意**  
如需使用哪些容器映像或範例配方的詳細資訊，請前往 [ Amazon Nova 配方](nova-model-recipes.md)。

**Topics**
+ [選擇合理模式 （僅限 Nova 2.0)](#nova-sft-2-reasoning-mode)
+ [工具呼叫資料格式](#nova-sft-2-tool-calling)
+ [文件了解資料格式](#nova-sft-2-document-understanding)
+ [影片了解 SFT](#nova-sft-2-video-understanding)
+ [資料上傳指示](#nova-sft-2-data-upload)
+ [建立微調任務](#nova-sft-2-creating-job)
+ [SFT 調校參數](#nova-sft-2-tuning-parameters)
+ [超參數指引](#nova-sft-2-hyperparameters)

## 範例 SFT 配方
<a name="nova-sft-2-sample-recipe"></a>

以下是 SFT 的範例配方。您可以在配方儲存庫中找到此[配方和其他項目](https://github.com/aws/sagemaker-hyperpod-recipes/tree/main/recipes_collection/recipes/fine-tuning/nova)。

```
run:
  name: my-full-rank-sft-run
  model_type: amazon.nova-2-lite-v1:0:256k
  model_name_or_path: nova-lite-2/prod
  data_s3_path: s3://my-bucket-name/train.jsonl  #  only and not compatible with SageMaker Training Jobs
  replicas: 4                                     # Number of compute instances for training, allowed values are 4, 8, 16, 32
  output_s3_path: s3://my-bucket-name/outputs/    # Output artifact path (HyperPod job-specific; not compatible with standard SageMaker Training Jobs)
  mlflow_tracking_uri: ""                         # Required for MLFlow
  mlflow_experiment_name: "my-full-rank-sft-experiment"  # Optional for MLFlow. Note: leave this field non-empty
  mlflow_run_name: "my-full-rank-sft-run"         # Optional for MLFlow. Note: leave this field non-empty

training_config:
  max_steps: 100                    # Maximum training steps. Minimal is 4.
  save_steps: ${oc.select:training_config.max_steps}  # How many training steps the checkpoint will be saved
  save_top_k: 5                     # Keep top K best checkpoints. Note supported only for  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
    adam_beta2: 0.95                 # Exponential decay rate for second-moment estimates

  peft:                             # Parameter-efficient fine-tuning (LoRA)
    peft_scheme: "null"             # Disable LoRA for PEFT
```

## 選擇合理模式 （僅限 Nova 2.0)
<a name="nova-sft-2-reasoning-mode"></a>

Amazon Nova 2.0 支援增強分析功能的推理模式：
+ **原因模式 （已啟用）**：
  + 在訓練組態`reasoning_enabled: true`中設定
  + 模型會訓練 在最終答案之前產生推理追蹤
  + 改善複雜推理任務的效能
+ **非原因模式 （已停用）**：
  + 設定`reasoning_enabled: false`或省略 參數 （預設）
  + 沒有明確推理的標準 SFT
  + 適合無法受益於step-by-step推理的任務

**注意**  
啟用推理時，會以高推理的努力運作。SFT 沒有低推理選項。
SFT 不支援多模式推理內容。原因模式適用於純文字輸入。

### 將推理模式與非合理的資料集搭配使用
<a name="nova-sft-2-reasoning-non-reasoning-data"></a>

`reasoning_enabled: true` 允許使用 在非合理資料集上訓練 Amazon Nova。不過，這樣做可能會導致模型失去其推理功能，因為 Amazon Nova 主要學習在不套用推理的情況下產生資料中呈現的回應。

如果在非合理資料集上訓練 Amazon Nova，但仍希望在推論期間使用推理：

1. 在訓練期間停用推理 (`reasoning_enabled: false`)

1. 稍後在推論期間啟用推理

雖然此方法允許在推論時間進行推理，但與推論相比，它不保證改善效能，無需推理。

**最佳實務：**在使用推理資料集時啟用訓練推理和推論，並在使用非合理的資料集時停用兩者。

**注意**  
如需使用哪些容器映像或範例配方的詳細資訊，請前往 [ Amazon Nova 配方](nova-model-recipes.md)。

## 工具呼叫資料格式
<a name="nova-sft-2-tool-calling"></a>

SFT 支援訓練模型以使用工具 （函數呼叫）。以下是工具呼叫的範例輸入格式：

**範例輸入：**

```
{
  "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 必須僅以助理輪換顯示
+ ToolResult 必須僅顯示在使用者轉彎中
+ ToolResult 只能是文字或 JSON；Amazon Nova 模型目前不支援其他模態
+ toolSpec 中的 inputSchema 必須是有效的 JSON 結構描述物件
+ 每個 ToolResult 必須參考先前助理 ToolUse 中的有效 ToolUse toolUseId，每個toolUseId 在每次對話中只使用一次

**注意**  
如需使用哪些容器映像或範例配方的詳細資訊，請前往 [ Amazon Nova 配方](nova-model-recipes.md)。

## 文件了解資料格式
<a name="nova-sft-2-document-understanding"></a>

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?"
        },
        {
          "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
+ 範例可以包含文件和文字，但無法將文件與其他形式 （例如影像或影片） 混合

**注意**  
如需使用哪些容器映像或範例配方的詳細資訊，請前往 [ Amazon Nova 配方](nova-model-recipes.md)。

## 影片了解 SFT
<a name="nova-sft-2-video-understanding"></a>

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 分鐘
+ 每個範例只允許一個影片；不支援相同範例中的多個影片
+ 範例可以包含影片和文字，但無法將影片與其他形式 （例如影像或文件） 混合

**注意**  
如需使用哪些容器映像或範例配方的詳細資訊，請前往 [ Amazon Nova 配方](nova-model-recipes.md)。

## 資料上傳指示
<a name="nova-sft-2-data-upload"></a>

將訓練和驗證資料集上傳至 S3 儲存貯體。在配方的 `run` 區塊中指定這些位置：

```
## Run config
run:
  ...
  data_s3_path: "s3://<bucket-name>/<training-directory>/<training-file>.jsonl"
```

**注意**：將 `<bucket-name>`、`<training-directory>`、`<training-file>`、 `<validation-directory>`和 取代`<validation-file>`為實際的 S3 路徑。

**注意**：使用 Amazon Nova 2.0 的 SFT 目前不支援驗證資料集。如果提供驗證資料集，則會予以忽略。

## 建立微調任務
<a name="nova-sft-2-creating-job"></a>

使用 `run` 區塊中的 `model_type`和 `model_name_or_path` 欄位來定義基本模型：

```
## Run config
run:
  ...
  model_type: amazon.nova-2-lite-v1:0:256k
  model_name_or_path: nova-lite-2/prod
  ...
```

## SFT 調校參數
<a name="nova-sft-2-tuning-parameters"></a>

可使用 SFT 進行調校的參數包括：

**執行組態**  

+ **名稱**：訓練任務的描述性名稱。這有助於在 AWS 管理主控台中識別您的任務。
+ **model\_type**：要使用的 Amazon Nova 模型變體。可用的選項為 `amazon.nova-2-lite-v1:0:256k`。
+ **model\_name\_or\_path**：用於訓練的基本模型路徑。可用的選項為 `nova-lite-2/prod`，或訓練後檢查點的 S3 路徑 (`s3://customer-escrow-bucket-unique_id/training_run_name`)。
+ **複本**：用於分散式訓練的運算執行個體數量。可用的值會根據您選擇的模型而有所不同。Amazon Nova Lite 2.0 支援 4、8、16 或 32 個複本。
+ **data\_s3\_path**：訓練資料集的 S3 位置，這是 JSONL 檔案。此檔案必須位於與叢集相同的 AWS 帳戶和區域。提供的所有 S3 位置都必須位於相同的帳戶和區域中。
+ **validation\_data\_s3\_path**：（選用） 驗證資料集的 S3 位置，這是 JSONL 檔案。此檔案必須位於與叢集相同的帳戶和區域中。提供的所有 S3 位置都必須位於相同的帳戶和區域中。
+ **output\_s3\_path**：儲存資訊清單和 TensorBoard 日誌的 S3 位置。提供的所有 S3 位置都必須位於相同的 AWS 帳戶和 AWS 區域。
+ **mlflow\_tracking\_uri**：用於 MLFlow 記錄的 MLFlow 應用程式的 ARN。
+ **mlflow\_experiment\_name**：MLFlow 實驗名稱。
+ **mlflow\_run\_name**：MLFlow 執行名稱。

**訓練組態**  

+ **max\_steps**：要執行的訓練步驟數目。每個步驟都會使用元素`global_batch_size`數目來訓練模型。
+ **save\_steps**：在訓練期間儲存模型檢查點的頻率 （步驟中）。
+ **save\_top\_k**：根據驗證指標要保留的最佳檢查點數目上限。
+ **max\_length**：字符的序列長度上限。這會決定訓練的內容範圍大小。SFT 的最大支援值為 32768 個字符。

  序列越長，越能提高訓練效率，但代價是需要增加記憶體。我們建議您將 max\_length 參數與資料分佈配對。
+ **global\_batch\_size**：跨所有裝置和工作者，一次向前或向後傳遞一起處理的訓練範例總數。

  此值會乘以每個裝置的批次大小和裝置數目。它會影響訓練和輸送量的穩定性。我們建議您從適合您記憶體的批次大小開始，並從該處向上擴展。對於特定網域的資料，批次越大可能會使梯度過度平滑。
+ **reasoning\_enabled**：在訓練期間啟用推理功能的布林值旗標。

**學習率排程器**  

+ **warmup\_steps**：逐步提高學習率的步驟數目。這可改善訓練穩定性。
+ **min\_lr**：衰減結束時的最低學習率。有效值介於 0-1 (含) 之間，但必須小於學習率。

**最佳化工具組態**  

+ **lr**：學習率，可在最佳化期間控制步進大小。我們建議使用介於 1e-6-1e-4 之間的值，以獲得良好的效能。有效值介於 0-1 (含) 之間。
+ **weight\_decay**：L2 正規化強度。較高的值 (介於 0.01-0.1 之間) 會增加正規化。
+ **adam\_beta1**：Adam 最佳化工具中第一時間預估的指數衰減率。預設為 0.9。
+ **adam\_beta2**：Adam 最佳化工具中第二個時刻預估的指數衰減率。預設為 0.95。

**PEFT 組態**  

+ **peft\_scheme**：要使用的參數有效微調方案。選項`'null'`適用於全等級微調或`lora`適用於以 LoRA 為基礎的微調。

**LoRA 調校 （當 peft\_scheme 為 'lora' 時）**  

+ **alpha**：LoRA 擴展參數。控制低階調整的大小。典型值的範圍是 8 到 128。
+ **lora\_plus\_lr\_ratio**：LoRA\+ 最佳化的學習率比率。此乘數會特別針對 LoRA 參數調整學習率。

## 超參數指引
<a name="nova-sft-2-hyperparameters"></a>

根據訓練方法，使用以下建議的超參數：

**完整排名訓練**
+ **Epochs**：1
+ **學習率 (lr)**：1e-5
+ **最低學習率 (min\_lr)**：1e-6

**LoRA （低排名調整）**
+ **Epochs**：2
+ **學習率 (lr)**：5e-5
+ **最低學習率 (min\_lr)**：1e-6

**注意**：根據資料集大小和驗證效能調整這些值。監控訓練指標以防止過度擬合。