






"""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      SCREAMING_SNAKE_CASE  :





	"""simple docstring"""




	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.02     ,     lowerCAmelCase_=3     ,     lowerCAmelCase_=4     ,     lowerCAmelCase_=None     ,     ):
		A_		:						List[Any]	    =   parent
		A_		:						Any	    =   batch_size
		A_		:						Union[str, Any]	    =   seq_length
		A_		:						Dict	    =   is_training
		A_		:						int	    =   use_input_mask
		A_		:						Dict	    =   use_token_type_ids
		A_		:						List[Any]	    =   use_labels
		A_		:						Tuple	    =   vocab_size
		A_		:						Optional[int]	    =   hidden_size
		A_		:						Any	    =   embedding_size
		A_		:						int	    =   num_hidden_layers
		A_		:						Union[str, Any]	    =   num_attention_heads
		A_		:						Dict	    =   intermediate_size
		A_		:						Optional[int]	    =   hidden_act
		A_		:						Union[str, Any]	    =   hidden_dropout_prob
		A_		:						List[str]	    =   attention_probs_dropout_prob
		A_		:						Tuple	    =   max_position_embeddings
		A_		:						Tuple	    =   type_vocab_size
		A_		:						List[str]	    =   type_sequence_label_size
		A_		:						Union[str, Any]	    =   initializer_range
		A_		:						int	    =   num_labels
		A_		:						Dict	    =   num_choices
		A_		:						Dict	    =   scope





	def    lowerCamelCase(self							):
		A_		:						Union[str, Any]	    =   ids_tensor([self.batch_size, self.seq_length]     ,     self.vocab_size							)

		A_		:						Optional[Any]	    =   None
		if self.use_input_mask:
			A_		:						Tuple	    =   random_attention_mask([self.batch_size, self.seq_length]							)

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

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

		A_		:						Any	    =   self.get_config()

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





	def    lowerCamelCase(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    lowerCamelCase(self     ,     lowerCAmelCase_     ,     lowerCAmelCase_     ,     lowerCAmelCase_     ,     lowerCAmelCase_     ,     lowerCAmelCase_     ,     lowerCAmelCase_     ,     lowerCAmelCase_							):
		A_		:						Optional[int]	    =   MobileBertModel(config=lowerCAmelCase_							)
		model.to(lowerCAmelCase_							)
		model.eval()
		A_		:						List[str]	    =   model(lowerCAmelCase_     ,     attention_mask=lowerCAmelCase_     ,     token_type_ids=lowerCAmelCase_							)
		A_		:						List[str]	    =   model(lowerCAmelCase_     ,     token_type_ids=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    lowerCamelCase(self     ,     lowerCAmelCase_     ,     lowerCAmelCase_     ,     lowerCAmelCase_     ,     lowerCAmelCase_     ,     lowerCAmelCase_     ,     lowerCAmelCase_     ,     lowerCAmelCase_							):
		A_		:						Union[str, Any]	    =   MobileBertForMaskedLM(config=lowerCAmelCase_							)
		model.to(lowerCAmelCase_							)
		model.eval()
		A_		:						Optional[Any]	    =   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    lowerCamelCase(self     ,     lowerCAmelCase_     ,     lowerCAmelCase_     ,     lowerCAmelCase_     ,     lowerCAmelCase_     ,     lowerCAmelCase_     ,     lowerCAmelCase_     ,     lowerCAmelCase_							):
		A_		:						int	    =   MobileBertForNextSentencePrediction(config=lowerCAmelCase_							)
		model.to(lowerCAmelCase_							)
		model.eval()
		A_		:						Union[str, Any]	    =   model(
		    lowerCAmelCase_     ,     attention_mask=lowerCAmelCase_     ,     token_type_ids=lowerCAmelCase_     ,     labels=lowerCAmelCase_     ,     )
		self.parent.assertEqual(result.logits.shape     ,     (self.batch_size, 2)							)





	def    lowerCamelCase(self     ,     lowerCAmelCase_     ,     lowerCAmelCase_     ,     lowerCAmelCase_     ,     lowerCAmelCase_     ,     lowerCAmelCase_     ,     lowerCAmelCase_     ,     lowerCAmelCase_							):
		A_		:						Optional[Any]	    =   MobileBertForPreTraining(config=lowerCAmelCase_							)
		model.to(lowerCAmelCase_							)
		model.eval()
		A_		:						int	    =   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    lowerCamelCase(self     ,     lowerCAmelCase_     ,     lowerCAmelCase_     ,     lowerCAmelCase_     ,     lowerCAmelCase_     ,     lowerCAmelCase_     ,     lowerCAmelCase_     ,     lowerCAmelCase_							):
		A_		:						Tuple	    =   MobileBertForQuestionAnswering(config=lowerCAmelCase_							)
		model.to(lowerCAmelCase_							)
		model.eval()
		A_		:						Tuple	    =   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    lowerCamelCase(self     ,     lowerCAmelCase_     ,     lowerCAmelCase_     ,     lowerCAmelCase_     ,     lowerCAmelCase_     ,     lowerCAmelCase_     ,     lowerCAmelCase_     ,     lowerCAmelCase_							):
		A_		:						List[str]	    =   self.num_labels
		A_		:						Optional[int]	    =   MobileBertForSequenceClassification(lowerCAmelCase_							)
		model.to(lowerCAmelCase_							)
		model.eval()
		A_		:						Optional[int]	    =   model(lowerCAmelCase_     ,     attention_mask=lowerCAmelCase_     ,     token_type_ids=lowerCAmelCase_     ,     labels=lowerCAmelCase_							)
		self.parent.assertEqual(result.logits.shape     ,     (self.batch_size, self.num_labels)							)





	def    lowerCamelCase(self     ,     lowerCAmelCase_     ,     lowerCAmelCase_     ,     lowerCAmelCase_     ,     lowerCAmelCase_     ,     lowerCAmelCase_     ,     lowerCAmelCase_     ,     lowerCAmelCase_							):
		A_		:						Union[str, Any]	    =   self.num_labels
		A_		:						int	    =   MobileBertForTokenClassification(config=lowerCAmelCase_							)
		model.to(lowerCAmelCase_							)
		model.eval()
		A_		:						Optional[int]	    =   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    lowerCamelCase(self     ,     lowerCAmelCase_     ,     lowerCAmelCase_     ,     lowerCAmelCase_     ,     lowerCAmelCase_     ,     lowerCAmelCase_     ,     lowerCAmelCase_     ,     lowerCAmelCase_							):
		A_		:						str	    =   self.num_choices
		A_		:						List[str]	    =   MobileBertForMultipleChoice(config=lowerCAmelCase_							)
		model.to(lowerCAmelCase_							)
		model.eval()
		A_		:						int	    =   input_ids.unsqueeze(1							).expand(-1     ,     self.num_choices     ,     -1							).contiguous()
		A_		:						List[str]	    =   token_type_ids.unsqueeze(1							).expand(-1     ,     self.num_choices     ,     -1							).contiguous()
		A_		:						Optional[int]	    =   input_mask.unsqueeze(1							).expand(-1     ,     self.num_choices     ,     -1							).contiguous()
		A_		:						Dict	    =   model(
		    lowerCAmelCase_     ,     attention_mask=lowerCAmelCase_     ,     token_type_ids=lowerCAmelCase_     ,     labels=lowerCAmelCase_     ,     )
		self.parent.assertEqual(result.logits.shape     ,     (self.batch_size, self.num_choices)							)





	def    lowerCamelCase(self							):
		A_		:						str	    =   self.prepare_config_and_inputs()
		(
		    A_     
		)		:						Any	    =   config_and_inputs
		A_		:						Any	    =   {"""input_ids""": input_ids, """token_type_ids""": token_type_ids, """attention_mask""": input_mask}
		return config, inputs_dict






@require_torch
class      SCREAMING_SNAKE_CASE  (				_SCREAMING_SNAKE_CASE       ,       _SCREAMING_SNAKE_CASE       ,       unittest.TestCase						):





	"""simple docstring"""




	_A       :				Optional[int]    =				(
	    (
	        MobileBertModel,
	        MobileBertForMaskedLM,
	        MobileBertForMultipleChoice,
	        MobileBertForNextSentencePrediction,
	        MobileBertForPreTraining,
	        MobileBertForQuestionAnswering,
	        MobileBertForSequenceClassification,
	        MobileBertForTokenClassification,
	    )
	    if is_torch_available()
	    else ()
	)
	_A       :				Optional[Any]    =				(
	    {
	        """feature-extraction""": MobileBertModel,
	        """fill-mask""": MobileBertForMaskedLM,
	        """question-answering""": MobileBertForQuestionAnswering,
	        """text-classification""": MobileBertForSequenceClassification,
	        """token-classification""": MobileBertForTokenClassification,
	        """zero-shot""": MobileBertForSequenceClassification,
	    }
	    if is_torch_available()
	    else {}
	)
	_A       :				int    =				True





	def    lowerCamelCase(self     ,     lowerCAmelCase_     ,     lowerCAmelCase_     ,     lowerCAmelCase_=False							):
		A_		:						Union[str, Any]	    =   super()._prepare_for_class(lowerCAmelCase_     ,     lowerCAmelCase_     ,     return_labels=lowerCAmelCase_							)

		if return_labels:
			if model_class in get_values(lowerCAmelCase_							):
				A_		:						Dict	    =   torch.zeros(
				    (self.model_tester.batch_size, self.model_tester.seq_length)     ,     dtype=torch.long     ,     device=lowerCAmelCase_							)
				A_		:						int	    =   torch.zeros(
				    self.model_tester.batch_size     ,     dtype=torch.long     ,     device=lowerCAmelCase_							)
		return inputs_dict





	def    lowerCamelCase(self							):
		A_		:						Union[str, Any]	    =   MobileBertModelTester(self							)
		A_		:						Dict	    =   ConfigTester(self     ,     config_class=lowerCAmelCase_     ,     hidden_size=37							)





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





	def    lowerCamelCase(self							):
		A_		:						int	    =   self.model_tester.prepare_config_and_inputs()
		self.model_tester.create_and_check_mobilebert_model(*lowerCAmelCase_							)





	def    lowerCamelCase(self							):
		A_		:						int	    =   self.model_tester.prepare_config_and_inputs()
		self.model_tester.create_and_check_mobilebert_for_masked_lm(*lowerCAmelCase_							)





	def    lowerCamelCase(self							):
		A_		:						int	    =   self.model_tester.prepare_config_and_inputs()
		self.model_tester.create_and_check_mobilebert_for_multiple_choice(*lowerCAmelCase_							)





	def    lowerCamelCase(self							):
		A_		:						Optional[Any]	    =   self.model_tester.prepare_config_and_inputs()
		self.model_tester.create_and_check_mobilebert_for_next_sequence_prediction(*lowerCAmelCase_							)





	def    lowerCamelCase(self							):
		A_		:						str	    =   self.model_tester.prepare_config_and_inputs()
		self.model_tester.create_and_check_mobilebert_for_pretraining(*lowerCAmelCase_							)





	def    lowerCamelCase(self							):
		A_		:						Tuple	    =   self.model_tester.prepare_config_and_inputs()
		self.model_tester.create_and_check_mobilebert_for_question_answering(*lowerCAmelCase_							)





	def    lowerCamelCase(self							):
		A_		:						List[Any]	    =   self.model_tester.prepare_config_and_inputs()
		self.model_tester.create_and_check_mobilebert_for_sequence_classification(*lowerCAmelCase_							)





	def    lowerCamelCase(self							):
		A_		:						Any	    =   self.model_tester.prepare_config_and_inputs()
		self.model_tester.create_and_check_mobilebert_for_token_classification(*lowerCAmelCase_							)






def 			__UpperCamelCase	(		snake_case__  ):
	return torch.tensor(
	    snake_case__   ,   dtype=torch.long   ,   device=snake_case__   ,   )


_lowerCAmelCase         =							1e-3




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





	"""simple docstring"""




	@slow
	def    lowerCamelCase(self							):
		A_		:						Tuple	    =   MobileBertModel.from_pretrained("""google/mobilebert-uncased"""							).to(lowerCAmelCase_							)
		A_		:						Tuple	    =   _long_tensor([[101, 7110, 1005, 1056, 2023, 11333, 17413, 1029, 102]]							)
		with torch.no_grad():
			A_		:						str	    =   model(lowerCAmelCase_							)[0]
		A_		:						Any	    =   torch.Size((1, 9, 512)							)
		self.assertEqual(output.shape     ,     lowerCAmelCase_							)
		A_		:						Optional[int]	    =   torch.tensor(
		    [
		        [
		            [-2.4_7_3_6_5_2_6e0_7, 8.2_6_9_1_6_5_6e0_4, 1.6_5_2_1_8_3_8e0_5],
		            [-5.7_5_4_1_7_0_4e-0_1, 3.9_0_5_6_0_2_2e0_0, 4.4_0_1_1_5_0_7e0_0],
		            [2.6_0_4_7_3_5_9e0_0, 1.5_6_7_7_6_5_2e0_0, -1.7_3_2_4_1_8_8e-0_1],
		        ]
		    ]     ,     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
		A_		:						Dict	    =   torch.all((expected_slice / output[..., :3, :3]) >= 1 - TOLERANCE							)
		A_		:						List[str]	    =   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


_lowerCAmelCase         =							logging.get_logger(__name__)

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



class      SCREAMING_SNAKE_CASE  (				_SCREAMING_SNAKE_CASE						):





	"""simple docstring"""




	_A       :				List[str]    =				"""deta"""
	_A       :				Tuple    =				{
	    """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.02     ,     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.25     ,     **lowerCAmelCase_     ,     ):
		if backbone_config is None:
			logger.info("""`backbone_config` is `None`. Initializing the config with the default `ResNet` backbone."""							)
			A_		:						Any	    =   CONFIG_MAPPING["""resnet"""](out_features=["""stage2""", """stage3""", """stage4"""]							)
		else:
			if isinstance(lowerCAmelCase_     ,     lowerCAmelCase_							):
				A_		:						List[str]	    =   backbone_config.pop("""model_type"""							)
				A_		:						List[str]	    =   CONFIG_MAPPING[backbone_model_type]
				A_		:						Tuple	    =   config_class.from_dict(lowerCAmelCase_							)

		A_		:						List[Any]	    =   backbone_config
		A_		:						List[str]	    =   num_queries
		A_		:						List[Any]	    =   max_position_embeddings
		A_		:						int	    =   d_model
		A_		:						Optional[Any]	    =   encoder_ffn_dim
		A_		:						Optional[Any]	    =   encoder_layers
		A_		:						List[Any]	    =   encoder_attention_heads
		A_		:						int	    =   decoder_ffn_dim
		A_		:						Dict	    =   decoder_layers
		A_		:						Dict	    =   decoder_attention_heads
		A_		:						Optional[Any]	    =   dropout
		A_		:						str	    =   attention_dropout
		A_		:						Any	    =   activation_dropout
		A_		:						Union[str, Any]	    =   activation_function
		A_		:						int	    =   init_std
		A_		:						List[Any]	    =   init_xavier_std
		A_		:						List[Any]	    =   encoder_layerdrop
		A_		:						Any	    =   auxiliary_loss
		A_		:						Tuple	    =   position_embedding_type
		# deformable attributes
		A_		:						List[str]	    =   num_feature_levels
		A_		:						Tuple	    =   encoder_n_points
		A_		:						str	    =   decoder_n_points
		A_		:						List[Any]	    =   two_stage
		A_		:						Optional[Any]	    =   two_stage_num_proposals
		A_		:						Union[str, Any]	    =   with_box_refine
		A_		:						Optional[int]	    =   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
		A_		:						List[Any]	    =   class_cost
		A_		:						str	    =   bbox_cost
		A_		:						Tuple	    =   giou_cost
		# Loss coefficients
		A_		:						int	    =   mask_loss_coefficient
		A_		:						Union[str, Any]	    =   dice_loss_coefficient
		A_		:						Union[str, Any]	    =   bbox_loss_coefficient
		A_		:						Optional[Any]	    =   giou_loss_coefficient
		A_		:						List[str]	    =   eos_coefficient
		A_		:						Optional[Any]	    =   focal_alpha
		super().__init__(is_encoder_decoder=lowerCAmelCase_     ,     **lowerCAmelCase_							)





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





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





	def    lowerCamelCase(self							):
		A_		:						str	    =   copy.deepcopy(self.__dict__							)
		A_		:						int	    =   self.backbone_config.to_dict()
		A_		:						Dict	    =   self.__class__.model_type
		return output

