

本文属于机器翻译版本。若本译文内容与英语原文存在差异，则一律以英文原文为准。

# 使用 Neo 的编译的推荐
<a name="inference-recommender-neo-compilation"></a>

在 Inference Recommender 中，您可以使用 Neo 编译模型，并获取已编译模型的端点推荐。[SageMaker Neo](https://docs.amazonaws.cn/sagemaker/latest/dg/neo.html) 是一项可以针对目标硬件平台（即特定的实例类型或环境）优化模型的服务。使用 Neo 优化模型可能会提升托管模型的性能。

对于 Neo 支持的框架和容器，Inference Recommender 会自动建议经 Neo 优化的推荐。要有资格获得 Neo 编译，您的输入必须满足以下先决条件：
+ 你使用的是 SageMaker AI 拥有的 [DLC](https://docs.amazonaws.cn/deep-learning-containers/latest/devguide/what-is-dlc.html) 或 XGBoost 容器。
+ 您使用的是 Neo 支持的框架版本。有关 Neo 支持的框架版本，请参阅 SageMaker Neo 文档[云实例](neo-supported-cloud.md#neo-supported-cloud-instances)中的。
+ Neo 要求您为模型提供正确的输入数据形状。在创建模型包时，可以将此数据形状指定为 `[InferenceSpecification](https://docs.amazonaws.cn/sagemaker/latest/APIReference/API_CreateModelPackage.html#sagemaker-CreateModelPackage-request-InferenceSpecification)` 中的 `[DataInputConfig](https://docs.amazonaws.cn/sagemaker/latest/APIReference/API_ModelInput.html#sagemaker-Type-ModelInput-DataInputConfig)`。有关每个框架的正确数据形状的信息，请参阅 SageMaker Neo 文档中的[准备模型以进行编译](https://docs.amazonaws.cn/sagemaker/latest/dg/neo-compilation-preparing-model.html)。

  以下示例说明如何指定 `InferenceSpecification` 中的 `DataInputConfig` 字段，其中 `data_input_configuration` 是一个变量，它包含字典格式的数据形状（例如 `{'input':[1,1024,1024,3]}`）。

  ```
  "InferenceSpecification": {
          "Containers": [
              {
                  "Image": dlc_uri,
                  "Framework": framework.upper(),
                  "FrameworkVersion": framework_version,
                  "NearestModelName": model_name,
                  "ModelInput": {"DataInputConfig": data_input_configuration},
              }
          ],
          "SupportedContentTypes": input_mime_types,  # required, must be non-null
          "SupportedResponseMIMETypes": [],
          "SupportedRealtimeInferenceInstanceTypes": supported_realtime_inference_types,  # optional
      }
  ```

如果您的请求满足这些条件，则 Inference Recommender 会为模型的已编译版本和未编译版本运行场景，并提供多种推荐组合以供您选择。您可以比较同一推理推荐的已编译版本和未编译版本的配置，并确定哪一个最适合您的使用案例。这些推荐按每次推理成本进行排名。

要获得 Neo 编译推荐，只需确保您的输入满足以上要求，而无需进行任何其他配置。如果您的输入满足要求，Inference Recommender 会自动在您的模型上运行 Neo 编译，并且您将收到包含 Neo 推荐的响应。

如果您在 Neo 编译过程中遇到错误，请参阅[对 Neo 编译错误进行问题排查](neo-troubleshooting-compilation.md)。

下表是您可能从 Inference Recommender 作业中获得的响应示例，其中包括针对已编译模型的推荐。如果 `InferenceSpecificationName` 字段为 `None`，则推荐是一个未编译的模型。最后一行（其中**InferenceSpecificationName**字段的值为）适用于使用 Neo 编译的模型。`neo-00011122-2333-4445-5566-677788899900`该字段中的值是用于编译和优化模型的 Neo 作业的名称。


| EndpointName | InstanceType | InitialInstanceCount | EnvironmentParameters | CostPerHour | CostPerInference | MaxInvocations | ModelLatency | InferenceSpecificationName | 
| --- | --- | --- | --- | --- | --- | --- | --- | --- | 
| sm-epc-example-000111222 | ml.c5.9xlarge | 1 | [] | 1.836 | 9.15E-07 | 33456 | 7 | 无 | 
| sm-epc-example-111222333 | ml.c5.2xlarge | 1 | [] | 0.408 | 2.11E-07 | 32211 | 21 | 无 | 
| sm-epc-example-222333444 | ml.c5.xlarge | 1 | [] | 0.204 | 1.86E-07 | 18276 | 92 | 无 | 
| sm-epc-example-333444555 | ml.c5.xlarge | 1 | [] | 0.204 | 1.60E-07 | 21286 | 42 | neo-00011122-2333-4445-5566-677788899900 | 

## 开始使用
<a name="inference-recommender-neo-compilation-get-started"></a>

用于创建包含 Neo 优化的推荐的 Inference Recommender 作业的一般步骤如下所示：
+ 准备机器学习模型以进行编译。有关更多信息，请参阅 Neo 文档中的[准备模型以进行编译](https://docs.amazonaws.cn/sagemaker/latest/dg/neo-compilation-preparing-model.html)。
+ 将您的模型打包到模型存档（`.tar.gz` 文件）中。
+ 创建示例负载存档。
+ 在 “模型注册表” 中注册您的 SageMaker 模型。
+ 创建 Inference Recommender 作业。
+ 查看 Inference Recommender 作业的结果并选择配置。
+ 调试编译失败（如果有）。有关更多信息，请参阅[对 Neo 编译错误进行问题排查](https://docs.amazonaws.cn/sagemaker/latest/dg/neo-troubleshooting-compilation.html)。

有关演示以前的工作流程以及如何使用获得 Neo 优化的推荐的示例 XGBoost，请参阅以下[示例](https://github.com/aws/amazon-sagemaker-examples/blob/main/sagemaker-inference-recommender/xgboost/xgboost-inference-recommender.ipynb)笔记本。有关展示如何使用获取 Neo 优化的推荐的示例 TensorFlow，请参阅以下[示例](https://github.com/aws/amazon-sagemaker-examples/blob/main/sagemaker-inference-recommender/inference-recommender.ipynb)笔记本。