

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

# 調用多模型端點
<a name="invoke-multi-model-endpoint"></a>

若要調用多模型端點，請使用 SageMaker AI 執行時期的 [https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/sagemaker-runtime.html#SageMakerRuntime.Client.invoke_endpoint](https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/sagemaker-runtime.html#SageMakerRuntime.Client.invoke_endpoint)，這類似調用單一模型端點，只有一項變更。傳遞新的 `TargetModel` 參數，指定要將目標鎖定於端點的哪些模型。SageMaker AI 執行時期 `InvokeEndpoint` 請求支援利用 `X-Amzn-SageMaker-Target-Model` 做為新標題，這會採用針對調用指定模型的相對路徑。SageMaker AI 系統會將 `CreateModel` API 呼叫提供的字首與模型的相對路徑加以結合，藉此建構模型的絕對路徑。

對於 CPU 與 GPU 支援的多模型端點，下列程序相同。

------
#### [ AWS SDK for Python (Boto 3) ]

下列範例預測請求會在範例筆記本使用 [適用於 Python 的AWS SDK (Boto 3)](https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/sagemaker-runtime.html)。

```
response = runtime_sagemaker_client.invoke_endpoint(
                        EndpointName = {{"<ENDPOINT_NAME>"}},
                        ContentType  = "text/csv",
                        TargetModel  = {{"<MODEL_FILENAME>.tar.gz"}},
                        Body         = body)
```

------
#### [ AWS CLI ]

 下列範例顯示如何利用 AWS Command Line Interface 提出兩行 CSV 請求 (AWS CLI)：

```
aws sagemaker-runtime invoke-endpoint \
  --endpoint-name {{"<ENDPOINT_NAME>"}} \
  --body {{"1.0,2.0,5.0"$'\n'"2.0,3.0,4.0"}} \
  --content-type "text/csv" \
  --target-model {{"<MODEL_NAME>.tar.gz"}}
  output_file.txt
```

如推論成功，就會針對您的推論請求提出具相關資訊的 `output_file.txt`。如需如何使用 進行預測的更多範例 AWS CLI，請參閱 SageMaker Python SDK 文件中的[使用 進行預測 AWS CLI](https://sagemaker.readthedocs.io/en/stable/frameworks/tensorflow/deploying_tensorflow_serving.html#making-predictions-with-the-aws-cli)。

------

多模型端點會視需要動態載入目標模型。您可以在執行 [MME 範例筆記本](https://sagemaker-examples.readthedocs.io/en/latest/advanced_functionality/multi_model_xgboost_home_value/xgboost_multi_model_endpoint_home_value.html)時觀察這一點，因為它會透過對託管於單一端點後方的多個目標模型的隨機調用反覆進行。對指定模型的第一個請求需要比較久的時間，因為必須從 Amazon Simple Storage Service (Amazon S3) 下載模型，然後再載入記憶體。這稱為*冷啟動*，預期在多模型端點進行最佳化，以便為客戶提供更優異的價格效能。後續呼叫完成的速度會比較快，因為載入模型後不會有其他負荷。

**注意**  
對於 GPU 支援的執行個體，若 GPU 容器含有 507 的 HTTP 回應碼，表示記憶體或其他資源不足。這會導致未使用的模型從容器卸載，以便載入更常用的模型。

## 針對 ModelNotReadyException 錯誤重試請求
<a name="invoke-multi-model-config-retry"></a>

當您第一次呼叫 `invoke_endpoint` 模型時，系統會從 Amazon Simple Storage Service 下載該模型，並載入推論容器。這會導致第一次呼叫需要更長時間才能返回。由於模型已載入，因此後續對相同模型的呼叫會較快完成。

SageMaker AI 會在呼叫 `invoke_endpoint` 之後，於 60 秒內傳回回應。部分模型太大，無法在 60 秒內下載。如模型未在 60 秒逾時限制之前完成載入，則對 `invoke_endpoint` 提出的請求會傳回錯誤碼 `ModelNotReadyException`，且模型會繼續下載並載入推論容器，最長可達 360 秒。如您收到 `invoke_endpoint` 請求的 `ModelNotReadyException` 錯誤碼，請重試請求。根據預設，Python (Boto 3) AWS SDKs （使用[舊版重試模式](https://boto3.amazonaws.com/v1/documentation/api/latest/guide/retries.html#legacy-retry-mode)) 和導致`ModelNotReadyException`錯誤的 Java 重試`invoke_endpoint`請求。您可設定重試策略，以便繼續重試請求長達 360 秒。如您預估模型下載並載入容器需要超過 60 秒，請設定 SDK 插槽逾時為 70 秒。如需進一步資訊了解如何針對 適用於 Python (Boto3) 的 AWS SDK設定重試策略，請參閱[設定重試模式](https://boto3.amazonaws.com/v1/documentation/api/latest/guide/retries.html#configuring-a-retry-mode)。下列程式碼顯示的範例設定重試策略為重試呼叫 `invoke_endpoint`，最長可達 180 秒。

```
import boto3
from botocore.config import Config

# This example retry strategy sets the retry attempts to 2. 
# With this setting, the request can attempt to download and/or load the model 
# for upto 180 seconds: 1 orginal request (60 seconds) + 2 retries (120 seconds)
config = Config(
    read_timeout=70,
    retries={
        'max_attempts': 2  # This value can be adjusted to 5 to go up to the 360s max timeout
    }
)
runtime_sagemaker_client = boto3.client('sagemaker-runtime', config=config)
```