'''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        __magic_name__		:







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







			def       SCREAMING_SNAKE_CASE_	(      self							:    Optional[Any]       ):
										lowercase_   :     Dict									=			ids_tensor([self.batch_size, self.seq_length]							,     self.vocab_size       )

										lowercase_   :     Any									=			None
										if self.use_input_mask:
																	lowercase_   :     Optional[int]									=			random_attention_mask([self.batch_size, self.seq_length]       )

										lowercase_   :     Union[str, Any]									=			None
										if self.use_token_type_ids:
																	lowercase_   :     List[Any]									=			ids_tensor([self.batch_size, self.seq_length]							,     self.type_vocab_size       )

										lowercase_   :     Tuple									=			None
										lowercase_   :     Tuple									=			None
										lowercase_   :     str									=			None
										if self.use_labels:
																	lowercase_   :     str									=			ids_tensor([self.batch_size]							,     self.type_sequence_label_size       )
																	lowercase_   :     Tuple									=			ids_tensor([self.batch_size, self.seq_length]							,     self.num_labels       )
																	lowercase_   :     Optional[Any]									=			ids_tensor([self.batch_size]							,     self.num_choices       )

										lowercase_   :     str									=			self.get_config()

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







			def       SCREAMING_SNAKE_CASE_	(      self							:    List[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       SCREAMING_SNAKE_CASE_	(      self							:    Union[str, Any]							,     lowercase_							:    Optional[Any]							,     lowercase_							:    str							,     lowercase_							:    Dict							,     lowercase_							:    int							,     lowercase_							:    Tuple							,     lowercase_							:    Tuple							,     lowercase_							:    str       ):
										lowercase_   :     str									=			DPRContextEncoder(config=lowercase_       )
										model.to(lowercase_       )
										model.eval()
										lowercase_   :     str									=			model(lowercase_							,     attention_mask=lowercase_							,     token_type_ids=lowercase_       )
										lowercase_   :     str									=			model(lowercase_							,     token_type_ids=lowercase_       )
										lowercase_   :     Optional[Any]									=			model(lowercase_       )
										self.parent.assertEqual(result.pooler_output.shape							,     (self.batch_size, self.projection_dim or self.hidden_size)       )







			def       SCREAMING_SNAKE_CASE_	(      self							:    List[Any]							,     lowercase_							:    List[str]							,     lowercase_							:    Dict							,     lowercase_							:    Any							,     lowercase_							:    Optional[Any]							,     lowercase_							:    Optional[int]							,     lowercase_							:    Optional[int]							,     lowercase_							:    Optional[Any]       ):
										lowercase_   :     Any									=			DPRQuestionEncoder(config=lowercase_       )
										model.to(lowercase_       )
										model.eval()
										lowercase_   :     Tuple									=			model(lowercase_							,     attention_mask=lowercase_							,     token_type_ids=lowercase_       )
										lowercase_   :     Any									=			model(lowercase_							,     token_type_ids=lowercase_       )
										lowercase_   :     str									=			model(lowercase_       )
										self.parent.assertEqual(result.pooler_output.shape							,     (self.batch_size, self.projection_dim or self.hidden_size)       )







			def       SCREAMING_SNAKE_CASE_	(      self							:    List[Any]							,     lowercase_							:    Optional[Any]							,     lowercase_							:    Any							,     lowercase_							:    int							,     lowercase_							:    Optional[Any]							,     lowercase_							:    Any							,     lowercase_							:    int							,     lowercase_							:    Any       ):
										lowercase_   :     List[str]									=			DPRReader(config=lowercase_       )
										model.to(lowercase_       )
										model.eval()
										lowercase_   :     List[Any]									=			model(
										    lowercase_							,     attention_mask=lowercase_							,     )

										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							:    Tuple       ):
										lowercase_   :     str									=			self.prepare_config_and_inputs()
										(
										    (
										    lowercase_
										)		,		(
										    lowercase_
										)		,		(
										    lowercase_
										)		,		(
										    lowercase_
										)		,		(
										    lowercase_
										)		,		(
										    lowercase_
										)		,		(
										    lowercase_
										)		,		
										)   :     Optional[int]									=			config_and_inputs
										lowercase_   :     Any									=			{"""input_ids""": input_ids}
										return config, inputs_dict





@require_torch
class        __magic_name__		(       _UpperCAmelCase,	_UpperCAmelCase,	unittest.TestCase):
			UpperCamelCase__							=			(
			    (
			        DPRContextEncoder,
			        DPRQuestionEncoder,
			        DPRReader,
			    )
			    if is_torch_available()
			    else ()
			)
			UpperCamelCase__							=			{'''feature-extraction''': DPRQuestionEncoder} if is_torch_available() else {}

			UpperCamelCase__							=			False
			UpperCamelCase__							=			False  # why?
			UpperCamelCase__							=			False
			UpperCamelCase__							=			False







			def       SCREAMING_SNAKE_CASE_	(      self							:    List[str]       ):
										lowercase_   :     Any									=			DPRModelTester(self       )
										lowercase_   :     Optional[Any]									=			ConfigTester(self							,     config_class=lowercase_							,     hidden_size=37       )







			def       SCREAMING_SNAKE_CASE_	(      self							:    str       ):
										self.config_tester.run_common_tests()







			def       SCREAMING_SNAKE_CASE_	(      self							:    List[Any]       ):
										lowercase_   :     str									=			self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_context_encoder(*lowercase_       )







			def       SCREAMING_SNAKE_CASE_	(      self							:    Dict       ):
										lowercase_   :     List[Any]									=			self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_question_encoder(*lowercase_       )







			def       SCREAMING_SNAKE_CASE_	(      self							:    int       ):
										lowercase_   :     Tuple									=			self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_reader(*lowercase_       )







			def       SCREAMING_SNAKE_CASE_	(      self							:    Union[str, Any]       ):
										lowercase_   :     int									=			self.model_tester.prepare_config_and_inputs()[0]

										lowercase_   :     str									=			DPRQuestionEncoder(config=lowercase_       )
										model.to(lowercase_       )
										model.eval()

										with tempfile.TemporaryDirectory() as tmp_dirname:
																	model.save_pretrained(lowercase_       )
																	lowercase_   :     Union[str, Any]									=			DPRQuestionEncoder.from_pretrained(lowercase_							,     projection_dim=512       )

										self.assertIsNotNone(lowercase_       )







			@slow
			def       SCREAMING_SNAKE_CASE_	(      self							:    List[Any]       ):
										for model_name in DPR_CONTEXT_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																	lowercase_   :     Optional[Any]									=			DPRContextEncoder.from_pretrained(lowercase_       )
																	self.assertIsNotNone(lowercase_       )

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

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

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





@require_torch
class        __magic_name__		(       unittest.TestCase):







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

										lowercase_   :     Dict									=			torch.tensor(
										    [[101, 7592, 1010, 2003, 2026, 3899, 10140, 1029, 102]]							,     dtype=torch.long							,     device=lowercase_       )  # [CLS] hello, is my dog cute? [SEP]
										lowercase_   :     Dict									=			model(lowercase_       )[0]  # embedding shape = (1, 768)
										# compare the actual values for a slice.
										lowercase_   :     str									=			torch.tensor(
										    [
										        [
										            0.03_23_62_53,
										            0.12_75_33_35,
										            0.16_81_85_09,
										            0.00_27_97_86,
										            0.3_89_69_33,
										            0.24_26_49_45,
										            0.2_17_89_71,
										            -0.02_33_52_27,
										            -0.08_48_19_59,
										            -0.14_32_41_17,
										        ]
										    ]							,     dtype=torch.float							,     device=lowercase_							,     )
										self.assertTrue(torch.allclose(output[:, :10]							,     lowercase_							,     atol=1E-4       )       )







			@slow
			def       SCREAMING_SNAKE_CASE_	(      self							:    int       ):
										lowercase_   :     Dict									=			DPRReaderTokenizer.from_pretrained("""facebook/dpr-reader-single-nq-base"""       )
										lowercase_   :     Any									=			DPRReader.from_pretrained("""facebook/dpr-reader-single-nq-base"""       )
										model.to(lowercase_       )

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

										lowercase_   :     List[Any]									=			model(**lowercase_       )

										# compare the actual values for a slice.
										lowercase_   :     Optional[int]									=			torch.tensor(
										    [[-10.30_05, -10.77_65, -11.48_72, -11.68_41, -11.93_12, -10.30_02, -9.85_44, -11.73_78, -12.08_21, -10.29_75]]							,     dtype=torch.float							,     device=lowercase_							,     )

										lowercase_   :     Any									=			torch.tensor(
										    [[-11.06_84, -11.70_41, -11.53_97, -10.34_65, -10.87_91, -6.84_43, -11.99_59, -11.03_64, -10.00_96, -6.84_05]]							,     dtype=torch.float							,     device=lowercase_							,     )
										self.assertTrue(torch.allclose(outputs.start_logits[:, :10]							,     lowercase_							,     atol=1E-4       )       )
										self.assertTrue(torch.allclose(outputs.end_logits[:, :10]							,     lowercase_							,     atol=1E-4       )       )






'''simple docstring'''




from __future__ import annotations





class        __magic_name__		:







			def __init__(      self							:    List[str]							,     lowercase_							:    int       ):
										lowercase_   :     List[str]									=			vertices
										lowercase_   :     Optional[Any]									=			[[0] * vertices for _ in range(lowercase_       )]







			def       SCREAMING_SNAKE_CASE_	(      self							:    int							,     lowercase_							:    list[int]       ):
										print("""Vertex \t Distance from Source"""       )
										for vertex in range(self.vertices       ):
																	print(lowercase_							,     """\t\t"""							,     distances_from_source[vertex]       )







			def       SCREAMING_SNAKE_CASE_	(      self							:    str							,     lowercase_							:    list[int]							,     lowercase_							:    list[bool]       ):
										lowercase_   :     Dict									=			1E7
										lowercase_   :     int									=			0

										# Search not nearest vertex not in the shortest path tree
										for vertex in range(self.vertices       ):
																	if distances_from_source[vertex] < minimum and visited[vertex] is False:
																								lowercase_   :     Tuple									=			distances_from_source[vertex]
																								lowercase_   :     str									=			vertex
										return min_index







			def       SCREAMING_SNAKE_CASE_	(      self							:    Union[str, Any]							,     lowercase_							:    int       ):
										lowercase_   :     Optional[Any]									=			[int(1E7       )] * self.vertices  # distances from the source
										lowercase_   :     List[Any]									=			0
										lowercase_   :     Any									=			[False] * self.vertices

										for _ in range(self.vertices       ):
																	lowercase_   :     Tuple									=			self.minimum_distance(lowercase_							,     lowercase_       )
																	lowercase_   :     Union[str, Any]									=			True

																	# Update dist value of the adjacent vertices
																	# of the picked vertex only if the current
																	# distance is greater than new distance and
																	# the vertex in not in the shortest path tree
																	for v in range(self.vertices       ):
																								if (
																								    self.graph[u][v] > 0
																								    and visited[v] is False
																								    and distances[v] > distances[u] + self.graph[u][v]
																								):
																															lowercase_   :     Any									=			distances[u] + self.graph[u][v]

										self.print_solution(lowercase_       )


if __name__ == "__main__":
						_lowercase		:					Any													=		Graph(9)
						_lowercase		:					Any													=		[
						    [0, 4, 0, 0, 0, 0, 0, 8, 0],
						    [4, 0, 8, 0, 0, 0, 0, 11, 0],
						    [0, 8, 0, 7, 0, 4, 0, 0, 2],
						    [0, 0, 7, 0, 9, 14, 0, 0, 0],
						    [0, 0, 0, 9, 0, 10, 0, 0, 0],
						    [0, 0, 4, 14, 10, 0, 2, 0, 0],
						    [0, 0, 0, 0, 0, 2, 0, 1, 6],
						    [8, 11, 0, 0, 0, 0, 1, 0, 7],
						    [0, 0, 2, 0, 0, 0, 6, 7, 0],
						]
						graph.dijkstra(0)





