





'''simple docstring'''







import os
import sys
import tempfile

import torch

from .state import AcceleratorState
from .utils import PrecisionType, PrepareForLaunch, is_mps_available, patch_environment

def    __UpperCamelCase	(	a    : Any					,   a    : List[str]=()					,   a    : Tuple=None					,   a    : List[str]="no"					,   a    : Dict="29500"     ) ->int:
 snake_case				=			False
 snake_case				=			False
 if any(key.startswith('''KAGGLE'''     ) for key in os.environ.keys()     ):
  snake_case				=			True
 elif "IPython" in sys.modules:
  snake_case				=			'''google.colab''' in str(sys.modules['''IPython'''].get_ipython()     )

 try:
  snake_case				=			PrecisionType(mixed_precision.lower()     )
 except ValueError:
  raise ValueError(
      f"""Unknown mixed_precision mode: {args.mixed_precision.lower()}. Choose between {PrecisionType.list()}."""     )

 if (in_colab or in_kaggle) and (os.environ.get('''TPU_NAME'''					,   a     ) is not None):
  # TPU launch
  import torch_xla.distributed.xla_multiprocessing as xmp

  if len(AcceleratorState._shared_state     ) > 0:
   raise ValueError(
       '''To train on TPU in Colab or Kaggle Kernel, the `Accelerator` should only be initialized inside '''
       '''your training function. Restart your notebook and make sure no cells initializes an '''
       '''`Accelerator`.'''     )
  if num_processes is None:
   snake_case				=			8

  snake_case				=			PrepareForLaunch(a					,   distributed_type='''TPU'''     )
  print(f"""Launching a training on {num_processes} TPU cores."""     )
  xmp.spawn(a					,   args=a					,   nprocs=a					,   start_method='''fork'''     )
 elif in_colab:
  # No need for a distributed launch otherwise as it's either CPU or one GPU.
  if torch.cuda.is_available():
   print('''Launching training on one GPU.'''     )
  else:
   print('''Launching training on one CPU.'''     )
  function(*a     )
 else:
  if num_processes is None:
   raise ValueError(
       '''You have to specify the number of GPUs you would like to use, add `num_processes=...` to your call.'''     )

  if num_processes > 1:
   # Multi-GPU launch
   from torch.multiprocessing import start_processes
   from torch.multiprocessing.spawn import ProcessRaisedException

   if len(AcceleratorState._shared_state     ) > 0:
    raise ValueError(
        '''To launch a multi-GPU training from your notebook, the `Accelerator` should only be initialized '''
        '''inside your training function. Restart your notebook and make sure no cells initializes an '''
        '''`Accelerator`.'''     )

   if torch.cuda.is_initialized():
    raise ValueError(
        '''To launch a multi-GPU training from your notebook, you need to avoid running any instruction '''
        '''using `torch.cuda` in any cell. Restart your notebook and make sure no cells use any CUDA '''
        '''function.'''     )

   # torch.distributed will expect a few environment variable to be here. We set the ones common to each
   # process here (the other ones will be set be the launcher).
   with patch_environment(
       world_size=a					,   master_addr='''127.0.01'''					,   master_port=a					,   mixed_precision=a     ):
    snake_case				=			PrepareForLaunch(a					,   distributed_type='''MULTI_GPU'''     )
    print(f"""Launching training on {num_processes} GPUs."""     )
    try:
     start_processes(a					,   args=a					,   nprocs=a					,   start_method='''fork'''     )
    except ProcessRaisedException as e:
     if "Cannot re-initialize CUDA in forked subprocess" in e.args[0]:
      raise RuntimeError(
          '''CUDA has been initialized before the `notebook_launcher` could create a forked subprocess. '''
          '''This likely stems from an outside import causing issues once the `notebook_launcher()` is called. '''
          '''Please review your imports and test them when running the `notebook_launcher()` to identify '''
          '''which one is problematic.'''     ) from e

  else:
   # No need for a distributed launch otherwise as it's either CPU, GPU or MPS.
   if is_mps_available():
    snake_case				=			'''1'''
    print('''Launching training on MPS.'''     )
   elif torch.cuda.is_available():
    print('''Launching training on one GPU.'''     )
   else:
    print('''Launching training on CPU.'''     )
   function(*a     )






def    __UpperCamelCase	(	a    : Dict					,   a    : Optional[int]=()					,   a    : Dict=2     ) ->int:
 from torch.multiprocessing import start_processes

 with tempfile.NamedTemporaryFile() as tmp_file:
  # torch.distributed will expect a few environment variable to be here. We set the ones common to each
  # process here (the other ones will be set be the launcher).
  with patch_environment(
      world_size=a					,   master_addr='''127.0.01'''					,   master_port='''29500'''					,   accelerate_mixed_precision='''no'''					,   accelerate_debug_rdv_file=tmp_file.name					,   accelerate_use_cpu='''yes'''					,   ):
   snake_case				=			PrepareForLaunch(a					,   debug=a     )
   start_processes(a					,   args=a					,   nprocs=a					,   start_method='''fork'''     )








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

_lowercase       						=			[
    ('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'),
]

_lowercase       						=			[
    '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    __UpperCamelCase	(	a    : List[str]     ) ->Optional[int]:
 snake_case				=			torch.load(a					,   map_location='''cpu'''     )
 return sd

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

@torch.no_grad()
def    __UpperCamelCase	(	a    : Optional[int]					,   a    : int     ) ->Union[str, Any]:
 assert (
     checkpoint_path.split('''/'''     )[-1] in ACCEPTABLE_CHECKPOINTS
 ), f"""The checkpoint provided must be in {ACCEPTABLE_CHECKPOINTS}."""

 # Get Config
 if "pre" in checkpoint_path:
  snake_case				=			'''pretraining'''
  if "vcr" in checkpoint_path:
   snake_case				=			{'''visual_embedding_dim''': 512}
  elif "vqa_advanced" in checkpoint_path:
   snake_case				=			{'''visual_embedding_dim''': 2048}
  elif "vqa" in checkpoint_path:
   snake_case				=			{'''visual_embedding_dim''': 2048}
  elif "nlvr" in checkpoint_path:
   snake_case				=			{'''visual_embedding_dim''': 1024}
  else:
   raise NotImplementedError(f"""No implementation found for `{checkpoint_path}`."""     )
 else:
  if "vcr" in checkpoint_path:
   snake_case				=			{'''visual_embedding_dim''': 512}
   snake_case				=			'''multichoice'''
  elif "vqa_advanced" in checkpoint_path:
   snake_case				=			{'''visual_embedding_dim''': 2048}
   snake_case				=			'''vqa_advanced'''
  elif "vqa" in checkpoint_path:
   snake_case				=			{'''visual_embedding_dim''': 2048, '''num_labels''': 3129}
   snake_case				=			'''vqa'''
  elif "nlvr" in checkpoint_path:
   snake_case				=			{
       '''visual_embedding_dim''': 1024,
       '''num_labels''': 2,
   }
   snake_case				=			'''nlvr'''

 snake_case				=			VisualBertConfig(**a     )

 # Load State Dict
 snake_case				=			load_state_dict(a     )

 snake_case				=			get_new_dict(a					,   a     )

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

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


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

