
'''simple docstring'''
import itertools
import random
import unittest

import numpy as np

from transformers import is_speech_available
from transformers.testing_utils import require_torch, require_torchaudio

from ...test_sequence_feature_extraction_common import SequenceFeatureExtractionTestMixin


if is_speech_available():
						from transformers import SpeechaTextFeatureExtractor

lowerCAmelCase					:    int           = random.Random()







def 		A_(     A						:		Optional[int]		,     A						:		Dict=1.0		,     A						:		List[Any]=None		,     A						:		Any=None):
							if rng is None:
														UpperCamelCase   			=     global_rng

							UpperCamelCase   			=     []
							for batch_idx in range(shape[0]):
														values.append([])
														for _ in range(shape[1]):
																					values[-1].append(rng.random() * scale)

							return values


@require_torch
@require_torchaudio
class 			SCREAMING_SNAKE_CASE__      (     unittest.TestCase):







				def __init__(       self						,				A_						,				A_=7						,				A_=400						,				A_=2000						,				A_=24						,				A_=24						,				A_=0.0						,				A_=16000						,				A_=True						,				A_=True						,				)->		int:







											'''simple docstring'''






											UpperCamelCase   			=     parent
											UpperCamelCase   			=     batch_size
											UpperCamelCase   			=     min_seq_length
											UpperCamelCase   			=     max_seq_length
											UpperCamelCase   			=     (self.max_seq_length - self.min_seq_length) // (self.batch_size - 1)
											UpperCamelCase   			=     feature_size
											UpperCamelCase   			=     num_mel_bins
											UpperCamelCase   			=     padding_value
											UpperCamelCase   			=     sampling_rate
											UpperCamelCase   			=     return_attention_mask
											UpperCamelCase   			=     do_normalize







				def 						UpperCAmelCase_    (       self     )->		Tuple:







											'''simple docstring'''






											return {
											    "feature_size": self.feature_size,
											    "num_mel_bins": self.num_mel_bins,
											    "padding_value": self.padding_value,
											    "sampling_rate": self.sampling_rate,
											    "return_attention_mask": self.return_attention_mask,
											    "do_normalize": self.do_normalize,
											}







				def 						UpperCAmelCase_    (       self						,				A_=False						,				A_=False     )->		Union[str, Any]:







											'''simple docstring'''






											def _flatten(A_     ):
																		return list(itertools.chain(*A_     )     )

											if equal_length:
																		UpperCamelCase   			=     [floats_list((self.max_seq_length, self.feature_size)     ) for _ in range(self.batch_size     )]
											else:
																		# make sure that inputs increase in size
																		UpperCamelCase   			=     [
																		    floats_list((x, self.feature_size)     )
																		    for x in range(self.min_seq_length						,				self.max_seq_length						,				self.seq_length_diff     )
																		]
											if numpify:
																		UpperCamelCase   			=     [np.asarray(A_     ) for x in speech_inputs]
											return speech_inputs


@require_torch
@require_torchaudio
class 			SCREAMING_SNAKE_CASE__      (     snake_case_							,       unittest.TestCase):
				lowerCAmelCase_     			=							SpeechaTextFeatureExtractor if is_speech_available() else None







				def 						UpperCAmelCase_    (       self     )->		Optional[Any]:







											'''simple docstring'''






											UpperCamelCase   			=     SpeechaTextFeatureExtractionTester(self     )







				def 						UpperCAmelCase_    (       self						,				A_     )->		Union[str, Any]:







											'''simple docstring'''






											self.assertTrue(np.all(np.mean(A_						,				axis=0     ) < 1e-3     )     )
											self.assertTrue(np.all(np.abs(np.var(A_						,				axis=0     ) - 1     ) < 1e-3     )     )







				def 						UpperCAmelCase_    (       self     )->		int:







											'''simple docstring'''






											UpperCamelCase   			=     self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()     )
											# create three inputs of length 800, 1000, and 1200
											UpperCamelCase   			=     [floats_list((1, x)     )[0] for x in range(800						,				1400						,				200     )]
											UpperCamelCase   			=     [np.asarray(A_     ) for speech_input in speech_inputs]

											# Test feature size
											UpperCamelCase   			=     feature_extractor(A_						,				padding=A_						,				return_tensors='np'     ).input_features
											self.assertTrue(input_features.ndim == 3     )
											self.assertTrue(input_features.shape[-1] == feature_extractor.feature_size     )

											# Test not batched input
											UpperCamelCase   			=     feature_extractor(speech_inputs[0]						,				return_tensors='np'     ).input_features
											UpperCamelCase   			=     feature_extractor(np_speech_inputs[0]						,				return_tensors='np'     ).input_features
											self.assertTrue(np.allclose(A_						,				A_						,				atol=1e-3     )     )

											# Test batched
											UpperCamelCase   			=     feature_extractor(A_						,				return_tensors='np'     ).input_features
											UpperCamelCase   			=     feature_extractor(A_						,				return_tensors='np'     ).input_features
											for enc_seq_a, enc_seq_a in zip(A_						,				A_     ):
																		self.assertTrue(np.allclose(A_						,				A_						,				atol=1e-3     )     )

											# Test 2-D numpy arrays are batched.
											UpperCamelCase   			=     [floats_list((1, x)     )[0] for x in (800, 800, 800)]
											UpperCamelCase   			=     np.asarray(A_     )
											UpperCamelCase   			=     feature_extractor(A_						,				return_tensors='np'     ).input_features
											UpperCamelCase   			=     feature_extractor(A_						,				return_tensors='np'     ).input_features
											for enc_seq_a, enc_seq_a in zip(A_						,				A_     ):
																		self.assertTrue(np.allclose(A_						,				A_						,				atol=1e-3     )     )







				def 						UpperCAmelCase_    (       self     )->		Union[str, Any]:







											'''simple docstring'''






											UpperCamelCase   			=     self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()     )
											UpperCamelCase   			=     [floats_list((1, x)     )[0] for x in range(800						,				1400						,				200     )]

											UpperCamelCase   			=     ['longest', 'max_length', 'do_not_pad']
											UpperCamelCase   			=     [None, 16, None]
											for max_length, padding in zip(A_						,				A_     ):
																		UpperCamelCase   			=     feature_extractor(
																		    A_						,				padding=A_						,				max_length=A_						,				return_attention_mask=A_     )
																		UpperCamelCase   			=     inputs.input_features
																		UpperCamelCase   			=     inputs.attention_mask
																		UpperCamelCase   			=     [np.sum(A_     ) for x in attention_mask]

																		self._check_zero_mean_unit_variance(input_features[0][: fbank_feat_lengths[0]]     )
																		self._check_zero_mean_unit_variance(input_features[1][: fbank_feat_lengths[1]]     )
																		self._check_zero_mean_unit_variance(input_features[2][: fbank_feat_lengths[2]]     )







				def 						UpperCAmelCase_    (       self     )->		List[Any]:







											'''simple docstring'''






											UpperCamelCase   			=     self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()     )
											UpperCamelCase   			=     [floats_list((1, x)     )[0] for x in range(800						,				1400						,				200     )]

											UpperCamelCase   			=     ['longest', 'max_length', 'do_not_pad']
											UpperCamelCase   			=     [None, 16, None]
											for max_length, padding in zip(A_						,				A_     ):
																		UpperCamelCase   			=     feature_extractor(
																		    A_						,				max_length=A_						,				padding=A_						,				return_tensors='np'						,				return_attention_mask=A_     )
																		UpperCamelCase   			=     inputs.input_features
																		UpperCamelCase   			=     inputs.attention_mask
																		UpperCamelCase   			=     [np.sum(A_     ) for x in attention_mask]

																		self._check_zero_mean_unit_variance(input_features[0][: fbank_feat_lengths[0]]     )
																		self.assertTrue(input_features[0][fbank_feat_lengths[0] :].sum() < 1e-6     )
																		self._check_zero_mean_unit_variance(input_features[1][: fbank_feat_lengths[1]]     )
																		self.assertTrue(input_features[0][fbank_feat_lengths[1] :].sum() < 1e-6     )
																		self._check_zero_mean_unit_variance(input_features[2][: fbank_feat_lengths[2]]     )







				def 						UpperCAmelCase_    (       self     )->		List[Any]:







											'''simple docstring'''






											UpperCamelCase   			=     self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()     )
											UpperCamelCase   			=     [floats_list((1, x)     )[0] for x in range(800						,				1400						,				200     )]
											UpperCamelCase   			=     feature_extractor(
											    A_						,				padding='max_length'						,				max_length=4						,				truncation=A_						,				return_tensors='np'						,				return_attention_mask=A_						,				)
											UpperCamelCase   			=     inputs.input_features
											UpperCamelCase   			=     inputs.attention_mask
											UpperCamelCase   			=     np.sum(attention_mask == 1						,				axis=1     )

											self._check_zero_mean_unit_variance(input_features[0, : fbank_feat_lengths[0]]     )
											self._check_zero_mean_unit_variance(input_features[1]     )
											self._check_zero_mean_unit_variance(input_features[2]     )







				def 						UpperCAmelCase_    (       self     )->		Any:







											'''simple docstring'''






											UpperCamelCase   			=     self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()     )
											UpperCamelCase   			=     [floats_list((1, x)     )[0] for x in range(800						,				1400						,				200     )]
											UpperCamelCase   			=     feature_extractor(
											    A_						,				padding='longest'						,				max_length=4						,				truncation=A_						,				return_tensors='np'						,				return_attention_mask=A_						,				)
											UpperCamelCase   			=     inputs.input_features
											UpperCamelCase   			=     inputs.attention_mask
											UpperCamelCase   			=     np.sum(attention_mask == 1						,				axis=1     )

											self._check_zero_mean_unit_variance(input_features[0, : fbank_feat_lengths[0]]     )
											self._check_zero_mean_unit_variance(input_features[1, : fbank_feat_lengths[1]]     )
											self._check_zero_mean_unit_variance(input_features[2]     )

											# make sure that if max_length < longest -> then pad to max_length
											self.assertEqual(input_features.shape						,				(3, 4, 24)     )

											UpperCamelCase   			=     [floats_list((1, x)     )[0] for x in range(800						,				1400						,				200     )]
											UpperCamelCase   			=     feature_extractor(
											    A_						,				padding='longest'						,				max_length=16						,				truncation=A_						,				return_tensors='np'						,				return_attention_mask=A_						,				)
											UpperCamelCase   			=     inputs.input_features
											UpperCamelCase   			=     inputs.attention_mask
											UpperCamelCase   			=     np.sum(attention_mask == 1						,				axis=1     )

											self._check_zero_mean_unit_variance(input_features[0, : fbank_feat_lengths[0]]     )
											self._check_zero_mean_unit_variance(input_features[1, : fbank_feat_lengths[1]]     )
											self._check_zero_mean_unit_variance(input_features[2]     )

											# make sure that if max_length < longest -> then pad to max_length
											self.assertEqual(input_features.shape						,				(3, 6, 24)     )







				def 						UpperCAmelCase_    (       self     )->		Dict:







											'''simple docstring'''






											import torch

											UpperCamelCase   			=     self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()     )
											UpperCamelCase   			=     np.random.rand(100						,				32     ).astype(np.floataa     )
											UpperCamelCase   			=     np_speech_inputs.tolist()

											for inputs in [py_speech_inputs, np_speech_inputs]:
																		UpperCamelCase   			=     feature_extractor.pad([{'input_features': inputs}]						,				return_tensors='np'     )
																		self.assertTrue(np_processed.input_features.dtype == np.floataa     )
																		UpperCamelCase   			=     feature_extractor.pad([{'input_features': inputs}]						,				return_tensors='pt'     )
																		self.assertTrue(pt_processed.input_features.dtype == torch.floataa     )







				def 						UpperCAmelCase_    (       self						,				A_     )->		Optional[Any]:







											'''simple docstring'''






											from datasets import load_dataset

											UpperCamelCase   			=     load_dataset('hf-internal-testing/librispeech_asr_dummy'						,				'clean'						,				split='validation'     )
											# automatic decoding with librispeech
											UpperCamelCase   			=     ds.sort('id'     ).select(range(A_     )     )[:num_samples]['audio']

											return [x["array"] for x in speech_samples]







				def 						UpperCAmelCase_    (       self     )->		Dict:







											'''simple docstring'''






											UpperCamelCase   			=     np.array([
											    -1.5_745, -1.7_713, -1.7_020, -1.6_069, -1.2_250, -1.1_105, -0.9_072, -0.8_241,
											    -1.2_310, -0.8_098, -0.3_320, -0.4_101, -0.7_985, -0.4_996, -0.8_213, -0.9_128,
											    -1.0_420, -1.1_286, -1.0_440, -0.7_999, -0.8_405, -1.2_275, -1.5_443, -1.4_625,
											]     )
											# fmt: on

											UpperCamelCase   			=     self._load_datasamples(1     )
											UpperCamelCase   			=     self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()     )
											UpperCamelCase   			=     feature_extractor(A_						,				return_tensors='pt'     ).input_features
											self.assertEquals(input_features.shape						,				(1, 584, 24)     )
											self.assertTrue(np.allclose(input_features[0, 0, :30]						,				A_						,				atol=1e-4     )     )






'''simple docstring'''
import functools
import operator

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


lowerCAmelCase					:    int           = logging.get_logger(__name__)

lowerCAmelCase					:    Tuple           = {
    'microsoft/unispeech-sat-base-100h-libri-ft': (
        'https://huggingface.co/microsoft/unispeech-sat-base-100h-libri-ft/resolve/main/config.json'
    ),
    # See all UniSpeechSat models at https://huggingface.co/models?filter=unispeech_sat
}


class 			SCREAMING_SNAKE_CASE__      (     snake_case_):
				lowerCAmelCase_     			=							"""unispeech-sat"""







				def __init__(       self						,				A_=32						,				A_=768						,				A_=12						,				A_=12						,				A_=3072						,				A_="gelu"						,				A_=0.1						,				A_=0.1						,				A_=0.1						,				A_=0.0						,				A_=0.0						,				A_=0.1						,				A_=0.1						,				A_=0.02						,				A_=1e-5						,				A_="group"						,				A_="gelu"						,				A_=(512, 512, 512, 512, 512, 512, 512)						,				A_=(5, 2, 2, 2, 2, 2, 2)						,				A_=(10, 3, 3, 3, 3, 2, 2)						,				A_=False						,				A_=128						,				A_=16						,				A_=False						,				A_=True						,				A_=0.05						,				A_=10						,				A_=2						,				A_=0.0						,				A_=10						,				A_=0						,				A_=320						,				A_=2						,				A_=0.1						,				A_=100						,				A_=256						,				A_=256						,				A_=0.1						,				A_="mean"						,				A_=False						,				A_=False						,				A_=256						,				A_=(512, 512, 512, 512, 1500)						,				A_=(5, 3, 3, 1, 1)						,				A_=(1, 2, 3, 1, 1)						,				A_=512						,				A_=0						,				A_=1						,				A_=2						,				A_=504						,				**A_						,				)->		Tuple:







											'''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_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   			=     vocab_size
											UpperCamelCase   			=     num_clusters
											UpperCamelCase   			=     do_stable_layer_norm
											UpperCamelCase   			=     use_weighted_layer_sum

											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
											UpperCamelCase   			=     mask_feature_min_masks

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

											# ctc loss
											UpperCamelCase   			=     ctc_loss_reduction
											UpperCamelCase   			=     ctc_zero_infinity

											# 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     )




