


import itertools
import random
import unittest

import numpy as np

from transformers import ASTFeatureExtractor
from transformers.testing_utils import require_torch, require_torchaudio
from transformers.utils.import_utils import is_torch_available

from ...test_sequence_feature_extraction_common import SequenceFeatureExtractionTestMixin


a_ :str  							=			random.Random()

if is_torch_available():
    import torch




def    a   (							A__      ,    A__=1.0      ,    A__=None      ,    A__=None							)	-> Optional[Any]:




      '''simple docstring'''

      if rng is None:
            SCREAMING_SNAKE_CASE__       :  Tuple						     =		global_rng

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

      return values

class 							lowercase  (						unittest.TestCase			):







     def __init__(						self :						Optional[Any]   ,   _lowercase :						Tuple   ,   _lowercase :						Optional[Any]=7   ,   _lowercase :						List[str]=4_00   ,   _lowercase :						Optional[Any]=20_00   ,   _lowercase :						Optional[int]=1   ,   _lowercase :						Optional[Any]=0.0   ,   _lowercase :						Union[str, Any]=1_60_00   ,   _lowercase :						str=True   ,   _lowercase :						List[str]=True   ,   ):
           SCREAMING_SNAKE_CASE__       :  Any						     =		parent
           SCREAMING_SNAKE_CASE__       :  List[Any]						     =		batch_size
           SCREAMING_SNAKE_CASE__       :  str						     =		min_seq_length
           SCREAMING_SNAKE_CASE__       :  Optional[Any]						     =		max_seq_length
           SCREAMING_SNAKE_CASE__       :  Optional[int]						     =		(self.max_seq_length - self.min_seq_length) // (self.batch_size - 1)
           SCREAMING_SNAKE_CASE__       :  List[Any]						     =		feature_size
           SCREAMING_SNAKE_CASE__       :  Tuple						     =		padding_value
           SCREAMING_SNAKE_CASE__       :  Optional[int]						     =		sampling_rate
           SCREAMING_SNAKE_CASE__       :  Optional[Any]						     =		return_attention_mask
           SCREAMING_SNAKE_CASE__       :  List[str]						     =		do_normalize







     def 							lowercase__       (						self :						Tuple      ):
           return {
               "feature_size": self.feature_size,
               "padding_value": self.padding_value,
               "sampling_rate": self.sampling_rate,
               "return_attention_mask": self.return_attention_mask,
               "do_normalize": self.do_normalize,
           }







     def 							lowercase__       (						self :						int   ,   _lowercase :						Tuple=False   ,   _lowercase :						Optional[Any]=False      ):
           def _flatten(_lowercase :						Union[str, Any]      ):
                 return list(itertools.chain(*_lowercase      )      )

           if equal_length:
                 SCREAMING_SNAKE_CASE__       :  Tuple						     =		floats_list((self.batch_size, self.max_seq_length)      )
           else:
                 # make sure that inputs increase in size
                 SCREAMING_SNAKE_CASE__       :  Dict						     =		[
                     _flatten(floats_list((x, self.feature_size)      )      )
                     for x in range(self.min_seq_length   ,   self.max_seq_length   ,   self.seq_length_diff      )
                 ]

           if numpify:
                 SCREAMING_SNAKE_CASE__       :  int						     =		[np.asarray(_lowercase      ) for x in speech_inputs]

           return speech_inputs




@require_torch
@require_torchaudio
class 							lowercase  (						_UpperCAmelCase    ,       unittest.TestCase			):
     lowerCamelCase			:			Any      = ASTFeatureExtractor







     def 							lowercase__       (						self :						Optional[int]      ):
           SCREAMING_SNAKE_CASE__       :  str						     =		ASTFeatureExtractionTester(self      )







     def 							lowercase__       (						self :						str      ):
           # Tests that all call wrap to encode_plus and batch_encode_plus
           SCREAMING_SNAKE_CASE__       :  Optional[int]						     =		self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()      )
           # create three inputs of length 800, 1000, and 1200
           SCREAMING_SNAKE_CASE__       :  Union[str, Any]						     =		[floats_list((1, x)      )[0] for x in range(8_00   ,   14_00   ,   2_00      )]
           SCREAMING_SNAKE_CASE__       :  Any						     =		[np.asarray(_lowercase      ) for speech_input in speech_inputs]

           # Test not batched input
           SCREAMING_SNAKE_CASE__       :  Any						     =		feat_extract(speech_inputs[0]   ,   return_tensors='''np'''      ).input_values
           SCREAMING_SNAKE_CASE__       :  Union[str, Any]						     =		feat_extract(np_speech_inputs[0]   ,   return_tensors='''np'''      ).input_values
           self.assertTrue(np.allclose(_lowercase   ,   _lowercase   ,   atol=1E-3      )      )

           # Test batched
           SCREAMING_SNAKE_CASE__       :  Any						     =		feat_extract(_lowercase   ,   padding=_lowercase   ,   return_tensors='''np'''      ).input_values
           SCREAMING_SNAKE_CASE__       :  List[Any]						     =		feat_extract(_lowercase   ,   padding=_lowercase   ,   return_tensors='''np'''      ).input_values
           for enc_seq_a, enc_seq_a in zip(_lowercase   ,   _lowercase      ):
                 self.assertTrue(np.allclose(_lowercase   ,   _lowercase   ,   atol=1E-3      )      )

           # Test 2-D numpy arrays are batched.
           SCREAMING_SNAKE_CASE__       :  Union[str, Any]						     =		[floats_list((1, x)      )[0] for x in (8_00, 8_00, 8_00)]
           SCREAMING_SNAKE_CASE__       :  str						     =		np.asarray(_lowercase      )
           SCREAMING_SNAKE_CASE__       :  Dict						     =		feat_extract(_lowercase   ,   return_tensors='''np'''      ).input_values
           SCREAMING_SNAKE_CASE__       :  List[str]						     =		feat_extract(_lowercase   ,   return_tensors='''np'''      ).input_values
           for enc_seq_a, enc_seq_a in zip(_lowercase   ,   _lowercase      ):
                 self.assertTrue(np.allclose(_lowercase   ,   _lowercase   ,   atol=1E-3      )      )







     @require_torch
     def 							lowercase__       (						self :						Tuple      ):
           import torch

           SCREAMING_SNAKE_CASE__       :  Tuple						     =		self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()      )
           SCREAMING_SNAKE_CASE__       :  Any						     =		np.random.rand(1_00      ).astype(np.floataa      )
           SCREAMING_SNAKE_CASE__       :  List[str]						     =		np_speech_inputs.tolist()

           for inputs in [py_speech_inputs, np_speech_inputs]:
                 SCREAMING_SNAKE_CASE__       :  Union[str, Any]						     =		feature_extractor.pad([{'''input_values''': inputs}]   ,   return_tensors='''np'''      )
                 self.assertTrue(np_processed.input_values.dtype == np.floataa      )
                 SCREAMING_SNAKE_CASE__       :  Tuple						     =		feature_extractor.pad([{'''input_values''': inputs}]   ,   return_tensors='''pt'''      )
                 self.assertTrue(pt_processed.input_values.dtype == torch.floataa      )







     def 							lowercase__       (						self :						Optional[Any]   ,   _lowercase :						int      ):
           from datasets import load_dataset

           SCREAMING_SNAKE_CASE__       :  int						     =		load_dataset('''hf-internal-testing/librispeech_asr_dummy'''   ,   '''clean'''   ,   split='''validation'''      )
           # automatic decoding with librispeech
           SCREAMING_SNAKE_CASE__       :  Union[str, Any]						     =		ds.sort('''id'''      ).select(range(_lowercase      )      )[:num_samples]['''audio''']

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







     @require_torch
     def 							lowercase__       (						self :						Optional[int]      ):
           # fmt: off
           SCREAMING_SNAKE_CASE__       :  Optional[int]						     =		torch.tensor(
               [-0.9894, -1.2776, -0.9066, -1.2776, -0.9349, -1.2609, -1.0386, -1.2776,
                -1.1561, -1.2776, -1.2052, -1.2723, -1.2190, -1.2132, -1.2776, -1.1133,
                -1.1953, -1.1343, -1.1584, -1.2203, -1.1770, -1.2474, -1.2381, -1.1936,
                -0.9270, -0.8317, -0.8049, -0.7706, -0.7565, -0.7869]      )
           # fmt: on

           SCREAMING_SNAKE_CASE__       :  str						     =		self._load_datasamples(1      )
           SCREAMING_SNAKE_CASE__       :  List[Any]						     =		ASTFeatureExtractor()
           SCREAMING_SNAKE_CASE__       :  List[Any]						     =		feature_extractor(_lowercase   ,   return_tensors='''pt'''      ).input_values
           self.assertEquals(input_values.shape   ,   (1, 10_24, 1_28)      )
           self.assertTrue(torch.allclose(input_values[0, 0, :30]   ,   _lowercase   ,   atol=1E-4      )      )







"""simple docstring"""



import functools
import operator

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


SCREAMING_SNAKE_CASE__:Tuple									=			logging.get_logger(__name__)

SCREAMING_SNAKE_CASE__:Optional[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  snake_case__      (       snake_case_       ):
 _snake_case       :					Union[str, Any]							  =						"""sew-d"""


 def __init__( self    ,		lowerCamelCase=32    ,		lowerCamelCase=768    ,		lowerCamelCase=12    ,		lowerCamelCase=12    ,		lowerCamelCase=3072    ,		lowerCamelCase=2    ,		lowerCamelCase=512    ,		lowerCamelCase=256    ,		lowerCamelCase=True    ,		lowerCamelCase=True    ,		lowerCamelCase=("p2c", "c2p")    ,		lowerCamelCase="layer_norm"    ,		lowerCamelCase="gelu_python"    ,		lowerCamelCase=0.1    ,		lowerCamelCase=0.1    ,		lowerCamelCase=0.1    ,		lowerCamelCase=0.0    ,		lowerCamelCase=0.1    ,		lowerCamelCase=0.02    ,		lowerCamelCase=1E-7    ,		lowerCamelCase=1E-5    ,		lowerCamelCase="group"    ,		lowerCamelCase="gelu"    ,		lowerCamelCase=(64, 128, 128, 128, 128, 256, 256, 256, 256, 512, 512, 512, 512)    ,		lowerCamelCase=(5, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1)    ,		lowerCamelCase=(10, 3, 1, 3, 1, 3, 1, 3, 1, 2, 1, 2, 1)    ,		lowerCamelCase=False    ,		lowerCamelCase=128    ,		lowerCamelCase=16    ,		lowerCamelCase=True    ,		lowerCamelCase=0.05    ,		lowerCamelCase=10    ,		lowerCamelCase=2    ,		lowerCamelCase=0.0    ,		lowerCamelCase=10    ,		lowerCamelCase=0    ,		lowerCamelCase="mean"    ,		lowerCamelCase=False    ,		lowerCamelCase=False    ,		lowerCamelCase=256    ,		lowerCamelCase=0    ,		lowerCamelCase=1    ,		lowerCamelCase=2    ,		**lowerCamelCase    ,		):
        super().__init__(**lowerCamelCase    ,		pad_token_id=lowerCamelCase    ,		bos_token_id=lowerCamelCase    ,		eos_token_id=lowerCamelCase						)
        __a       =    hidden_size
        __a       =    feat_extract_norm
        __a       =    feat_extract_activation
        __a       =    list(lowerCamelCase						)
        __a       =    list(lowerCamelCase						)
        __a       =    list(lowerCamelCase						)
        __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(lowerCamelCase						)
        __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 						a__   ( self						):
        return functools.reduce(operator.mul    ,		self.conv_stride    ,		1						)
