





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

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

a__: Optional[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       UpperCamelCase__(			UpperCamelCase__     :			List[Any]    )->Any:
       A__		   =		torch.load(UpperCamelCase__					,	map_location='''cpu'''    )
       return sd


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


@torch.no_grad()
def       UpperCamelCase__(			UpperCamelCase__     :			Tuple					,	UpperCamelCase__     :			int    )->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:
              A__		   =		'''pretraining'''
              if "vcr" in checkpoint_path:
                     A__		   =		{'''visual_embedding_dim''': 5_12}
              elif "vqa_advanced" in checkpoint_path:
                     A__		   =		{'''visual_embedding_dim''': 20_48}
              elif "vqa" in checkpoint_path:
                     A__		   =		{'''visual_embedding_dim''': 20_48}
              elif "nlvr" in checkpoint_path:
                     A__		   =		{'''visual_embedding_dim''': 10_24}
              else:
                     raise NotImplementedError(f"No implementation found for `{checkpoint_path}`."    )
       else:
              if "vcr" in checkpoint_path:
                     A__		   =		{'''visual_embedding_dim''': 5_12}
                     A__		   =		'''multichoice'''
              elif "vqa_advanced" in checkpoint_path:
                     A__		   =		{'''visual_embedding_dim''': 20_48}
                     A__		   =		'''vqa_advanced'''
              elif "vqa" in checkpoint_path:
                     A__		   =		{'''visual_embedding_dim''': 20_48, '''num_labels''': 31_29}
                     A__		   =		'''vqa'''
              elif "nlvr" in checkpoint_path:
                     A__		   =		{
                         '''visual_embedding_dim''': 10_24,
                         '''num_labels''': 2,
                     }
                     A__		   =		'''nlvr'''

       A__		   =		VisualBertConfig(**UpperCamelCase__    )

       # Load State Dict
       A__		   =		load_state_dict(UpperCamelCase__    )

       A__		   =		get_new_dict(UpperCamelCase__					,	UpperCamelCase__    )

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

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


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












import json
import os
import unittest

from transformers.models.ctrl.tokenization_ctrl import VOCAB_FILES_NAMES, CTRLTokenizer

from ...test_tokenization_common import TokenizerTesterMixin




class        SCREAMING_SNAKE_CASE__					(		UpperCamelCase__							,   unittest.TestCase ):
 __SCREAMING_SNAKE_CASE							=      CTRLTokenizer
 __SCREAMING_SNAKE_CASE							=      False
 __SCREAMING_SNAKE_CASE							=      False



 def 							UpperCamelCase     (				self					):
        super().setUp()

        # Adapted from Sennrich et al. 2015 and https://github.com/rsennrich/subword-nmt
        A__		   =		['''adapt''', '''re@@''', '''a@@''', '''apt''', '''c@@''', '''t''', '''<unk>''']
        A__		   =		dict(zip(__lowerCamelCase,range(len(__lowerCamelCase					)					)					)					)
        A__		   =		['''#version: 0.2''', '''a p''', '''ap t</w>''', '''r e''', '''a d''', '''ad apt</w>''', '''''']
        A__		   =		{'''unk_token''': '''<unk>'''}

        A__		   =		os.path.join(self.tmpdirname,VOCAB_FILES_NAMES['''vocab_file''']					)
        A__		   =		os.path.join(self.tmpdirname,VOCAB_FILES_NAMES['''merges_file''']					)
        with open(self.vocab_file,'''w''',encoding='''utf-8'''					) as fp:
               fp.write(json.dumps(__lowerCamelCase					) + '''\n'''					)
        with open(self.merges_file,'''w''',encoding='''utf-8'''					) as fp:
               fp.write('''\n'''.join(__lowerCamelCase					)					)



 def 							UpperCamelCase     (				self,**__lowerCamelCase					):
        kwargs.update(self.special_tokens_map					)
        return CTRLTokenizer.from_pretrained(self.tmpdirname,**__lowerCamelCase					)



 def 							UpperCamelCase     (				self,__lowerCamelCase					):
        A__		   =		'''adapt react readapt apt'''
        A__		   =		'''adapt react readapt apt'''
        return input_text, output_text




 def 							UpperCamelCase     (				self					):
        A__		   =		CTRLTokenizer(self.vocab_file,self.merges_file,**self.special_tokens_map					)
        A__		   =		'''adapt react readapt apt'''
        A__		   =		'''adapt re@@ a@@ c@@ t re@@ adapt apt'''.split()
        A__		   =		tokenizer.tokenize(__lowerCamelCase					)
        self.assertListEqual(__lowerCamelCase,__lowerCamelCase					)

        A__		   =		tokens + [tokenizer.unk_token]

        A__		   =		[0, 1, 2, 4, 5, 1, 0, 3, 6]
        self.assertListEqual(tokenizer.convert_tokens_to_ids(__lowerCamelCase					),__lowerCamelCase					)





