





import tempfile
import unittest

from transformers import DPRConfig, is_torch_available
from transformers.testing_utils import require_torch, slow, torch_device

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


if is_torch_available():
      import torch

      from transformers import DPRContextEncoder, DPRQuestionEncoder, DPRReader, DPRReaderTokenizer
      from transformers.models.dpr.modeling_dpr import (
          DPR_CONTEXT_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST,
          DPR_QUESTION_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST,
          DPR_READER_PRETRAINED_MODEL_ARCHIVE_LIST,
      )

class 						UpperCamelCase__					:



     '''simple docstring'''

     def __init__(				self		:       Dict		,lowerCamelCase__		:       Dict		,lowerCamelCase__		:       Optional[int]=13		,lowerCamelCase__		:       List[str]=7		,lowerCamelCase__		:       Tuple=False		,lowerCamelCase__		:       int=True		,lowerCamelCase__		:       Any=True		,lowerCamelCase__		:       List[Any]=True		,lowerCamelCase__		:       List[str]=99		,lowerCamelCase__		:       List[Any]=32		,lowerCamelCase__		:       int=5		,lowerCamelCase__		:       Optional[Any]=4		,lowerCamelCase__		:       Any=37		,lowerCamelCase__		:       List[str]="gelu"		,lowerCamelCase__		:       Any=0.1		,lowerCamelCase__		:       Optional[int]=0.1		,lowerCamelCase__		:       Optional[int]=512		,lowerCamelCase__		:       Optional[Any]=16		,lowerCamelCase__		:       Any=2		,lowerCamelCase__		:       Union[str, Any]=0.02		,lowerCamelCase__		:       Optional[int]=3		,lowerCamelCase__		:       Union[str, Any]=4		,lowerCamelCase__		:       Optional[int]=None		,lowerCamelCase__		:       str=0		,)	->						Union[str, Any]:




           '''simple docstring'''


           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
           SCREAMING_SNAKE_CASE						=		projection_dim






     def 							SCREAMING_SNAKE_CASE__    (				self		:       Optional[int]			)	->						List[str]:




           '''simple docstring'''


           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						=		self.get_config()

           return config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels






     def 							SCREAMING_SNAKE_CASE__    (				self		:       Any			)	->						Any:




           '''simple docstring'''


           return DPRConfig(
               projection_dim=self.projection_dim		,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		,initializer_range=self.initializer_range		,)






     def 							SCREAMING_SNAKE_CASE__    (				self		:       List[Any]		,lowerCamelCase__		:       Dict		,lowerCamelCase__		:       Dict		,lowerCamelCase__		:       List[Any]		,lowerCamelCase__		:       Union[str, Any]		,lowerCamelCase__		:       Optional[Any]		,lowerCamelCase__		:       Tuple		,lowerCamelCase__		:       int			)	->						Optional[int]:




           '''simple docstring'''


           SCREAMING_SNAKE_CASE						=		DPRContextEncoder(config=lowerCamelCase__			)
           model.to(lowerCamelCase__			)
           model.eval()
           SCREAMING_SNAKE_CASE						=		model(lowerCamelCase__		,attention_mask=lowerCamelCase__		,token_type_ids=lowerCamelCase__			)
           SCREAMING_SNAKE_CASE						=		model(lowerCamelCase__		,token_type_ids=lowerCamelCase__			)
           SCREAMING_SNAKE_CASE						=		model(lowerCamelCase__			)
           self.parent.assertEqual(result.pooler_output.shape		,(self.batch_size, self.projection_dim or self.hidden_size)			)






     def 							SCREAMING_SNAKE_CASE__    (				self		:       List[Any]		,lowerCamelCase__		:       str		,lowerCamelCase__		:       Union[str, Any]		,lowerCamelCase__		:       str		,lowerCamelCase__		:       Optional[Any]		,lowerCamelCase__		:       Dict		,lowerCamelCase__		:       str		,lowerCamelCase__		:       Optional[Any]			)	->						Dict:




           '''simple docstring'''


           SCREAMING_SNAKE_CASE						=		DPRQuestionEncoder(config=lowerCamelCase__			)
           model.to(lowerCamelCase__			)
           model.eval()
           SCREAMING_SNAKE_CASE						=		model(lowerCamelCase__		,attention_mask=lowerCamelCase__		,token_type_ids=lowerCamelCase__			)
           SCREAMING_SNAKE_CASE						=		model(lowerCamelCase__		,token_type_ids=lowerCamelCase__			)
           SCREAMING_SNAKE_CASE						=		model(lowerCamelCase__			)
           self.parent.assertEqual(result.pooler_output.shape		,(self.batch_size, self.projection_dim or self.hidden_size)			)






     def 							SCREAMING_SNAKE_CASE__    (				self		:       str		,lowerCamelCase__		:       List[str]		,lowerCamelCase__		:       Union[str, Any]		,lowerCamelCase__		:       int		,lowerCamelCase__		:       Union[str, Any]		,lowerCamelCase__		:       Any		,lowerCamelCase__		:       List[str]		,lowerCamelCase__		:       str			)	->						List[Any]:




           '''simple docstring'''


           SCREAMING_SNAKE_CASE						=		DPRReader(config=lowerCamelCase__			)
           model.to(lowerCamelCase__			)
           model.eval()
           SCREAMING_SNAKE_CASE						=		model(
               lowerCamelCase__		,attention_mask=lowerCamelCase__		,)

           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)			)
           self.parent.assertEqual(result.relevance_logits.shape		,(self.batch_size,)			)






     def 							SCREAMING_SNAKE_CASE__    (				self		:       Dict			)	->						List[str]:




           '''simple docstring'''


           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
           ),   
           )						=		config_and_inputs
           SCREAMING_SNAKE_CASE						=		{"""input_ids""": input_ids}
           return config, inputs_dict

@require_torch
class 						UpperCamelCase__					( lowerCAmelCase_				, lowerCAmelCase_				, unittest.TestCase  ):



     '''simple docstring'''

     __snake_case       :      Any						      =     (
         (
             DPRContextEncoder,
             DPRQuestionEncoder,
             DPRReader,
         )
         if is_torch_available()
         else ()
     )
     __snake_case       :      Optional[int]						      =     {"feature-extraction": DPRQuestionEncoder} if is_torch_available() else {}

     __snake_case       :      str						      =     False
     __snake_case       :      int						      =     False  # why?
     __snake_case       :      Union[str, Any]						      =     False
     __snake_case       :      List[Any]						      =     False






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




           '''simple docstring'''


           SCREAMING_SNAKE_CASE						=		DPRModelTester(self			)
           SCREAMING_SNAKE_CASE						=		ConfigTester(self		,config_class=lowerCamelCase__		,hidden_size=37			)






     def 							SCREAMING_SNAKE_CASE__    (				self		:       int			)	->						List[str]:




           '''simple docstring'''


           self.config_tester.run_common_tests()






     def 							SCREAMING_SNAKE_CASE__    (				self		:       str			)	->						Union[str, Any]:




           '''simple docstring'''


           SCREAMING_SNAKE_CASE						=		self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_context_encoder(*lowerCamelCase__			)






     def 							SCREAMING_SNAKE_CASE__    (				self		:       Optional[Any]			)	->						int:




           '''simple docstring'''


           SCREAMING_SNAKE_CASE						=		self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_question_encoder(*lowerCamelCase__			)






     def 							SCREAMING_SNAKE_CASE__    (				self		:       str			)	->						int:




           '''simple docstring'''


           SCREAMING_SNAKE_CASE						=		self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_reader(*lowerCamelCase__			)






     def 							SCREAMING_SNAKE_CASE__    (				self		:       List[Any]			)	->						Tuple:




           '''simple docstring'''


           SCREAMING_SNAKE_CASE						=		self.model_tester.prepare_config_and_inputs()[0]

           SCREAMING_SNAKE_CASE						=		DPRQuestionEncoder(config=lowerCamelCase__			)
           model.to(lowerCamelCase__			)
           model.eval()

           with tempfile.TemporaryDirectory() as tmp_dirname:
                 model.save_pretrained(lowerCamelCase__			)
                 SCREAMING_SNAKE_CASE						=		DPRQuestionEncoder.from_pretrained(lowerCamelCase__		,projection_dim=512			)

           self.assertIsNotNone(lowerCamelCase__			)






     @slow
     def 							SCREAMING_SNAKE_CASE__    (				self		:       List[Any]			)	->						Any:




           '''simple docstring'''


           for model_name in DPR_CONTEXT_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
                 SCREAMING_SNAKE_CASE						=		DPRContextEncoder.from_pretrained(lowerCamelCase__			)
                 self.assertIsNotNone(lowerCamelCase__			)

           for model_name in DPR_CONTEXT_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
                 SCREAMING_SNAKE_CASE						=		DPRContextEncoder.from_pretrained(lowerCamelCase__			)
                 self.assertIsNotNone(lowerCamelCase__			)

           for model_name in DPR_QUESTION_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
                 SCREAMING_SNAKE_CASE						=		DPRQuestionEncoder.from_pretrained(lowerCamelCase__			)
                 self.assertIsNotNone(lowerCamelCase__			)

           for model_name in DPR_READER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
                 SCREAMING_SNAKE_CASE						=		DPRReader.from_pretrained(lowerCamelCase__			)
                 self.assertIsNotNone(lowerCamelCase__			)

@require_torch
class 						UpperCamelCase__					( unittest.TestCase  ):



     '''simple docstring'''

     @slow
     def 							SCREAMING_SNAKE_CASE__    (				self		:       str			)	->						str:




           '''simple docstring'''


           SCREAMING_SNAKE_CASE						=		DPRQuestionEncoder.from_pretrained("""facebook/dpr-question_encoder-single-nq-base"""		,return_dict=lowerCamelCase__			)
           model.to(lowerCamelCase__			)

           SCREAMING_SNAKE_CASE						=		torch.tensor(
               [[101, 7592, 1010, 2003, 2026, 3899, 10140, 1029, 102]]		,dtype=torch.long		,device=lowerCamelCase__			)  # [CLS] hello, is my dog cute? [SEP]
           SCREAMING_SNAKE_CASE						=		model(lowerCamelCase__			)[0]  # embedding shape = (1, 768)
           # compare the actual values for a slice.
           SCREAMING_SNAKE_CASE						=		torch.tensor(
               [
                   [
                       0.03236253,
                       0.12753335,
                       0.16818509,
                       0.00279786,
                       0.3896933,
                       0.24264945,
                       0.2178971,
                       -0.02335227,
                       -0.08481959,
                       -0.14324117,
                   ]
               ]		,dtype=torch.float		,device=lowerCamelCase__		,)
           self.assertTrue(torch.allclose(output[:, :10]		,lowerCamelCase__		,atol=1e-4			)			)






     @slow
     def 							SCREAMING_SNAKE_CASE__    (				self		:       Optional[Any]			)	->						Optional[int]:




           '''simple docstring'''


           SCREAMING_SNAKE_CASE						=		DPRReaderTokenizer.from_pretrained("""facebook/dpr-reader-single-nq-base"""			)
           SCREAMING_SNAKE_CASE						=		DPRReader.from_pretrained("""facebook/dpr-reader-single-nq-base"""			)
           model.to(lowerCamelCase__			)

           SCREAMING_SNAKE_CASE						=		tokenizer(
               questions="""What is love ?"""		,titles="""Haddaway"""		,texts="""What Is Love is a song recorded by the artist Haddaway"""		,padding=lowerCamelCase__		,return_tensors="""pt"""		,)
           encoded_inputs.to(lowerCamelCase__			)

           SCREAMING_SNAKE_CASE						=		model(**lowerCamelCase__			)

           # compare the actual values for a slice.
           SCREAMING_SNAKE_CASE						=		torch.tensor(
               [[-10.3005, -10.7765, -11.4872, -11.6841, -11.9312, -10.3002, -9.8544, -11.7378, -12.0821, -10.2975]]		,dtype=torch.float		,device=lowerCamelCase__		,)

           SCREAMING_SNAKE_CASE						=		torch.tensor(
               [[-11.0684, -11.7041, -11.5397, -10.3465, -10.8791, -6.8443, -11.9959, -11.0364, -10.0096, -6.8405]]		,dtype=torch.float		,device=lowerCamelCase__		,)
           self.assertTrue(torch.allclose(outputs.start_logits[:, :10]		,lowerCamelCase__		,atol=1e-4			)			)
           self.assertTrue(torch.allclose(outputs.end_logits[:, :10]		,lowerCamelCase__		,atol=1e-4			)			)









import os



def   __lowercase  ( )					->  int:


      '''simple docstring'''


      SCREAMING_SNAKE_CASE						=		os.path.dirname(os.path.realpath(_SCREAMING_SNAKE_CASE      )      )
      SCREAMING_SNAKE_CASE						=		os.path.join(_SCREAMING_SNAKE_CASE					,		"""triangle.txt"""      )

      with open(_SCREAMING_SNAKE_CASE      ) as in_file:
            SCREAMING_SNAKE_CASE						=		[[int(_SCREAMING_SNAKE_CASE      ) for i in line.split()] for line in in_file]

      while len(_SCREAMING_SNAKE_CASE      ) != 1:
            SCREAMING_SNAKE_CASE						=		triangle.pop()
            SCREAMING_SNAKE_CASE						=		triangle[-1]
            for j in range(len(_SCREAMING_SNAKE_CASE      ) - 1      ):
                  curr_row[j] += max(last_row[j]					,		last_row[j + 1]      )
      return triangle[0][0]


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


