diff --git a/ludwig/hyperopt/execution.py b/ludwig/hyperopt/execution.py index bff2e457c53..e477520e694 100644 --- a/ludwig/hyperopt/execution.py +++ b/ludwig/hyperopt/execution.py @@ -14,7 +14,15 @@ from pathlib import Path from typing import Any, Callable, Dict, List, Optional, Tuple, Union +import ray from packaging import version +from ray import tune +from ray.tune import register_trainable, Stopper +from ray.tune.schedulers.resource_changing_scheduler import DistributeResources, ResourceChangingScheduler +from ray.tune.suggest import BasicVariantGenerator, ConcurrencyLimiter +from ray.tune.utils import wait_for_gpu +from ray.tune.utils.placement_groups import PlacementGroupFactory +from ray.util.queue import Queue as RayQueue from ludwig.api import LudwigModel from ludwig.backend import initialize_backend, RAY @@ -30,32 +38,16 @@ from ludwig.utils.fs_utils import has_remote_protocol from ludwig.utils.misc_utils import get_from_registry -logger = logging.getLogger(__name__) - -try: - import ray - from ray import tune - from ray.tune import register_trainable, Stopper - from ray.tune.schedulers.resource_changing_scheduler import DistributeResources, ResourceChangingScheduler - from ray.tune.suggest import BasicVariantGenerator, ConcurrencyLimiter - - _ray_114 = version.parse(ray.__version__) >= version.parse("1.14") - if _ray_114: - from ray.tune.search import SEARCH_ALG_IMPORT - from ray.tune.syncer import get_node_to_storage_syncer, SyncConfig - else: - from ray.tune.syncer import get_cloud_sync_client - from ray.tune.suggest import SEARCH_ALG_IMPORT +_ray_114 = version.parse(ray.__version__) >= version.parse("1.14") +if _ray_114: + from ray.tune.search import SEARCH_ALG_IMPORT + from ray.tune.syncer import get_node_to_storage_syncer, SyncConfig +else: + from ray.tune.suggest import SEARCH_ALG_IMPORT + from ray.tune.syncer import get_cloud_sync_client - from ray.tune.utils import wait_for_gpu - from ray.tune.utils.placement_groups import PlacementGroupFactory - from ray.util.queue import Queue as RayQueue -except ImportError as e: - logger.warning(f"ImportError (execution.py) failed to import ray with error: \n\t{e}") - ray = None - Stopper = object - get_horovod_kwargs = None +logger = logging.getLogger(__name__) try: diff --git a/ludwig/hyperopt/run.py b/ludwig/hyperopt/run.py index 8ea4ef7c99c..8740f4a365f 100644 --- a/ludwig/hyperopt/run.py +++ b/ludwig/hyperopt/run.py @@ -12,7 +12,6 @@ from ludwig.constants import COMBINED, EXECUTOR, HYPEROPT, LOSS, MINIMIZE, TEST, TRAINING, TYPE, VALIDATION from ludwig.data.split import get_splitter from ludwig.features.feature_registries import output_type_registry -from ludwig.hyperopt.execution import executor_registry, get_build_hyperopt_executor, RayTuneExecutor from ludwig.hyperopt.results import HyperoptResults from ludwig.hyperopt.utils import print_hyperopt_results, save_hyperopt_stats, should_tune_preprocessing from ludwig.utils.defaults import default_random_seed, merge_with_defaults @@ -162,6 +161,8 @@ def hyperopt( :return: (List[dict]) List of results for each trial, ordered by descending performance on the target metric. """ + from ludwig.hyperopt.execution import get_build_hyperopt_executor, RayTuneExecutor + # check if config is a path or a dict if isinstance(config, str): # assume path with open_file(config, "r") as def_file: @@ -359,6 +360,8 @@ def hyperopt( def update_hyperopt_params_with_defaults(hyperopt_params): + from ludwig.hyperopt.execution import executor_registry + set_default_value(hyperopt_params, EXECUTOR, {}) set_default_value(hyperopt_params, "split", VALIDATION) set_default_value(hyperopt_params, "output_feature", COMBINED) diff --git a/tests/integration_tests/test_hyperopt.py b/tests/integration_tests/test_hyperopt.py index b5f3bdc574a..eeb1bfc0910 100644 --- a/tests/integration_tests/test_hyperopt.py +++ b/tests/integration_tests/test_hyperopt.py @@ -21,7 +21,6 @@ import torch from ludwig.constants import ACCURACY, RAY, TRAINER -from ludwig.hyperopt.execution import get_build_hyperopt_executor from ludwig.hyperopt.results import HyperoptResults, RayTuneResults from ludwig.hyperopt.run import hyperopt, update_hyperopt_params_with_defaults from ludwig.utils.defaults import merge_with_defaults @@ -29,6 +28,8 @@ try: import ray + + from ludwig.hyperopt.execution import get_build_hyperopt_executor except ImportError: ray = None diff --git a/tests/integration_tests/test_hyperopt_ray.py b/tests/integration_tests/test_hyperopt_ray.py index 4bec78e0185..52ea05c12fa 100644 --- a/tests/integration_tests/test_hyperopt_ray.py +++ b/tests/integration_tests/test_hyperopt_ray.py @@ -23,7 +23,6 @@ from ludwig.constants import ACCURACY, TRAINER from ludwig.contribs import MlflowCallback -from ludwig.hyperopt.execution import get_build_hyperopt_executor from ludwig.hyperopt.run import hyperopt, update_hyperopt_params_with_defaults from ludwig.utils.defaults import merge_with_defaults from tests.integration_tests.utils import category_feature, generate_data, text_feature @@ -31,6 +30,7 @@ try: import ray + from ludwig.hyperopt.execution import get_build_hyperopt_executor from ludwig.hyperopt.results import RayTuneResults except ImportError: ray = None diff --git a/tests/ludwig/utils/test_hyperopt_ray_utils.py b/tests/ludwig/utils/test_hyperopt_ray_utils.py index cb4eab848c7..684e29c1df0 100644 --- a/tests/ludwig/utils/test_hyperopt_ray_utils.py +++ b/tests/ludwig/utils/test_hyperopt_ray_utils.py @@ -16,6 +16,8 @@ try: from ray import tune + + from ludwig.hyperopt.execution import get_build_hyperopt_executor except ImportError: RAY_AVAILABLE = False else: @@ -23,7 +25,6 @@ # from ludwig.hyperopt.sampling import RayTuneSampler TDOO: remove from ludwig.constants import RAY, TYPE -from ludwig.hyperopt.execution import get_build_hyperopt_executor HYPEROPT_PARAMS = { "test_1": {