






'''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    A_	:
							"""simple docstring"""




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


										'''simple docstring'''




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

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


										'''simple docstring'''




										snake_case_				:					Optional[int]   =       ids_tensor([self.batch_size, self.seq_length]       ,							self.vocab_size				)

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

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

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

										snake_case_				:					Optional[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 							_A	(		self			:str       ,							lowerCAmelCase__			:int       ,							lowerCAmelCase__			:int       ,							lowerCAmelCase__			:Optional[int]       ,							lowerCAmelCase__			:int       ,							*lowerCAmelCase__			:List[Any]				)				->  int:


										'''simple docstring'''




										snake_case_				:					Optional[int]   =       OpenAIGPTModel(config=__lowerCamelCase				)
										model.to(__lowerCamelCase				)
										model.eval()

										snake_case_				:					Tuple   =       model(__lowerCamelCase       ,							token_type_ids=__lowerCamelCase       ,							head_mask=__lowerCamelCase				)
										snake_case_				:					str   =       model(__lowerCamelCase       ,							token_type_ids=__lowerCamelCase				)
										snake_case_				:					Optional[int]   =       model(__lowerCamelCase				)

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

							def 							_A	(		self			:str       ,							lowerCAmelCase__			:List[Any]       ,							lowerCAmelCase__			:Union[str, Any]       ,							lowerCAmelCase__			:List[str]       ,							lowerCAmelCase__			:Any       ,							*lowerCAmelCase__			:Optional[int]				)				->  Any:


										'''simple docstring'''




										snake_case_				:					Tuple   =       OpenAIGPTLMHeadModel(__lowerCamelCase				)
										model.to(__lowerCamelCase				)
										model.eval()

										snake_case_				:					List[str]   =       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 							_A	(		self			:Dict       ,							lowerCAmelCase__			:Any       ,							lowerCAmelCase__			:Union[str, Any]       ,							lowerCAmelCase__			:Optional[int]       ,							lowerCAmelCase__			:Optional[int]       ,							*lowerCAmelCase__			:Tuple				)				->  str:


										'''simple docstring'''




										snake_case_				:					List[Any]   =       OpenAIGPTDoubleHeadsModel(__lowerCamelCase				)
										model.to(__lowerCamelCase				)
										model.eval()

										snake_case_				:					Optional[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 							_A	(		self			:Tuple       ,							lowerCAmelCase__			:Union[str, Any]       ,							lowerCAmelCase__			:Union[str, Any]       ,							lowerCAmelCase__			:Optional[Any]       ,							lowerCAmelCase__			:List[Any]       ,							*lowerCAmelCase__			:Optional[int]				)				->  int:


										'''simple docstring'''




										snake_case_				:					Dict   =       self.num_labels
										snake_case_				:					Tuple   =       OpenAIGPTForSequenceClassification(__lowerCamelCase				)
										model.to(__lowerCamelCase				)
										model.eval()

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






							def 							_A	(		self			:List[str]				)				->  Union[str, Any]:


										'''simple docstring'''




										snake_case_				:					str   =       self.prepare_config_and_inputs()
										(
										    snake_case_     
										)				:					Any   =       config_and_inputs
										snake_case_				:					Union[str, Any]   =       {
										    "input_ids": input_ids,
										    "token_type_ids": token_type_ids,
										    "head_mask": head_mask,
										}

										return config, inputs_dict







@require_torch
class    A_	(lowercase__						,					lowercase__						,					lowercase__						,					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 							_A	(		self			:List[str]       ,							lowerCAmelCase__			:str       ,							lowerCAmelCase__			:Tuple       ,							lowerCAmelCase__			:Any       ,							lowerCAmelCase__			:List[Any]       ,							lowerCAmelCase__			:Union[str, Any]				)				->  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 							_A	(		self			:Union[str, Any]       ,							lowerCAmelCase__			:Optional[int]       ,							lowerCAmelCase__			:Tuple       ,							lowerCAmelCase__			:Tuple=False				)				->  Optional[int]:


										'''simple docstring'''




										snake_case_				:					Tuple   =       super()._prepare_for_class(__lowerCamelCase       ,							__lowerCamelCase       ,							return_labels=__lowerCamelCase				)

										if return_labels:
													if model_class.__name__ == "OpenAIGPTDoubleHeadsModel":
																snake_case_				:					Optional[Any]   =       torch.zeros(
																    (self.model_tester.batch_size, self.model_tester.num_choices, self.model_tester.seq_length)       ,							dtype=torch.long       ,							device=__lowerCamelCase       ,							)
																snake_case_				:					Tuple   =       inputs_dict["labels"]
																snake_case_				:					Any   =       inputs_dict["labels"]
																snake_case_				:					Any   =       torch.zeros(
																    (self.model_tester.batch_size, self.model_tester.num_choices)       ,							dtype=torch.long       ,							device=__lowerCamelCase       ,							)
																snake_case_				:					Union[str, Any]   =       torch.zeros(
																    self.model_tester.batch_size       ,							dtype=torch.long       ,							device=__lowerCamelCase				)
										return inputs_dict

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


										'''simple docstring'''




										snake_case_				:					Tuple   =       OpenAIGPTModelTester(self				)
										snake_case_				:					Union[str, Any]   =       ConfigTester(self       ,							config_class=__lowerCamelCase       ,							n_embd=37				)

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


										'''simple docstring'''




										self.config_tester.run_common_tests()

							def 							_A	(		self			:Dict				)				->  Optional[Any]:


										'''simple docstring'''




										snake_case_				:					List[Any]   =       self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_openai_gpt_model(*__lowerCamelCase				)

							def 							_A	(		self			:str				)				->  Tuple:


										'''simple docstring'''




										snake_case_				:					Union[str, Any]   =       self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_lm_head_model(*__lowerCamelCase				)

							def 							_A	(		self			:Dict				)				->  Any:


										'''simple docstring'''




										snake_case_				:					Optional[int]   =       self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_double_lm_head_model(*__lowerCamelCase				)

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


										'''simple docstring'''




										snake_case_				:					Optional[int]   =       self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_openai_gpt_for_sequence_classification(*__lowerCamelCase				)






							@slow
							def 							_A	(		self			:List[str]				)				->  int:


										'''simple docstring'''




										for model_name in OPENAI_GPT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
													snake_case_				:					Any   =       OpenAIGPTModel.from_pretrained(__lowerCamelCase				)
													self.assertIsNotNone(__lowerCamelCase				)







@require_torch
class    A_	(unittest.TestCase							):
							"""simple docstring"""




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


										'''simple docstring'''




										snake_case_				:					List[Any]   =       OpenAIGPTLMHeadModel.from_pretrained("openai-gpt"				)
										model.to(__lowerCamelCase				)
										snake_case_				:					int   =       torch.tensor([[481, 4_735, 544]]       ,							dtype=torch.long       ,							device=__lowerCamelCase				)  # the president is
										snake_case_				:					Union[str, 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

										snake_case_				:					int   =       model.generate(__lowerCamelCase       ,							do_sample=__lowerCamelCase				)
										self.assertListEqual(output_ids[0].tolist()       ,							__lowerCamelCase				)












'''simple docstring'''




from ...configuration_utils import PretrainedConfig
from ...utils import logging


__lowerCamelCase   :					List[str]   							=					logging.get_logger(__name__)

__lowerCamelCase   :					int   							=					{
    '''microsoft/cvt-13''': '''https://huggingface.co/microsoft/cvt-13/resolve/main/config.json''',
    # See all Cvt models at https://huggingface.co/models?filter=cvt
}







class    A_	(a_							):
							"""simple docstring"""




							a__   		=	'''cvt'''

							def __init__(		self			:List[Any]       ,							lowerCAmelCase__			:Optional[int]=3       ,							lowerCAmelCase__			:Any=[7, 3, 3]       ,							lowerCAmelCase__			:Dict=[4, 2, 2]       ,							lowerCAmelCase__			:Union[str, Any]=[2, 1, 1]       ,							lowerCAmelCase__			:Any=[64, 192, 384]       ,							lowerCAmelCase__			:List[str]=[1, 3, 6]       ,							lowerCAmelCase__			:str=[1, 2, 10]       ,							lowerCAmelCase__			:Any=[4.0, 4.0, 4.0]       ,							lowerCAmelCase__			:int=[0.0, 0.0, 0.0]       ,							lowerCAmelCase__			:Optional[Any]=[0.0, 0.0, 0.0]       ,							lowerCAmelCase__			:Dict=[0.0, 0.0, 0.1]       ,							lowerCAmelCase__			:List[Any]=[True, True, True]       ,							lowerCAmelCase__			:List[Any]=[False, False, True]       ,							lowerCAmelCase__			:Dict=["dw_bn", "dw_bn", "dw_bn"]       ,							lowerCAmelCase__			:Any=[3, 3, 3]       ,							lowerCAmelCase__			:Tuple=[1, 1, 1]       ,							lowerCAmelCase__			:Optional[int]=[2, 2, 2]       ,							lowerCAmelCase__			:Union[str, Any]=[1, 1, 1]       ,							lowerCAmelCase__			:Any=[1, 1, 1]       ,							lowerCAmelCase__			:List[str]=0.0_2       ,							lowerCAmelCase__			:Dict=1E-1_2       ,							**lowerCAmelCase__			:Optional[Any]       ,							)				->  str:


										'''simple docstring'''




										super().__init__(**lowerCAmelCase__				)
										snake_case_				:					int   =       num_channels
										snake_case_				:					int   =       patch_sizes
										snake_case_				:					Optional[Any]   =       patch_stride
										snake_case_				:					Dict   =       patch_padding
										snake_case_				:					Tuple   =       embed_dim
										snake_case_				:					Optional[int]   =       num_heads
										snake_case_				:					Union[str, Any]   =       depth
										snake_case_				:					Optional[int]   =       mlp_ratio
										snake_case_				:					Tuple   =       attention_drop_rate
										snake_case_				:					str   =       drop_rate
										snake_case_				:					Tuple   =       drop_path_rate
										snake_case_				:					Any   =       qkv_bias
										snake_case_				:					Union[str, Any]   =       cls_token
										snake_case_				:					int   =       qkv_projection_method
										snake_case_				:					Any   =       kernel_qkv
										snake_case_				:					Union[str, Any]   =       padding_kv
										snake_case_				:					str   =       stride_kv
										snake_case_				:					Dict   =       padding_q
										snake_case_				:					Tuple   =       stride_q
										snake_case_				:					Any   =       initializer_range
										snake_case_				:					Any   =       layer_norm_eps




