
import argparse
import os
import re

import packaging.version


lowerCamelCase__    :		Tuple          =   """examples/"""
lowerCamelCase__    :		Union[str, Any]          =   {
    """examples""": (re.compile(R"""^check_min_version\(\"[^\"]+\"\)\s*$""", re.MULTILINE), """check_min_version(\"VERSION\")\n"""),
    """init""": (re.compile(R"""^__version__\s+=\s+\"([^\"]+)\"\s*$""", re.MULTILINE), """__version__ = \"VERSION\"\n"""),
    """setup""": (re.compile(R"""^(\s*)version\s*=\s*\"[^\"]+\",""", re.MULTILINE), R"""\1version=\"VERSION\","""),
    """doc""": (re.compile(R"""^(\s*)release\s*=\s*\"[^\"]+\"$""", re.MULTILINE), """release = \"VERSION\"\n"""),
}
lowerCamelCase__    :		List[Any]          =   {
    """init""": """src/transformers/__init__.py""",
    """setup""": """setup.py""",
}
lowerCamelCase__    :		Any          =   """README.md"""

def 		UpperCamelCase	(      lowercase_ , lowercase_ , lowercase_   )		->	str:


  '''simple docstring'''






  with open(lowercase_ , """r""" , encoding="""utf-8""" , newline="""\n"""   ) as f:
    lowercase__  :   str    		=      f.read()
  lowercase__		,    lowercase__  :   Optional[int]    		=      REPLACE_PATTERNS[pattern]
  lowercase__  :   Optional[int]    		=      replace.replace("""VERSION""" , lowercase_   )
  lowercase__  :   int    		=      re_pattern.sub(lowercase_ , lowercase_   )
  with open(lowercase_ , """w""" , encoding="""utf-8""" , newline="""\n"""   ) as f:
    f.write(lowercase_   )

def 		UpperCamelCase	(      lowercase_   )		->	Tuple:


  '''simple docstring'''






  for folder, directories, fnames in os.walk(lowercase_   ):
    # Removing some of the folders with non-actively maintained examples from the walk
    if "research_projects" in directories:
      directories.remove("""research_projects"""   )
    if "legacy" in directories:
      directories.remove("""legacy"""   )
    for fname in fnames:
      if fname.endswith(""".py"""   ):
        update_version_in_file(os.path.join(lowercase_ , lowercase_   ) , lowercase_ , pattern="""examples"""   )

def 		UpperCamelCase	(      lowercase_ , lowercase_=False   )		->	int:


  '''simple docstring'''






  for pattern, fname in REPLACE_FILES.items():
    update_version_in_file(lowercase_ , lowercase_ , lowercase_   )
  if not patch:
    update_version_in_examples(lowercase_   )

def 		UpperCamelCase	(      )		->	Union[str, Any]:


  '''simple docstring'''






  lowercase__  :   List[Any]    		=      """🤗 Transformers currently provides the following architectures"""
  lowercase__  :   int    		=      """1. Want to contribute a new model?"""
  with open(lowercase_ , """r""" , encoding="""utf-8""" , newline="""\n"""   ) as f:
    lowercase__  :   Any    		=      f.readlines()

  # Find the start of the list.
  lowercase__  :   Tuple    		=      0
  while not lines[start_index].startswith(_start_prompt   ):
    start_index += 1
  start_index += 1

  lowercase__  :   int    		=      start_index
  # Update the lines in the model list.
  while not lines[index].startswith(_end_prompt   ):
    if lines[index].startswith("""1."""   ):
      lowercase__  :   int    		=      lines[index].replace(
          """https://huggingface.co/docs/transformers/main/model_doc""" , """https://huggingface.co/docs/transformers/model_doc""" , )
    index += 1

  with open(lowercase_ , """w""" , encoding="""utf-8""" , newline="""\n"""   ) as f:
    f.writelines(lowercase_   )

def 		UpperCamelCase	(      )		->	List[Any]:


  '''simple docstring'''






  with open(REPLACE_FILES["""init"""] , """r"""   ) as f:
    lowercase__  :   Union[str, Any]    		=      f.read()
  lowercase__  :   Tuple    		=      REPLACE_PATTERNS["""init"""][0].search(lowercase_   ).groups()[0]
  return packaging.version.parse(lowercase_   )

def 		UpperCamelCase	(      lowercase_=False   )		->	str:


  '''simple docstring'''






  lowercase__  :   Union[str, Any]    		=      get_version()
  if patch and default_version.is_devrelease:
    raise ValueError("""Can't create a patch version from the dev branch, checkout a released version!"""   )
  if default_version.is_devrelease:
    lowercase__  :   List[Any]    		=      default_version.base_version
  elif patch:
    lowercase__  :   List[str]    		=      F'{default_version.major}.{default_version.minor}.{default_version.micro + 1}'
  else:
    lowercase__  :   List[Any]    		=      F'{default_version.major}.{default_version.minor + 1}.0'

  # Now let's ask nicely if that's the right one.
  lowercase__  :   List[Any]    		=      input(F'Which version are you releasing? [{default_version}]'   )
  if len(lowercase_   ) == 0:
    lowercase__  :   Tuple    		=      default_version

  print(F'Updating version to {version}.'   )
  global_version_update(lowercase_ , patch=lowercase_   )
  if not patch:
    print("""Cleaning main README, don't forget to run `make fix-copies`."""   )
    clean_main_ref_in_model_list()

def 		UpperCamelCase	(      )		->	List[Any]:


  '''simple docstring'''






  lowercase__  :   str    		=      get_version()
  lowercase__  :   List[Any]    		=      F'{current_version.major}.{current_version.minor + 1}.0.dev0'
  lowercase__  :   List[Any]    		=      current_version.base_version

  # Check with the user we got that right.
  lowercase__  :   int    		=      input(F'Which version are we developing now? [{dev_version}]'   )
  if len(lowercase_   ) == 0:
    lowercase__  :   List[Any]    		=      dev_version

  print(F'Updating version to {version}.'   )
  global_version_update(lowercase_   )
  print("""Cleaning main README, don't forget to run `make fix-copies`."""   )
  clean_main_ref_in_model_list()


if __name__ == "__main__":
     lowerCamelCase__    :		Union[str, Any]          =   argparse.ArgumentParser()
     parser.add_argument("""--post_release""", action="""store_true""", help="""Whether this is pre or post release.""")
     parser.add_argument("""--patch""", action="""store_true""", help="""Whether or not this is a patch release.""")
     lowerCamelCase__    :		Tuple          =   parser.parse_args()
     if not args.post_release:
          pre_release_work(patch=args.patch)
     elif args.patch:
          print("""Nothing to do after a patch :-)""")
     else:
          post_release_work()








import argparse
import os

import numpy as np
import tensorflow as tf
import torch

from transformers import BertModel

def 		UpperCamelCase	(      lowercase_ , lowercase_ , lowercase_   )		->	Dict:


  '''simple docstring'''






  lowercase__  :   Dict    		=      ("""dense.weight""", """attention.self.query""", """attention.self.key""", """attention.self.value""")

  lowercase__  :   Union[str, 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(lowercase_   ):
    os.makedirs(lowercase_   )

  lowercase__  :   Optional[Any]    		=      model.state_dict()

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

  def create_tf_var(lowercase_ , lowercase_ , lowercase_   ):
    lowercase__  :   List[str]    		=      tf.dtypes.as_dtype(tensor.dtype   )
    lowercase__  :   Tuple    		=      tf.get_variable(dtype=lowercase_ , shape=tensor.shape , name=lowercase_ , initializer=tf.zeros_initializer()   )
    session.run(tf.variables_initializer([tf_var]   )   )
    session.run(lowercase_   )
    return tf_var

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

    lowercase__  :   List[Any]    		=      tf.train.Saver(tf.trainable_variables()   )
    saver.save(lowercase_ , os.path.join(lowercase_ , model_name.replace("""-""" , """_"""   ) + """.ckpt"""   )   )

def 		UpperCamelCase	(      lowercase_=None   )		->	Union[str, Any]:


  '''simple docstring'''






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

  lowercase__  :   Any    		=      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=lowercase_ , ckpt_dir=args.tf_cache_dir , model_name=args.model_name   )


if __name__ == "__main__":
     main()






