






"""simple docstring"""







import inspect
import unittest

from transformers import SegformerConfig, is_torch_available, is_vision_available
from transformers.models.auto import get_values
from transformers.testing_utils import require_torch, slow, torch_device

from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
			import torch

			from transformers import (
			    MODEL_MAPPING,
			    SegformerForImageClassification,
			    SegformerForSemanticSegmentation,
			    SegformerModel,
			)
			from transformers.models.segformer.modeling_segformer import SEGFORMER_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
			from PIL import Image

			from transformers import SegformerImageProcessor


class    __UpperCAmelCase(       SCREAMING_SNAKE_CASE__							):




						"""simple docstring"""




						def 		UpperCAmelCase_	(       self      ):
												'''simple docstring'''
												lowercase__    :				int=	self.config_class(**self.inputs_dict      )
												self.parent.assertTrue(hasattr(snake_case__	,  "hidden_sizes"      )      )
												self.parent.assertTrue(hasattr(snake_case__	,  "num_attention_heads"      )      )
												self.parent.assertTrue(hasattr(snake_case__	,  "num_encoder_blocks"      )      )


class    __UpperCAmelCase:




						"""simple docstring"""




						def __init__(       self	,  snake_case__	,  snake_case__=13	,  snake_case__=64	,  snake_case__=3	,  snake_case__=4	,  snake_case__=[2, 2, 2, 2]	,  snake_case__=[8, 4, 2, 1]	,  snake_case__=[16, 32, 64, 128]	,  snake_case__=[1, 4, 8, 16]	,  snake_case__=[1, 2, 4, 8]	,  snake_case__=True	,  snake_case__=True	,  snake_case__="gelu"	,  snake_case__=0.1	,  snake_case__=0.1	,  snake_case__=0.02	,  snake_case__=3	,  snake_case__=None	,  ):
												'''simple docstring'''
												lowercase__    :				List[str]=	parent
												lowercase__    :				Optional[int]=	batch_size
												lowercase__    :				int=	image_size
												lowercase__    :				Optional[int]=	num_channels
												lowercase__    :				str=	num_encoder_blocks
												lowercase__    :				str=	sr_ratios
												lowercase__    :				List[str]=	depths
												lowercase__    :				List[str]=	hidden_sizes
												lowercase__    :				str=	downsampling_rates
												lowercase__    :				str=	num_attention_heads
												lowercase__    :				Tuple=	is_training
												lowercase__    :				Any=	use_labels
												lowercase__    :				Any=	hidden_act
												lowercase__    :				Optional[Any]=	hidden_dropout_prob
												lowercase__    :				Tuple=	attention_probs_dropout_prob
												lowercase__    :				Dict=	initializer_range
												lowercase__    :				Union[str, Any]=	num_labels
												lowercase__    :				Dict=	scope




						def 		UpperCAmelCase_	(       self      ):
												'''simple docstring'''
												lowercase__    :				Optional[int]=	floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]      )

												lowercase__    :				Dict=	None
												if self.use_labels:
																		lowercase__    :				List[Any]=	ids_tensor([self.batch_size, self.image_size, self.image_size]	,  self.num_labels      )

												lowercase__    :				Dict=	self.get_config()
												return config, pixel_values, labels




						def 		UpperCAmelCase_	(       self      ):
												'''simple docstring'''
												return SegformerConfig(
												    image_size=self.image_size	,  num_channels=self.num_channels	,  num_encoder_blocks=self.num_encoder_blocks	,  depths=self.depths	,  hidden_sizes=self.hidden_sizes	,  num_attention_heads=self.num_attention_heads	,  hidden_act=self.hidden_act	,  hidden_dropout_prob=self.hidden_dropout_prob	,  attention_probs_dropout_prob=self.attention_probs_dropout_prob	,  initializer_range=self.initializer_range	,  )




						def 		UpperCAmelCase_	(       self	,  snake_case__	,  snake_case__	,  snake_case__      ):
												'''simple docstring'''
												lowercase__    :				List[Any]=	SegformerModel(config=snake_case__      )
												model.to(snake_case__      )
												model.eval()
												lowercase__    :				List[str]=	model(snake_case__      )
												lowercase__    :				Any=	self.image_size // (self.downsampling_rates[-1] * 2)
												self.parent.assertEqual(
												    result.last_hidden_state.shape	,  (self.batch_size, self.hidden_sizes[-1], expected_height, expected_width)      )




						def 		UpperCAmelCase_	(       self	,  snake_case__	,  snake_case__	,  snake_case__      ):
												'''simple docstring'''
												lowercase__    :				Union[str, Any]=	self.num_labels
												lowercase__    :				Union[str, Any]=	SegformerForSemanticSegmentation(snake_case__      )
												model.to(snake_case__      )
												model.eval()
												lowercase__    :				Dict=	model(snake_case__      )
												self.parent.assertEqual(
												    result.logits.shape	,  (self.batch_size, self.num_labels, self.image_size // 4, self.image_size // 4)      )
												lowercase__    :				Dict=	model(snake_case__	,  labels=snake_case__      )
												self.parent.assertEqual(
												    result.logits.shape	,  (self.batch_size, self.num_labels, self.image_size // 4, self.image_size // 4)      )
												self.parent.assertGreater(result.loss	,  0.0      )




						def 		UpperCAmelCase_	(       self	,  snake_case__	,  snake_case__	,  snake_case__      ):
												'''simple docstring'''
												lowercase__    :				str=	1
												lowercase__    :				List[str]=	SegformerForSemanticSegmentation(config=snake_case__      )
												model.to(snake_case__      )
												model.eval()
												lowercase__    :				Union[str, Any]=	torch.randint(0	,  1	,  (self.batch_size, self.image_size, self.image_size)      ).to(snake_case__      )
												lowercase__    :				Optional[Any]=	model(snake_case__	,  labels=snake_case__      )
												self.parent.assertGreater(result.loss	,  0.0      )




						def 		UpperCAmelCase_	(       self      ):
												'''simple docstring'''
												lowercase__    :				List[Any]=	self.prepare_config_and_inputs()
												lowercase__,     lowercase__,     lowercase__    :				List[str]=	config_and_inputs
												lowercase__    :				Tuple=	{"pixel_values": pixel_values}
												return config, inputs_dict


@require_torch
class    __UpperCAmelCase(       SCREAMING_SNAKE_CASE__  ,  SCREAMING_SNAKE_CASE__  ,  unittest.TestCase							):




						"""simple docstring"""




						__lowerCamelCase    	= (
						    (
						        SegformerModel,
						        SegformerForSemanticSegmentation,
						        SegformerForImageClassification,
						    )
						    if is_torch_available()
						    else ()
						)
						__lowerCamelCase    	= (
						    {
						        "feature-extraction": SegformerModel,
						        "image-classification": SegformerForImageClassification,
						        "image-segmentation": SegformerForSemanticSegmentation,
						    }
						    if is_torch_available()
						    else {}
						)

						__lowerCamelCase    	= True
						__lowerCamelCase    	= False
						__lowerCamelCase    	= False
						__lowerCamelCase    	= False




						def 		UpperCAmelCase_	(       self      ):
												'''simple docstring'''
												lowercase__    :				int=	SegformerModelTester(self      )
												lowercase__    :				Optional[Any]=	SegformerConfigTester(self	,  config_class=snake_case__      )




						def 		UpperCAmelCase_	(       self      ):
												'''simple docstring'''
												self.config_tester.run_common_tests()




						def 		UpperCAmelCase_	(       self      ):
												'''simple docstring'''
												lowercase__    :				Union[str, Any]=	self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_model(*snake_case__      )




						def 		UpperCAmelCase_	(       self      ):
												'''simple docstring'''
												lowercase__    :				List[str]=	self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_for_binary_image_segmentation(*snake_case__      )




						def 		UpperCAmelCase_	(       self      ):
												'''simple docstring'''
												lowercase__    :				Any=	self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_for_image_segmentation(*snake_case__      )




						@unittest.skip("SegFormer does not use inputs_embeds"      )
						def 		UpperCAmelCase_	(       self      ):
												'''simple docstring'''
												pass




						@unittest.skip("SegFormer does not have get_input_embeddings method and get_output_embeddings methods"      )
						def 		UpperCAmelCase_	(       self      ):
												'''simple docstring'''
												pass




						def 		UpperCAmelCase_	(       self      ):
												'''simple docstring'''
												lowercase__,     lowercase__    :				Dict=	self.model_tester.prepare_config_and_inputs_for_common()

												for model_class in self.all_model_classes:
																		lowercase__    :				Tuple=	model_class(snake_case__      )
																		lowercase__    :				List[str]=	inspect.signature(model.forward      )
																		# signature.parameters is an OrderedDict => so arg_names order is deterministic
																		lowercase__    :				List[str]=	[*signature.parameters.keys()]

																		lowercase__    :				Optional[int]=	["pixel_values"]
																		self.assertListEqual(arg_names[:1]	,  snake_case__      )




						def 		UpperCAmelCase_	(       self      ):
												'''simple docstring'''
												lowercase__,     lowercase__    :				Optional[int]=	self.model_tester.prepare_config_and_inputs_for_common()
												lowercase__    :				str=	True

												for model_class in self.all_model_classes:
																		lowercase__    :				Dict=	True
																		lowercase__    :				Any=	False
																		lowercase__    :				Optional[int]=	True
																		lowercase__    :				Any=	model_class(snake_case__      )
																		model.to(snake_case__      )
																		model.eval()
																		with torch.no_grad():
																								lowercase__    :				Any=	model(**self._prepare_for_class(snake_case__	,  snake_case__      )      )
																		lowercase__    :				str=	outputs.attentions

																		lowercase__    :				Dict=	sum(self.model_tester.depths      )
																		self.assertEqual(len(snake_case__      )	,  snake_case__      )

																		# check that output_attentions also work using config
																		del inputs_dict["output_attentions"]
																		lowercase__    :				Union[str, Any]=	True
																		lowercase__    :				List[str]=	model_class(snake_case__      )
																		model.to(snake_case__      )
																		model.eval()
																		with torch.no_grad():
																								lowercase__    :				List[str]=	model(**self._prepare_for_class(snake_case__	,  snake_case__      )      )
																		lowercase__    :				Tuple=	outputs.attentions

																		self.assertEqual(len(snake_case__      )	,  snake_case__      )

																		# verify the first attentions (first block, first layer)
																		lowercase__    :				Union[str, Any]=	(self.model_tester.image_size // 4) ** 2
																		lowercase__    :				str=	(self.model_tester.image_size // (4 * self.model_tester.sr_ratios[0])) ** 2
																		self.assertListEqual(
																		    list(attentions[0].shape[-3:]      )	,  [self.model_tester.num_attention_heads[0], expected_seq_len, expected_reduced_seq_len]	,  )

																		# verify the last attentions (last block, last layer)
																		lowercase__    :				Any=	(self.model_tester.image_size // 32) ** 2
																		lowercase__    :				Union[str, Any]=	(self.model_tester.image_size // (32 * self.model_tester.sr_ratios[-1])) ** 2
																		self.assertListEqual(
																		    list(attentions[-1].shape[-3:]      )	,  [self.model_tester.num_attention_heads[-1], expected_seq_len, expected_reduced_seq_len]	,  )
																		lowercase__    :				Optional[int]=	len(snake_case__      )

																		# Check attention is always last and order is fine
																		lowercase__    :				Optional[int]=	True
																		lowercase__    :				Optional[int]=	True
																		lowercase__    :				str=	model_class(snake_case__      )
																		model.to(snake_case__      )
																		model.eval()
																		with torch.no_grad():
																								lowercase__    :				List[Any]=	model(**self._prepare_for_class(snake_case__	,  snake_case__      )      )

																		self.assertEqual(out_len + 1	,  len(snake_case__      )      )

																		lowercase__    :				Union[str, Any]=	outputs.attentions

																		self.assertEqual(len(snake_case__      )	,  snake_case__      )
																		# verify the first attentions (first block, first layer)
																		lowercase__    :				Optional[int]=	(self.model_tester.image_size // 4) ** 2
																		lowercase__    :				Any=	(self.model_tester.image_size // (4 * self.model_tester.sr_ratios[0])) ** 2
																		self.assertListEqual(
																		    list(self_attentions[0].shape[-3:]      )	,  [self.model_tester.num_attention_heads[0], expected_seq_len, expected_reduced_seq_len]	,  )




						def 		UpperCAmelCase_	(       self      ):
												'''simple docstring'''
												def check_hidden_states_output(snake_case__	,  snake_case__	,  snake_case__      ):
																		lowercase__    :				str=	model_class(snake_case__      )
																		model.to(snake_case__      )
																		model.eval()

																		with torch.no_grad():
																								lowercase__    :				str=	model(**self._prepare_for_class(snake_case__	,  snake_case__      )      )

																		lowercase__    :				int=	outputs.hidden_states

																		lowercase__    :				int=	self.model_tester.num_encoder_blocks
																		self.assertEqual(len(snake_case__      )	,  snake_case__      )

																		# verify the first hidden states (first block)
																		self.assertListEqual(
																		    list(hidden_states[0].shape[-3:]      )	,  [
																		        self.model_tester.hidden_sizes[0],
																		        self.model_tester.image_size // 4,
																		        self.model_tester.image_size // 4,
																		    ]	,  )

												lowercase__,     lowercase__    :				Tuple=	self.model_tester.prepare_config_and_inputs_for_common()

												for model_class in self.all_model_classes:
																		lowercase__    :				Tuple=	True
																		check_hidden_states_output(snake_case__	,  snake_case__	,  snake_case__      )

																		# check that output_hidden_states also work using config
																		del inputs_dict["output_hidden_states"]
																		lowercase__    :				List[Any]=	True

																		check_hidden_states_output(snake_case__	,  snake_case__	,  snake_case__      )




						def 		UpperCAmelCase_	(       self      ):
												'''simple docstring'''
												if not self.model_tester.is_training:
																		return

												lowercase__,     lowercase__    :				Dict=	self.model_tester.prepare_config_and_inputs_for_common()
												lowercase__    :				int=	True

												for model_class in self.all_model_classes:
																		if model_class in get_values(snake_case__      ):
																								continue

																		lowercase__    :				Any=	model_class(snake_case__      )
																		model.to(snake_case__      )
																		model.train()
																		lowercase__    :				Any=	self._prepare_for_class(snake_case__	,  snake_case__	,  return_labels=snake_case__      )
																		lowercase__    :				Dict=	model(**snake_case__      ).loss
																		loss.backward()




						@unittest.skip("Will be fixed soon by reducing the size of the model used for common tests."      )
						def 		UpperCAmelCase_	(       self      ):
												'''simple docstring'''
												pass




						@slow
						def 		UpperCAmelCase_	(       self      ):
												'''simple docstring'''
												for model_name in SEGFORMER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																		lowercase__    :				Tuple=	SegformerModel.from_pretrained(snake_case__      )
																		self.assertIsNotNone(snake_case__      )





def 						lowercase__()    ->int:




						"""simple docstring"""



						lowercase__    :				List[str]=	Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png"						)
						return image







@require_torch
class    __UpperCAmelCase(       unittest.TestCase							):




						"""simple docstring"""




						@slow
						def 		UpperCAmelCase_	(       self      ):
												'''simple docstring'''
												# only resize + normalize
												lowercase__    :				Optional[int]=	SegformerImageProcessor(
												    image_scale=(512, 512)	,  keep_ratio=snake_case__	,  align=snake_case__	,  do_random_crop=snake_case__      )
												lowercase__    :				Optional[Any]=	SegformerForSemanticSegmentation.from_pretrained("nvidia/segformer-b0-finetuned-ade-512-512"      ).to(
												    snake_case__      )

												lowercase__    :				Any=	prepare_img()
												lowercase__    :				Dict=	image_processor(images=snake_case__	,  return_tensors="pt"      )
												lowercase__    :				List[str]=	encoded_inputs.pixel_values.to(snake_case__      )

												with torch.no_grad():
																		lowercase__    :				List[Any]=	model(snake_case__      )

												lowercase__    :				Union[str, Any]=	torch.Size((1, model.config.num_labels, 128, 128)      )
												self.assertEqual(outputs.logits.shape	,  snake_case__      )

												lowercase__    :				int=	torch.tensor(
												    [
												        [[-4.63_10, -5.52_32, -6.23_56], [-5.19_21, -6.14_44, -6.59_96], [-5.44_24, -6.27_90, -6.75_74]],
												        [[-12.13_91, -13.31_22, -13.95_54], [-12.87_32, -13.93_52, -14.35_63], [-12.94_38, -13.82_26, -14.25_13]],
												        [[-12.51_34, -13.46_86, -14.49_15], [-12.86_69, -14.43_43, -14.77_58], [-13.25_23, -14.58_19, -15.06_94]],
												    ]      ).to(snake_case__      )
												self.assertTrue(torch.allclose(outputs.logits[0, :3, :3, :3]	,  snake_case__	,  atol=1e-4      )      )




						@slow
						def 		UpperCAmelCase_	(       self      ):
												'''simple docstring'''
												# only resize + normalize
												lowercase__    :				List[Any]=	SegformerImageProcessor(
												    image_scale=(512, 512)	,  keep_ratio=snake_case__	,  align=snake_case__	,  do_random_crop=snake_case__      )
												lowercase__    :				Tuple=	SegformerForSemanticSegmentation.from_pretrained(
												    "nvidia/segformer-b1-finetuned-cityscapes-1024-1024"      ).to(snake_case__      )

												lowercase__    :				List[Any]=	prepare_img()
												lowercase__    :				Optional[int]=	image_processor(images=snake_case__	,  return_tensors="pt"      )
												lowercase__    :				Optional[Any]=	encoded_inputs.pixel_values.to(snake_case__      )

												with torch.no_grad():
																		lowercase__    :				Union[str, Any]=	model(snake_case__      )

												lowercase__    :				str=	torch.Size((1, model.config.num_labels, 128, 128)      )
												self.assertEqual(outputs.logits.shape	,  snake_case__      )

												lowercase__    :				Union[str, Any]=	torch.tensor(
												    [
												        [[-13.57_48, -13.91_11, -12.65_00], [-14.35_00, -15.36_83, -14.23_28], [-14.75_32, -16.04_24, -15.60_87]],
												        [[-17.16_51, -15.87_25, -12.96_53], [-17.25_80, -17.37_18, -14.82_23], [-16.60_58, -16.87_83, -16.74_52]],
												        [[-3.64_56, -3.02_09, -1.42_03], [-3.07_97, -3.19_59, -2.00_00], [-1.87_57, -1.92_17, -1.69_97]],
												    ]      ).to(snake_case__      )
												self.assertTrue(torch.allclose(outputs.logits[0, :3, :3, :3]	,  snake_case__	,  atol=1e-1      )      )




						@slow
						def 		UpperCAmelCase_	(       self      ):
												'''simple docstring'''
												# only resize + normalize
												lowercase__    :				str=	SegformerImageProcessor(
												    image_scale=(512, 512)	,  keep_ratio=snake_case__	,  align=snake_case__	,  do_random_crop=snake_case__      )
												lowercase__    :				Dict=	SegformerForSemanticSegmentation.from_pretrained("nvidia/segformer-b0-finetuned-ade-512-512"      ).to(
												    snake_case__      )

												lowercase__    :				int=	prepare_img()
												lowercase__    :				Union[str, Any]=	image_processor(images=snake_case__	,  return_tensors="pt"      )
												lowercase__    :				Tuple=	encoded_inputs.pixel_values.to(snake_case__      )

												with torch.no_grad():
																		lowercase__    :				int=	model(snake_case__      )

												lowercase__    :				Tuple=	outputs.logits.detach().cpu()

												lowercase__    :				Union[str, Any]=	image_processor.post_process_semantic_segmentation(outputs=snake_case__	,  target_sizes=[(500, 300)]      )
												lowercase__    :				Optional[Any]=	torch.Size((500, 300)      )
												self.assertEqual(segmentation[0].shape	,  snake_case__      )

												lowercase__    :				str=	image_processor.post_process_semantic_segmentation(outputs=snake_case__      )
												lowercase__    :				str=	torch.Size((128, 128)      )
												self.assertEqual(segmentation[0].shape	,  snake_case__      )














"""simple docstring"""







import argparse
from collections import OrderedDict
from pathlib import Path

import torch

from transformers import (
    VisualBertConfig,
    VisualBertForMultipleChoice,
    VisualBertForPreTraining,
    VisualBertForQuestionAnswering,
    VisualBertForVisualReasoning,
)
from transformers.utils import logging


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

a			:    List[Any]				 =		[
    ("""bert.bert""", """visual_bert"""),
    ("""bert.cls""", """cls"""),
    ("""bert.classifier""", """cls"""),
    ("""token_type_embeddings_visual""", """visual_token_type_embeddings"""),
    ("""position_embeddings_visual""", """visual_position_embeddings"""),
    ("""projection""", """visual_projection"""),
]

a			:    Dict				 =		[
    """nlvr2_coco_pre_trained.th""",
    """nlvr2_fine_tuned.th""",
    """nlvr2_pre_trained.th""",
    """vcr_coco_pre_train.th""",
    """vcr_fine_tune.th""",
    """vcr_pre_train.th""",
    """vqa_coco_pre_trained.th""",
    """vqa_fine_tuned.th""",
    """vqa_pre_trained.th""",
]





def 						lowercase__(A						)    ->Optional[int]:




						"""simple docstring"""



						lowercase__    :				Any=	torch.load(A ,		map_location="cpu"						)
						return sd





def 						lowercase__(A ,		A ,		A=rename_keys_prefix						)    ->List[str]:




						"""simple docstring"""



						lowercase__    :				int=	OrderedDict()
						lowercase__    :				Optional[Any]=	torch.arange(config.max_position_embeddings						).expand((1, -1)						)
						# detector_d = OrderedDict()
						for key in d:
												if "detector" in key:
																		# detector_d[key.replace('detector.','')] = d[key]
																		continue
												lowercase__    :				Union[str, Any]=	key
												for name_pair in rename_keys_prefix:
																		lowercase__    :				str=	new_key.replace(name_pair[0] ,		name_pair[1]						)
												lowercase__    :				Union[str, Any]=	d[key]
												if key == "bert.cls.predictions.decoder.weight":
																		# Old bert code didn't have `decoder.bias`, but was added separately
																		lowercase__    :				Optional[int]=	new_d["cls.predictions.bias"]
						return new_d





@torch.no_grad()
def 						lowercase__(A ,		A						)    ->str:




						"""simple docstring"""



						assert (
						    checkpoint_path.split("/"						)[-1] in ACCEPTABLE_CHECKPOINTS
						), f'''The checkpoint provided must be in {ACCEPTABLE_CHECKPOINTS}.'''

						# Get Config
						if "pre" in checkpoint_path:
												lowercase__    :				Union[str, Any]=	"pretraining"
												if "vcr" in checkpoint_path:
																		lowercase__    :				str=	{"visual_embedding_dim": 512}
												elif "vqa_advanced" in checkpoint_path:
																		lowercase__    :				Optional[Any]=	{"visual_embedding_dim": 2_048}
												elif "vqa" in checkpoint_path:
																		lowercase__    :				int=	{"visual_embedding_dim": 2_048}
												elif "nlvr" in checkpoint_path:
																		lowercase__    :				Tuple=	{"visual_embedding_dim": 1_024}
												else:
																		raise NotImplementedError(f'''No implementation found for `{checkpoint_path}`.'''						)
						else:
												if "vcr" in checkpoint_path:
																		lowercase__    :				int=	{"visual_embedding_dim": 512}
																		lowercase__    :				int=	"multichoice"
												elif "vqa_advanced" in checkpoint_path:
																		lowercase__    :				Dict=	{"visual_embedding_dim": 2_048}
																		lowercase__    :				Optional[Any]=	"vqa_advanced"
												elif "vqa" in checkpoint_path:
																		lowercase__    :				Optional[int]=	{"visual_embedding_dim": 2_048, "num_labels": 3_129}
																		lowercase__    :				List[str]=	"vqa"
												elif "nlvr" in checkpoint_path:
																		lowercase__    :				Dict=	{
																		    "visual_embedding_dim": 1_024,
																		    "num_labels": 2,
																		}
																		lowercase__    :				Any=	"nlvr"

						lowercase__    :				List[Any]=	VisualBertConfig(**A						)

						# Load State Dict
						lowercase__    :				Union[str, Any]=	load_state_dict(A						)

						lowercase__    :				List[str]=	get_new_dict(A ,		A						)

						if model_type == "pretraining":
												lowercase__    :				Optional[Any]=	VisualBertForPreTraining(A						)
						elif model_type == "vqa":
												lowercase__    :				Any=	VisualBertForQuestionAnswering(A						)
						elif model_type == "nlvr":
												lowercase__    :				Union[str, Any]=	VisualBertForVisualReasoning(A						)
						elif model_type == "multichoice":
												lowercase__    :				str=	VisualBertForMultipleChoice(A						)

						model.load_state_dict(A						)
						# Save Checkpoints
						Path(A						).mkdir(exist_ok=A						)
						model.save_pretrained(A						)


if __name__ == "__main__":
			a			:    int				 =		argparse.ArgumentParser()
			# Required parameters
			parser.add_argument("""orig_checkpoint_path""", type=str, help="""A path to .th on local filesystem.""")
			parser.add_argument("""pytorch_dump_folder_path""", type=str, help="""Path to the output PyTorch model.""")
			a			:    Dict				 =		parser.parse_args()
			convert_visual_bert_checkpoint(args.orig_checkpoint_path, args.pytorch_dump_folder_path)






