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






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






   def       __UpperCamelCase     (      self				:     Optional[int]  )					->						Tuple:
       lowercase__ :   Optional[int]    					=				ids_tensor([self.batch_size, self.seq_length]	,						self.vocab_size  )

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

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

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

       lowercase__ :   Union[str, Any]    					=				self.get_config()

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






   def       __UpperCamelCase     (      self				:     Optional[int]  )					->						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=lowercase_	,						initializer_range=self.initializer_range	,						)






   def       __UpperCamelCase     (      self				:     List[str]	,						lowercase_				:     str	,						lowercase_				:     Optional[Any]	,						lowercase_				:     List[str]	,						lowercase_				:     int	,						lowercase_				:     Optional[int]	,						lowercase_				:     Union[str, Any]	,						lowercase_				:     Optional[int]  )					->						List[Any]:
       lowercase__ :   List[str]    					=				MobileBertModel(config=lowercase_  )
       model.to(lowercase_  )
       model.eval()
       lowercase__ :   List[Any]    					=				model(lowercase_	,						attention_mask=lowercase_	,						token_type_ids=lowercase_  )
       lowercase__ :   Union[str, Any]    					=				model(lowercase_	,						token_type_ids=lowercase_  )
       lowercase__ :   Any    					=				model(lowercase_  )

       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[Any]	,						lowercase_				:     Dict	,						lowercase_				:     Dict	,						lowercase_				:     int	,						lowercase_				:     Optional[int]	,						lowercase_				:     Dict	,						lowercase_				:     str	,						lowercase_				:     List[Any]  )					->						str:
       lowercase__ :   Tuple    					=				MobileBertForMaskedLM(config=lowercase_  )
       model.to(lowercase_  )
       model.eval()
       lowercase__ :   Any    					=				model(lowercase_	,						attention_mask=lowercase_	,						token_type_ids=lowercase_	,						labels=lowercase_  )
       self.parent.assertEqual(result.logits.shape	,						(self.batch_size, self.seq_length, self.vocab_size)  )






   def       __UpperCamelCase     (      self				:     Union[str, Any]	,						lowercase_				:     Any	,						lowercase_				:     Union[str, Any]	,						lowercase_				:     List[Any]	,						lowercase_				:     str	,						lowercase_				:     List[Any]	,						lowercase_				:     Optional[Any]	,						lowercase_				:     int  )					->						int:
       lowercase__ :   Union[str, Any]    					=				MobileBertForNextSentencePrediction(config=lowercase_  )
       model.to(lowercase_  )
       model.eval()
       lowercase__ :   List[str]    					=				model(
           lowercase_	,						attention_mask=lowercase_	,						token_type_ids=lowercase_	,						labels=lowercase_	,						)
       self.parent.assertEqual(result.logits.shape	,						(self.batch_size, 2)  )






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






   def       __UpperCamelCase     (      self				:     List[str]	,						lowercase_				:     Any	,						lowercase_				:     Any	,						lowercase_				:     Union[str, Any]	,						lowercase_				:     Optional[int]	,						lowercase_				:     Union[str, Any]	,						lowercase_				:     List[Any]	,						lowercase_				:     Tuple  )					->						Dict:
       lowercase__ :   int    					=				self.num_labels
       lowercase__ :   Dict    					=				MobileBertForTokenClassification(config=lowercase_  )
       model.to(lowercase_  )
       model.eval()
       lowercase__ :   Dict    					=				model(lowercase_	,						attention_mask=lowercase_	,						token_type_ids=lowercase_	,						labels=lowercase_  )
       self.parent.assertEqual(result.logits.shape	,						(self.batch_size, self.seq_length, self.num_labels)  )






   def       __UpperCamelCase     (      self				:     Tuple	,						lowercase_				:     Any	,						lowercase_				:     Union[str, Any]	,						lowercase_				:     Union[str, Any]	,						lowercase_				:     Union[str, Any]	,						lowercase_				:     List[Any]	,						lowercase_				:     Optional[Any]	,						lowercase_				:     Union[str, Any]  )					->						Union[str, Any]:
       lowercase__ :   Any    					=				self.num_choices
       lowercase__ :   Any    					=				MobileBertForMultipleChoice(config=lowercase_  )
       model.to(lowercase_  )
       model.eval()
       lowercase__ :   Optional[Any]    					=				input_ids.unsqueeze(1  ).expand(-1	,						self.num_choices	,						-1  ).contiguous()
       lowercase__ :   int    					=				token_type_ids.unsqueeze(1  ).expand(-1	,						self.num_choices	,						-1  ).contiguous()
       lowercase__ :   Optional[int]    					=				input_mask.unsqueeze(1  ).expand(-1	,						self.num_choices	,						-1  ).contiguous()
       lowercase__ :   Optional[int]    					=				model(
           lowercase_	,						attention_mask=lowercase_	,						token_type_ids=lowercase_	,						labels=lowercase_	,						)
       self.parent.assertEqual(result.logits.shape	,						(self.batch_size, self.num_choices)  )







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



@require_torch
class   snake_case_       (		__A  ,__A  ,unittest.TestCase       ):
   __A	:		Union[str, Any]         =				(
       (
           MobileBertModel,
           MobileBertForMaskedLM,
           MobileBertForMultipleChoice,
           MobileBertForNextSentencePrediction,
           MobileBertForPreTraining,
           MobileBertForQuestionAnswering,
           MobileBertForSequenceClassification,
           MobileBertForTokenClassification,
       )
       if is_torch_available()
       else ()
   )
   __A	:		int         =				(
       {
           "feature-extraction": MobileBertModel,
           "fill-mask": MobileBertForMaskedLM,
           "question-answering": MobileBertForQuestionAnswering,
           "text-classification": MobileBertForSequenceClassification,
           "token-classification": MobileBertForTokenClassification,
           "zero-shot": MobileBertForSequenceClassification,
       }
       if is_torch_available()
       else {}
   )
   __A	:		List[str]         =				True






   def       __UpperCamelCase     (      self				:     Dict	,						lowercase_				:     List[str]	,						lowercase_				:     Any	,						lowercase_				:     Dict=False  )					->						Any:
       lowercase__ :   str    					=				super()._prepare_for_class(lowercase_	,						lowercase_	,						return_labels=lowercase_  )

       if return_labels:
           if model_class in get_values(lowercase_  ):
               lowercase__ :   List[str]    					=				torch.zeros(
                   (self.model_tester.batch_size, self.model_tester.seq_length)	,						dtype=torch.long	,						device=lowercase_  )
               lowercase__ :   List[str]    					=				torch.zeros(
                   self.model_tester.batch_size	,						dtype=torch.long	,						device=lowercase_  )
       return inputs_dict






   def       __UpperCamelCase     (      self				:     Any  )					->						Dict:
       lowercase__ :   Optional[int]    					=				MobileBertModelTester(self  )
       lowercase__ :   Union[str, Any]    					=				ConfigTester(self	,						config_class=lowercase_	,						hidden_size=37  )






   def       __UpperCamelCase     (      self				:     Optional[Any]  )					->						Tuple:
       self.config_tester.run_common_tests()






   def       __UpperCamelCase     (      self				:     int  )					->						Tuple:
       lowercase__ :   List[Any]    					=				self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_mobilebert_model(*lowercase_  )






   def       __UpperCamelCase     (      self				:     str  )					->						Optional[Any]:
       lowercase__ :   List[str]    					=				self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_mobilebert_for_masked_lm(*lowercase_  )






   def       __UpperCamelCase     (      self				:     Dict  )					->						Dict:
       lowercase__ :   Optional[int]    					=				self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_mobilebert_for_multiple_choice(*lowercase_  )






   def       __UpperCamelCase     (      self				:     Any  )					->						Any:
       lowercase__ :   int    					=				self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_mobilebert_for_next_sequence_prediction(*lowercase_  )






   def       __UpperCamelCase     (      self				:     Dict  )					->						Tuple:
       lowercase__ :   Dict    					=				self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_mobilebert_for_pretraining(*lowercase_  )






   def       __UpperCamelCase     (      self				:     Optional[Any]  )					->						int:
       lowercase__ :   Optional[Any]    					=				self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_mobilebert_for_question_answering(*lowercase_  )






   def       __UpperCamelCase     (      self				:     Optional[int]  )					->						Optional[int]:
       lowercase__ :   List[Any]    					=				self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_mobilebert_for_sequence_classification(*lowercase_  )







   def       __UpperCamelCase     (      self				:     Dict  )					->						int:
       lowercase__ :   List[Any]    					=				self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_mobilebert_for_token_classification(*lowercase_  )



def 			lowercase_				(			_lowerCamelCase							:       int):
    return torch.tensor(
        _lowerCamelCase		,	dtype=torch.long		,	device=_lowerCamelCase		,	)


UpperCamelCase  							=						1E-3



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






   @slow
   def       __UpperCamelCase     (      self				:     List[Any]  )					->						Any:
       lowercase__ :   Dict    					=				MobileBertModel.from_pretrained("google/mobilebert-uncased"  ).to(lowercase_  )
       lowercase__ :   Dict    					=				_long_tensor([[1_01, 71_10, 10_05, 10_56, 20_23, 1_13_33, 1_74_13, 10_29, 1_02]]  )
       with torch.no_grad():
           lowercase__ :   Optional[int]    					=				model(lowercase_  )[0]
       lowercase__ :   str    					=				torch.Size((1, 9, 5_12)  )
       self.assertEqual(output.shape	,						lowercase_  )
       lowercase__ :   Optional[Any]    					=				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=lowercase_	,						)

       # 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
       lowercase__ :   Tuple    					=				torch.all((expected_slice / output[..., :3, :3]) >= 1 - TOLERANCE  )
       lowercase__ :   Optional[int]    					=				torch.all((expected_slice / output[..., :3, :3]) <= 1 + TOLERANCE  )

       self.assertTrue(lower_bound and upper_bound  )








'''simple docstring'''




from typing import List, Optional, Union

from ...image_utils import ImageInput
from ...processing_utils import ProcessorMixin
from ...tokenization_utils_base import BatchEncoding, PaddingStrategy, PreTokenizedInput, TextInput, TruncationStrategy
from ...utils import TensorType







class 	_lowerCAmelCase							(			__UpperCAmelCase ):
	__SCREAMING_SNAKE_CASE   :		Any					=					['image_processor', 'tokenizer']
	__SCREAMING_SNAKE_CASE   :		Union[str, Any]					=					'BlipImageProcessor'
	__SCREAMING_SNAKE_CASE   :		List[Any]					=					('BertTokenizer', 'BertTokenizerFast')


	def __init__(self       ,      lowercase       ,      lowercase							):
				A_	:						List[Any]	=     False
				super().__init__(lowercase       ,      lowercase							)
				A_	:						Tuple	=     self.image_processor


	def __call__(self       ,      lowercase = None       ,      lowercase = None       ,      lowercase = True       ,      lowercase = False       ,      lowercase = None       ,      lowercase = None       ,      lowercase = 0       ,      lowercase = None       ,      lowercase = None       ,      lowercase = False       ,      lowercase = False       ,      lowercase = False       ,      lowercase = False       ,      lowercase = False       ,      lowercase = True       ,      lowercase = None       ,      **lowercase       ,      ):
				if images is None and text is None:
							raise ValueError("""You have to specify either images or text."""							)

				# Get only text
				if images is None:
							A_	:						Optional[Any]	=     self.tokenizer
							A_	:						Tuple	=     self.tokenizer(
							    text=lowercase       ,      add_special_tokens=lowercase       ,      padding=lowercase       ,      truncation=lowercase       ,      max_length=lowercase       ,      stride=lowercase       ,      pad_to_multiple_of=lowercase       ,      return_attention_mask=lowercase       ,      return_overflowing_tokens=lowercase       ,      return_special_tokens_mask=lowercase       ,      return_offsets_mapping=lowercase       ,      return_token_type_ids=lowercase       ,      return_length=lowercase       ,      verbose=lowercase       ,      return_tensors=lowercase       ,      **lowercase       ,      )
							return text_encoding

				# add pixel_values
				A_	:						int	=     self.image_processor(lowercase       ,      return_tensors=lowercase							)

				if text is not None:
							A_	:						Optional[Any]	=     self.tokenizer(
							    text=lowercase       ,      add_special_tokens=lowercase       ,      padding=lowercase       ,      truncation=lowercase       ,      max_length=lowercase       ,      stride=lowercase       ,      pad_to_multiple_of=lowercase       ,      return_attention_mask=lowercase       ,      return_overflowing_tokens=lowercase       ,      return_special_tokens_mask=lowercase       ,      return_offsets_mapping=lowercase       ,      return_token_type_ids=lowercase       ,      return_length=lowercase       ,      verbose=lowercase       ,      return_tensors=lowercase       ,      **lowercase       ,      )
				else:
							A_	:						List[str]	=     None

				if text_encoding is not None:
							encoding_image_processor.update(lowercase							)

				return encoding_image_processor


	def     _a	(self       ,      *lowercase       ,      **lowercase							):
				return self.tokenizer.batch_decode(*lowercase       ,      **lowercase							)


	def     _a	(self       ,      *lowercase       ,      **lowercase							):
				return self.tokenizer.decode(*lowercase       ,      **lowercase							)


	@property
	def     _a	(self							):
				A_	:						int	=     self.tokenizer.model_input_names
				A_	:						Any	=     self.image_processor.model_input_names
				return list(dict.fromkeys(tokenizer_input_names + image_processor_input_names							)							)