import functools
import operator

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


lowercase_  							=			logging.get_logger(__name__)

lowercase_  							=			{
    "facebook/hubert-base-ls960": "https://huggingface.co/facebook/hubert-base-ls960/resolve/main/config.json",
    # See all Hubert models at https://huggingface.co/models?filter=hubert
}







class      SCREAMING_SNAKE_CASE__					(  __UpperCamelCase    ):
       A							:	List[str]          =	"hubert"

       def __init__(							self					:     List[str]			,			_lowerCAmelCase					:     Union[str, Any]=32			,			_lowerCAmelCase					:     Any=7_68			,			_lowerCAmelCase					:     Optional[Any]=12			,			_lowerCAmelCase					:     Optional[int]=12			,			_lowerCAmelCase					:     int=30_72			,			_lowerCAmelCase					:     Optional[Any]="gelu"			,			_lowerCAmelCase					:     List[str]=0.1			,			_lowerCAmelCase					:     Any=0.1			,			_lowerCAmelCase					:     List[str]=0.1			,			_lowerCAmelCase					:     Dict=True			,			_lowerCAmelCase					:     Dict=0.0			,			_lowerCAmelCase					:     List[str]=0.1			,			_lowerCAmelCase					:     Optional[int]=0.1			,			_lowerCAmelCase					:     Dict=0.02			,			_lowerCAmelCase					:     Any=1e-5			,			_lowerCAmelCase					:     str="group"			,			_lowerCAmelCase					:     Any="gelu"			,			_lowerCAmelCase					:     List[str]=(5_12, 5_12, 5_12, 5_12, 5_12, 5_12, 5_12)			,			_lowerCAmelCase					:     Any=(5, 2, 2, 2, 2, 2, 2)			,			_lowerCAmelCase					:     Dict=(10, 3, 3, 3, 3, 2, 2)			,			_lowerCAmelCase					:     List[str]=False			,			_lowerCAmelCase					:     Optional[Any]=1_28			,			_lowerCAmelCase					:     int=16			,			_lowerCAmelCase					:     Tuple=False			,			_lowerCAmelCase					:     int=True			,			_lowerCAmelCase					:     int=0.05			,			_lowerCAmelCase					:     Any=10			,			_lowerCAmelCase					:     Dict=2			,			_lowerCAmelCase					:     Dict=0.0			,			_lowerCAmelCase					:     Optional[int]=10			,			_lowerCAmelCase					:     Union[str, Any]=0			,			_lowerCAmelCase					:     int="sum"			,			_lowerCAmelCase					:     Optional[int]=False			,			_lowerCAmelCase					:     str=False			,			_lowerCAmelCase					:     Optional[Any]=2_56			,			_lowerCAmelCase					:     Any=0			,			_lowerCAmelCase					:     List[Any]=1			,			_lowerCAmelCase					:     List[str]=2			,			**_lowerCAmelCase					:     Optional[int]			,			):
           super().__init__(**_lowerCAmelCase			,			pad_token_id=_lowerCAmelCase			,			bos_token_id=_lowerCAmelCase			,			eos_token_id=_lowerCAmelCase			)
           __snake_case       :				Optional[int] 			=    hidden_size
           __snake_case       :				Optional[Any] 			=    feat_extract_norm
           __snake_case       :				Optional[int] 			=    feat_extract_activation
           __snake_case       :				List[str] 			=    list(_lowerCAmelCase			)
           __snake_case       :				Any 			=    list(_lowerCAmelCase			)
           __snake_case       :				List[Any] 			=    list(_lowerCAmelCase			)
           __snake_case       :				Any 			=    conv_bias
           __snake_case       :				Tuple 			=    num_conv_pos_embeddings
           __snake_case       :				List[Any] 			=    num_conv_pos_embedding_groups
           __snake_case       :				Union[str, Any] 			=    len(self.conv_dim			)
           __snake_case       :				List[Any] 			=    num_hidden_layers
           __snake_case       :				Tuple 			=    intermediate_size
           __snake_case       :				Optional[int] 			=    hidden_act
           __snake_case       :				Union[str, Any] 			=    num_attention_heads
           __snake_case       :				str 			=    hidden_dropout
           __snake_case       :				Union[str, Any] 			=    attention_dropout
           __snake_case       :				Dict 			=    activation_dropout
           __snake_case       :				List[Any] 			=    feat_proj_layer_norm
           __snake_case       :				List[str] 			=    feat_proj_dropout
           __snake_case       :				List[Any] 			=    final_dropout
           __snake_case       :				str 			=    layerdrop
           __snake_case       :				List[str] 			=    layer_norm_eps
           __snake_case       :				Optional[Any] 			=    initializer_range
           __snake_case       :				Optional[int] 			=    vocab_size
           __snake_case       :				Dict 			=    do_stable_layer_norm
           __snake_case       :				Tuple 			=    use_weighted_layer_sum
           __snake_case       :				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
           __snake_case       :				Union[str, Any] 			=    apply_spec_augment
           __snake_case       :				List[str] 			=    mask_time_prob
           __snake_case       :				Dict 			=    mask_time_length
           __snake_case       :				int 			=    mask_time_min_masks
           __snake_case       :				List[str] 			=    mask_feature_prob
           __snake_case       :				Tuple 			=    mask_feature_length
           __snake_case       :				int 			=    mask_feature_min_masks

           # ctc loss
           __snake_case       :				Dict 			=    ctc_loss_reduction
           __snake_case       :				List[str] 			=    ctc_zero_infinity





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




import torch
from torch import autograd







class      SCREAMING_SNAKE_CASE__					(  autograd.Function    ):

       @staticmethod
       def 			snake_case__    (							_lowerCAmelCase					:     Any			,			_lowerCAmelCase					:     torch.tensor			,			_lowerCAmelCase					:     float			,			_lowerCAmelCase					:     bool			):
           __snake_case       :				Optional[int] 			=    inputs.numel()
           __snake_case       :				str 			=    int(0.005 * nb_elems			) + 1
           if sigmoid:
               __snake_case       :				Union[str, Any] 			=    (torch.sigmoid(_lowerCAmelCase			) > threshold).type(inputs.type()			)
           else:
               __snake_case       :				Any 			=    (inputs > threshold).type(inputs.type()			)
           if mask.sum() < nb_min:
               # We limit the pruning so that at least 0.5% (half a percent) of the weights are remaining
               __snake_case       :				Dict 			=    inputs.flatten().kthvalue(max(nb_elems - nb_min			,			1			)			).values
               __snake_case       :				List[str] 			=    (inputs > k_threshold).type(inputs.type()			)
           return mask





       @staticmethod
       def 			snake_case__    (							_lowerCAmelCase					:     str			,			_lowerCAmelCase					:     Tuple			):
           return gradOutput, None, None







class      SCREAMING_SNAKE_CASE__					(  autograd.Function    ):

       @staticmethod
       def 			snake_case__    (							_lowerCAmelCase					:     Optional[int]			,			_lowerCAmelCase					:     torch.tensor			,			_lowerCAmelCase					:     float			):
           __snake_case       :				str 			=    inputs.clone()
           __snake_case			, __snake_case       :				Dict 			=    inputs.flatten().sort(descending=_lowerCAmelCase			)
           __snake_case       :				Optional[Any] 			=    int(threshold * inputs.numel()			)

           # flat_out and mask access the same memory.
           __snake_case       :				Optional[int] 			=    mask.flatten()
           __snake_case       :				Dict 			=    0
           __snake_case       :				Optional[Any] 			=    1
           return mask





       @staticmethod
       def 			snake_case__    (							_lowerCAmelCase					:     Any			,			_lowerCAmelCase					:     Optional[Any]			):
           return gradOutput, None







class      SCREAMING_SNAKE_CASE__					(  __UpperCamelCase    ):

       @staticmethod
       def 			snake_case__    (							_lowerCAmelCase					:     torch.tensor			,			_lowerCAmelCase					:     float			):
           __snake_case       :				List[str] 			=    inputs.clone()
           __snake_case			, __snake_case       :				str 			=    inputs.abs().flatten().sort(descending=_lowerCAmelCase			)
           __snake_case       :				List[Any] 			=    int(threshold * inputs.numel()			)

           # flat_out and mask access the same memory.
           __snake_case       :				List[Any] 			=    mask.flatten()
           __snake_case       :				int 			=    0
           __snake_case       :				Optional[int] 			=    1
           return mask



