






import argparse
import os

import numpy as np
import tensorflow as tf
import torch

from transformers import BertModel






def   UpperCamelCase  (       snake_case__     :   BertModel	,snake_case__     :   str	,snake_case__     :   str  ):




     '''simple docstring'''






     __snake_case			:Optional[Any]          =						("""dense.weight""", """attention.self.query""", """attention.self.key""", """attention.self.value""")

     __snake_case			:List[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(snake_case__  ):
          os.makedirs(snake_case__  )

     __snake_case			:Any          =						model.state_dict()

     def to_tf_var_name(snake_case__     :   str  ):
          for patt, repl in iter(snake_case__  ):
               __snake_case			:List[Any]          =						name.replace(snake_case__	,snake_case__  )
          return f'''bert/{name}'''

     def create_tf_var(snake_case__     :   np.ndarray	,snake_case__     :   str	,snake_case__     :   tf.Session  ):
          __snake_case			:Union[str, Any]          =						tf.dtypes.as_dtype(tensor.dtype  )
          __snake_case			:Tuple          =						tf.get_variable(dtype=snake_case__	,shape=tensor.shape	,name=snake_case__	,initializer=tf.zeros_initializer()  )
          session.run(tf.variables_initializer([tf_var]  )  )
          session.run(snake_case__  )
          return tf_var

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

          __snake_case			:Dict          =						tf.train.Saver(tf.trainable_variables()  )
          saver.save(snake_case__	,os.path.join(snake_case__	,model_name.replace("""-"""	,"""_"""  ) + """.ckpt"""  )  )






def   UpperCamelCase  (       snake_case__     :   Any=None  ):




     '''simple docstring'''






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

     __snake_case			:str          =						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=snake_case__	,ckpt_dir=args.tf_cache_dir	,model_name=args.model_name  )


if __name__ == "__main__":
     main()










import unittest

from transformers import load_tool

from .test_tools_common import ToolTesterMixin


lowerCamelCase__							=     """
Hugging Face was founded in 2016 by French entrepreneurs Clément Delangue, Julien Chaumond, and Thomas Wolf originally as a company that developed a chatbot app targeted at teenagers.[2] After open-sourcing the model behind the chatbot, the company pivoted to focus on being a platform for machine learning.

In March 2021, Hugging Face raised $40 million in a Series B funding round.[3]

On April 28, 2021, the company launched the BigScience Research Workshop in collaboration with several other research groups to release an open large language model.[4] In 2022, the workshop concluded with the announcement of BLOOM, a multilingual large language model with 176 billion parameters.[5]
"""




class 			snake_case__   (							unittest.TestCase	,    lowercase_):

       '''simple docstring'''



       def 					__lowercase						(							self    ) ->      List[str]:






            '''simple docstring'''





            __snake_case			:int          =						load_tool("""text-question-answering"""    )
            self.tool.setup()
            __snake_case			:Tuple          =						load_tool("""text-question-answering"""			,						remote=a__    )



       def 					__lowercase						(							self    ) ->      Any:






            '''simple docstring'''





            __snake_case			:List[str]          =						self.tool(a__			,						"""What did Hugging Face do in April 2021?"""    )
            self.assertEqual(a__			,						"""launched the BigScience Research Workshop"""    )



       def 					__lowercase						(							self    ) ->      Any:






            '''simple docstring'''





            __snake_case			:List[str]          =						self.remote_tool(a__			,						"""What did Hugging Face do in April 2021?"""    )
            self.assertEqual(a__			,						"""launched the BigScience Research Workshop"""    )



       def 					__lowercase						(							self    ) ->      Dict:






            '''simple docstring'''





            __snake_case			:Optional[int]          =						self.tool(text=a__			,						question="""What did Hugging Face do in April 2021?"""    )
            self.assertEqual(a__			,						"""launched the BigScience Research Workshop"""    )



       def 					__lowercase						(							self    ) ->      Tuple:






            '''simple docstring'''





            __snake_case			:List[str]          =						self.remote_tool(text=a__			,						question="""What did Hugging Face do in April 2021?"""    )
            self.assertEqual(a__			,						"""launched the BigScience Research Workshop"""    )


