




from .integrations import (
    is_optuna_available,
    is_ray_available,
    is_sigopt_available,
    is_wandb_available,
    run_hp_search_optuna,
    run_hp_search_ray,
    run_hp_search_sigopt,
    run_hp_search_wandb,
)
from .trainer_utils import (
    HPSearchBackend,
    default_hp_space_optuna,
    default_hp_space_ray,
    default_hp_space_sigopt,
    default_hp_space_wandb,
)
from .utils import logging


a__		     =     logging.get_logger(__name__)


class    SCREAMING_SNAKE_CASE_				:



	"""simple docstring"""


	__magic_name__   :       str
	__magic_name__   :       str      	=		None






	@staticmethod
	def 					lowerCamelCase__     (					)	->       Optional[Any]:







						"""simple docstring"""







						raise NotImplementedError






	def 					lowerCamelCase__     (					self		:						List[str]					,     lowerCAmelCase		:						Dict					,     lowerCAmelCase		:						int					,     lowerCAmelCase		:						str					,     **lowerCAmelCase		:						Dict	)	->       List[str]:







						"""simple docstring"""







						raise NotImplementedError






	def 					lowerCamelCase__     (					self		:						Optional[Any]					,     lowerCAmelCase		:						Optional[Any]	)	->       Optional[int]:







						"""simple docstring"""







						raise NotImplementedError






	def 					lowerCamelCase__     (					self		:						List[Any]	)	->       List[str]:







						"""simple docstring"""







						if not self.is_available():
											raise RuntimeError(
											    F'''You picked the {self.name} backend, but it is not installed. Run {self.pip_install()}.'''	)






	@classmethod
	def 					lowerCamelCase__     (					cls		:						Union[str, Any]	)	->       List[str]:







						"""simple docstring"""







						return F'''`pip install {cls.pip_package or cls.name}`'''






class    SCREAMING_SNAKE_CASE_				(       _UpperCamelCase   ):



	"""simple docstring"""


	__magic_name__   :       Tuple      	=		'optuna'






	@staticmethod
	def 					lowerCamelCase__     (					)	->       Optional[int]:







						"""simple docstring"""







						return is_optuna_available()






	def 					lowerCamelCase__     (					self		:						Any					,     lowerCAmelCase		:						Union[str, Any]					,     lowerCAmelCase		:						int					,     lowerCAmelCase		:						str					,     **lowerCAmelCase		:						Optional[Any]	)	->       Any:







						"""simple docstring"""







						return run_hp_search_optuna(lowerCAmelCase					,     lowerCAmelCase					,     lowerCAmelCase					,     **lowerCAmelCase	)






	def 					lowerCamelCase__     (					self		:						str					,     lowerCAmelCase		:						Union[str, Any]	)	->       Union[str, Any]:







						"""simple docstring"""







						return default_hp_space_optuna(lowerCAmelCase	)






class    SCREAMING_SNAKE_CASE_				(       _UpperCamelCase   ):



	"""simple docstring"""


	__magic_name__   :       Optional[Any]      	=		'ray'
	__magic_name__   :       Optional[int]      	=		'\'ray[tune]\''






	@staticmethod
	def 					lowerCamelCase__     (					)	->       Dict:







						"""simple docstring"""







						return is_ray_available()






	def 					lowerCamelCase__     (					self		:						List[str]					,     lowerCAmelCase		:						Optional[int]					,     lowerCAmelCase		:						int					,     lowerCAmelCase		:						str					,     **lowerCAmelCase		:						str	)	->       List[Any]:







						"""simple docstring"""







						return run_hp_search_ray(lowerCAmelCase					,     lowerCAmelCase					,     lowerCAmelCase					,     **lowerCAmelCase	)






	def 					lowerCamelCase__     (					self		:						List[str]					,     lowerCAmelCase		:						Union[str, Any]	)	->       str:







						"""simple docstring"""







						return default_hp_space_ray(lowerCAmelCase	)






class    SCREAMING_SNAKE_CASE_				(       _UpperCamelCase   ):



	"""simple docstring"""


	__magic_name__   :       List[str]      	=		'sigopt'






	@staticmethod
	def 					lowerCamelCase__     (					)	->       Union[str, Any]:







						"""simple docstring"""







						return is_sigopt_available()






	def 					lowerCamelCase__     (					self		:						int					,     lowerCAmelCase		:						List[Any]					,     lowerCAmelCase		:						int					,     lowerCAmelCase		:						str					,     **lowerCAmelCase		:						Optional[Any]	)	->       Optional[Any]:







						"""simple docstring"""







						return run_hp_search_sigopt(lowerCAmelCase					,     lowerCAmelCase					,     lowerCAmelCase					,     **lowerCAmelCase	)






	def 					lowerCamelCase__     (					self		:						Optional[int]					,     lowerCAmelCase		:						List[Any]	)	->       Union[str, Any]:







						"""simple docstring"""







						return default_hp_space_sigopt(lowerCAmelCase	)






class    SCREAMING_SNAKE_CASE_				(       _UpperCamelCase   ):



	"""simple docstring"""


	__magic_name__   :       int      	=		'wandb'






	@staticmethod
	def 					lowerCamelCase__     (					)	->       str:







						"""simple docstring"""







						return is_wandb_available()






	def 					lowerCamelCase__     (					self		:						List[Any]					,     lowerCAmelCase		:						List[str]					,     lowerCAmelCase		:						int					,     lowerCAmelCase		:						str					,     **lowerCAmelCase		:						int	)	->       Union[str, Any]:







						"""simple docstring"""







						return run_hp_search_wandb(lowerCAmelCase					,     lowerCAmelCase					,     lowerCAmelCase					,     **lowerCAmelCase	)






	def 					lowerCamelCase__     (					self		:						Optional[int]					,     lowerCAmelCase		:						Any	)	->       Any:







						"""simple docstring"""







						return default_hp_space_wandb(lowerCAmelCase	)






a__		     =     {
    HPSearchBackend(backend.name): backend for backend in [OptunaBackend, RayTuneBackend, SigOptBackend, WandbBackend]
}





def 				A__ ()     ->						str:
					__UpperCamelCase					:	Tuple             =      [backend for backend in ALL_HYPERPARAMETER_SEARCH_BACKENDS.values() if backend.is_available()]
					if len(snake_case				) > 0:
										__UpperCamelCase					:	List[Any]             =      available_backends[0].name
										if len(snake_case				) > 1:
															logger.info(
															    F'''{len(snake_case				)} hyperparameter search backends available. Using {name} as the default.'''				)
										return name
					raise RuntimeError(
					    """No hyperparameter search backend available.\n"""
					    + """\n""".join(
					        F''' - To install {backend.name} run {backend.pip_install()}'''
					        for backend in ALL_HYPERPARAMETER_SEARCH_BACKENDS.values()				)				)












import json
import logging
import os
import sys
from time import time
from unittest.mock import patch

from transformers.testing_utils import TestCasePlus, require_torch_tpu


logging.basicConfig(level=logging.DEBUG)

a__		     =     logging.getLogger()





def 				A__ (snake_case   :		Tuple				)     ->						Dict:
					__UpperCamelCase					:	Dict             =      {}
					__UpperCamelCase					:	Dict             =      os.path.join(snake_case		,	"""all_results.json"""				)
					if os.path.exists(snake_case				):
										with open(snake_case		,	"""r"""				) as f:
															__UpperCamelCase					:	List[Any]             =      json.load(snake_case				)
					else:
										raise ValueError(F'''can\'t find {path}'''				)
					return results


a__		     =     logging.StreamHandler(sys.stdout)
logger.addHandler(stream_handler)







@require_torch_tpu
class    SCREAMING_SNAKE_CASE_				(       _UpperCamelCase   ):



	"""simple docstring"""


	def 					lowerCamelCase__     (					self		:						Dict	)	->       Dict:







						"""simple docstring"""







						import xla_spawn

						__UpperCamelCase					:	Dict             =      self.get_auto_remove_tmp_dir()
						__UpperCamelCase					:	str             =      F'''
            ./examples/pytorch/text-classification/run_glue.py
            --num_cores=8
            ./examples/pytorch/text-classification/run_glue.py
            --model_name_or_path distilbert-base-uncased
            --output_dir {tmp_dir}
            --overwrite_output_dir
            --train_file ./tests/fixtures/tests_samples/MRPC/train.csv
            --validation_file ./tests/fixtures/tests_samples/MRPC/dev.csv
            --do_train
            --do_eval
            --debug tpu_metrics_debug
            --per_device_train_batch_size=2
            --per_device_eval_batch_size=1
            --learning_rate=1e-4
            --max_steps=10
            --warmup_steps=2
            --seed=42
            --max_seq_length=128
            '''.split()

						with patch.object(lowerCAmelCase					,     """argv"""					,     lowerCAmelCase	):
											__UpperCamelCase					:	Union[str, Any]             =      time()
											xla_spawn.main()
											__UpperCamelCase					:	List[str]             =      time()

											__UpperCamelCase					:	Optional[int]             =      get_results(lowerCAmelCase	)
											self.assertGreaterEqual(result["""eval_accuracy"""]					,     0.75	)

											# Assert that the script takes less than 500 seconds to make sure it doesn't hang.
											self.assertLess(end - start					,     500	)






	def 					lowerCamelCase__     (					self		:						int	)	->       List[Any]:







						"""simple docstring"""







						import xla_spawn

						__UpperCamelCase					:	List[Any]             =      """
            ./tests/test_trainer_tpu.py
            --num_cores=8
            ./tests/test_trainer_tpu.py
            """.split()
						with patch.object(lowerCAmelCase					,     """argv"""					,     lowerCAmelCase	):
											xla_spawn.main()






