


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

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






										'''simple docstring'''






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

					def   UpperCAmelCase__       (     self       :   List[str]       ):






										'''simple docstring'''






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

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

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

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

										lowercase_  							=			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   UpperCAmelCase__       (     self       :   str							,					UpperCamelCase__       :   Tuple							,					UpperCamelCase__       :   Optional[int]							,					UpperCamelCase__       :   Dict							,					UpperCamelCase__       :   Dict							,					*UpperCamelCase__       :   Tuple       ):






										'''simple docstring'''






										lowercase_  							=			OpenAIGPTModel(config=UpperCamelCase__       )
										model.to(UpperCamelCase__       )
										model.eval()

										lowercase_  							=			model(UpperCamelCase__							,					token_type_ids=UpperCamelCase__							,					head_mask=UpperCamelCase__       )
										lowercase_  							=			model(UpperCamelCase__							,					token_type_ids=UpperCamelCase__       )
										lowercase_  							=			model(UpperCamelCase__       )

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

					def   UpperCAmelCase__       (     self       :   Optional[int]							,					UpperCamelCase__       :   Union[str, Any]							,					UpperCamelCase__       :   Dict							,					UpperCamelCase__       :   Optional[Any]							,					UpperCamelCase__       :   List[str]							,					*UpperCamelCase__       :   Union[str, Any]       ):






										'''simple docstring'''






										lowercase_  							=			OpenAIGPTLMHeadModel(UpperCamelCase__       )
										model.to(UpperCamelCase__       )
										model.eval()

										lowercase_  							=			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   UpperCAmelCase__       (     self       :   Optional[Any]							,					UpperCamelCase__       :   Any							,					UpperCamelCase__       :   int							,					UpperCamelCase__       :   Union[str, Any]							,					UpperCamelCase__       :   Union[str, Any]							,					*UpperCamelCase__       :   int       ):






										'''simple docstring'''






										lowercase_  							=			OpenAIGPTDoubleHeadsModel(UpperCamelCase__       )
										model.to(UpperCamelCase__       )
										model.eval()

										lowercase_  							=			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   UpperCAmelCase__       (     self       :   Any							,					UpperCamelCase__       :   Union[str, Any]							,					UpperCamelCase__       :   List[str]							,					UpperCamelCase__       :   Optional[int]							,					UpperCamelCase__       :   Any							,					*UpperCamelCase__       :   Any       ):






										'''simple docstring'''






										lowercase_  							=			self.num_labels
										lowercase_  							=			OpenAIGPTForSequenceClassification(UpperCamelCase__       )
										model.to(UpperCamelCase__       )
										model.eval()

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







					def   UpperCAmelCase__       (     self       :   Optional[Any]       ):






										'''simple docstring'''






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

										return config, inputs_dict







@require_torch
class        UpperCamelCase__   (	__magic_name__							,				__magic_name__							,				__magic_name__							,				unittest.TestCase   ):
					__SCREAMING_SNAKE_CASE       :  str						=   (
					    (OpenAIGPTModel, OpenAIGPTLMHeadModel, OpenAIGPTDoubleHeadsModel, OpenAIGPTForSequenceClassification)
					    if is_torch_available()
					    else ()
					)
					__SCREAMING_SNAKE_CASE       :  List[Any]						=   (
					    (OpenAIGPTLMHeadModel,) if is_torch_available() else ()
					)  # TODO (PVP): Add Double HeadsModel when generate() function is changed accordingly
					__SCREAMING_SNAKE_CASE       :  str						=   (
					    {
					        'feature-extraction': OpenAIGPTModel,
					        'text-classification': OpenAIGPTForSequenceClassification,
					        'text-generation': OpenAIGPTLMHeadModel,
					        'zero-shot': OpenAIGPTForSequenceClassification,
					    }
					    if is_torch_available()
					    else {}
					)

					def   UpperCAmelCase__       (     self       :   int							,					UpperCamelCase__       :   List[str]							,					UpperCamelCase__       :   Union[str, Any]							,					UpperCamelCase__       :   str							,					UpperCamelCase__       :   Tuple							,					UpperCamelCase__       :   int       ):






										'''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   UpperCAmelCase__       (     self       :   Tuple							,					UpperCamelCase__       :   int							,					UpperCamelCase__       :   Dict							,					UpperCamelCase__       :   Dict=False       ):






										'''simple docstring'''






										lowercase_  							=			super()._prepare_for_class(UpperCamelCase__							,					UpperCamelCase__							,					return_labels=UpperCamelCase__       )

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

					def   UpperCAmelCase__       (     self       :   List[str]       ):






										'''simple docstring'''






										lowercase_  							=			OpenAIGPTModelTester(self       )
										lowercase_  							=			ConfigTester(self							,					config_class=UpperCamelCase__							,					n_embd=37       )

					def   UpperCAmelCase__       (     self       :   str       ):






										'''simple docstring'''






										self.config_tester.run_common_tests()

					def   UpperCAmelCase__       (     self       :   Optional[int]       ):






										'''simple docstring'''






										lowercase_  							=			self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_openai_gpt_model(*UpperCamelCase__       )

					def   UpperCAmelCase__       (     self       :   Tuple       ):






										'''simple docstring'''






										lowercase_  							=			self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_lm_head_model(*UpperCamelCase__       )

					def   UpperCAmelCase__       (     self       :   Optional[int]       ):






										'''simple docstring'''






										lowercase_  							=			self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_double_lm_head_model(*UpperCamelCase__       )

					def   UpperCAmelCase__       (     self       :   Optional[Any]       ):






										'''simple docstring'''






										lowercase_  							=			self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_openai_gpt_for_sequence_classification(*UpperCamelCase__       )







					@slow
					def   UpperCAmelCase__       (     self       :   str       ):






										'''simple docstring'''






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







@require_torch
class        UpperCamelCase__   (	unittest.TestCase   ):

					@slow
					def   UpperCAmelCase__       (     self       :   Dict       ):






										'''simple docstring'''






										lowercase_  							=			OpenAIGPTLMHeadModel.from_pretrained("""openai-gpt"""       )
										model.to(UpperCamelCase__       )
										lowercase_  							=			torch.tensor([[481, 4_735, 544]]							,					dtype=torch.long							,					device=UpperCamelCase__       )  # the president is
										lowercase_  							=			[
										    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

										lowercase_  							=			model.generate(UpperCamelCase__							,					do_sample=UpperCamelCase__       )
										self.assertListEqual(output_ids[0].tolist()							,					UpperCamelCase__       )






def 			UpperCAmelCase_				(      UpperCAmelCase__  ):
					if not isinstance(UpperCAmelCase__						,							UpperCAmelCase__  ):
										raise TypeError("""Input value must be an 'int' type"""  )

					lowercase_  							=			0
					while number:
										position += 1
										number >>= 1

					return position


if __name__ == "__main__":
							import doctest

							doctest.testmod()


