翻訳は機械翻訳により提供されています。提供された翻訳内容と英語版の間で齟齬、不一致または矛盾がある場合、英語版が優先します。
kubectl を使用して JumpStart からモデルをデプロイする
次のステップでは、kubectl を使用して JumpStart モデルを HyperPod クラスターにデプロイする方法を示します。
次の手順には、Amazon SageMaker Studio や SageMaker ノートブックインスタンスなどの Jupyter 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"
-
使用できる名前空間をクラスター管理者に確認します。管理者は、名前空間にハイパーポッド推論サービスアカウントを作成している必要があります。
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 設定を更新してモデルをデプロイする
-
HyperPod から EKS クラスター名を取得します。
!aws sagemaker describe-cluster --cluster-name $cluster_name --query "Orchestrator.Eks.ClusterArn"
-
EKS クラスターに接続するように kubectl を設定します。
!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 デプロイオブジェクトを検査します。これを使用して、ポッドスケジューリング、リソース割り当て、コンテナの起動に関する問題のトラブルシューティングを行います。
!kubectl describe deployment $model_id -n $cluster_namespace
-
JumpStart モデルリソースのステータスを確認します。このコマンドは、高レベルのモデル設定とデプロイライフサイクルを管理するカスタム JumpStartModel リソースを調べます。これを使用して、設定エラーや SageMaker エンドポイント作成の問題などのモデル固有の問題をトラブルシューティングします。
!kubectl describe JumpStartModel $model_id -n $cluster_namespace
-
すべての Kubernetes オブジェクトのステータスを確認します。このコマンドは、名前空間内のすべての関連 Kubernetes リソースの包括的な概要を提供します。これを使用して、モデルのデプロイに関連するポッド、サービス、デプロイ、カスタムリソースの全体的な状態をすばやくヘルスチェックできます。
!kubectl get pods,svc,deployment,JumpStartModel,sagemakerendpointregistration -n $cluster_namespace