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    __lowercase     (						lowerCamelCase					:     List[Any]	):
 UpperCamelCase_					:						List[str] =					torch.load(lowerCamelCase  ,  map_location='cpu'	)
 return sd





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





@torch.no_grad()
def    __lowercase     (						lowerCamelCase					:     Union[str, Any]  ,  lowerCamelCase					:     str	):
 assert (
     checkpoint_path.split('/'	)[-1] in ACCEPTABLE_CHECKPOINTS
 ), F"The checkpoint provided must be in {ACCEPTABLE_CHECKPOINTS}."

 # Get Config
 if "pre" in checkpoint_path:
  UpperCamelCase_					:						str =					'pretraining'
  if "vcr" in checkpoint_path:
   UpperCamelCase_					:						List[Any] =					{'visual_embedding_dim': 512}
  elif "vqa_advanced" in checkpoint_path:
   UpperCamelCase_					:						List[str] =					{'visual_embedding_dim': 2048}
  elif "vqa" in checkpoint_path:
   UpperCamelCase_					:						List[Any] =					{'visual_embedding_dim': 2048}
  elif "nlvr" in checkpoint_path:
   UpperCamelCase_					:						str =					{'visual_embedding_dim': 1024}
  else:
   raise NotImplementedError(F"No implementation found for `{checkpoint_path}`."	)
 else:
  if "vcr" in checkpoint_path:
   UpperCamelCase_					:						Any =					{'visual_embedding_dim': 512}
   UpperCamelCase_					:						List[str] =					'multichoice'
  elif "vqa_advanced" in checkpoint_path:
   UpperCamelCase_					:						List[Any] =					{'visual_embedding_dim': 2048}
   UpperCamelCase_					:						Tuple =					'vqa_advanced'
  elif "vqa" in checkpoint_path:
   UpperCamelCase_					:						Dict =					{'visual_embedding_dim': 2048, 'num_labels': 3129}
   UpperCamelCase_					:						Any =					'vqa'
  elif "nlvr" in checkpoint_path:
   UpperCamelCase_					:						Optional[int] =					{
       'visual_embedding_dim': 1024,
       'num_labels': 2,
   }
   UpperCamelCase_					:						Dict =					'nlvr'

 UpperCamelCase_					:						Tuple =					VisualBertConfig(**lowerCamelCase	)

 # Load State Dict
 UpperCamelCase_					:						Dict =					load_state_dict(lowerCamelCase	)

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

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

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


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)




import importlib
import os
import sys


# This is required to make the module import works (when the python process is running from the root of the repo)
sys.path.append('.')





def    __lowercase     (						lowerCamelCase					:     Any	):
 UpperCamelCase_					:						Union[str, Any] =					test_file.split(os.path.sep	)
 if components[0:2] != ["tests", "models"]:
  raise ValueError(
      '`test_file` should start with `tests/models/` (with `/` being the OS specific path separator). Got '
      F"{test_file} instead."	)
 UpperCamelCase_					:						str =					components[-1]
 if not test_fn.endswith('py'	):
  raise ValueError(F"`test_file` should be a python file. Got {test_fn} instead."	)
 if not test_fn.startswith('test_modeling_'	):
  raise ValueError(
      F"`test_file` should point to a file name of the form `test_modeling_*.py`. Got {test_fn} instead."	)

 UpperCamelCase_					:						Union[str, Any] =					components[:-1] + [test_fn.replace('.py'  ,  ''	)]
 UpperCamelCase_					:						List[Any] =					'.'.join(lowerCamelCase	)

 return test_module_path





def    __lowercase     (						lowerCamelCase					:     Optional[Any]	):
 UpperCamelCase_					:						List[Any] =					get_module_path(lowerCamelCase	)
 UpperCamelCase_					:						Union[str, Any] =					importlib.import_module(lowerCamelCase	)

 return test_module





def    __lowercase     (						lowerCamelCase					:     List[str]	):
 UpperCamelCase_					:						int =					[]
 UpperCamelCase_					:						Tuple =					get_test_module(lowerCamelCase	)
 for attr in dir(lowerCamelCase	):
  if attr.endswith('ModelTester'	):
   tester_classes.append(getattr(lowerCamelCase  ,  lowerCamelCase	)	)

    # sort with class names
 return sorted(lowerCamelCase  ,  key=lambda lowerCamelCase	: x.__name__	)





def    __lowercase     (						lowerCamelCase					:     str	):
 UpperCamelCase_					:						List[str] =					[]
 UpperCamelCase_					:						Union[str, Any] =					get_test_module(lowerCamelCase	)
 for attr in dir(lowerCamelCase	):
  UpperCamelCase_					:						Dict =					getattr(lowerCamelCase  ,  lowerCamelCase	)
  # (TF/Flax)ModelTesterMixin is also an attribute in specific model test module. Let's exclude them by checking
  # `all_model_classes` is not empty (which also excludes other special classes).
  UpperCamelCase_					:						Optional[int] =					getattr(lowerCamelCase  ,  'all_model_classes'  ,  []	)
  if len(lowerCamelCase	) > 0:
   test_classes.append(lowerCamelCase	)

    # sort with class names
 return sorted(lowerCamelCase  ,  key=lambda lowerCamelCase	: x.__name__	)





def    __lowercase     (						lowerCamelCase					:     Dict	):
 UpperCamelCase_					:						int =					get_test_classes(lowerCamelCase	)
 UpperCamelCase_					:						List[Any] =					set()
 for test_class in test_classes:
  model_classes.update(test_class.all_model_classes	)

 # sort with class names
 return sorted(lowerCamelCase  ,  key=lambda lowerCamelCase	: x.__name__	)





def    __lowercase     (						lowerCamelCase					:     Tuple	):
 UpperCamelCase_					:						int =					test_class()
 if hasattr(lowerCamelCase  ,  'setUp'	):
  test.setUp()

 UpperCamelCase_					:						List[Any] =					None
 if hasattr(lowerCamelCase  ,  'model_tester'	):
  # `(TF/Flax)ModelTesterMixin` has this attribute default to `None`. Let's skip this case.
  if test.model_tester is not None:
   UpperCamelCase_					:						Optional[Any] =					test.model_tester.__class__

 return model_tester





def    __lowercase     (						lowerCamelCase					:     Tuple  ,  lowerCamelCase					:     Dict	):
 UpperCamelCase_					:						Optional[Any] =					get_test_classes(lowerCamelCase	)

 UpperCamelCase_					:						Tuple =					[]
 for test_class in test_classes:
  if model_class in test_class.all_model_classes:
   target_test_classes.append(lowerCamelCase	)

    # sort with class names
 return sorted(lowerCamelCase  ,  key=lambda lowerCamelCase	: x.__name__	)





def    __lowercase     (						lowerCamelCase					:     Any  ,  lowerCamelCase					:     Tuple	):
 UpperCamelCase_					:						List[Any] =					get_test_classes_for_model(lowerCamelCase  ,  lowerCamelCase	)

 UpperCamelCase_					:						int =					[]
 for test_class in test_classes:
  UpperCamelCase_					:						Tuple =					get_model_tester_from_test_class(lowerCamelCase	)
  if tester_class is not None:
   tester_classes.append(lowerCamelCase	)

    # sort with class names
 return sorted(lowerCamelCase  ,  key=lambda lowerCamelCase	: x.__name__	)





def    __lowercase     (						lowerCamelCase					:     str	):
 UpperCamelCase_					:						Tuple =					get_test_classes(lowerCamelCase	)
 UpperCamelCase_					:						Tuple =					{test_class: get_model_tester_from_test_class(lowerCamelCase	) for test_class in test_classes}
 return test_tester_mapping





def    __lowercase     (						lowerCamelCase					:     Any	):
 UpperCamelCase_					:						List[str] =					get_model_classes(lowerCamelCase	)
 UpperCamelCase_					:						int =					{
     model_class: get_test_classes_for_model(lowerCamelCase  ,  lowerCamelCase	) for model_class in model_classes
 }
 return model_test_mapping





def    __lowercase     (						lowerCamelCase					:     Tuple	):
 UpperCamelCase_					:						Tuple =					get_model_classes(lowerCamelCase	)
 UpperCamelCase_					:						Optional[Any] =					{
     model_class: get_tester_classes_for_model(lowerCamelCase  ,  lowerCamelCase	) for model_class in model_classes
 }
 return model_to_tester_mapping






def    __lowercase     (						lowerCamelCase					:     Any	):
 if isinstance(lowerCamelCase  ,  lowerCamelCase	):
  return o
 elif isinstance(lowerCamelCase  ,  lowerCamelCase	):
  return o.__name__
 elif isinstance(lowerCamelCase  ,  (list, tuple)	):
  return [to_json(lowerCamelCase	) for x in o]
 elif isinstance(lowerCamelCase  ,  lowerCamelCase	):
  return {to_json(lowerCamelCase	): to_json(lowerCamelCase	) for k, v in o.items()}
 else:
  return o



