




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=1_3	,    _A=7	,    _A=True	,    _A=True	,    _A=True	,    _A=True	,    _A=9_9	,    _A=6_4	,    _A=3_2	,    _A=5	,    _A=4	,    _A=3_7	,    _A="gelu"	,    _A=0.1	,    _A=0.1	,    _A=5_1_2	,    _A=1_6	,    _A=2	,    _A=0.02	,    _A=3	,    _A=4	,    _A=None	,    ):




											"""simple docstring"""



											__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        __SCREAMING_SNAKE_CASE(     self    ):




											"""simple docstring"""



											__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        __SCREAMING_SNAKE_CASE(     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=lowercase_	,    initializer_range=self.initializer_range	,    )






						def        __SCREAMING_SNAKE_CASE(     self	,    _A	,    _A	,    _A	,    _A	,    _A	,    _A	,    _A    ):




											"""simple docstring"""



											__lowerCAmelCase            = MobileBertModel(config=lowercase_    )
											model.to(lowercase_    )
											model.eval()
											__lowerCAmelCase            = model(lowercase_	,    attention_mask=lowercase_	,    token_type_ids=lowercase_    )
											__lowerCAmelCase            = model(lowercase_	,    token_type_ids=lowercase_    )
											__lowerCAmelCase            = model(lowercase_    )

											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        __SCREAMING_SNAKE_CASE(     self	,    _A	,    _A	,    _A	,    _A	,    _A	,    _A	,    _A    ):




											"""simple docstring"""



											__lowerCAmelCase            = MobileBertForMaskedLM(config=lowercase_    )
											model.to(lowercase_    )
											model.eval()
											__lowerCAmelCase            = model(lowercase_	,    attention_mask=lowercase_	,    token_type_ids=lowercase_	,    labels=lowercase_    )
											self.parent.assertEqual(result.logits.shape	,    (self.batch_size, self.seq_length, self.vocab_size)    )






						def        __SCREAMING_SNAKE_CASE(     self	,    _A	,    _A	,    _A	,    _A	,    _A	,    _A	,    _A    ):




											"""simple docstring"""



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






						def        __SCREAMING_SNAKE_CASE(     self	,    _A	,    _A	,    _A	,    _A	,    _A	,    _A	,    _A    ):




											"""simple docstring"""



											__lowerCAmelCase            = MobileBertForPreTraining(config=lowercase_    )
											model.to(lowercase_    )
											model.eval()
											__lowerCAmelCase            = model(
											    lowercase_	,    attention_mask=lowercase_	,    token_type_ids=lowercase_	,    labels=lowercase_	,    next_sentence_label=lowercase_	,    )
											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        __SCREAMING_SNAKE_CASE(     self	,    _A	,    _A	,    _A	,    _A	,    _A	,    _A	,    _A    ):




											"""simple docstring"""



											__lowerCAmelCase            = MobileBertForQuestionAnswering(config=lowercase_    )
											model.to(lowercase_    )
											model.eval()
											__lowerCAmelCase            = model(
											    lowercase_	,    attention_mask=lowercase_	,    token_type_ids=lowercase_	,    start_positions=lowercase_	,    end_positions=lowercase_	,    )
											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        __SCREAMING_SNAKE_CASE(     self	,    _A	,    _A	,    _A	,    _A	,    _A	,    _A	,    _A    ):




											"""simple docstring"""



											__lowerCAmelCase            = self.num_labels
											__lowerCAmelCase            = MobileBertForSequenceClassification(lowercase_    )
											model.to(lowercase_    )
											model.eval()
											__lowerCAmelCase            = model(lowercase_	,    attention_mask=lowercase_	,    token_type_ids=lowercase_	,    labels=lowercase_    )
											self.parent.assertEqual(result.logits.shape	,    (self.batch_size, self.num_labels)    )






						def        __SCREAMING_SNAKE_CASE(     self	,    _A	,    _A	,    _A	,    _A	,    _A	,    _A	,    _A    ):




											"""simple docstring"""



											__lowerCAmelCase            = self.num_labels
											__lowerCAmelCase            = MobileBertForTokenClassification(config=lowercase_    )
											model.to(lowercase_    )
											model.eval()
											__lowerCAmelCase            = model(lowercase_	,    attention_mask=lowercase_	,    token_type_ids=lowercase_	,    labels=lowercase_    )
											self.parent.assertEqual(result.logits.shape	,    (self.batch_size, self.seq_length, self.num_labels)    )






						def        __SCREAMING_SNAKE_CASE(     self	,    _A	,    _A	,    _A	,    _A	,    _A	,    _A	,    _A    ):




											"""simple docstring"""



											__lowerCAmelCase            = self.num_choices
											__lowerCAmelCase            = MobileBertForMultipleChoice(config=lowercase_    )
											model.to(lowercase_    )
											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(
											    lowercase_	,    attention_mask=lowercase_	,    token_type_ids=lowercase_	,    labels=lowercase_	,    )
											self.parent.assertEqual(result.logits.shape	,    (self.batch_size, self.num_choices)    )






						def        __SCREAMING_SNAKE_CASE(     self    ):




											"""simple docstring"""



											__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     a__					(      snake_case__				,       snake_case__				,       unittest.TestCase   ):
						_a							:					Optional[Any]      =					(
						    (
						        MobileBertModel,
						        MobileBertForMaskedLM,
						        MobileBertForMultipleChoice,
						        MobileBertForNextSentencePrediction,
						        MobileBertForPreTraining,
						        MobileBertForQuestionAnswering,
						        MobileBertForSequenceClassification,
						        MobileBertForTokenClassification,
						    )
						    if is_torch_available()
						    else ()
						)
						_a							:					Tuple      =					(
						    {
						        """feature-extraction""": MobileBertModel,
						        """fill-mask""": MobileBertForMaskedLM,
						        """question-answering""": MobileBertForQuestionAnswering,
						        """text-classification""": MobileBertForSequenceClassification,
						        """token-classification""": MobileBertForTokenClassification,
						        """zero-shot""": MobileBertForSequenceClassification,
						    }
						    if is_torch_available()
						    else {}
						)
						_a							:					Union[str, Any]      =					True






						def        __SCREAMING_SNAKE_CASE(     self	,    _A	,    _A	,    _A=False    ):




											"""simple docstring"""



											__lowerCAmelCase            = super()._prepare_for_class(lowercase_	,    lowercase_	,    return_labels=lowercase_    )

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






						def        __SCREAMING_SNAKE_CASE(     self    ):




											"""simple docstring"""



											__lowerCAmelCase            = MobileBertModelTester(self    )
											__lowerCAmelCase            = ConfigTester(self	,    config_class=lowercase_	,    hidden_size=3_7    )






						def        __SCREAMING_SNAKE_CASE(     self    ):




											"""simple docstring"""



											self.config_tester.run_common_tests()






						def        __SCREAMING_SNAKE_CASE(     self    ):




											"""simple docstring"""



											__lowerCAmelCase            = self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_mobilebert_model(*lowercase_    )






						def        __SCREAMING_SNAKE_CASE(     self    ):




											"""simple docstring"""



											__lowerCAmelCase            = self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_mobilebert_for_masked_lm(*lowercase_    )






						def        __SCREAMING_SNAKE_CASE(     self    ):




											"""simple docstring"""



											__lowerCAmelCase            = self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_mobilebert_for_multiple_choice(*lowercase_    )






						def        __SCREAMING_SNAKE_CASE(     self    ):




											"""simple docstring"""



											__lowerCAmelCase            = self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_mobilebert_for_next_sequence_prediction(*lowercase_    )






						def        __SCREAMING_SNAKE_CASE(     self    ):




											"""simple docstring"""



											__lowerCAmelCase            = self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_mobilebert_for_pretraining(*lowercase_    )






						def        __SCREAMING_SNAKE_CASE(     self    ):




											"""simple docstring"""



											__lowerCAmelCase            = self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_mobilebert_for_question_answering(*lowercase_    )






						def        __SCREAMING_SNAKE_CASE(     self    ):




											"""simple docstring"""



											__lowerCAmelCase            = self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_mobilebert_for_sequence_classification(*lowercase_    )






						def        __SCREAMING_SNAKE_CASE(     self    ):




											"""simple docstring"""



											__lowerCAmelCase            = self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_mobilebert_for_token_classification(*lowercase_    )







def 			_a   (		SCREAMING_SNAKE_CASE_							:       int  ):

					'''simple docstring'''





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


UpperCamelCase__					    =    1E-3


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






						@slow
						def        __SCREAMING_SNAKE_CASE(     self    ):




											"""simple docstring"""



											__lowerCAmelCase            = MobileBertModel.from_pretrained("google/mobilebert-uncased"    ).to(lowercase_    )
											__lowerCAmelCase            = _long_tensor([[1_0_1, 7_1_1_0, 1_0_0_5, 1_0_5_6, 2_0_2_3, 1_1_3_3_3, 1_7_4_1_3, 1_0_2_9, 1_0_2]]    )
											with torch.no_grad():
																__lowerCAmelCase            = model(lowercase_    )[0]
											__lowerCAmelCase            = torch.Size((1, 9, 5_1_2)    )
											self.assertEqual(output.shape	,    lowercase_    )
											__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=lowercase_	,    )

											# 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    )










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






											@staticmethod
											def        __SCREAMING_SNAKE_CASE(     *_A	,    **_A    ):




																"""simple docstring"""



																pass
@is_pipeline_test
@require_torch
@require_vision
class     a__					(      unittest.TestCase   ):
						_a							:					Any      =					MODEL_FOR_VISUAL_QUESTION_ANSWERING_MAPPING






						def        __SCREAMING_SNAKE_CASE(     self	,    _A	,    _A	,    _A    ):




											"""simple docstring"""



											__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        __SCREAMING_SNAKE_CASE(     self	,    _A	,    _A    ):




											"""simple docstring"""



											__lowerCAmelCase            = vqa_pipeline(_A	,    top_k=1    )
											self.assertEqual(
											    _A	,    [
											        [{"score": ANY(_A    ), "answer": ANY(_A    )}],
											        [{"score": ANY(_A    ), "answer": ANY(_A    )}],
											    ]	,    )






						@require_torch
						def        __SCREAMING_SNAKE_CASE(     self    ):




											"""simple docstring"""



											__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=_A	,    question="How many cats are there?"	,    top_k=2    )
											self.assertEqual(
											    _A	,    [{"score": ANY(_A    ), "answer": ANY(_A    )}, {"score": ANY(_A    ), "answer": ANY(_A    )}]    )

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






						@slow
						@require_torch
						def        __SCREAMING_SNAKE_CASE(     self    ):




											"""simple docstring"""



											__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=_A	,    question=_A	,    top_k=2    )
											self.assertEqual(
											    nested_simplify(_A	,    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(_A	,    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(_A	,    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        __SCREAMING_SNAKE_CASE(     self    ):




											"""simple docstring"""



											pass




