hyperparameter_search.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. # coding=utf-8
  2. # Copyright 2023-present the HuggingFace Inc. team.
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. from .integrations import (
  16. is_optuna_available,
  17. is_ray_tune_available,
  18. is_sigopt_available,
  19. is_wandb_available,
  20. run_hp_search_optuna,
  21. run_hp_search_ray,
  22. run_hp_search_sigopt,
  23. run_hp_search_wandb,
  24. )
  25. from .trainer_utils import (
  26. HPSearchBackend,
  27. default_hp_space_optuna,
  28. default_hp_space_ray,
  29. default_hp_space_sigopt,
  30. default_hp_space_wandb,
  31. )
  32. from .utils import logging
  33. logger = logging.get_logger(__name__)
  34. class HyperParamSearchBackendBase:
  35. name: str
  36. pip_package: str = None
  37. @staticmethod
  38. def is_available():
  39. raise NotImplementedError
  40. def run(self, trainer, n_trials: int, direction: str, **kwargs):
  41. raise NotImplementedError
  42. def default_hp_space(self, trial):
  43. raise NotImplementedError
  44. def ensure_available(self):
  45. if not self.is_available():
  46. raise RuntimeError(
  47. f"You picked the {self.name} backend, but it is not installed. Run {self.pip_install()}."
  48. )
  49. @classmethod
  50. def pip_install(cls):
  51. return f"`pip install {cls.pip_package or cls.name}`"
  52. class OptunaBackend(HyperParamSearchBackendBase):
  53. name = "optuna"
  54. @staticmethod
  55. def is_available():
  56. return is_optuna_available()
  57. def run(self, trainer, n_trials: int, direction: str, **kwargs):
  58. return run_hp_search_optuna(trainer, n_trials, direction, **kwargs)
  59. def default_hp_space(self, trial):
  60. return default_hp_space_optuna(trial)
  61. class RayTuneBackend(HyperParamSearchBackendBase):
  62. name = "ray"
  63. pip_package = "'ray[tune]'"
  64. @staticmethod
  65. def is_available():
  66. return is_ray_tune_available()
  67. def run(self, trainer, n_trials: int, direction: str, **kwargs):
  68. return run_hp_search_ray(trainer, n_trials, direction, **kwargs)
  69. def default_hp_space(self, trial):
  70. return default_hp_space_ray(trial)
  71. class SigOptBackend(HyperParamSearchBackendBase):
  72. name = "sigopt"
  73. @staticmethod
  74. def is_available():
  75. return is_sigopt_available()
  76. def run(self, trainer, n_trials: int, direction: str, **kwargs):
  77. return run_hp_search_sigopt(trainer, n_trials, direction, **kwargs)
  78. def default_hp_space(self, trial):
  79. return default_hp_space_sigopt(trial)
  80. class WandbBackend(HyperParamSearchBackendBase):
  81. name = "wandb"
  82. @staticmethod
  83. def is_available():
  84. return is_wandb_available()
  85. def run(self, trainer, n_trials: int, direction: str, **kwargs):
  86. return run_hp_search_wandb(trainer, n_trials, direction, **kwargs)
  87. def default_hp_space(self, trial):
  88. return default_hp_space_wandb(trial)
  89. ALL_HYPERPARAMETER_SEARCH_BACKENDS = {
  90. HPSearchBackend(backend.name): backend for backend in [OptunaBackend, RayTuneBackend, SigOptBackend, WandbBackend]
  91. }
  92. def default_hp_search_backend() -> str:
  93. available_backends = [backend for backend in ALL_HYPERPARAMETER_SEARCH_BACKENDS.values() if backend.is_available()]
  94. if len(available_backends) > 0:
  95. name = available_backends[0].name
  96. if len(available_backends) > 1:
  97. logger.info(
  98. f"{len(available_backends)} hyperparameter search backends available. Using {name} as the default."
  99. )
  100. return name
  101. raise RuntimeError(
  102. "No hyperparameter search backend available.\n"
  103. + "\n".join(
  104. f" - To install {backend.name} run {backend.pip_install()}"
  105. for backend in ALL_HYPERPARAMETER_SEARCH_BACKENDS.values()
  106. )
  107. )