


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

UpperCAmelCase_				:			Tuple   =	[
    ('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'),
]

UpperCAmelCase_				:			List[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 	SCREAMING_SNAKE_CASE_						(      __A			:      Optional[Any]    )     ->		Optional[Any]:

       """simple docstring"""

       a_ :		Union[str, Any]							=  torch.load(__A  ,   map_location='cpu'    )
       return sd







def 	SCREAMING_SNAKE_CASE_						(      __A			:      str  ,   __A			:      Any  ,   __A			:      str=rename_keys_prefix    )     ->		Dict:

       """simple docstring"""

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







@torch.no_grad()
def 	SCREAMING_SNAKE_CASE_						(      __A			:      Dict  ,   __A			:      int    )     ->		List[str]:

       """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:
              a_ :		Union[str, Any]							=  'pretraining'
              if "vcr" in checkpoint_path:
                     a_ :		Dict							=  {'visual_embedding_dim': 5_12}
              elif "vqa_advanced" in checkpoint_path:
                     a_ :		List[Any]							=  {'visual_embedding_dim': 20_48}
              elif "vqa" in checkpoint_path:
                     a_ :		int							=  {'visual_embedding_dim': 20_48}
              elif "nlvr" in checkpoint_path:
                     a_ :		List[Any]							=  {'visual_embedding_dim': 10_24}
              else:
                     raise NotImplementedError(F"""No implementation found for `{checkpoint_path}`."""    )
       else:
              if "vcr" in checkpoint_path:
                     a_ :		int							=  {'visual_embedding_dim': 5_12}
                     a_ :		Dict							=  'multichoice'
              elif "vqa_advanced" in checkpoint_path:
                     a_ :		Any							=  {'visual_embedding_dim': 20_48}
                     a_ :		int							=  'vqa_advanced'
              elif "vqa" in checkpoint_path:
                     a_ :		List[Any]							=  {'visual_embedding_dim': 20_48, 'num_labels': 31_29}
                     a_ :		int							=  'vqa'
              elif "nlvr" in checkpoint_path:
                     a_ :		Optional[Any]							=  {
                         'visual_embedding_dim': 10_24,
                         'num_labels': 2,
                     }
                     a_ :		str							=  'nlvr'

       a_ :		Any							=  VisualBertConfig(**__A    )

       # Load State Dict
       a_ :		Tuple							=  load_state_dict(__A    )

       a_ :		Union[str, Any]							=  get_new_dict(__A  ,   __A    )

       if model_type == "pretraining":
              a_ :		Tuple							=  VisualBertForPreTraining(__A    )
       elif model_type == "vqa":
              a_ :		Any							=  VisualBertForQuestionAnswering(__A    )
       elif model_type == "nlvr":
              a_ :		Dict							=  VisualBertForVisualReasoning(__A    )
       elif model_type == "multichoice":
              a_ :		List[str]							=  VisualBertForMultipleChoice(__A    )

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


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






import unittest

from transformers import is_torch_available
from transformers.testing_utils import require_torch


if is_torch_available():
   import torch

   from transformers.activations import gelu_new, gelu_python, get_activation
@require_torch
class 			SCREAMING_SNAKE_CASE__  (    unittest.TestCase      ):
      def 	SCREAMING_SNAKE_CASE							(	self    :	Dict					)		->      List[str]:
             a_ :		List[str]							=  torch.tensor([-1_0_0, -1, -0.1, 0, 0.1, 1.0, 1_0_0]					)
             a_ :		Union[str, Any]							=  get_activation('gelu'					)
             self.assertTrue(torch.allclose(gelu_python(SCREAMING_SNAKE_CASE__					)					,							torch_builtin(SCREAMING_SNAKE_CASE__					)					)					)
             self.assertFalse(torch.allclose(gelu_python(SCREAMING_SNAKE_CASE__					)					,							gelu_new(SCREAMING_SNAKE_CASE__					)					)					)
      def 	SCREAMING_SNAKE_CASE							(	self    :	Union[str, Any]					)		->      Any:
             a_ :		List[str]							=  torch.tensor([-1_0_0, -1, -0.1, 0, 0.1, 1.0, 1_0_0]					)
             a_ :		Union[str, Any]							=  get_activation('gelu'					)
             a_ :		str							=  get_activation('gelu_10'					)

             a_ :		Tuple							=  torch_builtin(SCREAMING_SNAKE_CASE__					)
             a_ :		str							=  geluaa(SCREAMING_SNAKE_CASE__					)

             a_ :		List[Any]							=  torch.where(y_gelu_aa < 10.0					,							1					,							0					)

             self.assertTrue(torch.max(SCREAMING_SNAKE_CASE__					).item() == 10.0					)
             self.assertTrue(torch.allclose(y_gelu * clipped_mask					,							y_gelu_aa * clipped_mask					)					)
      def 	SCREAMING_SNAKE_CASE							(	self    :	Dict					)		->      List[Any]:
             get_activation('gelu'					)
             get_activation('gelu_10'					)
             get_activation('gelu_fast'					)
             get_activation('gelu_new'					)
             get_activation('gelu_python'					)
             get_activation('gelu_pytorch_tanh'					)
             get_activation('linear'					)
             get_activation('mish'					)
             get_activation('quick_gelu'					)
             get_activation('relu'					)
             get_activation('sigmoid'					)
             get_activation('silu'					)
             get_activation('swish'					)
             get_activation('tanh'					)
             with self.assertRaises(SCREAMING_SNAKE_CASE__					):
                    get_activation('bogus'					)
             with self.assertRaises(SCREAMING_SNAKE_CASE__					):
                    get_activation(SCREAMING_SNAKE_CASE__					)

      def 	SCREAMING_SNAKE_CASE							(	self    :	str					)		->      str:
             a_ :		Any							=  get_activation('gelu'					)
             a_ :		Any							=  1
             a_ :		int							=  get_activation('gelu'					)
             self.assertEqual(acta.a					,							1					)
             with self.assertRaises(SCREAMING_SNAKE_CASE__					):
                    a_ :		Tuple							=  acta.a


