调用多模型端点
要调用多模型端点,就像调用单个模型端点一样通过 SageMaker 运行时系统使用 invoke_endpoint
TargetModel
参数,该参数指定要定向到端点上的哪些模型。SageMaker 运行时系统 InvokeEndpoint
请求支持 X-Amzn-SageMaker-Target-Model
作为新标头,该标头采用为调用指定的模型的相对路径。SageMaker 系统通过组合在 CreateModel
API 调用中提供的前缀与模型的相对路径来构造模型的绝对路径。
CPU 和 GPU 支持的多模型端点的以下过程相同。
多模型端点根据需要动态加载目标模型。您可以在运行 MME 示例笔记本
注意
对于由 GPU 支持的实例,来自 GPU 容器的、带有 507 的 HTTP 响应代码表示内存或其他资源不足。这会导致从容器中卸载未使用的模型,以便加载更常用的模型。
发生 ModelNotReadyException 错误时重试请求
首次为模型调用 invoke_endpoint
时,会从 Amazon Simple Storage Service 下载模型并将其加载到推理容器中。因此需要更长的时间才能返回第一次调用。由于模型已加载完毕,因此对同一模型的后续调用会更快地完成。
SageMaker 会在 60 秒内返回调用 invoke_endpoint
的响应。有些模型太大,无法在 60 秒内下载。如果模型未在 60 秒超时限制之前完成加载,则对 invoke_endpoint
的请求将返回错误代码 ModelNotReadyException
,会继续下载模型并将其加载到推理容器中,最长 360 秒。如果您收到 invoke_endpoint
请求的 ModelNotReadyException
错误代码,重试该请求。默认情况下,适用于 Python 的 Amazon SDK (Boto 3)(使用旧版重试模式invoke_endpoint
请求会导致 ModelNotReadyException
错误。您可以将重试策略配置为继续重试请求最多 360 秒。如果您预计下载模型并将其加载到容器中的时间超过 60 秒,请将 SDK 套接字超时设置为 70 秒。有关为 Amazon SDK for Python (Boto3) 配置重试策略的更多信息,请参阅配置重试模式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)