


'''simple docstring'''

from __future__ import annotations

import unittest

from transformers import MPNetConfig, is_tf_available
from transformers.testing_utils import require_tf, slow

from ...test_configuration_common import ConfigTester
from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_tf_available():
 import tensorflow as tf

 from transformers.models.mpnet.modeling_tf_mpnet import (
     TFMPNetForMaskedLM,
     TFMPNetForMultipleChoice,
     TFMPNetForQuestionAnswering,
     TFMPNetForSequenceClassification,
     TFMPNetForTokenClassification,
     TFMPNetModel,
 )
class 						_UpperCAmelCase		:





      '''simple docstring'''






      def __init__(self					:					Tuple     ,		UpperCamelCase					:					str     ,		UpperCamelCase					:					Tuple=13     ,		UpperCamelCase					:					Any=7     ,		UpperCamelCase					:					Optional[Any]=True     ,		UpperCamelCase					:					str=True     ,		UpperCamelCase					:					List[Any]=False     ,		UpperCamelCase					:					Optional[int]=True     ,		UpperCamelCase					:					Any=99     ,		UpperCamelCase					:					Any=64     ,		UpperCamelCase					:					Tuple=2     ,		UpperCamelCase					:					int=4     ,		UpperCamelCase					:					Union[str, Any]=64     ,		UpperCamelCase					:					str="gelu"     ,		UpperCamelCase					:					str=0.1     ,		UpperCamelCase					:					Tuple=0.1     ,		UpperCamelCase					:					Optional[Any]=512     ,		UpperCamelCase					:					Optional[int]=16     ,		UpperCamelCase					:					int=2     ,		UpperCamelCase					:					List[str]=0.02     ,		UpperCamelCase					:					int=3     ,		UpperCamelCase					:					int=4     ,		UpperCamelCase					:					List[Any]=None     ,		):

             '''simple docstring'''







             lowercase__       =				parent
             lowercase__       =				batch_size
             lowercase__       =				seq_length
             lowercase__       =				is_training
             lowercase__       =				use_input_mask
             lowercase__       =				use_token_type_ids
             lowercase__       =				use_labels
             lowercase__       =				vocab_size
             lowercase__       =				hidden_size
             lowercase__       =				num_hidden_layers
             lowercase__       =				num_attention_heads
             lowercase__       =				intermediate_size
             lowercase__       =				hidden_act
             lowercase__       =				hidden_dropout_prob
             lowercase__       =				attention_probs_dropout_prob
             lowercase__       =				max_position_embeddings
             lowercase__       =				type_vocab_size
             lowercase__       =				type_sequence_label_size
             lowercase__       =				initializer_range
             lowercase__       =				num_labels
             lowercase__       =				num_choices
             lowercase__       =				scope



      def    UpperCamelCase__	(self					:					Tuple				):

             '''simple docstring'''







             lowercase__       =				ids_tensor([self.batch_size, self.seq_length]     ,		self.vocab_size				)

             lowercase__       =				None
             if self.use_input_mask:
                    lowercase__       =				random_attention_mask([self.batch_size, self.seq_length]				)

             lowercase__       =				None
             lowercase__       =				None
             lowercase__       =				None
             if self.use_labels:
                    lowercase__       =				ids_tensor([self.batch_size]     ,		self.type_sequence_label_size				)
                    lowercase__       =				ids_tensor([self.batch_size, self.seq_length]     ,		self.num_labels				)
                    lowercase__       =				ids_tensor([self.batch_size]     ,		self.num_choices				)

             lowercase__       =				MPNetConfig(
                 vocab_size=self.vocab_size     ,		hidden_size=self.hidden_size     ,		num_hidden_layers=self.num_hidden_layers     ,		num_attention_heads=self.num_attention_heads     ,		intermediate_size=self.intermediate_size     ,		hidden_act=self.hidden_act     ,		hidden_dropout_prob=self.hidden_dropout_prob     ,		attention_probs_dropout_prob=self.attention_probs_dropout_prob     ,		max_position_embeddings=self.max_position_embeddings     ,		initializer_range=self.initializer_range     ,		)
             return config, input_ids, input_mask, sequence_labels, token_labels, choice_labels



      def    UpperCamelCase__	(self					:					Optional[int]     ,		UpperCamelCase					:					List[Any]     ,		UpperCamelCase					:					Union[str, Any]     ,		UpperCamelCase					:					Dict     ,		UpperCamelCase					:					List[Any]     ,		UpperCamelCase					:					str     ,		UpperCamelCase					:					Optional[int]				):

             '''simple docstring'''







             lowercase__       =				TFMPNetModel(config=UpperCamelCase				)
             lowercase__       =				{'''input_ids''': input_ids, '''attention_mask''': input_mask}
             lowercase__       =				model(UpperCamelCase				)
             lowercase__       =				[input_ids, input_mask]
             lowercase__       =				model(UpperCamelCase				)
             self.parent.assertEqual(result.last_hidden_state.shape     ,		(self.batch_size, self.seq_length, self.hidden_size)				)



      def    UpperCamelCase__	(self					:					Dict     ,		UpperCamelCase					:					str     ,		UpperCamelCase					:					str     ,		UpperCamelCase					:					Dict     ,		UpperCamelCase					:					Dict     ,		UpperCamelCase					:					Union[str, Any]     ,		UpperCamelCase					:					int				):

             '''simple docstring'''







             lowercase__       =				TFMPNetForMaskedLM(config=UpperCamelCase				)
             lowercase__       =				{'''input_ids''': input_ids, '''attention_mask''': input_mask}
             lowercase__       =				model(UpperCamelCase				)
             self.parent.assertEqual(result.logits.shape     ,		(self.batch_size, self.seq_length, self.vocab_size)				)



      def    UpperCamelCase__	(self					:					List[str]     ,		UpperCamelCase					:					Optional[int]     ,		UpperCamelCase					:					Optional[int]     ,		UpperCamelCase					:					List[str]     ,		UpperCamelCase					:					int     ,		UpperCamelCase					:					Dict     ,		UpperCamelCase					:					List[Any]				):

             '''simple docstring'''







             lowercase__       =				TFMPNetForQuestionAnswering(config=UpperCamelCase				)
             lowercase__       =				{
                 '''input_ids''': input_ids,
                 '''attention_mask''': input_mask,
             }
             lowercase__       =				model(UpperCamelCase				)
             self.parent.assertEqual(result.start_logits.shape     ,		(self.batch_size, self.seq_length)				)
             self.parent.assertEqual(result.end_logits.shape     ,		(self.batch_size, self.seq_length)				)



      def    UpperCamelCase__	(self					:					Tuple     ,		UpperCamelCase					:					Tuple     ,		UpperCamelCase					:					Union[str, Any]     ,		UpperCamelCase					:					str     ,		UpperCamelCase					:					Dict     ,		UpperCamelCase					:					Optional[Any]     ,		UpperCamelCase					:					Optional[int]				):

             '''simple docstring'''







             lowercase__       =				self.num_labels
             lowercase__       =				TFMPNetForSequenceClassification(UpperCamelCase				)
             lowercase__       =				{'''input_ids''': input_ids, '''attention_mask''': input_mask}
             lowercase__       =				model(UpperCamelCase				)
             self.parent.assertEqual(result.logits.shape     ,		(self.batch_size, self.num_labels)				)



      def    UpperCamelCase__	(self					:					List[Any]     ,		UpperCamelCase					:					Tuple     ,		UpperCamelCase					:					Any     ,		UpperCamelCase					:					Optional[int]     ,		UpperCamelCase					:					Any     ,		UpperCamelCase					:					List[str]     ,		UpperCamelCase					:					Union[str, Any]				):

             '''simple docstring'''







             lowercase__       =				self.num_choices
             lowercase__       =				TFMPNetForMultipleChoice(UpperCamelCase				)
             lowercase__       =				tf.tile(tf.expand_dims(UpperCamelCase     ,		1				)     ,		(1, self.num_choices, 1)				)
             lowercase__       =				tf.tile(tf.expand_dims(UpperCamelCase     ,		1				)     ,		(1, self.num_choices, 1)				)
             lowercase__       =				{
                 '''input_ids''': multiple_choice_inputs_ids,
                 '''attention_mask''': multiple_choice_input_mask,
             }
             lowercase__       =				model(UpperCamelCase				)
             self.parent.assertEqual(result.logits.shape     ,		(self.batch_size, self.num_choices)				)



      def    UpperCamelCase__	(self					:					List[str]     ,		UpperCamelCase					:					List[Any]     ,		UpperCamelCase					:					Optional[Any]     ,		UpperCamelCase					:					Optional[Any]     ,		UpperCamelCase					:					str     ,		UpperCamelCase					:					Optional[int]     ,		UpperCamelCase					:					Optional[Any]				):

             '''simple docstring'''







             lowercase__       =				self.num_labels
             lowercase__       =				TFMPNetForTokenClassification(UpperCamelCase				)
             lowercase__       =				{'''input_ids''': input_ids, '''attention_mask''': input_mask}
             lowercase__       =				model(UpperCamelCase				)
             self.parent.assertEqual(result.logits.shape     ,		(self.batch_size, self.seq_length, self.num_labels)				)






      def    UpperCamelCase__	(self					:					List[str]				):

             '''simple docstring'''







             lowercase__       =				self.prepare_config_and_inputs()
             ((lowercase__)    ,(lowercase__)    ,(lowercase__)    ,(lowercase__)    ,(lowercase__)    ,(lowercase__))       =				config_and_inputs
             lowercase__       =				{'''input_ids''': input_ids, '''attention_mask''': input_mask}
             return config, inputs_dict






@require_tf
class 						_UpperCAmelCase		(SCREAMING_SNAKE_CASE__						,  SCREAMING_SNAKE_CASE__						,  unittest.TestCase  ):





      '''simple docstring'''






      lowerCAmelCase__      :  str						      =							(
          (
              TFMPNetForMaskedLM,
              TFMPNetForMultipleChoice,
              TFMPNetForQuestionAnswering,
              TFMPNetForSequenceClassification,
              TFMPNetForTokenClassification,
              TFMPNetModel,
          )
          if is_tf_available()
          else ()
      )
      lowerCAmelCase__      :  Tuple						      =							(
          {
              """feature-extraction""": TFMPNetModel,
              """fill-mask""": TFMPNetForMaskedLM,
              """question-answering""": TFMPNetForQuestionAnswering,
              """text-classification""": TFMPNetForSequenceClassification,
              """token-classification""": TFMPNetForTokenClassification,
              """zero-shot""": TFMPNetForSequenceClassification,
          }
          if is_tf_available()
          else {}
      )
      lowerCAmelCase__      :  Optional[Any]						      =							False
      lowerCAmelCase__      :  int						      =							False



      def    UpperCamelCase__	(self					:					List[str]				):

             '''simple docstring'''







             lowercase__       =				TFMPNetModelTester(self				)
             lowercase__       =				ConfigTester(self     ,		config_class=UpperCamelCase     ,		hidden_size=37				)



      def    UpperCamelCase__	(self					:					Optional[int]				):

             '''simple docstring'''







             self.config_tester.run_common_tests()



      def    UpperCamelCase__	(self					:					Tuple				):

             '''simple docstring'''







             lowercase__       =				self.model_tester.prepare_config_and_inputs()
             self.model_tester.create_and_check_mpnet_model(*UpperCamelCase				)



      def    UpperCamelCase__	(self					:					List[Any]				):

             '''simple docstring'''







             lowercase__       =				self.model_tester.prepare_config_and_inputs()
             self.model_tester.create_and_check_mpnet_for_masked_lm(*UpperCamelCase				)



      def    UpperCamelCase__	(self					:					Union[str, Any]				):

             '''simple docstring'''







             lowercase__       =				self.model_tester.prepare_config_and_inputs()
             self.model_tester.create_and_check_mpnet_for_question_answering(*UpperCamelCase				)



      def    UpperCamelCase__	(self					:					int				):

             '''simple docstring'''







             lowercase__       =				self.model_tester.prepare_config_and_inputs()
             self.model_tester.create_and_check_mpnet_for_sequence_classification(*UpperCamelCase				)



      def    UpperCamelCase__	(self					:					List[Any]				):

             '''simple docstring'''







             lowercase__       =				self.model_tester.prepare_config_and_inputs()
             self.model_tester.create_and_check_mpnet_for_multiple_choice(*UpperCamelCase				)



      def    UpperCamelCase__	(self					:					Dict				):

             '''simple docstring'''







             lowercase__       =				self.model_tester.prepare_config_and_inputs()
             self.model_tester.create_and_check_mpnet_for_token_classification(*UpperCamelCase				)






      @slow
      def    UpperCamelCase__	(self					:					Union[str, Any]				):

             '''simple docstring'''







             for model_name in ["microsoft/mpnet-base"]:
                    lowercase__       =				TFMPNetModel.from_pretrained(UpperCamelCase				)
                    self.assertIsNotNone(UpperCamelCase				)






@require_tf
class 						_UpperCAmelCase		(unittest.TestCase  ):





      '''simple docstring'''






      @slow
      def    UpperCamelCase__	(self					:					List[Any]				):

             '''simple docstring'''







             lowercase__       =				TFMPNetModel.from_pretrained('''microsoft/mpnet-base'''				)
             lowercase__       =				tf.constant([[0, 1, 2, 3, 4, 5]]				)
             lowercase__       =				model(UpperCamelCase				)[0]

             lowercase__       =				[1, 6, 768]
             self.assertEqual(output.shape     ,		UpperCamelCase				)

             lowercase__       =				tf.constant(
                 [
                     [
                         [-0.1_06_71_72, 0.08_21_64_73, 0.0_02_45_43],
                         [-0.03_46_58_79, 0.8_35_41_18, -0.03_25_22_88],
                         [-0.06_56_94_76, -0.12_42_41_11, -0.0_49_44_36],
                     ]
                 ]				)
             tf.debugging.assert_near(output[:, :3, :3]     ,		UpperCamelCase     ,		atol=1E-4				)








'''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


lowerCamelCase							:     List[str]								=			collections.namedtuple(
    'CheckpointState', ['model_dict', 'optimizer_dict', 'scheduler_dict', 'offset', 'epoch', 'encoder_params']
)

def _SCREAMING_SNAKE_CASE   (A ) -> CheckpointState:






       """simple docstring"""







       print(f"Reading saved model from {model_file}" )
       lowercase__       =				torch.load(A						,				map_location=lambda A						,				A : default_restore_location(A						,				'''cpu''' ) )
       return CheckpointState(**A )
class 						__lowerCAmelCase		:





      '''simple docstring'''






      def __init__(self					:					Any     ,		UpperCamelCase					:					Path				):

             '''simple docstring'''







             lowercase__       =				src_file



      def    UpperCamelCase__	(self					:					List[str]				):

             '''simple docstring'''







             raise NotImplementedError






      @staticmethod
      def    UpperCamelCase__	(UpperCamelCase					:					str     ,		*UpperCamelCase					:					Dict     ,		**UpperCamelCase					:					int				):

             '''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 						__lowerCAmelCase		(lowercase_  ):





      '''simple docstring'''






      def    UpperCamelCase__	(self					:					Any				):

             '''simple docstring'''







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






class 						__lowerCAmelCase		(lowercase_  ):





      '''simple docstring'''






      def    UpperCamelCase__	(self					:					Union[str, Any]				):

             '''simple docstring'''







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






class 						__lowerCAmelCase		(lowercase_  ):





      '''simple docstring'''






      def    UpperCamelCase__	(self					:					Optional[Any]				):

             '''simple docstring'''







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






def _SCREAMING_SNAKE_CASE   (A						,				A						,				A ) -> Optional[Any]:






       """simple docstring"""







       lowercase__       =				Path(A )
       dest_dir.mkdir(exist_ok=A )

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


if __name__ == "__main__":
 lowerCamelCase							:     List[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							:     Optional[int]								=			parser.parse_args()

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




