




"""simple docstring"""


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    :Optional[int]     				=     (
			    (
			        TFMobileBertModel,
			        TFMobileBertForMaskedLM,
			        TFMobileBertForNextSentencePrediction,
			        TFMobileBertForPreTraining,
			        TFMobileBertForQuestionAnswering,
			        TFMobileBertForSequenceClassification,
			        TFMobileBertForTokenClassification,
			        TFMobileBertForMultipleChoice,
			    )
			    if is_tf_available()
			    else ()
			)
			_lowerCamelCase    :str     				=     (
			    {
			        "feature-extraction": TFMobileBertModel,
			        "fill-mask": TFMobileBertForMaskedLM,
			        "question-answering": TFMobileBertForQuestionAnswering,
			        "text-classification": TFMobileBertForSequenceClassification,
			        "token-classification": TFMobileBertForTokenClassification,
			        "zero-shot": TFMobileBertForSequenceClassification,
			    }
			    if is_tf_available()
			    else {}
			)
			_lowerCamelCase    :int     				=     False
			_lowerCamelCase    :int     				=     False






			def 	_lowerCAmelCase   (		self       :		Dict   ,     UpperCamelCase       :		Union[str, Any]   ,     UpperCamelCase       :		Tuple   ,     UpperCamelCase       :		Optional[Any]=False   )			->						Tuple:

									"""simple docstring"""
									lowerCAmelCase__      :			Union[str, Any]   =	super()._prepare_for_class(UpperCamelCase   ,     UpperCamelCase   ,     return_labels=UpperCamelCase   )

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

									return inputs_dict


			class _lowerCamelCase    (					a_						):






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

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






						def 	_lowerCAmelCase   (		self       :		Dict   )			->						str:

												"""simple docstring"""
												lowerCAmelCase__      :			Optional[Any]   =	ids_tensor([self.batch_size, self.seq_length]   ,     self.vocab_size   )

												lowerCAmelCase__      :			int   =	None
												if self.use_input_mask:
																		lowerCAmelCase__      :			Tuple   =	random_attention_mask([self.batch_size, self.seq_length]   )

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

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

												lowerCAmelCase__      :			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 	_lowerCAmelCase   (		self       :		List[str]   ,     UpperCamelCase       :		Dict   ,     UpperCamelCase       :		Union[str, Any]   ,     UpperCamelCase       :		Optional[Any]   ,     UpperCamelCase       :		Dict   ,     UpperCamelCase       :		List[Any]   ,     UpperCamelCase       :		Any   ,     UpperCamelCase       :		List[str]   )			->						List[str]:

												"""simple docstring"""
												lowerCAmelCase__      :			List[str]   =	TFMobileBertModel(config=UpperCamelCase   )
												lowerCAmelCase__      :			Tuple   =	{"""input_ids""": input_ids, """attention_mask""": input_mask, """token_type_ids""": token_type_ids}
												lowerCAmelCase__      :			Dict   =	model(UpperCamelCase   )

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

												lowerCAmelCase__      :			Tuple   =	model(UpperCamelCase   )

												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       :		Optional[int]   ,     UpperCamelCase       :		int   ,     UpperCamelCase       :		List[Any]   ,     UpperCamelCase       :		Tuple   ,     UpperCamelCase       :		Tuple   ,     UpperCamelCase       :		Dict   ,     UpperCamelCase       :		int   ,     UpperCamelCase       :		int   )			->						str:

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






						def 	_lowerCAmelCase   (		self       :		List[Any]   ,     UpperCamelCase       :		str   ,     UpperCamelCase       :		int   ,     UpperCamelCase       :		Any   ,     UpperCamelCase       :		Tuple   ,     UpperCamelCase       :		Optional[Any]   ,     UpperCamelCase       :		str   ,     UpperCamelCase       :		List[Any]   )			->						Union[str, Any]:

												"""simple docstring"""
												lowerCAmelCase__      :			Any   =	TFMobileBertForNextSentencePrediction(config=UpperCamelCase   )
												lowerCAmelCase__      :			Any   =	{"""input_ids""": input_ids, """attention_mask""": input_mask, """token_type_ids""": token_type_ids}
												lowerCAmelCase__      :			Union[str, Any]   =	model(UpperCamelCase   )
												self.parent.assertEqual(result.logits.shape   ,     (self.batch_size, 2)   )






						def 	_lowerCAmelCase   (		self       :		Tuple   ,     UpperCamelCase       :		Optional[int]   ,     UpperCamelCase       :		Optional[Any]   ,     UpperCamelCase       :		Tuple   ,     UpperCamelCase       :		Any   ,     UpperCamelCase       :		Optional[int]   ,     UpperCamelCase       :		int   ,     UpperCamelCase       :		Any   )			->						List[Any]:

												"""simple docstring"""
												lowerCAmelCase__      :			List[Any]   =	TFMobileBertForPreTraining(config=UpperCamelCase   )
												lowerCAmelCase__      :			List[Any]   =	{"""input_ids""": input_ids, """attention_mask""": input_mask, """token_type_ids""": token_type_ids}
												lowerCAmelCase__      :			Any   =	model(UpperCamelCase   )
												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       :		List[Any]   ,     UpperCamelCase       :		Union[str, Any]   ,     UpperCamelCase       :		Optional[Any]   ,     UpperCamelCase       :		Tuple   ,     UpperCamelCase       :		Tuple   ,     UpperCamelCase       :		Optional[int]   ,     UpperCamelCase       :		Any   ,     UpperCamelCase       :		str   )			->						List[Any]:

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






						def 	_lowerCAmelCase   (		self       :		Tuple   ,     UpperCamelCase       :		Dict   ,     UpperCamelCase       :		List[str]   ,     UpperCamelCase       :		Tuple   ,     UpperCamelCase       :		Union[str, Any]   ,     UpperCamelCase       :		Union[str, Any]   ,     UpperCamelCase       :		Any   ,     UpperCamelCase       :		Dict   )			->						List[Any]:

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






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

												"""simple docstring"""
												lowerCAmelCase__      :			Optional[int]   =	self.num_labels
												lowerCAmelCase__      :			Optional[int]   =	TFMobileBertForTokenClassification(config=UpperCamelCase   )
												lowerCAmelCase__      :			Union[str, Any]   =	{"""input_ids""": input_ids, """attention_mask""": input_mask, """token_type_ids""": token_type_ids}
												lowerCAmelCase__      :			str   =	model(UpperCamelCase   )
												self.parent.assertEqual(result.logits.shape   ,     (self.batch_size, self.seq_length, self.num_labels)   )






						def 	_lowerCAmelCase   (		self       :		int   ,     UpperCamelCase       :		int   ,     UpperCamelCase       :		Dict   ,     UpperCamelCase       :		Union[str, Any]   ,     UpperCamelCase       :		Any   ,     UpperCamelCase       :		Dict   ,     UpperCamelCase       :		int   ,     UpperCamelCase       :		Union[str, Any]   )			->						str:

												"""simple docstring"""
												lowerCAmelCase__      :			Dict   =	TFMobileBertForQuestionAnswering(config=UpperCamelCase   )
												lowerCAmelCase__      :			int   =	{"""input_ids""": input_ids, """attention_mask""": input_mask, """token_type_ids""": token_type_ids}
												lowerCAmelCase__      :			Any   =	model(UpperCamelCase   )
												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       :		Any   )			->						Any:

												"""simple docstring"""
												lowerCAmelCase__      :			str   =	self.prepare_config_and_inputs()
												(
												    lowerCAmelCase__   
												)      :			Any   =	config_and_inputs
												lowerCAmelCase__      :			List[str]   =	{"""input_ids""": input_ids, """token_type_ids""": token_type_ids, """attention_mask""": input_mask}
												return config, inputs_dict






			def 	_lowerCAmelCase   (		self       :		Optional[int]   )			->						List[str]:

									"""simple docstring"""
									lowerCAmelCase__      :			int   =	TFMobileBertModelTest.TFMobileBertModelTester(self   )
									lowerCAmelCase__      :			List[str]   =	ConfigTester(self   ,     config_class=UpperCamelCase   ,     hidden_size=37   )






			def 	_lowerCAmelCase   (		self       :		str   )			->						Optional[Any]:

									"""simple docstring"""
									self.config_tester.run_common_tests()






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

									"""simple docstring"""
									lowerCAmelCase__      :			int   =	self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_mobilebert_model(*UpperCamelCase   )






			def 	_lowerCAmelCase   (		self       :		int   )			->						List[str]:

									"""simple docstring"""
									lowerCAmelCase__      :			Optional[Any]   =	self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_mobilebert_for_masked_lm(*UpperCamelCase   )






			def 	_lowerCAmelCase   (		self       :		Dict   )			->						Optional[Any]:

									"""simple docstring"""
									lowerCAmelCase__      :			Any   =	self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_mobilebert_for_multiple_choice(*UpperCamelCase   )






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

									"""simple docstring"""
									lowerCAmelCase__      :			Tuple   =	self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_mobilebert_for_next_sequence_prediction(*UpperCamelCase   )






			def 	_lowerCAmelCase   (		self       :		List[Any]   )			->						List[str]:

									"""simple docstring"""
									lowerCAmelCase__      :			List[Any]   =	self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_mobilebert_for_pretraining(*UpperCamelCase   )






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

									"""simple docstring"""
									lowerCAmelCase__      :			List[Any]   =	self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_mobilebert_for_question_answering(*UpperCamelCase   )






			def 	_lowerCAmelCase   (		self       :		Any   )			->						str:

									"""simple docstring"""
									lowerCAmelCase__      :			Dict   =	self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_mobilebert_for_sequence_classification(*UpperCamelCase   )






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

									"""simple docstring"""
									lowerCAmelCase__      :			Union[str, Any]   =	self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_mobilebert_for_token_classification(*UpperCamelCase   )






			@slow
			def 	_lowerCAmelCase   (		self       :		str   )			->						Optional[int]:

									"""simple docstring"""
									# for model_name in TF_MOBILEBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
									for model_name in ["google/mobilebert-uncased"]:
															lowerCAmelCase__      :			int   =	TFMobileBertModel.from_pretrained(UpperCamelCase   )
															self.assertIsNotNone(UpperCamelCase   )






@require_tf
class _lowerCamelCase    (					unittest.TestCase						):






			@slow
			def 	_lowerCAmelCase   (		self       :		Optional[int]   )			->						List[str]:

									"""simple docstring"""
									lowerCAmelCase__      :			List[str]   =	TFMobileBertForPreTraining.from_pretrained("""google/mobilebert-uncased"""   )
									lowerCAmelCase__      :			int   =	tf.constant([[0, 1, 2, 3, 4, 5]]   )
									lowerCAmelCase__      :			Any   =	model(UpperCamelCase   )[0]

									lowerCAmelCase__      :			Union[str, Any]   =	[1, 6, 3_05_22]
									self.assertEqual(output.shape   ,     UpperCamelCase   )

									lowerCAmelCase__      :			Any   =	tf.constant(
									    [
									        [
									            [-4.591_9547, -9.24_8295, -9.64_5256],
									            [-6.730_6175, -6.44_0284, -6.605_2837],
									            [-7.274_3506, -6.784_7915, -6.02_4673],
									        ]
									    ]   )
									tf.debugging.assert_near(output[:, :3, :3]   ,     UpperCamelCase   ,     atol=1E-4   )











"""simple docstring"""


from argparse import ArgumentParser

from . import BaseTransformersCLICommand



def 	lowercase_				(			__UpperCAmelCase				)						->    List[str]:
      return DownloadCommand(args.model    ,		args.cache_dir    ,		args.force    ,		args.trust_remote_code				)


class _lowerCamelCase    (					a_						):






   @staticmethod
   def 	_lowerCAmelCase   (		UpperCamelCase       :		ArgumentParser   )			->						List[Any]:

         """simple docstring"""
         lowerCAmelCase__      :			int   =	parser.add_parser("""download"""   )
         download_parser.add_argument(
             """--cache-dir"""   ,     type=UpperCamelCase   ,     default=UpperCamelCase   ,     help="""Path to location to store the models"""   )
         download_parser.add_argument(
             """--force"""   ,     action="""store_true"""   ,     help="""Force the model to be download even if already in cache-dir"""   )
         download_parser.add_argument(
             """--trust-remote-code"""   ,     action="""store_true"""   ,     help="""Whether or not to allow for custom models defined on the Hub in their own modeling files. Use only if you've reviewed the code as it will execute on your local machine"""   ,     )
         download_parser.add_argument("""model"""   ,     type=UpperCamelCase   ,     help="""Name of the model to download"""   )
         download_parser.set_defaults(func=UpperCamelCase   )






   def __init__(		self       :		Optional[int]   ,     UpperCamelCase       :		str   ,     UpperCamelCase       :		str   ,     UpperCamelCase       :		bool   ,     UpperCamelCase       :		bool   )			->						Any:

         """simple docstring"""
         lowerCAmelCase__      :			int   =	model
         lowerCAmelCase__      :			Union[str, Any]   =	cache
         lowerCAmelCase__      :			Optional[int]   =	force
         lowerCAmelCase__      :			Dict   =	trust_remote_code






   def 	_lowerCAmelCase   (		self       :		int   )			->						Any:

         """simple docstring"""
         from ..models.auto import AutoModel, AutoTokenizer

         AutoModel.from_pretrained(
             self._model   ,     cache_dir=self._cache   ,     force_download=self._force   ,     trust_remote_code=self._trust_remote_code   )
         AutoTokenizer.from_pretrained(
             self._model   ,     cache_dir=self._cache   ,     force_download=self._force   ,     trust_remote_code=self._trust_remote_code   )





