


'''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


lowerCamelCase__    	=     random.Random()






def  __lowerCAmelCase (__lowerCAmelCase      , __lowerCAmelCase=1.0      , __lowerCAmelCase=None      , __lowerCAmelCase=None   ):
		if rng is None:
				_UpperCAmelCase		:       List[str]								=      global_rng

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

		return values






class 	lowerCAmelCase__			(			unittest.TestCase	):




			def __init__(		self  :     Optional[Any]	,  lowerCamelCase__  :     Dict	,  lowerCamelCase__  :     Any=7	,  lowerCamelCase__  :     int=4_00	,  lowerCamelCase__  :     Tuple=20_00	,  lowerCamelCase__  :     Optional[int]=1	,  lowerCamelCase__  :     str=0.0	,  lowerCamelCase__  :     Union[str, Any]=1_60_00	,  lowerCamelCase__  :     str=True	,  lowerCamelCase__  :     int=True	,  )   ->Dict:



					'''simple docstring'''







					_UpperCAmelCase		:       Dict								=      parent
					_UpperCAmelCase		:       List[str]								=      batch_size
					_UpperCAmelCase		:       Dict								=      min_seq_length
					_UpperCAmelCase		:       int								=      max_seq_length
					_UpperCAmelCase		:       Optional[int]								=      (self.max_seq_length - self.min_seq_length) // (self.batch_size - 1)
					_UpperCAmelCase		:       List[Any]								=      feature_size
					_UpperCAmelCase		:       int								=      padding_value
					_UpperCAmelCase		:       str								=      sampling_rate
					_UpperCAmelCase		:       List[str]								=      return_attention_mask
					_UpperCAmelCase		:       Union[str, Any]								=      do_normalize




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



					'''simple docstring'''







					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 							lowerCAmelCase__    (		self  :     Union[str, Any]	,  lowerCamelCase__  :     Optional[int]=False	,  lowerCamelCase__  :     Optional[Any]=False )   ->Optional[int]:



					'''simple docstring'''







					def _flatten(lowerCamelCase__  :     int ):
							return list(itertools.chain(*lowerCamelCase__ ) )

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

					return speech_inputs






class 	lowerCAmelCase__			(			UpperCAmelCase__    ,       unittest.TestCase	):
			lowerCAmelCase :	Optional[int]          =     WavaVecaFeatureExtractor




			def 							lowerCAmelCase__    (		self  :     Optional[Any] )   ->Dict:



					'''simple docstring'''







					_UpperCAmelCase		:       List[Any]								=      WavaVecaFeatureExtractionTester(self )




			def 							lowerCAmelCase__    (		self  :     Optional[int]	,  lowerCamelCase__  :     List[Any] )   ->Tuple:



					'''simple docstring'''







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




			def 							lowerCAmelCase__    (		self  :     Dict )   ->int:



					'''simple docstring'''







					_UpperCAmelCase		:       Union[str, Any]								=      self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict() )
					# create three inputs of length 800, 1000, and 1200
					_UpperCAmelCase		:       List[Any]								=      [floats_list((1, x) )[0] for x in range(8_00	,  14_00	,  2_00 )]
					_UpperCAmelCase		:       Optional[Any]								=      [np.asarray(lowerCamelCase__ ) for speech_input in speech_inputs]

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

					# Test batched
					_UpperCAmelCase		:       Dict								=      feat_extract(lowerCamelCase__	,  return_tensors="np" ).input_values
					_UpperCAmelCase		:       Optional[Any]								=      feat_extract(lowerCamelCase__	,  return_tensors="np" ).input_values
					for enc_seq_a, enc_seq_a in zip(lowerCamelCase__	,  lowerCamelCase__ ):
							self.assertTrue(np.allclose(lowerCamelCase__	,  lowerCamelCase__	,  atol=1E-3 ) )

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




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



					'''simple docstring'''







					_UpperCAmelCase		:       Optional[int]								=      self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict() )
					_UpperCAmelCase		:       Union[str, Any]								=      [floats_list((1, x) )[0] for x in range(8_00	,  14_00	,  2_00 )]

					_UpperCAmelCase		:       Optional[int]								=      ["longest", "max_length", "do_not_pad"]
					_UpperCAmelCase		:       int								=      [None, 16_00, None]
					for max_length, padding in zip(lowerCamelCase__	,  lowerCamelCase__ ):
							_UpperCAmelCase		:       Any								=      feat_extract(lowerCamelCase__	,  padding=lowerCamelCase__	,  max_length=lowerCamelCase__	,  return_tensors="np" )
							_UpperCAmelCase		:       Any								=      processed.input_values

							self._check_zero_mean_unit_variance(input_values[0][:8_00] )
							self.assertTrue(input_values[0][8_00:].sum() < 1E-6 )
							self._check_zero_mean_unit_variance(input_values[1][:10_00] )
							self.assertTrue(input_values[0][10_00:].sum() < 1E-6 )
							self._check_zero_mean_unit_variance(input_values[2][:12_00] )




			def 							lowerCAmelCase__    (		self  :     Union[str, Any] )   ->Optional[int]:



					'''simple docstring'''







					_UpperCAmelCase		:       int								=      self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict() )
					_UpperCAmelCase		:       int								=      range(8_00	,  14_00	,  2_00 )
					_UpperCAmelCase		:       Optional[int]								=      [floats_list((1, x) )[0] for x in lengths]

					_UpperCAmelCase		:       List[str]								=      ["longest", "max_length", "do_not_pad"]
					_UpperCAmelCase		:       List[Any]								=      [None, 16_00, None]

					for max_length, padding in zip(lowerCamelCase__	,  lowerCamelCase__ ):
							_UpperCAmelCase		:       Optional[int]								=      feat_extract(lowerCamelCase__	,  max_length=lowerCamelCase__	,  padding=lowerCamelCase__ )
							_UpperCAmelCase		:       str								=      processed.input_values

							self._check_zero_mean_unit_variance(input_values[0][:8_00] )
							self._check_zero_mean_unit_variance(input_values[1][:10_00] )
							self._check_zero_mean_unit_variance(input_values[2][:12_00] )




			def 							lowerCAmelCase__    (		self  :     Optional[int] )   ->Any:



					'''simple docstring'''







					_UpperCAmelCase		:       List[Any]								=      self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict() )
					_UpperCAmelCase		:       List[str]								=      [floats_list((1, x) )[0] for x in range(8_00	,  14_00	,  2_00 )]
					_UpperCAmelCase		:       List[Any]								=      feat_extract(
					    lowerCamelCase__	,  truncation=lowerCamelCase__	,  max_length=10_00	,  padding="max_length"	,  return_tensors="np" )
					_UpperCAmelCase		:       Optional[int]								=      processed.input_values

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




			def 							lowerCAmelCase__    (		self  :     Optional[Any] )   ->int:



					'''simple docstring'''







					_UpperCAmelCase		:       Tuple								=      self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict() )
					_UpperCAmelCase		:       Tuple								=      [floats_list((1, x) )[0] for x in range(8_00	,  14_00	,  2_00 )]
					_UpperCAmelCase		:       List[str]								=      feat_extract(
					    lowerCamelCase__	,  truncation=lowerCamelCase__	,  max_length=10_00	,  padding="longest"	,  return_tensors="np" )
					_UpperCAmelCase		:       Optional[int]								=      processed.input_values

					self._check_zero_mean_unit_variance(input_values[0, :8_00] )
					self._check_zero_mean_unit_variance(input_values[1, :10_00] )
					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, 10_00) )

					_UpperCAmelCase		:       int								=      [floats_list((1, x) )[0] for x in range(8_00	,  14_00	,  2_00 )]
					_UpperCAmelCase		:       Dict								=      feat_extract(
					    lowerCamelCase__	,  truncation=lowerCamelCase__	,  max_length=20_00	,  padding="longest"	,  return_tensors="np" )
					_UpperCAmelCase		:       Optional[int]								=      processed.input_values

					self._check_zero_mean_unit_variance(input_values[0, :8_00] )
					self._check_zero_mean_unit_variance(input_values[1, :10_00] )
					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, 12_00) )




			@require_torch
			def 							lowerCAmelCase__    (		self  :     Optional[Any] )   ->Optional[Any]:



					'''simple docstring'''







					import torch

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

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






			@slow
			@require_torch
			def 							lowerCAmelCase__    (		self  :     int )   ->Dict:



					'''simple docstring'''







					for model_id in WAV_2_VEC_2_PRETRAINED_MODEL_ARCHIVE_LIST:
							_UpperCAmelCase		:       List[Any]								=      WavaVecaConfig.from_pretrained(lowerCamelCase__ )
							_UpperCAmelCase		:       List[str]								=      WavaVecaFeatureExtractor.from_pretrained(lowerCamelCase__ )

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


def  __lowerCAmelCase ():
		_UpperCAmelCase		:       str								=      0
		for i in range(1      , 1_001   ):
				total += i**i
		return str(__lowerCAmelCase   )[-10:]


if __name__ == "__main__":
						print(solution())



