


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        snake_case_					(  snake_case      ,							snake_case=1.0      ,							snake_case=None      ,							snake_case=None					)  ->    int:
       if rng is None:
              lowercase__:       Tuple        =       global_rng

       lowercase__:       str        =       []
       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    ,			lowerCAmelCase__    ,			lowerCAmelCase__=7    ,			lowerCAmelCase__=400    ,			lowerCAmelCase__=2_000    ,			lowerCAmelCase__=1    ,			lowerCAmelCase__=0.0    ,			lowerCAmelCase__=16_000    ,			lowerCAmelCase__=True    ,			lowerCAmelCase__=True    ,			) ->    Union[str, Any]:




        '''simple docstring'''
        lowercase__:       List[str]        =       parent
        lowercase__:       str        =       batch_size
        lowercase__:       Any        =       min_seq_length
        lowercase__:       List[Any]        =       max_seq_length
        lowercase__:       Any        =       (self.max_seq_length - self.min_seq_length) // (self.batch_size - 1)
        lowercase__:       Dict        =       feature_size
        lowercase__:       Tuple        =       padding_value
        lowercase__:       Union[str, Any]        =       sampling_rate
        lowercase__:       int        =       return_attention_mask
        lowercase__:       Any        =       do_normalize
 def 		SCREAMING_SNAKE_CASE__       (       self			) ->    List[str]:




        '''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 		SCREAMING_SNAKE_CASE__       (       self    ,			lowerCAmelCase__=False    ,			lowerCAmelCase__=False			) ->    int:




        '''simple docstring'''
        def _flatten(lowerCAmelCase__			):
               return list(itertools.chain(*lowerCAmelCase__			)			)

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

        return speech_inputs





class   __a      (   __UpperCamelCase	,	unittest.TestCase				):
 __lowercase			:							str       					=			WavaVecaFeatureExtractor
 def 		SCREAMING_SNAKE_CASE__       (       self			) ->    Tuple:




        '''simple docstring'''
        lowercase__:       int        =       WavaVecaFeatureExtractionTester(self			)
 def 		SCREAMING_SNAKE_CASE__       (       self    ,			lowerCAmelCase__			) ->    List[Any]:




        '''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 		SCREAMING_SNAKE_CASE__       (       self			) ->    Optional[int]:




        '''simple docstring'''
        lowercase__:       Union[str, Any]        =       self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()			)
        # create three inputs of length 800, 1000, and 1200
        lowercase__:       int        =       [floats_list((1, x)			)[0] for x in range(800    ,			1_400    ,			200			)]
        lowercase__:       Union[str, Any]        =       [np.asarray(lowerCAmelCase__			) for speech_input in speech_inputs]

        # Test not batched input
        lowercase__:       Optional[Any]        =       feat_extract(speech_inputs[0]    ,			return_tensors='np'			).input_values
        lowercase__:       Tuple        =       feat_extract(np_speech_inputs[0]    ,			return_tensors='np'			).input_values
        self.assertTrue(np.allclose(lowerCAmelCase__    ,			lowerCAmelCase__    ,			atol=1E-3			)			)

        # Test batched
        lowercase__:       List[Any]        =       feat_extract(lowerCAmelCase__    ,			return_tensors='np'			).input_values
        lowercase__:       List[str]        =       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.
        lowercase__:       Optional[int]        =       [floats_list((1, x)			)[0] for x in (800, 800, 800)]
        lowercase__:       int        =       np.asarray(lowerCAmelCase__			)
        lowercase__:       str        =       feat_extract(lowerCAmelCase__    ,			return_tensors='np'			).input_values
        lowercase__:       Dict        =       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 		SCREAMING_SNAKE_CASE__       (       self			) ->    str:




        '''simple docstring'''
        lowercase__:       Optional[int]        =       self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()			)
        lowercase__:       Any        =       [floats_list((1, x)			)[0] for x in range(800    ,			1_400    ,			200			)]

        lowercase__:       Tuple        =       ['longest', 'max_length', 'do_not_pad']
        lowercase__:       Optional[int]        =       [None, 1_600, None]
        for max_length, padding in zip(lowerCAmelCase__    ,			lowerCAmelCase__			):
               lowercase__:       Dict        =       feat_extract(lowerCAmelCase__    ,			padding=lowerCAmelCase__    ,			max_length=lowerCAmelCase__    ,			return_tensors='np'			)
               lowercase__:       str        =       processed.input_values

               self._check_zero_mean_unit_variance(input_values[0][:800]			)
               self.assertTrue(input_values[0][800:].sum() < 1E-6			)
               self._check_zero_mean_unit_variance(input_values[1][:1_000]			)
               self.assertTrue(input_values[0][1_000:].sum() < 1E-6			)
               self._check_zero_mean_unit_variance(input_values[2][:1_200]			)
 def 		SCREAMING_SNAKE_CASE__       (       self			) ->    Dict:




        '''simple docstring'''
        lowercase__:       Dict        =       self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()			)
        lowercase__:       List[Any]        =       range(800    ,			1_400    ,			200			)
        lowercase__:       List[str]        =       [floats_list((1, x)			)[0] for x in lengths]

        lowercase__:       Union[str, Any]        =       ['longest', 'max_length', 'do_not_pad']
        lowercase__:       Tuple        =       [None, 1_600, None]

        for max_length, padding in zip(lowerCAmelCase__    ,			lowerCAmelCase__			):
               lowercase__:       List[str]        =       feat_extract(lowerCAmelCase__    ,			max_length=lowerCAmelCase__    ,			padding=lowerCAmelCase__			)
               lowercase__:       Tuple        =       processed.input_values

               self._check_zero_mean_unit_variance(input_values[0][:800]			)
               self._check_zero_mean_unit_variance(input_values[1][:1_000]			)
               self._check_zero_mean_unit_variance(input_values[2][:1_200]			)
 def 		SCREAMING_SNAKE_CASE__       (       self			) ->    Union[str, Any]:




        '''simple docstring'''
        lowercase__:       Union[str, Any]        =       self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()			)
        lowercase__:       Optional[int]        =       [floats_list((1, x)			)[0] for x in range(800    ,			1_400    ,			200			)]
        lowercase__:       List[Any]        =       feat_extract(
            lowerCAmelCase__    ,			truncation=lowerCAmelCase__    ,			max_length=1_000    ,			padding='max_length'    ,			return_tensors='np'			)
        lowercase__:       List[str]        =       processed.input_values

        self._check_zero_mean_unit_variance(input_values[0, :800]			)
        self._check_zero_mean_unit_variance(input_values[1]			)
        self._check_zero_mean_unit_variance(input_values[2]			)
 def 		SCREAMING_SNAKE_CASE__       (       self			) ->    Optional[int]:




        '''simple docstring'''
        lowercase__:       Tuple        =       self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()			)
        lowercase__:       Tuple        =       [floats_list((1, x)			)[0] for x in range(800    ,			1_400    ,			200			)]
        lowercase__:       int        =       feat_extract(
            lowerCAmelCase__    ,			truncation=lowerCAmelCase__    ,			max_length=1_000    ,			padding='longest'    ,			return_tensors='np'			)
        lowercase__:       Optional[int]        =       processed.input_values

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

        lowercase__:       Optional[Any]        =       [floats_list((1, x)			)[0] for x in range(800    ,			1_400    ,			200			)]
        lowercase__:       Optional[Any]        =       feat_extract(
            lowerCAmelCase__    ,			truncation=lowerCAmelCase__    ,			max_length=2_000    ,			padding='longest'    ,			return_tensors='np'			)
        lowercase__:       Any        =       processed.input_values

        self._check_zero_mean_unit_variance(input_values[0, :800]			)
        self._check_zero_mean_unit_variance(input_values[1, :1_000]			)
        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_200)			)
 @require_torch
 def 		SCREAMING_SNAKE_CASE__       (       self			) ->    int:




        '''simple docstring'''
        import torch

        lowercase__:       Tuple        =       self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()			)
        lowercase__:       Union[str, Any]        =       np.random.rand(100			).astype(np.floataa			)
        lowercase__:       str        =       np_speech_inputs.tolist()

        for inputs in [py_speech_inputs, np_speech_inputs]:
               lowercase__:       Optional[int]        =       feature_extractor.pad([{'input_values': inputs}]    ,			return_tensors='np'			)
               self.assertTrue(np_processed.input_values.dtype == np.floataa			)
               lowercase__:       List[str]        =       feature_extractor.pad([{'input_values': inputs}]    ,			return_tensors='pt'			)
               self.assertTrue(pt_processed.input_values.dtype == torch.floataa			)
 @slow
 @require_torch
 def 		SCREAMING_SNAKE_CASE__       (       self			) ->    str:




        '''simple docstring'''
        # 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:
               lowercase__:       List[Any]        =       WavaVecaConfig.from_pretrained(lowerCAmelCase__			)
               lowercase__:       Union[str, Any]        =       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'			)






from __future__ import annotations




def        snake_case_					(  snake_case      ,							snake_case					)  ->    list[int]:
       lowercase__:       Tuple        =       0
       lowercase__:       str        =       len(snake_case					) - 1

       while i < j:
              if nums[i] + nums[j] == target:
                     return [i, j]
              elif nums[i] + nums[j] < target:
                     lowercase__:       str        =       i + 1
              else:
                     lowercase__:       Dict        =       j - 1

       return []


if __name__ == "__main__":
 import doctest

 doctest.testmod()
 print(F'''{two_pointer([2, 7, 11, 15], 9) = }''')


