



"""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     (					SCREAMING_SNAKE_CASE__	,					SCREAMING_SNAKE_CASE__	,					unittest.TestCase		):





		"""simple docstring"""






		_lowerCamelCase      	=							(
		    (
		        TFMobileBertModel,
		        TFMobileBertForMaskedLM,
		        TFMobileBertForNextSentencePrediction,
		        TFMobileBertForPreTraining,
		        TFMobileBertForQuestionAnswering,
		        TFMobileBertForSequenceClassification,
		        TFMobileBertForTokenClassification,
		        TFMobileBertForMultipleChoice,
		    )
		    if is_tf_available()
		    else ()
		)
		_lowerCamelCase      	=							(
		    {
		        '''feature-extraction''': TFMobileBertModel,
		        '''fill-mask''': TFMobileBertForMaskedLM,
		        '''question-answering''': TFMobileBertForQuestionAnswering,
		        '''text-classification''': TFMobileBertForSequenceClassification,
		        '''token-classification''': TFMobileBertForTokenClassification,
		        '''zero-shot''': TFMobileBertForSequenceClassification,
		    }
		    if is_tf_available()
		    else {}
		)
		_lowerCamelCase      	=							False
		_lowerCamelCase      	=							False





		def  UpperCAmelCase__				(       self				,     _lowercase				,     _lowercase				,     _lowercase=False )			->    Any:







								'''simple docstring'''

								snake_case_ :			str            =      super()._prepare_for_class(_lowercase				,     _lowercase				,     return_labels=_lowercase )

								if return_labels:
														if model_class in get_values(_lowercase ):
																				snake_case_ :			Optional[int]            =      tf.zeros(self.model_tester.batch_size				,     dtype=tf.intaa )

								return inputs_dict



		class  _lowerCAmelCase     (					SCREAMING_SNAKE_CASE__		):





				"""simple docstring"""






				def __init__(       self				,     _lowercase				,     _lowercase=1_3				,     _lowercase=7				,     _lowercase=True				,     _lowercase=True				,     _lowercase=True				,     _lowercase=True				,     _lowercase=9_9				,     _lowercase=3_2				,     _lowercase=3_2				,     _lowercase=2				,     _lowercase=4				,     _lowercase=3_7				,     _lowercase="gelu"				,     _lowercase=0.1				,     _lowercase=0.1				,     _lowercase=5_1_2				,     _lowercase=1_6				,     _lowercase=2				,     _lowercase=0.02				,     _lowercase=3				,     _lowercase=4				,     _lowercase=None				,     )			->    Tuple:







										'''simple docstring'''

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





				def  UpperCAmelCase__				(       self )			->    Union[str, Any]:







										'''simple docstring'''

										snake_case_ :			Dict            =      ids_tensor([self.batch_size, self.seq_length]				,     self.vocab_size )

										snake_case_ :			str            =      None
										if self.use_input_mask:
																snake_case_ :			Optional[Any]            =      random_attention_mask([self.batch_size, self.seq_length] )

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

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

										snake_case_ :			Dict            =      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				,     _lowercase				,     _lowercase				,     _lowercase				,     _lowercase				,     _lowercase				,     _lowercase				,     _lowercase )			->    List[str]:







										'''simple docstring'''

										snake_case_ :			int            =      TFMobileBertModel(config=_lowercase )
										snake_case_ :			List[str]            =      {"""input_ids""": input_ids, """attention_mask""": input_mask, """token_type_ids""": token_type_ids}
										snake_case_ :			Optional[Any]            =      model(_lowercase )

										snake_case_ :			Dict            =      [input_ids, input_mask]
										snake_case_ :			Any            =      model(_lowercase )

										snake_case_ :			Any            =      model(_lowercase )

										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				,     _lowercase				,     _lowercase				,     _lowercase				,     _lowercase				,     _lowercase				,     _lowercase				,     _lowercase )			->    List[Any]:







										'''simple docstring'''

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





				def  UpperCAmelCase__				(       self				,     _lowercase				,     _lowercase				,     _lowercase				,     _lowercase				,     _lowercase				,     _lowercase				,     _lowercase )			->    int:







										'''simple docstring'''

										snake_case_ :			str            =      TFMobileBertForNextSentencePrediction(config=_lowercase )
										snake_case_ :			Optional[int]            =      {"""input_ids""": input_ids, """attention_mask""": input_mask, """token_type_ids""": token_type_ids}
										snake_case_ :			List[str]            =      model(_lowercase )
										self.parent.assertEqual(result.logits.shape				,     (self.batch_size, 2) )





				def  UpperCAmelCase__				(       self				,     _lowercase				,     _lowercase				,     _lowercase				,     _lowercase				,     _lowercase				,     _lowercase				,     _lowercase )			->    List[str]:







										'''simple docstring'''

										snake_case_ :			Optional[int]            =      TFMobileBertForPreTraining(config=_lowercase )
										snake_case_ :			Optional[Any]            =      {"""input_ids""": input_ids, """attention_mask""": input_mask, """token_type_ids""": token_type_ids}
										snake_case_ :			Dict            =      model(_lowercase )
										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				,     _lowercase				,     _lowercase				,     _lowercase				,     _lowercase				,     _lowercase				,     _lowercase				,     _lowercase )			->    int:







										'''simple docstring'''

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





				def  UpperCAmelCase__				(       self				,     _lowercase				,     _lowercase				,     _lowercase				,     _lowercase				,     _lowercase				,     _lowercase				,     _lowercase )			->    Tuple:







										'''simple docstring'''

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





				def  UpperCAmelCase__				(       self				,     _lowercase				,     _lowercase				,     _lowercase				,     _lowercase				,     _lowercase				,     _lowercase				,     _lowercase )			->    str:







										'''simple docstring'''

										snake_case_ :			Any            =      self.num_labels
										snake_case_ :			Dict            =      TFMobileBertForTokenClassification(config=_lowercase )
										snake_case_ :			int            =      {"""input_ids""": input_ids, """attention_mask""": input_mask, """token_type_ids""": token_type_ids}
										snake_case_ :			Tuple            =      model(_lowercase )
										self.parent.assertEqual(result.logits.shape				,     (self.batch_size, self.seq_length, self.num_labels) )





				def  UpperCAmelCase__				(       self				,     _lowercase				,     _lowercase				,     _lowercase				,     _lowercase				,     _lowercase				,     _lowercase				,     _lowercase )			->    int:







										'''simple docstring'''

										snake_case_ :			Optional[Any]            =      TFMobileBertForQuestionAnswering(config=_lowercase )
										snake_case_ :			str            =      {"""input_ids""": input_ids, """attention_mask""": input_mask, """token_type_ids""": token_type_ids}
										snake_case_ :			Union[str, Any]            =      model(_lowercase )
										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 )			->    Union[str, Any]:







										'''simple docstring'''

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





		def  UpperCAmelCase__				(       self )			->    List[str]:







								'''simple docstring'''

								snake_case_ :			Optional[Any]            =      TFMobileBertModelTest.TFMobileBertModelTester(self )
								snake_case_ :			List[Any]            =      ConfigTester(self				,     config_class=_lowercase				,     hidden_size=3_7 )





		def  UpperCAmelCase__				(       self )			->    Any:







								'''simple docstring'''

								self.config_tester.run_common_tests()





		def  UpperCAmelCase__				(       self )			->    List[Any]:







								'''simple docstring'''

								snake_case_ :			Optional[Any]            =      self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_mobilebert_model(*_lowercase )





		def  UpperCAmelCase__				(       self )			->    Optional[Any]:







								'''simple docstring'''

								snake_case_ :			Optional[int]            =      self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_mobilebert_for_masked_lm(*_lowercase )





		def  UpperCAmelCase__				(       self )			->    Tuple:







								'''simple docstring'''

								snake_case_ :			Any            =      self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_mobilebert_for_multiple_choice(*_lowercase )





		def  UpperCAmelCase__				(       self )			->    List[str]:







								'''simple docstring'''

								snake_case_ :			Optional[Any]            =      self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_mobilebert_for_next_sequence_prediction(*_lowercase )





		def  UpperCAmelCase__				(       self )			->    Dict:







								'''simple docstring'''

								snake_case_ :			Union[str, Any]            =      self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_mobilebert_for_pretraining(*_lowercase )





		def  UpperCAmelCase__				(       self )			->    Optional[Any]:







								'''simple docstring'''

								snake_case_ :			str            =      self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_mobilebert_for_question_answering(*_lowercase )





		def  UpperCAmelCase__				(       self )			->    Union[str, Any]:







								'''simple docstring'''

								snake_case_ :			Dict            =      self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_mobilebert_for_sequence_classification(*_lowercase )





		def  UpperCAmelCase__				(       self )			->    Dict:







								'''simple docstring'''

								snake_case_ :			int            =      self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_mobilebert_for_token_classification(*_lowercase )





		@slow
		def  UpperCAmelCase__				(       self )			->    Optional[int]:







								'''simple docstring'''

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




@require_tf
class  _lowerCAmelCase     (					unittest.TestCase		):





		"""simple docstring"""






		@slow
		def  UpperCAmelCase__				(       self )			->    Tuple:







								'''simple docstring'''

								snake_case_ :			Optional[int]            =      TFMobileBertForPreTraining.from_pretrained("""google/mobilebert-uncased""" )
								snake_case_ :			Optional[Any]            =      tf.constant([[0, 1, 2, 3, 4, 5]] )
								snake_case_ :			int            =      model(_lowercase )[0]

								snake_case_ :			str            =      [1, 6, 3_0_5_2_2]
								self.assertEqual(output.shape				,     _lowercase )

								snake_case_ :			int            =      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]				,     _lowercase				,     atol=1E-4 )


def 				lowerCAmelCase__       (			a__ )      ->int:




  '''simple docstring'''

  if not isinstance(a__    ,     a__ ) or number < 0:
    raise ValueError("Input must be a non-negative integer" )

  _UpperCamelCase           =    0
  while number:
    # This way we arrive at next set bit (next 1) instead of looping
    # through each bit and checking for 1s hence the
    # loop won't run 32 times it will only run the number of `1` times
    number &= number - 1
    count += 1
  return count


if __name__ == "__main__":
   import doctest

   doctest.testmod()




