1.5. 将您的 AI 模型打包到 OCI 镜像中
您可以将模型打包成 OCI 镜像,并使用 ModelCar 方法来帮助设置离线环境。使用 ModelCar 方法时,您的模型可以像任何其他容器镜像一样嵌入。
如果您已经有 S3 兼容对象存储或配置的持久性卷声明,您可以将 AI 模型上传到这些资源,但只有 ModelCar 方法经过测试并被支持。
先决条件
- 有对机器的 root 用户访问权限。
-
已安装 OpenShift CLI (
oc)。 - podman 已安装。
- 您的模型可以使用。
您可以了解以下关于构建适合 vLLM 模型服务器的 OCI 镜像的"如何构建 ModelCar 容器"部分中的概念,并在 OpenShift AI 中构建和部署 ModelCar 容器。
注意确切的目录结构取决于模型服务器。以下示例使用一个与 OpenVINO Model Server OVMS 兼容的 ResNet-50 模型的 Containerfile。OVMS 通常不需要额外的硬件加速器。
流程
使用兼容模型和模型服务器准备 Containerfile。
带有与 OVMS 搭配使用的 ResNet-50 模型的 Containerfile 示例
FROM registry.access.redhat.com/ubi9/ubi-minimal:latest RUN microdnf install -y wget && microdnf clean all RUN mkdir -p /models/1 && chmod -R 755 /models/1 RUN wget -q -P /models/1 \ https://storage.openvinotoolkit.org/repositories/open_model_zoo/2022.1/models_bin/2/resnet50-binary-0001/FP32-INT1/resnet50-binary-0001.bin \ https://storage.openvinotoolkit.org/repositories/open_model_zoo/2022.1/models_bin/2/resnet50-binary-0001/FP32-INT1/resnet50-binary-0001.xml运行以下命令,将
IMAGE_REF环境变量设置为简化您的过程:$ IMAGE_REF=<ovms-resnet50:test>1 - 1
- 将
<ovms-resnet50:test> 替换为镜像引用的名称。在本例中,使用 <repo:tag> 格式。您的镜像引用名称特定于您的用例。
运行以下命令来构建 Containerfile:
$ sudo podman build -t $IMAGE_REF1 - 1
- 由于 CRI-O 和 Podman 共享存储,需要使用
sudo使镜像成为根容器存储的一部分,并可供 MicroShift 使用。
输出示例
STEP 1/4: FROM registry.access.redhat.com/ubi9/ubi-minimal:latest Trying to pull registry.access.redhat.com/ubi9/ubi-minimal:latest... Getting image source signatures Checking if image destination supports signatures Copying blob 533b69cfd644 done | Copying blob 863e9a7e2102 done | Copying config 098048e6f9 done | Writing manifest to image destination Storing signatures STEP 2/4: RUN microdnf install -y wget && microdnf clean all << SNIP >> --> 4c74352ad42e STEP 3/4: RUN mkdir -p /models/1 && chmod -R 755 /models/1 --> bfd31acb1e81 STEP 4/4: RUN wget -q -P /models/1 https://storage.openvinotoolkit.org/repositories/open_model_zoo/2022.1/models_bin/2/resnet50-binary-0001/FP32-INT1/resnet50-binary-0001.bin https://storage.openvinotoolkit.org/repositories/open_model_zoo/2022.1/models_bin/2/resnet50-binary-0001/FP32-INT1/resnet50-binary-0001.xml COMMIT ovms-resnet50:test --> 375b265c1c4b Successfully tagged localhost/ovms-resnet50:test 375b265c1c4bc6f0a059c8739fb2b3a46e1b563728f6d9c51f26f29bb2c87可选:运行以下命令将 Containerfile 推送到 registry 中:
$ sudo podman push $IMAGE_REF重要对于离线用例,请包括除
latest以外的标签。如果使用latest标签,则获取和设置模型的容器将被配置为imagePullPolicy:参数,并且忽略本地镜像。如果您使用除latest之外的任何其他标签,则imagePullPolicy:参数将设置为IfNotPresent。
验证
运行以下命令验证镜像是否存在:
$ sudo podman images ovms-resnet50输出示例
REPOSITORY TAG IMAGE ID CREATED SIZE localhost/ovms-resnet50 test 375b265c1c4b 3 minutes ago 136 MB
后续步骤
- 配置模型运行时。
- 确认您的 AI 模型已准备好推断。
- 向模型服务器发出请求。