View a markdown version of this page

部署生成式 AI 推論建議 - Amazon SageMaker AI

本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。

部署生成式 AI 推論建議

當建議任務完成時,每個建議都包含可立即部署的組態。您可以使用 SageMaker AI Studio 的單一動作,或透過 API 以程式設計方式,將所選組態部署至 SageMaker AI 推論端點。

了解部署組態

任務回應中的每個建議都包含具有下列資訊的DeploymentConfiguration物件:

ImageUri

針對建議的執行個體類型最佳化的容器映像 URI。

InstanceType

部署的建議執行個體類型。

InstanceCount

達到效能目標所需的執行個體數量。

CopyCountPerInstance

每個執行個體要執行的模型複本數量。設定為大於 1 的值時,會在每個執行個體上載入模型的多個複本,以增加輸送量。

EnvironmentVariables

針對最佳效能設定的環境變數,例如張量平行大小和最大序列長度。

S3

模型成品的 S3 頻道參考,包括任何最佳化的模型輸出。

使用 API 部署

若要以程式設計方式部署建議,請使用建議中的模型套件來建立 SageMaker AI 模型和端點。每個建議都包含具有模型套件 ARN 和推論規格名稱的ModelDetails物件。這是最簡單的部署路徑,因為模型套件已包含容器映像、環境變數和模型成品通道。

import boto3 client = boto3.client("sagemaker", region_name="us-west-2") # Get the recommendation from a completed job response = client.describe_ai_recommendation_job( AIRecommendationJobName="my-recommendation-job" ) # Select a recommendation (e.g., the first one) recommendation = response["Recommendations"][0] model_details = recommendation["ModelDetails"] deploy_config = recommendation["DeploymentConfiguration"] # Create a model from the model package. # The model package already contains the container image, environment # variables, and S3 data channels (base model + optimization artifacts). model_name = "my-recommended-model" container_def = { "ModelPackageName": model_details["ModelPackageArn"], } # If the recommendation uses a named inference specification (e.g., for # a specific optimization variant), specify it so SageMaker selects the # correct container and instance configuration from the model package. if model_details.get("InferenceSpecificationName"): container_def["InferenceSpecificationName"] = model_details["InferenceSpecificationName"] client.create_model( ModelName=model_name, PrimaryContainer=container_def, ExecutionRoleArn="arn:aws:iam::111122223333:role/ExampleRole", ) # Create an endpoint configuration endpoint_config_name = "my-recommended-endpoint-config" production_variant = { "VariantName": "AllTraffic", "ModelName": model_name, "InstanceType": deploy_config["InstanceType"], "InitialInstanceCount": deploy_config.get("InstanceCount", 1), } copy_count = deploy_config.get("CopyCountPerInstance") if copy_count and copy_count > 1: production_variant["InferenceAmiVersion"] = "al2-ami-sagemaker-inference-gpu-2" production_variant["RoutingConfig"] = {"RoutingStrategy": "LEAST_OUTSTANDING_REQUESTS"} client.create_endpoint_config( EndpointConfigName=endpoint_config_name, ProductionVariants=[production_variant], ) # Create the endpoint endpoint_name = "my-recommended-endpoint" client.create_endpoint( EndpointName=endpoint_name, EndpointConfigName=endpoint_config_name, ) print(f"Endpoint {endpoint_name} is being created.")

建立端點之後,您可以使用 DescribeEndpoint API 監控其狀態,直到達到InService狀態為止。

import time while True: response = client.describe_endpoint(EndpointName=endpoint_name) status = response["EndpointStatus"] print(f"Endpoint status: {status}") if status in ("InService", "Failed"): break time.sleep(60)

從 SageMaker AI Studio 部署

您也可以透過單一動作直接從 SageMaker AI Studio 部署建議的組態。在 SageMaker AI Studio 中,導覽至已完成的建議任務、檢閱建議及其效能指標,然後選擇您要部署的組態。