


import unittest

import numpy as np

from transformers import AlbertConfig, is_flax_available
from transformers.testing_utils import require_flax, slow

from ...test_modeling_flax_common import FlaxModelTesterMixin, ids_tensor, random_attention_mask


if is_flax_available():
				import jax.numpy as jnp

				from transformers.models.albert.modeling_flax_albert import (
				    FlaxAlbertForMaskedLM,
				    FlaxAlbertForMultipleChoice,
				    FlaxAlbertForPreTraining,
				    FlaxAlbertForQuestionAnswering,
				    FlaxAlbertForSequenceClassification,
				    FlaxAlbertForTokenClassification,
				    FlaxAlbertModel,
				)



class  lowerCAmelCase				(				unittest.TestCase							):



	def __init__(					self ,					snake_case__ ,					snake_case__=13 ,					snake_case__=7 ,					snake_case__=True ,					snake_case__=True ,					snake_case__=True ,					snake_case__=True ,					snake_case__=99 ,					snake_case__=32 ,					snake_case__=5 ,					snake_case__=4 ,					snake_case__=37 ,					snake_case__="gelu" ,					snake_case__=0.1 ,					snake_case__=0.1 ,					snake_case__=512 ,					snake_case__=16 ,					snake_case__=2 ,					snake_case__=0.0_2 ,					snake_case__=4 ,					):
		lowerCAmelCase					:    List[str]      							= parent
		lowerCAmelCase					:    List[str]      							= batch_size
		lowerCAmelCase					:    Optional[int]      							= seq_length
		lowerCAmelCase					:    str      							= is_training
		lowerCAmelCase					:    Optional[int]      							= use_attention_mask
		lowerCAmelCase					:    Any      							= use_token_type_ids
		lowerCAmelCase					:    Union[str, Any]      							= use_labels
		lowerCAmelCase					:    Tuple      							= vocab_size
		lowerCAmelCase					:    List[str]      							= hidden_size
		lowerCAmelCase					:    int      							= num_hidden_layers
		lowerCAmelCase					:    Optional[Any]      							= num_attention_heads
		lowerCAmelCase					:    int      							= intermediate_size
		lowerCAmelCase					:    Optional[int]      							= hidden_act
		lowerCAmelCase					:    str      							= hidden_dropout_prob
		lowerCAmelCase					:    Any      							= attention_probs_dropout_prob
		lowerCAmelCase					:    Optional[int]      							= max_position_embeddings
		lowerCAmelCase					:    List[Any]      							= type_vocab_size
		lowerCAmelCase					:    List[Any]      							= type_sequence_label_size
		lowerCAmelCase					:    List[Any]      							= initializer_range
		lowerCAmelCase					:    Dict      							= num_choices



	def 				lowercase     (					self    ):
		lowerCAmelCase					:    Dict      							= ids_tensor([self.batch_size, self.seq_length] ,					self.vocab_size    )

		lowerCAmelCase					:    str      							= None
		if self.use_attention_mask:
			lowerCAmelCase					:    List[Any]      							= random_attention_mask([self.batch_size, self.seq_length]    )

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

		lowerCAmelCase					:    Union[str, Any]      							= 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 ,					is_decoder=snake_case__ ,					initializer_range=self.initializer_range ,					)

		return config, input_ids, token_type_ids, attention_mask



	def 				lowercase     (					self    ):
		lowerCAmelCase					:    Tuple      							= self.prepare_config_and_inputs()
		lowerCAmelCase					:    str      							= config_and_inputs
		lowerCAmelCase					:    str      							= {'input_ids': input_ids, 'token_type_ids': token_type_ids, 'attention_mask': attention_mask}
		return config, inputs_dict



@require_flax
class  lowerCAmelCase				(				a      ,				unittest.TestCase							):
	_lowerCamelCase   :					Union[str, Any]			      = (
	    (
	        FlaxAlbertModel,
	        FlaxAlbertForPreTraining,
	        FlaxAlbertForMaskedLM,
	        FlaxAlbertForMultipleChoice,
	        FlaxAlbertForQuestionAnswering,
	        FlaxAlbertForSequenceClassification,
	        FlaxAlbertForTokenClassification,
	        FlaxAlbertForQuestionAnswering,
	    )
	    if is_flax_available()
	    else ()
	)



	def 				lowercase     (					self    ):
		lowerCAmelCase					:    Optional[int]      							= FlaxAlbertModelTester(self    )



	@slow
	def 				lowercase     (					self    ):
		for model_class_name in self.all_model_classes:
			lowerCAmelCase					:    Any      							= model_class_name.from_pretrained('albert-base-v2'    )
			lowerCAmelCase					:    Union[str, Any]      							= model(np.ones((1, 1)    )    )
			self.assertIsNotNone(snake_case__    )



@require_flax
class  lowerCAmelCase				(				unittest.TestCase							):



	@slow
	def 				lowercase     (					self    ):
		lowerCAmelCase					:    Union[str, Any]      							= FlaxAlbertModel.from_pretrained('albert-base-v2'    )
		lowerCAmelCase					:    Union[str, Any]      							= np.array([[0, 345, 232, 328, 740, 140, 1695, 69, 6078, 1588, 2]]    )
		lowerCAmelCase					:    Tuple      							= np.array([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]    )
		lowerCAmelCase					:    int      							= model(snake_case__ ,					attention_mask=snake_case__    )[0]
		lowerCAmelCase					:    Optional[Any]      							= (1, 11, 768)
		self.assertEqual(output.shape ,					snake_case__    )
		lowerCAmelCase					:    Any      							= np.array(
		    [[[-0.6_5_1_3, 1.5_0_3_5, -0.2_7_6_6], [-0.6_5_1_5, 1.5_0_4_6, -0.2_7_8_0], [-0.6_5_1_2, 1.5_0_4_9, -0.2_7_8_4]]]    )

		self.assertTrue(jnp.allclose(output[:, 1:4, 1:4] ,					snake_case__ ,					atol=1e-4    )    )




'''simple docstring'''




import copy

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


_lowerCAmelCase  :		Optional[Any]		     =	logging.get_logger(__name__)

_lowerCAmelCase  :		Dict		     =	{
    'SenseTime/deformable-detr': 'https://huggingface.co/sensetime/deformable-detr/resolve/main/config.json',
    # See all Deformable DETR models at https://huggingface.co/models?filter=deformable-detr
}



class  lowerCAmelCase				(				a							):
	_lowerCamelCase   :					Any			      = """deformable_detr"""
	_lowerCamelCase   :					List[str]			      = {
	    """hidden_size""": """d_model""",
	    """num_attention_heads""": """encoder_attention_heads""",
	}



	def __init__(					self ,					snake_case__=True ,					snake_case__=None ,					snake_case__=3 ,					snake_case__=300 ,					snake_case__=1024 ,					snake_case__=6 ,					snake_case__=1024 ,					snake_case__=8 ,					snake_case__=6 ,					snake_case__=1024 ,					snake_case__=8 ,					snake_case__=0.0 ,					snake_case__=True ,					snake_case__="relu" ,					snake_case__=256 ,					snake_case__=0.1 ,					snake_case__=0.0 ,					snake_case__=0.0 ,					snake_case__=0.0_2 ,					snake_case__=1.0 ,					snake_case__=True ,					snake_case__=False ,					snake_case__="sine" ,					snake_case__="resnet50" ,					snake_case__=True ,					snake_case__=False ,					snake_case__=4 ,					snake_case__=4 ,					snake_case__=4 ,					snake_case__=False ,					snake_case__=300 ,					snake_case__=False ,					snake_case__=1 ,					snake_case__=5 ,					snake_case__=2 ,					snake_case__=1 ,					snake_case__=1 ,					snake_case__=5 ,					snake_case__=2 ,					snake_case__=0.1 ,					snake_case__=0.2_5 ,					snake_case__=False ,					**snake_case__ ,					):
		if backbone_config is not None and use_timm_backbone:
			raise ValueError('You can\'t specify both `backbone_config` and `use_timm_backbone`.'    )

		if not use_timm_backbone:
			if backbone_config is None:
				logger.info('`backbone_config` is `None`. Initializing the config with the default `ResNet` backbone.'    )
				lowerCAmelCase					:    Optional[int]      							= CONFIG_MAPPING['resnet'](out_features=['stage4']    )
			elif isinstance(snake_case__ ,					snake_case__    ):
				lowerCAmelCase					:    List[str]      							= backbone_config.get('model_type'    )
				lowerCAmelCase					:    str      							= CONFIG_MAPPING[backbone_model_type]
				lowerCAmelCase					:    Optional[Any]      							= config_class.from_dict(snake_case__    )
		lowerCAmelCase					:    Union[str, Any]      							= use_timm_backbone
		lowerCAmelCase					:    List[Any]      							= backbone_config
		lowerCAmelCase					:    Any      							= num_channels
		lowerCAmelCase					:    Tuple      							= num_queries
		lowerCAmelCase					:    Dict      							= max_position_embeddings
		lowerCAmelCase					:    int      							= d_model
		lowerCAmelCase					:    List[str]      							= encoder_ffn_dim
		lowerCAmelCase					:    List[str]      							= encoder_layers
		lowerCAmelCase					:    int      							= encoder_attention_heads
		lowerCAmelCase					:    str      							= decoder_ffn_dim
		lowerCAmelCase					:    str      							= decoder_layers
		lowerCAmelCase					:    Dict      							= decoder_attention_heads
		lowerCAmelCase					:    str      							= dropout
		lowerCAmelCase					:    List[str]      							= attention_dropout
		lowerCAmelCase					:    Union[str, Any]      							= activation_dropout
		lowerCAmelCase					:    str      							= activation_function
		lowerCAmelCase					:    Any      							= init_std
		lowerCAmelCase					:    Any      							= init_xavier_std
		lowerCAmelCase					:    Dict      							= encoder_layerdrop
		lowerCAmelCase					:    int      							= auxiliary_loss
		lowerCAmelCase					:    Optional[Any]      							= position_embedding_type
		lowerCAmelCase					:    List[str]      							= backbone
		lowerCAmelCase					:    int      							= use_pretrained_backbone
		lowerCAmelCase					:    int      							= dilation
		# deformable attributes
		lowerCAmelCase					:    List[str]      							= num_feature_levels
		lowerCAmelCase					:    List[str]      							= encoder_n_points
		lowerCAmelCase					:    Union[str, Any]      							= decoder_n_points
		lowerCAmelCase					:    Tuple      							= two_stage
		lowerCAmelCase					:    Dict      							= two_stage_num_proposals
		lowerCAmelCase					:    Union[str, Any]      							= with_box_refine
		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					:    Union[str, Any]      							= class_cost
		lowerCAmelCase					:    Dict      							= bbox_cost
		lowerCAmelCase					:    List[Any]      							= giou_cost
		# Loss coefficients
		lowerCAmelCase					:    Dict      							= mask_loss_coefficient
		lowerCAmelCase					:    Any      							= dice_loss_coefficient
		lowerCAmelCase					:    str      							= bbox_loss_coefficient
		lowerCAmelCase					:    Tuple      							= giou_loss_coefficient
		lowerCAmelCase					:    List[str]      							= eos_coefficient
		lowerCAmelCase					:    Any      							= focal_alpha
		lowerCAmelCase					:    Dict      							= disable_custom_kernels
		super().__init__(is_encoder_decoder=snake_case__ ,					**snake_case__    )



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



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



	def 				lowercase     (					self    ):
		lowerCAmelCase					:    Union[str, Any]      							= copy.deepcopy(self.__dict__    )
		if self.backbone_config is not None:
			lowerCAmelCase					:    List[Any]      							= self.backbone_config.to_dict()
		lowerCAmelCase					:    str      							= self.__class__.model_type
		return output
