


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 ):
	_lowerCAmelCase     :		Union[str, Any]										=						RobertaConfig
	_lowerCAmelCase     :		Any										=						'''roberta'''


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

							__UpperCAmelCase			:							str	    =	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 ):
	_lowerCAmelCase     :		str										=						RobertaConfig
	_lowerCAmelCase     :		Union[str, Any]										=						'''roberta'''


	def __init__(    self				,     lowercase__):
							super().__init__(lowercase__)
							__UpperCAmelCase			:							Dict	    =	config.num_labels
							__UpperCAmelCase			:							Any	    =	config.num_hidden_layers

							__UpperCAmelCase			:							Union[str, Any]	    =	DeeRobertaModel(lowercase__)
							__UpperCAmelCase			:							Any	    =	nn.Dropout(config.hidden_dropout_prob)
							__UpperCAmelCase			:							Any	    =	nn.Linear(config.hidden_size				,     self.config.num_labels)


	@add_start_docstrings_to_model_forward(lowercase__)
	def  A(    self				,     lowercase__=None				,     lowercase__=None				,     lowercase__=None				,     lowercase__=None				,     lowercase__=None				,     lowercase__=None				,     lowercase__=None				,     lowercase__=-1				,     lowercase__=False				,     ):
							__UpperCAmelCase			:							Any	    =	self.num_layers
							try:
													__UpperCAmelCase			:							int	    =	self.roberta(
													    lowercase__				,     attention_mask=lowercase__				,     token_type_ids=lowercase__				,     position_ids=lowercase__				,     head_mask=lowercase__				,     inputs_embeds=lowercase__				,     )

													__UpperCAmelCase			:							str	    =	outputs[1]

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

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

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

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





def     __SCREAMING_SNAKE_CASE							(       )		->     Optional[int]:




						'''simple docstring'''







						__UpperCAmelCase			:							str	    =	[31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]

						__UpperCAmelCase			:							Union[str, Any]	    =	6
						__UpperCAmelCase			:							Optional[Any]	    =	1
						__UpperCAmelCase			:							Tuple	    =	1901

						__UpperCAmelCase			:							Any	    =	0

						while year < 2001:
												day += 7

												if (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0):
																		if day > days_per_month[month - 1] and month != 2:
																								month += 1
																								__UpperCAmelCase			:							Optional[int]	    =	day - days_per_month[month - 2]
																		elif day > 29 and month == 2:
																								month += 1
																								__UpperCAmelCase			:							Optional[Any]	    =	day - 29
												else:
																		if day > days_per_month[month - 1]:
																								month += 1
																								__UpperCAmelCase			:							Optional[Any]	    =	day - days_per_month[month - 2]

												if month > 12:
																		year += 1
																		__UpperCAmelCase			:							int	    =	1

												if year < 2001 and day == 1:
																		sundays += 1
						return sundays


if __name__ == "__main__":
				print(solution())

