

'''simple docstring'''







import argparse
import os

import numpy as np
import tensorflow as tf
import torch

from transformers import BertModel
def       UpperCAmelCase__    (      UpperCAmelCase__,       UpperCAmelCase__,       UpperCAmelCase__				)			->			Dict:
       A_						=      ("""dense.weight""", """attention.self.query""", """attention.self.key""", """attention.self.value""")

       A_						=      (
           ("""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__				)

       A_						=      model.state_dict()

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

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

              A_						=      tf.train.Saver(tf.trainable_variables()				)
              saver.save(UpperCAmelCase__,       os.path.join(UpperCAmelCase__,       model_name.replace("""-""",       """_"""				) + """.ckpt"""				)				)
def       UpperCAmelCase__    (      UpperCAmelCase__=None				)			->			Any:
       A_						=      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"""				)
       A_						=      parser.parse_args(UpperCAmelCase__				)

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



'''simple docstring'''







def       UpperCAmelCase__    (      UpperCAmelCase__,       UpperCAmelCase__,       UpperCAmelCase__ = 0,       UpperCAmelCase__ = 0				)			->			int:
       A_						=      right or len(UpperCAmelCase__				) - 1
       if left > right:
              return -1
       elif list_data[left] == key:
              return left
       elif list_data[right] == key:
              return right
       else:
              return search(UpperCAmelCase__,       UpperCAmelCase__,       left + 1,       right - 1				)


if __name__ == "__main__":
      import doctest

      doctest.testmod()
