


'''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 						__UpperCamelCase	(						lowercase__						,  lowercase__						,  unittest.TestCase							):
							lowercase			:		List[str]   		=  (
							    (
							        TFMobileBertModel,
							        TFMobileBertForMaskedLM,
							        TFMobileBertForNextSentencePrediction,
							        TFMobileBertForPreTraining,
							        TFMobileBertForQuestionAnswering,
							        TFMobileBertForSequenceClassification,
							        TFMobileBertForTokenClassification,
							        TFMobileBertForMultipleChoice,
							    )
							    if is_tf_available()
							    else ()
							)
							lowercase			:		Optional[Any]   		=  (
							    {
							        'feature-extraction': TFMobileBertModel,
							        'fill-mask': TFMobileBertForMaskedLM,
							        'question-answering': TFMobileBertForQuestionAnswering,
							        'text-classification': TFMobileBertForSequenceClassification,
							        'token-classification': TFMobileBertForTokenClassification,
							        'zero-shot': TFMobileBertForSequenceClassification,
							    }
							    if is_tf_available()
							    else {}
							)
							lowercase			:		Optional[Any]   		=  False
							lowercase			:		List[str]   		=  False





							def 		a__   (  self		:List[str]  ,_UpperCamelCase		:Optional[int]  ,_UpperCamelCase		:Optional[Any]  ,_UpperCamelCase		:str=False			):
									snake_case_		:							Dict			       =						super()._prepare_for_class(_UpperCamelCase  ,_UpperCamelCase  ,return_labels=_UpperCamelCase			)

									if return_labels:
											if model_class in get_values(_UpperCamelCase			):
													snake_case_		:							Dict			       =						tf.zeros(self.model_tester.batch_size  ,dtype=tf.intaa			)

									return inputs_dict
							class 						__UpperCamelCase	(						lowercase__							):





														def __init__(  self		:Optional[int]  ,_UpperCamelCase		:Union[str, Any]  ,_UpperCamelCase		:Tuple=1_3  ,_UpperCamelCase		:Optional[int]=7  ,_UpperCamelCase		:str=True  ,_UpperCamelCase		:List[Any]=True  ,_UpperCamelCase		:Dict=True  ,_UpperCamelCase		:Dict=True  ,_UpperCamelCase		:Dict=9_9  ,_UpperCamelCase		:Optional[Any]=3_2  ,_UpperCamelCase		:List[str]=3_2  ,_UpperCamelCase		:Optional[Any]=2  ,_UpperCamelCase		:Optional[Any]=4  ,_UpperCamelCase		:int=3_7  ,_UpperCamelCase		:int="gelu"  ,_UpperCamelCase		:Tuple=0.1  ,_UpperCamelCase		:List[str]=0.1  ,_UpperCamelCase		:List[str]=5_1_2  ,_UpperCamelCase		:str=1_6  ,_UpperCamelCase		:List[Any]=2  ,_UpperCamelCase		:List[str]=0.02  ,_UpperCamelCase		:Optional[Any]=3  ,_UpperCamelCase		:List[Any]=4  ,_UpperCamelCase		:Union[str, Any]=None  ,):
																snake_case_		:							List[Any]			       =						parent
																snake_case_		:							Dict			       =						batch_size
																snake_case_		:							Optional[Any]			       =						seq_length
																snake_case_		:							Optional[int]			       =						is_training
																snake_case_		:							List[Any]			       =						use_input_mask
																snake_case_		:							str			       =						use_token_type_ids
																snake_case_		:							List[Any]			       =						use_labels
																snake_case_		:							Union[str, Any]			       =						vocab_size
																snake_case_		:							List[str]			       =						hidden_size
																snake_case_		:							List[str]			       =						num_hidden_layers
																snake_case_		:							Dict			       =						num_attention_heads
																snake_case_		:							Any			       =						intermediate_size
																snake_case_		:							Optional[int]			       =						hidden_act
																snake_case_		:							Tuple			       =						hidden_dropout_prob
																snake_case_		:							List[Any]			       =						attention_probs_dropout_prob
																snake_case_		:							Optional[Any]			       =						max_position_embeddings
																snake_case_		:							Optional[Any]			       =						type_vocab_size
																snake_case_		:							Optional[Any]			       =						type_sequence_label_size
																snake_case_		:							List[str]			       =						initializer_range
																snake_case_		:							Optional[int]			       =						num_labels
																snake_case_		:							List[str]			       =						num_choices
																snake_case_		:							Dict			       =						scope
																snake_case_		:							List[Any]			       =						embedding_size





														def 		a__   (  self		:Any			):
																snake_case_		:							int			       =						ids_tensor([self.batch_size, self.seq_length]  ,self.vocab_size			)

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

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

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

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

																snake_case_		:							Any			       =						[input_ids, input_mask]
																snake_case_		:							Optional[int]			       =						model(_UpperCamelCase			)

																snake_case_		:							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 		a__   (  self		:int  ,_UpperCamelCase		:str  ,_UpperCamelCase		:Optional[Any]  ,_UpperCamelCase		:Optional[Any]  ,_UpperCamelCase		:Any  ,_UpperCamelCase		:Tuple  ,_UpperCamelCase		:Union[str, Any]  ,_UpperCamelCase		:Tuple			):
																snake_case_		:							Tuple			       =						TFMobileBertForMaskedLM(config=_UpperCamelCase			)
																snake_case_		:							Union[str, Any]			       =						{"""input_ids""": input_ids, """attention_mask""": input_mask, """token_type_ids""": token_type_ids}
																snake_case_		:							Any			       =						model(_UpperCamelCase			)
																self.parent.assertEqual(result.logits.shape  ,(self.batch_size, self.seq_length, self.vocab_size)			)





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





														def 		a__   (  self		:Optional[int]  ,_UpperCamelCase		:str  ,_UpperCamelCase		:Tuple  ,_UpperCamelCase		:Dict  ,_UpperCamelCase		:List[Any]  ,_UpperCamelCase		:str  ,_UpperCamelCase		:Tuple  ,_UpperCamelCase		:List[str]			):
																snake_case_		:							Union[str, Any]			       =						TFMobileBertForPreTraining(config=_UpperCamelCase			)
																snake_case_		:							Union[str, Any]			       =						{"""input_ids""": input_ids, """attention_mask""": input_mask, """token_type_ids""": token_type_ids}
																snake_case_		:							Union[str, 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 		a__   (  self		:Any  ,_UpperCamelCase		:Union[str, Any]  ,_UpperCamelCase		:Optional[Any]  ,_UpperCamelCase		:List[str]  ,_UpperCamelCase		:Optional[int]  ,_UpperCamelCase		:List[Any]  ,_UpperCamelCase		:List[Any]  ,_UpperCamelCase		:Union[str, Any]			):
																snake_case_		:							Optional[int]			       =						self.num_labels
																snake_case_		:							List[str]			       =						TFMobileBertForSequenceClassification(config=_UpperCamelCase			)
																snake_case_		:							int			       =						{"""input_ids""": input_ids, """attention_mask""": input_mask, """token_type_ids""": token_type_ids}
																snake_case_		:							int			       =						model(_UpperCamelCase			)
																self.parent.assertEqual(result.logits.shape  ,(self.batch_size, self.num_labels)			)





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





														def 		a__   (  self		:str  ,_UpperCamelCase		:List[Any]  ,_UpperCamelCase		:Optional[Any]  ,_UpperCamelCase		:str  ,_UpperCamelCase		:Optional[int]  ,_UpperCamelCase		:Optional[Any]  ,_UpperCamelCase		:List[str]  ,_UpperCamelCase		:Optional[Any]			):
																snake_case_		:							str			       =						self.num_labels
																snake_case_		:							Tuple			       =						TFMobileBertForTokenClassification(config=_UpperCamelCase			)
																snake_case_		:							Optional[Any]			       =						{"""input_ids""": input_ids, """attention_mask""": input_mask, """token_type_ids""": token_type_ids}
																snake_case_		:							Any			       =						model(_UpperCamelCase			)
																self.parent.assertEqual(result.logits.shape  ,(self.batch_size, self.seq_length, self.num_labels)			)





														def 		a__   (  self		:Optional[Any]  ,_UpperCamelCase		:List[str]  ,_UpperCamelCase		:Any  ,_UpperCamelCase		:Optional[int]  ,_UpperCamelCase		:Optional[int]  ,_UpperCamelCase		:int  ,_UpperCamelCase		:Union[str, Any]  ,_UpperCamelCase		:List[Any]			):
																snake_case_		:							Union[str, Any]			       =						TFMobileBertForQuestionAnswering(config=_UpperCamelCase			)
																snake_case_		:							List[str]			       =						{"""input_ids""": input_ids, """attention_mask""": input_mask, """token_type_ids""": token_type_ids}
																snake_case_		:							Optional[int]			       =						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 		a__   (  self		:Union[str, Any]			):
																snake_case_		:							str			       =						self.prepare_config_and_inputs()
																(
																    (
																    snake_case_
																)       ,      (
																    snake_case_
																)       ,      (
																    snake_case_
																)       ,      (
																    snake_case_
																)       ,      (
																    snake_case_
																)       ,      (
																    snake_case_
																)       ,      (
																    snake_case_
																)       ,      
																)		:							int			       =						config_and_inputs
																snake_case_		:							Any			       =						{"""input_ids""": input_ids, """token_type_ids""": token_type_ids, """attention_mask""": input_mask}
																return config, inputs_dict





							def 		a__   (  self		:Optional[Any]			):
									snake_case_		:							Any			       =						TFMobileBertModelTest.TFMobileBertModelTester(self			)
									snake_case_		:							Any			       =						ConfigTester(self  ,config_class=_UpperCamelCase  ,hidden_size=3_7			)





							def 		a__   (  self		:Union[str, Any]			):
									self.config_tester.run_common_tests()





							def 		a__   (  self		:int			):
									snake_case_		:							Any			       =						self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_mobilebert_model(*_UpperCamelCase			)





							def 		a__   (  self		:Union[str, Any]			):
									snake_case_		:							List[Any]			       =						self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_mobilebert_for_masked_lm(*_UpperCamelCase			)





							def 		a__   (  self		:Any			):
									snake_case_		:							int			       =						self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_mobilebert_for_multiple_choice(*_UpperCamelCase			)





							def 		a__   (  self		:str			):
									snake_case_		:							Tuple			       =						self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_mobilebert_for_next_sequence_prediction(*_UpperCamelCase			)





							def 		a__   (  self		:Union[str, Any]			):
									snake_case_		:							Dict			       =						self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_mobilebert_for_pretraining(*_UpperCamelCase			)





							def 		a__   (  self		:Tuple			):
									snake_case_		:							Tuple			       =						self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_mobilebert_for_question_answering(*_UpperCamelCase			)





							def 		a__   (  self		:List[Any]			):
									snake_case_		:							Dict			       =						self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_mobilebert_for_sequence_classification(*_UpperCamelCase			)





							def 		a__   (  self		:Optional[int]			):
									snake_case_		:							str			       =						self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_mobilebert_for_token_classification(*_UpperCamelCase			)





							@slow
							def 		a__   (  self		:Any			):
									# for model_name in TF_MOBILEBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
									for model_name in ["google/mobilebert-uncased"]:
											snake_case_		:							Any			       =						TFMobileBertModel.from_pretrained(_UpperCamelCase			)
											self.assertIsNotNone(_UpperCamelCase			)





@require_tf
class 						__UpperCamelCase	(						unittest.TestCase							):





							@slow
							def 		a__   (  self		:List[str]			):
									snake_case_		:							Any			       =						TFMobileBertForPreTraining.from_pretrained("""google/mobilebert-uncased"""			)
									snake_case_		:							Union[str, Any]			       =						tf.constant([[0, 1, 2, 3, 4, 5]]			)
									snake_case_		:							Union[str, Any]			       =						model(_UpperCamelCase			)[0]

									snake_case_		:							Dict			       =						[1, 6, 3_0_5_2_2]
									self.assertEqual(output.shape  ,_UpperCamelCase			)

									snake_case_		:							List[str]			       =						tf.constant(
									    [
									        [
									            [-4.5_91_95_47, -9.24_82_95, -9.64_52_56],
									            [-6.7_30_61_75, -6.44_02_84, -6.6_05_28_37],
									            [-7.2_74_35_06, -6.7_84_79_15, -6.02_46_73],
									        ]
									    ]			)
									tf.debugging.assert_near(output[:, :3, :3]  ,_UpperCamelCase  ,atol=1E-4			)



'''simple docstring'''






import tempfile
import unittest

from transformers import AutoModelForSeqaSeqLM, AutoTokenizer
from transformers.testing_utils import (
    is_torch_available,
    require_optimum,
    require_torch,
    slow,
)


if is_torch_available():
							import torch
@require_torch
@require_optimum
@slow
class 						__UpperCamelCase	(						unittest.TestCase							):





							def 		a__   (  self		:List[str]			):
									snake_case_		:							Union[str, Any]			       =						"""hf-internal-testing/tiny-random-t5"""
									snake_case_		:							str			       =						AutoTokenizer.from_pretrained(_UpperCamelCase			)
									snake_case_		:							str			       =						AutoModelForSeqaSeqLM.from_pretrained(_UpperCamelCase			)

									snake_case_		:							List[str]			       =						tokenizer("""This is me"""  ,return_tensors="""pt"""			)

									snake_case_		:							List[str]			       =						model.to_bettertransformer()

									self.assertTrue(any("""BetterTransformer""" in mod.__class__.__name__ for _, mod in model.named_modules()			)			)

									snake_case_		:							List[str]			       =						model.generate(**_UpperCamelCase			)

									snake_case_		:							Dict			       =						model.reverse_bettertransformer()

									self.assertFalse(any("""BetterTransformer""" in mod.__class__.__name__ for _, mod in model.named_modules()			)			)

									with tempfile.TemporaryDirectory() as tmpdirname:
											model.save_pretrained(_UpperCamelCase			)

											snake_case_		:							List[Any]			       =						AutoModelForSeqaSeqLM.from_pretrained(_UpperCamelCase			)

											self.assertFalse(
											    any("""BetterTransformer""" in mod.__class__.__name__ for _, mod in model_reloaded.named_modules()			)			)

											snake_case_		:							Optional[Any]			       =						model_reloaded.generate(**_UpperCamelCase			)
											self.assertTrue(torch.allclose(_UpperCamelCase  ,_UpperCamelCase			)			)





							def 		a__   (  self		:Union[str, Any]			):
									snake_case_		:							Optional[Any]			       =						"""hf-internal-testing/tiny-random-t5"""
									snake_case_		:							str			       =						AutoModelForSeqaSeqLM.from_pretrained(_UpperCamelCase			)

									snake_case_		:							List[Any]			       =						model.to_bettertransformer()

									with tempfile.TemporaryDirectory() as tmpdirname:
											with self.assertRaises(_UpperCamelCase			):
													model.save_pretrained(_UpperCamelCase			)

											snake_case_		:							Any			       =						model.reverse_bettertransformer()
											model.save_pretrained(_UpperCamelCase			)