将 MLflow 模型部署到 Modal
Modal 是一个为 AI/ML 工作负载优化的无服务器云平台,提供按需 GPU 访问和自动扩缩容功能。mlflow-modal-deploy 插件支持通过一条命令将 MLflow 模型部署到 Modal 基础设施。
如果您是 MLflow 模型部署的新手,请先阅读 MLflow 部署,以了解 MLflow 模型和部署的基本概念。
工作原理
该插件可实现部署过程的自动化:
- 提取:从模型 URI 中提取 MLflow 模型工件和依赖项
- 上传:将模型文件上传到 Modal 卷(Volume)以实现持久化存储
- 生成:生成一个带有 FastAPI 端点(
/invocations,/predict_stream)的 Modal 应用 - 部署:Modal 构建包含所有依赖项的容器,并将其部署到无服务器基础设施
- 服务:返回一个 HTTPS 端点 URL,随时准备处理预测请求
生成的容器镜像与您的训练环境一致,确保开发环境和生产环境的行为一致。Modal 会自动处理自动扩缩容、GPU 分配和容器生命周期管理。
将模型部署到 Modal
本节概述了使用 MLflow 部署插件将模型部署到 Modal 的过程。有关 Python API 参考和教程,请参阅有用链接部分。
第 0 步:准备工作
安装库
确保安装了以下库
pip install mlflow mlflow-modal-deploy modal
身份验证设置
配置 Modal 身份验证
# Interactive setup (recommended)
modal setup
# Or use environment variables
export MODAL_TOKEN_ID=your-token-id
export MODAL_TOKEN_SECRET=your-token-secret
创建 MLflow 模型
在部署之前,您必须拥有一个 MLflow 模型。如果没有,您可以按照 MLflow 追踪入门创建一个示例 scikit-learn 模型。请务必记下模型 URI,例如 runs:/<run_id>/model(如果您已在 MLflow 模型注册表中注册了该模型,则为 models:/<model_name>/<model_version>)。
第 1 步:在本地测试您的模型
建议在部署到生产环境之前先在本地测试模型。run_local 函数使用 modal serve 部署模型以进行本地测试。
from mlflow_modal import run_local
run_local(
target_uri="modal",
name="test-model",
model_uri="runs:/<run_id>/model",
config={"gpu": "T4"},
)
这使您可以验证:
- 模型及其所有依赖项是否加载正确
- 推理端点是否按预期响应
- GPU 配置是否有效
第 2 步:部署到 Modal
本地测试通过后,即可部署到 Modal 的云基础设施。
- Python API
- CLI
from mlflow.deployments import get_deploy_client
client = get_deploy_client("modal")
deployment = client.create_deployment(
name="my-classifier",
model_uri="runs:/<run_id>/model",
config={
"gpu": "T4",
"memory": 2048,
"min_containers": 1,
},
)
print(f"Deployed to: {deployment['endpoint_url']}")
# Deploy a model
mlflow deployments create -t modal -m runs:/<run_id>/model --name my-model
# Deploy with GPU and custom configuration
mlflow deployments create -t modal -m runs:/<run_id>/model --name gpu-model \
-C gpu=T4 -C memory=4096 -C min_containers=1
第 3 步:进行预测
部署完成后,您可以使用部署客户端进行预测。
- Python API
- CLI
from mlflow.deployments import get_deploy_client
client = get_deploy_client("modal")
# Standard predictions
predictions = client.predict(
deployment_name="my-classifier",
inputs={"feature1": [1, 2, 3], "feature2": [4, 5, 6]},
)
# Streaming predictions (for LLM models)
for chunk in client.predict_stream(
deployment_name="my-llm",
inputs={"messages": [{"role": "user", "content": "Hello!"}]},
):
print(chunk, end="", flush=True)
# Make predictions
mlflow deployments predict -t modal --name my-model --input-path input.json
配置选项
创建部署时可以使用以下配置选项:
| 选项 | 类型 | 默认值 | 描述 |
|---|---|---|---|
gpu | 字符串/列表 | 无 | GPU 类型:T4, L4, L40S, A10, A100, A100-40GB, A100-80GB, H100, H200, B200。支持多 GPU (H100:8)、专用 GPU (H100!) 或回退列表 (["H100", "A100"]) |
memory | int | 512 | 以 MB 为单位的内存分配 |
cpu | 浮点数 | 1.0 | CPU 核心数 |
timeout | int | 300 | 请求超时时间(秒) |
startup_timeout | int | 无 | 容器启动超时时间(对于大型模型很有用) |
scaledown_window | int | 60 | 空闲容器缩减前的等待时间(秒) |
concurrent_inputs | int | 1 | 每个容器的最大并发请求数 |
min_containers | int | 0 | 最小预热容器数量(设置为 > 0 以避免冷启动) |
max_containers | int | 无 | 最大容器数量 |
enable_batching | bool | False | 启用动态请求批处理 |
max_batch_size | int | 8 | 启用批处理时的最大批处理大小 |
batch_wait_ms | int | 100 | 批处理等待时间(毫秒) |
extra_pip_packages | list | [] | 需要额外安装的 pip 包 |
有关这些选项的详细信息,请参阅 Modal 文档:
高级用法
GPU 选择
Modal 支持多种 GPU 类型以满足不同的工作负载。请查看 Modal 的 GPU 文档以获取可用 GPU 和配置选项的完整列表。
# Single GPU
config = {"gpu": "T4"} # Cost-effective for inference
# High-performance GPU
config = {"gpu": "H100"} # Best for large models
# Multi-GPU for large models
config = {"gpu": "H100:8"} # 8x H100 GPUs
# Dedicated GPU (no sharing)
config = {"gpu": "H100!"}
# Fallback list (uses first available)
config = {"gpu": ["H100", "A100", "A10"]}
高吞吐量部署
对于高吞吐量工作负载,请启用动态批处理。
client.create_deployment(
name="batch-classifier",
model_uri="runs:/<run_id>/model",
config={
"gpu": "A100",
"enable_batching": True,
"max_batch_size": 32,
"batch_wait_ms": 50,
"min_containers": 2,
"max_containers": 20,
},
)
部署到特定工作区
部署到特定的 Modal 工作区。
# Use workspace-specific URI
client = get_deploy_client("modal:/production")
或通过 CLI 操作。
mlflow deployments create -t modal:/production -m runs:/<run_id>/model --name my-model
管理部署
# List all deployments
mlflow deployments list -t modal
# Get deployment info
mlflow deployments get -t modal --name my-model
# Delete deployment
mlflow deployments delete -t modal --name my-model
故障排除
Modal 身份验证失败
# Re-authenticate with Modal
modal setup
# Verify authentication
modal profile list
部署超时
对于加载时间较长的大型模型,请增加启动超时时间。
config = {
"startup_timeout": 600, # 10 minutes for model loading
"timeout": 300, # 5 minutes for inference requests
}
缺少依赖项
如果模型因导入错误而失败,请添加缺少的包。
config = {
"extra_pip_packages": ["missing-package>=1.0"],
}
查看构建日志
检查 Modal 控制台以获取详细的构建和运行时日志。
API 参考
mlflow-modal-deploy 插件与标准的 MLflow 部署接口集成。
有用链接
- mlflow-modal-deploy GitHub 仓库 - 源代码、问题追踪及贡献指南。
- Modal 文档 - 全面的 Modal 平台文档。
- Modal GPU 指南 - 关于 GPU 类型和配置的详细信息。
- MLflow 模型格式 - 了解 MLflow 模型打包和口味 (flavors)。