




'''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       A   (		snake_case_					,snake_case_					,unittest.TestCase   ):
       lowercase_          =    (
           (
               TFMobileBertModel,
               TFMobileBertForMaskedLM,
               TFMobileBertForNextSentencePrediction,
               TFMobileBertForPreTraining,
               TFMobileBertForQuestionAnswering,
               TFMobileBertForSequenceClassification,
               TFMobileBertForTokenClassification,
               TFMobileBertForMultipleChoice,
           )
           if is_tf_available()
           else ()
       )
       lowercase_          =    (
           {
               'feature-extraction': TFMobileBertModel,
               'fill-mask': TFMobileBertForMaskedLM,
               'question-answering': TFMobileBertForQuestionAnswering,
               'text-classification': TFMobileBertForSequenceClassification,
               'token-classification': TFMobileBertForTokenClassification,
               'zero-shot': TFMobileBertForSequenceClassification,
           }
           if is_tf_available()
           else {}
       )
       lowercase_          =    False
       lowercase_          =    False







       def        __lowerCAmelCase      (  self	:  Union[str, Any]							,      lowerCAmelCase_	:  Tuple							,      lowerCAmelCase_	:  Optional[Any]							,      lowerCAmelCase_	:  Dict=False )      ->							Optional[Any]:


              """simple docstring"""


              _a     					=    super()._prepare_for_class(lowerCAmelCase_							,      lowerCAmelCase_							,      return_labels=lowerCAmelCase_ )

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

              return inputs_dict

       class       A   (		snake_case_   ):







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


                     """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        __lowerCAmelCase      (  self	:  str )      ->							str:


                     """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        __lowerCAmelCase      (  self	:  Dict							,      lowerCAmelCase_	:  str							,      lowerCAmelCase_	:  Optional[int]							,      lowerCAmelCase_	:  Optional[Any]							,      lowerCAmelCase_	:  Tuple							,      lowerCAmelCase_	:  str							,      lowerCAmelCase_	:  Optional[Any]							,      lowerCAmelCase_	:  List[str] )      ->							int:


                     """simple docstring"""


                     _a     					=    TFMobileBertModel(config=lowerCAmelCase_ )
                     _a     					=    {'input_ids': input_ids, 'attention_mask': input_mask, 'token_type_ids': token_type_ids}
                     _a     					=    model(lowerCAmelCase_ )

                     _a     					=    [input_ids, input_mask]
                     _a     					=    model(lowerCAmelCase_ )

                     _a     					=    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	:  int							,      lowerCAmelCase_	:  Union[str, Any]							,      lowerCAmelCase_	:  Optional[int]							,      lowerCAmelCase_	:  Union[str, Any]							,      lowerCAmelCase_	:  Dict							,      lowerCAmelCase_	:  Any							,      lowerCAmelCase_	:  Dict							,      lowerCAmelCase_	:  int )      ->							Union[str, Any]:


                     """simple docstring"""


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







              def        __lowerCAmelCase      (  self	:  Tuple							,      lowerCAmelCase_	:  Any							,      lowerCAmelCase_	:  int							,      lowerCAmelCase_	:  int							,      lowerCAmelCase_	:  Union[str, Any]							,      lowerCAmelCase_	:  Optional[int]							,      lowerCAmelCase_	:  Optional[Any]							,      lowerCAmelCase_	:  Optional[int] )      ->							Optional[Any]:


                     """simple docstring"""


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







              def        __lowerCAmelCase      (  self	:  Union[str, Any]							,      lowerCAmelCase_	:  Optional[Any]							,      lowerCAmelCase_	:  List[Any]							,      lowerCAmelCase_	:  int							,      lowerCAmelCase_	:  str							,      lowerCAmelCase_	:  str							,      lowerCAmelCase_	:  Any							,      lowerCAmelCase_	:  List[Any] )      ->							Tuple:


                     """simple docstring"""


                     _a     					=    TFMobileBertForPreTraining(config=lowerCAmelCase_ )
                     _a     					=    {'input_ids': input_ids, 'attention_mask': input_mask, 'token_type_ids': token_type_ids}
                     _a     					=    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	:  Any							,      lowerCAmelCase_	:  Optional[int]							,      lowerCAmelCase_	:  Optional[Any]							,      lowerCAmelCase_	:  Dict							,      lowerCAmelCase_	:  List[str]							,      lowerCAmelCase_	:  str							,      lowerCAmelCase_	:  List[str]							,      lowerCAmelCase_	:  List[Any] )      ->							Optional[int]:


                     """simple docstring"""


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







              def        __lowerCAmelCase      (  self	:  List[Any]							,      lowerCAmelCase_	:  Tuple							,      lowerCAmelCase_	:  Any							,      lowerCAmelCase_	:  Optional[int]							,      lowerCAmelCase_	:  Optional[Any]							,      lowerCAmelCase_	:  str							,      lowerCAmelCase_	:  List[str]							,      lowerCAmelCase_	:  Any )      ->							List[str]:


                     """simple docstring"""


                     _a     					=    self.num_choices
                     _a     					=    TFMobileBertForMultipleChoice(config=lowerCAmelCase_ )
                     _a     					=    tf.tile(tf.expand_dims(lowerCAmelCase_							,      1 )							,      (1, self.num_choices, 1) )
                     _a     					=    tf.tile(tf.expand_dims(lowerCAmelCase_							,      1 )							,      (1, self.num_choices, 1) )
                     _a     					=    tf.tile(tf.expand_dims(lowerCAmelCase_							,      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(lowerCAmelCase_ )
                     self.parent.assertEqual(result.logits.shape							,      (self.batch_size, self.num_choices) )







              def        __lowerCAmelCase      (  self	:  Optional[int]							,      lowerCAmelCase_	:  Optional[int]							,      lowerCAmelCase_	:  Tuple							,      lowerCAmelCase_	:  str							,      lowerCAmelCase_	:  str							,      lowerCAmelCase_	:  Optional[int]							,      lowerCAmelCase_	:  str							,      lowerCAmelCase_	:  List[str] )      ->							Optional[int]:


                     """simple docstring"""


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







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


                     """simple docstring"""


                     _a     					=    TFMobileBertForQuestionAnswering(config=lowerCAmelCase_ )
                     _a     					=    {'input_ids': input_ids, 'attention_mask': input_mask, 'token_type_ids': token_type_ids}
                     _a     					=    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	:  Optional[int] )      ->							Dict:


                     """simple docstring"""


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







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


              """simple docstring"""


              _a     					=    TFMobileBertModelTest.TFMobileBertModelTester(self )
              _a     					=    ConfigTester(self							,      config_class=lowerCAmelCase_							,      hidden_size=37 )







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


              """simple docstring"""


              self.config_tester.run_common_tests()







       def        __lowerCAmelCase      (  self	:  int )      ->							str:


              """simple docstring"""


              _a     					=    self.model_tester.prepare_config_and_inputs()
              self.model_tester.create_and_check_mobilebert_model(*lowerCAmelCase_ )







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


              """simple docstring"""


              _a     					=    self.model_tester.prepare_config_and_inputs()
              self.model_tester.create_and_check_mobilebert_for_masked_lm(*lowerCAmelCase_ )







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


              """simple docstring"""


              _a     					=    self.model_tester.prepare_config_and_inputs()
              self.model_tester.create_and_check_mobilebert_for_multiple_choice(*lowerCAmelCase_ )







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


              """simple docstring"""


              _a     					=    self.model_tester.prepare_config_and_inputs()
              self.model_tester.create_and_check_mobilebert_for_next_sequence_prediction(*lowerCAmelCase_ )







       def        __lowerCAmelCase      (  self	:  List[Any] )      ->							Dict:


              """simple docstring"""


              _a     					=    self.model_tester.prepare_config_and_inputs()
              self.model_tester.create_and_check_mobilebert_for_pretraining(*lowerCAmelCase_ )







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


              """simple docstring"""


              _a     					=    self.model_tester.prepare_config_and_inputs()
              self.model_tester.create_and_check_mobilebert_for_question_answering(*lowerCAmelCase_ )







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


              """simple docstring"""


              _a     					=    self.model_tester.prepare_config_and_inputs()
              self.model_tester.create_and_check_mobilebert_for_sequence_classification(*lowerCAmelCase_ )







       def        __lowerCAmelCase      (  self	:  Any )      ->							str:


              """simple docstring"""


              _a     					=    self.model_tester.prepare_config_and_inputs()
              self.model_tester.create_and_check_mobilebert_for_token_classification(*lowerCAmelCase_ )







       @slow
       def        __lowerCAmelCase      (  self	:  str )      ->							Tuple:


              """simple docstring"""


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



@require_tf
class       A   (		unittest.TestCase   ):







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


              """simple docstring"""


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

              _a     					=    [1, 6, 3_05_22]
              self.assertEqual(output.shape							,      lowerCAmelCase_ )

              _a     					=    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 )







'''simple docstring'''


def 		snake_case_   (UpperCamelCase	:  str			,						UpperCamelCase	:  Any				):





							'''simple docstring'''


							return (pointa[0] - pointa[0]) ** 2 + (pointa[1] - pointa[1]) ** 2
def 		snake_case_   (UpperCamelCase	:  Any			,						UpperCamelCase	:  str=0				):





							'''simple docstring'''


							return sorted(UpperCamelCase			,						key=lambda UpperCamelCase				: x[column]				)
def 		snake_case_   (UpperCamelCase	:  Optional[int]			,						UpperCamelCase	:  Any			,						UpperCamelCase	:  Union[str, Any]=float('''inf'''				)				):





							'''simple docstring'''


							for i in range(points_counts - 1				):
														for j in range(i + 1			,						UpperCamelCase				):
																					_a     					=    euclidean_distance_sqr(points[i]			,						points[j]				)
																					if current_dis < min_dis:
																												_a     					=    current_dis
							return min_dis
def 		snake_case_   (UpperCamelCase	:  int			,						UpperCamelCase	:  Tuple			,						UpperCamelCase	:  List[str]=float('''inf'''				)				):





							'''simple docstring'''


							for i in range(min(6			,						points_counts - 1				)			,						UpperCamelCase				):
														for j in range(max(0			,						i - 6				)			,						UpperCamelCase				):
																					_a     					=    euclidean_distance_sqr(points[i]			,						points[j]				)
																					if current_dis < min_dis:
																												_a     					=    current_dis
							return min_dis
def 		snake_case_   (UpperCamelCase	:  int			,						UpperCamelCase	:  List[Any]			,						UpperCamelCase	:  int				):





							'''simple docstring'''


							if points_counts <= 3:
														return dis_between_closest_pair(UpperCamelCase			,						UpperCamelCase				)

							# recursion
							_a     					=    points_counts // 2
							_a     					=    closest_pair_of_points_sqr(
							    UpperCamelCase			,						points_sorted_on_y[:mid]			,						UpperCamelCase				)
							_a     					=    closest_pair_of_points_sqr(
							    UpperCamelCase			,						points_sorted_on_y[mid:]			,						points_counts - mid				)
							_a     					=    min(UpperCamelCase			,						UpperCamelCase				)
							_a     					=    []
							for point in points_sorted_on_x:
														if abs(point[0] - points_sorted_on_x[mid][0]				) < closest_pair_dis:
																					cross_strip.append(UpperCamelCase				)

							_a     					=    dis_between_closest_in_strip(
							    UpperCamelCase			,						len(UpperCamelCase				)			,						UpperCamelCase				)
							return min(UpperCamelCase			,						UpperCamelCase				)
def 		snake_case_   (UpperCamelCase	:  Optional[int]			,						UpperCamelCase	:  List[str]				):





							'''simple docstring'''


							_a     					=    column_based_sort(UpperCamelCase			,						column=0				)
							_a     					=    column_based_sort(UpperCamelCase			,						column=1				)
							return (
							    closest_pair_of_points_sqr(
							        UpperCamelCase			,						UpperCamelCase			,						UpperCamelCase				)
							) ** 0.5


if __name__ == "__main__":
				_snake_case				:				int       							=		[(2, 3), (12, 30), (40, 50), (5, 1), (12, 10), (3, 4)]
				print('Distance:', closest_pair_of_points(points, len(points)))

