




"""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				=							logging.get_logger(__name__)

A				=							[
    ("""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				=							[
    """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     (      UpperCamelCase				)      ->      List[Any]:


			"""simple docstring"""
			__UpperCAmelCase						:			str												=       torch.load(UpperCamelCase					,							map_location="cpu"				)
			return sd

def        _UpperCamelCase     (      UpperCamelCase					,							UpperCamelCase					,							UpperCamelCase=rename_keys_prefix				)      ->      List[str]:


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

@torch.no_grad()
def        _UpperCamelCase     (      UpperCamelCase					,							UpperCamelCase				)      ->      Union[str, Any]:


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

			__UpperCAmelCase						:			Optional[int]												=       VisualBertConfig(**UpperCamelCase				)

			# Load State Dict
			__UpperCAmelCase						:			Dict												=       load_state_dict(UpperCamelCase				)

			__UpperCAmelCase						:			List[Any]												=       get_new_dict(UpperCamelCase					,							UpperCamelCase				)

			if model_type == "pretraining":
						__UpperCAmelCase						:			Optional[int]												=       VisualBertForPreTraining(UpperCamelCase				)
			elif model_type == "vqa":
						__UpperCAmelCase						:			str												=       VisualBertForQuestionAnswering(UpperCamelCase				)
			elif model_type == "nlvr":
						__UpperCAmelCase						:			List[Any]												=       VisualBertForVisualReasoning(UpperCamelCase				)
			elif model_type == "multichoice":
						__UpperCAmelCase						:			Tuple												=       VisualBertForMultipleChoice(UpperCamelCase				)

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


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




import warnings
from contextlib import contextmanager

from ....processing_utils import ProcessorMixin





class     lowerCamelCase      (       __lowerCamelCase					):
	UpperCamelCase_  :					Optional[Any]    		=	'MCTCTFeatureExtractor'
	UpperCamelCase_  :					List[Any]    		=	'AutoTokenizer'







	def __init__(     self      :Tuple   ,     lowercase      :List[str]   ,     lowercase      :Dict			)		->      Tuple:







				"""simple docstring"""






				super().__init__(lowercase   ,     lowercase			)
				SCREAMING_SNAKE_CASE     						= self.feature_extractor
				SCREAMING_SNAKE_CASE     						= False







	def __call__(     self      :Union[str, Any]   ,     *lowercase      :Union[str, Any]   ,     **lowercase      :str			)		->      int:







				"""simple docstring"""






				if self._in_target_context_manager:
							return self.current_processor(*lowercase   ,     **lowercase			)

				if "raw_speech" in kwargs:
							warnings.warn('''Using `raw_speech` as a keyword argument is deprecated. Use `audio` instead.'''			)
							SCREAMING_SNAKE_CASE     						= kwargs.pop('''raw_speech'''			)
				else:
							SCREAMING_SNAKE_CASE     						= kwargs.pop('''audio'''   ,     lowercase			)
				SCREAMING_SNAKE_CASE     						= kwargs.pop('''sampling_rate'''   ,     lowercase			)
				SCREAMING_SNAKE_CASE     						= kwargs.pop('''text'''   ,     lowercase			)
				if len(lowercase			) > 0:
							SCREAMING_SNAKE_CASE     						= args[0]
							SCREAMING_SNAKE_CASE     						= 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:
							SCREAMING_SNAKE_CASE     						= self.feature_extractor(lowercase   ,     *lowercase   ,     sampling_rate=lowercase   ,     **lowercase			)
				if text is not None:
							SCREAMING_SNAKE_CASE     						= self.tokenizer(lowercase   ,     **lowercase			)

				if text is None:
							return inputs
				elif audio is None:
							return encodings
				else:
							SCREAMING_SNAKE_CASE     						= encodings['''input_ids''']
							return inputs







	def  snake_case__     (     self      :Dict   ,     *lowercase      :Union[str, Any]   ,     **lowercase      :List[str]			)		->      Union[str, Any]:







				"""simple docstring"""






				return self.tokenizer.batch_decode(*lowercase   ,     **lowercase			)







	def  snake_case__     (     self      :List[Any]   ,     *lowercase      :List[Any]   ,     **lowercase      :List[str]			)		->      int:







				"""simple docstring"""






				if self._in_target_context_manager:
							return self.current_processor.pad(*lowercase   ,     **lowercase			)

				SCREAMING_SNAKE_CASE     						= kwargs.pop('''input_features'''   ,     lowercase			)
				SCREAMING_SNAKE_CASE     						= kwargs.pop('''labels'''   ,     lowercase			)
				if len(lowercase			) > 0:
							SCREAMING_SNAKE_CASE     						= args[0]
							SCREAMING_SNAKE_CASE     						= args[1:]

				if input_features is not None:
							SCREAMING_SNAKE_CASE     						= self.feature_extractor.pad(lowercase   ,     *lowercase   ,     **lowercase			)
				if labels is not None:
							SCREAMING_SNAKE_CASE     						= self.tokenizer.pad(lowercase   ,     **lowercase			)

				if labels is None:
							return input_features
				elif input_features is None:
							return labels
				else:
							SCREAMING_SNAKE_CASE     						= labels['''input_ids''']
							return input_features







	def  snake_case__     (     self      :Dict   ,     *lowercase      :List[str]   ,     **lowercase      :Union[str, Any]			)		->      List[Any]:







				"""simple docstring"""






				return self.tokenizer.decode(*lowercase   ,     **lowercase			)







	@contextmanager
	def  snake_case__     (     self      :str			)		->      Union[str, Any]:







				"""simple docstring"""






				warnings.warn(
				    '''`as_target_processor` is deprecated and will be removed in v5 of Transformers. You can process your '''
				    '''labels by using the argument `text` of the regular `__call__` method (either in the same call as '''
				    '''your audio inputs, or in a separate call.'''			)
				SCREAMING_SNAKE_CASE     						= True
				SCREAMING_SNAKE_CASE     						= self.tokenizer
				yield
				SCREAMING_SNAKE_CASE     						= self.feature_extractor
				SCREAMING_SNAKE_CASE     						= False