






"""simple docstring"""




from collections import OrderedDict
from typing import TYPE_CHECKING, Any, Mapping, Optional, Union

from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfig, OnnxSeqaSeqConfigWithPast
from ...utils import logging


if TYPE_CHECKING:
 from ...feature_extraction_utils import FeatureExtractionMixin
 from ...tokenization_utils_base import PreTrainedTokenizerBase
 from ...utils import TensorType

_snake_case          =   logging.get_logger(__name__)

_snake_case          =   {
    "openai/whisper-base": "https://huggingface.co/openai/whisper-base/resolve/main/config.json",
}

# fmt: off
_snake_case          =   [
    1, 2, 7, 8, 9, 10, 14, 25,
    26, 27, 28, 29, 31, 58, 59, 60, 61, 62,
    63, 90, 91, 92, 93, 357, 366, 438, 532, 685,
    705, 796, 930, 1058, 1220, 1267, 1279, 1303, 1343, 1377,
    1391, 1635, 1782, 1875, 2162, 2361, 2488, 3467, 4008, 4211,
    4600, 4808, 5299, 5855, 6329, 7203, 9609, 9959, 1_0563, 1_0786,
    1_1420, 1_1709, 1_1907, 1_3163, 1_3697, 1_3700, 1_4808, 1_5306, 1_6410, 1_6791,
    1_7992, 1_9203, 1_9510, 2_0724, 2_2305, 2_2935, 2_7007, 3_0109, 3_0420, 3_3409,
    3_4949, 4_0283, 4_0493, 4_0549, 4_7282, 4_9146, 5_0257, 5_0359, 5_0360, 5_0361
]
_snake_case          =   [
    1, 2, 7, 8, 9, 10, 14, 25,
    26, 27, 28, 29, 31, 58, 59, 60, 61, 62,
    63, 90, 91, 92, 93, 359, 503, 522, 542, 873,
    893, 902, 918, 922, 931, 1350, 1853, 1982, 2460, 2627,
    3246, 3253, 3268, 3536, 3846, 3961, 4183, 4667, 6585, 6647,
    7273, 9061, 9383, 1_0428, 1_0929, 1_1938, 1_2033, 1_2331, 1_2562, 1_3793,
    1_4157, 1_4635, 1_5265, 1_5618, 1_6553, 1_6604, 1_8362, 1_8956, 2_0075, 2_1675,
    2_2520, 2_6130, 2_6161, 2_6435, 2_8279, 2_9464, 3_1650, 3_2302, 3_2470, 3_6865,
    4_2863, 4_7425, 4_9870, 5_0254, 5_0258, 5_0360, 5_0361, 5_0362
]






class        UpperCamelCase			(	lowerCAmelCase__      ):
 UpperCamelCase					:       Dict			=					"""whisper"""
 UpperCamelCase					:       str			=					["""past_key_values"""]
 UpperCamelCase					:       List[str]			=					{"""num_attention_heads""": """encoder_attention_heads""", """hidden_size""": """d_model"""}


 def __init__(     self		:						Union[str, Any]							,						UpperCAmelCase__		:						str=51865							,						UpperCAmelCase__		:						Union[str, Any]=80							,						UpperCAmelCase__		:						Any=6							,						UpperCAmelCase__		:						Optional[int]=4							,						UpperCAmelCase__		:						Dict=6							,						UpperCAmelCase__		:						str=4							,						UpperCAmelCase__		:						int=1536							,						UpperCAmelCase__		:						Optional[Any]=1536							,						UpperCAmelCase__		:						int=0.0							,						UpperCAmelCase__		:						int=0.0							,						UpperCAmelCase__		:						Tuple=50257							,						UpperCAmelCase__		:						Optional[Any]=True							,						UpperCAmelCase__		:						Any=True							,						UpperCAmelCase__		:						Tuple="gelu"							,						UpperCAmelCase__		:						int=256							,						UpperCAmelCase__		:						str=0.0							,						UpperCAmelCase__		:						Any=0.0							,						UpperCAmelCase__		:						Optional[Any]=0.0							,						UpperCAmelCase__		:						Union[str, Any]=0.0_2							,						UpperCAmelCase__		:						List[Any]=False							,						UpperCAmelCase__		:						Any=1500							,						UpperCAmelCase__		:						str=448							,						UpperCAmelCase__		:						Optional[int]=50256							,						UpperCAmelCase__		:						Any=50256							,						UpperCAmelCase__		:						str=50256							,						UpperCAmelCase__		:						List[Any]=None							,						UpperCAmelCase__		:						Optional[int]=[220, 50256]							,						UpperCAmelCase__		:						Dict=False							,						UpperCAmelCase__		:						str=256							,						UpperCAmelCase__		:						List[Any]=False							,						UpperCAmelCase__		:						int=0.0_5							,						UpperCAmelCase__		:						Any=10							,						UpperCAmelCase__		:						Dict=2							,						UpperCAmelCase__		:						Tuple=0.0							,						UpperCAmelCase__		:						List[Any]=10							,						UpperCAmelCase__		:						Optional[int]=0							,						UpperCAmelCase__		:						Tuple=7							,						**UpperCAmelCase__		:						Union[str, Any]							,						)					->       Tuple:
   _a  :       Any             =	vocab_size
   _a  :       Optional[Any]             =	num_mel_bins
   _a  :       Optional[Any]             =	d_model
   _a  :       List[str]             =	encoder_layers
   _a  :       str             =	encoder_attention_heads
   _a  :       Union[str, Any]             =	decoder_layers
   _a  :       str             =	decoder_attention_heads
   _a  :       Any             =	decoder_ffn_dim
   _a  :       int             =	encoder_ffn_dim
   _a  :       List[Any]             =	dropout
   _a  :       int             =	attention_dropout
   _a  :       Union[str, Any]             =	activation_dropout
   _a  :       str             =	activation_function
   _a  :       Optional[int]             =	init_std
   _a  :       Tuple             =	encoder_layerdrop
   _a  :       Optional[Any]             =	decoder_layerdrop
   _a  :       Union[str, Any]             =	use_cache
   _a  :       List[str]             =	encoder_layers
   _a  :       List[Any]             =	scale_embedding  # scale factor will be sqrt(d_model) if True
   _a  :       Dict             =	max_source_positions
   _a  :       Union[str, Any]             =	max_target_positions

   # Audio Classification-specific parameters. Feel free to ignore for other classes.
   _a  :       Union[str, Any]             =	classifier_proj_size
   _a  :       List[Any]             =	use_weighted_layer_sum

   # fine-tuning config parameters for SpecAugment: https://arxiv.org/abs/1904.08779
   _a  :       Optional[int]             =	apply_spec_augment
   _a  :       int             =	mask_time_prob
   _a  :       List[str]             =	mask_time_length
   _a  :       List[Any]             =	mask_time_min_masks
   _a  :       str             =	mask_feature_prob
   _a  :       Dict             =	mask_feature_length
   _a  :       Optional[int]             =	mask_feature_min_masks

   _a  :       str             =	median_filter_width

   super().__init__(
       pad_token_id=__a							,						bos_token_id=__a							,						eos_token_id=__a							,						is_encoder_decoder=__a							,						decoder_start_token_id=__a							,						suppress_tokens=__a							,						begin_suppress_tokens=__a							,						**__a							,						)







class        UpperCamelCase			(	lowerCAmelCase__      ):


 @property
 def _lowercase	(     self		:						Dict							)					->       List[Any]:
   _a  :       List[Any]             =	OrderedDict(
       [
           ("""input_features""", {0: """batch""", 1: """feature_size""", 2: """encoder_sequence"""}),
       ]							)
   if self.use_past:
     _a  :       Any             =	{0: """batch"""}
   else:
     _a  :       str             =	{0: """batch""", 1: """decoder_sequence"""}

   if self.use_past:
     self.fill_with_past_key_values_(__a							,						direction="""inputs"""							)

   return common_inputs


 def _lowercase	(     self		:						Tuple							,						UpperCAmelCase__		:						Union[str, Any]							,						UpperCAmelCase__		:						Union[str, Any] = -1							,						UpperCAmelCase__		:						List[str] = -1							,						UpperCAmelCase__		:						str = False							,						UpperCAmelCase__		:						List[str] = None							,						UpperCAmelCase__		:						Tuple = 22050							,						UpperCAmelCase__		:						Tuple = 5.0							,						UpperCAmelCase__		:						Any = 220							,						)					->       Union[str, Any]:
   _a  :       Dict             =	OrderedDict()
   _a  :       List[str]             =	OnnxConfig.generate_dummy_inputs(
       self							,						preprocessor=preprocessor.feature_extractor							,						batch_size=__a							,						framework=__a							,						sampling_rate=__a							,						time_duration=__a							,						frequency=__a							,						)
   _a  :       str             =	encoder_inputs["""input_features"""].shape[2]
   _a  :       List[Any]             =	encoder_sequence_length // 2 if self.use_past else seq_length

   _a  :       Dict             =	super().generate_dummy_inputs(
       preprocessor.tokenizer							,						__a							,						__a							,						__a							,						__a							)

   _a  :       Optional[Any]             =	encoder_inputs.pop("""input_features"""							)
   _a  :       List[str]             =	decoder_inputs.pop("""decoder_input_ids"""							)

   if "past_key_values" in decoder_inputs:
     _a  :       Any             =	decoder_inputs.pop("""past_key_values"""							)

   return dummy_inputs







 @property
 def _lowercase	(     self		:						str							)					->       Any:
   return 1E-3





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

A     :   Tuple			    =			[
    ("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     :   Optional[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 _lowerCamelCase	(		_UpperCamelCase      ):

			'''simple docstring'''




			__lowerCAmelCase				    =			torch.load(_UpperCamelCase    , map_location="cpu"      )
			return sd


def _lowerCamelCase	(		_UpperCamelCase    , _UpperCamelCase    , _UpperCamelCase=rename_keys_prefix      ):

			'''simple docstring'''




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


@torch.no_grad()
def _lowerCamelCase	(		_UpperCamelCase    , _UpperCamelCase      ):

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

			__lowerCAmelCase				    =			VisualBertConfig(**_UpperCamelCase      )

			# Load State Dict
			__lowerCAmelCase				    =			load_state_dict(_UpperCamelCase      )

			__lowerCAmelCase				    =			get_new_dict(_UpperCamelCase    , _UpperCamelCase      )

			if model_type == "pretraining":
						__lowerCAmelCase				    =			VisualBertForPreTraining(_UpperCamelCase      )
			elif model_type == "vqa":
						__lowerCAmelCase				    =			VisualBertForQuestionAnswering(_UpperCamelCase      )
			elif model_type == "nlvr":
						__lowerCAmelCase				    =			VisualBertForVisualReasoning(_UpperCamelCase      )
			elif model_type == "multichoice":
						__lowerCAmelCase				    =			VisualBertForMultipleChoice(_UpperCamelCase      )

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


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




