





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


__UpperCAmelCase							=   random.Random()

if is_torch_available():
  import torch
def       __UpperCamelCase					(      lowercase__						:							List[Any]		,     lowercase__						:							Optional[Any]=1.0		,     lowercase__						:							Dict=None		,     lowercase__						:							Optional[int]=None		)						->   Optional[Any]:
    '''simple docstring'''





    if rng is None:
        lowerCAmelCase_       :						Tuple							= global_rng

    lowerCAmelCase_       :						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						: str       ,      UpperCAmelCase						: Dict       ,      UpperCAmelCase						: str=7       ,      UpperCAmelCase						: int=4_00       ,      UpperCAmelCase						: Any=20_00       ,      UpperCAmelCase						: List[Any]=1       ,      UpperCAmelCase						: Optional[Any]=0.0       ,      UpperCAmelCase						: Optional[Any]=1_60_00       ,      UpperCAmelCase						: Tuple=True       ,      UpperCAmelCase						: str=True       ,      ):
           lowerCAmelCase_       :						Dict							= parent
           lowerCAmelCase_       :						Union[str, Any]							= batch_size
           lowerCAmelCase_       :						Optional[int]							= min_seq_length
           lowerCAmelCase_       :						Tuple							= max_seq_length
           lowerCAmelCase_       :						List[str]							= (self.max_seq_length - self.min_seq_length) // (self.batch_size - 1)
           lowerCAmelCase_       :						Tuple							= feature_size
           lowerCAmelCase_       :						Optional[int]							= padding_value
           lowerCAmelCase_       :						Tuple							= sampling_rate
           lowerCAmelCase_       :						str							= return_attention_mask
           lowerCAmelCase_       :						int							= do_normalize



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

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

           return speech_inputs






@require_torch
@require_torchaudio
class 		__a				(						__UpperCamelCase      ,unittest.TestCase   ):
       __snake_case    : Dict   =		ASTFeatureExtractor



       def 			A				(  self						: str      ):
           lowerCAmelCase_       :						Tuple							= ASTFeatureExtractionTester(self      )



       def 			A				(  self						: Tuple      ):
           lowerCAmelCase_       :						Dict							= self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()      )
           # create three inputs of length 800, 1000, and 1200
           lowerCAmelCase_       :						Any							= [floats_list((1, x)      )[0] for x in range(8_00       ,      14_00       ,      2_00      )]
           lowerCAmelCase_       :						List[str]							= [np.asarray(lowerCamelCase__      ) for speech_input in speech_inputs]

           # Test not batched input
           lowerCAmelCase_       :						List[str]							= feat_extract(speech_inputs[0]       ,      return_tensors="""np"""      ).input_values
           lowerCAmelCase_       :						Dict							= feat_extract(np_speech_inputs[0]       ,      return_tensors="""np"""      ).input_values
           self.assertTrue(np.allclose(lowerCamelCase__       ,      lowerCamelCase__       ,      atol=1e-3      )      )

           # Test batched
           lowerCAmelCase_       :						str							= feat_extract(lowerCamelCase__       ,      padding=lowerCamelCase__       ,      return_tensors="""np"""      ).input_values
           lowerCAmelCase_       :						int							= feat_extract(lowerCamelCase__       ,      padding=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.
           lowerCAmelCase_       :						Dict							= [floats_list((1, x)      )[0] for x in (8_00, 8_00, 8_00)]
           lowerCAmelCase_       :						Tuple							= np.asarray(lowerCamelCase__      )
           lowerCAmelCase_       :						int							= feat_extract(lowerCamelCase__       ,      return_tensors="""np"""      ).input_values
           lowerCAmelCase_       :						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      )      )



       @require_torch
       def 			A				(  self						: Optional[Any]      ):
           import torch

           lowerCAmelCase_       :						Dict							= self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()      )
           lowerCAmelCase_       :						List[Any]							= np.random.rand(1_00      ).astype(np.floataa      )
           lowerCAmelCase_       :						Optional[int]							= np_speech_inputs.tolist()

           for inputs in [py_speech_inputs, np_speech_inputs]:
               lowerCAmelCase_       :						List[Any]							= feature_extractor.pad([{"""input_values""": inputs}]       ,      return_tensors="""np"""      )
               self.assertTrue(np_processed.input_values.dtype == np.floataa      )
               lowerCAmelCase_       :						Union[str, Any]							= feature_extractor.pad([{"""input_values""": inputs}]       ,      return_tensors="""pt"""      )
               self.assertTrue(pt_processed.input_values.dtype == torch.floataa      )



       def 			A				(  self						: str       ,      UpperCAmelCase						: str      ):
           from datasets import load_dataset

           lowerCAmelCase_       :						List[str]							= load_dataset("""hf-internal-testing/librispeech_asr_dummy"""       ,      """clean"""       ,      split="""validation"""      )
           # automatic decoding with librispeech
           lowerCAmelCase_       :						Dict							= ds.sort("""id"""      ).select(range(lowerCamelCase__      )      )[:num_samples]['audio']

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



       @require_torch
       def 			A				(  self						: List[Any]      ):
           lowerCAmelCase_       :						str							= torch.tensor(
               [-0.9894, -1.2776, -0.9066, -1.2776, -0.9349, -1.2609, -1.0386, -1.2776,
                -1.1561, -1.2776, -1.2052, -1.2723, -1.2190, -1.2132, -1.2776, -1.1133,
                -1.1953, -1.1343, -1.1584, -1.2203, -1.1770, -1.2474, -1.2381, -1.1936,
                -0.9270, -0.8317, -0.8049, -0.7706, -0.7565, -0.7869]      )
           # fmt: on

           lowerCAmelCase_       :						Any							= self._load_datasamples(1      )
           lowerCAmelCase_       :						Tuple							= ASTFeatureExtractor()
           lowerCAmelCase_       :						str							= feature_extractor(lowerCamelCase__       ,      return_tensors="""pt"""      ).input_values
           self.assertEquals(input_values.shape       ,      (1, 10_24, 1_28)      )
           self.assertTrue(torch.allclose(input_values[0, 0, :30]       ,      lowerCamelCase__       ,      atol=1e-4      )      )







'''simple docstring'''
def A__       (	UpperCAmelCase_    ,    UpperCAmelCase_				):
						return int((input_a, input_a).count(0				) != 0				)






def A__       (	):
						assert nand_gate(0    ,    0				) == 1
						assert nand_gate(0    ,    1				) == 1
						assert nand_gate(1    ,    0				) == 1
						assert nand_gate(1    ,    1				) == 0


if __name__ == "__main__":
					print(nand_gate(0, 0))
					print(nand_gate(0, 1))
					print(nand_gate(1, 0))
					print(nand_gate(1, 1))


