
import unittest

from transformers import AlbertConfig, is_torch_available
from transformers.models.auto import get_values
from transformers.testing_utils import 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,
		    AlbertForMaskedLM,
		    AlbertForMultipleChoice,
		    AlbertForPreTraining,
		    AlbertForQuestionAnswering,
		    AlbertForSequenceClassification,
		    AlbertForTokenClassification,
		    AlbertModel,
		)
		from transformers.models.albert.modeling_albert import ALBERT_PRETRAINED_MODEL_ARCHIVE_LIST




class  lowerCamelCase__							:




	'''simple docstring'''




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







	def    _lowerCamelCase				(					self		:str						)		-> int:
								__UpperCamelCase	:   Any			   =					ids_tensor([self.batch_size, self.seq_length]     ,   self.vocab_size						)

								__UpperCamelCase	:   List[str]			   =					None
								if self.use_input_mask:
															__UpperCamelCase	:   str			   =					random_attention_mask([self.batch_size, self.seq_length]						)

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

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

								__UpperCamelCase	:   Optional[int]			   =					self.get_config()

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







	def    _lowerCamelCase				(					self		:Tuple						)		-> List[Any]:
								return AlbertConfig(
								    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     ,   num_hidden_groups=self.num_hidden_groups     ,   )







	def    _lowerCamelCase				(					self		:int     ,   a		:str     ,   a		:int     ,   a		:Any     ,   a		:Optional[int]     ,   a		:List[Any]     ,   a		:str     ,   a		:Optional[Any]						)		-> List[Any]:
								__UpperCamelCase	:   str			   =					AlbertModel(config=_UpperCAmelCase						)
								model.to(_UpperCAmelCase						)
								model.eval()
								__UpperCamelCase	:   int			   =					model(_UpperCAmelCase     ,   attention_mask=_UpperCAmelCase     ,   token_type_ids=_UpperCAmelCase						)
								__UpperCamelCase	:   str			   =					model(_UpperCAmelCase     ,   token_type_ids=_UpperCAmelCase						)
								__UpperCamelCase	:   int			   =					model(_UpperCAmelCase						)
								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		:str     ,   a		:Union[str, Any]     ,   a		:Any     ,   a		:str     ,   a		:Optional[int]     ,   a		:List[str]     ,   a		:Tuple     ,   a		:Union[str, Any]						)		-> int:
								__UpperCamelCase	:   Optional[Any]			   =					AlbertForPreTraining(config=_UpperCAmelCase						)
								model.to(_UpperCAmelCase						)
								model.eval()
								__UpperCamelCase	:   List[str]			   =					model(
								    _UpperCAmelCase     ,   attention_mask=_UpperCAmelCase     ,   token_type_ids=_UpperCAmelCase     ,   labels=_UpperCAmelCase     ,   sentence_order_label=_UpperCAmelCase     ,   )
								self.parent.assertEqual(result.prediction_logits.shape     ,   (self.batch_size, self.seq_length, self.vocab_size)						)
								self.parent.assertEqual(result.sop_logits.shape     ,   (self.batch_size, config.num_labels)						)







	def    _lowerCamelCase				(					self		:List[str]     ,   a		:List[str]     ,   a		:List[Any]     ,   a		:Optional[int]     ,   a		:List[Any]     ,   a		:Optional[Any]     ,   a		:Union[str, Any]     ,   a		:str						)		-> Optional[Any]:
								__UpperCamelCase	:   str			   =					AlbertForMaskedLM(config=_UpperCAmelCase						)
								model.to(_UpperCAmelCase						)
								model.eval()
								__UpperCamelCase	:   Union[str, Any]			   =					model(_UpperCAmelCase     ,   attention_mask=_UpperCAmelCase     ,   token_type_ids=_UpperCAmelCase     ,   labels=_UpperCAmelCase						)
								self.parent.assertEqual(result.logits.shape     ,   (self.batch_size, self.seq_length, self.vocab_size)						)







	def    _lowerCamelCase				(					self		:Any     ,   a		:Dict     ,   a		:List[Any]     ,   a		:str     ,   a		:Dict     ,   a		:Any     ,   a		:Union[str, Any]     ,   a		:Union[str, Any]						)		-> int:
								__UpperCamelCase	:   Any			   =					AlbertForQuestionAnswering(config=_UpperCAmelCase						)
								model.to(_UpperCAmelCase						)
								model.eval()
								__UpperCamelCase	:   Dict			   =					model(
								    _UpperCAmelCase     ,   attention_mask=_UpperCAmelCase     ,   token_type_ids=_UpperCAmelCase     ,   start_positions=_UpperCAmelCase     ,   end_positions=_UpperCAmelCase     ,   )
								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     ,   a		:Optional[int]     ,   a		:Optional[int]     ,   a		:int     ,   a		:List[str]     ,   a		:int     ,   a		:Any     ,   a		:Dict						)		-> str:
								__UpperCamelCase	:   str			   =					self.num_labels
								__UpperCamelCase	:   Any			   =					AlbertForSequenceClassification(_UpperCAmelCase						)
								model.to(_UpperCAmelCase						)
								model.eval()
								__UpperCamelCase	:   int			   =					model(_UpperCAmelCase     ,   attention_mask=_UpperCAmelCase     ,   token_type_ids=_UpperCAmelCase     ,   labels=_UpperCAmelCase						)
								self.parent.assertEqual(result.logits.shape     ,   (self.batch_size, self.num_labels)						)







	def    _lowerCamelCase				(					self		:int     ,   a		:Optional[Any]     ,   a		:Union[str, Any]     ,   a		:List[Any]     ,   a		:str     ,   a		:Optional[Any]     ,   a		:Union[str, Any]     ,   a		:List[Any]						)		-> Union[str, Any]:
								__UpperCamelCase	:   List[Any]			   =					self.num_labels
								__UpperCamelCase	:   Dict			   =					AlbertForTokenClassification(config=_UpperCAmelCase						)
								model.to(_UpperCAmelCase						)
								model.eval()
								__UpperCamelCase	:   Any			   =					model(_UpperCAmelCase     ,   attention_mask=_UpperCAmelCase     ,   token_type_ids=_UpperCAmelCase     ,   labels=_UpperCAmelCase						)
								self.parent.assertEqual(result.logits.shape     ,   (self.batch_size, self.seq_length, self.num_labels)						)







	def    _lowerCamelCase				(					self		:List[Any]     ,   a		:Optional[int]     ,   a		:List[Any]     ,   a		:Union[str, Any]     ,   a		:Optional[int]     ,   a		:int     ,   a		:Any     ,   a		:str						)		-> Optional[int]:
								__UpperCamelCase	:   Tuple			   =					self.num_choices
								__UpperCamelCase	:   List[Any]			   =					AlbertForMultipleChoice(config=_UpperCAmelCase						)
								model.to(_UpperCAmelCase						)
								model.eval()
								__UpperCamelCase	:   Any			   =					input_ids.unsqueeze(1						).expand(-1     ,   self.num_choices     ,   -1						).contiguous()
								__UpperCamelCase	:   Optional[int]			   =					token_type_ids.unsqueeze(1						).expand(-1     ,   self.num_choices     ,   -1						).contiguous()
								__UpperCamelCase	:   str			   =					input_mask.unsqueeze(1						).expand(-1     ,   self.num_choices     ,   -1						).contiguous()
								__UpperCamelCase	:   str			   =					model(
								    _UpperCAmelCase     ,   attention_mask=_UpperCAmelCase     ,   token_type_ids=_UpperCAmelCase     ,   labels=_UpperCAmelCase     ,   )
								self.parent.assertEqual(result.logits.shape     ,   (self.batch_size, self.num_choices)						)







	def    _lowerCamelCase				(					self		:List[str]						)		-> Dict:
								__UpperCamelCase	:   Dict			   =					self.prepare_config_and_inputs()
								(
								    __UpperCamelCase 
								)	:   Any			   =					config_and_inputs
								__UpperCamelCase	:   Optional[Any]			   =					{'''input_ids''': input_ids, '''token_type_ids''': token_type_ids, '''attention_mask''': input_mask}
								return config, inputs_dict




@require_torch
class  lowerCamelCase__							(				_UpperCAmelCase     ,						_UpperCAmelCase     ,						unittest.TestCase):




	'''simple docstring'''




	_A								=     (
	    (
	        AlbertModel,
	        AlbertForPreTraining,
	        AlbertForMaskedLM,
	        AlbertForMultipleChoice,
	        AlbertForSequenceClassification,
	        AlbertForTokenClassification,
	        AlbertForQuestionAnswering,
	    )
	    if is_torch_available()
	    else ()
	)
	_A								=     (
	    {
	        "feature-extraction": AlbertModel,
	        "fill-mask": AlbertForMaskedLM,
	        "question-answering": AlbertForQuestionAnswering,
	        "text-classification": AlbertForSequenceClassification,
	        "token-classification": AlbertForTokenClassification,
	        "zero-shot": AlbertForSequenceClassification,
	    }
	    if is_torch_available()
	    else {}
	)
	_A								=     True







	def    _lowerCamelCase				(					self		:str     ,   a		:Union[str, Any]     ,   a		:Optional[Any]     ,   a		:int=False						)		-> str:
								__UpperCamelCase	:   int			   =					super()._prepare_for_class(_UpperCAmelCase     ,   _UpperCAmelCase     ,   return_labels=_UpperCAmelCase						)

								if return_labels:
															if model_class in get_values(_UpperCAmelCase						):
																						__UpperCamelCase	:   str			   =					torch.zeros(
																						    (self.model_tester.batch_size, self.model_tester.seq_length)     ,   dtype=torch.long     ,   device=_UpperCAmelCase						)
																						__UpperCamelCase	:   List[str]			   =					torch.zeros(
																						    self.model_tester.batch_size     ,   dtype=torch.long     ,   device=_UpperCAmelCase						)
								return inputs_dict







	def    _lowerCamelCase				(					self		:Tuple						)		-> List[Any]:
								__UpperCamelCase	:   Optional[Any]			   =					AlbertModelTester(self						)
								__UpperCamelCase	:   int			   =					ConfigTester(self     ,   config_class=_UpperCAmelCase     ,   hidden_size=3_7						)







	def    _lowerCamelCase				(					self		:int						)		-> int:
								self.config_tester.run_common_tests()







	def    _lowerCamelCase				(					self		:Any						)		-> int:
								__UpperCamelCase	:   List[str]			   =					self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_model(*_UpperCAmelCase						)







	def    _lowerCamelCase				(					self		:List[str]						)		-> Optional[int]:
								__UpperCamelCase	:   Optional[Any]			   =					self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_for_pretraining(*_UpperCAmelCase						)







	def    _lowerCamelCase				(					self		:Dict						)		-> int:
								__UpperCamelCase	:   Optional[int]			   =					self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_for_masked_lm(*_UpperCAmelCase						)







	def    _lowerCamelCase				(					self		:Dict						)		-> Optional[Any]:
								__UpperCamelCase	:   Tuple			   =					self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_for_multiple_choice(*_UpperCAmelCase						)







	def    _lowerCamelCase				(					self		:Tuple						)		-> Union[str, Any]:
								__UpperCamelCase	:   Optional[Any]			   =					self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_for_question_answering(*_UpperCAmelCase						)







	def    _lowerCamelCase				(					self		:str						)		-> Optional[Any]:
								__UpperCamelCase	:   Optional[int]			   =					self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_for_sequence_classification(*_UpperCAmelCase						)







	def    _lowerCamelCase				(					self		:Tuple						)		-> Optional[int]:
								__UpperCamelCase	:   Tuple			   =					self.model_tester.prepare_config_and_inputs()
								for type in ["absolute", "relative_key", "relative_key_query"]:
															__UpperCamelCase	:   Optional[Any]			   =					type
															self.model_tester.create_and_check_model(*_UpperCAmelCase						)







	@slow
	def    _lowerCamelCase				(					self		:Dict						)		-> List[str]:
								for model_name in ALBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
															__UpperCamelCase	:   Dict			   =					AlbertModel.from_pretrained(_UpperCAmelCase						)
															self.assertIsNotNone(_UpperCAmelCase						)




@require_torch
class  lowerCamelCase__							(				unittest.TestCase):




	'''simple docstring'''




	@slow
	def    _lowerCamelCase				(					self		:Tuple						)		-> int:
								__UpperCamelCase	:   Tuple			   =					AlbertModel.from_pretrained("albert-base-v2"						)
								__UpperCamelCase	:   List[Any]			   =					torch.tensor([[0, 3_4_5, 2_3_2, 3_2_8, 7_4_0, 1_4_0, 1_6_9_5, 6_9, 6_0_7_8, 1_5_8_8, 2]]						)
								__UpperCamelCase	:   Union[str, Any]			   =					torch.tensor([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]						)
								with torch.no_grad():
															__UpperCamelCase	:   int			   =					model(_UpperCAmelCase     ,   attention_mask=_UpperCAmelCase						)[0]
								__UpperCamelCase	:   str			   =					torch.Size((1, 1_1, 7_6_8)						)
								self.assertEqual(output.shape     ,   _UpperCAmelCase						)
								__UpperCamelCase	:   int			   =					torch.tensor(
								    [[[-0.6513, 1.5035, -0.2766], [-0.6515, 1.5046, -0.2780], [-0.6512, 1.5049, -0.2784]]]						)

								self.assertTrue(torch.allclose(output[:, 1:4, 1:4]     ,   _UpperCAmelCase     ,   atol=1E-4						)						)
"""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						(_UpperCAmelCase  ):



    """simple docstring"""

    _UpperCAmelCase	:Dict						    = "deta"
    _UpperCAmelCase	:Any						    = {
        "hidden_size": "d_model",
        "num_attention_heads": "encoder_attention_heads",
    }






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

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






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






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






    def _snake_case			(   self							):
       lowercase__:   Union[str, Any]					=      copy.deepcopy(self.__dict__							)
       lowercase__:   Dict					=      self.backbone_config.to_dict()
       lowercase__:   Union[str, Any]					=      self.__class__.model_type
       return output
