



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

   '''simple docstring'''




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




         """simple docstring"""

         lowercase__     =  parent
         lowercase__     =  batch_size
         lowercase__     =  seq_length
         lowercase__     =  is_training
         lowercase__     =  use_input_mask
         lowercase__     =  use_token_type_ids
         lowercase__     =  use_labels
         lowercase__     =  vocab_size
         lowercase__     =  hidden_size
         lowercase__     =  num_hidden_layers
         lowercase__     =  num_attention_heads
         lowercase__     =  intermediate_size
         lowercase__     =  hidden_act
         lowercase__     =  hidden_dropout_prob
         lowercase__     =  attention_probs_dropout_prob
         lowercase__     =  max_position_embeddings
         lowercase__     =  type_vocab_size
         lowercase__     =  type_sequence_label_size
         lowercase__     =  initializer_range
         lowercase__     =  num_labels
         lowercase__     =  num_choices
         lowercase__     =  scope


   def 			UpperCAmelCase		(						self  :			Tuple)	->				Optional[int]:




         """simple docstring"""

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


   def 			UpperCAmelCase		(						self  :			Optional[Any])	->				Tuple:




         """simple docstring"""

         lowercase__     =  ids_tensor([self.batch_size, self.seq_length]			,				self.vocab_size)

         lowercase__     =  None
         if self.use_input_mask:
               lowercase__     =  random_attention_mask([self.batch_size, self.seq_length])

         lowercase__     =  None
         lowercase__     =  None
         lowercase__     =  None
         if self.use_labels:
               lowercase__     =  ids_tensor([self.batch_size]			,				self.type_sequence_label_size)
               lowercase__     =  ids_tensor([self.batch_size, self.seq_length]			,				self.num_labels)
               lowercase__     =  ids_tensor([self.batch_size]			,				self.num_choices)

         lowercase__     =  self.get_config()
         return config, input_ids, input_mask, sequence_labels, token_labels, choice_labels


   def 			UpperCAmelCase		(						self  :			Optional[Any])	->				int:




         """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 			UpperCAmelCase		(						self  :			Any			,				lowerCAmelCase  :			List[str]			,				lowerCAmelCase  :			Optional[int]			,				lowerCAmelCase  :			int			,				lowerCAmelCase  :			int			,				lowerCAmelCase  :			str			,				lowerCAmelCase  :			str)	->				int:




         """simple docstring"""

         lowercase__     =  MPNetModel(config=lowerCAmelCase)
         model.to(lowerCAmelCase)
         model.eval()
         lowercase__     =  model(lowerCAmelCase			,				lowerCAmelCase)
         lowercase__     =  model(lowerCAmelCase)
         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 			UpperCAmelCase		(						self  :			Optional[int]			,				lowerCAmelCase  :			List[Any]			,				lowerCAmelCase  :			int			,				lowerCAmelCase  :			List[Any]			,				lowerCAmelCase  :			Optional[int]			,				lowerCAmelCase  :			Optional[int]			,				lowerCAmelCase  :			Any)	->				List[Any]:




         """simple docstring"""

         lowercase__     =  MPNetForQuestionAnswering(config=lowerCAmelCase)
         model.to(lowerCAmelCase)
         model.eval()
         lowercase__     =  model(
             lowerCAmelCase			,				attention_mask=lowerCAmelCase			,				start_positions=lowerCAmelCase			,				end_positions=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 			UpperCAmelCase		(						self  :			List[Any]			,				lowerCAmelCase  :			int			,				lowerCAmelCase  :			str			,				lowerCAmelCase  :			List[str]			,				lowerCAmelCase  :			Dict			,				lowerCAmelCase  :			List[str]			,				lowerCAmelCase  :			Any)	->				int:




         """simple docstring"""

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


   def 			UpperCAmelCase		(						self  :			Optional[int]			,				lowerCAmelCase  :			Optional[int]			,				lowerCAmelCase  :			Any			,				lowerCAmelCase  :			Any			,				lowerCAmelCase  :			Optional[Any]			,				lowerCAmelCase  :			str			,				lowerCAmelCase  :			Optional[Any])	->				str:




         """simple docstring"""

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


   def 			UpperCAmelCase		(						self  :			Optional[int]			,				lowerCAmelCase  :			Tuple			,				lowerCAmelCase  :			Tuple			,				lowerCAmelCase  :			str			,				lowerCAmelCase  :			Optional[int]			,				lowerCAmelCase  :			Optional[Any]			,				lowerCAmelCase  :			Dict)	->				Any:




         """simple docstring"""

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



   def 			UpperCAmelCase		(						self  :			Union[str, Any])	->				Tuple:




         """simple docstring"""

         lowercase__     =  self.prepare_config_and_inputs()
         ((lowercase__),	(lowercase__),	(lowercase__),	(lowercase__),	(lowercase__),	(lowercase__))     =  config_and_inputs
         lowercase__     =  {'input_ids': input_ids, 'attention_mask': input_mask}
         return config, inputs_dict



@require_torch
class       UpperCAmelCase__(							lowerCamelCase     ,							lowerCamelCase     ,							unittest.TestCase	):

   '''simple docstring'''




   A    :  Tuple       				=    (
       (
           MPNetForMaskedLM,
           MPNetForMultipleChoice,
           MPNetForQuestionAnswering,
           MPNetForSequenceClassification,
           MPNetForTokenClassification,
           MPNetModel,
       )
       if is_torch_available()
       else ()
   )
   A    :  List[Any]       				=    (
       {
           "feature-extraction": MPNetModel,
           "fill-mask": MPNetForMaskedLM,
           "question-answering": MPNetForQuestionAnswering,
           "text-classification": MPNetForSequenceClassification,
           "token-classification": MPNetForTokenClassification,
           "zero-shot": MPNetForSequenceClassification,
       }
       if is_torch_available()
       else {}
   )
   A    :  List[Any]       				=    False
   A    :  Dict       				=    True


   def 			UpperCAmelCase		(						self  :			str)	->				Any:




         """simple docstring"""

         lowercase__     =  MPNetModelTester(self)
         lowercase__     =  ConfigTester(self			,				config_class=lowerCAmelCase			,				hidden_size=37)


   def 			UpperCAmelCase		(						self  :			Any)	->				Any:




         """simple docstring"""

         self.config_tester.run_common_tests()


   def 			UpperCAmelCase		(						self  :			Dict)	->				str:




         """simple docstring"""

         lowercase__     =  self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_mpnet_model(*lowerCAmelCase)


   def 			UpperCAmelCase		(						self  :			Tuple)	->				Tuple:




         """simple docstring"""

         lowercase__     =  self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_mpnet_for_sequence_classification(*lowerCAmelCase)


   def 			UpperCAmelCase		(						self  :			List[Any])	->				Dict:




         """simple docstring"""

         lowercase__     =  self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_mpnet_for_multiple_choice(*lowerCAmelCase)


   def 			UpperCAmelCase		(						self  :			Optional[int])	->				List[Any]:




         """simple docstring"""

         lowercase__     =  self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_mpnet_for_token_classification(*lowerCAmelCase)



   def 			UpperCAmelCase		(						self  :			Any)	->				int:




         """simple docstring"""

         lowercase__     =  self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_mpnet_for_question_answering(*lowerCAmelCase)



@require_torch
class       UpperCAmelCase__(							unittest.TestCase	):

   '''simple docstring'''




   @slow
   def 			UpperCAmelCase		(						self  :			int)	->				Tuple:




         """simple docstring"""

         lowercase__     =  MPNetModel.from_pretrained('microsoft/mpnet-base')
         lowercase__     =  torch.tensor([[0, 3_45, 2_32, 3_28, 7_40, 1_40, 16_95, 69, 60_78, 15_88, 2]])
         lowercase__     =  model(lowerCAmelCase)[0]
         lowercase__     =  torch.Size((1, 11, 7_68))
         self.assertEqual(output.shape			,				lowerCAmelCase)
         lowercase__     =  torch.tensor(
             [[[-0.05_50, 0.19_43, -0.07_40], [-0.05_62, 0.22_11, -0.05_79], [-0.04_37, 0.33_37, -0.06_41]]])
         # compare the actual values for a slice.
         self.assertTrue(torch.allclose(output[:, :3, :3]			,				lowerCAmelCase			,				atol=1E-4))





from ...configuration_utils import PretrainedConfig
from ...utils import logging


a__      :				Dict   	=			logging.get_logger(__name__)

a__      :				List[Any]   	=			{
    "facebook/s2t-small-librispeech-asr": (
        "https://huggingface.co/facebook/s2t-small-librispeech-asr/resolve/main/config.json"
    ),
    # See all Speech2Text models at https://huggingface.co/models?filter=speech_to_text
}



class       UpperCAmelCase__(							lowerCamelCase	):

   '''simple docstring'''




   A    :  int       				=    "speech_to_text"
   A    :  Optional[Any]       				=    ["past_key_values"]
   A    :  Optional[int]       				=    {"num_attention_heads": "encoder_attention_heads", "hidden_size": "d_model"}


   def __init__(						self  :			Optional[int]			,				lowerCAmelCase  :			Tuple=1_00_00			,				lowerCAmelCase  :			int=12			,				lowerCAmelCase  :			int=20_48			,				lowerCAmelCase  :			Union[str, Any]=4			,				lowerCAmelCase  :			str=6			,				lowerCAmelCase  :			Dict=20_48			,				lowerCAmelCase  :			Dict=4			,				lowerCAmelCase  :			Optional[int]=0.0			,				lowerCAmelCase  :			Union[str, Any]=0.0			,				lowerCAmelCase  :			int=True			,				lowerCAmelCase  :			Optional[Any]=True			,				lowerCAmelCase  :			Dict="relu"			,				lowerCAmelCase  :			Tuple=2_56			,				lowerCAmelCase  :			Dict=0.1			,				lowerCAmelCase  :			Optional[Any]=0.0			,				lowerCAmelCase  :			List[Any]=0.0			,				lowerCAmelCase  :			Any=0.02			,				lowerCAmelCase  :			List[Any]=2			,				lowerCAmelCase  :			Tuple=True			,				lowerCAmelCase  :			Tuple=1			,				lowerCAmelCase  :			List[str]=0			,				lowerCAmelCase  :			Union[str, Any]=2			,				lowerCAmelCase  :			Any=60_00			,				lowerCAmelCase  :			Optional[int]=10_24			,				lowerCAmelCase  :			Optional[Any]=2			,				lowerCAmelCase  :			Optional[Any]=(5, 5)			,				lowerCAmelCase  :			Union[str, Any]=10_24			,				lowerCAmelCase  :			List[Any]=80			,				lowerCAmelCase  :			List[str]=1			,				**lowerCAmelCase  :			List[str]			,				)	->				Dict:




         """simple docstring"""

         lowercase__     =  vocab_size
         lowercase__     =  d_model
         lowercase__     =  encoder_ffn_dim
         lowercase__     =  encoder_layers
         lowercase__     =  encoder_attention_heads
         lowercase__     =  decoder_ffn_dim
         lowercase__     =  decoder_layers
         lowercase__     =  decoder_attention_heads
         lowercase__     =  dropout
         lowercase__     =  attention_dropout
         lowercase__     =  activation_dropout
         lowercase__     =  activation_function
         lowercase__     =  init_std
         lowercase__     =  encoder_layerdrop
         lowercase__     =  decoder_layerdrop
         lowercase__     =  use_cache
         lowercase__     =  encoder_layers
         lowercase__     =  scale_embedding  # scale factor will be sqrt(d_model) if True
         lowercase__     =  max_source_positions
         lowercase__     =  max_target_positions
         lowercase__     =  num_conv_layers
         lowercase__     =  list(lowerCAmelCase)
         lowercase__     =  conv_channels
         lowercase__     =  input_feat_per_channel
         lowercase__     =  input_channels

         if len(self.conv_kernel_sizes) != self.num_conv_layers:
               raise ValueError(
                   'Configuration for convolutional module is incorrect. '
                   'It is required that `len(config.conv_kernel_sizes)` == `config.num_conv_layers` '
                   f'''but is `len(config.conv_kernel_sizes) = {len(self.conv_kernel_sizes)}`, '''
                   f'''`config.num_conv_layers = {self.num_conv_layers}`.''')

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