
"""simple docstring"""





import json
from typing import List, Optional, Tuple

from tokenizers import normalizers

from ...tokenization_utils_base import BatchEncoding
from ...tokenization_utils_fast import PreTrainedTokenizerFast
from ...utils import PaddingStrategy, logging
from .tokenization_realm import RealmTokenizer


_a								=						logging.get_logger(__name__)

_a								=						{"""vocab_file""": """vocab.txt""", """tokenizer_file""": """tokenizer.json"""}

_a								=						{
    """vocab_file""": {
        """google/realm-cc-news-pretrained-embedder""": (
            """https://huggingface.co/google/realm-cc-news-pretrained-embedder/resolve/main/vocab.txt"""
        ),
        """google/realm-cc-news-pretrained-encoder""": (
            """https://huggingface.co/google/realm-cc-news-pretrained-encoder/resolve/main/vocab.txt"""
        ),
        """google/realm-cc-news-pretrained-scorer""": (
            """https://huggingface.co/google/realm-cc-news-pretrained-scorer/resolve/main/vocab.txt"""
        ),
        """google/realm-cc-news-pretrained-openqa""": (
            """https://huggingface.co/google/realm-cc-news-pretrained-openqa/aresolve/main/vocab.txt"""
        ),
        """google/realm-orqa-nq-openqa""": """https://huggingface.co/google/realm-orqa-nq-openqa/resolve/main/vocab.txt""",
        """google/realm-orqa-nq-reader""": """https://huggingface.co/google/realm-orqa-nq-reader/resolve/main/vocab.txt""",
        """google/realm-orqa-wq-openqa""": """https://huggingface.co/google/realm-orqa-wq-openqa/resolve/main/vocab.txt""",
        """google/realm-orqa-wq-reader""": """https://huggingface.co/google/realm-orqa-wq-reader/resolve/main/vocab.txt""",
    },
    """tokenizer_file""": {
        """google/realm-cc-news-pretrained-embedder""": (
            """https://huggingface.co/google/realm-cc-news-pretrained-embedder/resolve/main/tokenizer.jsont"""
        ),
        """google/realm-cc-news-pretrained-encoder""": (
            """https://huggingface.co/google/realm-cc-news-pretrained-encoder/resolve/main/tokenizer.json"""
        ),
        """google/realm-cc-news-pretrained-scorer""": (
            """https://huggingface.co/google/realm-cc-news-pretrained-scorer/resolve/main/tokenizer.json"""
        ),
        """google/realm-cc-news-pretrained-openqa""": (
            """https://huggingface.co/google/realm-cc-news-pretrained-openqa/aresolve/main/tokenizer.json"""
        ),
        """google/realm-orqa-nq-openqa""": (
            """https://huggingface.co/google/realm-orqa-nq-openqa/resolve/main/tokenizer.json"""
        ),
        """google/realm-orqa-nq-reader""": (
            """https://huggingface.co/google/realm-orqa-nq-reader/resolve/main/tokenizer.json"""
        ),
        """google/realm-orqa-wq-openqa""": (
            """https://huggingface.co/google/realm-orqa-wq-openqa/resolve/main/tokenizer.json"""
        ),
        """google/realm-orqa-wq-reader""": (
            """https://huggingface.co/google/realm-orqa-wq-reader/resolve/main/tokenizer.json"""
        ),
    },
}

_a								=						{
    """google/realm-cc-news-pretrained-embedder""": 512,
    """google/realm-cc-news-pretrained-encoder""": 512,
    """google/realm-cc-news-pretrained-scorer""": 512,
    """google/realm-cc-news-pretrained-openqa""": 512,
    """google/realm-orqa-nq-openqa""": 512,
    """google/realm-orqa-nq-reader""": 512,
    """google/realm-orqa-wq-openqa""": 512,
    """google/realm-orqa-wq-reader""": 512,
}

_a								=						{
    """google/realm-cc-news-pretrained-embedder""": {"""do_lower_case""": True},
    """google/realm-cc-news-pretrained-encoder""": {"""do_lower_case""": True},
    """google/realm-cc-news-pretrained-scorer""": {"""do_lower_case""": True},
    """google/realm-cc-news-pretrained-openqa""": {"""do_lower_case""": True},
    """google/realm-orqa-nq-openqa""": {"""do_lower_case""": True},
    """google/realm-orqa-nq-reader""": {"""do_lower_case""": True},
    """google/realm-orqa-wq-openqa""": {"""do_lower_case""": True},
    """google/realm-orqa-wq-reader""": {"""do_lower_case""": True},
}






class        _UpperCAmelCase(       lowerCamelCase      ):
		lowercase__         = VOCAB_FILES_NAMES
		lowercase__         = PRETRAINED_VOCAB_FILES_MAP
		lowercase__         = PRETRAINED_INIT_CONFIGURATION
		lowercase__         = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
		lowercase__         = RealmTokenizer



		def __init__(   self							,						__a=None							,						__a=None							,						__a=True							,						__a="[UNK]"							,						__a="[SEP]"							,						__a="[PAD]"							,						__a="[CLS]"							,						__a="[MASK]"							,						__a=True							,						__a=None							,						**__a							,						)			->			str:






							'''simple docstring'''
							super().__init__(
							    __a							,						tokenizer_file=__a							,						do_lower_case=__a							,						unk_token=__a							,						sep_token=__a							,						pad_token=__a							,						cls_token=__a							,						mask_token=__a							,						tokenize_chinese_chars=__a							,						strip_accents=__a							,						**__a							,						)

							_UpperCamelCase									=    json.loads(self.backend_tokenizer.normalizer.__getstate__())
							if (
							    normalizer_state.get('''lowercase'''							,						__a) != do_lower_case
							    or normalizer_state.get('''strip_accents'''							,						__a) != strip_accents
							    or normalizer_state.get('''handle_chinese_chars'''							,						__a) != tokenize_chinese_chars
							):
												_UpperCamelCase									=    getattr(__a							,						normalizer_state.pop('''type'''))
												_UpperCamelCase									=    do_lower_case
												_UpperCamelCase									=    strip_accents
												_UpperCamelCase									=    tokenize_chinese_chars
												_UpperCamelCase									=    normalizer_class(**__a)

							_UpperCamelCase									=    do_lower_case



		def 		UpperCAmelCase				(   self							,						__a							,						**__a)			->			List[str]:






							'''simple docstring'''
							_UpperCamelCase									=    PaddingStrategy.MAX_LENGTH

							_UpperCamelCase									=    text
							_UpperCamelCase									=    kwargs.pop('''text_pair'''							,						__a)
							_UpperCamelCase									=    kwargs.pop('''return_tensors'''							,						__a)

							_UpperCamelCase									=    {
							    '''input_ids''': [],
							    '''attention_mask''': [],
							    '''token_type_ids''': [],
							}

							for idx, candidate_text in enumerate(__a):
												if batch_text_pair is not None:
																	_UpperCamelCase									=    batch_text_pair[idx]
												else:
																	_UpperCamelCase									=    None

												_UpperCamelCase									=    super().__call__(__a							,						__a							,						return_tensors=__a							,						**__a)

												_UpperCamelCase									=    encoded_candidates.get('''input_ids''')
												_UpperCamelCase									=    encoded_candidates.get('''attention_mask''')
												_UpperCamelCase									=    encoded_candidates.get('''token_type_ids''')

												if encoded_input_ids is not None:
																	output_data["input_ids"].append(__a)
												if encoded_attention_mask is not None:
																	output_data["attention_mask"].append(__a)
												if encoded_token_type_ids is not None:
																	output_data["token_type_ids"].append(__a)

							_UpperCamelCase									=    {key: item for key, item in output_data.items() if len(__a) != 0}

							return BatchEncoding(__a							,						tensor_type=__a)



		def 		UpperCAmelCase				(   self							,						__a							,						__a=None)			->			Union[str, Any]:






							'''simple docstring'''
							_UpperCamelCase									=    [self.cls_token_id] + token_ids_a + [self.sep_token_id]

							if token_ids_a:
												output += token_ids_a + [self.sep_token_id]

							return output



		def 		UpperCAmelCase				(   self							,						__a							,						__a = None)			->			List[int]:






							'''simple docstring'''
							_UpperCamelCase									=    [self.sep_token_id]
							_UpperCamelCase									=    [self.cls_token_id]
							if token_ids_a is None:
												return len(cls + token_ids_a + sep) * [0]
							return len(cls + token_ids_a + sep) * [0] + len(token_ids_a + sep) * [1]




		def 		UpperCAmelCase				(   self							,						__a							,						__a = None)			->			Tuple[str]:






							'''simple docstring'''
							_UpperCamelCase									=    self._tokenizer.model.save(__a							,						name=__a)
							return tuple(__a)












"""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()
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 		_snake_case	(   _snake_case				:   Optional[Any]				)       ->   str:






      '''simple docstring'''


      _A     =  torch.load(_snake_case							,    map_location='cpu'				)
      return sd






def 		_snake_case	(   _snake_case				:   Union[str, Any]							,    _snake_case				:   str							,    _snake_case				:   Tuple=rename_keys_prefix				)       ->   List[str]:






      '''simple docstring'''


      _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 		_snake_case	(   _snake_case				:   List[str]							,    _snake_case				:   Dict				)       ->   Dict:






      '''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     =  '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(**_snake_case				)

      # Load State Dict
      _A     =  load_state_dict(_snake_case				)

      _A     =  get_new_dict(_snake_case							,    _snake_case				)

      if model_type == "pretraining":
            _A     =  VisualBertForPreTraining(_snake_case				)
      elif model_type == "vqa":
            _A     =  VisualBertForQuestionAnswering(_snake_case				)
      elif model_type == "nlvr":
            _A     =  VisualBertForVisualReasoning(_snake_case				)
      elif model_type == "multichoice":
            _A     =  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__":
       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)

