



from argparse import ArgumentParser, Namespace

from ..utils import logging
from . import BaseTransformersCLICommand
def  UpperCamelCase						(       _A			:				Namespace    )->       Union[str, Any]:





						"""simple docstring"""





						return ConvertCommand(
						    args.model_type			,							args.tf_checkpoint			,							args.pytorch_dump_output			,							args.config			,							args.finetuning_task_name    )


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






class      UpperCamelCase    (						_UpperCAmelCase     ):



	@staticmethod
	def 			__A							(       UpperCAmelCase__		):
							A__							=       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			,	UpperCAmelCase__			,	UpperCAmelCase__			,	UpperCAmelCase__			,	UpperCAmelCase__			,	UpperCAmelCase__			,	*UpperCAmelCase__			,	):
							A__							=       logging.get_logger("transformers-cli/converting"		)

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



	def 			__A							(       self		):
							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():
																			A__							=       self._tf_checkpoint
																			A__							=       ""
													else:
																			A__							=       self._tf_checkpoint
																			A__							=       ""
													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]"		)










import math
from enum import Enum
from typing import Optional, Union

from torch.optim import Optimizer
from torch.optim.lr_scheduler import LambdaLR

from .utils import logging


UpperCAmelCase_							:      Optional[Any]      =  logging.get_logger(__name__)






class      UpperCamelCase    (						_UpperCAmelCase     ):
	lowerCAmelCase	:   List[str]				=	"""linear"""
	lowerCAmelCase	:   int				=	"""cosine"""
	lowerCAmelCase	:   Dict				=	"""cosine_with_restarts"""
	lowerCAmelCase	:   Optional[Any]				=	"""polynomial"""
	lowerCAmelCase	:   Dict				=	"""constant"""
	lowerCAmelCase	:   Any				=	"""constant_with_warmup"""
	lowerCAmelCase	:   Union[str, Any]				=	"""piecewise_constant"""





def  UpperCamelCase						(       _A			:				Optimizer			,							_A			:				int = -1    )->       Dict:





						"""simple docstring"""





						return LambdaLR(_A			,							lambda _A    : 1			,							last_epoch=_A    )
def  UpperCamelCase						(       _A			:				Optimizer			,							_A			:				int			,							_A			:				int = -1    )->       Optional[Any]:





						"""simple docstring"""





						def lr_lambda(_A			:				int    ):
												if current_step < num_warmup_steps:
																		return float(_A    ) / float(max(1.0			,							_A    )    )
												return 1.0

						return LambdaLR(_A			,							_A			,							last_epoch=_A    )
def  UpperCamelCase						(       _A			:				Optimizer			,							_A			:				str			,							_A			:				int = -1    )->       Dict:





						"""simple docstring"""





						A__							=       {}
						A__							=       step_rules.split(","    )
						for rule_str in rule_list[:-1]:
												A__						,       A__							=       rule_str.split(":"    )
												A__							=       int(_A    )
												A__							=       float(_A    )
												A__							=       value
						A__							=       float(rule_list[-1]    )

						def create_rules_function(_A			:				Any			,							_A			:				Optional[int]    ):
												def rule_func(_A			:				int    ) -> float:
																		A__							=       sorted(rules_dict.keys()    )
																		for i, sorted_step in enumerate(_A    ):
																								if steps < sorted_step:
																														return rules_dict[sorted_steps[i]]
																		return last_lr_multiple

												return rule_func

						A__							=       create_rules_function(_A			,							_A    )

						return LambdaLR(_A			,							_A			,							last_epoch=_A    )
def  UpperCamelCase						(       _A			:				Any			,							_A			:				Union[str, Any]			,							_A			:				str			,							_A			:				str=-1    )->       Tuple:





						"""simple docstring"""





						def lr_lambda(_A			:				int    ):
												if current_step < num_warmup_steps:
																		return float(_A    ) / float(max(1			,							_A    )    )
												return max(
												    0.0			,							float(num_training_steps - current_step    ) / float(max(1			,							num_training_steps - num_warmup_steps    )    )    )

						return LambdaLR(_A			,							_A			,							_A    )
def  UpperCamelCase						(       _A			:				Optimizer			,							_A			:				int			,							_A			:				int			,							_A			:				float = 0.5			,							_A			:				int = -1    )->       Any:





						"""simple docstring"""





						def lr_lambda(_A			:				Tuple    ):
												if current_step < num_warmup_steps:
																		return float(_A    ) / float(max(1			,							_A    )    )
												A__							=       float(current_step - num_warmup_steps    ) / float(max(1			,							num_training_steps - num_warmup_steps    )    )
												return max(0.0			,							0.5 * (1.0 + math.cos(math.pi * float(_A    ) * 2.0 * progress    ))    )

						return LambdaLR(_A			,							_A			,							_A    )
def  UpperCamelCase						(       _A			:				Optimizer			,							_A			:				int			,							_A			:				int			,							_A			:				int = 1			,							_A			:				int = -1    )->       Any:





						"""simple docstring"""





						def lr_lambda(_A			:				Tuple    ):
												if current_step < num_warmup_steps:
																		return float(_A    ) / float(max(1			,							_A    )    )
												A__							=       float(current_step - num_warmup_steps    ) / float(max(1			,							num_training_steps - num_warmup_steps    )    )
												if progress >= 1.0:
																		return 0.0
												return max(0.0			,							0.5 * (1.0 + math.cos(math.pi * ((float(_A    ) * progress) % 1.0)    ))    )

						return LambdaLR(_A			,							_A			,							_A    )
def  UpperCamelCase						(       _A			:				Union[str, Any]			,							_A			:				Union[str, Any]			,							_A			:				List[str]			,							_A			:				Tuple=1E-7			,							_A			:				Dict=1.0			,							_A			:				Union[str, Any]=-1    )->       Any:





						"""simple docstring"""





						A__							=       optimizer.defaults["lr"]
						if not (lr_init > lr_end):
												raise ValueError(f"""lr_end ({lr_end}) must be be smaller than initial lr ({lr_init})"""    )

						def lr_lambda(_A			:				int    ):
												if current_step < num_warmup_steps:
																		return float(_A    ) / float(max(1			,							_A    )    )
												elif current_step > num_training_steps:
																		return lr_end / lr_init  # as LambdaLR multiplies by lr_init
												else:
																		A__							=       lr_init - lr_end
																		A__							=       num_training_steps - num_warmup_steps
																		A__							=       1 - (current_step - num_warmup_steps) / decay_steps
																		A__							=       lr_range * pct_remaining**power + lr_end
																		return decay / lr_init  # as LambdaLR multiplies by lr_init

						return LambdaLR(_A			,							_A			,							_A    )


UpperCAmelCase_							:      Any      =  {
    SchedulerType.LINEAR: get_linear_schedule_with_warmup,
    SchedulerType.COSINE: get_cosine_schedule_with_warmup,
    SchedulerType.COSINE_WITH_RESTARTS: get_cosine_with_hard_restarts_schedule_with_warmup,
    SchedulerType.POLYNOMIAL: get_polynomial_decay_schedule_with_warmup,
    SchedulerType.CONSTANT: get_constant_schedule,
    SchedulerType.CONSTANT_WITH_WARMUP: get_constant_schedule_with_warmup,
    SchedulerType.PIECEWISE_CONSTANT: get_piecewise_constant_schedule,
}
def  UpperCamelCase						(       _A			:				Union[str, SchedulerType]			,							_A			:				Optimizer			,							_A			:				Optional[str] = None			,							_A			:				Optional[int] = None			,							_A			:				Optional[int] = None			,							_A			:				int = 1			,							_A			:				float = 1.0			,							_A			:				int = -1			,							)->       Union[str, Any]:





						"""simple docstring"""





						A__							=       SchedulerType(_A    )
						A__							=       TYPE_TO_SCHEDULER_FUNCTION[name]
						if name == SchedulerType.CONSTANT:
												return schedule_func(_A			,							last_epoch=_A    )

						if name == SchedulerType.PIECEWISE_CONSTANT:
												return schedule_func(_A			,							step_rules=_A			,							last_epoch=_A    )

						# All other schedulers require `num_warmup_steps`
						if num_warmup_steps is None:
												raise ValueError(f"""{name} requires `num_warmup_steps`, please provide that argument."""    )

						if name == SchedulerType.CONSTANT_WITH_WARMUP:
												return schedule_func(_A			,							num_warmup_steps=_A			,							last_epoch=_A    )

						# All other schedulers require `num_training_steps`
						if num_training_steps is None:
												raise ValueError(f"""{name} requires `num_training_steps`, please provide that argument."""    )

						if name == SchedulerType.COSINE_WITH_RESTARTS:
												return schedule_func(
												    _A			,							num_warmup_steps=_A			,							num_training_steps=_A			,							num_cycles=_A			,							last_epoch=_A			,							)

						if name == SchedulerType.POLYNOMIAL:
												return schedule_func(
												    _A			,							num_warmup_steps=_A			,							num_training_steps=_A			,							power=_A			,							last_epoch=_A			,							)

						return schedule_func(
						    _A			,							num_warmup_steps=_A			,							num_training_steps=_A			,							last_epoch=_A    )





