Skip to content

Commit e457a5a

Browse files
authored
5649 auto close mlflow run (#5656)
Fixes #5649 . ### Description Add the `auto_close` parameter to mlflow_handler so that it can close the mlflow run according to given value. Update the default handler parameter, so that trainer and evaluator could have `True` value on `auto_close`. ### Types of changes <!--- Put an `x` in all the boxes that apply, and remove the not applicable items --> - [x] Non-breaking change (fix or new feature that would not break existing functionality). - [ ] Breaking change (fix or new feature that would cause existing functionality to change). - [ ] New tests added to cover the changes. - [ ] Integration tests passed locally by running `./runtests.sh -f -u --net --coverage`. - [ ] Quick tests passed locally by running `./runtests.sh --quick --unittests --disttests`. - [ ] In-line docstrings updated. - [ ] Documentation updated, tested `make html` command in the `docs/` folder. Signed-off-by: binliu <binliu@nvidia.com>
1 parent 90f1893 commit e457a5a

5 files changed

Lines changed: 12 additions & 3 deletions

File tree

monai/bundle/scripts.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -561,6 +561,7 @@ def run(
561561
"run_name": "@run_name",
562562
"iteration_log": True,
563563
"output_transform": "$monai.handlers.from_engine(['loss'], first=True)",
564+
"close_on_complete": True,
564565
},
565566
"validator": {
566567
"_target_": "MLFlowHandler",
@@ -577,6 +578,7 @@ def run(
577578
"experiment_name": "@experiment_name",
578579
"run_name": "@run_name",
579580
"iteration_log": False,
581+
"close_on_complete": True,
580582
},
581583
},
582584
},

monai/bundle/utils.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@
122122
"epoch_log": True,
123123
"tag_name": "train_loss",
124124
"output_transform": "$monai.handlers.from_engine(['loss'], first=True)",
125+
"close_on_complete": True,
125126
},
126127
# MLFlowHandler config for the validator
127128
"validator": {
@@ -140,6 +141,7 @@
140141
"experiment_name": "@experiment_name",
141142
"run_name": "@run_name",
142143
"iteration_log": False,
144+
"close_on_complete": True,
143145
},
144146
},
145147
}

monai/handlers/mlflow_handler.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ class MLFlowHandler:
8282
artifacts: paths to images that need to be recorded after a whole run.
8383
optimizer_param_names: parameters' name in optimizer that need to be record during runing,
8484
defaults to "lr".
85+
close_on_complete: whether to close the mlflow run in `complete` phase in workflow, default to False.
8586
8687
For more details of MLFlow usage, please refer to: https://mlflow.org/docs/latest/index.html.
8788
@@ -101,11 +102,12 @@ def __init__(
101102
global_epoch_transform: Callable = lambda x: x,
102103
state_attributes: Optional[Sequence[str]] = None,
103104
tag_name: str = DEFAULT_TAG,
104-
experiment_name: str = "default_experiment",
105+
experiment_name: str = "monai_experiment",
105106
run_name: Optional[str] = None,
106107
experiment_param: Optional[Dict] = None,
107108
artifacts: Optional[Union[str, Sequence[Path]]] = None,
108109
optimizer_param_names: Union[str, Sequence[str]] = "lr",
110+
close_on_complete: bool = False,
109111
) -> None:
110112
if tracking_uri is not None:
111113
mlflow.set_tracking_uri(tracking_uri)
@@ -124,6 +126,7 @@ def __init__(
124126
self.artifacts = ensure_tuple(artifacts)
125127
self.optimizer_param_names = ensure_tuple(optimizer_param_names)
126128
self.client = mlflow.MlflowClient()
129+
self.close_on_complete = close_on_complete
127130

128131
def _delete_exist_param_in_dict(self, param_dict: Dict) -> None:
129132
"""
@@ -156,6 +159,8 @@ def attach(self, engine: Engine) -> None:
156159
engine.add_event_handler(Events.EPOCH_COMPLETED, self.epoch_completed)
157160
if not engine.has_event_handler(self.complete, Events.COMPLETED):
158161
engine.add_event_handler(Events.COMPLETED, self.complete)
162+
if self.close_on_complete and (not engine.has_event_handler(self.close, Events.COMPLETED)):
163+
engine.add_event_handler(Events.COMPLETED, self.close)
159164

160165
def start(self, engine: Engine) -> None:
161166
"""

tests/test_fl_monai_algo.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,7 @@ def test_train(self, input_params):
155155
"_target_": "MLFlowHandler",
156156
"tracking_uri": Path(data_dir).as_uri() + "/mlflow_override",
157157
"output_transform": "$monai.handlers.from_engine(['loss'], first=True)",
158+
"close_on_complete": True,
158159
}
159160
},
160161
}
@@ -175,8 +176,6 @@ def test_train(self, input_params):
175176
# test train
176177
algo.train(data=data, extra={})
177178
algo.finalize()
178-
# must close it as we are changing different temp dir for cases here
179-
algo.train_parser.get_parsed_content("train#handlers")[-1].close()
180179
self.assertTrue(os.path.exists(f"{data_dir}/mlflow_override"))
181180
shutil.rmtree(data_dir)
182181

tests/test_handler_mlflow.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ def _update_metric(engine):
5353
state_attributes=["test"],
5454
experiment_param=experiment_param,
5555
artifacts=[artifact_path],
56+
close_on_complete=True,
5657
)
5758
handler.attach(engine)
5859
engine.run(range(3), max_epochs=2)

0 commit comments

Comments
 (0)