


import itertools
import random
import unittest

import numpy as np

from transformers import is_speech_available
from transformers.testing_utils import require_torch, require_torchaudio

from ...test_sequence_feature_extraction_common import SequenceFeatureExtractionTestMixin


if is_speech_available():
 from transformers import SpeechaTextFeatureExtractor

lowerCamelCase											=       random.Random()




def 	a_      (    SCREAMING_SNAKE_CASE__					:      Dict		,			SCREAMING_SNAKE_CASE__					:      List[str]=1.0		,			SCREAMING_SNAKE_CASE__					:      str=None		,			SCREAMING_SNAKE_CASE__					:      List[str]=None     ):
       '''simple docstring'''





       if rng is None:
              _lowerCamelCase       :		Optional[Any]		=global_rng

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

       return values




@require_torch
@require_torchaudio
class A  (   unittest.TestCase     ):






      def __init__(					self						:     Optional[Any]			,   lowercase_						:     Dict			,   lowercase_						:     Optional[int]=7			,   lowercase_						:     Any=400			,   lowercase_						:     List[str]=2000			,   lowercase_						:     int=24			,   lowercase_						:     Union[str, Any]=24			,   lowercase_						:     Dict=0.0			,   lowercase_						:     Optional[Any]=1_6000			,   lowercase_						:     int=True			,   lowercase_						:     Union[str, Any]=True			,   )					->						Optional[int]:
             """simple docstring"""




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






      def        lowerCamelCase       (					self						:     Any )					->						int:
             """simple docstring"""




             return {
                 "feature_size": self.feature_size,
                 "num_mel_bins": self.num_mel_bins,
                 "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						:     Optional[int]			,   lowercase_						:     int=False			,   lowercase_						:     int=False )					->						Optional[int]:
             """simple docstring"""




             def _flatten(lowercase_						:     str ):
                    return list(itertools.chain(*lowercase_ ) )

             if equal_length:
                    _lowerCamelCase       :		Optional[int]		=[floats_list((self.max_seq_length, self.feature_size) ) for _ in range(self.batch_size )]
             else:
                    # make sure that inputs increase in size
                    _lowerCamelCase       :		Optional[Any]		=[
                        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       :		Optional[int]		=[np.asarray(lowercase_ ) for x in speech_inputs]
             return speech_inputs




@require_torch
@require_torchaudio
class A  (   UpperCamelCase_    ,							unittest.TestCase     ):
      UpperCamelCase__							:  List[Any]      =SpeechaTextFeatureExtractor if is_speech_available() else None






      def        lowerCamelCase       (					self						:     Union[str, Any] )					->						Any:
             """simple docstring"""




             _lowerCamelCase       :		Optional[Any]		=SpeechaTextFeatureExtractionTester(self )






      def        lowerCamelCase       (					self						:     Tuple			,   lowercase_						:     int )					->						List[Any]:
             """simple docstring"""




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






      def        lowerCamelCase       (					self						:     Optional[int] )					->						Dict:
             """simple docstring"""




             _lowerCamelCase       :		Optional[Any]		=self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict() )
             # create three inputs of length 800, 1000, and 1200
             _lowerCamelCase       :		Dict		=[floats_list((1, x) )[0] for x in range(800			,   1400			,   200 )]
             _lowerCamelCase       :		Optional[int]		=[np.asarray(lowercase_ ) for speech_input in speech_inputs]

             # Test feature size
             _lowerCamelCase       :		str		=feature_extractor(lowercase_			,   padding=lowercase_			,   return_tensors='np' ).input_features
             self.assertTrue(input_features.ndim == 3 )
             self.assertTrue(input_features.shape[-1] == feature_extractor.feature_size )

             # Test not batched input
             _lowerCamelCase       :		Optional[Any]		=feature_extractor(speech_inputs[0]			,   return_tensors='np' ).input_features
             _lowerCamelCase       :		List[str]		=feature_extractor(np_speech_inputs[0]			,   return_tensors='np' ).input_features
             self.assertTrue(np.allclose(lowercase_			,   lowercase_			,   atol=1E-3 ) )

             # Test batched
             _lowerCamelCase       :		int		=feature_extractor(lowercase_			,   return_tensors='np' ).input_features
             _lowerCamelCase       :		Optional[Any]		=feature_extractor(lowercase_			,   return_tensors='np' ).input_features
             for enc_seq_a, enc_seq_a in zip(lowercase_			,   lowercase_ ):
                    self.assertTrue(np.allclose(lowercase_			,   lowercase_			,   atol=1E-3 ) )

             # Test 2-D numpy arrays are batched.
             _lowerCamelCase       :		Optional[int]		=[floats_list((1, x) )[0] for x in (800, 800, 800)]
             _lowerCamelCase       :		Union[str, Any]		=np.asarray(lowercase_ )
             _lowerCamelCase       :		Any		=feature_extractor(lowercase_			,   return_tensors='np' ).input_features
             _lowerCamelCase       :		Tuple		=feature_extractor(lowercase_			,   return_tensors='np' ).input_features
             for enc_seq_a, enc_seq_a in zip(lowercase_			,   lowercase_ ):
                    self.assertTrue(np.allclose(lowercase_			,   lowercase_			,   atol=1E-3 ) )






      def        lowerCamelCase       (					self						:     Tuple )					->						Optional[int]:
             """simple docstring"""




             _lowerCamelCase       :		Any		=self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict() )
             _lowerCamelCase       :		List[Any]		=[floats_list((1, x) )[0] for x in range(800			,   1400			,   200 )]

             _lowerCamelCase       :		int		=['longest', 'max_length', 'do_not_pad']
             _lowerCamelCase       :		Any		=[None, 16, None]
             for max_length, padding in zip(lowercase_			,   lowercase_ ):
                    _lowerCamelCase       :		List[str]		=feature_extractor(
                        lowercase_			,   padding=lowercase_			,   max_length=lowercase_			,   return_attention_mask=lowercase_ )
                    _lowerCamelCase       :		Union[str, Any]		=inputs.input_features
                    _lowerCamelCase       :		List[Any]		=inputs.attention_mask
                    _lowerCamelCase       :		Union[str, Any]		=[np.sum(lowercase_ ) for x in attention_mask]

                    self._check_zero_mean_unit_variance(input_features[0][: fbank_feat_lengths[0]] )
                    self._check_zero_mean_unit_variance(input_features[1][: fbank_feat_lengths[1]] )
                    self._check_zero_mean_unit_variance(input_features[2][: fbank_feat_lengths[2]] )






      def        lowerCamelCase       (					self						:     Optional[Any] )					->						Dict:
             """simple docstring"""




             _lowerCamelCase       :		Union[str, Any]		=self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict() )
             _lowerCamelCase       :		Any		=[floats_list((1, x) )[0] for x in range(800			,   1400			,   200 )]

             _lowerCamelCase       :		Optional[int]		=['longest', 'max_length', 'do_not_pad']
             _lowerCamelCase       :		Any		=[None, 16, None]
             for max_length, padding in zip(lowercase_			,   lowercase_ ):
                    _lowerCamelCase       :		Optional[Any]		=feature_extractor(
                        lowercase_			,   max_length=lowercase_			,   padding=lowercase_			,   return_tensors='np'			,   return_attention_mask=lowercase_ )
                    _lowerCamelCase       :		Union[str, Any]		=inputs.input_features
                    _lowerCamelCase       :		Optional[int]		=inputs.attention_mask
                    _lowerCamelCase       :		List[Any]		=[np.sum(lowercase_ ) for x in attention_mask]

                    self._check_zero_mean_unit_variance(input_features[0][: fbank_feat_lengths[0]] )
                    self.assertTrue(input_features[0][fbank_feat_lengths[0] :].sum() < 1E-6 )
                    self._check_zero_mean_unit_variance(input_features[1][: fbank_feat_lengths[1]] )
                    self.assertTrue(input_features[0][fbank_feat_lengths[1] :].sum() < 1E-6 )
                    self._check_zero_mean_unit_variance(input_features[2][: fbank_feat_lengths[2]] )






      def        lowerCamelCase       (					self						:     Union[str, Any] )					->						Optional[int]:
             """simple docstring"""




             _lowerCamelCase       :		Optional[int]		=self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict() )
             _lowerCamelCase       :		Tuple		=[floats_list((1, x) )[0] for x in range(800			,   1400			,   200 )]
             _lowerCamelCase       :		Union[str, Any]		=feature_extractor(
                 lowercase_			,   padding='max_length'			,   max_length=4			,   truncation=lowercase_			,   return_tensors='np'			,   return_attention_mask=lowercase_			,   )
             _lowerCamelCase       :		Union[str, Any]		=inputs.input_features
             _lowerCamelCase       :		Dict		=inputs.attention_mask
             _lowerCamelCase       :		str		=np.sum(attention_mask == 1			,   axis=1 )

             self._check_zero_mean_unit_variance(input_features[0, : fbank_feat_lengths[0]] )
             self._check_zero_mean_unit_variance(input_features[1] )
             self._check_zero_mean_unit_variance(input_features[2] )






      def        lowerCamelCase       (					self						:     Union[str, Any] )					->						Tuple:
             """simple docstring"""




             _lowerCamelCase       :		List[Any]		=self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict() )
             _lowerCamelCase       :		List[Any]		=[floats_list((1, x) )[0] for x in range(800			,   1400			,   200 )]
             _lowerCamelCase       :		Optional[int]		=feature_extractor(
                 lowercase_			,   padding='longest'			,   max_length=4			,   truncation=lowercase_			,   return_tensors='np'			,   return_attention_mask=lowercase_			,   )
             _lowerCamelCase       :		List[str]		=inputs.input_features
             _lowerCamelCase       :		Optional[int]		=inputs.attention_mask
             _lowerCamelCase       :		Optional[Any]		=np.sum(attention_mask == 1			,   axis=1 )

             self._check_zero_mean_unit_variance(input_features[0, : fbank_feat_lengths[0]] )
             self._check_zero_mean_unit_variance(input_features[1, : fbank_feat_lengths[1]] )
             self._check_zero_mean_unit_variance(input_features[2] )

             # make sure that if max_length < longest -> then pad to max_length
             self.assertEqual(input_features.shape			,   (3, 4, 24) )

             _lowerCamelCase       :		str		=[floats_list((1, x) )[0] for x in range(800			,   1400			,   200 )]
             _lowerCamelCase       :		int		=feature_extractor(
                 lowercase_			,   padding='longest'			,   max_length=16			,   truncation=lowercase_			,   return_tensors='np'			,   return_attention_mask=lowercase_			,   )
             _lowerCamelCase       :		int		=inputs.input_features
             _lowerCamelCase       :		List[str]		=inputs.attention_mask
             _lowerCamelCase       :		int		=np.sum(attention_mask == 1			,   axis=1 )

             self._check_zero_mean_unit_variance(input_features[0, : fbank_feat_lengths[0]] )
             self._check_zero_mean_unit_variance(input_features[1, : fbank_feat_lengths[1]] )
             self._check_zero_mean_unit_variance(input_features[2] )

             # make sure that if max_length < longest -> then pad to max_length
             self.assertEqual(input_features.shape			,   (3, 6, 24) )






      def        lowerCamelCase       (					self						:     List[str] )					->						List[str]:
             """simple docstring"""




             import torch

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

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






      def        lowerCamelCase       (					self						:     Any			,   lowercase_						:     Tuple )					->						Tuple:
             """simple docstring"""




             from datasets import load_dataset

             _lowerCamelCase       :		Union[str, Any]		=load_dataset('hf-internal-testing/librispeech_asr_dummy'			,   'clean'			,   split='validation' )
             # automatic decoding with librispeech
             _lowerCamelCase       :		Tuple		=ds.sort('id' ).select(range(lowercase_ ) )[:num_samples]['audio']

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






      def        lowerCamelCase       (					self						:     Dict )					->						Optional[int]:
             """simple docstring"""




             _lowerCamelCase       :		Optional[Any]		=np.array([
                 -1.5745, -1.7713, -1.7020, -1.6069, -1.2250, -1.1105, -0.9072, -0.8241,
                 -1.2310, -0.8098, -0.3320, -0.4101, -0.7985, -0.4996, -0.8213, -0.9128,
                 -1.0420, -1.1286, -1.0440, -0.7999, -0.8405, -1.2275, -1.5443, -1.4625,
             ] )
             # fmt: on

             _lowerCamelCase       :		str		=self._load_datasamples(1 )
             _lowerCamelCase       :		Optional[int]		=self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict() )
             _lowerCamelCase       :		Union[str, Any]		=feature_extractor(lowercase_			,   return_tensors='pt' ).input_features
             self.assertEquals(input_features.shape			,   (1, 584, 24) )
             self.assertTrue(np.allclose(input_features[0, 0, :30]			,   lowercase_			,   atol=1E-4 ) )






import os
import sys
import unittest


lowerCamelCase											=       os.path.abspath(os.path.dirname(os.path.dirname(os.path.dirname(__file__))))
sys.path.append(os.path.join(git_repo_path, 'utils'))

import check_dummies  # noqa: E402
from check_dummies import create_dummy_files, create_dummy_object, find_backend, read_init  # noqa: E402


# Align TRANSFORMERS_PATH in check_dummies with the current path
lowerCamelCase											=       os.path.join(git_repo_path, 'src', 'transformers')

lowerCamelCase											=       '\n{0} = None\n'

lowerCamelCase											=       '\nclass {0}(metaclass=DummyObject):\n    _backends = {1}\n\n    def __init__(self, *args, **kwargs):\n        requires_backends(self, {1})\n'


lowerCamelCase											=       '\ndef {0}(*args, **kwargs):\n    requires_backends({0}, {1})\n'




class A  (   unittest.TestCase     ):






      def        lowerCamelCase       (					self						:     Tuple )					->						Union[str, Any]:
             """simple docstring"""




             _lowerCamelCase       :		int		=find_backend('    _import_structure["models.albert"].append("AlbertTokenizerFast")' )
             self.assertIsNone(lowercase_ )

             _lowerCamelCase       :		List[str]		=find_backend('    if not is_tokenizers_available():' )
             self.assertEqual(lowercase_			,   'tokenizers' )

             _lowerCamelCase       :		List[Any]		=find_backend('    if not is_tensorflow_text_available():' )
             self.assertEqual(lowercase_			,   'tensorflow_text' )

             _lowerCamelCase       :		int		=find_backend('    if not (is_sentencepiece_available() and is_tokenizers_available()):' )
             self.assertEqual(lowercase_			,   'sentencepiece_and_tokenizers' )

             _lowerCamelCase       :		Dict		=find_backend(
                 '    if not (is_sentencepiece_available() and is_tensorflow_text_available()):' )
             self.assertEqual(lowercase_			,   'sentencepiece_and_tensorflow_text' )

             _lowerCamelCase       :		List[Any]		=find_backend(
                 '    if not (is_sentencepiece_available() and is_tokenizers_available() and is_vision_available()):' )
             self.assertEqual(lowercase_			,   'sentencepiece_and_tokenizers_and_vision' )






      def        lowerCamelCase       (					self						:     Optional[Any] )					->						List[str]:
             """simple docstring"""




             _lowerCamelCase       :		Union[str, Any]		=read_init()
             # We don't assert on the exact list of keys to allow for smooth grow of backend-specific objects
             self.assertIn('torch'			,   lowercase_ )
             self.assertIn('tensorflow_text'			,   lowercase_ )
             self.assertIn('sentencepiece_and_tokenizers'			,   lowercase_ )

             # Likewise, we can't assert on the exact content of a key
             self.assertIn('BertModel'			,   objects['torch'] )
             self.assertIn('TFBertModel'			,   objects['tf'] )
             self.assertIn('FlaxBertModel'			,   objects['flax'] )
             self.assertIn('BertModel'			,   objects['torch'] )
             self.assertIn('TFBertTokenizer'			,   objects['tensorflow_text'] )
             self.assertIn('convert_slow_tokenizer'			,   objects['sentencepiece_and_tokenizers'] )






      def        lowerCamelCase       (					self						:     int )					->						Optional[Any]:
             """simple docstring"""




             _lowerCamelCase       :		Optional[Any]		=create_dummy_object('CONSTANT'			,   '\'torch\'' )
             self.assertEqual(lowercase_			,   '\nCONSTANT = None\n' )

             _lowerCamelCase       :		Dict		=create_dummy_object('function'			,   '\'torch\'' )
             self.assertEqual(
                 lowercase_			,   '\ndef function(*args, **kwargs):\n    requires_backends(function, \'torch\')\n' )

             _lowerCamelCase       :		Union[str, Any]		='\nclass FakeClass(metaclass=DummyObject):\n    _backends = \'torch\'\n\n    def __init__(self, *args, **kwargs):\n        requires_backends(self, \'torch\')\n'
             _lowerCamelCase       :		Tuple		=create_dummy_object('FakeClass'			,   '\'torch\'' )
             self.assertEqual(lowercase_			,   lowercase_ )






      def        lowerCamelCase       (					self						:     List[Any] )					->						Dict:
             """simple docstring"""




             _lowerCamelCase       :		Dict		='# This file is autogenerated by the command `make fix-copies`, do not edit.\nfrom ..utils import DummyObject, requires_backends\n\n\nCONSTANT = None\n\n\ndef function(*args, **kwargs):\n    requires_backends(function, ["torch"])\n\n\nclass FakeClass(metaclass=DummyObject):\n    _backends = ["torch"]\n\n    def __init__(self, *args, **kwargs):\n        requires_backends(self, ["torch"])\n'
             _lowerCamelCase       :		Optional[int]		=create_dummy_files({'torch': ['CONSTANT', 'function', 'FakeClass']} )
             self.assertEqual(dummy_files['torch']			,   lowercase_ )


