






"""simple docstring"""



import unittest
from pathlib import Path
from tempfile import NamedTemporaryFile, TemporaryDirectory

from transformers import BertConfig, BertTokenizerFast, FeatureExtractionPipeline
from transformers.convert_graph_to_onnx import (
    convert,
    ensure_valid_input,
    generate_identified_filename,
    infer_shapes,
    quantize,
)
from transformers.testing_utils import require_tf, require_tokenizers, require_torch, slow






class 							__SCREAMING_SNAKE_CASE						:


							def 						_UpperCamelCase       (			self      :						List[Any]			,    snake_case      :						Tuple			,    snake_case      :						int			,    snake_case      :						Optional[int]							):
													'''simple docstring'''





													return None






class 							__SCREAMING_SNAKE_CASE						:


							def 						_UpperCamelCase       (			self      :						Optional[Any]			,    snake_case      :						List[Any]			,    snake_case      :						List[Any]			,    snake_case      :						Dict			,    snake_case      :						List[str]							):
													'''simple docstring'''





													return None






class 							__SCREAMING_SNAKE_CASE						(				unittest.TestCase		):
							snake_case_     =							[
							    # (model_name, model_kwargs)
							    ('''bert-base-cased''', {}),
							    ('''gpt2''', {'''use_cache''': False}),  # We don't support exporting GPT2 past keys anymore
							]


							@require_tf
							@slow
							def 						_UpperCamelCase       (			self      :						Union[str, Any]							):
													'''simple docstring'''





													for model, model_kwargs in OnnxExportTestCase.MODEL_TO_TEST:
																			self._test_export(snake_case			,    """tf"""			,    12			,    **snake_case							)


							@require_torch
							@slow
							def 						_UpperCamelCase       (			self      :						Tuple							):
													'''simple docstring'''





													for model, model_kwargs in OnnxExportTestCase.MODEL_TO_TEST:
																			self._test_export(snake_case			,    """pt"""			,    12			,    **snake_case							)


							@require_torch
							@slow
							def 						_UpperCamelCase       (			self      :						str							):
													'''simple docstring'''





													from transformers import BertModel

													A__						:  List[str]		  =  ["""[UNK]""", """[SEP]""", """[CLS]""", """[PAD]""", """[MASK]""", """some""", """other""", """words"""]
													with NamedTemporaryFile(mode="""w+t"""							) as vocab_file:
																			vocab_file.write("""\n""".join(snake_case							)							)
																			vocab_file.flush()
																			A__						:  Optional[Any]		  =  BertTokenizerFast(vocab_file.name							)

													with TemporaryDirectory() as bert_save_dir:
																			A__						:  str		  =  BertModel(BertConfig(vocab_size=len(snake_case							)							)							)
																			model.save_pretrained(snake_case							)
																			self._test_export(snake_case			,    """pt"""			,    12			,    snake_case							)


							@require_tf
							@slow
							def 						_UpperCamelCase       (			self      :						Dict							):
													'''simple docstring'''





													for model, model_kwargs in OnnxExportTestCase.MODEL_TO_TEST:
																			A__						:  Dict		  =  self._test_export(snake_case			,    """tf"""			,    12			,    **snake_case							)
																			A__						:  str		  =  quantize(Path(snake_case							)							)

																			# Ensure the actual quantized model is not bigger than the original one
																			if quantized_path.stat().st_size >= Path(snake_case							).stat().st_size:
																									self.fail("""Quantized model is bigger than initial ONNX model"""							)


							@require_torch
							@slow
							def 						_UpperCamelCase       (			self      :						int							):
													'''simple docstring'''





													for model, model_kwargs in OnnxExportTestCase.MODEL_TO_TEST:
																			A__						:  Tuple		  =  self._test_export(snake_case			,    """pt"""			,    12			,    **snake_case							)
																			A__						:  Tuple		  =  quantize(snake_case							)

																			# Ensure the actual quantized model is not bigger than the original one
																			if quantized_path.stat().st_size >= Path(snake_case							).stat().st_size:
																									self.fail("""Quantized model is bigger than initial ONNX model"""							)


							def 						_UpperCamelCase       (			self      :						Optional[Any]			,    snake_case      :						Optional[int]			,    snake_case      :						int			,    snake_case      :						Dict			,    snake_case      :						Union[str, Any]=None			,    **snake_case      :						str							):
													'''simple docstring'''





													try:
																			# Compute path
																			with TemporaryDirectory() as tempdir:
																									A__						:  Union[str, Any]		  =  Path(snake_case							).joinpath("""model.onnx"""							)

																			# Remove folder if exists
																			if path.parent.exists():
																									path.parent.rmdir()

																			# Export
																			convert(snake_case			,    snake_case			,    snake_case			,    snake_case			,    snake_case			,    **snake_case							)

																			return path
													except Exception as e:
																			self.fail(snake_case							)


							@require_torch
							@require_tokenizers
							@slow
							def 						_UpperCamelCase       (			self      :						Optional[Any]							):
													'''simple docstring'''





													from transformers import BertModel

													A__						:  Any		  =  BertModel(BertConfig.from_pretrained("""lysandre/tiny-bert-random"""							)							)
													A__						:  int		  =  BertTokenizerFast.from_pretrained("""lysandre/tiny-bert-random"""							)
													self._test_infer_dynamic_axis(snake_case			,    snake_case			,    """pt"""							)


							@require_tf
							@require_tokenizers
							@slow
							def 						_UpperCamelCase       (			self      :						int							):
													'''simple docstring'''





													from transformers import TFBertModel

													A__						:  str		  =  TFBertModel(BertConfig.from_pretrained("""lysandre/tiny-bert-random"""							)							)
													A__						:  int		  =  BertTokenizerFast.from_pretrained("""lysandre/tiny-bert-random"""							)
													self._test_infer_dynamic_axis(snake_case			,    snake_case			,    """tf"""							)


							def 						_UpperCamelCase       (			self      :						Dict			,    snake_case      :						Union[str, Any]			,    snake_case      :						Optional[Any]			,    snake_case      :						Optional[Any]							):
													'''simple docstring'''





													A__						:  int		  =  FeatureExtractionPipeline(snake_case			,    snake_case							)

													A__						:  List[Any]		  =  ["""input_ids""", """token_type_ids""", """attention_mask""", """output_0""", """output_1"""]
													A__						:  List[Any]		  =  infer_shapes(snake_case			,    snake_case							)

													# Assert all variables are present
													self.assertEqual(len(snake_case							)			,    len(snake_case							)							)
													self.assertTrue(all(var_name in shapes for var_name in variable_names							)							)
													self.assertSequenceEqual(variable_names[:3]			,    snake_case							)
													self.assertSequenceEqual(variable_names[3:]			,    snake_case							)

													# Assert inputs are {0: batch, 1: sequence}
													for var_name in ["input_ids", "token_type_ids", "attention_mask"]:
																			self.assertDictEqual(shapes[var_name]			,    {0: """batch""", 1: """sequence"""}							)

													# Assert outputs are {0: batch, 1: sequence} and {0: batch}
													self.assertDictEqual(shapes["""output_0"""]			,    {0: """batch""", 1: """sequence"""}							)
													self.assertDictEqual(shapes["""output_1"""]			,    {0: """batch"""}							)


							def 						_UpperCamelCase       (			self      :						Optional[int]							):
													'''simple docstring'''





													A__						:  int		  =  ["""input_ids""", """attention_mask""", """token_type_ids"""]
													A__						:  str		  =  {"""input_ids""": [1, 2, 3, 4], """attention_mask""": [0, 0, 0, 0], """token_type_ids""": [1, 1, 1, 1]}
													A__						:  str		  =  ensure_valid_input(FuncContiguousArgs()			,    snake_case			,    snake_case							)

													# Should have exactly the same number of args (all are valid)
													self.assertEqual(len(snake_case							)			,    3							)

													# Should have exactly the same input names
													self.assertEqual(set(snake_case							)			,    set(snake_case							)							)

													# Parameter should be reordered according to their respective place in the function:
													# (input_ids, token_type_ids, attention_mask)
													self.assertEqual(snake_case			,    (tokens["""input_ids"""], tokens["""token_type_ids"""], tokens["""attention_mask"""])							)

													# Generated args are interleaved with another args (for instance parameter "past" in GPT2)
													A__						:  int		  =  ensure_valid_input(FuncNonContiguousArgs()			,    snake_case			,    snake_case							)

													# Should have exactly the one arg (all before the one not provided "some_other_args")
													self.assertEqual(len(snake_case							)			,    1							)
													self.assertEqual(len(snake_case							)			,    1							)

													# Should have only "input_ids"
													self.assertEqual(inputs_args[0]			,    tokens["""input_ids"""]							)
													self.assertEqual(ordered_input_names[0]			,    """input_ids"""							)


							def 						_UpperCamelCase       (			self      :						List[str]							):
													'''simple docstring'''





													A__						:  List[Any]		  =  generate_identified_filename(Path("""/home/something/my_fake_model.onnx"""							)			,    """-test"""							)
													self.assertEqual("""/home/something/my_fake_model-test.onnx"""			,    generated.as_posix()							)









"""simple docstring"""



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						:
							snake_case_     =							42
							snake_case_     =							None


							@staticmethod
							def 						_UpperCamelCase       (			):
													'''simple docstring'''





													raise NotImplementedError


							def 						_UpperCamelCase       (			self      :						str			,    snake_case      :						int			,    snake_case      :						int			,    snake_case      :						str			,    **snake_case      :						Any							):
													'''simple docstring'''





													raise NotImplementedError


							def 						_UpperCamelCase       (			self      :						Union[str, Any]			,    snake_case      :						Optional[int]							):
													'''simple docstring'''





													raise NotImplementedError


							def 						_UpperCamelCase       (			self      :						Union[str, Any]							):
													'''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 						_UpperCamelCase       (			cls      :						Optional[int]							):
													'''simple docstring'''





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






class 							__SCREAMING_SNAKE_CASE						(				UpperCamelCase		):
							snake_case_     =							'optuna'


							@staticmethod
							def 						_UpperCamelCase       (			):
													'''simple docstring'''





													return is_optuna_available()


							def 						_UpperCamelCase       (			self      :						str			,    snake_case      :						str			,    snake_case      :						int			,    snake_case      :						str			,    **snake_case      :						Any							):
													'''simple docstring'''





													return run_hp_search_optuna(snake_case			,    snake_case			,    snake_case			,    **snake_case							)


							def 						_UpperCamelCase       (			self      :						Optional[Any]			,    snake_case      :						int							):
													'''simple docstring'''





													return default_hp_space_optuna(snake_case							)






class 							__SCREAMING_SNAKE_CASE						(				UpperCamelCase		):
							snake_case_     =							'ray'
							snake_case_     =							'\'ray[tune]\''


							@staticmethod
							def 						_UpperCamelCase       (			):
													'''simple docstring'''





													return is_ray_available()


							def 						_UpperCamelCase       (			self      :						Any			,    snake_case      :						Tuple			,    snake_case      :						int			,    snake_case      :						str			,    **snake_case      :						List[Any]							):
													'''simple docstring'''





													return run_hp_search_ray(snake_case			,    snake_case			,    snake_case			,    **snake_case							)


							def 						_UpperCamelCase       (			self      :						Dict			,    snake_case      :						Dict							):
													'''simple docstring'''





													return default_hp_space_ray(snake_case							)






class 							__SCREAMING_SNAKE_CASE						(				UpperCamelCase		):
							snake_case_     =							'sigopt'


							@staticmethod
							def 						_UpperCamelCase       (			):
													'''simple docstring'''





													return is_sigopt_available()


							def 						_UpperCamelCase       (			self      :						str			,    snake_case      :						str			,    snake_case      :						int			,    snake_case      :						str			,    **snake_case      :						Union[str, Any]							):
													'''simple docstring'''





													return run_hp_search_sigopt(snake_case			,    snake_case			,    snake_case			,    **snake_case							)


							def 						_UpperCamelCase       (			self      :						List[str]			,    snake_case      :						Any							):
													'''simple docstring'''





													return default_hp_space_sigopt(snake_case							)






class 							__SCREAMING_SNAKE_CASE						(				UpperCamelCase		):
							snake_case_     =							'wandb'


							@staticmethod
							def 						_UpperCamelCase       (			):
													'''simple docstring'''





													return is_wandb_available()


							def 						_UpperCamelCase       (			self      :						int			,    snake_case      :						int			,    snake_case      :						int			,    snake_case      :						str			,    **snake_case      :						Any							):
													'''simple docstring'''





													return run_hp_search_wandb(snake_case			,    snake_case			,    snake_case			,    **snake_case							)


							def 						_UpperCamelCase       (			self      :						Optional[int]			,    snake_case      :						Union[str, Any]							):
													'''simple docstring'''





													return default_hp_space_wandb(snake_case							)




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




def      _lowerCAmelCase		(							)		->str:
						A__						:  List[Any]		  =  [backend for backend in ALL_HYPERPARAMETER_SEARCH_BACKENDS.values() if backend.is_available()]
						if len(UpperCAmelCase__      ) > 0:
												A__						:  Dict		  =  available_backends[0].name
												if len(UpperCAmelCase__      ) > 1:
																		logger.info(
																		    f'{len(UpperCAmelCase__      )} 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()      )      )






