





import argparse
import os

import numpy as np
import tensorflow as tf
import torch

from transformers import BertModel





def       _UpperCamelCase       (a__				:BertModel				,     a__				:str				,     a__				:str    ) ->    Tuple:






							"""simple docstring"""





							UpperCamelCase__			       =     ("""dense.weight""", """attention.self.query""", """attention.self.key""", """attention.self.value""")

							UpperCamelCase__			       =     (
							    ("""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__    )

							UpperCamelCase__			       =     model.state_dict()

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

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

														UpperCamelCase__			       =     tf.train.Saver(tf.trainable_variables()    )
														saver.save(a__				,     os.path.join(a__				,     model_name.replace("""-"""				,     """_"""    ) + """.ckpt"""    )    )





def       _UpperCamelCase       (a__				:int=None    ) ->    Dict:






							"""simple docstring"""





							UpperCamelCase__			       =     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"""    )
							UpperCamelCase__			       =     parser.parse_args(a__    )

							UpperCamelCase__			       =     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 ...configuration_utils import PretrainedConfig
from ...utils import logging


UpperCamelCase__     =	logging.get_logger(__name__)

UpperCamelCase__     =	{
    "facebook/s2t-wav2vec2-large-en-de": (
        "https://huggingface.co/facebook/s2t-wav2vec2-large-en-de/resolve/main/config.json"
    ),
    # See all Speech2Text models at https://huggingface.co/models?filter=speech2text2
}

class     __SCREAMING_SNAKE_CASE   (       _a    ):
							snake_case       : Dict         =    """speech_to_text_2"""
							snake_case       : List[Any]         =    ["""past_key_values"""]
							snake_case       : List[str]         =    {"""num_attention_heads""": """decoder_attention_heads""", """hidden_size""": """d_model"""}






							def __init__(  self					,	__lowerCAmelCase=10000					,	__lowerCAmelCase=6					,	__lowerCAmelCase=2048					,	__lowerCAmelCase=4					,	__lowerCAmelCase=0.0					,	__lowerCAmelCase=True					,	__lowerCAmelCase="relu"					,	__lowerCAmelCase=256					,	__lowerCAmelCase=0.1					,	__lowerCAmelCase=0.0					,	__lowerCAmelCase=0.0					,	__lowerCAmelCase=0.02					,	__lowerCAmelCase=2					,	__lowerCAmelCase=True					,	__lowerCAmelCase=1					,	__lowerCAmelCase=0					,	__lowerCAmelCase=2					,	__lowerCAmelCase=1024					,	**__lowerCAmelCase					,	):
														UpperCamelCase__			       =     vocab_size
														UpperCamelCase__			       =     d_model
														UpperCamelCase__			       =     decoder_ffn_dim
														UpperCamelCase__			       =     decoder_layers
														UpperCamelCase__			       =     decoder_attention_heads
														UpperCamelCase__			       =     dropout
														UpperCamelCase__			       =     attention_dropout
														UpperCamelCase__			       =     activation_dropout
														UpperCamelCase__			       =     activation_function
														UpperCamelCase__			       =     init_std
														UpperCamelCase__			       =     decoder_layerdrop
														UpperCamelCase__			       =     use_cache
														UpperCamelCase__			       =     decoder_layers
														UpperCamelCase__			       =     scale_embedding  # scale factor will be sqrt(d_model) if True
														UpperCamelCase__			       =     max_target_positions

														super().__init__(
														    pad_token_id=__lowerCAmelCase					,	bos_token_id=__lowerCAmelCase					,	eos_token_id=__lowerCAmelCase					,	decoder_start_token_id=__lowerCAmelCase					,	**__lowerCAmelCase					,	)




