


import argparse
import os

import numpy as np
import tensorflow as tf
import torch

from transformers import BertModel


def 				lowerCamelCase__	(      a	:   BertModel	,  a	:   str	,  a	:   str				)    ->	List[Any]:





			"""simple docstring"""
			a__ :int						  =    ("dense.weight", "attention.self.query", "attention.self.key", "attention.self.value")

			a__ :str						  =    (
			    ("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(_SCREAMING_SNAKE_CASE				):
						os.makedirs(_SCREAMING_SNAKE_CASE				)

			a__ :Dict						  =    model.state_dict()

			def to_tf_var_name(a	:   str				):
						for patt, repl in iter(_SCREAMING_SNAKE_CASE				):
									a__ :Any						  =    name.replace(_SCREAMING_SNAKE_CASE	,  _SCREAMING_SNAKE_CASE				)
						return F'''bert/{name}'''

			def create_tf_var(a	:   np.ndarray	,  a	:   str	,  a	:   tf.Session				):
						a__ :List[str]						  =    tf.dtypes.as_dtype(tensor.dtype				)
						a__ :str						  =    tf.get_variable(dtype=_SCREAMING_SNAKE_CASE	,  shape=tensor.shape	,  name=_SCREAMING_SNAKE_CASE	,  initializer=tf.zeros_initializer()				)
						session.run(tf.variables_initializer([tf_var]				)				)
						session.run(_SCREAMING_SNAKE_CASE				)
						return tf_var

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

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


def 				lowerCamelCase__	(      a	:   List[Any]=None				)    ->	Optional[int]:





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

			a__ :int						  =    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=_SCREAMING_SNAKE_CASE	,  ckpt_dir=args.tf_cache_dir	,  model_name=args.model_name				)


if __name__ == "__main__":
		main()






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


snake_case__			 =						logging.get_logger(__name__)

snake_case__			 =						{
    '''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 					lowerCAmelCase_ (       _a):
						lowerCamelCase_         =     'speech_to_text_2'
						lowerCamelCase_         =     ['past_key_values']
						lowerCamelCase_         =     {'num_attention_heads': 'decoder_attention_heads', 'hidden_size': 'd_model'}




						def __init__(	self						:   Dict   ,     __A						:   Union[str, Any]=10000   ,     __A						:   Tuple=6   ,     __A						:   Tuple=2048   ,     __A						:   Any=4   ,     __A						:   str=0.0   ,     __A						:   Tuple=True   ,     __A						:   List[Any]="relu"   ,     __A						:   Union[str, Any]=256   ,     __A						:   str=0.1   ,     __A						:   Optional[Any]=0.0   ,     __A						:   Tuple=0.0   ,     __A						:   Any=0.02   ,     __A						:   int=2   ,     __A						:   Union[str, Any]=True   ,     __A						:   Tuple=1   ,     __A						:   List[str]=0   ,     __A						:   List[str]=2   ,     __A						:   Union[str, Any]=1024   ,     **__A						:   Tuple   ,     )      ->List[str]:






									"""simple docstring"""






									a__ :Tuple						  =    vocab_size
									a__ :Dict						  =    d_model
									a__ :Optional[int]						  =    decoder_ffn_dim
									a__ :Optional[int]						  =    decoder_layers
									a__ :Optional[int]						  =    decoder_attention_heads
									a__ :Dict						  =    dropout
									a__ :Optional[Any]						  =    attention_dropout
									a__ :Any						  =    activation_dropout
									a__ :int						  =    activation_function
									a__ :Optional[int]						  =    init_std
									a__ :List[Any]						  =    decoder_layerdrop
									a__ :Optional[Any]						  =    use_cache
									a__ :List[str]						  =    decoder_layers
									a__ :int						  =    scale_embedding  # scale factor will be sqrt(d_model) if True
									a__ :Optional[int]						  =    max_target_positions

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


