





'''simple docstring'''






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). '''		,				__SCREAMING_SNAKE_CASE		,				)
class 					__snake_case  (					__SCREAMING_SNAKE_CASE							):






   '''simple docstring'''





   lowerCamelCase__    							=      RobertaConfig
   lowerCamelCase__    							=      '''roberta'''

   def __init__(							self	,		__SCREAMING_SNAKE_CASE       ):
    super().__init__(__SCREAMING_SNAKE_CASE       )

    snake_case__     :					Optional[Any]    =  RobertaEmbeddings(__SCREAMING_SNAKE_CASE       )
    self.init_weights()



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






   '''simple docstring'''





   lowerCamelCase__    							=      RobertaConfig
   lowerCamelCase__    							=      '''roberta'''

   def __init__(							self	,		__SCREAMING_SNAKE_CASE       ):
    super().__init__(__SCREAMING_SNAKE_CASE       )
    snake_case__     :					int    =  config.num_labels
    snake_case__     :					str    =  config.num_hidden_layers

    snake_case__     :					Optional[int]    =  DeeRobertaModel(__SCREAMING_SNAKE_CASE       )
    snake_case__     :					int    =  nn.Dropout(config.hidden_dropout_prob       )
    snake_case__     :					Dict    =  nn.Linear(config.hidden_size	,		self.config.num_labels       )


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

     snake_case__     :					int    =  outputs[1]

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

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

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

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

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










'''simple docstring'''







from typing import TYPE_CHECKING

from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available


lowercase      :   Union[str, Any]      =       {
    """configuration_instructblip""": [
        """INSTRUCTBLIP_PRETRAINED_CONFIG_ARCHIVE_MAP""",
        """InstructBlipConfig""",
        """InstructBlipQFormerConfig""",
        """InstructBlipVisionConfig""",
    ],
    """processing_instructblip""": ["""InstructBlipProcessor"""],
}

try:
      if not is_torch_available():
            raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
      pass
else:
      lowercase      :   Union[str, Any]      =       [
          """INSTRUCTBLIP_PRETRAINED_MODEL_ARCHIVE_LIST""",
          """InstructBlipQFormerModel""",
          """InstructBlipPreTrainedModel""",
          """InstructBlipForConditionalGeneration""",
          """InstructBlipVisionModel""",
      ]

if TYPE_CHECKING:
      from .configuration_instructblip import (
          INSTRUCTBLIP_PRETRAINED_CONFIG_ARCHIVE_MAP,
          InstructBlipConfig,
          InstructBlipQFormerConfig,
          InstructBlipVisionConfig,
      )
      from .processing_instructblip import InstructBlipProcessor

      try:
            if not is_torch_available():
                  raise OptionalDependencyNotAvailable()
      except OptionalDependencyNotAvailable:
            pass
      else:
            from .modeling_instructblip import (
                INSTRUCTBLIP_PRETRAINED_MODEL_ARCHIVE_LIST,
                InstructBlipForConditionalGeneration,
                InstructBlipPreTrainedModel,
                InstructBlipQFormerModel,
                InstructBlipVisionModel,
            )

else:
      import sys

      lowercase      :   List[Any]      =       _LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)



