From 2be0c5bf335c3e2f551a7dbcffd7f69e3962bab4 Mon Sep 17 00:00:00 2001 From: ChrisW09 <50968720+ChrisW09@users.noreply.github.com> Date: Wed, 21 Oct 2026 20:51:14 +0200 Subject: [PATCH] fix(models): honour mlflow_log_model for the checkpoint upload Only Lightning's MLFlowLogger received mlflow_log_model; the end-of-fit metadata upload in _log_run_metadata_to_mlflow logged every file in /checkpoints/ regardless. Skip that upload when the flag is off. Fixes #543 --- deeptab/models/_mixins/fit.py | 2 +- tests/integration/test_mlflow_artifacts.py | 40 ++++++++++++++++++++++ 2 files changed, 41 insertions(+), 1 deletion(-) create mode 100644 tests/integration/test_mlflow_artifacts.py diff --git a/deeptab/models/_mixins/fit.py b/deeptab/models/_mixins/fit.py index 036a0480..cb04e4ed 100644 --- a/deeptab/models/_mixins/fit.py +++ b/deeptab/models/_mixins/fit.py @@ -1095,7 +1095,7 @@ def _log_run_metadata_to_mlflow( except Exception: # noqa: S110 pass ckpt_dir = os.path.join(_run_dir, "checkpoints") - if os.path.isdir(ckpt_dir): + if obs.mlflow_log_model and os.path.isdir(ckpt_dir): for ckpt in os.listdir(ckpt_dir): try: client.log_artifact(run_id, os.path.join(ckpt_dir, ckpt), artifact_path="checkpoints") diff --git a/tests/integration/test_mlflow_artifacts.py b/tests/integration/test_mlflow_artifacts.py new file mode 100644 index 00000000..ca9b272d --- /dev/null +++ b/tests/integration/test_mlflow_artifacts.py @@ -0,0 +1,40 @@ +"""The MLflow checkpoint upload follows ``ObservabilityConfig.mlflow_log_model``.""" + +import numpy as np +import pytest + +from deeptab.core.observability import ObservabilityConfig +from deeptab.models import MLPRegressor + + +def _artifact_paths(client, run_id, path=None): + paths = [] + for artifact in client.list_artifacts(run_id, path): + paths.extend(_artifact_paths(client, run_id, artifact.path) if artifact.is_dir else [artifact.path]) + return paths + + +@pytest.mark.parametrize("log_model", [True, False]) +def test_checkpoint_upload_follows_mlflow_log_model(log_model, tmp_path): + pytest.importorskip("mlflow") + from mlflow.tracking import MlflowClient + + observability = ObservabilityConfig( + root_dir=str(tmp_path), experiment_trackers=["mlflow"], mlflow_log_model=log_model + ) + features = np.random.default_rng(0).normal(size=(60, 3)) + model = MLPRegressor(observability_config=observability, random_state=0) + model.fit(features, features[:, 0], max_epochs=1, accelerator="cpu", enable_progress_bar=False) + + client = MlflowClient(observability.mlflow_tracking_uri) + experiment = client.get_experiment_by_name(observability.mlflow_experiment_name) + assert experiment is not None + (run,) = client.search_runs([experiment.experiment_id]) + artifacts = _artifact_paths(client, run.info.run_id) + assert "summary.json" in artifacts + + checkpoints = [path for path in artifacts if path.endswith(".ckpt")] + if log_model: + assert "checkpoints/best_model.ckpt" in checkpoints + else: + assert checkpoints == []