

import functools
import operator

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


UpperCamelCase__		   =    logging.get_logger(__name__)

UpperCamelCase__		   =    {
    '''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 						__snake_case						(							snake_case__     ):
       """simple docstring"""







       __SCREAMING_SNAKE_CASE													=       "hubert"


       def __init__(		self			,   _UpperCamelCase=32			,   _UpperCamelCase=7_68			,   _UpperCamelCase=12			,   _UpperCamelCase=12			,   _UpperCamelCase=30_72			,   _UpperCamelCase="gelu"			,   _UpperCamelCase=0.1			,   _UpperCamelCase=0.1			,   _UpperCamelCase=0.1			,   _UpperCamelCase=True			,   _UpperCamelCase=0.0			,   _UpperCamelCase=0.1			,   _UpperCamelCase=0.1			,   _UpperCamelCase=0.02			,   _UpperCamelCase=1E-5			,   _UpperCamelCase="group"			,   _UpperCamelCase="gelu"			,   _UpperCamelCase=(5_12, 5_12, 5_12, 5_12, 5_12, 5_12, 5_12)			,   _UpperCamelCase=(5, 2, 2, 2, 2, 2, 2)			,   _UpperCamelCase=(10, 3, 3, 3, 3, 2, 2)			,   _UpperCamelCase=False			,   _UpperCamelCase=1_28			,   _UpperCamelCase=16			,   _UpperCamelCase=False			,   _UpperCamelCase=True			,   _UpperCamelCase=0.05			,   _UpperCamelCase=10			,   _UpperCamelCase=2			,   _UpperCamelCase=0.0			,   _UpperCamelCase=10			,   _UpperCamelCase=0			,   _UpperCamelCase="sum"			,   _UpperCamelCase=False			,   _UpperCamelCase=False			,   _UpperCamelCase=2_56			,   _UpperCamelCase=0			,   _UpperCamelCase=1			,   _UpperCamelCase=2			,   **_UpperCamelCase			,   )			->       Any:






              """simple docstring"""


              super().__init__(**_UpperCamelCase			,   pad_token_id=_UpperCamelCase			,   bos_token_id=_UpperCamelCase			,   eos_token_id=_UpperCamelCase      )
              __snake_case					  =       hidden_size
              __snake_case					  =       feat_extract_norm
              __snake_case					  =       feat_extract_activation
              __snake_case					  =       list(_UpperCamelCase      )
              __snake_case					  =       list(_UpperCamelCase      )
              __snake_case					  =       list(_UpperCamelCase      )
              __snake_case					  =       conv_bias
              __snake_case					  =       num_conv_pos_embeddings
              __snake_case					  =       num_conv_pos_embedding_groups
              __snake_case					  =       len(self.conv_dim      )
              __snake_case					  =       num_hidden_layers
              __snake_case					  =       intermediate_size
              __snake_case					  =       hidden_act
              __snake_case					  =       num_attention_heads
              __snake_case					  =       hidden_dropout
              __snake_case					  =       attention_dropout
              __snake_case					  =       activation_dropout
              __snake_case					  =       feat_proj_layer_norm
              __snake_case					  =       feat_proj_dropout
              __snake_case					  =       final_dropout
              __snake_case					  =       layerdrop
              __snake_case					  =       layer_norm_eps
              __snake_case					  =       initializer_range
              __snake_case					  =       vocab_size
              __snake_case					  =       do_stable_layer_norm
              __snake_case					  =       use_weighted_layer_sum
              __snake_case					  =       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					  =       apply_spec_augment
              __snake_case					  =       mask_time_prob
              __snake_case					  =       mask_time_length
              __snake_case					  =       mask_time_min_masks
              __snake_case					  =       mask_feature_prob
              __snake_case					  =       mask_feature_length
              __snake_case					  =       mask_feature_min_masks

              # ctc loss
              __snake_case					  =       ctc_loss_reduction
              __snake_case					  =       ctc_zero_infinity






       @property
       def  a		(		self      )			->       Union[str, Any]:






              """simple docstring"""


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




from typing import List, Optional, Union

import numpy as np

from ...feature_extraction_sequence_utils import SequenceFeatureExtractor
from ...feature_extraction_utils import BatchFeature
from ...utils import PaddingStrategy, TensorType, logging


UpperCamelCase__		   =    logging.get_logger(__name__)

class 						__snake_case						(							snake_case__     ):
       """simple docstring"""







       __SCREAMING_SNAKE_CASE													=       ["input_values", "attention_mask"]


       def __init__(		self			,   _UpperCamelCase=1			,   _UpperCamelCase=1_60_00			,   _UpperCamelCase=0.0			,   _UpperCamelCase=False			,   _UpperCamelCase=True			,   **_UpperCamelCase			,   )			->       List[Any]:






              """simple docstring"""


              super().__init__(feature_size=_UpperCamelCase			,   sampling_rate=_UpperCamelCase			,   padding_value=_UpperCamelCase			,   **_UpperCamelCase      )
              __snake_case					  =       return_attention_mask
              __snake_case					  =       do_normalize


       @staticmethod
       def  a		(		_UpperCamelCase			,   _UpperCamelCase			,   _UpperCamelCase = 0.0      )			->       List[np.ndarray]:






              """simple docstring"""


              if attention_mask is not None:
                     __snake_case					  =       np.array(_UpperCamelCase			,   np.intaa      )
                     __snake_case					  =       []

                     for vector, length in zip(_UpperCamelCase			,   attention_mask.sum(-1      )      ):
                            __snake_case					  =       (vector - vector[:length].mean()) / np.sqrt(vector[:length].var() + 1E-7      )
                            if length < normed_slice.shape[0]:
                                   __snake_case					  =       padding_value

                            normed_input_values.append(_UpperCamelCase      )
              else:
                     __snake_case					  =       [(x - x.mean()) / np.sqrt(x.var() + 1E-7      ) for x in input_values]

              return normed_input_values






       def __call__(		self			,   _UpperCamelCase			,   _UpperCamelCase = False			,   _UpperCamelCase = None			,   _UpperCamelCase = False			,   _UpperCamelCase = None			,   _UpperCamelCase = None			,   _UpperCamelCase = None			,   _UpperCamelCase = None			,   **_UpperCamelCase			,   )			->       BatchFeature:






              """simple docstring"""


              if sampling_rate is not None:
                     if sampling_rate != self.sampling_rate:
                            raise ValueError(
                                F'The model corresponding to this feature extractor: {self} was trained using a sampling rate of'
                                F' {self.sampling_rate}. Please make sure that the provided `raw_speech` input was sampled with'
                                F' {self.sampling_rate} and not {sampling_rate}.'      )
              else:
                     logger.warning(
                         """It is strongly recommended to pass the ``sampling_rate`` argument to this function. """
                         """Failing to do so can result in silent errors that might be hard to debug."""      )

              __snake_case					  =       isinstance(_UpperCamelCase			,   np.ndarray      ) and len(raw_speech.shape      ) > 1
              if is_batched_numpy and len(raw_speech.shape      ) > 2:
                     raise ValueError(F'Only mono-channel audio is supported for input to {self}'      )
              __snake_case					  =       is_batched_numpy or (
                  isinstance(_UpperCamelCase			,   (list, tuple)      ) and (isinstance(raw_speech[0]			,   (np.ndarray, tuple, list)      ))
              )

              # always return batch
              if not is_batched:
                     __snake_case					  =       [raw_speech]

              # convert into correct format for padding
              __snake_case					  =       BatchFeature({"""input_values""": raw_speech}      )

              __snake_case					  =       self.pad(
                  _UpperCamelCase			,   padding=_UpperCamelCase			,   max_length=_UpperCamelCase			,   truncation=_UpperCamelCase			,   pad_to_multiple_of=_UpperCamelCase			,   return_attention_mask=_UpperCamelCase			,   )

              # convert input values to correct format
              __snake_case					  =       padded_inputs["""input_values"""]
              if not isinstance(input_values[0]			,   np.ndarray      ):
                     __snake_case					  =       [np.asarray(_UpperCamelCase			,   dtype=np.floataa      ) for array in input_values]
              elif (
                  not isinstance(_UpperCamelCase			,   np.ndarray      )
                  and isinstance(input_values[0]			,   np.ndarray      )
                  and input_values[0].dtype is np.dtype(np.floataa      )
              ):
                     __snake_case					  =       [array.astype(np.floataa      ) for array in input_values]
              elif isinstance(_UpperCamelCase			,   np.ndarray      ) and input_values.dtype is np.dtype(np.floataa      ):
                     __snake_case					  =       input_values.astype(np.floataa      )

              # convert attention_mask to correct format
              __snake_case					  =       padded_inputs.get("""attention_mask"""      )
              if attention_mask is not None:
                     __snake_case					  =       [np.asarray(_UpperCamelCase			,   dtype=np.intaa      ) for array in attention_mask]

              # zero-mean and unit-variance normalization
              if self.do_normalize:
                     __snake_case					  =       (
                         attention_mask
                         if self._get_padding_strategies(_UpperCamelCase			,   max_length=_UpperCamelCase      ) is not PaddingStrategy.DO_NOT_PAD
                         else None
                     )
                     __snake_case					  =       self.zero_mean_unit_var_norm(
                         padded_inputs["""input_values"""]			,   attention_mask=_UpperCamelCase			,   padding_value=self.padding_value      )

              if return_tensors is not None:
                     __snake_case					  =       padded_inputs.convert_to_tensors(_UpperCamelCase      )

              return padded_inputs

