


from __future__ import annotations

import unittest

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

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


if is_tf_available():
		import tensorflow as tf

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


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




						def  _snake_case					(	self						:   str   ,     __A						:   Dict   ,     __A						:   List[str]   ,     __A						:   Any=False						)      ->Union[str, Any]:






									"""simple docstring"""






									a__ :List[Any]						  =    super()._prepare_for_class(__A   ,     __A   ,     return_labels=__A						)

									if return_labels:
												if model_class in get_values(__A						):
															a__ :Union[str, Any]						  =    tf.zeros(self.model_tester.batch_size   ,     dtype=tf.intaa						)

									return inputs_dict


						class 					lowerCAmelCase_ (       _a):




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






															"""simple docstring"""






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




												def  _snake_case					(	self						:   List[str]						)      ->List[str]:






															"""simple docstring"""






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

															a__ :Optional[Any]						  =    None
															if self.use_input_mask:
																		a__ :Any						  =    random_attention_mask([self.batch_size, self.seq_length]						)

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

															a__ :Optional[int]						  =    None
															a__ :Optional[Any]						  =    None
															a__ :Any						  =    None
															if self.use_labels:
																		a__ :Optional[Any]						  =    ids_tensor([self.batch_size]   ,     self.type_sequence_label_size						)
																		a__ :Tuple						  =    ids_tensor([self.batch_size, self.seq_length]   ,     self.num_labels						)
																		a__ :Tuple						  =    ids_tensor([self.batch_size]   ,     self.num_choices						)

															a__ :List[str]						  =    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  _snake_case					(	self						:   int   ,     __A						:   Dict   ,     __A						:   List[str]   ,     __A						:   List[str]   ,     __A						:   str   ,     __A						:   str   ,     __A						:   List[str]   ,     __A						:   Dict						)      ->str:






															"""simple docstring"""






															a__ :Any						  =    TFMobileBertModel(config=__A						)
															a__ :Dict						  =    {"input_ids": input_ids, "attention_mask": input_mask, "token_type_ids": token_type_ids}
															a__ :Optional[Any]						  =    model(__A						)

															a__ :Optional[Any]						  =    [input_ids, input_mask]
															a__ :Optional[int]						  =    model(__A						)

															a__ :Optional[Any]						  =    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  _snake_case					(	self						:   int   ,     __A						:   str   ,     __A						:   Optional[int]   ,     __A						:   List[Any]   ,     __A						:   Tuple   ,     __A						:   Optional[Any]   ,     __A						:   str   ,     __A						:   Union[str, Any]						)      ->Tuple:






															"""simple docstring"""






															a__ :str						  =    TFMobileBertForMaskedLM(config=__A						)
															a__ :str						  =    {"input_ids": input_ids, "attention_mask": input_mask, "token_type_ids": token_type_ids}
															a__ :List[Any]						  =    model(__A						)
															self.parent.assertEqual(result.logits.shape   ,     (self.batch_size, self.seq_length, self.vocab_size)						)




												def  _snake_case					(	self						:   Optional[Any]   ,     __A						:   List[str]   ,     __A						:   Tuple   ,     __A						:   List[Any]   ,     __A						:   List[str]   ,     __A						:   str   ,     __A						:   Union[str, Any]   ,     __A						:   List[str]						)      ->Union[str, Any]:






															"""simple docstring"""






															a__ :int						  =    TFMobileBertForNextSentencePrediction(config=__A						)
															a__ :Union[str, Any]						  =    {"input_ids": input_ids, "attention_mask": input_mask, "token_type_ids": token_type_ids}
															a__ :Optional[int]						  =    model(__A						)
															self.parent.assertEqual(result.logits.shape   ,     (self.batch_size, 2)						)




												def  _snake_case					(	self						:   List[Any]   ,     __A						:   Any   ,     __A						:   Any   ,     __A						:   int   ,     __A						:   int   ,     __A						:   Union[str, Any]   ,     __A						:   List[str]   ,     __A						:   str						)      ->List[Any]:






															"""simple docstring"""






															a__ :Dict						  =    TFMobileBertForPreTraining(config=__A						)
															a__ :List[str]						  =    {"input_ids": input_ids, "attention_mask": input_mask, "token_type_ids": token_type_ids}
															a__ :Optional[int]						  =    model(__A						)
															self.parent.assertEqual(
															    result.prediction_logits.shape   ,     (self.batch_size, self.seq_length, self.vocab_size)						)
															self.parent.assertEqual(result.seq_relationship_logits.shape   ,     (self.batch_size, 2)						)




												def  _snake_case					(	self						:   int   ,     __A						:   int   ,     __A						:   str   ,     __A						:   Union[str, Any]   ,     __A						:   str   ,     __A						:   Tuple   ,     __A						:   int   ,     __A						:   Optional[Any]						)      ->Dict:






															"""simple docstring"""






															a__ :Optional[int]						  =    self.num_labels
															a__ :int						  =    TFMobileBertForSequenceClassification(config=__A						)
															a__ :Union[str, Any]						  =    {"input_ids": input_ids, "attention_mask": input_mask, "token_type_ids": token_type_ids}
															a__ :Dict						  =    model(__A						)
															self.parent.assertEqual(result.logits.shape   ,     (self.batch_size, self.num_labels)						)




												def  _snake_case					(	self						:   int   ,     __A						:   Dict   ,     __A						:   List[Any]   ,     __A						:   Any   ,     __A						:   str   ,     __A						:   Union[str, Any]   ,     __A						:   Dict   ,     __A						:   Optional[Any]						)      ->int:






															"""simple docstring"""






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




												def  _snake_case					(	self						:   str   ,     __A						:   Optional[Any]   ,     __A						:   Optional[Any]   ,     __A						:   Any   ,     __A						:   Dict   ,     __A						:   Any   ,     __A						:   int   ,     __A						:   Optional[Any]						)      ->Dict:






															"""simple docstring"""






															a__ :Tuple						  =    self.num_labels
															a__ :Any						  =    TFMobileBertForTokenClassification(config=__A						)
															a__ :Dict						  =    {"input_ids": input_ids, "attention_mask": input_mask, "token_type_ids": token_type_ids}
															a__ :Dict						  =    model(__A						)
															self.parent.assertEqual(result.logits.shape   ,     (self.batch_size, self.seq_length, self.num_labels)						)




												def  _snake_case					(	self						:   List[str]   ,     __A						:   int   ,     __A						:   List[str]   ,     __A						:   str   ,     __A						:   Tuple   ,     __A						:   Any   ,     __A						:   str   ,     __A						:   Union[str, Any]						)      ->Tuple:






															"""simple docstring"""






															a__ :int						  =    TFMobileBertForQuestionAnswering(config=__A						)
															a__ :Union[str, Any]						  =    {"input_ids": input_ids, "attention_mask": input_mask, "token_type_ids": token_type_ids}
															a__ :Dict						  =    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  _snake_case					(	self						:   Optional[int]						)      ->List[str]:






															"""simple docstring"""






															a__ :int						  =    self.prepare_config_and_inputs()
															(
															    (
															    a__
															)  ,  (
															    a__
															)  ,  (
															    a__
															)  ,  (
															    a__
															)  ,  (
															    a__
															)  ,  (
															    a__
															)  ,  (
															    a__
															)  ,  
															) :int						  =    config_and_inputs
															a__ :List[str]						  =    {"input_ids": input_ids, "token_type_ids": token_type_ids, "attention_mask": input_mask}
															return config, inputs_dict






						def  _snake_case					(	self						:   Optional[int]						)      ->Dict:






									"""simple docstring"""






									a__ :Dict						  =    TFMobileBertModelTest.TFMobileBertModelTester(self						)
									a__ :str						  =    ConfigTester(self   ,     config_class=__A   ,     hidden_size=37						)




						def  _snake_case					(	self						:   int						)      ->Optional[int]:






									"""simple docstring"""






									self.config_tester.run_common_tests()




						def  _snake_case					(	self						:   Union[str, Any]						)      ->Union[str, Any]:






									"""simple docstring"""






									a__ :Optional[Any]						  =    self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_mobilebert_model(*__A						)




						def  _snake_case					(	self						:   Dict						)      ->List[Any]:






									"""simple docstring"""






									a__ :List[Any]						  =    self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_mobilebert_for_masked_lm(*__A						)




						def  _snake_case					(	self						:   Union[str, Any]						)      ->List[Any]:






									"""simple docstring"""






									a__ :Optional[int]						  =    self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_mobilebert_for_multiple_choice(*__A						)




						def  _snake_case					(	self						:   List[Any]						)      ->List[Any]:






									"""simple docstring"""






									a__ :Optional[int]						  =    self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_mobilebert_for_next_sequence_prediction(*__A						)




						def  _snake_case					(	self						:   Dict						)      ->Tuple:






									"""simple docstring"""






									a__ :Any						  =    self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_mobilebert_for_pretraining(*__A						)




						def  _snake_case					(	self						:   Optional[Any]						)      ->Any:






									"""simple docstring"""






									a__ :Optional[int]						  =    self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_mobilebert_for_question_answering(*__A						)




						def  _snake_case					(	self						:   str						)      ->List[str]:






									"""simple docstring"""






									a__ :int						  =    self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_mobilebert_for_sequence_classification(*__A						)




						def  _snake_case					(	self						:   Any						)      ->Tuple:






									"""simple docstring"""






									a__ :str						  =    self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_mobilebert_for_token_classification(*__A						)






						@slow
						def  _snake_case					(	self						:   Optional[int]						)      ->List[Any]:






									"""simple docstring"""






									for model_name in ["google/mobilebert-uncased"]:
												a__ :str						  =    TFMobileBertModel.from_pretrained(__A						)
												self.assertIsNotNone(__A						)






@require_tf
class 					lowerCAmelCase_ (       unittest.TestCase):




						@slow
						def  _snake_case					(	self						:   Optional[Any]						)      ->List[str]:






									"""simple docstring"""






									a__ :List[Any]						  =    TFMobileBertForPreTraining.from_pretrained("google/mobilebert-uncased"						)
									a__ :Any						  =    tf.constant([[0, 1, 2, 3, 4, 5]]						)
									a__ :Optional[int]						  =    model(__A						)[0]

									a__ :Tuple						  =    [1, 6, 30522]
									self.assertEqual(output.shape   ,     __A						)

									a__ :int						  =    tf.constant(
									    [
									        [
									            [-4.5_919_547, -9.248_295, -9.645_256],
									            [-6.7_306_175, -6.440_284, -6.6_052_837],
									            [-7.2_743_506, -6.7_847_915, -6.024_673],
									        ]
									    ]						)
									tf.debugging.assert_near(output[:, :3, :3]   ,     __A   ,     atol=1E-4						)






# Copyright 2023 The HuggingFace Team. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from typing import TYPE_CHECKING

from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available


snake_case__			 =						{
    '''configuration_mgp_str''': ['''MGP_STR_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''MgpstrConfig'''],
    '''processing_mgp_str''': ['''MgpstrProcessor'''],
    '''tokenization_mgp_str''': ['''MgpstrTokenizer'''],
}

try:
		if not is_torch_available():
				raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
		pass
else:
		snake_case__			 =						[
		    '''MGP_STR_PRETRAINED_MODEL_ARCHIVE_LIST''',
		    '''MgpstrModel''',
		    '''MgpstrPreTrainedModel''',
		    '''MgpstrForSceneTextRecognition''',
		]

if TYPE_CHECKING:
		from .configuration_mgp_str import MGP_STR_PRETRAINED_CONFIG_ARCHIVE_MAP, MgpstrConfig
		from .processing_mgp_str import MgpstrProcessor
		from .tokenization_mgp_str import MgpstrTokenizer

		try:
				if not is_torch_available():
						raise OptionalDependencyNotAvailable()
		except OptionalDependencyNotAvailable:
				pass
		else:
				from .modeling_mgp_str import (
				    MGP_STR_PRETRAINED_MODEL_ARCHIVE_LIST,
				    MgpstrForSceneTextRecognition,
				    MgpstrModel,
				    MgpstrPreTrainedModel,
				)
else:
		import sys

		snake_case__			 =						_LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)


