






"""simple docstring"""



import itertools
import random
import unittest

import numpy as np

from transformers import is_speech_available
from transformers.testing_utils import require_torch, require_torchaudio

from ...test_sequence_feature_extraction_common import SequenceFeatureExtractionTestMixin


if is_speech_available():
						from transformers import SpeechaTextFeatureExtractor

A__	:    str			 = random.Random()

def 			_snake_case    (					lowerCamelCase__     :   Optional[Any]    ,						lowerCamelCase__     :   Union[str, Any]=1.0    ,						lowerCamelCase__     :   List[str]=None    ,						lowerCamelCase__     :   str=None  )    ->     Dict:
							if rng is None:
														lowerCamelCase_	:							Union[str, Any]									=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







@require_torch
@require_torchaudio
class        lowercase__      (							unittest.TestCase       ):


			def __init__(				self					:  Union[str, Any]       ,	snake_case__					:  Optional[Any]       ,	snake_case__					:  List[Any]=7       ,	snake_case__					:  Union[str, Any]=400       ,	snake_case__					:  Union[str, Any]=2000       ,	snake_case__					:  Any=24       ,	snake_case__					:  Union[str, Any]=24       ,	snake_case__					:  Optional[Any]=0.0       ,	snake_case__					:  Optional[int]=1_6000       ,	snake_case__					:  str=True       ,	snake_case__					:  List[Any]=True       ,	):
										lowerCamelCase_	:							Tuple									=parent
										lowerCamelCase_	:							Tuple									=batch_size
										lowerCamelCase_	:							Tuple									=min_seq_length
										lowerCamelCase_	:							Any									=max_seq_length
										lowerCamelCase_	:							Union[str, Any]									=(self.max_seq_length - self.min_seq_length) // (self.batch_size - 1)
										lowerCamelCase_	:							Union[str, Any]									=feature_size
										lowerCamelCase_	:							str									=num_mel_bins
										lowerCamelCase_	:							Tuple									=padding_value
										lowerCamelCase_	:							str									=sampling_rate
										lowerCamelCase_	:							str									=return_attention_mask
										lowerCamelCase_	:							int									=do_normalize


			def      UpperCAmelCase__						(				self					:  Dict ):
										return {
										    "feature_size": self.feature_size,
										    "num_mel_bins": self.num_mel_bins,
										    "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					:  Union[str, Any]       ,	snake_case__					:  int=False       ,	snake_case__					:  Union[str, Any]=False ):
										def _flatten(snake_case__					:  List[str] ):
																	return list(itertools.chain(*snake_case__ ) )

										if equal_length:
																	lowerCamelCase_	:							Dict									=[floats_list((self.max_seq_length, self.feature_size) ) for _ in range(self.batch_size )]
										else:
																	# make sure that inputs increase in size
																	lowerCamelCase_	:							int									=[
																	    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_	:							Union[str, Any]									=[np.asarray(snake_case__ ) for x in speech_inputs]
										return speech_inputs







@require_torch
@require_torchaudio
class        lowercase__      (							snake_case__,       unittest.TestCase       ):
			_UpperCAmelCase		:int             =			SpeechaTextFeatureExtractor if is_speech_available() else None


			def      UpperCAmelCase__						(				self					:  Union[str, Any] ):
										lowerCamelCase_	:							str									=SpeechaTextFeatureExtractionTester(self )


			def      UpperCAmelCase__						(				self					:  List[Any]       ,	snake_case__					:  Optional[Any] ):
										self.assertTrue(np.all(np.mean(snake_case__       ,	axis=0 ) < 1E-3 ) )
										self.assertTrue(np.all(np.abs(np.var(snake_case__       ,	axis=0 ) - 1 ) < 1E-3 ) )


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

										# Test feature size
										lowerCamelCase_	:							Tuple									=feature_extractor(snake_case__       ,	padding=snake_case__       ,	return_tensors="np" ).input_features
										self.assertTrue(input_features.ndim == 3 )
										self.assertTrue(input_features.shape[-1] == feature_extractor.feature_size )

										# Test not batched input
										lowerCamelCase_	:							Union[str, Any]									=feature_extractor(speech_inputs[0]       ,	return_tensors="np" ).input_features
										lowerCamelCase_	:							List[str]									=feature_extractor(np_speech_inputs[0]       ,	return_tensors="np" ).input_features
										self.assertTrue(np.allclose(snake_case__       ,	snake_case__       ,	atol=1E-3 ) )

										# Test batched
										lowerCamelCase_	:							Tuple									=feature_extractor(snake_case__       ,	return_tensors="np" ).input_features
										lowerCamelCase_	:							List[str]									=feature_extractor(snake_case__       ,	return_tensors="np" ).input_features
										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.
										lowerCamelCase_	:							Tuple									=[floats_list((1, x) )[0] for x in (800, 800, 800)]
										lowerCamelCase_	:							Optional[Any]									=np.asarray(snake_case__ )
										lowerCamelCase_	:							Optional[int]									=feature_extractor(snake_case__       ,	return_tensors="np" ).input_features
										lowerCamelCase_	:							Optional[int]									=feature_extractor(snake_case__       ,	return_tensors="np" ).input_features
										for enc_seq_a, enc_seq_a in zip(snake_case__       ,	snake_case__ ):
																	self.assertTrue(np.allclose(snake_case__       ,	snake_case__       ,	atol=1E-3 ) )


			def      UpperCAmelCase__						(				self					:  List[Any] ):
										lowerCamelCase_	:							Optional[int]									=self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict() )
										lowerCamelCase_	:							Any									=[floats_list((1, x) )[0] for x in range(800       ,	1400       ,	200 )]

										lowerCamelCase_	:							List[Any]									=["longest", "max_length", "do_not_pad"]
										lowerCamelCase_	:							Optional[Any]									=[None, 16, None]
										for max_length, padding in zip(snake_case__       ,	snake_case__ ):
																	lowerCamelCase_	:							Optional[int]									=feature_extractor(
																	    snake_case__       ,	padding=snake_case__       ,	max_length=snake_case__       ,	return_attention_mask=snake_case__ )
																	lowerCamelCase_	:							Dict									=inputs.input_features
																	lowerCamelCase_	:							Any									=inputs.attention_mask
																	lowerCamelCase_	:							List[Any]									=[np.sum(snake_case__ ) for x in attention_mask]

																	self._check_zero_mean_unit_variance(input_features[0][: fbank_feat_lengths[0]] )
																	self._check_zero_mean_unit_variance(input_features[1][: fbank_feat_lengths[1]] )
																	self._check_zero_mean_unit_variance(input_features[2][: fbank_feat_lengths[2]] )


			def      UpperCAmelCase__						(				self					:  int ):
										lowerCamelCase_	:							Any									=self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict() )
										lowerCamelCase_	:							Union[str, Any]									=[floats_list((1, x) )[0] for x in range(800       ,	1400       ,	200 )]

										lowerCamelCase_	:							List[str]									=["longest", "max_length", "do_not_pad"]
										lowerCamelCase_	:							Union[str, Any]									=[None, 16, None]
										for max_length, padding in zip(snake_case__       ,	snake_case__ ):
																	lowerCamelCase_	:							str									=feature_extractor(
																	    snake_case__       ,	max_length=snake_case__       ,	padding=snake_case__       ,	return_tensors="np"       ,	return_attention_mask=snake_case__ )
																	lowerCamelCase_	:							List[Any]									=inputs.input_features
																	lowerCamelCase_	:							Dict									=inputs.attention_mask
																	lowerCamelCase_	:							List[Any]									=[np.sum(snake_case__ ) for x in attention_mask]

																	self._check_zero_mean_unit_variance(input_features[0][: fbank_feat_lengths[0]] )
																	self.assertTrue(input_features[0][fbank_feat_lengths[0] :].sum() < 1E-6 )
																	self._check_zero_mean_unit_variance(input_features[1][: fbank_feat_lengths[1]] )
																	self.assertTrue(input_features[0][fbank_feat_lengths[1] :].sum() < 1E-6 )
																	self._check_zero_mean_unit_variance(input_features[2][: fbank_feat_lengths[2]] )


			def      UpperCAmelCase__						(				self					:  List[Any] ):
										lowerCamelCase_	:							List[str]									=self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict() )
										lowerCamelCase_	:							int									=[floats_list((1, x) )[0] for x in range(800       ,	1400       ,	200 )]
										lowerCamelCase_	:							str									=feature_extractor(
										    snake_case__       ,	padding="max_length"       ,	max_length=4       ,	truncation=snake_case__       ,	return_tensors="np"       ,	return_attention_mask=snake_case__       ,	)
										lowerCamelCase_	:							List[Any]									=inputs.input_features
										lowerCamelCase_	:							str									=inputs.attention_mask
										lowerCamelCase_	:							Tuple									=np.sum(attention_mask == 1       ,	axis=1 )

										self._check_zero_mean_unit_variance(input_features[0, : fbank_feat_lengths[0]] )
										self._check_zero_mean_unit_variance(input_features[1] )
										self._check_zero_mean_unit_variance(input_features[2] )


			def      UpperCAmelCase__						(				self					:  str ):
										lowerCamelCase_	:							List[Any]									=self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict() )
										lowerCamelCase_	:							Union[str, Any]									=[floats_list((1, x) )[0] for x in range(800       ,	1400       ,	200 )]
										lowerCamelCase_	:							List[str]									=feature_extractor(
										    snake_case__       ,	padding="longest"       ,	max_length=4       ,	truncation=snake_case__       ,	return_tensors="np"       ,	return_attention_mask=snake_case__       ,	)
										lowerCamelCase_	:							List[str]									=inputs.input_features
										lowerCamelCase_	:							List[Any]									=inputs.attention_mask
										lowerCamelCase_	:							Optional[int]									=np.sum(attention_mask == 1       ,	axis=1 )

										self._check_zero_mean_unit_variance(input_features[0, : fbank_feat_lengths[0]] )
										self._check_zero_mean_unit_variance(input_features[1, : fbank_feat_lengths[1]] )
										self._check_zero_mean_unit_variance(input_features[2] )

										# make sure that if max_length < longest -> then pad to max_length
										self.assertEqual(input_features.shape       ,	(3, 4, 24) )

										lowerCamelCase_	:							List[Any]									=[floats_list((1, x) )[0] for x in range(800       ,	1400       ,	200 )]
										lowerCamelCase_	:							Any									=feature_extractor(
										    snake_case__       ,	padding="longest"       ,	max_length=16       ,	truncation=snake_case__       ,	return_tensors="np"       ,	return_attention_mask=snake_case__       ,	)
										lowerCamelCase_	:							int									=inputs.input_features
										lowerCamelCase_	:							List[str]									=inputs.attention_mask
										lowerCamelCase_	:							Optional[Any]									=np.sum(attention_mask == 1       ,	axis=1 )

										self._check_zero_mean_unit_variance(input_features[0, : fbank_feat_lengths[0]] )
										self._check_zero_mean_unit_variance(input_features[1, : fbank_feat_lengths[1]] )
										self._check_zero_mean_unit_variance(input_features[2] )

										# make sure that if max_length < longest -> then pad to max_length
										self.assertEqual(input_features.shape       ,	(3, 6, 24) )


			def      UpperCAmelCase__						(				self					:  Dict ):
										import torch

										lowerCamelCase_	:							Optional[int]									=self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict() )
										lowerCamelCase_	:							Optional[int]									=np.random.rand(100       ,	32 ).astype(np.floataa )
										lowerCamelCase_	:							Tuple									=np_speech_inputs.tolist()

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


			def      UpperCAmelCase__						(				self					:  List[Any]       ,	snake_case__					:  Optional[int] ):
										from datasets import load_dataset

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

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


			def      UpperCAmelCase__						(				self					:  int ):
										# fmt: off
										lowerCamelCase_	:							Dict									=np.array([
										    -1.5_745, -1.7_713, -1.7_020, -1.6_069, -1.2_250, -1.1_105, -0.9_072, -0.8_241,
										    -1.2_310, -0.8_098, -0.3_320, -0.4_101, -0.7_985, -0.4_996, -0.8_213, -0.9_128,
										    -1.0_420, -1.1_286, -1.0_440, -0.7_999, -0.8_405, -1.2_275, -1.5_443, -1.4_625,
										] )
										# fmt: on

										lowerCamelCase_	:							List[str]									=self._load_datasamples(1 )
										lowerCamelCase_	:							Optional[int]									=self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict() )
										lowerCamelCase_	:							Tuple									=feature_extractor(snake_case__       ,	return_tensors="pt" ).input_features
										self.assertEquals(input_features.shape       ,	(1, 584, 24) )
										self.assertTrue(np.allclose(input_features[0, 0, :30]       ,	snake_case__       ,	atol=1E-4 ) )











"""simple docstring"""



import copy
import json
import os
import tempfile

from transformers import is_torch_available

from .test_configuration_utils import config_common_kwargs







class        lowercase__      (							snake_case__       ):


			def __init__(				self					:  Tuple       ,	snake_case__					:  Optional[int]       ,	snake_case__					:  int=None       ,	snake_case__					:  Union[str, Any]=True       ,	snake_case__					:  Optional[int]=None       ,	**snake_case__					:  Optional[int] ):
										lowerCamelCase_	:							Dict									=parent
										lowerCamelCase_	:							List[str]									=config_class
										lowerCamelCase_	:							Union[str, Any]									=has_text_modality
										lowerCamelCase_	:							Optional[int]									=kwargs
										lowerCamelCase_	:							List[str]									=common_properties


			def      UpperCAmelCase__						(				self					:  Optional[Any] ):
										lowerCamelCase_	:							List[str]									=self.config_class(**self.inputs_dict )
										lowerCamelCase_	:							Any									=(
										    ["hidden_size", "num_attention_heads", "num_hidden_layers"]
										    if self.common_properties is None
										    else self.common_properties
										)

										# Add common fields for text models
										if self.has_text_modality:
																	common_properties.extend(["vocab_size"] )

										# Test that config has the common properties as getters
										for prop in common_properties:
																	self.parent.assertTrue(hasattr(snake_case__       ,	snake_case__ )       ,	msg=F"""`{prop}` does not exist""" )

										# Test that config has the common properties as setter
										for idx, name in enumerate(snake_case__ ):
																	try:
																								setattr(snake_case__       ,	snake_case__       ,	snake_case__ )
																								self.parent.assertEqual(
																								    getattr(snake_case__       ,	snake_case__ )       ,	snake_case__       ,	msg=F"""`{name} value {idx} expected, but was {getattr(snake_case__       ,	snake_case__ )}""" )
																	except NotImplementedError:
																								# Some models might not be able to implement setters for common_properties
																								# In that case, a NotImplementedError is raised
																								pass

        # Test if config class can be called with Config(prop_name=..)
										for idx, name in enumerate(snake_case__ ):
																	try:
																								lowerCamelCase_	:							Dict									=self.config_class(**{name: idx} )
																								self.parent.assertEqual(
																								    getattr(snake_case__       ,	snake_case__ )       ,	snake_case__       ,	msg=F"""`{name} value {idx} expected, but was {getattr(snake_case__       ,	snake_case__ )}""" )
																	except NotImplementedError:
																								# Some models might not be able to implement setters for common_properties
																								# In that case, a NotImplementedError is raised
																								pass


			def      UpperCAmelCase__						(				self					:  Any ):
										lowerCamelCase_	:							Tuple									=self.config_class(**self.inputs_dict )
										lowerCamelCase_	:							Any									=json.loads(config.to_json_string() )
										for key, value in self.inputs_dict.items():
																	self.parent.assertEqual(obj[key]       ,	snake_case__ )


			def      UpperCAmelCase__						(				self					:  int ):
										lowerCamelCase_	:							Tuple									=self.config_class(**self.inputs_dict )

										with tempfile.TemporaryDirectory() as tmpdirname:
																	lowerCamelCase_	:							List[Any]									=os.path.join(snake_case__       ,	"config.json" )
																	config_first.to_json_file(snake_case__ )
																	lowerCamelCase_	:							Optional[int]									=self.config_class.from_json_file(snake_case__ )

										self.parent.assertEqual(config_second.to_dict()       ,	config_first.to_dict() )


			def      UpperCAmelCase__						(				self					:  Dict ):
										lowerCamelCase_	:							Dict									=self.config_class(**self.inputs_dict )

										with tempfile.TemporaryDirectory() as tmpdirname:
																	config_first.save_pretrained(snake_case__ )
																	lowerCamelCase_	:							Optional[int]									=self.config_class.from_pretrained(snake_case__ )

										self.parent.assertEqual(config_second.to_dict()       ,	config_first.to_dict() )


			def      UpperCAmelCase__						(				self					:  str ):
										lowerCamelCase_	:							Dict									=self.config_class(**self.inputs_dict )

										lowerCamelCase_	:							Dict									="test"
										with tempfile.TemporaryDirectory() as tmpdirname:
																	lowerCamelCase_	:							str									=os.path.join(snake_case__       ,	snake_case__ )
																	config_first.save_pretrained(snake_case__ )
																	lowerCamelCase_	:							Optional[Any]									=self.config_class.from_pretrained(snake_case__       ,	subfolder=snake_case__ )

										self.parent.assertEqual(config_second.to_dict()       ,	config_first.to_dict() )


			def      UpperCAmelCase__						(				self					:  Optional[Any] ):
										lowerCamelCase_	:							Optional[Any]									=self.config_class(**self.inputs_dict       ,	num_labels=5 )
										self.parent.assertEqual(len(config.idalabel )       ,	5 )
										self.parent.assertEqual(len(config.labelaid )       ,	5 )

										lowerCamelCase_	:							List[Any]									=3
										self.parent.assertEqual(len(config.idalabel )       ,	3 )
										self.parent.assertEqual(len(config.labelaid )       ,	3 )


			def      UpperCAmelCase__						(				self					:  List[Any] ):
										if self.config_class.is_composition:
																	return
										lowerCamelCase_	:							Tuple									=self.config_class()
										self.parent.assertIsNotNone(snake_case__ )


			def      UpperCAmelCase__						(				self					:  List[Any] ):
										lowerCamelCase_	:							List[str]									=copy.deepcopy(snake_case__ )
										lowerCamelCase_	:							Optional[int]									=self.config_class(**snake_case__ )
										lowerCamelCase_	:							Union[str, Any]									=[]
										for key, value in config_common_kwargs.items():
																	if key == "torch_dtype":
																								if not is_torch_available():
																															continue
																								else:
																															import torch

																															if config.torch_dtype != torch.floataa:
																																						wrong_values.append(("torch_dtype", config.torch_dtype, torch.floataa) )
																	elif getattr(snake_case__       ,	snake_case__ ) != value:
																								wrong_values.append((key, getattr(snake_case__       ,	snake_case__ ), value) )

										if len(snake_case__ ) > 0:
																	lowerCamelCase_	:							Any									="\n".join([F"""- {v[0]}: got {v[1]} instead of {v[2]}""" for v in wrong_values] )
																	raise ValueError(F"""The following keys were not properly set in the config:\n{errors}""" )


			def      UpperCAmelCase__						(				self					:  int ):
										self.create_and_test_config_common_properties()
										self.create_and_test_config_to_json_string()
										self.create_and_test_config_to_json_file()
										self.create_and_test_config_from_and_save_pretrained()
										self.create_and_test_config_from_and_save_pretrained_subfolder()
										self.create_and_test_config_with_num_labels()
										self.check_config_can_be_init_without_params()
										self.check_config_arguments_init()



