


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  :






  """simple docstring"""





  def __init__(  self							:							Any     ,					_lowerCAmelCase							:							int     ,					_lowerCAmelCase							:							Any=1_3     ,					_lowerCAmelCase							:							Dict=7     ,					_lowerCAmelCase							:							Optional[int]=True     ,					_lowerCAmelCase							:							Any=True     ,					_lowerCAmelCase							:							Union[str, Any]=True     ,					_lowerCAmelCase							:							str=9_9     ,					_lowerCAmelCase							:							List[str]=3_2     ,					_lowerCAmelCase							:							Optional[int]=5     ,					_lowerCAmelCase							:							Dict=4     ,					_lowerCAmelCase							:							str=3_7     ,					_lowerCAmelCase							:							Union[str, Any]="gelu"     ,					_lowerCAmelCase							:							Any=0.1     ,					_lowerCAmelCase							:							str=0.1     ,					_lowerCAmelCase							:							Optional[int]=5_1_2     ,					_lowerCAmelCase							:							Optional[Any]=1_6     ,					_lowerCAmelCase							:							int=2     ,					_lowerCAmelCase							:							Tuple=0.02     ,					_lowerCAmelCase							:							str=3     ,					_lowerCAmelCase							:							Union[str, Any]=4     ,					_lowerCAmelCase							:							List[str]=None     ,					) ->			Union[str, Any]:




     """simple docstring"""
     snake_case_            =       parent
     snake_case_            =       batch_size
     snake_case_            =       seq_length
     snake_case_            =       is_training
     snake_case_            =       use_token_type_ids
     snake_case_            =       use_labels
     snake_case_            =       vocab_size
     snake_case_            =       hidden_size
     snake_case_            =       num_hidden_layers
     snake_case_            =       num_attention_heads
     snake_case_            =       intermediate_size
     snake_case_            =       hidden_act
     snake_case_            =       hidden_dropout_prob
     snake_case_            =       attention_probs_dropout_prob
     snake_case_            =       max_position_embeddings
     snake_case_            =       type_vocab_size
     snake_case_            =       type_sequence_label_size
     snake_case_            =       initializer_range
     snake_case_            =       num_labels
     snake_case_            =       num_choices
     snake_case_            =       scope
     snake_case_            =       self.vocab_size - 1



  def        lowerCAmelCase__	(  self							:							Dict	) ->			Tuple:




     """simple docstring"""
     snake_case_            =       ids_tensor([self.batch_size, self.seq_length]     ,					self.vocab_size	)

     snake_case_            =       None
     if self.use_token_type_ids:
        snake_case_            =       ids_tensor([self.batch_size, self.seq_length]     ,					self.type_vocab_size	)

     snake_case_            =       None
     snake_case_            =       None
     snake_case_            =       None
     if self.use_labels:
        snake_case_            =       ids_tensor([self.batch_size]     ,					self.type_sequence_label_size	)
        snake_case_            =       ids_tensor([self.batch_size, self.seq_length]     ,					self.num_labels	)
        snake_case_            =       ids_tensor([self.batch_size]     ,					self.num_choices	)

     snake_case_            =       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     ,					)

     snake_case_            =       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        lowerCAmelCase__	(  self							:							str     ,					_lowerCAmelCase							:							List[str]     ,					_lowerCAmelCase							:							Any     ,					_lowerCAmelCase							:							Tuple     ,					_lowerCAmelCase							:							int     ,					*_lowerCAmelCase							:							str	) ->			List[str]:




     """simple docstring"""
     snake_case_            =       OpenAIGPTModel(config=_lowerCAmelCase	)
     model.to(_lowerCAmelCase	)
     model.eval()

     snake_case_            =       model(_lowerCAmelCase     ,					token_type_ids=_lowerCAmelCase     ,					head_mask=_lowerCAmelCase	)
     snake_case_            =       model(_lowerCAmelCase     ,					token_type_ids=_lowerCAmelCase	)
     snake_case_            =       model(_lowerCAmelCase	)

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



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




     """simple docstring"""
     snake_case_            =       OpenAIGPTLMHeadModel(_lowerCAmelCase	)
     model.to(_lowerCAmelCase	)
     model.eval()

     snake_case_            =       model(_lowerCAmelCase     ,					token_type_ids=_lowerCAmelCase     ,					labels=_lowerCAmelCase	)
     self.parent.assertEqual(result.loss.shape     ,					()	)
     self.parent.assertEqual(result.logits.shape     ,					(self.batch_size, self.seq_length, self.vocab_size)	)



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




     """simple docstring"""
     snake_case_            =       OpenAIGPTDoubleHeadsModel(_lowerCAmelCase	)
     model.to(_lowerCAmelCase	)
     model.eval()

     snake_case_            =       model(_lowerCAmelCase     ,					token_type_ids=_lowerCAmelCase     ,					labels=_lowerCAmelCase	)
     self.parent.assertEqual(result.loss.shape     ,					()	)
     self.parent.assertEqual(result.logits.shape     ,					(self.batch_size, self.seq_length, self.vocab_size)	)



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




     """simple docstring"""
     snake_case_            =       self.num_labels
     snake_case_            =       OpenAIGPTForSequenceClassification(_lowerCAmelCase	)
     model.to(_lowerCAmelCase	)
     model.eval()

     snake_case_            =       ids_tensor([self.batch_size]     ,					self.type_sequence_label_size	)
     snake_case_            =       model(_lowerCAmelCase     ,					token_type_ids=_lowerCAmelCase     ,					labels=_lowerCAmelCase	)
     self.parent.assertEqual(result.logits.shape     ,					(self.batch_size, self.num_labels)	)



  def        lowerCAmelCase__	(  self							:							Optional[Any]	) ->			Tuple:




     """simple docstring"""
     snake_case_            =       self.prepare_config_and_inputs()
     (
         (
         snake_case_
     )     ,     (
         snake_case_
     )     ,     (
         snake_case_
     )     ,     (
         snake_case_
     )     ,     (
         snake_case_
     )     ,     (
         snake_case_
     )     ,     (
         snake_case_
     )     ,     
     )            =       config_and_inputs
     snake_case_            =       {
         "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       ):






  """simple docstring"""





  _SCREAMING_SNAKE_CASE		     =							(
      (OpenAIGPTModel, OpenAIGPTLMHeadModel, OpenAIGPTDoubleHeadsModel, OpenAIGPTForSequenceClassification)
      if is_torch_available()
      else ()
  )
  _SCREAMING_SNAKE_CASE		     =							(
      (OpenAIGPTLMHeadModel,) if is_torch_available() else ()
  )  # TODO (PVP): Add Double HeadsModel when generate() function is changed accordingly
  _SCREAMING_SNAKE_CASE		     =							(
      {
          'feature-extraction': OpenAIGPTModel,
          'text-classification': OpenAIGPTForSequenceClassification,
          'text-generation': OpenAIGPTLMHeadModel,
          'zero-shot': OpenAIGPTForSequenceClassification,
      }
      if is_torch_available()
      else {}
  )



  def        lowerCAmelCase__	(  self							:							Optional[int]     ,					_lowerCAmelCase							:							Optional[int]     ,					_lowerCAmelCase							:							Optional[Any]     ,					_lowerCAmelCase							:							List[str]     ,					_lowerCAmelCase							:							Dict     ,					_lowerCAmelCase							:							List[Any]	) ->			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        lowerCAmelCase__	(  self							:							Optional[Any]     ,					_lowerCAmelCase							:							Optional[int]     ,					_lowerCAmelCase							:							Optional[Any]     ,					_lowerCAmelCase							:							List[Any]=False	) ->			int:




     """simple docstring"""
     snake_case_            =       super()._prepare_for_class(_lowerCAmelCase     ,					_lowerCAmelCase     ,					return_labels=_lowerCAmelCase	)

     if return_labels:
        if model_class.__name__ == "OpenAIGPTDoubleHeadsModel":
           snake_case_            =       torch.zeros(
               (self.model_tester.batch_size, self.model_tester.num_choices, self.model_tester.seq_length)     ,					dtype=torch.long     ,					device=_lowerCAmelCase     ,					)
           snake_case_            =       inputs_dict["labels"]
           snake_case_            =       inputs_dict["labels"]
           snake_case_            =       torch.zeros(
               (self.model_tester.batch_size, self.model_tester.num_choices)     ,					dtype=torch.long     ,					device=_lowerCAmelCase     ,					)
           snake_case_            =       torch.zeros(
               self.model_tester.batch_size     ,					dtype=torch.long     ,					device=_lowerCAmelCase	)
     return inputs_dict



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




     """simple docstring"""
     snake_case_            =       OpenAIGPTModelTester(self	)
     snake_case_            =       ConfigTester(self     ,					config_class=_lowerCAmelCase     ,					n_embd=3_7	)



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




     """simple docstring"""
     self.config_tester.run_common_tests()



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




     """simple docstring"""
     snake_case_            =       self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_openai_gpt_model(*_lowerCAmelCase	)



  def        lowerCAmelCase__	(  self							:							Dict	) ->			Optional[Any]:




     """simple docstring"""
     snake_case_            =       self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_lm_head_model(*_lowerCAmelCase	)



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




     """simple docstring"""
     snake_case_            =       self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_double_lm_head_model(*_lowerCAmelCase	)



  def        lowerCAmelCase__	(  self							:							Any	) ->			int:




     """simple docstring"""
     snake_case_            =       self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_openai_gpt_for_sequence_classification(*_lowerCAmelCase	)



  @slow
  def        lowerCAmelCase__	(  self							:							int	) ->			Union[str, Any]:




     """simple docstring"""
     for model_name in OPENAI_GPT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
        snake_case_            =       OpenAIGPTModel.from_pretrained(_lowerCAmelCase	)
        self.assertIsNotNone(_lowerCAmelCase	)






@require_torch
class      __lowerCAmelCase  (			unittest.TestCase       ):






  """simple docstring"""





  @slow
  def        lowerCAmelCase__	(  self							:							str	) ->			Union[str, Any]:




     """simple docstring"""
     snake_case_            =       OpenAIGPTLMHeadModel.from_pretrained("openai-gpt"	)
     model.to(_lowerCAmelCase	)
     snake_case_            =       torch.tensor([[4_8_1, 4_7_3_5, 5_4_4]]     ,					dtype=torch.long     ,					device=_lowerCAmelCase	)  # the president is
     snake_case_            =       [
         4_8_1,
         4_7_3_5,
         5_4_4,
         2_4_6,
         9_6_3,
         8_7_0,
         7_6_2,
         2_3_9,
         2_4_4,
         4_0_4_7_7,
         2_4_4,
         2_4_9,
         7_1_9,
         8_8_1,
         4_8_7,
         5_4_4,
         2_4_0,
         2_4_4,
         6_0_3,
         4_8_1,
     ]  # the president is a very good man. " \n " i\'m sure he is, " said the

     snake_case_            =       model.generate(_lowerCAmelCase     ,					do_sample=_lowerCAmelCase	)
     self.assertListEqual(output_ids[0].tolist()     ,					_lowerCAmelCase	)








import importlib.metadata
from typing import Union

from packaging.version import Version, parse

from .constants import STR_OPERATION_TO_FUNC


SCREAMING_SNAKE_CASE						:Union[str, Any]			=  parse(importlib.metadata.version('''torch'''))



def       _lowerCAmelCase    (				lowerCAmelCase_				:Union[str, Version]		,    lowerCAmelCase_				:str		,    lowerCAmelCase_				:str				)->Any:
   '''simple docstring'''






   if operation not in STR_OPERATION_TO_FUNC.keys():
      raise ValueError(F'''`operation` must be one of {list(STR_OPERATION_TO_FUNC.keys()				)}, received {operation}'''				)
   snake_case_            =       STR_OPERATION_TO_FUNC[operation]
   if isinstance(lowerCAmelCase_		,    lowerCAmelCase_				):
      snake_case_            =       parse(importlib.metadata.version(lowerCAmelCase_				)				)
   return operation(lowerCAmelCase_		,    parse(lowerCAmelCase_				)				)



def       _lowerCAmelCase    (				lowerCAmelCase_				:str		,    lowerCAmelCase_				:str				)->Any:
   '''simple docstring'''






   return compare_versions(lowerCAmelCase_		,    lowerCAmelCase_		,    lowerCAmelCase_				)




