

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

# 部署自定义模型
<a name="deploy-trained-model"></a>

训练完成后，部署模型以进行推理。您可以使用 CLI 或 SDK 部署自定义模型。

## 查找模型构件
<a name="locate-model-artifacts"></a>
+ **检查 S3 存储桶**：确认模型构件已保存到 `s3://my-bucket/model-artifacts/`
+ **记录确切的路径**：您需要完整路径（例如 `s3://my-bucket/model-artifacts/test-pytorch-job/model.tar.gz`）

## 使用 CLI 进行部署
<a name="deploy-using-cli"></a>

运行以下命令以部署自定义模型：

```
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 \
    --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://<bucket_name> \
    --instance-type ml.g5.8xlarge \
    --endpoint-name endpoint-custom-pytorch \
    --model-name pytorch-custom-model
```

此命令将训练后的模型部署为一个名为 `endpoint-custom-pytorch` 的端点。`--model-location` 引用训练作业中的构件路径。

## 使用 Python SDK 进行部署
<a name="deploy-using-sdk"></a>

创建一个 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",
    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://<bucket_name>")

custom_endpoint = HPCustomEndpoint(
    model=model,
    server=server,
    resources=resources,
    environment=env,
    sage_maker_endpoint=endpoint_name,
    tls_config=tls_config
)

custom_endpoint.create()
```

## 调用端点
<a name="invoke-endpoint"></a>

### 使用 CLI
<a name="invoke-using-cli"></a>

使用示例输入测试端点：

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

这将返回模型的回应，例如“The capital of the USA is Washington, D.C.”

### 使用 SDK
<a name="invoke-using-sdk"></a>

向 Python 脚本添加以下代码：

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

## 管理端点
<a name="manage-endpoint"></a>

### 使用 CLI
<a name="manage-using-cli"></a>

列出并检查端点：

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

### 使用 SDK
<a name="manage-using-sdk"></a>

向 Python 脚本添加以下代码：

```
logs = custom_endpoint.get_logs()
print(logs)
```

## 清理 资源
<a name="cleanup-resources"></a>

完成后，请删除端点以避免不必要的费用。

### 使用 CLI
<a name="cleanup-using-cli"></a>

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

### 使用 SDK
<a name="cleanup-using-sdk"></a>

```
custom_endpoint.delete()
```

## 后续步骤
<a name="next-steps"></a>

您已使用成功部署和测试了自定义模型 SageMaker HyperPod。现在，您可以在应用程序中使用此端点进行推理。