





"""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 							snake_case							:

      '''simple docstring'''



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


        '''simple docstring'''




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

      def       A_					(							self       :      List[str] ):


        '''simple docstring'''




        __UpperCAmelCase    :						Union[str, Any]       =						ids_tensor([self.batch_size, self.seq_length]						,				self.vocab_size )

        __UpperCAmelCase    :						Optional[int]       =						None
        if self.use_token_type_ids:
          __UpperCAmelCase    :						Union[str, Any]       =						ids_tensor([self.batch_size, self.seq_length]						,				self.type_vocab_size )

        __UpperCAmelCase    :						Optional[int]       =						None
        __UpperCAmelCase    :						Tuple       =						None
        __UpperCAmelCase    :						Optional[Any]       =						None
        if self.use_labels:
          __UpperCAmelCase    :						Optional[Any]       =						ids_tensor([self.batch_size]						,				self.type_sequence_label_size )
          __UpperCAmelCase    :						str       =						ids_tensor([self.batch_size, self.seq_length]						,				self.num_labels )
          __UpperCAmelCase    :						int       =						ids_tensor([self.batch_size]						,				self.num_choices )

        __UpperCAmelCase    :						List[Any]       =						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    :						Union[str, Any]       =						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       A_					(							self       :      Tuple						,				__lowercase       :      Any						,				__lowercase       :      Any						,				__lowercase       :      List[str]						,				__lowercase       :      List[Any]						,				*__lowercase       :      Dict ):


        '''simple docstring'''




        __UpperCAmelCase    :						Optional[Any]       =						OpenAIGPTModel(config=__lowercase )
        model.to(__lowercase )
        model.eval()

        __UpperCAmelCase    :						str       =						model(__lowercase						,				token_type_ids=__lowercase						,				head_mask=__lowercase )
        __UpperCAmelCase    :						Dict       =						model(__lowercase						,				token_type_ids=__lowercase )
        __UpperCAmelCase    :						List[Any]       =						model(__lowercase )

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

      def       A_					(							self       :      Tuple						,				__lowercase       :      Optional[Any]						,				__lowercase       :      Dict						,				__lowercase       :      List[str]						,				__lowercase       :      List[Any]						,				*__lowercase       :      Optional[Any] ):


        '''simple docstring'''




        __UpperCAmelCase    :						List[str]       =						OpenAIGPTLMHeadModel(__lowercase )
        model.to(__lowercase )
        model.eval()

        __UpperCAmelCase    :						Optional[int]       =						model(__lowercase						,				token_type_ids=__lowercase						,				labels=__lowercase )
        self.parent.assertEqual(result.loss.shape						,				() )
        self.parent.assertEqual(result.logits.shape						,				(self.batch_size, self.seq_length, self.vocab_size) )

      def       A_					(							self       :      Any						,				__lowercase       :      Optional[Any]						,				__lowercase       :      Optional[int]						,				__lowercase       :      List[str]						,				__lowercase       :      Union[str, Any]						,				*__lowercase       :      List[Any] ):


        '''simple docstring'''




        __UpperCAmelCase    :						Optional[int]       =						OpenAIGPTDoubleHeadsModel(__lowercase )
        model.to(__lowercase )
        model.eval()

        __UpperCAmelCase    :						Tuple       =						model(__lowercase						,				token_type_ids=__lowercase						,				labels=__lowercase )
        self.parent.assertEqual(result.loss.shape						,				() )
        self.parent.assertEqual(result.logits.shape						,				(self.batch_size, self.seq_length, self.vocab_size) )

      def       A_					(							self       :      List[str]						,				__lowercase       :      List[str]						,				__lowercase       :      Union[str, Any]						,				__lowercase       :      str						,				__lowercase       :      Any						,				*__lowercase       :      str ):


        '''simple docstring'''




        __UpperCAmelCase    :						Any       =						self.num_labels
        __UpperCAmelCase    :						Union[str, Any]       =						OpenAIGPTForSequenceClassification(__lowercase )
        model.to(__lowercase )
        model.eval()

        __UpperCAmelCase    :						Union[str, Any]       =						ids_tensor([self.batch_size]						,				self.type_sequence_label_size )
        __UpperCAmelCase    :						Optional[int]       =						model(__lowercase						,				token_type_ids=__lowercase						,				labels=__lowercase )
        self.parent.assertEqual(result.logits.shape						,				(self.batch_size, self.num_labels) )







      def       A_					(							self       :      Optional[Any] ):


        '''simple docstring'''




        __UpperCAmelCase    :						List[str]       =						self.prepare_config_and_inputs()
        (
            __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 							snake_case							(			__UpperCAmelCase      ,  __UpperCAmelCase      ,  __UpperCAmelCase      ,  unittest.TestCase ):

      '''simple docstring'''



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

      def       A_					(							self       :      Union[str, Any]						,				__lowercase       :      str						,				__lowercase       :      List[Any]						,				__lowercase       :      Optional[int]						,				__lowercase       :      Union[str, Any]						,				__lowercase       :      str ):


        '''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       A_					(							self       :      Optional[int]						,				__lowercase       :      int						,				__lowercase       :      List[str]						,				__lowercase       :      int=False ):


        '''simple docstring'''




        __UpperCAmelCase    :						Optional[int]       =						super()._prepare_for_class(__lowercase						,				__lowercase						,				return_labels=__lowercase )

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

      def       A_					(							self       :      List[Any] ):


        '''simple docstring'''




        __UpperCAmelCase    :						Optional[Any]       =						OpenAIGPTModelTester(self )
        __UpperCAmelCase    :						Dict       =						ConfigTester(self						,				config_class=__lowercase						,				n_embd=37 )

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


        '''simple docstring'''




        self.config_tester.run_common_tests()

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


        '''simple docstring'''




        __UpperCAmelCase    :						Optional[int]       =						self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_openai_gpt_model(*__lowercase )

      def       A_					(							self       :      int ):


        '''simple docstring'''




        __UpperCAmelCase    :						List[Any]       =						self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_lm_head_model(*__lowercase )

      def       A_					(							self       :      Optional[Any] ):


        '''simple docstring'''




        __UpperCAmelCase    :						str       =						self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_double_lm_head_model(*__lowercase )

      def       A_					(							self       :      List[Any] ):


        '''simple docstring'''




        __UpperCAmelCase    :						Optional[Any]       =						self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_openai_gpt_for_sequence_classification(*__lowercase )







      @slow
      def       A_					(							self       :      int ):


        '''simple docstring'''




        for model_name in OPENAI_GPT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
          __UpperCAmelCase    :						int       =						OpenAIGPTModel.from_pretrained(__lowercase )
          self.assertIsNotNone(__lowercase )




@require_torch
class 							snake_case							(			unittest.TestCase ):

      '''simple docstring'''



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


        '''simple docstring'''




        __UpperCAmelCase    :						Dict       =						OpenAIGPTLMHeadModel.from_pretrained('''openai-gpt''' )
        model.to(__lowercase )
        __UpperCAmelCase    :						int       =						torch.tensor([[481, 4_735, 544]]						,				dtype=torch.long						,				device=__lowercase )  # the president is
        __UpperCAmelCase    :						int       =						[
            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    :						str       =						model.generate(__lowercase						,				do_sample=__lowercase )
        self.assertListEqual(output_ids[0].tolist()						,				__lowercase )






"""simple docstring"""





def       lowerCamelCase_       (   UpperCAmelCase_  ,			UpperCAmelCase_  )						->bool:





  """simple docstring"""


  return numa ^ numa < 0


if __name__ == "__main__":
  import doctest

  doctest.testmod()