
'''simple docstring'''



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 				_a			:





       """simple docstring"""




       def __init__(							self    ,							A__    ,							A__=13    ,							A__=7    ,							A__=False    ,							A__=True    ,							A__=True    ,							A__=True    ,							A__=99    ,							A__=32    ,							A__=5    ,							A__=4    ,							A__=37    ,							A__="gelu"    ,							A__=0.1    ,							A__=0.1    ,							A__=5_12    ,							A__=16    ,							A__=2    ,							A__=0.02    ,							A__=3    ,							A__=4    ,							A__=None    ,							A__=0    ,							)      ->     Optional[int]:
          _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     UpperCamelCase			(							self )      ->     str:
          _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     UpperCamelCase			(							self )      ->     Any:
          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     UpperCamelCase			(							self    ,							A__    ,							A__    ,							A__    ,							A__    ,							A__    ,							A__    ,							A__ )      ->     Any:
          _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     UpperCamelCase			(							self    ,							A__    ,							A__    ,							A__    ,							A__    ,							A__    ,							A__    ,							A__ )      ->     Dict:
          _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     UpperCamelCase			(							self    ,							A__    ,							A__    ,							A__    ,							A__    ,							A__    ,							A__    ,							A__ )      ->     Union[str, Any]:
          _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     UpperCamelCase			(							self )      ->     Tuple:
          _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 				_a			(lowercase__					,   lowercase__					,   unittest.TestCase):





       """simple docstring"""




       SCREAMING_SNAKE_CASE =					(
           (
               DPRContextEncoder,
               DPRQuestionEncoder,
               DPRReader,
           )
           if is_torch_available()
           else ()
       )
       SCREAMING_SNAKE_CASE =					{'''feature-extraction''': DPRQuestionEncoder} if is_torch_available() else {}

       SCREAMING_SNAKE_CASE =					False
       SCREAMING_SNAKE_CASE =					False  # why?
       SCREAMING_SNAKE_CASE =					False
       SCREAMING_SNAKE_CASE =					False






       def     UpperCamelCase			(							self )      ->     Tuple:
          _SCREAMING_SNAKE_CASE       	=			DPRModelTester(self )
          _SCREAMING_SNAKE_CASE       	=			ConfigTester(self    ,							config_class=lowerCAmelCase__    ,							hidden_size=37 )






       def     UpperCamelCase			(							self )      ->     List[Any]:
          self.config_tester.run_common_tests()






       def     UpperCamelCase			(							self )      ->     str:
          _SCREAMING_SNAKE_CASE       	=			self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_context_encoder(*lowerCAmelCase__ )






       def     UpperCamelCase			(							self )      ->     Optional[int]:
          _SCREAMING_SNAKE_CASE       	=			self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_question_encoder(*lowerCAmelCase__ )






       def     UpperCamelCase			(							self )      ->     List[Any]:
          _SCREAMING_SNAKE_CASE       	=			self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_reader(*lowerCAmelCase__ )






       def     UpperCamelCase			(							self )      ->     List[Any]:
          _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=5_12 )

          self.assertIsNotNone(lowerCAmelCase__ )






       @slow
       def     UpperCamelCase			(							self )      ->     List[str]:
          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 				_a			(unittest.TestCase):





       """simple docstring"""




       @slow
       def     UpperCamelCase			(							self )      ->     Optional[Any]:
          _SCREAMING_SNAKE_CASE       	=			DPRQuestionEncoder.from_pretrained("""facebook/dpr-question_encoder-single-nq-base"""    ,							return_dict=lowerCAmelCase__ )
          model.to(lowerCAmelCase__ )

          _SCREAMING_SNAKE_CASE       	=			torch.tensor(
              [[1_01, 75_92, 10_10, 20_03, 20_26, 38_99, 1_01_40, 10_29, 1_02]]    ,							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.0323_6253,
                      0.1275_3335,
                      0.1681_8509,
                      0.0027_9786,
                      0.389_6933,
                      0.2426_4945,
                      0.217_8971,
                      -0.0233_5227,
                      -0.0848_1959,
                      -0.1432_4117,
                  ]
              ]    ,							dtype=torch.float    ,							device=lowerCAmelCase__    ,							)
          self.assertTrue(torch.allclose(output[:, :10]    ,							lowerCAmelCase__    ,							atol=1E-4 ) )






       @slow
       def     UpperCamelCase			(							self )      ->     List[Any]:
          _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 ) )








'''simple docstring'''



def 				lowerCAmelCase_		(			SCREAMING_SNAKE_CASE_      , SCREAMING_SNAKE_CASE_  )       ->       Optional[int]:




   """simple docstring"""
   print("""\nVertex Distance"""  )
   for i in range(SCREAMING_SNAKE_CASE_  ):
      if dist[i] != float("""inf"""  ):
         print(SCREAMING_SNAKE_CASE_      , """\t"""      , int(dist[i]  )      , end="""\t"""  )
      else:
         print(SCREAMING_SNAKE_CASE_      , """\t"""      , """INF"""      , end="""\t"""  )
      print()
def 				lowerCAmelCase_		(			SCREAMING_SNAKE_CASE_      , SCREAMING_SNAKE_CASE_      , SCREAMING_SNAKE_CASE_  )       ->       Optional[Any]:




   """simple docstring"""
   _SCREAMING_SNAKE_CASE       	=			float("""inf"""  )
   _SCREAMING_SNAKE_CASE       	=			-1
   for i in range(SCREAMING_SNAKE_CASE_  ):
      if (not vset[i]) and mdist[i] < min_val:
         _SCREAMING_SNAKE_CASE       	=			i
         _SCREAMING_SNAKE_CASE       	=			mdist[i]
   return min_ind
def 				lowerCAmelCase_		(			SCREAMING_SNAKE_CASE_      , SCREAMING_SNAKE_CASE_      , SCREAMING_SNAKE_CASE_  )       ->       int:




   """simple docstring"""
   _SCREAMING_SNAKE_CASE       	=			[float("""inf"""  ) for _ in range(SCREAMING_SNAKE_CASE_  )]
   _SCREAMING_SNAKE_CASE       	=			[False for _ in range(SCREAMING_SNAKE_CASE_  )]
   _SCREAMING_SNAKE_CASE       	=			0.0

   for _ in range(v - 1  ):
      _SCREAMING_SNAKE_CASE       	=			min_dist(SCREAMING_SNAKE_CASE_      , SCREAMING_SNAKE_CASE_      , SCREAMING_SNAKE_CASE_  )
      _SCREAMING_SNAKE_CASE       	=			True

      for i in range(SCREAMING_SNAKE_CASE_  ):
         if (
             (not vset[i])
             and graph[u][i] != float("""inf"""  )
             and mdist[u] + graph[u][i] < mdist[i]
         ):
            _SCREAMING_SNAKE_CASE       	=			mdist[u] + graph[u][i]

   print_dist(SCREAMING_SNAKE_CASE_      , SCREAMING_SNAKE_CASE_  )


if __name__ == "__main__":
     UpperCamelCase__ :						int    							=					int(input("Enter number of vertices: ").strip())
     UpperCamelCase__ :						Tuple    							=					int(input("Enter number of edges: ").strip())

     UpperCamelCase__ :						Optional[Any]    							=					[[float("inf") for i in range(V)] for j in range(V)]

     for i in range(V):
          UpperCamelCase__ :						int    							=					0.0

     for i in range(E):
          print("\nEdge ", i + 1)
          UpperCamelCase__ :						Union[str, Any]    							=					int(input("Enter source:").strip())
          UpperCamelCase__ :						Tuple    							=					int(input("Enter destination:").strip())
          UpperCamelCase__ :						Any    							=					float(input("Enter weight:").strip())
          UpperCamelCase__ :						List[Any]    							=					weight

     UpperCamelCase__ :						Optional[Any]    							=					int(input("\nEnter shortest path source:").strip())
     dijkstra(graph, V, gsrc)






