
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       _snake_case   (	UpperCAmelCase_  ,						UpperCAmelCase_  ,						unittest.TestCase							):
 __lowerCAmelCase  :     int        =			(
     (
         TFMobileBertModel,
         TFMobileBertForMaskedLM,
         TFMobileBertForNextSentencePrediction,
         TFMobileBertForPreTraining,
         TFMobileBertForQuestionAnswering,
         TFMobileBertForSequenceClassification,
         TFMobileBertForTokenClassification,
         TFMobileBertForMultipleChoice,
     )
     if is_tf_available()
     else ()
 )
 __lowerCAmelCase  :     Dict        =			(
     {
         'feature-extraction': TFMobileBertModel,
         'fill-mask': TFMobileBertForMaskedLM,
         'question-answering': TFMobileBertForQuestionAnswering,
         'text-classification': TFMobileBertForSequenceClassification,
         'token-classification': TFMobileBertForTokenClassification,
         'zero-shot': TFMobileBertForSequenceClassification,
     }
     if is_tf_available()
     else {}
 )
 __lowerCAmelCase  :     Tuple        =			False
 __lowerCAmelCase  :     Optional[int]        =			False


 def      lowercase__     (					self					,  SCREAMING_SNAKE_CASE_					,  SCREAMING_SNAKE_CASE_					,  SCREAMING_SNAKE_CASE_=False):


   '''simple docstring'''




   lowercase__  :   Optional[int]    		=      super()._prepare_for_class(SCREAMING_SNAKE_CASE_					,  SCREAMING_SNAKE_CASE_					,  return_labels=SCREAMING_SNAKE_CASE_)

   if return_labels:
     if model_class in get_values(SCREAMING_SNAKE_CASE_):
       lowercase__  :   List[str]    		=      tf.zeros(self.model_tester.batch_size					,  dtype=tf.intaa)

   return inputs_dict



 class       _snake_case   (	UpperCAmelCase_							):


  def __init__(					self					,  SCREAMING_SNAKE_CASE_					,  SCREAMING_SNAKE_CASE_=13					,  SCREAMING_SNAKE_CASE_=7					,  SCREAMING_SNAKE_CASE_=True					,  SCREAMING_SNAKE_CASE_=True					,  SCREAMING_SNAKE_CASE_=True					,  SCREAMING_SNAKE_CASE_=True					,  SCREAMING_SNAKE_CASE_=99					,  SCREAMING_SNAKE_CASE_=32					,  SCREAMING_SNAKE_CASE_=32					,  SCREAMING_SNAKE_CASE_=2					,  SCREAMING_SNAKE_CASE_=4					,  SCREAMING_SNAKE_CASE_=37					,  SCREAMING_SNAKE_CASE_="gelu"					,  SCREAMING_SNAKE_CASE_=0.1					,  SCREAMING_SNAKE_CASE_=0.1					,  SCREAMING_SNAKE_CASE_=5_12					,  SCREAMING_SNAKE_CASE_=16					,  SCREAMING_SNAKE_CASE_=2					,  SCREAMING_SNAKE_CASE_=0.0_2					,  SCREAMING_SNAKE_CASE_=3					,  SCREAMING_SNAKE_CASE_=4					,  SCREAMING_SNAKE_CASE_=None					,  ):


    '''simple docstring'''




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


  def      lowercase__     (					self):


    '''simple docstring'''




    lowercase__  :   Union[str, Any]    		=      ids_tensor([self.batch_size, self.seq_length]					,  self.vocab_size)

    lowercase__  :   int    		=      None
    if self.use_input_mask:
      lowercase__  :   Union[str, Any]    		=      random_attention_mask([self.batch_size, self.seq_length])

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

    lowercase__  :   Tuple    		=      None
    lowercase__  :   List[str]    		=      None
    lowercase__  :   Any    		=      None
    if self.use_labels:
      lowercase__  :   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__  :   int    		=      ids_tensor([self.batch_size]					,  self.num_choices)

    lowercase__  :   List[str]    		=      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      lowercase__     (					self					,  SCREAMING_SNAKE_CASE_					,  SCREAMING_SNAKE_CASE_					,  SCREAMING_SNAKE_CASE_					,  SCREAMING_SNAKE_CASE_					,  SCREAMING_SNAKE_CASE_					,  SCREAMING_SNAKE_CASE_					,  SCREAMING_SNAKE_CASE_):


    '''simple docstring'''




    lowercase__  :   Optional[int]    		=      TFMobileBertModel(config=SCREAMING_SNAKE_CASE_)
    lowercase__  :   Optional[int]    		=      {"""input_ids""": input_ids, """attention_mask""": input_mask, """token_type_ids""": token_type_ids}
    lowercase__  :   Union[str, Any]    		=      model(SCREAMING_SNAKE_CASE_)

    lowercase__  :   List[str]    		=      [input_ids, input_mask]
    lowercase__  :   Any    		=      model(SCREAMING_SNAKE_CASE_)

    lowercase__  :   int    		=      model(SCREAMING_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      lowercase__     (					self					,  SCREAMING_SNAKE_CASE_					,  SCREAMING_SNAKE_CASE_					,  SCREAMING_SNAKE_CASE_					,  SCREAMING_SNAKE_CASE_					,  SCREAMING_SNAKE_CASE_					,  SCREAMING_SNAKE_CASE_					,  SCREAMING_SNAKE_CASE_):


    '''simple docstring'''




    lowercase__  :   Optional[int]    		=      TFMobileBertForMaskedLM(config=SCREAMING_SNAKE_CASE_)
    lowercase__  :   Any    		=      {"""input_ids""": input_ids, """attention_mask""": input_mask, """token_type_ids""": token_type_ids}
    lowercase__  :   Union[str, Any]    		=      model(SCREAMING_SNAKE_CASE_)
    self.parent.assertEqual(result.logits.shape					,  (self.batch_size, self.seq_length, self.vocab_size))


  def      lowercase__     (					self					,  SCREAMING_SNAKE_CASE_					,  SCREAMING_SNAKE_CASE_					,  SCREAMING_SNAKE_CASE_					,  SCREAMING_SNAKE_CASE_					,  SCREAMING_SNAKE_CASE_					,  SCREAMING_SNAKE_CASE_					,  SCREAMING_SNAKE_CASE_):


    '''simple docstring'''




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


  def      lowercase__     (					self					,  SCREAMING_SNAKE_CASE_					,  SCREAMING_SNAKE_CASE_					,  SCREAMING_SNAKE_CASE_					,  SCREAMING_SNAKE_CASE_					,  SCREAMING_SNAKE_CASE_					,  SCREAMING_SNAKE_CASE_					,  SCREAMING_SNAKE_CASE_):


    '''simple docstring'''




    lowercase__  :   Any    		=      TFMobileBertForPreTraining(config=SCREAMING_SNAKE_CASE_)
    lowercase__  :   str    		=      {"""input_ids""": input_ids, """attention_mask""": input_mask, """token_type_ids""": token_type_ids}
    lowercase__  :   Optional[Any]    		=      model(SCREAMING_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      lowercase__     (					self					,  SCREAMING_SNAKE_CASE_					,  SCREAMING_SNAKE_CASE_					,  SCREAMING_SNAKE_CASE_					,  SCREAMING_SNAKE_CASE_					,  SCREAMING_SNAKE_CASE_					,  SCREAMING_SNAKE_CASE_					,  SCREAMING_SNAKE_CASE_):


    '''simple docstring'''




    lowercase__  :   str    		=      self.num_labels
    lowercase__  :   Dict    		=      TFMobileBertForSequenceClassification(config=SCREAMING_SNAKE_CASE_)
    lowercase__  :   int    		=      {"""input_ids""": input_ids, """attention_mask""": input_mask, """token_type_ids""": token_type_ids}
    lowercase__  :   Optional[Any]    		=      model(SCREAMING_SNAKE_CASE_)
    self.parent.assertEqual(result.logits.shape					,  (self.batch_size, self.num_labels))


  def      lowercase__     (					self					,  SCREAMING_SNAKE_CASE_					,  SCREAMING_SNAKE_CASE_					,  SCREAMING_SNAKE_CASE_					,  SCREAMING_SNAKE_CASE_					,  SCREAMING_SNAKE_CASE_					,  SCREAMING_SNAKE_CASE_					,  SCREAMING_SNAKE_CASE_):


    '''simple docstring'''




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


  def      lowercase__     (					self					,  SCREAMING_SNAKE_CASE_					,  SCREAMING_SNAKE_CASE_					,  SCREAMING_SNAKE_CASE_					,  SCREAMING_SNAKE_CASE_					,  SCREAMING_SNAKE_CASE_					,  SCREAMING_SNAKE_CASE_					,  SCREAMING_SNAKE_CASE_):


    '''simple docstring'''




    lowercase__  :   List[str]    		=      self.num_labels
    lowercase__  :   Union[str, Any]    		=      TFMobileBertForTokenClassification(config=SCREAMING_SNAKE_CASE_)
    lowercase__  :   Optional[int]    		=      {"""input_ids""": input_ids, """attention_mask""": input_mask, """token_type_ids""": token_type_ids}
    lowercase__  :   Dict    		=      model(SCREAMING_SNAKE_CASE_)
    self.parent.assertEqual(result.logits.shape					,  (self.batch_size, self.seq_length, self.num_labels))


  def      lowercase__     (					self					,  SCREAMING_SNAKE_CASE_					,  SCREAMING_SNAKE_CASE_					,  SCREAMING_SNAKE_CASE_					,  SCREAMING_SNAKE_CASE_					,  SCREAMING_SNAKE_CASE_					,  SCREAMING_SNAKE_CASE_					,  SCREAMING_SNAKE_CASE_):


    '''simple docstring'''




    lowercase__  :   List[str]    		=      TFMobileBertForQuestionAnswering(config=SCREAMING_SNAKE_CASE_)
    lowercase__  :   Any    		=      {"""input_ids""": input_ids, """attention_mask""": input_mask, """token_type_ids""": token_type_ids}
    lowercase__  :   Union[str, Any]    		=      model(SCREAMING_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      lowercase__     (					self):


    '''simple docstring'''




    lowercase__  :   Optional[int]    		=      self.prepare_config_and_inputs()
    (
        (
        lowercase__
    )		,    (
        lowercase__
    )		,    (
        lowercase__
    )		,    (
        lowercase__
    )		,    (
        lowercase__
    )		,    (
        lowercase__
    )		,    (
        lowercase__
    )		,    
    )  :   List[str]    		=      config_and_inputs
    lowercase__  :   int    		=      {"""input_ids""": input_ids, """token_type_ids""": token_type_ids, """attention_mask""": input_mask}
    return config, inputs_dict


 def      lowercase__     (					self):


   '''simple docstring'''




   lowercase__  :   Dict    		=      TFMobileBertModelTest.TFMobileBertModelTester(self)
   lowercase__  :   Union[str, Any]    		=      ConfigTester(self					,  config_class=SCREAMING_SNAKE_CASE_					,  hidden_size=37)


 def      lowercase__     (					self):


   '''simple docstring'''




   self.config_tester.run_common_tests()


 def      lowercase__     (					self):


   '''simple docstring'''




   lowercase__  :   List[Any]    		=      self.model_tester.prepare_config_and_inputs()
   self.model_tester.create_and_check_mobilebert_model(*SCREAMING_SNAKE_CASE_)


 def      lowercase__     (					self):


   '''simple docstring'''




   lowercase__  :   int    		=      self.model_tester.prepare_config_and_inputs()
   self.model_tester.create_and_check_mobilebert_for_masked_lm(*SCREAMING_SNAKE_CASE_)


 def      lowercase__     (					self):


   '''simple docstring'''




   lowercase__  :   Tuple    		=      self.model_tester.prepare_config_and_inputs()
   self.model_tester.create_and_check_mobilebert_for_multiple_choice(*SCREAMING_SNAKE_CASE_)


 def      lowercase__     (					self):


   '''simple docstring'''




   lowercase__  :   List[Any]    		=      self.model_tester.prepare_config_and_inputs()
   self.model_tester.create_and_check_mobilebert_for_next_sequence_prediction(*SCREAMING_SNAKE_CASE_)


 def      lowercase__     (					self):


   '''simple docstring'''




   lowercase__  :   int    		=      self.model_tester.prepare_config_and_inputs()
   self.model_tester.create_and_check_mobilebert_for_pretraining(*SCREAMING_SNAKE_CASE_)


 def      lowercase__     (					self):


   '''simple docstring'''




   lowercase__  :   Tuple    		=      self.model_tester.prepare_config_and_inputs()
   self.model_tester.create_and_check_mobilebert_for_question_answering(*SCREAMING_SNAKE_CASE_)


 def      lowercase__     (					self):


   '''simple docstring'''




   lowercase__  :   Tuple    		=      self.model_tester.prepare_config_and_inputs()
   self.model_tester.create_and_check_mobilebert_for_sequence_classification(*SCREAMING_SNAKE_CASE_)


 def      lowercase__     (					self):


   '''simple docstring'''




   lowercase__  :   Tuple    		=      self.model_tester.prepare_config_and_inputs()
   self.model_tester.create_and_check_mobilebert_for_token_classification(*SCREAMING_SNAKE_CASE_)



 @slow
 def      lowercase__     (					self):


   '''simple docstring'''




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



@require_tf
class       _snake_case   (	unittest.TestCase							):


 @slow
 def      lowercase__     (					self):


   '''simple docstring'''




   lowercase__  :   Union[str, Any]    		=      TFMobileBertForPreTraining.from_pretrained("""google/mobilebert-uncased""")
   lowercase__  :   str    		=      tf.constant([[0, 1, 2, 3, 4, 5]])
   lowercase__  :   Optional[Any]    		=      model(SCREAMING_SNAKE_CASE_)[0]

   lowercase__  :   Optional[Any]    		=      [1, 6, 3_05_22]
   self.assertEqual(output.shape					,  SCREAMING_SNAKE_CASE_)

   lowercase__  :   Union[str, 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]					,  SCREAMING_SNAKE_CASE_					,  atol=1E-4)








from math import factorial

lowerCamelCase__    :		dict[str, int]          =   {str(digit): factorial(digit) for digit in range(1_0)}

def 		UpperCamelCase	(      lowercase_   )		->	int:


  '''simple docstring'''






  if not isinstance(lowercase_ , lowercase_   ):
    raise TypeError("""Parameter number must be int"""   )

  if number < 0:
    raise ValueError("""Parameter number must be greater than or equal to 0"""   )

  # Converts number in string to iterate on its digits and adds its factorial.
  return sum(DIGIT_FACTORIAL[digit] for digit in str(lowercase_   )   )

def 		UpperCamelCase	(      lowercase_ = 60 , lowercase_ = 1_00_00_00   )		->	int:


  '''simple docstring'''






  if not isinstance(lowercase_ , lowercase_   ) or not isinstance(lowercase_ , lowercase_   ):
    raise TypeError("""Parameters chain_length and number_limit must be int"""   )

  if chain_length <= 0 or number_limit <= 0:
    raise ValueError(
        """Parameters chain_length and number_limit must be greater than 0"""   )

  # the counter for the chains with the exact desired length
  lowercase__  :   List[Any]    		=      0
  # the cached sizes of the previous chains
  lowercase__  :   dict[int, int]    		=      {}

  for start_chain_element in range(1 , lowercase_   ):
    # The temporary set will contain the elements of the chain
    lowercase__  :   Optional[Any]    		=      set()
    lowercase__  :   Union[str, Any]    		=      0

    # Stop computing the chain when you find a cached size, a repeating item or the
    # length is greater then the desired one.
    lowercase__  :   Union[str, Any]    		=      start_chain_element
    while (
        chain_element not in chain_sets_lengths
        and chain_element not in chain_set
        and chain_set_length <= chain_length
    ):
      chain_set.add(lowercase_   )
      chain_set_length += 1
      lowercase__  :   str    		=      digit_factorial_sum(lowercase_   )

    if chain_element in chain_sets_lengths:
      chain_set_length += chain_sets_lengths[chain_element]

    lowercase__  :   str    		=      chain_set_length

    # If chain contains the exact amount of elements increase the counter
    if chain_set_length == chain_length:
      chains_counter += 1

  return chains_counter


if __name__ == "__main__":
     import doctest

     doctest.testmod()
     print(f'''{solution()}''')






