

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

# 故障診斷錯誤
<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 直接從相依服務傳遞這些錯誤。
+ 呼叫 sts: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.  檢查常見的 [framework-specific 錯誤](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_tw/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. 前往 [https://console.aws.amazon.com/cloudwatch/](https://console.aws.amazon.com/cloudwatch/) 登入 Amazon CloudWatch。

1. 從右上角的 **Region** (區域) 下拉式清單中，選取您建立編譯任務的區域。

1. 在 Amazon CloudWatch 導覽窗格中，選擇 **Logs** (日誌)。選取 **Log groups** (日誌群組)。

1. 搜尋名為 `/aws/sagemaker/CompilationJobs`的日誌群組。選取日誌群組。

1. 搜尋以編譯任務名稱命名的日誌串流。選取日誌串流。

# 故障診斷 Neo 推論錯誤
<a name="neo-troubleshooting-inference"></a>

本節包含如何防止和解決部署和/或調用端點時可能遇到的一些常見錯誤之資訊。本節適用於 **PyTorch 1.4.0 或較新版本**以及** MXNet v1.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_tw/sagemaker/latest/dg/neo-troubleshooting-inference.html)
+ 建立 Amazon SageMaker AI 模型時，請確定 `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`)。在傳送壓縮的 TAR 檔案進行編譯之前，Ambarella 裝置需要將其他檔案包含在該檔案中。如果您要使用 SageMaker Neo 編譯 Ambarella 目標的模型，請在壓縮過的 TAR 檔案中包含下列檔案：
+ 使用由 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`結尾。下列圖表顯示組態檔案的內容。


| 金錀 | Description | 範例 | 
| --- | --- | --- | 
| inputs | 字典將輸入圖層映射至屬性。 | <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>

透過提供校正影像量化訓練過的模型。量化模型可改善 CVFlow 引擎在 Ambarella System on a Chip (SoC) 上的效能。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)。