


import argparse
import re
from pathlib import Path

import requests
import torch
from PIL import Image
from torchvision.transforms import CenterCrop, Compose, Normalize, Resize, ToTensor

from transformers import (
    EfficientFormerConfig,
    EfficientFormerForImageClassificationWithTeacher,
    EfficientFormerImageProcessor,
)
from transformers.image_utils import IMAGENET_DEFAULT_MEAN, IMAGENET_DEFAULT_STD, PILImageResampling


def        _UpperCamelCase							(      lowercase__    ,       lowercase__      ):
						__SCREAMING_SNAKE_CASE						:     int					=		old_name

						if "patch_embed" in old_name:
												__SCREAMING_SNAKE_CASE			,			__SCREAMING_SNAKE_CASE			,			__SCREAMING_SNAKE_CASE						:     Any					=		old_name.split('''.'''      )

												if layer == "0":
																		__SCREAMING_SNAKE_CASE						:     Optional[Any]					=		old_name.replace('''0'''    ,       '''convolution1'''      )
												elif layer == "1":
																		__SCREAMING_SNAKE_CASE						:     Tuple					=		old_name.replace('''1'''    ,       '''batchnorm_before'''      )
												elif layer == "3":
																		__SCREAMING_SNAKE_CASE						:     List[str]					=		old_name.replace('''3'''    ,       '''convolution2'''      )
												else:
																		__SCREAMING_SNAKE_CASE						:     Optional[Any]					=		old_name.replace('''4'''    ,       '''batchnorm_after'''      )

						if "network" in old_name and re.search(R'''\d\.\d'''    ,       lowercase__      ):
												__SCREAMING_SNAKE_CASE						:     List[Any]					=		R'''\b\d{2}\b'''
												if bool(re.search(lowercase__    ,       lowercase__      )      ):
																		__SCREAMING_SNAKE_CASE						:     Dict					=		re.search(R'''\d\.\d\d.'''    ,       lowercase__      ).group()
												else:
																		__SCREAMING_SNAKE_CASE						:     Optional[int]					=		re.search(R'''\d\.\d.'''    ,       lowercase__      ).group()
												if int(match[0]      ) < 6:
																		__SCREAMING_SNAKE_CASE						:     List[Any]					=		old_name.replace(lowercase__    ,       ''''''      )
																		__SCREAMING_SNAKE_CASE						:     int					=		trimmed_name.replace('''network'''    ,       match[0] + '''.meta4D_layers.blocks.''' + match[2:-1]      )
																		__SCREAMING_SNAKE_CASE						:     Optional[Any]					=		'''intermediate_stages.''' + trimmed_name
												else:
																		__SCREAMING_SNAKE_CASE						:     Any					=		old_name.replace(lowercase__    ,       ''''''      )
																		if int(match[2]      ) < num_meta4D_last_stage:
																								__SCREAMING_SNAKE_CASE						:     Dict					=		trimmed_name.replace('''network'''    ,       '''meta4D_layers.blocks.''' + match[2]      )
																		else:
																								__SCREAMING_SNAKE_CASE						:     Optional[Any]					=		str(int(match[2]      ) - num_meta4D_last_stage      )
																								__SCREAMING_SNAKE_CASE						:     Optional[Any]					=		trimmed_name.replace('''network'''    ,       '''meta3D_layers.blocks.''' + layer_index      )
																								if "norm1" in old_name:
																														__SCREAMING_SNAKE_CASE						:     Union[str, Any]					=		trimmed_name.replace('''norm1'''    ,       '''layernorm1'''      )
																								elif "norm2" in old_name:
																														__SCREAMING_SNAKE_CASE						:     Union[str, Any]					=		trimmed_name.replace('''norm2'''    ,       '''layernorm2'''      )
																								elif "fc1" in old_name:
																														__SCREAMING_SNAKE_CASE						:     Union[str, Any]					=		trimmed_name.replace('''fc1'''    ,       '''linear_in'''      )
																								elif "fc2" in old_name:
																														__SCREAMING_SNAKE_CASE						:     Dict					=		trimmed_name.replace('''fc2'''    ,       '''linear_out'''      )

																		__SCREAMING_SNAKE_CASE						:     List[str]					=		'''last_stage.''' + trimmed_name

						elif "network" in old_name and re.search(R'''.\d.'''    ,       lowercase__      ):
												__SCREAMING_SNAKE_CASE						:     List[Any]					=		old_name.replace('''network'''    ,       '''intermediate_stages'''      )

						if "fc" in new_name:
												__SCREAMING_SNAKE_CASE						:     str					=		new_name.replace('''fc'''    ,       '''convolution'''      )
						elif ("norm1" in new_name) and ("layernorm1" not in new_name):
												__SCREAMING_SNAKE_CASE						:     Union[str, Any]					=		new_name.replace('''norm1'''    ,       '''batchnorm_before'''      )
						elif ("norm2" in new_name) and ("layernorm2" not in new_name):
												__SCREAMING_SNAKE_CASE						:     Union[str, Any]					=		new_name.replace('''norm2'''    ,       '''batchnorm_after'''      )
						if "proj" in new_name:
												__SCREAMING_SNAKE_CASE						:     List[Any]					=		new_name.replace('''proj'''    ,       '''projection'''      )
						if "dist_head" in new_name:
												__SCREAMING_SNAKE_CASE						:     Dict					=		new_name.replace('''dist_head'''    ,       '''distillation_classifier'''      )
						elif "head" in new_name:
												__SCREAMING_SNAKE_CASE						:     Tuple					=		new_name.replace('''head'''    ,       '''classifier'''      )
						elif "patch_embed" in new_name:
												__SCREAMING_SNAKE_CASE						:     Optional[Any]					=		'''efficientformer.''' + new_name
						elif new_name == "norm.weight" or new_name == "norm.bias":
												__SCREAMING_SNAKE_CASE						:     int					=		new_name.replace('''norm'''    ,       '''layernorm'''      )
												__SCREAMING_SNAKE_CASE						:     Union[str, Any]					=		'''efficientformer.''' + new_name
						else:
												__SCREAMING_SNAKE_CASE						:     Dict					=		'''efficientformer.encoder.''' + new_name

						return new_name


def        _UpperCamelCase							(      lowercase__    ,       lowercase__      ):
						for key in checkpoint.copy().keys():
												__SCREAMING_SNAKE_CASE						:     Tuple					=		checkpoint.pop(lowercase__      )
												__SCREAMING_SNAKE_CASE						:     Tuple					=		val

						return checkpoint


def        _UpperCamelCase							(      ):
						__SCREAMING_SNAKE_CASE						:     Optional[int]					=		'''http://images.cocodataset.org/val2017/000000039769.jpg'''
						__SCREAMING_SNAKE_CASE						:     Dict					=		Image.open(requests.get(lowercase__    ,       stream=lowercase__      ).raw      )

						return image


def        _UpperCamelCase							(      lowercase__    ,       lowercase__    ,       lowercase__    ,       lowercase__      ):
						__SCREAMING_SNAKE_CASE						:     Tuple					=		torch.load(lowercase__    ,       map_location='''cpu'''      )['''model''']
						__SCREAMING_SNAKE_CASE						:     Optional[Any]					=		EfficientFormerConfig.from_json_file(lowercase__      )
						__SCREAMING_SNAKE_CASE						:     Tuple					=		EfficientFormerForImageClassificationWithTeacher(lowercase__      )
						__SCREAMING_SNAKE_CASE						:     List[Any]					=		'''_'''.join(checkpoint_path.split('''/'''      )[-1].split('''.'''      )[0].split('''_'''      )[:-1]      )

						__SCREAMING_SNAKE_CASE						:     List[str]					=		config.depths[-1] - config.num_metaad_blocks + 1
						__SCREAMING_SNAKE_CASE						:     int					=		convert_torch_checkpoint(lowercase__    ,       lowercase__      )

						model.load_state_dict(lowercase__      )
						model.eval()

						__SCREAMING_SNAKE_CASE						:     Optional[Any]					=		{
						    '''bilinear''': PILImageResampling.BILINEAR,
						    '''bicubic''': PILImageResampling.BICUBIC,
						    '''nearest''': PILImageResampling.NEAREST,
						}

						# prepare image
						__SCREAMING_SNAKE_CASE						:     Optional[Any]					=		prepare_img()
						__SCREAMING_SNAKE_CASE						:     Any					=		256
						__SCREAMING_SNAKE_CASE						:     int					=		224
						__SCREAMING_SNAKE_CASE						:     List[str]					=		EfficientFormerImageProcessor(
						    size={'''shortest_edge''': image_size}    ,       crop_size={'''height''': crop_size, '''width''': crop_size}    ,       resample=pillow_resamplings['''bicubic''']    ,       )
						__SCREAMING_SNAKE_CASE						:     int					=		processor(images=lowercase__    ,       return_tensors='''pt'''      ).pixel_values

						# original processing pipeline
						__SCREAMING_SNAKE_CASE						:     int					=		Compose(
						    [
						        Resize(lowercase__    ,       interpolation=pillow_resamplings['''bicubic''']      ),
						        CenterCrop(lowercase__      ),
						        ToTensor(),
						        Normalize(lowercase__    ,       lowercase__      ),
						    ]      )
						__SCREAMING_SNAKE_CASE						:     List[str]					=		image_transforms(lowercase__      ).unsqueeze(0      )

						assert torch.allclose(lowercase__    ,       lowercase__      )

						__SCREAMING_SNAKE_CASE						:     Optional[Any]					=		model(lowercase__      )
						__SCREAMING_SNAKE_CASE						:     Tuple					=		outputs.logits

						__SCREAMING_SNAKE_CASE						:     Union[str, Any]					=		(1, 1000)

						if "l1" in model_name:
												__SCREAMING_SNAKE_CASE						:     Optional[int]					=		torch.Tensor(
												    [-0.1312, 0.4353, -1.0499, -0.5124, 0.4183, -0.6793, -1.3777, -0.0893, -0.7358, -2.4328]      )
												assert torch.allclose(logits[0, :10]    ,       lowercase__    ,       atol=1e-3      )
												assert logits.shape == expected_shape
						elif "l3" in model_name:
												__SCREAMING_SNAKE_CASE						:     List[Any]					=		torch.Tensor(
												    [-1.3150, -1.5456, -1.2556, -0.8496, -0.7127, -0.7897, -0.9728, -0.3052, 0.3751, -0.3127]      )
												assert torch.allclose(logits[0, :10]    ,       lowercase__    ,       atol=1e-3      )
												assert logits.shape == expected_shape
						elif "l7" in model_name:
												__SCREAMING_SNAKE_CASE						:     str					=		torch.Tensor(
												    [-1.0283, -1.4131, -0.5644, -1.3115, -0.5785, -1.2049, -0.7528, 0.1992, -0.3822, -0.0878]      )
												assert logits.shape == expected_shape
						else:
												raise ValueError(
												    F'''Unknown model checkpoint: {checkpoint_path}. Supported version of efficientformer are l1, l3 and l7'''      )

						# Save Checkpoints
						Path(lowercase__      ).mkdir(exist_ok=lowercase__      )
						model.save_pretrained(lowercase__      )
						print(F'''Checkpoint successfuly converted. Model saved at {pytorch_dump_path}'''      )
						processor.save_pretrained(lowercase__      )
						print(F'''Processor successfuly saved at {pytorch_dump_path}'''      )

						if push_to_hub:
												print('''Pushing model to the hub...'''      )

												model.push_to_hub(
												    repo_id=F'''Bearnardd/{pytorch_dump_path}'''    ,       commit_message='''Add model'''    ,       use_temp_dir=lowercase__    ,       )
												processor.push_to_hub(
												    repo_id=F'''Bearnardd/{pytorch_dump_path}'''    ,       commit_message='''Add image processor'''    ,       use_temp_dir=lowercase__    ,       )


if __name__ == "__main__":
						__lowerCAmelCase	: Optional[Any]  				=argparse.ArgumentParser()
						# Required parameters
						parser.add_argument(
						    '--pytorch_model_path',
						    default=None,
						    type=str,
						    required=True,
						    help='Path to EfficientFormer pytorch checkpoint.',
						)
						parser.add_argument(
						    '--config_file',
						    default=None,
						    type=str,
						    required=True,
						    help='The json file for EfficientFormer model config.',
						)
						parser.add_argument(
						    '--pytorch_dump_path', default=None, type=str, required=True, help='Path to the output PyTorch model.'
						)

						parser.add_argument('--push_to_hub', action='store_true', help='Push model and image processor to the hub')
						parser.add_argument(
						    '--no-push_to_hub',
						    dest='push_to_hub',
						    action='store_false',
						    help='Do not push model and image processor to the hub',
						)
						parser.set_defaults(push_to_hub=True)

						__lowerCAmelCase	: str  				=parser.parse_args()
						convert_efficientformer_checkpoint(
						    checkpoint_path=args.pytorch_model_path,
						    efficientformer_config_file=args.config_file,
						    pytorch_dump_path=args.pytorch_dump_path,
						    push_to_hub=args.push_to_hub,
						)




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






				'''simple docstring'''



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


				def 		__magic_name__(       self			:Optional[Any]	)		->			List[Any]:
										__SCREAMING_SNAKE_CASE						:     Optional[int]					=		ids_tensor([self.batch_size, self.seq_length]			,			self.vocab_size	)

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

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

										__SCREAMING_SNAKE_CASE						:     Optional[int]					=		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			,			)

										__SCREAMING_SNAKE_CASE						:     Any					=		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 		__magic_name__(       self			:Tuple			,			lowerCAmelCase__			:List[Any]			,			lowerCAmelCase__			:List[str]			,			lowerCAmelCase__			:Dict			,			lowerCAmelCase__			:Any			,			*lowerCAmelCase__			:Union[str, Any]	)		->			Any:
										__SCREAMING_SNAKE_CASE						:     Any					=		OpenAIGPTModel(config=lowerCAmelCase__	)
										model.to(lowerCAmelCase__	)
										model.eval()

										__SCREAMING_SNAKE_CASE						:     Dict					=		model(lowerCAmelCase__			,			token_type_ids=lowerCAmelCase__			,			head_mask=lowerCAmelCase__	)
										__SCREAMING_SNAKE_CASE						:     Tuple					=		model(lowerCAmelCase__			,			token_type_ids=lowerCAmelCase__	)
										__SCREAMING_SNAKE_CASE						:     str					=		model(lowerCAmelCase__	)

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


				def 		__magic_name__(       self			:Optional[Any]			,			lowerCAmelCase__			:Union[str, Any]			,			lowerCAmelCase__			:Union[str, Any]			,			lowerCAmelCase__			:Any			,			lowerCAmelCase__			:Dict			,			*lowerCAmelCase__			:List[Any]	)		->			Dict:
										__SCREAMING_SNAKE_CASE						:     Optional[Any]					=		OpenAIGPTLMHeadModel(lowerCAmelCase__	)
										model.to(lowerCAmelCase__	)
										model.eval()

										__SCREAMING_SNAKE_CASE						:     Tuple					=		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 		__magic_name__(       self			:Tuple			,			lowerCAmelCase__			:Dict			,			lowerCAmelCase__			:str			,			lowerCAmelCase__			:Optional[Any]			,			lowerCAmelCase__			:List[str]			,			*lowerCAmelCase__			:Optional[Any]	)		->			Any:
										__SCREAMING_SNAKE_CASE						:     Any					=		OpenAIGPTDoubleHeadsModel(lowerCAmelCase__	)
										model.to(lowerCAmelCase__	)
										model.eval()

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

										__SCREAMING_SNAKE_CASE						:     List[str]					=		ids_tensor([self.batch_size]			,			self.type_sequence_label_size	)
										__SCREAMING_SNAKE_CASE						:     Optional[Any]					=		model(lowerCAmelCase__			,			token_type_ids=lowerCAmelCase__			,			labels=lowerCAmelCase__	)
										self.parent.assertEqual(result.logits.shape			,			(self.batch_size, self.num_labels)	)



				def 		__magic_name__(       self			:Optional[Any]	)		->			str:
										__SCREAMING_SNAKE_CASE						:     str					=		self.prepare_config_and_inputs()
										(
										    (
										    __SCREAMING_SNAKE_CASE
										)			,			(
										    __SCREAMING_SNAKE_CASE
										)			,			(
										    __SCREAMING_SNAKE_CASE
										)			,			(
										    __SCREAMING_SNAKE_CASE
										)			,			(
										    __SCREAMING_SNAKE_CASE
										)			,			(
										    __SCREAMING_SNAKE_CASE
										)			,			(
										    __SCREAMING_SNAKE_CASE
										)			,			
										)						:     List[str]					=		config_and_inputs
										__SCREAMING_SNAKE_CASE						:     List[str]					=		{
										    '''input_ids''': input_ids,
										    '''token_type_ids''': token_type_ids,
										    '''head_mask''': head_mask,
										}

										return config, inputs_dict




@require_torch
class        _lowercase      (			A__						,			A__						,			A__						,			unittest.TestCase       ):






				'''simple docstring'''



				SCREAMING_SNAKE_CASE__				:			int						=		(
				    (OpenAIGPTModel, OpenAIGPTLMHeadModel, OpenAIGPTDoubleHeadsModel, OpenAIGPTForSequenceClassification)
				    if is_torch_available()
				    else ()
				)
				SCREAMING_SNAKE_CASE__				:			str						=		(
				    (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 		__magic_name__(       self			:Optional[int]			,			lowerCAmelCase__			:str			,			lowerCAmelCase__			:List[Any]			,			lowerCAmelCase__			:List[Any]			,			lowerCAmelCase__			:List[Any]			,			lowerCAmelCase__			:Union[str, Any]	)		->			Tuple:
										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 		__magic_name__(       self			:List[str]			,			lowerCAmelCase__			:Dict			,			lowerCAmelCase__			:int			,			lowerCAmelCase__			:int=False	)		->			Dict:
										__SCREAMING_SNAKE_CASE						:     Tuple					=		super()._prepare_for_class(lowerCAmelCase__			,			lowerCAmelCase__			,			return_labels=lowerCAmelCase__	)

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


				def 		__magic_name__(       self			:Optional[int]	)		->			Union[str, Any]:
										__SCREAMING_SNAKE_CASE						:     int					=		OpenAIGPTModelTester(self	)
										__SCREAMING_SNAKE_CASE						:     Optional[Any]					=		ConfigTester(self			,			config_class=lowerCAmelCase__			,			n_embd=37	)


				def 		__magic_name__(       self			:Any	)		->			Optional[Any]:
										self.config_tester.run_common_tests()


				def 		__magic_name__(       self			:List[str]	)		->			Union[str, Any]:
										__SCREAMING_SNAKE_CASE						:     Tuple					=		self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_openai_gpt_model(*lowerCAmelCase__	)


				def 		__magic_name__(       self			:int	)		->			int:
										__SCREAMING_SNAKE_CASE						:     Tuple					=		self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_lm_head_model(*lowerCAmelCase__	)


				def 		__magic_name__(       self			:List[str]	)		->			Dict:
										__SCREAMING_SNAKE_CASE						:     List[str]					=		self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_double_lm_head_model(*lowerCAmelCase__	)


				def 		__magic_name__(       self			:List[str]	)		->			str:
										__SCREAMING_SNAKE_CASE						:     List[Any]					=		self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_openai_gpt_for_sequence_classification(*lowerCAmelCase__	)



				@slow
				def 		__magic_name__(       self			:Any	)		->			List[Any]:
										for model_name in OPENAI_GPT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																__SCREAMING_SNAKE_CASE						:     Dict					=		OpenAIGPTModel.from_pretrained(lowerCAmelCase__	)
																self.assertIsNotNone(lowerCAmelCase__	)




@require_torch
class        _lowercase      (			unittest.TestCase       ):






				'''simple docstring'''



				@slow
				def 		__magic_name__(       self			:Union[str, Any]	)		->			Optional[int]:
										__SCREAMING_SNAKE_CASE						:     List[str]					=		OpenAIGPTLMHeadModel.from_pretrained('''openai-gpt'''	)
										model.to(lowerCAmelCase__	)
										__SCREAMING_SNAKE_CASE						:     Optional[int]					=		torch.tensor([[481, 4_735, 544]]			,			dtype=torch.long			,			device=lowerCAmelCase__	)  # the president is
										__SCREAMING_SNAKE_CASE						:     Dict					=		[
										    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

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