首页 > 解决方案 > 在 AI Platform 中部署 PyTorch 模型

问题描述

我在 Google Cloud AI Platform 中部署 Pytorch 模型,我收到以下错误:

ERROR: (gcloud.beta.ai-platform.versions.create) Create Version failed. Bad model detected with error: Model requires more memory than allowed. Please try to decrease the model size and re-deploy. If you continue to have error, please contact Cloud ML.

配置:

安装程序.py

from setuptools import setup

REQUIRED_PACKAGES = ['torch']

setup(
    name="iris-custom-model",
    version="0.1",
    scripts=["model.py"],
    install_requires=REQUIRED_PACKAGES
)

模型版本创建

MODEL_VERSION='v1'
RUNTIME_VERSION='1.15'
MODEL_CLASS='model.PyTorchIrisClassifier'

!gcloud beta ai-platform versions create {MODEL_VERSION} --model={MODEL_NAME} \
            --origin=gs://{BUCKET}/{GCS_MODEL_DIR} \
            --python-version=3.7 \
            --runtime-version={RUNTIME_VERSION} \
            --package-uris=gs://{BUCKET}/{GCS_PACKAGE_URI} \
            --prediction-class={MODEL_CLASS}

标签: google-cloud-ml

解决方案


您需要使用与 Cloud AI Platform 兼容的 Pytorch 编译包 包信息在这里

此存储桶包含与 Cloud AI Platform 预测兼容的 PyTorch 编译包。这些文件从https://download.pytorch.org/whl/cpu/torch_stable.html的官方版本镜像

从文档

为了在 Cloud AI Platform Online Predictions 上部署 PyTorch 模型,您必须将其中一个包添加到您部署的版本的 packageURIs 字段中。选择与您的 Python 和 PyTorch 版本匹配的包。包名称遵循此模板:

包名称 = torch-{TORCH_VERSION_NUMBER}-{PYTHON_VERSION}-linux_x86_64.whlwhere PYTHON_VERSION= cp35-cp35m 用于运行时版本 < 1.15 的 Python 3,cp37-cp37m 用于运行时版本 >= 1.15 的 Python 3

例如,如果我要部署基于 PyTorch 1.1.0 和 Python 3 的 PyTorch 模型,我的 gcloud 命令将如下所示:

gcloud beta ai-platform versions create {VERSION_NAME} --model {MODEL_NAME} 
 ...
--package-uris=gs://{MY_PACKAGE_BUCKET}/my_package-0.1.tar.gz,gs://cloud->ai-pytorch/torch-1.1.0-cp35-cp35m-linux_x86_64.whl

总之:

1)torch从您的install_requires依赖项中删除setup.py

2)torch创建版本模型时包含包。

!gcloud beta ai-platform versions create {VERSION_NAME} --model {MODEL_NAME} \
 --origin=gs://{BUCKET}/{MODEL_DIR}/ \
 --python-version=3.7 \
 --runtime-version={RUNTIME_VERSION} \
 --package-uris=gs://{BUCKET}/{PACKAGES_DIR}/text_classification-0.1.tar.gz,gs://cloud-ai-pytorch/torch-1.3.1+cpu-cp37-cp37m-linux_x86_64.whl \
 --prediction-class=model_prediction.CustomModelPrediction

推荐阅读