





'''simple docstring'''

import itertools
import os
import random
import tempfile
import unittest

import numpy as np

from transformers import TvltFeatureExtractor, is_datasets_available
from transformers.testing_utils import check_json_file_has_correct_format, require_torch, require_torchaudio
from transformers.utils.import_utils import is_torch_available

from ...test_sequence_feature_extraction_common import SequenceFeatureExtractionTestMixin


if is_torch_available():
    import torch

if is_datasets_available():
    from datasets import load_dataset

a        =						random.Random()

def 						__magic_name__       (							__UpperCAmelCase							,	__UpperCAmelCase=1.0							,	__UpperCAmelCase=None							,	__UpperCAmelCase=None      )						->					Optional[int]:

   '''simple docstring'''







   if rng is None:
      __SCREAMING_SNAKE_CASE      							=       global_rng

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

   return values







class __a (   unittest.TestCase			):





   def __init__(					self				:			List[Any]				,lowerCamelCase				:			Optional[Any]				,lowerCamelCase				:			Optional[Any]=7				,lowerCamelCase				:			Union[str, Any]=400				,lowerCamelCase				:			Any=2000				,lowerCamelCase				:			Optional[Any]=2048				,lowerCamelCase				:			Any=128				,lowerCamelCase				:			List[Any]=1				,lowerCamelCase				:			str=512				,lowerCamelCase				:			Any=30				,lowerCamelCase				:			List[str]=4_4100				,):

      '''simple docstring'''

      __SCREAMING_SNAKE_CASE      							=       parent
      __SCREAMING_SNAKE_CASE      							=       batch_size
      __SCREAMING_SNAKE_CASE      							=       min_seq_length
      __SCREAMING_SNAKE_CASE      							=       max_seq_length
      __SCREAMING_SNAKE_CASE      							=       (self.max_seq_length - self.min_seq_length) // (self.batch_size - 1)
      __SCREAMING_SNAKE_CASE      							=       spectrogram_length
      __SCREAMING_SNAKE_CASE      							=       feature_size
      __SCREAMING_SNAKE_CASE      							=       num_audio_channels
      __SCREAMING_SNAKE_CASE      							=       hop_length
      __SCREAMING_SNAKE_CASE      							=       chunk_length
      __SCREAMING_SNAKE_CASE      							=       sampling_rate





   def      UpperCAmelCase__     (					self				:			List[Any]    ):

      '''simple docstring'''

      return {
          "spectrogram_length": self.spectrogram_length,
          "feature_size": self.feature_size,
          "num_audio_channels": self.num_audio_channels,
          "hop_length": self.hop_length,
          "chunk_length": self.chunk_length,
          "sampling_rate": self.sampling_rate,
      }





   def      UpperCAmelCase__     (					self				:			str				,lowerCamelCase				:			str=False				,lowerCamelCase				:			Any=False    ):

      '''simple docstring'''

      def _flatten(lowerCamelCase				:			Optional[int]    ):
         return list(itertools.chain(*lowerCamelCase    )    )

      if equal_length:
         __SCREAMING_SNAKE_CASE      							=       [floats_list((self.max_seq_length, self.feature_size)    ) for _ in range(self.batch_size    )]
      else:
         # make sure that inputs increase in size
         __SCREAMING_SNAKE_CASE      							=       [
             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      							=       [np.asarray(lowerCamelCase    ) for x in speech_inputs]
      return speech_inputs







@require_torch
@require_torchaudio
class __a (   _snake_case,   unittest.TestCase			):
   __UpperCamelCase   : Any	      =	TvltFeatureExtractor





   def      UpperCAmelCase__     (					self				:			int    ):

      '''simple docstring'''

      __SCREAMING_SNAKE_CASE      							=       TvltFeatureExtractionTester(self    )





   def      UpperCAmelCase__     (					self				:			Any    ):

      '''simple docstring'''

      __SCREAMING_SNAKE_CASE      							=       self.feature_extraction_class(**self.feat_extract_dict    )
      self.assertTrue(hasattr(lowerCamelCase				,"""spectrogram_length"""    )    )
      self.assertTrue(hasattr(lowerCamelCase				,"""feature_size"""    )    )
      self.assertTrue(hasattr(lowerCamelCase				,"""num_audio_channels"""    )    )
      self.assertTrue(hasattr(lowerCamelCase				,"""hop_length"""    )    )
      self.assertTrue(hasattr(lowerCamelCase				,"""chunk_length"""    )    )
      self.assertTrue(hasattr(lowerCamelCase				,"""sampling_rate"""    )    )





   def      UpperCAmelCase__     (					self				:			Optional[Any]    ):

      '''simple docstring'''

      __SCREAMING_SNAKE_CASE      							=       self.feature_extraction_class(**self.feat_extract_dict    )

      with tempfile.TemporaryDirectory() as tmpdirname:
         __SCREAMING_SNAKE_CASE      							=       feat_extract_first.save_pretrained(lowerCamelCase    )[0]
         check_json_file_has_correct_format(lowerCamelCase    )
         __SCREAMING_SNAKE_CASE      							=       self.feature_extraction_class.from_pretrained(lowerCamelCase    )

      __SCREAMING_SNAKE_CASE      							=       feat_extract_first.to_dict()
      __SCREAMING_SNAKE_CASE      							=       feat_extract_second.to_dict()
      __SCREAMING_SNAKE_CASE      							=       dict_first.pop("""mel_filters"""    )
      __SCREAMING_SNAKE_CASE      							=       dict_second.pop("""mel_filters"""    )
      self.assertTrue(np.allclose(lowerCamelCase				,lowerCamelCase    )    )
      self.assertEqual(lowerCamelCase				,lowerCamelCase    )





   def      UpperCAmelCase__     (					self				:			List[str]    ):

      '''simple docstring'''

      __SCREAMING_SNAKE_CASE      							=       self.feature_extraction_class(**self.feat_extract_dict    )

      with tempfile.TemporaryDirectory() as tmpdirname:
         __SCREAMING_SNAKE_CASE      							=       os.path.join(lowerCamelCase				,"""feat_extract.json"""    )
         feat_extract_first.to_json_file(lowerCamelCase    )
         __SCREAMING_SNAKE_CASE      							=       self.feature_extraction_class.from_json_file(lowerCamelCase    )

      __SCREAMING_SNAKE_CASE      							=       feat_extract_first.to_dict()
      __SCREAMING_SNAKE_CASE      							=       feat_extract_second.to_dict()
      __SCREAMING_SNAKE_CASE      							=       dict_first.pop("""mel_filters"""    )
      __SCREAMING_SNAKE_CASE      							=       dict_second.pop("""mel_filters"""    )
      self.assertTrue(np.allclose(lowerCamelCase				,lowerCamelCase    )    )
      self.assertEqual(lowerCamelCase				,lowerCamelCase    )





   def      UpperCAmelCase__     (					self				:			int    ):

      '''simple docstring'''

      __SCREAMING_SNAKE_CASE      							=       self.feature_extraction_class(**self.feat_extract_dict    )

      # create three inputs of length 800, 1000, and 1200
      __SCREAMING_SNAKE_CASE      							=       [floats_list((1, x)    )[0] for x in range(800				,1400				,200    )]
      __SCREAMING_SNAKE_CASE      							=       [np.asarray(lowerCamelCase    ) for speech_input in speech_inputs]

      # Test not batched input
      __SCREAMING_SNAKE_CASE      							=       feature_extractor(np_speech_inputs[0]				,return_tensors="""np"""				,sampling_rate=4_4100    ).audio_values

      self.assertTrue(encoded_audios.ndim == 4    )
      self.assertTrue(encoded_audios.shape[-1] == feature_extractor.feature_size    )
      self.assertTrue(encoded_audios.shape[-2] <= feature_extractor.spectrogram_length    )
      self.assertTrue(encoded_audios.shape[-3] == feature_extractor.num_channels    )

      # Test batched
      __SCREAMING_SNAKE_CASE      							=       feature_extractor(lowerCamelCase				,return_tensors="""np"""				,sampling_rate=4_4100    ).audio_values

      self.assertTrue(encoded_audios.ndim == 4    )
      self.assertTrue(encoded_audios.shape[-1] == feature_extractor.feature_size    )
      self.assertTrue(encoded_audios.shape[-2] <= feature_extractor.spectrogram_length    )
      self.assertTrue(encoded_audios.shape[-3] == feature_extractor.num_channels    )

      # Test audio masking
      __SCREAMING_SNAKE_CASE      							=       feature_extractor(
          lowerCamelCase				,return_tensors="""np"""				,sampling_rate=4_4100				,mask_audio=lowerCamelCase    ).audio_values

      self.assertTrue(encoded_audios.ndim == 4    )
      self.assertTrue(encoded_audios.shape[-1] == feature_extractor.feature_size    )
      self.assertTrue(encoded_audios.shape[-2] <= feature_extractor.spectrogram_length    )
      self.assertTrue(encoded_audios.shape[-3] == feature_extractor.num_channels    )

      # Test 2-D numpy arrays are batched.
      __SCREAMING_SNAKE_CASE      							=       [floats_list((1, x)    )[0] for x in (800, 800, 800)]
      __SCREAMING_SNAKE_CASE      							=       np.asarray(lowerCamelCase    )
      __SCREAMING_SNAKE_CASE      							=       feature_extractor(lowerCamelCase				,return_tensors="""np"""				,sampling_rate=4_4100    ).audio_values
      self.assertTrue(encoded_audios.ndim == 4    )
      self.assertTrue(encoded_audios.shape[-1] == feature_extractor.feature_size    )
      self.assertTrue(encoded_audios.shape[-2] <= feature_extractor.spectrogram_length    )
      self.assertTrue(encoded_audios.shape[-3] == feature_extractor.num_channels    )





   def      UpperCAmelCase__     (					self				:			int				,lowerCamelCase				:			List[str]    ):

      '''simple docstring'''

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

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





   def      UpperCAmelCase__     (					self				:			List[str]    ):

      '''simple docstring'''

      __SCREAMING_SNAKE_CASE      							=       self._load_datasamples(1    )
      __SCREAMING_SNAKE_CASE      							=       TvltFeatureExtractor()
      __SCREAMING_SNAKE_CASE      							=       feature_extractor(lowerCamelCase				,return_tensors="""pt"""    ).audio_values

      self.assertEquals(audio_values.shape				,(1, 1, 192, 128)    )

      __SCREAMING_SNAKE_CASE      							=       torch.tensor([[-0.3_032, -0.2_708], [-0.4_434, -0.4_007]]    )
      self.assertTrue(torch.allclose(audio_values[0, 0, :2, :2]				,lowerCamelCase				,atol=1E-4    )    )












'''simple docstring'''

import functools
import operator

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


a        =						logging.get_logger(__name__)

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







class __a (   _snake_case			):
   __UpperCamelCase   : Tuple	      =	'sew'





   def __init__(					self				:			str				,lowerCamelCase				:			Any=32				,lowerCamelCase				:			str=768				,lowerCamelCase				:			str=12				,lowerCamelCase				:			Union[str, Any]=12				,lowerCamelCase				:			Union[str, Any]=3072				,lowerCamelCase				:			int=2				,lowerCamelCase				:			Union[str, Any]="gelu"				,lowerCamelCase				:			Tuple=0.1				,lowerCamelCase				:			Union[str, Any]=0.1				,lowerCamelCase				:			Union[str, Any]=0.1				,lowerCamelCase				:			Any=0.0				,lowerCamelCase				:			Optional[Any]=0.1				,lowerCamelCase				:			Union[str, Any]=0.1				,lowerCamelCase				:			Optional[Any]=0.02				,lowerCamelCase				:			List[str]=1E-5				,lowerCamelCase				:			Tuple="group"				,lowerCamelCase				:			Optional[Any]="gelu"				,lowerCamelCase				:			List[str]=(64, 128, 128, 128, 128, 256, 256, 256, 256, 512, 512, 512, 512)				,lowerCamelCase				:			Any=(5, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1)				,lowerCamelCase				:			Dict=(10, 3, 1, 3, 1, 3, 1, 3, 1, 2, 1, 2, 1)				,lowerCamelCase				:			Optional[int]=False				,lowerCamelCase				:			Dict=128				,lowerCamelCase				:			Union[str, Any]=16				,lowerCamelCase				:			List[Any]=True				,lowerCamelCase				:			List[Any]=0.05				,lowerCamelCase				:			Optional[int]=10				,lowerCamelCase				:			Any=2				,lowerCamelCase				:			Any=0.0				,lowerCamelCase				:			Tuple=10				,lowerCamelCase				:			str=0				,lowerCamelCase				:			Tuple="mean"				,lowerCamelCase				:			int=False				,lowerCamelCase				:			Dict=False				,lowerCamelCase				:			Optional[int]=256				,lowerCamelCase				:			str=0				,lowerCamelCase				:			Tuple=1				,lowerCamelCase				:			Tuple=2				,**lowerCamelCase				:			Union[str, Any]				,):

      '''simple docstring'''

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

      # ctc loss
      __SCREAMING_SNAKE_CASE      							=       ctc_loss_reduction
      __SCREAMING_SNAKE_CASE      							=       ctc_zero_infinity

      # sequence classification
      __SCREAMING_SNAKE_CASE      							=       use_weighted_layer_sum
      __SCREAMING_SNAKE_CASE      							=       classifier_proj_size





   @property
   def      UpperCAmelCase__     (					self				:			List[Any]    ):

      '''simple docstring'''

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





