






import unittest

from transformers import MPNetConfig, is_torch_available
from transformers.testing_utils import require_torch, slow, torch_device

from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
	import torch

	from transformers import (
	    MPNetForMaskedLM,
	    MPNetForMultipleChoice,
	    MPNetForQuestionAnswering,
	    MPNetForSequenceClassification,
	    MPNetForTokenClassification,
	    MPNetModel,
	)



class 							SCREAMING_SNAKE_CASE__		:





					"""simple docstring"""






					def __init__(      self  :						Union[str, Any]							,	__A  :						int							,	__A  :						Union[str, Any]=1_3							,	__A  :						int=7							,	__A  :						Optional[int]=True							,	__A  :						str=True							,	__A  :						Dict=False							,	__A  :						Tuple=True							,	__A  :						List[str]=9_9							,	__A  :						Tuple=6_4							,	__A  :						int=5							,	__A  :						Any=4							,	__A  :						List[str]=6_4							,	__A  :						int="gelu"							,	__A  :						Any=0.1							,	__A  :						str=0.1							,	__A  :						str=5_1_2							,	__A  :						str=1_6							,	__A  :						Tuple=2							,	__A  :						Any=0.0_2							,	__A  :						str=3							,	__A  :						Optional[int]=4							,	__A  :						Tuple=None							,	):
						snake_case__				:   int     =						parent
						snake_case__				:   Union[str, Any]     =						batch_size
						snake_case__				:   Union[str, Any]     =						seq_length
						snake_case__				:   int     =						is_training
						snake_case__				:   Tuple     =						use_input_mask
						snake_case__				:   Optional[int]     =						use_token_type_ids
						snake_case__				:   str     =						use_labels
						snake_case__				:   Optional[Any]     =						vocab_size
						snake_case__				:   List[Any]     =						hidden_size
						snake_case__				:   Tuple     =						num_hidden_layers
						snake_case__				:   List[Any]     =						num_attention_heads
						snake_case__				:   int     =						intermediate_size
						snake_case__				:   Optional[Any]     =						hidden_act
						snake_case__				:   Dict     =						hidden_dropout_prob
						snake_case__				:   str     =						attention_probs_dropout_prob
						snake_case__				:   List[Any]     =						max_position_embeddings
						snake_case__				:   Optional[Any]     =						type_vocab_size
						snake_case__				:   Dict     =						type_sequence_label_size
						snake_case__				:   str     =						initializer_range
						snake_case__				:   List[str]     =						num_labels
						snake_case__				:   int     =						num_choices
						snake_case__				:   Dict     =						scope



					def  _lowercase  (      self  :						Any ):
						return MPNetConfig.from_pretrained("microsoft/mpnet-base" )



					def  _lowercase  (      self  :						Optional[Any] ):
						snake_case__				:   Any     =						ids_tensor([self.batch_size, self.seq_length]							,	self.vocab_size )

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

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

						snake_case__				:   Dict     =						self.get_config()
						return config, input_ids, input_mask, sequence_labels, token_labels, choice_labels



					def  _lowercase  (      self  :						str ):
						return 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							,	)



					def  _lowercase  (      self  :						str							,	__A  :						Dict							,	__A  :						str							,	__A  :						Any							,	__A  :						int							,	__A  :						List[Any]							,	__A  :						Optional[Any] ):
						snake_case__				:   Any     =						MPNetModel(config=__A )
						model.to(__A )
						model.eval()
						snake_case__				:   Optional[Any]     =						model(__A							,	__A )
						snake_case__				:   Dict     =						model(__A )
						self.parent.assertEqual(result.last_hidden_state.shape							,	(self.batch_size, self.seq_length, self.hidden_size) )
						self.parent.assertEqual(result.pooler_output.shape							,	(self.batch_size, self.hidden_size) )



					def  _lowercase  (      self  :						Tuple							,	__A  :						Union[str, Any]							,	__A  :						List[str]							,	__A  :						Union[str, Any]							,	__A  :						List[Any]							,	__A  :						int							,	__A  :						Optional[Any] ):
						snake_case__				:   Any     =						MPNetForQuestionAnswering(config=__A )
						model.to(__A )
						model.eval()
						snake_case__				:   Union[str, Any]     =						model(
						    __A							,	attention_mask=__A							,	start_positions=__A							,	end_positions=__A							,	)
						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  :						Tuple							,	__A  :						int							,	__A  :						Tuple							,	__A  :						int							,	__A  :						str							,	__A  :						List[str]							,	__A  :						str ):
						snake_case__				:   str     =						self.num_labels
						snake_case__				:   Tuple     =						MPNetForSequenceClassification(__A )
						model.to(__A )
						model.eval()
						snake_case__				:   str     =						model(__A							,	attention_mask=__A							,	labels=__A )
						self.parent.assertEqual(result.logits.shape							,	(self.batch_size, self.num_labels) )



					def  _lowercase  (      self  :						Any							,	__A  :						List[str]							,	__A  :						Any							,	__A  :						Union[str, Any]							,	__A  :						Any							,	__A  :						Optional[Any]							,	__A  :						List[Any] ):
						snake_case__				:   int     =						self.num_choices
						snake_case__				:   Tuple     =						MPNetForMultipleChoice(config=__A )
						model.to(__A )
						model.eval()
						snake_case__				:   Optional[Any]     =						input_ids.unsqueeze(1 ).expand(-1							,	self.num_choices							,	-1 ).contiguous()
						snake_case__				:   str     =						input_mask.unsqueeze(1 ).expand(-1							,	self.num_choices							,	-1 ).contiguous()
						snake_case__				:   int     =						model(
						    __A							,	attention_mask=__A							,	labels=__A							,	)
						self.parent.assertEqual(result.logits.shape							,	(self.batch_size, self.num_choices) )



					def  _lowercase  (      self  :						str							,	__A  :						Union[str, Any]							,	__A  :						List[str]							,	__A  :						Optional[int]							,	__A  :						List[Any]							,	__A  :						Union[str, Any]							,	__A  :						List[str] ):
						snake_case__				:   Tuple     =						self.num_labels
						snake_case__				:   List[str]     =						MPNetForTokenClassification(config=__A )
						model.to(__A )
						model.eval()
						snake_case__				:   Any     =						model(__A							,	attention_mask=__A							,	labels=__A )
						self.parent.assertEqual(result.logits.shape							,	(self.batch_size, self.seq_length, self.num_labels) )






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



@require_torch
class 							SCREAMING_SNAKE_CASE__		(       UpperCamelCase_				,     UpperCamelCase_				,     unittest.TestCase		):





					"""simple docstring"""






					a_			=	(
					    (
					        MPNetForMaskedLM,
					        MPNetForMultipleChoice,
					        MPNetForQuestionAnswering,
					        MPNetForSequenceClassification,
					        MPNetForTokenClassification,
					        MPNetModel,
					    )
					    if is_torch_available()
					    else ()
					)
					a_			=	(
					    {
					        "feature-extraction": MPNetModel,
					        "fill-mask": MPNetForMaskedLM,
					        "question-answering": MPNetForQuestionAnswering,
					        "text-classification": MPNetForSequenceClassification,
					        "token-classification": MPNetForTokenClassification,
					        "zero-shot": MPNetForSequenceClassification,
					    }
					    if is_torch_available()
					    else {}
					)
					a_			=	False
					a_			=	True



					def  _lowercase  (      self  :						List[str] ):
						snake_case__				:   Any     =						MPNetModelTester(self )
						snake_case__				:   Tuple     =						ConfigTester(self							,	config_class=__A							,	hidden_size=3_7 )



					def  _lowercase  (      self  :						Tuple ):
						self.config_tester.run_common_tests()



					def  _lowercase  (      self  :						Optional[int] ):
						snake_case__				:   Tuple     =						self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_mpnet_model(*__A )



					def  _lowercase  (      self  :						Optional[Any] ):
						snake_case__				:   Optional[Any]     =						self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_mpnet_for_sequence_classification(*__A )



					def  _lowercase  (      self  :						Any ):
						snake_case__				:   List[Any]     =						self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_mpnet_for_multiple_choice(*__A )



					def  _lowercase  (      self  :						List[Any] ):
						snake_case__				:   List[str]     =						self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_mpnet_for_token_classification(*__A )






					def  _lowercase  (      self  :						Union[str, Any] ):
						snake_case__				:   int     =						self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_mpnet_for_question_answering(*__A )



@require_torch
class 							SCREAMING_SNAKE_CASE__		(       unittest.TestCase		):





					"""simple docstring"""






					@slow
					def  _lowercase  (      self  :						int ):
						snake_case__				:   List[Any]     =						MPNetModel.from_pretrained("microsoft/mpnet-base" )
						snake_case__				:   Optional[int]     =						torch.tensor([[0, 3_4_5, 2_3_2, 3_2_8, 7_4_0, 1_4_0, 1_6_9_5, 6_9, 6_0_7_8, 1_5_8_8, 2]] )
						snake_case__				:   List[Any]     =						model(__A )[0]
						snake_case__				:   Optional[int]     =						torch.Size((1, 1_1, 7_6_8) )
						self.assertEqual(output.shape							,	__A )
						snake_case__				:   Any     =						torch.tensor(
						    [[[-0.0_5_5_0, 0.1_9_4_3, -0.0_7_4_0], [-0.0_5_6_2, 0.2_2_1_1, -0.0_5_7_9], [-0.0_4_3_7, 0.3_3_3_7, -0.0_6_4_1]]] )
						# compare the actual values for a slice.
						self.assertTrue(torch.allclose(output[:, :3, :3]							,	__A							,	atol=1e-4 ) )











import copy
from dataclasses import dataclass, field
from typing import ClassVar, Dict

from ..features import ClassLabel, Features, Value
from .base import TaskTemplate



@dataclass(frozen=UpperCamelCase_		)
class 							SCREAMING_SNAKE_CASE__		(       UpperCamelCase_		):





					"""simple docstring"""






					a_			=	field(default="text-classification"				,     metadata={"include_in_asdict_even_if_is_default": True}		)
					a_			=	Features({"text": Value("string"		)}		)
					a_			=	Features({"labels": ClassLabel}		)
					a_			=	"text"
					a_			=	"labels"



					def  _lowercase  (      self  :						Tuple							,	__A  :						List[Any] ):
						if self.label_column not in features:
							raise ValueError(f'''Column {self.label_column} is not present in features.''' )
						if not isinstance(features[self.label_column]							,	__A ):
							raise ValueError(f'''Column {self.label_column} is not a ClassLabel.''' )
						snake_case__				:   Any     =						copy.deepcopy(self )
						snake_case__				:   Optional[Any]     =						self.label_schema.copy()
						snake_case__				:   List[str]     =						features[self.label_column]
						snake_case__				:   Dict     =						label_schema
						return task_template






					@property
					def  _lowercase  (      self  :						Tuple ):
						return {
						    self.text_column: "text",
						    self.label_column: "labels",
						}



