






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

_UpperCamelCase			:	List[str]								=[
    ('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'),
]

_UpperCamelCase			:	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  a__ (__lowercase							:Dict   )		-> str:
				_A	:				Any   =      torch.load(_UpperCamelCase					,  map_location='''cpu'''   )
				return sd



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



@torch.no_grad()
def  a__ (__lowercase							:int					,  __lowercase							:List[Any]   )		-> Optional[Any]:
				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	:				Optional[int]   =      '''pretraining'''
								if "vcr" in checkpoint_path:
												_A	:				List[Any]   =      {'''visual_embedding_dim''': 512}
								elif "vqa_advanced" in checkpoint_path:
												_A	:				Optional[int]   =      {'''visual_embedding_dim''': 2048}
								elif "vqa" in checkpoint_path:
												_A	:				List[str]   =      {'''visual_embedding_dim''': 2048}
								elif "nlvr" in checkpoint_path:
												_A	:				Any   =      {'''visual_embedding_dim''': 1024}
								else:
												raise NotImplementedError(f"""No implementation found for `{checkpoint_path}`."""   )
				else:
								if "vcr" in checkpoint_path:
												_A	:				List[Any]   =      {'''visual_embedding_dim''': 512}
												_A	:				int   =      '''multichoice'''
								elif "vqa_advanced" in checkpoint_path:
												_A	:				List[Any]   =      {'''visual_embedding_dim''': 2048}
												_A	:				Any   =      '''vqa_advanced'''
								elif "vqa" in checkpoint_path:
												_A	:				int   =      {'''visual_embedding_dim''': 2048, '''num_labels''': 3129}
												_A	:				Any   =      '''vqa'''
								elif "nlvr" in checkpoint_path:
												_A	:				str   =      {
												    '''visual_embedding_dim''': 1024,
												    '''num_labels''': 2,
												}
												_A	:				List[str]   =      '''nlvr'''

				_A	:				int   =      VisualBertConfig(**_UpperCamelCase   )

				# Load State Dict
				_A	:				List[Any]   =      load_state_dict(_UpperCamelCase   )

				_A	:				int   =      get_new_dict(_UpperCamelCase					,  _UpperCamelCase   )

				if model_type == "pretraining":
								_A	:				str   =      VisualBertForPreTraining(_UpperCamelCase   )
				elif model_type == "vqa":
								_A	:				Dict   =      VisualBertForQuestionAnswering(_UpperCamelCase   )
				elif model_type == "nlvr":
								_A	:				int   =      VisualBertForVisualReasoning(_UpperCamelCase   )
				elif model_type == "multichoice":
								_A	:				Optional[Any]   =      VisualBertForMultipleChoice(_UpperCamelCase   )

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


if __name__ == "__main__":
	_UpperCamelCase			:	Tuple								=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.')
	_UpperCamelCase			:	Any								=parser.parse_args()
	convert_visual_bert_checkpoint(args.orig_checkpoint_path, args.pytorch_dump_folder_path)








a_								=       {
    0: """0""",
    1: """1""",
    2: """2""",
    3: """3""",
    4: """4""",
    5: """5""",
    6: """6""",
    7: """7""",
    8: """8""",
    9: """9""",
    10: """a""",
    11: """b""",
    12: """c""",
    13: """d""",
    14: """e""",
    15: """f""",
}





def 		a__      (		_UpperCamelCase      :			float				):
   assert type(_UpperCamelCase				) in (int, float) and decimal == int(_UpperCamelCase				)
   __lowerCamelCase    				=     int(_UpperCamelCase				)
   __lowerCamelCase    				=     ''''''
   __lowerCamelCase    				=     False
   if decimal < 0:
      __lowerCamelCase    				=     True
      decimal *= -1
   while decimal > 0:
      __lowerCamelCase				,__lowerCamelCase    				=     divmod(_UpperCamelCase			,16				)
      __lowerCamelCase    				=     values[remainder] + hexadecimal
   __lowerCamelCase    				=     '''0x''' + hexadecimal
   if negative:
      __lowerCamelCase    				=     '''-''' + hexadecimal
   return hexadecimal


if __name__ == "__main__":
  import doctest

  doctest.testmod()






