部署自訂模型 - Amazon SageMaker AI

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

部署自訂模型

訓練完成後,請部署模型以進行推論。您可以使用 CLI 或 SDK 部署自訂模型。

找出您的模型成品

  • 檢查您的 S3 儲存貯體:確認模型成品儲存在 s3://my-bucket/model-artifacts/

  • 請注意確切路徑:您將需要完整路徑 (例如 s3://my-bucket/model-artifacts/test-pytorch-job/model.tar.gz)

使用 CLI 部署

執行下列命令來部署您的自訂模型:

hyp create hyp-custom-endpoint \ --version 1.0 \ --env '{"HF_MODEL_ID":"/opt/ml/model", "SAGEMAKER_PROGRAM":"inference.py", }' \ --model-source-type s3 \ --model-location test-pytorch-job/model.tar.gz \ --s3-bucket-name my-bucket \ --s3-region us-east-2 \ --prefetch-enabled true \ --image-uri 763104351884.dkr.ecr.us-east-1.amazonaws.com/pytorch-inference:latest \ --model-volume-mount-name model-weights \ --container-port 8080 \ --resources-requests '{"cpu": "30000m", "nvidia.com/gpu": 1, "memory": "100Gi"}' \ --resources-limits '{"nvidia.com/gpu": 1}' \ --tls-output-s3-uri s3://tls-bucket-inf1-beta2 \ --instance-type ml.g5.8xlarge \ --endpoint-name endpoint-custom-pytorch \ --model-name pytorch-custom-model \

此命令會將訓練模型部署為名為 的端點endpoint-custom-pytorch--model-location 參考訓練任務中的成品路徑。

使用 Python SDK 部署

使用下列內容建立 Python 指令碼:

from sagemaker.hyperpod.inference.config.hp_custom_endpoint_config import Model, Server, SageMakerEndpoint, TlsConfig, EnvironmentVariables from sagemaker.hyperpod.inference.hp_custom_endpoint import HPCustomEndpoint model = Model( model_source_type="s3", model_location="test-pytorch-job/model.tar.gz", s3_bucket_name="my-bucket", s3_region="us-east-2", prefetch_enabled=True ) server = Server( instance_type="ml.g5.8xlarge", image_uri="763104351884.dkr.ecr.us-east-2.amazonaws.com/huggingface-pytorch-tgi-inference:2.4.0-tgi2.3.1-gpu-py311-cu124-ubuntu22.04-v2.0", container_port=8080, model_volume_mount_name="model-weights" ) resources = { "requests": {"cpu": "30000m", "nvidia.com/gpu": 1, "memory": "100Gi"}, "limits": {"nvidia.com/gpu": 1} } env = EnvironmentVariables( HF_MODEL_ID="/opt/ml/model", SAGEMAKER_PROGRAM="inference.py", SAGEMAKER_SUBMIT_DIRECTORY="/opt/ml/model/code", MODEL_CACHE_ROOT="/opt/ml/model", SAGEMAKER_ENV="1" ) endpoint_name = SageMakerEndpoint(name="endpoint-custom-pytorch") tls_config = TlsConfig(tls_certificate_output_s3_uri="s3://tls-bucket-inf1-beta2") custom_endpoint = HPCustomEndpoint( model=model, server=server, resources=resources, environment=env, sage_maker_endpoint=endpoint_name, tls_config=tls_config, ) custom_endpoint.create()

調用端點

使用 CLI

使用範例輸入測試端點:

hyp invoke hyp-custom-endpoint \ --endpoint-name endpoint-custom-pytorch \ --body '{"inputs":"What is the capital of USA?"}'

這會傳回模型的回應,例如「美國首都是華盛頓特區」。

使用開發套件

將下列程式碼新增至您的 Python 指令碼:

data = '{"inputs":"What is the capital of USA?"}' response = custom_endpoint.invoke(body=data).body.read() print(response)

管理端點

使用 CLI

列出並檢查端點:

hyp list hyp-custom-endpoint hyp get hyp-custom-endpoint --name endpoint-custom-pytorch

使用開發套件

將下列程式碼新增至您的 Python 指令碼:

logs = custom_endpoint.get_logs() print(logs)

清除資源

完成後,請刪除端點以避免不必要的成本。

使用 CLI

hyp delete hyp-custom-endpoint --name endpoint-custom-pytorch

使用開發套件

custom_endpoint.delete()

後續步驟

您已成功使用 SageMaker HyperPod 部署和測試自訂模型。您現在可以將此端點用於應用程式中的推論。