"""simple docstring"""







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						(     lowercase_		, lowercase_		, unittest.TestCase    ):
   lowercase				=    (
       (
           TFMobileBertModel,
           TFMobileBertForMaskedLM,
           TFMobileBertForNextSentencePrediction,
           TFMobileBertForPreTraining,
           TFMobileBertForQuestionAnswering,
           TFMobileBertForSequenceClassification,
           TFMobileBertForTokenClassification,
           TFMobileBertForMultipleChoice,
       )
       if is_tf_available()
       else ()
   )
   lowercase				=    (
       {
           'feature-extraction': TFMobileBertModel,
           'fill-mask': TFMobileBertForMaskedLM,
           'question-answering': TFMobileBertForQuestionAnswering,
           'text-classification': TFMobileBertForSequenceClassification,
           'token-classification': TFMobileBertForTokenClassification,
           'zero-shot': TFMobileBertForSequenceClassification,
       }
       if is_tf_available()
       else {}
   )
   lowercase				=    False
   lowercase				=    False
   def      _UpperCAmelCase  (     self	,__UpperCamelCase	,__UpperCamelCase	,__UpperCamelCase=False     )      -> Dict:


        '''simple docstring'''


        lowercase_      :							Any						=      super()._prepare_for_class(_A	,_A	,return_labels=_A     )

        if return_labels:
             if model_class in get_values(_A     ):
                  lowercase_      :							List[Any]						=      tf.zeros(self.model_tester.batch_size	,dtype=tf.intaa     )

        return inputs_dict







   class 			UpperCamelCase						(     lowercase_    ):
      def __init__(     self	,__UpperCamelCase	,__UpperCamelCase=13	,__UpperCamelCase=7	,__UpperCamelCase=True	,__UpperCamelCase=True	,__UpperCamelCase=True	,__UpperCamelCase=True	,__UpperCamelCase=99	,__UpperCamelCase=32	,__UpperCamelCase=32	,__UpperCamelCase=2	,__UpperCamelCase=4	,__UpperCamelCase=37	,__UpperCamelCase="gelu"	,__UpperCamelCase=0.1	,__UpperCamelCase=0.1	,__UpperCamelCase=512	,__UpperCamelCase=16	,__UpperCamelCase=2	,__UpperCamelCase=0.02	,__UpperCamelCase=3	,__UpperCamelCase=4	,__UpperCamelCase=None	,)      -> Tuple:


           '''simple docstring'''


           lowercase_      :							str						=      parent
           lowercase_      :							Any						=      batch_size
           lowercase_      :							Dict						=      seq_length
           lowercase_      :							Any						=      is_training
           lowercase_      :							Dict						=      use_input_mask
           lowercase_      :							int						=      use_token_type_ids
           lowercase_      :							Union[str, Any]						=      use_labels
           lowercase_      :							Any						=      vocab_size
           lowercase_      :							Dict						=      hidden_size
           lowercase_      :							int						=      num_hidden_layers
           lowercase_      :							Optional[Any]						=      num_attention_heads
           lowercase_      :							List[Any]						=      intermediate_size
           lowercase_      :							Optional[int]						=      hidden_act
           lowercase_      :							Optional[Any]						=      hidden_dropout_prob
           lowercase_      :							str						=      attention_probs_dropout_prob
           lowercase_      :							Dict						=      max_position_embeddings
           lowercase_      :							List[Any]						=      type_vocab_size
           lowercase_      :							Any						=      type_sequence_label_size
           lowercase_      :							Tuple						=      initializer_range
           lowercase_      :							Tuple						=      num_labels
           lowercase_      :							List[str]						=      num_choices
           lowercase_      :							Tuple						=      scope
           lowercase_      :							int						=      embedding_size
      def      _UpperCAmelCase  (     self     )      -> int:


           '''simple docstring'''


           lowercase_      :							Tuple						=      ids_tensor([self.batch_size, self.seq_length]	,self.vocab_size     )

           lowercase_      :							Tuple						=      None
           if self.use_input_mask:
                lowercase_      :							List[Any]						=      random_attention_mask([self.batch_size, self.seq_length]     )

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

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

           lowercase_      :							Union[str, Any]						=      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	,__UpperCamelCase	,__UpperCamelCase	,__UpperCamelCase	,__UpperCamelCase	,__UpperCamelCase	,__UpperCamelCase	,__UpperCamelCase     )      -> Optional[Any]:


           '''simple docstring'''


           lowercase_      :							Tuple						=      TFMobileBertModel(config=_A     )
           lowercase_      :							Dict						=      {'''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids}
           lowercase_      :							Tuple						=      model(_A     )

           lowercase_      :							Tuple						=      [input_ids, input_mask]
           lowercase_      :							Tuple						=      model(_A     )

           lowercase_      :							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	,__UpperCamelCase	,__UpperCamelCase	,__UpperCamelCase	,__UpperCamelCase	,__UpperCamelCase	,__UpperCamelCase	,__UpperCamelCase     )      -> Dict:


           '''simple docstring'''


           lowercase_      :							Tuple						=      TFMobileBertForMaskedLM(config=_A     )
           lowercase_      :							Dict						=      {'''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids}
           lowercase_      :							Tuple						=      model(_A     )
           self.parent.assertEqual(result.logits.shape	,(self.batch_size, self.seq_length, self.vocab_size)     )
      def      _UpperCAmelCase  (     self	,__UpperCamelCase	,__UpperCamelCase	,__UpperCamelCase	,__UpperCamelCase	,__UpperCamelCase	,__UpperCamelCase	,__UpperCamelCase     )      -> Any:


           '''simple docstring'''


           lowercase_      :							Tuple						=      TFMobileBertForNextSentencePrediction(config=_A     )
           lowercase_      :							Optional[int]						=      {'''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids}
           lowercase_      :							int						=      model(_A     )
           self.parent.assertEqual(result.logits.shape	,(self.batch_size, 2)     )
      def      _UpperCAmelCase  (     self	,__UpperCamelCase	,__UpperCamelCase	,__UpperCamelCase	,__UpperCamelCase	,__UpperCamelCase	,__UpperCamelCase	,__UpperCamelCase     )      -> Union[str, Any]:


           '''simple docstring'''


           lowercase_      :							List[Any]						=      TFMobileBertForPreTraining(config=_A     )
           lowercase_      :							List[Any]						=      {'''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids}
           lowercase_      :							Optional[Any]						=      model(_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	,__UpperCamelCase	,__UpperCamelCase	,__UpperCamelCase	,__UpperCamelCase	,__UpperCamelCase	,__UpperCamelCase	,__UpperCamelCase     )      -> Dict:


           '''simple docstring'''


           lowercase_      :							Optional[int]						=      self.num_labels
           lowercase_      :							Union[str, Any]						=      TFMobileBertForSequenceClassification(config=_A     )
           lowercase_      :							List[str]						=      {'''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids}
           lowercase_      :							Dict						=      model(_A     )
           self.parent.assertEqual(result.logits.shape	,(self.batch_size, self.num_labels)     )
      def      _UpperCAmelCase  (     self	,__UpperCamelCase	,__UpperCamelCase	,__UpperCamelCase	,__UpperCamelCase	,__UpperCamelCase	,__UpperCamelCase	,__UpperCamelCase     )      -> int:


           '''simple docstring'''


           lowercase_      :							int						=      self.num_choices
           lowercase_      :							Dict						=      TFMobileBertForMultipleChoice(config=_A     )
           lowercase_      :							str						=      tf.tile(tf.expand_dims(_A	,1     )	,(1, self.num_choices, 1)     )
           lowercase_      :							Any						=      tf.tile(tf.expand_dims(_A	,1     )	,(1, self.num_choices, 1)     )
           lowercase_      :							Optional[int]						=      tf.tile(tf.expand_dims(_A	,1     )	,(1, self.num_choices, 1)     )
           lowercase_      :							List[Any]						=      {
               '''input_ids''': multiple_choice_inputs_ids,
               '''attention_mask''': multiple_choice_input_mask,
               '''token_type_ids''': multiple_choice_token_type_ids,
           }
           lowercase_      :							int						=      model(_A     )
           self.parent.assertEqual(result.logits.shape	,(self.batch_size, self.num_choices)     )
      def      _UpperCAmelCase  (     self	,__UpperCamelCase	,__UpperCamelCase	,__UpperCamelCase	,__UpperCamelCase	,__UpperCamelCase	,__UpperCamelCase	,__UpperCamelCase     )      -> Union[str, Any]:


           '''simple docstring'''


           lowercase_      :							str						=      self.num_labels
           lowercase_      :							Union[str, Any]						=      TFMobileBertForTokenClassification(config=_A     )
           lowercase_      :							str						=      {'''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids}
           lowercase_      :							List[str]						=      model(_A     )
           self.parent.assertEqual(result.logits.shape	,(self.batch_size, self.seq_length, self.num_labels)     )
      def      _UpperCAmelCase  (     self	,__UpperCamelCase	,__UpperCamelCase	,__UpperCamelCase	,__UpperCamelCase	,__UpperCamelCase	,__UpperCamelCase	,__UpperCamelCase     )      -> str:


           '''simple docstring'''


           lowercase_      :							Tuple						=      TFMobileBertForQuestionAnswering(config=_A     )
           lowercase_      :							Any						=      {'''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids}
           lowercase_      :							Any						=      model(_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     )      -> Dict:


           '''simple docstring'''


           lowercase_      :							Any						=      self.prepare_config_and_inputs()
           (
               lowercase_      
           )      :							Union[str, Any]						=      config_and_inputs
           lowercase_      :							Any						=      {'''input_ids''': input_ids, '''token_type_ids''': token_type_ids, '''attention_mask''': input_mask}
           return config, inputs_dict






   def      _UpperCAmelCase  (     self     )      -> Tuple:


        '''simple docstring'''


        lowercase_      :							str						=      TFMobileBertModelTest.TFMobileBertModelTester(self     )
        lowercase_      :							List[Any]						=      ConfigTester(self	,config_class=_A	,hidden_size=37     )
   def      _UpperCAmelCase  (     self     )      -> Optional[Any]:


        '''simple docstring'''


        self.config_tester.run_common_tests()
   def      _UpperCAmelCase  (     self     )      -> List[Any]:


        '''simple docstring'''


        lowercase_      :							Optional[int]						=      self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_mobilebert_model(*_A     )
   def      _UpperCAmelCase  (     self     )      -> str:


        '''simple docstring'''


        lowercase_      :							Any						=      self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_mobilebert_for_masked_lm(*_A     )
   def      _UpperCAmelCase  (     self     )      -> Optional[Any]:


        '''simple docstring'''


        lowercase_      :							Optional[int]						=      self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_mobilebert_for_multiple_choice(*_A     )
   def      _UpperCAmelCase  (     self     )      -> int:


        '''simple docstring'''


        lowercase_      :							Dict						=      self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_mobilebert_for_next_sequence_prediction(*_A     )
   def      _UpperCAmelCase  (     self     )      -> Dict:


        '''simple docstring'''


        lowercase_      :							Tuple						=      self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_mobilebert_for_pretraining(*_A     )
   def      _UpperCAmelCase  (     self     )      -> List[str]:


        '''simple docstring'''


        lowercase_      :							Dict						=      self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_mobilebert_for_question_answering(*_A     )
   def      _UpperCAmelCase  (     self     )      -> List[str]:


        '''simple docstring'''


        lowercase_      :							Dict						=      self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_mobilebert_for_sequence_classification(*_A     )
   def      _UpperCAmelCase  (     self     )      -> int:


        '''simple docstring'''


        lowercase_      :							Tuple						=      self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_mobilebert_for_token_classification(*_A     )







   @slow
   def      _UpperCAmelCase  (     self     )      -> str:


        '''simple docstring'''


        for model_name in ["google/mobilebert-uncased"]:
             lowercase_      :							Optional[int]						=      TFMobileBertModel.from_pretrained(_A     )
             self.assertIsNotNone(_A     )







@require_tf
class 			UpperCamelCase						(     unittest.TestCase    ):
   @slow
   def      _UpperCAmelCase  (     self     )      -> Union[str, Any]:


        '''simple docstring'''


        lowercase_      :							Union[str, Any]						=      TFMobileBertForPreTraining.from_pretrained('google/mobilebert-uncased'     )
        lowercase_      :							List[str]						=      tf.constant([[0, 1, 2, 3, 4, 5]]     )
        lowercase_      :							Dict						=      model(_A     )[0]

        lowercase_      :							List[str]						=      [1, 6, 3_0522]
        self.assertEqual(output.shape	,_A     )

        lowercase_      :							List[Any]						=      tf.constant(
            [
                [
                    [-4.591_9547, -9.24_8295, -9.64_5256],
                    [-6.730_6175, -6.44_0284, -6.605_2837],
                    [-7.274_3506, -6.784_7915, -6.02_4673],
                ]
            ]     )
        tf.debugging.assert_near(output[:, :3, :3]	,_A	,atol=1e-4     )









'''simple docstring'''





import unittest

import numpy as np

from transformers.testing_utils import require_torch, require_vision
from transformers.utils import is_torch_available, is_vision_available

from ...test_image_processing_common import ImageProcessingSavingTestMixin, prepare_image_inputs


if is_torch_available():
						import torch

if is_vision_available():
						from PIL import Image

						from transformers import MobileNetVaImageProcessor


class  snake_case__      (					unittest.TestCase):




					def __init__(  self  :		Optional[Any]				, _A  :		int				, _A  :		List[Any]=7				, _A  :		Tuple=3				, _A  :		int=18				, _A  :		Union[str, Any]=30				, _A  :		Any=4_00				, _A  :		List[Any]=True				, _A  :		Optional[int]=None				, _A  :		Optional[Any]=True				, _A  :		Union[str, Any]=None				, )    ->							Optional[int]:
								UpperCAmelCase_      :       Optional[Any]          =       size if size is not None else {'''shortest_edge''': 20}
								UpperCAmelCase_      :       Dict          =       crop_size if crop_size is not None else {'''height''': 18, '''width''': 18}
								UpperCAmelCase_      :       List[Any]          =       parent
								UpperCAmelCase_      :       List[Any]          =       batch_size
								UpperCAmelCase_      :       Optional[Any]          =       num_channels
								UpperCAmelCase_      :       Optional[Any]          =       image_size
								UpperCAmelCase_      :       Union[str, Any]          =       min_resolution
								UpperCAmelCase_      :       List[str]          =       max_resolution
								UpperCAmelCase_      :       Union[str, Any]          =       do_resize
								UpperCAmelCase_      :       Any          =       size
								UpperCAmelCase_      :       Union[str, Any]          =       do_center_crop
								UpperCAmelCase_      :       Any          =       crop_size





					def 		A      (  self  :		List[Any]      )    ->							List[Any]:
								return {
								    "do_resize": self.do_resize,
								    "size": self.size,
								    "do_center_crop": self.do_center_crop,
								    "crop_size": self.crop_size,
								}






@require_torch
@require_vision
class  snake_case__      (					UpperCamelCase     ,				unittest.TestCase):
					a_        =					MobileNetVaImageProcessor if is_vision_available() else None




					def 		A      (  self  :		List[Any]      )    ->							List[str]:
								UpperCAmelCase_      :       Dict          =       MobileNetVaImageProcessingTester(self      )




					@property
					def 		A      (  self  :		Optional[Any]      )    ->							Any:
								return self.image_processor_tester.prepare_image_processor_dict()




					def 		A      (  self  :		List[str]      )    ->							Dict:
								UpperCAmelCase_      :       int          =       self.image_processing_class(**self.image_processor_dict      )
								self.assertTrue(hasattr(_A				, '''do_resize'''      )      )
								self.assertTrue(hasattr(_A				, '''size'''      )      )
								self.assertTrue(hasattr(_A				, '''do_center_crop'''      )      )
								self.assertTrue(hasattr(_A				, '''crop_size'''      )      )




					def 		A      (  self  :		Tuple      )    ->							Optional[int]:
								UpperCAmelCase_      :       Any          =       self.image_processing_class.from_dict(self.image_processor_dict      )
								self.assertEqual(image_processor.size				, {'''shortest_edge''': 20}      )
								self.assertEqual(image_processor.crop_size				, {'''height''': 18, '''width''': 18}      )

								UpperCAmelCase_      :       List[str]          =       self.image_processing_class.from_dict(self.image_processor_dict				, size=42				, crop_size=84      )
								self.assertEqual(image_processor.size				, {'''shortest_edge''': 42}      )
								self.assertEqual(image_processor.crop_size				, {'''height''': 84, '''width''': 84}      )




					def 		A      (  self  :		int      )    ->							List[str]:
								pass




					def 		A      (  self  :		List[Any]      )    ->							Optional[Any]:
								# Initialize image_processing
								UpperCAmelCase_      :       List[Any]          =       self.image_processing_class(**self.image_processor_dict      )
								# create random PIL images
								UpperCAmelCase_      :       Optional[int]          =       prepare_image_inputs(self.image_processor_tester				, equal_resolution=_A      )
								for image in image_inputs:
											self.assertIsInstance(_A				, Image.Image      )

								# Test not batched input
								UpperCAmelCase_      :       Union[str, Any]          =       image_processing(image_inputs[0]				, return_tensors='''pt'''      ).pixel_values
								self.assertEqual(
								    encoded_images.shape				, (
								        1,
								        self.image_processor_tester.num_channels,
								        self.image_processor_tester.crop_size['''height'''],
								        self.image_processor_tester.crop_size['''width'''],
								    )				, )

								# Test batched
								UpperCAmelCase_      :       List[Any]          =       image_processing(_A				, return_tensors='''pt'''      ).pixel_values
								self.assertEqual(
								    encoded_images.shape				, (
								        self.image_processor_tester.batch_size,
								        self.image_processor_tester.num_channels,
								        self.image_processor_tester.crop_size['''height'''],
								        self.image_processor_tester.crop_size['''width'''],
								    )				, )




					def 		A      (  self  :		str      )    ->							List[str]:
								# Initialize image_processing
								UpperCAmelCase_      :       Tuple          =       self.image_processing_class(**self.image_processor_dict      )
								# create random numpy tensors
								UpperCAmelCase_      :       Any          =       prepare_image_inputs(self.image_processor_tester				, equal_resolution=_A				, numpify=_A      )
								for image in image_inputs:
											self.assertIsInstance(_A				, np.ndarray      )

								# Test not batched input
								UpperCAmelCase_      :       str          =       image_processing(image_inputs[0]				, return_tensors='''pt'''      ).pixel_values
								self.assertEqual(
								    encoded_images.shape				, (
								        1,
								        self.image_processor_tester.num_channels,
								        self.image_processor_tester.crop_size['''height'''],
								        self.image_processor_tester.crop_size['''width'''],
								    )				, )

								# Test batched
								UpperCAmelCase_      :       List[Any]          =       image_processing(_A				, return_tensors='''pt'''      ).pixel_values
								self.assertEqual(
								    encoded_images.shape				, (
								        self.image_processor_tester.batch_size,
								        self.image_processor_tester.num_channels,
								        self.image_processor_tester.crop_size['''height'''],
								        self.image_processor_tester.crop_size['''width'''],
								    )				, )





					def 		A      (  self  :		Optional[int]      )    ->							Dict:
								# Initialize image_processing
								UpperCAmelCase_      :       Dict          =       self.image_processing_class(**self.image_processor_dict      )
								# create random PyTorch tensors
								UpperCAmelCase_      :       Union[str, Any]          =       prepare_image_inputs(self.image_processor_tester				, equal_resolution=_A				, torchify=_A      )
								for image in image_inputs:
											self.assertIsInstance(_A				, torch.Tensor      )

								# Test not batched input
								UpperCAmelCase_      :       Any          =       image_processing(image_inputs[0]				, return_tensors='''pt'''      ).pixel_values
								self.assertEqual(
								    encoded_images.shape				, (
								        1,
								        self.image_processor_tester.num_channels,
								        self.image_processor_tester.crop_size['''height'''],
								        self.image_processor_tester.crop_size['''width'''],
								    )				, )

								# Test batched
								UpperCAmelCase_      :       List[str]          =       image_processing(_A				, return_tensors='''pt'''      ).pixel_values
								self.assertEqual(
								    encoded_images.shape				, (
								        self.image_processor_tester.batch_size,
								        self.image_processor_tester.num_channels,
								        self.image_processor_tester.crop_size['''height'''],
								        self.image_processor_tester.crop_size['''width'''],
								    )				, )


