





import logging
import os
import threading
import time


try:
     import warnings
except ImportError:
     __A     =    None

try:
     import msvcrt
except ImportError:
     __A     =    None

try:
     import fcntl
except ImportError:
     __A     =    None


# Backward compatibility
# ------------------------------------------------
try:
     TimeoutError
except NameError:
     __A     =    OSError


# Data
# ------------------------------------------------
__A     =    [
    "Timeout",
    "BaseFileLock",
    "WindowsFileLock",
    "UnixFileLock",
    "SoftFileLock",
    "FileLock",
]

__A     =    "3.0.12"


__A     =    None





def 					lowerCAmelCase_			(	)					->		int:







  """simple docstring"""





  global _logger
  lowerCamelCase__:				Any        =_logger or logging.getLogger(__name__		)
  return _logger



class        _SCREAMING_SNAKE_CASE						(     A__		):







 '''simple docstring'''

 def __init__(self    :       Dict	,			UpperCAmelCase_    :       List[str])	->Optional[Any]:



   '''simple docstring'''



   lowerCamelCase__:				Tuple        =lock_file
   return None







 def __str__(self    :       List[str])	->Dict:



   '''simple docstring'''



   lowerCamelCase__:				Union[str, Any]        =F"""The file lock '{self.lock_file}' could not be acquired."""
   return temp



class        _SCREAMING_SNAKE_CASE						:







 '''simple docstring'''

 def __init__(self    :       Any	,			UpperCAmelCase_    :       Optional[Any])	->List[Any]:



   '''simple docstring'''



   lowerCamelCase__:				int        =lock
   return None

 def __enter__(self    :       str)	->Any:



   '''simple docstring'''



   return self.lock







 def __exit__(self    :       Tuple	,			UpperCAmelCase_    :       Any	,			UpperCAmelCase_    :       Tuple	,			UpperCAmelCase_    :       Any)	->str:



   '''simple docstring'''



   self.lock.release()
   return None



class        _SCREAMING_SNAKE_CASE						:







 '''simple docstring'''

 def __init__(self    :       Optional[Any]	,			UpperCAmelCase_    :       Any	,			UpperCAmelCase_    :       Tuple=-1	,			UpperCAmelCase_    :       Optional[int]=None)	->str:



   '''simple docstring'''



   lowerCamelCase__:				Tuple        =max_filename_length if max_filename_length is not None else 255
   # Hash the filename if it's too long
   lowerCamelCase__:				Tuple        =self.hash_filename_if_too_long(__A	,			__A)
   # The path to the lock file.
   lowerCamelCase__:				int        =lock_file

   # The file descriptor for the *_lock_file* as it is returned by the
   # os.open() function.
   # This file lock is only NOT None, if the object currently holds the
   # lock.
   lowerCamelCase__:				Optional[Any]        =None

   # The default timeout value.
   lowerCamelCase__:				Tuple        =timeout

   # We use this lock primarily for the lock counter.
   lowerCamelCase__:				Optional[int]        =threading.Lock()

   # The lock counter is used for implementing the nested locking
   # mechanism. Whenever the lock is acquired, the counter is increased and
   # the lock is only released, when this value is 0 again.
   lowerCamelCase__:				str        =0
   return None

 @property
 def 							SCREAMING_SNAKE_CASE_ (self    :       Tuple)	->List[str]:



   '''simple docstring'''



   return self._lock_file

 @property
 def 							SCREAMING_SNAKE_CASE_ (self    :       Optional[Any])	->List[str]:



   '''simple docstring'''



   return self._timeout

 @timeout.setter
 def 							SCREAMING_SNAKE_CASE_ (self    :       List[Any]	,			UpperCAmelCase_    :       List[Any])	->Union[str, Any]:



   '''simple docstring'''



   lowerCamelCase__:				int        =float(__A)
   return None

 def 							SCREAMING_SNAKE_CASE_ (self    :       Optional[Any])	->Union[str, Any]:



   '''simple docstring'''



   raise NotImplementedError()

 def 							SCREAMING_SNAKE_CASE_ (self    :       Dict)	->str:



   '''simple docstring'''



   raise NotImplementedError()

 @property
 def 							SCREAMING_SNAKE_CASE_ (self    :       Any)	->str:



   '''simple docstring'''



   return self._lock_file_fd is not None

 def 							SCREAMING_SNAKE_CASE_ (self    :       str	,			UpperCAmelCase_    :       List[str]=None	,			UpperCAmelCase_    :       Optional[int]=0.05)	->Any:



   '''simple docstring'''



   if timeout is None:
     lowerCamelCase__:				Optional[Any]        =self.timeout

   # Increment the number right at the beginning.
   # We can still undo it, if something fails.
   with self._thread_lock:
     self._lock_counter += 1

   lowerCamelCase__:				str        =id(self)
   lowerCamelCase__:				Union[str, Any]        =self._lock_file
   lowerCamelCase__:				Dict        =time.time()
   try:
     while True:
       with self._thread_lock:
         if not self.is_locked:
           logger().debug(F"""Attempting to acquire lock {lock_id} on {lock_filename}""")
           self._acquire()

       if self.is_locked:
         logger().debug(F"""Lock {lock_id} acquired on {lock_filename}""")
         break
       elif timeout >= 0 and time.time() - start_time > timeout:
         logger().debug(F"""Timeout on acquiring lock {lock_id} on {lock_filename}""")
         raise Timeout(self._lock_file)
       else:
         logger().debug(
             F"""Lock {lock_id} not acquired on {lock_filename}, waiting {poll_intervall} seconds ...""")
         time.sleep(__A)
   except:  # noqa
     # Something did go wrong, so decrement the counter.
     with self._thread_lock:
       lowerCamelCase__:				Optional[Any]        =max(0	,			self._lock_counter - 1)

     raise
   return _Acquire_ReturnProxy(lock=self)

 def 							SCREAMING_SNAKE_CASE_ (self    :       Any	,			UpperCAmelCase_    :       Dict=False)	->List[Any]:



   '''simple docstring'''



   with self._thread_lock:
     if self.is_locked:
       self._lock_counter -= 1

       if self._lock_counter == 0 or force:
         lowerCamelCase__:				Optional[Any]        =id(self)
         lowerCamelCase__:				Optional[int]        =self._lock_file

         logger().debug(F"""Attempting to release lock {lock_id} on {lock_filename}""")
         self._release()
         lowerCamelCase__:				Dict        =0
         logger().debug(F"""Lock {lock_id} released on {lock_filename}""")

   return None

 def __enter__(self    :       Union[str, Any])	->Optional[int]:



   '''simple docstring'''



   self.acquire()
   return self

 def __exit__(self    :       Dict	,			UpperCAmelCase_    :       Optional[int]	,			UpperCAmelCase_    :       Tuple	,			UpperCAmelCase_    :       Tuple)	->str:



   '''simple docstring'''



   self.release()
   return None

 def __del__(self    :       List[Any])	->str:



   '''simple docstring'''



   self.release(force=__A)
   return None







 def 							SCREAMING_SNAKE_CASE_ (self    :       Union[str, Any]	,			UpperCAmelCase_    :       Dict	,			UpperCAmelCase_    :       Tuple)	->str:



   '''simple docstring'''



   lowerCamelCase__:				Any        =os.path.basename(__A)
   if len(__A) > max_length and max_length > 0:
     lowerCamelCase__:				List[Any]        =os.path.dirname(__A)
     lowerCamelCase__:				List[Any]        =str(hash(__A))
     lowerCamelCase__:				Optional[Any]        =filename[: max_length - len(__A) - 8] + """...""" + hashed_filename + """.lock"""
     return os.path.join(__A	,			__A)
   else:
     return path



class        _SCREAMING_SNAKE_CASE						(     A__		):







 '''simple docstring'''

 def __init__(self    :       str	,			UpperCAmelCase_    :       Optional[int]	,			UpperCAmelCase_    :       List[Any]=-1	,			UpperCAmelCase_    :       Dict=None)	->List[Any]:



   '''simple docstring'''



   from .file_utils import relative_to_absolute_path

   super().__init__(__A	,			timeout=__A	,			max_filename_length=__A)
   lowerCamelCase__:				Dict        ="""\\\\?\\""" + relative_to_absolute_path(self.lock_file)

 def 							SCREAMING_SNAKE_CASE_ (self    :       int)	->List[Any]:



   '''simple docstring'''



   lowerCamelCase__:				Any        =os.O_RDWR | os.O_CREAT | os.O_TRUNC

   try:
     lowerCamelCase__:				List[Any]        =os.open(self._lock_file	,			__A)
   except OSError:
     pass
   else:
     try:
       msvcrt.locking(__A	,			msvcrt.LK_NBLCK	,			1)
     except OSError:
       os.close(__A)
     else:
       lowerCamelCase__:				List[Any]        =fd
   return None







 def 							SCREAMING_SNAKE_CASE_ (self    :       Dict)	->Any:



   '''simple docstring'''



   lowerCamelCase__:				Optional[int]        =self._lock_file_fd
   lowerCamelCase__:				Any        =None
   msvcrt.locking(__A	,			msvcrt.LK_UNLCK	,			1)
   os.close(__A)

   try:
     os.remove(self._lock_file)
   # Probably another instance of the application
   # that acquired the file lock.
   except OSError:
     pass
   return None



class        _SCREAMING_SNAKE_CASE						(     A__		):







 '''simple docstring'''

 def __init__(self    :       Tuple	,			UpperCAmelCase_    :       int	,			UpperCAmelCase_    :       List[str]=-1	,			UpperCAmelCase_    :       Optional[int]=None)	->Union[str, Any]:



   '''simple docstring'''



   lowerCamelCase__:				List[Any]        =os.statvfs(os.path.dirname(__A)).f_namemax
   super().__init__(__A	,			timeout=__A	,			max_filename_length=__A)

 def 							SCREAMING_SNAKE_CASE_ (self    :       int)	->Optional[int]:



   '''simple docstring'''



   lowerCamelCase__:				Optional[int]        =os.O_RDWR | os.O_CREAT | os.O_TRUNC
   lowerCamelCase__:				str        =os.open(self._lock_file	,			__A)

   try:
     fcntl.flock(__A	,			fcntl.LOCK_EX | fcntl.LOCK_NB)
   except OSError:
     os.close(__A)
   else:
     lowerCamelCase__:				Any        =fd
   return None







 def 							SCREAMING_SNAKE_CASE_ (self    :       List[str])	->List[str]:



   '''simple docstring'''



   lowerCamelCase__:				Dict        =self._lock_file_fd
   lowerCamelCase__:				Union[str, Any]        =None
   fcntl.flock(__A	,			fcntl.LOCK_UN)
   os.close(__A)
   return None



class        _SCREAMING_SNAKE_CASE						(     A__		):







 '''simple docstring'''

 def 							SCREAMING_SNAKE_CASE_ (self    :       Optional[Any])	->Union[str, Any]:



   '''simple docstring'''



   lowerCamelCase__:				Union[str, Any]        =os.O_WRONLY | os.O_CREAT | os.O_EXCL | os.O_TRUNC
   try:
     lowerCamelCase__:				Dict        =os.open(self._lock_file	,			__A)
   except OSError:
     pass
   else:
     lowerCamelCase__:				str        =fd
   return None







 def 							SCREAMING_SNAKE_CASE_ (self    :       List[Any])	->Optional[Any]:



   '''simple docstring'''



   os.close(self._lock_file_fd)
   lowerCamelCase__:				str        =None

   try:
     os.remove(self._lock_file)
   # The file is already deleted and that's what we want.
   except OSError:
     pass
   return None



__A     =    None

if msvcrt:
     __A     =    WindowsFileLock
elif fcntl:
     __A     =    UnixFileLock
else:
     __A     =    SoftFileLock

     if warnings is not None:
          warnings.warn("only soft file lock is available")








import argparse
import os

import numpy as np
import tensorflow as tf
import torch

from transformers import BertModel





def 					lowerCAmelCase_			(	__a     ,   __a     ,   __a		)					->		List[str]:







  """simple docstring"""





  lowerCamelCase__:				int        =("dense.weight", "attention.self.query", "attention.self.key", "attention.self.value")

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

  lowerCamelCase__:				Tuple        =model.state_dict()

  def to_tf_var_name(__a		):
    for patt, repl in iter(__a		):
      lowerCamelCase__:				Tuple        =name.replace(__a     ,   __a		)
    return F"""bert/{name}"""

  def create_tf_var(__a     ,   __a     ,   __a		):
    lowerCamelCase__:				Union[str, Any]        =tf.dtypes.as_dtype(tensor.dtype		)
    lowerCamelCase__:				Any        =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:
      lowerCamelCase__:				Tuple        =to_tf_var_name(__a		)
      lowerCamelCase__:				str        =state_dict[var_name].numpy()
      if any(x in var_name for x in tensors_to_transpose		):
        lowerCamelCase__:				List[Any]        =torch_tensor.T
      lowerCamelCase__:				Optional[int]        =create_tf_var(tensor=__a     ,   name=__a     ,   session=__a		)
      tf.keras.backend.set_value(__a     ,   __a		)
      lowerCamelCase__:				Union[str, Any]        =session.run(__a		)
      print(F"""Successfully created {tf_name}: {np.allclose(__a     ,   __a		)}"""		)

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





def 					lowerCAmelCase_			(	__a=None		)					->		Tuple:







  """simple docstring"""





  lowerCamelCase__:				List[str]        =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"		)
  lowerCamelCase__:				Optional[Any]        =parser.parse_args(__a		)

  lowerCamelCase__:				Dict        =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()

