使用 Boto3 部署编译的模型 - Amazon SageMaker
Amazon Web Services 文档中描述的 Amazon Web Services 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅 中国的 Amazon Web Services 服务入门 (PDF)

使用 Boto3 部署编译的模型

如果您是使用 Amazon SDK for Python (Boto3)、Amazon CLI 或 Amazon SageMaker 控制台编译模型,则必须满足先决条件部分中的要求。按照以下步骤,使用适用于 Python 的 Amazon Web Services SDK (Boto3) 创建和部署 SageMaker Neo 编译模型。

部署模型

在满足先决条件后,使用 create_modelcreate_enpoint_configcreate_endpoint API。

以下示例演示如何使用这些 API 来部署使用 Neo 编译的模型:

import boto3 client = boto3.client('sagemaker') # create sagemaker model create_model_api_response = client.create_model( ModelName='my-sagemaker-model', PrimaryContainer={ 'Image': <insert the ECR Image URI>, 'ModelDataUrl': 's3://path/to/model/artifact/model.tar.gz', 'Environment': {} }, ExecutionRoleArn='ARN for AmazonSageMaker-ExecutionRole' ) print ("create_model API response", create_model_api_response) # create sagemaker endpoint config create_endpoint_config_api_response = client.create_endpoint_config( EndpointConfigName='sagemaker-neomxnet-endpoint-configuration', ProductionVariants=[ { 'VariantName': <provide your variant name>, 'ModelName': 'my-sagemaker-model', 'InitialInstanceCount': 1, 'InstanceType': <provide your instance type here> }, ] ) print ("create_endpoint_config API response", create_endpoint_config_api_response) # create sagemaker endpoint create_endpoint_api_response = client.create_endpoint( EndpointName='provide your endpoint name', EndpointConfigName=<insert your endpoint config name>, ) print ("create_endpoint API response", create_endpoint_api_response)
注意

AmazonSageMakerFullAccessAmazonS3ReadOnlyAccess 策略必须附加到 AmazonSageMaker-ExecutionRole IAM 角色。

有关 create_modelcreate_endpoint_configcreate_endpoint API 的完整语法,请分别参阅 create_modelcreate_endpoint_configcreate_endpoint

如果您没有使用 SageMaker 训练模型,请指定以下环境变量:

MXNet and PyTorch
"Environment": { "SAGEMAKER_PROGRAM": "inference.py", "SAGEMAKER_SUBMIT_DIRECTORY": "/opt/ml/model/code", "SAGEMAKER_CONTAINER_LOG_LEVEL": "20", "SAGEMAKER_REGION": "insert your region", "MMS_DEFAULT_RESPONSE_TIMEOUT": "500" }
TensorFlow
"Environment": { "SAGEMAKER_PROGRAM": "inference.py", "SAGEMAKER_SUBMIT_DIRECTORY": "/opt/ml/model/code", "SAGEMAKER_CONTAINER_LOG_LEVEL": "20", "SAGEMAKER_REGION": "insert your region" }

如果您使用 SageMaker 训练模型,请指定环境变量 SAGEMAKER_SUBMIT_DIRECTORY 作为包含训练脚本的完整 Amazon S3 存储桶 URI。