





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_     :     Dict       			=      (
			    (
			        TFMobileBertModel,
			        TFMobileBertForMaskedLM,
			        TFMobileBertForNextSentencePrediction,
			        TFMobileBertForPreTraining,
			        TFMobileBertForQuestionAnswering,
			        TFMobileBertForSequenceClassification,
			        TFMobileBertForTokenClassification,
			        TFMobileBertForMultipleChoice,
			    )
			    if is_tf_available()
			    else ()
			)
			lowerCamelCase_     :     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 {}
			)
			lowerCamelCase_     :     Optional[int]       			=      False
			lowerCamelCase_     :     Tuple       			=      False






			def 			lowerCamelCase  (self					,							__magic_name__					,							__magic_name__					,							__magic_name__=False    )     ->   int:


							'''simple docstring'''



							snake_case_						:    Any				=  super()._prepare_for_class(__magic_name__					,							__magic_name__					,							return_labels=__magic_name__    )

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

							return inputs_dict



			class 					__lowerCAmelCase       (			_a     ):






						def __init__(self					,							__magic_name__					,							__magic_name__=13					,							__magic_name__=7					,							__magic_name__=True					,							__magic_name__=True					,							__magic_name__=True					,							__magic_name__=True					,							__magic_name__=99					,							__magic_name__=32					,							__magic_name__=32					,							__magic_name__=2					,							__magic_name__=4					,							__magic_name__=37					,							__magic_name__="gelu"					,							__magic_name__=0.1					,							__magic_name__=0.1					,							__magic_name__=512					,							__magic_name__=16					,							__magic_name__=2					,							__magic_name__=0.02					,							__magic_name__=3					,							__magic_name__=4					,							__magic_name__=None					,							)     ->   int:


										'''simple docstring'''



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






						def 			lowerCamelCase  (self    )     ->   Dict:


										'''simple docstring'''



										snake_case_						:    str				=  ids_tensor([self.batch_size, self.seq_length]					,							self.vocab_size    )

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

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

										snake_case_						:    Tuple				=  None
										snake_case_						:    int				=  None
										snake_case_						:    Optional[Any]				=  None
										if self.use_labels:
														snake_case_						:    List[Any]				=  ids_tensor([self.batch_size]					,							self.type_sequence_label_size    )
														snake_case_						:    Union[str, Any]				=  ids_tensor([self.batch_size, self.seq_length]					,							self.num_labels    )
														snake_case_						:    Tuple				=  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 			lowerCamelCase  (self					,							__magic_name__					,							__magic_name__					,							__magic_name__					,							__magic_name__					,							__magic_name__					,							__magic_name__					,							__magic_name__    )     ->   List[Any]:


										'''simple docstring'''



										snake_case_						:    int				=  TFMobileBertModel(config=__magic_name__    )
										snake_case_						:    int				=  {'''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids}
										snake_case_						:    Any				=  model(__magic_name__    )

										snake_case_						:    int				=  [input_ids, input_mask]
										snake_case_						:    Dict				=  model(__magic_name__    )

										snake_case_						:    Any				=  model(__magic_name__    )

										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					,							__magic_name__					,							__magic_name__					,							__magic_name__					,							__magic_name__					,							__magic_name__					,							__magic_name__					,							__magic_name__    )     ->   List[str]:


										'''simple docstring'''



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






						def 			lowerCamelCase  (self					,							__magic_name__					,							__magic_name__					,							__magic_name__					,							__magic_name__					,							__magic_name__					,							__magic_name__					,							__magic_name__    )     ->   str:


										'''simple docstring'''



										snake_case_						:    Tuple				=  TFMobileBertForNextSentencePrediction(config=__magic_name__    )
										snake_case_						:    Tuple				=  {'''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids}
										snake_case_						:    Dict				=  model(__magic_name__    )
										self.parent.assertEqual(result.logits.shape					,							(self.batch_size, 2)    )






						def 			lowerCamelCase  (self					,							__magic_name__					,							__magic_name__					,							__magic_name__					,							__magic_name__					,							__magic_name__					,							__magic_name__					,							__magic_name__    )     ->   int:


										'''simple docstring'''



										snake_case_						:    Dict				=  TFMobileBertForPreTraining(config=__magic_name__    )
										snake_case_						:    Dict				=  {'''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids}
										snake_case_						:    List[str]				=  model(__magic_name__    )
										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					,							__magic_name__					,							__magic_name__					,							__magic_name__					,							__magic_name__					,							__magic_name__					,							__magic_name__					,							__magic_name__    )     ->   List[Any]:


										'''simple docstring'''



										snake_case_						:    Optional[Any]				=  self.num_labels
										snake_case_						:    Optional[int]				=  TFMobileBertForSequenceClassification(config=__magic_name__    )
										snake_case_						:    int				=  {'''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids}
										snake_case_						:    Any				=  model(__magic_name__    )
										self.parent.assertEqual(result.logits.shape					,							(self.batch_size, self.num_labels)    )






						def 			lowerCamelCase  (self					,							__magic_name__					,							__magic_name__					,							__magic_name__					,							__magic_name__					,							__magic_name__					,							__magic_name__					,							__magic_name__    )     ->   Optional[Any]:


										'''simple docstring'''



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






						def 			lowerCamelCase  (self					,							__magic_name__					,							__magic_name__					,							__magic_name__					,							__magic_name__					,							__magic_name__					,							__magic_name__					,							__magic_name__    )     ->   str:


										'''simple docstring'''



										snake_case_						:    List[str]				=  self.num_labels
										snake_case_						:    Any				=  TFMobileBertForTokenClassification(config=__magic_name__    )
										snake_case_						:    Any				=  {'''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids}
										snake_case_						:    str				=  model(__magic_name__    )
										self.parent.assertEqual(result.logits.shape					,							(self.batch_size, self.seq_length, self.num_labels)    )






						def 			lowerCamelCase  (self					,							__magic_name__					,							__magic_name__					,							__magic_name__					,							__magic_name__					,							__magic_name__					,							__magic_name__					,							__magic_name__    )     ->   Any:


										'''simple docstring'''



										snake_case_						:    str				=  TFMobileBertForQuestionAnswering(config=__magic_name__    )
										snake_case_						:    List[Any]				=  {'''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids}
										snake_case_						:    Dict				=  model(__magic_name__    )
										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    )     ->   str:


										'''simple docstring'''



										snake_case_						:    str				=  self.prepare_config_and_inputs()
										(
										    (
										    snake_case_
										)			,							(
										    snake_case_
										)			,							(
										    snake_case_
										)			,							(
										    snake_case_
										)			,							(
										    snake_case_
										)			,							(
										    snake_case_
										)			,							(
										    snake_case_
										)			,							
										)						:    Tuple				=  config_and_inputs
										snake_case_						:    Dict				=  {'''input_ids''': input_ids, '''token_type_ids''': token_type_ids, '''attention_mask''': input_mask}
										return config, inputs_dict






			def 			lowerCamelCase  (self    )     ->   str:


							'''simple docstring'''



							snake_case_						:    Tuple				=  TFMobileBertModelTest.TFMobileBertModelTester(self    )
							snake_case_						:    Union[str, Any]				=  ConfigTester(self					,							config_class=__magic_name__					,							hidden_size=37    )






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


							'''simple docstring'''



							self.config_tester.run_common_tests()






			def 			lowerCamelCase  (self    )     ->   Dict:


							'''simple docstring'''



							snake_case_						:    Dict				=  self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_mobilebert_model(*__magic_name__    )






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


							'''simple docstring'''



							snake_case_						:    List[str]				=  self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_mobilebert_for_masked_lm(*__magic_name__    )






			def 			lowerCamelCase  (self    )     ->   int:


							'''simple docstring'''



							snake_case_						:    Optional[int]				=  self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_mobilebert_for_multiple_choice(*__magic_name__    )






			def 			lowerCamelCase  (self    )     ->   Tuple:


							'''simple docstring'''



							snake_case_						:    Dict				=  self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_mobilebert_for_next_sequence_prediction(*__magic_name__    )






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


							'''simple docstring'''



							snake_case_						:    str				=  self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_mobilebert_for_pretraining(*__magic_name__    )






			def 			lowerCamelCase  (self    )     ->   int:


							'''simple docstring'''



							snake_case_						:    int				=  self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_mobilebert_for_question_answering(*__magic_name__    )






			def 			lowerCamelCase  (self    )     ->   Tuple:


							'''simple docstring'''



							snake_case_						:    Tuple				=  self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_mobilebert_for_sequence_classification(*__magic_name__    )






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


							'''simple docstring'''



							snake_case_						:    Dict				=  self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_mobilebert_for_token_classification(*__magic_name__    )






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


							'''simple docstring'''



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




@require_tf
class 					__lowerCAmelCase       (			unittest.TestCase     ):






			@slow
			def 			lowerCamelCase  (self    )     ->   Tuple:


							'''simple docstring'''



							snake_case_						:    Optional[int]				=  TFMobileBertForPreTraining.from_pretrained('''google/mobilebert-uncased'''    )
							snake_case_						:    Any				=  tf.constant([[0, 1, 2, 3, 4, 5]]    )
							snake_case_						:    Union[str, Any]				=  model(__magic_name__    )[0]

							snake_case_						:    Optional[Any]				=  [1, 6, 3_0522]
							self.assertEqual(output.shape					,							__magic_name__    )

							snake_case_						:    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]					,							__magic_name__					,							atol=1e-4    )







from ...utils import (
    OptionalDependencyNotAvailable,
    is_torch_available,
    is_transformers_available,
    is_transformers_version,
)


try:
	if not (is_transformers_available() and is_torch_available() and is_transformers_version('''>=''', '''4.25.0''')):
		raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
	from ...utils.dummy_torch_and_transformers_objects import UnCLIPImageVariationPipeline, UnCLIPPipeline
else:
	from .pipeline_unclip import UnCLIPPipeline
	from .pipeline_unclip_image_variation import UnCLIPImageVariationPipeline
	from .text_proj import UnCLIPTextProjModel
