



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

_UpperCAmelCase     =			[
    ('''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     =			[
    '''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      __magic_name__				(							lowercase ):
   SCREAMING_SNAKE_CASE_: str     			=torch.load(_lowercase							,			map_location="""cpu""" )
   return sd





def      __magic_name__				(							lowercase							,			lowercase							,			lowercase=rename_keys_prefix ):
   SCREAMING_SNAKE_CASE_: Dict     			=OrderedDict()
   SCREAMING_SNAKE_CASE_: Optional[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
      SCREAMING_SNAKE_CASE_: Any     			=key
      for name_pair in rename_keys_prefix:
         SCREAMING_SNAKE_CASE_: Any     			=new_key.replace(name_pair[0]							,			name_pair[1] )
      SCREAMING_SNAKE_CASE_: Union[str, Any]     			=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[str]     			=new_d['cls.predictions.bias']
   return new_d





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

   SCREAMING_SNAKE_CASE_: Optional[int]     			=VisualBertConfig(**_lowercase )

   # Load State Dict
   SCREAMING_SNAKE_CASE_: Tuple     			=load_state_dict(_lowercase )

   SCREAMING_SNAKE_CASE_: List[Any]     			=get_new_dict(_lowercase							,			_lowercase )

   if model_type == "pretraining":
      SCREAMING_SNAKE_CASE_: Optional[int]     			=VisualBertForPreTraining(_lowercase )
   elif model_type == "vqa":
      SCREAMING_SNAKE_CASE_: int     			=VisualBertForQuestionAnswering(_lowercase )
   elif model_type == "nlvr":
      SCREAMING_SNAKE_CASE_: List[Any]     			=VisualBertForVisualReasoning(_lowercase )
   elif model_type == "multichoice":
      SCREAMING_SNAKE_CASE_: Dict     			=VisualBertForMultipleChoice(_lowercase )

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


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








"""simple docstring"""
from __future__ import annotations





def      __magic_name__				(							lowercase							,			lowercase ):
   SCREAMING_SNAKE_CASE_: List[Any]     			=sorted(numsa + numsa )
   SCREAMING_SNAKE_CASE_			,		SCREAMING_SNAKE_CASE_: Tuple     			=divmod(len(lowercase )							,			2 )
   if mod == 1:
      return all_numbers[div]
   else:
      return (all_numbers[div] + all_numbers[div - 1]) / 2


if __name__ == "__main__":
   import doctest

   doctest.testmod()
   _UpperCAmelCase     =			[float(x) for x in input("""Enter the elements of first array: """).split()]
   _UpperCAmelCase     =			[float(x) for x in input("""Enter the elements of second array: """).split()]
   print(f"""The median of two arrays is: {median_of_two_arrays(array_a, array_a)}""")



