


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

_snake_case             =     [
    ("""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"""),
]

_snake_case             =     [
    """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 		lowercase_(  SCREAMING_SNAKE_CASE_	):


  '''simple docstring'''

  lowerCamelCase :					Dict    =				torch.load(lowerCAmelCase_      ,						map_location="cpu"	)
  return sd






def 		lowercase_(  SCREAMING_SNAKE_CASE_      ,						SCREAMING_SNAKE_CASE_      ,						SCREAMING_SNAKE_CASE_=rename_keys_prefix	):


  '''simple docstring'''

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






@torch.no_grad()
def 		lowercase_(  SCREAMING_SNAKE_CASE_      ,						SCREAMING_SNAKE_CASE_	):


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

  lowerCamelCase :					int    =				VisualBertConfig(**lowerCAmelCase_	)

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

  lowerCamelCase :					Optional[int]    =				get_new_dict(lowerCAmelCase_      ,						lowerCAmelCase_	)

  if model_type == "pretraining":
    lowerCamelCase :					List[str]    =				VisualBertForPreTraining(lowerCAmelCase_	)
  elif model_type == "vqa":
    lowerCamelCase :					Optional[int]    =				VisualBertForQuestionAnswering(lowerCAmelCase_	)
  elif model_type == "nlvr":
    lowerCamelCase :					str    =				VisualBertForVisualReasoning(lowerCAmelCase_	)
  elif model_type == "multichoice":
    lowerCamelCase :					Dict    =				VisualBertForMultipleChoice(lowerCAmelCase_	)

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


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








'''simple docstring'''






import argparse

import torch

from transformers import RemBertConfig, RemBertModel, load_tf_weights_in_rembert
from transformers.utils import logging


logging.set_verbosity_info()




def     snake_case_					(     lowerCAmelCase_       ,   lowerCAmelCase_       ,   lowerCAmelCase_		)->		Union[str, Any]:





       '''simple docstring'''


       _UpperCAmelCase  :						int			     = RemBertConfig.from_json_file(lowerCAmelCase_		)
       print("""Building PyTorch model from configuration: {}""".format(str(lowerCAmelCase_		)		)		)
       _UpperCAmelCase  :						Any			     = RemBertModel(lowerCAmelCase_		)

       # Load weights from tf checkpoint
       load_tf_weights_in_rembert(lowerCAmelCase_       ,   lowerCAmelCase_       ,   lowerCAmelCase_		)

       # Save pytorch-model
       print("""Save PyTorch model to {}""".format(lowerCAmelCase_		)		)
       torch.save(model.state_dict()       ,   lowerCAmelCase_		)


if __name__ == "__main__":
   A_							: Optional[Any]						 =	argparse.ArgumentParser()
   # Required parameters
   parser.add_argument(
       """--tf_checkpoint_path""", default=None, type=str, required=True, help="""Path to the TensorFlow checkpoint path."""
   )
   parser.add_argument(
       """--rembert_config_file""",
       default=None,
       type=str,
       required=True,
       help=(
           """The config json file corresponding to the pre-trained RemBERT model. \n"""
           """This specifies the model architecture."""
       ),
   )
   parser.add_argument(
       """--pytorch_dump_path""", default=None, type=str, required=True, help="""Path to the output PyTorch model."""
   )
   A_							: Any						 =	parser.parse_args()
   convert_rembert_tf_checkpoint_to_pytorch(args.tf_checkpoint_path, args.rembert_config_file, args.pytorch_dump_path)



