

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

# 編譯模型 (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 建立的模型定義檔案取得輸入組態。如需如何建立定義檔案的更多相關資訊，請參閱*儲存 SageMaker AI Neo 模型*的[PyTorch](neo-compilation-preparing-model.md#how-to-save-pytorch)一節。
僅針對 PyTorch 支援此 API 欄位。

------
#### [ 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` 和 `TargetPlatform` API 作業互斥。您必須選擇兩個選項之一。

   若要取決於架構尋找 `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
   ```