Repository navigation
Add MLFlowLogger.log_model to log and register models with MLflow - #22051
Open
Michael-Duck wants to merge 2 commits into
Open
Michael-Duck wants to merge 2 commits into
Michael-Duck wants to merge 2 commits into
Conversation
…ightning-AI#21437) feat: add `MLFlowLogger.log_model` to log a model to the MLflow run and to optionally register it in the MLflow model registry `MLFlowLogger` could only log checkpoints as plain MLflow artifacts, so the "Register Model" action of the MLflow UI was never available for models trained with Lightning. This adds the missing `mlflow.log_model` equivalent: ```python logger = MLFlowLogger(experiment_name="my-exp") trainer = Trainer(logger=logger) trainer.fit(model) logger.log_model(model, registered_model_name="my-model") ``` The model gets logged with the `pytorch` flavor of MLflow into the run owned by the logger and, if `registered_model_name` is given, registered in the MLflow model registry. Fixes Lightning-AI#21437 Signed-off-by: michael-duck <michael@realms.nexus>
…nagers guide Signed-off-by: michael-duck <michael@realms.nexus>
Michael-Duck
requested review from
ethanwharris,
justusschock and
tchaton
as code owners
October 23, 2026 01:08
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Adds
MLFlowLogger.log_model(), which logs a model to the MLflow run owned by the logger and optionally registers it in the MLflow model registry.MLFlowLoggercould only log checkpoints as plain MLflow artifacts, so the "Register Model" action of the MLflow UI was never available for models trained with Lightning. This adds the missingmlflow.log_modelequivalent:The model is logged with MLflow's
pytorchflavor, so it shows up as a model of the run in the MLflow UI and can be loaded again withmlflow.pyfunc.load_model("models:/my-model/1").Notes
mlflow.models.Model.log(..., run_id=...).mlflow.pytorch.log_model()would log into the globally active MLflow run and terminate that run when it exits, which would interfere with the run owned by the logger.serialization_format="pickle"is used by default when the installed MLflow supports it, because the newerpt2format needs an input example to trace the model, which the logger cannot infer. Passinput_example=...(orserialization_format=...) to override this.log_modelargument of the constructor is untouched and keeps controlling whether the checkpoints are logged as artifacts. The new method logs a model object instead of a checkpoint.Tests
pytest tests/tests_pytorch/loggers/test_mlflow.pytest_mlflow_logger_log_model: without this change it fails withAttributeError: 'MLFlowLogger' object has no attribute 'log_model', with this change it passes (it logs the model into the run, registers it in the model registry and loads it back for inference).21 passed, 1 failed, 2 errors. The failure (test_mlflow_run_name_setting) and the two errors (the environment variable leak check tripping overMLFLOW_TRACKING_URI, which MLflow 3.x exports) are pre-existing on this machine with MLflow 3.17 and fail identically without this change (20 passed, 1 failed, 2 errors).pytest tests/tests_pytorch/loggers/(wider run):107 passed, 32 failed, 2 skipped, 6 xfailed, 2 errors. All failures are environmental and unrelated to this change:tensorboard/tensorboardXandjsonargparseare not installed, and theddp_spawnbasedtest_logger_initializationcases cannot spawn processes in this sandbox (PermissionError: [Errno 1] Operation not permitted).ruff checkandruff format --checkon the changed files pass,sphinx-lintpasses on the changed documentation.Fixes #21437