

'''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	:						Tuple		,      lowerCAmelCase_	:						Union[str, Any]		,      lowerCAmelCase_	:						Union[str, Any]=13		,      lowerCAmelCase_	:						int=7		,      lowerCAmelCase_	:						Tuple=True		,      lowerCAmelCase_	:						Union[str, Any]=True		,      lowerCAmelCase_	:						str=True		,      lowerCAmelCase_	:						Union[str, Any]=99		,      lowerCAmelCase_	:						Optional[Any]=32		,      lowerCAmelCase_	:						Any=5		,      lowerCAmelCase_	:						Dict=4		,      lowerCAmelCase_	:						Any=37		,      lowerCAmelCase_	:						Tuple="gelu"		,      lowerCAmelCase_	:						Optional[int]=0.1		,      lowerCAmelCase_	:						int=0.1		,      lowerCAmelCase_	:						List[str]=5_12		,      lowerCAmelCase_	:						Any=16		,      lowerCAmelCase_	:						Optional[int]=2		,      lowerCAmelCase_	:						List[str]=0.02		,      lowerCAmelCase_	:						List[str]=3		,      lowerCAmelCase_	:						Tuple=4		,      lowerCAmelCase_	:						Optional[int]=None		,      )      ->		Tuple:





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


			def lowercase__						(				self	:						Any			)      ->		List[Any]:





									'''simple docstring'''
									A__					:       List[Any]     =ids_tensor([self.batch_size, self.seq_length]		,      self.vocab_size			)

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

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

									A__					:       Union[str, 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		,      )

									A__					:       List[str]     =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 lowercase__						(				self	:						List[str]		,      lowerCAmelCase_	:						List[str]		,      lowerCAmelCase_	:						int		,      lowerCAmelCase_	:						Optional[Any]		,      lowerCAmelCase_	:						Dict		,      *lowerCAmelCase_	:						List[Any]			)      ->		int:





									'''simple docstring'''
									A__					:       Optional[int]     =OpenAIGPTModel(config=lowerCAmelCase_			)
									model.to(lowerCAmelCase_			)
									model.eval()

									A__					:       Union[str, Any]     =model(lowerCAmelCase_		,      token_type_ids=lowerCAmelCase_		,      head_mask=lowerCAmelCase_			)
									A__					:       Any     =model(lowerCAmelCase_		,      token_type_ids=lowerCAmelCase_			)
									A__					:       Tuple     =model(lowerCAmelCase_			)

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


			def lowercase__						(				self	:						Optional[Any]		,      lowerCAmelCase_	:						Tuple		,      lowerCAmelCase_	:						Optional[Any]		,      lowerCAmelCase_	:						Tuple		,      lowerCAmelCase_	:						Union[str, Any]		,      *lowerCAmelCase_	:						Dict			)      ->		Union[str, Any]:





									'''simple docstring'''
									A__					:       Union[str, Any]     =OpenAIGPTLMHeadModel(lowerCAmelCase_			)
									model.to(lowerCAmelCase_			)
									model.eval()

									A__					:       Dict     =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 lowercase__						(				self	:						Optional[int]		,      lowerCAmelCase_	:						Union[str, Any]		,      lowerCAmelCase_	:						Tuple		,      lowerCAmelCase_	:						int		,      lowerCAmelCase_	:						Optional[Any]		,      *lowerCAmelCase_	:						Tuple			)      ->		Optional[Any]:





									'''simple docstring'''
									A__					:       Any     =OpenAIGPTDoubleHeadsModel(lowerCAmelCase_			)
									model.to(lowerCAmelCase_			)
									model.eval()

									A__					:       Any     =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 lowercase__						(				self	:						Optional[Any]		,      lowerCAmelCase_	:						List[Any]		,      lowerCAmelCase_	:						Tuple		,      lowerCAmelCase_	:						int		,      lowerCAmelCase_	:						str		,      *lowerCAmelCase_	:						Optional[Any]			)      ->		Optional[int]:





									'''simple docstring'''
									A__					:       Any     =self.num_labels
									A__					:       str     =OpenAIGPTForSequenceClassification(lowerCAmelCase_			)
									model.to(lowerCAmelCase_			)
									model.eval()

									A__					:       Tuple     =ids_tensor([self.batch_size]		,      self.type_sequence_label_size			)
									A__					:       Optional[Any]     =model(lowerCAmelCase_		,      token_type_ids=lowerCAmelCase_		,      labels=lowerCAmelCase_			)
									self.parent.assertEqual(result.logits.shape		,      (self.batch_size, self.num_labels)			)



			def lowercase__						(				self	:						Dict			)      ->		str:





									'''simple docstring'''
									A__					:       List[Any]     =self.prepare_config_and_inputs()
									(
									    (
									    A__
									)   ,						(
									    A__
									)   ,						(
									    A__
									)   ,						(
									    A__
									)   ,						(
									    A__
									)   ,						(
									    A__
									)   ,						(
									    A__
									)   ,						
									)					:       str     =config_and_inputs
									A__					:       Optional[int]     ={
									    """input_ids""": input_ids,
									    """token_type_ids""": token_type_ids,
									    """head_mask""": head_mask,
									}

									return config, inputs_dict



@require_torch
class lowerCamelCase			(  lowercase_	,						lowercase_	,						lowercase_	,						unittest.TestCase						):



			'''simple docstring'''






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


			def lowercase__						(				self	:						Dict		,      lowerCAmelCase_	:						Union[str, Any]		,      lowerCAmelCase_	:						List[str]		,      lowerCAmelCase_	:						List[str]		,      lowerCAmelCase_	:						str		,      lowerCAmelCase_	:						Optional[int]			)      ->		List[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 lowercase__						(				self	:						str		,      lowerCAmelCase_	:						List[Any]		,      lowerCAmelCase_	:						List[str]		,      lowerCAmelCase_	:						List[Any]=False			)      ->		Union[str, Any]:





									'''simple docstring'''
									A__					:       Any     =super()._prepare_for_class(lowerCAmelCase_		,      lowerCAmelCase_		,      return_labels=lowerCAmelCase_			)

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


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





									'''simple docstring'''
									A__					:       Optional[Any]     =OpenAIGPTModelTester(self			)
									A__					:       Optional[Any]     =ConfigTester(self		,      config_class=lowerCAmelCase_		,      n_embd=37			)


			def lowercase__						(				self	:						Dict			)      ->		Dict:





									'''simple docstring'''
									self.config_tester.run_common_tests()


			def lowercase__						(				self	:						Optional[int]			)      ->		Optional[int]:





									'''simple docstring'''
									A__					:       Tuple     =self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_openai_gpt_model(*lowerCAmelCase_			)


			def lowercase__						(				self	:						int			)      ->		int:





									'''simple docstring'''
									A__					:       List[str]     =self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_lm_head_model(*lowerCAmelCase_			)


			def lowercase__						(				self	:						List[Any]			)      ->		Dict:





									'''simple docstring'''
									A__					:       Any     =self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_double_lm_head_model(*lowerCAmelCase_			)


			def lowercase__						(				self	:						str			)      ->		Tuple:





									'''simple docstring'''
									A__					:       int     =self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_openai_gpt_for_sequence_classification(*lowerCAmelCase_			)



			@slow
			def lowercase__						(				self	:						Union[str, Any]			)      ->		int:





									'''simple docstring'''
									for model_name in OPENAI_GPT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
															A__					:       Tuple     =OpenAIGPTModel.from_pretrained(lowerCAmelCase_			)
															self.assertIsNotNone(lowerCAmelCase_			)



@require_torch
class lowerCamelCase			(  unittest.TestCase						):



			'''simple docstring'''






			@slow
			def lowercase__						(				self	:						str			)      ->		Union[str, Any]:





									'''simple docstring'''
									A__					:       Any     =OpenAIGPTLMHeadModel.from_pretrained("""openai-gpt"""			)
									model.to(lowerCAmelCase_			)
									A__					:       Optional[Any]     =torch.tensor([[4_81, 47_35, 5_44]]		,      dtype=torch.long		,      device=lowerCAmelCase_			)  # the president is
									A__					:       List[str]     =[
									    4_81,
									    47_35,
									    5_44,
									    2_46,
									    9_63,
									    8_70,
									    7_62,
									    2_39,
									    2_44,
									    4_04_77,
									    2_44,
									    2_49,
									    7_19,
									    8_81,
									    4_87,
									    5_44,
									    2_40,
									    2_44,
									    6_03,
									    4_81,
									]  # the president is a very good man. " \n " i\'m sure he is, " said the

									A__					:       List[Any]     =model.generate(lowerCAmelCase_		,      do_sample=lowerCAmelCase_			)
									self.assertListEqual(output_ids[0].tolist()		,      lowerCAmelCase_			)




'''simple docstring'''



from argparse import ArgumentParser

from .add_new_model import AddNewModelCommand
from .add_new_model_like import AddNewModelLikeCommand
from .convert import ConvertCommand
from .download import DownloadCommand
from .env import EnvironmentCommand
from .lfs import LfsCommands
from .pt_to_tf import PTtoTFCommand
from .run import RunCommand
from .serving import ServeCommand
from .user import UserCommands
def 						__lowerCamelCase	(       )	->							List[Any]:






						"""simple docstring"""





						A__					:       List[Any]     =ArgumentParser("""Transformers CLI tool""",		usage="""transformers-cli <command> [<args>]"""	)
						A__					:       List[Any]     =parser.add_subparsers(help="""transformers-cli command helpers"""	)

						# Register commands
						ConvertCommand.register_subcommand(__snake_case	)
						DownloadCommand.register_subcommand(__snake_case	)
						EnvironmentCommand.register_subcommand(__snake_case	)
						RunCommand.register_subcommand(__snake_case	)
						ServeCommand.register_subcommand(__snake_case	)
						UserCommands.register_subcommand(__snake_case	)
						AddNewModelCommand.register_subcommand(__snake_case	)
						AddNewModelLikeCommand.register_subcommand(__snake_case	)
						LfsCommands.register_subcommand(__snake_case	)
						PTtoTFCommand.register_subcommand(__snake_case	)

						# Let's go
						A__					:       List[str]     =parser.parse_args()

						if not hasattr(__snake_case,		"""func"""	):
												parser.print_help()
												exit(1	)

						# Run
						A__					:       Optional[Any]     =args.func(__snake_case	)
						service.run()


if __name__ == "__main__":
		main()

