


import argparse
import os
import re


# All paths are set with the intent you should run this script from the root of the repo with the command
# python utils/check_dummies.py
__UpperCamelCase							:							Optional[Any]    							=					"src/transformers"

# Matches is_xxx_available()
__UpperCamelCase							:							str    							=					re.compile(r"is\_([a-z_]*)_available()")
# Matches from xxx import bla
__UpperCamelCase							:							Union[str, Any]    							=					re.compile(r"\s+from\s+\S*\s+import\s+([^\(\s].*)\n")
__UpperCamelCase							:							Dict    							=					re.compile(r"^\s+if\s+not\s+\(?is\_[a-z_]*\_available\(\)")


__UpperCamelCase							:							Any    							=					"\n{0} = None\n"

__UpperCamelCase							:							Tuple    							=					"\nclass {0}(metaclass=DummyObject):\n    _backends = {1}\n\n    def __init__(self, *args, **kwargs):\n        requires_backends(self, {1})\n"


__UpperCamelCase							:							Optional[Any]    							=					"\ndef {0}(*args, **kwargs):\n    requires_backends({0}, {1})\n"

def       _a	(					SCREAMING_SNAKE_CASE						:					str  ):







 """simple docstring"""



 if _re_test_backend.search(SCREAMING_SNAKE_CASE  ) is None:
  return None
 UpperCamelCase__       :     List[str] 							= [b[0] for b in _re_backend.findall(SCREAMING_SNAKE_CASE  )]
 backends.sort()
 return "_and_".join(SCREAMING_SNAKE_CASE  )

def       _a	(					):







 """simple docstring"""



 with open(os.path.join(SCREAMING_SNAKE_CASE     ,			'''__init__.py'''  )     ,			'''r'''     ,			encoding='''utf-8'''     ,			newline='''\n'''  ) as f:
  UpperCamelCase__       :     Union[str, Any] 							= f.readlines()

 # Get to the point we do the actual imports for type checking
 UpperCamelCase__       :     int 							= 0
 while not lines[line_index].startswith('''if TYPE_CHECKING'''  ):
  line_index += 1

 UpperCamelCase__       :     Optional[Any] 							= {}
 # Go through the end of the file
 while line_index < len(SCREAMING_SNAKE_CASE  ):
  # If the line is an if is_backend_available, we grab all objects associated.
  UpperCamelCase__       :     Any 							= find_backend(lines[line_index]  )
  if backend is not None:
   while not lines[line_index].startswith('''    else:'''  ):
    line_index += 1
   line_index += 1

   UpperCamelCase__       :     List[str] 							= []
   # Until we unindent, add backend objects to the list
   while len(lines[line_index]  ) <= 1 or lines[line_index].startswith(''' ''' * 8  ):
    UpperCamelCase__       :     str 							= lines[line_index]
    UpperCamelCase__       :     Union[str, Any] 							= _re_single_line_import.search(SCREAMING_SNAKE_CASE  )
    if single_line_import_search is not None:
     objects.extend(single_line_import_search.groups()[0].split(''', '''  )  )
    elif line.startswith(''' ''' * 12  ):
     objects.append(line[12:-2]  )
    line_index += 1

   UpperCamelCase__       :     Optional[int] 							= objects
  else:
   line_index += 1

 return backend_specific_objects

def       _a	(					SCREAMING_SNAKE_CASE						:					str     ,			SCREAMING_SNAKE_CASE						:					List[str]  ):







 """simple docstring"""



 if name.isupper():
  return DUMMY_CONSTANT.format(SCREAMING_SNAKE_CASE  )
 elif name.islower():
  return DUMMY_FUNCTION.format(SCREAMING_SNAKE_CASE     ,			SCREAMING_SNAKE_CASE  )
 else:
  return DUMMY_CLASS.format(SCREAMING_SNAKE_CASE     ,			SCREAMING_SNAKE_CASE  )

def       _a	(					SCREAMING_SNAKE_CASE						:					Dict=None  ):







 """simple docstring"""



 if backend_specific_objects is None:
  UpperCamelCase__       :     Any 							= read_init()
 # For special correspondence backend to module name as used in the function requires_modulename
 UpperCamelCase__       :     Optional[Any] 							= {}

 for backend, objects in backend_specific_objects.items():
  UpperCamelCase__       :     int 							= '''[''' + ''', '''.join(F"\"{b}\"" for b in backend.split('''_and_'''  )  ) + ''']'''
  UpperCamelCase__       :     Any 							= '''# This file is autogenerated by the command `make fix-copies`, do not edit.\n'''
  dummy_file += "from ..utils import DummyObject, requires_backends\n\n"
  dummy_file += "\n".join([create_dummy_object(SCREAMING_SNAKE_CASE     ,			SCREAMING_SNAKE_CASE  ) for o in objects]  )
  UpperCamelCase__       :     List[str] 							= dummy_file

 return dummy_files

def       _a	(					SCREAMING_SNAKE_CASE						:					Union[str, Any]=False  ):







 """simple docstring"""



 UpperCamelCase__       :     Dict 							= create_dummy_files()
 # For special correspondence backend to shortcut as used in utils/dummy_xxx_objects.py
 UpperCamelCase__       :     Tuple 							= {'''torch''': '''pt'''}

 # Locate actual dummy modules and read their content.
 UpperCamelCase__       :     Optional[Any] 							= os.path.join(SCREAMING_SNAKE_CASE     ,			'''utils'''  )
 UpperCamelCase__       :     int 							= {
     backend: os.path.join(SCREAMING_SNAKE_CASE     ,			F"dummy_{short_names.get(SCREAMING_SNAKE_CASE     ,			SCREAMING_SNAKE_CASE  )}_objects.py"  )
     for backend in dummy_files.keys()
 }

 UpperCamelCase__       :     Optional[Any] 							= {}
 for backend, file_path in dummy_file_paths.items():
  if os.path.isfile(SCREAMING_SNAKE_CASE  ):
   with open(SCREAMING_SNAKE_CASE     ,			'''r'''     ,			encoding='''utf-8'''     ,			newline='''\n'''  ) as f:
    UpperCamelCase__       :     List[Any] 							= f.read()
  else:
   UpperCamelCase__       :     List[Any] 							= ''''''

 for backend in dummy_files.keys():
  if dummy_files[backend] != actual_dummies[backend]:
   if overwrite:
    print(
        F"Updating transformers.utils.dummy_{short_names.get(SCREAMING_SNAKE_CASE     ,			SCREAMING_SNAKE_CASE  )}_objects.py as the main "
        '''__init__ has new objects.'''  )
    with open(dummy_file_paths[backend]     ,			'''w'''     ,			encoding='''utf-8'''     ,			newline='''\n'''  ) as f:
     f.write(dummy_files[backend]  )
   else:
    raise ValueError(
        '''The main __init__ has objects that are not present in '''
        F"transformers.utils.dummy_{short_names.get(SCREAMING_SNAKE_CASE     ,			SCREAMING_SNAKE_CASE  )}_objects.py. Run `make fix-copies` "
        '''to fix this.'''  )


if __name__ == "__main__":
       __UpperCamelCase							:							Optional[int]    							=					argparse.ArgumentParser()
       parser.add_argument("--fix_and_overwrite", action="store_true", help="Whether to fix inconsistencies.")
       __UpperCamelCase							:							str    							=					parser.parse_args()

       check_dummies(args.fix_and_overwrite)






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


__UpperCamelCase							:							Tuple    							=					collections.namedtuple(
    "CheckpointState", ["model_dict", "optimizer_dict", "scheduler_dict", "offset", "epoch", "encoder_params"]
)

def       _a	(					SCREAMING_SNAKE_CASE						:					str  ):







 """simple docstring"""



 print(F"Reading saved model from {model_file}"  )
 UpperCamelCase__       :     int 							= torch.load(SCREAMING_SNAKE_CASE     ,			map_location=lambda SCREAMING_SNAKE_CASE     ,			SCREAMING_SNAKE_CASE  : default_restore_location(SCREAMING_SNAKE_CASE     ,			'''cpu'''  )  )
 return CheckpointState(**SCREAMING_SNAKE_CASE  )

class    __magic_name__						:


     def __init__(   self				:  str				,						lowerCamelCase__				:  Path      )	->   Tuple:





      '''simple docstring'''






      UpperCamelCase__       :     List[str] 							= src_file


     def   UpperCAmelCase__						(   self				:  Dict      )	->   int:





      '''simple docstring'''






      raise NotImplementedError


     @staticmethod
     def   UpperCAmelCase__						(   lowerCamelCase__				:  str				,						*lowerCamelCase__				:  Union[str, Any]				,						**lowerCamelCase__				:  Optional[int]      )	->   "DPRState":





      '''simple docstring'''






      if comp_type.startswith('''c'''      ):
       return DPRContextEncoderState(*lowerCamelCase__				,						**lowerCamelCase__      )
      if comp_type.startswith('''q'''      ):
       return DPRQuestionEncoderState(*lowerCamelCase__				,						**lowerCamelCase__      )
      if comp_type.startswith('''r'''      ):
       return DPRReaderState(*lowerCamelCase__				,						**lowerCamelCase__      )
      else:
       raise ValueError('''Component type must be either \'ctx_encoder\', \'question_encoder\' or \'reader\'.'''      )



class    __magic_name__						(  __lowerCAmelCase):


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





      '''simple docstring'''






      UpperCamelCase__       :     Union[str, Any] 							= DPRContextEncoder(DPRConfig(**BertConfig.get_config_dict('''bert-base-uncased'''      )[0]      )      )
      print(F"Loading DPR biencoder from {self.src_file}"      )
      UpperCamelCase__       :     int 							= load_states_from_checkpoint(self.src_file      )
      UpperCamelCase__							,					UpperCamelCase__       :     Any 							= model.ctx_encoder, '''ctx_model.'''
      # Fix changes from https://github.com/huggingface/transformers/commit/614fef1691edb806de976756d4948ecbcd0c0ca3
      UpperCamelCase__       :     Union[str, Any] 							= {'''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(lowerCamelCase__      ):
        UpperCamelCase__       :     int 							= key[len(lowerCamelCase__      ) :]
        if not key.startswith('''encode_proj.'''      ):
         UpperCamelCase__       :     Union[str, Any] 							= '''bert_model.''' + key
        UpperCamelCase__       :     Dict 							= value
      encoder.load_state_dict(lowerCamelCase__      )
      return model



class    __magic_name__						(  __lowerCAmelCase):


     def   UpperCAmelCase__						(   self				:  Any      )	->   Dict:





      '''simple docstring'''






      UpperCamelCase__       :     List[Any] 							= DPRQuestionEncoder(DPRConfig(**BertConfig.get_config_dict('''bert-base-uncased'''      )[0]      )      )
      print(F"Loading DPR biencoder from {self.src_file}"      )
      UpperCamelCase__       :     Optional[int] 							= load_states_from_checkpoint(self.src_file      )
      UpperCamelCase__							,					UpperCamelCase__       :     int 							= model.question_encoder, '''question_model.'''
      # Fix changes from https://github.com/huggingface/transformers/commit/614fef1691edb806de976756d4948ecbcd0c0ca3
      UpperCamelCase__       :     Optional[int] 							= {'''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(lowerCamelCase__      ):
        UpperCamelCase__       :     int 							= key[len(lowerCamelCase__      ) :]
        if not key.startswith('''encode_proj.'''      ):
         UpperCamelCase__       :     Union[str, Any] 							= '''bert_model.''' + key
        UpperCamelCase__       :     str 							= value
      encoder.load_state_dict(lowerCamelCase__      )
      return model



class    __magic_name__						(  __lowerCAmelCase):


     def   UpperCAmelCase__						(   self				:  int      )	->   Any:





      '''simple docstring'''






      UpperCamelCase__       :     int 							= DPRReader(DPRConfig(**BertConfig.get_config_dict('''bert-base-uncased'''      )[0]      )      )
      print(F"Loading DPR reader from {self.src_file}"      )
      UpperCamelCase__       :     List[str] 							= load_states_from_checkpoint(self.src_file      )
      # Fix changes from https://github.com/huggingface/transformers/commit/614fef1691edb806de976756d4948ecbcd0c0ca3
      UpperCamelCase__       :     Tuple 							= {
          '''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'''      ):
        UpperCamelCase__       :     List[str] 							= '''encoder.bert_model.''' + key[len('''encoder.'''      ) :]
       UpperCamelCase__       :     List[Any] 							= value
      model.span_predictor.load_state_dict(lowerCamelCase__      )
      return model



def       _a	(					SCREAMING_SNAKE_CASE						:					str     ,			SCREAMING_SNAKE_CASE						:					Path     ,			SCREAMING_SNAKE_CASE						:					Path  ):







 """simple docstring"""



 UpperCamelCase__       :     Any 							= Path(SCREAMING_SNAKE_CASE  )
 dest_dir.mkdir(exist_ok=SCREAMING_SNAKE_CASE  )

 UpperCamelCase__       :     str 							= DPRState.from_type(SCREAMING_SNAKE_CASE     ,			src_file=SCREAMING_SNAKE_CASE  )
 UpperCamelCase__       :     str 							= dpr_state.load_dpr_model()
 model.save_pretrained(SCREAMING_SNAKE_CASE  )
 model.from_pretrained(SCREAMING_SNAKE_CASE  )  # sanity check


if __name__ == "__main__":
       __UpperCamelCase							:							Optional[Any]    							=					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.")
       __UpperCamelCase							:							Union[str, Any]    							=					parser.parse_args()

       __UpperCamelCase							:							Optional[int]    							=					Path(args.src)
       __UpperCamelCase							:							Dict    							=					f"converted-{src_file.name}" if args.dest is None else args.dest
       __UpperCamelCase							:							str    							=					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)


