

"""simple docstring"""




import argparse
import os

import numpy as np
import tensorflow as tf
import torch

from transformers import BertModel
def   lowerCAmelCase						(					__UpperCamelCase							,    __UpperCamelCase							,    __UpperCamelCase      ):
 '''simple docstring'''





 UpperCAmelCase__					:  Optional[Any]					   =					("""dense.weight""", """attention.self.query""", """attention.self.key""", """attention.self.value""")

 UpperCAmelCase__					:  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(__UpperCamelCase      ):
  os.makedirs(__UpperCamelCase      )

 UpperCAmelCase__					:  Optional[int]					   =					model.state_dict()

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

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

  UpperCAmelCase__					:  Optional[int]					   =					tf.train.Saver(tf.trainable_variables()      )
  saver.save(__UpperCamelCase							,    os.path.join(__UpperCamelCase							,    model_name.replace("""-"""							,    """_"""      ) + """.ckpt"""      )      )
def   lowerCAmelCase						(					__UpperCamelCase=None      ):
 '''simple docstring'''





 UpperCAmelCase__					:  Tuple					   =					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"""      )
 UpperCAmelCase__					:  int					   =					parser.parse_args(__UpperCamelCase      )

 UpperCAmelCase__					:  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=__UpperCamelCase							,    ckpt_dir=args.tf_cache_dir							,    model_name=args.model_name      )


if __name__ == "__main__":
      main()








"""simple docstring"""




from ...configuration_utils import PretrainedConfig
from ...utils import logging


__UpperCAmelCase =				logging.get_logger(__name__)

__UpperCAmelCase =				{
    'microsoft/markuplm-base': 'https://huggingface.co/microsoft/markuplm-base/resolve/main/config.json',
    'microsoft/markuplm-large': 'https://huggingface.co/microsoft/markuplm-large/resolve/main/config.json',
}







class      __lowercase			(    __lowerCamelCase					):
  snake_case_				       =       """markuplm"""

  def __init__(  self		:			str					,A		:			List[Any]=30_522					,A		:			Tuple=768					,A		:			str=12					,A		:			int=12					,A		:			int=3_072					,A		:			Optional[int]="gelu"					,A		:			Optional[int]=0.1					,A		:			Optional[int]=0.1					,A		:			Any=512					,A		:			Any=2					,A		:			str=0.0_2					,A		:			int=1e-12					,A		:			int=0					,A		:			str=0					,A		:			List[Any]=2					,A		:			List[str]=256					,A		:			Union[str, Any]=1_024					,A		:			List[Any]=216					,A		:			Any=1_001					,A		:			Optional[int]=32					,A		:			Any=50					,A		:			int="absolute"					,A		:			Dict=True					,A		:			int=None					,**A		:			Optional[int]					,):






   '''simple docstring'''
   super().__init__(
       pad_token_id=A					,bos_token_id=A					,eos_token_id=A					,**A					,)
   UpperCAmelCase__					:  Dict					   =					vocab_size
   UpperCAmelCase__					:  Union[str, Any]					   =					hidden_size
   UpperCAmelCase__					:  Optional[int]					   =					num_hidden_layers
   UpperCAmelCase__					:  Tuple					   =					num_attention_heads
   UpperCAmelCase__					:  Optional[Any]					   =					hidden_act
   UpperCAmelCase__					:  Any					   =					intermediate_size
   UpperCAmelCase__					:  Dict					   =					hidden_dropout_prob
   UpperCAmelCase__					:  Optional[Any]					   =					attention_probs_dropout_prob
   UpperCAmelCase__					:  Optional[int]					   =					max_position_embeddings
   UpperCAmelCase__					:  Dict					   =					type_vocab_size
   UpperCAmelCase__					:  Optional[Any]					   =					initializer_range
   UpperCAmelCase__					:  str					   =					layer_norm_eps
   UpperCAmelCase__					:  Any					   =					position_embedding_type
   UpperCAmelCase__					:  int					   =					use_cache
   UpperCAmelCase__					:  List[str]					   =					classifier_dropout
   # additional properties
   UpperCAmelCase__					:  Optional[int]					   =					max_depth
   UpperCAmelCase__					:  List[str]					   =					max_xpath_tag_unit_embeddings
   UpperCAmelCase__					:  Any					   =					max_xpath_subs_unit_embeddings
   UpperCAmelCase__					:  str					   =					tag_pad_id
   UpperCAmelCase__					:  Dict					   =					subs_pad_id
   UpperCAmelCase__					:  List[str]					   =					xpath_unit_hidden_size





