




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

      """simple docstring"""

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







      def 					snake_case		(   self    :       List[str]       ,				snake_case    :       int       ,				snake_case    :       int       ,				snake_case    :       str       ,				snake_case    :       int       ,				snake_case    :       List[Any]       ,				snake_case    :       Dict ):
          __UpperCamelCase     						=  TFMPNetForMaskedLM(config=_lowerCamelCase )
          __UpperCamelCase     						=  {'''input_ids''': input_ids, '''attention_mask''': input_mask}
          __UpperCamelCase     						=  model(_lowerCamelCase )
          self.parent.assertEqual(result.logits.shape       ,				(self.batch_size, self.seq_length, self.vocab_size) )







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







      def 					snake_case		(   self    :       int       ,				snake_case    :       List[Any]       ,				snake_case    :       List[Any]       ,				snake_case    :       Union[str, Any]       ,				snake_case    :       str       ,				snake_case    :       int       ,				snake_case    :       Tuple ):
          __UpperCamelCase     						=  self.num_choices
          __UpperCamelCase     						=  TFMPNetForMultipleChoice(_lowerCamelCase )
          __UpperCamelCase     						=  tf.tile(tf.expand_dims(_lowerCamelCase       ,				1 )       ,				(1, self.num_choices, 1) )
          __UpperCamelCase     						=  tf.tile(tf.expand_dims(_lowerCamelCase       ,				1 )       ,				(1, self.num_choices, 1) )
          __UpperCamelCase     						=  {
              '''input_ids''': multiple_choice_inputs_ids,
              '''attention_mask''': multiple_choice_input_mask,
          }
          __UpperCamelCase     						=  model(_lowerCamelCase )
          self.parent.assertEqual(result.logits.shape       ,				(self.batch_size, self.num_choices) )







      def 					snake_case		(   self    :       str       ,				snake_case    :       Union[str, Any]       ,				snake_case    :       Union[str, Any]       ,				snake_case    :       Optional[Any]       ,				snake_case    :       Optional[int]       ,				snake_case    :       Optional[int]       ,				snake_case    :       List[Any] ):
          __UpperCamelCase     						=  self.num_labels
          __UpperCamelCase     						=  TFMPNetForTokenClassification(_lowerCamelCase )
          __UpperCamelCase     						=  {'''input_ids''': input_ids, '''attention_mask''': input_mask}
          __UpperCamelCase     						=  model(_lowerCamelCase )
          self.parent.assertEqual(result.logits.shape       ,				(self.batch_size, self.seq_length, self.num_labels) )







      def 					snake_case		(   self    :       Union[str, Any] ):
          __UpperCamelCase     						=  self.prepare_config_and_inputs()
          (__UpperCamelCase)     						=  config_and_inputs
          __UpperCamelCase     						=  {'''input_ids''': input_ids, '''attention_mask''': input_mask}
          return config, inputs_dict







@require_tf
class        _lowerCamelCase  (	__UpperCAmelCase							,	__UpperCAmelCase							,	unittest.TestCase      ):

      """simple docstring"""

      lowerCAmelCase__	:       List[str]			=    (
          (
              TFMPNetForMaskedLM,
              TFMPNetForMultipleChoice,
              TFMPNetForQuestionAnswering,
              TFMPNetForSequenceClassification,
              TFMPNetForTokenClassification,
              TFMPNetModel,
          )
          if is_tf_available()
          else ()
      )
      lowerCAmelCase__	:       Optional[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__	:       List[Any]			=    False
      lowerCAmelCase__	:       str			=    False







      def 					snake_case		(   self    :       Any ):
          __UpperCamelCase     						=  TFMPNetModelTester(self )
          __UpperCamelCase     						=  ConfigTester(self       ,				config_class=_lowerCamelCase       ,				hidden_size=37 )







      def 					snake_case		(   self    :       Any ):
          self.config_tester.run_common_tests()







      def 					snake_case		(   self    :       List[Any] ):
          __UpperCamelCase     						=  self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_mpnet_model(*_lowerCamelCase )







      def 					snake_case		(   self    :       Optional[Any] ):
          __UpperCamelCase     						=  self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_mpnet_for_masked_lm(*_lowerCamelCase )







      def 					snake_case		(   self    :       Tuple ):
          __UpperCamelCase     						=  self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_mpnet_for_question_answering(*_lowerCamelCase )







      def 					snake_case		(   self    :       Dict ):
          __UpperCamelCase     						=  self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_mpnet_for_sequence_classification(*_lowerCamelCase )







      def 					snake_case		(   self    :       Dict ):
          __UpperCamelCase     						=  self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_mpnet_for_multiple_choice(*_lowerCamelCase )







      def 					snake_case		(   self    :       Dict ):
          __UpperCamelCase     						=  self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_mpnet_for_token_classification(*_lowerCamelCase )







      @slow
      def 					snake_case		(   self    :       int ):
          for model_name in ["microsoft/mpnet-base"]:
              __UpperCamelCase     						=  TFMPNetModel.from_pretrained(_lowerCamelCase )
              self.assertIsNotNone(_lowerCamelCase )







@require_tf
class        _lowerCamelCase  (	unittest.TestCase      ):

      """simple docstring"""

      @slow
      def 					snake_case		(   self    :       str ):
          __UpperCamelCase     						=  TFMPNetModel.from_pretrained('''microsoft/mpnet-base''' )
          __UpperCamelCase     						=  tf.constant([[0, 1, 2, 3, 4, 5]] )
          __UpperCamelCase     						=  model(_lowerCamelCase )[0]

          __UpperCamelCase     						=  [1, 6, 768]
          self.assertEqual(output.shape       ,				_lowerCamelCase )

          __UpperCamelCase     						=  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 )













"""simple docstring"""





from collections import OrderedDict
from typing import Any, Mapping, Optional

from ... import PreTrainedTokenizer
from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfig, OnnxSeqaSeqConfigWithPast
from ...onnx.utils import compute_effective_axis_dimension
from ...utils import TensorType, is_torch_available, logging


_lowerCamelCase       =	logging.get_logger(__name__)

_lowerCamelCase       =	{
    '''facebook/m2m100_418M''': '''https://huggingface.co/facebook/m2m100_418M/resolve/main/config.json''',
    # See all M2M100 models at https://huggingface.co/models?filter=m2m_100
}





class 			snake_case		(   __UpperCAmelCase   ):
      lowerCamelCase__						= '''m2m_100'''
      lowerCamelCase__						= ['''past_key_values''']
      lowerCamelCase__						= {'''num_attention_heads''': '''encoder_attention_heads''', '''hidden_size''': '''d_model'''}

      def __init__(			self   :int						,							_lowerCamelCase   :Union[str, Any]=1_2_8_1_1_2						,							_lowerCamelCase   :str=1_0_2_4						,							_lowerCamelCase   :Any=1_2						,							_lowerCamelCase   :Dict=4_0_9_6						,							_lowerCamelCase   :List[str]=1_6						,							_lowerCamelCase   :Union[str, Any]=1_2						,							_lowerCamelCase   :Any=4_0_9_6						,							_lowerCamelCase   :str=1_6						,							_lowerCamelCase   :Union[str, Any]=0.0_5						,							_lowerCamelCase   :Any=0.0_5						,							_lowerCamelCase   :int=True						,							_lowerCamelCase   :Dict=True						,							_lowerCamelCase   :List[str]="relu"						,							_lowerCamelCase   :List[Any]=1_0_2_4						,							_lowerCamelCase   :Dict=0.1						,							_lowerCamelCase   :List[Any]=0.1						,							_lowerCamelCase   :Any=0.0						,							_lowerCamelCase   :Tuple=0.0_2						,							_lowerCamelCase   :Dict=2						,							_lowerCamelCase   :List[str]=True						,							_lowerCamelCase   :Any=1						,							_lowerCamelCase   :Dict=0						,							_lowerCamelCase   :List[Any]=2						,							**_lowerCamelCase   :Optional[Any]						,							):
       __SCREAMING_SNAKE_CASE :			Any												=       vocab_size
       __SCREAMING_SNAKE_CASE :			Any												=       max_position_embeddings
       __SCREAMING_SNAKE_CASE :			int												=       d_model
       __SCREAMING_SNAKE_CASE :			List[Any]												=       encoder_ffn_dim
       __SCREAMING_SNAKE_CASE :			Dict												=       encoder_layers
       __SCREAMING_SNAKE_CASE :			int												=       encoder_attention_heads
       __SCREAMING_SNAKE_CASE :			Dict												=       decoder_ffn_dim
       __SCREAMING_SNAKE_CASE :			Union[str, Any]												=       decoder_layers
       __SCREAMING_SNAKE_CASE :			Optional[Any]												=       decoder_attention_heads
       __SCREAMING_SNAKE_CASE :			Optional[Any]												=       dropout
       __SCREAMING_SNAKE_CASE :			Union[str, Any]												=       attention_dropout
       __SCREAMING_SNAKE_CASE :			List[str]												=       activation_dropout
       __SCREAMING_SNAKE_CASE :			List[Any]												=       activation_function
       __SCREAMING_SNAKE_CASE :			Any												=       init_std
       __SCREAMING_SNAKE_CASE :			List[Any]												=       encoder_layerdrop
       __SCREAMING_SNAKE_CASE :			Union[str, Any]												=       decoder_layerdrop
       __SCREAMING_SNAKE_CASE :			Union[str, Any]												=       use_cache
       __SCREAMING_SNAKE_CASE :			List[str]												=       encoder_layers
       __SCREAMING_SNAKE_CASE :			str												=       scale_embedding  # scale factor will be sqrt(d_model) if True

       super().__init__(
           pad_token_id=_lowerCamelCase						,							bos_token_id=_lowerCamelCase						,							eos_token_id=_lowerCamelCase						,							is_encoder_decoder=_lowerCamelCase						,							decoder_start_token_id=_lowerCamelCase						,							**_lowerCamelCase						,							)







class 			snake_case		(   __UpperCAmelCase   ):

      @property
      def 						SCREAMING_SNAKE_CASE_			(			self   :int						):
       __SCREAMING_SNAKE_CASE :			str												=       OrderedDict(
           [
               ('''input_ids''', {0: '''batch''', 1: '''encoder_sequence'''}),
               ('''attention_mask''', {0: '''batch''', 1: '''encoder_sequence'''}),
           ]						)

       if self.use_past:
        __SCREAMING_SNAKE_CASE :			Dict												=       {0: '''batch'''}
        __SCREAMING_SNAKE_CASE :			Any												=       {0: '''batch''', 1: '''past_decoder_sequence + sequence'''}
       else:
        __SCREAMING_SNAKE_CASE :			int												=       {0: '''batch''', 1: '''decoder_sequence'''}
        __SCREAMING_SNAKE_CASE :			List[Any]												=       {0: '''batch''', 1: '''decoder_sequence'''}

       if self.use_past:
        self.fill_with_past_key_values_(_lowerCamelCase						,							direction='''inputs'''						)
       return common_inputs

      def 						SCREAMING_SNAKE_CASE_			(			self   :Tuple						,							_lowerCamelCase   :PreTrainedTokenizer						,							_lowerCamelCase   :int = -1						,							_lowerCamelCase   :int = -1						,							_lowerCamelCase   :bool = False						,							_lowerCamelCase   :Optional[TensorType] = None						,							):
       # Copied from OnnxConfig.generate_dummy_inputs
       # Did not use super(OnnxConfigWithPast, self).generate_dummy_inputs for code clarity.
       # If dynamic axis (-1) we forward with a fixed dimension of 2 samples to avoid optimizations made by ONNX
       __SCREAMING_SNAKE_CASE :			List[Any]												=       compute_effective_axis_dimension(
           _lowerCamelCase						,							fixed_dimension=OnnxConfig.default_fixed_batch						,							num_token_to_add=0						)

       # If dynamic axis (-1) we forward with a fixed dimension of 8 tokens to avoid optimizations made by ONNX
       __SCREAMING_SNAKE_CASE :			List[str]												=       tokenizer.num_special_tokens_to_add(_lowerCamelCase						)
       __SCREAMING_SNAKE_CASE :			int												=       compute_effective_axis_dimension(
           _lowerCamelCase						,							fixed_dimension=OnnxConfig.default_fixed_sequence						,							num_token_to_add=_lowerCamelCase						)

       # Generate dummy inputs according to compute batch and sequence
       __SCREAMING_SNAKE_CASE :			int												=       [''' '''.join([tokenizer.unk_token]						) * seq_length] * batch_size
       __SCREAMING_SNAKE_CASE :			Union[str, Any]												=       dict(tokenizer(_lowerCamelCase						,							return_tensors=_lowerCamelCase						)						)
       return common_inputs

      def 						SCREAMING_SNAKE_CASE_			(			self   :str						,							_lowerCamelCase   :PreTrainedTokenizer						,							_lowerCamelCase   :int = -1						,							_lowerCamelCase   :int = -1						,							_lowerCamelCase   :bool = False						,							_lowerCamelCase   :Optional[TensorType] = None						,							):
       __SCREAMING_SNAKE_CASE :			Dict												=       self._generate_dummy_inputs_for_sequence_classification_and_question_answering(
           _lowerCamelCase						,							_lowerCamelCase						,							_lowerCamelCase						,							_lowerCamelCase						,							_lowerCamelCase						)

       # Generate decoder inputs
       __SCREAMING_SNAKE_CASE :			Dict												=       seq_length if not self.use_past else 1
       __SCREAMING_SNAKE_CASE :			Any												=       self._generate_dummy_inputs_for_sequence_classification_and_question_answering(
           _lowerCamelCase						,							_lowerCamelCase						,							_lowerCamelCase						,							_lowerCamelCase						,							_lowerCamelCase						)
       __SCREAMING_SNAKE_CASE :			Optional[int]												=       {f'''decoder_{name}''': tensor for name, tensor in decoder_inputs.items()}
       __SCREAMING_SNAKE_CASE :			Optional[Any]												=       dict(**_lowerCamelCase						,							**_lowerCamelCase						)

       if self.use_past:
        if not is_torch_available():
         raise ValueError('''Cannot generate dummy past_keys inputs without PyTorch installed.'''						)
        else:
         import torch
        __SCREAMING_SNAKE_CASE							,      __SCREAMING_SNAKE_CASE :			int												=       common_inputs['''input_ids'''].shape
        __SCREAMING_SNAKE_CASE :			Tuple												=       common_inputs['''decoder_input_ids'''].shape[1]
        __SCREAMING_SNAKE_CASE							,      __SCREAMING_SNAKE_CASE :			Optional[Any]												=       self.num_attention_heads
        __SCREAMING_SNAKE_CASE :			List[str]												=       (
            batch,
            num_encoder_attention_heads,
            encoder_seq_length,
            self._config.hidden_size // num_encoder_attention_heads,
        )
        __SCREAMING_SNAKE_CASE :			Union[str, Any]												=       decoder_seq_length + 3
        __SCREAMING_SNAKE_CASE :			List[str]												=       (
            batch,
            num_decoder_attention_heads,
            decoder_past_length,
            self._config.hidden_size // num_decoder_attention_heads,
        )

        __SCREAMING_SNAKE_CASE :			Any												=       torch.cat(
            [common_inputs['''decoder_attention_mask'''], torch.ones(_lowerCamelCase						,							_lowerCamelCase						)]						,							dim=1						)

        __SCREAMING_SNAKE_CASE :			Union[str, Any]												=       []
        # If the number of encoder and decoder layers are present in the model configuration, both are considered
        __SCREAMING_SNAKE_CASE							,      __SCREAMING_SNAKE_CASE :			Union[str, Any]												=       self.num_layers
        __SCREAMING_SNAKE_CASE :			Any												=       min(_lowerCamelCase						,							_lowerCamelCase						)
        __SCREAMING_SNAKE_CASE :			Tuple												=       max(_lowerCamelCase						,							_lowerCamelCase						) - min_num_layers
        __SCREAMING_SNAKE_CASE :			Optional[int]												=       '''encoder''' if num_encoder_layers > num_decoder_layers else '''decoder'''

        for _ in range(_lowerCamelCase						):
         common_inputs["past_key_values"].append(
             (
                 torch.zeros(_lowerCamelCase						),
                 torch.zeros(_lowerCamelCase						),
                 torch.zeros(_lowerCamelCase						),
                 torch.zeros(_lowerCamelCase						),
             )						)
        # TODO: test this.
        __SCREAMING_SNAKE_CASE :			Any												=       encoder_shape if remaining_side_name == '''encoder''' else decoder_shape
        for _ in range(_lowerCamelCase						,							_lowerCamelCase						):
         common_inputs["past_key_values"].append((torch.zeros(_lowerCamelCase						), torch.zeros(_lowerCamelCase						))						)
       return common_inputs



      lowerCamelCase__						= _generate_dummy_inputs_for_default_and_seqaseq_lm

