






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

SCREAMING_SNAKE_CASE__      :			Dict							=		[
    ('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'),
]

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







				"""simple docstring"""






				__magic_name__		:Union[str, Any]        =      torch.load(lowerCAmelCase_,  map_location='''cpu''' )
				return sd

def      __lowercase					( snake_case,  snake_case,  snake_case=rename_keys_prefix ):







				"""simple docstring"""






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

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

				__magic_name__		:str        =      VisualBertConfig(**lowerCAmelCase_ )

				# Load State Dict
				__magic_name__		:Union[str, Any]        =      load_state_dict(lowerCAmelCase_ )

				__magic_name__		:Tuple        =      get_new_dict(lowerCAmelCase_,  lowerCAmelCase_ )

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

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


if __name__ == "__main__":
					SCREAMING_SNAKE_CASE__      :			Optional[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.""")
					SCREAMING_SNAKE_CASE__      :			int							=		parser.parse_args()
					convert_visual_bert_checkpoint(args.orig_checkpoint_path, args.pytorch_dump_folder_path)














from ...processing_utils import ProcessorMixin
class 						lowerCamelCase_				(      lowerCamelCase ):
				a__					       =  '''WhisperFeatureExtractor'''
				a__					       =  '''WhisperTokenizer'''







				def __init__(						self       ,					__lowerCAmelCase       ,					__lowerCAmelCase     ):
								"""simple docstring"""
								super().__init__(__lowerCAmelCase       ,					__lowerCAmelCase     )
								__magic_name__		:int        =      self.feature_extractor
								__magic_name__		:int        =      False







				def 			A	(						self       ,					__lowerCAmelCase=None       ,					__lowerCAmelCase=None       ,					__lowerCAmelCase=True     ):
								"""simple docstring"""
								return self.tokenizer.get_decoder_prompt_ids(task=__lowerCAmelCase       ,					language=__lowerCAmelCase       ,					no_timestamps=__lowerCAmelCase     )







				def __call__(						self       ,					*__lowerCAmelCase       ,					**__lowerCAmelCase     ):
								"""simple docstring"""
								# For backward compatibility
								if self._in_target_context_manager:
												return self.current_processor(*__lowerCAmelCase       ,					**__lowerCAmelCase     )

								__magic_name__		:List[Any]        =      kwargs.pop('''audio'''       ,					__lowerCAmelCase     )
								__magic_name__		:Any        =      kwargs.pop('''sampling_rate'''       ,					__lowerCAmelCase     )
								__magic_name__		:str        =      kwargs.pop('''text'''       ,					__lowerCAmelCase     )
								if len(__lowerCAmelCase     ) > 0:
												__magic_name__		:Any        =      args[0]
												__magic_name__		:Union[str, Any]        =      args[1:]

								if audio is None and text is None:
												raise ValueError('''You need to specify either an `audio` or `text` input to process.'''     )

								if audio is not None:
												__magic_name__		:Optional[int]        =      self.feature_extractor(__lowerCAmelCase       ,					*__lowerCAmelCase       ,					sampling_rate=__lowerCAmelCase       ,					**__lowerCAmelCase     )
								if text is not None:
												__magic_name__		:Optional[int]        =      self.tokenizer(__lowerCAmelCase       ,					**__lowerCAmelCase     )

								if text is None:
												return inputs

								elif audio is None:
												return encodings
								else:
												__magic_name__		:str        =      encodings['''input_ids''']
												return inputs







				def 			A	(						self       ,					*__lowerCAmelCase       ,					**__lowerCAmelCase     ):
								"""simple docstring"""
								return self.tokenizer.batch_decode(*__lowerCAmelCase       ,					**__lowerCAmelCase     )







				def 			A	(						self       ,					*__lowerCAmelCase       ,					**__lowerCAmelCase     ):
								"""simple docstring"""
								return self.tokenizer.decode(*__lowerCAmelCase       ,					**__lowerCAmelCase     )







				def 			A	(						self       ,					__lowerCAmelCase       ,					__lowerCAmelCase="np"     ):
								"""simple docstring"""
								return self.tokenizer.get_prompt_ids(__lowerCAmelCase       ,					return_tensors=__lowerCAmelCase     )






