

'''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   , a_   , a_=1_3   , a_=7   , a_=True   , a_=True   , a_=False   , a_=True   , a_=9_9   , a_=6_4   , a_=2   , a_=4   , a_=6_4   , a_="gelu"   , a_=0.1   , a_=0.1   , a_=5_1_2   , a_=1_6   , a_=2   , a_=0.02   , a_=3   , a_=4   , a_=None   , )							->				Any:


    """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 						snake_case_			(     self     )							->				int:


    """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													=	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 						snake_case_			(     self   , a_   , a_   , a_   , a_   , a_   , a_     )							->				Optional[Any]:


    """simple docstring"""
    UpperCAmelCase													=	TFMPNetModel(config=a_     )
    UpperCAmelCase													=	{'input_ids': input_ids, 'attention_mask': input_mask}
    UpperCAmelCase													=	model(a_     )
    UpperCAmelCase													=	[input_ids, input_mask]
    UpperCAmelCase													=	model(a_     )
    self.parent.assertEqual(result.last_hidden_state.shape   , (self.batch_size, self.seq_length, self.hidden_size)     )
  def 						snake_case_			(     self   , a_   , a_   , a_   , a_   , a_   , a_     )							->				List[Any]:


    """simple docstring"""
    UpperCAmelCase													=	TFMPNetForMaskedLM(config=a_     )
    UpperCAmelCase													=	{'input_ids': input_ids, 'attention_mask': input_mask}
    UpperCAmelCase													=	model(a_     )
    self.parent.assertEqual(result.logits.shape   , (self.batch_size, self.seq_length, self.vocab_size)     )
  def 						snake_case_			(     self   , a_   , a_   , a_   , a_   , a_   , a_     )							->				Optional[Any]:


    """simple docstring"""
    UpperCAmelCase													=	TFMPNetForQuestionAnswering(config=a_     )
    UpperCAmelCase													=	{
        'input_ids': input_ids,
        'attention_mask': input_mask,
    }
    UpperCAmelCase													=	model(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 						snake_case_			(     self   , a_   , a_   , a_   , a_   , a_   , a_     )							->				int:


    """simple docstring"""
    UpperCAmelCase													=	self.num_labels
    UpperCAmelCase													=	TFMPNetForSequenceClassification(a_     )
    UpperCAmelCase													=	{'input_ids': input_ids, 'attention_mask': input_mask}
    UpperCAmelCase													=	model(a_     )
    self.parent.assertEqual(result.logits.shape   , (self.batch_size, self.num_labels)     )
  def 						snake_case_			(     self   , a_   , a_   , a_   , a_   , a_   , a_     )							->				Optional[Any]:


    """simple docstring"""
    UpperCAmelCase													=	self.num_choices
    UpperCAmelCase													=	TFMPNetForMultipleChoice(a_     )
    UpperCAmelCase													=	tf.tile(tf.expand_dims(a_   , 1     )   , (1, self.num_choices, 1)     )
    UpperCAmelCase													=	tf.tile(tf.expand_dims(a_   , 1     )   , (1, self.num_choices, 1)     )
    UpperCAmelCase													=	{
        'input_ids': multiple_choice_inputs_ids,
        'attention_mask': multiple_choice_input_mask,
    }
    UpperCAmelCase													=	model(a_     )
    self.parent.assertEqual(result.logits.shape   , (self.batch_size, self.num_choices)     )
  def 						snake_case_			(     self   , a_   , a_   , a_   , a_   , a_   , a_     )							->				Tuple:


    """simple docstring"""
    UpperCAmelCase													=	self.num_labels
    UpperCAmelCase													=	TFMPNetForTokenClassification(a_     )
    UpperCAmelCase													=	{'input_ids': input_ids, 'attention_mask': input_mask}
    UpperCAmelCase													=	model(a_     )
    self.parent.assertEqual(result.logits.shape   , (self.batch_size, self.seq_length, self.num_labels)     )





  def 						snake_case_			(     self     )							->				str:


    """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_tf
class        lowercase_	(		a ,       a ,       unittest.TestCase ):

  '''simple docstring'''





  __lowerCAmelCase					: List[str]      =   (
      (
          TFMPNetForMaskedLM,
          TFMPNetForMultipleChoice,
          TFMPNetForQuestionAnswering,
          TFMPNetForSequenceClassification,
          TFMPNetForTokenClassification,
          TFMPNetModel,
      )
      if is_tf_available()
      else ()
  )
  __lowerCAmelCase					: List[Any]      =   (
      {
          "feature-extraction": TFMPNetModel,
          "fill-mask": TFMPNetForMaskedLM,
          "question-answering": TFMPNetForQuestionAnswering,
          "text-classification": TFMPNetForSequenceClassification,
          "token-classification": TFMPNetForTokenClassification,
          "zero-shot": TFMPNetForSequenceClassification,
      }
      if is_tf_available()
      else {}
  )
  __lowerCAmelCase					: Optional[Any]      =   False
  __lowerCAmelCase					: Union[str, Any]      =   False
  def 						snake_case_			(     self     )							->				List[Any]:


    """simple docstring"""
    UpperCAmelCase													=	TFMPNetModelTester(self     )
    UpperCAmelCase													=	ConfigTester(self   , config_class=a_   , hidden_size=3_7     )
  def 						snake_case_			(     self     )							->				Union[str, Any]:


    """simple docstring"""
    self.config_tester.run_common_tests()
  def 						snake_case_			(     self     )							->				Tuple:


    """simple docstring"""
    UpperCAmelCase													=	self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_mpnet_model(*a_     )
  def 						snake_case_			(     self     )							->				int:


    """simple docstring"""
    UpperCAmelCase													=	self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_mpnet_for_masked_lm(*a_     )
  def 						snake_case_			(     self     )							->				Union[str, Any]:


    """simple docstring"""
    UpperCAmelCase													=	self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_mpnet_for_question_answering(*a_     )
  def 						snake_case_			(     self     )							->				Tuple:


    """simple docstring"""
    UpperCAmelCase													=	self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_mpnet_for_sequence_classification(*a_     )
  def 						snake_case_			(     self     )							->				List[str]:


    """simple docstring"""
    UpperCAmelCase													=	self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_mpnet_for_multiple_choice(*a_     )
  def 						snake_case_			(     self     )							->				Dict:


    """simple docstring"""
    UpperCAmelCase													=	self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_mpnet_for_token_classification(*a_     )





  @slow
  def 						snake_case_			(     self     )							->				Tuple:


    """simple docstring"""
    for model_name in ["microsoft/mpnet-base"]:
      UpperCAmelCase													=	TFMPNetModel.from_pretrained(a_     )
      self.assertIsNotNone(a_     )




@require_tf
class        lowercase_	(		unittest.TestCase ):

  '''simple docstring'''





  @slow
  def 						snake_case_			(     self     )							->				Any:


    """simple docstring"""
    UpperCAmelCase													=	TFMPNetModel.from_pretrained('microsoft/mpnet-base'     )
    UpperCAmelCase													=	tf.constant([[0, 1, 2, 3, 4, 5]]     )
    UpperCAmelCase													=	model(a_     )[0]

    UpperCAmelCase													=	[1, 6, 7_6_8]
    self.assertEqual(output.shape   , a_     )

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





'''simple docstring'''




from collections import OrderedDict
from dataclasses import fields
from typing import Any, Tuple

import numpy as np

from .import_utils import is_torch_available


def       lowerCamelCase__		(						SCREAMING_SNAKE_CASE      :   Optional[Any]						):
  if is_torch_available():
    import torch

    if isinstance(SCREAMING_SNAKE_CASE       ,   torch.Tensor						):
      return True

  return isinstance(SCREAMING_SNAKE_CASE       ,   np.ndarray						)



class        lowercase_	(		a ):

  '''simple docstring'''





  def 						snake_case_			(     self     )							->				List[str]:


    """simple docstring"""
    UpperCAmelCase													=	fields(self     )

    # Safety and consistency checks
    if not len(a_     ):
      raise ValueError(F'''{self.__class__.__name__} has no fields.'''     )

    UpperCAmelCase													=	getattr(self   , class_fields[0].name     )
    UpperCAmelCase													=	all(getattr(self   , field.name     ) is None for field in class_fields[1:]     )

    if other_fields_are_none and isinstance(a_   , a_     ):
      for key, value in first_field.items():
        UpperCAmelCase													=	value
    else:
      for field in class_fields:
        UpperCAmelCase													=	getattr(self   , field.name     )
        if v is not None:
          UpperCAmelCase													=	v
  def __delitem__(     self   , *a_   , **a_     )							->				int:


    """simple docstring"""
    raise Exception(F'''You cannot use ``__delitem__`` on a {self.__class__.__name__} instance.'''     )
  def 						snake_case_			(     self   , *a_   , **a_     )							->				Tuple:


    """simple docstring"""
    raise Exception(F'''You cannot use ``setdefault`` on a {self.__class__.__name__} instance.'''     )
  def 						snake_case_			(     self   , *a_   , **a_     )							->				int:


    """simple docstring"""
    raise Exception(F'''You cannot use ``pop`` on a {self.__class__.__name__} instance.'''     )
  def 						snake_case_			(     self   , *a_   , **a_     )							->				Dict:


    """simple docstring"""
    raise Exception(F'''You cannot use ``update`` on a {self.__class__.__name__} instance.'''     )
  def __getitem__(     self   , a_     )							->				Tuple:


    """simple docstring"""
    if isinstance(a_   , a_     ):
      UpperCAmelCase													=	dict(self.items()     )
      return inner_dict[k]
    else:
      return self.to_tuple()[k]
  def __setattr__(     self   , a_   , a_     )							->				Dict:


    """simple docstring"""
    if name in self.keys() and value is not None:
      # Don't call self.__setitem__ to avoid recursion errors
      super().__setitem__(a_   , a_     )
    super().__setattr__(a_   , a_     )
  def __setitem__(     self   , a_   , a_     )							->				str:


    """simple docstring"""
    # Will raise a KeyException if needed
    super().__setitem__(a_   , a_     )
    # Don't call self.__setattr__ to avoid recursion errors
    super().__setattr__(a_   , a_     )





  def 						snake_case_			(     self     )							->				Tuple[Any]:


    """simple docstring"""
    return tuple(self[k] for k in self.keys()     )


