





"""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 					__A  (_SCREAMING_SNAKE_CASE     )   ->Dict:





  """simple docstring"""


  lowerCAmelCase__     :str											=				torch.load(_SCREAMING_SNAKE_CASE	,  map_location='cpu'     )
  return sd
def 					__A  (_SCREAMING_SNAKE_CASE	,  _SCREAMING_SNAKE_CASE	,  _SCREAMING_SNAKE_CASE=rename_keys_prefix     )   ->Dict:





  """simple docstring"""


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





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

  lowerCAmelCase__     :int											=				VisualBertConfig(**_SCREAMING_SNAKE_CASE     )

  # Load State Dict
  lowerCAmelCase__     :int											=				load_state_dict(_SCREAMING_SNAKE_CASE     )

  lowerCAmelCase__     :List[Any]											=				get_new_dict(_SCREAMING_SNAKE_CASE	,  _SCREAMING_SNAKE_CASE     )

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

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


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)







"""simple docstring"""





import re
def 					__A  (_SCREAMING_SNAKE_CASE     )   ->list:





  """simple docstring"""


  return [char.split() for char in re.split(r'[^ a-z A-Z 0-9 \s]'	,  str_     )]
def 					__A  (_SCREAMING_SNAKE_CASE     )   ->str:





  """simple docstring"""


  lowerCAmelCase__     :Optional[Any]											=				split_input(str_     )
  return "".join(
      [''.join([char.capitalize() for char in sub_str]     ) for sub_str in string_split]     )
def 					__A  (_SCREAMING_SNAKE_CASE	,  _SCREAMING_SNAKE_CASE	,  _SCREAMING_SNAKE_CASE     )   ->str:





  """simple docstring"""


  try:
    lowerCAmelCase__     :Any											=				split_input(_SCREAMING_SNAKE_CASE     )
    if upper:
      lowerCAmelCase__     :str											=				''.join(
          [
              separator.join([char.upper() for char in sub_str]     )
              for sub_str in string_split
          ]     )
    else:
      lowerCAmelCase__     :int											=				''.join(
          [
              separator.join([char.lower() for char in sub_str]     )
              for sub_str in string_split
          ]     )
    return res_str
  except IndexError:
    return "not valid string"
def 					__A  (_SCREAMING_SNAKE_CASE     )   ->str:





  """simple docstring"""


  return to_simple_case(_SCREAMING_SNAKE_CASE     )
def 					__A  (_SCREAMING_SNAKE_CASE     )   ->str:





  """simple docstring"""


  try:
    lowerCAmelCase__     :str											=				to_simple_case(_SCREAMING_SNAKE_CASE     )
    return res_str[0].lower() + res_str[1:]
  except IndexError:
    return "not valid string"
def 					__A  (_SCREAMING_SNAKE_CASE	,  _SCREAMING_SNAKE_CASE     )   ->str:





  """simple docstring"""


  return to_complex_case(_SCREAMING_SNAKE_CASE	,  _SCREAMING_SNAKE_CASE	,  '_'     )
def 					__A  (_SCREAMING_SNAKE_CASE	,  _SCREAMING_SNAKE_CASE     )   ->str:





  """simple docstring"""


  return to_complex_case(_SCREAMING_SNAKE_CASE	,  _SCREAMING_SNAKE_CASE	,  '-'     )


if __name__ == "__main__":
  __import__("""doctest""").testmod()
