


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







def       snake_case_       (  SCREAMING_SNAKE_CASE__					):







				"""simple docstring"""





				print(f"""Reading saved model from {model_file}"""					)
				_SCREAMING_SNAKE_CASE			:	Optional[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    lowercase__  :





							'''simple docstring'''


							def __init__(   self   , __snake_case  ):
											_SCREAMING_SNAKE_CASE			:	List[str]							=				src_file






							def UpperCAmelCase_     (   self  ):
											raise NotImplementedError






							@staticmethod
							def UpperCAmelCase_     (   __snake_case   , *__snake_case   , **__snake_case  ):
											if comp_type.startswith("""c"""  ):
															return DPRContextEncoderState(*__snake_case   , **__snake_case  )
											if comp_type.startswith("""q"""  ):
															return DPRQuestionEncoderState(*__snake_case   , **__snake_case  )
											if comp_type.startswith("""r"""  ):
															return DPRReaderState(*__snake_case   , **__snake_case  )
											else:
															raise ValueError("""Component type must be either 'ctx_encoder', 'question_encoder' or 'reader'."""  )





class    lowercase__  (						_snake_case ):





							'''simple docstring'''


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





class    lowercase__  (						_snake_case ):





							'''simple docstring'''


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





class    lowercase__  (						_snake_case ):





							'''simple docstring'''


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







def       snake_case_       (  SCREAMING_SNAKE_CASE__							,				SCREAMING_SNAKE_CASE__							,				SCREAMING_SNAKE_CASE__					):







				"""simple docstring"""





				_SCREAMING_SNAKE_CASE			:	List[Any]							=				Path(SCREAMING_SNAKE_CASE__					)
				dest_dir.mkdir(exist_ok=SCREAMING_SNAKE_CASE__					)

				_SCREAMING_SNAKE_CASE			:	List[Any]							=				DPRState.from_type(SCREAMING_SNAKE_CASE__							,				src_file=SCREAMING_SNAKE_CASE__					)
				_SCREAMING_SNAKE_CASE			:	List[str]							=				dpr_state.load_dpr_model()
				model.save_pretrained(SCREAMING_SNAKE_CASE__					)
				model.from_pretrained(SCREAMING_SNAKE_CASE__					)  # sanity check


if __name__ == "__main__":
					UpperCAmelCase_		:       List[str]							       =		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_		:       List[str]							       =		parser.parse_args()

					UpperCAmelCase_		:       Optional[int]							       =		Path(args.src)
					UpperCAmelCase_		:       int							       =		F"converted-{src_file.name}" if args.dest is None else args.dest
					UpperCAmelCase_		:       Union[str, Any]							       =		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'''


import sys







def       snake_case_       (  SCREAMING_SNAKE_CASE__							,				SCREAMING_SNAKE_CASE__					):







				"""simple docstring"""





				if not numbers:
								return 0
				if target == 0 and target in numbers:
								return 0
				if not isinstance(SCREAMING_SNAKE_CASE__							,				(list, tuple)					) or not all(
				    isinstance(SCREAMING_SNAKE_CASE__							,				SCREAMING_SNAKE_CASE__					) for number in numbers					):
								raise ValueError("""numbers must be an iterable of integers"""					)

				_SCREAMING_SNAKE_CASE			:	str							=				0
				_SCREAMING_SNAKE_CASE			:	int							=				sys.maxsize

				while right < len(SCREAMING_SNAKE_CASE__					):
								curr_sum += numbers[right]
								while curr_sum >= target and left <= right:
												_SCREAMING_SNAKE_CASE			:	Any							=				min(SCREAMING_SNAKE_CASE__							,				right - left + 1					)
												curr_sum -= numbers[left]
												left += 1
								right += 1

				return 0 if min_len == sys.maxsize else min_len




