





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__: Dict							   =						random.Random()


def       UpperCamelCase__(			UpperCamelCase__     :			Optional[int]					,	UpperCamelCase__     :			Union[str, Any]=1.0					,	UpperCamelCase__     :			List[Any]=None					,	UpperCamelCase__     :			str=None    )->Tuple:
       if rng is None:
              A__		   =		global_rng

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

       return values




class        SCREAMING_SNAKE_CASE__					(		unittest.TestCase ):



 def __init__(				self,__lowerCamelCase,__lowerCamelCase=7,__lowerCamelCase=400,__lowerCamelCase=2000,__lowerCamelCase=1,__lowerCamelCase=0.0,__lowerCamelCase=1_6000,__lowerCamelCase=True,__lowerCamelCase=True,):
        A__		   =		parent
        A__		   =		batch_size
        A__		   =		min_seq_length
        A__		   =		max_seq_length
        A__		   =		(self.max_seq_length - self.min_seq_length) // (self.batch_size - 1)
        A__		   =		feature_size
        A__		   =		padding_value
        A__		   =		sampling_rate
        A__		   =		return_attention_mask
        A__		   =		do_normalize



 def 							UpperCamelCase     (				self					):
        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,__lowerCamelCase=False,__lowerCamelCase=False					):
        def _flatten(__lowerCamelCase					):
               return list(itertools.chain(*__lowerCamelCase					)					)

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

        return speech_inputs




class        SCREAMING_SNAKE_CASE__					(		UpperCamelCase__							,   unittest.TestCase ):
 __SCREAMING_SNAKE_CASE							=      WavaVecaFeatureExtractor



 def 							UpperCamelCase     (				self					):
        A__		   =		WavaVecaFeatureExtractionTester(self					)



 def 							UpperCamelCase     (				self,__lowerCamelCase					):
        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 							UpperCamelCase     (				self					):
        # Tests that all call wrap to encode_plus and batch_encode_plus
        A__		   =		self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()					)
        # create three inputs of length 800, 1000, and 1200
        A__		   =		[floats_list((1, x)					)[0] for x in range(800,1400,200					)]
        A__		   =		[np.asarray(__lowerCamelCase					) for speech_input in speech_inputs]

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

        # Test batched
        A__		   =		feat_extract(__lowerCamelCase,return_tensors='''np'''					).input_values
        A__		   =		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.
        A__		   =		[floats_list((1, x)					)[0] for x in (800, 800, 800)]
        A__		   =		np.asarray(__lowerCamelCase					)
        A__		   =		feat_extract(__lowerCamelCase,return_tensors='''np'''					).input_values
        A__		   =		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 							UpperCamelCase     (				self					):
        A__		   =		self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()					)
        A__		   =		[floats_list((1, x)					)[0] for x in range(800,1400,200					)]

        A__		   =		['''longest''', '''max_length''', '''do_not_pad''']
        A__		   =		[None, 1600, None]
        for max_length, padding in zip(__lowerCamelCase,__lowerCamelCase					):
               A__		   =		feat_extract(__lowerCamelCase,padding=__lowerCamelCase,max_length=__lowerCamelCase,return_tensors='''np'''					)
               A__		   =		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][:1000]					)
               self.assertTrue(input_values[0][1000:].sum() < 1E-6					)
               self._check_zero_mean_unit_variance(input_values[2][:1200]					)



 def 							UpperCamelCase     (				self					):
        A__		   =		self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()					)
        A__		   =		range(800,1400,200					)
        A__		   =		[floats_list((1, x)					)[0] for x in lengths]

        A__		   =		['''longest''', '''max_length''', '''do_not_pad''']
        A__		   =		[None, 1600, None]

        for max_length, padding in zip(__lowerCamelCase,__lowerCamelCase					):
               A__		   =		feat_extract(__lowerCamelCase,max_length=__lowerCamelCase,padding=__lowerCamelCase					)
               A__		   =		processed.input_values

               self._check_zero_mean_unit_variance(input_values[0][:800]					)
               self._check_zero_mean_unit_variance(input_values[1][:1000]					)
               self._check_zero_mean_unit_variance(input_values[2][:1200]					)



 def 							UpperCamelCase     (				self					):
        A__		   =		self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()					)
        A__		   =		[floats_list((1, x)					)[0] for x in range(800,1400,200					)]
        A__		   =		feat_extract(
            __lowerCamelCase,truncation=__lowerCamelCase,max_length=1000,padding='''max_length''',return_tensors='''np'''					)
        A__		   =		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 							UpperCamelCase     (				self					):
        A__		   =		self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()					)
        A__		   =		[floats_list((1, x)					)[0] for x in range(800,1400,200					)]
        A__		   =		feat_extract(
            __lowerCamelCase,truncation=__lowerCamelCase,max_length=1000,padding='''longest''',return_tensors='''np'''					)
        A__		   =		processed.input_values

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

        A__		   =		[floats_list((1, x)					)[0] for x in range(800,1400,200					)]
        A__		   =		feat_extract(
            __lowerCamelCase,truncation=__lowerCamelCase,max_length=2000,padding='''longest''',return_tensors='''np'''					)
        A__		   =		processed.input_values

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



 @require_torch
 def 							UpperCamelCase     (				self					):
        import torch

        A__		   =		self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()					)
        A__		   =		np.random.rand(100					).astype(np.floataa					)
        A__		   =		np_speech_inputs.tolist()

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




 @slow
 @require_torch
 def 							UpperCamelCase     (				self					):
        # 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:
               A__		   =		WavaVecaConfig.from_pretrained(__lowerCamelCase					)
               A__		   =		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'''					)












import pytest

import datasets


# Import fixture modules as plugins
a__: Dict							   =						['tests.fixtures.files', 'tests.fixtures.hub', 'tests.fixtures.fsspec']


def       UpperCamelCase__(			UpperCamelCase__     :			List[Any]					,	UpperCamelCase__     :			Tuple    )->List[str]:
       # Mark tests as "unit" by default if not marked as "integration" (or already marked as "unit")
       for item in items:
              if any(marker in item.keywords for marker in ['''integration''', '''unit''']    ):
                     continue
              item.add_marker(pytest.mark.unit    )


def       UpperCamelCase__(			UpperCamelCase__     :			Dict    )->List[Any]:
       config.addinivalue_line('''markers'''					,	'''torchaudio_latest: mark test to run with torchaudio>=0.12'''    )


@pytest.fixture(autouse=UpperCamelCase__    )
def       UpperCamelCase__(			UpperCamelCase__     :			Optional[int]					,	UpperCamelCase__     :			int    )->Union[str, Any]:
       # test_hf_cache_home = tmp_path_factory.mktemp("cache")  # TODO: why a cache dir per test function does not work?
       A__		   =		tmp_path_factory.getbasetemp() / '''cache'''
       A__		   =		test_hf_cache_home / '''datasets'''
       A__		   =		test_hf_cache_home / '''metrics'''
       A__		   =		test_hf_cache_home / '''modules'''
       monkeypatch.setattr('''datasets.config.HF_DATASETS_CACHE'''					,	str(UpperCamelCase__    )    )
       monkeypatch.setattr('''datasets.config.HF_METRICS_CACHE'''					,	str(UpperCamelCase__    )    )
       monkeypatch.setattr('''datasets.config.HF_MODULES_CACHE'''					,	str(UpperCamelCase__    )    )
       A__		   =		test_hf_datasets_cache / '''downloads'''
       monkeypatch.setattr('''datasets.config.DOWNLOADED_DATASETS_PATH'''					,	str(UpperCamelCase__    )    )
       A__		   =		test_hf_datasets_cache / '''downloads''' / '''extracted'''
       monkeypatch.setattr('''datasets.config.EXTRACTED_DATASETS_PATH'''					,	str(UpperCamelCase__    )    )


@pytest.fixture(autouse=UpperCamelCase__					,	scope='''session'''    )
def       UpperCamelCase__(			)->int:
       datasets.disable_progress_bar()


@pytest.fixture(autouse=UpperCamelCase__    )
def       UpperCamelCase__(			UpperCamelCase__     :			str    )->Tuple:
       # don't take tests into account when counting downloads
       monkeypatch.setattr('''datasets.config.HF_UPDATE_DOWNLOAD_COUNTS'''					,	UpperCamelCase__    )


@pytest.fixture
def       UpperCamelCase__(			UpperCamelCase__     :			List[Any]    )->Optional[Any]:
       # Required to suppress RemovedIn20Warning when feature(s) are not compatible with SQLAlchemy 2.0
       # To be removed once SQLAlchemy 2.0 supported
       monkeypatch.setattr('''sqlalchemy.util.deprecations.SILENCE_UBER_WARNING'''					,	UpperCamelCase__    )





