





'''simple docstring'''






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 			UpperCAmelCase					:




      def __init__(      self      :			List[str]      ,      __snake_case      :			List[Any]      ,      __snake_case      :			Optional[Any]=13      ,      __snake_case      :			Optional[Any]=7      ,      __snake_case      :			int=True      ,      __snake_case      :			List[str]=True      ,      __snake_case      :			Optional[Any]=True      ,      __snake_case      :			Dict=True      ,      __snake_case      :			Optional[int]=99      ,      __snake_case      :			int=64      ,      __snake_case      :			Optional[int]=32      ,      __snake_case      :			Tuple=5      ,      __snake_case      :			int=4      ,      __snake_case      :			Optional[Any]=37      ,      __snake_case      :			Dict="gelu"      ,      __snake_case      :			Dict=0.1      ,      __snake_case      :			Tuple=0.1      ,      __snake_case      :			str=5_12      ,      __snake_case      :			List[str]=16      ,      __snake_case      :			Any=2      ,      __snake_case      :			Optional[int]=0.02      ,      __snake_case      :			List[str]=3      ,      __snake_case      :			Union[str, Any]=4      ,      __snake_case      :			Optional[Any]=None      ,      )    ->				Tuple:
            _lowerCAmelCase												=							parent
            _lowerCAmelCase												=							batch_size
            _lowerCAmelCase												=							seq_length
            _lowerCAmelCase												=							is_training
            _lowerCAmelCase												=							use_input_mask
            _lowerCAmelCase												=							use_token_type_ids
            _lowerCAmelCase												=							use_labels
            _lowerCAmelCase												=							vocab_size
            _lowerCAmelCase												=							hidden_size
            _lowerCAmelCase												=							embedding_size
            _lowerCAmelCase												=							num_hidden_layers
            _lowerCAmelCase												=							num_attention_heads
            _lowerCAmelCase												=							intermediate_size
            _lowerCAmelCase												=							hidden_act
            _lowerCAmelCase												=							hidden_dropout_prob
            _lowerCAmelCase												=							attention_probs_dropout_prob
            _lowerCAmelCase												=							max_position_embeddings
            _lowerCAmelCase												=							type_vocab_size
            _lowerCAmelCase												=							type_sequence_label_size
            _lowerCAmelCase												=							initializer_range
            _lowerCAmelCase												=							num_labels
            _lowerCAmelCase												=							num_choices
            _lowerCAmelCase												=							scope




      def     lowercase__   (      self      :			List[str]  )    ->				List[Any]:
            _lowerCAmelCase												=							ids_tensor([self.batch_size, self.seq_length]      ,      self.vocab_size  )

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

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

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

            _lowerCAmelCase												=							self.get_config()

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




      def     lowercase__   (      self      :			Dict  )    ->				List[Any]:
            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=__snake_case      ,      initializer_range=self.initializer_range      ,      )




      def     lowercase__   (      self      :			List[str]      ,      __snake_case      :			int      ,      __snake_case      :			List[Any]      ,      __snake_case      :			List[Any]      ,      __snake_case      :			Tuple      ,      __snake_case      :			Union[str, Any]      ,      __snake_case      :			Optional[int]      ,      __snake_case      :			int  )    ->				Dict:
            _lowerCAmelCase												=							MobileBertModel(config=__snake_case  )
            model.to(__snake_case  )
            model.eval()
            _lowerCAmelCase												=							model(__snake_case      ,      attention_mask=__snake_case      ,      token_type_ids=__snake_case  )
            _lowerCAmelCase												=							model(__snake_case      ,      token_type_ids=__snake_case  )
            _lowerCAmelCase												=							model(__snake_case  )

            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      :			Optional[int]      ,      __snake_case      :			Optional[Any]      ,      __snake_case      :			Dict      ,      __snake_case      :			List[Any]      ,      __snake_case      :			Tuple      ,      __snake_case      :			Any      ,      __snake_case      :			Tuple      ,      __snake_case      :			List[str]  )    ->				Any:
            _lowerCAmelCase												=							MobileBertForMaskedLM(config=__snake_case  )
            model.to(__snake_case  )
            model.eval()
            _lowerCAmelCase												=							model(__snake_case      ,      attention_mask=__snake_case      ,      token_type_ids=__snake_case      ,      labels=__snake_case  )
            self.parent.assertEqual(result.logits.shape      ,      (self.batch_size, self.seq_length, self.vocab_size)  )




      def     lowercase__   (      self      :			int      ,      __snake_case      :			Dict      ,      __snake_case      :			Union[str, Any]      ,      __snake_case      :			Optional[Any]      ,      __snake_case      :			Tuple      ,      __snake_case      :			int      ,      __snake_case      :			Dict      ,      __snake_case      :			int  )    ->				str:
            _lowerCAmelCase												=							MobileBertForNextSentencePrediction(config=__snake_case  )
            model.to(__snake_case  )
            model.eval()
            _lowerCAmelCase												=							model(
                __snake_case      ,      attention_mask=__snake_case      ,      token_type_ids=__snake_case      ,      labels=__snake_case      ,      )
            self.parent.assertEqual(result.logits.shape      ,      (self.batch_size, 2)  )




      def     lowercase__   (      self      :			Optional[Any]      ,      __snake_case      :			Dict      ,      __snake_case      :			int      ,      __snake_case      :			Union[str, Any]      ,      __snake_case      :			Optional[int]      ,      __snake_case      :			Union[str, Any]      ,      __snake_case      :			Optional[int]      ,      __snake_case      :			List[str]  )    ->				Optional[Any]:
            _lowerCAmelCase												=							MobileBertForPreTraining(config=__snake_case  )
            model.to(__snake_case  )
            model.eval()
            _lowerCAmelCase												=							model(
                __snake_case      ,      attention_mask=__snake_case      ,      token_type_ids=__snake_case      ,      labels=__snake_case      ,      next_sentence_label=__snake_case      ,      )
            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      :			List[Any]      ,      __snake_case      :			Optional[Any]      ,      __snake_case      :			int      ,      __snake_case      :			Dict      ,      __snake_case      :			Tuple      ,      __snake_case      :			Optional[Any]      ,      __snake_case      :			Optional[int]      ,      __snake_case      :			Tuple  )    ->				str:
            _lowerCAmelCase												=							MobileBertForQuestionAnswering(config=__snake_case  )
            model.to(__snake_case  )
            model.eval()
            _lowerCAmelCase												=							model(
                __snake_case      ,      attention_mask=__snake_case      ,      token_type_ids=__snake_case      ,      start_positions=__snake_case      ,      end_positions=__snake_case      ,      )
            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      ,      __snake_case      :			Dict      ,      __snake_case      :			Tuple      ,      __snake_case      :			Dict      ,      __snake_case      :			Tuple      ,      __snake_case      :			Optional[int]      ,      __snake_case      :			Dict      ,      __snake_case      :			Optional[Any]  )    ->				List[Any]:
            _lowerCAmelCase												=							self.num_labels
            _lowerCAmelCase												=							MobileBertForSequenceClassification(__snake_case  )
            model.to(__snake_case  )
            model.eval()
            _lowerCAmelCase												=							model(__snake_case      ,      attention_mask=__snake_case      ,      token_type_ids=__snake_case      ,      labels=__snake_case  )
            self.parent.assertEqual(result.logits.shape      ,      (self.batch_size, self.num_labels)  )




      def     lowercase__   (      self      :			Any      ,      __snake_case      :			Any      ,      __snake_case      :			List[str]      ,      __snake_case      :			int      ,      __snake_case      :			List[str]      ,      __snake_case      :			Optional[int]      ,      __snake_case      :			Any      ,      __snake_case      :			Tuple  )    ->				Tuple:
            _lowerCAmelCase												=							self.num_labels
            _lowerCAmelCase												=							MobileBertForTokenClassification(config=__snake_case  )
            model.to(__snake_case  )
            model.eval()
            _lowerCAmelCase												=							model(__snake_case      ,      attention_mask=__snake_case      ,      token_type_ids=__snake_case      ,      labels=__snake_case  )
            self.parent.assertEqual(result.logits.shape      ,      (self.batch_size, self.seq_length, self.num_labels)  )




      def     lowercase__   (      self      :			str      ,      __snake_case      :			Optional[Any]      ,      __snake_case      :			List[Any]      ,      __snake_case      :			List[Any]      ,      __snake_case      :			List[str]      ,      __snake_case      :			List[Any]      ,      __snake_case      :			Tuple      ,      __snake_case      :			Optional[Any]  )    ->				Optional[Any]:
            _lowerCAmelCase												=							self.num_choices
            _lowerCAmelCase												=							MobileBertForMultipleChoice(config=__snake_case  )
            model.to(__snake_case  )
            model.eval()
            _lowerCAmelCase												=							input_ids.unsqueeze(1  ).expand(-1      ,      self.num_choices      ,      -1  ).contiguous()
            _lowerCAmelCase												=							token_type_ids.unsqueeze(1  ).expand(-1      ,      self.num_choices      ,      -1  ).contiguous()
            _lowerCAmelCase												=							input_mask.unsqueeze(1  ).expand(-1      ,      self.num_choices      ,      -1  ).contiguous()
            _lowerCAmelCase												=							model(
                __snake_case      ,      attention_mask=__snake_case      ,      token_type_ids=__snake_case      ,      labels=__snake_case      ,      )
            self.parent.assertEqual(result.logits.shape      ,      (self.batch_size, self.num_choices)  )




      def     lowercase__   (      self      :			Optional[Any]  )    ->				List[str]:
            _lowerCAmelCase												=							self.prepare_config_and_inputs()
            (
                (
                _lowerCAmelCase
            )					,							(
                _lowerCAmelCase
            )					,							(
                _lowerCAmelCase
            )					,							(
                _lowerCAmelCase
            )					,							(
                _lowerCAmelCase
            )					,							(
                _lowerCAmelCase
            )					,							(
                _lowerCAmelCase
            )					,							
            )												=							config_and_inputs
            _lowerCAmelCase												=							{"""input_ids""": input_ids, """token_type_ids""": token_type_ids, """attention_mask""": input_mask}
            return config, inputs_dict




@require_torch
class 			UpperCAmelCase					(      snake_case_							,		snake_case_							,		unittest.TestCase       ):
      _lowercase:      Tuple											=       (
          (
              MobileBertModel,
              MobileBertForMaskedLM,
              MobileBertForMultipleChoice,
              MobileBertForNextSentencePrediction,
              MobileBertForPreTraining,
              MobileBertForQuestionAnswering,
              MobileBertForSequenceClassification,
              MobileBertForTokenClassification,
          )
          if is_torch_available()
          else ()
      )
      _lowercase:      Optional[Any]											=       (
          {
              '''feature-extraction''': MobileBertModel,
              '''fill-mask''': MobileBertForMaskedLM,
              '''question-answering''': MobileBertForQuestionAnswering,
              '''text-classification''': MobileBertForSequenceClassification,
              '''token-classification''': MobileBertForTokenClassification,
              '''zero-shot''': MobileBertForSequenceClassification,
          }
          if is_torch_available()
          else {}
      )
      _lowercase:      Optional[Any]											=       True




      def     lowercase__   (      self      :			List[Any]      ,      __snake_case      :			str      ,      __snake_case      :			List[Any]      ,      __snake_case      :			Any=False  )    ->				int:
            _lowerCAmelCase												=							super()._prepare_for_class(__snake_case      ,      __snake_case      ,      return_labels=__snake_case  )

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




      def     lowercase__   (      self      :			Tuple  )    ->				Dict:
            _lowerCAmelCase												=							MobileBertModelTester(self  )
            _lowerCAmelCase												=							ConfigTester(self      ,      config_class=__snake_case      ,      hidden_size=37  )




      def     lowercase__   (      self      :			Union[str, Any]  )    ->				Union[str, Any]:
            self.config_tester.run_common_tests()




      def     lowercase__   (      self      :			int  )    ->				Dict:
            _lowerCAmelCase												=							self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_mobilebert_model(*__snake_case  )




      def     lowercase__   (      self      :			str  )    ->				int:
            _lowerCAmelCase												=							self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_mobilebert_for_masked_lm(*__snake_case  )




      def     lowercase__   (      self      :			Dict  )    ->				List[str]:
            _lowerCAmelCase												=							self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_mobilebert_for_multiple_choice(*__snake_case  )




      def     lowercase__   (      self      :			Union[str, Any]  )    ->				Optional[int]:
            _lowerCAmelCase												=							self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_mobilebert_for_next_sequence_prediction(*__snake_case  )




      def     lowercase__   (      self      :			int  )    ->				int:
            _lowerCAmelCase												=							self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_mobilebert_for_pretraining(*__snake_case  )




      def     lowercase__   (      self      :			Union[str, Any]  )    ->				Dict:
            _lowerCAmelCase												=							self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_mobilebert_for_question_answering(*__snake_case  )




      def     lowercase__   (      self      :			Optional[Any]  )    ->				Dict:
            _lowerCAmelCase												=							self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_mobilebert_for_sequence_classification(*__snake_case  )




      def     lowercase__   (      self      :			Any  )    ->				Optional[int]:
            _lowerCAmelCase												=							self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_mobilebert_for_token_classification(*__snake_case  )




def        UpperCamelCase__				(		lowerCAmelCase  ):





      """simple docstring"""


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


A__    :       str              =1e-3





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




      @slow
      def     lowercase__   (      self      :			int  )    ->				str:
            _lowerCAmelCase												=							MobileBertModel.from_pretrained("""google/mobilebert-uncased"""  ).to(__snake_case  )
            _lowerCAmelCase												=							_long_tensor([[1_01, 71_10, 10_05, 10_56, 20_23, 1_13_33, 1_74_13, 10_29, 1_02]]  )
            with torch.no_grad():
                  _lowerCAmelCase												=							model(__snake_case  )[0]
            _lowerCAmelCase												=							torch.Size((1, 9, 5_12)  )
            self.assertEqual(output.shape      ,      __snake_case  )
            _lowerCAmelCase												=							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=__snake_case      ,      )

            # 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
            _lowerCAmelCase												=							torch.all((expected_slice / output[..., :3, :3]) >= 1 - TOLERANCE  )
            _lowerCAmelCase												=							torch.all((expected_slice / output[..., :3, :3]) <= 1 + TOLERANCE  )

            self.assertTrue(lower_bound and upper_bound  )













'''simple docstring'''






import unittest

from transformers import MODEL_FOR_VISUAL_QUESTION_ANSWERING_MAPPING, is_vision_available
from transformers.pipelines import pipeline
from transformers.testing_utils import (
    is_pipeline_test,
    nested_simplify,
    require_tf,
    require_torch,
    require_vision,
    slow,
)

from .test_pipelines_common import ANY


if is_vision_available():
      from PIL import Image
else:




      class 			UpperCAmelCase					:




            @staticmethod
            def     lowercase__   (      *__snake_case      :			Optional[Any]      ,      **__snake_case      :			Any  )    ->				Tuple:
                  pass




@is_pipeline_test
@require_torch
@require_vision
class 			UpperCAmelCase					(      unittest.TestCase       ):
      _lowercase:      Union[str, Any]											=       MODEL_FOR_VISUAL_QUESTION_ANSWERING_MAPPING




      def     lowercase__   (      self      :			List[str]      ,      __snake_case      :			Optional[Any]      ,      __snake_case      :			str      ,      __snake_case      :			List[str]  )    ->				int:
            _lowerCAmelCase												=							pipeline("""visual-question-answering"""      ,      model="""hf-internal-testing/tiny-vilt-random-vqa"""  )
            _lowerCAmelCase												=							[
                {
                    """image""": Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png"""  ),
                    """question""": """How many cats are there?""",
                },
                {
                    """image""": """./tests/fixtures/tests_samples/COCO/000000039769.png""",
                    """question""": """How many cats are there?""",
                },
            ]
            return vqa_pipeline, examples




      def     lowercase__   (      self      :			Any      ,      __snake_case      :			List[Any]      ,      __snake_case      :			List[Any]  )    ->				Union[str, Any]:
            _lowerCAmelCase												=							vqa_pipeline(__snake_case      ,      top_k=1  )
            self.assertEqual(
                __snake_case      ,      [
                    [{"""score""": ANY(__snake_case  ), """answer""": ANY(__snake_case  )}],
                    [{"""score""": ANY(__snake_case  ), """answer""": ANY(__snake_case  )}],
                ]      ,      )




      @require_torch
      def     lowercase__   (      self      :			str  )    ->				int:
            _lowerCAmelCase												=							pipeline("""visual-question-answering"""      ,      model="""hf-internal-testing/tiny-vilt-random-vqa"""  )
            _lowerCAmelCase												=							"""./tests/fixtures/tests_samples/COCO/000000039769.png"""
            _lowerCAmelCase												=							"""How many cats are there?"""

            _lowerCAmelCase												=							vqa_pipeline(image=__snake_case      ,      question="""How many cats are there?"""      ,      top_k=2  )
            self.assertEqual(
                __snake_case      ,      [{"""score""": ANY(__snake_case  ), """answer""": ANY(__snake_case  )}, {"""score""": ANY(__snake_case  ), """answer""": ANY(__snake_case  )}]  )

            _lowerCAmelCase												=							vqa_pipeline({"""image""": image, """question""": question}      ,      top_k=2  )
            self.assertEqual(
                __snake_case      ,      [{"""score""": ANY(__snake_case  ), """answer""": ANY(__snake_case  )}, {"""score""": ANY(__snake_case  ), """answer""": ANY(__snake_case  )}]  )




      @slow
      @require_torch
      def     lowercase__   (      self      :			List[Any]  )    ->				List[str]:
            _lowerCAmelCase												=							pipeline("""visual-question-answering"""      ,      model="""dandelin/vilt-b32-finetuned-vqa"""  )
            _lowerCAmelCase												=							"""./tests/fixtures/tests_samples/COCO/000000039769.png"""
            _lowerCAmelCase												=							"""How many cats are there?"""

            _lowerCAmelCase												=							vqa_pipeline(image=__snake_case      ,      question=__snake_case      ,      top_k=2  )
            self.assertEqual(
                nested_simplify(__snake_case      ,      decimals=4  )      ,      [{"""score""": 0.87_99, """answer""": """2"""}, {"""score""": 0.2_96, """answer""": """1"""}]  )

            _lowerCAmelCase												=							vqa_pipeline({"""image""": image, """question""": question}      ,      top_k=2  )
            self.assertEqual(
                nested_simplify(__snake_case      ,      decimals=4  )      ,      [{"""score""": 0.87_99, """answer""": """2"""}, {"""score""": 0.2_96, """answer""": """1"""}]  )

            _lowerCAmelCase												=							vqa_pipeline(
                [{"""image""": image, """question""": question}, {"""image""": image, """question""": question}]      ,      top_k=2  )
            self.assertEqual(
                nested_simplify(__snake_case      ,      decimals=4  )      ,      [[{"""score""": 0.87_99, """answer""": """2"""}, {"""score""": 0.2_96, """answer""": """1"""}]] * 2      ,      )




      @require_tf
      @unittest.skip("""Visual question answering not implemented in TF"""  )
      def     lowercase__   (      self      :			List[str]  )    ->				Union[str, Any]:
            pass






