






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


			'''simple docstring'''





			def __init__(   self							:						Tuple ,      lowercase							:						Tuple ,      lowercase							:						Optional[int]=1_3 ,      lowercase							:						List[str]=7 ,      lowercase							:						Optional[Any]=True ,      lowercase							:						int=True ,      lowercase							:						int=False ,      lowercase							:						Union[str, Any]=True ,      lowercase							:						Optional[int]=9_9 ,      lowercase							:						Any=6_4 ,      lowercase							:						List[str]=5 ,      lowercase							:						Tuple=4 ,      lowercase							:						List[Any]=6_4 ,      lowercase							:						Union[str, Any]="gelu" ,      lowercase							:						Dict=0.1 ,      lowercase							:						Tuple=0.1 ,      lowercase							:						Any=5_1_2 ,      lowercase							:						str=1_6 ,      lowercase							:						Dict=2 ,      lowercase							:						Optional[Any]=0.0_2 ,      lowercase							:						Any=3 ,      lowercase							:						List[str]=4 ,      lowercase							:						Tuple=None ,      )							->    Optional[int]:

									'''simple docstring'''







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






			def 				A      (   self							:						Dict			)							->    str:

									'''simple docstring'''







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






			def 				A      (   self							:						List[str]			)							->    str:

									'''simple docstring'''







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

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

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

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






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

									'''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 				A      (   self							:						Optional[Any] ,      lowercase							:						Dict ,      lowercase							:						List[Any] ,      lowercase							:						List[str] ,      lowercase							:						Tuple ,      lowercase							:						Dict ,      lowercase							:						str			)							->    int:

									'''simple docstring'''







									UpperCamelCase__         = MPNetModel(config=snake_case__			)
									model.to(snake_case__			)
									model.eval()
									UpperCamelCase__         = model(snake_case__ ,      snake_case__			)
									UpperCamelCase__         = model(snake_case__			)
									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 				A      (   self							:						int ,      lowercase							:						Dict ,      lowercase							:						Tuple ,      lowercase							:						Tuple ,      lowercase							:						int ,      lowercase							:						int ,      lowercase							:						Dict			)							->    List[str]:

									'''simple docstring'''







									UpperCamelCase__         = MPNetForQuestionAnswering(config=snake_case__			)
									model.to(snake_case__			)
									model.eval()
									UpperCamelCase__         = model(
									    snake_case__ ,      attention_mask=snake_case__ ,      start_positions=snake_case__ ,      end_positions=snake_case__ ,      )
									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 				A      (   self							:						List[Any] ,      lowercase							:						int ,      lowercase							:						List[Any] ,      lowercase							:						List[str] ,      lowercase							:						str ,      lowercase							:						Optional[Any] ,      lowercase							:						int			)							->    List[str]:

									'''simple docstring'''







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






			def 				A      (   self							:						Optional[int] ,      lowercase							:						Union[str, Any] ,      lowercase							:						Union[str, Any] ,      lowercase							:						List[Any] ,      lowercase							:						Optional[int] ,      lowercase							:						Union[str, Any] ,      lowercase							:						Dict			)							->    Tuple:

									'''simple docstring'''







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






			def 				A      (   self							:						int ,      lowercase							:						Optional[Any] ,      lowercase							:						Optional[int] ,      lowercase							:						Any ,      lowercase							:						int ,      lowercase							:						str ,      lowercase							:						List[Any]			)							->    Tuple:

									'''simple docstring'''







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







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

									'''simple docstring'''







									UpperCamelCase__         = self.prepare_config_and_inputs()
									(UpperCamelCase__)         = config_and_inputs
									UpperCamelCase__         = {"input_ids": input_ids, "attention_mask": input_mask}
									return config, inputs_dict
@require_torch
class    _SCREAMING_SNAKE_CASE							(		SCREAMING_SNAKE_CASE__       ,SCREAMING_SNAKE_CASE__       ,unittest.TestCase	):


			'''simple docstring'''





			__a							:							Dict					     =					(
			    (
			        MPNetForMaskedLM,
			        MPNetForMultipleChoice,
			        MPNetForQuestionAnswering,
			        MPNetForSequenceClassification,
			        MPNetForTokenClassification,
			        MPNetModel,
			    )
			    if is_torch_available()
			    else ()
			)
			__a							:							Union[str, 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							:							Union[str, Any]					     =					False
			__a							:							Union[str, Any]					     =					True






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

									'''simple docstring'''







									UpperCamelCase__         = MPNetModelTester(self			)
									UpperCamelCase__         = ConfigTester(self ,      config_class=snake_case__ ,      hidden_size=3_7			)






			def 				A      (   self							:						List[Any]			)							->    Tuple:

									'''simple docstring'''







									self.config_tester.run_common_tests()






			def 				A      (   self							:						Optional[int]			)							->    List[str]:

									'''simple docstring'''







									UpperCamelCase__         = self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_mpnet_model(*snake_case__			)






			def 				A      (   self							:						Tuple			)							->    List[str]:

									'''simple docstring'''







									UpperCamelCase__         = self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_mpnet_for_sequence_classification(*snake_case__			)






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

									'''simple docstring'''







									UpperCamelCase__         = self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_mpnet_for_multiple_choice(*snake_case__			)






			def 				A      (   self							:						List[str]			)							->    int:

									'''simple docstring'''







									UpperCamelCase__         = self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_mpnet_for_token_classification(*snake_case__			)







			def 				A      (   self							:						Tuple			)							->    Dict:

									'''simple docstring'''







									UpperCamelCase__         = self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_mpnet_for_question_answering(*snake_case__			)
@require_torch
class    _SCREAMING_SNAKE_CASE							(		unittest.TestCase	):


			'''simple docstring'''





			@slow
			def 				A      (   self							:						Union[str, Any]			)							->    str:

									'''simple docstring'''







									UpperCamelCase__         = MPNetModel.from_pretrained("""microsoft/mpnet-base"""			)
									UpperCamelCase__         = torch.tensor([[0, 3_4_5, 2_3_2, 3_2_8, 7_4_0, 1_4_0, 1_6_9_5, 6_9, 6_0_7_8, 1_5_8_8, 2]]			)
									UpperCamelCase__         = model(snake_case__			)[0]
									UpperCamelCase__         = torch.Size((1, 1_1, 7_6_8)			)
									self.assertEqual(output.shape ,      snake_case__			)
									UpperCamelCase__         = torch.tensor(
									    [[[-0.0_5_5_0, 0.1_9_4_3, -0.0_7_4_0], [-0.0_5_6_2, 0.2_2_1_1, -0.0_5_7_9], [-0.0_4_3_7, 0.3_3_3_7, -0.0_6_4_1]]]			)
									# compare the actual values for a slice.
									self.assertTrue(torch.allclose(output[:, :3, :3] ,      snake_case__ ,      atol=1e-4			)			)








'''simple docstring'''


from __future__ import annotations

from collections.abc import Callable
from typing import Generic, TypeVar

lowerCamelCase_				: List[str]    	=							TypeVar('''T''')
lowerCamelCase_				: Optional[int]    	=							TypeVar('''U''')
class    _SCREAMING_SNAKE_CASE							(		Generic[T, U]	):


			'''simple docstring'''





			def __init__(   self							:						Dict ,      lowercase							:						T | None ,      lowercase							:						U | None			)							->    List[Any]:

									'''simple docstring'''







									UpperCamelCase__         = key
									UpperCamelCase__         = val
									UpperCamelCase__         = None
									UpperCamelCase__         = None







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

									'''simple docstring'''







									return (
									    f"Node: key: {self.key}, val: {self.val}, "
									    f"has next: {bool(self.next			)}, has prev: {bool(self.prev			)}"
									)
class    _SCREAMING_SNAKE_CASE							(		Generic[T, U]	):


			'''simple docstring'''





			def __init__(   self							:						Union[str, Any]			)							->    None:

									'''simple docstring'''







									UpperCamelCase__         = DoubleLinkedListNode(lowercase ,      lowercase			)
									UpperCamelCase__         = DoubleLinkedListNode(lowercase ,      lowercase			)
									UpperCamelCase__    ,   UpperCamelCase__         = self.rear, self.head






			def __repr__(   self							:						int			)							->    str:

									'''simple docstring'''







									UpperCamelCase__         = ["""DoubleLinkedList"""]
									UpperCamelCase__         = self.head
									while node.next is not None:
															rep.append(str(lowercase			)			)
															UpperCamelCase__         = node.next
									rep.append(str(self.rear			)			)
									return ",\n    ".join(lowercase			)






			def 				A      (   self							:						str ,      lowercase							:						DoubleLinkedListNode[T, U]			)							->    None:

									'''simple docstring'''







									UpperCamelCase__         = self.rear.prev

									# All nodes other than self.head are guaranteed to have non-None previous
									assert previous is not None

									UpperCamelCase__         = node
									UpperCamelCase__         = previous
									UpperCamelCase__         = node
									UpperCamelCase__         = self.rear







			def 				A      (   self							:						Any ,      lowercase							:						DoubleLinkedListNode[T, U]			)							->    DoubleLinkedListNode[T, U] | None:

									'''simple docstring'''







									if node.prev is None or node.next is None:
															return None

									UpperCamelCase__         = node.next
									UpperCamelCase__         = node.prev
									UpperCamelCase__         = None
									UpperCamelCase__         = None
									return node
class    _SCREAMING_SNAKE_CASE							(		Generic[T, U]	):


			'''simple docstring'''





			__a							:							dict[Callable[[T], U], LRUCache[T, U]]					     =					{}






			def __init__(   self							:						int ,      lowercase							:						int			)							->    Union[str, Any]:

									'''simple docstring'''







									UpperCamelCase__         = DoubleLinkedList()
									UpperCamelCase__         = capacity
									UpperCamelCase__         = 0
									UpperCamelCase__         = 0
									UpperCamelCase__         = 0
									UpperCamelCase__         = {}






			def __repr__(   self							:						Any			)							->    str:

									'''simple docstring'''







									return (
									    f"CacheInfo(hits={self.hits}, misses={self.miss}, "
									    f"capacity={self.capacity}, current size={self.num_keys})"
									)






			def __contains__(   self							:						Any ,      lowercase							:						T			)							->    bool:

									'''simple docstring'''







									return key in self.cache






			def 				A      (   self							:						Tuple ,      lowercase							:						T			)							->    U | None:

									'''simple docstring'''







									if key in self.cache:
															self.hits += 1
															UpperCamelCase__         = self.cache[key]
															UpperCamelCase__         = self.list.remove(self.cache[key]			)
															assert node == value_node

															# node is guaranteed not None because it is in self.cache
															assert node is not None
															self.list.add(lowercase			)
															return node.val
									self.miss += 1
									return None






			def 				A      (   self							:						Dict ,      lowercase							:						T ,      lowercase							:						U			)							->    None:

									'''simple docstring'''







									if key not in self.cache:
															if self.num_keys >= self.capacity:
																					# delete first node (oldest) when over capacity
																					UpperCamelCase__         = self.list.head.next

																					# guaranteed to have a non-None first node when num_keys > 0
																					# explain to type checker via assertions
																					assert first_node is not None
																					assert first_node.key is not None
																					assert (
																					    self.list.remove(lowercase			) is not None
																					)  # node guaranteed to be in list assert node.key is not None

																					del self.cache[first_node.key]
																					self.num_keys -= 1
															UpperCamelCase__         = DoubleLinkedListNode(lowercase ,      lowercase			)
															self.list.add(self.cache[key]			)
															self.num_keys += 1

									else:
															# bump node to the end of the list, update value
															UpperCamelCase__         = self.list.remove(self.cache[key]			)
															assert node is not None  # node guaranteed to be in list
															UpperCamelCase__         = value
															self.list.add(lowercase			)







			@classmethod
			def 				A      (   cls							:						Optional[int] ,      lowercase							:						int = 1_2_8			)							->    Callable[[Callable[[T], U]], Callable[..., U]]:

									'''simple docstring'''







									def cache_decorator_inner(lowercase							:						Callable[[T], U]			) -> Callable[..., U]:
															def cache_decorator_wrapper(*lowercase							:						T			) -> U:
																					if func not in cls.decorator_function_to_instance_map:
																											UpperCamelCase__         = LRUCache(lowercase			)

																					UpperCamelCase__         = cls.decorator_function_to_instance_map[func].get(args[0]			)
																					if result is None:
																											UpperCamelCase__         = func(*lowercase			)
																											cls.decorator_function_to_instance_map[func].put(args[0] ,      lowercase			)
																					return result

															def cache_info() -> LRUCache[T, U]:
																					return cls.decorator_function_to_instance_map[func]

															setattr(lowercase ,      """cache_info""" ,      lowercase			)  # noqa: B010

															return cache_decorator_wrapper

									return cache_decorator_inner


if __name__ == "__main__":
					import doctest

					doctest.testmod()
