

import unittest

from transformers import ElectraConfig, is_torch_available
from transformers.models.auto import get_values
from transformers.testing_utils import require_torch, slow, torch_device

from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
 import torch

 from transformers import (
     MODEL_FOR_PRETRAINING_MAPPING,
     ElectraForCausalLM,
     ElectraForMaskedLM,
     ElectraForMultipleChoice,
     ElectraForPreTraining,
     ElectraForQuestionAnswering,
     ElectraForSequenceClassification,
     ElectraForTokenClassification,
     ElectraModel,
 )
 from transformers.models.electra.modeling_electra import ELECTRA_PRETRAINED_MODEL_ARCHIVE_LIST



class 						a_			:



      """simple docstring"""







      def __init__( self	:				str     ,snake_case	:				List[Any]     ,snake_case	:				str=13     ,snake_case	:				str=7     ,snake_case	:				int=True     ,snake_case	:				Optional[Any]=True     ,snake_case	:				Tuple=True     ,snake_case	:				int=True     ,snake_case	:				List[str]=99     ,snake_case	:				Dict=32     ,snake_case	:				Optional[Any]=5     ,snake_case	:				int=4     ,snake_case	:				List[str]=37     ,snake_case	:				Optional[Any]="gelu"     ,snake_case	:				List[Any]=0.1     ,snake_case	:				List[Any]=0.1     ,snake_case	:				Dict=512     ,snake_case	:				List[Any]=16     ,snake_case	:				str=2     ,snake_case	:				int=0.02     ,snake_case	:				Optional[Any]=3     ,snake_case	:				Optional[int]=4     ,snake_case	:				str=None     ,):
           SCREAMING_SNAKE_CASE 		=parent
           SCREAMING_SNAKE_CASE 		=batch_size
           SCREAMING_SNAKE_CASE 		=seq_length
           SCREAMING_SNAKE_CASE 		=is_training
           SCREAMING_SNAKE_CASE 		=use_input_mask
           SCREAMING_SNAKE_CASE 		=use_token_type_ids
           SCREAMING_SNAKE_CASE 		=use_labels
           SCREAMING_SNAKE_CASE 		=vocab_size
           SCREAMING_SNAKE_CASE 		=hidden_size
           SCREAMING_SNAKE_CASE 		=num_hidden_layers
           SCREAMING_SNAKE_CASE 		=num_attention_heads
           SCREAMING_SNAKE_CASE 		=intermediate_size
           SCREAMING_SNAKE_CASE 		=hidden_act
           SCREAMING_SNAKE_CASE 		=hidden_dropout_prob
           SCREAMING_SNAKE_CASE 		=attention_probs_dropout_prob
           SCREAMING_SNAKE_CASE 		=max_position_embeddings
           SCREAMING_SNAKE_CASE 		=type_vocab_size
           SCREAMING_SNAKE_CASE 		=type_sequence_label_size
           SCREAMING_SNAKE_CASE 		=initializer_range
           SCREAMING_SNAKE_CASE 		=num_labels
           SCREAMING_SNAKE_CASE 		=num_choices
           SCREAMING_SNAKE_CASE 		=scope
      def 			_lowerCAmelCase	( self	:				Union[str, Any]							):
           SCREAMING_SNAKE_CASE 		=ids_tensor([self.batch_size, self.seq_length]     ,self.vocab_size							)

           SCREAMING_SNAKE_CASE 		=None
           if self.use_input_mask:
                SCREAMING_SNAKE_CASE 		=random_attention_mask([self.batch_size, self.seq_length]							)

           SCREAMING_SNAKE_CASE 		=None
           if self.use_token_type_ids:
                SCREAMING_SNAKE_CASE 		=ids_tensor([self.batch_size, self.seq_length]     ,self.type_vocab_size							)

           SCREAMING_SNAKE_CASE 		=None
           SCREAMING_SNAKE_CASE 		=None
           SCREAMING_SNAKE_CASE 		=None
           if self.use_labels:
                SCREAMING_SNAKE_CASE 		=ids_tensor([self.batch_size]     ,self.type_sequence_label_size							)
                SCREAMING_SNAKE_CASE 		=ids_tensor([self.batch_size, self.seq_length]     ,self.num_labels							)
                SCREAMING_SNAKE_CASE 		=ids_tensor([self.batch_size]     ,self.num_choices							)
                SCREAMING_SNAKE_CASE 		=ids_tensor([self.batch_size, self.seq_length]     ,1							)

           SCREAMING_SNAKE_CASE 		=self.get_config()

           return (
               config,
               input_ids,
               token_type_ids,
               input_mask,
               sequence_labels,
               token_labels,
               choice_labels,
               fake_token_labels,
           )
      def 			_lowerCAmelCase	( self	:				List[Any]							):
           return ElectraConfig(
               vocab_size=self.vocab_size     ,hidden_size=self.hidden_size     ,num_hidden_layers=self.num_hidden_layers     ,num_attention_heads=self.num_attention_heads     ,intermediate_size=self.intermediate_size     ,hidden_act=self.hidden_act     ,hidden_dropout_prob=self.hidden_dropout_prob     ,attention_probs_dropout_prob=self.attention_probs_dropout_prob     ,max_position_embeddings=self.max_position_embeddings     ,type_vocab_size=self.type_vocab_size     ,is_decoder=snake_case     ,initializer_range=self.initializer_range     ,)
      def 			_lowerCAmelCase	( self	:				Union[str, Any]							):
           (
               (
               SCREAMING_SNAKE_CASE
           )		,    (
               SCREAMING_SNAKE_CASE
           )		,    (
               SCREAMING_SNAKE_CASE
           )		,    (
               SCREAMING_SNAKE_CASE
           )		,    (
               SCREAMING_SNAKE_CASE
           )		,    (
               SCREAMING_SNAKE_CASE
           )		,    (
               SCREAMING_SNAKE_CASE
           )		,    (
               SCREAMING_SNAKE_CASE
           )		,    
           ) 		=self.prepare_config_and_inputs()

           SCREAMING_SNAKE_CASE 		=True
           SCREAMING_SNAKE_CASE 		=floats_tensor([self.batch_size, self.seq_length, self.hidden_size]							)
           SCREAMING_SNAKE_CASE 		=ids_tensor([self.batch_size, self.seq_length]     ,vocab_size=2							)

           return (
               config,
               input_ids,
               token_type_ids,
               input_mask,
               sequence_labels,
               token_labels,
               choice_labels,
               encoder_hidden_states,
               encoder_attention_mask,
           )
      def 			_lowerCAmelCase	( self	:				int     ,snake_case	:				Dict     ,snake_case	:				Union[str, Any]     ,snake_case	:				Optional[Any]     ,snake_case	:				Any     ,snake_case	:				Any     ,snake_case	:				Union[str, Any]     ,snake_case	:				List[Any]     ,snake_case	:				List[Any]     ,):
           SCREAMING_SNAKE_CASE 		=ElectraModel(config=snake_case							)
           model.to(snake_case							)
           model.eval()
           SCREAMING_SNAKE_CASE 		=model(snake_case     ,attention_mask=snake_case     ,token_type_ids=snake_case							)
           SCREAMING_SNAKE_CASE 		=model(snake_case     ,token_type_ids=snake_case							)
           SCREAMING_SNAKE_CASE 		=model(snake_case							)
           self.parent.assertEqual(result.last_hidden_state.shape     ,(self.batch_size, self.seq_length, self.hidden_size)							)
      def 			_lowerCAmelCase	( self	:				int     ,snake_case	:				Tuple     ,snake_case	:				Union[str, Any]     ,snake_case	:				Union[str, Any]     ,snake_case	:				List[str]     ,snake_case	:				Dict     ,snake_case	:				Any     ,snake_case	:				Any     ,snake_case	:				str     ,snake_case	:				Any     ,):
           SCREAMING_SNAKE_CASE 		=True
           SCREAMING_SNAKE_CASE 		=ElectraModel(snake_case							)
           model.to(snake_case							)
           model.eval()
           SCREAMING_SNAKE_CASE 		=model(
               snake_case     ,attention_mask=snake_case     ,token_type_ids=snake_case     ,encoder_hidden_states=snake_case     ,encoder_attention_mask=snake_case     ,)
           SCREAMING_SNAKE_CASE 		=model(
               snake_case     ,attention_mask=snake_case     ,token_type_ids=snake_case     ,encoder_hidden_states=snake_case     ,)
           SCREAMING_SNAKE_CASE 		=model(snake_case     ,attention_mask=snake_case     ,token_type_ids=snake_case							)
           self.parent.assertEqual(result.last_hidden_state.shape     ,(self.batch_size, self.seq_length, self.hidden_size)							)
      def 			_lowerCAmelCase	( self	:				Optional[int]     ,snake_case	:				Union[str, Any]     ,snake_case	:				List[str]     ,snake_case	:				List[Any]     ,snake_case	:				Dict     ,snake_case	:				Optional[int]     ,snake_case	:				Tuple     ,snake_case	:				List[str]     ,snake_case	:				Dict     ,):
           SCREAMING_SNAKE_CASE 		=ElectraForMaskedLM(config=snake_case							)
           model.to(snake_case							)
           model.eval()
           SCREAMING_SNAKE_CASE 		=model(snake_case     ,attention_mask=snake_case     ,token_type_ids=snake_case     ,labels=snake_case							)
           self.parent.assertEqual(result.logits.shape     ,(self.batch_size, self.seq_length, self.vocab_size)							)
      def 			_lowerCAmelCase	( self	:				List[Any]     ,snake_case	:				Dict     ,snake_case	:				List[str]     ,snake_case	:				Dict     ,snake_case	:				List[str]     ,snake_case	:				Any     ,snake_case	:				int     ,snake_case	:				Tuple     ,snake_case	:				int     ,snake_case	:				Any     ,):
           SCREAMING_SNAKE_CASE 		=ElectraForCausalLM(config=snake_case							)
           model.to(snake_case							)
           model.eval()
           SCREAMING_SNAKE_CASE 		=model(snake_case     ,attention_mask=snake_case     ,token_type_ids=snake_case     ,labels=snake_case							)
           self.parent.assertEqual(result.logits.shape     ,(self.batch_size, self.seq_length, self.vocab_size)							)
      def 			_lowerCAmelCase	( self	:				Dict     ,snake_case	:				str     ,snake_case	:				List[str]     ,snake_case	:				str     ,snake_case	:				int     ,snake_case	:				int     ,snake_case	:				int     ,snake_case	:				List[Any]     ,snake_case	:				List[str]     ,):
           SCREAMING_SNAKE_CASE 		=self.num_labels
           SCREAMING_SNAKE_CASE 		=ElectraForTokenClassification(config=snake_case							)
           model.to(snake_case							)
           model.eval()
           SCREAMING_SNAKE_CASE 		=model(snake_case     ,attention_mask=snake_case     ,token_type_ids=snake_case     ,labels=snake_case							)
           self.parent.assertEqual(result.logits.shape     ,(self.batch_size, self.seq_length, self.num_labels)							)
      def 			_lowerCAmelCase	( self	:				List[Any]     ,snake_case	:				Any     ,snake_case	:				List[str]     ,snake_case	:				int     ,snake_case	:				Any     ,snake_case	:				List[str]     ,snake_case	:				int     ,snake_case	:				Dict     ,snake_case	:				Dict     ,):
           SCREAMING_SNAKE_CASE 		=self.num_labels
           SCREAMING_SNAKE_CASE 		=ElectraForPreTraining(config=snake_case							)
           model.to(snake_case							)
           model.eval()
           SCREAMING_SNAKE_CASE 		=model(snake_case     ,attention_mask=snake_case     ,token_type_ids=snake_case     ,labels=snake_case							)
           self.parent.assertEqual(result.logits.shape     ,(self.batch_size, self.seq_length)							)
      def 			_lowerCAmelCase	( self	:				int     ,snake_case	:				List[str]     ,snake_case	:				Optional[int]     ,snake_case	:				str     ,snake_case	:				Optional[int]     ,snake_case	:				Dict     ,snake_case	:				Any     ,snake_case	:				List[str]     ,snake_case	:				int     ,):
           SCREAMING_SNAKE_CASE 		=self.num_labels
           SCREAMING_SNAKE_CASE 		=ElectraForSequenceClassification(snake_case							)
           model.to(snake_case							)
           model.eval()
           SCREAMING_SNAKE_CASE 		=model(snake_case     ,attention_mask=snake_case     ,token_type_ids=snake_case     ,labels=snake_case							)
           self.parent.assertEqual(result.logits.shape     ,(self.batch_size, self.num_labels)							)
      def 			_lowerCAmelCase	( self	:				Any     ,snake_case	:				Optional[int]     ,snake_case	:				Optional[Any]     ,snake_case	:				Tuple     ,snake_case	:				List[str]     ,snake_case	:				Tuple     ,snake_case	:				List[str]     ,snake_case	:				Dict     ,snake_case	:				Optional[Any]     ,):
           SCREAMING_SNAKE_CASE 		=ElectraForQuestionAnswering(config=snake_case							)
           model.to(snake_case							)
           model.eval()
           SCREAMING_SNAKE_CASE 		=model(
               snake_case     ,attention_mask=snake_case     ,token_type_ids=snake_case     ,start_positions=snake_case     ,end_positions=snake_case     ,)
           self.parent.assertEqual(result.start_logits.shape     ,(self.batch_size, self.seq_length)							)
           self.parent.assertEqual(result.end_logits.shape     ,(self.batch_size, self.seq_length)							)
      def 			_lowerCAmelCase	( self	:				Optional[Any]     ,snake_case	:				List[Any]     ,snake_case	:				List[Any]     ,snake_case	:				List[str]     ,snake_case	:				str     ,snake_case	:				List[str]     ,snake_case	:				List[str]     ,snake_case	:				Tuple     ,snake_case	:				Any     ,):
           SCREAMING_SNAKE_CASE 		=self.num_choices
           SCREAMING_SNAKE_CASE 		=ElectraForMultipleChoice(config=snake_case							)
           model.to(snake_case							)
           model.eval()
           SCREAMING_SNAKE_CASE 		=input_ids.unsqueeze(1							).expand(-1     ,self.num_choices     ,-1							).contiguous()
           SCREAMING_SNAKE_CASE 		=token_type_ids.unsqueeze(1							).expand(-1     ,self.num_choices     ,-1							).contiguous()
           SCREAMING_SNAKE_CASE 		=input_mask.unsqueeze(1							).expand(-1     ,self.num_choices     ,-1							).contiguous()
           SCREAMING_SNAKE_CASE 		=model(
               snake_case     ,attention_mask=snake_case     ,token_type_ids=snake_case     ,labels=snake_case     ,)
           self.parent.assertEqual(result.logits.shape     ,(self.batch_size, self.num_choices)							)


      def 			_lowerCAmelCase	( self	:				Optional[int]							):
           SCREAMING_SNAKE_CASE 		=self.prepare_config_and_inputs()
           (
               (
               SCREAMING_SNAKE_CASE
           )		,    (
               SCREAMING_SNAKE_CASE
           )		,    (
               SCREAMING_SNAKE_CASE
           )		,    (
               SCREAMING_SNAKE_CASE
           )		,    (
               SCREAMING_SNAKE_CASE
           )		,    (
               SCREAMING_SNAKE_CASE
           )		,    (
               SCREAMING_SNAKE_CASE
           )		,    (
               SCREAMING_SNAKE_CASE
           )		,    
           ) 		=config_and_inputs
           SCREAMING_SNAKE_CASE 		={'input_ids': input_ids, 'token_type_ids': token_type_ids, 'attention_mask': input_mask}
           return config, inputs_dict







@require_torch
class 						a_			(				lowerCamelCase_					,     lowerCamelCase_					,     unittest.TestCase	):



      """simple docstring"""







      __UpperCAmelCase     						= (
          (
              ElectraModel,
              ElectraForPreTraining,
              ElectraForMaskedLM,
              ElectraForCausalLM,
              ElectraForMultipleChoice,
              ElectraForTokenClassification,
              ElectraForSequenceClassification,
              ElectraForQuestionAnswering,
          )
          if is_torch_available()
          else ()
      )
      __UpperCAmelCase     						= (
          {
              'feature-extraction': ElectraModel,
              'fill-mask': ElectraForMaskedLM,
              'question-answering': ElectraForQuestionAnswering,
              'text-classification': ElectraForSequenceClassification,
              'text-generation': ElectraForCausalLM,
              'token-classification': ElectraForTokenClassification,
              'zero-shot': ElectraForSequenceClassification,
          }
          if is_torch_available()
          else {}
      )
      __UpperCAmelCase     						= True
      def 			_lowerCAmelCase	( self	:				Any     ,snake_case	:				Any     ,snake_case	:				List[Any]     ,snake_case	:				Optional[int]=False							):
           SCREAMING_SNAKE_CASE 		=super()._prepare_for_class(snake_case     ,snake_case     ,return_labels=snake_case							)

           if return_labels:
                if model_class in get_values(snake_case							):
                     SCREAMING_SNAKE_CASE 		=torch.zeros(
                         (self.model_tester.batch_size, self.model_tester.seq_length)     ,dtype=torch.long     ,device=snake_case							)
           return inputs_dict
      def 			_lowerCAmelCase	( self	:				Dict							):
           SCREAMING_SNAKE_CASE 		=ElectraModelTester(self							)
           SCREAMING_SNAKE_CASE 		=ConfigTester(self     ,config_class=snake_case     ,hidden_size=37							)
      def 			_lowerCAmelCase	( self	:				Union[str, Any]							):
           self.config_tester.run_common_tests()
      def 			_lowerCAmelCase	( self	:				Dict							):
           SCREAMING_SNAKE_CASE 		=self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_electra_model(*snake_case							)
      def 			_lowerCAmelCase	( self	:				Optional[Any]							):
           SCREAMING_SNAKE_CASE 		=self.model_tester.prepare_config_and_inputs_for_decoder()
           self.model_tester.create_and_check_electra_model_as_decoder(*snake_case							)
      def 			_lowerCAmelCase	( self	:				Optional[int]							):
           SCREAMING_SNAKE_CASE 		=self.model_tester.prepare_config_and_inputs()
           for type in ["absolute", "relative_key", "relative_key_query"]:
                SCREAMING_SNAKE_CASE 		=type
                self.model_tester.create_and_check_electra_model(*snake_case							)
      def 			_lowerCAmelCase	( self	:				int							):
           SCREAMING_SNAKE_CASE 		=self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_electra_for_masked_lm(*snake_case							)
      def 			_lowerCAmelCase	( self	:				Union[str, Any]							):
           SCREAMING_SNAKE_CASE 		=self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_electra_for_token_classification(*snake_case							)
      def 			_lowerCAmelCase	( self	:				Dict							):
           SCREAMING_SNAKE_CASE 		=self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_electra_for_pretraining(*snake_case							)
      def 			_lowerCAmelCase	( self	:				Dict							):
           SCREAMING_SNAKE_CASE 		=self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_electra_for_sequence_classification(*snake_case							)
      def 			_lowerCAmelCase	( self	:				Tuple							):
           SCREAMING_SNAKE_CASE 		=self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_electra_for_question_answering(*snake_case							)
      def 			_lowerCAmelCase	( self	:				str							):
           SCREAMING_SNAKE_CASE 		=self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_electra_for_multiple_choice(*snake_case							)
      @slow
      def 			_lowerCAmelCase	( self	:				str							):
           for model_name in ELECTRA_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
                SCREAMING_SNAKE_CASE 		=ElectraModel.from_pretrained(snake_case							)
                self.assertIsNotNone(snake_case							)


      def 			_lowerCAmelCase	( self	:				Any							):
           SCREAMING_SNAKE_CASE 		=self.model_tester.prepare_config_and_inputs_for_decoder()
           self.model_tester.create_and_check_electra_for_causal_lm(*snake_case							)







@require_torch
class 						a_			(				unittest.TestCase	):



      """simple docstring"""







      @slow
      def 			_lowerCAmelCase	( self	:				int							):
           SCREAMING_SNAKE_CASE 		=ElectraModel.from_pretrained('google/electra-small-discriminator'							)
           SCREAMING_SNAKE_CASE 		=torch.tensor([[0, 345, 232, 328, 740, 140, 1695, 69, 6078, 1588, 2]]							)
           SCREAMING_SNAKE_CASE 		=torch.tensor([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]							)
           SCREAMING_SNAKE_CASE 		=model(snake_case     ,attention_mask=snake_case							)[0]
           SCREAMING_SNAKE_CASE 		=torch.Size((1, 11, 256)							)
           self.assertEqual(output.shape     ,snake_case							)
           SCREAMING_SNAKE_CASE 		=torch.tensor(
               [[[0.4_471, 0.6_821, -0.3_265], [0.4_627, 0.5_255, -0.3_668], [0.4_532, 0.3_313, -0.4_344]]]							)

           self.assertTrue(torch.allclose(output[:, 1:4, 1:4]     ,snake_case     ,atol=1e-4							)							)




import os







def      snake_case__							(  ):





     """simple docstring"""







     SCREAMING_SNAKE_CASE 		=os.path.dirname(os.path.realpath(lowerCAmelCase_     )     )
     SCREAMING_SNAKE_CASE 		=os.path.join(lowerCAmelCase_,       'triangle.txt'     )

     with open(lowerCAmelCase_     ) as f:
          SCREAMING_SNAKE_CASE 		=f.readlines()

     SCREAMING_SNAKE_CASE 		=[[int(lowerCAmelCase_     ) for y in x.rstrip('\r\n'     ).split(' '     )] for x in triangle]

     for i in range(1,       len(lowerCAmelCase_     )     ):
          for j in range(len(a[i]     )     ):
               SCREAMING_SNAKE_CASE 		=a[i - 1][j] if j != len(a[i - 1]     ) else 0
               SCREAMING_SNAKE_CASE 		=a[i - 1][j - 1] if j > 0 else 0
               a[i][j] += max(lowerCAmelCase_,       lowerCAmelCase_     )
     return max(a[-1]     )


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

