






import random
import unittest

import numpy as np

from transformers.testing_utils import require_torch, require_vision
from transformers.utils import is_torch_available, is_vision_available

from ...test_image_processing_common import ImageProcessingSavingTestMixin, prepare_image_inputs


if is_torch_available():
	import torch

if is_vision_available():
	import PIL

	from transformers import FlavaImageProcessor
	from transformers.image_utils import PILImageResampling
	from transformers.models.flava.image_processing_flava import (
	    FLAVA_CODEBOOK_MEAN,
	    FLAVA_CODEBOOK_STD,
	    FLAVA_IMAGE_MEAN,
	    FLAVA_IMAGE_STD,
	)
else:
	UpperCAmelCase_							=      None


class        lowercase__       (     unittest.TestCase		):

	def __init__(							self						,      __UpperCAmelCase						,      __UpperCAmelCase=7						,      __UpperCAmelCase=3						,      __UpperCAmelCase=30						,      __UpperCAmelCase=400						,      __UpperCAmelCase=True						,      __UpperCAmelCase=None						,      __UpperCAmelCase=True						,      __UpperCAmelCase=None						,      __UpperCAmelCase=None						,      __UpperCAmelCase=True						,      __UpperCAmelCase=1 / 255						,      __UpperCAmelCase=True						,      __UpperCAmelCase=FLAVA_IMAGE_MEAN						,      __UpperCAmelCase=FLAVA_IMAGE_STD						,      __UpperCAmelCase=14						,      __UpperCAmelCase=75						,      __UpperCAmelCase=None						,      __UpperCAmelCase=16						,      __UpperCAmelCase=0.3						,      __UpperCAmelCase=None						,      __UpperCAmelCase=True						,      __UpperCAmelCase=None						,      __UpperCAmelCase=None						,      __UpperCAmelCase=True						,      __UpperCAmelCase=None						,      __UpperCAmelCase=True						,      __UpperCAmelCase=True						,      __UpperCAmelCase=FLAVA_CODEBOOK_MEAN						,      __UpperCAmelCase=FLAVA_CODEBOOK_STD						,      )->   List[str]:







							'''simple docstring'''





							lowerCAmelCase__	 =  size if size is not None else {"height": 224, "width": 224}
							lowerCAmelCase__	 =  crop_size if crop_size is not None else {"height": 224, "width": 224}
							lowerCAmelCase__	 =  codebook_size if codebook_size is not None else {"height": 112, "width": 112}
							lowerCAmelCase__	 =  codebook_crop_size if codebook_crop_size is not None else {"height": 112, "width": 112}

							lowerCAmelCase__	 =  parent
							lowerCAmelCase__	 =  batch_size
							lowerCAmelCase__	 =  num_channels
							lowerCAmelCase__	 =  do_resize
							lowerCAmelCase__	 =  do_rescale
							lowerCAmelCase__	 =  rescale_factor
							lowerCAmelCase__	 =  min_resolution
							lowerCAmelCase__	 =  max_resolution
							lowerCAmelCase__	 =  size
							lowerCAmelCase__	 =  resample if resample is not None else PILImageResampling.BICUBIC
							lowerCAmelCase__	 =  do_normalize
							lowerCAmelCase__	 =  image_mean
							lowerCAmelCase__	 =  image_std
							lowerCAmelCase__	 =  do_center_crop
							lowerCAmelCase__	 =  crop_size

							lowerCAmelCase__	 =  input_size_patches
							lowerCAmelCase__	 =  total_mask_patches
							lowerCAmelCase__	 =  mask_group_max_patches
							lowerCAmelCase__	 =  mask_group_min_patches
							lowerCAmelCase__	 =  mask_group_min_aspect_ratio
							lowerCAmelCase__	 =  mask_group_max_aspect_ratio

							lowerCAmelCase__	 =  codebook_do_resize
							lowerCAmelCase__	 =  codebook_size
							lowerCAmelCase__	 =  codebook_resample if codebook_resample is not None else PILImageResampling.LANCZOS
							lowerCAmelCase__	 =  codebook_do_center_crop
							lowerCAmelCase__	 =  codebook_crop_size
							lowerCAmelCase__	 =  codebook_do_map_pixels
							lowerCAmelCase__	 =  codebook_do_normalize
							lowerCAmelCase__	 =  codebook_image_mean
							lowerCAmelCase__	 =  codebook_image_std

	def 						UpperCAmelCase					(							self    )->   int:







							'''simple docstring'''





							return {
							    "image_mean": self.image_mean,
							    "image_std": self.image_std,
							    "do_normalize": self.do_normalize,
							    "do_resize": self.do_resize,
							    "size": self.size,
							    "resample": self.resample,
							    "do_rescale": self.do_rescale,
							    "rescale_factor": self.rescale_factor,
							    "do_center_crop": self.do_center_crop,
							    "crop_size": self.crop_size,
							    "input_size_patches": self.input_size_patches,
							    "total_mask_patches": self.total_mask_patches,
							    "mask_group_max_patches": self.mask_group_max_patches,
							    "mask_group_min_patches": self.mask_group_min_patches,
							    "mask_group_min_aspect_ratio": self.mask_group_min_aspect_ratio,
							    "mask_group_max_aspect_ratio": self.mask_group_min_aspect_ratio,
							    "codebook_do_resize": self.codebook_do_resize,
							    "codebook_size": self.codebook_size,
							    "codebook_resample": self.codebook_resample,
							    "codebook_do_center_crop": self.codebook_do_center_crop,
							    "codebook_crop_size": self.codebook_crop_size,
							    "codebook_do_map_pixels": self.codebook_do_map_pixels,
							    "codebook_do_normalize": self.codebook_do_normalize,
							    "codebook_image_mean": self.codebook_image_mean,
							    "codebook_image_std": self.codebook_image_std,
							}

	def 						UpperCAmelCase					(							self    )->   int:







							'''simple docstring'''





							return (self.size["height"], self.size["width"])

	def 						UpperCAmelCase					(							self    )->   str:







							'''simple docstring'''





							return (
							    (self.input_size_patches, self.input_size_patches)
							    if not isinstance(self.input_size_patches						,      __UpperCAmelCase    )
							    else self.input_size_patches
							)


	def 						UpperCAmelCase					(							self    )->   Optional[int]:







							'''simple docstring'''





							return (self.codebook_size["height"], self.codebook_size["width"])





@require_torch
@require_vision
class        lowercase__       (     A__,   unittest.TestCase		):
	a_             =FlavaImageProcessor if is_vision_available() else None
	a_             =None

	def 						UpperCAmelCase					(							self    )->   int:







							'''simple docstring'''





							lowerCAmelCase__	 =  FlavaImageProcessingTester(self    )

	@property
	def 						UpperCAmelCase					(							self    )->   Optional[Any]:







							'''simple docstring'''





							return self.image_processor_tester.prepare_image_processor_dict()

	def 						UpperCAmelCase					(							self    )->   Optional[int]:







							'''simple docstring'''





							lowerCAmelCase__	 =  self.image_processing_class(**self.image_processor_dict    )
							self.assertTrue(hasattr(__UpperCAmelCase						,      "image_mean"    )    )
							self.assertTrue(hasattr(__UpperCAmelCase						,      "image_std"    )    )
							self.assertTrue(hasattr(__UpperCAmelCase						,      "do_normalize"    )    )
							self.assertTrue(hasattr(__UpperCAmelCase						,      "do_resize"    )    )
							self.assertTrue(hasattr(__UpperCAmelCase						,      "resample"    )    )
							self.assertTrue(hasattr(__UpperCAmelCase						,      "crop_size"    )    )
							self.assertTrue(hasattr(__UpperCAmelCase						,      "do_center_crop"    )    )
							self.assertTrue(hasattr(__UpperCAmelCase						,      "do_rescale"    )    )
							self.assertTrue(hasattr(__UpperCAmelCase						,      "rescale_factor"    )    )
							self.assertTrue(hasattr(__UpperCAmelCase						,      "masking_generator"    )    )
							self.assertTrue(hasattr(__UpperCAmelCase						,      "codebook_do_resize"    )    )
							self.assertTrue(hasattr(__UpperCAmelCase						,      "codebook_size"    )    )
							self.assertTrue(hasattr(__UpperCAmelCase						,      "codebook_resample"    )    )
							self.assertTrue(hasattr(__UpperCAmelCase						,      "codebook_do_center_crop"    )    )
							self.assertTrue(hasattr(__UpperCAmelCase						,      "codebook_crop_size"    )    )
							self.assertTrue(hasattr(__UpperCAmelCase						,      "codebook_do_map_pixels"    )    )
							self.assertTrue(hasattr(__UpperCAmelCase						,      "codebook_do_normalize"    )    )
							self.assertTrue(hasattr(__UpperCAmelCase						,      "codebook_image_mean"    )    )
							self.assertTrue(hasattr(__UpperCAmelCase						,      "codebook_image_std"    )    )

	def 						UpperCAmelCase					(							self    )->   Optional[Any]:







							'''simple docstring'''





							lowerCAmelCase__	 =  self.image_processing_class.from_dict(self.image_processor_dict    )
							self.assertEqual(image_processor.size						,      {"height": 224, "width": 224}    )
							self.assertEqual(image_processor.crop_size						,      {"height": 224, "width": 224}    )
							self.assertEqual(image_processor.codebook_size						,      {"height": 112, "width": 112}    )
							self.assertEqual(image_processor.codebook_crop_size						,      {"height": 112, "width": 112}    )

							lowerCAmelCase__	 =  self.image_processing_class.from_dict(
							    self.image_processor_dict						,      size=42						,      crop_size=84						,      codebook_size=33						,      codebook_crop_size=66    )
							self.assertEqual(image_processor.size						,      {"height": 42, "width": 42}    )
							self.assertEqual(image_processor.crop_size						,      {"height": 84, "width": 84}    )
							self.assertEqual(image_processor.codebook_size						,      {"height": 33, "width": 33}    )
							self.assertEqual(image_processor.codebook_crop_size						,      {"height": 66, "width": 66}    )

	def 						UpperCAmelCase					(							self    )->   Optional[Any]:







							'''simple docstring'''





							pass

	def 						UpperCAmelCase					(							self    )->   List[str]:







							'''simple docstring'''





							lowerCAmelCase__	 =  self.image_processing_class(**self.image_processor_dict    )
							# create random PIL images
							lowerCAmelCase__	 =  prepare_image_inputs(self.image_processor_tester						,      equal_resolution=__UpperCAmelCase    )
							for image in image_inputs:
													self.assertIsInstance(__UpperCAmelCase						,      PIL.Image.Image    )

							# Test not batched input
							lowerCAmelCase__	 =  image_processing(image_inputs[0]						,      return_tensors="pt"    )

							# Test no bool masked pos
							self.assertFalse("bool_masked_pos" in encoded_images    )

							lowerCAmelCase__ ,							lowerCAmelCase__	 =  self.image_processor_tester.get_expected_image_size()

							self.assertEqual(
							    encoded_images.pixel_values.shape						,      (1, self.image_processor_tester.num_channels, expected_height, expected_width)						,      )

							# Test batched
							lowerCAmelCase__	 =  image_processing(__UpperCAmelCase						,      return_tensors="pt"    )
							lowerCAmelCase__ ,							lowerCAmelCase__	 =  self.image_processor_tester.get_expected_image_size()

							# Test no bool masked pos
							self.assertFalse("bool_masked_pos" in encoded_images    )

							self.assertEqual(
							    encoded_images.pixel_values.shape						,      (
							        self.image_processor_tester.batch_size,
							        self.image_processor_tester.num_channels,
							        expected_height,
							        expected_width,
							    )						,      )

	def 						UpperCAmelCase					(							self						,      __UpperCAmelCase						,      __UpperCAmelCase    )->   Dict:







							'''simple docstring'''





							lowerCAmelCase__	 =  self.image_processing_class(**self.image_processor_dict    )
							# create random tensors
							lowerCAmelCase__	 =  prepare_image_inputs(self.image_processor_tester						,      equal_resolution=__UpperCAmelCase						,      **__UpperCAmelCase    )
							for image in image_inputs:
													self.assertIsInstance(__UpperCAmelCase						,      __UpperCAmelCase    )

							# Test not batched input
							lowerCAmelCase__	 =  image_processing(image_inputs[0]						,      return_tensors="pt"    )

							lowerCAmelCase__ ,							lowerCAmelCase__	 =  self.image_processor_tester.get_expected_image_size()
							self.assertEqual(
							    encoded_images.pixel_values.shape						,      (1, self.image_processor_tester.num_channels, expected_height, expected_width)						,      )

							lowerCAmelCase__	 =  image_processing(__UpperCAmelCase						,      return_image_mask=__UpperCAmelCase						,      return_tensors="pt"    )

							lowerCAmelCase__ ,							lowerCAmelCase__	 =  self.image_processor_tester.get_expected_image_size()
							self.assertEqual(
							    encoded_images.pixel_values.shape						,      (
							        self.image_processor_tester.batch_size,
							        self.image_processor_tester.num_channels,
							        expected_height,
							        expected_width,
							    )						,      )

							lowerCAmelCase__ ,							lowerCAmelCase__	 =  self.image_processor_tester.get_expected_mask_size()
							self.assertEqual(
							    encoded_images.bool_masked_pos.shape						,      (
							        self.image_processor_tester.batch_size,
							        expected_height,
							        expected_width,
							    )						,      )

							# Test batched
							lowerCAmelCase__	 =  image_processing(__UpperCAmelCase						,      return_tensors="pt"    ).pixel_values

							lowerCAmelCase__ ,							lowerCAmelCase__	 =  self.image_processor_tester.get_expected_image_size()
							self.assertEqual(
							    encoded_images.shape						,      (
							        self.image_processor_tester.batch_size,
							        self.image_processor_tester.num_channels,
							        expected_height,
							        expected_width,
							    )						,      )

							# Test masking
							lowerCAmelCase__	 =  image_processing(__UpperCAmelCase						,      return_image_mask=__UpperCAmelCase						,      return_tensors="pt"    )

							lowerCAmelCase__ ,							lowerCAmelCase__	 =  self.image_processor_tester.get_expected_image_size()
							self.assertEqual(
							    encoded_images.pixel_values.shape						,      (
							        self.image_processor_tester.batch_size,
							        self.image_processor_tester.num_channels,
							        expected_height,
							        expected_width,
							    )						,      )

							lowerCAmelCase__ ,							lowerCAmelCase__	 =  self.image_processor_tester.get_expected_mask_size()
							self.assertEqual(
							    encoded_images.bool_masked_pos.shape						,      (
							        self.image_processor_tester.batch_size,
							        expected_height,
							        expected_width,
							    )						,      )

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







							'''simple docstring'''





							self._test_call_framework(np.ndarray						,      prepare_kwargs={"numpify": True}    )

	def 						UpperCAmelCase					(							self    )->   Tuple:







							'''simple docstring'''





							self._test_call_framework(torch.Tensor						,      prepare_kwargs={"torchify": True}    )

	def 						UpperCAmelCase					(							self    )->   List[str]:







							'''simple docstring'''





							random.seed(1234    )
							lowerCAmelCase__	 =  self.image_processing_class(**self.image_processor_dict    )
							lowerCAmelCase__	 =  prepare_image_inputs(self.image_processor_tester						,      equal_resolution=__UpperCAmelCase						,      torchify=__UpperCAmelCase    )

							# Test not batched input
							lowerCAmelCase__	 =  image_processing(image_inputs[0]						,      return_image_mask=__UpperCAmelCase						,      return_tensors="pt"    )
							self.assertEqual(encoded_images.bool_masked_pos.sum().item()						,      75    )


	def 						UpperCAmelCase					(							self    )->   List[str]:







							'''simple docstring'''





							lowerCAmelCase__	 =  self.image_processing_class(**self.image_processor_dict    )
							# create random PIL images
							lowerCAmelCase__	 =  prepare_image_inputs(self.image_processor_tester						,      equal_resolution=__UpperCAmelCase    )
							for image in image_inputs:
													self.assertIsInstance(__UpperCAmelCase						,      PIL.Image.Image    )

							# Test not batched input
							lowerCAmelCase__	 =  image_processing(image_inputs[0]						,      return_codebook_pixels=__UpperCAmelCase						,      return_tensors="pt"    )
							lowerCAmelCase__ ,							lowerCAmelCase__	 =  self.image_processor_tester.get_expected_codebook_image_size()
							self.assertEqual(
							    encoded_images.codebook_pixel_values.shape						,      (1, self.image_processor_tester.num_channels, expected_height, expected_width)						,      )

							# Test batched
							lowerCAmelCase__	 =  image_processing(__UpperCAmelCase						,      return_codebook_pixels=__UpperCAmelCase						,      return_tensors="pt"    )
							lowerCAmelCase__ ,							lowerCAmelCase__	 =  self.image_processor_tester.get_expected_codebook_image_size()
							self.assertEqual(
							    encoded_images.codebook_pixel_values.shape						,      (
							        self.image_processor_tester.batch_size,
							        self.image_processor_tester.num_channels,
							        expected_height,
							        expected_width,
							    )						,      )














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


a_							=      collections.namedtuple(
    '''CheckpointState''', ['''model_dict''', '''optimizer_dict''', '''scheduler_dict''', '''offset''', '''epoch''', '''encoder_params''']
)


def     _a					(       UpperCamelCase_   :			str				)	->				CheckpointState:
						"""simple docstring"""







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


class        lowercase__       :

	def __init__(							self						,      __UpperCAmelCase    )->   Dict:







							'''simple docstring'''





							lowerCAmelCase__	 =  src_file

	def 						UpperCAmelCase					(							self    )->   List[str]:







							'''simple docstring'''





							raise NotImplementedError


	@staticmethod
	def 						UpperCAmelCase					(							__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        lowercase__       (     _UpperCAmelCase		):

	def 						UpperCAmelCase					(							self    )->   str:







							'''simple docstring'''





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





class        lowercase__       (     _UpperCAmelCase		):

	def 						UpperCAmelCase					(							self    )->   int:







							'''simple docstring'''





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





class        lowercase__       (     _UpperCAmelCase		):

	def 						UpperCAmelCase					(							self    )->   List[Any]:







							'''simple docstring'''





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





def     _a					(       UpperCamelCase_   :			str      ,				UpperCamelCase_   :			Path      ,				UpperCamelCase_   :			Path				)	->				Any:
						"""simple docstring"""







						lowerCAmelCase__	 =  Path(UpperCamelCase_				)
						dest_dir.mkdir(exist_ok=UpperCamelCase_				)

						lowerCAmelCase__	 =  DPRState.from_type(UpperCamelCase_      ,				src_file=UpperCamelCase_				)
						lowerCAmelCase__	 =  dpr_state.load_dpr_model()
						model.save_pretrained(UpperCamelCase_				)
						model.from_pretrained(UpperCamelCase_				)  # sanity check


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

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






