





import os
import tempfile
import unittest
from pathlib import Path

from transformers import AutoConfig, is_tf_available
from transformers.testing_utils import require_tf


if is_tf_available():
     import tensorflow as tf

     from transformers import TensorFlowBenchmark, TensorFlowBenchmarkArguments



@require_tf
class        _SCREAMING_SNAKE_CASE						(     unittest.TestCase   ):







 '''simple docstring'''

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



   '''simple docstring'''



   for model_result in results.values():
     for batch_size, sequence_length in zip(model_result["bs"]	,			model_result["ss"]):
       lowerCamelCase__:				str        =model_result["result"][batch_size][sequence_length]
       self.assertIsNotNone(UpperCAmelCase_)

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



   '''simple docstring'''



   lowerCamelCase__:				Optional[Any]        ="sshleifer/tiny-gpt2"
   lowerCamelCase__:				Optional[int]        =TensorFlowBenchmarkArguments(
       models=[MODEL_ID]	,			training=UpperCAmelCase_	,			inference=UpperCAmelCase_	,			sequence_lengths=[8]	,			batch_sizes=[1]	,			eager_mode=UpperCAmelCase_	,			multi_process=UpperCAmelCase_	,			)
   lowerCamelCase__:				Any        =TensorFlowBenchmark(UpperCAmelCase_)
   lowerCamelCase__:				Optional[int]        =benchmark.run()
   self.check_results_dict_not_empty(results.time_inference_result)
   self.check_results_dict_not_empty(results.memory_inference_result)

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



   '''simple docstring'''



   lowerCamelCase__:				Any        ="sgugger/tiny-distilbert-classification"
   lowerCamelCase__:				Optional[int]        =TensorFlowBenchmarkArguments(
       models=[MODEL_ID]	,			training=UpperCAmelCase_	,			inference=UpperCAmelCase_	,			sequence_lengths=[8]	,			batch_sizes=[1]	,			multi_process=UpperCAmelCase_	,			only_pretrain_model=UpperCAmelCase_	,			)
   lowerCamelCase__:				str        =TensorFlowBenchmark(UpperCAmelCase_)
   lowerCamelCase__:				str        =benchmark.run()
   self.check_results_dict_not_empty(results.time_inference_result)
   self.check_results_dict_not_empty(results.memory_inference_result)

 def 							SCREAMING_SNAKE_CASE_ (self    :       Tuple)	->Optional[Any]:



   '''simple docstring'''



   lowerCamelCase__:				List[Any]        ="sshleifer/tiny-gpt2"
   lowerCamelCase__:				Dict        =TensorFlowBenchmarkArguments(
       models=[MODEL_ID]	,			training=UpperCAmelCase_	,			inference=UpperCAmelCase_	,			sequence_lengths=[8]	,			batch_sizes=[1]	,			multi_process=UpperCAmelCase_	,			)
   lowerCamelCase__:				Optional[int]        =TensorFlowBenchmark(UpperCAmelCase_)
   lowerCamelCase__:				List[Any]        =benchmark.run()
   self.check_results_dict_not_empty(results.time_inference_result)
   self.check_results_dict_not_empty(results.memory_inference_result)

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



   '''simple docstring'''



   lowerCamelCase__:				Dict        ="sshleifer/tiny-gpt2"
   lowerCamelCase__:				Any        =AutoConfig.from_pretrained(UpperCAmelCase_)
   lowerCamelCase__:				List[str]        =TensorFlowBenchmarkArguments(
       models=[MODEL_ID]	,			training=UpperCAmelCase_	,			inference=UpperCAmelCase_	,			sequence_lengths=[8]	,			batch_sizes=[1]	,			eager_mode=UpperCAmelCase_	,			multi_process=UpperCAmelCase_	,			)
   lowerCamelCase__:				str        =TensorFlowBenchmark(UpperCAmelCase_	,			[config])
   lowerCamelCase__:				Any        =benchmark.run()
   self.check_results_dict_not_empty(results.time_inference_result)
   self.check_results_dict_not_empty(results.memory_inference_result)

 def 							SCREAMING_SNAKE_CASE_ (self    :       str)	->Optional[Any]:



   '''simple docstring'''



   lowerCamelCase__:				int        ="sshleifer/tiny-gpt2"
   lowerCamelCase__:				Dict        =AutoConfig.from_pretrained(UpperCAmelCase_)
   lowerCamelCase__:				Optional[Any]        =TensorFlowBenchmarkArguments(
       models=[MODEL_ID]	,			training=UpperCAmelCase_	,			inference=UpperCAmelCase_	,			sequence_lengths=[8]	,			batch_sizes=[1]	,			multi_process=UpperCAmelCase_	,			)
   lowerCamelCase__:				Optional[int]        =TensorFlowBenchmark(UpperCAmelCase_	,			[config])
   lowerCamelCase__:				Tuple        =benchmark.run()
   self.check_results_dict_not_empty(results.time_inference_result)
   self.check_results_dict_not_empty(results.memory_inference_result)

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



   '''simple docstring'''



   lowerCamelCase__:				Union[str, Any]        ="sshleifer/tiny-gpt2"
   lowerCamelCase__:				Any        =TensorFlowBenchmarkArguments(
       models=[MODEL_ID]	,			training=UpperCAmelCase_	,			inference=UpperCAmelCase_	,			sequence_lengths=[8]	,			batch_sizes=[1]	,			multi_process=UpperCAmelCase_	,			)
   lowerCamelCase__:				int        =TensorFlowBenchmark(UpperCAmelCase_)
   lowerCamelCase__:				Any        =benchmark.run()
   self.check_results_dict_not_empty(results.time_train_result)
   self.check_results_dict_not_empty(results.memory_train_result)

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



   '''simple docstring'''



   lowerCamelCase__:				Optional[int]        ="sshleifer/tiny-gpt2"
   lowerCamelCase__:				Any        =AutoConfig.from_pretrained(UpperCAmelCase_)
   lowerCamelCase__:				Optional[int]        =TensorFlowBenchmarkArguments(
       models=[MODEL_ID]	,			training=UpperCAmelCase_	,			inference=UpperCAmelCase_	,			sequence_lengths=[8]	,			batch_sizes=[1]	,			multi_process=UpperCAmelCase_	,			)
   lowerCamelCase__:				Union[str, Any]        =TensorFlowBenchmark(UpperCAmelCase_	,			[config])
   lowerCamelCase__:				Tuple        =benchmark.run()
   self.check_results_dict_not_empty(results.time_train_result)
   self.check_results_dict_not_empty(results.memory_train_result)

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



   '''simple docstring'''



   lowerCamelCase__:				str        ="patrickvonplaten/t5-tiny-random"
   lowerCamelCase__:				str        =AutoConfig.from_pretrained(UpperCAmelCase_)
   lowerCamelCase__:				Union[str, Any]        =TensorFlowBenchmarkArguments(
       models=[MODEL_ID]	,			training=UpperCAmelCase_	,			inference=UpperCAmelCase_	,			sequence_lengths=[8]	,			batch_sizes=[1]	,			multi_process=UpperCAmelCase_	,			)
   lowerCamelCase__:				int        =TensorFlowBenchmark(UpperCAmelCase_	,			configs=[config])
   lowerCamelCase__:				List[Any]        =benchmark.run()
   self.check_results_dict_not_empty(results.time_inference_result)
   self.check_results_dict_not_empty(results.memory_inference_result)

 @unittest.skipIf(is_tf_available() and len(tf.config.list_physical_devices("GPU")) == 0	,			"Cannot do xla on CPU.")
 def 							SCREAMING_SNAKE_CASE_ (self    :       Any)	->int:



   '''simple docstring'''



   lowerCamelCase__:				List[Any]        ="sshleifer/tiny-gpt2"
   lowerCamelCase__:				Tuple        =TensorFlowBenchmarkArguments(
       models=[MODEL_ID]	,			training=UpperCAmelCase_	,			inference=UpperCAmelCase_	,			sequence_lengths=[8]	,			batch_sizes=[1]	,			use_xla=UpperCAmelCase_	,			multi_process=UpperCAmelCase_	,			)
   lowerCamelCase__:				Tuple        =TensorFlowBenchmark(UpperCAmelCase_)
   lowerCamelCase__:				Optional[Any]        =benchmark.run()
   self.check_results_dict_not_empty(results.time_inference_result)
   self.check_results_dict_not_empty(results.memory_inference_result)

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



   '''simple docstring'''



   lowerCamelCase__:				List[Any]        ="sshleifer/tiny-gpt2"
   with tempfile.TemporaryDirectory() as tmp_dir:
     lowerCamelCase__:				Any        =TensorFlowBenchmarkArguments(
         models=[MODEL_ID]	,			inference=UpperCAmelCase_	,			save_to_csv=UpperCAmelCase_	,			sequence_lengths=[8]	,			batch_sizes=[1]	,			inference_time_csv_file=os.path.join(UpperCAmelCase_	,			"inf_time.csv")	,			inference_memory_csv_file=os.path.join(UpperCAmelCase_	,			"inf_mem.csv")	,			env_info_csv_file=os.path.join(UpperCAmelCase_	,			"env.csv")	,			multi_process=UpperCAmelCase_	,			)
     lowerCamelCase__:				int        =TensorFlowBenchmark(UpperCAmelCase_)
     benchmark.run()
     self.assertTrue(Path(os.path.join(UpperCAmelCase_	,			"inf_time.csv")).exists())
     self.assertTrue(Path(os.path.join(UpperCAmelCase_	,			"inf_mem.csv")).exists())
     self.assertTrue(Path(os.path.join(UpperCAmelCase_	,			"env.csv")).exists())







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



   '''simple docstring'''



   lowerCamelCase__:				List[str]        ="sshleifer/tiny-gpt2"

   def _check_summary_is_not_empty(UpperCAmelCase_    :       Optional[int]):
     self.assertTrue(hasattr(UpperCAmelCase_	,			"sequential"))
     self.assertTrue(hasattr(UpperCAmelCase_	,			"cumulative"))
     self.assertTrue(hasattr(UpperCAmelCase_	,			"current"))
     self.assertTrue(hasattr(UpperCAmelCase_	,			"total"))

   with tempfile.TemporaryDirectory() as tmp_dir:
     lowerCamelCase__:				Any        =TensorFlowBenchmarkArguments(
         models=[MODEL_ID]	,			inference=UpperCAmelCase_	,			sequence_lengths=[8]	,			batch_sizes=[1]	,			log_filename=os.path.join(UpperCAmelCase_	,			"log.txt")	,			log_print=UpperCAmelCase_	,			trace_memory_line_by_line=UpperCAmelCase_	,			eager_mode=UpperCAmelCase_	,			multi_process=UpperCAmelCase_	,			)
     lowerCamelCase__:				List[Any]        =TensorFlowBenchmark(UpperCAmelCase_)
     lowerCamelCase__:				Tuple        =benchmark.run()
     _check_summary_is_not_empty(result.inference_summary)
     self.assertTrue(Path(os.path.join(UpperCAmelCase_	,			"log.txt")).exists())








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

