本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。
JumpStart 使用 kubectl 部署模型
以下步骤向您展示了如何使用 kubect JumpStart l 将模型部署到 HyperPod 集群。
以下说明包含专为在 Jupyter 笔记本环境(例如 Amazon SageMaker Studio 或 SageMaker 笔记本实例)中运行而设计的代码单元和命令。每个代码块代表一个应按顺序执行的笔记本单元。交互式元素(包括模型发现表和状态监控命令)已针对笔记本界面进行了优化,在其他环境中可能无法正常运行。在继续操作之前,请确保您拥有必要的 AWS 权限访问笔记本环境。
先决条件
确保您已在 Amazon SageMaker HyperPod 集群上设置推理功能。有关更多信息,请参阅 设置 HyperPod 集群以进行模型部署。
设置和配置
选择你所在的地区
选择要将 HyperPod 集群部署到的区域以及要运行推理工作负载的位置。您也可以向中添加其他自定义设置。sagemaker_client
region_name = <REGION> import boto3 from botocore.config import Config # Configure retry options boto3_config = Config( retries={ 'max_attempts': 10, # Maximum number of retry attempts 'mode': 'adaptive' # Use adaptive mode for exponential backoff } ) sagemaker_client=boto3.client("sagemaker", region_name=region_name, config=boto3_config)
选择您的模型和集群
-
查看所有 SageMaker 公共中心模型和 HyperPod 集群。
interactive_view(get_all_public_hub_model_data(sagemaker_client)) interactive_view(get_all_cluster_data(sagemaker_client))
-
在下面的变量中配置您选择的模型 ID 和集群名称。
注意
请咨询您的集群管理员,确保已为该笔记本执行角色授予权限。你可以运行
!aws sts get-caller-identity --query "Arn"
来检查你正在使用哪个执行角色# Change the model_id based on your requirement. A list of model IDs is available in step 1 of this notebook. # Proprietary models are not supported model_id = "<insert model id here>" from sagemaker.hyperpod.inference.notebook_utils import validate_public_hub_model_is_not_proprietary validate_public_hub_model_is_not_proprietary(sagemaker_client, model_id)
# Select the cluster name where you want to deploy the model. List of clusters is available in step 1 of this notebook. cluster_name = "<insert cluster name here>" from sagemaker.hyperpod.inference.notebook_utils import validate_cluster_can_support_public_hub_modelfrom sagemaker.hyperpod.inference.notebook_utils import get_public_hub_model_compatible_instances validate_cluster_can_support_public_hub_model(sagemaker_client, model_id, cluster_name) interactive_view(get_public_hub_model_compatible_instances(sagemaker_client, model_id))
# Select the instance that is relevant for your model deployment and exists within the selected cluster. instance_type = "ml.g5.8xlarge"
-
向集群管理员确认允许您使用哪个命名空间。管理员应该已经在你的命名空间中创建了一个 hypod-Inference 服务账号。
cluster_namespace = "default"
配置 S3 存储桶名称
为证书配置 S3 存储桶名称。此存储桶需要有一个名为 “证书” 的文件夹,用于上传证书。存储桶还必须位于上面定义的相同区域。
# Set the S3 bucket name where TLS certificates will be stored for secure model communication certificate_bucket = "<insert bucket name here>"
import yaml from datetime import datetime # Get current time in format suitable for endpoint name current_time = datetime.now().strftime("%Y%m%d-%H%M%S") sagemaker_endpoint_name=f"{model_id}-{current_time}" def generate_jumpstart_model_yaml(model_id, model_version, namespace, instance_type, output_file_path, certificate_bucket): """ Generate a JumpStartModel YAML file with the provided parameters. Args: model_id (str): The model ID model_version (str): The model version namespace (str): The namespace instance_type (str): The instance type output_file_path (str): Path where the YAML file will be saved """ # Create the YAML structure tlsCertificateOutputS3Uri = "s3://" + certificate_bucket + "/certificates/" model_config = { "apiVersion": "inference.sagemaker.aws.amazon.com/v1alpha1", "kind": "JumpStartModel", "metadata": { "name": model_id, "namespace": namespace }, "spec": { "sageMakerEndpoint": { "name": sagemaker_endpoint_name }, "model": { "modelHubName": "SageMakerPublicHub", "modelId": model_id, # modelVersion is optional "modelVersion": model_version # acceptEula is optional, set value to True when using a gated model }, "server": { "instanceType": instance_type }, "tlsConfig": { "tlsCertificateOutputS3Uri": tlsCertificateOutputS3Uri } } } # Write to YAML file with open(output_file_path, 'w') as file: yaml.dump(model_config, file, default_flow_style=False) print(f"YAML file created successfully at: {output_file_path}")
# Import JumpStart utilities to retrieve model specifications and version information from sagemaker.jumpstart import utilsfrom sagemaker.jumpstart.enums import JumpStartScriptScope model_specs = utils.verify_model_region_and_return_specs( model_id, "*", JumpStartScriptScope.INFERENCE, region=region_name) model_version = model_specs.version
# Generate the output filename for the Kubernetes YAML configuration output_file_path=f"jumpstart-model-{model_id}.yaml" generate_jumpstart_model_yaml( model_id=model_id, model_version=model_version, namespace=cluster_namespace, instance_type=instance_type, output_file_path=output_file_path, certificate_bucket=certificate_bucket ) import os os.environ["JUMPSTART_YAML_FILE_PATH"]=output_file_path os.environ["MODEL_ID"]=model_id
部署模型
更新你的 kubernetes 配置并部署你的模型
-
从中检索 EKS 集群名称 HyperPod。
!aws sagemaker describe-cluster --cluster-name $cluster_name --query "Orchestrator.Eks.ClusterArn"
-
将 kubectl 配置为连接到 EKS 集群。
!aws eks update-kubeconfig --name "<insert name of eks cluster from above>" --region $region_name
-
部署您的 JumpStart 模型。
!kubectl apply -f $JUMPSTART_YAML_FILE_PATH
监控模型部署的状态
-
确保模型已成功部署。
!kubectl describe JumpStartModel $model_id -n $cluster_namespace
-
确保成功创建终端节点。
!kubectl describe SageMakerEndPointRegistration sagemaker_endpoint_name -n $cluster_namespace
调用您的模型端点
您可以通过编程方式从对象中检索示例负载。 JumpStartModel
import boto3 prompt = "{\"inputs\": \"What is AWS SageMaker?\"}}" runtime_client = boto3.client('sagemaker-runtime', region_name=region_name) response = runtime_client.invoke_endpoint( EndpointName=sagemaker_endpoint_name, ContentType="application/json", Body=prompt ) print(response["Body"].read().decode())
管理您的部署
清理资源
一旦不再需要 JumpStart 模型部署,请将其删除。
!kubectl delete JumpStartModel $model_id -n $cluster_namespace
故障排除
如果您的部署未按预期运行,请使用这些调试命令。
-
检查 Kubernetes 部署的状态。此命令检查底层 Kubernetes 部署对象,该对象管理运行模型的 pod。使用它来解决 Pod 调度、资源分配和容器启动问题。
!kubectl describe deployment $model_id -n $cluster_namespace
-
检查 JumpStart 模型资源的状态。此命令检查管理高级模型配置和部署生命周期的自定义 JumpStartModel 资源。使用它来解决特定于模型的问题,例如配置错误或 SageMaker 端点创建问题。
!kubectl describe JumpStartModel $model_id -n $cluster_namespace
-
检查所有 Kubernetes 对象的状态。此命令全面概述了您的命名空间中所有相关的 Kubernetes 资源。使用它进行快速运行状况检查,以查看与您的模型部署关联的 pod、服务、部署和自定义资源的整体状态。
!kubectl get pods,svc,deployment,JumpStartModel,sagemakerendpointregistration -n $cluster_namespace