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






     """simple docstring"""







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







       """simple docstring"""




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



     def       UpperCAmelCase__				(							self				:				Dict      ):







       """simple docstring"""




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



     def       UpperCAmelCase__				(							self				:				int      ):







       """simple docstring"""




       __SCREAMING_SNAKE_CASE	:     Union[str, Any] =						ids_tensor([self.batch_size, self.seq_length]			,		self.vocab_size      )

       __SCREAMING_SNAKE_CASE	:     int =						None
       if self.use_input_mask:
         __SCREAMING_SNAKE_CASE	:     List[str] =						random_attention_mask([self.batch_size, self.seq_length]      )

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

       __SCREAMING_SNAKE_CASE	:     List[str] =						self.get_config()
       return config, input_ids, input_mask, sequence_labels, token_labels, choice_labels



     def       UpperCAmelCase__				(							self				:				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       UpperCAmelCase__				(							self				:				Dict			,		_A				:				Dict			,		_A				:				int			,		_A				:				Tuple			,		_A				:				List[str]			,		_A				:				str			,		_A				:				List[Any]      ):







       """simple docstring"""




       __SCREAMING_SNAKE_CASE	:     int =						MPNetModel(config=A_      )
       model.to(A_      )
       model.eval()
       __SCREAMING_SNAKE_CASE	:     Any =						model(A_			,		A_      )
       __SCREAMING_SNAKE_CASE	:     Union[str, Any] =						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       UpperCAmelCase__				(							self				:				List[str]			,		_A				:				Union[str, Any]			,		_A				:				List[Any]			,		_A				:				Optional[int]			,		_A				:				Optional[Any]			,		_A				:				Optional[int]			,		_A				:				Any      ):







       """simple docstring"""




       __SCREAMING_SNAKE_CASE	:     Any =						MPNetForQuestionAnswering(config=A_      )
       model.to(A_      )
       model.eval()
       __SCREAMING_SNAKE_CASE	:     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       UpperCAmelCase__				(							self				:				Optional[Any]			,		_A				:				Tuple			,		_A				:				List[str]			,		_A				:				Optional[Any]			,		_A				:				Dict			,		_A				:				Union[str, Any]			,		_A				:				Tuple      ):







       """simple docstring"""




       __SCREAMING_SNAKE_CASE	:     Tuple =						self.num_labels
       __SCREAMING_SNAKE_CASE	:     Any =						MPNetForSequenceClassification(A_      )
       model.to(A_      )
       model.eval()
       __SCREAMING_SNAKE_CASE	:     Dict =						model(A_			,		attention_mask=A_			,		labels=A_      )
       self.parent.assertEqual(result.logits.shape			,		(self.batch_size, self.num_labels)      )



     def       UpperCAmelCase__				(							self				:				Union[str, Any]			,		_A				:				Tuple			,		_A				:				Dict			,		_A				:				Tuple			,		_A				:				Dict			,		_A				:				List[str]			,		_A				:				List[Any]      ):







       """simple docstring"""




       __SCREAMING_SNAKE_CASE	:     int =						self.num_choices
       __SCREAMING_SNAKE_CASE	:     List[str] =						MPNetForMultipleChoice(config=A_      )
       model.to(A_      )
       model.eval()
       __SCREAMING_SNAKE_CASE	:     Optional[Any] =						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	:     Optional[int] =						model(
           A_			,		attention_mask=A_			,		labels=A_			,		)
       self.parent.assertEqual(result.logits.shape			,		(self.batch_size, self.num_choices)      )



     def       UpperCAmelCase__				(							self				:				Optional[Any]			,		_A				:				Any			,		_A				:				int			,		_A				:				Any			,		_A				:				List[Any]			,		_A				:				Any			,		_A				:				Union[str, Any]      ):







       """simple docstring"""




       __SCREAMING_SNAKE_CASE	:     int =						self.num_labels
       __SCREAMING_SNAKE_CASE	:     Tuple =						MPNetForTokenClassification(config=A_      )
       model.to(A_      )
       model.eval()
       __SCREAMING_SNAKE_CASE	:     Optional[int] =						model(A_			,		attention_mask=A_			,		labels=A_      )
       self.parent.assertEqual(result.logits.shape			,		(self.batch_size, self.seq_length, self.num_labels)      )




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







       """simple docstring"""




       __SCREAMING_SNAKE_CASE	:     List[Any] =						self.prepare_config_and_inputs()
       (__SCREAMING_SNAKE_CASE)	:     Union[str, Any] =						config_and_inputs
       __SCREAMING_SNAKE_CASE	:     List[Any] =						{'''input_ids''': input_ids, '''attention_mask''': input_mask}
       return config, inputs_dict






@require_torch
class   __UpperCamelCase      (				UpperCamelCase_      ,		UpperCamelCase_      ,		unittest.TestCase    ):






     """simple docstring"""







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



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







       """simple docstring"""




       __SCREAMING_SNAKE_CASE	:     List[Any] =						MPNetModelTester(self      )
       __SCREAMING_SNAKE_CASE	:     Optional[Any] =						ConfigTester(self			,		config_class=A_			,		hidden_size=37      )



     def       UpperCAmelCase__				(							self				:				Any      ):







       """simple docstring"""




       self.config_tester.run_common_tests()



     def       UpperCAmelCase__				(							self				:				List[Any]      ):







       """simple docstring"""




       __SCREAMING_SNAKE_CASE	:     int =						self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_mpnet_model(*A_      )



     def       UpperCAmelCase__				(							self				:				Tuple      ):







       """simple docstring"""




       __SCREAMING_SNAKE_CASE	:     Optional[int] =						self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_mpnet_for_sequence_classification(*A_      )



     def       UpperCAmelCase__				(							self				:				str      ):







       """simple docstring"""




       __SCREAMING_SNAKE_CASE	:     Dict =						self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_mpnet_for_multiple_choice(*A_      )



     def       UpperCAmelCase__				(							self				:				int      ):







       """simple docstring"""




       __SCREAMING_SNAKE_CASE	:     Dict =						self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_mpnet_for_token_classification(*A_      )




     def       UpperCAmelCase__				(							self				:				str      ):







       """simple docstring"""




       __SCREAMING_SNAKE_CASE	:     Any =						self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_mpnet_for_question_answering(*A_      )






@require_torch
class   __UpperCamelCase      (				unittest.TestCase    ):






     """simple docstring"""







     @slow
     def       UpperCAmelCase__				(							self				:				Tuple      ):







       """simple docstring"""




       __SCREAMING_SNAKE_CASE	:     Union[str, Any] =						MPNetModel.from_pretrained('''microsoft/mpnet-base'''      )
       __SCREAMING_SNAKE_CASE	:     Optional[Any] =						torch.tensor([[0, 345, 232, 328, 740, 140, 1695, 69, 6078, 1588, 2]]      )
       __SCREAMING_SNAKE_CASE	:     Union[str, Any] =						model(A_      )[0]
       __SCREAMING_SNAKE_CASE	:     Optional[int] =						torch.Size((1, 11, 768)      )
       self.assertEqual(output.shape			,		A_      )
       __SCREAMING_SNAKE_CASE	:     Tuple =						torch.tensor(
           [[[-0.05_50, 0.19_43, -0.07_40], [-0.05_62, 0.22_11, -0.05_79], [-0.04_37, 0.33_37, -0.06_41]]]      )
       # compare the actual values for a slice.
       self.assertTrue(torch.allclose(output[:, :3, :3]			,		A_			,		atol=1e-4      )      )





# Lint as: python3
import os
import re
import urllib.parse
from pathlib import Path
from typing import Callable, List, Optional, Union
from zipfile import ZipFile

from ..utils.file_utils import cached_path, hf_github_url
from ..utils.logging import get_logger
from ..utils.version import Version


A__  :  Tuple								=					get_logger(__name__)



class 				__snake_case						:
							_a			      = '''dummy_data'''
							_a			      = '''datasets'''
							_a			      = False




							def __init__(       self     :       Optional[Any]				,				A_     :       str				,				A_     :       str				,				A_     :       Union[Version, str]				,				A_     :       Optional[str] = None				,				A_     :       bool = False				,				A_     :       bool = True				,				A_     :       Optional[List[Callable]] = None				,				):
									lowerCAmelCase_			:     List[Any]									= 0
									lowerCAmelCase_			:     Any									= dataset_name
									lowerCAmelCase_			:     Union[str, Any]									= cache_dir
									lowerCAmelCase_			:     List[Any]									= use_local_dummy_data
									lowerCAmelCase_			:     Optional[Any]									= config
									# download_callbacks take a single url as input
									lowerCAmelCase_			:     List[Callable]									= download_callbacks or []
									# if False, it doesn't load existing files and it returns the paths of the dummy files relative
									# to the dummy_data zip file root
									lowerCAmelCase_			:     Tuple									= load_existing_dummy_data

									# TODO(PVP, QL) might need to make this more general
									lowerCAmelCase_			:     int									= str(A_)
									# to be downloaded
									lowerCAmelCase_			:     Dict									= None
									lowerCAmelCase_			:     Optional[int]									= None




							@property
							def    UpperCAmelCase__			(       self     :       List[str]):
									if self._dummy_file is None:
											lowerCAmelCase_			:     int									= self.download_dummy_data()
									return self._dummy_file




							@property
							def    UpperCAmelCase__			(       self     :       str):
									if self.config is not None:
											# structure is dummy / config_name / version_name
											return os.path.join('''dummy'''				,				self.config.name				,				self.version_name)
									# structure is dummy / version_name
									return os.path.join('''dummy'''				,				self.version_name)




							@property
							def    UpperCAmelCase__			(       self     :       str):
									return os.path.join(self.dummy_data_folder				,				'''dummy_data.zip''')




							def    UpperCAmelCase__			(       self     :       Any):
									lowerCAmelCase_			:     Any									= (
									    self.local_path_to_dummy_data if self.use_local_dummy_data is True else self.github_path_to_dummy_data
									)

									lowerCAmelCase_			:     Union[str, Any]									= cached_path(
									    A_				,				cache_dir=self.cache_dir				,				extract_compressed_file=A_				,				force_extract=A_)

									return os.path.join(A_				,				self.dummy_file_name)




							@property
							def    UpperCAmelCase__			(       self     :       List[str]):
									return os.path.join(self.datasets_scripts_dir				,				self.dataset_name				,				self.dummy_zip_file)




							@property
							def    UpperCAmelCase__			(       self     :       Optional[int]):
									if self._bucket_url is None:
											lowerCAmelCase_			:     str									= hf_github_url(self.dataset_name				,				self.dummy_zip_file.replace(os.sep				,				'''/'''))
									return self._bucket_url




							@property
							def    UpperCAmelCase__			(       self     :       List[Any]):
									# return full path if its a dir
									if os.path.isdir(self.dummy_file):
											return self.dummy_file
									# else cut off path to file -> example `xsum`.
									return "/".join(self.dummy_file.replace(os.sep				,				'''/''').split('''/''')[:-1])




							def    UpperCAmelCase__			(       self     :       Union[str, Any]				,				A_     :       Dict				,				*A_     :       List[Any]):
									if self.load_existing_dummy_data:
											# dummy data is downloaded and tested
											lowerCAmelCase_			:     Union[str, Any]									= self.dummy_file
									else:
											# dummy data cannot be downloaded and only the path to dummy file is returned
											lowerCAmelCase_			:     Optional[int]									= self.dummy_file_name

									# special case when data_url is a dict
									if isinstance(A_				,				A_):
											return self.create_dummy_data_dict(A_				,				A_)
									elif isinstance(A_				,				(list, tuple)):
											return self.create_dummy_data_list(A_				,				A_)
									else:
											return self.create_dummy_data_single(A_				,				A_)




							def    UpperCAmelCase__			(       self     :       Optional[int]				,				A_     :       Tuple				,				*A_     :       int):
									return self.download_and_extract(A_)




							def    UpperCAmelCase__			(       self     :       Tuple				,				A_     :       List[str]				,				A_     :       Optional[Any]):
									return self.download_and_extract(A_)




							def    UpperCAmelCase__			(       self     :       int				,				A_     :       Optional[int]				,				*A_     :       str				,				**A_     :       List[Any]):
									return path




							def    UpperCAmelCase__			(       self     :       Tuple):
									return {}




							def    UpperCAmelCase__			(       self     :       Optional[Any]				,				A_     :       Union[str, Any]				,				A_     :       List[Any]):
									lowerCAmelCase_			:     Union[str, Any]									= {}
									for key, single_urls in data_url.items():
											for download_callback in self.download_callbacks:
													if isinstance(A_				,				A_):
															for single_url in single_urls:
																	download_callback(A_)
													else:
															lowerCAmelCase_			:     Any									= single_urls
															download_callback(A_)
            # we force the name of each key to be the last file / folder name of the url path
            # if the url has arguments, we need to encode them with urllib.parse.quote_plus
											if isinstance(A_				,				A_):
													lowerCAmelCase_			:     Any									= [os.path.join(A_				,				urllib.parse.quote_plus(Path(A_).name)) for x in single_urls]
											else:
													lowerCAmelCase_			:     Optional[int]									= single_urls
													lowerCAmelCase_			:     List[str]									= os.path.join(A_				,				urllib.parse.quote_plus(Path(A_).name))
											lowerCAmelCase_			:     Dict									= value

									# make sure that values are unique
									if all(isinstance(A_				,				A_) for i in dummy_data_dict.values()) and len(set(dummy_data_dict.values())) < len(
									    dummy_data_dict.values()):
											# append key to value to make its name unique
											lowerCAmelCase_			:     Tuple									= {key: value + key for key, value in dummy_data_dict.items()}

									return dummy_data_dict




							def    UpperCAmelCase__			(       self     :       Dict				,				A_     :       List[str]				,				A_     :       str):
									lowerCAmelCase_			:     Optional[Any]									= []
									# trick: if there are many shards named like `data.txt-000001-of-00300`, only use the first one
									lowerCAmelCase_			:     str									= all(bool(re.findall('''[0-9]{3,}-of-[0-9]{3,}'''				,				A_)) for url in data_url)
									lowerCAmelCase_			:     Optional[Any]									= all(
									    url.startswith('''https://ftp.ncbi.nlm.nih.gov/pubmed/baseline/pubmed''') for url in data_url)
									if data_url and (is_tf_records or is_pubmed_records):
											lowerCAmelCase_			:     Any									= [data_url[0]] * len(A_)
									for single_url in data_url:
											for download_callback in self.download_callbacks:
													download_callback(A_)
											# we force the name of each key to be the last file / folder name of the url path
											# if the url has arguments, we need to encode them with urllib.parse.quote_plus
											lowerCAmelCase_			:     int									= os.path.join(A_				,				urllib.parse.quote_plus(single_url.split('''/''')[-1]))
											dummy_data_list.append(A_)
									return dummy_data_list




							def    UpperCAmelCase__			(       self     :       List[str]				,				A_     :       Optional[Any]				,				A_     :       Tuple):
									for download_callback in self.download_callbacks:
											download_callback(A_)
									# we force the name of each key to be the last file / folder name of the url path
									# if the url has arguments, we need to encode them with urllib.parse.quote_plus
									lowerCAmelCase_			:     Tuple									= os.path.join(A_				,				urllib.parse.quote_plus(data_url.split('''/''')[-1]))
									if os.path.exists(A_) or not self.load_existing_dummy_data:
											return value
									else:
											# Backward compatibility, maybe deprecate at one point.
											# For many datasets with single url calls to dl_manager.download_and_extract,
											# the dummy_data.zip file is actually the zipped downloaded file
											# while now we expected the dummy_data.zip file to be a directory containing
											# the downloaded file.
											return path_to_dummy_data




							def    UpperCAmelCase__			(       self     :       int):
									pass




							def    UpperCAmelCase__			(       self     :       Optional[int]):
									pass




							def    UpperCAmelCase__			(       self     :       List[str]				,				A_     :       str):
									def _iter_archive_members(A_     :       Any):
											# this preserves the order of the members inside the ZIP archive
											lowerCAmelCase_			:     Optional[int]									= Path(self.dummy_file).parent
											lowerCAmelCase_			:     Optional[int]									= path.relative_to(A_)
											with ZipFile(self.local_path_to_dummy_data) as zip_file:
													lowerCAmelCase_			:     Tuple									= zip_file.namelist()
											for member in members:
													if member.startswith(relative_path.as_posix()):
															yield dummy_parent_path.joinpath(A_)

									lowerCAmelCase_			:     List[Any]									= Path(A_)
									lowerCAmelCase_			:     Optional[int]									= _iter_archive_members(A_) if self.use_local_dummy_data else path.rglob('''*''')
									for file_path in file_paths:
											if file_path.is_file() and not file_path.name.startswith(('''.''', '''__''')):
													yield file_path.relative_to(A_).as_posix(), file_path.open('''rb''')







							def    UpperCAmelCase__			(       self     :       Dict				,				A_     :       Any):
									if not isinstance(A_				,				A_):
											lowerCAmelCase_			:     Dict									= [paths]
									for path in paths:
											if os.path.isfile(A_):
													if os.path.basename(A_).startswith(('''.''', '''__''')):
															return
													yield path
											else:
													for dirpath, dirnames, filenames in os.walk(A_):
															if os.path.basename(A_).startswith(('''.''', '''__''')):
																	continue
															dirnames.sort()
															for filename in sorted(A_):
																	if filename.startswith(('''.''', '''__''')):
																			continue
																	yield os.path.join(A_				,				A_)





