View a markdown version of this page

Autopilot 模型部署和预测 - 亚马逊 SageMaker AI

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

Autopilot 模型部署和预测

微调大型语言模型(LLM)后,您可以通过设置端点来获取交互式预测,从而部署该模型用于实时文本生成。

注意

为了获得更好的性能,我们建议在 ml.g5.12xlarge 上运行实时推理作业。或者,ml.g5.8xlarge实例适用于 Falcon-7B-Instruct MPT-7B-Instruct 文本生成任务。

在 Amazon EC2 提供的实例类型选择中,您可以在加速计算类别中找到这些实例的具体信息。

Real-time 文本生成

您可以使用 SageMaker API 将经过微调的模型手动部署到 SageMaker AI Host ing 实时推理端点,然后通过调用终端节点开始进行预测,如下所示。

注意

或者,当您在 Autopilot 中创建微调实验时,也可选择自动部署选项。有关设置模型自动部署的信息,请参阅如何启用自动部署

您还可以使用 SageMaker Python SDK 和JumpStartModel类对由自动驾驶仪微调的模型进行推断。为此,您可以在 Amazon S3 中为模型的构件指定自定义位置。有关将模型定义为模型以及部署 JumpStart 模型进行推理的信息,请参阅使用 JumpStartModel 类进行Low-code 部署

  1. 获取候选推理容器定义

    您可以在从 DescribeAutoMLJobV2API 调用的响应中检索到的BestCandidate对象中找到的。InferenceContainerDefinitions用于推理的容器定义是指设计用于部署和运行经训练模型以进行预测的容器化环境。

    以下 AWS CLI 命令示例使用 DescribeAutoMLJobV2API 为您的任务名称获取建议的容器定义。

    aws sagemaker describe-auto-ml-job-v2 --auto-ml-job-name job-name --region region
  2. 创建 A SageMaker I 模型

    使用上一步中的容器定义通过 CreateModelAP SageMaker I 创建 AI 模型。以以下 AWS CLI 命令为例。使用 CandidateName 作为您的模型名称。

    aws sagemaker create-model --model-name '<your-candidate-name>' \ --primary-container '<container-definition' \ --execution-role-arn '<execution-role-arn>' --region '<region>
  3. 创建端点配置

    以下 AWS CLI 命令示例使用 CreateEndpointConfigAPI 创建终端节点配置。

    注意

    为防止由于模型下载时间过长而导致端点创建超时,建议设置 ModelDataDownloadTimeoutInSeconds = 3600ContainerStartupHealthCheckTimeoutInSeconds = 3600

    aws sagemaker create-endpoint-config --endpoint-config-name '<your-endpoint-config-name>' \ --production-variants '<list-of-production-variants>' ModelDataDownloadTimeoutInSeconds=3600 ContainerStartupHealthCheckTimeoutInSeconds=3600 \ --region '<region>'
  4. 创建端点

    以下 AWS CLI 示例使用 CreateEndpointAPI 创建终端节点。

    aws sagemaker create-endpoint --endpoint-name '<your-endpoint-name>' \ --endpoint-config-name '<endpoint-config-name-you-just-created>' \ --region '<region>'

    使用 DescribeEndpointAPI 检查您的终端节点部署进度。以以下 AWS CLI 命令为例。

    aws sagemaker describe-endpoint —endpoint-name '<endpoint-name>' —region <region>

    EndpointStatus 更改为 InService 后,端点即可用于实时推理。

  5. 调用端点

    以下命令调用端点以进行实时推理。您的提示需要编码为字节。

    注意

    输入提示的格式取决于语言模型。有关文本生成提示格式的更多信息,请参阅文本生成模型实时推理的请求格式

    aws sagemaker invoke-endpoint --endpoint-name '<endpoint-name>' \ --region '<region>' --body '<your-promt-in-bytes>' [--content-type] 'application/json' <outfile>

文本生成模型实时推理的请求格式

不同的大型语言模型(LLM)可能会有特定的软件依赖性、运行时环境和硬件要求,从而影响 Autopilot 推荐的用于托管推理模型的容器。此外,每个模型都规定了所需的输入数据格式以及预测和输出的预期格式。

以下是一些模型的示例输入和推荐的容器。

  • 对于推荐了容器 huggingface-pytorch-tgi-inference:2.0.1-tgi1.0.3-gpu-py39-cu118-ubuntu20.04 的 Falcon 模型:

    payload = { "inputs": "Large language model fine-tuning is defined as", "parameters": { "do_sample": false, "top_p": 0.9, "temperature": 0.1, "max_new_tokens": 128, "stop": ["<|endoftext|>", "</s>"] } }
  • 对于所有其他模型,建议使用容器 djl-inference:0.22.1-fastertransformer5.3.0-cu118

    payload= { "text_inputs": "Large language model fine-tuning is defined as" }