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

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

使用部署编译后的模型 Amazon CLI

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

部署模型

满足先决条件后,使用create-modelcreate-enpoint-config、和create-endpoint Amazon CLI 命令。以下步骤说明如何使用这些命令部署使用 Neo 编译的模型:

创建模型

Neo 推理容器镜像中,选择推理图像 URI,然后使用 create-model API 创建 SageMaker 模型。为此,请完成两个步骤:

  1. 创建 create_model.json 文件。在文件中,指定模型名称、图像 URI、Amazon S3 存储桶中model.tar.gz文件的路径以及您的 SageMaker执行角色:

    { "ModelName": "insert model name", "PrimaryContainer": { "Image": "insert the ECR Image URI", "ModelDataUrl": "insert S3 archive URL", "Environment": {"See details below"} }, "ExecutionRoleArn": "ARN for AmazonSageMaker-ExecutionRole" }

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

    "Environment": { "SAGEMAKER_SUBMIT_DIRECTORY" : "[Full S3 path for *.tar.gz file containing the training script]" }

    如果您没有使用训练模型 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" }
    注意

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

  2. 运行以下命令:

    aws sagemaker create-model --cli-input-json file://create_model.json

    有关 create-model API 的完整语法,请参阅 create-model

创建端点配置

创建 SageMaker 模型后,使用 create-endpoint-config API 创建端点配置。为此,请使用端点配置规范创建一个 JSON 文件。例如,可使用以下代码模板并将其另存为 create_config.json

{ "EndpointConfigName": "<provide your endpoint config name>", "ProductionVariants": [ { "VariantName": "<provide your variant name>", "ModelName": "my-sagemaker-model", "InitialInstanceCount": 1, "InstanceType": "<provide your instance type here>", "InitialVariantWeight": 1.0 } ] }

现在运行以下 Amazon CLI 命令来创建您的终端节点配置:

aws sagemaker create-endpoint-config --cli-input-json file://create_config.json

有关 create-endpoint-config API 的完整语法,请参阅 create-endpoint-config

创建端点

创建端点配置后,使用 create-endpoint API 创建端点:

aws sagemaker create-endpoint --endpoint-name '<provide your endpoint name>' --endpoint-config-name '<insert your endpoint config name>'

有关 create-endpoint API 的完整语法,请参阅 create-endpoint