





"""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   ,		__lowerCamelCase    ):



     '''simple docstring'''




     super().__init__(__lowerCamelCase    )

     __A       :		str							=	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. """				,				SCREAMING_SNAKE_CASE__				,				)
class 						__snake_case   (						SCREAMING_SNAKE_CASE__	):





 """simple docstring"""


 _lowerCamelCase								=	RobertaConfig
 _lowerCamelCase								=	"""roberta"""

 def __init__(       self   ,		__lowerCamelCase    ):



     '''simple docstring'''




     super().__init__(__lowerCamelCase    )
     __A       :		List[Any]							=	config.num_labels
     __A       :		Optional[int]							=	config.num_hidden_layers

     __A       :		Any							=	DeeRobertaModel(__lowerCamelCase    )
     __A       :		int							=	nn.Dropout(config.hidden_dropout_prob    )
     __A       :		Dict							=	nn.Linear(config.hidden_size   ,		self.config.num_labels    )







 @add_start_docstrings_to_model_forward(__lowerCamelCase    )
 def    UpperCamelCase__(       self   ,		__lowerCamelCase=None   ,		__lowerCamelCase=None   ,		__lowerCamelCase=None   ,		__lowerCamelCase=None   ,		__lowerCamelCase=None   ,		__lowerCamelCase=None   ,		__lowerCamelCase=None   ,		__lowerCamelCase=-1   ,		__lowerCamelCase=False   ,		):



     '''simple docstring'''




     __A       :		Union[str, Any]							=	self.num_layers
     try:
         __A       :		Dict							=	self.roberta(
             __lowerCamelCase   ,		attention_mask=__lowerCamelCase   ,		token_type_ids=__lowerCamelCase   ,		position_ids=__lowerCamelCase   ,		head_mask=__lowerCamelCase   ,		inputs_embeds=__lowerCamelCase   ,		)

         __A       :		str							=	outputs[1]

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

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

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

         if train_highway:
             __A       :		List[str]							=	(sum(highway_losses[:-1]    ),) + outputs
             # exclude the final highway, of course
         else:
             __A       :		Tuple							=	(loss,) + outputs
     if not self.training:
         __A       :		Dict							=	outputs + ((original_entropy, highway_entropy), exit_layer)
         if output_layer >= 0:
             __A       :		List[str]							=	(
                 (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 collections import defaultdict
from math import gcd







def 						__lowercase	(  snake_case_    :    int = 1500000		)  ->int:



    '''simple docstring'''







    __A       :		defaultdict							=	defaultdict(snake_case_		)
    __A       :		Any							=	2
    while 2 * euclid_m * (euclid_m + 1) <= limit:
        for euclid_n in range((euclid_m % 2) + 1							,snake_case_							,2		):
            if gcd(snake_case_							,snake_case_		) > 1:
                continue
            __A       :		List[Any]							=	2 * euclid_m * (euclid_m + euclid_n)
            for perimeter in range(snake_case_							,limit + 1							,snake_case_		):
                frequencies[perimeter] += 1
        euclid_m += 1
    return sum(1 for frequency in frequencies.values() if frequency == 1		)


if __name__ == "__main__":
       print(f'''{solution() = }''')




