Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Try to fix experiment already exist issue in MLFlowHandler #7916

Merged
merged 7 commits into from
Jul 18, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 21 additions & 4 deletions monai/handlers/mlflow_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import torch
from torch.utils.data import Dataset

from monai.apps.utils import get_logger
from monai.config import IgniteInfo
from monai.utils import CommonKeys, ensure_tuple, min_version, optional_import

Expand All @@ -29,6 +30,9 @@
mlflow.entities, _ = optional_import(
"mlflow.entities", descriptor="Please install mlflow.entities before using MLFlowHandler."
)
MlflowException, _ = optional_import(
"mlflow.exceptions", name="MlflowException", descriptor="Please install mlflow before using MLFlowHandler."
)
pandas, _ = optional_import("pandas", descriptor="Please install pandas for recording the dataset.")
tqdm, _ = optional_import("tqdm", "4.47.0", min_version, "tqdm")

Expand All @@ -41,6 +45,8 @@

DEFAULT_TAG = "Loss"

logger = get_logger(module_name=__name__)


class MLFlowHandler:
"""
Expand Down Expand Up @@ -236,10 +242,21 @@ def start(self, engine: Engine) -> None:
def _set_experiment(self):
experiment = self.experiment
if not experiment:
experiment = self.client.get_experiment_by_name(self.experiment_name)
if not experiment:
experiment_id = self.client.create_experiment(self.experiment_name)
experiment = self.client.get_experiment(experiment_id)
for _retry_time in range(3):
try:
experiment = self.client.get_experiment_by_name(self.experiment_name)
binliunls marked this conversation as resolved.
Show resolved Hide resolved
if not experiment:
experiment_id = self.client.create_experiment(self.experiment_name)
experiment = self.client.get_experiment(experiment_id)
break
except MlflowException as e:
if "RESOURCE_ALREADY_EXISTS" in str(e):
logger.warning("Experiment already exists; delaying before retrying.")
time.sleep(1)
KumoLiu marked this conversation as resolved.
Show resolved Hide resolved
if _retry_time == 2:
raise e
else:
raise e

if experiment.lifecycle_stage != mlflow.entities.LifecycleStage.ACTIVE:
raise ValueError(f"Cannot set a deleted experiment '{self.experiment_name}' as the active experiment")
Expand Down
Loading