




"""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__       (			lowerCAmelCase_  ,			lowerCAmelCase_  ,			unittest.TestCase    ):


				"""simple docstring"""





				__UpperCAmelCase :     Dict       =      (
				    (
				        TFMobileBertModel,
				        TFMobileBertForMaskedLM,
				        TFMobileBertForNextSentencePrediction,
				        TFMobileBertForPreTraining,
				        TFMobileBertForQuestionAnswering,
				        TFMobileBertForSequenceClassification,
				        TFMobileBertForTokenClassification,
				        TFMobileBertForMultipleChoice,
				    )
				    if is_tf_available()
				    else ()
				)
				__UpperCAmelCase :     Optional[int]       =      (
				    {
				        "feature-extraction": TFMobileBertModel,
				        "fill-mask": TFMobileBertForMaskedLM,
				        "question-answering": TFMobileBertForQuestionAnswering,
				        "text-classification": TFMobileBertForSequenceClassification,
				        "token-classification": TFMobileBertForTokenClassification,
				        "zero-shot": TFMobileBertForSequenceClassification,
				    }
				    if is_tf_available()
				    else {}
				)
				__UpperCAmelCase :     Optional[Any]       =      False
				__UpperCAmelCase :     Tuple       =      False



				def       snake_case    (     self					:	List[str]    ,   lowercase__					:	Tuple    ,   lowercase__					:	int    ,   lowercase__					:	List[str]=False						):
						__lowercase				:		List[str]												=				super()._prepare_for_class(lowercase__    ,   lowercase__    ,   return_labels=lowercase__						)

						if return_labels:
								if model_class in get_values(lowercase__						):
										__lowercase				:		List[str]												=				tf.zeros(self.model_tester.batch_size    ,   dtype=tf.intaa						)

						return inputs_dict




				class       lowerCAmelCase__       (			lowerCAmelCase_    ):


								"""simple docstring"""





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



								def       snake_case    (     self					:	Optional[int]						):
										__lowercase				:		Tuple												=				ids_tensor([self.batch_size, self.seq_length]    ,   self.vocab_size						)

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

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

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

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

										__lowercase				:		Optional[Any]												=				[input_ids, input_mask]
										__lowercase				:		Any												=				model(lowercase__						)

										__lowercase				:		List[str]												=				model(lowercase__						)

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



								def       snake_case    (     self					:	int    ,   lowercase__					:	Tuple    ,   lowercase__					:	List[str]    ,   lowercase__					:	List[Any]    ,   lowercase__					:	Dict    ,   lowercase__					:	int    ,   lowercase__					:	Union[str, Any]    ,   lowercase__					:	Union[str, Any]						):
										__lowercase				:		Union[str, Any]												=				TFMobileBertForNextSentencePrediction(config=lowercase__						)
										__lowercase				:		int												=				{"input_ids": input_ids, "attention_mask": input_mask, "token_type_ids": token_type_ids}
										__lowercase				:		Union[str, Any]												=				model(lowercase__						)
										self.parent.assertEqual(result.logits.shape    ,   (self.batch_size, 2)						)



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



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



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



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





				def       snake_case    (     self					:	List[str]						):
						__lowercase				:		Optional[int]												=				TFMobileBertModelTest.TFMobileBertModelTester(self						)
						__lowercase				:		Union[str, Any]												=				ConfigTester(self    ,   config_class=lowercase__    ,   hidden_size=3_7						)



				def       snake_case    (     self					:	str						):
						self.config_tester.run_common_tests()



				def       snake_case    (     self					:	int						):
						__lowercase				:		Tuple												=				self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_mobilebert_model(*lowercase__						)



				def       snake_case    (     self					:	Any						):
						__lowercase				:		int												=				self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_mobilebert_for_masked_lm(*lowercase__						)



				def       snake_case    (     self					:	int						):
						__lowercase				:		Optional[Any]												=				self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_mobilebert_for_multiple_choice(*lowercase__						)



				def       snake_case    (     self					:	List[Any]						):
						__lowercase				:		Dict												=				self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_mobilebert_for_next_sequence_prediction(*lowercase__						)



				def       snake_case    (     self					:	Tuple						):
						__lowercase				:		Dict												=				self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_mobilebert_for_pretraining(*lowercase__						)



				def       snake_case    (     self					:	Optional[Any]						):
						__lowercase				:		Dict												=				self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_mobilebert_for_question_answering(*lowercase__						)



				def       snake_case    (     self					:	List[Any]						):
						__lowercase				:		Any												=				self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_mobilebert_for_sequence_classification(*lowercase__						)



				def       snake_case    (     self					:	int						):
						__lowercase				:		Union[str, Any]												=				self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_mobilebert_for_token_classification(*lowercase__						)



				@slow
				def       snake_case    (     self					:	int						):
						# for model_name in TF_MOBILEBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
						for model_name in ["google/mobilebert-uncased"]:
								__lowercase				:		List[Any]												=				TFMobileBertModel.from_pretrained(lowercase__						)
								self.assertIsNotNone(lowercase__						)





@require_tf
class       lowerCAmelCase__       (			unittest.TestCase    ):


				"""simple docstring"""





				@slow
				def       snake_case    (     self					:	List[Any]						):
						__lowercase				:		Any												=				TFMobileBertForPreTraining.from_pretrained("google/mobilebert-uncased"						)
						__lowercase				:		Dict												=				tf.constant([[0, 1, 2, 3, 4, 5]]						)
						__lowercase				:		Dict												=				model(lowercase__						)[0]

						__lowercase				:		Optional[int]												=				[1, 6, 3_0_5_2_2]
						self.assertEqual(output.shape    ,   lowercase__						)

						__lowercase				:		Any												=				tf.constant(
						    [
						        [
						            [-4.5_9_1_9_5_4_7, -9.2_4_8_2_9_5, -9.6_4_5_2_5_6],
						            [-6.7_3_0_6_1_7_5, -6.4_4_0_2_8_4, -6.6_0_5_2_8_3_7],
						            [-7.2_7_4_3_5_0_6, -6.7_8_4_7_9_1_5, -6.0_2_4_6_7_3],
						        ]
						    ]						)
						tf.debugging.assert_near(output[:, :3, :3]    ,   lowercase__    ,   atol=1e-4						)






"""simple docstring"""







import os
import sys
import unittest


__A   :  Optional[Any]					=      os.path.abspath(os.path.dirname(os.path.dirname(os.path.dirname(__file__))))
sys.path.append(os.path.join(git_repo_path, 'utils'))

import check_dummies  # noqa: E402
from check_dummies import create_dummy_files, create_dummy_object, find_backend, read_init  # noqa: E402


# Align TRANSFORMERS_PATH in check_dummies with the current path
__A   :  Optional[Any]					=      os.path.join(git_repo_path, 'src', 'diffusers')




class       lowerCAmelCase__       (			unittest.TestCase    ):


				"""simple docstring"""





				def       snake_case    (     self					:	str						):
						__lowercase				:		int												=				find_backend("    if not is_torch_available():"						)
						self.assertEqual(lowercase__    ,   "torch"						)

						# backend_with_underscore = find_backend("    if not is_tensorflow_text_available():")
						# self.assertEqual(backend_with_underscore, "tensorflow_text")

						__lowercase				:		int												=				find_backend("    if not (is_torch_available() and is_transformers_available()):"						)
						self.assertEqual(lowercase__    ,   "torch_and_transformers"						)

						# double_backend_with_underscore = find_backend(
						#    "    if not (is_sentencepiece_available() and is_tensorflow_text_available()):"
						# )
						# self.assertEqual(double_backend_with_underscore, "sentencepiece_and_tensorflow_text")

						__lowercase				:		str												=				find_backend(
						    "    if not (is_torch_available() and is_transformers_available() and is_onnx_available()):"						)
						self.assertEqual(lowercase__    ,   "torch_and_transformers_and_onnx"						)



				def       snake_case    (     self					:	Any						):
						__lowercase				:		List[Any]												=				read_init()
						# We don't assert on the exact list of keys to allow for smooth grow of backend-specific objects
						self.assertIn("torch"    ,   lowercase__						)
						self.assertIn("torch_and_transformers"    ,   lowercase__						)
						self.assertIn("flax_and_transformers"    ,   lowercase__						)
						self.assertIn("torch_and_transformers_and_onnx"    ,   lowercase__						)

						# Likewise, we can't assert on the exact content of a key
						self.assertIn("UNet2DModel"    ,   objects["torch"]						)
						self.assertIn("FlaxUNet2DConditionModel"    ,   objects["flax"]						)
						self.assertIn("StableDiffusionPipeline"    ,   objects["torch_and_transformers"]						)
						self.assertIn("FlaxStableDiffusionPipeline"    ,   objects["flax_and_transformers"]						)
						self.assertIn("LMSDiscreteScheduler"    ,   objects["torch_and_scipy"]						)
						self.assertIn("OnnxStableDiffusionPipeline"    ,   objects["torch_and_transformers_and_onnx"]						)



				def       snake_case    (     self					:	Dict						):
						__lowercase				:		Tuple												=				create_dummy_object("CONSTANT"    ,   "'torch'"						)
						self.assertEqual(lowercase__    ,   "\nCONSTANT = None\n"						)

						__lowercase				:		Union[str, Any]												=				create_dummy_object("function"    ,   "'torch'"						)
						self.assertEqual(
						    lowercase__    ,   "\ndef function(*args, **kwargs):\n    requires_backends(function, 'torch')\n"						)

						__lowercase				:		Tuple												=				"\nclass FakeClass(metaclass=DummyObject):\n    _backends = 'torch'\n\n    def __init__(self, *args, **kwargs):\n        requires_backends(self, 'torch')\n\n    @classmethod\n    def from_config(cls, *args, **kwargs):\n        requires_backends(cls, 'torch')\n\n    @classmethod\n    def from_pretrained(cls, *args, **kwargs):\n        requires_backends(cls, 'torch')\n"
						__lowercase				:		Dict												=				create_dummy_object("FakeClass"    ,   "'torch'"						)
						self.assertEqual(lowercase__    ,   lowercase__						)



				def       snake_case    (     self					:	List[Any]						):
						__lowercase				:		Optional[int]												=				"# This file is autogenerated by the command `make fix-copies`, do not edit.\nfrom ..utils import DummyObject, requires_backends\n\n\nCONSTANT = None\n\n\ndef function(*args, **kwargs):\n    requires_backends(function, [\"torch\"])\n\n\nclass FakeClass(metaclass=DummyObject):\n    _backends = [\"torch\"]\n\n    def __init__(self, *args, **kwargs):\n        requires_backends(self, [\"torch\"])\n\n    @classmethod\n    def from_config(cls, *args, **kwargs):\n        requires_backends(cls, [\"torch\"])\n\n    @classmethod\n    def from_pretrained(cls, *args, **kwargs):\n        requires_backends(cls, [\"torch\"])\n"
						__lowercase				:		List[str]												=				create_dummy_files({"torch": ["CONSTANT", "function", "FakeClass"]}						)
						self.assertEqual(dummy_files["torch"]    ,   lowercase__						)
