



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





	def       UpperCamelCase_    (					self   :							Optional[Any]					,lowerCamelCase__   :							Union[str, Any]					,lowerCamelCase__   :							List[str]					,lowerCamelCase__   :							Dict=False					):







							'''simple docstring'''
							_UpperCamelCase   :						Tuple												=  super()._prepare_for_class(lowerCamelCase__					,lowerCamelCase__					,return_labels=lowerCamelCase__					)

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

							return inputs_dict






	class 				lowercase__      (				lowercase  ):





		def __init__(					self   :							Union[str, Any]					,lowerCamelCase__   :							Optional[int]					,lowerCamelCase__   :							List[Any]=13					,lowerCamelCase__   :							Tuple=7					,lowerCamelCase__   :							Optional[Any]=True					,lowerCamelCase__   :							List[str]=True					,lowerCamelCase__   :							Union[str, Any]=True					,lowerCamelCase__   :							List[Any]=True					,lowerCamelCase__   :							Dict=99					,lowerCamelCase__   :							Union[str, Any]=32					,lowerCamelCase__   :							List[str]=32					,lowerCamelCase__   :							Optional[Any]=2					,lowerCamelCase__   :							Optional[Any]=4					,lowerCamelCase__   :							List[str]=37					,lowerCamelCase__   :							str="gelu"					,lowerCamelCase__   :							Tuple=0.1					,lowerCamelCase__   :							Any=0.1					,lowerCamelCase__   :							Optional[Any]=512					,lowerCamelCase__   :							str=16					,lowerCamelCase__   :							Any=2					,lowerCamelCase__   :							int=0.0_2					,lowerCamelCase__   :							Optional[int]=3					,lowerCamelCase__   :							str=4					,lowerCamelCase__   :							int=None					,):







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





		def       UpperCamelCase_    (					self   :							int					):







								'''simple docstring'''
								_UpperCamelCase   :						int												=  ids_tensor([self.batch_size, self.seq_length]					,self.vocab_size					)

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

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

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

								_UpperCamelCase   :						List[str]												=  MobileBertConfig(
								    vocab_size=self.vocab_size					,hidden_size=self.hidden_size					,num_hidden_layers=self.num_hidden_layers					,num_attention_heads=self.num_attention_heads					,intermediate_size=self.intermediate_size					,hidden_act=self.hidden_act					,hidden_dropout_prob=self.hidden_dropout_prob					,attention_probs_dropout_prob=self.attention_probs_dropout_prob					,max_position_embeddings=self.max_position_embeddings					,type_vocab_size=self.type_vocab_size					,initializer_range=self.initializer_range					,embedding_size=self.embedding_size					,)

								return config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels





		def       UpperCamelCase_    (					self   :							Dict					,lowerCamelCase__   :							List[str]					,lowerCamelCase__   :							Optional[Any]					,lowerCamelCase__   :							Union[str, Any]					,lowerCamelCase__   :							Any					,lowerCamelCase__   :							Optional[Any]					,lowerCamelCase__   :							List[str]					,lowerCamelCase__   :							Optional[Any]					):







								'''simple docstring'''
								_UpperCamelCase   :						Tuple												=  TFMobileBertModel(config=lowerCamelCase__					)
								_UpperCamelCase   :						int												=  {'input_ids': input_ids, 'attention_mask': input_mask, 'token_type_ids': token_type_ids}
								_UpperCamelCase   :						Tuple												=  model(lowerCamelCase__					)

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

								_UpperCamelCase   :						Optional[Any]												=  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       UpperCamelCase_    (					self   :							Dict					,lowerCamelCase__   :							Any					,lowerCamelCase__   :							Optional[Any]					,lowerCamelCase__   :							Union[str, Any]					,lowerCamelCase__   :							str					,lowerCamelCase__   :							int					,lowerCamelCase__   :							Dict					,lowerCamelCase__   :							List[str]					):







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





		def       UpperCamelCase_    (					self   :							Optional[int]					,lowerCamelCase__   :							int					,lowerCamelCase__   :							int					,lowerCamelCase__   :							Any					,lowerCamelCase__   :							str					,lowerCamelCase__   :							int					,lowerCamelCase__   :							Optional[Any]					,lowerCamelCase__   :							List[str]					):







								'''simple docstring'''
								_UpperCamelCase   :						Union[str, Any]												=  TFMobileBertForNextSentencePrediction(config=lowerCamelCase__					)
								_UpperCamelCase   :						Tuple												=  {'input_ids': input_ids, 'attention_mask': input_mask, 'token_type_ids': token_type_ids}
								_UpperCamelCase   :						Optional[Any]												=  model(lowerCamelCase__					)
								self.parent.assertEqual(result.logits.shape					,(self.batch_size, 2)					)





		def       UpperCamelCase_    (					self   :							int					,lowerCamelCase__   :							List[Any]					,lowerCamelCase__   :							List[str]					,lowerCamelCase__   :							int					,lowerCamelCase__   :							List[str]					,lowerCamelCase__   :							Dict					,lowerCamelCase__   :							Optional[int]					,lowerCamelCase__   :							Union[str, Any]					):







								'''simple docstring'''
								_UpperCamelCase   :						Union[str, Any]												=  TFMobileBertForPreTraining(config=lowerCamelCase__					)
								_UpperCamelCase   :						List[Any]												=  {'input_ids': input_ids, 'attention_mask': input_mask, 'token_type_ids': token_type_ids}
								_UpperCamelCase   :						str												=  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       UpperCamelCase_    (					self   :							Union[str, Any]					,lowerCamelCase__   :							str					,lowerCamelCase__   :							Union[str, Any]					,lowerCamelCase__   :							Optional[int]					,lowerCamelCase__   :							Tuple					,lowerCamelCase__   :							str					,lowerCamelCase__   :							List[Any]					,lowerCamelCase__   :							List[str]					):







								'''simple docstring'''
								_UpperCamelCase   :						List[Any]												=  self.num_labels
								_UpperCamelCase   :						Optional[int]												=  TFMobileBertForSequenceClassification(config=lowerCamelCase__					)
								_UpperCamelCase   :						Dict												=  {'input_ids': input_ids, 'attention_mask': input_mask, 'token_type_ids': token_type_ids}
								_UpperCamelCase   :						Optional[Any]												=  model(lowerCamelCase__					)
								self.parent.assertEqual(result.logits.shape					,(self.batch_size, self.num_labels)					)





		def       UpperCamelCase_    (					self   :							Union[str, Any]					,lowerCamelCase__   :							Tuple					,lowerCamelCase__   :							Optional[Any]					,lowerCamelCase__   :							Union[str, Any]					,lowerCamelCase__   :							List[Any]					,lowerCamelCase__   :							Any					,lowerCamelCase__   :							Tuple					,lowerCamelCase__   :							Dict					):







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





		def       UpperCamelCase_    (					self   :							int					,lowerCamelCase__   :							int					,lowerCamelCase__   :							Optional[int]					,lowerCamelCase__   :							Optional[int]					,lowerCamelCase__   :							int					,lowerCamelCase__   :							Dict					,lowerCamelCase__   :							str					,lowerCamelCase__   :							str					):







								'''simple docstring'''
								_UpperCamelCase   :						Union[str, Any]												=  self.num_labels
								_UpperCamelCase   :						List[Any]												=  TFMobileBertForTokenClassification(config=lowerCamelCase__					)
								_UpperCamelCase   :						Tuple												=  {'input_ids': input_ids, 'attention_mask': input_mask, 'token_type_ids': token_type_ids}
								_UpperCamelCase   :						Union[str, Any]												=  model(lowerCamelCase__					)
								self.parent.assertEqual(result.logits.shape					,(self.batch_size, self.seq_length, self.num_labels)					)





		def       UpperCamelCase_    (					self   :							Dict					,lowerCamelCase__   :							Optional[int]					,lowerCamelCase__   :							int					,lowerCamelCase__   :							List[str]					,lowerCamelCase__   :							int					,lowerCamelCase__   :							List[str]					,lowerCamelCase__   :							Optional[int]					,lowerCamelCase__   :							int					):







								'''simple docstring'''
								_UpperCamelCase   :						List[Any]												=  TFMobileBertForQuestionAnswering(config=lowerCamelCase__					)
								_UpperCamelCase   :						Dict												=  {'input_ids': input_ids, 'attention_mask': input_mask, 'token_type_ids': token_type_ids}
								_UpperCamelCase   :						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       UpperCamelCase_    (					self   :							Optional[Any]					):







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





	def       UpperCamelCase_    (					self   :							List[str]					):







							'''simple docstring'''
							_UpperCamelCase   :						List[str]												=  TFMobileBertModelTest.TFMobileBertModelTester(self					)
							_UpperCamelCase   :						Dict												=  ConfigTester(self					,config_class=lowerCamelCase__					,hidden_size=37					)





	def       UpperCamelCase_    (					self   :							str					):







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





	def       UpperCamelCase_    (					self   :							List[Any]					):







							'''simple docstring'''
							_UpperCamelCase   :						List[Any]												=  self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_mobilebert_model(*lowerCamelCase__					)





	def       UpperCamelCase_    (					self   :							int					):







							'''simple docstring'''
							_UpperCamelCase   :						Any												=  self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_mobilebert_for_masked_lm(*lowerCamelCase__					)





	def       UpperCamelCase_    (					self   :							str					):







							'''simple docstring'''
							_UpperCamelCase   :						List[str]												=  self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_mobilebert_for_multiple_choice(*lowerCamelCase__					)





	def       UpperCamelCase_    (					self   :							Optional[Any]					):







							'''simple docstring'''
							_UpperCamelCase   :						int												=  self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_mobilebert_for_next_sequence_prediction(*lowerCamelCase__					)





	def       UpperCamelCase_    (					self   :							Any					):







							'''simple docstring'''
							_UpperCamelCase   :						Optional[int]												=  self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_mobilebert_for_pretraining(*lowerCamelCase__					)





	def       UpperCamelCase_    (					self   :							List[Any]					):







							'''simple docstring'''
							_UpperCamelCase   :						List[str]												=  self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_mobilebert_for_question_answering(*lowerCamelCase__					)





	def       UpperCamelCase_    (					self   :							Any					):







							'''simple docstring'''
							_UpperCamelCase   :						List[str]												=  self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_mobilebert_for_sequence_classification(*lowerCamelCase__					)





	def       UpperCamelCase_    (					self   :							List[str]					):







							'''simple docstring'''
							_UpperCamelCase   :						Dict												=  self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_mobilebert_for_token_classification(*lowerCamelCase__					)





	@slow
	def       UpperCamelCase_    (					self   :							Union[str, Any]					):







							'''simple docstring'''
							# for model_name in TF_MOBILEBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
							for model_name in ["google/mobilebert-uncased"]:
													_UpperCamelCase   :						Tuple												=  TFMobileBertModel.from_pretrained(lowerCamelCase__					)
													self.assertIsNotNone(lowerCamelCase__					)






@require_tf
class 				lowercase__      (				unittest.TestCase  ):





	@slow
	def       UpperCamelCase_    (					self   :							int					):







							'''simple docstring'''
							_UpperCamelCase   :						Any												=  TFMobileBertForPreTraining.from_pretrained('google/mobilebert-uncased'					)
							_UpperCamelCase   :						str												=  tf.constant([[0, 1, 2, 3, 4, 5]]					)
							_UpperCamelCase   :						Union[str, Any]												=  model(lowerCamelCase__					)[0]

							_UpperCamelCase   :						Optional[Any]												=  [1, 6, 30522]
							self.assertEqual(output.shape					,lowerCamelCase__					)

							_UpperCamelCase   :						Optional[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]					,lowerCamelCase__					,atol=1E-4					)







'''simple docstring'''
import os
from pathlib import Path






def A__       (	):
						from torch.utils.cpp_extension import load

						_UpperCamelCase   :						Optional[Any]												=  Path(UpperCAmelCase_				).resolve().parent.parent.parent / 'kernels' / 'deformable_detr'
						_UpperCamelCase   :						Tuple												=  [
						    root / filename
						    for filename in [
						        'vision.cpp',
						        os.path.join('cpu'    ,    'ms_deform_attn_cpu.cpp'				),
						        os.path.join('cuda'    ,    'ms_deform_attn_cuda.cu'				),
						    ]
						]

						load(
						    'MultiScaleDeformableAttention'    ,    UpperCAmelCase_    ,    with_cuda=UpperCAmelCase_    ,    extra_include_paths=[str(UpperCAmelCase_				)]    ,    extra_cflags=['-DWITH_CUDA=1']    ,    extra_cuda_cflags=[
						        '-DCUDA_HAS_FP16=1',
						        '-D__CUDA_NO_HALF_OPERATORS__',
						        '-D__CUDA_NO_HALF_CONVERSIONS__',
						        '-D__CUDA_NO_HALF2_OPERATORS__',
						    ]    ,    )

						import MultiScaleDeformableAttention as MSDA

						return MSDA


