



'''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_					:      Optional[Any]    			=    random.Random()

if is_torch_available():
					import torch






def A__       (	UpperCAmelCase_    ,    UpperCAmelCase_=1.0    ,    UpperCAmelCase_=None    ,    UpperCAmelCase_=None				):
						if rng is None:
												_UpperCamelCase   :						Tuple												=  global_rng

						_UpperCamelCase   :						str												=  []
						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   :							str					,lowerCamelCase__   :							Dict					,lowerCamelCase__   :							str=7					,lowerCamelCase__   :							int=400					,lowerCamelCase__   :							Any=2000					,lowerCamelCase__   :							List[Any]=1					,lowerCamelCase__   :							Optional[Any]=0.0					,lowerCamelCase__   :							Optional[Any]=16000					,lowerCamelCase__   :							Tuple=True					,lowerCamelCase__   :							str=True					,):







							'''simple docstring'''
							_UpperCamelCase   :						Dict												=  parent
							_UpperCamelCase   :						Union[str, Any]												=  batch_size
							_UpperCamelCase   :						Optional[int]												=  min_seq_length
							_UpperCamelCase   :						Tuple												=  max_seq_length
							_UpperCamelCase   :						List[str]												=  (self.max_seq_length - self.min_seq_length) // (self.batch_size - 1)
							_UpperCamelCase   :						Tuple												=  feature_size
							_UpperCamelCase   :						Optional[int]												=  padding_value
							_UpperCamelCase   :						Tuple												=  sampling_rate
							_UpperCamelCase   :						str												=  return_attention_mask
							_UpperCamelCase   :						int												=  do_normalize





	def       UpperCamelCase_    (					self   :							Any					):







							'''simple docstring'''
							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       UpperCamelCase_    (					self   :							Optional[Any]					,lowerCamelCase__   :							Optional[Any]=False					,lowerCamelCase__   :							Dict=False					):







							'''simple docstring'''
							def _flatten(lowerCamelCase__   :							Dict					):
													return list(itertools.chain(*lowerCamelCase__					)					)

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

							return speech_inputs






@require_torch
@require_torchaudio
class 				lowercase__      (				lowercase		,   unittest.TestCase  ):
	lowercase__    							=     ASTFeatureExtractor





	def       UpperCamelCase_    (					self   :							str					):







							'''simple docstring'''
							_UpperCamelCase   :						Tuple												=  ASTFeatureExtractionTester(self					)





	def       UpperCamelCase_    (					self   :							Tuple					):







							'''simple docstring'''
							# Tests that all call wrap to encode_plus and batch_encode_plus
							_UpperCamelCase   :						Dict												=  self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()					)
							# create three inputs of length 800, 1000, and 1200
							_UpperCamelCase   :						Any												=  [floats_list((1, x)					)[0] for x in range(800					,1400					,200					)]
							_UpperCamelCase   :						List[str]												=  [np.asarray(lowerCamelCase__					) for speech_input in speech_inputs]

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

							# Test batched
							_UpperCamelCase   :						str												=  feat_extract(lowerCamelCase__					,padding=lowerCamelCase__					,return_tensors='np'					).input_values
							_UpperCamelCase   :						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.
							_UpperCamelCase   :						Dict												=  [floats_list((1, x)					)[0] for x in (800, 800, 800)]
							_UpperCamelCase   :						Tuple												=  np.asarray(lowerCamelCase__					)
							_UpperCamelCase   :						int												=  feat_extract(lowerCamelCase__					,return_tensors='np'					).input_values
							_UpperCamelCase   :						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       UpperCamelCase_    (					self   :							Optional[Any]					):







							'''simple docstring'''
							import torch

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

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





	def       UpperCamelCase_    (					self   :							str					,lowerCamelCase__   :							str					):







							'''simple docstring'''
							from datasets import load_dataset

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

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





	@require_torch
	def       UpperCamelCase_    (					self   :							List[Any]					):







							'''simple docstring'''
							# fmt: off
							_UpperCamelCase   :						str												=  torch.tensor(
							    [-0.9_8_9_4, -1.2_7_7_6, -0.9_0_6_6, -1.2_7_7_6, -0.9_3_4_9, -1.2_6_0_9, -1.0_3_8_6, -1.2_7_7_6,
							     -1.1_5_6_1, -1.2_7_7_6, -1.2_0_5_2, -1.2_7_2_3, -1.2_1_9_0, -1.2_1_3_2, -1.2_7_7_6, -1.1_1_3_3,
							     -1.1_9_5_3, -1.1_3_4_3, -1.1_5_8_4, -1.2_2_0_3, -1.1_7_7_0, -1.2_4_7_4, -1.2_3_8_1, -1.1_9_3_6,
							     -0.9_2_7_0, -0.8_3_1_7, -0.8_0_4_9, -0.7_7_0_6, -0.7_5_6_5, -0.7_8_6_9]					)
							# fmt: on

							_UpperCamelCase   :						Any												=  self._load_datasamples(1					)
							_UpperCamelCase   :						Tuple												=  ASTFeatureExtractor()
							_UpperCamelCase   :						str												=  feature_extractor(lowerCamelCase__					,return_tensors='pt'					).input_values
							self.assertEquals(input_values.shape					,(1, 1024, 128)					)
							self.assertTrue(torch.allclose(input_values[0, 0, :30]					,lowerCamelCase__					,atol=1E-4					)					)







'''simple docstring'''
from typing import TYPE_CHECKING

from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tf_available, is_torch_available


snake_case_					:      Union[str, Any]    			=    {
    'configuration_transfo_xl': ['TRANSFO_XL_PRETRAINED_CONFIG_ARCHIVE_MAP', 'TransfoXLConfig'],
    'tokenization_transfo_xl': ['TransfoXLCorpus', 'TransfoXLTokenizer'],
}

try:
					if not is_torch_available():
										raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
					pass
else:
					snake_case_					:      str    			=    [
					    'TRANSFO_XL_PRETRAINED_MODEL_ARCHIVE_LIST',
					    'AdaptiveEmbedding',
					    'TransfoXLForSequenceClassification',
					    'TransfoXLLMHeadModel',
					    'TransfoXLModel',
					    'TransfoXLPreTrainedModel',
					    'load_tf_weights_in_transfo_xl',
					]

try:
					if not is_tf_available():
										raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
					pass
else:
					snake_case_					:      Any    			=    [
					    'TF_TRANSFO_XL_PRETRAINED_MODEL_ARCHIVE_LIST',
					    'TFAdaptiveEmbedding',
					    'TFTransfoXLForSequenceClassification',
					    'TFTransfoXLLMHeadModel',
					    'TFTransfoXLMainLayer',
					    'TFTransfoXLModel',
					    'TFTransfoXLPreTrainedModel',
					]


if TYPE_CHECKING:
					from .configuration_transfo_xl import TRANSFO_XL_PRETRAINED_CONFIG_ARCHIVE_MAP, TransfoXLConfig
					from .tokenization_transfo_xl import TransfoXLCorpus, TransfoXLTokenizer

					try:
										if not is_torch_available():
															raise OptionalDependencyNotAvailable()
					except OptionalDependencyNotAvailable:
										pass
					else:
										from .modeling_transfo_xl import (
										    TRANSFO_XL_PRETRAINED_MODEL_ARCHIVE_LIST,
										    AdaptiveEmbedding,
										    TransfoXLForSequenceClassification,
										    TransfoXLLMHeadModel,
										    TransfoXLModel,
										    TransfoXLPreTrainedModel,
										    load_tf_weights_in_transfo_xl,
										)

					try:
										if not is_tf_available():
															raise OptionalDependencyNotAvailable()
					except OptionalDependencyNotAvailable:
										pass
					else:
										from .modeling_tf_transfo_xl import (
										    TF_TRANSFO_XL_PRETRAINED_MODEL_ARCHIVE_LIST,
										    TFAdaptiveEmbedding,
										    TFTransfoXLForSequenceClassification,
										    TFTransfoXLLMHeadModel,
										    TFTransfoXLMainLayer,
										    TFTransfoXLModel,
										    TFTransfoXLPreTrainedModel,
										)

else:
					import sys

					snake_case_					:      Dict    			=    _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)


