





import unittest

from transformers import MobileBertConfig, is_torch_available
from transformers.models.auto import get_values
from transformers.testing_utils import require_sentencepiece, require_tokenizers, require_torch, slow, torch_device

from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
   import torch

   from transformers import (
       MODEL_FOR_PRETRAINING_MAPPING,
       MobileBertForMaskedLM,
       MobileBertForMultipleChoice,
       MobileBertForNextSentencePrediction,
       MobileBertForPreTraining,
       MobileBertForQuestionAnswering,
       MobileBertForSequenceClassification,
       MobileBertForTokenClassification,
       MobileBertModel,
   )

class  __A:

     def __init__(      self,       A,       A=13,       A=7,       A=True,       A=True,       A=True,       A=True,       A=99,       A=64,       A=32,       A=5,       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"""

            _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              =						embedding_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

     def 	_UpperCamelCase     (      self      ):





            """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              =						self.get_config()

            return config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels

     def 	_UpperCamelCase     (      self      ):





            """simple docstring"""

            return 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,       embedding_size=self.embedding_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,       is_decoder=A,       initializer_range=self.initializer_range,       )

     def 	_UpperCamelCase     (      self,       A,       A,       A,       A,       A,       A,       A      ):





            """simple docstring"""

            _UpperCamelCase              =						MobileBertModel(config=A      )
            model.to(A      )
            model.eval()
            _UpperCamelCase              =						model(A,       attention_mask=A,       token_type_ids=A      )
            _UpperCamelCase              =						model(A,       token_type_ids=A      )
            _UpperCamelCase              =						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"""

            _UpperCamelCase              =						MobileBertForMaskedLM(config=A      )
            model.to(A      )
            model.eval()
            _UpperCamelCase              =						model(A,       attention_mask=A,       token_type_ids=A,       labels=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"""

            _UpperCamelCase              =						MobileBertForNextSentencePrediction(config=A      )
            model.to(A      )
            model.eval()
            _UpperCamelCase              =						model(
                A,       attention_mask=A,       token_type_ids=A,       labels=A,       )
            self.parent.assertEqual(result.logits.shape,       (self.batch_size, 2)      )

     def 	_UpperCamelCase     (      self,       A,       A,       A,       A,       A,       A,       A      ):





            """simple docstring"""

            _UpperCamelCase              =						MobileBertForPreTraining(config=A      )
            model.to(A      )
            model.eval()
            _UpperCamelCase              =						model(
                A,       attention_mask=A,       token_type_ids=A,       labels=A,       next_sentence_label=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"""

            _UpperCamelCase              =						MobileBertForQuestionAnswering(config=A      )
            model.to(A      )
            model.eval()
            _UpperCamelCase              =						model(
                A,       attention_mask=A,       token_type_ids=A,       start_positions=A,       end_positions=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,       A,       A,       A,       A,       A,       A,       A      ):





            """simple docstring"""

            _UpperCamelCase              =						self.num_labels
            _UpperCamelCase              =						MobileBertForSequenceClassification(A      )
            model.to(A      )
            model.eval()
            _UpperCamelCase              =						model(A,       attention_mask=A,       token_type_ids=A,       labels=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"""

            _UpperCamelCase              =						self.num_labels
            _UpperCamelCase              =						MobileBertForTokenClassification(config=A      )
            model.to(A      )
            model.eval()
            _UpperCamelCase              =						model(A,       attention_mask=A,       token_type_ids=A,       labels=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"""

            _UpperCamelCase              =						self.num_choices
            _UpperCamelCase              =						MobileBertForMultipleChoice(config=A      )
            model.to(A      )
            model.eval()
            _UpperCamelCase              =						input_ids.unsqueeze(1      ).expand(-1,       self.num_choices,       -1      ).contiguous()
            _UpperCamelCase              =						token_type_ids.unsqueeze(1      ).expand(-1,       self.num_choices,       -1      ).contiguous()
            _UpperCamelCase              =						input_mask.unsqueeze(1      ).expand(-1,       self.num_choices,       -1      ).contiguous()
            _UpperCamelCase              =						model(
                A,       attention_mask=A,       token_type_ids=A,       labels=A,       )
            self.parent.assertEqual(result.logits.shape,       (self.batch_size, self.num_choices)      )



     def 	_UpperCamelCase     (      self      ):





            """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






@require_torch
class  __A(						__UpperCAmelCase						,			__UpperCAmelCase						,			unittest.TestCase ):
     __A	  =       (
         (
             MobileBertModel,
             MobileBertForMaskedLM,
             MobileBertForMultipleChoice,
             MobileBertForNextSentencePrediction,
             MobileBertForPreTraining,
             MobileBertForQuestionAnswering,
             MobileBertForSequenceClassification,
             MobileBertForTokenClassification,
         )
         if is_torch_available()
         else ()
     )
     __A	  =       (
         {
             "feature-extraction": MobileBertModel,
             "fill-mask": MobileBertForMaskedLM,
             "question-answering": MobileBertForQuestionAnswering,
             "text-classification": MobileBertForSequenceClassification,
             "token-classification": MobileBertForTokenClassification,
             "zero-shot": MobileBertForSequenceClassification,
         }
         if is_torch_available()
         else {}
     )
     __A	  =       True

     def 	_UpperCamelCase     (      self,       A,       A,       A=False      ):





            """simple docstring"""

            _UpperCamelCase              =						super()._prepare_for_class(A,       A,       return_labels=A      )

            if return_labels:
                   if model_class in get_values(A      ):
                          _UpperCamelCase              =						torch.zeros(
                              (self.model_tester.batch_size, self.model_tester.seq_length),       dtype=torch.long,       device=A      )
                          _UpperCamelCase              =						torch.zeros(
                              self.model_tester.batch_size,       dtype=torch.long,       device=A      )
            return inputs_dict

     def 	_UpperCamelCase     (      self      ):





            """simple docstring"""

            _UpperCamelCase              =						MobileBertModelTester(self      )
            _UpperCamelCase              =						ConfigTester(self,       config_class=A,       hidden_size=37      )

     def 	_UpperCamelCase     (      self      ):





            """simple docstring"""

            self.config_tester.run_common_tests()

     def 	_UpperCamelCase     (      self      ):





            """simple docstring"""

            _UpperCamelCase              =						self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_mobilebert_model(*A      )

     def 	_UpperCamelCase     (      self      ):





            """simple docstring"""

            _UpperCamelCase              =						self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_mobilebert_for_masked_lm(*A      )

     def 	_UpperCamelCase     (      self      ):





            """simple docstring"""

            _UpperCamelCase              =						self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_mobilebert_for_multiple_choice(*A      )

     def 	_UpperCamelCase     (      self      ):





            """simple docstring"""

            _UpperCamelCase              =						self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_mobilebert_for_next_sequence_prediction(*A      )

     def 	_UpperCamelCase     (      self      ):





            """simple docstring"""

            _UpperCamelCase              =						self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_mobilebert_for_pretraining(*A      )

     def 	_UpperCamelCase     (      self      ):





            """simple docstring"""

            _UpperCamelCase              =						self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_mobilebert_for_question_answering(*A      )

     def 	_UpperCamelCase     (      self      ):





            """simple docstring"""

            _UpperCamelCase              =						self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_mobilebert_for_sequence_classification(*A      )



     def 	_UpperCamelCase     (      self      ):





            """simple docstring"""

            _UpperCamelCase              =						self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_mobilebert_for_token_classification(*A      )






def     SCREAMING_SNAKE_CASE			(      lowerCAmelCase					):
       return torch.tensor(
           lowerCAmelCase    ,       dtype=torch.long    ,       device=lowerCAmelCase    ,       )


lowercase    : Optional[Any]    	=  1e-3

@require_torch
@require_sentencepiece
@require_tokenizers
class  __A(						unittest.TestCase ):

     @slow
     def 	_UpperCamelCase     (      self      ):





            """simple docstring"""

            _UpperCamelCase              =						MobileBertModel.from_pretrained('''google/mobilebert-uncased'''      ).to(A      )
            _UpperCamelCase              =						_long_tensor([[101, 7110, 1005, 1056, 2023, 1_1333, 1_7413, 1029, 102]]      )
            with torch.no_grad():
                   _UpperCamelCase              =						model(A      )[0]
            _UpperCamelCase              =						torch.Size((1, 9, 512)      )
            self.assertEqual(output.shape,       A      )
            _UpperCamelCase              =						torch.tensor(
                [
                    [
                        [-2.4_7_3_6_5_2_6E0_7, 8.2_6_9_1_6_5_6E0_4, 1.6_5_2_1_8_3_8E0_5],
                        [-5.7_5_4_1_7_0_4E-0_1, 3.9_0_5_6_0_2_2E0_0, 4.4_0_1_1_5_0_7E0_0],
                        [2.6_0_4_7_3_5_9E0_0, 1.5_6_7_7_6_5_2E0_0, -1.7_3_2_4_1_8_8E-0_1],
                    ]
                ],       device=A,       )

            # MobileBERT results range from 10e0 to 10e8. Even a 0.0000001% difference with a value of 10e8 results in a
            # ~1 difference, it's therefore not a good idea to measure using addition.
            # Here, we instead divide the expected result with the result in order to obtain ~1. We then check that the
            # result is held between bounds: 1 - TOLERANCE < expected_result / result < 1 + TOLERANCE
            _UpperCamelCase              =						torch.all((expected_slice / output[..., :3, :3]) >= 1 - TOLERANCE      )
            _UpperCamelCase              =						torch.all((expected_slice / output[..., :3, :3]) <= 1 + TOLERANCE      )

            self.assertTrue(lower_bound and upper_bound      )













from __future__ import annotations

import math

import numpy as np
from numpy.linalg import norm


def     SCREAMING_SNAKE_CASE			(      lowerCAmelCase    ,       lowerCAmelCase					):
       return math.sqrt(sum(pow(a - b    ,       2					) for a, b in zip(lowerCAmelCase    ,       lowerCAmelCase					)					)					)


def     SCREAMING_SNAKE_CASE			(      lowerCAmelCase    ,       lowerCAmelCase					):

       if dataset.ndim != value_array.ndim:
              _UpperCamelCase              =						(
                  '''Wrong input data\'s dimensions... '''
                  f'''dataset : {dataset.ndim}, value_array : {value_array.ndim}'''
              )
              raise ValueError(lowerCAmelCase					)

       try:
              if dataset.shape[1] != value_array.shape[1]:
                     _UpperCamelCase              =						(
                         '''Wrong input data\'s shape... '''
                         f'''dataset : {dataset.shape[1]}, value_array : {value_array.shape[1]}'''
                     )
                     raise ValueError(lowerCAmelCase					)
       except IndexError:
              if dataset.ndim != value_array.ndim:
                     raise TypeError('''Wrong shape'''					)

       if dataset.dtype != value_array.dtype:
              _UpperCamelCase              =						(
                  '''Input data have different datatype... '''
                  f'''dataset : {dataset.dtype}, value_array : {value_array.dtype}'''
              )
              raise TypeError(lowerCAmelCase					)

       _UpperCamelCase              =						[]

       for value in value_array:
              _UpperCamelCase              =						euclidean(lowerCAmelCase    ,       dataset[0]					)
              _UpperCamelCase              =						dataset[0].tolist()

              for dataset_value in dataset[1:]:
                     _UpperCamelCase              =						euclidean(lowerCAmelCase    ,       lowerCAmelCase					)

                     if dist > temp_dist:
                            _UpperCamelCase              =						temp_dist
                            _UpperCamelCase              =						dataset_value.tolist()

              answer.append([vector, dist]					)

       return answer


def     SCREAMING_SNAKE_CASE			(      lowerCAmelCase    ,       lowerCAmelCase					):
       return np.dot(lowerCAmelCase    ,       lowerCAmelCase					) / (norm(lowerCAmelCase					) * norm(lowerCAmelCase					))


if __name__ == "__main__":
       import doctest

       doctest.testmod()






