
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   A      :
			'''simple docstring'''




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







					"""simple docstring"""






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






			def        a_	(      self       :   Tuple       )     ->     Optional[int]:







					"""simple docstring"""






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






			def        a_	(      self       :   str       )     ->     Tuple:







					"""simple docstring"""






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

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

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

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






			def        a_	(      self       :   int       )     ->     int:







					"""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       :   Tuple							,     __lowerCAmelCase       :   Union[str, Any]							,     __lowerCAmelCase       :   int							,     __lowerCAmelCase       :   List[str]							,     __lowerCAmelCase       :   List[Any]							,     __lowerCAmelCase       :   Tuple							,     __lowerCAmelCase       :   List[Any]       )     ->     Optional[int]:







					"""simple docstring"""






					A__  				=       MPNetModel(config=snake_case__       )
					model.to(snake_case__       )
					model.eval()
					A__  				=       model(snake_case__							,     snake_case__       )
					A__  				=       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       :   Dict							,     __lowerCAmelCase       :   Union[str, Any]							,     __lowerCAmelCase       :   Tuple							,     __lowerCAmelCase       :   int							,     __lowerCAmelCase       :   Optional[int]							,     __lowerCAmelCase       :   List[Any]							,     __lowerCAmelCase       :   Any       )     ->     List[str]:







					"""simple docstring"""






					A__  				=       MPNetForQuestionAnswering(config=snake_case__       )
					model.to(snake_case__       )
					model.eval()
					A__  				=       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       :   str							,     __lowerCAmelCase       :   Any							,     __lowerCAmelCase       :   Union[str, Any]							,     __lowerCAmelCase       :   Tuple							,     __lowerCAmelCase       :   List[Any]							,     __lowerCAmelCase       :   Tuple							,     __lowerCAmelCase       :   Dict       )     ->     Union[str, Any]:







					"""simple docstring"""






					A__  				=       self.num_labels
					A__  				=       MPNetForSequenceClassification(snake_case__       )
					model.to(snake_case__       )
					model.eval()
					A__  				=       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       :   Union[str, Any]							,     __lowerCAmelCase       :   Tuple							,     __lowerCAmelCase       :   Union[str, Any]							,     __lowerCAmelCase       :   List[str]							,     __lowerCAmelCase       :   str							,     __lowerCAmelCase       :   Optional[int]							,     __lowerCAmelCase       :   Tuple       )     ->     Dict:







					"""simple docstring"""






					A__  				=       self.num_choices
					A__  				=       MPNetForMultipleChoice(config=snake_case__       )
					model.to(snake_case__       )
					model.eval()
					A__  				=       input_ids.unsqueeze(1       ).expand(-1							,     self.num_choices							,     -1       ).contiguous()
					A__  				=       input_mask.unsqueeze(1       ).expand(-1							,     self.num_choices							,     -1       ).contiguous()
					A__  				=       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       :   Optional[int]							,     __lowerCAmelCase       :   Dict							,     __lowerCAmelCase       :   Optional[int]							,     __lowerCAmelCase       :   Tuple							,     __lowerCAmelCase       :   List[Any]							,     __lowerCAmelCase       :   List[Any]							,     __lowerCAmelCase       :   Any       )     ->     List[str]:







					"""simple docstring"""






					A__  				=       self.num_labels
					A__  				=       MPNetForTokenClassification(config=snake_case__       )
					model.to(snake_case__       )
					model.eval()
					A__  				=       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       :   List[Any]       )     ->     List[Any]:







					"""simple docstring"""






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


@require_torch
class   A      (lowerCamelCase__       ,					lowerCamelCase__       ,					unittest.TestCase   ):
			'''simple docstring'''




			__lowerCamelCase     :	Tuple          =   (
			    (
			        MPNetForMaskedLM,
			        MPNetForMultipleChoice,
			        MPNetForQuestionAnswering,
			        MPNetForSequenceClassification,
			        MPNetForTokenClassification,
			        MPNetModel,
			    )
			    if is_torch_available()
			    else ()
			)
			__lowerCamelCase     :	Tuple          =   (
			    {
			        """feature-extraction""": MPNetModel,
			        """fill-mask""": MPNetForMaskedLM,
			        """question-answering""": MPNetForQuestionAnswering,
			        """text-classification""": MPNetForSequenceClassification,
			        """token-classification""": MPNetForTokenClassification,
			        """zero-shot""": MPNetForSequenceClassification,
			    }
			    if is_torch_available()
			    else {}
			)
			__lowerCamelCase     :	int          =   False
			__lowerCamelCase     :	Union[str, Any]          =   True






			def        a_	(      self       :   Tuple       )     ->     Any:







					"""simple docstring"""






					A__  				=       MPNetModelTester(self       )
					A__  				=       ConfigTester(self							,     config_class=snake_case__							,     hidden_size=37       )






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







					"""simple docstring"""






					self.config_tester.run_common_tests()






			def        a_	(      self       :   Any       )     ->     Any:







					"""simple docstring"""






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






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







					"""simple docstring"""






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






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







					"""simple docstring"""






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






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







					"""simple docstring"""






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







			def        a_	(      self       :   Tuple       )     ->     int:







					"""simple docstring"""






					A__  				=       self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_mpnet_for_question_answering(*snake_case__       )


@require_torch
class   A      (unittest.TestCase   ):
			'''simple docstring'''




			@slow
			def        a_	(      self       :   Any       )     ->     str:







					"""simple docstring"""






					A__  				=       MPNetModel.from_pretrained("""microsoft/mpnet-base"""       )
					A__  				=       torch.tensor([[0, 3_45, 2_32, 3_28, 7_40, 1_40, 16_95, 69, 60_78, 15_88, 2]]       )
					A__  				=       model(snake_case__       )[0]
					A__  				=       torch.Size((1, 11, 7_68)       )
					self.assertEqual(output.shape							,     snake_case__       )
					A__  				=       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       )       )










import itertools
import os
import random
import tempfile
import unittest

import numpy as np
from datasets import load_dataset

from transformers import is_speech_available
from transformers.testing_utils import check_json_file_has_correct_format, require_torch, require_torchaudio
from transformers.utils.import_utils import is_torch_available

from ...test_sequence_feature_extraction_common import SequenceFeatureExtractionTestMixin


if is_speech_available():
					from transformers import WhisperFeatureExtractor

if is_torch_available():
					import torch

A   : Dict			=				random.Random()







def        __lowerCamelCase (							__a       :Dict   ,					__a       :str=1.0   ,					__a       :List[Any]=None   ,					__a       :List[str]=None   )		->    Optional[int]:






		"""simple docstring"""

		if rng is None:
				A__  				=       global_rng

		A__  				=       []
		for batch_idx in range(shape[0]   ):
				values.append([]   )
				for _ in range(shape[1]   ):
						values[-1].append(rng.random() * scale   )

		return values


@require_torch
@require_torchaudio
class   A      (unittest.TestCase   ):
			'''simple docstring'''




			def __init__(      self       :   Dict							,     __lowerCAmelCase       :   Any							,     __lowerCAmelCase       :   str=7							,     __lowerCAmelCase       :   List[Any]=4_00							,     __lowerCAmelCase       :   Optional[Any]=20_00							,     __lowerCAmelCase       :   Dict=10							,     __lowerCAmelCase       :   Union[str, Any]=1_60							,     __lowerCAmelCase       :   List[Any]=8							,     __lowerCAmelCase       :   List[Any]=0.0							,     __lowerCAmelCase       :   int=40_00							,     __lowerCAmelCase       :   Any=False							,     __lowerCAmelCase       :   List[str]=True							,     )     ->     Optional[int]:







					"""simple docstring"""






					A__  				=       parent
					A__  				=       batch_size
					A__  				=       min_seq_length
					A__  				=       max_seq_length
					A__  				=       (self.max_seq_length - self.min_seq_length) // (self.batch_size - 1)
					A__  				=       padding_value
					A__  				=       sampling_rate
					A__  				=       return_attention_mask
					A__  				=       do_normalize
					A__  				=       feature_size
					A__  				=       chunk_length
					A__  				=       hop_length






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







					"""simple docstring"""






					return {
					    "feature_size": self.feature_size,
					    "hop_length": self.hop_length,
					    "chunk_length": self.chunk_length,
					    "padding_value": self.padding_value,
					    "sampling_rate": self.sampling_rate,
					    "return_attention_mask": self.return_attention_mask,
					    "do_normalize": self.do_normalize,
					}







			def        a_	(      self       :   List[str]							,     __lowerCAmelCase       :   List[Any]=False							,     __lowerCAmelCase       :   int=False       )     ->     str:







					"""simple docstring"""






					def _flatten(__lowerCAmelCase       :   Optional[int]       ):
							return list(itertools.chain(*__lowerCAmelCase       )       )

					if equal_length:
							A__  				=       [floats_list((self.max_seq_length, self.feature_size)       ) for _ in range(self.batch_size       )]
					else:
							# make sure that inputs increase in size
							A__  				=       [
							    floats_list((x, self.feature_size)       )
							    for x in range(self.min_seq_length							,     self.max_seq_length							,     self.seq_length_diff       )
							]
					if numpify:
							A__  				=       [np.asarray(__lowerCAmelCase       ) for x in speech_inputs]
					return speech_inputs


@require_torch
@require_torchaudio
class   A      (SCREAMING_SNAKE_CASE       ,					unittest.TestCase   ):
			'''simple docstring'''




			__lowerCamelCase     :	int          =   WhisperFeatureExtractor if is_speech_available() else None






			def        a_	(      self       :   Any       )     ->     str:







					"""simple docstring"""






					A__  				=       WhisperFeatureExtractionTester(self       )






			def        a_	(      self       :   Dict       )     ->     Tuple:







					"""simple docstring"""






					A__  				=       self.feature_extraction_class(**self.feat_extract_dict       )

					with tempfile.TemporaryDirectory() as tmpdirname:
							A__  				=       feat_extract_first.save_pretrained(__lowerCAmelCase       )[0]
							check_json_file_has_correct_format(__lowerCAmelCase       )
							A__  				=       self.feature_extraction_class.from_pretrained(__lowerCAmelCase       )

					A__  				=       feat_extract_first.to_dict()
					A__  				=       feat_extract_second.to_dict()
					A__  				=       feat_extract_first.mel_filters
					A__  				=       feat_extract_second.mel_filters
					self.assertTrue(np.allclose(__lowerCAmelCase							,     __lowerCAmelCase       )       )
					self.assertEqual(__lowerCAmelCase							,     __lowerCAmelCase       )






			def        a_	(      self       :   Dict       )     ->     str:







					"""simple docstring"""






					A__  				=       self.feature_extraction_class(**self.feat_extract_dict       )

					with tempfile.TemporaryDirectory() as tmpdirname:
							A__  				=       os.path.join(__lowerCAmelCase							,     """feat_extract.json"""       )
							feat_extract_first.to_json_file(__lowerCAmelCase       )
							A__  				=       self.feature_extraction_class.from_json_file(__lowerCAmelCase       )

					A__  				=       feat_extract_first.to_dict()
					A__  				=       feat_extract_second.to_dict()
					A__  				=       feat_extract_first.mel_filters
					A__  				=       feat_extract_second.mel_filters
					self.assertTrue(np.allclose(__lowerCAmelCase							,     __lowerCAmelCase       )       )
					self.assertEqual(__lowerCAmelCase							,     __lowerCAmelCase       )






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







					"""simple docstring"""






					A__  				=       self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()       )
					# create three inputs of length 800, 1000, and 1200
					A__  				=       [floats_list((1, x)       )[0] for x in range(8_00							,     14_00							,     2_00       )]
					A__  				=       [np.asarray(__lowerCAmelCase       ) for speech_input in speech_inputs]

					# Test feature size
					A__  				=       feature_extractor(__lowerCAmelCase							,     padding="""max_length"""							,     return_tensors="""np"""       ).input_features
					self.assertTrue(input_features.ndim == 3       )
					self.assertTrue(input_features.shape[-1] == feature_extractor.nb_max_frames       )
					self.assertTrue(input_features.shape[-2] == feature_extractor.feature_size       )

					# Test not batched input
					A__  				=       feature_extractor(speech_inputs[0]							,     return_tensors="""np"""       ).input_features
					A__  				=       feature_extractor(np_speech_inputs[0]							,     return_tensors="""np"""       ).input_features
					self.assertTrue(np.allclose(__lowerCAmelCase							,     __lowerCAmelCase							,     atol=1e-3       )       )

					# Test batched
					A__  				=       feature_extractor(__lowerCAmelCase							,     return_tensors="""np"""       ).input_features
					A__  				=       feature_extractor(__lowerCAmelCase							,     return_tensors="""np"""       ).input_features
					for enc_seq_a, enc_seq_a in zip(__lowerCAmelCase							,     __lowerCAmelCase       ):
							self.assertTrue(np.allclose(__lowerCAmelCase							,     __lowerCAmelCase							,     atol=1e-3       )       )

					# Test 2-D numpy arrays are batched.
					A__  				=       [floats_list((1, x)       )[0] for x in (8_00, 8_00, 8_00)]
					A__  				=       np.asarray(__lowerCAmelCase       )
					A__  				=       feature_extractor(__lowerCAmelCase							,     return_tensors="""np"""       ).input_features
					A__  				=       feature_extractor(__lowerCAmelCase							,     return_tensors="""np"""       ).input_features
					for enc_seq_a, enc_seq_a in zip(__lowerCAmelCase							,     __lowerCAmelCase       ):
							self.assertTrue(np.allclose(__lowerCAmelCase							,     __lowerCAmelCase							,     atol=1e-3       )       )

					# Test truncation required
					A__  				=       [floats_list((1, x)       )[0] for x in range(2_00							,     (feature_extractor.n_samples + 5_00)							,     2_00       )]
					A__  				=       [np.asarray(__lowerCAmelCase       ) for speech_input in speech_inputs]

					A__  				=       [x[: feature_extractor.n_samples] for x in speech_inputs]
					A__  				=       [np.asarray(__lowerCAmelCase       ) for speech_input in speech_inputs_truncated]

					A__  				=       feature_extractor(__lowerCAmelCase							,     return_tensors="""np"""       ).input_features
					A__  				=       feature_extractor(__lowerCAmelCase							,     return_tensors="""np"""       ).input_features
					for enc_seq_a, enc_seq_a in zip(__lowerCAmelCase							,     __lowerCAmelCase       ):
							self.assertTrue(np.allclose(__lowerCAmelCase							,     __lowerCAmelCase							,     atol=1e-3       )       )






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







					"""simple docstring"""






					import torch

					A__  				=       self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()       )
					A__  				=       np.random.rand(1_00							,     32       ).astype(np.floataa       )
					A__  				=       np_speech_inputs.tolist()

					for inputs in [py_speech_inputs, np_speech_inputs]:
							A__  				=       feature_extractor.pad([{"""input_features""": inputs}]							,     return_tensors="""np"""       )
							self.assertTrue(np_processed.input_features.dtype == np.floataa       )
							A__  				=       feature_extractor.pad([{"""input_features""": inputs}]							,     return_tensors="""pt"""       )
							self.assertTrue(pt_processed.input_features.dtype == torch.floataa       )






			def        a_	(      self       :   List[Any]							,     __lowerCAmelCase       :   Any       )     ->     Dict:







					"""simple docstring"""






					A__  				=       load_dataset("""hf-internal-testing/librispeech_asr_dummy"""							,     """clean"""							,     split="""validation"""       )
					# automatic decoding with librispeech
					A__  				=       ds.sort("""id"""       ).select(range(__lowerCAmelCase       )       )[:num_samples]["""audio"""]

					return [x["array"] for x in speech_samples]






			def        a_	(      self       :   Dict       )     ->     Any:







					"""simple docstring"""






					A__  				=       torch.tensor(
					    [
					        0.1_1_9_3, -0.0_9_4_6, -0.1_0_9_8, -0.0_1_9_6, 0.0_2_2_5, -0.0_6_9_0, -0.1_7_3_6, 0.0_9_5_1,
					        0.0_9_7_1, -0.0_8_1_7, -0.0_7_0_2, 0.0_1_6_2, 0.0_2_6_0, 0.0_0_1_7, -0.0_1_9_2, -0.1_6_7_8,
					        0.0_7_0_9, -0.1_8_6_7, -0.0_6_5_5, -0.0_2_7_4, -0.0_2_3_4, -0.1_8_8_4, -0.0_5_1_6, -0.0_5_5_4,
					        -0.0_2_7_4, -0.1_4_2_5, -0.1_4_2_3, 0.0_8_3_7, 0.0_3_7_7, -0.0_8_5_4
					    ]       )
					# fmt: on

					A__  				=       self._load_datasamples(1       )
					A__  				=       WhisperFeatureExtractor()
					A__  				=       feature_extractor(__lowerCAmelCase							,     return_tensors="""pt"""       ).input_features
					self.assertEqual(input_features.shape							,     (1, 80, 30_00)       )
					self.assertTrue(torch.allclose(input_features[0, 0, :30]							,     __lowerCAmelCase							,     atol=1e-4       )       )







			def        a_	(      self       :   Dict       )     ->     Tuple:







					"""simple docstring"""






					A__  				=       self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()       )
					A__  				=       self._load_datasamples(1       )[0]
					A__  				=       ((audio - audio.min()) / (audio.max() - audio.min())) * 6_55_35  # Rescale to [0, 65535] to show issue
					A__  				=       feat_extract.zero_mean_unit_var_norm([audio]							,     attention_mask=__lowerCAmelCase       )[0]

					self.assertTrue(np.all(np.mean(__lowerCAmelCase       ) < 1e-3       )       )
					self.assertTrue(np.all(np.abs(np.var(__lowerCAmelCase       ) - 1       ) < 1e-3       )       )



