






import functools
import operator

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


SCREAMING_SNAKE_CASE__		:							Dict 				=				logging.get_logger(__name__)

SCREAMING_SNAKE_CASE__		:							int 				=				{
    """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 				UpperCAmelCase_  (		__lowerCamelCase		):
     __lowerCamelCase      				=  'unispeech'




     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.0	,						_lowerCAmelCase=0.1	,						_lowerCAmelCase=0.1	,						_lowerCAmelCase=0.0_2	,						_lowerCAmelCase=1e-5	,						_lowerCAmelCase="group"	,						_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=128	,						_lowerCAmelCase=16	,						_lowerCAmelCase=False	,						_lowerCAmelCase=True	,						_lowerCAmelCase=0.0_5	,						_lowerCAmelCase=10	,						_lowerCAmelCase=2	,						_lowerCAmelCase=0.0	,						_lowerCAmelCase=10	,						_lowerCAmelCase=0	,						_lowerCAmelCase=320	,						_lowerCAmelCase=2	,						_lowerCAmelCase=0.1	,						_lowerCAmelCase=100	,						_lowerCAmelCase=256	,						_lowerCAmelCase=256	,						_lowerCAmelCase=0.1	,						_lowerCAmelCase="mean"	,						_lowerCAmelCase=False	,						_lowerCAmelCase=False	,						_lowerCAmelCase=256	,						_lowerCAmelCase=80	,						_lowerCAmelCase=0	,						_lowerCAmelCase=1	,						_lowerCAmelCase=2	,						_lowerCAmelCase=0.5	,						**_lowerCAmelCase	,						):
          super().__init__(**_lowerCAmelCase	,						pad_token_id=_lowerCAmelCase	,						bos_token_id=_lowerCAmelCase	,						eos_token_id=_lowerCAmelCase		)
          UpperCAmelCase__      :  Optional[Any]				      =			hidden_size
          UpperCAmelCase__      :  Optional[Any]				      =			feat_extract_norm
          UpperCAmelCase__      :  Any				      =			feat_extract_activation
          UpperCAmelCase__      :  List[Any]				      =			list(_lowerCAmelCase		)
          UpperCAmelCase__      :  Union[str, Any]				      =			list(_lowerCAmelCase		)
          UpperCAmelCase__      :  Tuple				      =			list(_lowerCAmelCase		)
          UpperCAmelCase__      :  Union[str, Any]				      =			conv_bias
          UpperCAmelCase__      :  Optional[int]				      =			num_conv_pos_embeddings
          UpperCAmelCase__      :  int				      =			num_conv_pos_embedding_groups
          UpperCAmelCase__      :  Tuple				      =			len(self.conv_dim		)
          UpperCAmelCase__      :  Union[str, Any]				      =			num_hidden_layers
          UpperCAmelCase__      :  List[str]				      =			intermediate_size
          UpperCAmelCase__      :  str				      =			hidden_act
          UpperCAmelCase__      :  Union[str, Any]				      =			num_attention_heads
          UpperCAmelCase__      :  Optional[Any]				      =			hidden_dropout
          UpperCAmelCase__      :  List[str]				      =			attention_dropout
          UpperCAmelCase__      :  Dict				      =			activation_dropout
          UpperCAmelCase__      :  List[str]				      =			feat_proj_dropout
          UpperCAmelCase__      :  Any				      =			final_dropout
          UpperCAmelCase__      :  Optional[int]				      =			layerdrop
          UpperCAmelCase__      :  str				      =			layer_norm_eps
          UpperCAmelCase__      :  Dict				      =			initializer_range
          UpperCAmelCase__      :  int				      =			num_ctc_classes
          UpperCAmelCase__      :  Any				      =			vocab_size
          UpperCAmelCase__      :  List[str]				      =			do_stable_layer_norm
          UpperCAmelCase__      :  Any				      =			use_weighted_layer_sum
          UpperCAmelCase__      :  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
          UpperCAmelCase__      :  Any				      =			apply_spec_augment
          UpperCAmelCase__      :  List[Any]				      =			mask_time_prob
          UpperCAmelCase__      :  List[Any]				      =			mask_time_length
          UpperCAmelCase__      :  Tuple				      =			mask_time_min_masks
          UpperCAmelCase__      :  Optional[int]				      =			mask_feature_prob
          UpperCAmelCase__      :  List[str]				      =			mask_feature_length
          UpperCAmelCase__      :  Optional[Any]				      =			mask_feature_min_masks

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

          # ctc loss
          UpperCAmelCase__      :  int				      =			ctc_loss_reduction
          UpperCAmelCase__      :  List[str]				      =			ctc_zero_infinity

          # pretraining loss
          UpperCAmelCase__      :  Union[str, Any]				      =			replace_prob




     @property
     def   __UpperCAmelCase  (				self		):
          return functools.reduce(operator.mul	,						self.conv_stride	,						1		)







'''simple docstring'''




import re

from flax.core.frozen_dict import freeze
from flax.traverse_util import flatten_dict, unflatten_dict
from jax.experimental import PartitionSpec as P


# Sentinels
__UpperCAmelCase					=object()

# For specifying empty leaf dict `{}`
__UpperCAmelCase					=object()






def __lowerCAmelCase					(				UpperCamelCase__							,    UpperCamelCase__							)      ->       List[Any]:
    __lowerCamelCase            =    tuple((re.compile(x + '''$'''							) for x in qs)							)
    for i in range(len(UpperCamelCase__							) - len(UpperCamelCase__							) + 1							):
        __lowerCamelCase            =    [x.match(UpperCamelCase__							) for x, y in zip(UpperCamelCase__							,    ks[i:]							)]
        if matches and all(UpperCamelCase__							):
            return True
    return False






def __lowerCAmelCase					(				UpperCamelCase__							)      ->       Optional[int]:
    def replace(UpperCamelCase__							,    UpperCamelCase__							):
        for rule, replacement in rules:
            if _match(UpperCamelCase__							,    UpperCamelCase__							):
                return replacement
        return val

    return replace






def __lowerCAmelCase					(				)      ->       Dict:
    return [
        # embeddings
        (("transformer", "wpe", "embedding"), P('''mp'''							,    UpperCamelCase__							)),
        (("transformer", "wte", "embedding"), P('''mp'''							,    UpperCamelCase__							)),
        # atention
        (("attention", "(q_proj|k_proj|v_proj)", "kernel"), P(UpperCamelCase__							,    '''mp'''							)),
        (("attention", "out_proj", "kernel"), P('''mp'''							,    UpperCamelCase__							)),
        (("attention", "out_proj", "bias"), None),
        # mlp
        (("mlp", "c_fc", "kernel"), P(UpperCamelCase__							,    '''mp'''							)),
        (("mlp", "c_fc", "bias"), P('''mp'''							)),
        (("mlp", "c_proj", "kernel"), P('''mp'''							,    UpperCamelCase__							)),
        (("mlp", "c_proj", "bias"), None),
        # layer norms
        ((r"ln_\d+", "bias"), None),
        ((r"\d+", r"ln_\d+", "scale"), None),
        (("ln_f", "bias"), None),
        (("ln_f", "scale"), None),
    ]






def __lowerCAmelCase					(				UpperCamelCase__							)      ->       int:
    __lowerCamelCase            =    _get_partition_rules()
    __lowerCamelCase            =    _replacement_rules(UpperCamelCase__							)
    __lowerCamelCase            =    {k: _unmatched for k in flatten_dict(UpperCamelCase__							)}
    __lowerCamelCase            =    {k: replace(UpperCamelCase__							,    UpperCamelCase__							) for k, v in initd.items()}
    assert _unmatched not in result.values(), "Incomplete partition spec."
    return freeze(unflatten_dict(UpperCamelCase__							)							)
