


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

UpperCAmelCase_						:  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'),
]

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      snake_case_  (    SCREAMING_SNAKE_CASE__	):







				"""simple docstring"""





				_SCREAMING_SNAKE_CASE					:			List[Any]							= torch.load(SCREAMING_SNAKE_CASE__				, map_location="""cpu"""	)
				return sd







def      snake_case_  (    SCREAMING_SNAKE_CASE__				, SCREAMING_SNAKE_CASE__				, SCREAMING_SNAKE_CASE__=rename_keys_prefix	):







				"""simple docstring"""





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







@torch.no_grad()
def      snake_case_  (    SCREAMING_SNAKE_CASE__				, SCREAMING_SNAKE_CASE__	):







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

				_SCREAMING_SNAKE_CASE					:			Dict							= VisualBertConfig(**SCREAMING_SNAKE_CASE__	)

				# Load State Dict
				_SCREAMING_SNAKE_CASE					:			List[Any]							= load_state_dict(SCREAMING_SNAKE_CASE__	)

				_SCREAMING_SNAKE_CASE					:			Optional[int]							= get_new_dict(SCREAMING_SNAKE_CASE__				, SCREAMING_SNAKE_CASE__	)

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

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


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








'''simple docstring'''


def      snake_case_  (    SCREAMING_SNAKE_CASE__				, SCREAMING_SNAKE_CASE__	):







				"""simple docstring"""





				if b == 0:
								return 1
				if (b % 2) == 0:
								return actual_power(SCREAMING_SNAKE_CASE__				, int(b / 2	)	) * actual_power(SCREAMING_SNAKE_CASE__				, int(b / 2	)	)
				else:
								return a * actual_power(SCREAMING_SNAKE_CASE__				, int(b / 2	)	) * actual_power(SCREAMING_SNAKE_CASE__				, int(b / 2	)	)







def      snake_case_  (    SCREAMING_SNAKE_CASE__				, SCREAMING_SNAKE_CASE__	):







				"""simple docstring"""





				if b < 0:
								return 1 / actual_power(SCREAMING_SNAKE_CASE__				, SCREAMING_SNAKE_CASE__	)
				return actual_power(SCREAMING_SNAKE_CASE__				, SCREAMING_SNAKE_CASE__	)


if __name__ == "__main__":
				print(power(-2, -3))




