

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()
__snake_case							:	str											=       logging.get_logger(__name__)

__snake_case							:	int											=       [
    ("""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"""),
]

__snake_case							:	List[Any]											=       [
    """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  _UpperCAmelCase							(		a__):

			'''simple docstring'''



			a_		:		Any      				=      torch.load(a__   ,     map_location="""cpu""")
			return sd


def  _UpperCAmelCase							(		a__   ,     a__   ,     a__=rename_keys_prefix):

			'''simple docstring'''



			a_		:		str      				=      OrderedDict()
			a_		:		Tuple      				=      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
						a_		:		int      				=      key
						for name_pair in rename_keys_prefix:
									a_		:		Dict      				=      new_key.replace(name_pair[0]   ,     name_pair[1])
						a_		:		Dict      				=      d[key]
						if key == "bert.cls.predictions.decoder.weight":
									# Old bert code didn't have `decoder.bias`, but was added separately
									a_		:		Any      				=      new_d["""cls.predictions.bias"""]
			return new_d


@torch.no_grad()
def  _UpperCAmelCase							(		a__   ,     a__):

			'''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:
						a_		:		Any      				=      """pretraining"""
						if "vcr" in checkpoint_path:
									a_		:		Dict      				=      {"""visual_embedding_dim""": 5_1_2}
						elif "vqa_advanced" in checkpoint_path:
									a_		:		Tuple      				=      {"""visual_embedding_dim""": 2_0_4_8}
						elif "vqa" in checkpoint_path:
									a_		:		List[str]      				=      {"""visual_embedding_dim""": 2_0_4_8}
						elif "nlvr" in checkpoint_path:
									a_		:		int      				=      {"""visual_embedding_dim""": 1_0_2_4}
						else:
									raise NotImplementedError(f'''No implementation found for `{checkpoint_path}`.''')
			else:
						if "vcr" in checkpoint_path:
									a_		:		List[str]      				=      {"""visual_embedding_dim""": 5_1_2}
									a_		:		Union[str, Any]      				=      """multichoice"""
						elif "vqa_advanced" in checkpoint_path:
									a_		:		List[Any]      				=      {"""visual_embedding_dim""": 2_0_4_8}
									a_		:		str      				=      """vqa_advanced"""
						elif "vqa" in checkpoint_path:
									a_		:		str      				=      {"""visual_embedding_dim""": 2_0_4_8, """num_labels""": 3_1_2_9}
									a_		:		Optional[int]      				=      """vqa"""
						elif "nlvr" in checkpoint_path:
									a_		:		Tuple      				=      {
									    """visual_embedding_dim""": 1_0_2_4,
									    """num_labels""": 2,
									}
									a_		:		List[str]      				=      """nlvr"""

			a_		:		Dict      				=      VisualBertConfig(**a__)

			# Load State Dict
			a_		:		List[Any]      				=      load_state_dict(a__)

			a_		:		List[str]      				=      get_new_dict(a__   ,     a__)

			if model_type == "pretraining":
						a_		:		Optional[int]      				=      VisualBertForPreTraining(a__)
			elif model_type == "vqa":
						a_		:		Optional[int]      				=      VisualBertForQuestionAnswering(a__)
			elif model_type == "nlvr":
						a_		:		int      				=      VisualBertForVisualReasoning(a__)
			elif model_type == "multichoice":
						a_		:		Optional[int]      				=      VisualBertForMultipleChoice(a__)

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


if __name__ == "__main__":
	__snake_case							:	List[Any]											=       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.""")
	__snake_case							:	Any											=       parser.parse_args()
	convert_visual_bert_checkpoint(args.orig_checkpoint_path, args.pytorch_dump_folder_path)




_lowerCamelCase					   ={
    "joule": 1.0,
    "kilojoule": 1_0_0_0,
    "megajoule": 1_0_0_0_0_0_0,
    "gigajoule": 1_0_0_0_0_0_0_0_0_0,
    "wattsecond": 1.0,
    "watthour": 3_6_0_0,
    "kilowatthour": 3_6_0_0_0_0_0,
    "newtonmeter": 1.0,
    "calorie_nutr": 4_1_8_6.8,
    "kilocalorie_nutr": 4_1_8_6_8_0_0.0_0,
    "electronvolt": 1.6_0_2_1_7_6_6_3_4E-1_9,
    "britishthermalunit_it": 1_0_5_5.0_5_5_8_5,
    "footpound": 1.355818,
}





def     _a					(						lowerCamelCase,	lowerCamelCase,	lowerCamelCase							):
 if to_type not in ENERGY_CONVERSION or from_type not in ENERGY_CONVERSION:
  lowerCamelCase		:	Dict							=      (
      F'''Incorrect \'from_type\' or \'to_type\' value: {from_type!r}, {to_type!r}\n'''
      F'''Valid values are: {", ".join(lowerCamelCase							)}'''
  )
  raise ValueError(lowerCamelCase							)
 return value * ENERGY_CONVERSION[from_type] / ENERGY_CONVERSION[to_type]


if __name__ == "__main__":
      import doctest

      doctest.testmod()




