

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

 '''simple docstring'''

 def __init__(     self		,				_UpperCAmelCase		,				_UpperCAmelCase=13		,				_UpperCAmelCase=7		,				_UpperCAmelCase=True		,				_UpperCAmelCase=True		,				_UpperCAmelCase=False		,				_UpperCAmelCase=True		,				_UpperCAmelCase=99		,				_UpperCAmelCase=64		,				_UpperCAmelCase=5		,				_UpperCAmelCase=4		,				_UpperCAmelCase=64		,				_UpperCAmelCase="gelu"		,				_UpperCAmelCase=0.1		,				_UpperCAmelCase=0.1		,				_UpperCAmelCase=5_12		,				_UpperCAmelCase=16		,				_UpperCAmelCase=2		,				_UpperCAmelCase=0.02		,				_UpperCAmelCase=3		,				_UpperCAmelCase=4		,				_UpperCAmelCase=None		,				):
       snake_case_	=				parent
       snake_case_	=				batch_size
       snake_case_	=				seq_length
       snake_case_	=				is_training
       snake_case_	=				use_input_mask
       snake_case_	=				use_token_type_ids
       snake_case_	=				use_labels
       snake_case_	=				vocab_size
       snake_case_	=				hidden_size
       snake_case_	=				num_hidden_layers
       snake_case_	=				num_attention_heads
       snake_case_	=				intermediate_size
       snake_case_	=				hidden_act
       snake_case_	=				hidden_dropout_prob
       snake_case_	=				attention_probs_dropout_prob
       snake_case_	=				max_position_embeddings
       snake_case_	=				type_vocab_size
       snake_case_	=				type_sequence_label_size
       snake_case_	=				initializer_range
       snake_case_	=				num_labels
       snake_case_	=				num_choices
       snake_case_	=				scope



 def 						UpperCamelCase__					(     self ):
       return MPNetConfig.from_pretrained('''microsoft/mpnet-base''' )



 def 						UpperCamelCase__					(     self ):
       snake_case_	=				ids_tensor([self.batch_size, self.seq_length]		,				self.vocab_size )

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

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

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



 def 						UpperCamelCase__					(     self ):
       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		,				_UpperCAmelCase		,				_UpperCAmelCase		,				_UpperCAmelCase		,				_UpperCAmelCase		,				_UpperCAmelCase		,				_UpperCAmelCase ):
       snake_case_	=				MPNetModel(config=UpperCamelCase_ )
       model.to(UpperCamelCase_ )
       model.eval()
       snake_case_	=				model(UpperCamelCase_		,				UpperCamelCase_ )
       snake_case_	=				model(UpperCamelCase_ )
       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		,				_UpperCAmelCase		,				_UpperCAmelCase		,				_UpperCAmelCase		,				_UpperCAmelCase		,				_UpperCAmelCase		,				_UpperCAmelCase ):
       snake_case_	=				MPNetForQuestionAnswering(config=UpperCamelCase_ )
       model.to(UpperCamelCase_ )
       model.eval()
       snake_case_	=				model(
           UpperCamelCase_		,				attention_mask=UpperCamelCase_		,				start_positions=UpperCamelCase_		,				end_positions=UpperCamelCase_		,				)
       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		,				_UpperCAmelCase		,				_UpperCAmelCase		,				_UpperCAmelCase		,				_UpperCAmelCase		,				_UpperCAmelCase		,				_UpperCAmelCase ):
       snake_case_	=				self.num_labels
       snake_case_	=				MPNetForSequenceClassification(UpperCamelCase_ )
       model.to(UpperCamelCase_ )
       model.eval()
       snake_case_	=				model(UpperCamelCase_		,				attention_mask=UpperCamelCase_		,				labels=UpperCamelCase_ )
       self.parent.assertEqual(result.logits.shape		,				(self.batch_size, self.num_labels) )



 def 						UpperCamelCase__					(     self		,				_UpperCAmelCase		,				_UpperCAmelCase		,				_UpperCAmelCase		,				_UpperCAmelCase		,				_UpperCAmelCase		,				_UpperCAmelCase ):
       snake_case_	=				self.num_choices
       snake_case_	=				MPNetForMultipleChoice(config=UpperCamelCase_ )
       model.to(UpperCamelCase_ )
       model.eval()
       snake_case_	=				input_ids.unsqueeze(1 ).expand(-1		,				self.num_choices		,				-1 ).contiguous()
       snake_case_	=				input_mask.unsqueeze(1 ).expand(-1		,				self.num_choices		,				-1 ).contiguous()
       snake_case_	=				model(
           UpperCamelCase_		,				attention_mask=UpperCamelCase_		,				labels=UpperCamelCase_		,				)
       self.parent.assertEqual(result.logits.shape		,				(self.batch_size, self.num_choices) )



 def 						UpperCamelCase__					(     self		,				_UpperCAmelCase		,				_UpperCAmelCase		,				_UpperCAmelCase		,				_UpperCAmelCase		,				_UpperCAmelCase		,				_UpperCAmelCase ):
       snake_case_	=				self.num_labels
       snake_case_	=				MPNetForTokenClassification(config=UpperCamelCase_ )
       model.to(UpperCamelCase_ )
       model.eval()
       snake_case_	=				model(UpperCamelCase_		,				attention_mask=UpperCamelCase_		,				labels=UpperCamelCase_ )
       self.parent.assertEqual(result.logits.shape		,				(self.batch_size, self.seq_length, self.num_labels) )





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







@require_torch
class lowerCAmelCase_						(			__lowerCamelCase ,						__lowerCamelCase ,						unittest.TestCase	):

 '''simple docstring'''

 __snake_case												=  (
     (
         MPNetForMaskedLM,
         MPNetForMultipleChoice,
         MPNetForQuestionAnswering,
         MPNetForSequenceClassification,
         MPNetForTokenClassification,
         MPNetModel,
     )
     if is_torch_available()
     else ()
 )
 __snake_case												=  (
     {
         'feature-extraction': MPNetModel,
         'fill-mask': MPNetForMaskedLM,
         'question-answering': MPNetForQuestionAnswering,
         'text-classification': MPNetForSequenceClassification,
         'token-classification': MPNetForTokenClassification,
         'zero-shot': MPNetForSequenceClassification,
     }
     if is_torch_available()
     else {}
 )
 __snake_case												=  False
 __snake_case												=  True



 def 						UpperCamelCase__					(     self ):
       snake_case_	=				MPNetModelTester(self )
       snake_case_	=				ConfigTester(self		,				config_class=UpperCamelCase_		,				hidden_size=37 )



 def 						UpperCamelCase__					(     self ):
       self.config_tester.run_common_tests()



 def 						UpperCamelCase__					(     self ):
       snake_case_	=				self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_mpnet_model(*UpperCamelCase_ )



 def 						UpperCamelCase__					(     self ):
       snake_case_	=				self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_mpnet_for_sequence_classification(*UpperCamelCase_ )



 def 						UpperCamelCase__					(     self ):
       snake_case_	=				self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_mpnet_for_multiple_choice(*UpperCamelCase_ )



 def 						UpperCamelCase__					(     self ):
       snake_case_	=				self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_mpnet_for_token_classification(*UpperCamelCase_ )





 def 						UpperCamelCase__					(     self ):
       snake_case_	=				self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_mpnet_for_question_answering(*UpperCamelCase_ )







@require_torch
class lowerCAmelCase_						(			unittest.TestCase	):

 '''simple docstring'''

 @slow
 def 						UpperCamelCase__					(     self ):
       snake_case_	=				MPNetModel.from_pretrained('''microsoft/mpnet-base''' )
       snake_case_	=				torch.tensor([[0, 3_45, 2_32, 3_28, 7_40, 1_40, 16_95, 69, 60_78, 15_88, 2]] )
       snake_case_	=				model(UpperCamelCase_ )[0]
       snake_case_	=				torch.Size((1, 11, 7_68) )
       self.assertEqual(output.shape		,				UpperCamelCase_ )
       snake_case_	=				torch.tensor(
           [[[-0.0_550, 0.1_943, -0.0_740], [-0.0_562, 0.2_211, -0.0_579], [-0.0_437, 0.3_337, -0.0_641]]] )
       # compare the actual values for a slice.
       self.assertTrue(torch.allclose(output[:, :3, :3]		,				UpperCamelCase_		,				atol=1E-4 ) )



from ...configuration_utils import PretrainedConfig
from ...utils import logging
from ...utils.backbone_utils import BackboneConfigMixin, get_aligned_output_features_output_indices


UpperCAmelCase      =   logging.get_logger(__name__)

UpperCAmelCase      =   {
    """facebook/convnextv2-tiny-1k-224""": """https://huggingface.co/facebook/convnextv2-tiny-1k-224/resolve/main/config.json""",
}







class lowerCAmelCase_						(			lowerCamelCase__ ,						lowerCamelCase__	):

 '''simple docstring'''

 __snake_case												=  "convnextv2"



 def __init__(     self		,				_UpperCAmelCase=3		,				_UpperCAmelCase=4		,				_UpperCAmelCase=4		,				_UpperCAmelCase=None		,				_UpperCAmelCase=None		,				_UpperCAmelCase="gelu"		,				_UpperCAmelCase=0.02		,				_UpperCAmelCase=1E-12		,				_UpperCAmelCase=0.0		,				_UpperCAmelCase=2_24		,				_UpperCAmelCase=None		,				_UpperCAmelCase=None		,				**_UpperCAmelCase		,				):
       super().__init__(**_UpperCAmelCase )

       snake_case_	=				num_channels
       snake_case_	=				patch_size
       snake_case_	=				num_stages
       snake_case_	=				[96, 1_92, 3_84, 7_68] if hidden_sizes is None else hidden_sizes
       snake_case_	=				[3, 3, 9, 3] if depths is None else depths
       snake_case_	=				hidden_act
       snake_case_	=				initializer_range
       snake_case_	=				layer_norm_eps
       snake_case_	=				drop_path_rate
       snake_case_	=				image_size
       snake_case_	=				['''stem'''] + [F'''stage{idx}''' for idx in range(1		,				len(self.depths ) + 1 )]
       snake_case_	,   snake_case_	=				get_aligned_output_features_output_indices(
           out_features=_UpperCAmelCase		,				out_indices=_UpperCAmelCase		,				stage_names=self.stage_names )