code: 
  
  
  
  
  
  
  import copy
  import inspect
  
  from transformers.testing_utils import require_torch, torch_device
  from transformers.utils.backbone_utils import BackboneType
  
  
  
  @require_torch
  class 			a      :
  		__lowercase													=		()
  		__lowercase													=		True
  
  
  
  
  
  
  		def       lowerCAmelCase_  (  self							)->			Any:
  						'''simple docstring'''
  
  
  						A__     :							List[str]   				=self.config_class
  
  						# test default config
  						A__     :							Tuple   				=config_class()
  						self.assertIsNotNone(__UpperCamelCase							)
  						A__     :							Any   				=['''stem'''] + [F'stage{idx}' for idx in range(1	,     len(config.depths							) + 1							)]
  						self.assertEqual(config.stage_names	,     __UpperCamelCase							)
  						self.assertTrue(set(config.out_features							).issubset(set(config.stage_names							)							)							)
  
  						# Test out_features and out_indices are correctly set
  						# out_features and out_indices both None
  						A__     :							List[str]   				=config_class(out_features=__UpperCamelCase	,     out_indices=__UpperCamelCase							)
  						self.assertEqual(config.out_features	,     [config.stage_names[-1]]							)
  						self.assertEqual(config.out_indices	,     [len(config.stage_names							) - 1]							)
  
  						# out_features and out_indices both set
  						A__     :							List[Any]   				=config_class(out_features=['''stem''', '''stage1''']	,     out_indices=[0, 1]							)
  						self.assertEqual(config.out_features	,     ['''stem''', '''stage1''']							)
  						self.assertEqual(config.out_indices	,     [0, 1]							)
  
  						# Only out_features set
  						A__     :							int   				=config_class(out_features=['''stage1''', '''stage3''']							)
  						self.assertEqual(config.out_features	,     ['''stage1''', '''stage3''']							)
  						self.assertEqual(config.out_indices	,     [1, 3]							)
  
  						# Only out_indices set
  						A__     :							str   				=config_class(out_indices=[0, 2]							)
  						self.assertEqual(config.out_features	,     [config.stage_names[0], config.stage_names[2]]							)
  						self.assertEqual(config.out_indices	,     [0, 2]							)
  
  						# Error raised when out_indices do not correspond to out_features
  						with self.assertRaises(__UpperCamelCase							):
  										A__     :							List[Any]   				=config_class(out_features=['''stage1''', '''stage2''']	,     out_indices=[0, 2]							)
  
  
  
  
  
  
  		def       lowerCAmelCase_  (  self							)->			int:
  						'''simple docstring'''
  
  
  						A__     :							int   				=self.model_tester.prepare_config_and_inputs_for_common()
  
  						for model_class in self.all_model_classes:
  										A__     :							Dict   				=model_class(__UpperCamelCase							)
  										A__     :							Dict   				=inspect.signature(model.forward							)
  										# signature.parameters is an OrderedDict => so arg_names order is deterministic
  										A__     :							Tuple   				=[*signature.parameters.keys()]
  										A__     :							Optional[Any]   				=['''pixel_values''']
  										self.assertListEqual(arg_names[:1]	,     __UpperCamelCase							)
  
  
  
  
  
  
  		def       lowerCAmelCase_  (  self							)->			Tuple:
  						'''simple docstring'''
  
  
  						A__     :							List[Any]   				=self.model_tester.prepare_config_and_inputs_for_common()
  
  						for model_class in self.all_model_classes:
  										A__     :							Dict   				=model_class(__UpperCamelCase							)
  										self.assertEqual(len(model.channels							)	,     len(config.out_features							)							)
  										A__     :							int   				=model.num_features
  										A__     :							List[str]   				=[config.stage_names.index(__UpperCamelCase							) for feat in config.out_features]
  										A__     :							List[Any]   				=[num_features[idx] for idx in out_indices]
  										self.assertListEqual(model.channels	,     __UpperCamelCase							)
  
  										A__     :							List[str]   				=copy.deepcopy(__UpperCamelCase							)
  										A__     :							Union[str, Any]   				=None
  										A__     :							Tuple   				=model_class(__UpperCamelCase							)
  										self.assertEqual(len(model.channels							)	,     1							)
  										self.assertListEqual(model.channels	,     [num_features[-1]]							)
  
  										A__     :							List[str]   				=copy.deepcopy(__UpperCamelCase							)
  										A__     :							Optional[Any]   				=None
  										A__     :							Union[str, Any]   				=model_class(__UpperCamelCase							)
  										self.assertEqual(len(model.channels							)	,     1							)
  										self.assertListEqual(model.channels	,     [num_features[-1]]							)
  
  
  
  
  
  
  		def       lowerCAmelCase_  (  self							)->			str:
  						'''simple docstring'''
  
  
  						A__     :							Optional[int]   				=self.model_tester.prepare_config_and_inputs_for_common()
  
  						for model_class in self.all_model_classes:
  										A__     :							str   				=model_class(__UpperCamelCase							)
  										model.to(__UpperCamelCase							)
  										model.eval()
  										A__     :							Tuple   				=model(**__UpperCamelCase							)
  
  										self.assertEqual(len(result.feature_maps							)	,     len(config.out_features							)							)
  										self.assertEqual(len(model.channels							)	,     len(config.out_features							)							)
  										self.assertEqual(len(result.feature_maps							)	,     len(config.out_indices							)							)
  										self.assertEqual(len(model.channels							)	,     len(config.out_indices							)							)
  
  										# Check output of last stage is taken if out_features=None, out_indices=None
  										A__     :							List[str]   				=copy.deepcopy(__UpperCamelCase							)
  										A__     :							int   				=None
  										A__     :							Dict   				=model_class(__UpperCamelCase							)
  										model.to(__UpperCamelCase							)
  										model.eval()
  										A__     :							Union[str, Any]   				=model(**__UpperCamelCase							)
  
  										self.assertEqual(len(result.feature_maps							)	,     1							)
  										self.assertEqual(len(model.channels							)	,     1							)
  
  										A__     :							Dict   				=copy.deepcopy(__UpperCamelCase							)
  										A__     :							Dict   				=None
  										A__     :							str   				=model_class(__UpperCamelCase							)
  										model.to(__UpperCamelCase							)
  										model.eval()
  										A__     :							Union[str, Any]   				=model(**__UpperCamelCase							)
  
  										self.assertEqual(len(result.feature_maps							)	,     1							)
  										self.assertEqual(len(model.channels							)	,     1							)
  
  										# Check backbone can be initialized with fresh weights
  										A__     :							str   				=copy.deepcopy(__UpperCamelCase							)
  										A__     :							Union[str, Any]   				=False
  										A__     :							int   				=model_class(__UpperCamelCase							)
  										model.to(__UpperCamelCase							)
  										model.eval()
  										A__     :							Optional[Any]   				=model(**__UpperCamelCase							)
  
  
  
  
  
  
  		def       lowerCAmelCase_  (  self							)->			List[Any]:
  						'''simple docstring'''
  
  
  						A__     :							Dict   				=self.model_tester.prepare_config_and_inputs_for_common()
  
  						for backbone_class in self.all_model_classes:
  										A__     :							List[Any]   				=backbone_class(__UpperCamelCase							)
  
  										self.assertTrue(hasattr(__UpperCamelCase	,     '''backbone_type'''							)							)
  										self.assertTrue(hasattr(__UpperCamelCase	,     '''stage_names'''							)							)
  										self.assertTrue(hasattr(__UpperCamelCase	,     '''num_features'''							)							)
  										self.assertTrue(hasattr(__UpperCamelCase	,     '''out_indices'''							)							)
  										self.assertTrue(hasattr(__UpperCamelCase	,     '''out_features'''							)							)
  										self.assertTrue(hasattr(__UpperCamelCase	,     '''out_feature_channels'''							)							)
  										self.assertTrue(hasattr(__UpperCamelCase	,     '''channels'''							)							)
  
  										self.assertIsInstance(backbone.backbone_type	,     __UpperCamelCase							)
  										# Verify num_features has been initialized in the backbone init
  										self.assertIsNotNone(backbone.num_features							)
  										self.assertTrue(len(backbone.channels							) == len(backbone.out_indices							)							)
  										self.assertTrue(len(backbone.stage_names							) == len(backbone.num_features							)							)
  										self.assertTrue(len(backbone.channels							) <= len(backbone.num_features							)							)
  										self.assertTrue(len(backbone.out_feature_channels							) == len(backbone.stage_names							)							)
  
  
  
  
  
  
  		def       lowerCAmelCase_  (  self							)->			str:
  						'''simple docstring'''
  
  
  						A__     :							str   				=self.model_tester.prepare_config_and_inputs_for_common()
  						A__     :							Any   				=inputs_dict['''pixel_values'''].shape[0]
  
  						for backbone_class in self.all_model_classes:
  										A__     :							Optional[int]   				=backbone_class(__UpperCamelCase							)
  										backbone.to(__UpperCamelCase							)
  										backbone.eval()
  
  										A__     :							List[Any]   				=backbone(**__UpperCamelCase							)
  
  										# Test default outputs and verify feature maps
  										self.assertIsInstance(outputs.feature_maps	,     __UpperCamelCase							)
  										self.assertTrue(len(outputs.feature_maps							) == len(backbone.channels							)							)
  										for feature_map, n_channels in zip(outputs.feature_maps	,     backbone.channels							):
  														self.assertTrue(feature_map.shape[:2]	,     (batch_size, n_channels)							)
  										self.assertIsNone(outputs.hidden_states							)
  										self.assertIsNone(outputs.attentions							)
  
  										# Test output_hidden_states=True
  										A__     :							List[Any]   				=backbone(**__UpperCamelCase	,     output_hidden_states=__UpperCamelCase							)
  										self.assertIsNotNone(outputs.hidden_states							)
  										self.assertTrue(len(outputs.hidden_states							)	,     len(backbone.stage_names							)							)
  										for hidden_state, n_channels in zip(outputs.hidden_states	,     backbone.channels							):
  														self.assertTrue(hidden_state.shape[:2]	,     (batch_size, n_channels)							)
  
  										# Test output_attentions=True
  										if self.has_attentions:
  														A__     :							int   				=backbone(**__UpperCamelCase	,     output_attentions=__UpperCamelCase							)
  														self.assertIsNotNone(outputs.attentions							)
  
  
  
  
style_context: 
  
  
  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       :       Optional[Any]				   =			collections.namedtuple(
      "CheckpointState", ["model_dict", "optimizer_dict", "scheduler_dict", "offset", "epoch", "encoder_params"]
  )
  
  
  
  def   SCREAMING_SNAKE_CASE__						(							snake_case_	)			->							CheckpointState:
  				print(f'Reading saved model from {model_file}'	)
  				A__     :							Optional[int]   				=torch.load(snake_case_,	map_location=lambda snake_case_,	snake_case_	: default_restore_location(snake_case_,	'''cpu'''	)	)
  				return CheckpointState(**snake_case_	)
  
  
  
  class 			a      :
  
  
  
  
  
  
  		def __init__(  self	,     __UpperCamelCase							)->			Union[str, Any]:
  						'''simple docstring'''
  
  
  						A__     :							Dict   				=src_file
  
  
  
  
  
  
  		def       lowerCAmelCase_  (  self							)->			Optional[Any]:
  						'''simple docstring'''
  
  
  						raise NotImplementedError
  
  
  
  
  
  
  		@staticmethod
  		def       lowerCAmelCase_  (  __UpperCamelCase	,     *__UpperCamelCase	,     **__UpperCamelCase							)->			"DPRState":
  						'''simple docstring'''
  
  
  						if comp_type.startswith('''c'''							):
  										return DPRContextEncoderState(*__UpperCamelCase	,     **__UpperCamelCase							)
  						if comp_type.startswith('''q'''							):
  										return DPRQuestionEncoderState(*__UpperCamelCase	,     **__UpperCamelCase							)
  						if comp_type.startswith('''r'''							):
  										return DPRReaderState(*__UpperCamelCase	,     **__UpperCamelCase							)
  						else:
  										raise ValueError('''Component type must be either \'ctx_encoder\', \'question_encoder\' or \'reader\'.'''							)
  
  
  
  
  
  
  
  class 			a      (      UpperCamelCase_   ):
  
  
  
  
  
  
  		def       lowerCAmelCase_  (  self							)->			Optional[Any]:
  						'''simple docstring'''
  
  
  						A__     :							Optional[int]   				=DPRContextEncoder(DPRConfig(**BertConfig.get_config_dict('''bert-base-uncased'''							)[0]							)							)
  						print(F'Loading DPR biencoder from {self.src_file}'							)
  						A__     :							Optional[int]   				=load_states_from_checkpoint(self.src_file							)
  						A__				,  A__     :							List[str]   				=model.ctx_encoder, '''ctx_model.'''
  						# Fix changes from https://github.com/huggingface/transformers/commit/614fef1691edb806de976756d4948ecbcd0c0ca3
  						A__     :							Optional[int]   				={'''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(__UpperCamelCase							):
  														A__     :							int   				=key[len(__UpperCamelCase							) :]
  														if not key.startswith('''encode_proj.'''							):
  																		A__     :							Any   				='''bert_model.''' + key
  														A__     :							List[str]   				=value
  						encoder.load_state_dict(__UpperCamelCase							)
  						return model
  
  
  
  
  
  
  
  class 			a      (      UpperCamelCase_   ):
  
  
  
  
  
  
  		def       lowerCAmelCase_  (  self							)->			Dict:
  						'''simple docstring'''
  
  
  						A__     :							Union[str, Any]   				=DPRQuestionEncoder(DPRConfig(**BertConfig.get_config_dict('''bert-base-uncased'''							)[0]							)							)
  						print(F'Loading DPR biencoder from {self.src_file}'							)
  						A__     :							List[str]   				=load_states_from_checkpoint(self.src_file							)
  						A__				,  A__     :							Optional[int]   				=model.question_encoder, '''question_model.'''
  						# Fix changes from https://github.com/huggingface/transformers/commit/614fef1691edb806de976756d4948ecbcd0c0ca3
  						A__     :							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(__UpperCamelCase							):
  														A__     :							List[Any]   				=key[len(__UpperCamelCase							) :]
  														if not key.startswith('''encode_proj.'''							):
  																		A__     :							Tuple   				='''bert_model.''' + key
  														A__     :							str   				=value
  						encoder.load_state_dict(__UpperCamelCase							)
  						return model
  
  
  
  
  
  
  
  class 			a      (      UpperCamelCase_   ):
  
  
  
  
  
  
  		def       lowerCAmelCase_  (  self							)->			int:
  						'''simple docstring'''
  
  
  						A__     :							Optional[int]   				=DPRReader(DPRConfig(**BertConfig.get_config_dict('''bert-base-uncased'''							)[0]							)							)
  						print(F'Loading DPR reader from {self.src_file}'							)
  						A__     :							Dict   				=load_states_from_checkpoint(self.src_file							)
  						# Fix changes from https://github.com/huggingface/transformers/commit/614fef1691edb806de976756d4948ecbcd0c0ca3
  						A__     :							Optional[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'''							):
  														A__     :							List[Any]   				='''encoder.bert_model.''' + key[len('''encoder.'''							) :]
  										A__     :							Optional[int]   				=value
  						model.span_predictor.load_state_dict(__UpperCamelCase							)
  						return model
  
  
  
  
  
  
  
  def   SCREAMING_SNAKE_CASE__						(							snake_case_,	snake_case_,	snake_case_	)			->							Optional[int]:
  				A__     :							Optional[int]   				=Path(snake_case_	)
  				dest_dir.mkdir(exist_ok=snake_case_	)
  
  				A__     :							Tuple   				=DPRState.from_type(snake_case_,	src_file=snake_case_	)
  				A__     :							str   				=dpr_state.load_dpr_model()
  				model.save_pretrained(snake_case_	)
  				model.from_pretrained(snake_case_	)  # sanity check
  
  
  if __name__ == "__main__":
  						__lowerCamelCase       :       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.")
  						__lowerCamelCase       :       Tuple				   =			parser.parse_args()
  
  						__lowerCamelCase       :       Optional[Any]				   =			Path(args.src)
  						__lowerCamelCase       :       Optional[int]				   =			F"converted-{src_file.name}" if args.dest is None else args.dest
  						__lowerCamelCase       :       Optional[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)
  
  
  
  