



'''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        SCREAMING_SNAKE_CASE:







	"""simple docstring"""






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







	def   A			(		self    :  int	)    ->	Optional[Any]:
								UpperCAmelCase				: List[str]											=	ids_tensor([self.batch_size, self.seq_length]	,    self.vocab_size	)

								UpperCAmelCase				: Dict											=	None
								if self.use_input_mask:
															UpperCAmelCase				: Optional[int]											=	random_attention_mask([self.batch_size, self.seq_length]	)

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

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

								UpperCAmelCase				: Optional[Any]											=	self.get_config()

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







	def   A			(		self    :  List[str]	)    ->	Optional[int]:
								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   A			(		self    :  Optional[Any]	,    __snake_case    :  Union[str, Any]	,    __snake_case    :  Tuple	,    __snake_case    :  List[Any]	,    __snake_case    :  Dict	,    __snake_case    :  Optional[Any]	,    __snake_case    :  Any	,    __snake_case    :  Tuple	)    ->	int:
								UpperCAmelCase				: Optional[Any]											=	MobileBertModel(config=__snake_case	)
								model.to(__snake_case	)
								model.eval()
								UpperCAmelCase				: Any											=	model(__snake_case	,    attention_mask=__snake_case	,    token_type_ids=__snake_case	)
								UpperCAmelCase				: Dict											=	model(__snake_case	,    token_type_ids=__snake_case	)
								UpperCAmelCase				: Dict											=	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   A			(		self    :  Optional[Any]	,    __snake_case    :  int	,    __snake_case    :  Union[str, Any]	,    __snake_case    :  Optional[Any]	,    __snake_case    :  Dict	,    __snake_case    :  Optional[int]	,    __snake_case    :  List[str]	,    __snake_case    :  Dict	)    ->	Union[str, Any]:
								UpperCAmelCase				: str											=	MobileBertForMaskedLM(config=__snake_case	)
								model.to(__snake_case	)
								model.eval()
								UpperCAmelCase				: Optional[int]											=	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   A			(		self    :  Dict	,    __snake_case    :  Optional[int]	,    __snake_case    :  Tuple	,    __snake_case    :  str	,    __snake_case    :  Any	,    __snake_case    :  Optional[int]	,    __snake_case    :  int	,    __snake_case    :  Dict	)    ->	int:
								UpperCAmelCase				: int											=	MobileBertForNextSentencePrediction(config=__snake_case	)
								model.to(__snake_case	)
								model.eval()
								UpperCAmelCase				: Dict											=	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   A			(		self    :  Dict	,    __snake_case    :  Optional[int]	,    __snake_case    :  List[str]	,    __snake_case    :  Tuple	,    __snake_case    :  Optional[int]	,    __snake_case    :  str	,    __snake_case    :  str	,    __snake_case    :  Union[str, Any]	)    ->	Tuple:
								UpperCAmelCase				: List[str]											=	MobileBertForPreTraining(config=__snake_case	)
								model.to(__snake_case	)
								model.eval()
								UpperCAmelCase				: Dict											=	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   A			(		self    :  int	,    __snake_case    :  List[Any]	,    __snake_case    :  List[Any]	,    __snake_case    :  List[str]	,    __snake_case    :  Tuple	,    __snake_case    :  int	,    __snake_case    :  Optional[int]	,    __snake_case    :  Union[str, Any]	)    ->	Optional[int]:
								UpperCAmelCase				: Dict											=	MobileBertForQuestionAnswering(config=__snake_case	)
								model.to(__snake_case	)
								model.eval()
								UpperCAmelCase				: List[str]											=	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   A			(		self    :  List[str]	,    __snake_case    :  Optional[Any]	,    __snake_case    :  Any	,    __snake_case    :  Tuple	,    __snake_case    :  Union[str, Any]	,    __snake_case    :  str	,    __snake_case    :  int	,    __snake_case    :  Any	)    ->	List[Any]:
								UpperCAmelCase				: str											=	self.num_labels
								UpperCAmelCase				: Optional[Any]											=	MobileBertForSequenceClassification(__snake_case	)
								model.to(__snake_case	)
								model.eval()
								UpperCAmelCase				: List[Any]											=	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   A			(		self    :  str	,    __snake_case    :  List[Any]	,    __snake_case    :  Union[str, Any]	,    __snake_case    :  Optional[Any]	,    __snake_case    :  int	,    __snake_case    :  Dict	,    __snake_case    :  Optional[int]	,    __snake_case    :  Optional[int]	)    ->	int:
								UpperCAmelCase				: List[Any]											=	self.num_labels
								UpperCAmelCase				: Union[str, Any]											=	MobileBertForTokenClassification(config=__snake_case	)
								model.to(__snake_case	)
								model.eval()
								UpperCAmelCase				: List[Any]											=	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   A			(		self    :  List[str]	,    __snake_case    :  Optional[Any]	,    __snake_case    :  Tuple	,    __snake_case    :  str	,    __snake_case    :  Dict	,    __snake_case    :  int	,    __snake_case    :  List[Any]	,    __snake_case    :  str	)    ->	int:
								UpperCAmelCase				: Union[str, Any]											=	self.num_choices
								UpperCAmelCase				: Union[str, Any]											=	MobileBertForMultipleChoice(config=__snake_case	)
								model.to(__snake_case	)
								model.eval()
								UpperCAmelCase				: List[str]											=	input_ids.unsqueeze(1	).expand(-1	,    self.num_choices	,    -1	).contiguous()
								UpperCAmelCase				: Optional[Any]											=	token_type_ids.unsqueeze(1	).expand(-1	,    self.num_choices	,    -1	).contiguous()
								UpperCAmelCase				: Dict											=	input_mask.unsqueeze(1	).expand(-1	,    self.num_choices	,    -1	).contiguous()
								UpperCAmelCase				: Dict											=	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   A			(		self    :  Optional[Any]	)    ->	Dict:
								UpperCAmelCase				: List[Any]											=	self.prepare_config_and_inputs()
								(
								    (
								    UpperCAmelCase
								)							,	(
								    UpperCAmelCase
								)							,	(
								    UpperCAmelCase
								)							,	(
								    UpperCAmelCase
								)							,	(
								    UpperCAmelCase
								)							,	(
								    UpperCAmelCase
								)							,	(
								    UpperCAmelCase
								)							,	
								)				: Dict											=	config_and_inputs
								UpperCAmelCase				: Optional[int]											=	{'''input_ids''': input_ids, '''token_type_ids''': token_type_ids, '''attention_mask''': input_mask}
								return config, inputs_dict

@require_torch
class        SCREAMING_SNAKE_CASE(							A__							,	A__							,	unittest.TestCase    ):







	"""simple docstring"""






	lowerCamelCase__    				= (
	    (
	        MobileBertModel,
	        MobileBertForMaskedLM,
	        MobileBertForMultipleChoice,
	        MobileBertForNextSentencePrediction,
	        MobileBertForPreTraining,
	        MobileBertForQuestionAnswering,
	        MobileBertForSequenceClassification,
	        MobileBertForTokenClassification,
	    )
	    if is_torch_available()
	    else ()
	)
	lowerCamelCase__    				= (
	    {
	        """feature-extraction""": MobileBertModel,
	        """fill-mask""": MobileBertForMaskedLM,
	        """question-answering""": MobileBertForQuestionAnswering,
	        """text-classification""": MobileBertForSequenceClassification,
	        """token-classification""": MobileBertForTokenClassification,
	        """zero-shot""": MobileBertForSequenceClassification,
	    }
	    if is_torch_available()
	    else {}
	)
	lowerCamelCase__    				= True







	def   A			(		self    :  Dict	,    __snake_case    :  Union[str, Any]	,    __snake_case    :  Optional[int]	,    __snake_case    :  List[Any]=False	)    ->	Any:
								UpperCAmelCase				: List[str]											=	super()._prepare_for_class(__snake_case	,    __snake_case	,    return_labels=__snake_case	)

								if return_labels:
															if model_class in get_values(__snake_case	):
																						UpperCAmelCase				: Dict											=	torch.zeros(
																						    (self.model_tester.batch_size, self.model_tester.seq_length)	,    dtype=torch.long	,    device=__snake_case	)
																						UpperCAmelCase				: Optional[int]											=	torch.zeros(
																						    self.model_tester.batch_size	,    dtype=torch.long	,    device=__snake_case	)
								return inputs_dict







	def   A			(		self    :  Any	)    ->	Optional[int]:
								UpperCAmelCase				: Optional[Any]											=	MobileBertModelTester(self	)
								UpperCAmelCase				: Optional[int]											=	ConfigTester(self	,    config_class=__snake_case	,    hidden_size=37	)







	def   A			(		self    :  str	)    ->	Union[str, Any]:
								self.config_tester.run_common_tests()







	def   A			(		self    :  Optional[Any]	)    ->	Optional[int]:
								UpperCAmelCase				: Optional[Any]											=	self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_mobilebert_model(*__snake_case	)







	def   A			(		self    :  str	)    ->	Optional[int]:
								UpperCAmelCase				: List[Any]											=	self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_mobilebert_for_masked_lm(*__snake_case	)







	def   A			(		self    :  str	)    ->	str:
								UpperCAmelCase				: Dict											=	self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_mobilebert_for_multiple_choice(*__snake_case	)







	def   A			(		self    :  List[str]	)    ->	Any:
								UpperCAmelCase				: Tuple											=	self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_mobilebert_for_next_sequence_prediction(*__snake_case	)







	def   A			(		self    :  Optional[Any]	)    ->	str:
								UpperCAmelCase				: List[str]											=	self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_mobilebert_for_pretraining(*__snake_case	)







	def   A			(		self    :  List[Any]	)    ->	Optional[Any]:
								UpperCAmelCase				: Union[str, Any]											=	self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_mobilebert_for_question_answering(*__snake_case	)







	def   A			(		self    :  Tuple	)    ->	str:
								UpperCAmelCase				: Dict											=	self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_mobilebert_for_sequence_classification(*__snake_case	)







	def   A			(		self    :  List[str]	)    ->	int:
								UpperCAmelCase				: int											=	self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_mobilebert_for_token_classification(*__snake_case	)


def 				snake_case_  (      _lowerCAmelCase						:		Optional[Any]						)   ->     str:
							return torch.tensor(
							    _lowerCAmelCase     ,   dtype=torch.long     ,   device=_lowerCAmelCase     ,   )


UpperCamelCase__:     Optional[int]										=    1E-3




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







	"""simple docstring"""






	@slow
	def   A			(		self    :  Optional[Any]	)    ->	Tuple:
								UpperCAmelCase				: Any											=	MobileBertModel.from_pretrained('''google/mobilebert-uncased'''	).to(__snake_case	)
								UpperCAmelCase				: Any											=	_long_tensor([[101, 7110, 1005, 1056, 2023, 11333, 17413, 1029, 102]]	)
								with torch.no_grad():
															UpperCAmelCase				: Optional[int]											=	model(__snake_case	)[0]
								UpperCAmelCase				: Optional[Any]											=	torch.Size((1, 9, 512)	)
								self.assertEqual(output.shape	,    __snake_case	)
								UpperCAmelCase				: int											=	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
								UpperCAmelCase				: Optional[Any]											=	torch.all((expected_slice / output[..., :3, :3]) >= 1 - TOLERANCE	)
								UpperCAmelCase				: Dict											=	torch.all((expected_slice / output[..., :3, :3]) <= 1 + TOLERANCE	)

								self.assertTrue(lower_bound and upper_bound	)










'''simple docstring'''






from typing import TYPE_CHECKING

from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available


UpperCamelCase__:     Union[str, Any]										=    {
    "configuration_lilt": ["LILT_PRETRAINED_CONFIG_ARCHIVE_MAP", "LiltConfig"],
}

try:
						if not is_torch_available():
												raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
						pass
else:
						UpperCamelCase__:     List[str]										=    [
						    "LILT_PRETRAINED_MODEL_ARCHIVE_LIST",
						    "LiltForQuestionAnswering",
						    "LiltForSequenceClassification",
						    "LiltForTokenClassification",
						    "LiltModel",
						    "LiltPreTrainedModel",
						]

if TYPE_CHECKING:
						from .configuration_lilt import LILT_PRETRAINED_CONFIG_ARCHIVE_MAP, LiltConfig

						try:
												if not is_torch_available():
																		raise OptionalDependencyNotAvailable()
						except OptionalDependencyNotAvailable:
												pass
						else:
												from .modeling_lilt import (
												    LILT_PRETRAINED_MODEL_ARCHIVE_LIST,
												    LiltForQuestionAnswering,
												    LiltForSequenceClassification,
												    LiltForTokenClassification,
												    LiltModel,
												    LiltPreTrainedModel,
												)

else:
						import sys

						UpperCamelCase__:     Optional[Any]										=    _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)





