




import argparse
import os

import numpy as np
import tensorflow as tf
import torch

from transformers import BertModel

def  UpperCAmelCase_							(				__UpperCAmelCase						:     BertModel      ,      __UpperCAmelCase						:     str      ,      __UpperCAmelCase						:     str							) ->			Any:
      SCREAMING_SNAKE_CASE_     						=      ('dense.weight', 'attention.self.query', 'attention.self.key', 'attention.self.value')

      SCREAMING_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(__a							):
            os.makedirs(__a							)

      SCREAMING_SNAKE_CASE_     						=      model.state_dict()

      def to_tf_var_name(__UpperCAmelCase						:     str							):
            for patt, repl in iter(__a							):
                  SCREAMING_SNAKE_CASE_     						=      name.replace(__a      ,      __a							)
            return f"bert/{name}"

      def create_tf_var(__UpperCAmelCase						:     np.ndarray      ,      __UpperCAmelCase						:     str      ,      __UpperCAmelCase						:     tf.Session							):
            SCREAMING_SNAKE_CASE_     						=      tf.dtypes.as_dtype(tensor.dtype							)
            SCREAMING_SNAKE_CASE_     						=      tf.get_variable(dtype=__a      ,      shape=tensor.shape      ,      name=__a      ,      initializer=tf.zeros_initializer()							)
            session.run(tf.variables_initializer([tf_var]							)							)
            session.run(__a							)
            return tf_var

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

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

def  UpperCAmelCase_							(				__UpperCAmelCase						:     Union[str, Any]=None							) ->			Union[str, Any]:
      SCREAMING_SNAKE_CASE_     						=      argparse.ArgumentParser()
      parser.add_argument('--model_name'      ,      type=__a      ,      required=__a      ,      help='model name e.g. bert-base-uncased'							)
      parser.add_argument(
          '--cache_dir'      ,      type=__a      ,      default=__a      ,      required=__a      ,      help='Directory containing pytorch model'							)
      parser.add_argument('--pytorch_model_path'      ,      type=__a      ,      required=__a      ,      help='/path/to/<pytorch-model-name>.bin'							)
      parser.add_argument('--tf_cache_dir'      ,      type=__a      ,      required=__a      ,      help='Directory in which to save tensorflow model'							)
      SCREAMING_SNAKE_CASE_     						=      parser.parse_args(__a							)

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


if __name__ == "__main__":
   main()





from packaging import version

from .import_utils import is_accelerate_available


if is_accelerate_available():
   import accelerate

def  UpperCAmelCase_							(				__UpperCAmelCase						:     Optional[int]							) ->			int:
      if not is_accelerate_available():
            return method
      SCREAMING_SNAKE_CASE_     						=      version.parse(accelerate.__version__							).base_version
      if version.parse(__UpperCAmelCase							) < version.parse('0.17.0'							):
            return method

      def wrapper(self						:     Optional[int]      ,      *__UpperCAmelCase						:     Optional[Any]      ,      **__UpperCAmelCase						:     Optional[Any]							):
            if hasattr(self      ,      '_hf_hook'							) and hasattr(self._hf_hook      ,      'pre_forward'							):
                  self._hf_hook.pre_forward(self							)
            return method(self      ,      *__UpperCAmelCase      ,      **__UpperCAmelCase							)

      return wrapper