


import argparse
import os

import numpy as np
import tensorflow as tf
import torch

from transformers import BertModel
def      UpperCAmelCase__		(UpperCamelCase_				,UpperCamelCase_				,UpperCamelCase_							):



     """simple docstring"""






     snake_case											=							('''dense.weight''', '''attention.self.query''', '''attention.self.key''', '''attention.self.value''')

     snake_case											=							(
         ('''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(UpperCamelCase_							):
          os.makedirs(UpperCamelCase_							)

     snake_case											=							model.state_dict()

     def to_tf_var_name(UpperCamelCase_							):
          for patt, repl in iter(UpperCamelCase_							):
               snake_case											=							name.replace(UpperCamelCase_				,UpperCamelCase_							)
          return F'''bert/{name}'''

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

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

          snake_case											=							tf.train.Saver(tf.trainable_variables()							)
          saver.save(UpperCamelCase_				,os.path.join(UpperCamelCase_				,model_name.replace('''-'''				,'''_'''							) + '''.ckpt'''							)							)
def      UpperCAmelCase__		(UpperCamelCase_=None							):



     """simple docstring"""






     snake_case											=							argparse.ArgumentParser()
     parser.add_argument('''--model_name'''				,type=UpperCamelCase_				,required=UpperCamelCase_				,help='''model name e.g. bert-base-uncased'''							)
     parser.add_argument(
         '''--cache_dir'''				,type=UpperCamelCase_				,default=UpperCamelCase_				,required=UpperCamelCase_				,help='''Directory containing pytorch model'''							)
     parser.add_argument('''--pytorch_model_path'''				,type=UpperCamelCase_				,required=UpperCamelCase_				,help='''/path/to/<pytorch-model-name>.bin'''							)
     parser.add_argument('''--tf_cache_dir'''				,type=UpperCamelCase_				,required=UpperCamelCase_				,help='''Directory in which to save tensorflow model'''							)
     snake_case											=							parser.parse_args(UpperCamelCase_							)

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


if __name__ == "__main__":
 main()








def      UpperCAmelCase__		(UpperCamelCase_ = 10_00							):



     """simple docstring"""






     return sum(2 * a * ((a - 1) // 2) for a in range(3				,n + 1							)							)


if __name__ == "__main__":
 print(solution())




