


import contextlib
import importlib
import io
import unittest

import transformers

# Try to import everything from transformers to ensure every object can be loaded.
from transformers import *  # noqa F406
from transformers.testing_utils import DUMMY_UNKNOWN_IDENTIFIER, require_flax, require_tf, require_torch
from transformers.utils import ContextManagers, find_labels, is_flax_available, is_tf_available, is_torch_available


if is_torch_available():
 from transformers import BertForPreTraining, BertForQuestionAnswering, BertForSequenceClassification

if is_tf_available():
 from transformers import TFBertForPreTraining, TFBertForQuestionAnswering, TFBertForSequenceClassification

if is_flax_available():
 from transformers import FlaxBertForPreTraining, FlaxBertForQuestionAnswering, FlaxBertForSequenceClassification


__a	 =      DUMMY_UNKNOWN_IDENTIFIER
# An actual model hosted on huggingface.co

__a	 =      """main"""
# Default branch name
__a	 =      """f2c752cfc5c0ab6f4bdec59acea69eefbee381c2"""
# One particular commit (not the top of `main`)
__a	 =      """aaaaaaa"""
# This commit does not exist, so we should 404.

__a	 =      """d9e9f15bc825e4b2c9249e9578f884bbcb5e3684"""
# Sha-1 of config.json on the top of `main`, for checking purposes
__a	 =      """4b243c475af8d0a7754e87d7d096c92e5199ec2fe168a2ee7998e3b8e9bcb1d3"""
@contextlib.contextmanager
def 	_UpperCamelCase					(    )    ->int:
   print("""Welcome!""" )
   yield
   print("""Bye!""" )
@contextlib.contextmanager
def 	_UpperCamelCase					(    )    ->Dict:
   print("""Bonjour!""" )
   yield
   print("""Au revoir!""" )






class 	__lowercase					(	unittest.TestCase	):





      def 							_lowercase					(		self    :       Optional[int]							)     -> str:






         """simple docstring"""







         assert transformers.__spec__ is not None
         assert importlib.util.find_spec("""transformers"""							) is not None






class 	__lowercase					(	unittest.TestCase	):





      @unittest.mock.patch("""sys.stdout"""       ,							new_callable=io.StringIO							)
      def 							_lowercase					(		self    :       Optional[Any]       ,							__lowerCamelCase    :       List[str]							)     -> str:






         """simple docstring"""







         with ContextManagers([]							):
            print("""Transformers are awesome!"""							)
         # The print statement adds a new line at the end of the output
         self.assertEqual(mock_stdout.getvalue()       ,							"""Transformers are awesome!\n"""							)





      @unittest.mock.patch("""sys.stdout"""       ,							new_callable=io.StringIO							)
      def 							_lowercase					(		self    :       List[Any]       ,							__lowerCamelCase    :       Tuple							)     -> Dict:






         """simple docstring"""







         with ContextManagers([context_en()]							):
            print("""Transformers are awesome!"""							)
         # The output should be wrapped with an English welcome and goodbye
         self.assertEqual(mock_stdout.getvalue()       ,							"""Welcome!\nTransformers are awesome!\nBye!\n"""							)





      @unittest.mock.patch("""sys.stdout"""       ,							new_callable=io.StringIO							)
      def 							_lowercase					(		self    :       List[Any]       ,							__lowerCamelCase    :       List[Any]							)     -> str:






         """simple docstring"""







         with ContextManagers([context_fr(), context_en()]							):
            print("""Transformers are awesome!"""							)
         # The output should be wrapped with an English and French welcome and goodbye
         self.assertEqual(mock_stdout.getvalue()       ,							"""Bonjour!\nWelcome!\nTransformers are awesome!\nBye!\nAu revoir!\n"""							)





      @require_torch
      def 							_lowercase					(		self    :       Tuple							)     -> str:






         """simple docstring"""







         self.assertEqual(find_labels(__lowerCamelCase							)       ,							["""labels"""]							)
         self.assertEqual(find_labels(__lowerCamelCase							)       ,							["""labels""", """next_sentence_label"""]							)
         self.assertEqual(find_labels(__lowerCamelCase							)       ,							["""start_positions""", """end_positions"""]							)






         class 	__lowercase					(	__snake_case	):
               pass

         self.assertEqual(find_labels(__lowerCamelCase							)       ,							["""labels"""]							)





      @require_tf
      def 							_lowercase					(		self    :       List[Any]							)     -> List[str]:






         """simple docstring"""







         self.assertEqual(find_labels(__lowerCamelCase							)       ,							["""labels"""]							)
         self.assertEqual(find_labels(__lowerCamelCase							)       ,							["""labels""", """next_sentence_label"""]							)
         self.assertEqual(find_labels(__lowerCamelCase							)       ,							["""start_positions""", """end_positions"""]							)






         class 	__lowercase					(	__snake_case	):
               pass

         self.assertEqual(find_labels(__lowerCamelCase							)       ,							["""labels"""]							)





      @require_flax
      def 							_lowercase					(		self    :       Tuple							)     -> str:






         """simple docstring"""







         self.assertEqual(find_labels(__lowerCamelCase							)       ,							[]							)
         self.assertEqual(find_labels(__lowerCamelCase							)       ,							[]							)
         self.assertEqual(find_labels(__lowerCamelCase							)       ,							[]							)






         class 	__lowercase					(	__snake_case	):
               pass

         self.assertEqual(find_labels(__lowerCamelCase							)       ,							[]							)







import argparse
import os

import numpy as np
import tensorflow as tf
import torch

from transformers import BertModel
def 	_UpperCamelCase					(    lowerCAmelCase_	,    lowerCAmelCase_	,    lowerCAmelCase_ )    ->Optional[Any]:
   UpperCAmelCase      					=  ("""dense.weight""", """attention.self.query""", """attention.self.key""", """attention.self.value""")

   UpperCAmelCase      					=  (
       ("""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(lowerCAmelCase_ ):
      os.makedirs(lowerCAmelCase_ )

   UpperCAmelCase      					=  model.state_dict()

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

   def create_tf_var(lowerCAmelCase_	,    lowerCAmelCase_	,    lowerCAmelCase_ ):
      UpperCAmelCase      					=  tf.dtypes.as_dtype(tensor.dtype )
      UpperCAmelCase      					=  tf.get_variable(dtype=lowerCAmelCase_	,    shape=tensor.shape	,    name=lowerCAmelCase_	,    initializer=tf.zeros_initializer() )
      session.run(tf.variables_initializer([tf_var] ) )
      session.run(lowerCAmelCase_ )
      return tf_var

   tf.reset_default_graph()
   with tf.Session() as session:
      for var_name in state_dict:
         UpperCAmelCase      					=  to_tf_var_name(lowerCAmelCase_ )
         UpperCAmelCase      					=  state_dict[var_name].numpy()
         if any(x in var_name for x in tensors_to_transpose ):
            UpperCAmelCase      					=  torch_tensor.T
         UpperCAmelCase      					=  create_tf_var(tensor=lowerCAmelCase_	,    name=lowerCAmelCase_	,    session=lowerCAmelCase_ )
         tf.keras.backend.set_value(lowerCAmelCase_	,    lowerCAmelCase_ )
         UpperCAmelCase      					=  session.run(lowerCAmelCase_ )
         print(F"""Successfully created {tf_name}: {np.allclose(lowerCAmelCase_	,    lowerCAmelCase_ )}""" )

      UpperCAmelCase      					=  tf.train.Saver(tf.trainable_variables() )
      saver.save(lowerCAmelCase_	,    os.path.join(lowerCAmelCase_	,    model_name.replace("""-"""	,    """_""" ) + """.ckpt""" ) )
def 	_UpperCamelCase					(    lowerCAmelCase_=None )    ->Union[str, Any]:
   UpperCAmelCase      					=  argparse.ArgumentParser()
   parser.add_argument("""--model_name"""	,    type=lowerCAmelCase_	,    required=lowerCAmelCase_	,    help="""model name e.g. bert-base-uncased""" )
   parser.add_argument(
       """--cache_dir"""	,    type=lowerCAmelCase_	,    default=lowerCAmelCase_	,    required=lowerCAmelCase_	,    help="""Directory containing pytorch model""" )
   parser.add_argument("""--pytorch_model_path"""	,    type=lowerCAmelCase_	,    required=lowerCAmelCase_	,    help="""/path/to/<pytorch-model-name>.bin""" )
   parser.add_argument("""--tf_cache_dir"""	,    type=lowerCAmelCase_	,    required=lowerCAmelCase_	,    help="""Directory in which to save tensorflow model""" )
   UpperCAmelCase      					=  parser.parse_args(lowerCAmelCase_ )

   UpperCAmelCase      					=  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=lowerCAmelCase_	,    ckpt_dir=args.tf_cache_dir	,    model_name=args.model_name )


if __name__ == "__main__":
 main()



