






from argparse import ArgumentParser, Namespace

from ..utils import logging
from . import BaseTransformersCLICommand




def       _SCREAMING_SNAKE_CASE (							__snake_case )				->						List[str]:
       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 		SCREAMING_SNAKE_CASE__	(  __lowercase     ):

  '''simple docstring'''







  @staticmethod
  def  lowerCamelCase       (      lowerCamelCase		:			List[Any]							)						->	List[Any]:


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


  def __init__(      self		:			List[Any]       ,				lowerCamelCase		:			Dict       ,				lowerCamelCase		:			Tuple       ,				lowerCamelCase		:			Tuple       ,				lowerCamelCase		:			List[str]       ,				lowerCamelCase		:			Union[str, Any]       ,				*lowerCamelCase		:			Optional[int]       ,				)						->	Tuple:


         """simple docstring"""
         _UpperCAmelCase							=    logging.get_logger("""transformers-cli/converting"""							)

         self._logger.info(f"""Loading model {model_type}"""							)
         _UpperCAmelCase							=    model_type
         _UpperCAmelCase							=    tf_checkpoint
         _UpperCAmelCase							=    pytorch_dump_output
         _UpperCAmelCase							=    config
         _UpperCAmelCase							=    finetuning_task_name





  def  lowerCamelCase       (      self		:			Any							)						->	str:


         """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(_lowerCAmelCase							)

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

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

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

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

                if "ckpt" in self._tf_checkpoint.lower():
                       _UpperCAmelCase							=    self._tf_checkpoint
                       _UpperCAmelCase							=    ''''''
                else:
                       _UpperCAmelCase							=    self._tf_checkpoint
                       _UpperCAmelCase							=    ''''''
                convert_transfo_xl_checkpoint_to_pytorch(
                    _lowerCAmelCase       ,				self._config       ,				self._pytorch_dump_output       ,				_lowerCAmelCase							)
         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(_lowerCAmelCase							)

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

                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]"""							)






import baseaa
import io
import json
import os
from copy import deepcopy

from ..optimizer import AcceleratedOptimizer
from ..scheduler import AcceleratedScheduler





class 				a			:






				def __init__(  self			, _lowerCAmelCase					):

										"""simple docstring"""




										if isinstance(_lowerCAmelCase			, _lowerCAmelCase					):
																# Don't modify user's data should they want to reuse it (e.g. in tests), because once we
																# modified it, it will not be accepted here again, since `auto` values would have been overridden
																__SCREAMING_SNAKE_CASE:				Optional[int]           =   deepcopy(_lowerCAmelCase					)
										elif os.path.exists(_lowerCAmelCase					):
																with io.open(_lowerCAmelCase			, '''r'''			, encoding='''utf-8'''					) as f:
																						__SCREAMING_SNAKE_CASE:				int           =   json.load(_lowerCAmelCase					)
										else:
																try:
																						__SCREAMING_SNAKE_CASE:				Union[str, Any]           =   baseaa.urlsafe_baadecode(_lowerCAmelCase					).decode('''utf-8'''					)
																						__SCREAMING_SNAKE_CASE:				List[Any]           =   json.loads(_lowerCAmelCase					)
																except (UnicodeDecodeError, AttributeError, ValueError):
																						raise ValueError(
																						    f"""Expected a string path to an existing deepspeed config, or a dictionary, or a base64 encoded string. Received: {config_file_or_dict}"""					)

										__SCREAMING_SNAKE_CASE:				str           =   config

										self.set_stage_and_offload()






				def 			snake_case_    (  self					):

										"""simple docstring"""




										__SCREAMING_SNAKE_CASE:				str           =   self.get_value('''zero_optimization.stage'''			, -1					)

										# offload
										__SCREAMING_SNAKE_CASE:				Dict           =   False
										if self.is_zeroa() or self.is_zeroa():
																__SCREAMING_SNAKE_CASE:				str           =   set(['''cpu''', '''nvme''']					)
																__SCREAMING_SNAKE_CASE:				Tuple           =   set(
																    [
																        self.get_value('''zero_optimization.offload_optimizer.device'''					),
																        self.get_value('''zero_optimization.offload_param.device'''					),
																    ]					)
																if len(offload_devices & offload_devices_valid					) > 0:
																						__SCREAMING_SNAKE_CASE:				Dict           =   True






				def 			snake_case_    (  self			, _lowerCAmelCase					):

										"""simple docstring"""




										__SCREAMING_SNAKE_CASE:				int           =   self.config

										# find the config node of interest if it exists
										__SCREAMING_SNAKE_CASE:				Optional[Any]           =   ds_key_long.split('''.'''					)
										__SCREAMING_SNAKE_CASE:				Union[str, Any]           =   nodes.pop()
										for node in nodes:
																__SCREAMING_SNAKE_CASE:				int           =   config.get(_lowerCAmelCase					)
																if config is None:
																						return None, ds_key

										return config, ds_key






				def 			snake_case_    (  self			, _lowerCAmelCase			, _lowerCAmelCase=None					):

										"""simple docstring"""




										__SCREAMING_SNAKE_CASE       ,__SCREAMING_SNAKE_CASE:				List[Any]           =   self.find_config_node(_lowerCAmelCase					)
										if config is None:
																return default
										return config.get(_lowerCAmelCase			, _lowerCAmelCase					)






				def 			snake_case_    (  self			, _lowerCAmelCase			, _lowerCAmelCase=False					):

										"""simple docstring"""




										__SCREAMING_SNAKE_CASE:				str           =   self.config

										# find the config node of interest if it exists
										__SCREAMING_SNAKE_CASE:				str           =   ds_key_long.split('''.'''					)
										for node in nodes:
																__SCREAMING_SNAKE_CASE:				Tuple           =   config
																__SCREAMING_SNAKE_CASE:				List[str]           =   config.get(_lowerCAmelCase					)
																if config is None:
																						if must_exist:
																												raise ValueError(f"""Can't find {ds_key_long} entry in the config: {self.config}"""					)
																						else:
																												return

        # if found remove it
										if parent_config is not None:
																parent_config.pop(_lowerCAmelCase					)






				def 			snake_case_    (  self			, _lowerCAmelCase					):

										"""simple docstring"""




										__SCREAMING_SNAKE_CASE:				Optional[Any]           =   self.get_value(_lowerCAmelCase					)
										return False if value is None else bool(_lowerCAmelCase					)






				def 			snake_case_    (  self			, _lowerCAmelCase					):

										"""simple docstring"""




										__SCREAMING_SNAKE_CASE:				Any           =   self.get_value(_lowerCAmelCase					)
										return False if value is None else not bool(_lowerCAmelCase					)






				def 			snake_case_    (  self					):

										"""simple docstring"""




										return self._stage == 2






				def 			snake_case_    (  self					):

										"""simple docstring"""




										return self._stage == 3






				def 			snake_case_    (  self					):

										"""simple docstring"""




										return self._offload





class 				a			:






				def __init__(  self			, _lowerCAmelCase					):

										"""simple docstring"""




										__SCREAMING_SNAKE_CASE:				Optional[int]           =   engine






				def 			snake_case_    (  self			, _lowerCAmelCase			, **_lowerCAmelCase					):

										"""simple docstring"""




										self.engine.backward(_lowerCAmelCase			, **_lowerCAmelCase					)

										# Deepspeed's `engine.step` performs the following operations:
										# - gradient accumulation check
										# - gradient clipping
										# - optimizer step
										# - zero grad
										# - checking overflow
										# - lr_scheduler step (only if engine.lr_scheduler is not None)
										self.engine.step()
										# and this plugin overrides the above calls with no-ops when Accelerate runs under
										# Deepspeed, but allows normal functionality for non-Deepspeed cases thus enabling a simple
										# training loop that works transparently under many training regimes.





class 				a			(      __lowercase			):






				def __init__(  self			, _lowerCAmelCase					):

										"""simple docstring"""




										super().__init__(_lowerCAmelCase			, device_placement=_lowerCAmelCase			, scaler=_lowerCAmelCase					)
										__SCREAMING_SNAKE_CASE:				str           =   hasattr(self.optimizer			, '''overflow'''					)






				def 			snake_case_    (  self			, _lowerCAmelCase=None					):

										"""simple docstring"""




										pass  # `accelerator.backward(loss)` is doing that automatically. Therefore, its implementation is not needed






				def 			snake_case_    (  self					):

										"""simple docstring"""




										pass  # `accelerator.backward(loss)` is doing that automatically. Therefore, its implementation is not needed






				@property
				def 			snake_case_    (  self					):

										"""simple docstring"""




										if self.__has_overflow__:
																return self.optimizer.overflow
										return False





class 				a			(      __lowercase			):






				def __init__(  self			, _lowerCAmelCase			, _lowerCAmelCase					):

										"""simple docstring"""




										super().__init__(_lowerCAmelCase			, _lowerCAmelCase					)






				def 			snake_case_    (  self					):

										"""simple docstring"""




										pass  # `accelerator.backward(loss)` is doing that automatically. Therefore, its implementation is not needed





class 				a			:






				def __init__(  self			, _lowerCAmelCase			, _lowerCAmelCase=0.001			, _lowerCAmelCase=0			, **_lowerCAmelCase					):

										"""simple docstring"""




										__SCREAMING_SNAKE_CASE:				str           =   params
										__SCREAMING_SNAKE_CASE:				int           =   lr
										__SCREAMING_SNAKE_CASE:				List[str]           =   weight_decay
										__SCREAMING_SNAKE_CASE:				List[Any]           =   kwargs





class 				a			:






				def __init__(  self			, _lowerCAmelCase			, _lowerCAmelCase=None			, _lowerCAmelCase=0			, **_lowerCAmelCase					):

										"""simple docstring"""




										__SCREAMING_SNAKE_CASE:				int           =   optimizer
										__SCREAMING_SNAKE_CASE:				int           =   total_num_steps
										__SCREAMING_SNAKE_CASE:				Optional[int]           =   warmup_num_steps
										__SCREAMING_SNAKE_CASE:				str           =   kwargs



