

# Nova 2.0 での SFT
<a name="nova-sft-2-fine-tune"></a>

Amazon Nova Lite 2.0 は、高度な推論モード、マルチモーダル理解の向上、拡張コンテキスト処理など、教師ありファインチューニングの拡張機能を提供します。Nova 2.0 の SFT を使用すると、複雑なタスクでモデルの優れたパフォーマンスを維持しながら、これらの強力な機能を特定のユースケースに適応させることができます。

Nova 2.0 での SFT の主な機能は次のとおりです。
+ **推論モードのサポート**: 分析機能を強化するため、最終回答を出力する前に明示的な推論トレースを生成するようにモデルをトレーニングします。
+ **高度なマルチモーダルトレーニング**: ドキュメント理解 (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 のサンプルレシピです。このレシピなどは [recipes](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
  + ステップバイステップの推論の恩恵を受けないタスクに適しています

**注記**  
推論を有効にすると、動作に高い推論労力がかかります。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 Schema オブジェクトである必要があります
+ 各 ToolResult は、前述のアシスタント ToolUse の有効な toolUseId を参照し、各 toolUseId は会話ごとに 1 回だけ使用する必要があります

**注記**  
使用するコンテナイメージまたはサンプルレシピの詳細については、[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 分です
+ サンプルごとに 1 つの動画のみが許可されます。同じサンプル内で複数の動画を使用することはできません
+ サンプルには動画とテキストを含めることができますが、動画を他のモダリティ (画像やドキュメントなど) と混ぜ合わせることはできません

**注記**  
使用するコンテナイメージまたはサンプルレシピの詳細については、[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>`、`<validation-directory>`、`<training-file>`、`<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 で調整できるパラメータは次のとおりです。

**設定の実行**  

+ **name**: トレーニングジョブのわかりやすい名前。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`) です。
+ **replicas**: 分散トレーニングに使用するコンピューティングインスタンスの数。使用可能な値は選択したモデルによって異なります。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**: すべてのデバイスとワーカーで 1 回の前方パスまたは後方パスで一緒に処理されたトレーニングサンプルの合計数。

  この値は、デバイスごとのバッチサイズとデバイスの数を乗算します。これは、トレーニングとスループットの安定性に影響します。メモリ内で快適に学習するバッチサイズから始めて、そこからスケールアップすることをお勧めします。ドメイン固有のデータの場合、大きなバッチでは勾配が滑らかになりすぎる可能性があります。
+ **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 オプティマイザーでの 2 番目のモーメント推定の指数関数的減衰率です。デフォルトは 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>

トレーニングアプローチに基づいて、次の推奨ハイパーパラメータを使用します。

**フルランクトレーニング**
+ **エポック**: 1
+ **学習レート (lr)**: 1e-5
+ **最小学習レート (min\_lr)**: 1e-6

**LoRA (低ランク適応)**
+ **エポック**: 2
+ **学習レート (lr)**: 5e-5
+ **最小学習レート (min\_lr)**: 1e-6

**注**: データセットのサイズと検証パフォーマンスに基づいてこれらの値を調整します。トレーニングメトリクスをモニタリングして、オーバーフィットを防止します。