





import shutil
import tempfile
import unittest
from unittest.mock import patch

from transformers import (
    DefaultFlowCallback,
    IntervalStrategy,
    PrinterCallback,
    ProgressCallback,
    Trainer,
    TrainerCallback,
    TrainingArguments,
    is_torch_available,
)
from transformers.testing_utils import require_torch


if is_torch_available():
     from transformers.trainer import DEFAULT_CALLBACKS

     from .test_trainer import RegressionDataset, RegressionModelConfig, RegressionPreTrainedModel



class        _SCREAMING_SNAKE_CASE						(     __SCREAMING_SNAKE_CASE   ):







 '''simple docstring'''

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



   '''simple docstring'''



   lowerCamelCase__:				List[Any]        =[]

 def 							SCREAMING_SNAKE_CASE_ (self    :       Any	,			UpperCAmelCase_    :       int	,			UpperCAmelCase_    :       str	,			UpperCAmelCase_    :       Dict	,			**UpperCAmelCase_    :       Any)	->Dict:



   '''simple docstring'''



   self.events.append("on_init_end")

 def 							SCREAMING_SNAKE_CASE_ (self    :       Union[str, Any]	,			UpperCAmelCase_    :       Any	,			UpperCAmelCase_    :       Dict	,			UpperCAmelCase_    :       Dict	,			**UpperCAmelCase_    :       str)	->List[str]:



   '''simple docstring'''



   self.events.append("on_train_begin")

 def 							SCREAMING_SNAKE_CASE_ (self    :       List[str]	,			UpperCAmelCase_    :       Dict	,			UpperCAmelCase_    :       Optional[Any]	,			UpperCAmelCase_    :       List[Any]	,			**UpperCAmelCase_    :       str)	->int:



   '''simple docstring'''



   self.events.append("on_train_end")

 def 							SCREAMING_SNAKE_CASE_ (self    :       Tuple	,			UpperCAmelCase_    :       Union[str, Any]	,			UpperCAmelCase_    :       Any	,			UpperCAmelCase_    :       List[str]	,			**UpperCAmelCase_    :       Optional[int])	->List[Any]:



   '''simple docstring'''



   self.events.append("on_epoch_begin")

 def 							SCREAMING_SNAKE_CASE_ (self    :       Optional[int]	,			UpperCAmelCase_    :       Dict	,			UpperCAmelCase_    :       List[Any]	,			UpperCAmelCase_    :       int	,			**UpperCAmelCase_    :       Any)	->Tuple:



   '''simple docstring'''



   self.events.append("on_epoch_end")

 def 							SCREAMING_SNAKE_CASE_ (self    :       List[Any]	,			UpperCAmelCase_    :       Dict	,			UpperCAmelCase_    :       Any	,			UpperCAmelCase_    :       Any	,			**UpperCAmelCase_    :       List[Any])	->Optional[int]:



   '''simple docstring'''



   self.events.append("on_step_begin")

 def 							SCREAMING_SNAKE_CASE_ (self    :       List[str]	,			UpperCAmelCase_    :       List[str]	,			UpperCAmelCase_    :       List[str]	,			UpperCAmelCase_    :       Optional[int]	,			**UpperCAmelCase_    :       List[str])	->Tuple:



   '''simple docstring'''



   self.events.append("on_step_end")

 def 							SCREAMING_SNAKE_CASE_ (self    :       Union[str, Any]	,			UpperCAmelCase_    :       Optional[Any]	,			UpperCAmelCase_    :       Dict	,			UpperCAmelCase_    :       Optional[Any]	,			**UpperCAmelCase_    :       str)	->Optional[int]:



   '''simple docstring'''



   self.events.append("on_evaluate")

 def 							SCREAMING_SNAKE_CASE_ (self    :       Tuple	,			UpperCAmelCase_    :       List[str]	,			UpperCAmelCase_    :       List[Any]	,			UpperCAmelCase_    :       Union[str, Any]	,			**UpperCAmelCase_    :       Any)	->int:



   '''simple docstring'''



   self.events.append("on_predict")

 def 							SCREAMING_SNAKE_CASE_ (self    :       Union[str, Any]	,			UpperCAmelCase_    :       List[Any]	,			UpperCAmelCase_    :       Optional[int]	,			UpperCAmelCase_    :       Tuple	,			**UpperCAmelCase_    :       List[Any])	->Any:



   '''simple docstring'''



   self.events.append("on_save")

 def 							SCREAMING_SNAKE_CASE_ (self    :       str	,			UpperCAmelCase_    :       Any	,			UpperCAmelCase_    :       List[str]	,			UpperCAmelCase_    :       Any	,			**UpperCAmelCase_    :       Optional[Any])	->str:



   '''simple docstring'''



   self.events.append("on_log")







 def 							SCREAMING_SNAKE_CASE_ (self    :       Optional[Any]	,			UpperCAmelCase_    :       Any	,			UpperCAmelCase_    :       List[Any]	,			UpperCAmelCase_    :       str	,			**UpperCAmelCase_    :       List[str])	->Optional[int]:



   '''simple docstring'''



   self.events.append("on_prediction_step")



@require_torch
class        _SCREAMING_SNAKE_CASE						(     unittest.TestCase   ):







 '''simple docstring'''

 def 							SCREAMING_SNAKE_CASE_ (self    :       List[Any])	->int:



   '''simple docstring'''



   lowerCamelCase__:				Tuple        =tempfile.mkdtemp()

 def 							SCREAMING_SNAKE_CASE_ (self    :       List[str])	->Tuple:



   '''simple docstring'''



   shutil.rmtree(self.output_dir)

 def 							SCREAMING_SNAKE_CASE_ (self    :       List[Any]	,			UpperCAmelCase_    :       int=0	,			UpperCAmelCase_    :       Tuple=0	,			UpperCAmelCase_    :       str=64	,			UpperCAmelCase_    :       List[Any]=64	,			UpperCAmelCase_    :       Union[str, Any]=None	,			UpperCAmelCase_    :       List[str]=False	,			**UpperCAmelCase_    :       Tuple)	->Any:



   '''simple docstring'''



   lowerCamelCase__:				Union[str, Any]        =RegressionDataset(length=UpperCAmelCase_)
   lowerCamelCase__:				int        =RegressionDataset(length=UpperCAmelCase_)
   lowerCamelCase__:				str        =RegressionModelConfig(a=UpperCAmelCase_	,			b=UpperCAmelCase_)
   lowerCamelCase__:				Union[str, Any]        =RegressionPreTrainedModel(UpperCAmelCase_)

   lowerCamelCase__:				int        =TrainingArguments(self.output_dir	,			disable_tqdm=UpperCAmelCase_	,			report_to=[]	,			**UpperCAmelCase_)
   return Trainer(
       UpperCAmelCase_	,			UpperCAmelCase_	,			train_dataset=UpperCAmelCase_	,			eval_dataset=UpperCAmelCase_	,			callbacks=UpperCAmelCase_	,			)

 def 							SCREAMING_SNAKE_CASE_ (self    :       List[str]	,			UpperCAmelCase_    :       str	,			UpperCAmelCase_    :       List[str])	->Dict:



   '''simple docstring'''



   self.assertEqual(len(UpperCAmelCase_)	,			len(UpperCAmelCase_))

   # Order doesn't matter
   lowerCamelCase__:				Dict        =sorted(UpperCAmelCase_	,			key=lambda UpperCAmelCase_: cb.__name__ if isinstance(UpperCAmelCase_	,			UpperCAmelCase_) else cb.__class__.__name__)
   lowerCamelCase__:				Optional[int]        =sorted(UpperCAmelCase_	,			key=lambda UpperCAmelCase_: cb.__name__ if isinstance(UpperCAmelCase_	,			UpperCAmelCase_) else cb.__class__.__name__)

   for cba, cba in zip(UpperCAmelCase_	,			UpperCAmelCase_):
     if isinstance(UpperCAmelCase_	,			UpperCAmelCase_) and isinstance(UpperCAmelCase_	,			UpperCAmelCase_):
       self.assertEqual(UpperCAmelCase_	,			UpperCAmelCase_)
     elif isinstance(UpperCAmelCase_	,			UpperCAmelCase_) and not isinstance(UpperCAmelCase_	,			UpperCAmelCase_):
       self.assertEqual(UpperCAmelCase_	,			cba.__class__)
     elif not isinstance(UpperCAmelCase_	,			UpperCAmelCase_) and isinstance(UpperCAmelCase_	,			UpperCAmelCase_):
       self.assertEqual(cba.__class__	,			UpperCAmelCase_)
     else:
       self.assertEqual(UpperCAmelCase_	,			UpperCAmelCase_)

 def 							SCREAMING_SNAKE_CASE_ (self    :       List[Any]	,			UpperCAmelCase_    :       Optional[Any])	->Optional[int]:



   '''simple docstring'''



   lowerCamelCase__:				Dict        =["on_init_end", "on_train_begin"]
   lowerCamelCase__:				List[str]        =0
   lowerCamelCase__:				List[Any]        =len(trainer.get_eval_dataloader())
   lowerCamelCase__:				Dict        =["on_prediction_step"] * len(trainer.get_eval_dataloader()) + ["on_log", "on_evaluate"]
   for _ in range(trainer.state.num_train_epochs):
     expected_events.append("on_epoch_begin")
     for _ in range(UpperCAmelCase_):
       step += 1
       expected_events += ["on_step_begin", "on_step_end"]
       if step % trainer.args.logging_steps == 0:
         expected_events.append("on_log")
       if trainer.args.evaluation_strategy == IntervalStrategy.STEPS and step % trainer.args.eval_steps == 0:
         expected_events += evaluation_events.copy()
       if step % trainer.args.save_steps == 0:
         expected_events.append("on_save")
     expected_events.append("on_epoch_end")
     if trainer.args.evaluation_strategy == IntervalStrategy.EPOCH:
       expected_events += evaluation_events.copy()
   expected_events += ["on_log", "on_train_end"]
   return expected_events

 def 							SCREAMING_SNAKE_CASE_ (self    :       int)	->Optional[int]:



   '''simple docstring'''



   lowerCamelCase__:				Optional[Any]        =self.get_trainer()
   lowerCamelCase__:				Any        =DEFAULT_CALLBACKS.copy() + [ProgressCallback]
   self.check_callbacks_equality(trainer.callback_handler.callbacks	,			UpperCAmelCase_)

   # Callbacks passed at init are added to the default callbacks
   lowerCamelCase__:				Dict        =self.get_trainer(callbacks=[MyTestTrainerCallback])
   expected_callbacks.append(UpperCAmelCase_)
   self.check_callbacks_equality(trainer.callback_handler.callbacks	,			UpperCAmelCase_)

   # TrainingArguments.disable_tqdm controls if use ProgressCallback or PrinterCallback
   lowerCamelCase__:				int        =self.get_trainer(disable_tqdm=UpperCAmelCase_)
   lowerCamelCase__:				Tuple        =DEFAULT_CALLBACKS.copy() + [PrinterCallback]
   self.check_callbacks_equality(trainer.callback_handler.callbacks	,			UpperCAmelCase_)

 def 							SCREAMING_SNAKE_CASE_ (self    :       List[str])	->Union[str, Any]:



   '''simple docstring'''



   lowerCamelCase__:				Union[str, Any]        =DEFAULT_CALLBACKS.copy() + [ProgressCallback]
   lowerCamelCase__:				Optional[int]        =self.get_trainer()

   # We can add, pop, or remove by class name
   trainer.remove_callback(UpperCAmelCase_)
   expected_callbacks.remove(UpperCAmelCase_)
   self.check_callbacks_equality(trainer.callback_handler.callbacks	,			UpperCAmelCase_)

   lowerCamelCase__:				Dict        =self.get_trainer()
   lowerCamelCase__:				str        =trainer.pop_callback(UpperCAmelCase_)
   self.assertEqual(cb.__class__	,			UpperCAmelCase_)
   self.check_callbacks_equality(trainer.callback_handler.callbacks	,			UpperCAmelCase_)

   trainer.add_callback(UpperCAmelCase_)
   expected_callbacks.insert(0	,			UpperCAmelCase_)
   self.check_callbacks_equality(trainer.callback_handler.callbacks	,			UpperCAmelCase_)

   # We can also add, pop, or remove by instance
   lowerCamelCase__:				List[str]        =self.get_trainer()
   lowerCamelCase__:				List[str]        =trainer.callback_handler.callbacks[0]
   trainer.remove_callback(UpperCAmelCase_)
   expected_callbacks.remove(UpperCAmelCase_)
   self.check_callbacks_equality(trainer.callback_handler.callbacks	,			UpperCAmelCase_)

   lowerCamelCase__:				str        =self.get_trainer()
   lowerCamelCase__:				List[Any]        =trainer.callback_handler.callbacks[0]
   lowerCamelCase__:				Dict        =trainer.pop_callback(UpperCAmelCase_)
   self.assertEqual(UpperCAmelCase_	,			UpperCAmelCase_)
   self.check_callbacks_equality(trainer.callback_handler.callbacks	,			UpperCAmelCase_)

   trainer.add_callback(UpperCAmelCase_)
   expected_callbacks.insert(0	,			UpperCAmelCase_)
   self.check_callbacks_equality(trainer.callback_handler.callbacks	,			UpperCAmelCase_)







 def 							SCREAMING_SNAKE_CASE_ (self    :       List[Any])	->int:



   '''simple docstring'''



   import warnings

   # XXX: for now ignore scatter_gather warnings in this test since it's not relevant to what's being tested
   warnings.simplefilter(action="ignore"	,			category=UpperCAmelCase_)

   lowerCamelCase__:				Union[str, Any]        =self.get_trainer(callbacks=[MyTestTrainerCallback])
   trainer.train()
   lowerCamelCase__:				int        =trainer.callback_handler.callbacks[-2].events
   self.assertEqual(UpperCAmelCase_	,			self.get_expected_events(UpperCAmelCase_))

   # Independent log/save/eval
   lowerCamelCase__:				Dict        =self.get_trainer(callbacks=[MyTestTrainerCallback]	,			logging_steps=5)
   trainer.train()
   lowerCamelCase__:				Optional[int]        =trainer.callback_handler.callbacks[-2].events
   self.assertEqual(UpperCAmelCase_	,			self.get_expected_events(UpperCAmelCase_))

   lowerCamelCase__:				Any        =self.get_trainer(callbacks=[MyTestTrainerCallback]	,			save_steps=5)
   trainer.train()
   lowerCamelCase__:				List[Any]        =trainer.callback_handler.callbacks[-2].events
   self.assertEqual(UpperCAmelCase_	,			self.get_expected_events(UpperCAmelCase_))

   lowerCamelCase__:				int        =self.get_trainer(callbacks=[MyTestTrainerCallback]	,			eval_steps=5	,			evaluation_strategy="steps")
   trainer.train()
   lowerCamelCase__:				str        =trainer.callback_handler.callbacks[-2].events
   self.assertEqual(UpperCAmelCase_	,			self.get_expected_events(UpperCAmelCase_))

   lowerCamelCase__:				Dict        =self.get_trainer(callbacks=[MyTestTrainerCallback]	,			evaluation_strategy="epoch")
   trainer.train()
   lowerCamelCase__:				Tuple        =trainer.callback_handler.callbacks[-2].events
   self.assertEqual(UpperCAmelCase_	,			self.get_expected_events(UpperCAmelCase_))

   # A bit of everything
   lowerCamelCase__:				Tuple        =self.get_trainer(
       callbacks=[MyTestTrainerCallback]	,			logging_steps=3	,			save_steps=10	,			eval_steps=5	,			evaluation_strategy="steps"	,			)
   trainer.train()
   lowerCamelCase__:				int        =trainer.callback_handler.callbacks[-2].events
   self.assertEqual(UpperCAmelCase_	,			self.get_expected_events(UpperCAmelCase_))

   # warning should be emitted for duplicated callbacks
   with patch("transformers.trainer_callback.logger.warning") as warn_mock:
     lowerCamelCase__:				Optional[int]        =self.get_trainer(
         callbacks=[MyTestTrainerCallback, MyTestTrainerCallback]	,			)
     assert str(UpperCAmelCase_) in warn_mock.call_args[0][0]








from argparse import ArgumentParser, Namespace

from ..utils import logging
from . import BaseTransformersCLICommand





def 					lowerCAmelCase_			(	__a		)					->		int:







  """simple docstring"""





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


__A     =    "\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        _SCREAMING_SNAKE_CASE						(     __SCREAMING_SNAKE_CASE   ):







 '''simple docstring'''

 @staticmethod
 def 							SCREAMING_SNAKE_CASE_ (UpperCAmelCase_    :       ArgumentParser)	->str:



   '''simple docstring'''



   lowerCamelCase__:				Dict        =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    :       List[Any]	,			UpperCAmelCase_    :       str	,			UpperCAmelCase_    :       str	,			UpperCAmelCase_    :       str	,			UpperCAmelCase_    :       str	,			UpperCAmelCase_    :       str	,			*UpperCAmelCase_    :       Optional[int]	,			)	->List[str]:



   '''simple docstring'''



   lowerCamelCase__:				Dict        =logging.get_logger("transformers-cli/converting")

   self._logger.info(F"""Loading model {model_type}""")
   lowerCamelCase__:				Any        =model_type
   lowerCamelCase__:				Optional[int]        =tf_checkpoint
   lowerCamelCase__:				Any        =pytorch_dump_output
   lowerCamelCase__:				Union[str, Any]        =config
   lowerCamelCase__:				str        =finetuning_task_name







 def 							SCREAMING_SNAKE_CASE_ (self    :       Union[str, Any])	->Tuple:



   '''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(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__:				Tuple        =self._tf_checkpoint
       lowerCamelCase__:				List[str]        =""
     else:
       lowerCamelCase__:				Any        =self._tf_checkpoint
       lowerCamelCase__:				Dict        =""
     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]")

