




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

lowercase__				:      Dict	   = random.Random()

def  SCREAMING_SNAKE_CASE							(       __UpperCamelCase		, __UpperCamelCase=1.0		, __UpperCamelCase=None		, __UpperCamelCase=None)      ->					str:
     if rng is None:
          a     =				global_rng

     a     =				[]
     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       a__	( 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  ,		)  ->   str:



          '''simple docstring'''



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







     def       lowerCAmelCase_    (     self			)  ->   Any:



          '''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       lowerCAmelCase_    (     self  ,		A=False  ,		A=False			)  ->   Union[str, Any]:



          '''simple docstring'''



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

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







@require_torch
@require_torchaudio
class       a__	( UpperCamelCase__       , unittest.TestCase				):
     a		:      Any		 =     SpeechaTextFeatureExtractor if is_speech_available() else None







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



          '''simple docstring'''



          a     =				SpeechaTextFeatureExtractionTester(self			)







     def       lowerCAmelCase_    (     self  ,		A			)  ->   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       lowerCAmelCase_    (     self			)  ->   Any:



          '''simple docstring'''



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

          # Test feature size
          a     =				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
          a     =				feature_extractor(speech_inputs[0]  ,		return_tensors="np"			).input_features
          a     =				feature_extractor(np_speech_inputs[0]  ,		return_tensors="np"			).input_features
          self.assertTrue(np.allclose(A  ,		A  ,		atol=1e-3			)			)

          # Test batched
          a     =				feature_extractor(A  ,		return_tensors="np"			).input_features
          a     =				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.
          a     =				[floats_list((1, x)			)[0] for x in (800, 800, 800)]
          a     =				np.asarray(A			)
          a     =				feature_extractor(A  ,		return_tensors="np"			).input_features
          a     =				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       lowerCAmelCase_    (     self			)  ->   str:



          '''simple docstring'''



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

          a     =				["longest", "max_length", "do_not_pad"]
          a     =				[None, 16, None]
          for max_length, padding in zip(A  ,		A			):
               a     =				feature_extractor(
                   A  ,		padding=A  ,		max_length=A  ,		return_attention_mask=A			)
               a     =				inputs.input_features
               a     =				inputs.attention_mask
               a     =				[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       lowerCAmelCase_    (     self			)  ->   Optional[int]:



          '''simple docstring'''



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

          a     =				["longest", "max_length", "do_not_pad"]
          a     =				[None, 16, None]
          for max_length, padding in zip(A  ,		A			):
               a     =				feature_extractor(
                   A  ,		max_length=A  ,		padding=A  ,		return_tensors="np"  ,		return_attention_mask=A			)
               a     =				inputs.input_features
               a     =				inputs.attention_mask
               a     =				[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       lowerCAmelCase_    (     self			)  ->   int:



          '''simple docstring'''



          a     =				self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()			)
          a     =				[floats_list((1, x)			)[0] for x in range(800  ,		1400  ,		200			)]
          a     =				feature_extractor(
              A  ,		padding="max_length"  ,		max_length=4  ,		truncation=A  ,		return_tensors="np"  ,		return_attention_mask=A  ,		)
          a     =				inputs.input_features
          a     =				inputs.attention_mask
          a     =				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       lowerCAmelCase_    (     self			)  ->   str:



          '''simple docstring'''



          a     =				self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()			)
          a     =				[floats_list((1, x)			)[0] for x in range(800  ,		1400  ,		200			)]
          a     =				feature_extractor(
              A  ,		padding="longest"  ,		max_length=4  ,		truncation=A  ,		return_tensors="np"  ,		return_attention_mask=A  ,		)
          a     =				inputs.input_features
          a     =				inputs.attention_mask
          a     =				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)			)

          a     =				[floats_list((1, x)			)[0] for x in range(800  ,		1400  ,		200			)]
          a     =				feature_extractor(
              A  ,		padding="longest"  ,		max_length=16  ,		truncation=A  ,		return_tensors="np"  ,		return_attention_mask=A  ,		)
          a     =				inputs.input_features
          a     =				inputs.attention_mask
          a     =				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       lowerCAmelCase_    (     self			)  ->   Optional[Any]:



          '''simple docstring'''



          import torch

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

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







     def       lowerCAmelCase_    (     self  ,		A			)  ->   Dict:



          '''simple docstring'''



          from datasets import load_dataset

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

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







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



          '''simple docstring'''



          a     =				np.array([
              -1.5_7_4_5, -1.7_7_1_3, -1.7_0_2_0, -1.6_0_6_9, -1.2_2_5_0, -1.1_1_0_5, -0.9_0_7_2, -0.8_2_4_1,
              -1.2_3_1_0, -0.8_0_9_8, -0.3_3_2_0, -0.4_1_0_1, -0.7_9_8_5, -0.4_9_9_6, -0.8_2_1_3, -0.9_1_2_8,
              -1.0_4_2_0, -1.1_2_8_6, -1.0_4_4_0, -0.7_9_9_9, -0.8_4_0_5, -1.2_2_7_5, -1.5_4_4_3, -1.4_6_2_5,
          ]			)
          # fmt: on

          a     =				self._load_datasamples(1			)
          a     =				self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()			)
          a     =				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			)			)







import functools
import operator

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


lowercase__				:      Any	   = logging.get_logger(__name__)

lowercase__				:      int	   = {
    "asapp/sew-d-tiny-100k": "https://huggingface.co/asapp/sew-d-tiny-100k/resolve/main/config.json",
    # See all SEW-D models at https://huggingface.co/models?filter=sew-d
}




class       a__	( UpperCamelCase__				):
     a		:      Optional[Any]		 =     """sew-d"""







     def __init__(     self  ,		A=32  ,		A=768  ,		A=12  ,		A=12  ,		A=3072  ,		A=2  ,		A=512  ,		A=256  ,		A=True  ,		A=True  ,		A=("p2c", "c2p")  ,		A="layer_norm"  ,		A="gelu_python"  ,		A=0.1  ,		A=0.1  ,		A=0.1  ,		A=0.0  ,		A=0.1  ,		A=0.0_2  ,		A=1e-7  ,		A=1e-5  ,		A="group"  ,		A="gelu"  ,		A=(64, 128, 128, 128, 128, 256, 256, 256, 256, 512, 512, 512, 512)  ,		A=(5, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1)  ,		A=(10, 3, 1, 3, 1, 3, 1, 3, 1, 2, 1, 2, 1)  ,		A=False  ,		A=128  ,		A=16  ,		A=True  ,		A=0.0_5  ,		A=10  ,		A=2  ,		A=0.0  ,		A=10  ,		A=0  ,		A="mean"  ,		A=False  ,		A=False  ,		A=256  ,		A=0  ,		A=1  ,		A=2  ,		**A  ,		)  ->   Dict:



          '''simple docstring'''



          super().__init__(**A  ,		pad_token_id=A  ,		bos_token_id=A  ,		eos_token_id=A			)
          a     =				hidden_size
          a     =				feat_extract_norm
          a     =				feat_extract_activation
          a     =				list(A			)
          a     =				list(A			)
          a     =				list(A			)
          a     =				conv_bias
          a     =				num_conv_pos_embeddings
          a     =				num_conv_pos_embedding_groups
          a     =				len(self.conv_dim			)
          a     =				num_hidden_layers
          a     =				intermediate_size
          a     =				squeeze_factor
          a     =				max_position_embeddings
          a     =				position_buckets
          a     =				share_att_key
          a     =				relative_attention
          a     =				norm_rel_ebd
          a     =				list(A			)
          a     =				hidden_act
          a     =				num_attention_heads
          a     =				hidden_dropout
          a     =				attention_dropout
          a     =				activation_dropout
          a     =				feat_proj_dropout
          a     =				final_dropout
          a     =				layer_norm_eps
          a     =				feature_layer_norm_eps
          a     =				initializer_range
          a     =				vocab_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)`,"
                   F'''but is `len(config.conv_dim) = {len(self.conv_dim			)}`, `len(config.conv_stride)'''
                   F'''= {len(self.conv_stride			)}`, `len(config.conv_kernel) = {len(self.conv_kernel			)}`.'''			)

          # fine-tuning config parameters for SpecAugment: https://arxiv.org/abs/1904.08779
          a     =				apply_spec_augment
          a     =				mask_time_prob
          a     =				mask_time_length
          a     =				mask_time_min_masks
          a     =				mask_feature_prob
          a     =				mask_feature_length
          a     =				mask_feature_min_masks

          # ctc loss
          a     =				ctc_loss_reduction
          a     =				ctc_zero_infinity

          # sequence classification
          a     =				use_weighted_layer_sum
          a     =				classifier_proj_size







     @property
     def       lowerCAmelCase_    (     self			)  ->   List[str]:



          '''simple docstring'''



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

