

import argparse
import json
import os

import numpy as np
import PIL
import requests
import tensorflow.keras.applications.efficientnet as efficientnet
import torch
from huggingface_hub import hf_hub_download
from PIL import Image
from tensorflow.keras.preprocessing import image

from transformers import (
    EfficientNetConfig,
    EfficientNetForImageClassification,
    EfficientNetImageProcessor,
)
from transformers.utils import logging


logging.set_verbosity_info()
_a		       =   logging.get_logger(__name__)

_a		       =   {
    "b0": efficientnet.EfficientNetBa,
    "b1": efficientnet.EfficientNetBa,
    "b2": efficientnet.EfficientNetBa,
    "b3": efficientnet.EfficientNetBa,
    "b4": efficientnet.EfficientNetBa,
    "b5": efficientnet.EfficientNetBa,
    "b6": efficientnet.EfficientNetBa,
    "b7": efficientnet.EfficientNetBa,
}

_a		       =   {
    "b0": {
        "hidden_dim": 1_2_8_0,
        "width_coef": 1.0,
        "depth_coef": 1.0,
        "image_size": 2_2_4,
        "dropout_rate": 0.2,
        "dw_padding": [],
    },
    "b1": {
        "hidden_dim": 1_2_8_0,
        "width_coef": 1.0,
        "depth_coef": 1.1,
        "image_size": 2_4_0,
        "dropout_rate": 0.2,
        "dw_padding": [1_6],
    },
    "b2": {
        "hidden_dim": 1_4_0_8,
        "width_coef": 1.1,
        "depth_coef": 1.2,
        "image_size": 2_6_0,
        "dropout_rate": 0.3,
        "dw_padding": [5, 8, 1_6],
    },
    "b3": {
        "hidden_dim": 1_5_3_6,
        "width_coef": 1.2,
        "depth_coef": 1.4,
        "image_size": 3_0_0,
        "dropout_rate": 0.3,
        "dw_padding": [5, 1_8],
    },
    "b4": {
        "hidden_dim": 1_7_9_2,
        "width_coef": 1.4,
        "depth_coef": 1.8,
        "image_size": 3_8_0,
        "dropout_rate": 0.4,
        "dw_padding": [6],
    },
    "b5": {
        "hidden_dim": 2_0_4_8,
        "width_coef": 1.6,
        "depth_coef": 2.2,
        "image_size": 4_5_6,
        "dropout_rate": 0.4,
        "dw_padding": [1_3, 2_7],
    },
    "b6": {
        "hidden_dim": 2_3_0_4,
        "width_coef": 1.8,
        "depth_coef": 2.6,
        "image_size": 5_2_8,
        "dropout_rate": 0.5,
        "dw_padding": [3_1],
    },
    "b7": {
        "hidden_dim": 2_5_6_0,
        "width_coef": 2.0,
        "depth_coef": 3.1,
        "image_size": 6_0_0,
        "dropout_rate": 0.5,
        "dw_padding": [1_8],
    },
}







def _a (       SCREAMING_SNAKE_CASE		:			Union[str, Any]		)    ->    Tuple:

 """simple docstring"""






 __lowerCAmelCase:   List[Any]   	=      EfficientNetConfig()
 __lowerCAmelCase:   List[str]   	=      CONFIG_MAP[model_name]['hidden_dim']
 __lowerCAmelCase:   List[str]   	=      CONFIG_MAP[model_name]['width_coef']
 __lowerCAmelCase:   str   	=      CONFIG_MAP[model_name]['depth_coef']
 __lowerCAmelCase:   List[Any]   	=      CONFIG_MAP[model_name]['image_size']
 __lowerCAmelCase:   int   	=      CONFIG_MAP[model_name]['dropout_rate']
 __lowerCAmelCase:   List[Any]   	=      CONFIG_MAP[model_name]['dw_padding']

 __lowerCAmelCase:   List[str]   	=      'huggingface/label-files'
 __lowerCAmelCase:   Tuple   	=      'imagenet-1k-id2label.json'
 __lowerCAmelCase:   int   	=      10_00
 __lowerCAmelCase:   List[Any]   	=      json.load(open(hf_hub_download(SCREAMING_SNAKE_CASE    ,					SCREAMING_SNAKE_CASE    ,					repo_type='dataset'		)    ,					'r'		)		)
 __lowerCAmelCase:   Optional[int]   	=      {int(SCREAMING_SNAKE_CASE		): v for k, v in idalabel.items()}

 __lowerCAmelCase:   List[str]   	=      idalabel
 __lowerCAmelCase:   str   	=      {v: k for k, v in idalabel.items()}
 return config







def _a (       )    ->    Dict:

 """simple docstring"""






 __lowerCAmelCase:   List[Any]   	=      'http://images.cocodataset.org/val2017/000000039769.jpg'
 __lowerCAmelCase:   Any   	=      Image.open(requests.get(SCREAMING_SNAKE_CASE    ,					stream=SCREAMING_SNAKE_CASE		).raw		)
 return im







def _a (       SCREAMING_SNAKE_CASE		:			Any		)    ->    List[str]:

 """simple docstring"""






 __lowerCAmelCase:   int   	=      CONFIG_MAP[model_name]['image_size']
 __lowerCAmelCase:   Any   	=      EfficientNetImageProcessor(
     size={'height': size, 'width': size}    ,					image_mean=[0.4_8_5, 0.4_5_6, 0.4_0_6]    ,					image_std=[0.4_7_8_5_3_9_4_4, 0.4_7_3_2_8_6_4, 0.4_7_4_3_4_1_6_3]    ,					do_center_crop=SCREAMING_SNAKE_CASE    ,					)
 return preprocessor







def _a (       SCREAMING_SNAKE_CASE		:			Optional[int]		)    ->    List[str]:

 """simple docstring"""






 __lowerCAmelCase:   Any   	=      [v.split('_'		)[0].split('block'		)[1] for v in original_param_names if v.startswith('block'		)]
 __lowerCAmelCase:   str   	=      sorted(set(SCREAMING_SNAKE_CASE		)		)
 __lowerCAmelCase:   List[Any]   	=      len(SCREAMING_SNAKE_CASE		)
 __lowerCAmelCase:   Optional[int]   	=      {b: str(SCREAMING_SNAKE_CASE		) for b, i in zip(SCREAMING_SNAKE_CASE    ,					range(SCREAMING_SNAKE_CASE		)		)}

 __lowerCAmelCase:   Tuple   	=      []
 rename_keys.append(('stem_conv/kernel:0', 'embeddings.convolution.weight')		)
 rename_keys.append(('stem_bn/gamma:0', 'embeddings.batchnorm.weight')		)
 rename_keys.append(('stem_bn/beta:0', 'embeddings.batchnorm.bias')		)
 rename_keys.append(('stem_bn/moving_mean:0', 'embeddings.batchnorm.running_mean')		)
 rename_keys.append(('stem_bn/moving_variance:0', 'embeddings.batchnorm.running_var')		)

 for b in block_names:
  __lowerCAmelCase:   List[Any]   	=      block_name_mapping[b]
  rename_keys.append((f'''block{b}_expand_conv/kernel:0''', f'''encoder.blocks.{hf_b}.expansion.expand_conv.weight''')		)
  rename_keys.append((f'''block{b}_expand_bn/gamma:0''', f'''encoder.blocks.{hf_b}.expansion.expand_bn.weight''')		)
  rename_keys.append((f'''block{b}_expand_bn/beta:0''', f'''encoder.blocks.{hf_b}.expansion.expand_bn.bias''')		)
  rename_keys.append(
      (f'''block{b}_expand_bn/moving_mean:0''', f'''encoder.blocks.{hf_b}.expansion.expand_bn.running_mean''')		)
  rename_keys.append(
      (f'''block{b}_expand_bn/moving_variance:0''', f'''encoder.blocks.{hf_b}.expansion.expand_bn.running_var''')		)
  rename_keys.append(
      (f'''block{b}_dwconv/depthwise_kernel:0''', f'''encoder.blocks.{hf_b}.depthwise_conv.depthwise_conv.weight''')		)
  rename_keys.append((f'''block{b}_bn/gamma:0''', f'''encoder.blocks.{hf_b}.depthwise_conv.depthwise_norm.weight''')		)
  rename_keys.append((f'''block{b}_bn/beta:0''', f'''encoder.blocks.{hf_b}.depthwise_conv.depthwise_norm.bias''')		)
  rename_keys.append(
      (f'''block{b}_bn/moving_mean:0''', f'''encoder.blocks.{hf_b}.depthwise_conv.depthwise_norm.running_mean''')		)
  rename_keys.append(
      (f'''block{b}_bn/moving_variance:0''', f'''encoder.blocks.{hf_b}.depthwise_conv.depthwise_norm.running_var''')		)

  rename_keys.append((f'''block{b}_se_reduce/kernel:0''', f'''encoder.blocks.{hf_b}.squeeze_excite.reduce.weight''')		)
  rename_keys.append((f'''block{b}_se_reduce/bias:0''', f'''encoder.blocks.{hf_b}.squeeze_excite.reduce.bias''')		)
  rename_keys.append((f'''block{b}_se_expand/kernel:0''', f'''encoder.blocks.{hf_b}.squeeze_excite.expand.weight''')		)
  rename_keys.append((f'''block{b}_se_expand/bias:0''', f'''encoder.blocks.{hf_b}.squeeze_excite.expand.bias''')		)
  rename_keys.append(
      (f'''block{b}_project_conv/kernel:0''', f'''encoder.blocks.{hf_b}.projection.project_conv.weight''')		)
  rename_keys.append((f'''block{b}_project_bn/gamma:0''', f'''encoder.blocks.{hf_b}.projection.project_bn.weight''')		)
  rename_keys.append((f'''block{b}_project_bn/beta:0''', f'''encoder.blocks.{hf_b}.projection.project_bn.bias''')		)
  rename_keys.append(
      (f'''block{b}_project_bn/moving_mean:0''', f'''encoder.blocks.{hf_b}.projection.project_bn.running_mean''')		)
  rename_keys.append(
      (f'''block{b}_project_bn/moving_variance:0''', f'''encoder.blocks.{hf_b}.projection.project_bn.running_var''')		)

 rename_keys.append(('top_conv/kernel:0', 'encoder.top_conv.weight')		)
 rename_keys.append(('top_bn/gamma:0', 'encoder.top_bn.weight')		)
 rename_keys.append(('top_bn/beta:0', 'encoder.top_bn.bias')		)
 rename_keys.append(('top_bn/moving_mean:0', 'encoder.top_bn.running_mean')		)
 rename_keys.append(('top_bn/moving_variance:0', 'encoder.top_bn.running_var')		)

 __lowerCAmelCase:   Optional[int]   	=      {}
 for item in rename_keys:
  if item[0] in original_param_names:
   __lowerCAmelCase:   Dict   	=      'efficientnet.' + item[1]

 __lowerCAmelCase:   List[str]   	=      'classifier.weight'
 __lowerCAmelCase:   List[Any]   	=      'classifier.bias'
 return key_mapping







def _a (       SCREAMING_SNAKE_CASE		:			Tuple    ,					SCREAMING_SNAKE_CASE		:			Any    ,					SCREAMING_SNAKE_CASE		:			List[Any]		)    ->    Optional[Any]:

 """simple docstring"""






 for key, value in tf_params.items():
  if "normalization" in key:
   continue

  __lowerCAmelCase:   Optional[Any]   	=      key_mapping[key]
  if "_conv" in key and "kernel" in key:
   __lowerCAmelCase:   Optional[int]   	=      torch.from_numpy(SCREAMING_SNAKE_CASE		).permute(3    ,					2    ,					0    ,					1		)
  elif "depthwise_kernel" in key:
   __lowerCAmelCase:   List[str]   	=      torch.from_numpy(SCREAMING_SNAKE_CASE		).permute(2    ,					3    ,					0    ,					1		)
  elif "kernel" in key:
   __lowerCAmelCase:   List[Any]   	=      torch.from_numpy(np.transpose(SCREAMING_SNAKE_CASE		)		)
  else:
   __lowerCAmelCase:   Any   	=      torch.from_numpy(SCREAMING_SNAKE_CASE		)

  # Replace HF parameters with original TF model parameters
  assert hf_params[hf_key].shape == new_hf_value.shape
  hf_params[hf_key].copy_(SCREAMING_SNAKE_CASE		)







@torch.no_grad()
def _a (       SCREAMING_SNAKE_CASE		:			Union[str, Any]    ,					SCREAMING_SNAKE_CASE		:			str    ,					SCREAMING_SNAKE_CASE		:			Dict    ,					SCREAMING_SNAKE_CASE		:			Dict		)    ->    str:

 """simple docstring"""






 __lowerCAmelCase:   Tuple   	=      model_classes[model_name](
     include_top=SCREAMING_SNAKE_CASE    ,					weights='imagenet'    ,					input_tensor=SCREAMING_SNAKE_CASE    ,					input_shape=SCREAMING_SNAKE_CASE    ,					pooling=SCREAMING_SNAKE_CASE    ,					classes=10_00    ,					classifier_activation='softmax'    ,					)

 __lowerCAmelCase:   Optional[Any]   	=      original_model.trainable_variables
 __lowerCAmelCase:   Optional[Any]   	=      original_model.non_trainable_variables
 __lowerCAmelCase:   List[Any]   	=      {param.name: param.numpy() for param in tf_params}
 for param in tf_non_train_params:
  __lowerCAmelCase:   Optional[Any]   	=      param.numpy()
 __lowerCAmelCase:   Optional[int]   	=      list(tf_params.keys()		)

 # Load HuggingFace model
 __lowerCAmelCase:   int   	=      get_efficientnet_config(SCREAMING_SNAKE_CASE		)
 __lowerCAmelCase:   Optional[Any]   	=      EfficientNetForImageClassification(SCREAMING_SNAKE_CASE		).eval()
 __lowerCAmelCase:   Union[str, Any]   	=      hf_model.state_dict()

 # Create src-to-dst parameter name mapping dictionary
 print('Converting parameters...'		)
 __lowerCAmelCase:   Optional[int]   	=      rename_keys(SCREAMING_SNAKE_CASE		)
 replace_params(SCREAMING_SNAKE_CASE    ,					SCREAMING_SNAKE_CASE    ,					SCREAMING_SNAKE_CASE		)

 # Initialize preprocessor and preprocess input image
 __lowerCAmelCase:   Optional[int]   	=      convert_image_processor(SCREAMING_SNAKE_CASE		)
 __lowerCAmelCase:   Any   	=      preprocessor(images=prepare_img()    ,					return_tensors='pt'		)

 # HF model inference
 hf_model.eval()
 with torch.no_grad():
  __lowerCAmelCase:   Union[str, Any]   	=      hf_model(**SCREAMING_SNAKE_CASE		)
 __lowerCAmelCase:   List[str]   	=      outputs.logits.detach().numpy()

 # Original model inference
 __lowerCAmelCase:   Union[str, Any]   	=      False
 __lowerCAmelCase:   Optional[Any]   	=      CONFIG_MAP[model_name]['image_size']
 __lowerCAmelCase:   int   	=      prepare_img().resize((image_size, image_size)    ,					resample=PIL.Image.NEAREST		)
 __lowerCAmelCase:   int   	=      image.img_to_array(SCREAMING_SNAKE_CASE		)
 __lowerCAmelCase:   int   	=      np.expand_dims(SCREAMING_SNAKE_CASE    ,					axis=0		)
 __lowerCAmelCase:   Optional[Any]   	=      original_model.predict(SCREAMING_SNAKE_CASE		)

 # Check whether original and HF model outputs match  -> np.allclose
 assert np.allclose(SCREAMING_SNAKE_CASE    ,					SCREAMING_SNAKE_CASE    ,					atol=1E-3		), "The predicted logits are not the same."
 print('Model outputs match!'		)

 if save_model:
  # Create folder to save model
  if not os.path.isdir(SCREAMING_SNAKE_CASE		):
   os.mkdir(SCREAMING_SNAKE_CASE		)
  # Save converted model and image processor
  hf_model.save_pretrained(SCREAMING_SNAKE_CASE		)
  preprocessor.save_pretrained(SCREAMING_SNAKE_CASE		)

 if push_to_hub:
  # Push model and image processor to hub
  print(f'''Pushing converted {model_name} to the hub...'''		)
  __lowerCAmelCase:   List[str]   	=      f'''efficientnet-{model_name}'''
  preprocessor.push_to_hub(SCREAMING_SNAKE_CASE		)
  hf_model.push_to_hub(SCREAMING_SNAKE_CASE		)


if __name__ == "__main__":
  _a		       =   argparse.ArgumentParser()
  # Required parameters
  parser.add_argument(
      '''--model_name''',
      default='''b0''',
      type=str,
      help='''Version name of the EfficientNet model you want to convert, select from [b0, b1, b2, b3, b4, b5, b6, b7].''',
  )
  parser.add_argument(
      '''--pytorch_dump_folder_path''',
      default='''hf_model''',
      type=str,
      help='''Path to the output PyTorch model directory.''',
  )
  parser.add_argument('''--save_model''', action='''store_true''', help='''Save model to local''')
  parser.add_argument('''--push_to_hub''', action='''store_true''', help='''Push model and image processor to the hub''')

  _a		       =   parser.parse_args()
  convert_efficientnet_checkpoint(args.model_name, args.pytorch_dump_folder_path, args.save_model, 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 					snake_case__						:






     """simple docstring"""


     def __init__(       self  :  List[str], _snake_case  :  Any, _snake_case  :  int=1_3, _snake_case  :  Optional[int]=7, _snake_case  :  int=True, _snake_case  :  Optional[Any]=True, _snake_case  :  Optional[Any]=True, _snake_case  :  Union[str, Any]=9_9, _snake_case  :  Optional[Any]=3_2, _snake_case  :  Tuple=5, _snake_case  :  str=4, _snake_case  :  Any=3_7, _snake_case  :  int="gelu", _snake_case  :  Optional[Any]=0.1, _snake_case  :  str=0.1, _snake_case  :  str=5_1_2, _snake_case  :  Dict=1_6, _snake_case  :  str=2, _snake_case  :  Union[str, Any]=0.0_2, _snake_case  :  Optional[int]=3, _snake_case  :  Union[str, Any]=4, _snake_case  :  Tuple=None, )   ->Optional[Any]:
            snake_case__							:	Optional[int]   	=	parent
            snake_case__							:	List[Any]   	=	batch_size
            snake_case__							:	Tuple   	=	seq_length
            snake_case__							:	str   	=	is_training
            snake_case__							:	Optional[int]   	=	use_token_type_ids
            snake_case__							:	Any   	=	use_labels
            snake_case__							:	Dict   	=	vocab_size
            snake_case__							:	str   	=	hidden_size
            snake_case__							:	Union[str, Any]   	=	num_hidden_layers
            snake_case__							:	List[str]   	=	num_attention_heads
            snake_case__							:	Union[str, Any]   	=	intermediate_size
            snake_case__							:	List[Any]   	=	hidden_act
            snake_case__							:	int   	=	hidden_dropout_prob
            snake_case__							:	str   	=	attention_probs_dropout_prob
            snake_case__							:	Any   	=	max_position_embeddings
            snake_case__							:	Union[str, Any]   	=	type_vocab_size
            snake_case__							:	Optional[Any]   	=	type_sequence_label_size
            snake_case__							:	Optional[int]   	=	initializer_range
            snake_case__							:	Optional[int]   	=	num_labels
            snake_case__							:	str   	=	num_choices
            snake_case__							:	int   	=	scope
            snake_case__							:	List[str]   	=	self.vocab_size - 1






     def    lowercase_   (       self  :  Union[str, Any]					)   ->Tuple:
            snake_case__							:	List[str]   	=	ids_tensor([self.batch_size, self.seq_length], self.vocab_size					)

            snake_case__							:	List[str]   	=	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__							:	Tuple   	=	None
            snake_case__							:	str   	=	None
            snake_case__							:	List[Any]   	=	None
            if self.use_labels:
                   snake_case__							:	Dict   	=	ids_tensor([self.batch_size], self.type_sequence_label_size					)
                   snake_case__							:	int   	=	ids_tensor([self.batch_size, self.seq_length], self.num_labels					)
                   snake_case__							:	List[str]   	=	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__							:	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  :  Any, _snake_case  :  List[str], _snake_case  :  Any, _snake_case  :  List[Any], _snake_case  :  Tuple, *_snake_case  :  Optional[Any]					)   ->Tuple:
            snake_case__							:	Union[str, Any]   	=	OpenAIGPTModel(config=_snake_case					)
            model.to(_snake_case					)
            model.eval()

            snake_case__							:	Optional[Any]   	=	model(_snake_case, token_type_ids=_snake_case, head_mask=_snake_case					)
            snake_case__							:	Union[str, Any]   	=	model(_snake_case, token_type_ids=_snake_case					)
            snake_case__							:	Optional[Any]   	=	model(_snake_case					)

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






     def    lowercase_   (       self  :  Optional[int], _snake_case  :  Optional[Any], _snake_case  :  Union[str, Any], _snake_case  :  Optional[int], _snake_case  :  List[Any], *_snake_case  :  Dict					)   ->Optional[int]:
            snake_case__							:	Optional[Any]   	=	OpenAIGPTLMHeadModel(_snake_case					)
            model.to(_snake_case					)
            model.eval()

            snake_case__							:	Tuple   	=	model(_snake_case, token_type_ids=_snake_case, labels=_snake_case					)
            self.parent.assertEqual(result.loss.shape, ()					)
            self.parent.assertEqual(result.logits.shape, (self.batch_size, self.seq_length, self.vocab_size)					)






     def    lowercase_   (       self  :  int, _snake_case  :  Tuple, _snake_case  :  List[str], _snake_case  :  List[Any], _snake_case  :  List[Any], *_snake_case  :  List[Any]					)   ->Optional[int]:
            snake_case__							:	List[str]   	=	OpenAIGPTDoubleHeadsModel(_snake_case					)
            model.to(_snake_case					)
            model.eval()

            snake_case__							:	Optional[Any]   	=	model(_snake_case, token_type_ids=_snake_case, labels=_snake_case					)
            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], _snake_case  :  Tuple, _snake_case  :  Dict, _snake_case  :  List[str], _snake_case  :  Optional[Any], *_snake_case  :  Union[str, Any]					)   ->str:
            snake_case__							:	List[str]   	=	self.num_labels
            snake_case__							:	Dict   	=	OpenAIGPTForSequenceClassification(_snake_case					)
            model.to(_snake_case					)
            model.eval()

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






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

            return config, inputs_dict




@require_torch
class 					snake_case__						(       lowerCAmelCase_				,     lowerCAmelCase_				,     lowerCAmelCase_				,     unittest.TestCase     ):






     """simple docstring"""


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






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

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






     def    lowercase_   (       self  :  Union[str, Any]					)   ->List[str]:
            snake_case__							:	List[str]   	=	OpenAIGPTModelTester(self					)
            snake_case__							:	Any   	=	ConfigTester(self, config_class=_snake_case, n_embd=3_7					)






     def    lowercase_   (       self  :  Optional[int]					)   ->str:
            self.config_tester.run_common_tests()






     def    lowercase_   (       self  :  int					)   ->Tuple:
            snake_case__							:	str   	=	self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_openai_gpt_model(*_snake_case					)






     def    lowercase_   (       self  :  Tuple					)   ->List[str]:
            snake_case__							:	Optional[int]   	=	self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_lm_head_model(*_snake_case					)






     def    lowercase_   (       self  :  Dict					)   ->int:
            snake_case__							:	Optional[Any]   	=	self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_double_lm_head_model(*_snake_case					)






     def    lowercase_   (       self  :  int					)   ->str:
            snake_case__							:	List[Any]   	=	self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_openai_gpt_for_sequence_classification(*_snake_case					)






     @slow
     def    lowercase_   (       self  :  Optional[Any]					)   ->str:
            for model_name in OPENAI_GPT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
                   snake_case__							:	Optional[int]   	=	OpenAIGPTModel.from_pretrained(_snake_case					)
                   self.assertIsNotNone(_snake_case					)




@require_torch
class 					snake_case__						(       unittest.TestCase     ):






     """simple docstring"""


     @slow
     def    lowercase_   (       self  :  Tuple					)   ->Optional[int]:
            snake_case__							:	Union[str, Any]   	=	OpenAIGPTLMHeadModel.from_pretrained('openai-gpt'					)
            model.to(_snake_case					)
            snake_case__							:	Tuple   	=	torch.tensor([[4_8_1, 4_7_3_5, 5_4_4]], dtype=torch.long, device=_snake_case					)  # the president is
            snake_case__							:	int   	=	[
                4_8_1,
                4_7_3_5,
                5_4_4,
                2_4_6,
                9_6_3,
                8_7_0,
                7_6_2,
                2_3_9,
                2_4_4,
                4_0_4_7_7,
                2_4_4,
                2_4_9,
                7_1_9,
                8_8_1,
                4_8_7,
                5_4_4,
                2_4_0,
                2_4_4,
                6_0_3,
                4_8_1,
            ]  # the president is a very good man. " \n " i\'m sure he is, " said the

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






