You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何将PyTorchModel部署至已有的SageMaker端点?

解决方法

要将新训练的PyTorch模型部署到已有的SageMaker端点,不能直接调用PyTorchModel.deploy()并指定已有端点名称,而需要通过创建新的模型实体和端点配置,再更新端点的方式实现,具体步骤如下:

步骤1:初始化PyTorchModel对象

先按常规方式定义你的PyTorchModel,确保模型数据、角色、框架版本等参数正确:

import sagemaker
from sagemaker.pytorch import PyTorchModel

# 替换为你的实际参数
pytorch_model = PyTorchModel(
    model_data="s3://your-bucket/path/to/new-model.tar.gz",
    role="your-sagemaker-role-arn",
    framework_version="2.1.0",  # 匹配你的PyTorch版本
    source_dir="inference-src",  # 若有自定义推理脚本需指定
    entry_point="inference.py"
)

步骤2:创建SageMaker模型实体

将PyTorchModel转换为SageMaker服务端的模型实体:

sagemaker_model = pytorch_model.create_model()

步骤3:创建新的端点配置

由于已有同名端点配置,需要生成一个新的端点配置名称(比如加版本号区分),并关联新创建的模型:

session = sagemaker.Session()
# 自定义新的端点配置名称,避免与现有冲突
new_endpoint_config = "some-existing-endpoint-config-v2"

session.create_endpoint_config(
    endpoint_config_name=new_endpoint_config,
    production_variants=[
        {
            "VariantName": "AllTraffic",
            "ModelName": sagemaker_model.name,
            "InstanceType": "ml.m5d.large",
            "InitialInstanceCount": 1
        }
    ]
)

步骤4:更新已有端点

将现有端点指向新的端点配置,完成模型替换:

session.update_endpoint(
    endpoint_name="some-existing-endpoint",
    endpoint_config_name=new_endpoint_config
)

为什么之前的方法报错?

  • 直接调用deploy()时,SDK会默认用endpoint_name作为端点配置名称,而该名称的端点配置已存在,因此触发ValidationException。
  • update_endpoint是TensorFlowModel专属的deploy参数,PyTorchModel的deploy方法不支持该参数,所以会抛出TypeError。

内容的提问来源于stack exchange,提问作者Larry Mckuydee

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.21 00:40:00