





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


__SCREAMING_SNAKE_CASE     :List[str]       =				random.Random()
def 	UpperCAmelCase_							(					__lowercase							:      int			,     __lowercase							:      List[str]=1.0			,     __lowercase							:      Optional[Any]=None			,     __lowercase							:      str=None       )		->       Optional[int]:


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




 def __init__(					self       :    List[Any]	,			snake_case_       :    Optional[Any]	,			snake_case_       :    int=7	,			snake_case_       :    Optional[int]=4_0_0	,			snake_case_       :    Optional[int]=2_0_0_0	,			snake_case_       :    int=1	,			snake_case_       :    Union[str, Any]=0.0	,			snake_case_       :    Any=1_6_0_0_0	,			snake_case_       :    Optional[int]=True	,			snake_case_       :    List[Any]=True	,			):
      _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       :    Optional[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       :    Dict	,			snake_case_       :    Any=False	,			snake_case_       :    List[Any]=False    ):





      def _flatten(snake_case_       :    Any    ):
           return list(itertools.chain(*snake_case_    )    )

      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(snake_case_    ) for x in speech_inputs]

      return speech_inputs







class   A_	(  __lowerCAmelCase							,						unittest.TestCase			):
 _lowerCamelCase  : Any           =							WavaVecaFeatureExtractor




 def      lowercase	(					self       :    Dict    ):
      _UpperCAmelCase     							=   WavaVecaFeatureExtractionTester(self    )




 def      lowercase	(					self       :    Optional[Any]	,			snake_case_       :    Optional[int]    ):
      self.assertTrue(np.all(np.mean(snake_case_	,			axis=0    ) < 1e-3    )    )
      self.assertTrue(np.all(np.abs(np.var(snake_case_	,			axis=0    ) - 1    ) < 1e-3    )    )




 def      lowercase	(					self       :    str    ):
      _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(snake_case_    ) 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(snake_case_	,			snake_case_	,			atol=1e-3    )    )

      # Test batched
      _UpperCAmelCase     							=   feat_extract(snake_case_	,			return_tensors="np"    ).input_values
      _UpperCAmelCase     							=   feat_extract(snake_case_	,			return_tensors="np"    ).input_values
      for enc_seq_a, enc_seq_a in zip(snake_case_	,			snake_case_    ):
           self.assertTrue(np.allclose(snake_case_	,			snake_case_	,			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(snake_case_    )
      _UpperCAmelCase     							=   feat_extract(snake_case_	,			return_tensors="np"    ).input_values
      _UpperCAmelCase     							=   feat_extract(snake_case_	,			return_tensors="np"    ).input_values
      for enc_seq_a, enc_seq_a in zip(snake_case_	,			snake_case_    ):
           self.assertTrue(np.allclose(snake_case_	,			snake_case_	,			atol=1e-3    )    )




 def      lowercase	(					self       :    List[Any]    ):
      _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(snake_case_	,			snake_case_    ):
           _UpperCAmelCase     							=   feat_extract(snake_case_	,			padding=snake_case_	,			max_length=snake_case_	,			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       :    Dict    ):
      _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(snake_case_	,			snake_case_    ):
           _UpperCAmelCase     							=   feat_extract(snake_case_	,			max_length=snake_case_	,			padding=snake_case_    )
           _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       :    Union[str, Any]    ):
      _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(
          snake_case_	,			truncation=snake_case_	,			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       :    Union[str, Any]    ):
      _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(
          snake_case_	,			truncation=snake_case_	,			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(
          snake_case_	,			truncation=snake_case_	,			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       :    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       :    Optional[Any]    ):





      for model_id in WAV_2_VEC_2_PRETRAINED_MODEL_ARCHIVE_LIST:
           _UpperCAmelCase     							=   WavaVecaConfig.from_pretrained(snake_case_    )
           _UpperCAmelCase     							=   WavaVecaFeatureExtractor.from_pretrained(snake_case_    )

           # 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 						__a   (	_SCREAMING_SNAKE_CASE		,     _SCREAMING_SNAKE_CASE   )	->int:
  while a != 0:
    a__							,    a__:     List[str]										=	b % a, a
  return b







def 						__a   (	_SCREAMING_SNAKE_CASE		,     _SCREAMING_SNAKE_CASE   )	->int:
  if gcd(_SCREAMING_SNAKE_CASE		,     _SCREAMING_SNAKE_CASE   ) != 1:
    a__:     Dict										=	F'mod inverse of {a!r} and {m!r} does not exist'
    raise ValueError(_SCREAMING_SNAKE_CASE   )
  a__							,    a__							,    a__:     Union[str, Any]										=	1, 0, a
  a__							,    a__							,    a__:     Any										=	0, 1, m
  while va != 0:
    a__:     int										=	ua // va
    a__							,    a__							,    a__							,    a__							,    a__							,    a__:     Any										=	(ua - q * va), (ua - q * va), (ua - q * va), va, va, va
  return ua % m


