


'''simple docstring'''
import unittest

from transformers import is_torch_available
from transformers.testing_utils import require_torch, slow, torch_device

from ...generation.test_utils import GenerationTesterMixin
from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
  import torch

  from transformers import (
      OPENAI_GPT_PRETRAINED_MODEL_ARCHIVE_LIST,
      OpenAIGPTConfig,
      OpenAIGPTDoubleHeadsModel,
      OpenAIGPTForSequenceClassification,
      OpenAIGPTLMHeadModel,
      OpenAIGPTModel,
  )




class      lowerCamelCase_  :






      def __init__(   self							:       List[str]		,		_A							:       Union[str, Any]		,		_A							:       Tuple=13		,		_A							:       Optional[int]=7		,		_A							:       Optional[Any]=True		,		_A							:       Tuple=True		,		_A							:       Union[str, Any]=True		,		_A							:       Any=99		,		_A							:       Optional[int]=32		,		_A							:       Optional[Any]=5		,		_A							:       Optional[Any]=4		,		_A							:       Tuple=37		,		_A							:       Tuple="gelu"		,		_A							:       int=0.1		,		_A							:       Dict=0.1		,		_A							:       Any=512		,		_A							:       Union[str, Any]=16		,		_A							:       Optional[int]=2		,		_A							:       List[str]=0.0_2		,		_A							:       Optional[Any]=3		,		_A							:       Dict=4		,		_A							:       int=None		,		):


          '''simple docstring'''


          UpperCAmelCase__     :						int						=							parent
          UpperCAmelCase__     :						List[Any]						=							batch_size
          UpperCAmelCase__     :						Optional[int]						=							seq_length
          UpperCAmelCase__     :						Union[str, Any]						=							is_training
          UpperCAmelCase__     :						Any						=							use_token_type_ids
          UpperCAmelCase__     :						Any						=							use_labels
          UpperCAmelCase__     :						Tuple						=							vocab_size
          UpperCAmelCase__     :						Optional[int]						=							hidden_size
          UpperCAmelCase__     :						Union[str, Any]						=							num_hidden_layers
          UpperCAmelCase__     :						List[Any]						=							num_attention_heads
          UpperCAmelCase__     :						Tuple						=							intermediate_size
          UpperCAmelCase__     :						int						=							hidden_act
          UpperCAmelCase__     :						Union[str, Any]						=							hidden_dropout_prob
          UpperCAmelCase__     :						List[Any]						=							attention_probs_dropout_prob
          UpperCAmelCase__     :						Dict						=							max_position_embeddings
          UpperCAmelCase__     :						Any						=							type_vocab_size
          UpperCAmelCase__     :						Optional[Any]						=							type_sequence_label_size
          UpperCAmelCase__     :						Dict						=							initializer_range
          UpperCAmelCase__     :						Dict						=							num_labels
          UpperCAmelCase__     :						Optional[Any]						=							num_choices
          UpperCAmelCase__     :						int						=							scope
          UpperCAmelCase__     :						List[Any]						=							self.vocab_size - 1






      def lowercase_				(   self							:       Any					):


          '''simple docstring'''


          UpperCAmelCase__     :						Any						=							ids_tensor([self.batch_size, self.seq_length]		,		self.vocab_size					)

          UpperCAmelCase__     :						Optional[int]						=							None
          if self.use_token_type_ids:
              UpperCAmelCase__     :						Dict						=							ids_tensor([self.batch_size, self.seq_length]		,		self.type_vocab_size					)

          UpperCAmelCase__     :						List[str]						=							None
          UpperCAmelCase__     :						str						=							None
          UpperCAmelCase__     :						Any						=							None
          if self.use_labels:
              UpperCAmelCase__     :						Tuple						=							ids_tensor([self.batch_size]		,		self.type_sequence_label_size					)
              UpperCAmelCase__     :						Union[str, Any]						=							ids_tensor([self.batch_size, self.seq_length]		,		self.num_labels					)
              UpperCAmelCase__     :						str						=							ids_tensor([self.batch_size]		,		self.num_choices					)

          UpperCAmelCase__     :						Optional[int]						=							OpenAIGPTConfig(
              vocab_size=self.vocab_size		,		n_embd=self.hidden_size		,		n_layer=self.num_hidden_layers		,		n_head=self.num_attention_heads		,		n_positions=self.max_position_embeddings		,		pad_token_id=self.pad_token_id		,		)

          UpperCAmelCase__     :						str						=							ids_tensor([self.num_hidden_layers, self.num_attention_heads]		,		2					)

          return (
              config,
              input_ids,
              head_mask,
              token_type_ids,
              sequence_labels,
              token_labels,
              choice_labels,
          )






      def lowercase_				(   self							:       Tuple		,		_A							:       Tuple		,		_A							:       Optional[int]		,		_A							:       int		,		_A							:       Tuple		,		*_A							:       int					):


          '''simple docstring'''


          UpperCAmelCase__     :						Optional[int]						=							OpenAIGPTModel(config=_A					)
          model.to(_A					)
          model.eval()

          UpperCAmelCase__     :						Optional[int]						=							model(_A		,		token_type_ids=_A		,		head_mask=_A					)
          UpperCAmelCase__     :						Union[str, Any]						=							model(_A		,		token_type_ids=_A					)
          UpperCAmelCase__     :						Optional[int]						=							model(_A					)

          self.parent.assertEqual(result.last_hidden_state.shape		,		(self.batch_size, self.seq_length, self.hidden_size)					)






      def lowercase_				(   self							:       Union[str, Any]		,		_A							:       str		,		_A							:       Dict		,		_A							:       List[str]		,		_A							:       int		,		*_A							:       str					):


          '''simple docstring'''


          UpperCAmelCase__     :						Any						=							OpenAIGPTLMHeadModel(_A					)
          model.to(_A					)
          model.eval()

          UpperCAmelCase__     :						List[str]						=							model(_A		,		token_type_ids=_A		,		labels=_A					)
          self.parent.assertEqual(result.loss.shape		,		()					)
          self.parent.assertEqual(result.logits.shape		,		(self.batch_size, self.seq_length, self.vocab_size)					)






      def lowercase_				(   self							:       List[Any]		,		_A							:       Union[str, Any]		,		_A							:       Dict		,		_A							:       Dict		,		_A							:       List[Any]		,		*_A							:       str					):


          '''simple docstring'''


          UpperCAmelCase__     :						Dict						=							OpenAIGPTDoubleHeadsModel(_A					)
          model.to(_A					)
          model.eval()

          UpperCAmelCase__     :						Union[str, Any]						=							model(_A		,		token_type_ids=_A		,		labels=_A					)
          self.parent.assertEqual(result.loss.shape		,		()					)
          self.parent.assertEqual(result.logits.shape		,		(self.batch_size, self.seq_length, self.vocab_size)					)






      def lowercase_				(   self							:       str		,		_A							:       int		,		_A							:       Tuple		,		_A							:       Optional[int]		,		_A							:       Tuple		,		*_A							:       Optional[Any]					):


          '''simple docstring'''


          UpperCAmelCase__     :						List[Any]						=							self.num_labels
          UpperCAmelCase__     :						List[str]						=							OpenAIGPTForSequenceClassification(_A					)
          model.to(_A					)
          model.eval()

          UpperCAmelCase__     :						List[Any]						=							ids_tensor([self.batch_size]		,		self.type_sequence_label_size					)
          UpperCAmelCase__     :						str						=							model(_A		,		token_type_ids=_A		,		labels=_A					)
          self.parent.assertEqual(result.logits.shape		,		(self.batch_size, self.num_labels)					)






      def lowercase_				(   self							:       Optional[int]					):


          '''simple docstring'''


          UpperCAmelCase__     :						Any						=							self.prepare_config_and_inputs()
          (
              (
              UpperCAmelCase__
          )		,		(
              UpperCAmelCase__
          )		,		(
              UpperCAmelCase__
          )		,		(
              UpperCAmelCase__
          )		,		(
              UpperCAmelCase__
          )		,		(
              UpperCAmelCase__
          )		,		(
              UpperCAmelCase__
          )		,		
          )     :						List[str]						=							config_and_inputs
          UpperCAmelCase__     :						Union[str, Any]						=							{
              '''input_ids''': input_ids,
              '''token_type_ids''': token_type_ids,
              '''head_mask''': head_mask,
          }

          return config, inputs_dict




@require_torch
class      lowerCamelCase_  (      __a	,			__a	,			__a	,			unittest.TestCase ):
      lowerCAmelCase__							=	(
          (OpenAIGPTModel, OpenAIGPTLMHeadModel, OpenAIGPTDoubleHeadsModel, OpenAIGPTForSequenceClassification)
          if is_torch_available()
          else ()
      )
      lowerCAmelCase__							=	(
          (OpenAIGPTLMHeadModel,) if is_torch_available() else ()
      )  # TODO (PVP): Add Double HeadsModel when generate() function is changed accordingly
      lowerCAmelCase__							=	(
          {
              'feature-extraction': OpenAIGPTModel,
              'text-classification': OpenAIGPTForSequenceClassification,
              'text-generation': OpenAIGPTLMHeadModel,
              'zero-shot': OpenAIGPTForSequenceClassification,
          }
          if is_torch_available()
          else {}
      )






      def lowercase_				(   self							:       str		,		_A							:       Tuple		,		_A							:       Union[str, Any]		,		_A							:       Tuple		,		_A							:       Tuple		,		_A							:       Dict					):


          '''simple docstring'''


          if pipeline_test_casse_name == "ZeroShotClassificationPipelineTests":
              # Get `tokenizer does not have a padding token` error for both fast/slow tokenizers.
              # `OpenAIGPTConfig` was never used in pipeline tests, either because of a missing checkpoint or because a
              # tiny config could not be created.
              return True

          return False






      def lowercase_				(   self							:       Union[str, Any]		,		_A							:       List[Any]		,		_A							:       int		,		_A							:       Union[str, Any]=False					):


          '''simple docstring'''


          UpperCAmelCase__     :						Union[str, Any]						=							super()._prepare_for_class(_A		,		_A		,		return_labels=_A					)

          if return_labels:
              if model_class.__name__ == "OpenAIGPTDoubleHeadsModel":
                  UpperCAmelCase__     :						Union[str, Any]						=							torch.zeros(
                      (self.model_tester.batch_size, self.model_tester.num_choices, self.model_tester.seq_length)		,		dtype=torch.long		,		device=_A		,		)
                  UpperCAmelCase__     :						List[str]						=							inputs_dict['''labels''']
                  UpperCAmelCase__     :						Optional[Any]						=							inputs_dict['''labels''']
                  UpperCAmelCase__     :						Union[str, Any]						=							torch.zeros(
                      (self.model_tester.batch_size, self.model_tester.num_choices)		,		dtype=torch.long		,		device=_A		,		)
                  UpperCAmelCase__     :						List[Any]						=							torch.zeros(
                      self.model_tester.batch_size		,		dtype=torch.long		,		device=_A					)
          return inputs_dict






      def lowercase_				(   self							:       Union[str, Any]					):


          '''simple docstring'''


          UpperCAmelCase__     :						Optional[int]						=							OpenAIGPTModelTester(self					)
          UpperCAmelCase__     :						int						=							ConfigTester(self		,		config_class=_A		,		n_embd=37					)






      def lowercase_				(   self							:       Union[str, Any]					):


          '''simple docstring'''


          self.config_tester.run_common_tests()






      def lowercase_				(   self							:       str					):


          '''simple docstring'''


          UpperCAmelCase__     :						int						=							self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_openai_gpt_model(*_A					)






      def lowercase_				(   self							:       int					):


          '''simple docstring'''


          UpperCAmelCase__     :						Union[str, Any]						=							self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_lm_head_model(*_A					)






      def lowercase_				(   self							:       Union[str, Any]					):


          '''simple docstring'''


          UpperCAmelCase__     :						Dict						=							self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_double_lm_head_model(*_A					)






      def lowercase_				(   self							:       Optional[int]					):


          '''simple docstring'''


          UpperCAmelCase__     :						Optional[Any]						=							self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_openai_gpt_for_sequence_classification(*_A					)






      @slow
      def lowercase_				(   self							:       Dict					):


          '''simple docstring'''


          for model_name in OPENAI_GPT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
              UpperCAmelCase__     :						Union[str, Any]						=							OpenAIGPTModel.from_pretrained(_A					)
              self.assertIsNotNone(_A					)




@require_torch
class      lowerCamelCase_  (      unittest.TestCase ):






      @slow
      def lowercase_				(   self							:       Union[str, Any]					):


          '''simple docstring'''


          UpperCAmelCase__     :						int						=							OpenAIGPTLMHeadModel.from_pretrained('''openai-gpt'''					)
          model.to(_A					)
          UpperCAmelCase__     :						List[str]						=							torch.tensor([[481, 4_735, 544]]		,		dtype=torch.long		,		device=_A					)  # the president is
          UpperCAmelCase__     :						str						=							[
              481,
              4_735,
              544,
              246,
              963,
              870,
              762,
              239,
              244,
              40_477,
              244,
              249,
              719,
              881,
              487,
              544,
              240,
              244,
              603,
              481,
          ]  # the president is a very good man. " \n " i\'m sure he is, " said the

          UpperCAmelCase__     :						Tuple						=							model.generate(_A		,		do_sample=_A					)
          self.assertListEqual(output_ids[0].tolist()		,		_A					)




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


SCREAMING_SNAKE_CASE						:Union[str, Any]			=  logging.get_logger(__name__)


class      __lowerCAmelCase  :






  """simple docstring"""





  _SCREAMING_SNAKE_CASE		     =							42
  _SCREAMING_SNAKE_CASE		     =							None



  @staticmethod
  def        lowerCAmelCase__	(  ) ->			Any:




     """simple docstring"""
     raise NotImplementedError



  def        lowerCAmelCase__	(  self							:							Dict     ,					_lowerCAmelCase							:							List[str]     ,					_lowerCAmelCase							:							int     ,					_lowerCAmelCase							:							str     ,					**_lowerCAmelCase							:							int	) ->			str:




     """simple docstring"""
     raise NotImplementedError



  def        lowerCAmelCase__	(  self							:							int     ,					_lowerCAmelCase							:							List[str]	) ->			Optional[int]:




     """simple docstring"""
     raise NotImplementedError



  def        lowerCAmelCase__	(  self							:							Optional[int]	) ->			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							:							Dict	) ->			Tuple:




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






class      __lowerCAmelCase  (			a       ):






  """simple docstring"""





  _SCREAMING_SNAKE_CASE		     =							'optuna'



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




     """simple docstring"""
     return is_optuna_available()



  def        lowerCAmelCase__	(  self							:							str     ,					_lowerCAmelCase							:							int     ,					_lowerCAmelCase							:							int     ,					_lowerCAmelCase							:							str     ,					**_lowerCAmelCase							:							Optional[int]	) ->			Optional[int]:




     """simple docstring"""
     return run_hp_search_optuna(_lowerCAmelCase     ,					_lowerCAmelCase     ,					_lowerCAmelCase     ,					**_lowerCAmelCase	)



  def        lowerCAmelCase__	(  self							:							Tuple     ,					_lowerCAmelCase							:							int	) ->			str:




     """simple docstring"""
     return default_hp_space_optuna(_lowerCAmelCase	)






class      __lowerCAmelCase  (			a       ):






  """simple docstring"""





  _SCREAMING_SNAKE_CASE		     =							'ray'
  _SCREAMING_SNAKE_CASE		     =							'\'ray[tune]\''



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




     """simple docstring"""
     return is_ray_available()



  def        lowerCAmelCase__	(  self							:							List[Any]     ,					_lowerCAmelCase							:							List[Any]     ,					_lowerCAmelCase							:							int     ,					_lowerCAmelCase							:							str     ,					**_lowerCAmelCase							:							List[Any]	) ->			List[str]:




     """simple docstring"""
     return run_hp_search_ray(_lowerCAmelCase     ,					_lowerCAmelCase     ,					_lowerCAmelCase     ,					**_lowerCAmelCase	)



  def        lowerCAmelCase__	(  self							:							str     ,					_lowerCAmelCase							:							List[Any]	) ->			Any:




     """simple docstring"""
     return default_hp_space_ray(_lowerCAmelCase	)






class      __lowerCAmelCase  (			a       ):






  """simple docstring"""





  _SCREAMING_SNAKE_CASE		     =							'sigopt'



  @staticmethod
  def        lowerCAmelCase__	(  ) ->			List[str]:




     """simple docstring"""
     return is_sigopt_available()



  def        lowerCAmelCase__	(  self							:							str     ,					_lowerCAmelCase							:							int     ,					_lowerCAmelCase							:							int     ,					_lowerCAmelCase							:							str     ,					**_lowerCAmelCase							:							Tuple	) ->			str:




     """simple docstring"""
     return run_hp_search_sigopt(_lowerCAmelCase     ,					_lowerCAmelCase     ,					_lowerCAmelCase     ,					**_lowerCAmelCase	)



  def        lowerCAmelCase__	(  self							:							Optional[Any]     ,					_lowerCAmelCase							:							Tuple	) ->			Union[str, Any]:




     """simple docstring"""
     return default_hp_space_sigopt(_lowerCAmelCase	)






class      __lowerCAmelCase  (			a       ):






  """simple docstring"""





  _SCREAMING_SNAKE_CASE		     =							'wandb'



  @staticmethod
  def        lowerCAmelCase__	(  ) ->			List[Any]:




     """simple docstring"""
     return is_wandb_available()



  def        lowerCAmelCase__	(  self							:							List[Any]     ,					_lowerCAmelCase							:							Tuple     ,					_lowerCAmelCase							:							int     ,					_lowerCAmelCase							:							str     ,					**_lowerCAmelCase							:							Any	) ->			Dict:




     """simple docstring"""
     return run_hp_search_wandb(_lowerCAmelCase     ,					_lowerCAmelCase     ,					_lowerCAmelCase     ,					**_lowerCAmelCase	)



  def        lowerCAmelCase__	(  self							:							int     ,					_lowerCAmelCase							:							List[str]	) ->			Any:




     """simple docstring"""
     return default_hp_space_wandb(_lowerCAmelCase	)






SCREAMING_SNAKE_CASE						:Union[str, Any]			=  {
    HPSearchBackend(backend.name): backend for backend in [OptunaBackend, RayTuneBackend, SigOptBackend, WandbBackend]
}



def       _lowerCAmelCase    (				)->str:
   '''simple docstring'''






   snake_case_            =       [backend for backend in ALL_HYPERPARAMETER_SEARCH_BACKENDS.values() if backend.is_available()]
   if len(lowerCAmelCase_				) > 0:
      snake_case_            =       available_backends[0].name
      if len(lowerCAmelCase_				) > 1:
         logger.info(
             F'''{len(lowerCAmelCase_				)} 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()				)				)




