






'''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). '''						,					a_						,					)
class    A_	(a_							):
							"""simple docstring"""




							a__   		=	RobertaConfig
							a__   		=	'''roberta'''

							def __init__(		self			:List[Any]       ,							lowerCAmelCase__			:Union[str, Any]				)				->  List[Any]:


										'''simple docstring'''




										super().__init__(lowerCAmelCase__				)

										snake_case_				:					int   =       RobertaEmbeddings(lowerCAmelCase__				)
										self.init_weights()







@add_start_docstrings(
    '''RoBERTa Model (with early exiting - DeeRoBERTa) with a classifier on top,
    also takes care of multi-layer training. '''						,					a_						,					)
class    A_	(a_							):
							"""simple docstring"""




							a__   		=	RobertaConfig
							a__   		=	'''roberta'''

							def __init__(		self			:Union[str, Any]       ,							lowerCAmelCase__			:int				)				->  Dict:


										'''simple docstring'''




										super().__init__(lowerCAmelCase__				)
										snake_case_				:					Tuple   =       config.num_labels
										snake_case_				:					List[str]   =       config.num_hidden_layers

										snake_case_				:					Optional[int]   =       DeeRobertaModel(lowerCAmelCase__				)
										snake_case_				:					Optional[int]   =       nn.Dropout(config.hidden_dropout_prob				)
										snake_case_				:					Optional[int]   =       nn.Linear(config.hidden_size       ,							self.config.num_labels				)






							@add_start_docstrings_to_model_forward(lowerCAmelCase__				)
							def 							_A	(		self			:Union[str, Any]       ,							lowerCAmelCase__			:Tuple=None       ,							lowerCAmelCase__			:Any=None       ,							lowerCAmelCase__			:Optional[int]=None       ,							lowerCAmelCase__			:List[Any]=None       ,							lowerCAmelCase__			:Any=None       ,							lowerCAmelCase__			:Tuple=None       ,							lowerCAmelCase__			:str=None       ,							lowerCAmelCase__			:List[Any]=-1       ,							lowerCAmelCase__			:Union[str, Any]=False       ,							)				->  Optional[Any]:


										'''simple docstring'''




										snake_case_				:					Dict   =       self.num_layers
										try:
													snake_case_				:					Optional[Any]   =       self.roberta(
													    lowerCAmelCase__       ,							attention_mask=lowerCAmelCase__       ,							token_type_ids=lowerCAmelCase__       ,							position_ids=lowerCAmelCase__       ,							head_mask=lowerCAmelCase__       ,							inputs_embeds=lowerCAmelCase__       ,							)

													snake_case_				:					str   =       outputs[1]

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

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

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

													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_				:					Tuple   =       outputs + ((original_entropy, highway_entropy), exit_layer)
													if output_layer >= 0:
																snake_case_				:					Union[str, Any]   =       (
																    (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 __future__ import annotations

import math

import numpy as np
from numpy.linalg import norm

def 	__UpperCAmelCase       ( __magic_name__     ,__magic_name__						)->    float:



			"""simple docstring"""



			return math.sqrt(sum(pow(a - b     ,2						) for a, b in zip(__magic_name__     ,__magic_name__						)						)						)

def 	__UpperCAmelCase       ( __magic_name__     ,__magic_name__						)->    list[list[list[float] | float]]:



			"""simple docstring"""



			if dataset.ndim != value_array.ndim:
						snake_case_				:					int   =       (
						    "Wrong input data's dimensions... "
						    F'''dataset : {dataset.ndim}, value_array : {value_array.ndim}'''
						)
						raise ValueError(__magic_name__						)

			try:
						if dataset.shape[1] != value_array.shape[1]:
									snake_case_				:					Dict   =       (
									    "Wrong input data's shape... "
									    F'''dataset : {dataset.shape[1]}, value_array : {value_array.shape[1]}'''
									)
									raise ValueError(__magic_name__						)
			except IndexError:
						if dataset.ndim != value_array.ndim:
									raise TypeError("Wrong shape"						)

			if dataset.dtype != value_array.dtype:
						snake_case_				:					Dict   =       (
						    "Input data have different datatype... "
						    F'''dataset : {dataset.dtype}, value_array : {value_array.dtype}'''
						)
						raise TypeError(__magic_name__						)

			snake_case_				:					Optional[int]   =       []

			for value in value_array:
						snake_case_				:					List[str]   =       euclidean(__magic_name__     ,dataset[0]						)
						snake_case_				:					int   =       dataset[0].tolist()

						for dataset_value in dataset[1:]:
									snake_case_				:					Optional[Any]   =       euclidean(__magic_name__     ,__magic_name__						)

									if dist > temp_dist:
												snake_case_				:					Tuple   =       temp_dist
												snake_case_				:					Optional[int]   =       dataset_value.tolist()

						answer.append([vector, dist]						)

			return answer

def 	__UpperCAmelCase       ( __magic_name__     ,__magic_name__						)->    float:



			"""simple docstring"""



			return np.dot(__magic_name__     ,__magic_name__						) / (norm(__magic_name__						) * norm(__magic_name__						))


if __name__ == "__main__":
		import doctest

		doctest.testmod()




