



"""simple docstring"""






import functools
import operator

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


lowerCAmelCase__        =				logging.get_logger(__name__)

lowerCAmelCase__        =				{
    '''microsoft/unispeech-large-1500h-cv''': (
        '''https://huggingface.co/microsoft/unispeech-large-1500h-cv/resolve/main/config.json'''
    ),
    # See all UniSpeech models at https://huggingface.co/models?filter=unispeech
}






class        __snake_case       (	_lowercase):
  snake_case__				:   List[str]  			=						"unispeech"




  def __init__( self     :						List[str]						,					__lowerCAmelCase     :						List[Any]=3_2						,					__lowerCAmelCase     :						str=7_6_8						,					__lowerCAmelCase     :						int=1_2						,					__lowerCAmelCase     :						int=1_2						,					__lowerCAmelCase     :						int=3_0_7_2						,					__lowerCAmelCase     :						Tuple="gelu"						,					__lowerCAmelCase     :						Dict=0.1						,					__lowerCAmelCase     :						Dict=0.1						,					__lowerCAmelCase     :						str=0.1						,					__lowerCAmelCase     :						Tuple=0.0						,					__lowerCAmelCase     :						Optional[int]=0.0						,					__lowerCAmelCase     :						List[Any]=0.1						,					__lowerCAmelCase     :						Optional[int]=0.1						,					__lowerCAmelCase     :						Tuple=0.02						,					__lowerCAmelCase     :						Dict=1E-5						,					__lowerCAmelCase     :						Optional[int]="group"						,					__lowerCAmelCase     :						Dict="gelu"						,					__lowerCAmelCase     :						int=(5_1_2, 5_1_2, 5_1_2, 5_1_2, 5_1_2, 5_1_2, 5_1_2)						,					__lowerCAmelCase     :						Optional[int]=(5, 2, 2, 2, 2, 2, 2)						,					__lowerCAmelCase     :						Union[str, Any]=(1_0, 3, 3, 3, 3, 2, 2)						,					__lowerCAmelCase     :						List[Any]=False						,					__lowerCAmelCase     :						List[str]=1_2_8						,					__lowerCAmelCase     :						Any=1_6						,					__lowerCAmelCase     :						Optional[int]=False						,					__lowerCAmelCase     :						Optional[Any]=True						,					__lowerCAmelCase     :						Union[str, Any]=0.05						,					__lowerCAmelCase     :						Union[str, Any]=1_0						,					__lowerCAmelCase     :						List[Any]=2						,					__lowerCAmelCase     :						Dict=0.0						,					__lowerCAmelCase     :						Optional[int]=1_0						,					__lowerCAmelCase     :						Dict=0						,					__lowerCAmelCase     :						List[str]=3_2_0						,					__lowerCAmelCase     :						List[Any]=2						,					__lowerCAmelCase     :						Dict=0.1						,					__lowerCAmelCase     :						Tuple=1_0_0						,					__lowerCAmelCase     :						Dict=2_5_6						,					__lowerCAmelCase     :						str=2_5_6						,					__lowerCAmelCase     :						List[Any]=0.1						,					__lowerCAmelCase     :						Dict="mean"						,					__lowerCAmelCase     :						Union[str, Any]=False						,					__lowerCAmelCase     :						Dict=False						,					__lowerCAmelCase     :						Optional[Any]=2_5_6						,					__lowerCAmelCase     :						Dict=8_0						,					__lowerCAmelCase     :						int=0						,					__lowerCAmelCase     :						Optional[int]=1						,					__lowerCAmelCase     :						Dict=2						,					__lowerCAmelCase     :						Any=0.5						,					**__lowerCAmelCase     :						Optional[Any]						,					):






         """simple docstring"""






         super().__init__(**__lowerCAmelCase						,					pad_token_id=__lowerCAmelCase						,					bos_token_id=__lowerCAmelCase						,					eos_token_id=__lowerCAmelCase		)
         _lowerCamelCase						:				Dict    			= hidden_size
         _lowerCamelCase						:				Any    			= feat_extract_norm
         _lowerCamelCase						:				List[Any]    			= feat_extract_activation
         _lowerCamelCase						:				Any    			= list(__lowerCAmelCase		)
         _lowerCamelCase						:				Tuple    			= list(__lowerCAmelCase		)
         _lowerCamelCase						:				int    			= list(__lowerCAmelCase		)
         _lowerCamelCase						:				List[str]    			= conv_bias
         _lowerCamelCase						:				List[str]    			= num_conv_pos_embeddings
         _lowerCamelCase						:				Tuple    			= num_conv_pos_embedding_groups
         _lowerCamelCase						:				List[str]    			= len(self.conv_dim		)
         _lowerCamelCase						:				Tuple    			= num_hidden_layers
         _lowerCamelCase						:				List[Any]    			= intermediate_size
         _lowerCamelCase						:				Dict    			= hidden_act
         _lowerCamelCase						:				Union[str, Any]    			= num_attention_heads
         _lowerCamelCase						:				Tuple    			= hidden_dropout
         _lowerCamelCase						:				List[Any]    			= attention_dropout
         _lowerCamelCase						:				Optional[int]    			= activation_dropout
         _lowerCamelCase						:				Optional[Any]    			= feat_proj_dropout
         _lowerCamelCase						:				Optional[int]    			= final_dropout
         _lowerCamelCase						:				Any    			= layerdrop
         _lowerCamelCase						:				Any    			= layer_norm_eps
         _lowerCamelCase						:				List[Any]    			= initializer_range
         _lowerCamelCase						:				List[str]    			= num_ctc_classes
         _lowerCamelCase						:				List[Any]    			= vocab_size
         _lowerCamelCase						:				Optional[Any]    			= do_stable_layer_norm
         _lowerCamelCase						:				Tuple    			= use_weighted_layer_sum
         _lowerCamelCase						:				List[Any]    			= classifier_proj_size

         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
         _lowerCamelCase						:				Any    			= apply_spec_augment
         _lowerCamelCase						:				Dict    			= mask_time_prob
         _lowerCamelCase						:				List[str]    			= mask_time_length
         _lowerCamelCase						:				Optional[Any]    			= mask_time_min_masks
         _lowerCamelCase						:				List[str]    			= mask_feature_prob
         _lowerCamelCase						:				int    			= mask_feature_length
         _lowerCamelCase						:				Dict    			= mask_feature_min_masks

         # parameters for pretraining with codevector quantized representations
         _lowerCamelCase						:				Optional[Any]    			= num_codevectors_per_group
         _lowerCamelCase						:				int    			= num_codevector_groups
         _lowerCamelCase						:				List[Any]    			= contrastive_logits_temperature
         _lowerCamelCase						:				List[str]    			= feat_quantizer_dropout
         _lowerCamelCase						:				Dict    			= num_negatives
         _lowerCamelCase						:				Optional[int]    			= codevector_dim
         _lowerCamelCase						:				List[Any]    			= proj_codevector_dim
         _lowerCamelCase						:				List[Any]    			= diversity_loss_weight

         # ctc loss
         _lowerCamelCase						:				Union[str, Any]    			= ctc_loss_reduction
         _lowerCamelCase						:				Any    			= ctc_zero_infinity

         # pretraining loss
         _lowerCamelCase						:				str    			= replace_prob




  @property
  def 						SCREAMING_SNAKE_CASE				( self     :						int		):






         """simple docstring"""






         return functools.reduce(operator.mul						,					self.conv_stride						,					1		)





import re

import jax.numpy as jnp
from flax.traverse_util import flatten_dict, unflatten_dict
from jax.random import PRNGKey

from ..utils import logging


a__				=		logging.get_logger(__name__)



def    __UpperCAmelCase (	__a      :			Dict	)			->     Tuple:

					"""simple docstring"""

					_a	:    Any         =		R'''\w+[.]\d+'''
					_a	:    Union[str, Any]         =		re.findall(__a		,__a	)
					for pat in pats:
										_a	:    int         =		key.replace(__a		,'''_'''.join(pat.split('''.'''	)	)	)
					return key



def    __UpperCAmelCase (	__a      :			List[str]		,__a      :			Union[str, Any]		,__a      :			Optional[int]	)			->     Tuple:

					"""simple docstring"""

					_a	:    Dict         =		pt_tuple_key[:-1] + ('''scale''',)
					if (
					    any('''norm''' in str_ for str_ in pt_tuple_key	)
					    and (pt_tuple_key[-1] == "bias")
					    and (pt_tuple_key[:-1] + ("bias",) not in random_flax_state_dict)
					    and (pt_tuple_key[:-1] + ("scale",) in random_flax_state_dict)
					):
										_a	:    Dict         =		pt_tuple_key[:-1] + ('''scale''',)
										return renamed_pt_tuple_key, pt_tensor
					elif pt_tuple_key[-1] in ["weight", "gamma"] and pt_tuple_key[:-1] + ("scale",) in random_flax_state_dict:
										_a	:    Optional[int]         =		pt_tuple_key[:-1] + ('''scale''',)
										return renamed_pt_tuple_key, pt_tensor

					# embedding
					if pt_tuple_key[-1] == "weight" and pt_tuple_key[:-1] + ("embedding",) in random_flax_state_dict:
										_a	:    Union[str, Any]         =		pt_tuple_key[:-1] + ('''embedding''',)
										return renamed_pt_tuple_key, pt_tensor

					# conv layer
					_a	:    Tuple         =		pt_tuple_key[:-1] + ('''kernel''',)
					if pt_tuple_key[-1] == "weight" and pt_tensor.ndim == 4:
										_a	:    List[str]         =		pt_tensor.transpose(2		,3		,1		,0	)
										return renamed_pt_tuple_key, pt_tensor

					# linear layer
					_a	:    Dict         =		pt_tuple_key[:-1] + ('''kernel''',)
					if pt_tuple_key[-1] == "weight":
										_a	:    Union[str, Any]         =		pt_tensor.T
										return renamed_pt_tuple_key, pt_tensor

					# old PyTorch layer norm weight
					_a	:    Dict         =		pt_tuple_key[:-1] + ('''weight''',)
					if pt_tuple_key[-1] == "gamma":
										return renamed_pt_tuple_key, pt_tensor

					# old PyTorch layer norm bias
					_a	:    Union[str, Any]         =		pt_tuple_key[:-1] + ('''bias''',)
					if pt_tuple_key[-1] == "beta":
										return renamed_pt_tuple_key, pt_tensor

					return pt_tuple_key, pt_tensor







def    __UpperCAmelCase (	__a      :			Dict		,__a      :			str		,__a      :			str=42	)			->     Optional[int]:

					"""simple docstring"""

					_a	:    Tuple         =		{k: v.numpy() for k, v in pt_state_dict.items()}

					# Step 2: Since the model is stateless, get random Flax params
					_a	:    List[Any]         =		flax_model.init_weights(PRNGKey(__a	)	)

					_a	:    Optional[int]         =		flatten_dict(__a	)
					_a	:    Tuple         =		{}

					# Need to change some parameters name to match Flax names
					for pt_key, pt_tensor in pt_state_dict.items():
										_a	:    List[str]         =		rename_key(__a	)
										_a	:    Optional[Any]         =		tuple(renamed_pt_key.split('''.'''	)	)

										# Correctly rename weight parameters
										_a      ,					_a	:    List[str]         =		rename_key_and_reshape_tensor(__a		,__a		,__a	)

										if flax_key in random_flax_state_dict:
															if flax_tensor.shape != random_flax_state_dict[flax_key].shape:
																				raise ValueError(
																				    F"""PyTorch checkpoint seems to be incorrect. Weight {pt_key} was expected to be of shape """
																				    F"""{random_flax_state_dict[flax_key].shape}, but is {flax_tensor.shape}."""	)

        # also add unexpected weight so that warning is thrown
										_a	:    Dict         =		jnp.asarray(__a	)

					return unflatten_dict(__a	)



