





'''simple docstring'''





import argparse
import os

import numpy as np
import tensorflow as tf
import torch

from transformers import BertModel





def     _UpperCamelCase     (a__   :Tuple   ,			a__   :Union[str, Any]   ,			a__   :List[str]       ):






							"""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(lowerCAmelCase_       ):
														os.makedirs(lowerCAmelCase_       )

							UpperCamelCase__				     =			model.state_dict()

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

							def create_tf_var(a__   :Optional[int]   ,			a__   :Tuple   ,			a__   :str       ):
														UpperCamelCase__				     =			tf.dtypes.as_dtype(tensor.dtype       )
														UpperCamelCase__				     =			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__				     =			to_tf_var_name(lowerCAmelCase_       )
																					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=lowerCAmelCase_   ,			name=lowerCAmelCase_   ,			session=lowerCAmelCase_       )
																					tf.keras.backend.set_value(lowerCAmelCase_   ,			lowerCAmelCase_       )
																					UpperCamelCase__				     =			session.run(lowerCAmelCase_       )
																					print(f"""Successfully created {tf_name}: {np.allclose(lowerCAmelCase_   ,			lowerCAmelCase_       )}"""       )

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





def     _UpperCamelCase     (a__   :List[str]=None       ):






							"""simple docstring"""





							UpperCamelCase__				     =			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__				     =			parser.parse_args(lowerCAmelCase_       )

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


if __name__ == "__main__":
					main()











import math

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


UpperCamelCase__											= logging.get_logger(__name__)

UpperCamelCase__											= {
    "facebook/data2vec-base-960h": "https://huggingface.co/facebook/data2vec-audio-base-960h/resolve/main/config.json",
    # See all Data2VecAudio models at https://huggingface.co/models?filter=data2vec-audio
}

class        __SCREAMING_SNAKE_CASE					(     _a       ):
							snake_case     :      Optional[Any]						     =       """data2vec-audio"""






							def __init__(						self  , __lowerCAmelCase=32  , __lowerCAmelCase=768  , __lowerCAmelCase=12  , __lowerCAmelCase=12  , __lowerCAmelCase=3072  , __lowerCAmelCase="gelu"  , __lowerCAmelCase=0.1  , __lowerCAmelCase=0.1  , __lowerCAmelCase=0.1  , __lowerCAmelCase=0.0  , __lowerCAmelCase=0.1  , __lowerCAmelCase=0.1  , __lowerCAmelCase=0.02  , __lowerCAmelCase=1E-5  , __lowerCAmelCase="gelu"  , __lowerCAmelCase=(512, 512, 512, 512, 512, 512, 512)  , __lowerCAmelCase=(5, 2, 2, 2, 2, 2, 2)  , __lowerCAmelCase=(10, 3, 3, 3, 3, 2, 2)  , __lowerCAmelCase=False  , __lowerCAmelCase=16  , __lowerCAmelCase=19  , __lowerCAmelCase=5  , __lowerCAmelCase=0.05  , __lowerCAmelCase=10  , __lowerCAmelCase=2  , __lowerCAmelCase=0.0  , __lowerCAmelCase=10  , __lowerCAmelCase=0  , __lowerCAmelCase="sum"  , __lowerCAmelCase=False  , __lowerCAmelCase=False  , __lowerCAmelCase=256  , __lowerCAmelCase=(512, 512, 512, 512, 1500)  , __lowerCAmelCase=(5, 3, 3, 1, 1)  , __lowerCAmelCase=(1, 2, 3, 1, 1)  , __lowerCAmelCase=512  , __lowerCAmelCase=0  , __lowerCAmelCase=1  , __lowerCAmelCase=2  , __lowerCAmelCase=False  , __lowerCAmelCase=3  , __lowerCAmelCase=2  , __lowerCAmelCase=3  , __lowerCAmelCase=None  , **__lowerCAmelCase  , ):
														super().__init__(**__lowerCAmelCase  , pad_token_id=__lowerCAmelCase  , bos_token_id=__lowerCAmelCase  , eos_token_id=__lowerCAmelCase  )
														UpperCamelCase__				     =			hidden_size
														UpperCamelCase__				     =			feat_extract_activation
														UpperCamelCase__				     =			list(__lowerCAmelCase  )
														UpperCamelCase__				     =			list(__lowerCAmelCase  )
														UpperCamelCase__				     =			list(__lowerCAmelCase  )
														UpperCamelCase__				     =			conv_bias
														UpperCamelCase__				     =			num_conv_pos_embeddings
														UpperCamelCase__				     =			num_conv_pos_embedding_groups
														UpperCamelCase__				     =			conv_pos_kernel_size
														UpperCamelCase__				     =			len(self.conv_dim  )
														UpperCamelCase__				     =			num_hidden_layers
														UpperCamelCase__				     =			intermediate_size
														UpperCamelCase__				     =			hidden_act
														UpperCamelCase__				     =			num_attention_heads
														UpperCamelCase__				     =			hidden_dropout
														UpperCamelCase__				     =			attention_dropout
														UpperCamelCase__				     =			activation_dropout
														UpperCamelCase__				     =			feat_proj_dropout
														UpperCamelCase__				     =			final_dropout
														UpperCamelCase__				     =			layerdrop
														UpperCamelCase__				     =			layer_norm_eps
														UpperCamelCase__				     =			initializer_range
														UpperCamelCase__				     =			vocab_size
														UpperCamelCase__				     =			use_weighted_layer_sum

														if (
														    (len(self.conv_stride  ) != self.num_feat_extract_layers)
														    or (len(self.conv_kernel  ) != self.num_feat_extract_layers)
														    or (len(self.conv_dim  ) != self.num_feat_extract_layers)
														):
																					raise ValueError(
																					    """Configuration for convolutional layers is incorrect. It is required that `len(config.conv_dim)` =="""
																					    """ `len(config.conv_stride)` == `len(config.conv_kernel)`, but is `len(config.conv_dim) ="""
																					    f""" {len(self.conv_dim  )}`, `len(config.conv_stride) = {len(self.conv_stride  )}`,"""
																					    f""" `len(config.conv_kernel) = {len(self.conv_kernel  )}`."""  )

														# fine-tuning config parameters for SpecAugment: https://arxiv.org/abs/1904.08779
														UpperCamelCase__				     =			mask_time_prob
														UpperCamelCase__				     =			mask_time_length
														UpperCamelCase__				     =			mask_time_min_masks
														UpperCamelCase__				     =			mask_feature_prob
														UpperCamelCase__				     =			mask_feature_length
														UpperCamelCase__				     =			mask_feature_min_masks

														# ctc loss
														UpperCamelCase__				     =			ctc_loss_reduction
														UpperCamelCase__				     =			ctc_zero_infinity

														# adapter
														UpperCamelCase__				     =			add_adapter
														UpperCamelCase__				     =			adapter_kernel_size
														UpperCamelCase__				     =			adapter_stride
														UpperCamelCase__				     =			num_adapter_layers
														UpperCamelCase__				     =			output_hidden_size or hidden_size

														# SequenceClassification-specific parameter. Feel free to ignore for other classes.
														UpperCamelCase__				     =			classifier_proj_size

														# XVector-specific parameters. Feel free to ignore for other classes.
														UpperCamelCase__				     =			list(__lowerCAmelCase  )
														UpperCamelCase__				     =			list(__lowerCAmelCase  )
														UpperCamelCase__				     =			list(__lowerCAmelCase  )
														UpperCamelCase__				     =			xvector_output_dim







							@property
							def 				_lowerCamelCase		(						self  ):
														return math.prod(self.conv_stride  )




