


'''simple docstring'''







from argparse import ArgumentParser, Namespace

from ..utils import logging
from . import BaseTransformersCLICommand


def 	SCREAMING_SNAKE_CASE_					(  _UpperCAmelCase	:	Namespace  )  ->			Union[str, Any]:
       return ConvertCommand(
           args.model_type    ,args.tf_checkpoint    ,args.pytorch_dump_output    ,args.config    ,args.finetuning_task_name  )


A__:    str					    =       '''
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 A__       (						UpperCAmelCase__      ):






      @staticmethod
      def __UpperCAmelCase       ( SCREAMING_SNAKE_CASE			:ArgumentParser	)       ->					int:




             '''simple docstring'''
             _a    : Optional[int]     					=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=SCREAMING_SNAKE_CASE     ,						required=SCREAMING_SNAKE_CASE     ,						help="""Model's type."""	)
             train_parser.add_argument(
                 """--tf_checkpoint"""     ,						type=SCREAMING_SNAKE_CASE     ,						required=SCREAMING_SNAKE_CASE     ,						help="""TensorFlow checkpoint path or folder."""	)
             train_parser.add_argument(
                 """--pytorch_dump_output"""     ,						type=SCREAMING_SNAKE_CASE     ,						required=SCREAMING_SNAKE_CASE     ,						help="""Path to the PyTorch saved model output."""	)
             train_parser.add_argument("""--config"""     ,						type=SCREAMING_SNAKE_CASE     ,						default=""""""     ,						help="""Configuration file path or folder."""	)
             train_parser.add_argument(
                 """--finetuning_task_name"""     ,						type=SCREAMING_SNAKE_CASE     ,						default=SCREAMING_SNAKE_CASE     ,						help="""Optional fine-tuning task name if the TF model was a finetuned model."""     ,						)
             train_parser.set_defaults(func=SCREAMING_SNAKE_CASE	)






      def __init__( self			:Dict     ,						SCREAMING_SNAKE_CASE			:str     ,						SCREAMING_SNAKE_CASE			:str     ,						SCREAMING_SNAKE_CASE			:str     ,						SCREAMING_SNAKE_CASE			:str     ,						SCREAMING_SNAKE_CASE			:str     ,						*SCREAMING_SNAKE_CASE			:str     ,						)       ->					List[str]:




             '''simple docstring'''
             _a    : str     					=logging.get_logger("""transformers-cli/converting"""	)

             self._logger.info(f"Loading model {model_type}"	)
             _a    : Optional[int]     					=model_type
             _a    : Any     					=tf_checkpoint
             _a    : Tuple     					=pytorch_dump_output
             _a    : int     					=config
             _a    : Any     					=finetuning_task_name






      def __UpperCAmelCase       ( self			:Dict	)       ->					List[Any]:




             '''simple docstring'''
             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(SCREAMING_SNAKE_CASE	)

                    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(SCREAMING_SNAKE_CASE	)

                    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(SCREAMING_SNAKE_CASE	)

                    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(SCREAMING_SNAKE_CASE	)

                    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(SCREAMING_SNAKE_CASE	)

                    if "ckpt" in self._tf_checkpoint.lower():
                           _a    : Dict     					=self._tf_checkpoint
                           _a    : Union[str, Any]     					=""""""
                    else:
                           _a    : Any     					=self._tf_checkpoint
                           _a    : Union[str, Any]     					=""""""
                    convert_transfo_xl_checkpoint_to_pytorch(
                        SCREAMING_SNAKE_CASE     ,						self._config     ,						self._pytorch_dump_output     ,						SCREAMING_SNAKE_CASE	)
             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(SCREAMING_SNAKE_CASE	)

                    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(SCREAMING_SNAKE_CASE	)

                    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'''







class A__       :






      def __init__( self			:List[str]	)       ->					List[Any]:




             '''simple docstring'''
             _a    : Tuple     					=0
             _a    : Any     					=0
             _a    : int     					={}






      def __UpperCAmelCase       ( self			:Any     ,						SCREAMING_SNAKE_CASE			:List[str]	)       ->					Optional[int]:




             '''simple docstring'''
             if vertex not in self.adjacency:
                    _a    : Dict     					={}
                    self.num_vertices += 1






      def __UpperCAmelCase       ( self			:Optional[Any]     ,						SCREAMING_SNAKE_CASE			:str     ,						SCREAMING_SNAKE_CASE			:str     ,						SCREAMING_SNAKE_CASE			:Any	)       ->					List[str]:




             '''simple docstring'''
             self.add_vertex(SCREAMING_SNAKE_CASE	)
             self.add_vertex(SCREAMING_SNAKE_CASE	)

             if head == tail:
                    return

             _a    : Any     					=weight
             _a    : Tuple     					=weight






      def __UpperCAmelCase       ( self			:Dict	)       ->					Optional[int]:




             '''simple docstring'''
             _a    : Union[str, Any]     					=self.get_edges()
             for edge in edges:
                    _a					,    _a					,    _a    : List[str]     					=edge
                    edges.remove((tail, head, weight)	)
             for i in range(len(SCREAMING_SNAKE_CASE	)	):
                    _a    : str     					=list(edges[i]	)

             edges.sort(key=lambda SCREAMING_SNAKE_CASE	: e[2]	)
             for i in range(len(SCREAMING_SNAKE_CASE	) - 1	):
                    if edges[i][2] >= edges[i + 1][2]:
                           _a    : Union[str, Any]     					=edges[i][2] + 1
             for edge in edges:
                    _a					,    _a					,    _a    : Tuple     					=edge
                    _a    : Tuple     					=weight
                    _a    : List[Any]     					=weight






      def __str__( self			:int	)       ->					str:




             '''simple docstring'''
             _a    : int     					=""""""
             for tail in self.adjacency:
                    for head in self.adjacency[tail]:
                           _a    : str     					=self.adjacency[head][tail]
                           string += f"{head} -> {tail} == {weight}\n"
             return string.rstrip("""\n"""	)






      def __UpperCAmelCase       ( self			:Optional[int]	)       ->					Optional[Any]:




             '''simple docstring'''
             _a    : Union[str, Any]     					=[]
             for tail in self.adjacency:
                    for head in self.adjacency[tail]:
                           output.append((tail, head, self.adjacency[head][tail])	)
             return output






      def __UpperCAmelCase       ( self			:List[Any]	)       ->					List[Any]:




             '''simple docstring'''
             return self.adjacency.keys()






      @staticmethod
      def __UpperCAmelCase       ( SCREAMING_SNAKE_CASE			:Dict=None     ,						SCREAMING_SNAKE_CASE			:List[Any]=None	)       ->					Optional[int]:




             '''simple docstring'''
             _a    : str     					=Graph()
             if vertices is None:
                    _a    : Union[str, Any]     					=[]
             if edges is None:
                    _a    : List[Any]     					=[]
             for vertex in vertices:
                    g.add_vertex(SCREAMING_SNAKE_CASE	)
             for edge in edges:
                    g.add_edge(*SCREAMING_SNAKE_CASE	)
             return g







      class A__       :






            def __init__( self			:Optional[int]	)       ->					Union[str, Any]:




                   '''simple docstring'''
                   _a    : Optional[int]     					={}
                   _a    : List[str]     					={}






            def __len__( self			:List[Any]	)       ->					List[Any]:




                   '''simple docstring'''
                   return len(self.parent	)






            def __UpperCAmelCase       ( self			:Tuple     ,						SCREAMING_SNAKE_CASE			:Tuple	)       ->					Dict:




                   '''simple docstring'''
                   if item in self.parent:
                          return self.find(SCREAMING_SNAKE_CASE	)

                   _a    : Optional[Any]     					=item
                   _a    : List[str]     					=0
                   return item






            def __UpperCAmelCase       ( self			:int     ,						SCREAMING_SNAKE_CASE			:Dict	)       ->					List[str]:




                   '''simple docstring'''
                   if item not in self.parent:
                          return self.make_set(SCREAMING_SNAKE_CASE	)
                   if item != self.parent[item]:
                          _a    : str     					=self.find(self.parent[item]	)
                   return self.parent[item]






            def __UpperCAmelCase       ( self			:Optional[int]     ,						SCREAMING_SNAKE_CASE			:Tuple     ,						SCREAMING_SNAKE_CASE			:List[Any]	)       ->					Optional[Any]:




                   '''simple docstring'''
                   _a    : Optional[int]     					=self.find(SCREAMING_SNAKE_CASE	)
                   _a    : Dict     					=self.find(SCREAMING_SNAKE_CASE	)

                   if roota == roota:
                          return roota

                   if self.rank[roota] > self.rank[roota]:
                          _a    : Any     					=roota
                          return roota

                   if self.rank[roota] < self.rank[roota]:
                          _a    : List[str]     					=roota
                          return roota

                   if self.rank[roota] == self.rank[roota]:
                          self.rank[roota] += 1
                          _a    : List[Any]     					=roota
                          return roota
                   return None






      @staticmethod
      def __UpperCAmelCase       ( SCREAMING_SNAKE_CASE			:Dict	)       ->					Union[str, Any]:




             '''simple docstring'''
             _a    : Any     					=graph.num_vertices

             _a    : Union[str, Any]     					=Graph.UnionFind()
             _a    : Optional[int]     					=[]
             while num_components > 1:
                    _a    : str     					={}
                    for vertex in graph.get_vertices():
                           _a    : List[str]     					=-1

                    _a    : Any     					=graph.get_edges()
                    for edge in edges:
                           _a					,    _a					,    _a    : Tuple     					=edge
                           edges.remove((tail, head, weight)	)
                    for edge in edges:
                           _a					,    _a					,    _a    : Any     					=edge
                           _a    : Any     					=union_find.find(SCREAMING_SNAKE_CASE	)
                           _a    : List[Any]     					=union_find.find(SCREAMING_SNAKE_CASE	)
                           if seta != seta:
                                  if cheap_edge[seta] == -1 or cheap_edge[seta][2] > weight:
                                         _a    : Optional[int]     					=[head, tail, weight]

                                  if cheap_edge[seta] == -1 or cheap_edge[seta][2] > weight:
                                         _a    : List[Any]     					=[head, tail, weight]
                    for vertex in cheap_edge:
                           if cheap_edge[vertex] != -1:
                                  _a					,    _a					,    _a    : Optional[Any]     					=cheap_edge[vertex]
                                  if union_find.find(SCREAMING_SNAKE_CASE	) != union_find.find(SCREAMING_SNAKE_CASE	):
                                         union_find.union(SCREAMING_SNAKE_CASE     ,						SCREAMING_SNAKE_CASE	)
                                         mst_edges.append(cheap_edge[vertex]	)
                                         _a    : str     					=num_components - 1
             _a    : str     					=Graph.build(edges=SCREAMING_SNAKE_CASE	)
             return mst


