
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[str]     ,     lowerCAmelCase_	:				int     ,     lowerCAmelCase_	:				List[Any]=1_3     ,     lowerCAmelCase_	:				Optional[int]=7     ,     lowerCAmelCase_	:				Dict=True     ,     lowerCAmelCase_	:				List[Any]=True     ,     lowerCAmelCase_	:				Optional[Any]=False     ,     lowerCAmelCase_	:				Optional[Any]=True     ,     lowerCAmelCase_	:				int=9_9     ,     lowerCAmelCase_	:				int=6_4     ,     lowerCAmelCase_	:				List[Any]=2     ,     lowerCAmelCase_	:				Optional[int]=4     ,     lowerCAmelCase_	:				Optional[Any]=6_4     ,     lowerCAmelCase_	:				str="gelu"     ,     lowerCAmelCase_	:				Optional[Any]=0.1     ,     lowerCAmelCase_	:				Union[str, Any]=0.1     ,     lowerCAmelCase_	:				int=5_1_2     ,     lowerCAmelCase_	:				Dict=1_6     ,     lowerCAmelCase_	:				str=2     ,     lowerCAmelCase_	:				str=0.02     ,     lowerCAmelCase_	:				int=3     ,     lowerCAmelCase_	:				List[str]=4     ,     lowerCAmelCase_	:				Dict=None     ,     ):

      """simple docstring"""
      _A:    Dict						     =			parent
      _A:    Union[str, Any]						     =			batch_size
      _A:    Optional[int]						     =			seq_length
      _A:    str						     =			is_training
      _A:    int						     =			use_input_mask
      _A:    int						     =			use_token_type_ids
      _A:    Optional[Any]						     =			use_labels
      _A:    Optional[int]						     =			vocab_size
      _A:    Optional[int]						     =			hidden_size
      _A:    Tuple						     =			num_hidden_layers
      _A:    List[str]						     =			num_attention_heads
      _A:    int						     =			intermediate_size
      _A:    int						     =			hidden_act
      _A:    Any						     =			hidden_dropout_prob
      _A:    List[Any]						     =			attention_probs_dropout_prob
      _A:    Union[str, Any]						     =			max_position_embeddings
      _A:    str						     =			type_vocab_size
      _A:    List[str]						     =			type_sequence_label_size
      _A:    List[Any]						     =			initializer_range
      _A:    Tuple						     =			num_labels
      _A:    Tuple						     =			num_choices
      _A:    List[Any]						     =			scope


   def 					__magic_name__							(   self	:				Tuple	):

      """simple docstring"""
      _A:    str						     =			ids_tensor([self.batch_size, self.seq_length]     ,     self.vocab_size	)

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

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

      _A:    Dict						     =			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 					__magic_name__							(   self	:				int     ,     lowerCAmelCase_	:				Tuple     ,     lowerCAmelCase_	:				Optional[int]     ,     lowerCAmelCase_	:				Tuple     ,     lowerCAmelCase_	:				Optional[int]     ,     lowerCAmelCase_	:				Any     ,     lowerCAmelCase_	:				int	):

      """simple docstring"""
      _A:    List[Any]						     =			TFMPNetModel(config=lowerCAmelCase_	)
      _A:    Optional[int]						     =			{'''input_ids''': input_ids, '''attention_mask''': input_mask}
      _A:    List[str]						     =			model(lowerCAmelCase_	)
      _A:    str						     =			[input_ids, input_mask]
      _A:    Dict						     =			model(lowerCAmelCase_	)
      self.parent.assertEqual(result.last_hidden_state.shape     ,     (self.batch_size, self.seq_length, self.hidden_size)	)


   def 					__magic_name__							(   self	:				Optional[int]     ,     lowerCAmelCase_	:				Dict     ,     lowerCAmelCase_	:				List[str]     ,     lowerCAmelCase_	:				str     ,     lowerCAmelCase_	:				Tuple     ,     lowerCAmelCase_	:				Tuple     ,     lowerCAmelCase_	:				Optional[int]	):

      """simple docstring"""
      _A:    Any						     =			TFMPNetForMaskedLM(config=lowerCAmelCase_	)
      _A:    Optional[Any]						     =			{'''input_ids''': input_ids, '''attention_mask''': input_mask}
      _A:    Any						     =			model(lowerCAmelCase_	)
      self.parent.assertEqual(result.logits.shape     ,     (self.batch_size, self.seq_length, self.vocab_size)	)


   def 					__magic_name__							(   self	:				str     ,     lowerCAmelCase_	:				Union[str, Any]     ,     lowerCAmelCase_	:				str     ,     lowerCAmelCase_	:				Any     ,     lowerCAmelCase_	:				Tuple     ,     lowerCAmelCase_	:				Any     ,     lowerCAmelCase_	:				Dict	):

      """simple docstring"""
      _A:    int						     =			TFMPNetForQuestionAnswering(config=lowerCAmelCase_	)
      _A:    Any						     =			{
          '''input_ids''': input_ids,
          '''attention_mask''': input_mask,
      }
      _A:    str						     =			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 					__magic_name__							(   self	:				List[str]     ,     lowerCAmelCase_	:				int     ,     lowerCAmelCase_	:				str     ,     lowerCAmelCase_	:				Union[str, Any]     ,     lowerCAmelCase_	:				Tuple     ,     lowerCAmelCase_	:				str     ,     lowerCAmelCase_	:				str	):

      """simple docstring"""
      _A:    Tuple						     =			self.num_labels
      _A:    List[str]						     =			TFMPNetForSequenceClassification(lowerCAmelCase_	)
      _A:    int						     =			{'''input_ids''': input_ids, '''attention_mask''': input_mask}
      _A:    Union[str, Any]						     =			model(lowerCAmelCase_	)
      self.parent.assertEqual(result.logits.shape     ,     (self.batch_size, self.num_labels)	)


   def 					__magic_name__							(   self	:				Optional[Any]     ,     lowerCAmelCase_	:				str     ,     lowerCAmelCase_	:				Tuple     ,     lowerCAmelCase_	:				List[Any]     ,     lowerCAmelCase_	:				Any     ,     lowerCAmelCase_	:				str     ,     lowerCAmelCase_	:				int	):

      """simple docstring"""
      _A:    Tuple						     =			self.num_choices
      _A:    int						     =			TFMPNetForMultipleChoice(lowerCAmelCase_	)
      _A:    str						     =			tf.tile(tf.expand_dims(lowerCAmelCase_     ,     1	)     ,     (1, self.num_choices, 1)	)
      _A:    str						     =			tf.tile(tf.expand_dims(lowerCAmelCase_     ,     1	)     ,     (1, self.num_choices, 1)	)
      _A:    Optional[Any]						     =			{
          '''input_ids''': multiple_choice_inputs_ids,
          '''attention_mask''': multiple_choice_input_mask,
      }
      _A:    Optional[Any]						     =			model(lowerCAmelCase_	)
      self.parent.assertEqual(result.logits.shape     ,     (self.batch_size, self.num_choices)	)


   def 					__magic_name__							(   self	:				Tuple     ,     lowerCAmelCase_	:				Tuple     ,     lowerCAmelCase_	:				List[str]     ,     lowerCAmelCase_	:				int     ,     lowerCAmelCase_	:				List[Any]     ,     lowerCAmelCase_	:				List[Any]     ,     lowerCAmelCase_	:				Optional[int]	):

      """simple docstring"""
      _A:    str						     =			self.num_labels
      _A:    List[Any]						     =			TFMPNetForTokenClassification(lowerCAmelCase_	)
      _A:    List[str]						     =			{'''input_ids''': input_ids, '''attention_mask''': input_mask}
      _A:    int						     =			model(lowerCAmelCase_	)
      self.parent.assertEqual(result.logits.shape     ,     (self.batch_size, self.seq_length, self.num_labels)	)


   def 					__magic_name__							(   self	:				Optional[Any]	):

      """simple docstring"""
      _A:    Tuple						     =			self.prepare_config_and_inputs()
      ((_A)     ,    (_A)     ,    (_A)     ,    (_A)     ,    (_A)     ,    (_A)):    Optional[int]						     =			config_and_inputs
      _A:    Any						     =			{'''input_ids''': input_ids, '''attention_mask''': input_mask}
      return config, inputs_dict






@require_tf
class UpperCAmelCase  (		SCREAMING_SNAKE_CASE__				,				SCREAMING_SNAKE_CASE__				,				unittest.TestCase  ):





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


   def 					__magic_name__							(   self	:				Any	):

      """simple docstring"""
      _A:    Dict						     =			TFMPNetModelTester(self	)
      _A:    str						     =			ConfigTester(self     ,     config_class=lowerCAmelCase_     ,     hidden_size=3_7	)


   def 					__magic_name__							(   self	:				Optional[Any]	):

      """simple docstring"""
      self.config_tester.run_common_tests()


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

      """simple docstring"""
      _A:    Union[str, Any]						     =			self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_mpnet_model(*lowerCAmelCase_	)


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

      """simple docstring"""
      _A:    Any						     =			self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_mpnet_for_masked_lm(*lowerCAmelCase_	)


   def 					__magic_name__							(   self	:				Dict	):

      """simple docstring"""
      _A:    int						     =			self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_mpnet_for_question_answering(*lowerCAmelCase_	)


   def 					__magic_name__							(   self	:				Dict	):

      """simple docstring"""
      _A:    int						     =			self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_mpnet_for_sequence_classification(*lowerCAmelCase_	)


   def 					__magic_name__							(   self	:				int	):

      """simple docstring"""
      _A:    Tuple						     =			self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_mpnet_for_multiple_choice(*lowerCAmelCase_	)


   def 					__magic_name__							(   self	:				Dict	):

      """simple docstring"""
      _A:    List[str]						     =			self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_mpnet_for_token_classification(*lowerCAmelCase_	)


   @slow
   def 					__magic_name__							(   self	:				Dict	):

      """simple docstring"""
      for model_name in ["microsoft/mpnet-base"]:
         _A:    Optional[Any]						     =			TFMPNetModel.from_pretrained(lowerCAmelCase_	)
         self.assertIsNotNone(lowerCAmelCase_	)






@require_tf
class UpperCAmelCase  (		unittest.TestCase  ):





   '''simple docstring'''


   @slow
   def 					__magic_name__							(   self	:				Optional[Any]	):

      """simple docstring"""
      _A:    Optional[Any]						     =			TFMPNetModel.from_pretrained('''microsoft/mpnet-base'''	)
      _A:    Optional[int]						     =			tf.constant([[0, 1, 2, 3, 4, 5]]	)
      _A:    Any						     =			model(lowerCAmelCase_	)[0]

      _A:    List[Any]						     =			[1, 6, 7_6_8]
      self.assertEqual(output.shape     ,     lowerCAmelCase_	)

      _A:    Dict						     =			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]     ,     lowerCAmelCase_     ,     atol=1e-4	)





from collections import OrderedDict
from typing import Mapping

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


UpperCAmelCase__						:	List[str]	     = logging.get_logger(__name__)

UpperCAmelCase__						:	str	     = {
    'flaubert/flaubert_small_cased': 'https://huggingface.co/flaubert/flaubert_small_cased/resolve/main/config.json',
    'flaubert/flaubert_base_uncased': 'https://huggingface.co/flaubert/flaubert_base_uncased/resolve/main/config.json',
    'flaubert/flaubert_base_cased': 'https://huggingface.co/flaubert/flaubert_base_cased/resolve/main/config.json',
    'flaubert/flaubert_large_cased': 'https://huggingface.co/flaubert/flaubert_large_cased/resolve/main/config.json',
}






class UpperCAmelCase  (		SCREAMING_SNAKE_CASE__  ):





   '''simple docstring'''
   __UpperCamelCase						:       Tuple												=       '''flaubert'''
   __UpperCamelCase						:       str												=       {
       '''hidden_size''': '''emb_dim''',
       '''num_attention_heads''': '''n_heads''',
       '''num_hidden_layers''': '''n_layers''',
       '''n_words''': '''vocab_size''',  # For backward compatibility
   }


   def __init__(   self	:				Union[str, Any]     ,     lowerCAmelCase_	:				Union[str, Any]=False     ,     lowerCAmelCase_	:				List[str]=0.0     ,     lowerCAmelCase_	:				Dict=3_0_1_4_5     ,     lowerCAmelCase_	:				Tuple=2_0_4_8     ,     lowerCAmelCase_	:				List[Any]=1_2     ,     lowerCAmelCase_	:				str=1_6     ,     lowerCAmelCase_	:				Union[str, Any]=0.1     ,     lowerCAmelCase_	:				List[str]=0.1     ,     lowerCAmelCase_	:				str=True     ,     lowerCAmelCase_	:				Optional[int]=False     ,     lowerCAmelCase_	:				Any=False     ,     lowerCAmelCase_	:				int=False     ,     lowerCAmelCase_	:				int=1     ,     lowerCAmelCase_	:				List[str]=True     ,     lowerCAmelCase_	:				Dict=5_1_2     ,     lowerCAmelCase_	:				Any=2_0_4_8**-0.5     ,     lowerCAmelCase_	:				List[str]=1e-12     ,     lowerCAmelCase_	:				Any=0.02     ,     lowerCAmelCase_	:				Optional[Any]=0     ,     lowerCAmelCase_	:				Union[str, Any]=1     ,     lowerCAmelCase_	:				List[Any]=2     ,     lowerCAmelCase_	:				Dict=3     ,     lowerCAmelCase_	:				Optional[Any]=5     ,     lowerCAmelCase_	:				Any=True     ,     lowerCAmelCase_	:				Union[str, Any]="first"     ,     lowerCAmelCase_	:				List[Any]=True     ,     lowerCAmelCase_	:				Tuple=None     ,     lowerCAmelCase_	:				Optional[int]=True     ,     lowerCAmelCase_	:				int=0.1     ,     lowerCAmelCase_	:				Optional[int]=5     ,     lowerCAmelCase_	:				Union[str, Any]=5     ,     lowerCAmelCase_	:				List[Any]=0     ,     lowerCAmelCase_	:				Dict=0     ,     lowerCAmelCase_	:				Union[str, Any]=2     ,     lowerCAmelCase_	:				Optional[int]=0     ,     **lowerCAmelCase_	:				Any     ,     ):

      """simple docstring"""
      _A:    List[str]						     =			pre_norm
      _A:    List[Any]						     =			layerdrop
      _A:    Dict						     =			vocab_size
      _A:    List[Any]						     =			emb_dim
      _A:    Optional[Any]						     =			n_layers
      _A:    Union[str, Any]						     =			n_heads
      _A:    Optional[Any]						     =			dropout
      _A:    Optional[Any]						     =			attention_dropout
      _A:    Dict						     =			gelu_activation
      _A:    Tuple						     =			sinusoidal_embeddings
      _A:    Tuple						     =			causal
      _A:    List[Any]						     =			asm
      _A:    Dict						     =			n_langs
      _A:    Optional[int]						     =			use_lang_emb
      _A:    Any						     =			layer_norm_eps
      _A:    str						     =			bos_index
      _A:    Optional[int]						     =			eos_index
      _A:    int						     =			pad_index
      _A:    Tuple						     =			unk_index
      _A:    int						     =			mask_index
      _A:    int						     =			is_encoder
      _A:    Union[str, Any]						     =			max_position_embeddings
      _A:    List[str]						     =			embed_init_std
      _A:    List[str]						     =			init_std
      _A:    Tuple						     =			summary_type
      _A:    List[str]						     =			summary_use_proj
      _A:    Union[str, Any]						     =			summary_activation
      _A:    Union[str, Any]						     =			summary_proj_to_labels
      _A:    int						     =			summary_first_dropout
      _A:    Optional[Any]						     =			start_n_top
      _A:    Optional[Any]						     =			end_n_top
      _A:    Dict						     =			mask_token_id
      _A:    str						     =			lang_id

      if "n_words" in kwargs:
         _A:    int						     =			kwargs['''n_words''']

      super().__init__(pad_token_id=lowerCAmelCase_     ,     bos_token_id=lowerCAmelCase_     ,     **lowerCAmelCase_	)






class UpperCAmelCase  (		SCREAMING_SNAKE_CASE__  ):





   '''simple docstring'''


   @property
   def 					__magic_name__							(   self	:				Tuple	):

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



