

翻訳は機械翻訳により提供されています。提供された翻訳内容と英語版の間で齟齬、不一致または矛盾がある場合、英語版が優先します。

# Autopilot モデルのデプロイと予測
<a name="autopilot-llms-finetuning-deploy-models"></a>

大規模言語モデル (LLM) をファインチューニングした後、インタラクティブな予測を取得するエンドポイントを設定することで、リアルタイムのテキスト生成用のモデルをデプロイできます。

**注記**  
パフォーマンスを向上させるために、`ml.g5.12xlarge` でリアルタイム推論ジョブを実行することをお勧めします。または、Falcon-7B-Instruct と MPT-7B-Instruct のテキスト生成タスクには `ml.g5.8xlarge` インスタンスが適しています。  
これらのインスタンスの詳細は、Amazon EC2 が提供するさまざまなインスタンスタイプのうちの「[高速コンピューティング](https://aws.amazon.com/ec2/instance-types/)」カテゴリで確認できます。

## リアルタイムのテキスト生成
<a name="autopilot-llms-finetuning-realtime"></a>

SageMaker API を使用して、ファインチューニングされたモデルを SageMaker AI ホスティングの[リアルタイム推論エンドポイント](https://docs.aws.amazon.com/sagemaker/latest/dg/realtime-endpoints.html)に手動でデプロイし、次のようにエンドポイントを呼び出して予測を開始できます。

**注記**  
あるいは、Autopilot でファインチューニング実験を作成するときに、自動デプロイオプションを選択することもできます。モデルの自動デプロイの設定については、「[自動デプロイを有効にする方法](autopilot-create-experiment-finetune-llms.md#autopilot-llms-finetuning-auto-model-deployment)」を参照してください。  
また、SageMaker Python SDK と `JumpStartModel` クラスを使用して、Autopilot によってファインチューニングされたモデルで推論を実行することもできます。そのためには、Amazon S3 内にあるモデルのアーティファクトのカスタムロケーションを指定する必要がありますす。モデルを JumpStart モデルとして定義し、推論用にモデルをデプロイする方法については、「[Low-code deployment with the JumpStartModel class](https://sagemaker.readthedocs.io/en/stable/overview.html#deploy-a-pre-trained-model-directly-to-a-sagemaker-endpoint)」を参照してください。

1. **候補となる推論用コンテナ定義を取得する**

   [DescribeAutoMLJobV2](https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_DescribeAutoMLJobV2.html#API_DescribeAutoMLJobV2_ResponseSyntax) API コールへのレスポンスから取得した `BestCandidate` オブジェクトで `InferenceContainerDefinitions` を見つけることができます。推論用のコンテナ定義とは、トレーニング済みのモデルをデプロイして実行し、予測を行うために設計された、コンテナ化された環境を指します。

   次の AWS CLI コマンド例では、[DescribeAutoMLJobV2](https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_DescribeAutoMLJobV2.html) API を使用して、ジョブ名に推奨されるコンテナ定義を取得します。

   ```
   aws sagemaker describe-auto-ml-job-v2 --auto-ml-job-name job-name --region region
   ```

1. **SageMaker AI モデルを作成する**

   前のステップのコンテナ定義を使用して、[CreateModel](https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_CreateModel.html) API を用いて SageMaker AI モデルを作成します。例として、次の AWS CLI コマンドを参照してください。モデル名には `CandidateName` を使用します。

   ```
   aws sagemaker create-model --model-name '<your-candidate-name>' \
                       --primary-container '<container-definition' \
                       --execution-role-arn '<execution-role-arn>' --region '<region>
   ```

1. **エンドポイント設定の作成**

   次の AWS CLI コマンド例では、[CreateEndpointConfig](https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_CreateEndpointConfig.html) API を使用してエンドポイント設定を作成します。
**注記**  
モデルのダウンロード時間が長引いてエンドポイントの作成がタイムアウトしないように、`ModelDataDownloadTimeoutInSeconds = 3600` と `ContainerStartupHealthCheckTimeoutInSeconds = 3600` を設定することをお勧めします。

   ```
   aws sagemaker create-endpoint-config --endpoint-config-name '<your-endpoint-config-name>' \
                       --production-variants '<list-of-production-variants>' ModelDataDownloadTimeoutInSeconds=3600 ContainerStartupHealthCheckTimeoutInSeconds=3600 \
                       --region '<region>'
   ```

1. **エンドポイントの作成** 

   次の AWS CLI 例では、[CreateEndpoint](https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_CreateEndpoint.html) API を使用してエンドポイントを作成します。

   ```
   aws sagemaker create-endpoint --endpoint-name '<your-endpoint-name>' \
                       --endpoint-config-name '<endpoint-config-name-you-just-created>' \
                       --region '<region>'
   ```

   [DescribeEndpoint](https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_DescribeEndpoint.html) API を使用して、エンドポイントのデプロイの進行状況を確認します。例として、次の AWS CLI コマンドを参照してください。

   ```
   aws sagemaker describe-endpoint —endpoint-name '<endpoint-name>' —region <region>
   ```

   `EndpointStatus` が `InService` に変更されると、エンドポイントはリアルタイム推論に使用できるようになります。

1. **エンドポイントの呼び出し** 

   以下のコマンドは、エンドポイントを呼び出してリアルタイム推論を行います。プロンプトはバイト単位でエンコードする必要があります。
**注記**  
入力プロンプトの形式は、言語モデルによって異なります。テキスト生成プロンプトの形式の詳細については、「[テキスト生成モデルのリアルタイム推論のリクエスト形式](#autopilot-llms-finetuning-realtime-prompt-examples)」を参照してください。

   ```
   aws sagemaker invoke-endpoint --endpoint-name '<endpoint-name>' \ 
                     --region '<region>' --body '<your-promt-in-bytes>' [--content-type] 'application/json' <outfile>
   ```

## テキスト生成モデルのリアルタイム推論のリクエスト形式
<a name="autopilot-llms-finetuning-realtime-prompt-examples"></a>

大規模言語モデル (LLM) には、それぞれに固有のソフトウェア依存関係、ランタイム環境、ハードウェア要件があり、Autopilot がモデルをホストして推論を行う際に推奨されるコンテナに影響する場合があります。さらに、必須となる入力データ形式や予測と出力で予想される形式も、各モデルによって決定されます。

一部のモデルの入力例と推奨されるコンテナを次に示します。
+ 推奨コンテナが `huggingface-pytorch-tgi-inference:2.0.1-tgi1.0.3-gpu-py39-cu118-ubuntu20.04` の Falcon モデル

  ```
  payload = {
      "inputs": "Large language model fine-tuning is defined as",
      "parameters": {
          "do_sample": false,
          "top_p": 0.9,
          "temperature": 0.1,
          "max_new_tokens": 128,
          "stop": ["<|endoftext|>", "</s>"]
      }
  }
  ```
+ 推奨コンテナが `djl-inference:0.22.1-fastertransformer5.3.0-cu118` の他のすべてのモデル

  ```
  payload= {
      "text_inputs": "Large language model fine-tuning is defined as"
  }
  ```