





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							(					__UpperCamelCase,  __UpperCamelCase,  unittest.TestCase			):
 UpperCAmelCase__													=		(
     (
         TFMobileBertModel,
         TFMobileBertForMaskedLM,
         TFMobileBertForNextSentencePrediction,
         TFMobileBertForPreTraining,
         TFMobileBertForQuestionAnswering,
         TFMobileBertForSequenceClassification,
         TFMobileBertForTokenClassification,
         TFMobileBertForMultipleChoice,
     )
     if is_tf_available()
     else ()
 )
 UpperCAmelCase__													=		(
     {
         """feature-extraction""": TFMobileBertModel,
         """fill-mask""": TFMobileBertForMaskedLM,
         """question-answering""": TFMobileBertForQuestionAnswering,
         """text-classification""": TFMobileBertForSequenceClassification,
         """token-classification""": TFMobileBertForTokenClassification,
         """zero-shot""": TFMobileBertForSequenceClassification,
     }
     if is_tf_available()
     else {}
 )
 UpperCAmelCase__													=		False
 UpperCAmelCase__													=		False
 def      A_	(      self  :      str			,     UpperCAmelCase  :      Optional[int]			,     UpperCAmelCase  :      Optional[int]			,     UpperCAmelCase  :      str=False	)			->	int:
     lowerCamelCase__    :      Dict					 =		super()._prepare_for_class(UpperCAmelCase			,     UpperCAmelCase			,     return_labels=UpperCAmelCase	)

     if return_labels:
         if model_class in get_values(UpperCAmelCase	):
             lowerCamelCase__    :      Any					 =		tf.zeros(self.model_tester.batch_size			,     dtype=tf.intaa	)

     return inputs_dict

 class    lowerCAmelCase							(					__UpperCamelCase			):
  def __init__(      self  :      Optional[int]			,     UpperCAmelCase  :      Union[str, Any]			,     UpperCAmelCase  :      Optional[Any]=13			,     UpperCAmelCase  :      List[str]=7			,     UpperCAmelCase  :      Optional[Any]=True			,     UpperCAmelCase  :      Optional[Any]=True			,     UpperCAmelCase  :      str=True			,     UpperCAmelCase  :      Union[str, Any]=True			,     UpperCAmelCase  :      Union[str, Any]=99			,     UpperCAmelCase  :      Tuple=32			,     UpperCAmelCase  :      Union[str, Any]=32			,     UpperCAmelCase  :      Union[str, Any]=2			,     UpperCAmelCase  :      Dict=4			,     UpperCAmelCase  :      Tuple=37			,     UpperCAmelCase  :      Optional[int]="gelu"			,     UpperCAmelCase  :      int=0.1			,     UpperCAmelCase  :      Any=0.1			,     UpperCAmelCase  :      Optional[int]=512			,     UpperCAmelCase  :      str=16			,     UpperCAmelCase  :      int=2			,     UpperCAmelCase  :      Dict=0.0_2			,     UpperCAmelCase  :      Union[str, Any]=3			,     UpperCAmelCase  :      str=4			,     UpperCAmelCase  :      List[str]=None			,     )			->	Tuple:
      lowerCamelCase__    :      List[str]					 =		parent
      lowerCamelCase__    :      Any					 =		batch_size
      lowerCamelCase__    :      List[str]					 =		seq_length
      lowerCamelCase__    :      List[str]					 =		is_training
      lowerCamelCase__    :      Optional[Any]					 =		use_input_mask
      lowerCamelCase__    :      List[Any]					 =		use_token_type_ids
      lowerCamelCase__    :      Union[str, Any]					 =		use_labels
      lowerCamelCase__    :      Union[str, Any]					 =		vocab_size
      lowerCamelCase__    :      List[Any]					 =		hidden_size
      lowerCamelCase__    :      int					 =		num_hidden_layers
      lowerCamelCase__    :      Union[str, Any]					 =		num_attention_heads
      lowerCamelCase__    :      List[Any]					 =		intermediate_size
      lowerCamelCase__    :      Tuple					 =		hidden_act
      lowerCamelCase__    :      int					 =		hidden_dropout_prob
      lowerCamelCase__    :      Optional[Any]					 =		attention_probs_dropout_prob
      lowerCamelCase__    :      Tuple					 =		max_position_embeddings
      lowerCamelCase__    :      Optional[int]					 =		type_vocab_size
      lowerCamelCase__    :      Any					 =		type_sequence_label_size
      lowerCamelCase__    :      str					 =		initializer_range
      lowerCamelCase__    :      Dict					 =		num_labels
      lowerCamelCase__    :      List[str]					 =		num_choices
      lowerCamelCase__    :      Optional[Any]					 =		scope
      lowerCamelCase__    :      Dict					 =		embedding_size
  def      A_	(      self  :      Union[str, Any]	)			->	Tuple:
      lowerCamelCase__    :      Union[str, Any]					 =		ids_tensor([self.batch_size, self.seq_length]			,     self.vocab_size	)

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

      lowerCamelCase__    :      List[str]					 =		None
      if self.use_token_type_ids:
          lowerCamelCase__    :      Any					 =		ids_tensor([self.batch_size, self.seq_length]			,     self.type_vocab_size	)

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

      lowerCamelCase__    :      Optional[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      A_	(      self  :      str			,     UpperCAmelCase  :      Tuple			,     UpperCAmelCase  :      List[str]			,     UpperCAmelCase  :      List[str]			,     UpperCAmelCase  :      Dict			,     UpperCAmelCase  :      List[str]			,     UpperCAmelCase  :      Dict			,     UpperCAmelCase  :      Tuple	)			->	Optional[int]:
      lowerCamelCase__    :      Union[str, Any]					 =		TFMobileBertModel(config=UpperCAmelCase	)
      lowerCamelCase__    :      Tuple					 =		{'input_ids': input_ids, 'attention_mask': input_mask, 'token_type_ids': token_type_ids}
      lowerCamelCase__    :      Dict					 =		model(UpperCAmelCase	)

      lowerCamelCase__    :      List[Any]					 =		[input_ids, input_mask]
      lowerCamelCase__    :      Optional[Any]					 =		model(UpperCAmelCase	)

      lowerCamelCase__    :      Optional[Any]					 =		model(UpperCAmelCase	)

      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      A_	(      self  :      Tuple			,     UpperCAmelCase  :      Dict			,     UpperCAmelCase  :      Optional[int]			,     UpperCAmelCase  :      Optional[Any]			,     UpperCAmelCase  :      Tuple			,     UpperCAmelCase  :      str			,     UpperCAmelCase  :      Tuple			,     UpperCAmelCase  :      Optional[Any]	)			->	Any:
      lowerCamelCase__    :      List[Any]					 =		TFMobileBertForMaskedLM(config=UpperCAmelCase	)
      lowerCamelCase__    :      str					 =		{'input_ids': input_ids, 'attention_mask': input_mask, 'token_type_ids': token_type_ids}
      lowerCamelCase__    :      Tuple					 =		model(UpperCAmelCase	)
      self.parent.assertEqual(result.logits.shape			,     (self.batch_size, self.seq_length, self.vocab_size)	)
  def      A_	(      self  :      List[Any]			,     UpperCAmelCase  :      Optional[int]			,     UpperCAmelCase  :      Any			,     UpperCAmelCase  :      List[str]			,     UpperCAmelCase  :      Tuple			,     UpperCAmelCase  :      Any			,     UpperCAmelCase  :      Optional[Any]			,     UpperCAmelCase  :      Optional[Any]	)			->	str:
      lowerCamelCase__    :      Union[str, Any]					 =		TFMobileBertForNextSentencePrediction(config=UpperCAmelCase	)
      lowerCamelCase__    :      Optional[int]					 =		{'input_ids': input_ids, 'attention_mask': input_mask, 'token_type_ids': token_type_ids}
      lowerCamelCase__    :      str					 =		model(UpperCAmelCase	)
      self.parent.assertEqual(result.logits.shape			,     (self.batch_size, 2)	)
  def      A_	(      self  :      str			,     UpperCAmelCase  :      Optional[int]			,     UpperCAmelCase  :      List[str]			,     UpperCAmelCase  :      List[str]			,     UpperCAmelCase  :      Union[str, Any]			,     UpperCAmelCase  :      Any			,     UpperCAmelCase  :      Union[str, Any]			,     UpperCAmelCase  :      Union[str, Any]	)			->	List[Any]:
      lowerCamelCase__    :      Union[str, Any]					 =		TFMobileBertForPreTraining(config=UpperCAmelCase	)
      lowerCamelCase__    :      Optional[Any]					 =		{'input_ids': input_ids, 'attention_mask': input_mask, 'token_type_ids': token_type_ids}
      lowerCamelCase__    :      Dict					 =		model(UpperCAmelCase	)
      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      A_	(      self  :      Tuple			,     UpperCAmelCase  :      Union[str, Any]			,     UpperCAmelCase  :      List[Any]			,     UpperCAmelCase  :      Tuple			,     UpperCAmelCase  :      int			,     UpperCAmelCase  :      Union[str, Any]			,     UpperCAmelCase  :      int			,     UpperCAmelCase  :      Optional[Any]	)			->	Optional[Any]:
      lowerCamelCase__    :      List[Any]					 =		self.num_labels
      lowerCamelCase__    :      Tuple					 =		TFMobileBertForSequenceClassification(config=UpperCAmelCase	)
      lowerCamelCase__    :      Union[str, Any]					 =		{'input_ids': input_ids, 'attention_mask': input_mask, 'token_type_ids': token_type_ids}
      lowerCamelCase__    :      Optional[Any]					 =		model(UpperCAmelCase	)
      self.parent.assertEqual(result.logits.shape			,     (self.batch_size, self.num_labels)	)
  def      A_	(      self  :      Dict			,     UpperCAmelCase  :      Any			,     UpperCAmelCase  :      List[str]			,     UpperCAmelCase  :      Optional[int]			,     UpperCAmelCase  :      Dict			,     UpperCAmelCase  :      Tuple			,     UpperCAmelCase  :      Dict			,     UpperCAmelCase  :      List[Any]	)			->	List[Any]:
      lowerCamelCase__    :      Union[str, Any]					 =		self.num_choices
      lowerCamelCase__    :      List[Any]					 =		TFMobileBertForMultipleChoice(config=UpperCAmelCase	)
      lowerCamelCase__    :      Optional[Any]					 =		tf.tile(tf.expand_dims(UpperCAmelCase			,     1	)			,     (1, self.num_choices, 1)	)
      lowerCamelCase__    :      Optional[Any]					 =		tf.tile(tf.expand_dims(UpperCAmelCase			,     1	)			,     (1, self.num_choices, 1)	)
      lowerCamelCase__    :      Tuple					 =		tf.tile(tf.expand_dims(UpperCAmelCase			,     1	)			,     (1, self.num_choices, 1)	)
      lowerCamelCase__    :      int					 =		{
          'input_ids': multiple_choice_inputs_ids,
          'attention_mask': multiple_choice_input_mask,
          'token_type_ids': multiple_choice_token_type_ids,
      }
      lowerCamelCase__    :      Optional[int]					 =		model(UpperCAmelCase	)
      self.parent.assertEqual(result.logits.shape			,     (self.batch_size, self.num_choices)	)
  def      A_	(      self  :      Dict			,     UpperCAmelCase  :      Optional[int]			,     UpperCAmelCase  :      str			,     UpperCAmelCase  :      str			,     UpperCAmelCase  :      int			,     UpperCAmelCase  :      Union[str, Any]			,     UpperCAmelCase  :      Dict			,     UpperCAmelCase  :      int	)			->	int:
      lowerCamelCase__    :      Optional[Any]					 =		self.num_labels
      lowerCamelCase__    :      List[str]					 =		TFMobileBertForTokenClassification(config=UpperCAmelCase	)
      lowerCamelCase__    :      List[Any]					 =		{'input_ids': input_ids, 'attention_mask': input_mask, 'token_type_ids': token_type_ids}
      lowerCamelCase__    :      Optional[Any]					 =		model(UpperCAmelCase	)
      self.parent.assertEqual(result.logits.shape			,     (self.batch_size, self.seq_length, self.num_labels)	)
  def      A_	(      self  :      List[str]			,     UpperCAmelCase  :      Any			,     UpperCAmelCase  :      Optional[Any]			,     UpperCAmelCase  :      Tuple			,     UpperCAmelCase  :      List[Any]			,     UpperCAmelCase  :      int			,     UpperCAmelCase  :      Any			,     UpperCAmelCase  :      Optional[int]	)			->	Dict:
      lowerCamelCase__    :      Optional[Any]					 =		TFMobileBertForQuestionAnswering(config=UpperCAmelCase	)
      lowerCamelCase__    :      List[str]					 =		{'input_ids': input_ids, 'attention_mask': input_mask, 'token_type_ids': token_type_ids}
      lowerCamelCase__    :      List[Any]					 =		model(UpperCAmelCase	)
      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      A_	(      self  :      Optional[int]	)			->	List[str]:
      lowerCamelCase__    :      Optional[int]					 =		self.prepare_config_and_inputs()
      (
          (
          lowerCamelCase__
      ) ,							(
          lowerCamelCase__
      ) ,							(
          lowerCamelCase__
      ) ,							(
          lowerCamelCase__
      ) ,							(
          lowerCamelCase__
      ) ,							(
          lowerCamelCase__
      ) ,							(
          lowerCamelCase__
      ) ,							
      )    :      int					 =		config_and_inputs
      lowerCamelCase__    :      str					 =		{'input_ids': input_ids, 'token_type_ids': token_type_ids, 'attention_mask': input_mask}
      return config, inputs_dict


 def      A_	(      self  :      str	)			->	Optional[Any]:
     lowerCamelCase__    :      str					 =		TFMobileBertModelTest.TFMobileBertModelTester(self	)
     lowerCamelCase__    :      Optional[Any]					 =		ConfigTester(self			,     config_class=UpperCAmelCase			,     hidden_size=37	)
 def      A_	(      self  :      Tuple	)			->	List[str]:
     self.config_tester.run_common_tests()
 def      A_	(      self  :      Union[str, Any]	)			->	Tuple:
     lowerCamelCase__    :      str					 =		self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_mobilebert_model(*UpperCAmelCase	)
 def      A_	(      self  :      Union[str, Any]	)			->	Union[str, Any]:
     lowerCamelCase__    :      Optional[int]					 =		self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_mobilebert_for_masked_lm(*UpperCAmelCase	)
 def      A_	(      self  :      Optional[int]	)			->	Any:
     lowerCamelCase__    :      Tuple					 =		self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_mobilebert_for_multiple_choice(*UpperCAmelCase	)
 def      A_	(      self  :      int	)			->	Optional[Any]:
     lowerCamelCase__    :      str					 =		self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_mobilebert_for_next_sequence_prediction(*UpperCAmelCase	)
 def      A_	(      self  :      Tuple	)			->	Optional[int]:
     lowerCamelCase__    :      Dict					 =		self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_mobilebert_for_pretraining(*UpperCAmelCase	)
 def      A_	(      self  :      Union[str, Any]	)			->	Any:
     lowerCamelCase__    :      int					 =		self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_mobilebert_for_question_answering(*UpperCAmelCase	)
 def      A_	(      self  :      Union[str, Any]	)			->	Dict:
     lowerCamelCase__    :      int					 =		self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_mobilebert_for_sequence_classification(*UpperCAmelCase	)
 def      A_	(      self  :      Tuple	)			->	Optional[Any]:
     lowerCamelCase__    :      Dict					 =		self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_mobilebert_for_token_classification(*UpperCAmelCase	)







 @slow
 def      A_	(      self  :      List[str]	)			->	List[Any]:
     # for model_name in TF_MOBILEBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
     for model_name in ["google/mobilebert-uncased"]:
         lowerCamelCase__    :      Union[str, Any]					 =		TFMobileBertModel.from_pretrained(UpperCAmelCase	)
         self.assertIsNotNone(UpperCAmelCase	)


@require_tf
class    lowerCAmelCase							(					unittest.TestCase			):
 @slow
 def      A_	(      self  :      Optional[Any]	)			->	Union[str, Any]:
     lowerCamelCase__    :      int					 =		TFMobileBertForPreTraining.from_pretrained('google/mobilebert-uncased'	)
     lowerCamelCase__    :      List[str]					 =		tf.constant([[0, 1, 2, 3, 4, 5]]	)
     lowerCamelCase__    :      List[str]					 =		model(UpperCAmelCase	)[0]

     lowerCamelCase__    :      Tuple					 =		[1, 6, 30522]
     self.assertEqual(output.shape			,     UpperCAmelCase	)

     lowerCamelCase__    :      List[Any]					 =		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]			,     UpperCAmelCase			,     atol=1e-4	)












def        SCREAMING_SNAKE_CASE      (       _UpperCAmelCase						,	_UpperCAmelCase )			->     bool:
    lowerCamelCase__    :      List[str]					 =		len(_UpperCAmelCase )
    lowerCamelCase__    :      str					 =		[[False] * (required_sum + 1) for _ in range(arr_len + 1 )]

    # for each arr value, a sum of zero(0) can be formed by not taking any element
    # hence True/1
    for i in range(arr_len + 1 ):
        lowerCamelCase__    :      Tuple					 =		True

    # sum is not zero and set is empty then false
    for i in range(1						,	required_sum + 1 ):
        lowerCamelCase__    :      Dict					 =		False

    for i in range(1						,	arr_len + 1 ):
        for j in range(1						,	required_sum + 1 ):
            if arr[i - 1] > j:
                lowerCamelCase__    :      str					 =		subset[i - 1][j]
            if arr[i - 1] <= j:
                lowerCamelCase__    :      Dict					 =		subset[i - 1][j] or subset[i - 1][j - arr[i - 1]]

    return subset[arr_len][required_sum]


if __name__ == "__main__":
    import doctest

    doctest.testmod()





