






'''simple docstring'''




from __future__ import annotations

import unittest

from transformers import DebertaVaConfig, is_tf_available
from transformers.testing_utils import require_tf, slow

from ...test_configuration_common import ConfigTester
from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_tf_available():
    import tensorflow as tf

    from transformers import (
        TFDebertaVaForMaskedLM,
        TFDebertaVaForQuestionAnswering,
        TFDebertaVaForSequenceClassification,
        TFDebertaVaForTokenClassification,
        TFDebertaVaModel,
    )

class    _UpperCAmelCase			:




   """simple docstring"""

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





         '''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			     =	relative_attention
         _A			     =	position_biased_input
         _A			     =	pos_att_type
         _A			     =	scope

   def 						lowerCAmelCase (		self :	int		):





         '''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
         if self.use_token_type_ids:
               _A			     =	ids_tensor([self.batch_size, self.seq_length]      ,      self.type_vocab_size		)

         _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			     =	DebertaVaConfig(
             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      ,      type_vocab_size=self.type_vocab_size      ,      relative_attention=self.relative_attention      ,      position_biased_input=self.position_biased_input      ,      initializer_range=self.initializer_range      ,      return_dict=__UpperCAmelCase      ,      )

         return config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels

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





         '''simple docstring'''





         _A			     =	TFDebertaVaModel(config=__UpperCAmelCase		)
         _A			     =	{"input_ids": input_ids, "attention_mask": input_mask, "token_type_ids": token_type_ids}

         _A			     =	[input_ids, input_mask]
         _A			     =	model(__UpperCAmelCase		)

         _A			     =	model(__UpperCAmelCase		)

         self.parent.assertEqual(result.last_hidden_state.shape      ,      (self.batch_size, self.seq_length, self.hidden_size)		)

   def 						lowerCAmelCase (		self :	str      ,      __UpperCAmelCase :	Union[str, Any]      ,      __UpperCAmelCase :	List[Any]      ,      __UpperCAmelCase :	Dict      ,      __UpperCAmelCase :	Union[str, Any]      ,      __UpperCAmelCase :	Union[str, Any]      ,      __UpperCAmelCase :	Tuple      ,      __UpperCAmelCase :	Dict		):





         '''simple docstring'''





         _A			     =	TFDebertaVaForMaskedLM(config=__UpperCAmelCase		)
         _A			     =	{
             "input_ids": input_ids,
             "attention_mask": input_mask,
             "token_type_ids": token_type_ids,
         }
         _A			     =	model(__UpperCAmelCase		)
         self.parent.assertEqual(result.logits.shape      ,      (self.batch_size, self.seq_length, self.vocab_size)		)

   def 						lowerCAmelCase (		self :	Optional[Any]      ,      __UpperCAmelCase :	List[str]      ,      __UpperCAmelCase :	Any      ,      __UpperCAmelCase :	List[str]      ,      __UpperCAmelCase :	Any      ,      __UpperCAmelCase :	str      ,      __UpperCAmelCase :	List[Any]      ,      __UpperCAmelCase :	Any		):





         '''simple docstring'''





         _A			     =	self.num_labels
         _A			     =	TFDebertaVaForSequenceClassification(config=__UpperCAmelCase		)
         _A			     =	{
             "input_ids": input_ids,
             "attention_mask": input_mask,
             "token_type_ids": token_type_ids,
         }

         _A			     =	model(__UpperCAmelCase		)
         self.parent.assertEqual(result.logits.shape      ,      (self.batch_size, self.num_labels)		)

   def 						lowerCAmelCase (		self :	List[str]      ,      __UpperCAmelCase :	str      ,      __UpperCAmelCase :	Dict      ,      __UpperCAmelCase :	Dict      ,      __UpperCAmelCase :	List[str]      ,      __UpperCAmelCase :	Any      ,      __UpperCAmelCase :	Optional[Any]      ,      __UpperCAmelCase :	Tuple		):





         '''simple docstring'''





         _A			     =	self.num_labels
         _A			     =	TFDebertaVaForTokenClassification(config=__UpperCAmelCase		)
         _A			     =	{
             "input_ids": input_ids,
             "attention_mask": input_mask,
             "token_type_ids": token_type_ids,
         }
         _A			     =	model(__UpperCAmelCase		)
         self.parent.assertEqual(result.logits.shape      ,      (self.batch_size, self.seq_length, self.num_labels)		)

   def 						lowerCAmelCase (		self :	Tuple      ,      __UpperCAmelCase :	Dict      ,      __UpperCAmelCase :	Union[str, Any]      ,      __UpperCAmelCase :	List[str]      ,      __UpperCAmelCase :	List[str]      ,      __UpperCAmelCase :	Dict      ,      __UpperCAmelCase :	Any      ,      __UpperCAmelCase :	Optional[Any]		):





         '''simple docstring'''





         _A			     =	TFDebertaVaForQuestionAnswering(config=__UpperCAmelCase		)
         _A			     =	{
             "input_ids": input_ids,
             "attention_mask": input_mask,
             "token_type_ids": token_type_ids,
         }

         _A			     =	model(__UpperCAmelCase		)
         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 						lowerCAmelCase (		self :	List[Any]		):





         '''simple docstring'''





         _A			     =	self.prepare_config_and_inputs()
         (
             (
             _A
         )     ,	(
             _A
         )     ,	(
             _A
         )     ,	(
             _A
         )     ,	(
             _A
         )     ,	(
             _A
         )     ,	(
             _A
         )     ,	
         )			     =	config_and_inputs
         _A			     =	{"input_ids": input_ids, "token_type_ids": token_type_ids, "attention_mask": input_mask}
         return config, inputs_dict





@require_tf
class    _UpperCAmelCase			(     snake_case_							,    snake_case_							,    unittest.TestCase						):




   """simple docstring"""

   snake_case    				=						(
       (
           TFDebertaVaModel,
           TFDebertaVaForMaskedLM,
           TFDebertaVaForQuestionAnswering,
           TFDebertaVaForSequenceClassification,
           TFDebertaVaForTokenClassification,
       )
       if is_tf_available()
       else ()
   )
   snake_case    				=						(
       {
           '''feature-extraction''': TFDebertaVaModel,
           '''fill-mask''': TFDebertaVaForMaskedLM,
           '''question-answering''': TFDebertaVaForQuestionAnswering,
           '''text-classification''': TFDebertaVaForSequenceClassification,
           '''token-classification''': TFDebertaVaForTokenClassification,
           '''zero-shot''': TFDebertaVaForSequenceClassification,
       }
       if is_tf_available()
       else {}
   )

   snake_case    				=						False
   snake_case    				=						False

   def 						lowerCAmelCase (		self :	int		):





         '''simple docstring'''





         _A			     =	TFDebertaVaModelTester(self		)
         _A			     =	ConfigTester(self      ,      config_class=__UpperCAmelCase      ,      hidden_size=37		)

   def 						lowerCAmelCase (		self :	List[str]		):





         '''simple docstring'''





         self.config_tester.run_common_tests()

   def 						lowerCAmelCase (		self :	List[Any]		):





         '''simple docstring'''





         _A			     =	self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_model(*__UpperCAmelCase		)

   def 						lowerCAmelCase (		self :	Optional[Any]		):





         '''simple docstring'''





         _A			     =	self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_for_masked_lm(*__UpperCAmelCase		)

   def 						lowerCAmelCase (		self :	Optional[Any]		):





         '''simple docstring'''





         _A			     =	self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_for_question_answering(*__UpperCAmelCase		)

   def 						lowerCAmelCase (		self :	List[Any]		):





         '''simple docstring'''





         _A			     =	self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_for_sequence_classification(*__UpperCAmelCase		)

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





         '''simple docstring'''





         _A			     =	self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_for_token_classification(*__UpperCAmelCase		)

   @slow
   def 						lowerCAmelCase (		self :	Optional[int]		):





         '''simple docstring'''





         _A			     =	TFDebertaVaModel.from_pretrained("kamalkraj/deberta-v2-xlarge"		)
         self.assertIsNotNone(__UpperCAmelCase		)





@require_tf
class    _UpperCAmelCase			(     unittest.TestCase						):




   """simple docstring"""

   @unittest.skip(reason="Model not available yet"		)
   def 						lowerCAmelCase (		self :	str		):





         '''simple docstring'''





         pass

   @slow
   def 						lowerCAmelCase (		self :	List[str]		):





         '''simple docstring'''





         _A			     =	TFDebertaVaModel.from_pretrained("kamalkraj/deberta-v2-xlarge"		)
         _A			     =	tf.constant([[0, 31414, 232, 328, 740, 1140, 12695, 69, 46078, 1588, 2]]		)
         _A			     =	tf.constant([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]		)
         _A			     =	model(__UpperCAmelCase      ,      attention_mask=__UpperCAmelCase		)[0]

         _A			     =	tf.constant(
             [[[0.2356, 0.1948, 0.0369], [-0.1063, 0.3586, -0.5152], [-0.6399, -0.0259, -0.2525]]]		)
         tf.debugging.assert_near(output[:, 1:4, 1:4]      ,      __UpperCAmelCase      ,      atol=1E-4		)






from typing import List, Optional, Union

import numpy as np

from ....audio_utils import mel_filter_bank, optimal_fft_length, spectrogram, window_function
from ....feature_extraction_sequence_utils import SequenceFeatureExtractor
from ....feature_extraction_utils import BatchFeature
from ....file_utils import PaddingStrategy, TensorType
from ....utils import logging


__UpperCamelCase		:       Optional[Any]									=     logging.get_logger(__name__)





class 					lowercase__						(   UpperCamelCase_):
						UpperCamelCase_     =		["""input_features""", """attention_mask"""]


						def __init__(       self	:				Any    ,		UpperCamelCase__	:				List[str]=80    ,		UpperCamelCase__	:				Tuple=1_6000    ,		UpperCamelCase__	:				Dict=0.0    ,		UpperCamelCase__	:				Tuple=10    ,		UpperCamelCase__	:				int=25    ,		UpperCamelCase__	:				Optional[Any]="hamming_window"    ,		UpperCamelCase__	:				Tuple=3_2768.0    ,		UpperCamelCase__	:				str=0.97    ,		UpperCamelCase__	:				List[str]=1.0    ,		UpperCamelCase__	:				Optional[int]=True    ,		UpperCamelCase__	:				Dict=True    ,		UpperCamelCase__	:				List[str]=False    ,		**UpperCamelCase__	:				List[str]    ,		):





												'''simple docstring'''





												super().__init__(feature_size=UpperCamelCase__    ,		sampling_rate=UpperCamelCase__    ,		padding_value=UpperCamelCase__    ,		**UpperCamelCase__						)

												SCREAMING_SNAKE_CASE							:    Dict							=							feature_size
												SCREAMING_SNAKE_CASE							:    Union[str, Any]							=							sampling_rate
												SCREAMING_SNAKE_CASE							:    int							=							padding_value
												SCREAMING_SNAKE_CASE							:    Optional[Any]							=							hop_length
												SCREAMING_SNAKE_CASE							:    Tuple							=							win_length
												SCREAMING_SNAKE_CASE							:    Union[str, Any]							=							frame_signal_scale
												SCREAMING_SNAKE_CASE							:    int							=							preemphasis_coeff
												SCREAMING_SNAKE_CASE							:    List[Any]							=							mel_floor
												SCREAMING_SNAKE_CASE							:    int							=							normalize_means
												SCREAMING_SNAKE_CASE							:    List[str]							=							normalize_vars
												SCREAMING_SNAKE_CASE							:    Any							=							win_function
												SCREAMING_SNAKE_CASE							:    Union[str, Any]							=							return_attention_mask

												SCREAMING_SNAKE_CASE							:    int							=							win_length * sampling_rate // 1000
												SCREAMING_SNAKE_CASE							:    Optional[int]							=							hop_length * sampling_rate // 1000

												SCREAMING_SNAKE_CASE							:    int							=							optimal_fft_length(self.sample_size						)
												SCREAMING_SNAKE_CASE							:    Tuple							=							(self.n_fft // 2) + 1


						def        __A       (       self	:				str    ,		UpperCamelCase__	:				np.array						):





												'''simple docstring'''





												if self.win_function == "hamming_window":
																		SCREAMING_SNAKE_CASE							:    List[str]							=							window_function(window_length=self.sample_size    ,		name=self.win_function    ,		periodic=UpperCamelCase__						)
												else:
																		SCREAMING_SNAKE_CASE							:    Tuple							=							window_function(window_length=self.sample_size    ,		name=self.win_function						)

												SCREAMING_SNAKE_CASE							:    Optional[Any]							=							mel_filter_bank(
												    num_frequency_bins=self.n_freqs    ,		num_mel_filters=self.feature_size    ,		min_frequency=0.0    ,		max_frequency=self.sampling_rate / 2.0    ,		sampling_rate=self.sampling_rate    ,		)

												SCREAMING_SNAKE_CASE							:    Tuple							=							spectrogram(
												    one_waveform * self.frame_signal_scale    ,		window=UpperCamelCase__    ,		frame_length=self.sample_size    ,		hop_length=self.sample_stride    ,		fft_length=self.n_fft    ,		center=UpperCamelCase__    ,		preemphasis=self.preemphasis_coeff    ,		mel_filters=UpperCamelCase__    ,		mel_floor=self.mel_floor    ,		log_mel='''log'''    ,		)
												return msfc_features.T


						def        __A       (       self	:				int    ,		UpperCamelCase__	:				Optional[Any]    ,		UpperCamelCase__	:				List[Any]    ,		UpperCamelCase__	:				Optional[int]						):





												'''simple docstring'''





												if self.normalize_means:
																		SCREAMING_SNAKE_CASE							:    str							=							x[:input_length].mean(axis=0						)
																		SCREAMING_SNAKE_CASE							:    List[str]							=							np.subtract(UpperCamelCase__    ,		UpperCamelCase__						)
												if self.normalize_vars:
																		SCREAMING_SNAKE_CASE							:    str							=							x[:input_length].std(axis=0						)
																		SCREAMING_SNAKE_CASE							:    Optional[int]							=							np.divide(UpperCamelCase__    ,		UpperCamelCase__						)

												if input_length < x.shape[0]:
																		SCREAMING_SNAKE_CASE							:    List[str]							=							padding_value

												# make sure array is in float32
												SCREAMING_SNAKE_CASE							:    str							=							x.astype(np.floataa						)

												return x


						def        __A       (       self	:				Union[str, Any]    ,		UpperCamelCase__	:				List[np.ndarray]    ,		UpperCamelCase__	:				Optional[np.ndarray] = None						):





												'''simple docstring'''





												SCREAMING_SNAKE_CASE							:    Tuple							=							attention_mask.sum(-1						) if attention_mask is not None else [x.shape[0] for x in input_features]
												return [self._normalize_one(UpperCamelCase__    ,		UpperCamelCase__    ,		self.padding_value						) for x, n in zip(UpperCamelCase__    ,		UpperCamelCase__						)]



						def __call__(       self	:				Dict    ,		UpperCamelCase__	:				Union[np.ndarray, List[float], List[np.ndarray], List[List[float]]]    ,		UpperCamelCase__	:				Union[bool, str, PaddingStrategy] = False    ,		UpperCamelCase__	:				Optional[int] = None    ,		UpperCamelCase__	:				bool = False    ,		UpperCamelCase__	:				Optional[int] = None    ,		UpperCamelCase__	:				Optional[bool] = None    ,		UpperCamelCase__	:				Optional[Union[str, TensorType]] = None    ,		UpperCamelCase__	:				Optional[int] = None    ,		**UpperCamelCase__	:				Optional[int]    ,		):





												'''simple docstring'''





												if sampling_rate is not None:
																		if sampling_rate != self.sampling_rate:
																								raise ValueError(
																								    f"""The model corresponding to this feature extractor: {self} was trained using a sampling rate of"""
																								    f""" {self.sampling_rate}. Please make sure that the provided `raw_speech` input was sampled with"""
																								    f""" {self.sampling_rate} and not {sampling_rate}."""						)
												else:
																		logger.warning(
																		    '''It is strongly recommended to pass the ``sampling_rate`` argument to this function. '''
																		    '''Failing to do so can result in silent errors that might be hard to debug.'''						)

												SCREAMING_SNAKE_CASE							:    Any							=							isinstance(UpperCamelCase__    ,		np.ndarray						) and len(raw_speech.shape						) > 1
												if is_batched_numpy and len(raw_speech.shape						) > 2:
																		raise ValueError(f"""Only mono-channel audio is supported for input to {self}"""						)
												SCREAMING_SNAKE_CASE							:    str							=							is_batched_numpy or (
												    isinstance(UpperCamelCase__    ,		(list, tuple)						) and (isinstance(raw_speech[0]    ,		(np.ndarray, tuple, list)						))
												)

												if is_batched:
																		SCREAMING_SNAKE_CASE							:    int							=							[np.asarray(UpperCamelCase__    ,		dtype=np.floataa						) for speech in raw_speech]
												elif not is_batched and not isinstance(UpperCamelCase__    ,		np.ndarray						):
																		SCREAMING_SNAKE_CASE							:    Dict							=							np.asarray(UpperCamelCase__    ,		dtype=np.floataa						)
												elif isinstance(UpperCamelCase__    ,		np.ndarray						) and raw_speech.dtype is np.dtype(np.floataa						):
																		SCREAMING_SNAKE_CASE							:    Optional[Any]							=							raw_speech.astype(np.floataa						)

												# always return batch
												if not is_batched:
																		SCREAMING_SNAKE_CASE							:    List[str]							=							[raw_speech]

												# extract fbank features
												SCREAMING_SNAKE_CASE							:    Optional[Any]							=							[self._extract_mfsc_features(UpperCamelCase__						) for one_waveform in raw_speech]

												# convert into correct format for padding
												SCREAMING_SNAKE_CASE							:    Tuple							=							BatchFeature({'''input_features''': features}						)

												SCREAMING_SNAKE_CASE							:    Optional[Any]							=							self.pad(
												    UpperCamelCase__    ,		padding=UpperCamelCase__    ,		max_length=UpperCamelCase__    ,		truncation=UpperCamelCase__    ,		pad_to_multiple_of=UpperCamelCase__    ,		return_attention_mask=UpperCamelCase__    ,		**UpperCamelCase__    ,		)
												# make sure list is in array format
												SCREAMING_SNAKE_CASE							:    Union[str, Any]							=							padded_inputs.get('''input_features'''						)
												if isinstance(input_features[0]    ,		UpperCamelCase__						):
																		SCREAMING_SNAKE_CASE							:    Any							=							[np.asarray(UpperCamelCase__    ,		dtype=np.floataa						) for feature in input_features]

												SCREAMING_SNAKE_CASE							:    Union[str, Any]							=							padded_inputs.get('''attention_mask'''						)
												if attention_mask is not None:
																		SCREAMING_SNAKE_CASE							:    Optional[int]							=							[np.asarray(UpperCamelCase__    ,		dtype=np.intaa						) for array in attention_mask]

												if self.normalize_means or self.normalize_vars:
																		SCREAMING_SNAKE_CASE							:    Optional[Any]							=							(
																		    np.array(UpperCamelCase__    ,		dtype=np.intaa						)
																		    if self._get_padding_strategies(UpperCamelCase__    ,		max_length=UpperCamelCase__						) is not PaddingStrategy.DO_NOT_PAD
																		    and padding
																		    else None
																		)
																		SCREAMING_SNAKE_CASE							:    List[Any]							=							self.normalize(
																		    padded_inputs['''input_features''']    ,		attention_mask=UpperCamelCase__						)

												if return_tensors is not None:
																		SCREAMING_SNAKE_CASE							:    Union[str, Any]							=							padded_inputs.convert_to_tensors(UpperCamelCase__						)

												return padded_inputs





