



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					(    _lowerCAmelCase       ,		_lowerCAmelCase       ,		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 					_lowerCamelCase (		self     :						Dict				,	A     :						str				,	A     :						List[Any]				,	A     :						List[str]=False)     ->					List[Any]:



						"""simple docstring"""






						_UpperCAmelCase	 =     super()._prepare_for_class(_lowerCAmelCase				,	_lowerCAmelCase				,	return_labels=_lowerCAmelCase)

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

						return inputs_dict







			class     __lowerCAmelCase					(    _lowerCAmelCase		):



						def __init__(		self     :						int				,	A     :						Union[str, Any]				,	A     :						Tuple=13				,	A     :						Dict=7				,	A     :						Optional[int]=True				,	A     :						List[str]=True				,	A     :						Dict=True				,	A     :						Tuple=True				,	A     :						int=99				,	A     :						Optional[int]=32				,	A     :						Tuple=32				,	A     :						Union[str, Any]=2				,	A     :						Optional[int]=4				,	A     :						int=37				,	A     :						List[Any]="gelu"				,	A     :						Any=0.1				,	A     :						Union[str, Any]=0.1				,	A     :						Optional[int]=5_12				,	A     :						List[str]=16				,	A     :						Any=2				,	A     :						List[str]=0.0_2				,	A     :						Dict=3				,	A     :						int=4				,	A     :						str=None				,	)     ->					Tuple:



									"""simple docstring"""






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



						def 					_lowerCamelCase (		self     :						Tuple)     ->					Dict:



									"""simple docstring"""






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

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

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

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

									_UpperCAmelCase	 =     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 					_lowerCamelCase (		self     :						Optional[int]				,	A     :						Optional[Any]				,	A     :						int				,	A     :						int				,	A     :						Any				,	A     :						Tuple				,	A     :						List[str]				,	A     :						Dict)     ->					Dict:



									"""simple docstring"""






									_UpperCAmelCase	 =     TFMobileBertModel(config=_lowerCAmelCase)
									_UpperCAmelCase	 =     {'input_ids': input_ids, 'attention_mask': input_mask, 'token_type_ids': token_type_ids}
									_UpperCAmelCase	 =     model(_lowerCAmelCase)

									_UpperCAmelCase	 =     [input_ids, input_mask]
									_UpperCAmelCase	 =     model(_lowerCAmelCase)

									_UpperCAmelCase	 =     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 					_lowerCamelCase (		self     :						Dict				,	A     :						Optional[int]				,	A     :						List[Any]				,	A     :						Optional[int]				,	A     :						Optional[Any]				,	A     :						List[str]				,	A     :						Tuple				,	A     :						str)     ->					Dict:



									"""simple docstring"""






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



						def 					_lowerCamelCase (		self     :						Union[str, Any]				,	A     :						str				,	A     :						Any				,	A     :						str				,	A     :						Optional[Any]				,	A     :						int				,	A     :						List[Any]				,	A     :						Optional[int])     ->					Union[str, Any]:



									"""simple docstring"""






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



						def 					_lowerCamelCase (		self     :						List[str]				,	A     :						Tuple				,	A     :						Optional[Any]				,	A     :						List[Any]				,	A     :						str				,	A     :						Dict				,	A     :						int				,	A     :						Dict)     ->					Optional[int]:



									"""simple docstring"""






									_UpperCAmelCase	 =     TFMobileBertForPreTraining(config=_lowerCAmelCase)
									_UpperCAmelCase	 =     {'input_ids': input_ids, 'attention_mask': input_mask, 'token_type_ids': token_type_ids}
									_UpperCAmelCase	 =     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 					_lowerCamelCase (		self     :						Optional[Any]				,	A     :						List[Any]				,	A     :						Optional[Any]				,	A     :						List[str]				,	A     :						List[Any]				,	A     :						Union[str, Any]				,	A     :						Union[str, Any]				,	A     :						Optional[Any])     ->					str:



									"""simple docstring"""






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



						def 					_lowerCamelCase (		self     :						List[Any]				,	A     :						Tuple				,	A     :						Any				,	A     :						Optional[Any]				,	A     :						Union[str, Any]				,	A     :						int				,	A     :						Union[str, Any]				,	A     :						Dict)     ->					int:



									"""simple docstring"""






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



						def 					_lowerCamelCase (		self     :						Dict				,	A     :						Dict				,	A     :						Optional[int]				,	A     :						str				,	A     :						Tuple				,	A     :						Dict				,	A     :						Optional[Any]				,	A     :						Optional[int])     ->					List[Any]:



									"""simple docstring"""






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



						def 					_lowerCamelCase (		self     :						int				,	A     :						Union[str, Any]				,	A     :						str				,	A     :						str				,	A     :						List[str]				,	A     :						str				,	A     :						Any				,	A     :						str)     ->					Any:



									"""simple docstring"""






									_UpperCAmelCase	 =     TFMobileBertForQuestionAnswering(config=_lowerCAmelCase)
									_UpperCAmelCase	 =     {'input_ids': input_ids, 'attention_mask': input_mask, 'token_type_ids': token_type_ids}
									_UpperCAmelCase	 =     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 					_lowerCamelCase (		self     :						Union[str, Any])     ->					Tuple:



									"""simple docstring"""






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





			def 					_lowerCamelCase (		self     :						List[str])     ->					Dict:



						"""simple docstring"""






						_UpperCAmelCase	 =     TFMobileBertModelTest.TFMobileBertModelTester(self)
						_UpperCAmelCase	 =     ConfigTester(self				,	config_class=_lowerCAmelCase				,	hidden_size=37)



			def 					_lowerCamelCase (		self     :						Any)     ->					Tuple:



						"""simple docstring"""






						self.config_tester.run_common_tests()



			def 					_lowerCamelCase (		self     :						Optional[int])     ->					int:



						"""simple docstring"""






						_UpperCAmelCase	 =     self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_mobilebert_model(*_lowerCAmelCase)



			def 					_lowerCamelCase (		self     :						List[Any])     ->					Optional[int]:



						"""simple docstring"""






						_UpperCAmelCase	 =     self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_mobilebert_for_masked_lm(*_lowerCAmelCase)



			def 					_lowerCamelCase (		self     :						str)     ->					List[Any]:



						"""simple docstring"""






						_UpperCAmelCase	 =     self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_mobilebert_for_multiple_choice(*_lowerCAmelCase)



			def 					_lowerCamelCase (		self     :						str)     ->					Dict:



						"""simple docstring"""






						_UpperCAmelCase	 =     self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_mobilebert_for_next_sequence_prediction(*_lowerCAmelCase)



			def 					_lowerCamelCase (		self     :						int)     ->					int:



						"""simple docstring"""






						_UpperCAmelCase	 =     self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_mobilebert_for_pretraining(*_lowerCAmelCase)



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



						"""simple docstring"""






						_UpperCAmelCase	 =     self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_mobilebert_for_question_answering(*_lowerCAmelCase)



			def 					_lowerCamelCase (		self     :						List[Any])     ->					List[str]:



						"""simple docstring"""






						_UpperCAmelCase	 =     self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_mobilebert_for_sequence_classification(*_lowerCAmelCase)



			def 					_lowerCamelCase (		self     :						int)     ->					List[Any]:



						"""simple docstring"""






						_UpperCAmelCase	 =     self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_mobilebert_for_token_classification(*_lowerCAmelCase)



			@slow
			def 					_lowerCamelCase (		self     :						Dict)     ->					Union[str, Any]:



						"""simple docstring"""






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







@require_tf
class     __lowerCAmelCase					(    unittest.TestCase		):



			@slow
			def 					_lowerCamelCase (		self     :						Any)     ->					Any:



						"""simple docstring"""






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

						_UpperCAmelCase	 =     [1, 6, 3_05_22]
						self.assertEqual(output.shape				,	_lowerCAmelCase)

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





def 							A			(  _UpperCAmelCase       :     int							,     _UpperCAmelCase       :     int					)       ->				str:







			'''simple docstring'''
			if a < 0 or b < 0:
						raise ValueError('the value of both inputs must be positive'					)

			_UpperCAmelCase	 =     str(bin(_UpperCAmelCase					)					)[2:]  # remove the leading "0b"
			_UpperCAmelCase	 =     str(bin(_UpperCAmelCase					)					)[2:]  # remove the leading "0b"

			_UpperCAmelCase	 =     max(len(_UpperCAmelCase					)							,     len(_UpperCAmelCase					)					)

			return "0b" + "".join(
			    str(int(char_a != char_b					)					)
			    for char_a, char_b in zip(a_binary.zfill(_UpperCAmelCase					)							,     b_binary.zfill(_UpperCAmelCase					)					)					)


if __name__ == "__main__":
							import doctest

							doctest.testmod()
