


"""simple docstring"""





import itertools
import random
import unittest

import numpy as np

from transformers import ASTFeatureExtractor
from transformers.testing_utils import require_torch, require_torchaudio
from transformers.utils.import_utils import is_torch_available

from ...test_sequence_feature_extraction_common import SequenceFeatureExtractionTestMixin


snake_case__		:     Union[str, Any]    				=				random.Random()

if is_torch_available():
 import torch


def      _snake_case						(						_snake_case				:			str ,   _snake_case				:			List[str]=1.0 ,   _snake_case				:			int=None ,   _snake_case				:			Optional[int]=None			)     ->   int:
      if rng is None:
            lowerCAmelCase					:	int       =   global_rng

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

      return values



class 		snake_case_(       unittest.TestCase			):






     def __init__(	self  :					Union[str, Any]					,				UpperCamelCase_  :					Union[str, Any]					,				UpperCamelCase_  :					Tuple=7					,				UpperCamelCase_  :					List[Any]=4_0_0					,				UpperCamelCase_  :					Any=2_0_0_0					,				UpperCamelCase_  :					Optional[Any]=1					,				UpperCamelCase_  :					Dict=0.0					,				UpperCamelCase_  :					List[Any]=1_6_0_0_0					,				UpperCamelCase_  :					Union[str, Any]=True					,				UpperCamelCase_  :					Optional[Any]=True					,				):
           lowerCAmelCase					:	str       =   parent
           lowerCAmelCase					:	Dict       =   batch_size
           lowerCAmelCase					:	str       =   min_seq_length
           lowerCAmelCase					:	List[Any]       =   max_seq_length
           lowerCAmelCase					:	Tuple       =   (self.max_seq_length - self.min_seq_length) // (self.batch_size - 1)
           lowerCAmelCase					:	List[str]       =   feature_size
           lowerCAmelCase					:	Tuple       =   padding_value
           lowerCAmelCase					:	Union[str, Any]       =   sampling_rate
           lowerCAmelCase					:	str       =   return_attention_mask
           lowerCAmelCase					:	Optional[int]       =   do_normalize






     def      lowerCamelCase__     (	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      lowerCamelCase__     (	self  :					Optional[Any]					,				UpperCamelCase_  :					str=False					,				UpperCamelCase_  :					int=False				):
           def _flatten(UpperCamelCase_  :					Tuple				):
                 return list(itertools.chain(*UpperCamelCase_				)				)

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

           return speech_inputs



@require_torch
@require_torchaudio
class 		snake_case_(       a__							,					unittest.TestCase			):
     __UpperCamelCase  =					ASTFeatureExtractor






     def      lowerCamelCase__     (	self  :					Optional[Any]				):
           lowerCAmelCase					:	Any       =   ASTFeatureExtractionTester(self				)






     def      lowerCamelCase__     (	self  :					Optional[int]				):
           # Tests that all call wrap to encode_plus and batch_encode_plus
           lowerCAmelCase					:	str       =   self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()				)
           # create three inputs of length 800, 1000, and 1200
           lowerCAmelCase					:	Optional[int]       =   [floats_list((1, x)				)[0] for x in range(8_0_0					,				1_4_0_0					,				2_0_0				)]
           lowerCAmelCase					:	str       =   [np.asarray(UpperCamelCase_				) for speech_input in speech_inputs]

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

           # Test batched
           lowerCAmelCase					:	int       =   feat_extract(UpperCamelCase_					,				padding=UpperCamelCase_					,				return_tensors='''np'''				).input_values
           lowerCAmelCase					:	Union[str, Any]       =   feat_extract(UpperCamelCase_					,				padding=UpperCamelCase_					,				return_tensors='''np'''				).input_values
           for enc_seq_a, enc_seq_a in zip(UpperCamelCase_					,				UpperCamelCase_				):
                 self.assertTrue(np.allclose(UpperCamelCase_					,				UpperCamelCase_					,				atol=1E-3				)				)

           # Test 2-D numpy arrays are batched.
           lowerCAmelCase					:	str       =   [floats_list((1, x)				)[0] for x in (8_0_0, 8_0_0, 8_0_0)]
           lowerCAmelCase					:	Optional[int]       =   np.asarray(UpperCamelCase_				)
           lowerCAmelCase					:	Optional[int]       =   feat_extract(UpperCamelCase_					,				return_tensors='''np'''				).input_values
           lowerCAmelCase					:	int       =   feat_extract(UpperCamelCase_					,				return_tensors='''np'''				).input_values
           for enc_seq_a, enc_seq_a in zip(UpperCamelCase_					,				UpperCamelCase_				):
                 self.assertTrue(np.allclose(UpperCamelCase_					,				UpperCamelCase_					,				atol=1E-3				)				)






     @require_torch
     def      lowerCamelCase__     (	self  :					List[Any]				):
           import torch

           lowerCAmelCase					:	str       =   self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()				)
           lowerCAmelCase					:	Any       =   np.random.rand(1_0_0				).astype(np.floataa				)
           lowerCAmelCase					:	List[str]       =   np_speech_inputs.tolist()

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






     def      lowerCamelCase__     (	self  :					List[Any]					,				UpperCamelCase_  :					List[str]				):
           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(UpperCamelCase_				)				)[:num_samples]['''audio''']

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






     @require_torch
     def      lowerCamelCase__     (	self  :					List[str]				):
           # fmt: off
           lowerCAmelCase					:	List[str]       =   torch.tensor(
               [-0.9_894, -1.2_776, -0.9_066, -1.2_776, -0.9_349, -1.2_609, -1.0_386, -1.2_776,
                -1.1_561, -1.2_776, -1.2_052, -1.2_723, -1.2_190, -1.2_132, -1.2_776, -1.1_133,
                -1.1_953, -1.1_343, -1.1_584, -1.2_203, -1.1_770, -1.2_474, -1.2_381, -1.1_936,
                -0.9_270, -0.8_317, -0.8_049, -0.7_706, -0.7_565, -0.7_869]				)
           # fmt: on

           lowerCAmelCase					:	Union[str, Any]       =   self._load_datasamples(1				)
           lowerCAmelCase					:	Tuple       =   ASTFeatureExtractor()
           lowerCAmelCase					:	Optional[Any]       =   feature_extractor(UpperCamelCase_					,				return_tensors='''pt'''				).input_values
           self.assertEquals(input_values.shape					,				(1, 1_0_2_4, 1_2_8)				)
           self.assertTrue(torch.allclose(input_values[0, 0, :3_0]					,				UpperCamelCase_					,				atol=1E-4				)				)






"""simple docstring"""





snake_case__		:     List[Any]    				=				'''Tobias Carryer'''

from time import time



class 		snake_case_:






     def __init__(	self  :					Optional[Any]					,				UpperCamelCase_  :					Optional[Any]					,				UpperCamelCase_  :					Tuple					,				UpperCamelCase_  :					Optional[int]					,				UpperCamelCase_  :					Dict=int(time()				)				):  # noqa: B008
           lowerCAmelCase					:	str       =   multiplier
           lowerCAmelCase					:	Optional[int]       =   increment
           lowerCAmelCase					:	Optional[Any]       =   modulo
           lowerCAmelCase					:	Optional[Any]       =   seed






     def      lowerCamelCase__     (	self  :					Union[str, Any]				):
           lowerCAmelCase					:	Optional[int]       =   (self.multiplier * self.seed + self.increment) % self.modulo
           return self.seed


if __name__ == "__main__":
 # Show the LCG in action.
 snake_case__		:     int    				=				LinearCongruentialGenerator(1_664_525, 1_013_904_223, 2 << 31)
 while True:
  print(lcg.next_number())


