



'''simple docstring'''






import argparse
import os

import numpy as np
import tensorflow as tf
import torch

from transformers import BertModel


def 				snake_case_  (      _lowerCAmelCase						:		BertModel     ,   _lowerCAmelCase						:		str     ,   _lowerCAmelCase						:		str						)   ->     Union[str, Any]:
							UpperCAmelCase				: int											=	('''dense.weight''', '''attention.self.query''', '''attention.self.key''', '''attention.self.value''')

							UpperCAmelCase				: Optional[int]											=	(
							    ('''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(_lowerCAmelCase						):
														os.makedirs(_lowerCAmelCase						)

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

							def to_tf_var_name(_lowerCAmelCase						:		str						):
														for patt, repl in iter(_lowerCAmelCase						):
																					UpperCAmelCase				: List[str]											=	name.replace(_lowerCAmelCase     ,   _lowerCAmelCase						)
														return f"""bert/{name}"""

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

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

														UpperCAmelCase				: List[str]											=	tf.train.Saver(tf.trainable_variables()						)
														saver.save(_lowerCAmelCase     ,   os.path.join(_lowerCAmelCase     ,   model_name.replace('''-'''     ,   '''_'''						) + '''.ckpt'''						)						)


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

							UpperCAmelCase				: Tuple											=	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=_lowerCAmelCase     ,   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__:     Dict										=    logging.get_logger(__name__)

UpperCamelCase__:     int										=    {
    # See all MEGATRON_BERT models at https://huggingface.co/models?filter=bert
}

class        SCREAMING_SNAKE_CASE(							A__    ):







	"""simple docstring"""






	lowerCamelCase__    				= """megatron-bert"""







	def __init__(		self    :  int	,    __snake_case    :  List[Any]=29056	,    __snake_case    :  Dict=1024	,    __snake_case    :  Tuple=24	,    __snake_case    :  Any=16	,    __snake_case    :  Tuple=4096	,    __snake_case    :  int="gelu"	,    __snake_case    :  Dict=0.1	,    __snake_case    :  str=0.1	,    __snake_case    :  Optional[Any]=512	,    __snake_case    :  Tuple=2	,    __snake_case    :  Tuple=0.02	,    __snake_case    :  int=1E-12	,    __snake_case    :  Union[str, Any]=0	,    __snake_case    :  int="absolute"	,    __snake_case    :  str=True	,    **__snake_case    :  Tuple	,    )    ->	Union[str, Any]:
								super().__init__(pad_token_id=__snake_case	,    **__snake_case	)

								UpperCAmelCase				: Any											=	vocab_size
								UpperCAmelCase				: Tuple											=	hidden_size
								UpperCAmelCase				: List[Any]											=	num_hidden_layers
								UpperCAmelCase				: str											=	num_attention_heads
								UpperCAmelCase				: Optional[int]											=	hidden_act
								UpperCAmelCase				: Optional[int]											=	intermediate_size
								UpperCAmelCase				: Dict											=	hidden_dropout_prob
								UpperCAmelCase				: List[Any]											=	attention_probs_dropout_prob
								UpperCAmelCase				: Dict											=	max_position_embeddings
								UpperCAmelCase				: Any											=	type_vocab_size
								UpperCAmelCase				: str											=	initializer_range
								UpperCAmelCase				: str											=	layer_norm_eps
								UpperCAmelCase				: Optional[Any]											=	position_embedding_type
								UpperCAmelCase				: str											=	use_cache





