


import itertools
import os
import random
import tempfile
import unittest

import numpy as np
from datasets import load_dataset

from transformers import is_speech_available
from transformers.testing_utils import check_json_file_has_correct_format, require_torch, require_torchaudio
from transformers.utils.import_utils import is_torch_available

from ...test_sequence_feature_extraction_common import SequenceFeatureExtractionTestMixin


if is_speech_available():
	from transformers import WhisperFeatureExtractor

if is_torch_available():
	import torch

A			:						Union[str, Any]        =  random.Random()






def 					UpperCamelCase   (      __magic_name__	:				Tuple       ,       __magic_name__	:				List[Any]=1.0       ,       __magic_name__	:				Dict=None       ,       __magic_name__	:				List[Any]=None	)  ->    Optional[Any]:
		"""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



@require_torch
@require_torchaudio
class 				A       (			unittest.TestCase				):
					'''simple docstring'''






					def __init__(self       :		Union[str, Any] ,   _UpperCAmelCase       :		Union[str, Any] ,   _UpperCAmelCase       :		List[Any]=7 ,   _UpperCAmelCase       :		int=400 ,   _UpperCAmelCase       :		Tuple=2000 ,   _UpperCAmelCase       :		str=10 ,   _UpperCAmelCase       :		Union[str, Any]=160 ,   _UpperCAmelCase       :		List[Any]=8 ,   _UpperCAmelCase       :		List[str]=0.0 ,   _UpperCAmelCase       :		int=4000 ,   _UpperCAmelCase       :		Optional[int]=False ,   _UpperCAmelCase       :		Optional[int]=True ,   )		->  Tuple:




							"""simple docstring"""


							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__	     =						padding_value
							lowercase__	     =						sampling_rate
							lowercase__	     =						return_attention_mask
							lowercase__	     =						do_normalize
							lowercase__	     =						feature_size
							lowercase__	     =						chunk_length
							lowercase__	     =						hop_length

					def     lowerCamelCase__	(self       :		str						)		->  Any:




							"""simple docstring"""


							return {
							    "feature_size": self.feature_size,
							    "hop_length": self.hop_length,
							    "chunk_length": self.chunk_length,
							    "padding_value": self.padding_value,
							    "sampling_rate": self.sampling_rate,
							    "return_attention_mask": self.return_attention_mask,
							    "do_normalize": self.do_normalize,
							}


					def     lowerCamelCase__	(self       :		Dict ,   _UpperCAmelCase       :		str=False ,   _UpperCAmelCase       :		Optional[int]=False						)		->  Tuple:




							"""simple docstring"""


							def _flatten(_UpperCAmelCase       :		Any						):
									return list(itertools.chain(*_UpperCAmelCase						)						)

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






@require_torch
@require_torchaudio
class 				A       (			UpperCAmelCase__					,    unittest.TestCase				):
					'''simple docstring'''






					A__     =					WhisperFeatureExtractor if is_speech_available() else None

					def     lowerCamelCase__	(self       :		List[Any]						)		->  Any:




							"""simple docstring"""


							lowercase__	     =						WhisperFeatureExtractionTester(self						)

					def     lowerCamelCase__	(self       :		Union[str, Any]						)		->  List[str]:




							"""simple docstring"""


							lowercase__	     =						self.feature_extraction_class(**self.feat_extract_dict						)

							with tempfile.TemporaryDirectory() as tmpdirname:
									lowercase__	     =						feat_extract_first.save_pretrained(_UpperCAmelCase						)[0]
									check_json_file_has_correct_format(_UpperCAmelCase						)
									lowercase__	     =						self.feature_extraction_class.from_pretrained(_UpperCAmelCase						)

							lowercase__	     =						feat_extract_first.to_dict()
							lowercase__	     =						feat_extract_second.to_dict()
							lowercase__	     =						feat_extract_first.mel_filters
							lowercase__	     =						feat_extract_second.mel_filters
							self.assertTrue(np.allclose(_UpperCAmelCase ,   _UpperCAmelCase						)						)
							self.assertEqual(_UpperCAmelCase ,   _UpperCAmelCase						)

					def     lowerCamelCase__	(self       :		List[Any]						)		->  List[Any]:




							"""simple docstring"""


							lowercase__	     =						self.feature_extraction_class(**self.feat_extract_dict						)

							with tempfile.TemporaryDirectory() as tmpdirname:
									lowercase__	     =						os.path.join(_UpperCAmelCase ,   """feat_extract.json"""						)
									feat_extract_first.to_json_file(_UpperCAmelCase						)
									lowercase__	     =						self.feature_extraction_class.from_json_file(_UpperCAmelCase						)

							lowercase__	     =						feat_extract_first.to_dict()
							lowercase__	     =						feat_extract_second.to_dict()
							lowercase__	     =						feat_extract_first.mel_filters
							lowercase__	     =						feat_extract_second.mel_filters
							self.assertTrue(np.allclose(_UpperCAmelCase ,   _UpperCAmelCase						)						)
							self.assertEqual(_UpperCAmelCase ,   _UpperCAmelCase						)

					def     lowerCamelCase__	(self       :		List[str]						)		->  str:




							"""simple docstring"""


							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(_UpperCAmelCase						) for speech_input in speech_inputs]

							# Test feature size
							lowercase__	     =						feature_extractor(_UpperCAmelCase ,   padding="""max_length""" ,   return_tensors="""np"""						).input_features
							self.assertTrue(input_features.ndim == 3						)
							self.assertTrue(input_features.shape[-1] == feature_extractor.nb_max_frames						)
							self.assertTrue(input_features.shape[-2] == feature_extractor.feature_size						)

							# Test not batched input
							lowercase__	     =						feature_extractor(speech_inputs[0] ,   return_tensors="""np"""						).input_features
							lowercase__	     =						feature_extractor(np_speech_inputs[0] ,   return_tensors="""np"""						).input_features
							self.assertTrue(np.allclose(_UpperCAmelCase ,   _UpperCAmelCase ,   atol=1E-3						)						)

							# Test batched
							lowercase__	     =						feature_extractor(_UpperCAmelCase ,   return_tensors="""np"""						).input_features
							lowercase__	     =						feature_extractor(_UpperCAmelCase ,   return_tensors="""np"""						).input_features
							for enc_seq_a, enc_seq_a in zip(_UpperCAmelCase ,   _UpperCAmelCase						):
									self.assertTrue(np.allclose(_UpperCAmelCase ,   _UpperCAmelCase ,   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(_UpperCAmelCase						)
							lowercase__	     =						feature_extractor(_UpperCAmelCase ,   return_tensors="""np"""						).input_features
							lowercase__	     =						feature_extractor(_UpperCAmelCase ,   return_tensors="""np"""						).input_features
							for enc_seq_a, enc_seq_a in zip(_UpperCAmelCase ,   _UpperCAmelCase						):
									self.assertTrue(np.allclose(_UpperCAmelCase ,   _UpperCAmelCase ,   atol=1E-3						)						)

							# Test truncation required
							lowercase__	     =						[floats_list((1, x)						)[0] for x in range(200 ,   (feature_extractor.n_samples + 500) ,   200						)]
							lowercase__	     =						[np.asarray(_UpperCAmelCase						) for speech_input in speech_inputs]

							lowercase__	     =						[x[: feature_extractor.n_samples] for x in speech_inputs]
							lowercase__	     =						[np.asarray(_UpperCAmelCase						) for speech_input in speech_inputs_truncated]

							lowercase__	     =						feature_extractor(_UpperCAmelCase ,   return_tensors="""np"""						).input_features
							lowercase__	     =						feature_extractor(_UpperCAmelCase ,   return_tensors="""np"""						).input_features
							for enc_seq_a, enc_seq_a in zip(_UpperCAmelCase ,   _UpperCAmelCase						):
									self.assertTrue(np.allclose(_UpperCAmelCase ,   _UpperCAmelCase ,   atol=1E-3						)						)

					def     lowerCamelCase__	(self       :		List[str]						)		->  Any:




							"""simple docstring"""


							import torch

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

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

					def     lowerCamelCase__	(self       :		List[str] ,   _UpperCAmelCase       :		Union[str, Any]						)		->  List[Any]:




							"""simple docstring"""


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

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

					def     lowerCamelCase__	(self       :		Union[str, Any]						)		->  int:




							"""simple docstring"""


							lowercase__	     =						torch.tensor(
							    [
							        0.1_193, -0.0_946, -0.1_098, -0.0_196, 0.0_225, -0.0_690, -0.1_736, 0.0_951,
							        0.0_971, -0.0_817, -0.0_702, 0.0_162, 0.0_260, 0.0_017, -0.0_192, -0.1_678,
							        0.0_709, -0.1_867, -0.0_655, -0.0_274, -0.0_234, -0.1_884, -0.0_516, -0.0_554,
							        -0.0_274, -0.1_425, -0.1_423, 0.0_837, 0.0_377, -0.0_854
							    ]						)
							# fmt: on

							lowercase__	     =						self._load_datasamples(1						)
							lowercase__	     =						WhisperFeatureExtractor()
							lowercase__	     =						feature_extractor(_UpperCAmelCase ,   return_tensors="""pt"""						).input_features
							self.assertEqual(input_features.shape ,   (1, 80, 3000)						)
							self.assertTrue(torch.allclose(input_features[0, 0, :30] ,   _UpperCAmelCase ,   atol=1E-4						)						)


					def     lowerCamelCase__	(self       :		Optional[Any]						)		->  Optional[Any]:




							"""simple docstring"""


							lowercase__	     =						self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()						)
							lowercase__	     =						self._load_datasamples(1						)[0]
							lowercase__	     =						((audio - audio.min()) / (audio.max() - audio.min())) * 6_5535  # Rescale to [0, 65535] to show issue
							lowercase__	     =						feat_extract.zero_mean_unit_var_norm([audio] ,   attention_mask=_UpperCAmelCase						)[0]

							self.assertTrue(np.all(np.mean(_UpperCAmelCase						) < 1E-3						)						)
							self.assertTrue(np.all(np.abs(np.var(_UpperCAmelCase						) - 1						) < 1E-3						)						)





from collections import OrderedDict
from typing import Mapping

from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfig
from ...utils import logging


A			:						List[str]        =  logging.get_logger(__name__)

A			:						Tuple        =  {
    'kssteven/ibert-roberta-base': 'https://huggingface.co/kssteven/ibert-roberta-base/resolve/main/config.json',
    'kssteven/ibert-roberta-large': 'https://huggingface.co/kssteven/ibert-roberta-large/resolve/main/config.json',
    'kssteven/ibert-roberta-large-mnli': (
        'https://huggingface.co/kssteven/ibert-roberta-large-mnli/resolve/main/config.json'
    ),
}



class 				A       (			UpperCAmelCase__				):
					'''simple docstring'''






					A__     =					'''ibert'''

					def __init__(self       :		int ,   _UpperCAmelCase       :		Union[str, Any]=3_0522 ,   _UpperCAmelCase       :		Any=768 ,   _UpperCAmelCase       :		Optional[Any]=12 ,   _UpperCAmelCase       :		Union[str, Any]=12 ,   _UpperCAmelCase       :		List[Any]=3072 ,   _UpperCAmelCase       :		List[Any]="gelu" ,   _UpperCAmelCase       :		List[str]=0.1 ,   _UpperCAmelCase       :		Union[str, Any]=0.1 ,   _UpperCAmelCase       :		Optional[int]=512 ,   _UpperCAmelCase       :		str=2 ,   _UpperCAmelCase       :		Union[str, Any]=0.02 ,   _UpperCAmelCase       :		Dict=1E-1_2 ,   _UpperCAmelCase       :		int=1 ,   _UpperCAmelCase       :		str=0 ,   _UpperCAmelCase       :		Tuple=2 ,   _UpperCAmelCase       :		int="absolute" ,   _UpperCAmelCase       :		Any=False ,   _UpperCAmelCase       :		List[Any]="none" ,   **_UpperCAmelCase       :		List[Any] ,   )		->  Any:




							"""simple docstring"""


							super().__init__(pad_token_id=_UpperCAmelCase ,   bos_token_id=_UpperCAmelCase ,   eos_token_id=_UpperCAmelCase ,   **_UpperCAmelCase						)

							lowercase__	     =						vocab_size
							lowercase__	     =						hidden_size
							lowercase__	     =						num_hidden_layers
							lowercase__	     =						num_attention_heads
							lowercase__	     =						hidden_act
							lowercase__	     =						intermediate_size
							lowercase__	     =						hidden_dropout_prob
							lowercase__	     =						attention_probs_dropout_prob
							lowercase__	     =						max_position_embeddings
							lowercase__	     =						type_vocab_size
							lowercase__	     =						initializer_range
							lowercase__	     =						layer_norm_eps
							lowercase__	     =						position_embedding_type
							lowercase__	     =						quant_mode
							lowercase__	     =						force_dequant






class 				A       (			UpperCAmelCase__				):
					'''simple docstring'''






					@property
					def     lowerCamelCase__	(self       :		Dict						)		->  Mapping[str, Mapping[int, str]]:




							"""simple docstring"""


							if self.task == "multiple-choice":
									lowercase__	     =						{0: """batch""", 1: """choice""", 2: """sequence"""}
							else:
									lowercase__	     =						{0: """batch""", 1: """sequence"""}
							return OrderedDict(
							    [
							        ("""input_ids""", dynamic_axis),
							        ("""attention_mask""", dynamic_axis),
							    ]						)

