


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__ :






      def __init__(     self					:   Dict,		__lowerCamelCase					:   List[Any],		__lowerCamelCase					:   str=13,		__lowerCamelCase					:   Any=7,		__lowerCamelCase					:   List[str]=True,		__lowerCamelCase					:   Optional[Any]=True,		__lowerCamelCase					:   Union[str, Any]=False,		__lowerCamelCase					:   List[Any]=True,		__lowerCamelCase					:   Dict=99,		__lowerCamelCase					:   Tuple=64,		__lowerCamelCase					:   Any=2,		__lowerCamelCase					:   Union[str, Any]=4,		__lowerCamelCase					:   Optional[int]=64,		__lowerCamelCase					:   str="gelu",		__lowerCamelCase					:   int=0.1,		__lowerCamelCase					:   List[Any]=0.1,		__lowerCamelCase					:   str=5_12,		__lowerCamelCase					:   Optional[Any]=16,		__lowerCamelCase					:   Union[str, Any]=2,		__lowerCamelCase					:   List[Any]=0.02,		__lowerCamelCase					:   Any=3,		__lowerCamelCase					:   Dict=4,		__lowerCamelCase					:   List[Any]=None,		)  -> Union[str, Any]:
           UpperCamelCase__							:  Any								=     parent
           UpperCamelCase__							:  Any								=     batch_size
           UpperCamelCase__							:  List[str]								=     seq_length
           UpperCamelCase__							:  Any								=     is_training
           UpperCamelCase__							:  str								=     use_input_mask
           UpperCamelCase__							:  Any								=     use_token_type_ids
           UpperCamelCase__							:  Any								=     use_labels
           UpperCamelCase__							:  str								=     vocab_size
           UpperCamelCase__							:  Dict								=     hidden_size
           UpperCamelCase__							:  str								=     num_hidden_layers
           UpperCamelCase__							:  List[Any]								=     num_attention_heads
           UpperCamelCase__							:  Union[str, Any]								=     intermediate_size
           UpperCamelCase__							:  str								=     hidden_act
           UpperCamelCase__							:  Union[str, Any]								=     hidden_dropout_prob
           UpperCamelCase__							:  Dict								=     attention_probs_dropout_prob
           UpperCamelCase__							:  List[str]								=     max_position_embeddings
           UpperCamelCase__							:  Dict								=     type_vocab_size
           UpperCamelCase__							:  List[Any]								=     type_sequence_label_size
           UpperCamelCase__							:  Union[str, Any]								=     initializer_range
           UpperCamelCase__							:  List[str]								=     num_labels
           UpperCamelCase__							:  Any								=     num_choices
           UpperCamelCase__							:  Optional[int]								=     scope






      def 	__lowercase(     self					:   Tuple   )  -> Union[str, Any]:
           UpperCamelCase__							:  str								=     ids_tensor([self.batch_size, self.seq_length],		self.vocab_size   )

           UpperCamelCase__							:  Union[str, Any]								=     None
           if self.use_input_mask:
                UpperCamelCase__							:  Union[str, Any]								=     random_attention_mask([self.batch_size, self.seq_length]   )

           UpperCamelCase__							:  List[Any]								=     None
           UpperCamelCase__							:  str								=     None
           UpperCamelCase__							:  List[str]								=     None
           if self.use_labels:
                UpperCamelCase__							:  Any								=     ids_tensor([self.batch_size],		self.type_sequence_label_size   )
                UpperCamelCase__							:  Dict								=     ids_tensor([self.batch_size, self.seq_length],		self.num_labels   )
                UpperCamelCase__							:  Optional[Any]								=     ids_tensor([self.batch_size],		self.num_choices   )

           UpperCamelCase__							:  Optional[Any]								=     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 	__lowercase(     self					:   int,		__lowerCamelCase					:   Tuple,		__lowerCamelCase					:   Optional[int],		__lowerCamelCase					:   Optional[int],		__lowerCamelCase					:   int,		__lowerCamelCase					:   int,		__lowerCamelCase					:   Any   )  -> List[str]:
           UpperCamelCase__							:  List[Any]								=     TFMPNetModel(config=__lowerCamelCase   )
           UpperCamelCase__							:  Any								=     {'''input_ids''': input_ids, '''attention_mask''': input_mask}
           UpperCamelCase__							:  Optional[int]								=     model(__lowerCamelCase   )
           UpperCamelCase__							:  Optional[Any]								=     [input_ids, input_mask]
           UpperCamelCase__							:  int								=     model(__lowerCamelCase   )
           self.parent.assertEqual(result.last_hidden_state.shape,		(self.batch_size, self.seq_length, self.hidden_size)   )






      def 	__lowercase(     self					:   str,		__lowerCamelCase					:   Union[str, Any],		__lowerCamelCase					:   Dict,		__lowerCamelCase					:   str,		__lowerCamelCase					:   Tuple,		__lowerCamelCase					:   List[str],		__lowerCamelCase					:   List[Any]   )  -> str:
           UpperCamelCase__							:  int								=     TFMPNetForMaskedLM(config=__lowerCamelCase   )
           UpperCamelCase__							:  Tuple								=     {'''input_ids''': input_ids, '''attention_mask''': input_mask}
           UpperCamelCase__							:  List[str]								=     model(__lowerCamelCase   )
           self.parent.assertEqual(result.logits.shape,		(self.batch_size, self.seq_length, self.vocab_size)   )






      def 	__lowercase(     self					:   int,		__lowerCamelCase					:   str,		__lowerCamelCase					:   Optional[Any],		__lowerCamelCase					:   str,		__lowerCamelCase					:   Any,		__lowerCamelCase					:   Tuple,		__lowerCamelCase					:   List[str]   )  -> Dict:
           UpperCamelCase__							:  List[Any]								=     TFMPNetForQuestionAnswering(config=__lowerCamelCase   )
           UpperCamelCase__							:  int								=     {
               '''input_ids''': input_ids,
               '''attention_mask''': input_mask,
           }
           UpperCamelCase__							:  int								=     model(__lowerCamelCase   )
           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 	__lowercase(     self					:   Any,		__lowerCamelCase					:   List[str],		__lowerCamelCase					:   Union[str, Any],		__lowerCamelCase					:   str,		__lowerCamelCase					:   Dict,		__lowerCamelCase					:   Union[str, Any],		__lowerCamelCase					:   Union[str, Any]   )  -> Dict:
           UpperCamelCase__							:  Optional[Any]								=     self.num_labels
           UpperCamelCase__							:  List[str]								=     TFMPNetForSequenceClassification(__lowerCamelCase   )
           UpperCamelCase__							:  str								=     {'''input_ids''': input_ids, '''attention_mask''': input_mask}
           UpperCamelCase__							:  Dict								=     model(__lowerCamelCase   )
           self.parent.assertEqual(result.logits.shape,		(self.batch_size, self.num_labels)   )






      def 	__lowercase(     self					:   Any,		__lowerCamelCase					:   int,		__lowerCamelCase					:   List[Any],		__lowerCamelCase					:   Dict,		__lowerCamelCase					:   Optional[int],		__lowerCamelCase					:   List[str],		__lowerCamelCase					:   Dict   )  -> Optional[Any]:
           UpperCamelCase__							:  int								=     self.num_choices
           UpperCamelCase__							:  Dict								=     TFMPNetForMultipleChoice(__lowerCamelCase   )
           UpperCamelCase__							:  str								=     tf.tile(tf.expand_dims(__lowerCamelCase,		1   ),		(1, self.num_choices, 1)   )
           UpperCamelCase__							:  Union[str, Any]								=     tf.tile(tf.expand_dims(__lowerCamelCase,		1   ),		(1, self.num_choices, 1)   )
           UpperCamelCase__							:  Tuple								=     {
               '''input_ids''': multiple_choice_inputs_ids,
               '''attention_mask''': multiple_choice_input_mask,
           }
           UpperCamelCase__							:  List[Any]								=     model(__lowerCamelCase   )
           self.parent.assertEqual(result.logits.shape,		(self.batch_size, self.num_choices)   )






      def 	__lowercase(     self					:   Dict,		__lowerCamelCase					:   Optional[Any],		__lowerCamelCase					:   int,		__lowerCamelCase					:   Optional[int],		__lowerCamelCase					:   Dict,		__lowerCamelCase					:   int,		__lowerCamelCase					:   Optional[int]   )  -> List[str]:
           UpperCamelCase__							:  Dict								=     self.num_labels
           UpperCamelCase__							:  List[str]								=     TFMPNetForTokenClassification(__lowerCamelCase   )
           UpperCamelCase__							:  Union[str, Any]								=     {'''input_ids''': input_ids, '''attention_mask''': input_mask}
           UpperCamelCase__							:  List[Any]								=     model(__lowerCamelCase   )
           self.parent.assertEqual(result.logits.shape,		(self.batch_size, self.seq_length, self.num_labels)   )






      def 	__lowercase(     self					:   str   )  -> Union[str, Any]:
           UpperCamelCase__							:  List[str]								=     self.prepare_config_and_inputs()
           (UpperCamelCase__)							:  int								=     config_and_inputs
           UpperCamelCase__							:  int								=     {'''input_ids''': input_ids, '''attention_mask''': input_mask}
           return config, inputs_dict

@require_tf
class  UpperCamelCase__ ( __lowerCamelCase							,						__lowerCamelCase							,						unittest.TestCase			):
      a__   :     Union[str, Any]								=						(
          (
              TFMPNetForMaskedLM,
              TFMPNetForMultipleChoice,
              TFMPNetForQuestionAnswering,
              TFMPNetForSequenceClassification,
              TFMPNetForTokenClassification,
              TFMPNetModel,
          )
          if is_tf_available()
          else ()
      )
      a__   :     str								=						(
          {
              'feature-extraction': TFMPNetModel,
              'fill-mask': TFMPNetForMaskedLM,
              'question-answering': TFMPNetForQuestionAnswering,
              'text-classification': TFMPNetForSequenceClassification,
              'token-classification': TFMPNetForTokenClassification,
              'zero-shot': TFMPNetForSequenceClassification,
          }
          if is_tf_available()
          else {}
      )
      a__   :     List[str]								=						False
      a__   :     Any								=						False






      def 	__lowercase(     self					:   List[str]   )  -> Tuple:
           UpperCamelCase__							:  List[str]								=     TFMPNetModelTester(self   )
           UpperCamelCase__							:  Optional[Any]								=     ConfigTester(self,		config_class=__lowerCamelCase,		hidden_size=37   )






      def 	__lowercase(     self					:   Tuple   )  -> Union[str, Any]:
           self.config_tester.run_common_tests()






      def 	__lowercase(     self					:   Union[str, Any]   )  -> Optional[int]:
           UpperCamelCase__							:  int								=     self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_mpnet_model(*__lowerCamelCase   )






      def 	__lowercase(     self					:   Any   )  -> str:
           UpperCamelCase__							:  int								=     self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_mpnet_for_masked_lm(*__lowerCamelCase   )






      def 	__lowercase(     self					:   Any   )  -> Dict:
           UpperCamelCase__							:  Optional[Any]								=     self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_mpnet_for_question_answering(*__lowerCamelCase   )






      def 	__lowercase(     self					:   Optional[int]   )  -> str:
           UpperCamelCase__							:  Optional[int]								=     self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_mpnet_for_sequence_classification(*__lowerCamelCase   )






      def 	__lowercase(     self					:   Dict   )  -> Any:
           UpperCamelCase__							:  Optional[int]								=     self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_mpnet_for_multiple_choice(*__lowerCamelCase   )






      def 	__lowercase(     self					:   Dict   )  -> List[Any]:
           UpperCamelCase__							:  Union[str, Any]								=     self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_mpnet_for_token_classification(*__lowerCamelCase   )






      @slow
      def 	__lowercase(     self					:   List[str]   )  -> int:
           for model_name in ["microsoft/mpnet-base"]:
                UpperCamelCase__							:  List[Any]								=     TFMPNetModel.from_pretrained(__lowerCamelCase   )
                self.assertIsNotNone(__lowerCamelCase   )

@require_tf
class  UpperCamelCase__ ( unittest.TestCase			):






      @slow
      def 	__lowercase(     self					:   Optional[Any]   )  -> Dict:
           UpperCamelCase__							:  Any								=     TFMPNetModel.from_pretrained('''microsoft/mpnet-base'''   )
           UpperCamelCase__							:  str								=     tf.constant([[0, 1, 2, 3, 4, 5]]   )
           UpperCamelCase__							:  int								=     model(__lowerCamelCase   )[0]

           UpperCamelCase__							:  Optional[int]								=     [1, 6, 7_68]
           self.assertEqual(output.shape,		__lowerCamelCase   )

           UpperCamelCase__							:  List[Any]								=     tf.constant(
               [
                   [
                       [-0.106_7172, 0.0821_6473, 0.002_4543],
                       [-0.0346_5879, 0.835_4118, -0.0325_2288],
                       [-0.0656_9476, -0.1242_4111, -0.049_4436],
                   ]
               ]   )
           tf.debugging.assert_near(output[:, :3, :3],		__lowerCamelCase,		atol=1e-4   )








import json
from typing import TYPE_CHECKING, List, Optional, Tuple

from tokenizers import pre_tokenizers

from ...tokenization_utils_base import AddedToken, BatchEncoding
from ...tokenization_utils_fast import PreTrainedTokenizerFast
from ...utils import logging
from .tokenization_deberta import DebertaTokenizer


if TYPE_CHECKING:
   from transformers.pipelines.conversational import Conversation


_SCREAMING_SNAKE_CASE :					Tuple			=	logging.get_logger(__name__)

_SCREAMING_SNAKE_CASE :					Tuple			=	{"""vocab_file""": """vocab.json""", """merges_file""": """merges.txt""", """tokenizer_file""": """tokenizer.json"""}

_SCREAMING_SNAKE_CASE :					str			=	{
    """vocab_file""": {
        """microsoft/deberta-base""": """https://huggingface.co/microsoft/deberta-base/resolve/main/vocab.json""",
        """microsoft/deberta-large""": """https://huggingface.co/microsoft/deberta-large/resolve/main/vocab.json""",
        """microsoft/deberta-xlarge""": """https://huggingface.co/microsoft/deberta-xlarge/resolve/main/vocab.json""",
        """microsoft/deberta-base-mnli""": """https://huggingface.co/microsoft/deberta-base-mnli/resolve/main/vocab.json""",
        """microsoft/deberta-large-mnli""": """https://huggingface.co/microsoft/deberta-large-mnli/resolve/main/vocab.json""",
        """microsoft/deberta-xlarge-mnli""": (
            """https://huggingface.co/microsoft/deberta-xlarge-mnli/resolve/main/vocab.json"""
        ),
    },
    """merges_file""": {
        """microsoft/deberta-base""": """https://huggingface.co/microsoft/deberta-base/resolve/main/merges.txt""",
        """microsoft/deberta-large""": """https://huggingface.co/microsoft/deberta-large/resolve/main/merges.txt""",
        """microsoft/deberta-xlarge""": """https://huggingface.co/microsoft/deberta-xlarge/resolve/main/merges.txt""",
        """microsoft/deberta-base-mnli""": """https://huggingface.co/microsoft/deberta-base-mnli/resolve/main/merges.txt""",
        """microsoft/deberta-large-mnli""": """https://huggingface.co/microsoft/deberta-large-mnli/resolve/main/merges.txt""",
        """microsoft/deberta-xlarge-mnli""": (
            """https://huggingface.co/microsoft/deberta-xlarge-mnli/resolve/main/merges.txt"""
        ),
    },
}

_SCREAMING_SNAKE_CASE :					Any			=	{
    """microsoft/deberta-base""": 512,
    """microsoft/deberta-large""": 512,
    """microsoft/deberta-xlarge""": 512,
    """microsoft/deberta-base-mnli""": 512,
    """microsoft/deberta-large-mnli""": 512,
    """microsoft/deberta-xlarge-mnli""": 512,
}

_SCREAMING_SNAKE_CASE :					Any			=	{
    """microsoft/deberta-base""": {"""do_lower_case""": False},
    """microsoft/deberta-large""": {"""do_lower_case""": False},
}

class  UpperCamelCase__ ( __lowerCamelCase			):
      a__   :     int								=						VOCAB_FILES_NAMES
      a__   :     Optional[Any]								=						PRETRAINED_VOCAB_FILES_MAP
      a__   :     int								=						PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
      a__   :     str								=						['input_ids', 'attention_mask', 'token_type_ids']
      a__   :     int								=						DebertaTokenizer






      def __init__(     self					:   Optional[int],		__lowerCamelCase					:   List[str]=None,		__lowerCamelCase					:   Optional[Any]=None,		__lowerCamelCase					:   Union[str, Any]=None,		__lowerCamelCase					:   str="replace",		__lowerCamelCase					:   Any="[CLS]",		__lowerCamelCase					:   Optional[int]="[SEP]",		__lowerCamelCase					:   List[Any]="[SEP]",		__lowerCamelCase					:   Optional[Any]="[CLS]",		__lowerCamelCase					:   List[Any]="[UNK]",		__lowerCamelCase					:   Optional[Any]="[PAD]",		__lowerCamelCase					:   Dict="[MASK]",		__lowerCamelCase					:   List[Any]=False,		**__lowerCamelCase					:   Union[str, Any],		)  -> Optional[int]:
           super().__init__(
               __lowerCamelCase,		__lowerCamelCase,		tokenizer_file=__lowerCamelCase,		errors=__lowerCamelCase,		bos_token=__lowerCamelCase,		eos_token=__lowerCamelCase,		unk_token=__lowerCamelCase,		sep_token=__lowerCamelCase,		cls_token=__lowerCamelCase,		pad_token=__lowerCamelCase,		mask_token=__lowerCamelCase,		add_prefix_space=__lowerCamelCase,		**__lowerCamelCase,		)
           UpperCamelCase__							:  Any								=     kwargs.pop('''add_bos_token''',		__lowerCamelCase   )

           UpperCamelCase__							:  List[Any]								=     json.loads(self.backend_tokenizer.pre_tokenizer.__getstate__()   )
           if pre_tok_state.get('''add_prefix_space''',		__lowerCamelCase   ) != add_prefix_space:
                UpperCamelCase__							:  Tuple								=     getattr(__lowerCamelCase,		pre_tok_state.pop('''type'''   )   )
                UpperCamelCase__							:  Tuple								=     add_prefix_space
                UpperCamelCase__							:  Union[str, Any]								=     pre_tok_class(**__lowerCamelCase   )

           UpperCamelCase__							:  Optional[int]								=     add_prefix_space






      @property
      def 	__lowercase(     self					:   Dict   )  -> str:
           if self._mask_token is None:
                if self.verbose:
                     logger.error('''Using mask_token, but it is not set yet.'''   )
                return None
           return str(self._mask_token   )






      @mask_token.setter
      def 	__lowercase(     self					:   List[Any],		__lowerCamelCase					:   Union[str, Any]   )  -> List[str]:
           UpperCamelCase__							:  List[Any]								=     AddedToken(__lowerCamelCase,		lstrip=__lowerCamelCase,		rstrip=__lowerCamelCase   ) if isinstance(__lowerCamelCase,		__lowerCamelCase   ) else value
           UpperCamelCase__							:  List[str]								=     value






      def 	__lowercase(     self					:   List[str],		__lowerCamelCase					:   List[int],		__lowerCamelCase					:   Optional[List[int]] = None   )  -> List[int]:
           if token_ids_a is None:
                return [self.cls_token_id] + token_ids_a + [self.sep_token_id]
           UpperCamelCase__							:  Union[str, Any]								=     [self.cls_token_id]
           UpperCamelCase__							:  Union[str, Any]								=     [self.sep_token_id]
           return cls + token_ids_a + sep + token_ids_a + sep






      def 	__lowercase(     self					:   str,		__lowerCamelCase					:   List[int],		__lowerCamelCase					:   Optional[List[int]] = None   )  -> List[int]:
           UpperCamelCase__							:  Dict								=     [self.sep_token_id]
           UpperCamelCase__							:  int								=     [self.cls_token_id]

           if token_ids_a is None:
                return len(cls + token_ids_a + sep   ) * [0]
           return len(cls + token_ids_a + sep   ) * [0] + len(token_ids_a + sep   ) * [1]






      def 	__lowercase(     self					:   Union[str, Any],		*__lowerCamelCase					:   Any,		**__lowerCamelCase					:   List[Any]   )  -> BatchEncoding:
           UpperCamelCase__							:  str								=     kwargs.get('''is_split_into_words''',		__lowerCamelCase   )
           assert self.add_prefix_space or not is_split_into_words, (
               f'You need to instantiate {self.__class__.__name__} with add_prefix_space=True '
               "to use it with pretokenized inputs."
           )

           return super()._batch_encode_plus(*__lowerCamelCase,		**__lowerCamelCase   )






      def 	__lowercase(     self					:   Optional[Any],		*__lowerCamelCase					:   str,		**__lowerCamelCase					:   int   )  -> BatchEncoding:
           UpperCamelCase__							:  str								=     kwargs.get('''is_split_into_words''',		__lowerCamelCase   )

           assert self.add_prefix_space or not is_split_into_words, (
               f'You need to instantiate {self.__class__.__name__} with add_prefix_space=True '
               "to use it with pretokenized inputs."
           )

           return super()._encode_plus(*__lowerCamelCase,		**__lowerCamelCase   )






      def 	__lowercase(     self					:   Union[str, Any],		__lowerCamelCase					:   str,		__lowerCamelCase					:   Optional[str] = None   )  -> Tuple[str]:
           UpperCamelCase__							:  Dict								=     self._tokenizer.model.save(__lowerCamelCase,		name=__lowerCamelCase   )
           return tuple(__lowerCamelCase   )






      def 	__lowercase(     self					:   int,		__lowerCamelCase					:   "Conversation"   )  -> List[int]:
           UpperCamelCase__							:  List[Any]								=     []
           for is_user, text in conversation.iter_texts():
                input_ids.extend(self.encode(__lowerCamelCase,		add_special_tokens=__lowerCamelCase   ) + [self.eos_token_id]   )

           if len(__lowerCamelCase   ) > self.model_max_length:
                UpperCamelCase__							:  List[Any]								=     input_ids[-self.model_max_length :]
           return input_ids




