





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 	UpperCamelCase__				:



     '''simple docstring'''

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




           '''simple docstring'''


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






     def        SCREAMING_SNAKE_CASE__						(							self		:					Dict )  ->	str:




           '''simple docstring'''


           SCREAMING_SNAKE_CASE	=		ids_tensor([self.batch_size, self.seq_length]		,self.vocab_size )

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

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

           SCREAMING_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		,)

           SCREAMING_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        SCREAMING_SNAKE_CASE__						(							self		:					Tuple		,lowerCamelCase__		:					List[Any]		,lowerCamelCase__		:					Optional[int]		,lowerCamelCase__		:					Any		,lowerCamelCase__		:					List[str]		,*lowerCamelCase__		:					int )  ->	List[str]:




           '''simple docstring'''


           SCREAMING_SNAKE_CASE	=		OpenAIGPTModel(config=lowerCamelCase__ )
           model.to(lowerCamelCase__ )
           model.eval()

           SCREAMING_SNAKE_CASE	=		model(lowerCamelCase__		,token_type_ids=lowerCamelCase__		,head_mask=lowerCamelCase__ )
           SCREAMING_SNAKE_CASE	=		model(lowerCamelCase__		,token_type_ids=lowerCamelCase__ )
           SCREAMING_SNAKE_CASE	=		model(lowerCamelCase__ )

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






     def        SCREAMING_SNAKE_CASE__						(							self		:					int		,lowerCamelCase__		:					Dict		,lowerCamelCase__		:					Optional[int]		,lowerCamelCase__		:					Any		,lowerCamelCase__		:					Dict		,*lowerCamelCase__		:					Optional[Any] )  ->	Tuple:




           '''simple docstring'''


           SCREAMING_SNAKE_CASE	=		OpenAIGPTLMHeadModel(lowerCamelCase__ )
           model.to(lowerCamelCase__ )
           model.eval()

           SCREAMING_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        SCREAMING_SNAKE_CASE__						(							self		:					List[str]		,lowerCamelCase__		:					Any		,lowerCamelCase__		:					int		,lowerCamelCase__		:					Dict		,lowerCamelCase__		:					Dict		,*lowerCamelCase__		:					Tuple )  ->	Tuple:




           '''simple docstring'''


           SCREAMING_SNAKE_CASE	=		OpenAIGPTDoubleHeadsModel(lowerCamelCase__ )
           model.to(lowerCamelCase__ )
           model.eval()

           SCREAMING_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        SCREAMING_SNAKE_CASE__						(							self		:					Any		,lowerCamelCase__		:					List[Any]		,lowerCamelCase__		:					List[Any]		,lowerCamelCase__		:					Tuple		,lowerCamelCase__		:					str		,*lowerCamelCase__		:					List[str] )  ->	int:




           '''simple docstring'''


           SCREAMING_SNAKE_CASE	=		self.num_labels
           SCREAMING_SNAKE_CASE	=		OpenAIGPTForSequenceClassification(lowerCamelCase__ )
           model.to(lowerCamelCase__ )
           model.eval()

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






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




           '''simple docstring'''


           SCREAMING_SNAKE_CASE	=		self.prepare_config_and_inputs()
           (
               (
               SCREAMING_SNAKE_CASE
           ),      (
               SCREAMING_SNAKE_CASE
           ),      (
               SCREAMING_SNAKE_CASE
           ),      (
               SCREAMING_SNAKE_CASE
           ),      (
               SCREAMING_SNAKE_CASE
           ),      (
               SCREAMING_SNAKE_CASE
           ),      (
               SCREAMING_SNAKE_CASE
           ),      
           )	=		config_and_inputs
           SCREAMING_SNAKE_CASE	=		{
               """input_ids""": input_ids,
               """token_type_ids""": token_type_ids,
               """head_mask""": head_mask,
           }

           return config, inputs_dict

@require_torch
class 	UpperCamelCase__				(   lowerCAmelCase_							,   lowerCAmelCase_							,   lowerCAmelCase_							,   unittest.TestCase     ):



     '''simple docstring'''

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






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




           '''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        SCREAMING_SNAKE_CASE__						(							self		:					Optional[Any]		,lowerCamelCase__		:					List[Any]		,lowerCamelCase__		:					int		,lowerCamelCase__		:					Optional[Any]=False )  ->	int:




           '''simple docstring'''


           SCREAMING_SNAKE_CASE	=		super()._prepare_for_class(lowerCamelCase__		,lowerCamelCase__		,return_labels=lowerCamelCase__ )

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






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




           '''simple docstring'''


           SCREAMING_SNAKE_CASE	=		OpenAIGPTModelTester(self )
           SCREAMING_SNAKE_CASE	=		ConfigTester(self		,config_class=lowerCamelCase__		,n_embd=37 )






     def        SCREAMING_SNAKE_CASE__						(							self		:					List[str] )  ->	Tuple:




           '''simple docstring'''


           self.config_tester.run_common_tests()






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




           '''simple docstring'''


           SCREAMING_SNAKE_CASE	=		self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_openai_gpt_model(*lowerCamelCase__ )






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




           '''simple docstring'''


           SCREAMING_SNAKE_CASE	=		self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_lm_head_model(*lowerCamelCase__ )






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




           '''simple docstring'''


           SCREAMING_SNAKE_CASE	=		self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_double_lm_head_model(*lowerCamelCase__ )






     def        SCREAMING_SNAKE_CASE__						(							self		:					List[str] )  ->	Tuple:




           '''simple docstring'''


           SCREAMING_SNAKE_CASE	=		self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_openai_gpt_for_sequence_classification(*lowerCamelCase__ )






     @slow
     def        SCREAMING_SNAKE_CASE__						(							self		:					str )  ->	str:




           '''simple docstring'''


           for model_name in OPENAI_GPT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
                 SCREAMING_SNAKE_CASE	=		OpenAIGPTModel.from_pretrained(lowerCamelCase__ )
                 self.assertIsNotNone(lowerCamelCase__ )

@require_torch
class 	UpperCamelCase__				(   unittest.TestCase     ):



     '''simple docstring'''

     @slow
     def        SCREAMING_SNAKE_CASE__						(							self		:					List[str] )  ->	List[Any]:




           '''simple docstring'''


           SCREAMING_SNAKE_CASE	=		OpenAIGPTLMHeadModel.from_pretrained("""openai-gpt""" )
           model.to(lowerCamelCase__ )
           SCREAMING_SNAKE_CASE	=		torch.tensor([[481, 4735, 544]]		,dtype=torch.long		,device=lowerCamelCase__ )  # the president is
           SCREAMING_SNAKE_CASE	=		[
               481,
               4735,
               544,
               246,
               963,
               870,
               762,
               239,
               244,
               40477,
               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

           SCREAMING_SNAKE_CASE	=		model.generate(lowerCamelCase__		,do_sample=lowerCamelCase__ )
           self.assertListEqual(output_ids[0].tolist()		,lowerCamelCase__ )









from ...configuration_utils import PretrainedConfig
from ...utils import logging


SCREAMING_SNAKE_CASE_		     = logging.get_logger(__name__)

SCREAMING_SNAKE_CASE_		     = {
    """google/pegasus-large""": """https://huggingface.co/google/pegasus-large/resolve/main/config.json""",
    # See all PEGASUS models at https://huggingface.co/models?filter=pegasus
}

class 	UpperCamelCase__				(   lowerCAmelCase_     ):



     '''simple docstring'''

     __snake_case					:			str				=    "pegasus"
     __snake_case					:			List[Any]				=    ["past_key_values"]
     __snake_case					:			Any				=    {"num_attention_heads": "encoder_attention_heads", "hidden_size": "d_model"}






     def __init__(							self		:					Dict		,lowerCamelCase__		:					List[str]=50265		,lowerCamelCase__		:					int=1024		,lowerCamelCase__		:					Optional[Any]=12		,lowerCamelCase__		:					Tuple=4096		,lowerCamelCase__		:					Dict=16		,lowerCamelCase__		:					Optional[Any]=12		,lowerCamelCase__		:					str=4096		,lowerCamelCase__		:					Dict=16		,lowerCamelCase__		:					Tuple=0.0		,lowerCamelCase__		:					Dict=0.0		,lowerCamelCase__		:					str=True		,lowerCamelCase__		:					Any=True		,lowerCamelCase__		:					List[Any]="gelu"		,lowerCamelCase__		:					Tuple=1024		,lowerCamelCase__		:					Optional[int]=0.1		,lowerCamelCase__		:					Tuple=0.0		,lowerCamelCase__		:					Dict=0.0		,lowerCamelCase__		:					Optional[int]=0.02		,lowerCamelCase__		:					Optional[int]=0		,lowerCamelCase__		:					List[Any]=False		,lowerCamelCase__		:					Union[str, Any]=0		,lowerCamelCase__		:					Optional[int]=1		,lowerCamelCase__		:					Any=1		,**lowerCamelCase__		:					Dict		,)  ->	str:




           '''simple docstring'''


           SCREAMING_SNAKE_CASE	=		vocab_size
           SCREAMING_SNAKE_CASE	=		max_position_embeddings
           SCREAMING_SNAKE_CASE	=		d_model
           SCREAMING_SNAKE_CASE	=		encoder_ffn_dim
           SCREAMING_SNAKE_CASE	=		encoder_layers
           SCREAMING_SNAKE_CASE	=		encoder_attention_heads
           SCREAMING_SNAKE_CASE	=		decoder_ffn_dim
           SCREAMING_SNAKE_CASE	=		decoder_layers
           SCREAMING_SNAKE_CASE	=		decoder_attention_heads
           SCREAMING_SNAKE_CASE	=		dropout
           SCREAMING_SNAKE_CASE	=		attention_dropout
           SCREAMING_SNAKE_CASE	=		activation_dropout
           SCREAMING_SNAKE_CASE	=		activation_function
           SCREAMING_SNAKE_CASE	=		init_std
           SCREAMING_SNAKE_CASE	=		encoder_layerdrop
           SCREAMING_SNAKE_CASE	=		decoder_layerdrop
           SCREAMING_SNAKE_CASE	=		use_cache
           SCREAMING_SNAKE_CASE	=		encoder_layers
           SCREAMING_SNAKE_CASE	=		scale_embedding  # scale factor will be sqrt(d_model) if True
           super().__init__(
               pad_token_id=lowerCamelCase__		,eos_token_id=lowerCamelCase__		,is_encoder_decoder=lowerCamelCase__		,decoder_start_token_id=lowerCamelCase__		,forced_eos_token_id=lowerCamelCase__		,**lowerCamelCase__		,)






     @property
     def        SCREAMING_SNAKE_CASE__						(							self		:					Tuple )  ->	int:




           '''simple docstring'''


           return self.encoder_attention_heads






     @property
     def        SCREAMING_SNAKE_CASE__						(							self		:					List[str] )  ->	int:




           '''simple docstring'''


           return self.d_model


