


'''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_   (	__a     ,       __a     ,       unittest.TestCase      ):
      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 lowercase_							(					self				:    Optional[int]						,						_A				:    Union[str, Any]						,						_A				:    List[str]						,						_A				:    Optional[int]=False ):


          '''simple docstring'''


          UpperCAmelCase__			:		List[Any]													=				super()._prepare_for_class(_A						,						_A						,						return_labels=_A )

          if return_labels:
              if model_class in get_values(_A ):
                  UpperCAmelCase__			:		Dict													=				tf.zeros(self.model_tester.batch_size						,						dtype=tf.intaa )

          return inputs_dict




      class 			lowerCamelCase_   (	__a      ):






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


                '''simple docstring'''


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






            def lowercase_							(					self				:    List[str] ):


                '''simple docstring'''


                UpperCAmelCase__			:		List[Any]													=				ids_tensor([self.batch_size, self.seq_length]						,						self.vocab_size )

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

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

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

                UpperCAmelCase__			:		int													=				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				:    int						,						_A				:    List[str]						,						_A				:    List[Any]						,						_A				:    Union[str, Any]						,						_A				:    Optional[Any]						,						_A				:    Any						,						_A				:    int						,						_A				:    List[Any] ):


                '''simple docstring'''


                UpperCAmelCase__			:		str													=				TFMobileBertModel(config=_A )
                UpperCAmelCase__			:		Tuple													=				{'''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids}
                UpperCAmelCase__			:		Union[str, Any]													=				model(_A )

                UpperCAmelCase__			:		Optional[int]													=				[input_ids, input_mask]
                UpperCAmelCase__			:		str													=				model(_A )

                UpperCAmelCase__			:		Optional[int]													=				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 lowercase_							(					self				:    Any						,						_A				:    Union[str, Any]						,						_A				:    Tuple						,						_A				:    List[Any]						,						_A				:    Dict						,						_A				:    Dict						,						_A				:    Union[str, Any]						,						_A				:    str ):


                '''simple docstring'''


                UpperCAmelCase__			:		Optional[Any]													=				TFMobileBertForMaskedLM(config=_A )
                UpperCAmelCase__			:		Any													=				{'''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids}
                UpperCAmelCase__			:		List[Any]													=				model(_A )
                self.parent.assertEqual(result.logits.shape						,						(self.batch_size, self.seq_length, self.vocab_size) )






            def lowercase_							(					self				:    Optional[int]						,						_A				:    int						,						_A				:    List[Any]						,						_A				:    List[Any]						,						_A				:    Any						,						_A				:    Optional[Any]						,						_A				:    Tuple						,						_A				:    Optional[Any] ):


                '''simple docstring'''


                UpperCAmelCase__			:		List[str]													=				TFMobileBertForNextSentencePrediction(config=_A )
                UpperCAmelCase__			:		str													=				{'''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids}
                UpperCAmelCase__			:		Union[str, Any]													=				model(_A )
                self.parent.assertEqual(result.logits.shape						,						(self.batch_size, 2) )






            def lowercase_							(					self				:    Dict						,						_A				:    Optional[Any]						,						_A				:    Optional[int]						,						_A				:    List[str]						,						_A				:    Optional[int]						,						_A				:    Dict						,						_A				:    Optional[int]						,						_A				:    Tuple ):


                '''simple docstring'''


                UpperCAmelCase__			:		Tuple													=				TFMobileBertForPreTraining(config=_A )
                UpperCAmelCase__			:		Union[str, Any]													=				{'''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids}
                UpperCAmelCase__			:		Optional[int]													=				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 lowercase_							(					self				:    Any						,						_A				:    Any						,						_A				:    List[Any]						,						_A				:    Dict						,						_A				:    List[Any]						,						_A				:    List[Any]						,						_A				:    Optional[int]						,						_A				:    str ):


                '''simple docstring'''


                UpperCAmelCase__			:		Dict													=				self.num_labels
                UpperCAmelCase__			:		Dict													=				TFMobileBertForSequenceClassification(config=_A )
                UpperCAmelCase__			:		Union[str, Any]													=				{'''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids}
                UpperCAmelCase__			:		Union[str, Any]													=				model(_A )
                self.parent.assertEqual(result.logits.shape						,						(self.batch_size, self.num_labels) )






            def lowercase_							(					self				:    List[str]						,						_A				:    Optional[Any]						,						_A				:    Any						,						_A				:    Any						,						_A				:    Dict						,						_A				:    Union[str, Any]						,						_A				:    Tuple						,						_A				:    List[str] ):


                '''simple docstring'''


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






            def lowercase_							(					self				:    Any						,						_A				:    Optional[Any]						,						_A				:    List[str]						,						_A				:    List[Any]						,						_A				:    Tuple						,						_A				:    Optional[int]						,						_A				:    str						,						_A				:    List[Any] ):


                '''simple docstring'''


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






            def lowercase_							(					self				:    int						,						_A				:    Dict						,						_A				:    str						,						_A				:    Optional[Any]						,						_A				:    Tuple						,						_A				:    List[str]						,						_A				:    int						,						_A				:    int ):


                '''simple docstring'''


                UpperCAmelCase__			:		Dict													=				TFMobileBertForQuestionAnswering(config=_A )
                UpperCAmelCase__			:		int													=				{'''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids}
                UpperCAmelCase__			:		Tuple													=				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 lowercase_							(					self				:    int ):


                '''simple docstring'''


                UpperCAmelCase__			:		int													=				self.prepare_config_and_inputs()
                (
                    (
                    UpperCAmelCase__
                )						,						(
                    UpperCAmelCase__
                )						,						(
                    UpperCAmelCase__
                )						,						(
                    UpperCAmelCase__
                )						,						(
                    UpperCAmelCase__
                )						,						(
                    UpperCAmelCase__
                )						,						(
                    UpperCAmelCase__
                )						,						
                )			:		Optional[Any]													=				config_and_inputs
                UpperCAmelCase__			:		Union[str, Any]													=				{'''input_ids''': input_ids, '''token_type_ids''': token_type_ids, '''attention_mask''': input_mask}
                return config, inputs_dict






      def lowercase_							(					self				:    int ):


          '''simple docstring'''


          UpperCAmelCase__			:		List[Any]													=				TFMobileBertModelTest.TFMobileBertModelTester(self )
          UpperCAmelCase__			:		Optional[int]													=				ConfigTester(self						,						config_class=_A						,						hidden_size=37 )






      def lowercase_							(					self				:    str ):


          '''simple docstring'''


          self.config_tester.run_common_tests()






      def lowercase_							(					self				:    Any ):


          '''simple docstring'''


          UpperCAmelCase__			:		Optional[Any]													=				self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_mobilebert_model(*_A )






      def lowercase_							(					self				:    List[str] ):


          '''simple docstring'''


          UpperCAmelCase__			:		str													=				self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_mobilebert_for_masked_lm(*_A )






      def lowercase_							(					self				:    List[str] ):


          '''simple docstring'''


          UpperCAmelCase__			:		Union[str, Any]													=				self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_mobilebert_for_multiple_choice(*_A )






      def lowercase_							(					self				:    int ):


          '''simple docstring'''


          UpperCAmelCase__			:		Dict													=				self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_mobilebert_for_next_sequence_prediction(*_A )






      def lowercase_							(					self				:    Optional[int] ):


          '''simple docstring'''


          UpperCAmelCase__			:		List[str]													=				self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_mobilebert_for_pretraining(*_A )






      def lowercase_							(					self				:    List[Any] ):


          '''simple docstring'''


          UpperCAmelCase__			:		List[Any]													=				self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_mobilebert_for_question_answering(*_A )






      def lowercase_							(					self				:    str ):


          '''simple docstring'''


          UpperCAmelCase__			:		List[Any]													=				self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_mobilebert_for_sequence_classification(*_A )






      def lowercase_							(					self				:    Any ):


          '''simple docstring'''


          UpperCAmelCase__			:		Optional[int]													=				self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_mobilebert_for_token_classification(*_A )






      @slow
      def lowercase_							(					self				:    Optional[Any] ):


          '''simple docstring'''


          for model_name in ["google/mobilebert-uncased"]:
              UpperCAmelCase__			:		Optional[Any]													=				TFMobileBertModel.from_pretrained(_A )
              self.assertIsNotNone(_A )




@require_tf
class 			lowerCamelCase_   (	unittest.TestCase      ):






      @slow
      def lowercase_							(					self				:    Optional[Any] ):


          '''simple docstring'''


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

          UpperCAmelCase__			:		Optional[Any]													=				[1, 6, 30_522]
          self.assertEqual(output.shape						,						_A )

          UpperCAmelCase__			:		List[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]						,						_A						,						atol=1e-4 )




'''simple docstring'''
import math




class 			lowerCamelCase_   :






      def lowercase_							(					self				:    Optional[Any]						,						_A				:    list[list[float]]						,						_A				:    list[int] ):


          '''simple docstring'''


          UpperCAmelCase__			:		List[str]													=				0.0
          UpperCAmelCase__			:		Union[str, Any]													=				0.0
          for i in range(len(_A ) ):
              da += math.pow((sample[i] - weights[0][i])						,						2 )
              da += math.pow((sample[i] - weights[1][i])						,						2 )
              return 0 if da > da else 1
          return 0






      def lowercase_							(					self				:    Dict						,						_A				:    list[list[int | float]]						,						_A				:    list[int]						,						_A				:    int						,						_A				:    float ):


          '''simple docstring'''


          for i in range(len(_A ) ):
              weights[j][i] += alpha * (sample[i] - weights[j][i])
          return weights





def 					a__       (					)    ->	None:
    # Training Examples ( m, n )
    UpperCAmelCase__			:		Optional[Any]													=				[[1, 1, 0, 0], [0, 0, 0, 1], [1, 0, 0, 0], [0, 0, 1, 1]]

    # weight initialization ( n, C )
    UpperCAmelCase__			:		str													=				[[0.2, 0.6, 0.5, 0.9], [0.8, 0.4, 0.7, 0.3]]

    # training
    UpperCAmelCase__			:		Optional[Any]													=				SelfOrganizingMap()
    UpperCAmelCase__			:		Tuple													=				3
    UpperCAmelCase__			:		Dict													=				0.5

    for _ in range(lowerCAmelCase__						):
        for j in range(len(lowerCAmelCase__						)						):
            # training sample
            UpperCAmelCase__			:		Optional[int]													=				training_samples[j]

            # Compute the winning vector
            UpperCAmelCase__			:		str													=				self_organizing_map.get_winner(lowerCAmelCase__  ,					lowerCAmelCase__						)

            # Update the winning vector
            UpperCAmelCase__			:		int													=				self_organizing_map.update(lowerCAmelCase__  ,					lowerCAmelCase__  ,					lowerCAmelCase__  ,					lowerCAmelCase__						)

    # classify test sample
    UpperCAmelCase__			:		str													=				[0, 0, 0, 1]
    UpperCAmelCase__			:		int													=				self_organizing_map.get_winner(lowerCAmelCase__  ,					lowerCAmelCase__						)

    # results
    print(F"""Clusters that the test sample belongs to : {winner}"""						)
    print(F"""Weights that have been trained : {weights}"""						)


# running the main() function
if __name__ == "__main__":
  main()
