


'''simple docstring'''






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







    """simple docstring"""



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

    def __lowercase   (   self    :  int		)   ->  Any:
      return MPNetConfig.from_pretrained("""microsoft/mpnet-base"""		)

    def __lowercase   (   self    :  int		)   ->  List[Any]:
      lowerCAmelCase_			:      Any			=   ids_tensor([self.batch_size, self.seq_length]    ,						self.vocab_size		)

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

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

      lowerCAmelCase_			:      Tuple			=   self.get_config()
      return config, input_ids, input_mask, sequence_labels, token_labels, choice_labels

    def __lowercase   (   self    :  str		)   ->  Union[str, Any]:
      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 __lowercase   (   self    :  Optional[int]    ,						lowerCamelCase    :  int    ,						lowerCamelCase    :  List[str]    ,						lowerCamelCase    :  Dict    ,						lowerCamelCase    :  str    ,						lowerCamelCase    :  Union[str, Any]    ,						lowerCamelCase    :  List[str]		)   ->  Union[str, Any]:
      lowerCAmelCase_			:      Any			=   MPNetModel(config=_A		)
      model.to(_A		)
      model.eval()
      lowerCAmelCase_			:      Union[str, Any]			=   model(_A    ,						_A		)
      lowerCAmelCase_			:      List[str]			=   model(_A		)
      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 __lowercase   (   self    :  Union[str, Any]    ,						lowerCamelCase    :  Union[str, Any]    ,						lowerCamelCase    :  int    ,						lowerCamelCase    :  Dict    ,						lowerCamelCase    :  Any    ,						lowerCamelCase    :  str    ,						lowerCamelCase    :  List[Any]		)   ->  Tuple:
      lowerCAmelCase_			:      Any			=   MPNetForQuestionAnswering(config=_A		)
      model.to(_A		)
      model.eval()
      lowerCAmelCase_			:      Optional[int]			=   model(
          _A    ,						attention_mask=_A    ,						start_positions=_A    ,						end_positions=_A    ,						)
      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 __lowercase   (   self    :  Dict    ,						lowerCamelCase    :  Dict    ,						lowerCamelCase    :  List[str]    ,						lowerCamelCase    :  Union[str, Any]    ,						lowerCamelCase    :  Union[str, Any]    ,						lowerCamelCase    :  List[Any]    ,						lowerCamelCase    :  Tuple		)   ->  Any:
      lowerCAmelCase_			:      Union[str, Any]			=   self.num_labels
      lowerCAmelCase_			:      Dict			=   MPNetForSequenceClassification(_A		)
      model.to(_A		)
      model.eval()
      lowerCAmelCase_			:      List[Any]			=   model(_A    ,						attention_mask=_A    ,						labels=_A		)
      self.parent.assertEqual(result.logits.shape    ,						(self.batch_size, self.num_labels)		)

    def __lowercase   (   self    :  Optional[Any]    ,						lowerCamelCase    :  Any    ,						lowerCamelCase    :  Optional[int]    ,						lowerCamelCase    :  str    ,						lowerCamelCase    :  Optional[int]    ,						lowerCamelCase    :  str    ,						lowerCamelCase    :  List[Any]		)   ->  int:
      lowerCAmelCase_			:      Any			=   self.num_choices
      lowerCAmelCase_			:      List[str]			=   MPNetForMultipleChoice(config=_A		)
      model.to(_A		)
      model.eval()
      lowerCAmelCase_			:      Dict			=   input_ids.unsqueeze(1		).expand(-1    ,						self.num_choices    ,						-1		).contiguous()
      lowerCAmelCase_			:      str			=   input_mask.unsqueeze(1		).expand(-1    ,						self.num_choices    ,						-1		).contiguous()
      lowerCAmelCase_			:      Dict			=   model(
          _A    ,						attention_mask=_A    ,						labels=_A    ,						)
      self.parent.assertEqual(result.logits.shape    ,						(self.batch_size, self.num_choices)		)

    def __lowercase   (   self    :  Optional[int]    ,						lowerCamelCase    :  Any    ,						lowerCamelCase    :  List[Any]    ,						lowerCamelCase    :  int    ,						lowerCamelCase    :  List[str]    ,						lowerCamelCase    :  int    ,						lowerCamelCase    :  Tuple		)   ->  Dict:
      lowerCAmelCase_			:      Dict			=   self.num_labels
      lowerCAmelCase_			:      int			=   MPNetForTokenClassification(config=_A		)
      model.to(_A		)
      model.eval()
      lowerCAmelCase_			:      int			=   model(_A    ,						attention_mask=_A    ,						labels=_A		)
      self.parent.assertEqual(result.logits.shape    ,						(self.batch_size, self.seq_length, self.num_labels)		)





    def __lowercase   (   self    :  Any		)   ->  Tuple:
      lowerCAmelCase_			:      int			=   self.prepare_config_and_inputs()
      (lowerCAmelCase_)			:      List[Any]			=   config_and_inputs
      lowerCAmelCase_			:      List[Any]			=   {'''input_ids''': input_ids, '''attention_mask''': input_mask}
      return config, inputs_dict







@require_torch
class    __snake_case       (   __a	,__a	,unittest.TestCase):







    """simple docstring"""



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

    def __lowercase   (   self    :  List[Any]		)   ->  Optional[int]:
      lowerCAmelCase_			:      List[Any]			=   MPNetModelTester(self		)
      lowerCAmelCase_			:      Optional[Any]			=   ConfigTester(self    ,						config_class=_A    ,						hidden_size=37		)

    def __lowercase   (   self    :  Optional[Any]		)   ->  Dict:
      self.config_tester.run_common_tests()

    def __lowercase   (   self    :  int		)   ->  Optional[Any]:
      lowerCAmelCase_			:      List[str]			=   self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_mpnet_model(*_A		)

    def __lowercase   (   self    :  Optional[int]		)   ->  str:
      lowerCAmelCase_			:      Tuple			=   self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_mpnet_for_sequence_classification(*_A		)

    def __lowercase   (   self    :  Optional[int]		)   ->  List[Any]:
      lowerCAmelCase_			:      Tuple			=   self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_mpnet_for_multiple_choice(*_A		)

    def __lowercase   (   self    :  str		)   ->  List[Any]:
      lowerCAmelCase_			:      List[Any]			=   self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_mpnet_for_token_classification(*_A		)





    def __lowercase   (   self    :  int		)   ->  List[Any]:
      lowerCAmelCase_			:      int			=   self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_mpnet_for_question_answering(*_A		)







@require_torch
class    __snake_case       (   unittest.TestCase):







    """simple docstring"""



    @slow
    def __lowercase   (   self    :  List[Any]		)   ->  Union[str, Any]:
      lowerCAmelCase_			:      List[Any]			=   MPNetModel.from_pretrained("""microsoft/mpnet-base"""		)
      lowerCAmelCase_			:      Union[str, Any]			=   torch.tensor([[0, 3_45, 2_32, 3_28, 7_40, 1_40, 16_95, 69, 60_78, 15_88, 2]]		)
      lowerCAmelCase_			:      List[str]			=   model(_A		)[0]
      lowerCAmelCase_			:      int			=   torch.Size((1, 11, 7_68)		)
      self.assertEqual(output.shape    ,						_A		)
      lowerCAmelCase_			:      Optional[Any]			=   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]    ,						_A    ,						atol=1E-4		)		)







'''simple docstring'''
def 					a__       (					lowerCAmelCase__						)    ->	int:
    if a < 0:
        raise ValueError('''Input value must be a positive integer'''						)
    elif isinstance(lowerCAmelCase__  ,					lowerCAmelCase__						):
        raise TypeError('''Input value must be a \'int\' type'''						)
    return bin(lowerCAmelCase__						).count('''1'''						)


if __name__ == "__main__":
  import doctest

  doctest.testmod()
