
"""simple docstring"""





from math import ceil
from typing import List, Optional, Union

import numpy as np

from ...audio_utils import mel_filter_bank, spectrogram, window_function
from ...feature_extraction_sequence_utils import BatchFeature, SequenceFeatureExtractor
from ...utils import TensorType, logging


_a								=						logging.get_logger(__name__)






class        _UpperCAmelCase(       lowerCamelCase      ):
		lowercase__         = ['audio_values', 'audio_mask']



		def __init__(   self							,						__a=20_48							,						__a=1							,						__a=[16, 16]							,						__a=1_28							,						__a=4_41_00							,						__a=86							,						__a=20_48							,						__a=0.0							,						**__a							,						)			->			Optional[int]:






							'''simple docstring'''
							super().__init__(
							    feature_size=__a							,						sampling_rate=__a							,						padding_value=__a							,						**__a							,						)

							_UpperCamelCase									=    spectrogram_length
							_UpperCamelCase									=    num_channels
							_UpperCamelCase									=    patch_size
							_UpperCamelCase									=    feature_size // self.patch_size[1]
							_UpperCamelCase									=    n_fft
							_UpperCamelCase									=    sampling_rate // hop_length_to_sampling_rate
							_UpperCamelCase									=    sampling_rate
							_UpperCamelCase									=    padding_value
							_UpperCamelCase									=    mel_filter_bank(
							    num_frequency_bins=1 + n_fft // 2							,						num_mel_filters=__a							,						min_frequency=0.0							,						max_frequency=2_2050.0							,						sampling_rate=__a							,						norm='''slaney'''							,						mel_scale='''slaney'''							,						).T



		def 		UpperCAmelCase				(   self							,						__a)			->			np.ndarray:






							'''simple docstring'''
							_UpperCamelCase									=    spectrogram(
							    __a							,						window_function(self.n_fft							,						'''hann''')							,						frame_length=self.n_fft							,						hop_length=self.hop_length							,						power=2.0							,						mel_filters=self.mel_filters.T							,						log_mel='''dB'''							,						db_range=80.0							,						)
							_UpperCamelCase									=    log_spec[:, :-1]
							_UpperCamelCase									=    log_spec - 20.0
							_UpperCamelCase									=    np.clip(log_spec / 40.0							,						-2.0							,						0.0) + 1.0
							return log_spec




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






							'''simple docstring'''

							if sampling_rate is not None:
												if sampling_rate != self.sampling_rate:
																	raise ValueError(
																	    '''This feature extractor is set to support sampling rate'''
																	    F''' of {self.sampling_rate}. Please make sure that the provided `raw_speech` input was sampled'''
																	    F''' with {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.''')

							_UpperCamelCase									=    isinstance(__a							,						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}''')
							_UpperCamelCase									=    is_batched_numpy or (
							    isinstance(__a							,						(list, tuple)) and (isinstance(raw_speech[0]							,						(np.ndarray, tuple, list)))
							)
							if is_batched:
												_UpperCamelCase									=    [np.asarray([speech]							,						dtype=np.floataa).T for speech in raw_speech]
							elif not is_batched and not isinstance(__a							,						np.ndarray):
												_UpperCamelCase									=    np.asarray(__a							,						dtype=np.floataa)
							elif isinstance(__a							,						np.ndarray) and raw_speech.dtype is np.dtype(np.floataa):
												_UpperCamelCase									=    raw_speech.astype(np.floataa)
							# always return batch
							if not is_batched:
												_UpperCamelCase									=    [np.asarray([raw_speech]).T]

							# Convert audio signals to log mel spectrograms, truncate by time axis
							_UpperCamelCase									=    [
							    self._np_extract_fbank_features(waveform.squeeze()).T[: self.spectrogram_length] for waveform in raw_speech
							]
							if isinstance(audio_features[0]							,						__a):
												_UpperCamelCase									=    [np.asarray(__a							,						dtype=np.floataa) for feature in audio_features]

							# Create audio attention mask
							_UpperCamelCase									=    max(
							    [ceil(feature.shape[0] / self.patch_size[0]) * self.freq_len for feature in audio_features])  # The maximum number of audio patches in a batch
							if return_attention_mask:
												_UpperCamelCase									=    [
												    (ceil(feature.shape[0] / self.patch_size[0]) * self.freq_len) * [1]
												    + (max_patch_len - ceil(feature.shape[0] / self.patch_size[0]) * self.freq_len) * [0]
												    for feature in audio_features
												]
												_UpperCamelCase									=    np.array(__a).astype(np.floataa)

							# convert into correct format for padding
							_UpperCamelCase									=    max_patch_len // self.freq_len * self.patch_size[0]  # The maximum audio size in a batch
							_UpperCamelCase									=    np.ones([len(__a), 1, max_time_len, self.feature_size]).astype(np.floataa)
							_UpperCamelCase									=    padded_audio_features * self.padding_value
							for i in range(len(__a)):
												_UpperCamelCase									=    audio_features[i]
												_UpperCamelCase									=    feature

							# return as BatchFeature
							if return_attention_mask:
												_UpperCamelCase									=    {'''audio_values''': padded_audio_features, '''audio_mask''': audio_mask}
							else:
												_UpperCamelCase									=    {'''audio_values''': padded_audio_features}

							_UpperCamelCase									=    BatchFeature(data=__a							,						tensor_type=__a)
							return encoded_inputs








"""simple docstring"""





import functools
import operator

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


_a								=						logging.get_logger(__name__)

_a								=						{
    """microsoft/wavlm-base""": """https://huggingface.co/microsoft/wavlm-base/resolve/main/config.json""",
    # See all WavLM models at https://huggingface.co/models?filter=wavlm
}






class        _UpperCAmelCase(       lowerCamelCase      ):
		lowercase__         = 'wavlm'



		def __init__(   self							,						__a=32							,						__a=7_68							,						__a=12							,						__a=12							,						__a=30_72							,						__a="gelu"							,						__a=0.1							,						__a=0.1							,						__a=0.1							,						__a=0.0							,						__a=0.1							,						__a=0.1							,						__a=0.02							,						__a=1e-5							,						__a="group"							,						__a="gelu"							,						__a=(5_12, 5_12, 5_12, 5_12, 5_12, 5_12, 5_12)							,						__a=(5, 2, 2, 2, 2, 2, 2)							,						__a=(10, 3, 3, 3, 3, 2, 2)							,						__a=False							,						__a=1_28							,						__a=16							,						__a=3_20							,						__a=8_00							,						__a=False							,						__a=True							,						__a=0.05							,						__a=10							,						__a=2							,						__a=0.0							,						__a=10							,						__a=3_20							,						__a=2							,						__a=0.1							,						__a=1_00							,						__a=2_56							,						__a=2_56							,						__a=0.1							,						__a="mean"							,						__a=False							,						__a=False							,						__a=2_56							,						__a=(5_12, 5_12, 5_12, 5_12, 15_00)							,						__a=(5, 3, 3, 1, 1)							,						__a=(1, 2, 3, 1, 1)							,						__a=5_12							,						__a=80							,						__a=0							,						__a=1							,						__a=2							,						__a=False							,						__a=3							,						__a=2							,						__a=3							,						__a=None							,						**__a							,						)			->			Union[str, Any]:






							'''simple docstring'''
							super().__init__(**__a							,						pad_token_id=__a							,						bos_token_id=__a							,						eos_token_id=__a)
							_UpperCamelCase									=    hidden_size
							_UpperCamelCase									=    feat_extract_norm
							_UpperCamelCase									=    feat_extract_activation
							_UpperCamelCase									=    list(__a)
							_UpperCamelCase									=    list(__a)
							_UpperCamelCase									=    list(__a)
							_UpperCamelCase									=    conv_bias
							_UpperCamelCase									=    num_buckets
							_UpperCamelCase									=    max_bucket_distance
							_UpperCamelCase									=    num_conv_pos_embeddings
							_UpperCamelCase									=    num_conv_pos_embedding_groups
							_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									=    num_ctc_classes
							_UpperCamelCase									=    vocab_size
							_UpperCamelCase									=    do_stable_layer_norm
							_UpperCamelCase									=    use_weighted_layer_sum
							_UpperCamelCase									=    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									=    apply_spec_augment
							_UpperCamelCase									=    mask_time_prob
							_UpperCamelCase									=    mask_time_length
							_UpperCamelCase									=    mask_time_min_masks
							_UpperCamelCase									=    mask_feature_prob
							_UpperCamelCase									=    mask_feature_length

							# parameters for pretraining with codevector quantized representations
							_UpperCamelCase									=    num_codevectors_per_group
							_UpperCamelCase									=    num_codevector_groups
							_UpperCamelCase									=    contrastive_logits_temperature
							_UpperCamelCase									=    num_negatives
							_UpperCamelCase									=    codevector_dim
							_UpperCamelCase									=    proj_codevector_dim
							_UpperCamelCase									=    diversity_loss_weight

							# 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(__a)
							_UpperCamelCase									=    list(__a)
							_UpperCamelCase									=    list(__a)
							_UpperCamelCase									=    xvector_output_dim




		@property
		def 		UpperCAmelCase				(   self)			->			Optional[Any]:






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






