



'''simple docstring'''



import inspect
import unittest
import warnings
from math import ceil, floor

from transformers import LevitConfig
from transformers.file_utils import cached_property, is_torch_available, is_vision_available
from transformers.models.auto import get_values
from transformers.testing_utils import require_torch, require_vision, slow, torch_device

from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
		import torch

		from transformers import (
		    MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING,
		    MODEL_MAPPING,
		    LevitForImageClassification,
		    LevitForImageClassificationWithTeacher,
		    LevitModel,
		)
		from transformers.models.levit.modeling_levit import LEVIT_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
		from PIL import Image

		from transformers import LevitImageProcessor





class _UpperCamelCase		(  a__		):





				'''simple docstring'''





				def 					__lowerCamelCase  (				self	:	Tuple):







											'''simple docstring'''

											__lowercase													=self.config_class(**self.inputs_dict)
											self.parent.assertTrue(hasattr(lowerCAmelCase__	,							'hidden_sizes'))
											self.parent.assertTrue(hasattr(lowerCAmelCase__	,							'num_attention_heads'))





class _UpperCamelCase		:





				'''simple docstring'''





				def __init__(				self	:	List[str]	,							_lowerCAmelCase	:	str	,							_lowerCAmelCase	:	List[Any]=1_3	,							_lowerCAmelCase	:	int=6_4	,							_lowerCAmelCase	:	List[str]=3	,							_lowerCAmelCase	:	Optional[Any]=3	,							_lowerCAmelCase	:	Tuple=2	,							_lowerCAmelCase	:	Union[str, Any]=1	,							_lowerCAmelCase	:	List[str]=1_6	,							_lowerCAmelCase	:	List[Any]=[1_2_8, 2_5_6, 3_8_4]	,							_lowerCAmelCase	:	Any=[4, 6, 8]	,							_lowerCAmelCase	:	List[str]=[2, 3, 4]	,							_lowerCAmelCase	:	Optional[int]=[1_6, 1_6, 1_6]	,							_lowerCAmelCase	:	Optional[Any]=0	,							_lowerCAmelCase	:	Optional[Any]=[2, 2, 2]	,							_lowerCAmelCase	:	int=[2, 2, 2]	,							_lowerCAmelCase	:	Optional[Any]=0.02	,							_lowerCAmelCase	:	Dict=True	,							_lowerCAmelCase	:	Dict=True	,							_lowerCAmelCase	:	Dict=2	,							):







											'''simple docstring'''

											__lowercase													=parent
											__lowercase													=batch_size
											__lowercase													=image_size
											__lowercase													=num_channels
											__lowercase													=kernel_size
											__lowercase													=stride
											__lowercase													=padding
											__lowercase													=hidden_sizes
											__lowercase													=num_attention_heads
											__lowercase													=depths
											__lowercase													=key_dim
											__lowercase													=drop_path_rate
											__lowercase													=patch_size
											__lowercase													=attention_ratio
											__lowercase													=mlp_ratio
											__lowercase													=initializer_range
											__lowercase													=[
											    ["Subsample", key_dim[0], hidden_sizes[0] // key_dim[0], 4, 2, 2],
											    ["Subsample", key_dim[0], hidden_sizes[1] // key_dim[0], 4, 2, 2],
											]
											__lowercase													=is_training
											__lowercase													=use_labels
											__lowercase													=num_labels
											__lowercase													=initializer_range

				def 					__lowerCamelCase  (				self	:	int):







											'''simple docstring'''

											__lowercase													=floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size])

											__lowercase													=None
											if self.use_labels:
																		__lowercase													=ids_tensor([self.batch_size]	,							self.num_labels)

											__lowercase													=self.get_config()
											return config, pixel_values, labels

				def 					__lowerCamelCase  (				self	:	Tuple):







											'''simple docstring'''

											return LevitConfig(
											    image_size=self.image_size	,							num_channels=self.num_channels	,							kernel_size=self.kernel_size	,							stride=self.stride	,							padding=self.padding	,							patch_size=self.patch_size	,							hidden_sizes=self.hidden_sizes	,							num_attention_heads=self.num_attention_heads	,							depths=self.depths	,							key_dim=self.key_dim	,							drop_path_rate=self.drop_path_rate	,							mlp_ratio=self.mlp_ratio	,							attention_ratio=self.attention_ratio	,							initializer_range=self.initializer_range	,							down_ops=self.down_ops	,							)

				def 					__lowerCamelCase  (				self	:	int	,							_lowerCAmelCase	:	List[str]	,							_lowerCAmelCase	:	List[str]	,							_lowerCAmelCase	:	Dict):







											'''simple docstring'''

											__lowercase													=LevitModel(config=lowerCAmelCase__)
											model.to(lowerCAmelCase__)
											model.eval()
											__lowercase													=model(lowerCAmelCase__)
											__lowercase													=(self.image_size, self.image_size)
											__lowercase													=image_size[0], image_size[1]
											for _ in range(4):
																		__lowercase													=floor(((height + 2 * self.padding - self.kernel_size) / self.stride) + 1)
																		__lowercase													=floor(((width + 2 * self.padding - self.kernel_size) / self.stride) + 1)
											self.parent.assertEqual(
											    result.last_hidden_state.shape	,							(self.batch_size, ceil(height / 4) * ceil(width / 4), self.hidden_sizes[-1])	,							)

				def 					__lowerCamelCase  (				self	:	Optional[int]	,							_lowerCAmelCase	:	Optional[int]	,							_lowerCAmelCase	:	Dict	,							_lowerCAmelCase	:	Optional[int]):







											'''simple docstring'''

											__lowercase													=self.num_labels
											__lowercase													=LevitForImageClassification(lowerCAmelCase__)
											model.to(lowerCAmelCase__)
											model.eval()
											__lowercase													=model(lowerCAmelCase__	,							labels=lowerCAmelCase__)
											self.parent.assertEqual(result.logits.shape	,							(self.batch_size, self.num_labels))

				def 					__lowerCamelCase  (				self	:	List[Any]):







											'''simple docstring'''

											__lowercase													=self.prepare_config_and_inputs()
											__lowercase													=config_and_inputs
											__lowercase													={"pixel_values": pixel_values}
											return config, inputs_dict





@require_torch
class _UpperCamelCase		(  a__   ,							a__   ,							unittest.TestCase		):





				'''simple docstring'''





				lowerCAmelCase__            =     (
				    (LevitModel, LevitForImageClassification, LevitForImageClassificationWithTeacher)
				    if is_torch_available()
				    else ()
				)
				lowerCAmelCase__            =     (
				    {
				        """feature-extraction""": LevitModel,
				        """image-classification""": (LevitForImageClassification, LevitForImageClassificationWithTeacher),
				    }
				    if is_torch_available()
				    else {}
				)

				lowerCAmelCase__            =     False
				lowerCAmelCase__            =     False
				lowerCAmelCase__            =     False
				lowerCAmelCase__            =     False
				lowerCAmelCase__            =     False

				def 					__lowerCamelCase  (				self	:	Dict):







											'''simple docstring'''

											__lowercase													=LevitModelTester(self)
											__lowercase													=ConfigTester(self	,							config_class=lowerCAmelCase__	,							has_text_modality=lowerCAmelCase__	,							hidden_size=3_7)

				def 					__lowerCamelCase  (				self	:	List[Any]):







											'''simple docstring'''

											self.create_and_test_config_common_properties()
											self.config_tester.create_and_test_config_to_json_string()
											self.config_tester.create_and_test_config_to_json_file()
											self.config_tester.create_and_test_config_from_and_save_pretrained()
											self.config_tester.create_and_test_config_with_num_labels()
											self.config_tester.check_config_can_be_init_without_params()
											self.config_tester.check_config_arguments_init()

				def 					__lowerCamelCase  (				self	:	List[Any]):







											'''simple docstring'''

											return

				@unittest.skip(reason='Levit does not use inputs_embeds')
				def 					__lowerCamelCase  (				self	:	List[Any]):







											'''simple docstring'''

											pass

				@unittest.skip(reason='Levit does not support input and output embeddings')
				def 					__lowerCamelCase  (				self	:	Any):







											'''simple docstring'''

											pass

				@unittest.skip(reason='Levit does not output attentions')
				def 					__lowerCamelCase  (				self	:	str):







											'''simple docstring'''

											pass

				def 					__lowerCamelCase  (				self	:	str):







											'''simple docstring'''

											__lowercase													=self.model_tester.prepare_config_and_inputs_for_common()

											for model_class in self.all_model_classes:
																		__lowercase													=model_class(lowerCAmelCase__)
																		__lowercase													=inspect.signature(model.forward)
																		# signature.parameters is an OrderedDict => so arg_names order is deterministic
																		__lowercase													=[*signature.parameters.keys()]

																		__lowercase													=["pixel_values"]
																		self.assertListEqual(arg_names[:1]	,							lowerCAmelCase__)

				def 					__lowerCamelCase  (				self	:	str):







											'''simple docstring'''

											def check_hidden_states_output(_lowerCAmelCase	:	Any	,							_lowerCAmelCase	:	Dict	,							_lowerCAmelCase	:	int):
																		__lowercase													=model_class(lowerCAmelCase__)
																		model.to(lowerCAmelCase__)
																		model.eval()

																		with torch.no_grad():
																									__lowercase													=model(**self._prepare_for_class(lowerCAmelCase__	,							lowerCAmelCase__))

																		__lowercase													=outputs.hidden_states

																		__lowercase													=len(self.model_tester.depths) + 1
																		self.assertEqual(len(lowerCAmelCase__)	,							lowerCAmelCase__)

																		__lowercase													=(self.model_tester.image_size, self.model_tester.image_size)
																		__lowercase													=image_size[0], image_size[1]
																		for _ in range(4):
																									__lowercase													=floor(
																									    (
																									        (height + 2 * self.model_tester.padding - self.model_tester.kernel_size)
																									        / self.model_tester.stride
																									    )
																									    + 1)
																									__lowercase													=floor(
																									    (
																									        (width + 2 * self.model_tester.padding - self.model_tester.kernel_size)
																									        / self.model_tester.stride
																									    )
																									    + 1)
																		# verify the first hidden states (first block)
																		self.assertListEqual(
																		    list(hidden_states[0].shape[-2:])	,							[
																		        height * width,
																		        self.model_tester.hidden_sizes[0],
																		    ]	,							)

											__lowercase													=self.model_tester.prepare_config_and_inputs_for_common()

											for model_class in self.all_model_classes:
																		__lowercase													=True
																		check_hidden_states_output(lowerCAmelCase__	,							lowerCAmelCase__	,							lowerCAmelCase__)

																		# check that output_hidden_states also work using config
																		del inputs_dict["output_hidden_states"]
																		__lowercase													=True

																		check_hidden_states_output(lowerCAmelCase__	,							lowerCAmelCase__	,							lowerCAmelCase__)

				@unittest.skip('Will be fixed soon by reducing the size of the model used for common tests.')
				def 					__lowerCamelCase  (				self	:	List[Any]):







											'''simple docstring'''

											pass

				def 					__lowerCamelCase  (				self	:	Any	,							_lowerCAmelCase	:	str	,							_lowerCAmelCase	:	Optional[Any]	,							_lowerCAmelCase	:	Tuple=False):







											'''simple docstring'''

											__lowercase													=super()._prepare_for_class(lowerCAmelCase__	,							lowerCAmelCase__	,							return_labels=lowerCAmelCase__)

											if return_labels:
																		if model_class.__name__ == "LevitForImageClassificationWithTeacher":
																									del inputs_dict["labels"]

											return inputs_dict

				def 					__lowerCamelCase  (				self	:	Optional[int]):







											'''simple docstring'''

											__lowercase													=self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_model(*lowerCAmelCase__)

				def 					__lowerCamelCase  (				self	:	str):







											'''simple docstring'''

											__lowercase													=self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_for_image_classification(*lowerCAmelCase__)

				def 					__lowerCamelCase  (				self	:	Tuple):







											'''simple docstring'''

											if not self.model_tester.is_training:
																		return

											__lowercase													=self.model_tester.prepare_config_and_inputs_for_common()
											__lowercase													=True

											for model_class in self.all_model_classes:
																		# LevitForImageClassificationWithTeacher supports inference-only
																		if (
																		    model_class in get_values(lowerCAmelCase__)
																		    or model_class.__name__ == "LevitForImageClassificationWithTeacher"
																		):
																									continue
																		__lowercase													=model_class(lowerCAmelCase__)
																		model.to(lowerCAmelCase__)
																		model.train()
																		__lowercase													=self._prepare_for_class(lowerCAmelCase__	,							lowerCAmelCase__	,							return_labels=lowerCAmelCase__)
																		__lowercase													=model(**lowerCAmelCase__).loss
																		loss.backward()

				def 					__lowerCamelCase  (				self	:	Dict):







											'''simple docstring'''

											__lowercase													=self.model_tester.prepare_config_and_inputs_for_common()
											if not self.model_tester.is_training:
																		return

											__lowercase													=False
											__lowercase													=True

											for model_class in self.all_model_classes:
																		if model_class in get_values(lowerCAmelCase__) or not model_class.supports_gradient_checkpointing:
																									continue
																		# LevitForImageClassificationWithTeacher supports inference-only
																		if model_class.__name__ == "LevitForImageClassificationWithTeacher":
																									continue
																		__lowercase													=model_class(lowerCAmelCase__)
																		model.gradient_checkpointing_enable()
																		model.to(lowerCAmelCase__)
																		model.train()
																		__lowercase													=self._prepare_for_class(lowerCAmelCase__	,							lowerCAmelCase__	,							return_labels=lowerCAmelCase__)
																		__lowercase													=model(**lowerCAmelCase__).loss
																		loss.backward()

				def 					__lowerCamelCase  (				self	:	int):







											'''simple docstring'''

											__lowercase													=self.model_tester.prepare_config_and_inputs_for_common()

											__lowercase													=[
											    {"title": "multi_label_classification", "num_labels": 2, "dtype": torch.float},
											    {"title": "single_label_classification", "num_labels": 1, "dtype": torch.long},
											    {"title": "regression", "num_labels": 1, "dtype": torch.float},
											]

											for model_class in self.all_model_classes:
																		if (
																		    model_class
																		    not in [
																		        *get_values(lowerCAmelCase__),
																		    ]
																		    or model_class.__name__ == "LevitForImageClassificationWithTeacher"
																		):
																									continue

																		for problem_type in problem_types:
																									with self.subTest(msg=f"""Testing {model_class} with {problem_type['title']}"""):
																																__lowercase													=problem_type["title"]
																																__lowercase													=problem_type["num_labels"]

																																__lowercase													=model_class(lowerCAmelCase__)
																																model.to(lowerCAmelCase__)
																																model.train()

																																__lowercase													=self._prepare_for_class(lowerCAmelCase__	,							lowerCAmelCase__	,							return_labels=lowerCAmelCase__)

																																if problem_type["num_labels"] > 1:
																																							__lowercase													=inputs["labels"].unsqueeze(1).repeat(1	,							problem_type['num_labels'])

																																__lowercase													=inputs["labels"].to(problem_type['dtype'])

																																# This tests that we do not trigger the warning form PyTorch "Using a target size that is different
																																# to the input size. This will likely lead to incorrect results due to broadcasting. Please ensure
																																# they have the same size." which is a symptom something in wrong for the regression problem.
																																# See https://github.com/huggingface/transformers/issues/11780
																																with warnings.catch_warnings(record=lowerCAmelCase__) as warning_list:
																																							__lowercase													=model(**lowerCAmelCase__).loss
																																for w in warning_list:
																																							if "Using a target size that is different to the input size" in str(w.message):
																																														raise ValueError(
																																														    f"""Something is going wrong in the regression problem: intercepted {w.message}""")

																																loss.backward()

				@slow
				def 					__lowerCamelCase  (				self	:	Optional[Any]):







											'''simple docstring'''

											for model_name in LEVIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																		__lowercase													=LevitModel.from_pretrained(lowerCAmelCase__)
																		self.assertIsNotNone(lowerCAmelCase__)



def 	_A							( ):



							"""simple docstring"""



							__lowercase													=Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png'       )
							return image





@require_torch
@require_vision
class _UpperCamelCase		(  unittest.TestCase		):





				'''simple docstring'''





				@cached_property
				def 					__lowerCamelCase  (				self	:	Dict):







											'''simple docstring'''

											return LevitImageProcessor.from_pretrained(LEVIT_PRETRAINED_MODEL_ARCHIVE_LIST[0])

				@slow
				def 					__lowerCamelCase  (				self	:	str):







											'''simple docstring'''

											__lowercase													=LevitForImageClassificationWithTeacher.from_pretrained(LEVIT_PRETRAINED_MODEL_ARCHIVE_LIST[0]).to(
											    lowerCAmelCase__)

											__lowercase													=self.default_image_processor
											__lowercase													=prepare_img()
											__lowercase													=image_processor(images=lowerCAmelCase__	,							return_tensors='pt').to(lowerCAmelCase__)

											# forward pass
											with torch.no_grad():
																		__lowercase													=model(**lowerCAmelCase__)

											# verify the logits
											__lowercase													=torch.Size((1, 1_0_0_0))
											self.assertEqual(outputs.logits.shape	,							lowerCAmelCase__)

											__lowercase													=torch.tensor([1.0448, -0.3745, -1.8317]).to(lowerCAmelCase__)

											self.assertTrue(torch.allclose(outputs.logits[0, :3]	,							lowerCAmelCase__	,							atol=1e-4))










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






							def        __a  (  self ,   lowerCAmelCase__ ,   lowerCAmelCase__ ,   lowerCAmelCase__=False )      ->					Any:
								a :						Dict      =							super()._prepare_for_class(lowerCAmelCase__ ,   lowerCAmelCase__ ,   return_labels=lowerCAmelCase__ )

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

								return inputs_dict


							class 			__UpperCamelCase							(		a__		):






														def __init__(  self ,   lowerCAmelCase__ ,   lowerCAmelCase__=13 ,   lowerCAmelCase__=7 ,   lowerCAmelCase__=True ,   lowerCAmelCase__=True ,   lowerCAmelCase__=True ,   lowerCAmelCase__=True ,   lowerCAmelCase__=99 ,   lowerCAmelCase__=32 ,   lowerCAmelCase__=32 ,   lowerCAmelCase__=2 ,   lowerCAmelCase__=4 ,   lowerCAmelCase__=37 ,   lowerCAmelCase__="gelu" ,   lowerCAmelCase__=0.1 ,   lowerCAmelCase__=0.1 ,   lowerCAmelCase__=512 ,   lowerCAmelCase__=16 ,   lowerCAmelCase__=2 ,   lowerCAmelCase__=0.02 ,   lowerCAmelCase__=3 ,   lowerCAmelCase__=4 ,   lowerCAmelCase__=None ,   )      ->					List[str]:
															a :						Union[str, Any]      =							parent
															a :						str      =							batch_size
															a :						Optional[int]      =							seq_length
															a :						Tuple      =							is_training
															a :						int      =							use_input_mask
															a :						Union[str, Any]      =							use_token_type_ids
															a :						List[str]      =							use_labels
															a :						str      =							vocab_size
															a :						List[Any]      =							hidden_size
															a :						Any      =							num_hidden_layers
															a :						List[Any]      =							num_attention_heads
															a :						Tuple      =							intermediate_size
															a :						str      =							hidden_act
															a :						List[Any]      =							hidden_dropout_prob
															a :						Optional[Any]      =							attention_probs_dropout_prob
															a :						Union[str, Any]      =							max_position_embeddings
															a :						Tuple      =							type_vocab_size
															a :						Union[str, Any]      =							type_sequence_label_size
															a :						Optional[int]      =							initializer_range
															a :						Any      =							num_labels
															a :						str      =							num_choices
															a :						List[Any]      =							scope
															a :						str      =							embedding_size






														def        __a  (  self )      ->					Dict:
															a :						Optional[int]      =							ids_tensor([self.batch_size, self.seq_length] ,   self.vocab_size )

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

															a :						int      =							None
															if self.use_token_type_ids:
																a :						Tuple      =							ids_tensor([self.batch_size, self.seq_length] ,   self.type_vocab_size )

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

															a :						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        __a  (  self ,   lowerCAmelCase__ ,   lowerCAmelCase__ ,   lowerCAmelCase__ ,   lowerCAmelCase__ ,   lowerCAmelCase__ ,   lowerCAmelCase__ ,   lowerCAmelCase__ )      ->					int:
															a :						List[Any]      =							TFMobileBertModel(config=lowerCAmelCase__ )
															a :						int      =							{"input_ids": input_ids, "attention_mask": input_mask, "token_type_ids": token_type_ids}
															a :						List[Any]      =							model(lowerCAmelCase__ )

															a :						List[str]      =							[input_ids, input_mask]
															a :						Dict      =							model(lowerCAmelCase__ )

															a :						List[str]      =							model(lowerCAmelCase__ )

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






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






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






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






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






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






							def        __a  (  self )      ->					List[str]:
								a :						List[str]      =							TFMobileBertModelTest.TFMobileBertModelTester(self )
								a :						str      =							ConfigTester(self ,   config_class=lowerCAmelCase__ ,   hidden_size=37 )






							def        __a  (  self )      ->					Any:
								self.config_tester.run_common_tests()






							def        __a  (  self )      ->					List[str]:
								a :						List[str]      =							self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_mobilebert_model(*lowerCAmelCase__ )






							def        __a  (  self )      ->					Dict:
								a :						Any      =							self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_mobilebert_for_masked_lm(*lowerCAmelCase__ )






							def        __a  (  self )      ->					Any:
								a :						Union[str, Any]      =							self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_mobilebert_for_multiple_choice(*lowerCAmelCase__ )






							def        __a  (  self )      ->					List[str]:
								a :						Tuple      =							self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_mobilebert_for_next_sequence_prediction(*lowerCAmelCase__ )






							def        __a  (  self )      ->					int:
								a :						List[str]      =							self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_mobilebert_for_pretraining(*lowerCAmelCase__ )






							def        __a  (  self )      ->					int:
								a :						Dict      =							self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_mobilebert_for_question_answering(*lowerCAmelCase__ )






							def        __a  (  self )      ->					Dict:
								a :						Optional[Any]      =							self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_mobilebert_for_sequence_classification(*lowerCAmelCase__ )






							def        __a  (  self )      ->					Optional[Any]:
								a :						Optional[Any]      =							self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_mobilebert_for_token_classification(*lowerCAmelCase__ )






							@slow
							def        __a  (  self )      ->					Any:
								# for model_name in TF_MOBILEBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
								for model_name in ["google/mobilebert-uncased"]:
									a :						Union[str, Any]      =							TFMobileBertModel.from_pretrained(lowerCAmelCase__ )
									self.assertIsNotNone(lowerCAmelCase__ )


@require_tf
class 			__UpperCamelCase							(		unittest.TestCase		):






							@slow
							def        __a  (  self )      ->					Optional[int]:
								a :						Optional[int]      =							TFMobileBertForPreTraining.from_pretrained("google/mobilebert-uncased" )
								a :						Any      =							tf.constant([[0, 1, 2, 3, 4, 5]] )
								a :						int      =							model(lowerCAmelCase__ )[0]

								a :						Optional[int]      =							[1, 6, 3_0522]
								self.assertEqual(output.shape ,   lowerCAmelCase__ )

								a :						Optional[Any]      =							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] ,   lowerCAmelCase__ ,   atol=1E-4 )


