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

if is_torch_available():
							import torch



def 	UpperCamelCase   (							lowerCAmelCase__			,      lowerCAmelCase__=1.0			,      lowerCAmelCase__=None			,      lowerCAmelCase__=None		):
					'''simple docstring'''
					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     lowercase		( unittest.TestCase		):




					def __init__(    self       ,A__       ,A__=7       ,A__=4_0_0       ,A__=2_0_0_0       ,A__=1       ,A__=0.0       ,A__=1_6_0_0_0       ,A__=True       ,A__=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):
										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       ,A__=False       ,A__=False):
										def _flatten(A__):
															return list(itertools.chain(*__snake_case))

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

										return speech_inputs







@require_torch
@require_torchaudio
class     lowercase		( lowerCamelCase_	,  unittest.TestCase		):
					lowercase_	:	Union[str, Any]	 =ASTFeatureExtractor




					def 			A__				(    self):
										lowercase						     =						ASTFeatureExtractionTester(self)




					def 			A__				(    self):
										# 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(8_0_0       ,1_4_0_0       ,2_0_0)]
										lowercase						     =						[np.asarray(__snake_case) 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(__snake_case       ,__snake_case       ,atol=1E-3))

										# Test batched
										lowercase						     =						feat_extract(__snake_case       ,padding=__snake_case       ,return_tensors='''np''').input_values
										lowercase						     =						feat_extract(__snake_case       ,padding=__snake_case       ,return_tensors='''np''').input_values
										for enc_seq_a, enc_seq_a in zip(__snake_case       ,__snake_case):
															self.assertTrue(np.allclose(__snake_case       ,__snake_case       ,atol=1E-3))

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




					@require_torch
					def 			A__				(    self):
										import torch

										lowercase						     =						self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict())
										lowercase						     =						np.random.rand(1_0_0).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       ,A__):
										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(__snake_case))[:num_samples]['''audio''']

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





					@require_torch
					def 			A__				(    self):
										# 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(__snake_case       ,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]       ,__snake_case       ,atol=1E-4))






from typing import TYPE_CHECKING

from ...utils import (
    OptionalDependencyNotAvailable,
    _LazyModule,
    is_flax_available,
    is_tf_available,
    is_tokenizers_available,
    is_torch_available,
)


lowercase__		:Any			  =   {
    "configuration_distilbert": [
        "DISTILBERT_PRETRAINED_CONFIG_ARCHIVE_MAP",
        "DistilBertConfig",
        "DistilBertOnnxConfig",
    ],
    "tokenization_distilbert": ["DistilBertTokenizer"],
}

try:
							if not is_tokenizers_available():
														raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
							pass
else:
							lowercase__		:Tuple			  =   ["DistilBertTokenizerFast"]

try:
							if not is_torch_available():
														raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
							pass
else:
							lowercase__		:Union[str, Any]			  =   [
							    "DISTILBERT_PRETRAINED_MODEL_ARCHIVE_LIST",
							    "DistilBertForMaskedLM",
							    "DistilBertForMultipleChoice",
							    "DistilBertForQuestionAnswering",
							    "DistilBertForSequenceClassification",
							    "DistilBertForTokenClassification",
							    "DistilBertModel",
							    "DistilBertPreTrainedModel",
							]

try:
							if not is_tf_available():
														raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
							pass
else:
							lowercase__		:Optional[int]			  =   [
							    "TF_DISTILBERT_PRETRAINED_MODEL_ARCHIVE_LIST",
							    "TFDistilBertForMaskedLM",
							    "TFDistilBertForMultipleChoice",
							    "TFDistilBertForQuestionAnswering",
							    "TFDistilBertForSequenceClassification",
							    "TFDistilBertForTokenClassification",
							    "TFDistilBertMainLayer",
							    "TFDistilBertModel",
							    "TFDistilBertPreTrainedModel",
							]

try:
							if not is_flax_available():
														raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
							pass
else:
							lowercase__		:Union[str, Any]			  =   [
							    "FlaxDistilBertForMaskedLM",
							    "FlaxDistilBertForMultipleChoice",
							    "FlaxDistilBertForQuestionAnswering",
							    "FlaxDistilBertForSequenceClassification",
							    "FlaxDistilBertForTokenClassification",
							    "FlaxDistilBertModel",
							    "FlaxDistilBertPreTrainedModel",
							]


if TYPE_CHECKING:
							from .configuration_distilbert import (
							    DISTILBERT_PRETRAINED_CONFIG_ARCHIVE_MAP,
							    DistilBertConfig,
							    DistilBertOnnxConfig,
							)
							from .tokenization_distilbert import DistilBertTokenizer

							try:
														if not is_tokenizers_available():
																					raise OptionalDependencyNotAvailable()
							except OptionalDependencyNotAvailable:
														pass
							else:
														from .tokenization_distilbert_fast import DistilBertTokenizerFast

							try:
														if not is_torch_available():
																					raise OptionalDependencyNotAvailable()
							except OptionalDependencyNotAvailable:
														pass
							else:
														from .modeling_distilbert import (
														    DISTILBERT_PRETRAINED_MODEL_ARCHIVE_LIST,
														    DistilBertForMaskedLM,
														    DistilBertForMultipleChoice,
														    DistilBertForQuestionAnswering,
														    DistilBertForSequenceClassification,
														    DistilBertForTokenClassification,
														    DistilBertModel,
														    DistilBertPreTrainedModel,
														)

							try:
														if not is_tf_available():
																					raise OptionalDependencyNotAvailable()
							except OptionalDependencyNotAvailable:
														pass
							else:
														from .modeling_tf_distilbert import (
														    TF_DISTILBERT_PRETRAINED_MODEL_ARCHIVE_LIST,
														    TFDistilBertForMaskedLM,
														    TFDistilBertForMultipleChoice,
														    TFDistilBertForQuestionAnswering,
														    TFDistilBertForSequenceClassification,
														    TFDistilBertForTokenClassification,
														    TFDistilBertMainLayer,
														    TFDistilBertModel,
														    TFDistilBertPreTrainedModel,
														)

							try:
														if not is_flax_available():
																					raise OptionalDependencyNotAvailable()
							except OptionalDependencyNotAvailable:
														pass
							else:
														from .modeling_flax_distilbert import (
														    FlaxDistilBertForMaskedLM,
														    FlaxDistilBertForMultipleChoice,
														    FlaxDistilBertForQuestionAnswering,
														    FlaxDistilBertForSequenceClassification,
														    FlaxDistilBertForTokenClassification,
														    FlaxDistilBertModel,
														    FlaxDistilBertPreTrainedModel,
														)

else:
							import sys

							lowercase__		:List[str]			  =   _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)




