



"""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 					A_:







			"""simple docstring"""



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

						_lowerCamelCase					:					List[str]	       =    None
						if self.use_input_mask:
									_lowerCamelCase					:					str	       =    random_attention_mask([self.batch_size, self.seq_length]							)

						_lowerCamelCase					:					List[str]	       =    None
						if self.use_token_type_ids:
									_lowerCamelCase					:					Tuple	       =    ids_tensor([self.batch_size, self.seq_length]				,   self.type_vocab_size							)

						_lowerCamelCase					:					List[str]	       =    None
						_lowerCamelCase					:					Optional[Any]	       =    None
						_lowerCamelCase					:					List[Any]	       =    None
						if self.use_labels:
									_lowerCamelCase					:					Any	       =    ids_tensor([self.batch_size]				,   self.type_sequence_label_size							)
									_lowerCamelCase					:					int	       =    ids_tensor([self.batch_size, self.seq_length]				,   self.num_labels							)
									_lowerCamelCase					:					str	       =    ids_tensor([self.batch_size]				,   self.num_choices							)

						_lowerCamelCase					:					Dict	       =    self.get_config()

						return config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels
			def    _lowerCAmelCase		(	self							):
						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=SCREAMING_SNAKE_CASE_				,   initializer_range=self.initializer_range				,   )
			def    _lowerCAmelCase		(	self				,   A				,   A				,   A				,   A				,   A				,   A				,   A							):
						_lowerCamelCase					:					Dict	       =    MobileBertModel(config=SCREAMING_SNAKE_CASE_							)
						model.to(SCREAMING_SNAKE_CASE_							)
						model.eval()
						_lowerCamelCase					:					Optional[Any]	       =    model(SCREAMING_SNAKE_CASE_				,   attention_mask=SCREAMING_SNAKE_CASE_				,   token_type_ids=SCREAMING_SNAKE_CASE_							)
						_lowerCamelCase					:					Dict	       =    model(SCREAMING_SNAKE_CASE_				,   token_type_ids=SCREAMING_SNAKE_CASE_							)
						_lowerCamelCase					:					Union[str, Any]	       =    model(SCREAMING_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				,   A				,   A				,   A				,   A				,   A				,   A				,   A							):
						_lowerCamelCase					:					Optional[Any]	       =    MobileBertForMaskedLM(config=SCREAMING_SNAKE_CASE_							)
						model.to(SCREAMING_SNAKE_CASE_							)
						model.eval()
						_lowerCamelCase					:					Dict	       =    model(SCREAMING_SNAKE_CASE_				,   attention_mask=SCREAMING_SNAKE_CASE_				,   token_type_ids=SCREAMING_SNAKE_CASE_				,   labels=SCREAMING_SNAKE_CASE_							)
						self.parent.assertEqual(result.logits.shape				,   (self.batch_size, self.seq_length, self.vocab_size)							)
			def    _lowerCAmelCase		(	self				,   A				,   A				,   A				,   A				,   A				,   A				,   A							):
						_lowerCamelCase					:					Tuple	       =    MobileBertForNextSentencePrediction(config=SCREAMING_SNAKE_CASE_							)
						model.to(SCREAMING_SNAKE_CASE_							)
						model.eval()
						_lowerCamelCase					:					Dict	       =    model(
						    SCREAMING_SNAKE_CASE_				,   attention_mask=SCREAMING_SNAKE_CASE_				,   token_type_ids=SCREAMING_SNAKE_CASE_				,   labels=SCREAMING_SNAKE_CASE_				,   )
						self.parent.assertEqual(result.logits.shape				,   (self.batch_size, 2)							)
			def    _lowerCAmelCase		(	self				,   A				,   A				,   A				,   A				,   A				,   A				,   A							):
						_lowerCamelCase					:					str	       =    MobileBertForPreTraining(config=SCREAMING_SNAKE_CASE_							)
						model.to(SCREAMING_SNAKE_CASE_							)
						model.eval()
						_lowerCamelCase					:					int	       =    model(
						    SCREAMING_SNAKE_CASE_				,   attention_mask=SCREAMING_SNAKE_CASE_				,   token_type_ids=SCREAMING_SNAKE_CASE_				,   labels=SCREAMING_SNAKE_CASE_				,   next_sentence_label=SCREAMING_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				,   A				,   A				,   A				,   A				,   A				,   A				,   A							):
						_lowerCamelCase					:					int	       =    MobileBertForQuestionAnswering(config=SCREAMING_SNAKE_CASE_							)
						model.to(SCREAMING_SNAKE_CASE_							)
						model.eval()
						_lowerCamelCase					:					Optional[int]	       =    model(
						    SCREAMING_SNAKE_CASE_				,   attention_mask=SCREAMING_SNAKE_CASE_				,   token_type_ids=SCREAMING_SNAKE_CASE_				,   start_positions=SCREAMING_SNAKE_CASE_				,   end_positions=SCREAMING_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				,   A				,   A				,   A				,   A				,   A				,   A				,   A							):
						_lowerCamelCase					:					Union[str, Any]	       =    self.num_labels
						_lowerCamelCase					:					Optional[int]	       =    MobileBertForSequenceClassification(SCREAMING_SNAKE_CASE_							)
						model.to(SCREAMING_SNAKE_CASE_							)
						model.eval()
						_lowerCamelCase					:					Union[str, Any]	       =    model(SCREAMING_SNAKE_CASE_				,   attention_mask=SCREAMING_SNAKE_CASE_				,   token_type_ids=SCREAMING_SNAKE_CASE_				,   labels=SCREAMING_SNAKE_CASE_							)
						self.parent.assertEqual(result.logits.shape				,   (self.batch_size, self.num_labels)							)
			def    _lowerCAmelCase		(	self				,   A				,   A				,   A				,   A				,   A				,   A				,   A							):
						_lowerCamelCase					:					Union[str, Any]	       =    self.num_labels
						_lowerCamelCase					:					Union[str, Any]	       =    MobileBertForTokenClassification(config=SCREAMING_SNAKE_CASE_							)
						model.to(SCREAMING_SNAKE_CASE_							)
						model.eval()
						_lowerCamelCase					:					str	       =    model(SCREAMING_SNAKE_CASE_				,   attention_mask=SCREAMING_SNAKE_CASE_				,   token_type_ids=SCREAMING_SNAKE_CASE_				,   labels=SCREAMING_SNAKE_CASE_							)
						self.parent.assertEqual(result.logits.shape				,   (self.batch_size, self.seq_length, self.num_labels)							)
			def    _lowerCAmelCase		(	self				,   A				,   A				,   A				,   A				,   A				,   A				,   A							):
						_lowerCamelCase					:					List[str]	       =    self.num_choices
						_lowerCamelCase					:					Optional[Any]	       =    MobileBertForMultipleChoice(config=SCREAMING_SNAKE_CASE_							)
						model.to(SCREAMING_SNAKE_CASE_							)
						model.eval()
						_lowerCamelCase					:					Optional[Any]	       =    input_ids.unsqueeze(1							).expand(-1				,   self.num_choices				,   -1							).contiguous()
						_lowerCamelCase					:					Any	       =    token_type_ids.unsqueeze(1							).expand(-1				,   self.num_choices				,   -1							).contiguous()
						_lowerCamelCase					:					Tuple	       =    input_mask.unsqueeze(1							).expand(-1				,   self.num_choices				,   -1							).contiguous()
						_lowerCamelCase					:					Optional[Any]	       =    model(
						    SCREAMING_SNAKE_CASE_				,   attention_mask=SCREAMING_SNAKE_CASE_				,   token_type_ids=SCREAMING_SNAKE_CASE_				,   labels=SCREAMING_SNAKE_CASE_				,   )
						self.parent.assertEqual(result.logits.shape				,   (self.batch_size, self.num_choices)							)

			def    _lowerCAmelCase		(	self							):
						_lowerCamelCase					:					Dict	       =    self.prepare_config_and_inputs()
						(
						    _lowerCamelCase							
						)					:					Union[str, Any]	       =    config_and_inputs
						_lowerCamelCase					:					Union[str, Any]	       =    {"""input_ids""": input_ids, """token_type_ids""": token_type_ids, """attention_mask""": input_mask}
						return config, inputs_dict







@require_torch
class 					A_(_lowerCAmelCase   ,   _lowerCAmelCase   ,   unittest.TestCase      ):







			"""simple docstring"""



			a_       :			List[str]												=			(
			    (
			        MobileBertModel,
			        MobileBertForMaskedLM,
			        MobileBertForMultipleChoice,
			        MobileBertForNextSentencePrediction,
			        MobileBertForPreTraining,
			        MobileBertForQuestionAnswering,
			        MobileBertForSequenceClassification,
			        MobileBertForTokenClassification,
			    )
			    if is_torch_available()
			    else ()
			)
			a_       :			str												=			(
			    {
			        """feature-extraction""": MobileBertModel,
			        """fill-mask""": MobileBertForMaskedLM,
			        """question-answering""": MobileBertForQuestionAnswering,
			        """text-classification""": MobileBertForSequenceClassification,
			        """token-classification""": MobileBertForTokenClassification,
			        """zero-shot""": MobileBertForSequenceClassification,
			    }
			    if is_torch_available()
			    else {}
			)
			a_       :			int												=			True
			def    _lowerCAmelCase		(	self				,   A				,   A				,   A=False							):
						_lowerCamelCase					:					Union[str, Any]	       =    super()._prepare_for_class(SCREAMING_SNAKE_CASE_				,   SCREAMING_SNAKE_CASE_				,   return_labels=SCREAMING_SNAKE_CASE_							)

						if return_labels:
									if model_class in get_values(SCREAMING_SNAKE_CASE_							):
												_lowerCamelCase					:					Union[str, Any]	       =    torch.zeros(
												    (self.model_tester.batch_size, self.model_tester.seq_length)				,   dtype=torch.long				,   device=SCREAMING_SNAKE_CASE_							)
												_lowerCamelCase					:					Union[str, Any]	       =    torch.zeros(
												    self.model_tester.batch_size				,   dtype=torch.long				,   device=SCREAMING_SNAKE_CASE_							)
						return inputs_dict
			def    _lowerCAmelCase		(	self							):
						_lowerCamelCase					:					Tuple	       =    MobileBertModelTester(self							)
						_lowerCamelCase					:					str	       =    ConfigTester(self				,   config_class=SCREAMING_SNAKE_CASE_				,   hidden_size=37							)
			def    _lowerCAmelCase		(	self							):
						self.config_tester.run_common_tests()
			def    _lowerCAmelCase		(	self							):
						_lowerCamelCase					:					Optional[Any]	       =    self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_mobilebert_model(*SCREAMING_SNAKE_CASE_							)
			def    _lowerCAmelCase		(	self							):
						_lowerCamelCase					:					Any	       =    self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_mobilebert_for_masked_lm(*SCREAMING_SNAKE_CASE_							)
			def    _lowerCAmelCase		(	self							):
						_lowerCamelCase					:					Optional[Any]	       =    self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_mobilebert_for_multiple_choice(*SCREAMING_SNAKE_CASE_							)
			def    _lowerCAmelCase		(	self							):
						_lowerCamelCase					:					Any	       =    self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_mobilebert_for_next_sequence_prediction(*SCREAMING_SNAKE_CASE_							)
			def    _lowerCAmelCase		(	self							):
						_lowerCamelCase					:					Dict	       =    self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_mobilebert_for_pretraining(*SCREAMING_SNAKE_CASE_							)
			def    _lowerCAmelCase		(	self							):
						_lowerCamelCase					:					Any	       =    self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_mobilebert_for_question_answering(*SCREAMING_SNAKE_CASE_							)
			def    _lowerCAmelCase		(	self							):
						_lowerCamelCase					:					Any	       =    self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_mobilebert_for_sequence_classification(*SCREAMING_SNAKE_CASE_							)

			def    _lowerCAmelCase		(	self							):
						_lowerCamelCase					:					str	       =    self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_mobilebert_for_token_classification(*SCREAMING_SNAKE_CASE_							)






def     UpperCAmelCase_					(				__a   :			Tuple						):




			'''simple docstring'''



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


a_							=    1e-3







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







			"""simple docstring"""



			@slow
			def    _lowerCAmelCase		(	self							):
						_lowerCamelCase					:					int	       =    MobileBertModel.from_pretrained('google/mobilebert-uncased'							).to(SCREAMING_SNAKE_CASE_							)
						_lowerCamelCase					:					List[Any]	       =    _long_tensor([[101, 7110, 1005, 1056, 2023, 1_1333, 1_7413, 1029, 102]]							)
						with torch.no_grad():
									_lowerCamelCase					:					int	       =    model(SCREAMING_SNAKE_CASE_							)[0]
						_lowerCamelCase					:					int	       =    torch.Size((1, 9, 512)							)
						self.assertEqual(output.shape				,   SCREAMING_SNAKE_CASE_							)
						_lowerCamelCase					:					int	       =    torch.tensor(
						    [
						        [
						            [-2.473_6526E07, 8.269_1656E04, 1.652_1838E05],
						            [-5.754_1704E-01, 3.905_6022E00, 4.401_1507E00],
						            [2.604_7359E00, 1.567_7652E00, -1.732_4188E-01],
						        ]
						    ]				,   device=SCREAMING_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
						_lowerCamelCase					:					Optional[int]	       =    torch.all((expected_slice / output[..., :3, :3]) >= 1 - TOLERANCE							)
						_lowerCamelCase					:					List[str]	       =    torch.all((expected_slice / output[..., :3, :3]) <= 1 + TOLERANCE							)

						self.assertTrue(lower_bound and upper_bound							)










from typing import TYPE_CHECKING

from ...utils import _LazyModule


a      :  int			     =			{'tokenization_wav2vec2_phoneme': ['Wav2Vec2PhonemeCTCTokenizer']}


if TYPE_CHECKING:
	from .tokenization_wavaveca_phoneme import WavaVecaPhonemeCTCTokenizer
else:
	import sys

	a      :  int			     =			_LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)






