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

       def __init__(							self					:     List[Any]			,			_lowerCAmelCase					:     int			,			_lowerCAmelCase					:     List[str]=13			,			_lowerCAmelCase					:     Dict=7			,			_lowerCAmelCase					:     int=True			,			_lowerCAmelCase					:     Tuple=True			,			_lowerCAmelCase					:     List[str]=False			,			_lowerCAmelCase					:     Union[str, Any]=True			,			_lowerCAmelCase					:     Tuple=99			,			_lowerCAmelCase					:     Any=64			,			_lowerCAmelCase					:     Dict=2			,			_lowerCAmelCase					:     List[str]=4			,			_lowerCAmelCase					:     Dict=64			,			_lowerCAmelCase					:     str="gelu"			,			_lowerCAmelCase					:     List[str]=0.1			,			_lowerCAmelCase					:     str=0.1			,			_lowerCAmelCase					:     Any=5_12			,			_lowerCAmelCase					:     Optional[int]=16			,			_lowerCAmelCase					:     Union[str, Any]=2			,			_lowerCAmelCase					:     Dict=0.02			,			_lowerCAmelCase					:     Tuple=3			,			_lowerCAmelCase					:     List[Any]=4			,			_lowerCAmelCase					:     List[str]=None			,			):
           __snake_case       :				str 			=    parent
           __snake_case       :				Union[str, Any] 			=    batch_size
           __snake_case       :				str 			=    seq_length
           __snake_case       :				List[str] 			=    is_training
           __snake_case       :				int 			=    use_input_mask
           __snake_case       :				List[str] 			=    use_token_type_ids
           __snake_case       :				List[Any] 			=    use_labels
           __snake_case       :				Union[str, Any] 			=    vocab_size
           __snake_case       :				List[Any] 			=    hidden_size
           __snake_case       :				Any 			=    num_hidden_layers
           __snake_case       :				Dict 			=    num_attention_heads
           __snake_case       :				Any 			=    intermediate_size
           __snake_case       :				Optional[int] 			=    hidden_act
           __snake_case       :				List[Any] 			=    hidden_dropout_prob
           __snake_case       :				int 			=    attention_probs_dropout_prob
           __snake_case       :				str 			=    max_position_embeddings
           __snake_case       :				Tuple 			=    type_vocab_size
           __snake_case       :				Optional[int] 			=    type_sequence_label_size
           __snake_case       :				List[Any] 			=    initializer_range
           __snake_case       :				Optional[Any] 			=    num_labels
           __snake_case       :				Any 			=    num_choices
           __snake_case       :				Tuple 			=    scope

       def 			snake_case__    (							self					:     Tuple			):
           __snake_case       :				Tuple 			=    ids_tensor([self.batch_size, self.seq_length]			,			self.vocab_size			)

           __snake_case       :				Tuple 			=    None
           if self.use_input_mask:
               __snake_case       :				int 			=    random_attention_mask([self.batch_size, self.seq_length]			)

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

           __snake_case       :				int 			=    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					:     int			,			_lowerCAmelCase					:     Optional[Any]			,			_lowerCAmelCase					:     Union[str, Any]			,			_lowerCAmelCase					:     int			,			_lowerCAmelCase					:     Any			,			_lowerCAmelCase					:     Optional[Any]			,			_lowerCAmelCase					:     int			):
           __snake_case       :				int 			=    TFMPNetModel(config=_lowerCAmelCase			)
           __snake_case       :				int 			=    {"""input_ids""": input_ids, """attention_mask""": input_mask}
           __snake_case       :				Dict 			=    model(_lowerCAmelCase			)
           __snake_case       :				Dict 			=    [input_ids, input_mask]
           __snake_case       :				Tuple 			=    model(_lowerCAmelCase			)
           self.parent.assertEqual(result.last_hidden_state.shape			,			(self.batch_size, self.seq_length, self.hidden_size)			)

       def 			snake_case__    (							self					:     List[Any]			,			_lowerCAmelCase					:     Any			,			_lowerCAmelCase					:     Dict			,			_lowerCAmelCase					:     List[Any]			,			_lowerCAmelCase					:     Optional[int]			,			_lowerCAmelCase					:     Optional[Any]			,			_lowerCAmelCase					:     Union[str, Any]			):
           __snake_case       :				str 			=    TFMPNetForMaskedLM(config=_lowerCAmelCase			)
           __snake_case       :				List[str] 			=    {"""input_ids""": input_ids, """attention_mask""": input_mask}
           __snake_case       :				List[Any] 			=    model(_lowerCAmelCase			)
           self.parent.assertEqual(result.logits.shape			,			(self.batch_size, self.seq_length, self.vocab_size)			)

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

       def 			snake_case__    (							self					:     int			,			_lowerCAmelCase					:     Optional[int]			,			_lowerCAmelCase					:     int			,			_lowerCAmelCase					:     Any			,			_lowerCAmelCase					:     Optional[int]			,			_lowerCAmelCase					:     List[str]			,			_lowerCAmelCase					:     Union[str, Any]			):
           __snake_case       :				List[Any] 			=    self.num_choices
           __snake_case       :				Dict 			=    TFMPNetForMultipleChoice(_lowerCAmelCase			)
           __snake_case       :				List[Any] 			=    tf.tile(tf.expand_dims(_lowerCAmelCase			,			1			)			,			(1, self.num_choices, 1)			)
           __snake_case       :				int 			=    tf.tile(tf.expand_dims(_lowerCAmelCase			,			1			)			,			(1, self.num_choices, 1)			)
           __snake_case       :				Optional[Any] 			=    {
               """input_ids""": multiple_choice_inputs_ids,
               """attention_mask""": multiple_choice_input_mask,
           }
           __snake_case       :				str 			=    model(_lowerCAmelCase			)
           self.parent.assertEqual(result.logits.shape			,			(self.batch_size, self.num_choices)			)

       def 			snake_case__    (							self					:     Union[str, Any]			,			_lowerCAmelCase					:     str			,			_lowerCAmelCase					:     Tuple			,			_lowerCAmelCase					:     Dict			,			_lowerCAmelCase					:     Optional[int]			,			_lowerCAmelCase					:     Tuple			,			_lowerCAmelCase					:     Any			):
           __snake_case       :				Any 			=    self.num_labels
           __snake_case       :				Any 			=    TFMPNetForTokenClassification(_lowerCAmelCase			)
           __snake_case       :				Union[str, Any] 			=    {"""input_ids""": input_ids, """attention_mask""": input_mask}
           __snake_case       :				str 			=    model(_lowerCAmelCase			)
           self.parent.assertEqual(result.logits.shape			,			(self.batch_size, self.seq_length, self.num_labels)			)





       def 			snake_case__    (							self					:     List[str]			):
           __snake_case       :				List[Any] 			=    self.prepare_config_and_inputs()
           ((__snake_case)			, (__snake_case)			, (__snake_case)			, (__snake_case)			, (__snake_case)			, (__snake_case))       :				str 			=    config_and_inputs
           __snake_case       :				str 			=    {"""input_ids""": input_ids, """attention_mask""": input_mask}
           return config, inputs_dict







@require_tf
class      SCREAMING_SNAKE_CASE__					(  __UpperCamelCase   ,							__UpperCamelCase   ,							unittest.TestCase    ):
       A							:	List[str]          =	(
           (
               TFMPNetForMaskedLM,
               TFMPNetForMultipleChoice,
               TFMPNetForQuestionAnswering,
               TFMPNetForSequenceClassification,
               TFMPNetForTokenClassification,
               TFMPNetModel,
           )
           if is_tf_available()
           else ()
       )
       A							:	str          =	(
           {
               "feature-extraction": TFMPNetModel,
               "fill-mask": TFMPNetForMaskedLM,
               "question-answering": TFMPNetForQuestionAnswering,
               "text-classification": TFMPNetForSequenceClassification,
               "token-classification": TFMPNetForTokenClassification,
               "zero-shot": TFMPNetForSequenceClassification,
           }
           if is_tf_available()
           else {}
       )
       A							:	Optional[Any]          =	False
       A							:	str          =	False

       def 			snake_case__    (							self					:     Any			):
           __snake_case       :				List[Any] 			=    TFMPNetModelTester(self			)
           __snake_case       :				Dict 			=    ConfigTester(self			,			config_class=_lowerCAmelCase			,			hidden_size=37			)

       def 			snake_case__    (							self					:     Optional[Any]			):
           self.config_tester.run_common_tests()

       def 			snake_case__    (							self					:     List[str]			):
           __snake_case       :				List[str] 			=    self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_mpnet_model(*_lowerCAmelCase			)

       def 			snake_case__    (							self					:     Any			):
           __snake_case       :				int 			=    self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_mpnet_for_masked_lm(*_lowerCAmelCase			)

       def 			snake_case__    (							self					:     List[Any]			):
           __snake_case       :				Any 			=    self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_mpnet_for_question_answering(*_lowerCAmelCase			)

       def 			snake_case__    (							self					:     Tuple			):
           __snake_case       :				str 			=    self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_mpnet_for_sequence_classification(*_lowerCAmelCase			)

       def 			snake_case__    (							self					:     str			):
           __snake_case       :				Dict 			=    self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_mpnet_for_multiple_choice(*_lowerCAmelCase			)

       def 			snake_case__    (							self					:     int			):
           __snake_case       :				int 			=    self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_mpnet_for_token_classification(*_lowerCAmelCase			)





       @slow
       def 			snake_case__    (							self					:     Tuple			):
           for model_name in ["microsoft/mpnet-base"]:
               __snake_case       :				List[str] 			=    TFMPNetModel.from_pretrained(_lowerCAmelCase			)
               self.assertIsNotNone(_lowerCAmelCase			)







@require_tf
class      SCREAMING_SNAKE_CASE__					(  unittest.TestCase    ):

       @slow
       def 			snake_case__    (							self					:     Dict			):
           __snake_case       :				Tuple 			=    TFMPNetModel.from_pretrained("""microsoft/mpnet-base"""			)
           __snake_case       :				Optional[int] 			=    tf.constant([[0, 1, 2, 3, 4, 5]]			)
           __snake_case       :				int 			=    model(_lowerCAmelCase			)[0]

           __snake_case       :				int 			=    [1, 6, 7_68]
           self.assertEqual(output.shape			,			_lowerCAmelCase			)

           __snake_case       :				int 			=    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			)




import functools
import operator

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


lowercase_  							=			logging.get_logger(__name__)

lowercase_  							=			{
    "facebook/hubert-base-ls960": "https://huggingface.co/facebook/hubert-base-ls960/resolve/main/config.json",
    # See all Hubert models at https://huggingface.co/models?filter=hubert
}







class      SCREAMING_SNAKE_CASE__					(  __UpperCamelCase    ):
       A							:	List[str]          =	"hubert"

       def __init__(							self					:     List[str]			,			_lowerCAmelCase					:     Union[str, Any]=32			,			_lowerCAmelCase					:     Any=7_68			,			_lowerCAmelCase					:     Optional[Any]=12			,			_lowerCAmelCase					:     Optional[int]=12			,			_lowerCAmelCase					:     int=30_72			,			_lowerCAmelCase					:     Optional[Any]="gelu"			,			_lowerCAmelCase					:     List[str]=0.1			,			_lowerCAmelCase					:     Any=0.1			,			_lowerCAmelCase					:     List[str]=0.1			,			_lowerCAmelCase					:     Dict=True			,			_lowerCAmelCase					:     Dict=0.0			,			_lowerCAmelCase					:     List[str]=0.1			,			_lowerCAmelCase					:     Optional[int]=0.1			,			_lowerCAmelCase					:     Dict=0.02			,			_lowerCAmelCase					:     Any=1e-5			,			_lowerCAmelCase					:     str="group"			,			_lowerCAmelCase					:     Any="gelu"			,			_lowerCAmelCase					:     List[str]=(5_12, 5_12, 5_12, 5_12, 5_12, 5_12, 5_12)			,			_lowerCAmelCase					:     Any=(5, 2, 2, 2, 2, 2, 2)			,			_lowerCAmelCase					:     Dict=(10, 3, 3, 3, 3, 2, 2)			,			_lowerCAmelCase					:     List[str]=False			,			_lowerCAmelCase					:     Optional[Any]=1_28			,			_lowerCAmelCase					:     int=16			,			_lowerCAmelCase					:     Tuple=False			,			_lowerCAmelCase					:     int=True			,			_lowerCAmelCase					:     int=0.05			,			_lowerCAmelCase					:     Any=10			,			_lowerCAmelCase					:     Dict=2			,			_lowerCAmelCase					:     Dict=0.0			,			_lowerCAmelCase					:     Optional[int]=10			,			_lowerCAmelCase					:     Union[str, Any]=0			,			_lowerCAmelCase					:     int="sum"			,			_lowerCAmelCase					:     Optional[int]=False			,			_lowerCAmelCase					:     str=False			,			_lowerCAmelCase					:     Optional[Any]=2_56			,			_lowerCAmelCase					:     Any=0			,			_lowerCAmelCase					:     List[Any]=1			,			_lowerCAmelCase					:     List[str]=2			,			**_lowerCAmelCase					:     Optional[int]			,			):
           super().__init__(**_lowerCAmelCase			,			pad_token_id=_lowerCAmelCase			,			bos_token_id=_lowerCAmelCase			,			eos_token_id=_lowerCAmelCase			)
           __snake_case       :				Optional[int] 			=    hidden_size
           __snake_case       :				Optional[Any] 			=    feat_extract_norm
           __snake_case       :				Optional[int] 			=    feat_extract_activation
           __snake_case       :				List[str] 			=    list(_lowerCAmelCase			)
           __snake_case       :				Any 			=    list(_lowerCAmelCase			)
           __snake_case       :				List[Any] 			=    list(_lowerCAmelCase			)
           __snake_case       :				Any 			=    conv_bias
           __snake_case       :				Tuple 			=    num_conv_pos_embeddings
           __snake_case       :				List[Any] 			=    num_conv_pos_embedding_groups
           __snake_case       :				Union[str, Any] 			=    len(self.conv_dim			)
           __snake_case       :				List[Any] 			=    num_hidden_layers
           __snake_case       :				Tuple 			=    intermediate_size
           __snake_case       :				Optional[int] 			=    hidden_act
           __snake_case       :				Union[str, Any] 			=    num_attention_heads
           __snake_case       :				str 			=    hidden_dropout
           __snake_case       :				Union[str, Any] 			=    attention_dropout
           __snake_case       :				Dict 			=    activation_dropout
           __snake_case       :				List[Any] 			=    feat_proj_layer_norm
           __snake_case       :				List[str] 			=    feat_proj_dropout
           __snake_case       :				List[Any] 			=    final_dropout
           __snake_case       :				str 			=    layerdrop
           __snake_case       :				List[str] 			=    layer_norm_eps
           __snake_case       :				Optional[Any] 			=    initializer_range
           __snake_case       :				Optional[int] 			=    vocab_size
           __snake_case       :				Dict 			=    do_stable_layer_norm
           __snake_case       :				Tuple 			=    use_weighted_layer_sum
           __snake_case       :				List[Any] 			=    classifier_proj_size

           if (
               (len(self.conv_stride			) != self.num_feat_extract_layers)
               or (len(self.conv_kernel			) != self.num_feat_extract_layers)
               or (len(self.conv_dim			) != self.num_feat_extract_layers)
           ):
               raise ValueError(
                   """Configuration for convolutional layers is incorrect. It is required that `len(config.conv_dim)` =="""
                   """ `len(config.conv_stride)` == `len(config.conv_kernel)`, but is `len(config.conv_dim) ="""
                   f''' {len(self.conv_dim			)}`, `len(config.conv_stride) = {len(self.conv_stride			)}`,'''
                   f''' `len(config.conv_kernel) = {len(self.conv_kernel			)}`.'''			)

           # fine-tuning config parameters for SpecAugment: https://arxiv.org/abs/1904.08779
           __snake_case       :				Union[str, Any] 			=    apply_spec_augment
           __snake_case       :				List[str] 			=    mask_time_prob
           __snake_case       :				Dict 			=    mask_time_length
           __snake_case       :				int 			=    mask_time_min_masks
           __snake_case       :				List[str] 			=    mask_feature_prob
           __snake_case       :				Tuple 			=    mask_feature_length
           __snake_case       :				int 			=    mask_feature_min_masks

           # ctc loss
           __snake_case       :				Dict 			=    ctc_loss_reduction
           __snake_case       :				List[str] 			=    ctc_zero_infinity





       @property
       def 			snake_case__    (							self					:     Any			):
           return functools.reduce(operator.mul			,			self.conv_stride			,			1			)



