





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




					def __init__(		self				,						lowerCAmelCase_				,						lowerCAmelCase_=13				,						lowerCAmelCase_=7				,						lowerCAmelCase_=True				,						lowerCAmelCase_=True				,						lowerCAmelCase_=True				,						lowerCAmelCase_=True				,						lowerCAmelCase_=99				,						lowerCAmelCase_=64				,						lowerCAmelCase_=32				,						lowerCAmelCase_=5				,						lowerCAmelCase_=4				,						lowerCAmelCase_=37				,						lowerCAmelCase_="gelu"				,						lowerCAmelCase_=0.1				,						lowerCAmelCase_=0.1				,						lowerCAmelCase_=512				,						lowerCAmelCase_=16				,						lowerCAmelCase_=2				,						lowerCAmelCase_=0.02				,						lowerCAmelCase_=3				,						lowerCAmelCase_=4				,						lowerCAmelCase_=None				,						)		->							Union[str, Any]:
										_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  )		->							Dict:
										_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  )		->							str:
										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=lowerCAmelCase_				,						initializer_range=self.initializer_range				,						)




					def 	lowerCAmelCase			(		self				,						lowerCAmelCase_				,						lowerCAmelCase_				,						lowerCAmelCase_				,						lowerCAmelCase_				,						lowerCAmelCase_				,						lowerCAmelCase_				,						lowerCAmelCase_  )		->							int:
										_snake_case									=				MobileBertModel(config=lowerCAmelCase_  )
										model.to(lowerCAmelCase_  )
										model.eval()
										_snake_case									=				model(lowerCAmelCase_				,						attention_mask=lowerCAmelCase_				,						token_type_ids=lowerCAmelCase_  )
										_snake_case									=				model(lowerCAmelCase_				,						token_type_ids=lowerCAmelCase_  )
										_snake_case									=				model(lowerCAmelCase_  )

										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				,						lowerCAmelCase_				,						lowerCAmelCase_				,						lowerCAmelCase_				,						lowerCAmelCase_				,						lowerCAmelCase_				,						lowerCAmelCase_				,						lowerCAmelCase_  )		->							Tuple:
										_snake_case									=				MobileBertForMaskedLM(config=lowerCAmelCase_  )
										model.to(lowerCAmelCase_  )
										model.eval()
										_snake_case									=				model(lowerCAmelCase_				,						attention_mask=lowerCAmelCase_				,						token_type_ids=lowerCAmelCase_				,						labels=lowerCAmelCase_  )
										self.parent.assertEqual(result.logits.shape				,						(self.batch_size, self.seq_length, self.vocab_size)  )




					def 	lowerCAmelCase			(		self				,						lowerCAmelCase_				,						lowerCAmelCase_				,						lowerCAmelCase_				,						lowerCAmelCase_				,						lowerCAmelCase_				,						lowerCAmelCase_				,						lowerCAmelCase_  )		->							Tuple:
										_snake_case									=				MobileBertForNextSentencePrediction(config=lowerCAmelCase_  )
										model.to(lowerCAmelCase_  )
										model.eval()
										_snake_case									=				model(
										    lowerCAmelCase_				,						attention_mask=lowerCAmelCase_				,						token_type_ids=lowerCAmelCase_				,						labels=lowerCAmelCase_				,						)
										self.parent.assertEqual(result.logits.shape				,						(self.batch_size, 2)  )




					def 	lowerCAmelCase			(		self				,						lowerCAmelCase_				,						lowerCAmelCase_				,						lowerCAmelCase_				,						lowerCAmelCase_				,						lowerCAmelCase_				,						lowerCAmelCase_				,						lowerCAmelCase_  )		->							int:
										_snake_case									=				MobileBertForPreTraining(config=lowerCAmelCase_  )
										model.to(lowerCAmelCase_  )
										model.eval()
										_snake_case									=				model(
										    lowerCAmelCase_				,						attention_mask=lowerCAmelCase_				,						token_type_ids=lowerCAmelCase_				,						labels=lowerCAmelCase_				,						next_sentence_label=lowerCAmelCase_				,						)
										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				,						lowerCAmelCase_				,						lowerCAmelCase_				,						lowerCAmelCase_				,						lowerCAmelCase_				,						lowerCAmelCase_				,						lowerCAmelCase_				,						lowerCAmelCase_  )		->							Tuple:
										_snake_case									=				MobileBertForQuestionAnswering(config=lowerCAmelCase_  )
										model.to(lowerCAmelCase_  )
										model.eval()
										_snake_case									=				model(
										    lowerCAmelCase_				,						attention_mask=lowerCAmelCase_				,						token_type_ids=lowerCAmelCase_				,						start_positions=lowerCAmelCase_				,						end_positions=lowerCAmelCase_				,						)
										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				,						lowerCAmelCase_				,						lowerCAmelCase_				,						lowerCAmelCase_				,						lowerCAmelCase_				,						lowerCAmelCase_				,						lowerCAmelCase_				,						lowerCAmelCase_  )		->							List[str]:
										_snake_case									=				self.num_labels
										_snake_case									=				MobileBertForSequenceClassification(lowerCAmelCase_  )
										model.to(lowerCAmelCase_  )
										model.eval()
										_snake_case									=				model(lowerCAmelCase_				,						attention_mask=lowerCAmelCase_				,						token_type_ids=lowerCAmelCase_				,						labels=lowerCAmelCase_  )
										self.parent.assertEqual(result.logits.shape				,						(self.batch_size, self.num_labels)  )




					def 	lowerCAmelCase			(		self				,						lowerCAmelCase_				,						lowerCAmelCase_				,						lowerCAmelCase_				,						lowerCAmelCase_				,						lowerCAmelCase_				,						lowerCAmelCase_				,						lowerCAmelCase_  )		->							int:
										_snake_case									=				self.num_labels
										_snake_case									=				MobileBertForTokenClassification(config=lowerCAmelCase_  )
										model.to(lowerCAmelCase_  )
										model.eval()
										_snake_case									=				model(lowerCAmelCase_				,						attention_mask=lowerCAmelCase_				,						token_type_ids=lowerCAmelCase_				,						labels=lowerCAmelCase_  )
										self.parent.assertEqual(result.logits.shape				,						(self.batch_size, self.seq_length, self.num_labels)  )




					def 	lowerCAmelCase			(		self				,						lowerCAmelCase_				,						lowerCAmelCase_				,						lowerCAmelCase_				,						lowerCAmelCase_				,						lowerCAmelCase_				,						lowerCAmelCase_				,						lowerCAmelCase_  )		->							List[Any]:
										_snake_case									=				self.num_choices
										_snake_case									=				MobileBertForMultipleChoice(config=lowerCAmelCase_  )
										model.to(lowerCAmelCase_  )
										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(
										    lowerCAmelCase_				,						attention_mask=lowerCAmelCase_				,						token_type_ids=lowerCAmelCase_				,						labels=lowerCAmelCase_				,						)
										self.parent.assertEqual(result.logits.shape				,						(self.batch_size, self.num_choices)  )






					def 	lowerCAmelCase			(		self  )		->							List[str]:
										_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 			UpperCamelCase_		(  _lowerCamelCase							,  _lowerCamelCase							,  unittest.TestCase					):
					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 	lowerCAmelCase			(		self				,						lowerCAmelCase_				,						lowerCAmelCase_				,						lowerCAmelCase_=False  )		->							int:
										_snake_case									=				super()._prepare_for_class(lowerCAmelCase_				,						lowerCAmelCase_				,						return_labels=lowerCAmelCase_  )

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




					def 	lowerCAmelCase			(		self  )		->							List[str]:
										_snake_case									=				MobileBertModelTester(self  )
										_snake_case									=				ConfigTester(self				,						config_class=lowerCAmelCase_				,						hidden_size=37  )




					def 	lowerCAmelCase			(		self  )		->							List[str]:
										self.config_tester.run_common_tests()




					def 	lowerCAmelCase			(		self  )		->							List[Any]:
										_snake_case									=				self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_mobilebert_model(*lowerCAmelCase_  )




					def 	lowerCAmelCase			(		self  )		->							List[str]:
										_snake_case									=				self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_mobilebert_for_masked_lm(*lowerCAmelCase_  )




					def 	lowerCAmelCase			(		self  )		->							Dict:
										_snake_case									=				self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_mobilebert_for_multiple_choice(*lowerCAmelCase_  )




					def 	lowerCAmelCase			(		self  )		->							List[str]:
										_snake_case									=				self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_mobilebert_for_next_sequence_prediction(*lowerCAmelCase_  )




					def 	lowerCAmelCase			(		self  )		->							str:
										_snake_case									=				self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_mobilebert_for_pretraining(*lowerCAmelCase_  )




					def 	lowerCAmelCase			(		self  )		->							Optional[int]:
										_snake_case									=				self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_mobilebert_for_question_answering(*lowerCAmelCase_  )




					def 	lowerCAmelCase			(		self  )		->							List[Any]:
										_snake_case									=				self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_mobilebert_for_sequence_classification(*lowerCAmelCase_  )






					def 	lowerCAmelCase			(		self  )		->							Optional[int]:
										_snake_case									=				self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_mobilebert_for_token_classification(*lowerCAmelCase_  )





def 						lowerCamelCase__     (     UpperCamelCase__			:       Dict						)			-> Optional[Any]:



					'''simple docstring'''




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


UpperCAmelCase_      							=     1E-3




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




					@slow
					def 	lowerCAmelCase			(		self  )		->							Optional[Any]:
										_snake_case									=				MobileBertModel.from_pretrained('google/mobilebert-uncased'  ).to(lowerCAmelCase_  )
										_snake_case									=				_long_tensor([[101, 7110, 1005, 1056, 2023, 1_1333, 1_7413, 1029, 102]]  )
										with torch.no_grad():
															_snake_case									=				model(lowerCAmelCase_  )[0]
										_snake_case									=				torch.Size((1, 9, 512)  )
										self.assertEqual(output.shape				,						lowerCAmelCase_  )
										_snake_case									=				torch.tensor(
										    [
										        [
										            [-2.4736526E07, 8.2691656E04, 1.6521838E05],
										            [-5.7541704E-01, 3.9056022E00, 4.4011507E00],
										            [2.6047359E00, 1.5677652E00, -1.7324188E-01],
										        ]
										    ]				,						device=lowerCAmelCase_				,						)

										# 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  )










from cva import destroyAllWindows, imread, imshow, waitKey





def 						lowerCamelCase__     (     UpperCamelCase__			:       Optional[int]						)			-> Optional[int]:



					'''simple docstring'''




					_snake_case							,     _snake_case									=				img.shape[0], img.shape[1]

					# converting each pixel's color to its negative
					for i in range(UpperCamelCase__						):
										for j in range(UpperCamelCase__						):
															_snake_case									=				[255, 255, 255] - img[i][j]

					return img


if __name__ == "__main__":
					# read original image
					UpperCAmelCase_      							=     imread("""image_data/lena.jpg""", 1)

					# convert to its negative
					UpperCAmelCase_      							=     convert_to_negative(img)

					# show result image
					imshow("""negative of original image""", img)
					waitKey(0)
					destroyAllWindows()



