from __future__ import absolute_import, division, print_function, unicode_literals

from torch import nn
from torch.nn import CrossEntropyLoss, MSELoss

from transformers import RobertaConfig
from transformers.file_utils import add_start_docstrings, add_start_docstrings_to_model_forward
from transformers.models.roberta.modeling_roberta import (
    ROBERTA_INPUTS_DOCSTRING,
    ROBERTA_START_DOCSTRING,
    RobertaEmbeddings,
)

from .modeling_highway_bert import BertPreTrainedModel, DeeBertModel, HighwayException, entropy


@add_start_docstrings(
    """The RoBERTa Model transformer with early exiting (DeeRoBERTa). """						,	UpperCAmelCase_						,	)
class 	lowerCamelCase_     (   UpperCAmelCase_ ):






    '''simple docstring'''






    a__	:						Optional[int]						=      RobertaConfig
    a__	:						List[str]						=      """roberta"""







    def __init__(	self , __lowercase)       ->	str:
        super().__init__(__lowercase)

        __UpperCamelCase     :Dict			  =		RobertaEmbeddings(__lowercase)
        self.init_weights()


@add_start_docstrings(
    """RoBERTa Model (with early exiting - DeeRoBERTa) with a classifier on top,
    also takes care of multi-layer training. """						,	UpperCAmelCase_						,	)
class 	lowerCamelCase_     (   UpperCAmelCase_ ):






    '''simple docstring'''






    a__	:						List[Any]						=      RobertaConfig
    a__	:						str						=      """roberta"""







    def __init__(	self , __lowercase)       ->	Any:
        super().__init__(__lowercase)
        __UpperCamelCase     :List[str]			  =		config.num_labels
        __UpperCamelCase     :List[Any]			  =		config.num_hidden_layers

        __UpperCamelCase     :Optional[Any]			  =		DeeRobertaModel(__lowercase)
        __UpperCamelCase     :Any			  =		nn.Dropout(config.hidden_dropout_prob)
        __UpperCamelCase     :str			  =		nn.Linear(config.hidden_size , self.config.num_labels)







    @add_start_docstrings_to_model_forward(__lowercase)
    def 			UpperCamelCase__   (	self , __lowercase=None , __lowercase=None , __lowercase=None , __lowercase=None , __lowercase=None , __lowercase=None , __lowercase=None , __lowercase=-1 , __lowercase=False , )       ->	List[Any]:
        __UpperCamelCase     :List[str]			  =		self.num_layers
        try:
            __UpperCamelCase     :Tuple			  =		self.roberta(
                __lowercase , attention_mask=__lowercase , token_type_ids=__lowercase , position_ids=__lowercase , head_mask=__lowercase , inputs_embeds=__lowercase , )

            __UpperCamelCase     :int			  =		outputs[1]

            __UpperCamelCase     :Optional[int]			  =		self.dropout(__lowercase)
            __UpperCamelCase     :Any			  =		self.classifier(__lowercase)
            __UpperCamelCase     :Tuple			  =		(logits,) + outputs[2:]  # add hidden states and attention if they are here
        except HighwayException as e:
            __UpperCamelCase     :Optional[Any]			  =		e.message
            __UpperCamelCase     :List[str]			  =		e.exit_layer
            __UpperCamelCase     :Any			  =		outputs[0]

        if not self.training:
            __UpperCamelCase     :Optional[Any]			  =		entropy(__lowercase)
            __UpperCamelCase     :Dict			  =		[]
            __UpperCamelCase     :int			  =		[]
        if labels is not None:
            if self.num_labels == 1:
                #  We are doing regression
                __UpperCamelCase     :Dict			  =		MSELoss()
                __UpperCamelCase     :str			  =		loss_fct(logits.view(-1) , labels.view(-1))
            else:
                __UpperCamelCase     :Optional[Any]			  =		CrossEntropyLoss()
                __UpperCamelCase     :Any			  =		loss_fct(logits.view(-1 , self.num_labels) , labels.view(-1))

            # work with highway exits
            __UpperCamelCase     :List[Any]			  =		[]
            for highway_exit in outputs[-1]:
                __UpperCamelCase     :Any			  =		highway_exit[0]
                if not self.training:
                    highway_logits_all.append(__lowercase)
                    highway_entropy.append(highway_exit[2])
                if self.num_labels == 1:
                    #  We are doing regression
                    __UpperCamelCase     :Any			  =		MSELoss()
                    __UpperCamelCase     :Optional[Any]			  =		loss_fct(highway_logits.view(-1) , labels.view(-1))
                else:
                    __UpperCamelCase     :Union[str, Any]			  =		CrossEntropyLoss()
                    __UpperCamelCase     :Union[str, Any]			  =		loss_fct(highway_logits.view(-1 , self.num_labels) , labels.view(-1))
                highway_losses.append(__lowercase)

            if train_highway:
                __UpperCamelCase     :Any			  =		(sum(highway_losses[:-1]),) + outputs
                # exclude the final highway, of course
            else:
                __UpperCamelCase     :Union[str, Any]			  =		(loss,) + outputs
        if not self.training:
            __UpperCamelCase     :Tuple			  =		outputs + ((original_entropy, highway_entropy), exit_layer)
            if output_layer >= 0:
                __UpperCamelCase     :List[str]			  =		(
                    (outputs[0],) + (highway_logits_all[output_layer],) + outputs[2:]
                )  # use the highway of the last layer

        return outputs  # (loss), logits, (hidden_states), (attentions), entropy






import copy

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


__lowercase							 =					logging.get_logger(__name__)

__lowercase							 =					{
    '''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_     (   UpperCAmelCase_ ):






    '''simple docstring'''






    a__	:						List[str]						=      """deformable_detr"""
    a__	:						Union[str, Any]						=      {
        """hidden_size""": """d_model""",
        """num_attention_heads""": """encoder_attention_heads""",
    }







    def __init__(	self , __lowercase=True , __lowercase=None , __lowercase=3 , __lowercase=300 , __lowercase=1_024 , __lowercase=6 , __lowercase=1_024 , __lowercase=8 , __lowercase=6 , __lowercase=1_024 , __lowercase=8 , __lowercase=0.0 , __lowercase=True , __lowercase="relu" , __lowercase=256 , __lowercase=0.1 , __lowercase=0.0 , __lowercase=0.0 , __lowercase=0.02 , __lowercase=1.0 , __lowercase=True , __lowercase=False , __lowercase="sine" , __lowercase="resnet50" , __lowercase=True , __lowercase=False , __lowercase=4 , __lowercase=4 , __lowercase=4 , __lowercase=False , __lowercase=300 , __lowercase=False , __lowercase=1 , __lowercase=5 , __lowercase=2 , __lowercase=1 , __lowercase=1 , __lowercase=5 , __lowercase=2 , __lowercase=0.1 , __lowercase=0.25 , __lowercase=False , **__lowercase , )       ->	int:
        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.''')
                __UpperCamelCase     :str			  =		CONFIG_MAPPING['''resnet'''](out_features=['''stage4'''])
            elif isinstance(__lowercase , __lowercase):
                __UpperCamelCase     :str			  =		backbone_config.get('''model_type''')
                __UpperCamelCase     :Tuple			  =		CONFIG_MAPPING[backbone_model_type]
                __UpperCamelCase     :Any			  =		config_class.from_dict(__lowercase)
        __UpperCamelCase     :int			  =		use_timm_backbone
        __UpperCamelCase     :Dict			  =		backbone_config
        __UpperCamelCase     :Any			  =		num_channels
        __UpperCamelCase     :Optional[int]			  =		num_queries
        __UpperCamelCase     :Any			  =		max_position_embeddings
        __UpperCamelCase     :str			  =		d_model
        __UpperCamelCase     :Tuple			  =		encoder_ffn_dim
        __UpperCamelCase     :Union[str, Any]			  =		encoder_layers
        __UpperCamelCase     :List[Any]			  =		encoder_attention_heads
        __UpperCamelCase     :Any			  =		decoder_ffn_dim
        __UpperCamelCase     :List[str]			  =		decoder_layers
        __UpperCamelCase     :int			  =		decoder_attention_heads
        __UpperCamelCase     :str			  =		dropout
        __UpperCamelCase     :Any			  =		attention_dropout
        __UpperCamelCase     :int			  =		activation_dropout
        __UpperCamelCase     :List[Any]			  =		activation_function
        __UpperCamelCase     :List[Any]			  =		init_std
        __UpperCamelCase     :List[Any]			  =		init_xavier_std
        __UpperCamelCase     :int			  =		encoder_layerdrop
        __UpperCamelCase     :str			  =		auxiliary_loss
        __UpperCamelCase     :Optional[Any]			  =		position_embedding_type
        __UpperCamelCase     :Union[str, Any]			  =		backbone
        __UpperCamelCase     :Any			  =		use_pretrained_backbone
        __UpperCamelCase     :str			  =		dilation
        # deformable attributes
        __UpperCamelCase     :Optional[Any]			  =		num_feature_levels
        __UpperCamelCase     :str			  =		encoder_n_points
        __UpperCamelCase     :int			  =		decoder_n_points
        __UpperCamelCase     :Union[str, Any]			  =		two_stage
        __UpperCamelCase     :Optional[Any]			  =		two_stage_num_proposals
        __UpperCamelCase     :Dict			  =		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
        __UpperCamelCase     :Optional[int]			  =		class_cost
        __UpperCamelCase     :List[Any]			  =		bbox_cost
        __UpperCamelCase     :str			  =		giou_cost
        # Loss coefficients
        __UpperCamelCase     :Tuple			  =		mask_loss_coefficient
        __UpperCamelCase     :Tuple			  =		dice_loss_coefficient
        __UpperCamelCase     :int			  =		bbox_loss_coefficient
        __UpperCamelCase     :Any			  =		giou_loss_coefficient
        __UpperCamelCase     :Dict			  =		eos_coefficient
        __UpperCamelCase     :Optional[Any]			  =		focal_alpha
        __UpperCamelCase     :Optional[Any]			  =		disable_custom_kernels
        super().__init__(is_encoder_decoder=__lowercase , **__lowercase)







    @property
    def 			UpperCamelCase__   (	self)       ->	int:
        return self.encoder_attention_heads







    @property
    def 			UpperCamelCase__   (	self)       ->	int:
        return self.d_model







    def 			UpperCamelCase__   (	self)       ->	List[Any]:
        __UpperCamelCase     :Dict			  =		copy.deepcopy(self.__dict__)
        if self.backbone_config is not None:
            __UpperCamelCase     :Tuple			  =		self.backbone_config.to_dict()
        __UpperCamelCase     :List[Any]			  =		self.__class__.model_type
        return output





