CoolFace
Apppublic

Aluode/PerceptionLabPortable

sourceHugging Faceupdated 9mo agoView on Hugging Face
0likes
hyperparameter_search.py142 linesDownload Raw Back to transformers
1# Copyright 2023-present the HuggingFace Inc. team.2#3# Licensed under the Apache License, Version 2.0 (the "License");4# you may not use this file except in compliance with the License.5# You may obtain a copy of the License at6#7#     http://www.apache.org/licenses/LICENSE-2.08#9# Unless required by applicable law or agreed to in writing, software10# distributed under the License is distributed on an "AS IS" BASIS,11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.12# See the License for the specific language governing permissions and13# limitations under the License.14from typing import Optional15 16from .integrations import (17    is_optuna_available,18    is_ray_tune_available,19    is_sigopt_available,20    is_wandb_available,21    run_hp_search_optuna,22    run_hp_search_ray,23    run_hp_search_sigopt,24    run_hp_search_wandb,25)26from .trainer_utils import (27    HPSearchBackend,28    default_hp_space_optuna,29    default_hp_space_ray,30    default_hp_space_sigopt,31    default_hp_space_wandb,32)33from .utils import logging34 35 36logger = logging.get_logger(__name__)37 38 39class HyperParamSearchBackendBase:40    name: str41    pip_package: Optional[str] = None42 43    @staticmethod44    def is_available():45        raise NotImplementedError46 47    def run(self, trainer, n_trials: int, direction: str, **kwargs):48        raise NotImplementedError49 50    def default_hp_space(self, trial):51        raise NotImplementedError52 53    def ensure_available(self):54        if not self.is_available():55            raise RuntimeError(56                f"You picked the {self.name} backend, but it is not installed. Run {self.pip_install()}."57            )58 59    @classmethod60    def pip_install(cls):61        return f"`pip install {cls.pip_package or cls.name}`"62 63 64class OptunaBackend(HyperParamSearchBackendBase):65    name = "optuna"66 67    @staticmethod68    def is_available():69        return is_optuna_available()70 71    def run(self, trainer, n_trials: int, direction: str, **kwargs):72        return run_hp_search_optuna(trainer, n_trials, direction, **kwargs)73 74    def default_hp_space(self, trial):75        return default_hp_space_optuna(trial)76 77 78class RayTuneBackend(HyperParamSearchBackendBase):79    name = "ray"80    pip_package = "'ray[tune]'"81 82    @staticmethod83    def is_available():84        return is_ray_tune_available()85 86    def run(self, trainer, n_trials: int, direction: str, **kwargs):87        return run_hp_search_ray(trainer, n_trials, direction, **kwargs)88 89    def default_hp_space(self, trial):90        return default_hp_space_ray(trial)91 92 93class SigOptBackend(HyperParamSearchBackendBase):94    name = "sigopt"95 96    @staticmethod97    def is_available():98        return is_sigopt_available()99 100    def run(self, trainer, n_trials: int, direction: str, **kwargs):101        return run_hp_search_sigopt(trainer, n_trials, direction, **kwargs)102 103    def default_hp_space(self, trial):104        return default_hp_space_sigopt(trial)105 106 107class WandbBackend(HyperParamSearchBackendBase):108    name = "wandb"109 110    @staticmethod111    def is_available():112        return is_wandb_available()113 114    def run(self, trainer, n_trials: int, direction: str, **kwargs):115        return run_hp_search_wandb(trainer, n_trials, direction, **kwargs)116 117    def default_hp_space(self, trial):118        return default_hp_space_wandb(trial)119 120 121ALL_HYPERPARAMETER_SEARCH_BACKENDS = {122    HPSearchBackend(backend.name): backend for backend in [OptunaBackend, RayTuneBackend, SigOptBackend, WandbBackend]123}124 125 126def default_hp_search_backend() -> str:127    available_backends = [backend for backend in ALL_HYPERPARAMETER_SEARCH_BACKENDS.values() if backend.is_available()]128    if len(available_backends) > 0:129        name = available_backends[0].name130        if len(available_backends) > 1:131            logger.info(132                f"{len(available_backends)} hyperparameter search backends available. Using {name} as the default."133            )134        return name135    raise RuntimeError(136        "No hyperparameter search backend available.\n"137        + "\n".join(138            f" - To install {backend.name} run {backend.pip_install()}"139            for backend in ALL_HYPERPARAMETER_SEARCH_BACKENDS.values()140        )141    )142