
"""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 __lowerCAmelCase	(   _lowerCAmelCase    ,						_lowerCAmelCase    ,						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   ):

      '''simple docstring'''




      __UpperCamelCase       						= super()._prepare_for_class(_lowerCAmelCase			,  _lowerCAmelCase			,  return_labels=_lowerCAmelCase   )

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

      return inputs_dict

     class __lowerCAmelCase	(   _lowerCAmelCase    ):





          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.0_2			,  __UpperCAmelCase=3			,  __UpperCAmelCase=4			,  __UpperCAmelCase=None			,  ):

           '''simple docstring'''




           __UpperCamelCase       						= parent
           __UpperCamelCase       						= batch_size
           __UpperCamelCase       						= seq_length
           __UpperCamelCase       						= is_training
           __UpperCamelCase       						= use_input_mask
           __UpperCamelCase       						= use_token_type_ids
           __UpperCamelCase       						= use_labels
           __UpperCamelCase       						= vocab_size
           __UpperCamelCase       						= hidden_size
           __UpperCamelCase       						= num_hidden_layers
           __UpperCamelCase       						= num_attention_heads
           __UpperCamelCase       						= intermediate_size
           __UpperCamelCase       						= hidden_act
           __UpperCamelCase       						= hidden_dropout_prob
           __UpperCamelCase       						= attention_probs_dropout_prob
           __UpperCamelCase       						= max_position_embeddings
           __UpperCamelCase       						= type_vocab_size
           __UpperCamelCase       						= type_sequence_label_size
           __UpperCamelCase       						= initializer_range
           __UpperCamelCase       						= num_labels
           __UpperCamelCase       						= num_choices
           __UpperCamelCase       						= scope
           __UpperCamelCase       						= embedding_size





          def 		UpperCAmelCase      (			self   ):

           '''simple docstring'''




           __UpperCamelCase       						= ids_tensor([self.batch_size, self.seq_length]			,  self.vocab_size   )

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

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

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

           __UpperCamelCase       						= 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   ):

           '''simple docstring'''




           __UpperCamelCase       						= TFMobileBertModel(config=_lowerCAmelCase   )
           __UpperCamelCase       						= {'input_ids': input_ids, 'attention_mask': input_mask, 'token_type_ids': token_type_ids}
           __UpperCamelCase       						= model(_lowerCAmelCase   )

           __UpperCamelCase       						= [input_ids, input_mask]
           __UpperCamelCase       						= model(_lowerCAmelCase   )

           __UpperCamelCase       						= 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 		UpperCAmelCase      (			self			,  __UpperCAmelCase			,  __UpperCAmelCase			,  __UpperCAmelCase			,  __UpperCAmelCase			,  __UpperCAmelCase			,  __UpperCAmelCase			,  __UpperCAmelCase   ):

           '''simple docstring'''




           __UpperCamelCase       						= TFMobileBertForMaskedLM(config=_lowerCAmelCase   )
           __UpperCamelCase       						= {'input_ids': input_ids, 'attention_mask': input_mask, 'token_type_ids': token_type_ids}
           __UpperCamelCase       						= model(_lowerCAmelCase   )
           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   ):

           '''simple docstring'''




           __UpperCamelCase       						= TFMobileBertForNextSentencePrediction(config=_lowerCAmelCase   )
           __UpperCamelCase       						= {'input_ids': input_ids, 'attention_mask': input_mask, 'token_type_ids': token_type_ids}
           __UpperCamelCase       						= model(_lowerCAmelCase   )
           self.parent.assertEqual(result.logits.shape			,  (self.batch_size, 2)   )





          def 		UpperCAmelCase      (			self			,  __UpperCAmelCase			,  __UpperCAmelCase			,  __UpperCAmelCase			,  __UpperCAmelCase			,  __UpperCAmelCase			,  __UpperCAmelCase			,  __UpperCAmelCase   ):

           '''simple docstring'''




           __UpperCamelCase       						= TFMobileBertForPreTraining(config=_lowerCAmelCase   )
           __UpperCamelCase       						= {'input_ids': input_ids, 'attention_mask': input_mask, 'token_type_ids': token_type_ids}
           __UpperCamelCase       						= model(_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 		UpperCAmelCase      (			self			,  __UpperCAmelCase			,  __UpperCAmelCase			,  __UpperCAmelCase			,  __UpperCAmelCase			,  __UpperCAmelCase			,  __UpperCAmelCase			,  __UpperCAmelCase   ):

           '''simple docstring'''




           __UpperCamelCase       						= self.num_labels
           __UpperCamelCase       						= TFMobileBertForSequenceClassification(config=_lowerCAmelCase   )
           __UpperCamelCase       						= {'input_ids': input_ids, 'attention_mask': input_mask, 'token_type_ids': token_type_ids}
           __UpperCamelCase       						= model(_lowerCAmelCase   )
           self.parent.assertEqual(result.logits.shape			,  (self.batch_size, self.num_labels)   )





          def 		UpperCAmelCase      (			self			,  __UpperCAmelCase			,  __UpperCAmelCase			,  __UpperCAmelCase			,  __UpperCAmelCase			,  __UpperCAmelCase			,  __UpperCAmelCase			,  __UpperCAmelCase   ):

           '''simple docstring'''




           __UpperCamelCase       						= self.num_choices
           __UpperCamelCase       						= TFMobileBertForMultipleChoice(config=_lowerCAmelCase   )
           __UpperCamelCase       						= tf.tile(tf.expand_dims(_lowerCAmelCase			,  1   )			,  (1, self.num_choices, 1)   )
           __UpperCamelCase       						= tf.tile(tf.expand_dims(_lowerCAmelCase			,  1   )			,  (1, self.num_choices, 1)   )
           __UpperCamelCase       						= tf.tile(tf.expand_dims(_lowerCAmelCase			,  1   )			,  (1, self.num_choices, 1)   )
           __UpperCamelCase       						= {
               'input_ids': multiple_choice_inputs_ids,
               'attention_mask': multiple_choice_input_mask,
               'token_type_ids': multiple_choice_token_type_ids,
           }
           __UpperCamelCase       						= model(_lowerCAmelCase   )
           self.parent.assertEqual(result.logits.shape			,  (self.batch_size, self.num_choices)   )





          def 		UpperCAmelCase      (			self			,  __UpperCAmelCase			,  __UpperCAmelCase			,  __UpperCAmelCase			,  __UpperCAmelCase			,  __UpperCAmelCase			,  __UpperCAmelCase			,  __UpperCAmelCase   ):

           '''simple docstring'''




           __UpperCamelCase       						= self.num_labels
           __UpperCamelCase       						= TFMobileBertForTokenClassification(config=_lowerCAmelCase   )
           __UpperCamelCase       						= {'input_ids': input_ids, 'attention_mask': input_mask, 'token_type_ids': token_type_ids}
           __UpperCamelCase       						= model(_lowerCAmelCase   )
           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   ):

           '''simple docstring'''




           __UpperCamelCase       						= TFMobileBertForQuestionAnswering(config=_lowerCAmelCase   )
           __UpperCamelCase       						= {'input_ids': input_ids, 'attention_mask': input_mask, 'token_type_ids': token_type_ids}
           __UpperCamelCase       						= model(_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 		UpperCAmelCase      (			self   ):

           '''simple docstring'''




           __UpperCamelCase       						= self.prepare_config_and_inputs()
           (
               (
               __UpperCamelCase						
           )						,			(
               __UpperCamelCase						
           )						,			(
               __UpperCamelCase						
           )						,			(
               __UpperCamelCase						
           )						,			(
               __UpperCamelCase						
           )						,			(
               __UpperCamelCase						
           )						,			(
               __UpperCamelCase						
           )						,									
           )       						= config_and_inputs
           __UpperCamelCase       						= {'input_ids': input_ids, 'token_type_ids': token_type_ids, 'attention_mask': input_mask}
           return config, inputs_dict





     def 		UpperCAmelCase      (			self   ):

      '''simple docstring'''




      __UpperCamelCase       						= TFMobileBertModelTest.TFMobileBertModelTester(self   )
      __UpperCamelCase       						= ConfigTester(self			,  config_class=_lowerCAmelCase			,  hidden_size=37   )





     def 		UpperCAmelCase      (			self   ):

      '''simple docstring'''




      self.config_tester.run_common_tests()





     def 		UpperCAmelCase      (			self   ):

      '''simple docstring'''




      __UpperCamelCase       						= self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_mobilebert_model(*_lowerCAmelCase   )





     def 		UpperCAmelCase      (			self   ):

      '''simple docstring'''




      __UpperCamelCase       						= self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_mobilebert_for_masked_lm(*_lowerCAmelCase   )





     def 		UpperCAmelCase      (			self   ):

      '''simple docstring'''




      __UpperCamelCase       						= self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_mobilebert_for_multiple_choice(*_lowerCAmelCase   )





     def 		UpperCAmelCase      (			self   ):

      '''simple docstring'''




      __UpperCamelCase       						= self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_mobilebert_for_next_sequence_prediction(*_lowerCAmelCase   )





     def 		UpperCAmelCase      (			self   ):

      '''simple docstring'''




      __UpperCamelCase       						= self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_mobilebert_for_pretraining(*_lowerCAmelCase   )





     def 		UpperCAmelCase      (			self   ):

      '''simple docstring'''




      __UpperCamelCase       						= self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_mobilebert_for_question_answering(*_lowerCAmelCase   )





     def 		UpperCAmelCase      (			self   ):

      '''simple docstring'''




      __UpperCamelCase       						= self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_mobilebert_for_sequence_classification(*_lowerCAmelCase   )





     def 		UpperCAmelCase      (			self   ):

      '''simple docstring'''




      __UpperCamelCase       						= self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_mobilebert_for_token_classification(*_lowerCAmelCase   )







     @slow
     def 		UpperCAmelCase      (			self   ):

      '''simple docstring'''




      for model_name in ["google/mobilebert-uncased"]:
       __UpperCamelCase       						= TFMobileBertModel.from_pretrained(_lowerCAmelCase   )
       self.assertIsNotNone(_lowerCAmelCase   )


@require_tf
class __lowerCAmelCase	(   unittest.TestCase    ):





     @slow
     def 		UpperCAmelCase      (			self   ):

      '''simple docstring'''




      __UpperCamelCase       						= TFMobileBertForPreTraining.from_pretrained('google/mobilebert-uncased'   )
      __UpperCamelCase       						= tf.constant([[0, 1, 2, 3, 4, 5]]   )
      __UpperCamelCase       						= model(_lowerCAmelCase   )[0]

      __UpperCamelCase       						= [1, 6, 3_0522]
      self.assertEqual(output.shape			,  _lowerCAmelCase   )

      __UpperCamelCase       						= tf.constant(
          [
              [
                  [-4.5_9_1_9_5_4_7, -9.2_4_8_2_9_5, -9.6_4_5_2_5_6],
                  [-6.7_3_0_6_1_7_5, -6.4_4_0_2_8_4, -6.6_0_5_2_8_3_7],
                  [-7.2_7_4_3_5_0_6, -6.7_8_4_7_9_1_5, -6.0_2_4_6_7_3],
              ]
          ]   )
      tf.debugging.assert_near(output[:, :3, :3]			,  _lowerCAmelCase			,  atol=1E-4   )





"""simple docstring"""

from typing import List

from ...configuration_utils import PretrainedConfig
from ...utils import logging


UpperCamelCase			:       Tuple									=				logging.get_logger(__name__)

UpperCamelCase			:       List[str]									=				{
    "snap-research/efficientformer-l1-300": (
        "https://huggingface.co/snap-research/efficientformer-l1-300/resolve/main/config.json"
    ),
}

class __lowerCAmelCase	(   __SCREAMING_SNAKE_CASE    ):
     lowercase		     =		"efficientformer"





     def __init__(			self			,  __UpperCAmelCase = [3, 2, 6, 4]			,  __UpperCAmelCase = [48, 96, 224, 448]			,  __UpperCAmelCase = [True, True, True, True]			,  __UpperCAmelCase = 448			,  __UpperCAmelCase = 32			,  __UpperCAmelCase = 4			,  __UpperCAmelCase = 7			,  __UpperCAmelCase = 5			,  __UpperCAmelCase = 8			,  __UpperCAmelCase = 4			,  __UpperCAmelCase = 0.0			,  __UpperCAmelCase = 16			,  __UpperCAmelCase = 3			,  __UpperCAmelCase = 3			,  __UpperCAmelCase = 3			,  __UpperCAmelCase = 2			,  __UpperCAmelCase = 1			,  __UpperCAmelCase = 0.0			,  __UpperCAmelCase = 1			,  __UpperCAmelCase = True			,  __UpperCAmelCase = True			,  __UpperCAmelCase = 1E-5			,  __UpperCAmelCase = "gelu"			,  __UpperCAmelCase = 0.0_2			,  __UpperCAmelCase = 1E-12			,  __UpperCAmelCase = 224			,  __UpperCAmelCase = 1E-05			,  **__UpperCAmelCase			,  ):

      '''simple docstring'''




      super().__init__(**__UpperCAmelCase   )

      __UpperCamelCase       						= hidden_act
      __UpperCamelCase       						= hidden_dropout_prob
      __UpperCamelCase       						= hidden_sizes
      __UpperCamelCase       						= num_hidden_layers
      __UpperCamelCase       						= num_attention_heads
      __UpperCamelCase       						= initializer_range
      __UpperCamelCase       						= layer_norm_eps
      __UpperCamelCase       						= patch_size
      __UpperCamelCase       						= num_channels
      __UpperCamelCase       						= depths
      __UpperCamelCase       						= mlp_expansion_ratio
      __UpperCamelCase       						= downsamples
      __UpperCamelCase       						= dim
      __UpperCamelCase       						= key_dim
      __UpperCamelCase       						= attention_ratio
      __UpperCamelCase       						= resolution
      __UpperCamelCase       						= pool_size
      __UpperCamelCase       						= downsample_patch_size
      __UpperCamelCase       						= downsample_stride
      __UpperCamelCase       						= downsample_pad
      __UpperCamelCase       						= drop_path_rate
      __UpperCamelCase       						= num_metaad_blocks
      __UpperCamelCase       						= distillation
      __UpperCamelCase       						= use_layer_scale
      __UpperCamelCase       						= layer_scale_init_value
      __UpperCamelCase       						= image_size
      __UpperCamelCase       						= batch_norm_eps



