Sub topic 8
Promoting a Model Version to Champion
This chapter assumes that you know basic Python, virtual environments, MLflow experiments, runs, metrics, and artifacts. You should also know how to train and save a simple scikit-learn model and use a command line.
A model version becomes useful in production only after you choose which version serving code should load. In MLflow, promotion means assigning a named alias, such as champion, to a model version. The alias is a movable pointer. The model version remains fixed.
The promotion model
A registered model, such as iris-classifier, contains multiple versions. Each version points to a model artifact logged by a run. An alias points to one version at a time.
A typical workflow is:
- Register a newly trained model.
- Validate the new version.
- Record the validation result.
- Move the
championalias to that version. - Make serving code load the alias.
The alias separates deployment code from version numbers. The code does not need to change when a new version is promoted.
Record validation before promotion
Tags and descriptions document why a version is eligible for promotion. A tag is a key-value label that software can search or evaluate. A description is longer human-readable text.
import mlflow
from mlflow import MlflowClient
mlflow.set_tracking_uri("http://127.0.0.1:5000")
client = MlflowClient()
model_name = "iris-classifier"
version = "2"
client.update_model_version(
name=model_name,
version=version,
description="Validated on the held-out Iris test set.",
)
client.set_model_version_tag(
name=model_name,
version=version,
key="validation_status",
value="passed",
)
client.set_model_version_tag(
name=model_name,
version=version,
key="validated_by",
value="offline-evaluation",
)Tags do not enforce a promotion policy by themselves. Your automation or review process must check them before changing the alias.
Move the champion alias
Use MlflowClient to assign the alias:
from mlflow import MlflowClient
client = MlflowClient(tracking_uri="http://127.0.0.1:5000")
client.set_registered_model_alias(
name="iris-classifier",
alias="champion",
version="2",
)
current = client.get_model_version_by_alias(
name="iris-classifier",
alias="champion",
)
print(f"Champion version: {current.version}")If champion already points to version 1, this operation moves it to version 2. The old version is not deleted. A single version can have several aliases, such as champion and approved, but an alias name resolves to one version for a registered model.
Common errors
- Using a run ID as a version: A run ID identifies an MLflow run. A model version is identified by its registered model name and version number.
- Promoting before validation: Registration records an artifact. It does not prove that the artifact passed testing.
- Loading a fixed version in serving code:
models:/iris-classifier/2always loads version 2. It does not follow future promotions. - Confusing aliases with stages: Modern MLflow code should use aliases instead of deprecated stage transitions.
- Ignoring permissions: The identity that promotes a model needs permission to update the registered model alias.
Verify loading and rollback
Serving code can follow the alias:
import mlflow
mlflow.set_tracking_uri("http://127.0.0.1:5000")
model = mlflow.pyfunc.load_model(
"models:/iris-classifier@champion"
)If version 2 fails after promotion, point the same alias at the last known good version:
from mlflow import MlflowClient
client = MlflowClient(tracking_uri="http://127.0.0.1:5000")
client.set_registered_model_alias(
"iris-classifier",
"champion",
"1",
)The serving code stays unchanged because it still loads models:/iris-classifier@champion.
Exercise
What changes when the champion alias moves from version 1 to version 2, and what does not change?
Show answer
The alias now resolves to version 2, so alias-based loading uses version 2. The registered versions, their artifacts, and the serving code do not change.