

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

# 错误排查
<a name="neo-troubleshooting"></a>

此部分包含的信息介绍如何了解和预防常见错误，这些错误所生成的错误消息，以及如何解决这些错误的指南。在继续之前，请问自己以下问题：

 **在部署模型之前是否遇到过错误？** 如果遇到过，请参阅[对 Neo 编译错误进行问题排查](https://docs.aws.amazon.com/sagemaker/latest/dg/neo-troubleshooting-compilation.html)。

 **在编译模型之后是否遇到过错误？** 如果遇到过，请参阅[对 Neo 推理错误进行问题排查](https://docs.aws.amazon.com/sagemaker/latest/dg/neo-troubleshooting-inference.html)。

**您在尝试为 Ambarella 设备编译模型时是否遇到过错误？** 如果遇到过，请参阅 [排查 Ambarella 错误](neo-troubleshooting-target-devices-ambarella.md)。

## 错误分类类型
<a name="neo-error-messages"></a>

此列表对您可能从 Neo 收到的*用户错误* 进行了分类。这包括各个所支持框架的访问和权限错误以及加载错误。所有其他错误为*系统错误*。

### 客户端权限错误
<a name="neo-error-client-permission"></a>

 Neo 直接传递来自相关服务的这些错误。
+ 调用 st@@ *s 时访问被拒绝*：AssumeRole
+ 调用 Amazon S3 以下载或上传客户端模型时的*任何 400 错误*。
+ *PassRole*错误

### 加载错误
<a name="collapsible-section-2"></a>

假设 Neo 编译器从 Amazon S3 成功加载了 .tar.gz，请检查 tarball 是否包含编译所必需的文件。检查标准特定于框架：
+ **TensorFlow**: 只需要使用 protobuf 文件（\$1.pb 或 \$1.pbtxt）。对于保存的模型，需要一个变量文件夹。
+ **Pytorch**：仅需要一个 pytorch 文件 (\$1.pth)。
+ **MXNET**：仅需要一个符号文件 (\$1.json) 和一个参数文件 (\$1.params)。
+ **XGBoost**: 预计只有一个 XGBoost 模型文件 (\$1.model)。输入模型有大小限制。

### 编译错误
<a name="neo-error-compilation"></a>

假设 Neo 编译器成功从 Amazon S3 加载了 .tar.gz，并且 tarball 包含编译所必需的文件。检查标准是：
+ **OperatorNotImplemented**: 尚未实现运算符。
+ **OperatorAttributeNotImplemented**: 指定运算符中的属性尚未实现。
+ **OperatorAttributeRequired**：内部符号图需要属性，但用户输入模型图表中未列出该属性。
+ **OperatorAttributeValueNotValid**: 特定运算符中的属性值无效。

**Topics**
+ [错误分类类型](#neo-error-messages)
+ [对 Neo 编译错误进行问题排查](neo-troubleshooting-compilation.md)
+ [排除 Neo 推理错误](neo-troubleshooting-inference.md)
+ [排查 Ambarella 错误](neo-troubleshooting-target-devices-ambarella.md)

# 对 Neo 编译错误进行问题排查
<a name="neo-troubleshooting-compilation"></a>

此部分包含的信息是关于如何了解和预防常见错误，这些错误所生成的错误消息以及如何解决这些错误的指南。

**Topics**
+ [如何使用此页](#neo-troubleshooting-compilation-how-to-use)
+ [框架相关的错误](#neo-troubleshooting-compilation-framework-related-errors)
+ [基础设施相关错误](#neo-troubleshooting-compilation-infrastructure-errors)
+ [查看您的编译日志](#neo-troubleshooting-compilation-logs)

## 如何使用此页
<a name="neo-troubleshooting-compilation-how-to-use"></a>

尝试按以下顺序浏览这些部分来解决您的错误：

1. 检查编译作业的输入是否满足输入要求。请参阅 [SageMaker Neo 期望什么样的输入数据形状？](neo-compilation-preparing-model.md#neo-job-compilation-expected-inputs)。

1.  查看常见的[框架特定的错误](https://docs.aws.amazon.com/sagemaker/latest/dg/neo-troubleshooting-compilation.html#neo-troubleshooting-compilation-framework-related-errors)。

1.  查看您的错误是否是[基础设施错误](https://docs.aws.amazon.com/sagemaker/latest/dg/neo-troubleshooting-compilation.html#neo-troubleshooting-compilation-infrastructure-errors)。

1. 查看您的[编译日志](https://docs.aws.amazon.com/sagemaker/latest/dg/neo-troubleshooting-compilation.html#neo-troubleshooting-compilation-logs)。

## 框架相关的错误
<a name="neo-troubleshooting-compilation-framework-related-errors"></a>

### Keras
<a name="neo-troubleshooting-compilation-framework-related-errors-keras"></a>


| 错误 | 解决方案 | 
| --- | --- | 
|   `InputConfiguration: No h5 file provided in <model path>`   |   检查您的 h5 文件是否在您指定的 Amazon S3 URI 中。 * 或者 * 检查 [h5 文件格式是否正确](https://www.tensorflow.org/guide/keras/save_and_serialize#keras_h5_format)。  | 
|   `InputConfiguration: Multiple h5 files provided, <model path>, when only one is allowed`   |  检查您是否只提供了一个 `h5` 文件。  | 
|   `ClientError: InputConfiguration: Unable to load provided Keras model. Error: 'sample_weight_mode'`   |  检查您指定的 Keras 版本是否受支持。请参阅[云实例](https://docs.aws.amazon.com/sagemaker/latest/dg/neo-supported-cloud.html)和[边缘设备](https://docs.aws.amazon.com/sagemaker/latest/dg/neo-supported-devices-edge.html)支持的框架。  | 
|   `ClientError: InputConfiguration: Input input has wrong shape in Input Shape dictionary. Input shapes should be provided in NCHW format. `   |   检查您的模型输入是否遵循 NCHW 格式。请参阅 [ SageMaker Neo 期望什么输入数据形状？](https://docs.aws.amazon.com/sagemaker/latest/dg/neo-job-compilation.html#neo-job-compilation-expected-inputs)   | 

### MXNet
<a name="neo-troubleshooting-compilation-framework-related-errors-mxnet"></a>


| 错误 | 解决方案 | 
| --- | --- | 
|   `ClientError: InputConfiguration: Only one parameter file is allowed for MXNet model. Please make sure the framework you select is correct.`   |   SageMaker Neo 将选择第一个给出的参数文件进行编译。  | 

### TensorFlow
<a name="neo-troubleshooting-compilation-framework-related-errors-tensorflow"></a>


| 错误 | 解决方案 | 
| --- | --- | 
|   `InputConfiguration: Exactly one .pb file is allowed for TensorFlow models.`   |  确保您只提供一个.pb 或.pbtxt 文件。  | 
|  `InputConfiguration: Exactly one .pb or .pbtxt file is allowed for TensorFlow models.`  |  确保您只提供一个.pb 或.pbtxt 文件。  | 
|   ` ClientError: InputConfiguration: TVM cannot convert <model zoo> model. Please make sure the framework you selected is correct. The following operators are not implemented: {<operator name>} `   |   检查您选择的运算符是否受支持。请参阅 [SageMaker Neo 支持的框架和运算符](https://aws.amazon.com/releasenotes/sagemaker-neo-supported-frameworks-and-operators/)。  | 

### PyTorch
<a name="neo-troubleshooting-compilation-framework-related-errors-pytorch"></a>


| 错误 | 解决方案 | 
| --- | --- | 
|   `InputConfiguration: We are unable to extract DataInputConfig from the model due to input_config_derivation_error. Please override by providing a DataInputConfig during compilation job creation.`  |  请执行以下任一操作： [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/sagemaker/latest/dg/neo-troubleshooting-compilation.html)  | 

## 基础设施相关错误
<a name="neo-troubleshooting-compilation-infrastructure-errors"></a>


| 错误 | 解决方案 | 
| --- | --- | 
|   `ClientError: InputConfiguration: S3 object does not exist. Bucket: <bucket>, Key: <bucket key>`   |  请查看您提供的 Amazon S3 URI。  | 
|   ` ClientError: InputConfiguration: Bucket <bucket name> is in region <region name> which is different from AWS Sagemaker service region <service region> `   |   创建与服务位于同一区域中的 Amazon S3 存储桶。  | 
|   ` ClientError: InputConfiguration: Unable to untar input model. Please confirm the model is a tar.gz file `   |   检查 Amazon S3 中的模型是否已压缩成 `tar.gz` 文件。  | 

## 查看您的编译日志
<a name="neo-troubleshooting-compilation-logs"></a>

1. 前往亚马逊，网 CloudWatch 址为[https://console.aws.amazon.com/cloudwatch/](https://console.aws.amazon.com/cloudwatch/)。

1. 从右上角的**区域**下拉列表中选择您创建编译作业的区域。

1. 在 Amazon 的导航窗格中 CloudWatch，选择**日志**。选择**日志组**。

1. 搜索名为 `/aws/sagemaker/CompilationJobs` 的日志组。选择日志组。

1. 搜索以编译作业名称命名的日志流。选择日志流。

# 排除 Neo 推理错误
<a name="neo-troubleshooting-inference"></a>

本节包含有关如何防止和解决您在部署 and/or 调用端点时可能遇到的一些常见错误的信息。本节适用于 **PyTorch 1.4.0 或更高版本**以及 **MXNetv1.7.** 0 或更高版本。
+ 如果您在推理脚本中定义了 `model_fn`，请确保对有效输入数据的第一次推理（预热推理）是在 `model_fn()` 中完成的，否则调用 [https://sagemaker.readthedocs.io/en/stable/api/inference/predictors.html#sagemaker.predictor.Predictor.predict](https://sagemaker.readthedocs.io/en/stable/api/inference/predictors.html#sagemaker.predictor.Predictor.predict) 时终端上可能会显示以下错误消息：

  ```
  An error occurred (ModelError) when calling the InvokeEndpoint operation: Received server error (0) from <users-sagemaker-endpoint> with message "Your invocation timed out while waiting for a response from container model. Review the latency metrics for each container in Amazon CloudWatch, resolve the issue, and try again."                
  ```
+ 确保设置下表所示的环境变量。如果未设置，则可能会显示以下错误消息：

  **在终端上：**

  ```
  An error occurred (ModelError) when calling the InvokeEndpoint operation: Received server error (503) from <users-sagemaker-endpoint> with message "{ "code": 503, "type": "InternalServerException", "message": "Prediction failed" } ".
  ```

  **在 CloudWatch：**

  ```
  W-9001-model-stdout com.amazonaws.ml.mms.wlm.WorkerLifeCycle - AttributeError: 'NoneType' object has no attribute 'transform'
  ```    
[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/sagemaker/latest/dg/neo-troubleshooting-inference.html)
+ 创建 Amazon A SageMaker I 模型时，请确保将`MMS_DEFAULT_RESPONSE_TIMEOUT`环境变量设置为 500 或更高的值；否则，终端上可能会出现以下错误消息：

  ```
  An error occurred (ModelError) when calling the InvokeEndpoint operation: Received server error (0) from <users-sagemaker-endpoint> with message "Your invocation timed out while waiting for a response from container model. Review the latency metrics for each container in Amazon CloudWatch, resolve the issue, and try again."
  ```

# 排查 Ambarella 错误
<a name="neo-troubleshooting-target-devices-ambarella"></a>

SageMaker Neo 要求将模型打包到压缩的 TAR 文件中 (`*.tar.gz`)。Ambarella 设备需要在压缩的 TAR 文件中包含其他文件，然后才能将其发送出去以进行编译。如果您想使用 Neo 为 Ambarella 目标编译模型，请在压缩的 TAR 文件中加入以下文件： SageMaker 
+ 使用 SageMaker Neo 支持的框架的训练模型 
+ JSON 配置文件
+ 校准图像

例如，压缩后的 TAR 文件内容应类似如下示例：

```
├──amba_config.json
├──calib_data
|    ├── data1
|    ├── data2
|    ├── .
|    ├── .
|    ├── .
|    └── data500
└──mobilenet_v1_1.0_0224_frozen.pb
```

该目录的配置如下：
+ `amba_config.json`：配置文件
+ `calib_data`：包含校准图像的文件夹
+ `mobilenet_v1_1.0_0224_frozen.pb`: TensorFlow 模型另存为冻结图形

有关 SageMaker Neo 支持的框架的信息，请参阅[支持的框架](neo-supported-devices-edge-frameworks.md)。

## 设置配置文件
<a name="neo-troubleshooting-target-devices-ambarella-config"></a>

配置文件提供 Ambarella 工具链编译模型所需的信息。配置文件必须另存为 JSON 文件，并且文件名必须以 `*config.json` 结尾。下表显示配置文件的内容。


| Key | 说明 | 示例 | 
| --- | --- | --- | 
| 输入 | 将输入层映射到属性的字典。 | <pre>{inputs:{"data":{...},"data1":{...}}}</pre> | 
| “data” | 输入层名称。注意：“data”是可用于标记输入层的名称的示例。 | “data” | 
| shape | 描述模型输入的形状。这遵循与 SageMaker Neo 相同的惯例。 | "shape": "1,3,224,224" | 
| filepath | 包含校准图像的目录的相对路径。这些文件可以是二进制文件或图像文件，例如 JPG 或 PNG。 | "filepath": "calib\$1data/" | 
| colorformat | 模型期望的颜色格式。这将在将图像转换为二进制时使用。支持的值：[RGB、BGR]。默认为 RGB。 | "colorformat":"RGB" | 
| mean | 要从输入中减去的平均值。可以是单个值或值的列表。当以列表形式给出平均值时，条目数量必须与输入的通道维度相匹配。 | "mean":128.0 | 
| scale | 用于对输入进行统一的小数位数值。可以是单个值或值的列表。当以列表形式给出小数位数时，条目数量必须与输入的通道维度相匹配。 | "scale": 255.0 | 

以下是一个示例代理配置文件：

```
{
    "inputs": {
        "data": {
                "shape": "1, 3, 224, 224",
                "filepath": "calib_data/",
                "colorformat": "RGB",
                "mean":[128,128,128],
                "scale":[128.0,128.0,128.0]
        }
    }
}
```

## 校准图像
<a name="neo-troubleshooting-target-devices-ambarella-calibration-images"></a>

通过提供校准图像来量化训练的模型。对模型进行量化可以提高安霸片上系统 (SoC) 上 CVFlow 引擎的性能。Ambarella 工具链使用校准图像来确定如何量化模型中的每一层，以实现最佳性能和精度。每个图层都独立于 INT8 或 INT16格式进行量化。最终模型在量化后混合了 INT8 和 INT16 层。

**您应该使用多少个图像？**

建议您包含 100–200 个图像，用于代表模型预期要处理的场景类型。模型编译时间与输入文件中校准图像的数量为线性关系，随后者增加而增加。

**推荐的图像格式有哪些？**

校准图像可以是原始二进制格式或图像格式，例如 JPG 和 PNG。

您的校准文件夹可以同时包含图像文件和二进制文件。如果校准文件夹同时包含图像文件和二进制文件，则工具链会首先将图像文件转换为二进制文件。转换完成后，它将使用新生成的二进制文件以及最初位于该文件夹中的二进制文件。

**我可以先将图像转换为二进制格式吗？**

可以。您可以使用 [OpenCV](https://opencv.org/) 或 [PIL](https://python-pillow.org/) 等开源软件包将图像文件转换为二进制格式。裁剪图像并调整其大小，使其符合训练模型的输入层的要求。



## 平均值和小数位数
<a name="neo-troubleshooting-target-devices-ambarella-mean-scale"></a>

您可以为 Amberalla 工具链指定平均值和缩放预处理选项。这些运算嵌入到网络中，并在推理期间应用于每个输入。如果您指定平均值或小数位数，请不要提供经过处理的数据。更具体地说，不要提供已从中减去平均值或已应用缩放的数据。

## 查看您的编译日志
<a name="neo-troubleshooting-target-devices-ambarella-compilation"></a>

有关查看 Ambarella 设备的编译日志的信息，请参阅 [查看您的编译日志](neo-troubleshooting-compilation.md#neo-troubleshooting-compilation-logs)。