
"""simple docstring"""







import itertools
import random
import unittest

import numpy as np

from transformers import BatchFeature, SpeechTaFeatureExtractor
from transformers.testing_utils import require_torch
from transformers.utils.import_utils import is_torch_available

from ...test_sequence_feature_extraction_common import SequenceFeatureExtractionTestMixin


if is_torch_available():
		import torch


_SCREAMING_SNAKE_CASE	       =      random.Random()
def   __UpperCamelCase   (					SCREAMING_SNAKE_CASE							,    SCREAMING_SNAKE_CASE=1.0							,    SCREAMING_SNAKE_CASE=None							,    SCREAMING_SNAKE_CASE=None )  ->     List[Any]:







							"""simple docstring"""


							if rng is None:
														__snake_case     			=       global_rng

							__snake_case     			=       []
							for batch_idx in range(shape[0] ):
														values.append([] )
														for _ in range(shape[1] ):
																					values[-1].append(rng.random() * scale )

							return values







@require_torch
class 							__magic_name__     (		unittest.TestCase   ):





					def __init__(				self   :							Optional[int]							,						snake_case_   :							Union[str, Any]							,						snake_case_   :							List[Any]=7							,						snake_case_   :							str=400							,						snake_case_   :							Any=2000							,						snake_case_   :							Optional[Any]=1							,						snake_case_   :							List[Any]=0.0							,						snake_case_   :							str=16000							,						snake_case_   :							Any=True							,						snake_case_   :							List[Any]=80							,						snake_case_   :							Dict=16							,						snake_case_   :							List[Any]=64							,						snake_case_   :							Optional[Any]="hann_window"							,						snake_case_   :							Union[str, Any]=80							,						snake_case_   :							Dict=7600							,						snake_case_   :							Optional[Any]=1e-10							,						snake_case_   :							List[str]=True							,						):
												__snake_case     			=       parent
												__snake_case     			=       batch_size
												__snake_case     			=       min_seq_length
												__snake_case     			=       max_seq_length
												__snake_case     			=       (self.max_seq_length - self.min_seq_length) // (self.batch_size - 1)
												__snake_case     			=       feature_size
												__snake_case     			=       padding_value
												__snake_case     			=       sampling_rate
												__snake_case     			=       do_normalize
												__snake_case     			=       num_mel_bins
												__snake_case     			=       hop_length
												__snake_case     			=       win_length
												__snake_case     			=       win_function
												__snake_case     			=       fmin
												__snake_case     			=       fmax
												__snake_case     			=       mel_floor
												__snake_case     			=       return_attention_mask





					def 							lowerCAmelCase      (				self   :							Optional[Any]    ):
												return {
												    "feature_size": self.feature_size,
												    "padding_value": self.padding_value,
												    "sampling_rate": self.sampling_rate,
												    "do_normalize": self.do_normalize,
												    "num_mel_bins": self.num_mel_bins,
												    "hop_length": self.hop_length,
												    "win_length": self.win_length,
												    "win_function": self.win_function,
												    "fmin": self.fmin,
												    "fmax": self.fmax,
												    "mel_floor": self.mel_floor,
												    "return_attention_mask": self.return_attention_mask,
												}





					def 							lowerCAmelCase      (				self   :							Optional[int]							,						snake_case_   :							Optional[int]=False							,						snake_case_   :							str=False    ):
												def _flatten(snake_case_   :							Optional[int]    ):
																			return list(itertools.chain(*snake_case_    )    )

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

												return speech_inputs





					def 							lowerCAmelCase      (				self   :							str							,						snake_case_   :							Any=False							,						snake_case_   :							Any=False    ):
												if equal_length:
																			__snake_case     			=       [floats_list((self.max_seq_length, self.num_mel_bins)    ) for _ in range(self.batch_size    )]
												else:
																			# make sure that inputs increase in size
																			__snake_case     			=       [
																			    floats_list((x, self.num_mel_bins)    )
																			    for x in range(self.min_seq_length							,						self.max_seq_length							,						self.seq_length_diff    )
																			]

												if numpify:
																			__snake_case     			=       [np.asarray(snake_case_    ) for x in speech_inputs]

												return speech_inputs







@require_torch
class 							__magic_name__     (		lowercase__      ,		unittest.TestCase   ):
					_SCREAMING_SNAKE_CASE       :					Any     							=   SpeechTaFeatureExtractor





					def 							lowerCAmelCase      (				self   :							Optional[int]    ):
												__snake_case     			=       SpeechTaFeatureExtractionTester(self    )





					def 							lowerCAmelCase      (				self   :							str							,						snake_case_   :							List[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 							lowerCAmelCase      (				self   :							Optional[int]    ):
												# Tests that all call wrap to encode_plus and batch_encode_plus
												__snake_case     			=       self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()    )
												# create three inputs of length 800, 1000, and 1200
												__snake_case     			=       [floats_list((1, x)    )[0] for x in range(800							,						1400							,						200    )]
												__snake_case     			=       [np.asarray(snake_case_    ) for speech_input in speech_inputs]

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

												# Test batched
												__snake_case     			=       feat_extract(snake_case_							,						return_tensors="np"    ).input_values
												__snake_case     			=       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    )    )





					def 							lowerCAmelCase      (				self   :							Tuple    ):
												__snake_case     			=       self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()    )
												__snake_case     			=       [floats_list((1, x)    )[0] for x in range(800							,						1400							,						200    )]

												__snake_case     			=       ["longest", "max_length", "do_not_pad"]
												__snake_case     			=       [None, 1600, None]
												for max_length, padding in zip(snake_case_							,						snake_case_    ):
																			__snake_case     			=       feat_extract(snake_case_							,						padding=snake_case_							,						max_length=snake_case_							,						return_tensors="np"    )
																			__snake_case     			=       processed.input_values

																			self._check_zero_mean_unit_variance(input_values[0][:800]    )
																			self.assertTrue(input_values[0][800:].sum() < 1e-6    )
																			self._check_zero_mean_unit_variance(input_values[1][:1000]    )
																			self.assertTrue(input_values[0][1000:].sum() < 1e-6    )
																			self._check_zero_mean_unit_variance(input_values[2][:1200]    )





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

												__snake_case     			=       ["longest", "max_length", "do_not_pad"]
												__snake_case     			=       [None, 1600, None]

												for max_length, padding in zip(snake_case_							,						snake_case_    ):
																			__snake_case     			=       feat_extract(snake_case_							,						max_length=snake_case_							,						padding=snake_case_    )
																			__snake_case     			=       processed.input_values

																			self._check_zero_mean_unit_variance(input_values[0][:800]    )
																			self._check_zero_mean_unit_variance(input_values[1][:1000]    )
																			self._check_zero_mean_unit_variance(input_values[2][:1200]    )





					def 							lowerCAmelCase      (				self   :							Dict    ):
												__snake_case     			=       self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()    )
												__snake_case     			=       [floats_list((1, x)    )[0] for x in range(800							,						1400							,						200    )]
												__snake_case     			=       feat_extract(
												    snake_case_							,						truncation=snake_case_							,						max_length=1000							,						padding="max_length"							,						return_tensors="np"    )
												__snake_case     			=       processed.input_values

												self._check_zero_mean_unit_variance(input_values[0, :800]    )
												self._check_zero_mean_unit_variance(input_values[1]    )
												self._check_zero_mean_unit_variance(input_values[2]    )





					def 							lowerCAmelCase      (				self   :							Optional[Any]    ):
												__snake_case     			=       self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()    )
												__snake_case     			=       [floats_list((1, x)    )[0] for x in range(800							,						1400							,						200    )]
												__snake_case     			=       feat_extract(
												    snake_case_							,						truncation=snake_case_							,						max_length=1000							,						padding="longest"							,						return_tensors="np"    )
												__snake_case     			=       processed.input_values

												self._check_zero_mean_unit_variance(input_values[0, :800]    )
												self._check_zero_mean_unit_variance(input_values[1, :1000]    )
												self._check_zero_mean_unit_variance(input_values[2]    )

												# make sure that if max_length < longest -> then pad to max_length
												self.assertTrue(input_values.shape == (3, 1000)    )

												__snake_case     			=       [floats_list((1, x)    )[0] for x in range(800							,						1400							,						200    )]
												__snake_case     			=       feat_extract(
												    snake_case_							,						truncation=snake_case_							,						max_length=2000							,						padding="longest"							,						return_tensors="np"    )
												__snake_case     			=       processed.input_values

												self._check_zero_mean_unit_variance(input_values[0, :800]    )
												self._check_zero_mean_unit_variance(input_values[1, :1000]    )
												self._check_zero_mean_unit_variance(input_values[2]    )

												# make sure that if max_length > longest -> then pad to longest
												self.assertTrue(input_values.shape == (3, 1200)    )





					def 							lowerCAmelCase      (				self   :							Union[str, Any]    ):
												__snake_case     			=       self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()    )
												__snake_case     			=       np.random.rand(100    ).astype(np.floataa    )
												__snake_case     			=       np_speech_inputs.tolist()

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





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

												# Test feature size
												__snake_case     			=       feature_extractor(audio_target=snake_case_							,						padding=snake_case_							,						return_tensors="np"    ).input_values
												self.assertTrue(input_values.ndim == 3    )
												self.assertTrue(input_values.shape[-1] == feature_extractor.num_mel_bins    )

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

												# Test batched
												__snake_case     			=       feature_extractor(snake_case_							,						return_tensors="np"    ).input_values
												__snake_case     			=       feature_extractor(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.
												__snake_case     			=       [floats_list((1, x)    )[0] for x in (800, 800, 800)]
												__snake_case     			=       np.asarray(snake_case_    )
												__snake_case     			=       feature_extractor(snake_case_							,						return_tensors="np"    ).input_values
												__snake_case     			=       feature_extractor(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    )    )





					def 							lowerCAmelCase      (				self   :							int    ):
												__snake_case     			=       self.feat_extract_tester.prepare_inputs_for_target()
												__snake_case     			=       self.feature_extraction_class(**self.feat_extract_dict    )
												__snake_case     			=       feat_extract.model_input_names[0]

												__snake_case     			=       BatchFeature({input_name: speech_inputs}    )

												self.assertTrue(all(len(snake_case_    ) == len(snake_case_    ) for x, y in zip(snake_case_							,						processed_features[input_name]    )    )    )

												__snake_case     			=       self.feat_extract_tester.prepare_inputs_for_target(equal_length=snake_case_    )
												__snake_case     			=       BatchFeature({input_name: speech_inputs}							,						tensor_type="np"    )

												__snake_case     			=       processed_features[input_name]

												if len(batch_features_input.shape    ) < 3:
																			__snake_case     			=       batch_features_input[:, :, None]

												self.assertTrue(
												    batch_features_input.shape
												    == (self.feat_extract_tester.batch_size, len(speech_inputs[0]    ), self.feat_extract_tester.num_mel_bins)    )





					@require_torch
					def 							lowerCAmelCase      (				self   :							List[str]    ):
												__snake_case     			=       self.feat_extract_tester.prepare_inputs_for_target(equal_length=snake_case_    )
												__snake_case     			=       self.feature_extraction_class(**self.feat_extract_dict    )
												__snake_case     			=       feat_extract.model_input_names[0]

												__snake_case     			=       BatchFeature({input_name: speech_inputs}							,						tensor_type="pt"    )

												__snake_case     			=       processed_features[input_name]

												if len(batch_features_input.shape    ) < 3:
																			__snake_case     			=       batch_features_input[:, :, None]

												self.assertTrue(
												    batch_features_input.shape
												    == (self.feat_extract_tester.batch_size, len(speech_inputs[0]    ), self.feat_extract_tester.num_mel_bins)    )





					@require_torch
					def 							lowerCAmelCase      (				self   :							Union[str, Any]    ):
												__snake_case     			=       self.feature_extraction_class(**self.feat_extract_dict    )
												__snake_case     			=       self.feat_extract_tester.prepare_inputs_for_target()
												__snake_case     			=       feat_extract.model_input_names[0]

												__snake_case     			=       BatchFeature({input_name: speech_inputs}    )

												__snake_case     			=       feat_extract.num_mel_bins  # hack!

												__snake_case     			=       feat_extract.pad(snake_case_							,						padding="longest"							,						return_tensors="np"    )[input_name]
												__snake_case     			=       feat_extract.pad(snake_case_							,						padding="longest"							,						return_tensors="pt"    )[input_name]

												self.assertTrue(abs(input_np.astype(np.floataa    ).sum() - input_pt.numpy().astype(np.floataa    ).sum()    ) < 1e-2    )





					def 							lowerCAmelCase      (				self   :							Union[str, Any]    ):
												__snake_case     			=       self.feat_extract_dict
												__snake_case     			=       True
												__snake_case     			=       self.feature_extraction_class(**snake_case_    )
												__snake_case     			=       self.feat_extract_tester.prepare_inputs_for_target()
												__snake_case     			=       [len(snake_case_    ) for x in speech_inputs]
												__snake_case     			=       feat_extract.model_input_names[0]

												__snake_case     			=       BatchFeature({input_name: speech_inputs}    )

												__snake_case     			=       feat_extract.num_mel_bins  # hack!

												__snake_case     			=       feat_extract.pad(snake_case_							,						padding="longest"							,						return_tensors="np"    )
												self.assertIn("attention_mask"							,						snake_case_    )
												self.assertListEqual(list(processed.attention_mask.shape    )							,						list(processed[input_name].shape[:2]    )    )
												self.assertListEqual(processed.attention_mask.sum(-1    ).tolist()							,						snake_case_    )





					def 							lowerCAmelCase      (				self   :							Union[str, Any]    ):
												__snake_case     			=       self.feat_extract_dict
												__snake_case     			=       True
												__snake_case     			=       self.feature_extraction_class(**snake_case_    )
												__snake_case     			=       self.feat_extract_tester.prepare_inputs_for_target()
												__snake_case     			=       [len(snake_case_    ) for x in speech_inputs]
												__snake_case     			=       feat_extract.model_input_names[0]

												__snake_case     			=       BatchFeature({input_name: speech_inputs}    )
												__snake_case     			=       min(snake_case_    )

												__snake_case     			=       feat_extract.num_mel_bins  # hack!

												__snake_case     			=       feat_extract.pad(
												    snake_case_							,						padding="max_length"							,						max_length=snake_case_							,						truncation=snake_case_							,						return_tensors="np"    )
												self.assertIn("attention_mask"							,						snake_case_    )
												self.assertListEqual(
												    list(processed_pad.attention_mask.shape    )							,						[processed_pad[input_name].shape[0], max_length]    )
												self.assertListEqual(
												    processed_pad.attention_mask[:, :max_length].sum(-1    ).tolist()							,						[max_length for x in speech_inputs]    )





					def 							lowerCAmelCase      (				self   :							Tuple							,						snake_case_   :							Dict    ):
												from datasets import load_dataset

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

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





					def 							lowerCAmelCase      (				self   :							Optional[int]    ):
												# fmt: off
												__snake_case     			=       torch.tensor(
												    [2.38_04e-03, 2.07_52e-03, 1.98_36e-03, 2.10_57e-03, 1.61_74e-03,
												     3.05_18e-04, 9.15_53e-05, 3.35_69e-04, 9.76_56e-04, 1.83_11e-03,
												     2.01_42e-03, 2.10_57e-03, 1.73_95e-03, 4.57_76e-04, -3.96_73e-04,
												     4.57_76e-04, 1.00_71e-03, 9.15_53e-05, 4.88_28e-04, 1.15_97e-03,
												     7.32_42e-04, 9.46_04e-04, 1.80_05e-03, 1.83_11e-03, 8.85_01e-04,
												     4.27_25e-04, 4.88_28e-04, 7.32_42e-04, 1.09_86e-03, 2.10_57e-03]    )
												# fmt: on

												__snake_case     			=       self._load_datasamples(1    )
												__snake_case     			=       SpeechTaFeatureExtractor()
												__snake_case     			=       feature_extractor(snake_case_							,						return_tensors="pt"    ).input_values
												self.assertEquals(input_values.shape							,						(1, 93680)    )
												self.assertTrue(torch.allclose(input_values[0, :30]							,						snake_case_							,						atol=1e-6    )    )





					def 							lowerCAmelCase      (				self   :							Union[str, Any]    ):
												# fmt: off
												__snake_case     			=       torch.tensor(
												    [-2.6870, -3.0104, -3.1356, -3.5352, -3.0044, -3.0353, -3.4719, -3.6777,
												     -3.1520, -2.9435, -2.6553, -2.8795, -2.9944, -2.5921, -3.0279, -3.0386,
												     -3.0864, -3.1291, -3.2353, -2.7444, -2.6831, -2.7287, -3.1761, -3.1571,
												     -3.2726, -3.0582, -3.1007, -3.4533, -3.4695, -3.0998]    )
												# fmt: on

												__snake_case     			=       self._load_datasamples(1    )
												__snake_case     			=       SpeechTaFeatureExtractor()
												__snake_case     			=       feature_extractor(audio_target=snake_case_							,						return_tensors="pt"    ).input_values
												self.assertEquals(input_values.shape							,						(1, 366, 80)    )
												self.assertTrue(torch.allclose(input_values[0, 0, :30]							,						snake_case_							,						atol=1e-4    )    )


"""simple docstring"""







import unittest

from transformers import MobileBertConfig, is_torch_available
from transformers.models.auto import get_values
from transformers.testing_utils import require_sentencepiece, require_tokenizers, require_torch, slow, torch_device

from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
		import torch

		from transformers import (
		    MODEL_FOR_PRETRAINING_MAPPING,
		    MobileBertForMaskedLM,
		    MobileBertForMultipleChoice,
		    MobileBertForNextSentencePrediction,
		    MobileBertForPreTraining,
		    MobileBertForQuestionAnswering,
		    MobileBertForSequenceClassification,
		    MobileBertForTokenClassification,
		    MobileBertModel,
		)







class 							__magic_name__     :





					def __init__(				self   :							Optional[int]							,						snake_case_   :							List[str]							,						snake_case_   :							str=13							,						snake_case_   :							Optional[int]=7							,						snake_case_   :							Optional[Any]=True							,						snake_case_   :							Union[str, Any]=True							,						snake_case_   :							Tuple=True							,						snake_case_   :							int=True							,						snake_case_   :							int=99							,						snake_case_   :							Optional[int]=64							,						snake_case_   :							Dict=32							,						snake_case_   :							Dict=5							,						snake_case_   :							List[str]=4							,						snake_case_   :							List[Any]=37							,						snake_case_   :							Optional[Any]="gelu"							,						snake_case_   :							Optional[Any]=0.1							,						snake_case_   :							List[Any]=0.1							,						snake_case_   :							Union[str, Any]=512							,						snake_case_   :							int=16							,						snake_case_   :							List[str]=2							,						snake_case_   :							Union[str, Any]=0.02							,						snake_case_   :							Union[str, Any]=3							,						snake_case_   :							str=4							,						snake_case_   :							int=None							,						):
												__snake_case     			=       parent
												__snake_case     			=       batch_size
												__snake_case     			=       seq_length
												__snake_case     			=       is_training
												__snake_case     			=       use_input_mask
												__snake_case     			=       use_token_type_ids
												__snake_case     			=       use_labels
												__snake_case     			=       vocab_size
												__snake_case     			=       hidden_size
												__snake_case     			=       embedding_size
												__snake_case     			=       num_hidden_layers
												__snake_case     			=       num_attention_heads
												__snake_case     			=       intermediate_size
												__snake_case     			=       hidden_act
												__snake_case     			=       hidden_dropout_prob
												__snake_case     			=       attention_probs_dropout_prob
												__snake_case     			=       max_position_embeddings
												__snake_case     			=       type_vocab_size
												__snake_case     			=       type_sequence_label_size
												__snake_case     			=       initializer_range
												__snake_case     			=       num_labels
												__snake_case     			=       num_choices
												__snake_case     			=       scope





					def 							lowerCAmelCase      (				self   :							Union[str, Any]    ):
												__snake_case     			=       ids_tensor([self.batch_size, self.seq_length]							,						self.vocab_size    )

												__snake_case     			=       None
												if self.use_input_mask:
																			__snake_case     			=       random_attention_mask([self.batch_size, self.seq_length]    )

												__snake_case     			=       None
												if self.use_token_type_ids:
																			__snake_case     			=       ids_tensor([self.batch_size, self.seq_length]							,						self.type_vocab_size    )

												__snake_case     			=       None
												__snake_case     			=       None
												__snake_case     			=       None
												if self.use_labels:
																			__snake_case     			=       ids_tensor([self.batch_size]							,						self.type_sequence_label_size    )
																			__snake_case     			=       ids_tensor([self.batch_size, self.seq_length]							,						self.num_labels    )
																			__snake_case     			=       ids_tensor([self.batch_size]							,						self.num_choices    )

												__snake_case     			=       self.get_config()

												return config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels





					def 							lowerCAmelCase      (				self   :							Union[str, Any]    ):
												return MobileBertConfig(
												    vocab_size=self.vocab_size							,						hidden_size=self.hidden_size							,						num_hidden_layers=self.num_hidden_layers							,						num_attention_heads=self.num_attention_heads							,						intermediate_size=self.intermediate_size							,						embedding_size=self.embedding_size							,						hidden_act=self.hidden_act							,						hidden_dropout_prob=self.hidden_dropout_prob							,						attention_probs_dropout_prob=self.attention_probs_dropout_prob							,						max_position_embeddings=self.max_position_embeddings							,						type_vocab_size=self.type_vocab_size							,						is_decoder=snake_case_							,						initializer_range=self.initializer_range							,						)





					def 							lowerCAmelCase      (				self   :							Optional[Any]							,						snake_case_   :							Any							,						snake_case_   :							Optional[Any]							,						snake_case_   :							Tuple							,						snake_case_   :							Any							,						snake_case_   :							str							,						snake_case_   :							Dict							,						snake_case_   :							List[str]    ):
												__snake_case     			=       MobileBertModel(config=snake_case_    )
												model.to(snake_case_    )
												model.eval()
												__snake_case     			=       model(snake_case_							,						attention_mask=snake_case_							,						token_type_ids=snake_case_    )
												__snake_case     			=       model(snake_case_							,						token_type_ids=snake_case_    )
												__snake_case     			=       model(snake_case_    )

												self.parent.assertEqual(result.last_hidden_state.shape							,						(self.batch_size, self.seq_length, self.hidden_size)    )
												self.parent.assertEqual(result.pooler_output.shape							,						(self.batch_size, self.hidden_size)    )





					def 							lowerCAmelCase      (				self   :							List[str]							,						snake_case_   :							List[Any]							,						snake_case_   :							Any							,						snake_case_   :							Optional[int]							,						snake_case_   :							int							,						snake_case_   :							int							,						snake_case_   :							str							,						snake_case_   :							str    ):
												__snake_case     			=       MobileBertForMaskedLM(config=snake_case_    )
												model.to(snake_case_    )
												model.eval()
												__snake_case     			=       model(snake_case_							,						attention_mask=snake_case_							,						token_type_ids=snake_case_							,						labels=snake_case_    )
												self.parent.assertEqual(result.logits.shape							,						(self.batch_size, self.seq_length, self.vocab_size)    )





					def 							lowerCAmelCase      (				self   :							Optional[Any]							,						snake_case_   :							Optional[int]							,						snake_case_   :							Union[str, Any]							,						snake_case_   :							List[str]							,						snake_case_   :							int							,						snake_case_   :							int							,						snake_case_   :							int							,						snake_case_   :							Any    ):
												__snake_case     			=       MobileBertForNextSentencePrediction(config=snake_case_    )
												model.to(snake_case_    )
												model.eval()
												__snake_case     			=       model(
												    snake_case_							,						attention_mask=snake_case_							,						token_type_ids=snake_case_							,						labels=snake_case_							,						)
												self.parent.assertEqual(result.logits.shape							,						(self.batch_size, 2)    )





					def 							lowerCAmelCase      (				self   :							Optional[int]							,						snake_case_   :							List[str]							,						snake_case_   :							List[str]							,						snake_case_   :							str							,						snake_case_   :							Optional[Any]							,						snake_case_   :							List[str]							,						snake_case_   :							List[str]							,						snake_case_   :							Dict    ):
												__snake_case     			=       MobileBertForPreTraining(config=snake_case_    )
												model.to(snake_case_    )
												model.eval()
												__snake_case     			=       model(
												    snake_case_							,						attention_mask=snake_case_							,						token_type_ids=snake_case_							,						labels=snake_case_							,						next_sentence_label=snake_case_							,						)
												self.parent.assertEqual(result.prediction_logits.shape							,						(self.batch_size, self.seq_length, self.vocab_size)    )
												self.parent.assertEqual(result.seq_relationship_logits.shape							,						(self.batch_size, 2)    )





					def 							lowerCAmelCase      (				self   :							Any							,						snake_case_   :							List[str]							,						snake_case_   :							int							,						snake_case_   :							Any							,						snake_case_   :							Union[str, Any]							,						snake_case_   :							Dict							,						snake_case_   :							int							,						snake_case_   :							int    ):
												__snake_case     			=       MobileBertForQuestionAnswering(config=snake_case_    )
												model.to(snake_case_    )
												model.eval()
												__snake_case     			=       model(
												    snake_case_							,						attention_mask=snake_case_							,						token_type_ids=snake_case_							,						start_positions=snake_case_							,						end_positions=snake_case_							,						)
												self.parent.assertEqual(result.start_logits.shape							,						(self.batch_size, self.seq_length)    )
												self.parent.assertEqual(result.end_logits.shape							,						(self.batch_size, self.seq_length)    )





					def 							lowerCAmelCase      (				self   :							str							,						snake_case_   :							Tuple							,						snake_case_   :							int							,						snake_case_   :							str							,						snake_case_   :							Dict							,						snake_case_   :							Optional[int]							,						snake_case_   :							str							,						snake_case_   :							List[str]    ):
												__snake_case     			=       self.num_labels
												__snake_case     			=       MobileBertForSequenceClassification(snake_case_    )
												model.to(snake_case_    )
												model.eval()
												__snake_case     			=       model(snake_case_							,						attention_mask=snake_case_							,						token_type_ids=snake_case_							,						labels=snake_case_    )
												self.parent.assertEqual(result.logits.shape							,						(self.batch_size, self.num_labels)    )





					def 							lowerCAmelCase      (				self   :							Optional[Any]							,						snake_case_   :							Tuple							,						snake_case_   :							str							,						snake_case_   :							Union[str, Any]							,						snake_case_   :							Tuple							,						snake_case_   :							Optional[int]							,						snake_case_   :							Tuple							,						snake_case_   :							List[Any]    ):
												__snake_case     			=       self.num_labels
												__snake_case     			=       MobileBertForTokenClassification(config=snake_case_    )
												model.to(snake_case_    )
												model.eval()
												__snake_case     			=       model(snake_case_							,						attention_mask=snake_case_							,						token_type_ids=snake_case_							,						labels=snake_case_    )
												self.parent.assertEqual(result.logits.shape							,						(self.batch_size, self.seq_length, self.num_labels)    )





					def 							lowerCAmelCase      (				self   :							Tuple							,						snake_case_   :							Union[str, Any]							,						snake_case_   :							Tuple							,						snake_case_   :							Optional[Any]							,						snake_case_   :							Any							,						snake_case_   :							Any							,						snake_case_   :							Dict							,						snake_case_   :							Union[str, Any]    ):
												__snake_case     			=       self.num_choices
												__snake_case     			=       MobileBertForMultipleChoice(config=snake_case_    )
												model.to(snake_case_    )
												model.eval()
												__snake_case     			=       input_ids.unsqueeze(1    ).expand(-1							,						self.num_choices							,						-1    ).contiguous()
												__snake_case     			=       token_type_ids.unsqueeze(1    ).expand(-1							,						self.num_choices							,						-1    ).contiguous()
												__snake_case     			=       input_mask.unsqueeze(1    ).expand(-1							,						self.num_choices							,						-1    ).contiguous()
												__snake_case     			=       model(
												    snake_case_							,						attention_mask=snake_case_							,						token_type_ids=snake_case_							,						labels=snake_case_							,						)
												self.parent.assertEqual(result.logits.shape							,						(self.batch_size, self.num_choices)    )





					def 							lowerCAmelCase      (				self   :							List[Any]    ):
												__snake_case     			=       self.prepare_config_and_inputs()
												(
												    (
												    __snake_case
												)			,   (
												    __snake_case
												)			,   (
												    __snake_case
												)			,   (
												    __snake_case
												)			,   (
												    __snake_case
												)			,   (
												    __snake_case
												)			,   (
												    __snake_case
												)			,   
												)     			=       config_and_inputs
												__snake_case     			=       {"input_ids": input_ids, "token_type_ids": token_type_ids, "attention_mask": input_mask}
												return config, inputs_dict







@require_torch
class 							__magic_name__     (		lowercase__      ,		lowercase__      ,		unittest.TestCase   ):
					_SCREAMING_SNAKE_CASE       :					Dict     							=   (
					    (
					        MobileBertModel,
					        MobileBertForMaskedLM,
					        MobileBertForMultipleChoice,
					        MobileBertForNextSentencePrediction,
					        MobileBertForPreTraining,
					        MobileBertForQuestionAnswering,
					        MobileBertForSequenceClassification,
					        MobileBertForTokenClassification,
					    )
					    if is_torch_available()
					    else ()
					)
					_SCREAMING_SNAKE_CASE       :					Optional[int]     							=   (
					    {
					        'feature-extraction': MobileBertModel,
					        'fill-mask': MobileBertForMaskedLM,
					        'question-answering': MobileBertForQuestionAnswering,
					        'text-classification': MobileBertForSequenceClassification,
					        'token-classification': MobileBertForTokenClassification,
					        'zero-shot': MobileBertForSequenceClassification,
					    }
					    if is_torch_available()
					    else {}
					)
					_SCREAMING_SNAKE_CASE       :					int     							=   True





					def 							lowerCAmelCase      (				self   :							List[str]							,						snake_case_   :							List[str]							,						snake_case_   :							str							,						snake_case_   :							str=False    ):
												__snake_case     			=       super()._prepare_for_class(snake_case_							,						snake_case_							,						return_labels=snake_case_    )

												if return_labels:
																			if model_class in get_values(snake_case_    ):
																										__snake_case     			=       torch.zeros(
																										    (self.model_tester.batch_size, self.model_tester.seq_length)							,						dtype=torch.long							,						device=snake_case_    )
																										__snake_case     			=       torch.zeros(
																										    self.model_tester.batch_size							,						dtype=torch.long							,						device=snake_case_    )
												return inputs_dict





					def 							lowerCAmelCase      (				self   :							Optional[Any]    ):
												__snake_case     			=       MobileBertModelTester(self    )
												__snake_case     			=       ConfigTester(self							,						config_class=snake_case_							,						hidden_size=37    )





					def 							lowerCAmelCase      (				self   :							List[Any]    ):
												self.config_tester.run_common_tests()





					def 							lowerCAmelCase      (				self   :							Tuple    ):
												__snake_case     			=       self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_mobilebert_model(*snake_case_    )





					def 							lowerCAmelCase      (				self   :							Optional[Any]    ):
												__snake_case     			=       self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_mobilebert_for_masked_lm(*snake_case_    )





					def 							lowerCAmelCase      (				self   :							Tuple    ):
												__snake_case     			=       self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_mobilebert_for_multiple_choice(*snake_case_    )





					def 							lowerCAmelCase      (				self   :							Any    ):
												__snake_case     			=       self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_mobilebert_for_next_sequence_prediction(*snake_case_    )





					def 							lowerCAmelCase      (				self   :							Any    ):
												__snake_case     			=       self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_mobilebert_for_pretraining(*snake_case_    )





					def 							lowerCAmelCase      (				self   :							List[str]    ):
												__snake_case     			=       self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_mobilebert_for_question_answering(*snake_case_    )





					def 							lowerCAmelCase      (				self   :							List[Any]    ):
												__snake_case     			=       self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_mobilebert_for_sequence_classification(*snake_case_    )





					def 							lowerCAmelCase      (				self   :							str    ):
												__snake_case     			=       self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_mobilebert_for_token_classification(*snake_case_    )




def   __UpperCamelCase   (					SCREAMING_SNAKE_CASE )  ->     List[Any]:







							"""simple docstring"""


							return torch.tensor(
							    SCREAMING_SNAKE_CASE							,    dtype=torch.long							,    device=SCREAMING_SNAKE_CASE							,    )


_SCREAMING_SNAKE_CASE	       =      1E-3







@require_torch
@require_sentencepiece
@require_tokenizers
class 							__magic_name__     (		unittest.TestCase   ):





					@slow
					def 							lowerCAmelCase      (				self   :							Tuple    ):
												__snake_case     			=       MobileBertModel.from_pretrained("google/mobilebert-uncased"    ).to(snake_case_    )
												__snake_case     			=       _long_tensor([[101, 7110, 1005, 1056, 2023, 11333, 17413, 1029, 102]]    )
												with torch.no_grad():
																			__snake_case     			=       model(snake_case_    )[0]
												__snake_case     			=       torch.Size((1, 9, 512)    )
												self.assertEqual(output.shape							,						snake_case_    )
												__snake_case     			=       torch.tensor(
												    [
												        [
												            [-2.4_73_65_26e07, 8.2_69_16_56e04, 1.6_52_18_38e05],
												            [-5.7_54_17_04e-01, 3.9_05_60_22e00, 4.4_01_15_07e00],
												            [2.6_04_73_59e00, 1.5_67_76_52e00, -1.7_32_41_88e-01],
												        ]
												    ]							,						device=snake_case_							,						)

												# MobileBERT results range from 10e0 to 10e8. Even a 0.0000001% difference with a value of 10e8 results in a
												# ~1 difference, it's therefore not a good idea to measure using addition.
												# Here, we instead divide the expected result with the result in order to obtain ~1. We then check that the
												# result is held between bounds: 1 - TOLERANCE < expected_result / result < 1 + TOLERANCE
												__snake_case     			=       torch.all((expected_slice / output[..., :3, :3]) >= 1 - TOLERANCE    )
												__snake_case     			=       torch.all((expected_slice / output[..., :3, :3]) <= 1 + TOLERANCE    )

												self.assertTrue(lower_bound and upper_bound    )
