



"""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    SCREAMING_SNAKE_CASE__ (							__UpperCAmelCase      ,  __UpperCAmelCase      ,  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			:				Union[str, Any] ,  lowerCAmelCase_			:				Optional[Any] ,  lowerCAmelCase_			:				Any ,  lowerCAmelCase_			:				List[str]=False):







      """simple docstring"""







      lowercase_								= super()._prepare_for_class(lowerCAmelCase_ ,  lowerCAmelCase_ ,  return_labels=lowerCAmelCase_)

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

      return inputs_dict


    class    SCREAMING_SNAKE_CASE__ (							__UpperCAmelCase   ):

        def __init__(				self			:				str ,  lowerCAmelCase_			:				int ,  lowerCAmelCase_			:				Union[str, Any]=1_3 ,  lowerCAmelCase_			:				Dict=7 ,  lowerCAmelCase_			:				str=True ,  lowerCAmelCase_			:				Dict=True ,  lowerCAmelCase_			:				str=True ,  lowerCAmelCase_			:				str=True ,  lowerCAmelCase_			:				Optional[Any]=9_9 ,  lowerCAmelCase_			:				str=3_2 ,  lowerCAmelCase_			:				Union[str, Any]=3_2 ,  lowerCAmelCase_			:				Union[str, Any]=2 ,  lowerCAmelCase_			:				Optional[Any]=4 ,  lowerCAmelCase_			:				int=3_7 ,  lowerCAmelCase_			:				List[Any]="gelu" ,  lowerCAmelCase_			:				Union[str, Any]=0.1 ,  lowerCAmelCase_			:				str=0.1 ,  lowerCAmelCase_			:				Tuple=5_1_2 ,  lowerCAmelCase_			:				str=1_6 ,  lowerCAmelCase_			:				Optional[Any]=2 ,  lowerCAmelCase_			:				Union[str, Any]=0.02 ,  lowerCAmelCase_			:				Dict=3 ,  lowerCAmelCase_			:				Tuple=4 ,  lowerCAmelCase_			:				List[str]=None ,  ):







          """simple docstring"""







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

        def 					_UpperCAmelCase	(				self			:				Dict):







          """simple docstring"""







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

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

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

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

          lowercase_								= 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			:				str ,  lowerCAmelCase_			:				Dict ,  lowerCAmelCase_			:				List[Any] ,  lowerCAmelCase_			:				List[str] ,  lowerCAmelCase_			:				Tuple ,  lowerCAmelCase_			:				Dict ,  lowerCAmelCase_			:				str ,  lowerCAmelCase_			:				Union[str, Any]):







          """simple docstring"""







          lowercase_								= TFMobileBertModel(config=lowerCAmelCase_)
          lowercase_								= {"""input_ids""": input_ids, """attention_mask""": input_mask, """token_type_ids""": token_type_ids}
          lowercase_								= model(lowerCAmelCase_)

          lowercase_								= [input_ids, input_mask]
          lowercase_								= model(lowerCAmelCase_)

          lowercase_								= 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			:				List[str] ,  lowerCAmelCase_			:				int ,  lowerCAmelCase_			:				Tuple ,  lowerCAmelCase_			:				Dict ,  lowerCAmelCase_			:				int ,  lowerCAmelCase_			:				str ,  lowerCAmelCase_			:				str ,  lowerCAmelCase_			:				Optional[int]):







          """simple docstring"""







          lowercase_								= TFMobileBertForMaskedLM(config=lowerCAmelCase_)
          lowercase_								= {"""input_ids""": input_ids, """attention_mask""": input_mask, """token_type_ids""": token_type_ids}
          lowercase_								= model(lowerCAmelCase_)
          self.parent.assertEqual(result.logits.shape ,  (self.batch_size, self.seq_length, self.vocab_size))

        def 					_UpperCAmelCase	(				self			:				List[Any] ,  lowerCAmelCase_			:				Dict ,  lowerCAmelCase_			:				List[Any] ,  lowerCAmelCase_			:				int ,  lowerCAmelCase_			:				Any ,  lowerCAmelCase_			:				str ,  lowerCAmelCase_			:				Tuple ,  lowerCAmelCase_			:				Union[str, Any]):







          """simple docstring"""







          lowercase_								= TFMobileBertForNextSentencePrediction(config=lowerCAmelCase_)
          lowercase_								= {"""input_ids""": input_ids, """attention_mask""": input_mask, """token_type_ids""": token_type_ids}
          lowercase_								= model(lowerCAmelCase_)
          self.parent.assertEqual(result.logits.shape ,  (self.batch_size, 2))

        def 					_UpperCAmelCase	(				self			:				str ,  lowerCAmelCase_			:				Optional[int] ,  lowerCAmelCase_			:				Union[str, Any] ,  lowerCAmelCase_			:				str ,  lowerCAmelCase_			:				Optional[int] ,  lowerCAmelCase_			:				str ,  lowerCAmelCase_			:				Any ,  lowerCAmelCase_			:				Union[str, Any]):







          """simple docstring"""







          lowercase_								= TFMobileBertForPreTraining(config=lowerCAmelCase_)
          lowercase_								= {"""input_ids""": input_ids, """attention_mask""": input_mask, """token_type_ids""": token_type_ids}
          lowercase_								= 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			:				Dict ,  lowerCAmelCase_			:				List[str] ,  lowerCAmelCase_			:				Union[str, Any] ,  lowerCAmelCase_			:				Optional[int] ,  lowerCAmelCase_			:				Dict ,  lowerCAmelCase_			:				str ,  lowerCAmelCase_			:				Optional[Any] ,  lowerCAmelCase_			:				List[Any]):







          """simple docstring"""







          lowercase_								= self.num_labels
          lowercase_								= TFMobileBertForSequenceClassification(config=lowerCAmelCase_)
          lowercase_								= {"""input_ids""": input_ids, """attention_mask""": input_mask, """token_type_ids""": token_type_ids}
          lowercase_								= model(lowerCAmelCase_)
          self.parent.assertEqual(result.logits.shape ,  (self.batch_size, self.num_labels))

        def 					_UpperCAmelCase	(				self			:				Optional[Any] ,  lowerCAmelCase_			:				Optional[int] ,  lowerCAmelCase_			:				List[str] ,  lowerCAmelCase_			:				Optional[int] ,  lowerCAmelCase_			:				List[Any] ,  lowerCAmelCase_			:				Tuple ,  lowerCAmelCase_			:				Any ,  lowerCAmelCase_			:				Tuple):







          """simple docstring"""







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

        def 					_UpperCAmelCase	(				self			:				Dict ,  lowerCAmelCase_			:				int ,  lowerCAmelCase_			:				str ,  lowerCAmelCase_			:				str ,  lowerCAmelCase_			:				List[Any] ,  lowerCAmelCase_			:				Any ,  lowerCAmelCase_			:				Optional[Any] ,  lowerCAmelCase_			:				Dict):







          """simple docstring"""







          lowercase_								= self.num_labels
          lowercase_								= TFMobileBertForTokenClassification(config=lowerCAmelCase_)
          lowercase_								= {"""input_ids""": input_ids, """attention_mask""": input_mask, """token_type_ids""": token_type_ids}
          lowercase_								= model(lowerCAmelCase_)
          self.parent.assertEqual(result.logits.shape ,  (self.batch_size, self.seq_length, self.num_labels))

        def 					_UpperCAmelCase	(				self			:				Any ,  lowerCAmelCase_			:				Dict ,  lowerCAmelCase_			:				List[str] ,  lowerCAmelCase_			:				Optional[Any] ,  lowerCAmelCase_			:				List[str] ,  lowerCAmelCase_			:				List[str] ,  lowerCAmelCase_			:				Union[str, Any] ,  lowerCAmelCase_			:				int):







          """simple docstring"""







          lowercase_								= TFMobileBertForQuestionAnswering(config=lowerCAmelCase_)
          lowercase_								= {"""input_ids""": input_ids, """attention_mask""": input_mask, """token_type_ids""": token_type_ids}
          lowercase_								= 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			:				Any):







          """simple docstring"""







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







    def 					_UpperCAmelCase	(				self			:				Union[str, Any]):







      """simple docstring"""







      lowercase_								= TFMobileBertModelTest.TFMobileBertModelTester(self)
      lowercase_								= ConfigTester(self ,  config_class=lowerCAmelCase_ ,  hidden_size=3_7)

    def 					_UpperCAmelCase	(				self			:				Tuple):







      """simple docstring"""







      self.config_tester.run_common_tests()

    def 					_UpperCAmelCase	(				self			:				Dict):







      """simple docstring"""







      lowercase_								= self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_mobilebert_model(*lowerCAmelCase_)

    def 					_UpperCAmelCase	(				self			:				Optional[int]):







      """simple docstring"""







      lowercase_								= self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_mobilebert_for_masked_lm(*lowerCAmelCase_)

    def 					_UpperCAmelCase	(				self			:				Any):







      """simple docstring"""







      lowercase_								= self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_mobilebert_for_multiple_choice(*lowerCAmelCase_)

    def 					_UpperCAmelCase	(				self			:				Dict):







      """simple docstring"""







      lowercase_								= self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_mobilebert_for_next_sequence_prediction(*lowerCAmelCase_)

    def 					_UpperCAmelCase	(				self			:				Dict):







      """simple docstring"""







      lowercase_								= self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_mobilebert_for_pretraining(*lowerCAmelCase_)

    def 					_UpperCAmelCase	(				self			:				str):







      """simple docstring"""







      lowercase_								= self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_mobilebert_for_question_answering(*lowerCAmelCase_)

    def 					_UpperCAmelCase	(				self			:				Optional[Any]):







      """simple docstring"""







      lowercase_								= self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_mobilebert_for_sequence_classification(*lowerCAmelCase_)

    def 					_UpperCAmelCase	(				self			:				int):







      """simple docstring"""







      lowercase_								= self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_mobilebert_for_token_classification(*lowerCAmelCase_)




    @slow
    def 					_UpperCAmelCase	(				self			:				Dict):







      """simple docstring"""







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







@require_tf
class    SCREAMING_SNAKE_CASE__ (							unittest.TestCase   ):

    @slow
    def 					_UpperCAmelCase	(				self			:				Optional[int]):







      """simple docstring"""







      lowercase_								= TFMobileBertForPreTraining.from_pretrained("""google/mobilebert-uncased""")
      lowercase_								= tf.constant([[0, 1, 2, 3, 4, 5]])
      lowercase_								= model(lowerCAmelCase_)[0]

      lowercase_								= [1, 6, 3_0_5_2_2]
      self.assertEqual(output.shape ,  lowerCAmelCase_)

      lowercase_								= tf.constant(
          [
              [
                  [-4.5_919_547, -9.248_295, -9.645_256],
                  [-6.7_306_175, -6.440_284, -6.6_052_837],
                  [-7.2_743_506, -6.7_847_915, -6.024_673],
              ]
          ])
      tf.debugging.assert_near(output[:, :3, :3] ,  lowerCAmelCase_ ,  atol=1E-4)






"""simple docstring"""
from typing import TYPE_CHECKING

from ...utils import _LazyModule


UpperCAmelCase						:		List[Any]			= {"tokenization_bertweet": ["BertweetTokenizer"]}


if TYPE_CHECKING:
       from .tokenization_bertweet import BertweetTokenizer

else:
       import sys

       UpperCAmelCase						:		Tuple			= _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)

