


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







					"""simple docstring"""


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






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






					def lowerCamelCase__							(       self    : str  ):
												'''simple docstring'''






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

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

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

												__UpperCAmelCase   :						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   :						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 lowerCamelCase__							(       self    : List[Any] ,       UpperCamelCase    : str ,       UpperCamelCase    : str ,       UpperCamelCase    : Dict ,       UpperCamelCase    : str ,       *UpperCamelCase    : Any  ):
												'''simple docstring'''






												__UpperCAmelCase   :						str      							=					OpenAIGPTModel(config=UpperCamelCase  )
												model.to(UpperCamelCase  )
												model.eval()

												__UpperCAmelCase   :						Optional[int]      							=					model(UpperCamelCase ,       token_type_ids=UpperCamelCase ,       head_mask=UpperCamelCase  )
												__UpperCAmelCase   :						Optional[Any]      							=					model(UpperCamelCase ,       token_type_ids=UpperCamelCase  )
												__UpperCAmelCase   :						Optional[Any]      							=					model(UpperCamelCase  )

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






					def lowerCamelCase__							(       self    : Optional[Any] ,       UpperCamelCase    : Optional[Any] ,       UpperCamelCase    : List[Any] ,       UpperCamelCase    : Union[str, Any] ,       UpperCamelCase    : Any ,       *UpperCamelCase    : List[str]  ):
												'''simple docstring'''






												__UpperCAmelCase   :						Tuple      							=					OpenAIGPTLMHeadModel(UpperCamelCase  )
												model.to(UpperCamelCase  )
												model.eval()

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






					def lowerCamelCase__							(       self    : Optional[int] ,       UpperCamelCase    : Union[str, Any] ,       UpperCamelCase    : str ,       UpperCamelCase    : List[str] ,       UpperCamelCase    : Tuple ,       *UpperCamelCase    : Any  ):
												'''simple docstring'''






												__UpperCAmelCase   :						List[Any]      							=					OpenAIGPTDoubleHeadsModel(UpperCamelCase  )
												model.to(UpperCamelCase  )
												model.eval()

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






					def lowerCamelCase__							(       self    : Any ,       UpperCamelCase    : Optional[Any] ,       UpperCamelCase    : List[Any] ,       UpperCamelCase    : Tuple ,       UpperCamelCase    : Tuple ,       *UpperCamelCase    : str  ):
												'''simple docstring'''






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

												__UpperCAmelCase   :						List[str]      							=					ids_tensor([self.batch_size] ,       self.type_sequence_label_size  )
												__UpperCAmelCase   :						Dict      							=					model(UpperCamelCase ,       token_type_ids=UpperCamelCase ,       labels=UpperCamelCase  )
												self.parent.assertEqual(result.logits.shape ,       (self.batch_size, self.num_labels)  )






					def lowerCamelCase__							(       self    : List[Any]  ):
												'''simple docstring'''






												__UpperCAmelCase   :						Optional[int]      							=					self.prepare_config_and_inputs()
												(
												    (
												    __UpperCAmelCase
												)							,(
												    __UpperCAmelCase
												)							,(
												    __UpperCAmelCase
												)							,(
												    __UpperCAmelCase
												)							,(
												    __UpperCAmelCase
												)							,(
												    __UpperCAmelCase
												)							,(
												    __UpperCAmelCase
												)							,
												)   :						int      							=					config_and_inputs
												__UpperCAmelCase   :						int      							=					{
												    """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"""


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






					def lowerCamelCase__							(       self    : Tuple ,       UpperCamelCase    : Union[str, Any] ,       UpperCamelCase    : Optional[int] ,       UpperCamelCase    : Union[str, Any] ,       UpperCamelCase    : int ,       UpperCamelCase    : 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 lowerCamelCase__							(       self    : Tuple ,       UpperCamelCase    : Optional[int] ,       UpperCamelCase    : Optional[int] ,       UpperCamelCase    : str=False  ):
												'''simple docstring'''






												__UpperCAmelCase   :						List[str]      							=					super()._prepare_for_class(UpperCamelCase ,       UpperCamelCase ,       return_labels=UpperCamelCase  )

												if return_labels:
																			if model_class.__name__ == "OpenAIGPTDoubleHeadsModel":
																										__UpperCAmelCase   :						Optional[int]      							=					torch.zeros(
																										    (self.model_tester.batch_size, self.model_tester.num_choices, self.model_tester.seq_length) ,       dtype=torch.long ,       device=UpperCamelCase ,       )
																										__UpperCAmelCase   :						Optional[int]      							=					inputs_dict["""labels"""]
																										__UpperCAmelCase   :						Dict      							=					inputs_dict["""labels"""]
																										__UpperCAmelCase   :						Optional[int]      							=					torch.zeros(
																										    (self.model_tester.batch_size, self.model_tester.num_choices) ,       dtype=torch.long ,       device=UpperCamelCase ,       )
																										__UpperCAmelCase   :						Any      							=					torch.zeros(
																										    self.model_tester.batch_size ,       dtype=torch.long ,       device=UpperCamelCase  )
												return inputs_dict






					def lowerCamelCase__							(       self    : str  ):
												'''simple docstring'''






												__UpperCAmelCase   :						List[str]      							=					OpenAIGPTModelTester(self  )
												__UpperCAmelCase   :						List[Any]      							=					ConfigTester(self ,       config_class=UpperCamelCase ,       n_embd=37  )






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






												self.config_tester.run_common_tests()






					def lowerCamelCase__							(       self    : int  ):
												'''simple docstring'''






												__UpperCAmelCase   :						Dict      							=					self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_openai_gpt_model(*UpperCamelCase  )






					def lowerCamelCase__							(       self    : str  ):
												'''simple docstring'''






												__UpperCAmelCase   :						Optional[int]      							=					self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_lm_head_model(*UpperCamelCase  )






					def lowerCamelCase__							(       self    : str  ):
												'''simple docstring'''






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






					def lowerCamelCase__							(       self    : int  ):
												'''simple docstring'''






												__UpperCAmelCase   :						Tuple      							=					self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_openai_gpt_for_sequence_classification(*UpperCamelCase  )






					@slow
					def lowerCamelCase__							(       self    : str  ):
												'''simple docstring'''






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






@require_torch
class      lowerCamelCase__   (     unittest.TestCase				):







					"""simple docstring"""


					@slow
					def lowerCamelCase__							(       self    : List[str]  ):
												'''simple docstring'''






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




"""simple docstring"""






from ..utils import DummyObject, requires_backends





class      lowerCamelCase__   (     metaclass=A				):







					"""simple docstring"""


					__a	       =							["""note_seq"""]






					def __init__(       self    : List[Any] ,       *UpperCamelCase    : List[Any] ,       **UpperCamelCase    : Tuple  ):
												'''simple docstring'''






												requires_backends(self ,       ["""note_seq"""]  )






					@classmethod
					def lowerCamelCase__							(       cls    : Union[str, Any] ,       *UpperCamelCase    : Union[str, Any] ,       **UpperCamelCase    : str  ):
												'''simple docstring'''






												requires_backends(cls ,       ["""note_seq"""]  )






					@classmethod
					def lowerCamelCase__							(       cls    : List[str] ,       *UpperCamelCase    : Optional[Any] ,       **UpperCamelCase    : Union[str, Any]  ):
												'''simple docstring'''






												requires_backends(cls ,       ["""note_seq"""]  )
