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

 """simple docstring"""

 def __init__(						self			:			int					, SCREAMING_SNAKE_CASE__			:			Any					, SCREAMING_SNAKE_CASE__			:			Optional[int]=13					, SCREAMING_SNAKE_CASE__			:			Dict=7					, SCREAMING_SNAKE_CASE__			:			List[str]=True					, SCREAMING_SNAKE_CASE__			:			Any=True					, SCREAMING_SNAKE_CASE__			:			Tuple=False					, SCREAMING_SNAKE_CASE__			:			Any=True					, SCREAMING_SNAKE_CASE__			:			Union[str, Any]=99					, SCREAMING_SNAKE_CASE__			:			str=64					, SCREAMING_SNAKE_CASE__			:			Optional[int]=5					, SCREAMING_SNAKE_CASE__			:			str=4					, SCREAMING_SNAKE_CASE__			:			List[Any]=64					, SCREAMING_SNAKE_CASE__			:			Dict="gelu"					, SCREAMING_SNAKE_CASE__			:			Tuple=0.1					, SCREAMING_SNAKE_CASE__			:			Any=0.1					, SCREAMING_SNAKE_CASE__			:			List[Any]=5_12					, SCREAMING_SNAKE_CASE__			:			List[Any]=16					, SCREAMING_SNAKE_CASE__			:			Dict=2					, SCREAMING_SNAKE_CASE__			:			List[str]=0.02					, SCREAMING_SNAKE_CASE__			:			Tuple=3					, SCREAMING_SNAKE_CASE__			:			Tuple=4					, SCREAMING_SNAKE_CASE__			:			Dict=None					, ):






      """simple docstring"""



      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      __lowerCAmelCase  (						self			:			Optional[int]   ):






      """simple docstring"""



      return MPNetConfig.from_pretrained('microsoft/mpnet-base'   )

 def      __lowerCAmelCase  (						self			:			Union[str, Any]   ):






      """simple docstring"""



      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						    =					self.get_config()
      return config, input_ids, input_mask, sequence_labels, token_labels, choice_labels

 def      __lowerCAmelCase  (						self			:			List[Any]   ):






      """simple docstring"""



      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      __lowerCAmelCase  (						self			:			List[str]					, SCREAMING_SNAKE_CASE__			:			Optional[int]					, SCREAMING_SNAKE_CASE__			:			Tuple					, SCREAMING_SNAKE_CASE__			:			List[str]					, SCREAMING_SNAKE_CASE__			:			Dict					, SCREAMING_SNAKE_CASE__			:			Any					, SCREAMING_SNAKE_CASE__			:			str   ):






      """simple docstring"""



      UpperCamelCase						    =					MPNetModel(config=SCREAMING_SNAKE_CASE__   )
      model.to(SCREAMING_SNAKE_CASE__   )
      model.eval()
      UpperCamelCase						    =					model(SCREAMING_SNAKE_CASE__					, SCREAMING_SNAKE_CASE__   )
      UpperCamelCase						    =					model(SCREAMING_SNAKE_CASE__   )
      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      __lowerCAmelCase  (						self			:			str					, SCREAMING_SNAKE_CASE__			:			Union[str, Any]					, SCREAMING_SNAKE_CASE__			:			Tuple					, SCREAMING_SNAKE_CASE__			:			Tuple					, SCREAMING_SNAKE_CASE__			:			Any					, SCREAMING_SNAKE_CASE__			:			Any					, SCREAMING_SNAKE_CASE__			:			Tuple   ):






      """simple docstring"""



      UpperCamelCase						    =					MPNetForQuestionAnswering(config=SCREAMING_SNAKE_CASE__   )
      model.to(SCREAMING_SNAKE_CASE__   )
      model.eval()
      UpperCamelCase						    =					model(
          SCREAMING_SNAKE_CASE__					, attention_mask=SCREAMING_SNAKE_CASE__					, start_positions=SCREAMING_SNAKE_CASE__					, end_positions=SCREAMING_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      __lowerCAmelCase  (						self			:			List[Any]					, SCREAMING_SNAKE_CASE__			:			str					, SCREAMING_SNAKE_CASE__			:			str					, SCREAMING_SNAKE_CASE__			:			Dict					, SCREAMING_SNAKE_CASE__			:			Dict					, SCREAMING_SNAKE_CASE__			:			List[Any]					, SCREAMING_SNAKE_CASE__			:			Optional[int]   ):






      """simple docstring"""



      UpperCamelCase						    =					self.num_labels
      UpperCamelCase						    =					MPNetForSequenceClassification(SCREAMING_SNAKE_CASE__   )
      model.to(SCREAMING_SNAKE_CASE__   )
      model.eval()
      UpperCamelCase						    =					model(SCREAMING_SNAKE_CASE__					, attention_mask=SCREAMING_SNAKE_CASE__					, labels=SCREAMING_SNAKE_CASE__   )
      self.parent.assertEqual(result.logits.shape					, (self.batch_size, self.num_labels)   )

 def      __lowerCAmelCase  (						self			:			List[str]					, SCREAMING_SNAKE_CASE__			:			Union[str, Any]					, SCREAMING_SNAKE_CASE__			:			Dict					, SCREAMING_SNAKE_CASE__			:			Optional[Any]					, SCREAMING_SNAKE_CASE__			:			List[Any]					, SCREAMING_SNAKE_CASE__			:			List[Any]					, SCREAMING_SNAKE_CASE__			:			Tuple   ):






      """simple docstring"""



      UpperCamelCase						    =					self.num_choices
      UpperCamelCase						    =					MPNetForMultipleChoice(config=SCREAMING_SNAKE_CASE__   )
      model.to(SCREAMING_SNAKE_CASE__   )
      model.eval()
      UpperCamelCase						    =					input_ids.unsqueeze(1   ).expand(-1					, self.num_choices					, -1   ).contiguous()
      UpperCamelCase						    =					input_mask.unsqueeze(1   ).expand(-1					, self.num_choices					, -1   ).contiguous()
      UpperCamelCase						    =					model(
          SCREAMING_SNAKE_CASE__					, attention_mask=SCREAMING_SNAKE_CASE__					, labels=SCREAMING_SNAKE_CASE__					, )
      self.parent.assertEqual(result.logits.shape					, (self.batch_size, self.num_choices)   )

 def      __lowerCAmelCase  (						self			:			List[str]					, SCREAMING_SNAKE_CASE__			:			List[Any]					, SCREAMING_SNAKE_CASE__			:			Tuple					, SCREAMING_SNAKE_CASE__			:			Optional[Any]					, SCREAMING_SNAKE_CASE__			:			Optional[Any]					, SCREAMING_SNAKE_CASE__			:			Optional[Any]					, SCREAMING_SNAKE_CASE__			:			Union[str, Any]   ):






      """simple docstring"""



      UpperCamelCase						    =					self.num_labels
      UpperCamelCase						    =					MPNetForTokenClassification(config=SCREAMING_SNAKE_CASE__   )
      model.to(SCREAMING_SNAKE_CASE__   )
      model.eval()
      UpperCamelCase						    =					model(SCREAMING_SNAKE_CASE__					, attention_mask=SCREAMING_SNAKE_CASE__					, labels=SCREAMING_SNAKE_CASE__   )
      self.parent.assertEqual(result.logits.shape					, (self.batch_size, self.seq_length, self.num_labels)   )






 def      __lowerCAmelCase  (						self			:			Dict   ):






      """simple docstring"""



      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_torch
class       _lowerCAmelCase  (		__magic_name__			,	__magic_name__			,	unittest.TestCase							):

 """simple docstring"""
 SCREAMING_SNAKE_CASE_				:					Tuple       						=(
     (
         MPNetForMaskedLM,
         MPNetForMultipleChoice,
         MPNetForQuestionAnswering,
         MPNetForSequenceClassification,
         MPNetForTokenClassification,
         MPNetModel,
     )
     if is_torch_available()
     else ()
 )
 SCREAMING_SNAKE_CASE_				:					Optional[int]       						=(
     {
         "feature-extraction": MPNetModel,
         "fill-mask": MPNetForMaskedLM,
         "question-answering": MPNetForQuestionAnswering,
         "text-classification": MPNetForSequenceClassification,
         "token-classification": MPNetForTokenClassification,
         "zero-shot": MPNetForSequenceClassification,
     }
     if is_torch_available()
     else {}
 )
 SCREAMING_SNAKE_CASE_				:					Union[str, Any]       						=False
 SCREAMING_SNAKE_CASE_				:					Tuple       						=True

 def      __lowerCAmelCase  (						self			:			List[str]   ):






      """simple docstring"""



      UpperCamelCase						    =					MPNetModelTester(self   )
      UpperCamelCase						    =					ConfigTester(self					, config_class=SCREAMING_SNAKE_CASE__					, hidden_size=37   )

 def      __lowerCAmelCase  (						self			:			Optional[Any]   ):






      """simple docstring"""



      self.config_tester.run_common_tests()

 def      __lowerCAmelCase  (						self			:			int   ):






      """simple docstring"""



      UpperCamelCase						    =					self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_mpnet_model(*SCREAMING_SNAKE_CASE__   )

 def      __lowerCAmelCase  (						self			:			List[str]   ):






      """simple docstring"""



      UpperCamelCase						    =					self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_mpnet_for_sequence_classification(*SCREAMING_SNAKE_CASE__   )

 def      __lowerCAmelCase  (						self			:			List[Any]   ):






      """simple docstring"""



      UpperCamelCase						    =					self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_mpnet_for_multiple_choice(*SCREAMING_SNAKE_CASE__   )

 def      __lowerCAmelCase  (						self			:			Optional[Any]   ):






      """simple docstring"""



      UpperCamelCase						    =					self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_mpnet_for_token_classification(*SCREAMING_SNAKE_CASE__   )






 def      __lowerCAmelCase  (						self			:			List[Any]   ):






      """simple docstring"""



      UpperCamelCase						    =					self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_mpnet_for_question_answering(*SCREAMING_SNAKE_CASE__   )




@require_torch
class       _lowerCAmelCase  (		unittest.TestCase							):

 """simple docstring"""

 @slow
 def      __lowerCAmelCase  (						self			:			List[str]   ):






      """simple docstring"""



      UpperCamelCase						    =					MPNetModel.from_pretrained('microsoft/mpnet-base'   )
      UpperCamelCase						    =					torch.tensor([[0, 3_45, 2_32, 3_28, 7_40, 1_40, 16_95, 69, 60_78, 15_88, 2]]   )
      UpperCamelCase						    =					model(SCREAMING_SNAKE_CASE__   )[0]
      UpperCamelCase						    =					torch.Size((1, 11, 7_68)   )
      self.assertEqual(output.shape					, SCREAMING_SNAKE_CASE__   )
      UpperCamelCase						    =					torch.tensor(
          [[[-0.0550, 0.1943, -0.0740], [-0.0562, 0.2211, -0.0579], [-0.0437, 0.3337, -0.0641]]]   )
      # compare the actual values for a slice.
      self.assertTrue(torch.allclose(output[:, :3, :3]					, SCREAMING_SNAKE_CASE__					, atol=1e-4   )   )




from collections import OrderedDict
from typing import Mapping

from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfig
from ...utils import logging


_snake_case      = logging.get_logger(__name__)

_snake_case      = {
    '''roberta-base''': '''https://huggingface.co/roberta-base/resolve/main/config.json''',
    '''roberta-large''': '''https://huggingface.co/roberta-large/resolve/main/config.json''',
    '''roberta-large-mnli''': '''https://huggingface.co/roberta-large-mnli/resolve/main/config.json''',
    '''distilroberta-base''': '''https://huggingface.co/distilroberta-base/resolve/main/config.json''',
    '''roberta-base-openai-detector''': '''https://huggingface.co/roberta-base-openai-detector/resolve/main/config.json''',
    '''roberta-large-openai-detector''': '''https://huggingface.co/roberta-large-openai-detector/resolve/main/config.json''',
}
class       _lowerCAmelCase  (		__magic_name__							):

 """simple docstring"""
 SCREAMING_SNAKE_CASE_				:					Dict       						="roberta"

 def __init__(						self			:			Union[str, Any]					, SCREAMING_SNAKE_CASE__			:			Tuple=5_02_65					, SCREAMING_SNAKE_CASE__			:			List[str]=7_68					, SCREAMING_SNAKE_CASE__			:			List[str]=12					, SCREAMING_SNAKE_CASE__			:			Any=12					, SCREAMING_SNAKE_CASE__			:			Tuple=30_72					, SCREAMING_SNAKE_CASE__			:			str="gelu"					, SCREAMING_SNAKE_CASE__			:			Union[str, Any]=0.1					, SCREAMING_SNAKE_CASE__			:			int=0.1					, SCREAMING_SNAKE_CASE__			:			int=5_12					, SCREAMING_SNAKE_CASE__			:			Optional[Any]=2					, SCREAMING_SNAKE_CASE__			:			Dict=0.02					, SCREAMING_SNAKE_CASE__			:			List[str]=1e-12					, SCREAMING_SNAKE_CASE__			:			Any=1					, SCREAMING_SNAKE_CASE__			:			Optional[Any]=0					, SCREAMING_SNAKE_CASE__			:			str=2					, SCREAMING_SNAKE_CASE__			:			Any="absolute"					, SCREAMING_SNAKE_CASE__			:			Any=True					, SCREAMING_SNAKE_CASE__			:			List[Any]=None					, **SCREAMING_SNAKE_CASE__			:			Optional[int]					, ):






      """simple docstring"""



      super().__init__(pad_token_id=SCREAMING_SNAKE_CASE__					, bos_token_id=SCREAMING_SNAKE_CASE__					, eos_token_id=SCREAMING_SNAKE_CASE__					, **SCREAMING_SNAKE_CASE__   )

      UpperCamelCase						    =					vocab_size
      UpperCamelCase						    =					hidden_size
      UpperCamelCase						    =					num_hidden_layers
      UpperCamelCase						    =					num_attention_heads
      UpperCamelCase						    =					hidden_act
      UpperCamelCase						    =					intermediate_size
      UpperCamelCase						    =					hidden_dropout_prob
      UpperCamelCase						    =					attention_probs_dropout_prob
      UpperCamelCase						    =					max_position_embeddings
      UpperCamelCase						    =					type_vocab_size
      UpperCamelCase						    =					initializer_range
      UpperCamelCase						    =					layer_norm_eps
      UpperCamelCase						    =					position_embedding_type
      UpperCamelCase						    =					use_cache
      UpperCamelCase						    =					classifier_dropout




class       _lowerCAmelCase  (		__magic_name__							):

 """simple docstring"""

 @property
 def      __lowerCAmelCase  (						self			:			Optional[int]   ):






      """simple docstring"""



      if self.task == "multiple-choice":
           UpperCamelCase						    =					{0: 'batch', 1: 'choice', 2: 'sequence'}
      else:
           UpperCamelCase						    =					{0: 'batch', 1: 'sequence'}
      return OrderedDict(
          [
              ('input_ids', dynamic_axis),
              ('attention_mask', dynamic_axis),
          ]   )


