
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       :List[Any]		,						lowerCAmelCase__       :Optional[Any]		,						lowerCAmelCase__       :Tuple=13		,						lowerCAmelCase__       :List[str]=7		,						lowerCAmelCase__       :Any=True		,						lowerCAmelCase__       :Tuple=True		,						lowerCAmelCase__       :Any=False		,						lowerCAmelCase__       :str=True		,						lowerCAmelCase__       :Any=99		,						lowerCAmelCase__       :Optional[int]=64		,						lowerCAmelCase__       :List[Any]=2		,						lowerCAmelCase__       :Union[str, Any]=4		,						lowerCAmelCase__       :Any=64		,						lowerCAmelCase__       :Dict="gelu"		,						lowerCAmelCase__       :Union[str, Any]=0.1		,						lowerCAmelCase__       :Optional[int]=0.1		,						lowerCAmelCase__       :Union[str, Any]=512		,						lowerCAmelCase__       :Optional[Any]=16		,						lowerCAmelCase__       :str=2		,						lowerCAmelCase__       :Any=0.02		,						lowerCAmelCase__       :Dict=3		,						lowerCAmelCase__       :List[str]=4		,						lowerCAmelCase__       :str=None		,						)     ->Any:
										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 					SCREAMING_SNAKE_CASE( self       :str    )     ->int:
										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 					SCREAMING_SNAKE_CASE( self       :Tuple		,						lowerCAmelCase__       :List[str]		,						lowerCAmelCase__       :Union[str, Any]		,						lowerCAmelCase__       :List[str]		,						lowerCAmelCase__       :Any		,						lowerCAmelCase__       :Any		,						lowerCAmelCase__       :Optional[int]    )     ->Optional[Any]:
										lowercase						    =			TFMPNetModel(config=lowerCAmelCase__    )
										lowercase						    =			{"input_ids": input_ids, "attention_mask": input_mask}
										lowercase						    =			model(lowerCAmelCase__    )
										lowercase						    =			[input_ids, input_mask]
										lowercase						    =			model(lowerCAmelCase__    )
										self.parent.assertEqual(result.last_hidden_state.shape		,						(self.batch_size, self.seq_length, self.hidden_size)    )





						def 					SCREAMING_SNAKE_CASE( self       :List[Any]		,						lowerCAmelCase__       :Union[str, Any]		,						lowerCAmelCase__       :Tuple		,						lowerCAmelCase__       :Union[str, Any]		,						lowerCAmelCase__       :str		,						lowerCAmelCase__       :List[str]		,						lowerCAmelCase__       :Tuple    )     ->Tuple:
										lowercase						    =			TFMPNetForMaskedLM(config=lowerCAmelCase__    )
										lowercase						    =			{"input_ids": input_ids, "attention_mask": input_mask}
										lowercase						    =			model(lowerCAmelCase__    )
										self.parent.assertEqual(result.logits.shape		,						(self.batch_size, self.seq_length, self.vocab_size)    )





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





						def 					SCREAMING_SNAKE_CASE( self       :Union[str, Any]		,						lowerCAmelCase__       :List[str]		,						lowerCAmelCase__       :Union[str, Any]		,						lowerCAmelCase__       :Dict		,						lowerCAmelCase__       :List[Any]		,						lowerCAmelCase__       :Tuple		,						lowerCAmelCase__       :List[str]    )     ->Tuple:
										lowercase						    =			self.num_choices
										lowercase						    =			TFMPNetForMultipleChoice(lowerCAmelCase__    )
										lowercase						    =			tf.tile(tf.expand_dims(lowerCAmelCase__		,						1    )		,						(1, self.num_choices, 1)    )
										lowercase						    =			tf.tile(tf.expand_dims(lowerCAmelCase__		,						1    )		,						(1, self.num_choices, 1)    )
										lowercase						    =			{
										    "input_ids": multiple_choice_inputs_ids,
										    "attention_mask": multiple_choice_input_mask,
										}
										lowercase						    =			model(lowerCAmelCase__    )
										self.parent.assertEqual(result.logits.shape		,						(self.batch_size, self.num_choices)    )





						def 					SCREAMING_SNAKE_CASE( self       :Any		,						lowerCAmelCase__       :Dict		,						lowerCAmelCase__       :List[str]		,						lowerCAmelCase__       :Dict		,						lowerCAmelCase__       :Optional[Any]		,						lowerCAmelCase__       :Tuple		,						lowerCAmelCase__       :Tuple    )     ->Tuple:
										lowercase						    =			self.num_labels
										lowercase						    =			TFMPNetForTokenClassification(lowerCAmelCase__    )
										lowercase						    =			{"input_ids": input_ids, "attention_mask": input_mask}
										lowercase						    =			model(lowerCAmelCase__    )
										self.parent.assertEqual(result.logits.shape		,						(self.batch_size, self.seq_length, self.num_labels)    )





						def 					SCREAMING_SNAKE_CASE( self       :Union[str, Any]    )     ->Optional[int]:
										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_			(			__a		,						__a		,						unittest.TestCase		):







						'''simple docstring'''



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





						def 					SCREAMING_SNAKE_CASE( self       :Tuple    )     ->Optional[Any]:
										lowercase						    =			TFMPNetModelTester(self    )
										lowercase						    =			ConfigTester(self		,						config_class=lowerCAmelCase__		,						hidden_size=37    )





						def 					SCREAMING_SNAKE_CASE( self       :List[Any]    )     ->str:
										self.config_tester.run_common_tests()





						def 					SCREAMING_SNAKE_CASE( self       :List[Any]    )     ->Any:
										lowercase						    =			self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_mpnet_model(*lowerCAmelCase__    )





						def 					SCREAMING_SNAKE_CASE( self       :Tuple    )     ->Dict:
										lowercase						    =			self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_mpnet_for_masked_lm(*lowerCAmelCase__    )





						def 					SCREAMING_SNAKE_CASE( self       :List[str]    )     ->Tuple:
										lowercase						    =			self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_mpnet_for_question_answering(*lowerCAmelCase__    )





						def 					SCREAMING_SNAKE_CASE( self       :Tuple    )     ->Tuple:
										lowercase						    =			self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_mpnet_for_sequence_classification(*lowerCAmelCase__    )





						def 					SCREAMING_SNAKE_CASE( self       :str    )     ->Dict:
										lowercase						    =			self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_mpnet_for_multiple_choice(*lowerCAmelCase__    )





						def 					SCREAMING_SNAKE_CASE( self       :int    )     ->List[str]:
										lowercase						    =			self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_mpnet_for_token_classification(*lowerCAmelCase__    )





						@slow
						def 					SCREAMING_SNAKE_CASE( self       :str    )     ->List[str]:
										for model_name in ["microsoft/mpnet-base"]:
														lowercase						    =			TFMPNetModel.from_pretrained(lowerCAmelCase__    )
														self.assertIsNotNone(lowerCAmelCase__    )





@require_tf
class      UpperCamelCase_			(			unittest.TestCase		):







						'''simple docstring'''



						@slow
						def 					SCREAMING_SNAKE_CASE( self       :Dict    )     ->Optional[int]:
										lowercase						    =			TFMPNetModel.from_pretrained("microsoft/mpnet-base"    )
										lowercase						    =			tf.constant([[0, 1, 2, 3, 4, 5]]    )
										lowercase						    =			model(lowerCAmelCase__    )[0]

										lowercase						    =			[1, 6, 768]
										self.assertEqual(output.shape		,						lowerCAmelCase__    )

										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]		,						lowerCAmelCase__		,						atol=1E-4    )


from ...configuration_utils import PretrainedConfig
from ...utils import logging


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

_snake_case			:       Dict      =						{
    "google/reformer-crime-and-punishment": (
        "https://huggingface.co/google/reformer-crime-and-punishment/resolve/main/config.json"
    ),
    "google/reformer-enwik8": "https://huggingface.co/google/reformer-enwik8/resolve/main/config.json",
}





class      UpperCamelCase_			(			__a		):







						'''simple docstring'''



						UpperCamelCase			:     Any							      =				'''reformer'''
						UpperCamelCase			:     List[str]							      =				['''past_buckets_states''']
						UpperCamelCase			:     Optional[int]							      =				{}





						def __init__( self       :Dict		,						lowerCAmelCase__       :Any=64		,						lowerCAmelCase__       :str=["local", "lsh", "local", "lsh", "local", "lsh"]		,						lowerCAmelCase__       :Optional[Any]=1.0		,						lowerCAmelCase__       :List[str]=True		,						lowerCAmelCase__       :Optional[Any]=[64, 64]		,						lowerCAmelCase__       :Tuple=[64, 192]		,						lowerCAmelCase__       :Union[str, Any]=0		,						lowerCAmelCase__       :Optional[Any]=2		,						lowerCAmelCase__       :List[str]=512		,						lowerCAmelCase__       :Any=None		,						lowerCAmelCase__       :int="relu"		,						lowerCAmelCase__       :Tuple=0.05		,						lowerCAmelCase__       :int=256		,						lowerCAmelCase__       :Optional[Any]=0.02		,						lowerCAmelCase__       :Optional[int]=False		,						lowerCAmelCase__       :Optional[Any]=1E-1_2		,						lowerCAmelCase__       :int=1		,						lowerCAmelCase__       :Optional[Any]=0		,						lowerCAmelCase__       :Any=0.05		,						lowerCAmelCase__       :Optional[int]=64		,						lowerCAmelCase__       :List[Any]=64		,						lowerCAmelCase__       :Tuple=0.0		,						lowerCAmelCase__       :Tuple=1		,						lowerCAmelCase__       :List[str]=0		,						lowerCAmelCase__       :Dict=4096		,						lowerCAmelCase__       :Tuple=12		,						lowerCAmelCase__       :Tuple=None		,						lowerCAmelCase__       :Union[str, Any]=1		,						lowerCAmelCase__       :int=0		,						lowerCAmelCase__       :Optional[Any]=320		,						lowerCAmelCase__       :Optional[Any]=False		,						lowerCAmelCase__       :Optional[int]=True		,						lowerCAmelCase__       :str=None		,						**lowerCAmelCase__       :Any		,						)     ->Any:
										lowercase						    =			hash_seed
										lowercase						    =			vocab_size
										lowercase						    =			attention_head_size
										lowercase						    =			hidden_size
										lowercase						    =			num_attention_heads
										lowercase						    =			num_hashes
										lowercase						    =			len(lowerCAmelCase__    )
										lowercase						    =			tuple(lowerCAmelCase__    ) if isinstance(lowerCAmelCase__		,						lowerCAmelCase__    ) else num_buckets
										lowercase						    =			lsh_attn_chunk_length
										lowercase						    =			local_attn_chunk_length
										lowercase						    =			lsh_num_chunks_after
										lowercase						    =			lsh_num_chunks_before
										lowercase						    =			local_num_chunks_after
										lowercase						    =			local_num_chunks_before
										lowercase						    =			hidden_act
										lowercase						    =			feed_forward_size
										lowercase						    =			hidden_dropout_prob
										lowercase						    =			lsh_attention_probs_dropout_prob
										lowercase						    =			local_attention_probs_dropout_prob
										lowercase						    =			max_position_embeddings
										lowercase						    =			initializer_range
										lowercase						    =			layer_norm_eps
										lowercase						    =			axial_pos_embds
										lowercase						    =			tuple(lowerCAmelCase__    )
										lowercase						    =			tuple(lowerCAmelCase__    )
										lowercase						    =			axial_norm_std
										lowercase						    =			chunk_size_lm_head
										lowercase						    =			attn_layers
										lowercase						    =			use_cache
										lowercase						    =			classifier_dropout
										super().__init__(
										    pad_token_id=lowerCAmelCase__		,						eos_token_id=lowerCAmelCase__		,						is_decoder=lowerCAmelCase__		,						tie_word_embeddings=lowerCAmelCase__		,						**lowerCAmelCase__		,						)
