




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__			:    Optional[int]			     =      logging.get_logger(__name__)

snake_case__			:    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'''),
]

snake_case__			:    Union[str, 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     lowercase		(  _lowerCAmelCase   ):
  UpperCAmelCase__         = torch.load(__snake_case ,  map_location="""cpu"""   )
  return sd


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


@torch.no_grad()
def     lowercase		(  _lowerCAmelCase ,  _lowerCAmelCase   ):
  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__         = """pretraining"""
    if "vcr" in checkpoint_path:
      UpperCAmelCase__         = {"""visual_embedding_dim""": 512}
    elif "vqa_advanced" in checkpoint_path:
      UpperCAmelCase__         = {"""visual_embedding_dim""": 2048}
    elif "vqa" in checkpoint_path:
      UpperCAmelCase__         = {"""visual_embedding_dim""": 2048}
    elif "nlvr" in checkpoint_path:
      UpperCAmelCase__         = {"""visual_embedding_dim""": 1024}
    else:
      raise NotImplementedError(F'''No implementation found for `{checkpoint_path}`.'''   )
  else:
    if "vcr" in checkpoint_path:
      UpperCAmelCase__         = {"""visual_embedding_dim""": 512}
      UpperCAmelCase__         = """multichoice"""
    elif "vqa_advanced" in checkpoint_path:
      UpperCAmelCase__         = {"""visual_embedding_dim""": 2048}
      UpperCAmelCase__         = """vqa_advanced"""
    elif "vqa" in checkpoint_path:
      UpperCAmelCase__         = {"""visual_embedding_dim""": 2048, """num_labels""": 3129}
      UpperCAmelCase__         = """vqa"""
    elif "nlvr" in checkpoint_path:
      UpperCAmelCase__         = {
          """visual_embedding_dim""": 1024,
          """num_labels""": 2,
      }
      UpperCAmelCase__         = """nlvr"""

  UpperCAmelCase__         = VisualBertConfig(**__snake_case   )

  # Load State Dict
  UpperCAmelCase__         = load_state_dict(__snake_case   )

  UpperCAmelCase__         = get_new_dict(__snake_case ,  __snake_case   )

  if model_type == "pretraining":
    UpperCAmelCase__         = VisualBertForPreTraining(__snake_case   )
  elif model_type == "vqa":
    UpperCAmelCase__         = VisualBertForQuestionAnswering(__snake_case   )
  elif model_type == "nlvr":
    UpperCAmelCase__         = VisualBertForVisualReasoning(__snake_case   )
  elif model_type == "multichoice":
    UpperCAmelCase__         = VisualBertForMultipleChoice(__snake_case   )

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


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












import gc
import threading
import time

import psutil
import torch
class 		SCREAMING_SNAKE_CASE__	:

  '''simple docstring'''







  def __init__(      self		:			Union[str, Any]							)						->	str:


         """simple docstring"""
         _UpperCAmelCase							=    psutil.Process()
         _UpperCAmelCase							=    False


  def  lowerCamelCase       (      self		:			List[Any]							)						->	int:


         """simple docstring"""
         _UpperCAmelCase							=    -1

         while True:
                _UpperCAmelCase							=    max(self.process.memory_info().rss       ,				self.cpu_memory_peak							)

                # can't sleep or will not catch the peak right (this comment is here on purpose)
                if not self.peak_monitoring:
                       break


  def  lowerCamelCase       (      self		:			Optional[Any]							)						->	List[Any]:


         """simple docstring"""
         _UpperCAmelCase							=    True
         _UpperCAmelCase							=    threading.Thread(target=self.peak_monitor							)
         _UpperCAmelCase							=    True
         self.thread.start()





  def  lowerCamelCase       (      self		:			Optional[int]							)						->	List[str]:


         """simple docstring"""
         _UpperCAmelCase							=    False
         self.thread.join()
         return self.cpu_memory_peak



__a:     Union[str, Any]   					=					PeakCPUMemory()




def       _SCREAMING_SNAKE_CASE (							)				->						List[Any]:
       # Time
       _UpperCAmelCase							=    {"""time""": time.time()}

       gc.collect()
       torch.cuda.empty_cache()

       # CPU mem
       _UpperCAmelCase							=    psutil.Process().memory_info().rss
       cpu_peak_tracker.start()

       # GPU mem
       for i in range(torch.cuda.device_count() ):
              _UpperCAmelCase							=    torch.cuda.memory_allocated(__snake_case )
       torch.cuda.reset_peak_memory_stats()

       return measures




def       _SCREAMING_SNAKE_CASE (							__snake_case )				->						Union[str, Any]:
       # Time
       _UpperCAmelCase							=    {"""time""": time.time() - start_measures["""time"""]}

       gc.collect()
       torch.cuda.empty_cache()

       # CPU mem
       _UpperCAmelCase							=    (psutil.Process().memory_info().rss - start_measures["""cpu"""]) / 2**2_0
       _UpperCAmelCase							=    (cpu_peak_tracker.stop() - start_measures["""cpu"""]) / 2**2_0

       # GPU mem
       for i in range(torch.cuda.device_count() ):
              _UpperCAmelCase							=    (torch.cuda.memory_allocated(__snake_case ) - start_measures[str(__snake_case )]) / 2**2_0
              _UpperCAmelCase							=    (torch.cuda.max_memory_allocated(__snake_case ) - start_measures[str(__snake_case )]) / 2**2_0

       return measures





def       _SCREAMING_SNAKE_CASE (							__snake_case   ,      __snake_case )				->						str:
       print(f"""{description}:""" )
       print(f"""- Time: {measures["time"]:.2f}s""" )
       for i in range(torch.cuda.device_count() ):
              print(f"""- GPU {i} allocated: {measures[str(__snake_case )]:.2f}MiB""" )
              _UpperCAmelCase							=    measures[f"""{i}-peak"""]
              print(f"""- GPU {i} peak: {peak:.2f}MiB""" )
       print(f"""- CPU RAM allocated: {measures["cpu"]:.2f}MiB""" )
       print(f"""- CPU RAM peak: {measures["cpu-peak"]:.2f}MiB""" )