
"""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). '	,					_lowerCamelCase	,					)
class 			A__	(     _lowerCamelCase):
		A_						:	Any           =     RobertaConfig
		A_						:	Any           =     'roberta'

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

			__lowerCAmelCase      :						List[Any]						      =				RobertaEmbeddings(_SCREAMING_SNAKE_CASE			)
			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. '	,					_lowerCamelCase	,					)
class 			A__	(     _lowerCamelCase):
		A_						:	Tuple           =     RobertaConfig
		A_						:	str           =     'roberta'

		def __init__(				self	,		_SCREAMING_SNAKE_CASE			):
			super().__init__(_SCREAMING_SNAKE_CASE			)
			__lowerCAmelCase      :						int						      =				config.num_labels
			__lowerCAmelCase      :						Tuple						      =				config.num_hidden_layers

			__lowerCAmelCase      :						List[str]						      =				DeeRobertaModel(_SCREAMING_SNAKE_CASE			)
			__lowerCAmelCase      :						Tuple						      =				nn.Dropout(config.hidden_dropout_prob			)
			__lowerCAmelCase      :						List[str]						      =				nn.Linear(config.hidden_size	,		self.config.num_labels			)







		@add_start_docstrings_to_model_forward(_SCREAMING_SNAKE_CASE			)
		def 							__lowerCamelCase							(				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	,		):
			__lowerCAmelCase      :						Dict						      =				self.num_layers
			try:
				__lowerCAmelCase      :						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	,		)

				__lowerCAmelCase      :						str						      =				outputs[1]

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

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

				# work with highway exits
				__lowerCAmelCase      :						Union[str, Any]						      =				[]
				for highway_exit in outputs[-1]:
					__lowerCAmelCase      :						Tuple						      =				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
						__lowerCAmelCase      :						Optional[int]						      =				MSELoss()
						__lowerCAmelCase      :						Tuple						      =				loss_fct(highway_logits.view(-1			)	,		labels.view(-1			)			)
					else:
						__lowerCAmelCase      :						Any						      =				CrossEntropyLoss()
						__lowerCAmelCase      :						Optional[int]						      =				loss_fct(highway_logits.view(-1	,		self.num_labels			)	,		labels.view(-1			)			)
					highway_losses.append(_SCREAMING_SNAKE_CASE			)

				if train_highway:
					__lowerCAmelCase      :						Any						      =				(sum(highway_losses[:-1]			),) + outputs
					# exclude the final highway, of course
				else:
					__lowerCAmelCase      :						Optional[int]						      =				(loss,) + outputs
			if not self.training:
				__lowerCAmelCase      :						Dict						      =				outputs + ((original_entropy, highway_entropy), exit_layer)
				if output_layer >= 0:
					__lowerCAmelCase      :						Dict						      =				(
					    (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"""







import copy
from dataclasses import dataclass, field
from typing import ClassVar, Dict

from ..features import ClassLabel, Features, Value
from .base import TaskTemplate




@dataclass(frozen=_lowerCamelCase)
class 			A__	(     _lowerCamelCase):
		# `task` is not a ClassVar since we want it to be part of the `asdict` output for JSON serialization
		A_						:	str           =     field(default='text-classification'	,					metadata={'include_in_asdict_even_if_is_default': True})
		A_						:	ClassVar[Features]           =     Features({'text': Value('string')})
		A_						:	ClassVar[Features]           =     Features({'labels': ClassLabel})
		A_						:	str           =     "text"
		A_						:	str           =     "labels"

		def 							__lowerCamelCase							(				self	,		_SCREAMING_SNAKE_CASE			):
			if self.label_column not in features:
				raise ValueError(f"Column {self.label_column} is not present in features."			)
			if not isinstance(features[self.label_column]	,		_SCREAMING_SNAKE_CASE			):
				raise ValueError(f"Column {self.label_column} is not a ClassLabel."			)
			__lowerCAmelCase      :						Any						      =				copy.deepcopy(self			)
			__lowerCAmelCase      :						Dict						      =				self.label_schema.copy()
			__lowerCAmelCase      :						List[Any]						      =				features[self.label_column]
			__lowerCAmelCase      :						Dict						      =				label_schema
			return task_template







		@property
		def 							__lowerCamelCase							(				self			):
			return {
			    self.text_column: "text",
			    self.label_column: "labels",
			}