






"""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   ,			__lowercase   ,			__lowercase=13   ,			__lowercase=7   ,			__lowercase=True   ,			__lowercase=True   ,			__lowercase=False   ,			__lowercase=True   ,			__lowercase=99   ,			__lowercase=64   ,			__lowercase=2   ,			__lowercase=4   ,			__lowercase=64   ,			__lowercase="gelu"   ,			__lowercase=0.1   ,			__lowercase=0.1   ,			__lowercase=512   ,			__lowercase=16   ,			__lowercase=2   ,			__lowercase=0.02   ,			__lowercase=3   ,			__lowercase=4   ,			__lowercase=None   ,			):
     UpperCAmelCase__ 						=				parent
     UpperCAmelCase__ 						=				batch_size
     UpperCAmelCase__ 						=				seq_length
     UpperCAmelCase__ 						=				is_training
     UpperCAmelCase__ 						=				use_input_mask
     UpperCAmelCase__ 						=				use_token_type_ids
     UpperCAmelCase__ 						=				use_labels
     UpperCAmelCase__ 						=				vocab_size
     UpperCAmelCase__ 						=				hidden_size
     UpperCAmelCase__ 						=				num_hidden_layers
     UpperCAmelCase__ 						=				num_attention_heads
     UpperCAmelCase__ 						=				intermediate_size
     UpperCAmelCase__ 						=				hidden_act
     UpperCAmelCase__ 						=				hidden_dropout_prob
     UpperCAmelCase__ 						=				attention_probs_dropout_prob
     UpperCAmelCase__ 						=				max_position_embeddings
     UpperCAmelCase__ 						=				type_vocab_size
     UpperCAmelCase__ 						=				type_sequence_label_size
     UpperCAmelCase__ 						=				initializer_range
     UpperCAmelCase__ 						=				num_labels
     UpperCAmelCase__ 						=				num_choices
     UpperCAmelCase__ 						=				scope


    def 		A__      (      self			):
     UpperCAmelCase__ 						=				ids_tensor([self.batch_size, self.seq_length]   ,			self.vocab_size			)

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

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

     UpperCAmelCase__ 						=				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 		A__      (      self   ,			__lowercase   ,			__lowercase   ,			__lowercase   ,			__lowercase   ,			__lowercase   ,			__lowercase			):
     UpperCAmelCase__ 						=				TFMPNetModel(config=__lowercase			)
     UpperCAmelCase__ 						=				{"""input_ids""": input_ids, """attention_mask""": input_mask}
     UpperCAmelCase__ 						=				model(__lowercase			)
     UpperCAmelCase__ 						=				[input_ids, input_mask]
     UpperCAmelCase__ 						=				model(__lowercase			)
     self.parent.assertEqual(result.last_hidden_state.shape   ,			(self.batch_size, self.seq_length, self.hidden_size)			)


    def 		A__      (      self   ,			__lowercase   ,			__lowercase   ,			__lowercase   ,			__lowercase   ,			__lowercase   ,			__lowercase			):
     UpperCAmelCase__ 						=				TFMPNetForMaskedLM(config=__lowercase			)
     UpperCAmelCase__ 						=				{"""input_ids""": input_ids, """attention_mask""": input_mask}
     UpperCAmelCase__ 						=				model(__lowercase			)
     self.parent.assertEqual(result.logits.shape   ,			(self.batch_size, self.seq_length, self.vocab_size)			)


    def 		A__      (      self   ,			__lowercase   ,			__lowercase   ,			__lowercase   ,			__lowercase   ,			__lowercase   ,			__lowercase			):
     UpperCAmelCase__ 						=				TFMPNetForQuestionAnswering(config=__lowercase			)
     UpperCAmelCase__ 						=				{
         """input_ids""": input_ids,
         """attention_mask""": input_mask,
     }
     UpperCAmelCase__ 						=				model(__lowercase			)
     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 		A__      (      self   ,			__lowercase   ,			__lowercase   ,			__lowercase   ,			__lowercase   ,			__lowercase   ,			__lowercase			):
     UpperCAmelCase__ 						=				self.num_labels
     UpperCAmelCase__ 						=				TFMPNetForSequenceClassification(__lowercase			)
     UpperCAmelCase__ 						=				{"""input_ids""": input_ids, """attention_mask""": input_mask}
     UpperCAmelCase__ 						=				model(__lowercase			)
     self.parent.assertEqual(result.logits.shape   ,			(self.batch_size, self.num_labels)			)


    def 		A__      (      self   ,			__lowercase   ,			__lowercase   ,			__lowercase   ,			__lowercase   ,			__lowercase   ,			__lowercase			):
     UpperCAmelCase__ 						=				self.num_choices
     UpperCAmelCase__ 						=				TFMPNetForMultipleChoice(__lowercase			)
     UpperCAmelCase__ 						=				tf.tile(tf.expand_dims(__lowercase   ,			1			)   ,			(1, self.num_choices, 1)			)
     UpperCAmelCase__ 						=				tf.tile(tf.expand_dims(__lowercase   ,			1			)   ,			(1, self.num_choices, 1)			)
     UpperCAmelCase__ 						=				{
         """input_ids""": multiple_choice_inputs_ids,
         """attention_mask""": multiple_choice_input_mask,
     }
     UpperCAmelCase__ 						=				model(__lowercase			)
     self.parent.assertEqual(result.logits.shape   ,			(self.batch_size, self.num_choices)			)


    def 		A__      (      self   ,			__lowercase   ,			__lowercase   ,			__lowercase   ,			__lowercase   ,			__lowercase   ,			__lowercase			):
     UpperCAmelCase__ 						=				self.num_labels
     UpperCAmelCase__ 						=				TFMPNetForTokenClassification(__lowercase			)
     UpperCAmelCase__ 						=				{"""input_ids""": input_ids, """attention_mask""": input_mask}
     UpperCAmelCase__ 						=				model(__lowercase			)
     self.parent.assertEqual(result.logits.shape   ,			(self.batch_size, self.seq_length, self.num_labels)			)






    def 		A__      (      self			):
     UpperCAmelCase__ 						=				self.prepare_config_and_inputs()
     ((UpperCAmelCase__)						,	(UpperCAmelCase__)						,	(UpperCAmelCase__)						,	(UpperCAmelCase__)						,	(UpperCAmelCase__)						,	(UpperCAmelCase__)) 						=				config_and_inputs
     UpperCAmelCase__ 						=				{"""input_ids""": input_ids, """attention_mask""": input_mask}
     return config, inputs_dict



@require_tf
class 	_UpperCamelCase ( __UpperCamelCase		,     __UpperCamelCase		,     unittest.TestCase							):



    '''simple docstring'''


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


    def 		A__      (      self			):
     UpperCAmelCase__ 						=				TFMPNetModelTester(self			)
     UpperCAmelCase__ 						=				ConfigTester(self   ,			config_class=__lowercase   ,			hidden_size=37			)


    def 		A__      (      self			):
     self.config_tester.run_common_tests()


    def 		A__      (      self			):
     UpperCAmelCase__ 						=				self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_mpnet_model(*__lowercase			)


    def 		A__      (      self			):
     UpperCAmelCase__ 						=				self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_mpnet_for_masked_lm(*__lowercase			)


    def 		A__      (      self			):
     UpperCAmelCase__ 						=				self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_mpnet_for_question_answering(*__lowercase			)


    def 		A__      (      self			):
     UpperCAmelCase__ 						=				self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_mpnet_for_sequence_classification(*__lowercase			)


    def 		A__      (      self			):
     UpperCAmelCase__ 						=				self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_mpnet_for_multiple_choice(*__lowercase			)


    def 		A__      (      self			):
     UpperCAmelCase__ 						=				self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_mpnet_for_token_classification(*__lowercase			)






    @slow
    def 		A__      (      self			):
     for model_name in ["microsoft/mpnet-base"]:
      UpperCAmelCase__ 						=				TFMPNetModel.from_pretrained(__lowercase			)
      self.assertIsNotNone(__lowercase			)



@require_tf
class 	_UpperCamelCase ( unittest.TestCase							):



    '''simple docstring'''


    @slow
    def 		A__      (      self			):
     UpperCAmelCase__ 						=				TFMPNetModel.from_pretrained("""microsoft/mpnet-base"""			)
     UpperCAmelCase__ 						=				tf.constant([[0, 1, 2, 3, 4, 5]]			)
     UpperCAmelCase__ 						=				model(__lowercase			)[0]

     UpperCAmelCase__ 						=				[1, 6, 768]
     self.assertEqual(output.shape   ,			__lowercase			)

     UpperCAmelCase__ 						=				tf.constant(
         [
             [
                 [-0.1_067_172, 0.08_216_473, 0.0_024_543],
                 [-0.03_465_879, 0.8_354_118, -0.03_252_288],
                 [-0.06_569_476, -0.12_424_111, -0.0_494_436],
             ]
         ]			)
     tf.debugging.assert_near(output[:, :3, :3]   ,			__lowercase   ,			atol=1e-4			)











"""simple docstring"""



import json
from typing import List, Optional, Tuple

from tokenizers import normalizers

from ...tokenization_utils import AddedToken
from ...tokenization_utils_fast import PreTrainedTokenizerFast
from ...utils import logging
from .tokenization_mpnet import MPNetTokenizer


a  :						List[Any]					       =							logging.get_logger(__name__)

a  :						List[Any]					       =							{'''vocab_file''': '''vocab.txt''', '''tokenizer_file''': '''tokenizer.json'''}

a  :						Any					       =							{
    '''vocab_file''': {
        '''microsoft/mpnet-base''': '''https://huggingface.co/microsoft/mpnet-base/resolve/main/vocab.txt''',
    },
    '''tokenizer_file''': {
        '''microsoft/mpnet-base''': '''https://huggingface.co/microsoft/mpnet-base/resolve/main/tokenizer.json''',
    },
}

a  :						str					       =							{
    '''microsoft/mpnet-base''': 512,
}

a  :						Union[str, Any]					       =							{
    '''microsoft/mpnet-base''': {'''do_lower_case''': True},
}


class 	_UpperCamelCase ( __UpperCamelCase							):



    '''simple docstring'''


    __lowercase    :   Optional[Any]											=						VOCAB_FILES_NAMES
    __lowercase    :   Optional[int]											=						PRETRAINED_VOCAB_FILES_MAP
    __lowercase    :   Dict											=						PRETRAINED_INIT_CONFIGURATION
    __lowercase    :   Optional[Any]											=						PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
    __lowercase    :   Dict											=						MPNetTokenizer
    __lowercase    :   Dict											=						['input_ids', 'attention_mask']


    def __init__(      self   ,			__lowercase=None   ,			__lowercase=None   ,			__lowercase=True   ,			__lowercase="<s>"   ,			__lowercase="</s>"   ,			__lowercase="</s>"   ,			__lowercase="<s>"   ,			__lowercase="[UNK]"   ,			__lowercase="<pad>"   ,			__lowercase="<mask>"   ,			__lowercase=True   ,			__lowercase=None   ,			**__lowercase   ,			):
     super().__init__(
         __lowercase   ,			tokenizer_file=__lowercase   ,			do_lower_case=__lowercase   ,			bos_token=__lowercase   ,			eos_token=__lowercase   ,			sep_token=__lowercase   ,			cls_token=__lowercase   ,			unk_token=__lowercase   ,			pad_token=__lowercase   ,			mask_token=__lowercase   ,			tokenize_chinese_chars=__lowercase   ,			strip_accents=__lowercase   ,			**__lowercase   ,			)

     UpperCAmelCase__ 						=				json.loads(self.backend_tokenizer.normalizer.__getstate__()			)
     if (
         pre_tok_state.get("""lowercase"""   ,			__lowercase			) != do_lower_case
         or pre_tok_state.get("""strip_accents"""   ,			__lowercase			) != strip_accents
     ):
      UpperCAmelCase__ 						=				getattr(__lowercase   ,			pre_tok_state.pop("""type"""			)			)
      UpperCAmelCase__ 						=				do_lower_case
      UpperCAmelCase__ 						=				strip_accents
      UpperCAmelCase__ 						=				pre_tok_class(**__lowercase			)

     UpperCAmelCase__ 						=				do_lower_case


    @property
    def 		A__      (      self			):
     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 		A__      (      self   ,			__lowercase			):
     UpperCAmelCase__ 						=				AddedToken(__lowercase   ,			lstrip=__lowercase   ,			rstrip=__lowercase			) if isinstance(__lowercase   ,			__lowercase			) else value
     UpperCAmelCase__ 						=				value


    def 		A__      (      self   ,			__lowercase   ,			__lowercase=None			):
     UpperCAmelCase__ 						=				[self.bos_token_id] + token_ids_a + [self.eos_token_id]
     if token_ids_a is None:
      return output

     return output + [self.eos_token_id] + token_ids_a + [self.eos_token_id]


    def 		A__      (      self   ,			__lowercase   ,			__lowercase = None			):
     UpperCAmelCase__ 						=				[self.sep_token_id]
     UpperCAmelCase__ 						=				[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 + sep + token_ids_a + sep			) * [0]






    def 		A__      (      self   ,			__lowercase   ,			__lowercase = None			):
     UpperCAmelCase__ 						=				self._tokenizer.model.save(__lowercase   ,			name=__lowercase			)
     return tuple(__lowercase			)



