code: 
  
  import argparse
  import collections
  from pathlib import Path
  
  import torch
  from torch.serialization import default_restore_location
  
  from transformers import BertConfig, DPRConfig, DPRContextEncoder, DPRQuestionEncoder, DPRReader
  
  
  lowerCamelCase     =      collections.namedtuple(
      '''CheckpointState''', ['''model_dict''', '''optimizer_dict''', '''scheduler_dict''', '''offset''', '''epoch''', '''encoder_params''']
  )
  
  
  
  
  def     UpperCAmelCase__       (    _A   :				str							):
  
  
  
  
  
    '''simple docstring'''
  
  
  
  
  
  
  
    print(F"""Reading saved model from {model_file}"""							)
    a__						=torch.load(_A			,   map_location=lambda _A			,   _A							: default_restore_location(_A			,   '''cpu'''							)							)
    return CheckpointState(**_A							)
  
  
  
  
  
  
  
  class 		__magic_name__    :
  
     '''simple docstring'''
  
  
  
  
     def __init__(       self,					lowercase_ )							->					int:
  
  
  
  
  
  
       """simple docstring"""
  
  
  
  
  
  
       a__						=src_file
  
  
  
     def 				_UpperCAmelCase				(       self )							->					int:
  
  
  
  
  
  
       """simple docstring"""
  
  
  
  
  
  
       raise NotImplementedError
  
  
  
     @staticmethod
     def 				_UpperCAmelCase				(       lowercase_,					*lowercase_,					**lowercase_ )							->					"DPRState":
  
  
  
  
  
  
       """simple docstring"""
  
  
  
  
  
  
       if comp_type.startswith('''c''' ):
         return DPRContextEncoderState(*lowercase_,					**lowercase_ )
       if comp_type.startswith('''q''' ):
         return DPRQuestionEncoderState(*lowercase_,					**lowercase_ )
       if comp_type.startswith('''r''' ):
         return DPRReaderState(*lowercase_,					**lowercase_ )
       else:
         raise ValueError('''Component type must be either \'ctx_encoder\', \'question_encoder\' or \'reader\'.''' )
  
  
  
  
  
  
  
  class 		__magic_name__    (							lowerCamelCase__							):
  
     '''simple docstring'''
  
  
  
  
     def 				_UpperCAmelCase				(       self )							->					Any:
  
  
  
  
  
  
       """simple docstring"""
  
  
  
  
  
  
       a__						=DPRContextEncoder(DPRConfig(**BertConfig.get_config_dict('''bert-base-uncased''' )[0] ) )
       print(F"""Loading DPR biencoder from {self.src_file}""" )
       a__						=load_states_from_checkpoint(self.src_file )
       a__,   a__						=model.ctx_encoder, '''ctx_model.'''
       # Fix changes from https://github.com/huggingface/transformers/commit/614fef1691edb806de976756d4948ecbcd0c0ca3
       a__						={'''bert_model.embeddings.position_ids''': model.ctx_encoder.bert_model.embeddings.position_ids}
       for key, value in saved_state.model_dict.items():
         if key.startswith(lowercase_ ):
           a__						=key[len(lowercase_ ) :]
           if not key.startswith('''encode_proj.''' ):
             a__						='''bert_model.''' + key
           a__						=value
       encoder.load_state_dict(lowercase_ )
       return model
  
  
  
  
  
  
  
  class 		__magic_name__    (							lowerCamelCase__							):
  
     '''simple docstring'''
  
  
  
  
     def 				_UpperCAmelCase				(       self )							->					Tuple:
  
  
  
  
  
  
       """simple docstring"""
  
  
  
  
  
  
       a__						=DPRQuestionEncoder(DPRConfig(**BertConfig.get_config_dict('''bert-base-uncased''' )[0] ) )
       print(F"""Loading DPR biencoder from {self.src_file}""" )
       a__						=load_states_from_checkpoint(self.src_file )
       a__,   a__						=model.question_encoder, '''question_model.'''
       # Fix changes from https://github.com/huggingface/transformers/commit/614fef1691edb806de976756d4948ecbcd0c0ca3
       a__						={'''bert_model.embeddings.position_ids''': model.question_encoder.bert_model.embeddings.position_ids}
       for key, value in saved_state.model_dict.items():
         if key.startswith(lowercase_ ):
           a__						=key[len(lowercase_ ) :]
           if not key.startswith('''encode_proj.''' ):
             a__						='''bert_model.''' + key
           a__						=value
       encoder.load_state_dict(lowercase_ )
       return model
  
  
  
  
  
  
  
  class 		__magic_name__    (							lowerCamelCase__							):
  
     '''simple docstring'''
  
  
  
  
     def 				_UpperCAmelCase				(       self )							->					Union[str, Any]:
  
  
  
  
  
  
       """simple docstring"""
  
  
  
  
  
  
       a__						=DPRReader(DPRConfig(**BertConfig.get_config_dict('''bert-base-uncased''' )[0] ) )
       print(F"""Loading DPR reader from {self.src_file}""" )
       a__						=load_states_from_checkpoint(self.src_file )
       # Fix changes from https://github.com/huggingface/transformers/commit/614fef1691edb806de976756d4948ecbcd0c0ca3
       a__						={
           '''encoder.bert_model.embeddings.position_ids''': model.span_predictor.encoder.bert_model.embeddings.position_ids
       }
       for key, value in saved_state.model_dict.items():
         if key.startswith('''encoder.''' ) and not key.startswith('''encoder.encode_proj''' ):
           a__						='''encoder.bert_model.''' + key[len('''encoder.''' ) :]
         a__						=value
       model.span_predictor.load_state_dict(lowercase_ )
       return model
  
  
  
  
  
  def     UpperCAmelCase__       (    _A   :				str			,   _A   :				Path			,   _A   :				Path							):
  
  
  
  
  
    '''simple docstring'''
  
  
  
  
  
  
  
    a__						=Path(_A							)
    dest_dir.mkdir(exist_ok=_A							)
  
    a__						=DPRState.from_type(_A			,   src_file=_A							)
    a__						=dpr_state.load_dpr_model()
    model.save_pretrained(_A							)
    model.from_pretrained(_A							)  # sanity check
  
  
  if __name__ == "__main__":
         lowerCamelCase     =      argparse.ArgumentParser()
         # Required parameters
         parser.add_argument(
             '''--type''', type=str, help='''Type of the component to convert: \'ctx_encoder\', \'question_encoder\' or \'reader\'.'''
         )
         parser.add_argument(
             '''--src''',
             type=str,
             help=(
                 '''Path to the dpr checkpoint file. They can be downloaded from the official DPR repo'''
                 ''' https://github.com/facebookresearch/DPR. Note that in the official repo, both encoders are stored in the'''
                 ''' \'retriever\' checkpoints.'''
             ),
         )
         parser.add_argument('''--dest''', type=str, default=None, help='''Path to the output PyTorch model directory.''')
         lowerCamelCase     =      parser.parse_args()
  
         lowerCamelCase     =      Path(args.src)
         lowerCamelCase     =      f"""converted-{src_file.name}""" if args.dest is None else args.dest
         lowerCamelCase     =      Path(dest_dir)
         assert src_file.exists()
         assert (
             args.type is not None
         ), "Please specify the component type of the DPR model to convert: 'ctx_encoder', 'question_encoder' or 'reader'."
         convert(args.type, src_file, dest_dir)
  
style_context: 
  
  import heapq
  
  
  
  
  def     UpperCAmelCase__       (    _A   :				Optional[Any]			,   _A   :				str			,   _A   :				Optional[Any]							):
  
  
  
  
  
    '''simple docstring'''
  
  
  
  
  
  
  
    a__						=[(0, start)]  # cost from start node,end node
    a__						=set()
    while heap:
      ((a__),   (a__))						=heapq.heappop(_A							)
      if u in visited:
        continue
      visited.add(_A							)
      if u == end:
        return cost
      for v, c in graph[u]:
        if v in visited:
          continue
        a__						=cost + c
        heapq.heappush(_A			,   (next_item, v)							)
    return -1
  
  
  lowerCamelCase     =      {
      '''A''': [['''B''', 2], ['''C''', 5]],
      '''B''': [['''A''', 2], ['''D''', 3], ['''E''', 1], ['''F''', 1]],
      '''C''': [['''A''', 5], ['''F''', 3]],
      '''D''': [['''B''', 3]],
      '''E''': [['''B''', 4], ['''F''', 3]],
      '''F''': [['''C''', 3], ['''E''', 3]],
  }
  lowerCamelCase     =      {
      '''B''': [['''C''', 1]],
      '''C''': [['''D''', 1]],
      '''D''': [['''F''', 1]],
      '''E''': [['''B''', 1], ['''F''', 3]],
      '''F''': [],
  }
  lowerCamelCase     =      {
      '''B''': [['''C''', 1]],
      '''C''': [['''D''', 1]],
      '''D''': [['''F''', 1]],
      '''E''': [['''B''', 1], ['''G''', 2]],
      '''F''': [],
      '''G''': [['''F''', 1]],
  }
  
  lowerCamelCase     =      dijkstra(G, '''E''', '''C''')
  print(short_distance)  # E -- 3 --> F -- 3 --> C == 6
  
  lowerCamelCase     =      dijkstra(Ga, '''E''', '''F''')
  print(short_distance)  # E -- 3 --> F == 3
  
  lowerCamelCase     =      dijkstra(Ga, '''E''', '''F''')
  print(short_distance)  # E -- 2 --> G -- 1 --> F == 3
  
  if __name__ == "__main__":
         import doctest
  
         doctest.testmod()
  