


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_					:   Union[str, 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 							_lowerCAmelCase (  _a   :					Optional[int]			)	->			Optional[int]:
     lowerCAmelCase_					:       int    =		torch.load(_a					,   map_location="""cpu"""			)
     return sd






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






@torch.no_grad()
def 							_lowerCAmelCase (  _a   :					str					,   _a   :					Dict			)	->			List[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:
          lowerCAmelCase_					:       Tuple    =		"""pretraining"""
          if "vcr" in checkpoint_path:
               lowerCAmelCase_					:       Union[str, Any]    =		{"""visual_embedding_dim""": 5_12}
          elif "vqa_advanced" in checkpoint_path:
               lowerCAmelCase_					:       int    =		{"""visual_embedding_dim""": 20_48}
          elif "vqa" in checkpoint_path:
               lowerCAmelCase_					:       Optional[int]    =		{"""visual_embedding_dim""": 20_48}
          elif "nlvr" in checkpoint_path:
               lowerCAmelCase_					:       List[Any]    =		{"""visual_embedding_dim""": 10_24}
          else:
               raise NotImplementedError(F'No implementation found for `{checkpoint_path}`.'			)
     else:
          if "vcr" in checkpoint_path:
               lowerCAmelCase_					:       Optional[Any]    =		{"""visual_embedding_dim""": 5_12}
               lowerCAmelCase_					:       str    =		"""multichoice"""
          elif "vqa_advanced" in checkpoint_path:
               lowerCAmelCase_					:       int    =		{"""visual_embedding_dim""": 20_48}
               lowerCAmelCase_					:       int    =		"""vqa_advanced"""
          elif "vqa" in checkpoint_path:
               lowerCAmelCase_					:       List[str]    =		{"""visual_embedding_dim""": 20_48, """num_labels""": 31_29}
               lowerCAmelCase_					:       List[Any]    =		"""vqa"""
          elif "nlvr" in checkpoint_path:
               lowerCAmelCase_					:       Optional[Any]    =		{
                   """visual_embedding_dim""": 10_24,
                   """num_labels""": 2,
               }
               lowerCAmelCase_					:       Union[str, Any]    =		"""nlvr"""

     lowerCAmelCase_					:       Dict    =		VisualBertConfig(**_a			)

     # Load State Dict
     lowerCAmelCase_					:       Any    =		load_state_dict(_a			)

     lowerCAmelCase_					:       Dict    =		get_new_dict(_a					,   _a			)

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

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


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







from ...configuration_utils import PretrainedConfig
from ...utils import logging
from ...utils.backbone_utils import BackboneConfigMixin, get_aligned_output_features_output_indices


UpperCAmelCase_					:   List[str] 							=						logging.get_logger(__name__)

UpperCAmelCase_					:   int 							=						{
    """google/bit-50""": """https://huggingface.co/google/bit-50/resolve/main/config.json""",
}





class      lowercase__    (							__A     ,					__A  ):
  __UpperCamelCase	     =  """bit"""
  __UpperCamelCase	     =  ["""preactivation""", """bottleneck"""]
  __UpperCamelCase	     =  ["""SAME""", """VALID"""]







  def __init__(	self    ,						_lowercase=3    ,						_lowercase=64    ,						_lowercase=[256, 512, 1_024, 2_048]    ,						_lowercase=[3, 4, 6, 3]    ,						_lowercase="preactivation"    ,						_lowercase="relu"    ,						_lowercase=None    ,						_lowercase=32    ,						_lowercase=0.0    ,						_lowercase=False    ,						_lowercase=32    ,						_lowercase=1    ,						_lowercase=None    ,						_lowercase=None    ,						**_lowercase    ,						):
       super().__init__(**_lowercase						)
       if layer_type not in self.layer_types:
            raise ValueError(F'layer_type={layer_type} is not one of {",".join(self.layer_types						)}'						)
       if global_padding is not None:
            if global_padding.upper() in self.supported_padding:
                 lowerCAmelCase_					:       int    =		global_padding.upper()
            else:
                 raise ValueError(F'Padding strategy {global_padding} not supported'						)
       lowerCAmelCase_					:       Optional[Any]    =		num_channels
       lowerCAmelCase_					:       List[str]    =		embedding_size
       lowerCAmelCase_					:       Dict    =		hidden_sizes
       lowerCAmelCase_					:       Optional[int]    =		depths
       lowerCAmelCase_					:       List[Any]    =		layer_type
       lowerCAmelCase_					:       Union[str, Any]    =		hidden_act
       lowerCAmelCase_					:       Optional[int]    =		global_padding
       lowerCAmelCase_					:       Optional[Any]    =		num_groups
       lowerCAmelCase_					:       Optional[Any]    =		drop_path_rate
       lowerCAmelCase_					:       Tuple    =		embedding_dynamic_padding
       lowerCAmelCase_					:       Any    =		output_stride
       lowerCAmelCase_					:       List[Any]    =		width_factor

       lowerCAmelCase_					:       Union[str, Any]    =		["""stem"""] + [F'stage{idx}' for idx in range(1    ,						len(_lowercase						) + 1						)]
       lowerCAmelCase_   ,  lowerCAmelCase_					:       Tuple    =		get_aligned_output_features_output_indices(
           out_features=_lowercase    ,						out_indices=_lowercase    ,						stage_names=self.stage_names						)
