






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



	"""simple docstring"""

	SCREAMING_SNAKE_CASE_:							List[str]   			=					(
	    (
	        TFMobileBertModel,
	        TFMobileBertForMaskedLM,
	        TFMobileBertForNextSentencePrediction,
	        TFMobileBertForPreTraining,
	        TFMobileBertForQuestionAnswering,
	        TFMobileBertForSequenceClassification,
	        TFMobileBertForTokenClassification,
	        TFMobileBertForMultipleChoice,
	    )
	    if is_tf_available()
	    else ()
	)
	SCREAMING_SNAKE_CASE_:							Tuple   			=					(
	    {
	        """feature-extraction""": TFMobileBertModel,
	        """fill-mask""": TFMobileBertForMaskedLM,
	        """question-answering""": TFMobileBertForQuestionAnswering,
	        """text-classification""": TFMobileBertForSequenceClassification,
	        """token-classification""": TFMobileBertForTokenClassification,
	        """zero-shot""": TFMobileBertForSequenceClassification,
	    }
	    if is_tf_available()
	    else {}
	)
	SCREAMING_SNAKE_CASE_:							Any   			=					False
	SCREAMING_SNAKE_CASE_:							Optional[int]   			=					False


	def       _UpperCAmelCase						(      self  ,					__a  ,					__a  ,					__a=False						):


					"""simple docstring"""

					A__							 =       super()._prepare_for_class(__a  ,					__a  ,					return_labels=__a						)

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

					return inputs_dict





	class       snake_case_       (    _lowerCamelCase       ):



		"""simple docstring"""

		def __init__(      self  ,					__a  ,					__a=13  ,					__a=7  ,					__a=True  ,					__a=True  ,					__a=True  ,					__a=True  ,					__a=99  ,					__a=32  ,					__a=32  ,					__a=2  ,					__a=4  ,					__a=37  ,					__a="gelu"  ,					__a=0.1  ,					__a=0.1  ,					__a=512  ,					__a=16  ,					__a=2  ,					__a=0.02  ,					__a=3  ,					__a=4  ,					__a=None  ,					):


						"""simple docstring"""

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


		def       _UpperCAmelCase						(      self						):


						"""simple docstring"""

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

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

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

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

						A__							 =       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  ,					__a  ,					__a  ,					__a  ,					__a  ,					__a  ,					__a  ,					__a						):


						"""simple docstring"""

						A__							 =       TFMobileBertModel(config=__a						)
						A__							 =       {'input_ids': input_ids, 'attention_mask': input_mask, 'token_type_ids': token_type_ids}
						A__							 =       model(__a						)

						A__							 =       [input_ids, input_mask]
						A__							 =       model(__a						)

						A__							 =       model(__a						)

						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  ,					__a  ,					__a  ,					__a  ,					__a  ,					__a  ,					__a  ,					__a						):


						"""simple docstring"""

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


		def       _UpperCAmelCase						(      self  ,					__a  ,					__a  ,					__a  ,					__a  ,					__a  ,					__a  ,					__a						):


						"""simple docstring"""

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


		def       _UpperCAmelCase						(      self  ,					__a  ,					__a  ,					__a  ,					__a  ,					__a  ,					__a  ,					__a						):


						"""simple docstring"""

						A__							 =       TFMobileBertForPreTraining(config=__a						)
						A__							 =       {'input_ids': input_ids, 'attention_mask': input_mask, 'token_type_ids': token_type_ids}
						A__							 =       model(__a						)
						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  ,					__a  ,					__a  ,					__a  ,					__a  ,					__a  ,					__a  ,					__a						):


						"""simple docstring"""

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


		def       _UpperCAmelCase						(      self  ,					__a  ,					__a  ,					__a  ,					__a  ,					__a  ,					__a  ,					__a						):


						"""simple docstring"""

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


		def       _UpperCAmelCase						(      self  ,					__a  ,					__a  ,					__a  ,					__a  ,					__a  ,					__a  ,					__a						):


						"""simple docstring"""

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


		def       _UpperCAmelCase						(      self  ,					__a  ,					__a  ,					__a  ,					__a  ,					__a  ,					__a  ,					__a						):


						"""simple docstring"""

						A__							 =       TFMobileBertForQuestionAnswering(config=__a						)
						A__							 =       {'input_ids': input_ids, 'attention_mask': input_mask, 'token_type_ids': token_type_ids}
						A__							 =       model(__a						)
						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"""

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





	def       _UpperCAmelCase						(      self						):


					"""simple docstring"""

					A__							 =       TFMobileBertModelTest.TFMobileBertModelTester(self						)
					A__							 =       ConfigTester(self  ,					config_class=__a  ,					hidden_size=37						)


	def       _UpperCAmelCase						(      self						):


					"""simple docstring"""

					self.config_tester.run_common_tests()


	def       _UpperCAmelCase						(      self						):


					"""simple docstring"""

					A__							 =       self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_mobilebert_model(*__a						)


	def       _UpperCAmelCase						(      self						):


					"""simple docstring"""

					A__							 =       self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_mobilebert_for_masked_lm(*__a						)


	def       _UpperCAmelCase						(      self						):


					"""simple docstring"""

					A__							 =       self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_mobilebert_for_multiple_choice(*__a						)


	def       _UpperCAmelCase						(      self						):


					"""simple docstring"""

					A__							 =       self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_mobilebert_for_next_sequence_prediction(*__a						)


	def       _UpperCAmelCase						(      self						):


					"""simple docstring"""

					A__							 =       self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_mobilebert_for_pretraining(*__a						)


	def       _UpperCAmelCase						(      self						):


					"""simple docstring"""

					A__							 =       self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_mobilebert_for_question_answering(*__a						)


	def       _UpperCAmelCase						(      self						):


					"""simple docstring"""

					A__							 =       self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_mobilebert_for_sequence_classification(*__a						)


	def       _UpperCAmelCase						(      self						):


					"""simple docstring"""

					A__							 =       self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_mobilebert_for_token_classification(*__a						)






	@slow
	def       _UpperCAmelCase						(      self						):


					"""simple docstring"""

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





@require_tf
class       snake_case_       (    unittest.TestCase       ):



	"""simple docstring"""

	@slow
	def       _UpperCAmelCase						(      self						):


					"""simple docstring"""

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

					A__							 =       [1, 6, 3_0522]
					self.assertEqual(output.shape  ,					__a						)

					A__							 =       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]  ,					__a  ,					atol=1E-4						)










"""simple docstring"""






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


SCREAMING_SNAKE_CASE						:						Any					   =       logging.get_logger(__name__)





class       snake_case_       (    _lowerCamelCase       ):



	"""simple docstring"""

	SCREAMING_SNAKE_CASE_:							Union[str, Any]   			=					"""timm_backbone"""


	def __init__(      self  ,					__a=None  ,					__a=3  ,					__a=True  ,					__a=True  ,					__a=None  ,					**__a  ,					):


					"""simple docstring"""

					super().__init__(**__a						)
					A__							 =       backbone
					A__							 =       num_channels
					A__							 =       features_only
					A__							 =       use_pretrained_backbone
					A__							 =       True
					A__							 =       out_indices if out_indices is not None else (-1,)


