

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

# モデルをコンパイルする (AWS Command Line Interface)
<a name="neo-job-compilation-cli"></a>

このセクションでは、 AWS Command Line Interface (CLI) を使用して機械学習モデルの Amazon SageMaker Neo コンパイルジョブを管理する方法を示します。コンパイルジョブを作成、説明、停止、一覧表示できます。

1. コンパイルジョブの作成

   [CreateCompilationJob](https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_CreateCompilationJob.html) API オペレーションを使うと、データの入力形式、モデルを保存する S3 バケット、コンパイル済みモデルを書き込む S3 バケット、ターゲットのハードウェアデバイスまたはプラットフォームを指定できます。

   次の表は、ターゲットがデバイスかプラットフォームかに応じて `CreateCompilationJob` API を設定する方法を示しています。

------
#### [ Device Example ]

   ```
   {
       "CompilationJobName": "neo-compilation-job-demo",
       "RoleArn": "arn:aws:iam::<your-account>:role/service-role/AmazonSageMaker-ExecutionRole-yyyymmddThhmmss",
       "InputConfig": {
           "S3Uri": "s3://<your-bucket>/sagemaker/neo-compilation-job-demo-data/train",
           "DataInputConfig":  "{'data': [1,3,1024,1024]}",
           "Framework": "MXNET"
       },
       "OutputConfig": {
           "S3OutputLocation": "s3://<your-bucket>/sagemaker/neo-compilation-job-demo-data/compile",
           # A target device specification example for a ml_c5 instance family
           "TargetDevice": "ml_c5"
       },
       "StoppingCondition": {
           "MaxRuntimeInSeconds": 300
       }
   }
   ```

   オプションで、PyTorch フレームワークを使ってモデルをトレーニング済みで、ターゲットデバイスが `ml_* ` ターゲットである場合は、[[https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_InputConfig.html#sagemaker-Type-InputConfig-FrameworkVersion](https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_InputConfig.html#sagemaker-Type-InputConfig-FrameworkVersion)] フィールドに使ったフレームワークのバージョンを指定できます。

   ```
   {
       "CompilationJobName": "neo-compilation-job-demo",
       "RoleArn": "arn:aws:iam::<your-account>:role/service-role/AmazonSageMaker-ExecutionRole-yyyymmddThhmmss",
       "InputConfig": {
           "S3Uri": "s3://<your-bucket>/sagemaker/neo-compilation-job-demo-data/train",
           "DataInputConfig":  "{'data': [1,3,1024,1024]}",
           "Framework": "PYTORCH",
           "FrameworkVersion": "1.6"
       },
       "OutputConfig": {
           "S3OutputLocation": "s3://<your-bucket>/sagemaker/neo-compilation-job-demo-data/compile",
           # A target device specification example for a ml_c5 instance family
           "TargetDevice": "ml_c5",
           # When compiling for ml_* instances using PyTorch framework, use the "CompilerOptions" field in 
           # OutputConfig to provide the correct data type ("dtype") of the model’s input. Default assumed is "float32"
           "CompilerOptions": "{'dtype': 'long'}"
       },
       "StoppingCondition": {
           "MaxRuntimeInSeconds": 300
       }
   }
   ```

**注記:**  
PyTorch バージョン 2.0 以降を使用してモデルを保存した場合、`DataInputConfig` フィールドはオプションです。SageMaker AI Neo は PyTorch で作成したモデル定義ファイルから入力設定を取得します。定義ファイルの作成方法の詳細については、「Saving Models for SageMaker AI Neo」の「[PyTorch](neo-compilation-preparing-model.md#how-to-save-pytorch)」セクションを参照してください。**
この API フィールドは PyTorch についてのみサポートされています。

------
#### [ Platform Example ]

   ```
   {
       "CompilationJobName": "neo-test-compilation-job",
       "RoleArn": "arn:aws:iam::<your-account>:role/service-role/AmazonSageMaker-ExecutionRole-yyyymmddThhmmss",
       "InputConfig": {
           "S3Uri": "s3://<your-bucket>/sagemaker/neo-compilation-job-demo-data/train",
           "DataInputConfig":  "{'data': [1,3,1024,1024]}",
           "Framework": "MXNET"
       },
       "OutputConfig": {
           "S3OutputLocation": "s3://<your-bucket>/sagemaker/neo-compilation-job-demo-data/compile",
           # A target platform configuration example for a p3.2xlarge instance
           "TargetPlatform": {
               "Os": "LINUX",
               "Arch": "X86_64",
               "Accelerator": "NVIDIA"
           },
           "CompilerOptions": "{'cuda-ver': '10.0', 'trt-ver': '6.0.1', 'gpu-code': 'sm_70'}"
       },
       "StoppingCondition": {
           "MaxRuntimeInSeconds": 300
       }
   }
   ```

------
**注記**  
`OutputConfig` API オペレーションの場合、`TargetDevice` API と`TargetPlatform` API のオペレーションは相互に排他的です。2 つのオプションのいずれかを選択する必要があります。

   フレームワークに応じた `DataInputConfig` の JSON 文字列の例を見つけるには、「[Neo が想定する入力データの形状](https://docs.aws.amazon.com/sagemaker/latest/dg/neo-troubleshooting-compilation.html#neo-troubleshooting-errors-preventing)」を参照してください。

   設定の詳細については、SageMaker API リファレンスの「[InputConfig](https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_InputConfig.html)」、「[OutputConfig](https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_OutputConfig.html)」、「[TargetPlatform](https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_TargetPlatform.html)」の API オペレーションを参照してください。

1. JSON ファイルを設定したら、次のコマンドを実行してコンパイルジョブを作成します。

   ```
   aws sagemaker create-compilation-job \
   --cli-input-json file://job.json \
   --region us-west-2 
   
   # You should get CompilationJobArn
   ```

1. 次のコマンドを実行して、コンパイルジョブを記述します。

   ```
   aws sagemaker describe-compilation-job \
   --compilation-job-name $JOB_NM \
   --region us-west-2
   ```

1. 次のコマンドを実行して、コンパイルジョブを停止します。

   ```
   aws sagemaker stop-compilation-job \
   --compilation-job-name $JOB_NM \
   --region us-west-2
   
   # There is no output for compilation-job operation
   ```

1. 次のコマンドを実行して、コンパイルジョブをリストします。

   ```
   aws sagemaker list-compilation-jobs \
   --region us-west-2
   ```