


'''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). "    ,		lowercase    ,		)
class   snake_case					(					lowercase    ):





		"""simple docstring"""




		_lowerCamelCase						=						RobertaConfig
		_lowerCamelCase						=						"roberta"






		def __init__(					self    ,						UpperCamelCase  ):







			"""simple docstring"""
			super().__init__(UpperCamelCase  )

			lowerCamelCase_  				=						RobertaEmbeddings(UpperCamelCase  )
			self.init_weights()





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





		"""simple docstring"""




		_lowerCamelCase						=						RobertaConfig
		_lowerCamelCase						=						"roberta"






		def __init__(					self    ,						UpperCamelCase  ):







			"""simple docstring"""
			super().__init__(UpperCamelCase  )
			lowerCamelCase_  				=						config.num_labels
			lowerCamelCase_  				=						config.num_hidden_layers

			lowerCamelCase_  				=						DeeRobertaModel(UpperCamelCase  )
			lowerCamelCase_  				=						nn.Dropout(config.hidden_dropout_prob  )
			lowerCamelCase_  				=						nn.Linear(config.hidden_size    ,						self.config.num_labels  )







		@add_start_docstrings_to_model_forward(UpperCamelCase  )
		def 						snake_case	(					self    ,						UpperCamelCase=None    ,						UpperCamelCase=None    ,						UpperCamelCase=None    ,						UpperCamelCase=None    ,						UpperCamelCase=None    ,						UpperCamelCase=None    ,						UpperCamelCase=None    ,						UpperCamelCase=-1    ,						UpperCamelCase=False    ,						):







			"""simple docstring"""
			lowerCamelCase_  				=						self.num_layers
			try:
				lowerCamelCase_  				=						self.roberta(
				    UpperCamelCase    ,						attention_mask=UpperCamelCase    ,						token_type_ids=UpperCamelCase    ,						position_ids=UpperCamelCase    ,						head_mask=UpperCamelCase    ,						inputs_embeds=UpperCamelCase    ,						)

				lowerCamelCase_  				=						outputs[1]

				lowerCamelCase_  				=						self.dropout(UpperCamelCase  )
				lowerCamelCase_  				=						self.classifier(UpperCamelCase  )
				lowerCamelCase_  				=						(logits,) + outputs[2:]  # add hidden states and attention if they are here
			except HighwayException as e:
				lowerCamelCase_  				=						e.message
				lowerCamelCase_  				=						e.exit_layer
				lowerCamelCase_  				=						outputs[0]

			if not self.training:
				lowerCamelCase_  				=						entropy(UpperCamelCase  )
				lowerCamelCase_  				=						[]
				lowerCamelCase_  				=						[]
			if labels is not None:
				if self.num_labels == 1:
					#  We are doing regression
					lowerCamelCase_  				=						MSELoss()
					lowerCamelCase_  				=						loss_fct(logits.view(-1  )    ,						labels.view(-1  )  )
				else:
					lowerCamelCase_  				=						CrossEntropyLoss()
					lowerCamelCase_  				=						loss_fct(logits.view(-1    ,						self.num_labels  )    ,						labels.view(-1  )  )

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

				if train_highway:
					lowerCamelCase_  				=						(sum(highway_losses[:-1]  ),) + outputs
					# exclude the final highway, of course
				else:
					lowerCamelCase_  				=						(loss,) + outputs
			if not self.training:
				lowerCamelCase_  				=						outputs + ((original_entropy, highway_entropy), exit_layer)
				if output_layer >= 0:
					lowerCamelCase_  				=						(
					    (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_flax_available,
    is_tf_available,
    is_tokenizers_available,
    is_torch_available,
    is_vision_available,
)


a_						:       List[Any]      			=				{
    """configuration_owlvit""": [
        """OWLVIT_PRETRAINED_CONFIG_ARCHIVE_MAP""",
        """OwlViTConfig""",
        """OwlViTOnnxConfig""",
        """OwlViTTextConfig""",
        """OwlViTVisionConfig""",
    ],
    """processing_owlvit""": ["""OwlViTProcessor"""],
}


try:
     if not is_vision_available():
          raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
     pass
else:
     a_						:       int      			=				["""OwlViTFeatureExtractor"""]
     a_						:       Any      			=				["""OwlViTImageProcessor"""]

try:
     if not is_torch_available():
          raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
     pass
else:
     a_						:       Tuple      			=				[
         """OWLVIT_PRETRAINED_MODEL_ARCHIVE_LIST""",
         """OwlViTModel""",
         """OwlViTPreTrainedModel""",
         """OwlViTTextModel""",
         """OwlViTVisionModel""",
         """OwlViTForObjectDetection""",
     ]

if TYPE_CHECKING:
     from .configuration_owlvit import (
         OWLVIT_PRETRAINED_CONFIG_ARCHIVE_MAP,
         OwlViTConfig,
         OwlViTOnnxConfig,
         OwlViTTextConfig,
         OwlViTVisionConfig,
     )
     from .processing_owlvit import OwlViTProcessor

     try:
          if not is_vision_available():
               raise OptionalDependencyNotAvailable()
     except OptionalDependencyNotAvailable:
          pass
     else:
          from .feature_extraction_owlvit import OwlViTFeatureExtractor
          from .image_processing_owlvit import OwlViTImageProcessor

     try:
          if not is_torch_available():
               raise OptionalDependencyNotAvailable()
     except OptionalDependencyNotAvailable:
          pass
     else:
          from .modeling_owlvit import (
              OWLVIT_PRETRAINED_MODEL_ARCHIVE_LIST,
              OwlViTForObjectDetection,
              OwlViTModel,
              OwlViTPreTrainedModel,
              OwlViTTextModel,
              OwlViTVisionModel,
          )

else:
     import sys

     a_						:       Tuple      			=				_LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)



