


'''simple docstring'''




from argparse import ArgumentParser, Namespace

from ..utils import logging
from . import BaseTransformersCLICommand





def 	a_					( lowerCamelCase	:     Namespace				):
   return ConvertCommand(
       args.model_type	, args.tf_checkpoint	, args.pytorch_dump_output	, args.config	, args.finetuning_task_name				)


__snake_case    						="""
transformers can only be used from the commandline to convert TensorFlow models in PyTorch, In that case, it requires
TensorFlow to be installed. Please see https://www.tensorflow.org/install/ for installation instructions.
"""




class 		UpperCAmelCase_      (  __lowercase		):





     @staticmethod
     def 							__UpperCAmelCase      ( UpperCAmelCase__       :      ArgumentParser    )				->  Optional[int]:
        lowerCAmelCase     =			parser.add_parser(
            'convert'    ,      help='CLI tool to run convert model from original author checkpoints to Transformers PyTorch checkpoints.'    ,      )
        train_parser.add_argument('--model_type'    ,      type=UpperCAmelCase__    ,      required=UpperCAmelCase__    ,      help='Model\'s type.'    )
        train_parser.add_argument(
            '--tf_checkpoint'    ,      type=UpperCAmelCase__    ,      required=UpperCAmelCase__    ,      help='TensorFlow checkpoint path or folder.'    )
        train_parser.add_argument(
            '--pytorch_dump_output'    ,      type=UpperCAmelCase__    ,      required=UpperCAmelCase__    ,      help='Path to the PyTorch saved model output.'    )
        train_parser.add_argument('--config'    ,      type=UpperCAmelCase__    ,      default=''    ,      help='Configuration file path or folder.'    )
        train_parser.add_argument(
            '--finetuning_task_name'    ,      type=UpperCAmelCase__    ,      default=UpperCAmelCase__    ,      help='Optional fine-tuning task name if the TF model was a finetuned model.'    ,      )
        train_parser.set_defaults(func=UpperCAmelCase__    )





     def __init__( self       :      Optional[int]    ,      UpperCAmelCase__       :      str    ,      UpperCAmelCase__       :      str    ,      UpperCAmelCase__       :      str    ,      UpperCAmelCase__       :      str    ,      UpperCAmelCase__       :      str    ,      *UpperCAmelCase__       :      List[str]    ,      )				->  Optional[Any]:
        lowerCAmelCase     =			logging.get_logger('transformers-cli/converting'    )

        self._logger.info(F'''Loading model {model_type}'''    )
        lowerCAmelCase     =			model_type
        lowerCAmelCase     =			tf_checkpoint
        lowerCAmelCase     =			pytorch_dump_output
        lowerCAmelCase     =			config
        lowerCAmelCase     =			finetuning_task_name





     def 							__UpperCAmelCase      ( self       :      List[str]    )				->  Dict:
        if self._model_type == "albert":
           try:
              from ..models.albert.convert_albert_original_tf_checkpoint_to_pytorch import (
                  convert_tf_checkpoint_to_pytorch,
              )
           except ImportError:
              raise ImportError(UpperCAmelCase__    )

           convert_tf_checkpoint_to_pytorch(self._tf_checkpoint    ,      self._config    ,      self._pytorch_dump_output    )
        elif self._model_type == "bert":
           try:
              from ..models.bert.convert_bert_original_tf_checkpoint_to_pytorch import (
                  convert_tf_checkpoint_to_pytorch,
              )
           except ImportError:
              raise ImportError(UpperCAmelCase__    )

           convert_tf_checkpoint_to_pytorch(self._tf_checkpoint    ,      self._config    ,      self._pytorch_dump_output    )
        elif self._model_type == "funnel":
           try:
              from ..models.funnel.convert_funnel_original_tf_checkpoint_to_pytorch import (
                  convert_tf_checkpoint_to_pytorch,
              )
           except ImportError:
              raise ImportError(UpperCAmelCase__    )

           convert_tf_checkpoint_to_pytorch(self._tf_checkpoint    ,      self._config    ,      self._pytorch_dump_output    )
        elif self._model_type == "t5":
           try:
              from ..models.ta.convert_ta_original_tf_checkpoint_to_pytorch import convert_tf_checkpoint_to_pytorch
           except ImportError:
              raise ImportError(UpperCAmelCase__    )

           convert_tf_checkpoint_to_pytorch(self._tf_checkpoint    ,      self._config    ,      self._pytorch_dump_output    )
        elif self._model_type == "gpt":
           from ..models.openai.convert_openai_original_tf_checkpoint_to_pytorch import (
               convert_openai_checkpoint_to_pytorch,
           )

           convert_openai_checkpoint_to_pytorch(self._tf_checkpoint    ,      self._config    ,      self._pytorch_dump_output    )
        elif self._model_type == "transfo_xl":
           try:
              from ..models.transfo_xl.convert_transfo_xl_original_tf_checkpoint_to_pytorch import (
                  convert_transfo_xl_checkpoint_to_pytorch,
              )
           except ImportError:
              raise ImportError(UpperCAmelCase__    )

           if "ckpt" in self._tf_checkpoint.lower():
              lowerCAmelCase     =			self._tf_checkpoint
              lowerCAmelCase     =			''
           else:
              lowerCAmelCase     =			self._tf_checkpoint
              lowerCAmelCase     =			''
           convert_transfo_xl_checkpoint_to_pytorch(
               UpperCAmelCase__    ,      self._config    ,      self._pytorch_dump_output    ,      UpperCAmelCase__    )
        elif self._model_type == "gpt2":
           try:
              from ..models.gpta.convert_gpta_original_tf_checkpoint_to_pytorch import (
                  convert_gpta_checkpoint_to_pytorch,
              )
           except ImportError:
              raise ImportError(UpperCAmelCase__    )

           convert_gpta_checkpoint_to_pytorch(self._tf_checkpoint    ,      self._config    ,      self._pytorch_dump_output    )
        elif self._model_type == "xlnet":
           try:
              from ..models.xlnet.convert_xlnet_original_tf_checkpoint_to_pytorch import (
                  convert_xlnet_checkpoint_to_pytorch,
              )
           except ImportError:
              raise ImportError(UpperCAmelCase__    )

           convert_xlnet_checkpoint_to_pytorch(
               self._tf_checkpoint    ,      self._config    ,      self._pytorch_dump_output    ,      self._finetuning_task_name    )
        elif self._model_type == "xlm":
           from ..models.xlm.convert_xlm_original_pytorch_checkpoint_to_pytorch import (
               convert_xlm_checkpoint_to_pytorch,
           )

           convert_xlm_checkpoint_to_pytorch(self._tf_checkpoint    ,      self._pytorch_dump_output    )
        elif self._model_type == "lxmert":
           from ..models.lxmert.convert_lxmert_original_tf_checkpoint_to_pytorch import (
               convert_lxmert_checkpoint_to_pytorch,
           )

           convert_lxmert_checkpoint_to_pytorch(self._tf_checkpoint    ,      self._pytorch_dump_output    )
        elif self._model_type == "rembert":
           from ..models.rembert.convert_rembert_tf_checkpoint_to_pytorch import (
               convert_rembert_tf_checkpoint_to_pytorch,
           )

           convert_rembert_tf_checkpoint_to_pytorch(self._tf_checkpoint    ,      self._config    ,      self._pytorch_dump_output    )
        else:
           raise ValueError(
               '--model_type should be selected in the list [bert, gpt, gpt2, t5, transfo_xl, xlnet, xlm, lxmert]'    )









"""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     :   Tuple			    =			logging.get_logger(__name__)

A     :   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"),
]

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 _lowerCamelCase	(		_UpperCamelCase      ):

			'''simple docstring'''




			__lowerCAmelCase				    =			torch.load(_UpperCamelCase    , map_location="cpu"      )
			return sd


def _lowerCamelCase	(		_UpperCamelCase    , _UpperCamelCase    , _UpperCamelCase=rename_keys_prefix      ):

			'''simple docstring'''




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


@torch.no_grad()
def _lowerCamelCase	(		_UpperCamelCase    , _UpperCamelCase      ):

			'''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:
						__lowerCAmelCase				    =			"pretraining"
						if "vcr" in checkpoint_path:
									__lowerCAmelCase				    =			{"visual_embedding_dim": 512}
						elif "vqa_advanced" in checkpoint_path:
									__lowerCAmelCase				    =			{"visual_embedding_dim": 2048}
						elif "vqa" in checkpoint_path:
									__lowerCAmelCase				    =			{"visual_embedding_dim": 2048}
						elif "nlvr" in checkpoint_path:
									__lowerCAmelCase				    =			{"visual_embedding_dim": 1024}
						else:
									raise NotImplementedError(f"No implementation found for `{checkpoint_path}`."      )
			else:
						if "vcr" in checkpoint_path:
									__lowerCAmelCase				    =			{"visual_embedding_dim": 512}
									__lowerCAmelCase				    =			"multichoice"
						elif "vqa_advanced" in checkpoint_path:
									__lowerCAmelCase				    =			{"visual_embedding_dim": 2048}
									__lowerCAmelCase				    =			"vqa_advanced"
						elif "vqa" in checkpoint_path:
									__lowerCAmelCase				    =			{"visual_embedding_dim": 2048, "num_labels": 3129}
									__lowerCAmelCase				    =			"vqa"
						elif "nlvr" in checkpoint_path:
									__lowerCAmelCase				    =			{
									    "visual_embedding_dim": 1024,
									    "num_labels": 2,
									}
									__lowerCAmelCase				    =			"nlvr"

			__lowerCAmelCase				    =			VisualBertConfig(**_UpperCamelCase      )

			# Load State Dict
			__lowerCAmelCase				    =			load_state_dict(_UpperCamelCase      )

			__lowerCAmelCase				    =			get_new_dict(_UpperCamelCase    , _UpperCamelCase      )

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

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


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




