



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


lowercase_								=     random.Random()

if is_torch_available():
							import torch



def __lowerCAmelCase (     SCREAMING_SNAKE_CASE_						,  SCREAMING_SNAKE_CASE_=1.0						,  SCREAMING_SNAKE_CASE_=None						,  SCREAMING_SNAKE_CASE_=None  ):
			if rng is None:
						lowercase__					  =				global_rng

			lowercase__					  =				[]
			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					:						Tuple,					__lowercase					:						Union[str, Any],					__lowercase					:						Dict=7,					__lowercase					:						List[Any]=400,					__lowercase					:						Tuple=2000,					__lowercase					:						Dict=1,					__lowercase					:						List[str]=0.0,					__lowercase					:						Dict=1_6000,					__lowercase					:						Union[str, Any]=True,					__lowercase					:						Union[str, Any]=True,					):
							lowercase__					  =				parent
							lowercase__					  =				batch_size
							lowercase__					  =				min_seq_length
							lowercase__					  =				max_seq_length
							lowercase__					  =				(self.max_seq_length - self.min_seq_length) // (self.batch_size - 1)
							lowercase__					  =				feature_size
							lowercase__					  =				padding_value
							lowercase__					  =				sampling_rate
							lowercase__					  =				return_attention_mask
							lowercase__					  =				do_normalize




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

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

							return speech_inputs
@require_torch
@require_torchaudio
class 				_snake_case			(					lowercase__    ,	unittest.TestCase):
				UpperCamelCase__				:					Tuple     		=ASTFeatureExtractor




				def 							A__      (						self					:						str						):
							lowercase__					  =				ASTFeatureExtractionTester(self						)




				def 							A__      (						self					:						str						):
							# Tests that all call wrap to encode_plus and batch_encode_plus
							lowercase__					  =				self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()						)
							# create three inputs of length 800, 1000, and 1200
							lowercase__					  =				[floats_list((1, x)						)[0] for x in range(800,					1400,					200						)]
							lowercase__					  =				[np.asarray(__lowercase						) for speech_input in speech_inputs]

							# Test not batched input
							lowercase__					  =				feat_extract(speech_inputs[0],					return_tensors="np"						).input_values
							lowercase__					  =				feat_extract(np_speech_inputs[0],					return_tensors="np"						).input_values
							self.assertTrue(np.allclose(__lowercase,					__lowercase,					atol=1e-3						)						)

							# Test batched
							lowercase__					  =				feat_extract(__lowercase,					padding=__lowercase,					return_tensors="np"						).input_values
							lowercase__					  =				feat_extract(__lowercase,					padding=__lowercase,					return_tensors="np"						).input_values
							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.
							lowercase__					  =				[floats_list((1, x)						)[0] for x in (800, 800, 800)]
							lowercase__					  =				np.asarray(__lowercase						)
							lowercase__					  =				feat_extract(__lowercase,					return_tensors="np"						).input_values
							lowercase__					  =				feat_extract(__lowercase,					return_tensors="np"						).input_values
							for enc_seq_a, enc_seq_a in zip(__lowercase,					__lowercase						):
										self.assertTrue(np.allclose(__lowercase,					__lowercase,					atol=1e-3						)						)




				@require_torch
				def 							A__      (						self					:						Dict						):
							import torch

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

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




				def 							A__      (						self					:						List[str],					__lowercase					:						List[Any]						):
							from datasets import load_dataset

							lowercase__					  =				load_dataset("hf-internal-testing/librispeech_asr_dummy",					"clean",					split="validation"						)
							# automatic decoding with librispeech
							lowercase__					  =				ds.sort("id"						).select(range(__lowercase						)						)[:num_samples]["audio"]

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




				@require_torch
				def 							A__      (						self					:						str						):
							# fmt: off
							lowercase__					  =				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

							lowercase__					  =				self._load_datasamples(1						)
							lowercase__					  =				ASTFeatureExtractor()
							lowercase__					  =				feature_extractor(__lowercase,					return_tensors="pt"						).input_values
							self.assertEquals(input_values.shape,					(1, 1024, 128)						)
							self.assertTrue(torch.allclose(input_values[0, 0, :30],					__lowercase,					atol=1e-4						)						)









def __lowerCAmelCase (     SCREAMING_SNAKE_CASE_  ):
			lowercase__					  =				0
			for ch in input_str:
						lowercase__					  =				ord(SCREAMING_SNAKE_CASE_  )
						lowercase__					  =				pow(2						,  SCREAMING_SNAKE_CASE_  )

						# If we already turned on bit for current character's unicode
						if bitmap >> ch_unicode & 1 == 1:
									return False
						bitmap |= ch_bit_index_on
			return True


if __name__ == "__main__":
							import doctest

							doctest.testmod()




