


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





							'''simple docstring'''


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






							def UpperCAmelCase_     (   self  ):
											_SCREAMING_SNAKE_CASE			:	Tuple							=				ids_tensor([self.batch_size, self.seq_length]   , self.vocab_size  )

											_SCREAMING_SNAKE_CASE			:	Any							=				None
											if self.use_input_mask:
															_SCREAMING_SNAKE_CASE			:	int							=				random_attention_mask([self.batch_size, self.seq_length]  )

											_SCREAMING_SNAKE_CASE			:	int							=				None
											_SCREAMING_SNAKE_CASE			:	Union[str, Any]							=				None
											_SCREAMING_SNAKE_CASE			:	List[str]							=				None
											if self.use_labels:
															_SCREAMING_SNAKE_CASE			:	Union[str, Any]							=				ids_tensor([self.batch_size]   , self.type_sequence_label_size  )
															_SCREAMING_SNAKE_CASE			:	str							=				ids_tensor([self.batch_size, self.seq_length]   , self.num_labels  )
															_SCREAMING_SNAKE_CASE			:	Tuple							=				ids_tensor([self.batch_size]   , self.num_choices  )

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






							def UpperCAmelCase_     (   self   , __snake_case   , __snake_case   , __snake_case   , __snake_case   , __snake_case   , __snake_case  ):
											_SCREAMING_SNAKE_CASE			:	Union[str, Any]							=				TFMPNetForMaskedLM(config=__snake_case  )
											_SCREAMING_SNAKE_CASE			:	Tuple							=				{"""input_ids""": input_ids, """attention_mask""": input_mask}
											_SCREAMING_SNAKE_CASE			:	Optional[Any]							=				model(__snake_case  )
											self.parent.assertEqual(result.logits.shape   , (self.batch_size, self.seq_length, self.vocab_size)  )






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






							def UpperCAmelCase_     (   self   , __snake_case   , __snake_case   , __snake_case   , __snake_case   , __snake_case   , __snake_case  ):
											_SCREAMING_SNAKE_CASE			:	List[Any]							=				self.num_choices
											_SCREAMING_SNAKE_CASE			:	str							=				TFMPNetForMultipleChoice(__snake_case  )
											_SCREAMING_SNAKE_CASE			:	str							=				tf.tile(tf.expand_dims(__snake_case   , 1  )   , (1, self.num_choices, 1)  )
											_SCREAMING_SNAKE_CASE			:	str							=				tf.tile(tf.expand_dims(__snake_case   , 1  )   , (1, self.num_choices, 1)  )
											_SCREAMING_SNAKE_CASE			:	List[str]							=				{
											    """input_ids""": multiple_choice_inputs_ids,
											    """attention_mask""": multiple_choice_input_mask,
											}
											_SCREAMING_SNAKE_CASE			:	Optional[int]							=				model(__snake_case  )
											self.parent.assertEqual(result.logits.shape   , (self.batch_size, self.num_choices)  )






							def UpperCAmelCase_     (   self   , __snake_case   , __snake_case   , __snake_case   , __snake_case   , __snake_case   , __snake_case  ):
											_SCREAMING_SNAKE_CASE			:	str							=				self.num_labels
											_SCREAMING_SNAKE_CASE			:	int							=				TFMPNetForTokenClassification(__snake_case  )
											_SCREAMING_SNAKE_CASE			:	List[str]							=				{"""input_ids""": input_ids, """attention_mask""": input_mask}
											_SCREAMING_SNAKE_CASE			:	Dict							=				model(__snake_case  )
											self.parent.assertEqual(result.logits.shape   , (self.batch_size, self.seq_length, self.num_labels)  )






							def UpperCAmelCase_     (   self  ):
											_SCREAMING_SNAKE_CASE			:	str							=				self.prepare_config_and_inputs()
											((_SCREAMING_SNAKE_CASE)		,	(_SCREAMING_SNAKE_CASE)		,	(_SCREAMING_SNAKE_CASE)		,	(_SCREAMING_SNAKE_CASE)		,	(_SCREAMING_SNAKE_CASE)		,	(_SCREAMING_SNAKE_CASE))			:	Optional[int]							=				config_and_inputs
											_SCREAMING_SNAKE_CASE			:	List[str]							=				{"""input_ids""": input_ids, """attention_mask""": input_mask}
											return config, inputs_dict





@require_tf
class    lowercase__  (						_snake_case					,					_snake_case					,					unittest.TestCase ):





							'''simple docstring'''


							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_							:				int				     =  False
							A_							:				Optional[int]				     =  False






							def UpperCAmelCase_     (   self  ):
											_SCREAMING_SNAKE_CASE			:	List[str]							=				TFMPNetModelTester(self  )
											_SCREAMING_SNAKE_CASE			:	Dict							=				ConfigTester(self   , config_class=__snake_case   , hidden_size=37  )






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






							def UpperCAmelCase_     (   self  ):
											_SCREAMING_SNAKE_CASE			:	str							=				self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_mpnet_model(*__snake_case  )






							def UpperCAmelCase_     (   self  ):
											_SCREAMING_SNAKE_CASE			:	Dict							=				self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_mpnet_for_masked_lm(*__snake_case  )






							def UpperCAmelCase_     (   self  ):
											_SCREAMING_SNAKE_CASE			:	Optional[Any]							=				self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_mpnet_for_question_answering(*__snake_case  )






							def UpperCAmelCase_     (   self  ):
											_SCREAMING_SNAKE_CASE			:	Any							=				self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_mpnet_for_sequence_classification(*__snake_case  )






							def UpperCAmelCase_     (   self  ):
											_SCREAMING_SNAKE_CASE			:	Union[str, Any]							=				self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_mpnet_for_multiple_choice(*__snake_case  )






							def UpperCAmelCase_     (   self  ):
											_SCREAMING_SNAKE_CASE			:	List[str]							=				self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_mpnet_for_token_classification(*__snake_case  )






							@slow
							def UpperCAmelCase_     (   self  ):
											for model_name in ["microsoft/mpnet-base"]:
															_SCREAMING_SNAKE_CASE			:	List[Any]							=				TFMPNetModel.from_pretrained(__snake_case  )
															self.assertIsNotNone(__snake_case  )





@require_tf
class    lowercase__  (						unittest.TestCase ):





							'''simple docstring'''


							@slow
							def UpperCAmelCase_     (   self  ):
											_SCREAMING_SNAKE_CASE			:	List[Any]							=				TFMPNetModel.from_pretrained("""microsoft/mpnet-base"""  )
											_SCREAMING_SNAKE_CASE			:	Optional[int]							=				tf.constant([[0, 1, 2, 3, 4, 5]]  )
											_SCREAMING_SNAKE_CASE			:	List[Any]							=				model(__snake_case  )[0]

											_SCREAMING_SNAKE_CASE			:	Union[str, Any]							=				[1, 6, 768]
											self.assertEqual(output.shape   , __snake_case  )

											_SCREAMING_SNAKE_CASE			:	Union[str, Any]							=				tf.constant(
											    [
											        [
											            [-0.1067172, 0.08216473, 0.0024543],
											            [-0.03465879, 0.8354118, -0.03252288],
											            [-0.06569476, -0.12424111, -0.0494436],
											        ]
											    ]  )
											tf.debugging.assert_near(output[:, :3, :3]   , __snake_case   , atol=1e-4  )








'''simple docstring'''


import os
import tempfile
import unittest
from typing import List

from transformers.models.esm.tokenization_esm import VOCAB_FILES_NAMES, EsmTokenizer
from transformers.testing_utils import require_tokenizers
from transformers.tokenization_utils import PreTrainedTokenizer
from transformers.tokenization_utils_base import PreTrainedTokenizerBase





@require_tokenizers
class    lowercase__  (						unittest.TestCase ):





							'''simple docstring'''


							A_							:				Optional[Any]				     =  EsmTokenizer






							def UpperCAmelCase_     (   self  ):
											super().setUp()
											_SCREAMING_SNAKE_CASE			:	List[Any]							=				tempfile.mkdtemp()
											# fmt: off
											_SCREAMING_SNAKE_CASE			:	List[str]							=				["<cls>", "<pad>", "<eos>", "<unk>", "L", "A", "G", "V", "S", "E", "R", "T", "I", "D", "P", "K", "Q", "N", "F", "Y", "M", "H", "W", "C", "X", "B", "U", "Z", "O", ".", "-", "<null_1>", "<mask>"]  # noqa: E501
											# fmt: on
											_SCREAMING_SNAKE_CASE			:	Any							=				os.path.join(self.tmpdirname   , VOCAB_FILES_NAMES["""vocab_file"""]  )
											with open(self.vocab_file   , """w"""   , encoding="""utf-8"""  ) as vocab_writer:
															vocab_writer.write("""""".join([x + """\n""" for x in vocab_tokens]  )  )






							def UpperCAmelCase_     (   self   , **__snake_case  ):
											return [self.get_tokenizer(**__snake_case  )]






							def UpperCAmelCase_     (   self   , **__snake_case  ):
											return self.tokenizer_class.from_pretrained(self.tmpdirname   , **__snake_case  )






							def UpperCAmelCase_     (   self  ):
											_SCREAMING_SNAKE_CASE			:	List[str]							=				self.tokenizer_class(self.vocab_file  )

											_SCREAMING_SNAKE_CASE			:	int							=				tokenizer.tokenize("""LAGVS"""  )
											self.assertListEqual(__snake_case   , ["""L""", """A""", """G""", """V""", """S"""]  )
											self.assertListEqual(tokenizer.convert_tokens_to_ids(__snake_case  )   , [4, 5, 6, 7, 8]  )






							def UpperCAmelCase_     (   self  ):
											_SCREAMING_SNAKE_CASE			:	int							=				self.tokenizer_class(self.vocab_file  )

											_SCREAMING_SNAKE_CASE			:	Optional[Any]							=				"""LAGVS"""
											self.assertListEqual(tokenizer.encode(__snake_case  )   , [0, 4, 5, 6, 7, 8, 2]  )






							def UpperCAmelCase_     (   self  ):
											_SCREAMING_SNAKE_CASE			:	Any							=				self.tokenizer_class(self.vocab_file  )

											_SCREAMING_SNAKE_CASE			:	List[Any]							=				["""LAGVS""", """WCB"""]
											_SCREAMING_SNAKE_CASE			:	Union[str, Any]							=				tokenizer(__snake_case   , padding=__snake_case  )["""input_ids"""]

											self.assertListEqual(__snake_case   , [[0, 4, 5, 6, 7, 8, 2], [0, 22, 23, 25, 2]]  )






							def UpperCAmelCase_     (   self  ):
											_SCREAMING_SNAKE_CASE			:	Union[str, Any]							=				self.tokenizer_class(self.vocab_file  )

											_SCREAMING_SNAKE_CASE			:	str							=				["""LAGVS""", """WCB"""]
											_SCREAMING_SNAKE_CASE			:	Optional[int]							=				tokenizer(__snake_case   , padding=__snake_case  )["""input_ids"""]

											self.assertListEqual(__snake_case   , [[0, 4, 5, 6, 7, 8, 2], [0, 22, 23, 25, 2, 1, 1]]  )






							def UpperCAmelCase_     (   self  ):
											_SCREAMING_SNAKE_CASE			:	str							=				self.get_tokenizers(fast=__snake_case  )
											for tokenizer in tokenizers:
															with self.subTest(f"""{tokenizer.__class__.__name__}"""  ):
																			_SCREAMING_SNAKE_CASE			:	Optional[int]							=				"""<unk>"""
																			_SCREAMING_SNAKE_CASE			:	Optional[int]							=				"""<mask>"""

																			_SCREAMING_SNAKE_CASE			:	Union[str, Any]							=				tokenizer.tokenize(__snake_case  )
																			_SCREAMING_SNAKE_CASE			:	Optional[Any]							=				tokenizer.tokenize(__snake_case  )

																			self.assertEqual(len(__snake_case  )   , 1  )
																			self.assertEqual(len(__snake_case  )   , 1  )
																			self.assertEqual(token_a[0]   , __snake_case  )
																			self.assertEqual(token_a[0]   , __snake_case  )




