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

Fail hyperopt with full import error when Ray not installed #2203

Merged
merged 1 commit into from
Jun 28, 2022
Merged
Show file tree
Hide file tree
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
40 changes: 16 additions & 24 deletions ludwig/hyperopt/execution.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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:
Expand Down
5 changes: 4 additions & 1 deletion ludwig/hyperopt/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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)
Expand Down
3 changes: 2 additions & 1 deletion tests/integration_tests/test_hyperopt.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,15 @@
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
from tests.integration_tests.utils import category_feature, generate_data, text_feature

try:
import ray

from ludwig.hyperopt.execution import get_build_hyperopt_executor
except ImportError:
ray = None

Expand Down
2 changes: 1 addition & 1 deletion tests/integration_tests/test_hyperopt_ray.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@

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

try:
import ray

from ludwig.hyperopt.execution import get_build_hyperopt_executor
from ludwig.hyperopt.results import RayTuneResults
except ImportError:
ray = None
Expand Down
3 changes: 2 additions & 1 deletion tests/ludwig/utils/test_hyperopt_ray_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,15 @@

try:
from ray import tune

from ludwig.hyperopt.execution import get_build_hyperopt_executor
except ImportError:
RAY_AVAILABLE = False
else:
RAY_AVAILABLE = True

# 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": {
Expand Down