



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







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







							def 				__lowercase     (				self   :			List[str]						):
									_a	: Optional[int]								=				ids_tensor([self.batch_size, self.seq_length]      ,self.vocab_size						)

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

									_a	: int								=				None
									_a	: Tuple								=				None
									_a	: str								=				None
									if self.use_labels:
											_a	: Any								=				ids_tensor([self.batch_size]      ,self.type_sequence_label_size						)
											_a	: Union[str, Any]								=				ids_tensor([self.batch_size, self.seq_length]      ,self.num_labels						)
											_a	: Tuple								=				ids_tensor([self.batch_size]      ,self.num_choices						)

									_a	: Tuple								=				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	: int								=				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   :			str      ,_UpperCAmelCase   :			Optional[Any]      ,_UpperCAmelCase   :			Any      ,_UpperCAmelCase   :			int      ,_UpperCAmelCase   :			Optional[Any]      ,*_UpperCAmelCase   :			Dict						):
									_a	: Union[str, Any]								=				OpenAIGPTModel(config=_UpperCAmelCase						)
									model.to(_UpperCAmelCase						)
									model.eval()

									_a	: Optional[Any]								=				model(_UpperCAmelCase      ,token_type_ids=_UpperCAmelCase      ,head_mask=_UpperCAmelCase						)
									_a	: str								=				model(_UpperCAmelCase      ,token_type_ids=_UpperCAmelCase						)
									_a	: Optional[int]								=				model(_UpperCAmelCase						)

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







							def 				__lowercase     (				self   :			List[str]      ,_UpperCAmelCase   :			List[str]      ,_UpperCAmelCase   :			List[str]      ,_UpperCAmelCase   :			str      ,_UpperCAmelCase   :			Optional[Any]      ,*_UpperCAmelCase   :			List[Any]						):
									_a	: List[str]								=				OpenAIGPTLMHeadModel(_UpperCAmelCase						)
									model.to(_UpperCAmelCase						)
									model.eval()

									_a	: Optional[int]								=				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 				__lowercase     (				self   :			Any      ,_UpperCAmelCase   :			int      ,_UpperCAmelCase   :			Dict      ,_UpperCAmelCase   :			List[Any]      ,_UpperCAmelCase   :			Any      ,*_UpperCAmelCase   :			Union[str, Any]						):
									_a	: Any								=				OpenAIGPTDoubleHeadsModel(_UpperCAmelCase						)
									model.to(_UpperCAmelCase						)
									model.eval()

									_a	: 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 				__lowercase     (				self   :			str      ,_UpperCAmelCase   :			Dict      ,_UpperCAmelCase   :			List[str]      ,_UpperCAmelCase   :			Union[str, Any]      ,_UpperCAmelCase   :			Optional[Any]      ,*_UpperCAmelCase   :			Union[str, Any]						):
									_a	: Dict								=				self.num_labels
									_a	: Any								=				OpenAIGPTForSequenceClassification(_UpperCAmelCase						)
									model.to(_UpperCAmelCase						)
									model.eval()

									_a	: Union[str, Any]								=				ids_tensor([self.batch_size]      ,self.type_sequence_label_size						)
									_a	: Union[str, Any]								=				model(_UpperCAmelCase      ,token_type_ids=_UpperCAmelCase      ,labels=_UpperCAmelCase						)
									self.parent.assertEqual(result.logits.shape      ,(self.batch_size, self.num_labels)						)







							def 				__lowercase     (				self   :			Dict						):
									_a	: Tuple								=				self.prepare_config_and_inputs()
									(
									    (
									    _a
									)			,   (
									    _a
									)			,   (
									    _a
									)			,   (
									    _a
									)			,   (
									    _a
									)			,   (
									    _a
									)			,   (
									    _a
									)			,   
									)	: Dict								=				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       __magic_name__		(       _UpperCamelCase     ,		_UpperCamelCase     ,		_UpperCamelCase     ,		unittest.TestCase  ):
							lowerCAmelCase			:       str  							=   (
							    (OpenAIGPTModel, OpenAIGPTLMHeadModel, OpenAIGPTDoubleHeadsModel, OpenAIGPTForSequenceClassification)
							    if is_torch_available()
							    else ()
							)
							lowerCAmelCase			:       Tuple  							=   (
							    (OpenAIGPTLMHeadModel,) if is_torch_available() else ()
							)  # TODO (PVP): Add Double HeadsModel when generate() function is changed accordingly
							lowerCAmelCase			:       Tuple  							=   (
							    {
							        'feature-extraction': OpenAIGPTModel,
							        'text-classification': OpenAIGPTForSequenceClassification,
							        'text-generation': OpenAIGPTLMHeadModel,
							        'zero-shot': OpenAIGPTForSequenceClassification,
							    }
							    if is_torch_available()
							    else {}
							)







							def 				__lowercase     (				self   :			List[Any]      ,_UpperCAmelCase   :			List[str]      ,_UpperCAmelCase   :			str      ,_UpperCAmelCase   :			Optional[int]      ,_UpperCAmelCase   :			int      ,_UpperCAmelCase   :			List[str]						):
									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   :			int      ,_UpperCAmelCase   :			Dict      ,_UpperCAmelCase   :			Tuple      ,_UpperCAmelCase   :			Union[str, Any]=False						):
									_a	: Dict								=				super()._prepare_for_class(_UpperCAmelCase      ,_UpperCAmelCase      ,return_labels=_UpperCAmelCase						)

									if return_labels:
											if model_class.__name__ == "OpenAIGPTDoubleHeadsModel":
													_a	: Optional[Any]								=				torch.zeros(
													    (self.model_tester.batch_size, self.model_tester.num_choices, self.model_tester.seq_length)      ,dtype=torch.long      ,device=_UpperCAmelCase      ,)
													_a	: Dict								=				inputs_dict['labels']
													_a	: List[Any]								=				inputs_dict['labels']
													_a	: Tuple								=				torch.zeros(
													    (self.model_tester.batch_size, self.model_tester.num_choices)      ,dtype=torch.long      ,device=_UpperCAmelCase      ,)
													_a	: Tuple								=				torch.zeros(
													    self.model_tester.batch_size      ,dtype=torch.long      ,device=_UpperCAmelCase						)
									return inputs_dict







							def 				__lowercase     (				self   :			Dict						):
									_a	: Any								=				OpenAIGPTModelTester(self						)
									_a	: Any								=				ConfigTester(self      ,config_class=_UpperCAmelCase      ,n_embd=37						)







							def 				__lowercase     (				self   :			Optional[int]						):
									self.config_tester.run_common_tests()







							def 				__lowercase     (				self   :			Optional[int]						):
									_a	: List[Any]								=				self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_openai_gpt_model(*_UpperCAmelCase						)







							def 				__lowercase     (				self   :			int						):
									_a	: Optional[int]								=				self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_lm_head_model(*_UpperCAmelCase						)







							def 				__lowercase     (				self   :			str						):
									_a	: Union[str, Any]								=				self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_double_lm_head_model(*_UpperCAmelCase						)







							def 				__lowercase     (				self   :			Any						):
									_a	: str								=				self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_openai_gpt_for_sequence_classification(*_UpperCAmelCase						)







							@slow
							def 				__lowercase     (				self   :			int						):
									for model_name in OPENAI_GPT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
											_a	: Union[str, Any]								=				OpenAIGPTModel.from_pretrained(_UpperCAmelCase						)
											self.assertIsNotNone(_UpperCAmelCase						)

@require_torch
class       __magic_name__		(       unittest.TestCase  ):







							@slow
							def 				__lowercase     (				self   :			Tuple						):
									_a	: Any								=				OpenAIGPTLMHeadModel.from_pretrained('openai-gpt'						)
									model.to(_UpperCAmelCase						)
									_a	: str								=				torch.tensor([[481, 4735, 544]]      ,dtype=torch.long      ,device=_UpperCAmelCase						)  # the president is
									_a	: Optional[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

									_a	: Dict								=				model.generate(_UpperCAmelCase      ,do_sample=_UpperCAmelCase						)
									self.assertListEqual(output_ids[0].tolist()      ,_UpperCAmelCase						)









'''simple docstring'''

from abc import ABC, abstractmethod
from argparse import ArgumentParser

class       __magic_name__		(       _UpperCamelCase  ):







							@staticmethod
							@abstractmethod
							def 				__lowercase     (				_UpperCAmelCase   :			ArgumentParser						):
									raise NotImplementedError()







							@abstractmethod
							def 				__lowercase     (				self   :			str						):
									raise NotImplementedError()




