





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_  :






						def __init__(					self					:					Any						,      _A					:					List[Any]						,      _A					:					Any=13						,      _A					:					Any=7						,      _A					:					str=True						,      _A					:					str=True						,      _A					:					str=True						,      _A					:					Optional[Any]=True						,      _A					:					Union[str, Any]=99						,      _A					:					Optional[int]=64						,      _A					:					List[str]=32						,      _A					:					Optional[int]=5						,      _A					:					Optional[Any]=4						,      _A					:					List[str]=37						,      _A					:					Optional[int]="gelu"						,      _A					:					Optional[Any]=0.1						,      _A					:					str=0.1						,      _A					:					Tuple=512						,      _A					:					Tuple=16						,      _A					:					Any=2						,      _A					:					Optional[Any]=0.0_2						,      _A					:					Optional[int]=3						,      _A					:					Optional[int]=4						,      _A					:					Any=None						,      )							->			str:







							"""simple docstring"""


							snake_case_						:	List[str]      =		parent
							snake_case_						:	Tuple      =		batch_size
							snake_case_						:	str      =		seq_length
							snake_case_						:	Optional[Any]      =		is_training
							snake_case_						:	Optional[Any]      =		use_input_mask
							snake_case_						:	Optional[int]      =		use_token_type_ids
							snake_case_						:	Optional[Any]      =		use_labels
							snake_case_						:	List[str]      =		vocab_size
							snake_case_						:	Union[str, Any]      =		hidden_size
							snake_case_						:	Tuple      =		embedding_size
							snake_case_						:	Optional[int]      =		num_hidden_layers
							snake_case_						:	List[str]      =		num_attention_heads
							snake_case_						:	Optional[Any]      =		intermediate_size
							snake_case_						:	Optional[Any]      =		hidden_act
							snake_case_						:	Optional[int]      =		hidden_dropout_prob
							snake_case_						:	Optional[int]      =		attention_probs_dropout_prob
							snake_case_						:	str      =		max_position_embeddings
							snake_case_						:	List[str]      =		type_vocab_size
							snake_case_						:	int      =		type_sequence_label_size
							snake_case_						:	Tuple      =		initializer_range
							snake_case_						:	Tuple      =		num_labels
							snake_case_						:	Tuple      =		num_choices
							snake_case_						:	Tuple      =		scope






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







							"""simple docstring"""


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

							snake_case_						:	Dict      =		None
							if self.use_input_mask:
								snake_case_						:	Union[str, Any]      =		random_attention_mask([self.batch_size, self.seq_length]		)

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

							snake_case_						:	List[Any]      =		None
							snake_case_						:	Dict      =		None
							snake_case_						:	Tuple      =		None
							if self.use_labels:
								snake_case_						:	Any      =		ids_tensor([self.batch_size]						,      self.type_sequence_label_size		)
								snake_case_						:	List[Any]      =		ids_tensor([self.batch_size, self.seq_length]						,      self.num_labels		)
								snake_case_						:	List[Any]      =		ids_tensor([self.batch_size]						,      self.num_choices		)

							snake_case_						:	Optional[int]      =		self.get_config()

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






						def UpperCAmelCase_ (					self					:					Any		)							->			str:







							"""simple docstring"""


							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=_A						,      initializer_range=self.initializer_range						,      )






						def UpperCAmelCase_ (					self					:					List[str]						,      _A					:					Dict						,      _A					:					List[str]						,      _A					:					Any						,      _A					:					Tuple						,      _A					:					str						,      _A					:					Optional[Any]						,      _A					:					str		)							->			Dict:







							"""simple docstring"""


							snake_case_						:	List[str]      =		MobileBertModel(config=_A		)
							model.to(_A		)
							model.eval()
							snake_case_						:	Tuple      =		model(_A						,      attention_mask=_A						,      token_type_ids=_A		)
							snake_case_						:	Optional[int]      =		model(_A						,      token_type_ids=_A		)
							snake_case_						:	Optional[Any]      =		model(_A		)

							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 UpperCAmelCase_ (					self					:					Optional[int]						,      _A					:					List[str]						,      _A					:					List[str]						,      _A					:					Any						,      _A					:					int						,      _A					:					Any						,      _A					:					str						,      _A					:					Optional[Any]		)							->			int:







							"""simple docstring"""


							snake_case_						:	Any      =		MobileBertForMaskedLM(config=_A		)
							model.to(_A		)
							model.eval()
							snake_case_						:	Dict      =		model(_A						,      attention_mask=_A						,      token_type_ids=_A						,      labels=_A		)
							self.parent.assertEqual(result.logits.shape						,      (self.batch_size, self.seq_length, self.vocab_size)		)






						def UpperCAmelCase_ (					self					:					Optional[Any]						,      _A					:					str						,      _A					:					Union[str, Any]						,      _A					:					Optional[int]						,      _A					:					List[str]						,      _A					:					List[Any]						,      _A					:					Tuple						,      _A					:					int		)							->			List[str]:







							"""simple docstring"""


							snake_case_						:	int      =		MobileBertForNextSentencePrediction(config=_A		)
							model.to(_A		)
							model.eval()
							snake_case_						:	str      =		model(
							    _A						,      attention_mask=_A						,      token_type_ids=_A						,      labels=_A						,      )
							self.parent.assertEqual(result.logits.shape						,      (self.batch_size, 2)		)






						def UpperCAmelCase_ (					self					:					List[str]						,      _A					:					str						,      _A					:					int						,      _A					:					Any						,      _A					:					str						,      _A					:					Optional[Any]						,      _A					:					List[Any]						,      _A					:					int		)							->			str:







							"""simple docstring"""


							snake_case_						:	Any      =		MobileBertForPreTraining(config=_A		)
							model.to(_A		)
							model.eval()
							snake_case_						:	List[str]      =		model(
							    _A						,      attention_mask=_A						,      token_type_ids=_A						,      labels=_A						,      next_sentence_label=_A						,      )
							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 UpperCAmelCase_ (					self					:					Optional[Any]						,      _A					:					Any						,      _A					:					str						,      _A					:					str						,      _A					:					Optional[int]						,      _A					:					Dict						,      _A					:					Any						,      _A					:					str		)							->			List[str]:







							"""simple docstring"""


							snake_case_						:	Tuple      =		MobileBertForQuestionAnswering(config=_A		)
							model.to(_A		)
							model.eval()
							snake_case_						:	Any      =		model(
							    _A						,      attention_mask=_A						,      token_type_ids=_A						,      start_positions=_A						,      end_positions=_A						,      )
							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 UpperCAmelCase_ (					self					:					Tuple						,      _A					:					List[Any]						,      _A					:					Tuple						,      _A					:					Any						,      _A					:					Dict						,      _A					:					Union[str, Any]						,      _A					:					Optional[Any]						,      _A					:					List[str]		)							->			str:







							"""simple docstring"""


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






						def UpperCAmelCase_ (					self					:					Any						,      _A					:					List[str]						,      _A					:					List[str]						,      _A					:					Any						,      _A					:					int						,      _A					:					Dict						,      _A					:					Optional[Any]						,      _A					:					str		)							->			Optional[Any]:







							"""simple docstring"""


							snake_case_						:	int      =		self.num_labels
							snake_case_						:	Dict      =		MobileBertForTokenClassification(config=_A		)
							model.to(_A		)
							model.eval()
							snake_case_						:	List[str]      =		model(_A						,      attention_mask=_A						,      token_type_ids=_A						,      labels=_A		)
							self.parent.assertEqual(result.logits.shape						,      (self.batch_size, self.seq_length, self.num_labels)		)






						def UpperCAmelCase_ (					self					:					Optional[Any]						,      _A					:					List[str]						,      _A					:					Optional[int]						,      _A					:					str						,      _A					:					int						,      _A					:					Dict						,      _A					:					str						,      _A					:					List[Any]		)							->			Any:







							"""simple docstring"""


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






						def UpperCAmelCase_ (					self					:					Optional[Any]		)							->			Union[str, Any]:







							"""simple docstring"""


							snake_case_						:	Optional[int]      =		self.prepare_config_and_inputs()
							(
							    snake_case_							
							)						:	List[Any]      =		config_and_inputs
							snake_case_						:	List[str]      =		{'''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    ):
						__magic_name__:	int       					=				(
						    (
						        MobileBertModel,
						        MobileBertForMaskedLM,
						        MobileBertForMultipleChoice,
						        MobileBertForNextSentencePrediction,
						        MobileBertForPreTraining,
						        MobileBertForQuestionAnswering,
						        MobileBertForSequenceClassification,
						        MobileBertForTokenClassification,
						    )
						    if is_torch_available()
						    else ()
						)
						__magic_name__:	Union[str, Any]       					=				(
						    {
						        '''feature-extraction''': MobileBertModel,
						        '''fill-mask''': MobileBertForMaskedLM,
						        '''question-answering''': MobileBertForQuestionAnswering,
						        '''text-classification''': MobileBertForSequenceClassification,
						        '''token-classification''': MobileBertForTokenClassification,
						        '''zero-shot''': MobileBertForSequenceClassification,
						    }
						    if is_torch_available()
						    else {}
						)
						__magic_name__:	Union[str, Any]       					=				True






						def UpperCAmelCase_ (					self					:					List[Any]						,      _A					:					str						,      _A					:					Union[str, Any]						,      _A					:					Tuple=False		)							->			List[str]:







							"""simple docstring"""


							snake_case_						:	Dict      =		super()._prepare_for_class(_A						,      _A						,      return_labels=_A		)

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






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







							"""simple docstring"""


							snake_case_						:	Tuple      =		MobileBertModelTester(self		)
							snake_case_						:	List[str]      =		ConfigTester(self						,      config_class=_A						,      hidden_size=37		)






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







							"""simple docstring"""


							self.config_tester.run_common_tests()






						def UpperCAmelCase_ (					self					:					List[str]		)							->			Tuple:







							"""simple docstring"""


							snake_case_						:	Union[str, Any]      =		self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_mobilebert_model(*_A		)






						def UpperCAmelCase_ (					self					:					str		)							->			Any:







							"""simple docstring"""


							snake_case_						:	Optional[int]      =		self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_mobilebert_for_masked_lm(*_A		)






						def UpperCAmelCase_ (					self					:					Optional[Any]		)							->			Dict:







							"""simple docstring"""


							snake_case_						:	str      =		self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_mobilebert_for_multiple_choice(*_A		)






						def UpperCAmelCase_ (					self					:					Dict		)							->			str:







							"""simple docstring"""


							snake_case_						:	List[Any]      =		self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_mobilebert_for_next_sequence_prediction(*_A		)






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







							"""simple docstring"""


							snake_case_						:	List[str]      =		self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_mobilebert_for_pretraining(*_A		)






						def UpperCAmelCase_ (					self					:					Optional[Any]		)							->			str:







							"""simple docstring"""


							snake_case_						:	Dict      =		self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_mobilebert_for_question_answering(*_A		)






						def UpperCAmelCase_ (					self					:					Dict		)							->			Dict:







							"""simple docstring"""


							snake_case_						:	Optional[Any]      =		self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_mobilebert_for_sequence_classification(*_A		)






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







							"""simple docstring"""


							snake_case_						:	str      =		self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_mobilebert_for_token_classification(*_A		)


def 							SCREAMING_SNAKE_CASE__  (			__a      ):
	return torch.tensor(
	    _UpperCamelCase      ,       dtype=torch.long      ,       device=_UpperCamelCase      ,       )


_SCREAMING_SNAKE_CASE							=						1e-3


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






						@slow
						def UpperCAmelCase_ (					self					:					List[str]		)							->			Optional[Any]:







							"""simple docstring"""


							snake_case_						:	Optional[Any]      =		MobileBertModel.from_pretrained('google/mobilebert-uncased'		).to(_A		)
							snake_case_						:	Optional[Any]      =		_long_tensor([[101, 7110, 1005, 1056, 2023, 11333, 17413, 1029, 102]]		)
							with torch.no_grad():
								snake_case_						:	Optional[int]      =		model(_A		)[0]
							snake_case_						:	Union[str, Any]      =		torch.Size((1, 9, 512)		)
							self.assertEqual(output.shape						,      _A		)
							snake_case_						:	List[str]      =		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=_A						,      )

							# 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_						:	Any      =		torch.all((expected_slice / output[..., :3, :3]) >= 1 - TOLERANCE		)
							snake_case_						:	Union[str, Any]      =		torch.all((expected_slice / output[..., :3, :3]) <= 1 + TOLERANCE		)

							self.assertTrue(lower_bound and upper_bound		)








from ...utils import (
    OptionalDependencyNotAvailable,
    is_torch_available,
    is_transformers_available,
    is_transformers_version,
)


try:
	if not (is_transformers_available() and is_torch_available() and is_transformers_version('''>=''', '''4.25.0''')):
		raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
	from ...utils.dummy_torch_and_transformers_objects import UnCLIPImageVariationPipeline, UnCLIPPipeline
else:
	from .pipeline_unclip import UnCLIPPipeline
	from .pipeline_unclip_image_variation import UnCLIPImageVariationPipeline
	from .text_proj import UnCLIPTextProjModel
