



"""simple docstring"""


import itertools
import random
import unittest

import numpy as np

from transformers import WAV_2_VEC_2_PRETRAINED_MODEL_ARCHIVE_LIST, WavaVecaConfig, WavaVecaFeatureExtractor
from transformers.testing_utils import require_torch, slow

from ...test_sequence_feature_extraction_common import SequenceFeatureExtractionTestMixin


__A      :							List[Any]   =      random.Random()







def        lowercase		(					_SCREAMING_SNAKE_CASE      :  List[Any]    ,   _SCREAMING_SNAKE_CASE      :  Dict=1.0    ,   _SCREAMING_SNAKE_CASE      :  Dict=None    ,   _SCREAMING_SNAKE_CASE      :  str=None ):






				'''simple docstring'''



				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
class 	_a       (    unittest.TestCase):
			"""simple docstring"""



			def __init__(						self   :			List[str] ,      __UpperCamelCase   :			List[Any] ,      __UpperCamelCase   :			Optional[Any]=7 ,      __UpperCamelCase   :			str=4_0_0 ,      __UpperCamelCase   :			List[Any]=2_0_0_0 ,      __UpperCamelCase   :			Dict=1 ,      __UpperCamelCase   :			Dict=0.0 ,      __UpperCamelCase   :			List[Any]=1_6_0_0_0 ,      __UpperCamelCase   :			Union[str, Any]=True ,      __UpperCamelCase   :			Tuple=True ,      )->List[Any]:
							_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							=			padding_value
							_UpperCAmelCase							=			sampling_rate
							_UpperCAmelCase							=			return_attention_mask
							_UpperCAmelCase							=			do_normalize



			def        lowercase__  (						self   :			Any      )->List[Any]:
							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   :			Optional[int] ,      __UpperCamelCase   :			Optional[int]=False ,      __UpperCamelCase   :			Optional[int]=False      )->Dict:
							def _flatten(__UpperCamelCase   :			Tuple      ):
											return list(itertools.chain(*UpperCAmelCase__      )      )

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

							return speech_inputs



class 	_a       (    __lowercase	,							unittest.TestCase):
			"""simple docstring"""
			UpperCamelCase__						=		WavaVecaFeatureExtractor



			def        lowercase__  (						self   :			Optional[int]      )->Optional[Any]:
							_UpperCAmelCase							=			WavaVecaFeatureExtractionTester(self      )



			def        lowercase__  (						self   :			Union[str, Any] ,      __UpperCamelCase   :			Optional[Any]      )->Union[str, Any]:
							self.assertTrue(np.all(np.mean(UpperCAmelCase__ ,      axis=0      ) < 1e-3      )      )
							self.assertTrue(np.all(np.abs(np.var(UpperCAmelCase__ ,      axis=0      ) - 1      ) < 1e-3      )      )



			def        lowercase__  (						self   :			List[str]      )->Optional[int]:
							# Tests that all call wrap to encode_plus and batch_encode_plus
							_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(8_0_0 ,      1_4_0_0 ,      2_0_0      )]
							_UpperCAmelCase							=			[np.asarray(UpperCAmelCase__      ) for speech_input in speech_inputs]

							# Test not batched input
							_UpperCAmelCase							=			feat_extract(speech_inputs[0] ,      return_tensors='''np'''      ).input_values
							_UpperCAmelCase							=			feat_extract(np_speech_inputs[0] ,      return_tensors='''np'''      ).input_values
							self.assertTrue(np.allclose(UpperCAmelCase__ ,      UpperCAmelCase__ ,      atol=1e-3      )      )

							# Test batched
							_UpperCAmelCase							=			feat_extract(UpperCAmelCase__ ,      return_tensors='''np'''      ).input_values
							_UpperCAmelCase							=			feat_extract(UpperCAmelCase__ ,      return_tensors='''np'''      ).input_values
							for enc_seq_a, enc_seq_a in zip(UpperCAmelCase__ ,      UpperCAmelCase__      ):
											self.assertTrue(np.allclose(UpperCAmelCase__ ,      UpperCAmelCase__ ,      atol=1e-3      )      )

							# Test 2-D numpy arrays are batched.
							_UpperCAmelCase							=			[floats_list((1, x)      )[0] for x in (8_0_0, 8_0_0, 8_0_0)]
							_UpperCAmelCase							=			np.asarray(UpperCAmelCase__      )
							_UpperCAmelCase							=			feat_extract(UpperCAmelCase__ ,      return_tensors='''np'''      ).input_values
							_UpperCAmelCase							=			feat_extract(UpperCAmelCase__ ,      return_tensors='''np'''      ).input_values
							for enc_seq_a, enc_seq_a in zip(UpperCAmelCase__ ,      UpperCAmelCase__      ):
											self.assertTrue(np.allclose(UpperCAmelCase__ ,      UpperCAmelCase__ ,      atol=1e-3      )      )



			def        lowercase__  (						self   :			Optional[int]      )->List[str]:
							_UpperCAmelCase							=			self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()      )
							_UpperCAmelCase							=			[floats_list((1, x)      )[0] for x in range(8_0_0 ,      1_4_0_0 ,      2_0_0      )]

							_UpperCAmelCase							=			['''longest''', '''max_length''', '''do_not_pad''']
							_UpperCAmelCase							=			[None, 1_6_0_0, None]
							for max_length, padding in zip(UpperCAmelCase__ ,      UpperCAmelCase__      ):
											_UpperCAmelCase							=			feat_extract(UpperCAmelCase__ ,      padding=UpperCAmelCase__ ,      max_length=UpperCAmelCase__ ,      return_tensors='''np'''      )
											_UpperCAmelCase							=			processed.input_values

											self._check_zero_mean_unit_variance(input_values[0][:8_0_0]      )
											self.assertTrue(input_values[0][8_0_0:].sum() < 1e-6      )
											self._check_zero_mean_unit_variance(input_values[1][:1_0_0_0]      )
											self.assertTrue(input_values[0][1_0_0_0:].sum() < 1e-6      )
											self._check_zero_mean_unit_variance(input_values[2][:1_2_0_0]      )



			def        lowercase__  (						self   :			int      )->str:
							_UpperCAmelCase							=			self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()      )
							_UpperCAmelCase							=			range(8_0_0 ,      1_4_0_0 ,      2_0_0      )
							_UpperCAmelCase							=			[floats_list((1, x)      )[0] for x in lengths]

							_UpperCAmelCase							=			['''longest''', '''max_length''', '''do_not_pad''']
							_UpperCAmelCase							=			[None, 1_6_0_0, None]

							for max_length, padding in zip(UpperCAmelCase__ ,      UpperCAmelCase__      ):
											_UpperCAmelCase							=			feat_extract(UpperCAmelCase__ ,      max_length=UpperCAmelCase__ ,      padding=UpperCAmelCase__      )
											_UpperCAmelCase							=			processed.input_values

											self._check_zero_mean_unit_variance(input_values[0][:8_0_0]      )
											self._check_zero_mean_unit_variance(input_values[1][:1_0_0_0]      )
											self._check_zero_mean_unit_variance(input_values[2][:1_2_0_0]      )



			def        lowercase__  (						self   :			Optional[Any]      )->Tuple:
							_UpperCAmelCase							=			self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()      )
							_UpperCAmelCase							=			[floats_list((1, x)      )[0] for x in range(8_0_0 ,      1_4_0_0 ,      2_0_0      )]
							_UpperCAmelCase							=			feat_extract(
							    UpperCAmelCase__ ,      truncation=UpperCAmelCase__ ,      max_length=1_0_0_0 ,      padding='''max_length''' ,      return_tensors='''np'''      )
							_UpperCAmelCase							=			processed.input_values

							self._check_zero_mean_unit_variance(input_values[0, :8_0_0]      )
							self._check_zero_mean_unit_variance(input_values[1]      )
							self._check_zero_mean_unit_variance(input_values[2]      )



			def        lowercase__  (						self   :			Any      )->Tuple:
							_UpperCAmelCase							=			self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()      )
							_UpperCAmelCase							=			[floats_list((1, x)      )[0] for x in range(8_0_0 ,      1_4_0_0 ,      2_0_0      )]
							_UpperCAmelCase							=			feat_extract(
							    UpperCAmelCase__ ,      truncation=UpperCAmelCase__ ,      max_length=1_0_0_0 ,      padding='''longest''' ,      return_tensors='''np'''      )
							_UpperCAmelCase							=			processed.input_values

							self._check_zero_mean_unit_variance(input_values[0, :8_0_0]      )
							self._check_zero_mean_unit_variance(input_values[1, :1_0_0_0]      )
							self._check_zero_mean_unit_variance(input_values[2]      )

							# make sure that if max_length < longest -> then pad to max_length
							self.assertTrue(input_values.shape == (3, 1_0_0_0)      )

							_UpperCAmelCase							=			[floats_list((1, x)      )[0] for x in range(8_0_0 ,      1_4_0_0 ,      2_0_0      )]
							_UpperCAmelCase							=			feat_extract(
							    UpperCAmelCase__ ,      truncation=UpperCAmelCase__ ,      max_length=2_0_0_0 ,      padding='''longest''' ,      return_tensors='''np'''      )
							_UpperCAmelCase							=			processed.input_values

							self._check_zero_mean_unit_variance(input_values[0, :8_0_0]      )
							self._check_zero_mean_unit_variance(input_values[1, :1_0_0_0]      )
							self._check_zero_mean_unit_variance(input_values[2]      )

							# make sure that if max_length > longest -> then pad to longest
							self.assertTrue(input_values.shape == (3, 1_2_0_0)      )



			@require_torch
			def        lowercase__  (						self   :			Optional[Any]      )->List[Any]:
							import torch

							_UpperCAmelCase							=			self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()      )
							_UpperCAmelCase							=			np.random.rand(1_0_0      ).astype(np.floataa      )
							_UpperCAmelCase							=			np_speech_inputs.tolist()

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



			@slow
			@require_torch
			def        lowercase__  (						self   :			Any      )->Optional[int]:
							# this test makes sure that models that are using
							# group norm don't have their feature extractor return the
							# attention_mask
							for model_id in WAV_2_VEC_2_PRETRAINED_MODEL_ARCHIVE_LIST:
											_UpperCAmelCase							=			WavaVecaConfig.from_pretrained(UpperCAmelCase__      )
											_UpperCAmelCase							=			WavaVecaFeatureExtractor.from_pretrained(UpperCAmelCase__      )

											# only "layer" feature extraction norm should make use of
											# attention_mask
											self.assertEqual(feat_extract.return_attention_mask ,      config.feat_extract_norm == '''layer'''      )







'''simple docstring'''




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


__snake_case    						=random.Random()

if is_torch_available():
       import torch





def 	a_					( lowerCamelCase	:     Dict	, lowerCamelCase	:     Dict=1.0	, lowerCamelCase	:     List[Any]=None	, lowerCamelCase	:     Union[str, Any]=None				):
   if rng is None:
      lowerCAmelCase     =			global_rng

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

   return values

class 		UpperCAmelCase_      (  unittest.TestCase		):





     def __init__( self       :      List[Any]    ,      UpperCAmelCase__       :      Optional[Any]    ,      UpperCAmelCase__       :      List[str]=7    ,      UpperCAmelCase__       :      int=4_0_0    ,      UpperCAmelCase__       :      int=2_0_0_0    ,      UpperCAmelCase__       :      List[str]=1    ,      UpperCAmelCase__       :      Tuple=0.0    ,      UpperCAmelCase__       :      Tuple=1_6_0_0_0    ,      UpperCAmelCase__       :      Tuple=True    ,      UpperCAmelCase__       :      Union[str, Any]=True    ,      )				->  Any:
        lowerCAmelCase     =			parent
        lowerCAmelCase     =			batch_size
        lowerCAmelCase     =			min_seq_length
        lowerCAmelCase     =			max_seq_length
        lowerCAmelCase     =			(self.max_seq_length - self.min_seq_length) // (self.batch_size - 1)
        lowerCAmelCase     =			feature_size
        lowerCAmelCase     =			padding_value
        lowerCAmelCase     =			sampling_rate
        lowerCAmelCase     =			return_attention_mask
        lowerCAmelCase     =			do_normalize





     def 							__UpperCAmelCase      ( self       :      Optional[Any]    )				->  List[str]:
        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 							__UpperCAmelCase      ( self       :      str    ,      UpperCAmelCase__       :      Union[str, Any]=False    ,      UpperCAmelCase__       :      Union[str, Any]=False    )				->  Optional[Any]:
        def _flatten(UpperCAmelCase__       :      int    ):
           return list(itertools.chain(*UpperCAmelCase__    )    )

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

        return speech_inputs




@require_torch
@require_torchaudio
class 		UpperCAmelCase_      (  __lowercase       ,	unittest.TestCase		):
     lowerCamelCase				:	Dict		=     ASTFeatureExtractor





     def 							__UpperCAmelCase      ( self       :      str    )				->  Optional[int]:
        lowerCAmelCase     =			ASTFeatureExtractionTester(self    )





     def 							__UpperCAmelCase      ( self       :      Optional[int]    )				->  Optional[int]:
        # Tests that all call wrap to encode_plus and batch_encode_plus
        lowerCAmelCase     =			self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()    )
        # create three inputs of length 800, 1000, and 1200
        lowerCAmelCase     =			[floats_list((1, x)    )[0] for x in range(8_0_0    ,      1_4_0_0    ,      2_0_0    )]
        lowerCAmelCase     =			[np.asarray(UpperCAmelCase__    ) for speech_input in speech_inputs]

        # Test not batched input
        lowerCAmelCase     =			feat_extract(speech_inputs[0]    ,      return_tensors='np'    ).input_values
        lowerCAmelCase     =			feat_extract(np_speech_inputs[0]    ,      return_tensors='np'    ).input_values
        self.assertTrue(np.allclose(UpperCAmelCase__    ,      UpperCAmelCase__    ,      atol=1E-3    )    )

        # Test batched
        lowerCAmelCase     =			feat_extract(UpperCAmelCase__    ,      padding=UpperCAmelCase__    ,      return_tensors='np'    ).input_values
        lowerCAmelCase     =			feat_extract(UpperCAmelCase__    ,      padding=UpperCAmelCase__    ,      return_tensors='np'    ).input_values
        for enc_seq_a, enc_seq_a in zip(UpperCAmelCase__    ,      UpperCAmelCase__    ):
           self.assertTrue(np.allclose(UpperCAmelCase__    ,      UpperCAmelCase__    ,      atol=1E-3    )    )

        # Test 2-D numpy arrays are batched.
        lowerCAmelCase     =			[floats_list((1, x)    )[0] for x in (8_0_0, 8_0_0, 8_0_0)]
        lowerCAmelCase     =			np.asarray(UpperCAmelCase__    )
        lowerCAmelCase     =			feat_extract(UpperCAmelCase__    ,      return_tensors='np'    ).input_values
        lowerCAmelCase     =			feat_extract(UpperCAmelCase__    ,      return_tensors='np'    ).input_values
        for enc_seq_a, enc_seq_a in zip(UpperCAmelCase__    ,      UpperCAmelCase__    ):
           self.assertTrue(np.allclose(UpperCAmelCase__    ,      UpperCAmelCase__    ,      atol=1E-3    )    )





     @require_torch
     def 							__UpperCAmelCase      ( self       :      Union[str, Any]    )				->  Optional[int]:
        import torch

        lowerCAmelCase     =			self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()    )
        lowerCAmelCase     =			np.random.rand(1_0_0    ).astype(np.floataa    )
        lowerCAmelCase     =			np_speech_inputs.tolist()

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





     def 							__UpperCAmelCase      ( self       :      int    ,      UpperCAmelCase__       :      str    )				->  Tuple:
        from datasets import load_dataset

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

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





     @require_torch
     def 							__UpperCAmelCase      ( self       :      str    )				->  Optional[Any]:
        # fmt: off
        lowerCAmelCase     =			torch.tensor(
            [-0.9_894, -1.2_776, -0.9_066, -1.2_776, -0.9_349, -1.2_609, -1.0_386, -1.2_776,
             -1.1_561, -1.2_776, -1.2_052, -1.2_723, -1.2_190, -1.2_132, -1.2_776, -1.1_133,
             -1.1_953, -1.1_343, -1.1_584, -1.2_203, -1.1_770, -1.2_474, -1.2_381, -1.1_936,
             -0.9_270, -0.8_317, -0.8_049, -0.7_706, -0.7_565, -0.7_869]    )
        # fmt: on

        lowerCAmelCase     =			self._load_datasamples(1    )
        lowerCAmelCase     =			ASTFeatureExtractor()
        lowerCAmelCase     =			feature_extractor(UpperCAmelCase__    ,      return_tensors='pt'    ).input_values
        self.assertEquals(input_values.shape    ,      (1, 1_0_2_4, 1_2_8)    )
        self.assertTrue(torch.allclose(input_values[0, 0, :3_0]    ,      UpperCAmelCase__    ,      atol=1E-4    )    )





