





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]








import argparse
import os

import numpy as np
import tensorflow as tf
import torch

from transformers import BertModel





def 					lowerCAmelCase_			(	__a     ,   __a     ,   __a		)					->		List[str]:







  """simple docstring"""





  lowerCamelCase__:				int        =("dense.weight", "attention.self.query", "attention.self.key", "attention.self.value")

  lowerCamelCase__:				Any        =(
      ("layer.", "layer_"),
      ("word_embeddings.weight", "word_embeddings"),
      ("position_embeddings.weight", "position_embeddings"),
      ("token_type_embeddings.weight", "token_type_embeddings"),
      (".", "/"),
      ("LayerNorm/weight", "LayerNorm/gamma"),
      ("LayerNorm/bias", "LayerNorm/beta"),
      ("weight", "kernel"),
  )

  if not os.path.isdir(__a		):
    os.makedirs(__a		)

  lowerCamelCase__:				Tuple        =model.state_dict()

  def to_tf_var_name(__a		):
    for patt, repl in iter(__a		):
      lowerCamelCase__:				Tuple        =name.replace(__a     ,   __a		)
    return F"""bert/{name}"""

  def create_tf_var(__a     ,   __a     ,   __a		):
    lowerCamelCase__:				Union[str, Any]        =tf.dtypes.as_dtype(tensor.dtype		)
    lowerCamelCase__:				Any        =tf.get_variable(dtype=__a     ,   shape=tensor.shape     ,   name=__a     ,   initializer=tf.zeros_initializer()		)
    session.run(tf.variables_initializer([tf_var]		)		)
    session.run(__a		)
    return tf_var

  tf.reset_default_graph()
  with tf.Session() as session:
    for var_name in state_dict:
      lowerCamelCase__:				Tuple        =to_tf_var_name(__a		)
      lowerCamelCase__:				str        =state_dict[var_name].numpy()
      if any(x in var_name for x in tensors_to_transpose		):
        lowerCamelCase__:				List[Any]        =torch_tensor.T
      lowerCamelCase__:				Optional[int]        =create_tf_var(tensor=__a     ,   name=__a     ,   session=__a		)
      tf.keras.backend.set_value(__a     ,   __a		)
      lowerCamelCase__:				Union[str, Any]        =session.run(__a		)
      print(F"""Successfully created {tf_name}: {np.allclose(__a     ,   __a		)}"""		)

    lowerCamelCase__:				List[Any]        =tf.train.Saver(tf.trainable_variables()		)
    saver.save(__a     ,   os.path.join(__a     ,   model_name.replace("-"     ,   "_"		) + ".ckpt"		)		)





def 					lowerCAmelCase_			(	__a=None		)					->		Tuple:







  """simple docstring"""





  lowerCamelCase__:				List[str]        =argparse.ArgumentParser()
  parser.add_argument("--model_name"     ,   type=__a     ,   required=__a     ,   help="model name e.g. bert-base-uncased"		)
  parser.add_argument(
      "--cache_dir"     ,   type=__a     ,   default=__a     ,   required=__a     ,   help="Directory containing pytorch model"		)
  parser.add_argument("--pytorch_model_path"     ,   type=__a     ,   required=__a     ,   help="/path/to/<pytorch-model-name>.bin"		)
  parser.add_argument("--tf_cache_dir"     ,   type=__a     ,   required=__a     ,   help="Directory in which to save tensorflow model"		)
  lowerCamelCase__:				Optional[Any]        =parser.parse_args(__a		)

  lowerCamelCase__:				Dict        =BertModel.from_pretrained(
      pretrained_model_name_or_path=args.model_name     ,   state_dict=torch.load(args.pytorch_model_path		)     ,   cache_dir=args.cache_dir     ,   )

  convert_pytorch_checkpoint_to_tf(model=__a     ,   ckpt_dir=args.tf_cache_dir     ,   model_name=args.model_name		)


if __name__ == "__main__":
     main()

