跳到主要内容

将 MLflow 模型部署到 Modal

Modal 是一个为 AI/ML 工作负载优化的无服务器云平台,提供按需 GPU 访问和自动扩缩容功能。mlflow-modal-deploy 插件支持通过一条命令将 MLflow 模型部署到 Modal 基础设施。

如果您是 MLflow 模型部署的新手,请先阅读 MLflow 部署,以了解 MLflow 模型和部署的基本概念。

工作原理

该插件可实现部署过程的自动化:

  1. 提取:从模型 URI 中提取 MLflow 模型工件和依赖项
  2. 上传:将模型文件上传到 Modal 卷(Volume)以实现持久化存储
  3. 生成:生成一个带有 FastAPI 端点(/invocations, /predict_stream)的 Modal 应用
  4. 部署:Modal 构建包含所有依赖项的容器,并将其部署到无服务器基础设施
  5. 服务:返回一个 HTTPS 端点 URL,随时准备处理预测请求

生成的容器镜像与您的训练环境一致,确保开发环境和生产环境的行为一致。Modal 会自动处理自动扩缩容、GPU 分配和容器生命周期管理。

将模型部署到 Modal

本节概述了使用 MLflow 部署插件将模型部署到 Modal 的过程。有关 Python API 参考和教程,请参阅有用链接部分。

第 0 步:准备工作

安装库

确保安装了以下库

bash
pip install mlflow mlflow-modal-deploy modal

身份验证设置

配置 Modal 身份验证

bash
# 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 部署模型以进行本地测试。

python
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
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']}")

第 3 步:进行预测

部署完成后,您可以使用部署客户端进行预测。

python
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)

配置选项

创建部署时可以使用以下配置选项:

选项类型默认值描述
gpu字符串/列表GPU 类型:T4, L4, L40S, A10, A100, A100-40GB, A100-80GB, H100, H200, B200。支持多 GPU (H100:8)、专用 GPU (H100!) 或回退列表 (["H100", "A100"])
memoryint512以 MB 为单位的内存分配
cpu浮点数1.0CPU 核心数
timeoutint300请求超时时间(秒)
startup_timeoutint容器启动超时时间(对于大型模型很有用)
scaledown_windowint60空闲容器缩减前的等待时间(秒)
concurrent_inputsint1每个容器的最大并发请求数
min_containersint0最小预热容器数量(设置为 > 0 以避免冷启动)
max_containersint最大容器数量
enable_batchingboolFalse启用动态请求批处理
max_batch_sizeint8启用批处理时的最大批处理大小
batch_wait_msint100批处理等待时间(毫秒)
extra_pip_packageslist[]需要额外安装的 pip 包

有关这些选项的详细信息,请参阅 Modal 文档:

高级用法

GPU 选择

Modal 支持多种 GPU 类型以满足不同的工作负载。请查看 Modal 的 GPU 文档以获取可用 GPU 和配置选项的完整列表。

python
# 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"]}

高吞吐量部署

对于高吞吐量工作负载,请启用动态批处理。

python
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 工作区。

python
# Use workspace-specific URI
client = get_deploy_client("modal:/production")

或通过 CLI 操作。

bash
mlflow deployments create -t modal:/production -m runs:/<run_id>/model --name my-model

管理部署

bash
# 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

故障排除

bash
# Re-authenticate with Modal
modal setup

# Verify authentication
modal profile list

部署超时

对于加载时间较长的大型模型,请增加启动超时时间。

python
config = {
"startup_timeout": 600, # 10 minutes for model loading
"timeout": 300, # 5 minutes for inference requests
}

缺少依赖项

如果模型因导入错误而失败,请添加缺少的包。

python
config = {
"extra_pip_packages": ["missing-package>=1.0"],
}

查看构建日志

检查 Modal 控制台以获取详细的构建和运行时日志。

API 参考

mlflow-modal-deploy 插件与标准的 MLflow 部署接口集成。

有用链接