"""simple docstring"""


import unittest

from transformers import MobileBertConfig, is_torch_available
from transformers.models.auto import get_values
from transformers.testing_utils import require_sentencepiece, require_tokenizers, require_torch, slow, torch_device

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


if is_torch_available():
	import torch

	from transformers import (
	    MODEL_FOR_PRETRAINING_MAPPING,
	    MobileBertForMaskedLM,
	    MobileBertForMultipleChoice,
	    MobileBertForNextSentencePrediction,
	    MobileBertForPreTraining,
	    MobileBertForQuestionAnswering,
	    MobileBertForSequenceClassification,
	    MobileBertForTokenClassification,
	    MobileBertModel,
	)






class   __UpperCAmelCase		:

				def __init__( self      ,					_lowerCamelCase      ,					_lowerCamelCase=13      ,					_lowerCamelCase=7      ,					_lowerCamelCase=True      ,					_lowerCamelCase=True      ,					_lowerCamelCase=True      ,					_lowerCamelCase=True      ,					_lowerCamelCase=99      ,					_lowerCamelCase=64      ,					_lowerCamelCase=32      ,					_lowerCamelCase=5      ,					_lowerCamelCase=4      ,					_lowerCamelCase=37      ,					_lowerCamelCase="gelu"      ,					_lowerCamelCase=0.1      ,					_lowerCamelCase=0.1      ,					_lowerCamelCase=512      ,					_lowerCamelCase=16      ,					_lowerCamelCase=2      ,					_lowerCamelCase=0.0_2      ,					_lowerCamelCase=3      ,					_lowerCamelCase=4      ,					_lowerCamelCase=None      ,					):
										lowerCamelCase__   =parent
										lowerCamelCase__   =batch_size
										lowerCamelCase__   =seq_length
										lowerCamelCase__   =is_training
										lowerCamelCase__   =use_input_mask
										lowerCamelCase__   =use_token_type_ids
										lowerCamelCase__   =use_labels
										lowerCamelCase__   =vocab_size
										lowerCamelCase__   =hidden_size
										lowerCamelCase__   =embedding_size
										lowerCamelCase__   =num_hidden_layers
										lowerCamelCase__   =num_attention_heads
										lowerCamelCase__   =intermediate_size
										lowerCamelCase__   =hidden_act
										lowerCamelCase__   =hidden_dropout_prob
										lowerCamelCase__   =attention_probs_dropout_prob
										lowerCamelCase__   =max_position_embeddings
										lowerCamelCase__   =type_vocab_size
										lowerCamelCase__   =type_sequence_label_size
										lowerCamelCase__   =initializer_range
										lowerCamelCase__   =num_labels
										lowerCamelCase__   =num_choices
										lowerCamelCase__   =scope

				def 						_a      ( self ):
										lowerCamelCase__   =ids_tensor([self.batch_size, self.seq_length]      ,					self.vocab_size )

										lowerCamelCase__   =None
										if self.use_input_mask:
																lowerCamelCase__   =random_attention_mask([self.batch_size, self.seq_length] )

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

										lowerCamelCase__   =None
										lowerCamelCase__   =None
										lowerCamelCase__   =None
										if self.use_labels:
																lowerCamelCase__   =ids_tensor([self.batch_size]      ,					self.type_sequence_label_size )
																lowerCamelCase__   =ids_tensor([self.batch_size, self.seq_length]      ,					self.num_labels )
																lowerCamelCase__   =ids_tensor([self.batch_size]      ,					self.num_choices )

										lowerCamelCase__   =self.get_config()

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

				def 						_a      ( self ):
										return 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      ,					embedding_size=self.embedding_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      ,					is_decoder=_lowerCamelCase      ,					initializer_range=self.initializer_range      ,					)

				def 						_a      ( self      ,					_lowerCamelCase      ,					_lowerCamelCase      ,					_lowerCamelCase      ,					_lowerCamelCase      ,					_lowerCamelCase      ,					_lowerCamelCase      ,					_lowerCamelCase ):
										lowerCamelCase__   =MobileBertModel(config=_lowerCamelCase )
										model.to(_lowerCamelCase )
										model.eval()
										lowerCamelCase__   =model(_lowerCamelCase      ,					attention_mask=_lowerCamelCase      ,					token_type_ids=_lowerCamelCase )
										lowerCamelCase__   =model(_lowerCamelCase      ,					token_type_ids=_lowerCamelCase )
										lowerCamelCase__   =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 ):
										lowerCamelCase__   =MobileBertForMaskedLM(config=_lowerCamelCase )
										model.to(_lowerCamelCase )
										model.eval()
										lowerCamelCase__   =model(_lowerCamelCase      ,					attention_mask=_lowerCamelCase      ,					token_type_ids=_lowerCamelCase      ,					labels=_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 ):
										lowerCamelCase__   =MobileBertForNextSentencePrediction(config=_lowerCamelCase )
										model.to(_lowerCamelCase )
										model.eval()
										lowerCamelCase__   =model(
										    _lowerCamelCase      ,					attention_mask=_lowerCamelCase      ,					token_type_ids=_lowerCamelCase      ,					labels=_lowerCamelCase      ,					)
										self.parent.assertEqual(result.logits.shape      ,					(self.batch_size, 2) )

				def 						_a      ( self      ,					_lowerCamelCase      ,					_lowerCamelCase      ,					_lowerCamelCase      ,					_lowerCamelCase      ,					_lowerCamelCase      ,					_lowerCamelCase      ,					_lowerCamelCase ):
										lowerCamelCase__   =MobileBertForPreTraining(config=_lowerCamelCase )
										model.to(_lowerCamelCase )
										model.eval()
										lowerCamelCase__   =model(
										    _lowerCamelCase      ,					attention_mask=_lowerCamelCase      ,					token_type_ids=_lowerCamelCase      ,					labels=_lowerCamelCase      ,					next_sentence_label=_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 ):
										lowerCamelCase__   =MobileBertForQuestionAnswering(config=_lowerCamelCase )
										model.to(_lowerCamelCase )
										model.eval()
										lowerCamelCase__   =model(
										    _lowerCamelCase      ,					attention_mask=_lowerCamelCase      ,					token_type_ids=_lowerCamelCase      ,					start_positions=_lowerCamelCase      ,					end_positions=_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      ,					_lowerCamelCase      ,					_lowerCamelCase      ,					_lowerCamelCase      ,					_lowerCamelCase      ,					_lowerCamelCase      ,					_lowerCamelCase      ,					_lowerCamelCase ):
										lowerCamelCase__   =self.num_labels
										lowerCamelCase__   =MobileBertForSequenceClassification(_lowerCamelCase )
										model.to(_lowerCamelCase )
										model.eval()
										lowerCamelCase__   =model(_lowerCamelCase      ,					attention_mask=_lowerCamelCase      ,					token_type_ids=_lowerCamelCase      ,					labels=_lowerCamelCase )
										self.parent.assertEqual(result.logits.shape      ,					(self.batch_size, self.num_labels) )

				def 						_a      ( self      ,					_lowerCamelCase      ,					_lowerCamelCase      ,					_lowerCamelCase      ,					_lowerCamelCase      ,					_lowerCamelCase      ,					_lowerCamelCase      ,					_lowerCamelCase ):
										lowerCamelCase__   =self.num_labels
										lowerCamelCase__   =MobileBertForTokenClassification(config=_lowerCamelCase )
										model.to(_lowerCamelCase )
										model.eval()
										lowerCamelCase__   =model(_lowerCamelCase      ,					attention_mask=_lowerCamelCase      ,					token_type_ids=_lowerCamelCase      ,					labels=_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 ):
										lowerCamelCase__   =self.num_choices
										lowerCamelCase__   =MobileBertForMultipleChoice(config=_lowerCamelCase )
										model.to(_lowerCamelCase )
										model.eval()
										lowerCamelCase__   =input_ids.unsqueeze(1 ).expand(-1      ,					self.num_choices      ,					-1 ).contiguous()
										lowerCamelCase__   =token_type_ids.unsqueeze(1 ).expand(-1      ,					self.num_choices      ,					-1 ).contiguous()
										lowerCamelCase__   =input_mask.unsqueeze(1 ).expand(-1      ,					self.num_choices      ,					-1 ).contiguous()
										lowerCamelCase__   =model(
										    _lowerCamelCase      ,					attention_mask=_lowerCamelCase      ,					token_type_ids=_lowerCamelCase      ,					labels=_lowerCamelCase      ,					)
										self.parent.assertEqual(result.logits.shape      ,					(self.batch_size, self.num_choices) )






				def 						_a      ( self ):
										lowerCamelCase__   =self.prepare_config_and_inputs()
										(
										    (
										    lowerCamelCase__
										)   , (
										    lowerCamelCase__
										)   , (
										    lowerCamelCase__
										)   , (
										    lowerCamelCase__
										)   , (
										    lowerCamelCase__
										)   , (
										    lowerCamelCase__
										)   , (
										    lowerCamelCase__
										)   , 
										)   =config_and_inputs
										lowerCamelCase__   ={"input_ids": input_ids, "token_type_ids": token_type_ids, "attention_mask": input_mask}
										return config, inputs_dict






@require_torch
class   __UpperCAmelCase		(      __lowerCAmelCase , __lowerCAmelCase , unittest.TestCase      ):
				A__ :  Union[str, Any]    				=			(
				    (
				        MobileBertModel,
				        MobileBertForMaskedLM,
				        MobileBertForMultipleChoice,
				        MobileBertForNextSentencePrediction,
				        MobileBertForPreTraining,
				        MobileBertForQuestionAnswering,
				        MobileBertForSequenceClassification,
				        MobileBertForTokenClassification,
				    )
				    if is_torch_available()
				    else ()
				)
				A__ :  Optional[int]    				=			(
				    {
				        '''feature-extraction''': MobileBertModel,
				        '''fill-mask''': MobileBertForMaskedLM,
				        '''question-answering''': MobileBertForQuestionAnswering,
				        '''text-classification''': MobileBertForSequenceClassification,
				        '''token-classification''': MobileBertForTokenClassification,
				        '''zero-shot''': MobileBertForSequenceClassification,
				    }
				    if is_torch_available()
				    else {}
				)
				A__ :  Dict    				=			True

				def 						_a      ( self      ,					_lowerCamelCase      ,					_lowerCamelCase      ,					_lowerCamelCase=False ):
										lowerCamelCase__   =super()._prepare_for_class(_lowerCamelCase      ,					_lowerCamelCase      ,					return_labels=_lowerCamelCase )

										if return_labels:
																if model_class in get_values(_lowerCamelCase ):
																						lowerCamelCase__   =torch.zeros(
																						    (self.model_tester.batch_size, self.model_tester.seq_length)      ,					dtype=torch.long      ,					device=_lowerCamelCase )
																						lowerCamelCase__   =torch.zeros(
																						    self.model_tester.batch_size      ,					dtype=torch.long      ,					device=_lowerCamelCase )
										return inputs_dict

				def 						_a      ( self ):
										lowerCamelCase__   =MobileBertModelTester(self )
										lowerCamelCase__   =ConfigTester(self      ,					config_class=_lowerCamelCase      ,					hidden_size=37 )

				def 						_a      ( self ):
										self.config_tester.run_common_tests()

				def 						_a      ( self ):
										lowerCamelCase__   =self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_mobilebert_model(*_lowerCamelCase )

				def 						_a      ( self ):
										lowerCamelCase__   =self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_mobilebert_for_masked_lm(*_lowerCamelCase )

				def 						_a      ( self ):
										lowerCamelCase__   =self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_mobilebert_for_multiple_choice(*_lowerCamelCase )

				def 						_a      ( self ):
										lowerCamelCase__   =self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_mobilebert_for_next_sequence_prediction(*_lowerCamelCase )

				def 						_a      ( self ):
										lowerCamelCase__   =self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_mobilebert_for_pretraining(*_lowerCamelCase )

				def 						_a      ( self ):
										lowerCamelCase__   =self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_mobilebert_for_question_answering(*_lowerCamelCase )

				def 						_a      ( self ):
										lowerCamelCase__   =self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_mobilebert_for_sequence_classification(*_lowerCamelCase )






				def 						_a      ( self ):
										lowerCamelCase__   =self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_mobilebert_for_token_classification(*_lowerCamelCase )





def      lowerCamelCase_					(   __lowerCAmelCase							)				->   Any:




						'''simple docstring'''



						return torch.tensor(
						    __lowerCAmelCase  ,       dtype=torch.long  ,       device=__lowerCAmelCase  ,       )


a				=1E-3






@require_torch
@require_sentencepiece
@require_tokenizers
class   __UpperCAmelCase		(      unittest.TestCase      ):

				@slow
				def 						_a      ( self ):
										lowerCamelCase__   =MobileBertModel.from_pretrained("google/mobilebert-uncased" ).to(_lowerCamelCase )
										lowerCamelCase__   =_long_tensor([[101, 7110, 1005, 1056, 2023, 11333, 17413, 1029, 102]] )
										with torch.no_grad():
																lowerCamelCase__   =model(_lowerCamelCase )[0]
										lowerCamelCase__   =torch.Size((1, 9, 512) )
										self.assertEqual(output.shape      ,					_lowerCamelCase )
										lowerCamelCase__   =torch.tensor(
										    [
										        [
										            [-2.473_6526E07, 8.269_1656E04, 1.652_1838E05],
										            [-5.754_1704E-01, 3.905_6022E00, 4.401_1507E00],
										            [2.604_7359E00, 1.567_7652E00, -1.732_4188E-01],
										        ]
										    ]      ,					device=_lowerCamelCase      ,					)

										# MobileBERT results range from 10e0 to 10e8. Even a 0.0000001% difference with a value of 10e8 results in a
										# ~1 difference, it's therefore not a good idea to measure using addition.
										# Here, we instead divide the expected result with the result in order to obtain ~1. We then check that the
										# result is held between bounds: 1 - TOLERANCE < expected_result / result < 1 + TOLERANCE
										lowerCamelCase__   =torch.all((expected_slice / output[..., :3, :3]) >= 1 - TOLERANCE )
										lowerCamelCase__   =torch.all((expected_slice / output[..., :3, :3]) <= 1 + TOLERANCE )

										self.assertTrue(lower_bound and upper_bound )



"""simple docstring"""


import copy

from ...configuration_utils import PretrainedConfig
from ...utils import logging
from ..auto import CONFIG_MAPPING


a				=logging.get_logger(__name__)

a				={
    'ut/deta': 'https://huggingface.co/ut/deta/resolve/main/config.json',
}






class   __UpperCAmelCase		(      __lowerCAmelCase      ):
				A__ :  str    				=			'''deta'''
				A__ :  List[str]    				=			{
				    '''hidden_size''': '''d_model''',
				    '''num_attention_heads''': '''encoder_attention_heads''',
				}

				def __init__( self      ,					_lowerCamelCase=None      ,					_lowerCamelCase=900      ,					_lowerCamelCase=2048      ,					_lowerCamelCase=6      ,					_lowerCamelCase=2048      ,					_lowerCamelCase=8      ,					_lowerCamelCase=6      ,					_lowerCamelCase=1024      ,					_lowerCamelCase=8      ,					_lowerCamelCase=0.0      ,					_lowerCamelCase=True      ,					_lowerCamelCase="relu"      ,					_lowerCamelCase=256      ,					_lowerCamelCase=0.1      ,					_lowerCamelCase=0.0      ,					_lowerCamelCase=0.0      ,					_lowerCamelCase=0.0_2      ,					_lowerCamelCase=1.0      ,					_lowerCamelCase=True      ,					_lowerCamelCase=False      ,					_lowerCamelCase="sine"      ,					_lowerCamelCase=5      ,					_lowerCamelCase=4      ,					_lowerCamelCase=4      ,					_lowerCamelCase=True      ,					_lowerCamelCase=300      ,					_lowerCamelCase=True      ,					_lowerCamelCase=True      ,					_lowerCamelCase=1      ,					_lowerCamelCase=5      ,					_lowerCamelCase=2      ,					_lowerCamelCase=1      ,					_lowerCamelCase=1      ,					_lowerCamelCase=5      ,					_lowerCamelCase=2      ,					_lowerCamelCase=0.1      ,					_lowerCamelCase=0.2_5      ,					**_lowerCamelCase      ,					):
										if backbone_config is None:
																logger.info("`backbone_config` is `None`. Initializing the config with the default `ResNet` backbone." )
																lowerCamelCase__   =CONFIG_MAPPING["resnet"](out_features=["stage2", "stage3", "stage4"] )
										else:
																if isinstance(_lowerCamelCase      ,					_lowerCamelCase ):
																						lowerCamelCase__   =backbone_config.pop("model_type" )
																						lowerCamelCase__   =CONFIG_MAPPING[backbone_model_type]
																						lowerCamelCase__   =config_class.from_dict(_lowerCamelCase )

										lowerCamelCase__   =backbone_config
										lowerCamelCase__   =num_queries
										lowerCamelCase__   =max_position_embeddings
										lowerCamelCase__   =d_model
										lowerCamelCase__   =encoder_ffn_dim
										lowerCamelCase__   =encoder_layers
										lowerCamelCase__   =encoder_attention_heads
										lowerCamelCase__   =decoder_ffn_dim
										lowerCamelCase__   =decoder_layers
										lowerCamelCase__   =decoder_attention_heads
										lowerCamelCase__   =dropout
										lowerCamelCase__   =attention_dropout
										lowerCamelCase__   =activation_dropout
										lowerCamelCase__   =activation_function
										lowerCamelCase__   =init_std
										lowerCamelCase__   =init_xavier_std
										lowerCamelCase__   =encoder_layerdrop
										lowerCamelCase__   =auxiliary_loss
										lowerCamelCase__   =position_embedding_type
										# deformable attributes
										lowerCamelCase__   =num_feature_levels
										lowerCamelCase__   =encoder_n_points
										lowerCamelCase__   =decoder_n_points
										lowerCamelCase__   =two_stage
										lowerCamelCase__   =two_stage_num_proposals
										lowerCamelCase__   =with_box_refine
										lowerCamelCase__   =assign_first_stage
										if two_stage is True and with_box_refine is False:
																raise ValueError("If two_stage is True, with_box_refine must be True." )
										# Hungarian matcher
										lowerCamelCase__   =class_cost
										lowerCamelCase__   =bbox_cost
										lowerCamelCase__   =giou_cost
										# Loss coefficients
										lowerCamelCase__   =mask_loss_coefficient
										lowerCamelCase__   =dice_loss_coefficient
										lowerCamelCase__   =bbox_loss_coefficient
										lowerCamelCase__   =giou_loss_coefficient
										lowerCamelCase__   =eos_coefficient
										lowerCamelCase__   =focal_alpha
										super().__init__(is_encoder_decoder=_lowerCamelCase      ,					**_lowerCamelCase )

				@property
				def 						_a      ( self ):
										return self.encoder_attention_heads

				@property
				def 						_a      ( self ):
										return self.d_model






				def 						_a      ( self ):
										lowerCamelCase__   =copy.deepcopy(self.__dict__ )
										lowerCamelCase__   =self.backbone_config.to_dict()
										lowerCamelCase__   =self.__class__.model_type
										return output


