《OpenShift / RHEL / DevSecOps 匯總目錄》
說明:本文已經在 OpenShift 4.18 + OpenShift AI 2.19 的環境中驗證
文章目錄
- 什么是 ModelCar
- 構建模型鏡像
- 在 OpenShift AI 使用模型鏡像
- 部署模型
- 擴展速度對比
- 參考
什么是 ModelCar
KServe 典型的模型初始化方法是從 S3 Bucket 獲取模型。由于每次初始化都要下載模型文件,這一過程對于小型模型來說是可行的,但對于大型模型來說就成了性能瓶頸,因為在自動擴展的過程中會大大延遲啟動時間。
ModelCar 是 KServe 為應對這一問題的方案,它具備以下突出優勢:
- 模型文件已放在容器鏡像中,當鏡像在節點中已被緩存的時候,可避免重復下載模型文件,從而可顯著減少模型啟動的延遲。
- 因為在節點上運行相同模型的 pod 將訪問同一鏡像,無需在每個 pod 中下載模型數據,因此可減少本地磁盤空間的使用。
構建模型鏡像
- 創建下載模型的文件 download_model.py。
$ cat > download_model.py << EOF
from huggingface_hub import snapshot_download# Specify the Hugging Face repository containing the model
model_repo = "Qwen/Qwen2.5-0.5B-Instruct"
snapshot_download(repo_id=model_repo,local_dir="/models",allow_patterns=["*.safetensors", "*.json", "*.txt"],
)
EOF
- 創建構建鏡像的文件 Containerfile。
$ cat > Containerfile << EOF
FROM registry.access.redhat.com/ubi9/python-311:latest as baseUSER rootRUN pip install huggingface-hub# Download the model file from hugging face
COPY download_model.py .RUN python download_model.py # Final image containing only the essential model files
FROM registry.access.redhat.com/ubi9/ubi-micro:9.4# Copy the model files from the base container
COPY --from=base /models /modelsUSER 1001
EOF
- 構建包含模型的鏡像。
podman build . -t modelcar-example:latest --platform linux/amd64
- 將鏡像推送到 Registry。
$ podman images localhost/modelcar-example
REPOSITORY TAG IMAGE ID CREATED SIZE
localhost/modelcar-example latest ae4aac72bb2c 59 minutes ago 1.02 GB$ podman push localhost/modelcar-example quay.io/your-registry/modelcar-example:latest
在 OpenShift AI 使用模型鏡像
部署模型
- 按下圖創建一個使用鏡像作為源的 connection。
- 使用以上 connection 部署模型。將 Deployment mode 設為 Advanced,即使用 Serverless 運行模型;Number of model server replicas to deploy 設為 0,即初始運行副本數為零。
擴展速度對比
結合《OpenShift AI - 在 OpenShift 和 OpenShift AI 上運行 LLM》中基于 S3 的模型部署模式,在同一環境中對 ModelCar 和 S3 方式運行的 ibm-granite/granite-3.2-2b-instruct 模型進行同時擴展。測試結果:
- ModelCar 模式的擴展時間:1分12秒,明顯快。
- S3 模式的擴展時間:2分22秒。
參考
https://developers.redhat.com/articles/2025/01/30/build-and-deploy-modelcar-container-openshift-ai#modelcar_containers_pros_and_cons
https://github.com/redhat-ai-services/modelcar-catalog
https://opendatahub.io/docs/serving-models/
https://github.com/redhat-ai-services/modelcar-catalog/tree/main/modelcar-images/qwen2.5-0.5b-instruc