

'''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 		__SCREAMING_SNAKE_CASE       :







				def __init__(					self    :		List[Any]		,				UpperCAmelCase__    :		List[Any]		,				UpperCAmelCase__    :		Optional[Any]=13		,				UpperCAmelCase__    :		Optional[int]=7		,				UpperCAmelCase__    :		Union[str, Any]=True		,				UpperCAmelCase__    :		Optional[int]=True		,				UpperCAmelCase__    :		List[str]=True		,				UpperCAmelCase__    :		List[Any]=99		,				UpperCAmelCase__    :		Optional[Any]=32		,				UpperCAmelCase__    :		List[str]=5		,				UpperCAmelCase__    :		Union[str, Any]=4		,				UpperCAmelCase__    :		int=37		,				UpperCAmelCase__    :		Tuple="gelu"		,				UpperCAmelCase__    :		Optional[int]=0.1		,				UpperCAmelCase__    :		Union[str, Any]=0.1		,				UpperCAmelCase__    :		Dict=512		,				UpperCAmelCase__    :		Optional[Any]=16		,				UpperCAmelCase__    :		Dict=2		,				UpperCAmelCase__    :		str=0.02		,				UpperCAmelCase__    :		Any=3		,				UpperCAmelCase__    :		Tuple=4		,				UpperCAmelCase__    :		int=None		,				):
										'''simple docstring'''
										lowercase   :       Dict								=parent
										lowercase   :       Tuple								=batch_size
										lowercase   :       Optional[int]								=seq_length
										lowercase   :       Tuple								=is_training
										lowercase   :       Tuple								=use_token_type_ids
										lowercase   :       Union[str, Any]								=use_labels
										lowercase   :       int								=vocab_size
										lowercase   :       List[Any]								=hidden_size
										lowercase   :       int								=num_hidden_layers
										lowercase   :       int								=num_attention_heads
										lowercase   :       Dict								=intermediate_size
										lowercase   :       Dict								=hidden_act
										lowercase   :       Union[str, Any]								=hidden_dropout_prob
										lowercase   :       Any								=attention_probs_dropout_prob
										lowercase   :       Dict								=max_position_embeddings
										lowercase   :       Dict								=type_vocab_size
										lowercase   :       Union[str, Any]								=type_sequence_label_size
										lowercase   :       Tuple								=initializer_range
										lowercase   :       Tuple								=num_labels
										lowercase   :       Dict								=num_choices
										lowercase   :       Union[str, Any]								=scope
										lowercase   :       Any								=self.vocab_size - 1







				def    lowerCamelCase_  (					self    :		Optional[int]      ):
										'''simple docstring'''
										lowercase   :       str								=ids_tensor([self.batch_size, self.seq_length]		,				self.vocab_size      )

										lowercase   :       List[Any]								=None
										if self.use_token_type_ids:
																lowercase   :       str								=ids_tensor([self.batch_size, self.seq_length]		,				self.type_vocab_size      )

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

										lowercase   :       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		,				)

										lowercase   :       Tuple								=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[str]		,				UpperCAmelCase__    :		List[str]		,				UpperCAmelCase__    :		Tuple		,				UpperCAmelCase__    :		Dict		,				UpperCAmelCase__    :		Tuple		,				*UpperCAmelCase__    :		Union[str, Any]      ):
										'''simple docstring'''
										lowercase   :       int								=OpenAIGPTModel(config=UpperCamelCase__      )
										model.to(UpperCamelCase__      )
										model.eval()

										lowercase   :       str								=model(UpperCamelCase__		,				token_type_ids=UpperCamelCase__		,				head_mask=UpperCamelCase__      )
										lowercase   :       Tuple								=model(UpperCamelCase__		,				token_type_ids=UpperCamelCase__      )
										lowercase   :       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__    :		Dict		,				UpperCAmelCase__    :		Any		,				UpperCAmelCase__    :		Any		,				UpperCAmelCase__    :		Dict		,				*UpperCAmelCase__    :		Union[str, Any]      ):
										'''simple docstring'''
										lowercase   :       Tuple								=OpenAIGPTLMHeadModel(UpperCamelCase__      )
										model.to(UpperCamelCase__      )
										model.eval()

										lowercase   :       List[str]								=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    :		str		,				UpperCAmelCase__    :		List[Any]		,				UpperCAmelCase__    :		Any		,				UpperCAmelCase__    :		int		,				UpperCAmelCase__    :		List[Any]		,				*UpperCAmelCase__    :		List[str]      ):
										'''simple docstring'''
										lowercase   :       Any								=OpenAIGPTDoubleHeadsModel(UpperCamelCase__      )
										model.to(UpperCamelCase__      )
										model.eval()

										lowercase   :       str								=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    :		str		,				UpperCAmelCase__    :		int		,				UpperCAmelCase__    :		Optional[Any]		,				UpperCAmelCase__    :		Dict		,				UpperCAmelCase__    :		Optional[Any]		,				*UpperCAmelCase__    :		Dict      ):
										'''simple docstring'''
										lowercase   :       str								=self.num_labels
										lowercase   :       int								=OpenAIGPTForSequenceClassification(UpperCamelCase__      )
										model.to(UpperCamelCase__      )
										model.eval()

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







				def    lowerCamelCase_  (					self    :		Any      ):
										'''simple docstring'''
										lowercase   :       Optional[int]								=self.prepare_config_and_inputs()
										(
										    lowercase       
										)   :       Optional[Any]								=config_and_inputs
										lowercase   :       List[Any]								={
										    'input_ids': input_ids,
										    'token_type_ids': token_type_ids,
										    'head_mask': head_mask,
										}

										return config, inputs_dict





@require_torch
class 		__SCREAMING_SNAKE_CASE       ( __A	,       __A	,       __A	,       unittest.TestCase			):
				lowerCamelCase_							=						(
				    (OpenAIGPTModel, OpenAIGPTLMHeadModel, OpenAIGPTDoubleHeadsModel, OpenAIGPTForSequenceClassification)
				    if is_torch_available()
				    else ()
				)
				lowerCamelCase_							=						(
				    (OpenAIGPTLMHeadModel,) if is_torch_available() else ()
				)  # TODO (PVP): Add Double HeadsModel when generate() function is changed accordingly
				lowerCamelCase_							=						(
				    {
				        """feature-extraction""": OpenAIGPTModel,
				        """text-classification""": OpenAIGPTForSequenceClassification,
				        """text-generation""": OpenAIGPTLMHeadModel,
				        """zero-shot""": OpenAIGPTForSequenceClassification,
				    }
				    if is_torch_available()
				    else {}
				)







				def    lowerCamelCase_  (					self    :		Optional[Any]		,				UpperCAmelCase__    :		str		,				UpperCAmelCase__    :		Dict		,				UpperCAmelCase__    :		List[str]		,				UpperCAmelCase__    :		Any		,				UpperCAmelCase__    :		Optional[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    :		str		,				UpperCAmelCase__    :		List[Any]		,				UpperCAmelCase__    :		Dict		,				UpperCAmelCase__    :		Optional[Any]=False      ):
										'''simple docstring'''
										lowercase   :       Any								=super()._prepare_for_class(UpperCamelCase__		,				UpperCamelCase__		,				return_labels=UpperCamelCase__      )

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







				def    lowerCamelCase_  (					self    :		Tuple      ):
										'''simple docstring'''
										lowercase   :       List[Any]								=OpenAIGPTModelTester(self      )
										lowercase   :       Union[str, Any]								=ConfigTester(self		,				config_class=UpperCamelCase__		,				n_embd=37      )







				def    lowerCamelCase_  (					self    :		List[Any]      ):
										'''simple docstring'''
										self.config_tester.run_common_tests()







				def    lowerCamelCase_  (					self    :		List[str]      ):
										'''simple docstring'''
										lowercase   :       Any								=self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_openai_gpt_model(*UpperCamelCase__      )







				def    lowerCamelCase_  (					self    :		Tuple      ):
										'''simple docstring'''
										lowercase   :       str								=self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_lm_head_model(*UpperCamelCase__      )







				def    lowerCamelCase_  (					self    :		str      ):
										'''simple docstring'''
										lowercase   :       Dict								=self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_double_lm_head_model(*UpperCamelCase__      )







				def    lowerCamelCase_  (					self    :		Union[str, Any]      ):
										'''simple docstring'''
										lowercase   :       List[Any]								=self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_openai_gpt_for_sequence_classification(*UpperCamelCase__      )







				@slow
				def    lowerCamelCase_  (					self    :		Dict      ):
										'''simple docstring'''
										for model_name in OPENAI_GPT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																lowercase   :       Optional[int]								=OpenAIGPTModel.from_pretrained(UpperCamelCase__      )
																self.assertIsNotNone(UpperCamelCase__      )





@require_torch
class 		__SCREAMING_SNAKE_CASE       ( unittest.TestCase			):







				@slow
				def    lowerCamelCase_  (					self    :		Union[str, Any]      ):
										'''simple docstring'''
										lowercase   :       List[Any]								=OpenAIGPTLMHeadModel.from_pretrained('''openai-gpt'''      )
										model.to(UpperCamelCase__      )
										lowercase   :       List[Any]								=torch.tensor([[481, 4735, 544]]		,				dtype=torch.long		,				device=UpperCamelCase__      )  # the president is
										lowercase   :       Any								=[
										    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

										lowercase   :       Optional[int]								=model.generate(UpperCamelCase__		,				do_sample=UpperCamelCase__      )
										self.assertListEqual(output_ids[0].tolist()		,				UpperCamelCase__      )








'''simple docstring'''





from transformers import HfArgumentParser, TensorFlowBenchmark, TensorFlowBenchmarkArguments






def 							_lowerCAmelCase (      )							->     List[Any]:
						lowercase   :       Tuple								=HfArgumentParser(__magic_name__	)
						lowercase   :       Union[str, Any]								=parser.parse_args_into_dataclasses()[0]
						lowercase   :       Any								=TensorFlowBenchmark(args=__magic_name__	)
						try:
												lowercase   :       List[Any]								=parser.parse_args_into_dataclasses()[0]
						except ValueError as e:
												lowercase   :       List[Any]								='''Arg --no_{0} is no longer used, please use --no-{0} instead.'''
												lowercase   :       Any								=''' '''.join(str(__magic_name__	).split(''' '''	)[:-1]	)
												lowercase   :       Optional[Any]								=''''''
												lowercase   :       List[str]								=eval(str(__magic_name__	).split(''' '''	)[-1]	)
												lowercase   :       Optional[Any]								=[]
												for arg in depreciated_args:
																		# arg[2:] removes '--'
																		if arg[2:] in TensorFlowBenchmark.deprecated_args:
																								# arg[5:] removes '--no_'
																								full_error_msg += arg_error_msg.format(arg[5:]	)
																		else:
																								wrong_args.append(__magic_name__	)
												if len(__magic_name__	) > 0:
																		lowercase   :       int								=full_error_msg + begin_error_msg + str(__magic_name__	)
												raise ValueError(__magic_name__	)
						benchmark.run()


if __name__ == "__main__":
							main()





