

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

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

要调用多模型端点，请使用来[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)自 SageMaker AI 运行时的，就像调用单个模型端点一样，只需进行一次更改。传递新 `TargetModel` 参数，该参数指定要定向到端点上的哪些模型。 SageMaker AI Runtime `InvokeEndpoint` 请求支持`X-Amzn-SageMaker-Target-Model`作为新的标头，该标头采用为调用指定的模型的相对路径。 SageMaker AI 系统通过将 `CreateModel` API 调用中提供的前缀与模型的相对路径相结合来构造模型的绝对路径。

CPU 和 GPU 支持的多模型端点的以下过程相同。

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

以下预测请求示例使用示例笔记本中的[适用于 Python 的Amazon 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)
```

------
#### [ Amazon CLI ]

 以下示例说明如何使用 Amazon Command Line Interface (Amazon CLI) 发出两行的 CSV 请求。

```
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`。有关如何使用进行预测的更多示例 Amazon CLI，请参阅 SageMaker Python SDK 文档 Amazon 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` 错误代码，重试该请求。默认情况下，会导致错误 Amazon SDKs 的 Python（Boto 3）（使用[旧版重试模式](https://boto3.amazonaws.com/v1/documentation/api/latest/guide/retries.html#legacy-retry-mode)）和 Java 重试`invoke_endpoint`请求。`ModelNotReadyException`您可以将重试策略配置为继续重试请求最多 360 秒。如果您预计下载模型并将其加载到容器中的时间超过 60 秒，请将 SDK 套接字超时设置为 70 秒。有关为 适用于 Python (Boto3) 的 Amazon 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)
```