



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





	"""simple docstring"""




	def __init__(self						,			lowerCamelCase_						,			lowerCamelCase_=13						,			lowerCamelCase_=7						,			lowerCamelCase_=True						,			lowerCamelCase_=True						,			lowerCamelCase_=False						,			lowerCamelCase_=True						,			lowerCamelCase_=99						,			lowerCamelCase_=64						,			lowerCamelCase_=2						,			lowerCamelCase_=4						,			lowerCamelCase_=64						,			lowerCamelCase_="gelu"						,			lowerCamelCase_=0.1						,			lowerCamelCase_=0.1						,			lowerCamelCase_=512						,			lowerCamelCase_=16						,			lowerCamelCase_=2						,			lowerCamelCase_=0.02						,			lowerCamelCase_=3						,			lowerCamelCase_=4						,			lowerCamelCase_=None						,			):
						"""simple docstring"""

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




	def 	UpperCamelCase_			(self						):
						"""simple docstring"""

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

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

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

						a   	=  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						,			lowerCamelCase_						,			lowerCamelCase_						,			lowerCamelCase_						,			lowerCamelCase_						,			lowerCamelCase_						,			lowerCamelCase_						):
						"""simple docstring"""

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




	def 	UpperCamelCase_			(self						,			lowerCamelCase_						,			lowerCamelCase_						,			lowerCamelCase_						,			lowerCamelCase_						,			lowerCamelCase_						,			lowerCamelCase_						):
						"""simple docstring"""

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




	def 	UpperCamelCase_			(self						,			lowerCamelCase_						,			lowerCamelCase_						,			lowerCamelCase_						,			lowerCamelCase_						,			lowerCamelCase_						,			lowerCamelCase_						):
						"""simple docstring"""

						a   	=  TFMPNetForQuestionAnswering(config=lowerCamelCase_						)
						a   	=  {
						    "input_ids": input_ids,
						    "attention_mask": input_mask,
						}
						a   	=  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 	UpperCamelCase_			(self						,			lowerCamelCase_						,			lowerCamelCase_						,			lowerCamelCase_						,			lowerCamelCase_						,			lowerCamelCase_						,			lowerCamelCase_						):
						"""simple docstring"""

						a   	=  self.num_labels
						a   	=  TFMPNetForSequenceClassification(lowerCamelCase_						)
						a   	=  {"input_ids": input_ids, "attention_mask": input_mask}
						a   	=  model(lowerCamelCase_						)
						self.parent.assertEqual(result.logits.shape						,			(self.batch_size, self.num_labels)						)




	def 	UpperCamelCase_			(self						,			lowerCamelCase_						,			lowerCamelCase_						,			lowerCamelCase_						,			lowerCamelCase_						,			lowerCamelCase_						,			lowerCamelCase_						):
						"""simple docstring"""

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




	def 	UpperCamelCase_			(self						,			lowerCamelCase_						,			lowerCamelCase_						,			lowerCamelCase_						,			lowerCamelCase_						,			lowerCamelCase_						,			lowerCamelCase_						):
						"""simple docstring"""

						a   	=  self.num_labels
						a   	=  TFMPNetForTokenClassification(lowerCamelCase_						)
						a   	=  {"input_ids": input_ids, "attention_mask": input_mask}
						a   	=  model(lowerCamelCase_						)
						self.parent.assertEqual(result.logits.shape						,			(self.batch_size, self.seq_length, self.num_labels)						)




	def 	UpperCamelCase_			(self						):
						"""simple docstring"""

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




@require_tf
class       _lowercase				(  lowerCAmelCase, lowerCAmelCase, unittest.TestCase      ):





	"""simple docstring"""
	__A		      =				(
	    (
	        TFMPNetForMaskedLM,
	        TFMPNetForMultipleChoice,
	        TFMPNetForQuestionAnswering,
	        TFMPNetForSequenceClassification,
	        TFMPNetForTokenClassification,
	        TFMPNetModel,
	    )
	    if is_tf_available()
	    else ()
	)
	__A		      =				(
	    {
	        "feature-extraction": TFMPNetModel,
	        "fill-mask": TFMPNetForMaskedLM,
	        "question-answering": TFMPNetForQuestionAnswering,
	        "text-classification": TFMPNetForSequenceClassification,
	        "token-classification": TFMPNetForTokenClassification,
	        "zero-shot": TFMPNetForSequenceClassification,
	    }
	    if is_tf_available()
	    else {}
	)
	__A		      =				False
	__A		      =				False




	def 	UpperCamelCase_			(self						):
						"""simple docstring"""

						a   	=  TFMPNetModelTester(self						)
						a   	=  ConfigTester(self						,			config_class=lowerCamelCase_						,			hidden_size=37						)




	def 	UpperCamelCase_			(self						):
						"""simple docstring"""

						self.config_tester.run_common_tests()




	def 	UpperCamelCase_			(self						):
						"""simple docstring"""

						a   	=  self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_mpnet_model(*lowerCamelCase_						)




	def 	UpperCamelCase_			(self						):
						"""simple docstring"""

						a   	=  self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_mpnet_for_masked_lm(*lowerCamelCase_						)




	def 	UpperCamelCase_			(self						):
						"""simple docstring"""

						a   	=  self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_mpnet_for_question_answering(*lowerCamelCase_						)




	def 	UpperCamelCase_			(self						):
						"""simple docstring"""

						a   	=  self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_mpnet_for_sequence_classification(*lowerCamelCase_						)




	def 	UpperCamelCase_			(self						):
						"""simple docstring"""

						a   	=  self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_mpnet_for_multiple_choice(*lowerCamelCase_						)




	def 	UpperCamelCase_			(self						):
						"""simple docstring"""

						a   	=  self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_mpnet_for_token_classification(*lowerCamelCase_						)




	@slow
	def 	UpperCamelCase_			(self						):
						"""simple docstring"""

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




@require_tf
class       _lowercase				(  unittest.TestCase      ):





	"""simple docstring"""




	@slow
	def 	UpperCamelCase_			(self						):
						"""simple docstring"""

						a   	=  TFMPNetModel.from_pretrained("microsoft/mpnet-base"						)
						a   	=  tf.constant([[0, 1, 2, 3, 4, 5]]						)
						a   	=  model(lowerCamelCase_						)[0]

						a   	=  [1, 6, 768]
						self.assertEqual(output.shape						,			lowerCamelCase_						)

						a   	=  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						)





from typing import Dict, List, Optional, Tuple

from ...tokenization_utils import AddedToken, PreTrainedTokenizer
from ...utils import logging


_lowercase:					int      =						logging.get_logger(__name__)



class       _lowercase				(  lowerCAmelCase      ):





	"""simple docstring"""
	__A		      =				["input_ids", "attention_mask"]




	def __init__(self						,			lowerCamelCase_="[PAD]"						,			lowerCamelCase_="[BOS]"						,			lowerCamelCase_="[EOS]"						,			lowerCamelCase_="[MASK]"						,			lowerCamelCase_="[CLS]"						,			lowerCamelCase_="[SEP]"						,			lowerCamelCase_=2048						,			**lowerCamelCase_						,			):
						"""simple docstring"""

						a   	=  AddedToken(lowerCamelCase_						,			lstrip=lowerCamelCase_						,			rstrip=lowerCamelCase_						) if isinstance(lowerCamelCase_						,			lowerCamelCase_						) else pad_token
						a   	=  AddedToken(lowerCamelCase_						,			lstrip=lowerCamelCase_						,			rstrip=lowerCamelCase_						) if isinstance(lowerCamelCase_						,			lowerCamelCase_						) else bos_token
						a   	=  AddedToken(lowerCamelCase_						,			lstrip=lowerCamelCase_						,			rstrip=lowerCamelCase_						) if isinstance(lowerCamelCase_						,			lowerCamelCase_						) else eos_token
						a   	=  AddedToken(lowerCamelCase_						,			lstrip=lowerCamelCase_						,			rstrip=lowerCamelCase_						) if isinstance(lowerCamelCase_						,			lowerCamelCase_						) else mask_token
						a   	=  AddedToken(lowerCamelCase_						,			lstrip=lowerCamelCase_						,			rstrip=lowerCamelCase_						) if isinstance(lowerCamelCase_						,			lowerCamelCase_						) else cls_token
						a   	=  AddedToken(lowerCamelCase_						,			lstrip=lowerCamelCase_						,			rstrip=lowerCamelCase_						) if isinstance(lowerCamelCase_						,			lowerCamelCase_						) else sep_token

						super().__init__(
						    pad_token=lowerCamelCase_						,			bos_token=lowerCamelCase_						,			eos_token=lowerCamelCase_						,			mask_token=lowerCamelCase_						,			cls_token=lowerCamelCase_						,			sep_token=lowerCamelCase_						,			model_max_length=lowerCamelCase_						,			**lowerCamelCase_						,			)

						a   	=  2**8  # utf is 8 bits

						# define special tokens dict
						a   	=  {
						    self.pad_token: 0,
						    self.bos_token: 1,
						    self.eos_token: 2,
						    self.mask_token: 3,
						    self.cls_token: 4,
						    self.sep_token: 5,
						}
						a   	=  len(self.special_tokens_encoder						)
						a   	=  {v: k for k, v in self.special_tokens_encoder.items()}




	def 	UpperCamelCase_			(self						):
						"""simple docstring"""

						a   	=  self.special_tokens_encoder.copy()
						vocab.update(self.added_tokens_encoder						)
						for i in range(self._utf_vocab_size						):
											a   	=  chr(lowerCamelCase_						)
											a   	=  i + len(self.special_tokens_encoder						)
						return vocab




	@property
	def 	UpperCamelCase_			(self						):
						"""simple docstring"""

						return self._utf_vocab_size + self._num_special_tokens




	def 	UpperCamelCase_			(self						,			lowerCamelCase_						,			lowerCamelCase_ = None						,			lowerCamelCase_ = False						):
						"""simple docstring"""

						if already_has_special_tokens:
											return super().get_special_tokens_mask(
											    token_ids_a=lowerCamelCase_						,			token_ids_a=lowerCamelCase_						,			already_has_special_tokens=lowerCamelCase_						)

						# normal case: some special tokens
						if token_ids_a is None:
											return [1] + [0] * len(lowerCamelCase_						) + [1]
						return [1] + ([0] * len(lowerCamelCase_						)) + [1] + ([0] * len(lowerCamelCase_						)) + [1]




	def 	UpperCamelCase_			(self						,			lowerCamelCase_						,			lowerCamelCase_ = None						):
						"""simple docstring"""

						if token_ids_a is None:
											return [self.cls_token_id] + token_ids_a + [self.sep_token_id]
						else:
											return [self.cls_token_id] + token_ids_a + [self.sep_token_id] + token_ids_a + [self.sep_token_id]




	def 	UpperCamelCase_			(self						,			lowerCamelCase_						):
						"""simple docstring"""

						a   	=  [chr(lowerCamelCase_						) for i in text.encode("utf-8"						)]
						return tokens




	def 	UpperCamelCase_			(self						,			lowerCamelCase_						):
						"""simple docstring"""

						if token in self.special_tokens_encoder:
											a   	=  self.special_tokens_encoder[token]
						elif token in self.added_tokens_encoder:
											a   	=  self.added_tokens_encoder[token]
						elif len(lowerCamelCase_						) != 1:
											a   	=  self.unk_token_id
						else:
											a   	=  ord(lowerCamelCase_						) + self._num_special_tokens
						return token_id




	def 	UpperCamelCase_			(self						,			lowerCamelCase_						):
						"""simple docstring"""

						if index in self.special_tokens_decoder:
											a   	=  self.special_tokens_decoder[index]
						elif index in self.added_tokens_decoder:
											a   	=  self.added_tokens_decoder[index]
						else:
											a   	=  chr(index - self._num_special_tokens						)
						return token




	def 	UpperCamelCase_			(self						,			lowerCamelCase_						):
						"""simple docstring"""

						a   	=  B""
						for token in tokens:
											if token in self.special_tokens_decoder:
																a   	=  self.special_tokens_decoder[token].encode("utf-8"						)
											elif token in self.added_tokens_decoder:
																a   	=  self.special_tokens_decoder[token].encode("utf-8"						)
											elif token in self.special_tokens_encoder:
																a   	=  token.encode("utf-8"						)
											elif token in self.added_tokens_encoder:
																a   	=  token.encode("utf-8"						)
											else:
																a   	=  bytes([ord(lowerCamelCase_						)]						)
											bstring += tok_string
						a   	=  bstring.decode("utf-8"						,			errors="replace"						)
						return string




	def 	UpperCamelCase_			(self						,			lowerCamelCase_						,			lowerCamelCase_ = None						):
						"""simple docstring"""

						return ()
