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

使用 Amazon CLI 部署编译的模型

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

部署模型

在满足先决条件后,使用 create-modelcreate-enpoint-configcreate-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