





"""simple docstring"""




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_	 =collections.namedtuple(
    """CheckpointState""", ["""model_dict""", """optimizer_dict""", """scheduler_dict""", """offset""", """epoch""", """encoder_params"""]
)





def 			a_							(				_lowercase  ):
						print(F"""Reading saved model from {model_file}"""  )
						_UpperCamelCase     :   Optional[Any]      =       torch.load(_lowercase			,		map_location=lambda _lowercase			,		_lowercase  : default_restore_location(_lowercase			,		'''cpu'''  )  )
						return CheckpointState(**_lowercase  )




class 					_a		:






				def __init__(					self						:       Dict,					lowerCAmelCase__						:       Path				)      -> List[str]:




										'''simple docstring'''



										_UpperCamelCase     :   Tuple      =       src_file






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




										'''simple docstring'''



										raise NotImplementedError






				@staticmethod
				def 							snake_case      (					lowerCAmelCase__						:       str,					*lowerCAmelCase__						:       Optional[Any],					**lowerCAmelCase__						:       List[str]				)      -> "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 					_a		( _lowerCAmelCase							):






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




										'''simple docstring'''



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




class 					_a		( _lowerCAmelCase							):






				def 							snake_case      (					self						:       Optional[int]				)      -> Tuple:




										'''simple docstring'''



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




class 					_a		( _lowerCAmelCase							):






				def 							snake_case      (					self						:       Optional[Any]				)      -> List[Any]:




										'''simple docstring'''



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





def 			a_							(				_lowercase			,		_lowercase			,		_lowercase  ):
						_UpperCamelCase     :   Dict      =       Path(_lowercase  )
						dest_dir.mkdir(exist_ok=_lowercase  )

						_UpperCamelCase     :   int      =       DPRState.from_type(_lowercase			,		src_file=_lowercase  )
						_UpperCamelCase     :   List[Any]      =       dpr_state.load_dpr_model()
						model.save_pretrained(_lowercase  )
						model.from_pretrained(_lowercase  )  # sanity check


if __name__ == "__main__":
						UpperCamelCase_	 =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_	 =parser.parse_args()

						UpperCamelCase_	 =Path(args.src)
						UpperCamelCase_	 =F"converted-{src_file.name}" if args.dest is None else args.dest
						UpperCamelCase_	 =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)




"""simple docstring"""


from typing import TYPE_CHECKING

from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tokenizers_available, is_torch_available


__A       			=      {
    """configuration_realm""": ["""REALM_PRETRAINED_CONFIG_ARCHIVE_MAP""", """RealmConfig"""],
    """tokenization_realm""": ["""RealmTokenizer"""],
}

try:
 if not is_tokenizers_available():
  raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
 pass
else:
 __A       			=      ["""RealmTokenizerFast"""]

try:
 if not is_torch_available():
  raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
 pass
else:
 __A       			=      [
     """REALM_PRETRAINED_MODEL_ARCHIVE_LIST""",
     """RealmEmbedder""",
     """RealmForOpenQA""",
     """RealmKnowledgeAugEncoder""",
     """RealmPreTrainedModel""",
     """RealmReader""",
     """RealmScorer""",
     """load_tf_weights_in_realm""",
 ]
 __A       			=      ["""RealmRetriever"""]


if TYPE_CHECKING:
 from .configuration_realm import REALM_PRETRAINED_CONFIG_ARCHIVE_MAP, RealmConfig
 from .tokenization_realm import RealmTokenizer

 try:
  if not is_tokenizers_available():
   raise OptionalDependencyNotAvailable()
 except OptionalDependencyNotAvailable:
  pass
 else:
  from .tokenization_realm import RealmTokenizerFast

 try:
  if not is_torch_available():
   raise OptionalDependencyNotAvailable()
 except OptionalDependencyNotAvailable:
  pass
 else:
  from .modeling_realm import (
      REALM_PRETRAINED_MODEL_ARCHIVE_LIST,
      RealmEmbedder,
      RealmForOpenQA,
      RealmKnowledgeAugEncoder,
      RealmPreTrainedModel,
      RealmReader,
      RealmScorer,
      load_tf_weights_in_realm,
  )
  from .retrieval_realm import RealmRetriever


else:
 import sys

 __A       			=      _LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)
