
from __future__ import annotations

import unittest

from transformers import MobileBertConfig, is_tf_available
from transformers.models.auto import get_values
from transformers.testing_utils import require_tf, slow

from ...test_configuration_common import ConfigTester
from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_tf_available():
					import tensorflow as tf

					from transformers import (
					    TF_MODEL_FOR_PRETRAINING_MAPPING,
					    TFMobileBertForMaskedLM,
					    TFMobileBertForMultipleChoice,
					    TFMobileBertForNextSentencePrediction,
					    TFMobileBertForPreTraining,
					    TFMobileBertForQuestionAnswering,
					    TFMobileBertForSequenceClassification,
					    TFMobileBertForTokenClassification,
					    TFMobileBertModel,
					)







@require_tf
class      A__							(     __magic_name__						,   __magic_name__						,   unittest.TestCase	):
				lowercase			    =  (
				    (
				        TFMobileBertModel,
				        TFMobileBertForMaskedLM,
				        TFMobileBertForNextSentencePrediction,
				        TFMobileBertForPreTraining,
				        TFMobileBertForQuestionAnswering,
				        TFMobileBertForSequenceClassification,
				        TFMobileBertForTokenClassification,
				        TFMobileBertForMultipleChoice,
				    )
				    if is_tf_available()
				    else ()
				)
				lowercase			    =  (
				    {
				        'feature-extraction': TFMobileBertModel,
				        'fill-mask': TFMobileBertForMaskedLM,
				        'question-answering': TFMobileBertForQuestionAnswering,
				        'text-classification': TFMobileBertForSequenceClassification,
				        'token-classification': TFMobileBertForTokenClassification,
				        'zero-shot': TFMobileBertForSequenceClassification,
				    }
				    if is_tf_available()
				    else {}
				)
				lowercase			    =  False
				lowercase			    =  False






				def 							_lowerCamelCase		(						self					:      int    ,     a					:      Union[str, Any]    ,     a					:      int    ,     a					:      Dict=False   ):






							'''simple docstring'''







							lowerCAmelCase__      :      List[str]         =		super()._prepare_for_class(a    ,     a    ,     return_labels=a   )

							if return_labels:
										if model_class in get_values(a   ):
													lowerCAmelCase__      :      int         =		tf.zeros(self.model_tester.batch_size    ,     dtype=tf.intaa   )

							return inputs_dict







				class      A__							(     __magic_name__	):






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






											'''simple docstring'''







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






								def 							_lowerCamelCase		(						self					:      int   ):






											'''simple docstring'''







											lowerCAmelCase__      :      str         =		ids_tensor([self.batch_size, self.seq_length]    ,     self.vocab_size   )

											lowerCAmelCase__      :      Dict         =		None
											if self.use_input_mask:
														lowerCAmelCase__      :      List[Any]         =		random_attention_mask([self.batch_size, self.seq_length]   )

											lowerCAmelCase__      :      int         =		None
											if self.use_token_type_ids:
														lowerCAmelCase__      :      Union[str, Any]         =		ids_tensor([self.batch_size, self.seq_length]    ,     self.type_vocab_size   )

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

											lowerCAmelCase__      :      Tuple         =		MobileBertConfig(
											    vocab_size=self.vocab_size    ,     hidden_size=self.hidden_size    ,     num_hidden_layers=self.num_hidden_layers    ,     num_attention_heads=self.num_attention_heads    ,     intermediate_size=self.intermediate_size    ,     hidden_act=self.hidden_act    ,     hidden_dropout_prob=self.hidden_dropout_prob    ,     attention_probs_dropout_prob=self.attention_probs_dropout_prob    ,     max_position_embeddings=self.max_position_embeddings    ,     type_vocab_size=self.type_vocab_size    ,     initializer_range=self.initializer_range    ,     embedding_size=self.embedding_size    ,     )

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






								def 							_lowerCamelCase		(						self					:      Optional[int]    ,     a					:      int    ,     a					:      str    ,     a					:      Optional[Any]    ,     a					:      Any    ,     a					:      Tuple    ,     a					:      int    ,     a					:      Union[str, Any]   ):






											'''simple docstring'''







											lowerCAmelCase__      :      Tuple         =		TFMobileBertModel(config=a   )
											lowerCAmelCase__      :      List[str]         =		{'input_ids': input_ids, 'attention_mask': input_mask, 'token_type_ids': token_type_ids}
											lowerCAmelCase__      :      List[Any]         =		model(a   )

											lowerCAmelCase__      :      str         =		[input_ids, input_mask]
											lowerCAmelCase__      :      Optional[Any]         =		model(a   )

											lowerCAmelCase__      :      Dict         =		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 							_lowerCamelCase		(						self					:      int    ,     a					:      Dict    ,     a					:      str    ,     a					:      int    ,     a					:      Any    ,     a					:      Union[str, Any]    ,     a					:      Optional[int]    ,     a					:      Optional[Any]   ):






											'''simple docstring'''







											lowerCAmelCase__      :      List[str]         =		TFMobileBertForMaskedLM(config=a   )
											lowerCAmelCase__      :      List[str]         =		{'input_ids': input_ids, 'attention_mask': input_mask, 'token_type_ids': token_type_ids}
											lowerCAmelCase__      :      Tuple         =		model(a   )
											self.parent.assertEqual(result.logits.shape    ,     (self.batch_size, self.seq_length, self.vocab_size)   )






								def 							_lowerCamelCase		(						self					:      str    ,     a					:      Optional[int]    ,     a					:      List[Any]    ,     a					:      int    ,     a					:      Union[str, Any]    ,     a					:      Union[str, Any]    ,     a					:      Union[str, Any]    ,     a					:      Tuple   ):






											'''simple docstring'''







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






								def 							_lowerCamelCase		(						self					:      int    ,     a					:      List[str]    ,     a					:      Optional[Any]    ,     a					:      List[Any]    ,     a					:      List[Any]    ,     a					:      Optional[int]    ,     a					:      List[str]    ,     a					:      Optional[int]   ):






											'''simple docstring'''







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






											'''simple docstring'''







											lowerCAmelCase__      :      Dict         =		self.num_labels
											lowerCAmelCase__      :      int         =		TFMobileBertForSequenceClassification(config=a   )
											lowerCAmelCase__      :      str         =		{'input_ids': input_ids, 'attention_mask': input_mask, 'token_type_ids': token_type_ids}
											lowerCAmelCase__      :      str         =		model(a   )
											self.parent.assertEqual(result.logits.shape    ,     (self.batch_size, self.num_labels)   )






								def 							_lowerCamelCase		(						self					:      Any    ,     a					:      str    ,     a					:      str    ,     a					:      Any    ,     a					:      Dict    ,     a					:      str    ,     a					:      Dict    ,     a					:      Dict   ):






											'''simple docstring'''







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






								def 							_lowerCamelCase		(						self					:      Optional[int]    ,     a					:      Optional[Any]    ,     a					:      str    ,     a					:      List[Any]    ,     a					:      Optional[int]    ,     a					:      Any    ,     a					:      List[Any]    ,     a					:      Any   ):






											'''simple docstring'''







											lowerCAmelCase__      :      List[str]         =		self.num_labels
											lowerCAmelCase__      :      List[Any]         =		TFMobileBertForTokenClassification(config=a   )
											lowerCAmelCase__      :      str         =		{'input_ids': input_ids, 'attention_mask': input_mask, 'token_type_ids': token_type_ids}
											lowerCAmelCase__      :      Union[str, Any]         =		model(a   )
											self.parent.assertEqual(result.logits.shape    ,     (self.batch_size, self.seq_length, self.num_labels)   )






								def 							_lowerCamelCase		(						self					:      str    ,     a					:      List[str]    ,     a					:      List[str]    ,     a					:      Tuple    ,     a					:      List[str]    ,     a					:      int    ,     a					:      Tuple    ,     a					:      Tuple   ):






											'''simple docstring'''







											lowerCAmelCase__      :      List[str]         =		TFMobileBertForQuestionAnswering(config=a   )
											lowerCAmelCase__      :      List[Any]         =		{'input_ids': input_ids, 'attention_mask': input_mask, 'token_type_ids': token_type_ids}
											lowerCAmelCase__      :      str         =		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 							_lowerCamelCase		(						self					:      List[Any]   ):






											'''simple docstring'''







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






				def 							_lowerCamelCase		(						self					:      int   ):






							'''simple docstring'''







							lowerCAmelCase__      :      Dict         =		TFMobileBertModelTest.TFMobileBertModelTester(self   )
							lowerCAmelCase__      :      List[Any]         =		ConfigTester(self    ,     config_class=a    ,     hidden_size=37   )






				def 							_lowerCamelCase		(						self					:      Union[str, Any]   ):






							'''simple docstring'''







							self.config_tester.run_common_tests()






				def 							_lowerCamelCase		(						self					:      Any   ):






							'''simple docstring'''







							lowerCAmelCase__      :      List[Any]         =		self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_mobilebert_model(*a   )






				def 							_lowerCamelCase		(						self					:      Union[str, Any]   ):






							'''simple docstring'''







							lowerCAmelCase__      :      Optional[Any]         =		self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_mobilebert_for_masked_lm(*a   )






				def 							_lowerCamelCase		(						self					:      Optional[int]   ):






							'''simple docstring'''







							lowerCAmelCase__      :      int         =		self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_mobilebert_for_multiple_choice(*a   )






				def 							_lowerCamelCase		(						self					:      Tuple   ):






							'''simple docstring'''







							lowerCAmelCase__      :      str         =		self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_mobilebert_for_next_sequence_prediction(*a   )






				def 							_lowerCamelCase		(						self					:      Optional[int]   ):






							'''simple docstring'''







							lowerCAmelCase__      :      Optional[int]         =		self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_mobilebert_for_pretraining(*a   )






				def 							_lowerCamelCase		(						self					:      int   ):






							'''simple docstring'''







							lowerCAmelCase__      :      Optional[Any]         =		self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_mobilebert_for_question_answering(*a   )






				def 							_lowerCamelCase		(						self					:      Tuple   ):






							'''simple docstring'''







							lowerCAmelCase__      :      str         =		self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_mobilebert_for_sequence_classification(*a   )






				def 							_lowerCamelCase		(						self					:      List[Any]   ):






							'''simple docstring'''







							lowerCAmelCase__      :      Optional[int]         =		self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_mobilebert_for_token_classification(*a   )






				@slow
				def 							_lowerCamelCase		(						self					:      int   ):






							'''simple docstring'''







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







@require_tf
class      A__							(     unittest.TestCase	):






				@slow
				def 							_lowerCamelCase		(						self					:      int   ):






							'''simple docstring'''







							lowerCAmelCase__      :      List[str]         =		TFMobileBertForPreTraining.from_pretrained('google/mobilebert-uncased'   )
							lowerCAmelCase__      :      Union[str, Any]         =		tf.constant([[0, 1, 2, 3, 4, 5]]   )
							lowerCAmelCase__      :      Any         =		model(a   )[0]

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

							lowerCAmelCase__      :      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   )

from typing import List, Union

from ..utils import (
    add_end_docstrings,
    is_tf_available,
    is_torch_available,
    is_vision_available,
    logging,
    requires_backends,
)
from .base import PIPELINE_INIT_ARGS, Pipeline


if is_vision_available():
					from PIL import Image

					from ..image_utils import load_image

if is_tf_available():
					import tensorflow as tf

					from ..models.auto.modeling_tf_auto import TF_MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING
					from ..tf_utils import stable_softmax

if is_torch_available():
					from ..models.auto.modeling_auto import MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING

lowerCamelCase__          =		logging.get_logger(__name__)







@add_end_docstrings(__magic_name__	)
class      A__							(     __magic_name__	):






				def __init__(						self					:      int    ,     *a					:      Dict    ,     **a					:      Union[str, Any]   ):






							'''simple docstring'''







							super().__init__(*a    ,     **a   )
							requires_backends(self    ,     'vision'   )
							self.check_model_type(
							    TF_MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING
							    if self.framework == 'tf'
							    else MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING   )






				def 							_lowerCamelCase		(						self					:      Dict    ,     a					:      List[str]=None   ):






							'''simple docstring'''







							lowerCAmelCase__      :      Any         =		{}
							if top_k is not None:
										lowerCAmelCase__      :      Tuple         =		top_k
							return {}, {}, postprocess_params






				def __call__(						self					:      Any    ,     a					:      Union[str, List[str], "Image.Image", List["Image.Image"]]    ,     **a					:      List[Any]   ):






							'''simple docstring'''







							return super().__call__(a    ,     **a   )






				def 							_lowerCamelCase		(						self					:      Any    ,     a					:      Optional[Any]   ):






							'''simple docstring'''







							lowerCAmelCase__      :      List[Any]         =		load_image(a   )
							lowerCAmelCase__      :      Optional[int]         =		self.image_processor(images=a    ,     return_tensors=self.framework   )
							return model_inputs






				def 							_lowerCamelCase		(						self					:      Optional[int]    ,     a					:      List[str]   ):






							'''simple docstring'''







							lowerCAmelCase__      :      Optional[Any]         =		self.model(**a   )
							return model_outputs






				def 							_lowerCamelCase		(						self					:      Optional[Any]    ,     a					:      List[Any]    ,     a					:      List[Any]=5   ):






							'''simple docstring'''







							if top_k > self.model.config.num_labels:
										lowerCAmelCase__      :      Optional[int]         =		self.model.config.num_labels

							if self.framework == "pt":
										lowerCAmelCase__      :      List[Any]         =		model_outputs.logits.softmax(-1   )[0]
										lowerCAmelCase__      ,				lowerCAmelCase__      :      Optional[int]         =		probs.topk(a   )
							elif self.framework == "tf":
										lowerCAmelCase__      :      Any         =		stable_softmax(model_outputs.logits    ,     axis=-1   )[0]
										lowerCAmelCase__      :      Any         =		tf.math.top_k(a    ,     k=a   )
										lowerCAmelCase__      ,				lowerCAmelCase__      :      int         =		topk.values.numpy(), topk.indices.numpy()
							else:
										raise ValueError(f'''Unsupported framework: {self.framework}'''   )

							lowerCAmelCase__      :      List[Any]         =		scores.tolist()
							lowerCAmelCase__      :      List[str]         =		ids.tolist()
							return [{"score": score, "label": self.model.config.idalabel[_id]} for score, _id in zip(a    ,     a   )]