




'''simple docstring'''




import argparse
import os

import numpy as np
import tensorflow as tf
import torch

from transformers import BertModel





def      a_							(      __snake_case							:			BertModel   ,       __snake_case							:			str   ,       __snake_case							:			str   )							->						str:



       """simple docstring"""
       lowerCamelCase_							=('''dense.weight''', '''attention.self.query''', '''attention.self.key''', '''attention.self.value''')

       lowerCamelCase_							=(
           ('''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   )

       lowerCamelCase_							=model.state_dict()

       def to_tf_var_name(__snake_case							:			str   ):
              for patt, repl in iter(__snake_case   ):
                     lowerCamelCase_							=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   ):
              lowerCamelCase_							=tf.dtypes.as_dtype(tensor.dtype   )
              lowerCamelCase_							=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:
                     lowerCamelCase_							=to_tf_var_name(__snake_case   )
                     lowerCamelCase_							=state_dict[var_name].numpy()
                     if any(x in var_name for x in tensors_to_transpose   ):
                            lowerCamelCase_							=torch_tensor.T
                     lowerCamelCase_							=create_tf_var(tensor=__snake_case   ,       name=__snake_case   ,       session=__snake_case   )
                     tf.keras.backend.set_value(__snake_case   ,       __snake_case   )
                     lowerCamelCase_							=session.run(__snake_case   )
                     print(F'''Successfully created {tf_name}: {np.allclose(__snake_case   ,       __snake_case   )}'''   )

              lowerCamelCase_							=tf.train.Saver(tf.trainable_variables()   )
              saver.save(__snake_case   ,       os.path.join(__snake_case   ,       model_name.replace('''-'''   ,       '''_'''   ) + '''.ckpt'''   )   )





def      a_							(      __snake_case							:			Union[str, Any]=None   )							->						Any:



       """simple docstring"""
       lowerCamelCase_							=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'''   )
       lowerCamelCase_							=parser.parse_args(__snake_case   )

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











'''simple docstring'''


import unittest

import numpy as np

from transformers import BertConfig, is_flax_available
from transformers.testing_utils import require_flax, slow

from ...test_modeling_flax_common import FlaxModelTesterMixin, floats_tensor, ids_tensor, random_attention_mask


if is_flax_available():
 from transformers.models.bert.modeling_flax_bert import (
     FlaxBertForMaskedLM,
     FlaxBertForMultipleChoice,
     FlaxBertForNextSentencePrediction,
     FlaxBertForPreTraining,
     FlaxBertForQuestionAnswering,
     FlaxBertForSequenceClassification,
     FlaxBertForTokenClassification,
     FlaxBertModel,
 )
class       a_   (				unittest.TestCase							):





     def __init__( self		:					List[str]	,      lowercase		:					str	,      lowercase		:					Union[str, Any]=13	,      lowercase		:					int=7	,      lowercase		:					List[str]=True	,      lowercase		:					int=True	,      lowercase		:					str=True	,      lowercase		:					Any=True	,      lowercase		:					List[str]=99	,      lowercase		:					Union[str, Any]=32	,      lowercase		:					Optional[Any]=5	,      lowercase		:					Dict=4	,      lowercase		:					Dict=37	,      lowercase		:					Dict="gelu"	,      lowercase		:					Optional[int]=0.1	,      lowercase		:					str=0.1	,      lowercase		:					List[Any]=512	,      lowercase		:					str=16	,      lowercase		:					Dict=2	,      lowercase		:					Any=0.02	,      lowercase		:					Any=4	,      ):





        """simple docstring"""




        lowercase_			:List[str]    			= parent
        lowercase_			:Any    			= batch_size
        lowercase_			:Dict    			= seq_length
        lowercase_			:Union[str, Any]    			= is_training
        lowercase_			:Optional[int]    			= use_attention_mask
        lowercase_			:Any    			= use_token_type_ids
        lowercase_			:Union[str, Any]    			= use_labels
        lowercase_			:Dict    			= vocab_size
        lowercase_			:Tuple    			= hidden_size
        lowercase_			:Tuple    			= num_hidden_layers
        lowercase_			:Optional[int]    			= num_attention_heads
        lowercase_			:Optional[Any]    			= intermediate_size
        lowercase_			:str    			= hidden_act
        lowercase_			:Tuple    			= hidden_dropout_prob
        lowercase_			:Optional[Any]    			= attention_probs_dropout_prob
        lowercase_			:Tuple    			= max_position_embeddings
        lowercase_			:Any    			= type_vocab_size
        lowercase_			:int    			= type_sequence_label_size
        lowercase_			:Tuple    			= initializer_range
        lowercase_			:Optional[Any]    			= num_choices





     def 							lowercase__      ( self		:					List[Any]						):





        """simple docstring"""




        lowercase_			:int    			= ids_tensor([self.batch_size, self.seq_length]	,      self.vocab_size						)

        lowercase_			:Union[str, Any]    			= None
        if self.use_attention_mask:
           lowercase_			:Dict    			= random_attention_mask([self.batch_size, self.seq_length]						)

        lowercase_			:List[str]    			= None
        if self.use_token_type_ids:
           lowercase_			:str    			= ids_tensor([self.batch_size, self.seq_length]	,      self.type_vocab_size						)

        lowercase_			:Optional[Any]    			= BertConfig(
            vocab_size=self.vocab_size	,      hidden_size=self.hidden_size	,      num_hidden_layers=self.num_hidden_layers	,      num_attention_heads=self.num_attention_heads	,      intermediate_size=self.intermediate_size	,      hidden_act=self.hidden_act	,      hidden_dropout_prob=self.hidden_dropout_prob	,      attention_probs_dropout_prob=self.attention_probs_dropout_prob	,      max_position_embeddings=self.max_position_embeddings	,      type_vocab_size=self.type_vocab_size	,      is_decoder=lowercase	,      initializer_range=self.initializer_range	,      )

        return config, input_ids, token_type_ids, attention_mask





     def 							lowercase__      ( self		:					Union[str, Any]						):





        """simple docstring"""




        lowercase_			:int    			= self.prepare_config_and_inputs()
        lowercase_			,							lowercase_			,							lowercase_			,							lowercase_			:Tuple    			= config_and_inputs
        lowercase_			:Tuple    			= {"input_ids": input_ids, "token_type_ids": token_type_ids, "attention_mask": attention_mask}
        return config, inputs_dict







     def 							lowercase__      ( self		:					List[Any]						):





        """simple docstring"""




        lowercase_			:Any    			= self.prepare_config_and_inputs()
        lowercase_			,							lowercase_			,							lowercase_			,							lowercase_			:Union[str, Any]    			= config_and_inputs

        lowercase_			:Dict    			= True
        lowercase_			:Optional[int]    			= floats_tensor([self.batch_size, self.seq_length, self.hidden_size]						)
        lowercase_			:str    			= ids_tensor([self.batch_size, self.seq_length]	,      vocab_size=2						)

        return (
            config,
            input_ids,
            attention_mask,
            encoder_hidden_states,
            encoder_attention_mask,
        )







@require_flax
class       a_   (				_lowerCAmelCase	,						unittest.TestCase							):
     __A      =			True

     __A      =			(
         (
             FlaxBertModel,
             FlaxBertForPreTraining,
             FlaxBertForMaskedLM,
             FlaxBertForMultipleChoice,
             FlaxBertForQuestionAnswering,
             FlaxBertForNextSentencePrediction,
             FlaxBertForSequenceClassification,
             FlaxBertForTokenClassification,
             FlaxBertForQuestionAnswering,
         )
         if is_flax_available()
         else ()
     )





     def 							lowercase__      ( self		:					Any						):





        """simple docstring"""




        lowercase_			:Optional[Any]    			= FlaxBertModelTester(self						)







     @slow
     def 							lowercase__      ( self		:					List[str]						):





        """simple docstring"""




        lowercase_			:List[str]    			= FlaxBertModel.from_pretrained("bert-base-cased"						)
        lowercase_			:str    			= model(np.ones((1, 1)						)						)
        self.assertIsNotNone(lowercase						)


