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       :




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




       """simple docstring"""
       SCREAMING_SNAKE_CASE__					:					List[Any]      =		parent
       SCREAMING_SNAKE_CASE__					:					Optional[Any]      =		batch_size
       SCREAMING_SNAKE_CASE__					:					Optional[Any]      =		seq_length
       SCREAMING_SNAKE_CASE__					:					int      =		is_training
       SCREAMING_SNAKE_CASE__					:					int      =		use_input_mask
       SCREAMING_SNAKE_CASE__					:					str      =		use_token_type_ids
       SCREAMING_SNAKE_CASE__					:					int      =		use_labels
       SCREAMING_SNAKE_CASE__					:					Tuple      =		vocab_size
       SCREAMING_SNAKE_CASE__					:					Any      =		hidden_size
       SCREAMING_SNAKE_CASE__					:					Any      =		num_hidden_layers
       SCREAMING_SNAKE_CASE__					:					Optional[int]      =		num_attention_heads
       SCREAMING_SNAKE_CASE__					:					Union[str, Any]      =		intermediate_size
       SCREAMING_SNAKE_CASE__					:					Tuple      =		hidden_act
       SCREAMING_SNAKE_CASE__					:					List[Any]      =		hidden_dropout_prob
       SCREAMING_SNAKE_CASE__					:					Tuple      =		attention_probs_dropout_prob
       SCREAMING_SNAKE_CASE__					:					Dict      =		max_position_embeddings
       SCREAMING_SNAKE_CASE__					:					Any      =		type_vocab_size
       SCREAMING_SNAKE_CASE__					:					Dict      =		type_sequence_label_size
       SCREAMING_SNAKE_CASE__					:					List[Any]      =		initializer_range
       SCREAMING_SNAKE_CASE__					:					Union[str, Any]      =		num_labels
       SCREAMING_SNAKE_CASE__					:					Union[str, Any]      =		num_choices
       SCREAMING_SNAKE_CASE__					:					List[Any]      =		scope




  def 						__lowercase(   self  :						Dict					)->							List[str]:




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




  def 						__lowercase(   self  :						Any					)->							str:




       """simple docstring"""
       SCREAMING_SNAKE_CASE__					:					Optional[Any]      =		ids_tensor([self.batch_size, self.seq_length]  ,			self.vocab_size					)

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

       SCREAMING_SNAKE_CASE__					:					Dict      =		None
       SCREAMING_SNAKE_CASE__					:					List[Any]      =		None
       SCREAMING_SNAKE_CASE__					:					Union[str, Any]      =		None
       if self.use_labels:
            SCREAMING_SNAKE_CASE__					:					Union[str, Any]      =		ids_tensor([self.batch_size]  ,			self.type_sequence_label_size					)
            SCREAMING_SNAKE_CASE__					:					List[str]      =		ids_tensor([self.batch_size, self.seq_length]  ,			self.num_labels					)
            SCREAMING_SNAKE_CASE__					:					List[Any]      =		ids_tensor([self.batch_size]  ,			self.num_choices					)

       SCREAMING_SNAKE_CASE__					:					Any      =		self.get_config()
       return config, input_ids, input_mask, sequence_labels, token_labels, choice_labels




  def 						__lowercase(   self  :						Optional[int]					)->							Dict:




       """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 						__lowercase(   self  :						int  ,			a_  :						Any  ,			a_  :						Dict  ,			a_  :						Dict  ,			a_  :						Optional[Any]  ,			a_  :						List[Any]  ,			a_  :						Optional[int]					)->							int:




       """simple docstring"""
       SCREAMING_SNAKE_CASE__					:					Optional[int]      =		MPNetModel(config=a_					)
       model.to(a_					)
       model.eval()
       SCREAMING_SNAKE_CASE__					:					Tuple      =		model(a_  ,			a_					)
       SCREAMING_SNAKE_CASE__					:					Dict      =		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  :						Tuple  ,			a_  :						int  ,			a_  :						Union[str, Any]  ,			a_  :						List[str]  ,			a_  :						Optional[Any]  ,			a_  :						Tuple  ,			a_  :						int					)->							List[str]:




       """simple docstring"""
       SCREAMING_SNAKE_CASE__					:					str      =		MPNetForQuestionAnswering(config=a_					)
       model.to(a_					)
       model.eval()
       SCREAMING_SNAKE_CASE__					:					Optional[Any]      =		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  :						str  ,			a_  :						Union[str, Any]  ,			a_  :						Optional[int]  ,			a_  :						Dict  ,			a_  :						int  ,			a_  :						List[Any]  ,			a_  :						List[str]					)->							Tuple:




       """simple docstring"""
       SCREAMING_SNAKE_CASE__					:					Optional[Any]      =		self.num_labels
       SCREAMING_SNAKE_CASE__					:					int      =		MPNetForSequenceClassification(a_					)
       model.to(a_					)
       model.eval()
       SCREAMING_SNAKE_CASE__					:					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]  ,			a_  :						Union[str, Any]  ,			a_  :						Optional[Any]  ,			a_  :						Dict  ,			a_  :						Optional[int]  ,			a_  :						Union[str, Any]  ,			a_  :						int					)->							Union[str, Any]:




       """simple docstring"""
       SCREAMING_SNAKE_CASE__					:					Dict      =		self.num_choices
       SCREAMING_SNAKE_CASE__					:					Any      =		MPNetForMultipleChoice(config=a_					)
       model.to(a_					)
       model.eval()
       SCREAMING_SNAKE_CASE__					:					Tuple      =		input_ids.unsqueeze(1					).expand(-1  ,			self.num_choices  ,			-1					).contiguous()
       SCREAMING_SNAKE_CASE__					:					int      =		input_mask.unsqueeze(1					).expand(-1  ,			self.num_choices  ,			-1					).contiguous()
       SCREAMING_SNAKE_CASE__					:					Tuple      =		model(
           a_  ,			attention_mask=a_  ,			labels=a_  ,			)
       self.parent.assertEqual(result.logits.shape  ,			(self.batch_size, self.num_choices)					)




  def 						__lowercase(   self  :						int  ,			a_  :						Optional[int]  ,			a_  :						Union[str, Any]  ,			a_  :						Any  ,			a_  :						int  ,			a_  :						List[str]  ,			a_  :						Any					)->							Union[str, Any]:




       """simple docstring"""
       SCREAMING_SNAKE_CASE__					:					List[Any]      =		self.num_labels
       SCREAMING_SNAKE_CASE__					:					Any      =		MPNetForTokenClassification(config=a_					)
       model.to(a_					)
       model.eval()
       SCREAMING_SNAKE_CASE__					:					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  :						str					)->							Union[str, Any]:




       """simple docstring"""
       SCREAMING_SNAKE_CASE__					:					List[Any]      =		self.prepare_config_and_inputs()
       ((SCREAMING_SNAKE_CASE__)      ,				(SCREAMING_SNAKE_CASE__)      ,				(SCREAMING_SNAKE_CASE__)      ,				(SCREAMING_SNAKE_CASE__)      ,				(SCREAMING_SNAKE_CASE__)      ,				(SCREAMING_SNAKE_CASE__))					:					Dict      =		config_and_inputs
       SCREAMING_SNAKE_CASE__					:					str      =		{'input_ids': input_ids, 'attention_mask': input_mask}
       return config, inputs_dict




@require_torch
class    snake_case       (   UpperCamelCase_ ,    UpperCamelCase_ ,    unittest.TestCase    ):
  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  :						Optional[Any]					)->							str:




       """simple docstring"""
       SCREAMING_SNAKE_CASE__					:					List[str]      =		MPNetModelTester(self					)
       SCREAMING_SNAKE_CASE__					:					Optional[Any]      =		ConfigTester(self  ,			config_class=a_  ,			hidden_size=37					)




  def 						__lowercase(   self  :						Optional[int]					)->							Union[str, Any]:




       """simple docstring"""
       self.config_tester.run_common_tests()




  def 						__lowercase(   self  :						int					)->							Optional[int]:




       """simple docstring"""
       SCREAMING_SNAKE_CASE__					:					Any      =		self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_mpnet_model(*a_					)




  def 						__lowercase(   self  :						Dict					)->							Optional[int]:




       """simple docstring"""
       SCREAMING_SNAKE_CASE__					:					Optional[Any]      =		self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_mpnet_for_sequence_classification(*a_					)




  def 						__lowercase(   self  :						str					)->							Tuple:




       """simple docstring"""
       SCREAMING_SNAKE_CASE__					:					str      =		self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_mpnet_for_multiple_choice(*a_					)




  def 						__lowercase(   self  :						List[str]					)->							Any:




       """simple docstring"""
       SCREAMING_SNAKE_CASE__					:					List[str]      =		self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_mpnet_for_token_classification(*a_					)




  def 						__lowercase(   self  :						Any					)->							Tuple:




       """simple docstring"""
       SCREAMING_SNAKE_CASE__					:					List[Any]      =		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    ):




  @slow
  def 						__lowercase(   self  :						str					)->							Tuple:




       """simple docstring"""
       SCREAMING_SNAKE_CASE__					:					Tuple      =		MPNetModel.from_pretrained('microsoft/mpnet-base'					)
       SCREAMING_SNAKE_CASE__					:					Any      =		torch.tensor([[0, 345, 232, 328, 740, 140, 1695, 69, 6078, 1588, 2]]					)
       SCREAMING_SNAKE_CASE__					:					Tuple      =		model(a_					)[0]
       SCREAMING_SNAKE_CASE__					:					int      =		torch.Size((1, 11, 768)					)
       self.assertEqual(output.shape  ,			a_					)
       SCREAMING_SNAKE_CASE__					:					Dict      =		torch.tensor(
           [[[-0.0550, 0.1943, -0.0740], [-0.0562, 0.2211, -0.0579], [-0.0437, 0.3337, -0.0641]]]					)
       # compare the actual values for a slice.
       self.assertTrue(torch.allclose(output[:, :3, :3]  ,			a_  ,			atol=1e-4					)					)








"""simple docstring"""






from copy import deepcopy



class     UpperCAmelCase    :



   def __init__(       self    :	Optional[Any]							,       __lowerCamelCase    :	list[int] | None = None							,       __lowerCamelCase    :	int | None = None				):



     """simple docstring"""

     if arr is None and size is not None:
       _snake_case   	=      size
       _snake_case   	=      [0] * size
     elif arr is not None:
       self.init(__lowerCamelCase				)
     else:
       raise ValueError('''Either arr or size must be specified'''				)



   def    __UpperCAmelCase    (       self    :	Any							,       __lowerCamelCase    :	list[int]				):



     """simple docstring"""
     _snake_case   	=      len(__lowerCamelCase				)
     _snake_case   	=      deepcopy(__lowerCamelCase				)
     for i in range(1							,       self.size				):
       _snake_case   	=      self.next_(__lowerCamelCase				)
       if j < self.size:
         self.tree[j] += self.tree[i]



   def    __UpperCAmelCase    (       self    :	Union[str, Any]				):



     """simple docstring"""
     _snake_case   	=      self.tree[:]
     for i in range(self.size - 1							,       0							,       -1				):
       _snake_case   	=      self.next_(__lowerCamelCase				)
       if j < self.size:
         arr[j] -= arr[i]
     return arr



   @staticmethod
   def    __UpperCAmelCase    (       __lowerCamelCase    :	int				):



     """simple docstring"""
     return index + (index & (-index))



   @staticmethod
   def    __UpperCAmelCase    (       __lowerCamelCase    :	int				):



     """simple docstring"""
     return index - (index & (-index))



   def    __UpperCAmelCase    (       self    :	Dict							,       __lowerCamelCase    :	int							,       __lowerCamelCase    :	int				):



     """simple docstring"""
     if index == 0:
       self.tree[0] += value
       return
     while index < self.size:
       self.tree[index] += value
       _snake_case   	=      self.next_(__lowerCamelCase				)



   def    __UpperCAmelCase    (       self    :	List[str]							,       __lowerCamelCase    :	int							,       __lowerCamelCase    :	int				):



     """simple docstring"""
     self.add(__lowerCamelCase							,       value - self.get(__lowerCamelCase				)				)



   def    __UpperCAmelCase    (       self    :	Optional[Any]							,       __lowerCamelCase    :	int				):



     """simple docstring"""
     if right == 0:
       return 0
     _snake_case   	=      self.tree[0]
     right -= 1  # make right inclusive
     while right > 0:
       result += self.tree[right]
       _snake_case   	=      self.prev(__lowerCamelCase				)
     return result



   def    __UpperCAmelCase    (       self    :	str							,       __lowerCamelCase    :	int							,       __lowerCamelCase    :	int				):



     """simple docstring"""
     return self.prefix(__lowerCamelCase				) - self.prefix(__lowerCamelCase				)



   def    __UpperCAmelCase    (       self    :	List[str]							,       __lowerCamelCase    :	int				):



     """simple docstring"""
     return self.query(__lowerCamelCase							,       index + 1				)





   def    __UpperCAmelCase    (       self    :	Optional[int]							,       __lowerCamelCase    :	int				):



     """simple docstring"""
     value -= self.tree[0]
     if value < 0:
       return -1

     _snake_case   	=      1  # Largest power of 2 <= size
     while j * 2 < self.size:
       j *= 2

     _snake_case   	=      0

     while j > 0:
       if i + j < self.size and self.tree[i + j] <= value:
         value -= self.tree[i + j]
         i += j
       j //= 2
     return i


if __name__ == "__main__":
     import doctest

     doctest.testmod()





