



import argparse
import json
from pathlib import Path

import requests
import torch
from huggingface_hub import hf_hub_download
from PIL import Image

from transformers import (
    BertTokenizer,
    ViltConfig,
    ViltForImageAndTextRetrieval,
    ViltForImagesAndTextClassification,
    ViltForMaskedLM,
    ViltForQuestionAnswering,
    ViltImageProcessor,
    ViltProcessor,
)
from transformers.utils import logging


logging.set_verbosity_info()
a_   :str        =      logging.get_logger(__name__)
def        lowercase_							(A : Dict		,					A : int=False		,					A : int=False		,					A : int=False      ):
       snake_case__							:	Optional[Any]   	=	[]
       for i in range(config.num_hidden_layers      ):
              # encoder layers: output projection, 2 feedforward neural networks and 2 layernorms
              rename_keys.append((F'''transformer.blocks.{i}.norm1.weight''', F'''vilt.encoder.layer.{i}.layernorm_before.weight''')      )
              rename_keys.append((F'''transformer.blocks.{i}.norm1.bias''', F'''vilt.encoder.layer.{i}.layernorm_before.bias''')      )
              rename_keys.append(
                  (F'''transformer.blocks.{i}.attn.proj.weight''', F'''vilt.encoder.layer.{i}.attention.output.dense.weight''')      )
              rename_keys.append(
                  (F'''transformer.blocks.{i}.attn.proj.bias''', F'''vilt.encoder.layer.{i}.attention.output.dense.bias''')      )
              rename_keys.append((F'''transformer.blocks.{i}.norm2.weight''', F'''vilt.encoder.layer.{i}.layernorm_after.weight''')      )
              rename_keys.append((F'''transformer.blocks.{i}.norm2.bias''', F'''vilt.encoder.layer.{i}.layernorm_after.bias''')      )
              rename_keys.append(
                  (F'''transformer.blocks.{i}.mlp.fc1.weight''', F'''vilt.encoder.layer.{i}.intermediate.dense.weight''')      )
              rename_keys.append((F'''transformer.blocks.{i}.mlp.fc1.bias''', F'''vilt.encoder.layer.{i}.intermediate.dense.bias''')      )
              rename_keys.append((F'''transformer.blocks.{i}.mlp.fc2.weight''', F'''vilt.encoder.layer.{i}.output.dense.weight''')      )
              rename_keys.append((F'''transformer.blocks.{i}.mlp.fc2.bias''', F'''vilt.encoder.layer.{i}.output.dense.bias''')      )

       # embeddings
       rename_keys.extend(
           [
               # text embeddings
               ('text_embeddings.word_embeddings.weight', 'vilt.embeddings.text_embeddings.word_embeddings.weight'),
               (
                   'text_embeddings.position_embeddings.weight',
                   'vilt.embeddings.text_embeddings.position_embeddings.weight',
               ),
               ('text_embeddings.position_ids', 'vilt.embeddings.text_embeddings.position_ids'),
               (
                   'text_embeddings.token_type_embeddings.weight',
                   'vilt.embeddings.text_embeddings.token_type_embeddings.weight',
               ),
               ('text_embeddings.LayerNorm.weight', 'vilt.embeddings.text_embeddings.LayerNorm.weight'),
               ('text_embeddings.LayerNorm.bias', 'vilt.embeddings.text_embeddings.LayerNorm.bias'),
               # patch embeddings
               ('transformer.cls_token', 'vilt.embeddings.cls_token'),
               ('transformer.patch_embed.proj.weight', 'vilt.embeddings.patch_embeddings.projection.weight'),
               ('transformer.patch_embed.proj.bias', 'vilt.embeddings.patch_embeddings.projection.bias'),
               ('transformer.pos_embed', 'vilt.embeddings.position_embeddings'),
               # token type embeddings
               ('token_type_embeddings.weight', 'vilt.embeddings.token_type_embeddings.weight'),
           ]      )

       # final layernorm + pooler
       rename_keys.extend(
           [
               ('transformer.norm.weight', 'vilt.layernorm.weight'),
               ('transformer.norm.bias', 'vilt.layernorm.bias'),
               ('pooler.dense.weight', 'vilt.pooler.dense.weight'),
               ('pooler.dense.bias', 'vilt.pooler.dense.bias'),
           ]      )

       # classifier head(s)
       if vqa_model:
              # classification head
              rename_keys.extend(
                  [
                      ('vqa_classifier.0.weight', 'classifier.0.weight'),
                      ('vqa_classifier.0.bias', 'classifier.0.bias'),
                      ('vqa_classifier.1.weight', 'classifier.1.weight'),
                      ('vqa_classifier.1.bias', 'classifier.1.bias'),
                      ('vqa_classifier.3.weight', 'classifier.3.weight'),
                      ('vqa_classifier.3.bias', 'classifier.3.bias'),
                  ]      )
       elif nlvr_model:
              # classification head
              rename_keys.extend(
                  [
                      ('nlvr2_classifier.0.weight', 'classifier.0.weight'),
                      ('nlvr2_classifier.0.bias', 'classifier.0.bias'),
                      ('nlvr2_classifier.1.weight', 'classifier.1.weight'),
                      ('nlvr2_classifier.1.bias', 'classifier.1.bias'),
                      ('nlvr2_classifier.3.weight', 'classifier.3.weight'),
                      ('nlvr2_classifier.3.bias', 'classifier.3.bias'),
                  ]      )
       else:
              pass

       return rename_keys
def        lowercase_							(A : List[str]		,					A : List[Any]      ):
       for i in range(config.num_hidden_layers      ):
              snake_case__							:	Union[str, Any]   	=	'vilt.'
              # read in weights + bias of input projection layer (in timm, this is a single matrix + bias)
              snake_case__							:	int   	=	state_dict.pop(F'''transformer.blocks.{i}.attn.qkv.weight'''      )
              snake_case__							:	int   	=	state_dict.pop(F'''transformer.blocks.{i}.attn.qkv.bias'''      )
              # next, add query, keys and values (in that order) to the state dict
              snake_case__							:	Optional[Any]   	=	in_proj_weight[
                  : config.hidden_size, :
              ]
              snake_case__							:	Tuple   	=	in_proj_bias[: config.hidden_size]
              snake_case__							:	Optional[Any]   	=	in_proj_weight[
                  config.hidden_size : config.hidden_size * 2, :
              ]
              snake_case__							:	Tuple   	=	in_proj_bias[
                  config.hidden_size : config.hidden_size * 2
              ]
              snake_case__							:	str   	=	in_proj_weight[
                  -config.hidden_size :, :
              ]
              snake_case__							:	Any   	=	in_proj_bias[-config.hidden_size :]
def        lowercase_							(A : int      ):
       snake_case__							:	Optional[int]   	=	['head.weight', 'head.bias']
       for k in ignore_keys:
              state_dict.pop(A		,					A      )
def        lowercase_							(A : int		,					A : List[Any]		,					A : List[str]      ):
       snake_case__							:	Optional[int]   	=	dct.pop(A      )
       snake_case__							:	int   	=	val
@torch.no_grad()
def        lowercase_							(A : int		,					A : Union[str, Any]      ):
       snake_case__							:	Dict   	=	ViltConfig(image_size=3_8_4		,					patch_size=3_2		,					tie_word_embeddings=A      )
       snake_case__							:	Optional[int]   	=	False
       snake_case__							:	str   	=	False
       snake_case__							:	Tuple   	=	False
       snake_case__							:	Dict   	=	False
       if "vqa" in checkpoint_url:
              snake_case__							:	Union[str, Any]   	=	True
              snake_case__							:	List[str]   	=	3_1_2_9
              snake_case__							:	str   	=	'huggingface/label-files'
              snake_case__							:	List[str]   	=	'vqa2-id2label.json'
              snake_case__							:	Any   	=	json.load(open(hf_hub_download(A		,					A		,					repo_type='dataset'      )		,					'r'      )      )
              snake_case__							:	Optional[Any]   	=	{int(A      ): v for k, v in idalabel.items()}
              snake_case__							:	str   	=	idalabel
              snake_case__							:	Tuple   	=	{v: k for k, v in idalabel.items()}
              snake_case__							:	Dict   	=	ViltForQuestionAnswering(A      )
       elif "nlvr" in checkpoint_url:
              snake_case__							:	Optional[Any]   	=	True
              snake_case__							:	List[Any]   	=	2
              snake_case__							:	Optional[int]   	=	{0: 'False', 1: 'True'}
              snake_case__							:	str   	=	{v: k for k, v in config.idalabel.items()}
              snake_case__							:	int   	=	3
              snake_case__							:	str   	=	ViltForImagesAndTextClassification(A      )
       elif "irtr" in checkpoint_url:
              snake_case__							:	List[Any]   	=	True
              snake_case__							:	Dict   	=	ViltForImageAndTextRetrieval(A      )
       elif "mlm_itm" in checkpoint_url:
              snake_case__							:	Dict   	=	True
              snake_case__							:	Union[str, Any]   	=	ViltForMaskedLM(A      )
       else:
              raise ValueError('Unknown model type'      )

       # load state_dict of original model, remove and rename some keys
       snake_case__							:	str   	=	torch.hub.load_state_dict_from_url(A		,					map_location='cpu'      )['state_dict']
       snake_case__							:	Optional[Any]   	=	create_rename_keys(A		,					A		,					A		,					A      )
       for src, dest in rename_keys:
              rename_key(A		,					A		,					A      )
       read_in_q_k_v(A		,					A      )
       if mlm_model or irtr_model:
              snake_case__							:	Dict   	=	['itm_score.fc.weight', 'itm_score.fc.bias']
              for k in ignore_keys:
                     state_dict.pop(A		,					A      )

    # load state dict into HuggingFace model
       model.eval()
       if mlm_model:
              snake_case__	,				snake_case__							:	Optional[int]   	=	model.load_state_dict(A		,					strict=A      )
              assert missing_keys == ["mlm_score.decoder.bias"]
       else:
              model.load_state_dict(A      )

       # Define processor
       snake_case__							:	List[str]   	=	ViltImageProcessor(size=3_8_4      )
       snake_case__							:	str   	=	BertTokenizer.from_pretrained('bert-base-uncased'      )
       snake_case__							:	List[str]   	=	ViltProcessor(A		,					A      )

       # Forward pass on example inputs (image + text)
       if nlvr_model:
              snake_case__							:	List[Any]   	=	Image.open(requests.get('https://lil.nlp.cornell.edu/nlvr/exs/ex0_0.jpg'		,					stream=A      ).raw      )
              snake_case__							:	int   	=	Image.open(requests.get('https://lil.nlp.cornell.edu/nlvr/exs/ex0_0.jpg'		,					stream=A      ).raw      )
              snake_case__							:	Any   	=	(
                  'The left image contains twice the number of dogs as the right image, and at least two dogs in total are'
                  ' standing.'
              )
              snake_case__							:	List[Any]   	=	processor(A		,					A		,					return_tensors='pt'      )
              snake_case__							:	Optional[Any]   	=	processor(A		,					A		,					return_tensors='pt'      )
              snake_case__							:	List[Any]   	=	model(
                  input_ids=encoding_a.input_ids		,					pixel_values=encoding_a.pixel_values		,					pixel_values_a=encoding_a.pixel_values		,					)
       else:
              snake_case__							:	str   	=	Image.open(requests.get('http://images.cocodataset.org/val2017/000000039769.jpg'		,					stream=A      ).raw      )
              if mlm_model:
                     snake_case__							:	str   	=	'a bunch of [MASK] laying on a [MASK].'
              else:
                     snake_case__							:	Optional[Any]   	=	'How many cats are there?'
              snake_case__							:	Any   	=	processor(A		,					A		,					return_tensors='pt'      )
              snake_case__							:	Optional[Any]   	=	model(**A      )

       # Verify outputs
       if mlm_model:
              snake_case__							:	int   	=	torch.Size([1, 1_1, 3_0_5_2_2]      )
              snake_case__							:	List[Any]   	=	torch.tensor([-12.5061, -12.5123, -12.5174]      )
              assert outputs.logits.shape == expected_shape
              assert torch.allclose(outputs.logits[0, 0, :3]		,					A		,					atol=1e-4      )

              # verify masked token prediction equals "cats"
              snake_case__							:	Any   	=	outputs.logits[0, 4, :].argmax(-1      ).item()
              assert tokenizer.decode([predicted_id]      ) == "cats"
       elif vqa_model:
              snake_case__							:	Dict   	=	torch.Size([1, 3_1_2_9]      )
              snake_case__							:	Optional[Any]   	=	torch.tensor([-15.9495, -18.1472, -10.3041]      )
              assert torch.allclose(outputs.logits[0, :3]		,					A		,					atol=1e-4      )
              assert outputs.logits.shape == expected_shape
              assert torch.allclose(outputs.logits[0, 0, :3]		,					A		,					atol=1e-4      )

              # verify vqa prediction equals "2"
              snake_case__							:	Dict   	=	outputs.logits.argmax(-1      ).item()
              assert model.config.idalabel[predicted_idx] == "2"
       elif nlvr_model:
              snake_case__							:	int   	=	torch.Size([1, 2]      )
              snake_case__							:	List[Any]   	=	torch.tensor([-2.8721, 2.1291]      )
              assert torch.allclose(outputs.logits[0, :3]		,					A		,					atol=1e-4      )
              assert outputs.logits.shape == expected_shape

       Path(A      ).mkdir(exist_ok=A      )
       print(F'''Saving model and processor to {pytorch_dump_folder_path}'''      )
       model.save_pretrained(A      )
       processor.save_pretrained(A      )


if __name__ == "__main__":
 a_   :Optional[int]        =      argparse.ArgumentParser()
 # Required parameters
 parser.add_argument(
     "--checkpoint_url",
     default="https://github.com/dandelin/ViLT/releases/download/200k/vilt_200k_mlm_itm.ckpt",
     type=str,
     help="URL of the checkpoint you'd like to convert.",
 )
 parser.add_argument(
     "--pytorch_dump_folder_path", default=None, type=str, help="Path to the output PyTorch model directory."
 )

 a_   :Union[str, Any]        =      parser.parse_args()
 convert_vilt_checkpoint(args.checkpoint_url, args.pytorch_dump_folder_path)











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					)






