
from __future__ import annotations

import unittest

from transformers import MobileBertConfig, is_tf_available
from transformers.models.auto import get_values
from transformers.testing_utils import require_tf, slow

from ...test_configuration_common import ConfigTester
from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_tf_available():
     import tensorflow as tf

     from transformers import (
         TF_MODEL_FOR_PRETRAINING_MAPPING,
         TFMobileBertForMaskedLM,
         TFMobileBertForMultipleChoice,
         TFMobileBertForNextSentencePrediction,
         TFMobileBertForPreTraining,
         TFMobileBertForQuestionAnswering,
         TFMobileBertForSequenceClassification,
         TFMobileBertForTokenClassification,
         TFMobileBertModel,
     )





@require_tf
class 							UpperCAmelCase				(  A__      ,      A__      ,      unittest.TestCase  ):




 '''simple docstring'''





 snake_case_    =					(
     (
         TFMobileBertModel,
         TFMobileBertForMaskedLM,
         TFMobileBertForNextSentencePrediction,
         TFMobileBertForPreTraining,
         TFMobileBertForQuestionAnswering,
         TFMobileBertForSequenceClassification,
         TFMobileBertForTokenClassification,
         TFMobileBertForMultipleChoice,
     )
     if is_tf_available()
     else ()
 )
 snake_case_    =					(
     {
         "feature-extraction": TFMobileBertModel,
         "fill-mask": TFMobileBertForMaskedLM,
         "question-answering": TFMobileBertForQuestionAnswering,
         "text-classification": TFMobileBertForSequenceClassification,
         "token-classification": TFMobileBertForTokenClassification,
         "zero-shot": TFMobileBertForSequenceClassification,
     }
     if is_tf_available()
     else {}
 )
 snake_case_    =					False
 snake_case_    =					False






 def     UpperCamelCase_  (	self :			Dict							,A :			Tuple							,A :			str							,A :			Tuple=False			):
     __A             =			super()._prepare_for_class(__snake_case							,__snake_case							,return_labels=__snake_case			)

     if return_labels:
         if model_class in get_values(__snake_case			):
             __A             =			tf.zeros(self.model_tester.batch_size							,dtype=tf.intaa			)

     return inputs_dict





 class 							UpperCAmelCase				(  A__  ):




  '''simple docstring'''





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






  def     UpperCamelCase_  (	self :			int			):
      __A             =			ids_tensor([self.batch_size, self.seq_length]							,self.vocab_size			)

      __A             =			None
      if self.use_input_mask:
          __A             =			random_attention_mask([self.batch_size, self.seq_length]			)

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

      __A             =			None
      __A             =			None
      __A             =			None
      if self.use_labels:
          __A             =			ids_tensor([self.batch_size]							,self.type_sequence_label_size			)
          __A             =			ids_tensor([self.batch_size, self.seq_length]							,self.num_labels			)
          __A             =			ids_tensor([self.batch_size]							,self.num_choices			)

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

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






  def     UpperCamelCase_  (	self :			Union[str, Any]							,A :			Optional[int]							,A :			List[Any]							,A :			Union[str, Any]							,A :			List[Any]							,A :			Any							,A :			Dict							,A :			Tuple			):
      __A             =			TFMobileBertModel(config=__snake_case			)
      __A             =			{"input_ids": input_ids, "attention_mask": input_mask, "token_type_ids": token_type_ids}
      __A             =			model(__snake_case			)

      __A             =			[input_ids, input_mask]
      __A             =			model(__snake_case			)

      __A             =			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     UpperCamelCase_  (	self :			str							,A :			str							,A :			int							,A :			List[Any]							,A :			int							,A :			str							,A :			List[str]							,A :			Optional[int]			):
      __A             =			TFMobileBertForMaskedLM(config=__snake_case			)
      __A             =			{"input_ids": input_ids, "attention_mask": input_mask, "token_type_ids": token_type_ids}
      __A             =			model(__snake_case			)
      self.parent.assertEqual(result.logits.shape							,(self.batch_size, self.seq_length, self.vocab_size)			)






  def     UpperCamelCase_  (	self :			Any							,A :			Any							,A :			int							,A :			Tuple							,A :			List[Any]							,A :			Tuple							,A :			List[Any]							,A :			Tuple			):
      __A             =			TFMobileBertForNextSentencePrediction(config=__snake_case			)
      __A             =			{"input_ids": input_ids, "attention_mask": input_mask, "token_type_ids": token_type_ids}
      __A             =			model(__snake_case			)
      self.parent.assertEqual(result.logits.shape							,(self.batch_size, 2)			)






  def     UpperCamelCase_  (	self :			Optional[Any]							,A :			List[str]							,A :			int							,A :			Dict							,A :			List[Any]							,A :			Optional[Any]							,A :			str							,A :			List[Any]			):
      __A             =			TFMobileBertForPreTraining(config=__snake_case			)
      __A             =			{"input_ids": input_ids, "attention_mask": input_mask, "token_type_ids": token_type_ids}
      __A             =			model(__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     UpperCamelCase_  (	self :			Tuple							,A :			List[Any]							,A :			Dict							,A :			List[Any]							,A :			List[str]							,A :			str							,A :			List[Any]							,A :			List[str]			):
      __A             =			self.num_labels
      __A             =			TFMobileBertForSequenceClassification(config=__snake_case			)
      __A             =			{"input_ids": input_ids, "attention_mask": input_mask, "token_type_ids": token_type_ids}
      __A             =			model(__snake_case			)
      self.parent.assertEqual(result.logits.shape							,(self.batch_size, self.num_labels)			)






  def     UpperCamelCase_  (	self :			int							,A :			Optional[int]							,A :			Optional[Any]							,A :			Any							,A :			Tuple							,A :			int							,A :			Union[str, Any]							,A :			int			):
      __A             =			self.num_choices
      __A             =			TFMobileBertForMultipleChoice(config=__snake_case			)
      __A             =			tf.tile(tf.expand_dims(__snake_case							,1			)							,(1, self.num_choices, 1)			)
      __A             =			tf.tile(tf.expand_dims(__snake_case							,1			)							,(1, self.num_choices, 1)			)
      __A             =			tf.tile(tf.expand_dims(__snake_case							,1			)							,(1, self.num_choices, 1)			)
      __A             =			{
          "input_ids": multiple_choice_inputs_ids,
          "attention_mask": multiple_choice_input_mask,
          "token_type_ids": multiple_choice_token_type_ids,
      }
      __A             =			model(__snake_case			)
      self.parent.assertEqual(result.logits.shape							,(self.batch_size, self.num_choices)			)






  def     UpperCamelCase_  (	self :			str							,A :			List[Any]							,A :			List[str]							,A :			Tuple							,A :			Any							,A :			Tuple							,A :			Union[str, Any]							,A :			Optional[int]			):
      __A             =			self.num_labels
      __A             =			TFMobileBertForTokenClassification(config=__snake_case			)
      __A             =			{"input_ids": input_ids, "attention_mask": input_mask, "token_type_ids": token_type_ids}
      __A             =			model(__snake_case			)
      self.parent.assertEqual(result.logits.shape							,(self.batch_size, self.seq_length, self.num_labels)			)






  def     UpperCamelCase_  (	self :			List[Any]							,A :			Dict							,A :			Union[str, Any]							,A :			Union[str, Any]							,A :			Any							,A :			Union[str, Any]							,A :			Tuple							,A :			Dict			):
      __A             =			TFMobileBertForQuestionAnswering(config=__snake_case			)
      __A             =			{"input_ids": input_ids, "attention_mask": input_mask, "token_type_ids": token_type_ids}
      __A             =			model(__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     UpperCamelCase_  (	self :			Optional[int]			):
      __A             =			self.prepare_config_and_inputs()
      (
          (
          __A  
      )      ,							(
          __A  
      )      ,							(
          __A  
      )      ,							(
          __A  
      )      ,							(
          __A  
      )      ,							(
          __A  
      )      ,							(
          __A  
      )      ,							  
      )             =			config_and_inputs
      __A             =			{"input_ids": input_ids, "token_type_ids": token_type_ids, "attention_mask": input_mask}
      return config, inputs_dict






 def     UpperCamelCase_  (	self :			Union[str, Any]			):
     __A             =			TFMobileBertModelTest.TFMobileBertModelTester(self			)
     __A             =			ConfigTester(self							,config_class=__snake_case							,hidden_size=37			)






 def     UpperCamelCase_  (	self :			int			):
     self.config_tester.run_common_tests()






 def     UpperCamelCase_  (	self :			Dict			):
     __A             =			self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_mobilebert_model(*__snake_case			)






 def     UpperCamelCase_  (	self :			Tuple			):
     __A             =			self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_mobilebert_for_masked_lm(*__snake_case			)






 def     UpperCamelCase_  (	self :			List[Any]			):
     __A             =			self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_mobilebert_for_multiple_choice(*__snake_case			)






 def     UpperCamelCase_  (	self :			Any			):
     __A             =			self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_mobilebert_for_next_sequence_prediction(*__snake_case			)






 def     UpperCamelCase_  (	self :			int			):
     __A             =			self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_mobilebert_for_pretraining(*__snake_case			)






 def     UpperCamelCase_  (	self :			Optional[Any]			):
     __A             =			self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_mobilebert_for_question_answering(*__snake_case			)






 def     UpperCamelCase_  (	self :			Tuple			):
     __A             =			self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_mobilebert_for_sequence_classification(*__snake_case			)






 def     UpperCamelCase_  (	self :			Optional[int]			):
     __A             =			self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_mobilebert_for_token_classification(*__snake_case			)







 @slow
 def     UpperCamelCase_  (	self :			Any			):
     # for model_name in TF_MOBILEBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
     for model_name in ["google/mobilebert-uncased"]:
         __A             =			TFMobileBertModel.from_pretrained(__snake_case			)
         self.assertIsNotNone(__snake_case			)





@require_tf
class 							UpperCAmelCase				(  unittest.TestCase  ):




 '''simple docstring'''





 @slow
 def     UpperCamelCase_  (	self :			Optional[int]			):
     __A             =			TFMobileBertForPreTraining.from_pretrained("google/mobilebert-uncased"			)
     __A             =			tf.constant([[0, 1, 2, 3, 4, 5]]			)
     __A             =			model(__snake_case			)[0]

     __A             =			[1, 6, 3_05_22]
     self.assertEqual(output.shape							,__snake_case			)

     __A             =			tf.constant(
         [
             [
                 [-4.5_91_95_47, -9.24_82_95, -9.64_52_56],
                 [-6.7_30_61_75, -6.44_02_84, -6.6_05_28_37],
                 [-7.2_74_35_06, -6.7_84_79_15, -6.02_46_73],
             ]
         ]			)
     tf.debugging.assert_near(output[:, :3, :3]							,__snake_case							,atol=1E-4			)


import warnings

from ...utils import logging
from .image_processing_clip import CLIPImageProcessor


SCREAMING_SNAKE_CASE							:Any										=    logging.get_logger(__name__)





class 							UpperCAmelCase				(  __SCREAMING_SNAKE_CASE  ):




	'''simple docstring'''





	def __init__(	self :			int							,*A :			Dict							,**A :			Dict			):
					warnings.warn(
					    "The class CLIPFeatureExtractor is deprecated and will be removed in version 5 of Transformers. Please"
					    " use CLIPImageProcessor instead."							,A							,)
					super().__init__(*A							,**A			)
