模型注册表:用 MLflow Registry 和 OCI 制品管理版本与别名。
模型注册表解决三件事:版本可追溯、部署引用稳定、回滚有据。MLflow Model Registry 提供版本、别名和 lineage;OCI 制品适合存放权重和推理镜像。
基础实验跟踪见 mlflow;本页聚焦注册、别名发布和部署引用。
pip install -U mlflow
mlflow server --host 0.0.0.0 --port 5000 \
--backend-store-uri "postgresql://$PG_USER:$PG_PASS@$PG_HOST:5432/mlflow" \
--artifacts-destination "s3://$ARTIFACT_BUCKET"
生产必须用外部数据库和对象存储;SQLite 加本地目录只能单机试用。
训练后记录模型并注册:
python - <<'PY'
import mlflow, mlflow.sklearn
mlflow.set_tracking_uri("http://mlflow.internal:5000")
with mlflow.start_run() as run:
mlflow.sklearn.log_model(model, name="model",
registered_model_name="qwen-lora")
print("run_id:", run.info.run_id)
PY
MLflow 3 用 name=,2.x 用 artifact_path=;客户端大版本要和服务端匹配。
用 REST API 创建版本和别名:
curl -s -X POST "$MLFLOW_URI/api/2.0/mlflow/model-versions/create" \
-H 'Content-Type: application/json' \
-d "{\"name\":\"qwen-lora\",\"source\":\"runs:/$RUN_ID/model\"}"
curl -s -X POST "$MLFLOW_URI/api/2.0/mlflow/registered-models/set-alias" \
-H 'Content-Type: application/json' \
-d '{"name":"qwen-lora","alias":"champion","version":"3"}'
curl -s "$MLFLOW_URI/api/2.0/mlflow/registered-models/alias?name=qwen-lora&alias=champion"
部署端只引用别名,不写死版本:
python - <<'PY'
import mlflow.pyfunc
mlflow.set_tracking_uri("http://mlflow.internal:5000")
model = mlflow.pyfunc.load_model("models:/qwen-lora@champion")
print(model.predict(df))
PY
用 OCI 仓库存放权重并保留不可变引用:
oras login registry.example.com
oras push registry.example.com/models/qwen-lora:v3 \
./model:/model:application/octet-stream
oras manifest fetch registry.example.com/models/qwen-lora:v3
source 必须是服务端能解析的 URI;runs:/ 依赖 artifact 根配置,跨集群迁移时改用绝对路径。log_model 时传入 signature。合上页面,先回答,再展开对照。
runs:/ 来源在什么情况下会失效?runs:/ 无法解析。跨环境发布应使用服务端可访问的绝对 URI。models:/name@alias 加载失败。