






"""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 						snake_case  :





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


      '''simple docstring'''




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





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


      '''simple docstring'''




      a					:      int    =       ids_tensor([self.batch_size, self.seq_length]    ,		self.vocab_size      )

      a					:      Tuple    =       None
      if self.use_input_mask:
        a					:      str    =       random_attention_mask([self.batch_size, self.seq_length]      )

      a					:      str    =       None
      if self.use_token_type_ids:
        a					:      str    =       ids_tensor([self.batch_size, self.seq_length]    ,		self.type_vocab_size      )

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

      a					:      str    =       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=A    ,		)

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





    def     lowerCamelCase__				(							self     : Optional[Any]    ,		A     : Any    ,		A     : str    ,		A     : Union[str, Any]    ,		A     : Dict    ,		A     : Optional[Any]    ,		A     : str    ,		A     : int      ):


      '''simple docstring'''




      a					:      Dict    =       TFDebertaVaModel(config=A      )
      a					:      Optional[int]    =       {'input_ids': input_ids, 'attention_mask': input_mask, 'token_type_ids': token_type_ids}

      a					:      int    =       [input_ids, input_mask]
      a					:      Union[str, Any]    =       model(A      )

      a					:      Any    =       model(A      )

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





    def     lowerCamelCase__				(							self     : List[Any]    ,		A     : Optional[int]    ,		A     : Optional[Any]    ,		A     : str    ,		A     : List[str]    ,		A     : str    ,		A     : str    ,		A     : Tuple      ):


      '''simple docstring'''




      a					:      Any    =       TFDebertaVaForMaskedLM(config=A      )
      a					:      Any    =       {
          'input_ids': input_ids,
          'attention_mask': input_mask,
          'token_type_ids': token_type_ids,
      }
      a					:      str    =       model(A      )
      self.parent.assertEqual(result.logits.shape    ,		(self.batch_size, self.seq_length, self.vocab_size)      )





    def     lowerCamelCase__				(							self     : Any    ,		A     : int    ,		A     : Optional[Any]    ,		A     : Tuple    ,		A     : Optional[int]    ,		A     : Union[str, Any]    ,		A     : Tuple    ,		A     : int      ):


      '''simple docstring'''




      a					:      str    =       self.num_labels
      a					:      List[Any]    =       TFDebertaVaForSequenceClassification(config=A      )
      a					:      List[Any]    =       {
          'input_ids': input_ids,
          'attention_mask': input_mask,
          'token_type_ids': token_type_ids,
      }

      a					:      str    =       model(A      )
      self.parent.assertEqual(result.logits.shape    ,		(self.batch_size, self.num_labels)      )





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


      '''simple docstring'''




      a					:      List[Any]    =       self.num_labels
      a					:      List[str]    =       TFDebertaVaForTokenClassification(config=A      )
      a					:      Optional[Any]    =       {
          'input_ids': input_ids,
          'attention_mask': input_mask,
          'token_type_ids': token_type_ids,
      }
      a					:      Optional[Any]    =       model(A      )
      self.parent.assertEqual(result.logits.shape    ,		(self.batch_size, self.seq_length, self.num_labels)      )





    def     lowerCamelCase__				(							self     : Any    ,		A     : Dict    ,		A     : str    ,		A     : Tuple    ,		A     : int    ,		A     : Optional[int]    ,		A     : Any    ,		A     : List[str]      ):


      '''simple docstring'''




      a					:      str    =       TFDebertaVaForQuestionAnswering(config=A      )
      a					:      str    =       {
          'input_ids': input_ids,
          'attention_mask': input_mask,
          'token_type_ids': token_type_ids,
      }

      a					:      str    =       model(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     lowerCamelCase__				(							self     : List[Any]      ):


      '''simple docstring'''




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






@require_tf
class 						snake_case  (  UpperCAmelCase       ,			UpperCAmelCase       ,			unittest.TestCase	):
    __magic_name__   					=					(
        (
            TFDebertaVaModel,
            TFDebertaVaForMaskedLM,
            TFDebertaVaForQuestionAnswering,
            TFDebertaVaForSequenceClassification,
            TFDebertaVaForTokenClassification,
        )
        if is_tf_available()
        else ()
    )
    __magic_name__   					=					(
        {
            '''feature-extraction''': TFDebertaVaModel,
            '''fill-mask''': TFDebertaVaForMaskedLM,
            '''question-answering''': TFDebertaVaForQuestionAnswering,
            '''text-classification''': TFDebertaVaForSequenceClassification,
            '''token-classification''': TFDebertaVaForTokenClassification,
            '''zero-shot''': TFDebertaVaForSequenceClassification,
        }
        if is_tf_available()
        else {}
    )

    __magic_name__   					=					False
    __magic_name__   					=					False





    def     lowerCamelCase__				(							self     : Dict      ):


      '''simple docstring'''




      a					:      Tuple    =       TFDebertaVaModelTester(self      )
      a					:      Union[str, Any]    =       ConfigTester(self    ,		config_class=A    ,		hidden_size=3_7      )





    def     lowerCamelCase__				(							self     : int      ):


      '''simple docstring'''




      self.config_tester.run_common_tests()





    def     lowerCamelCase__				(							self     : Tuple      ):


      '''simple docstring'''




      a					:      Union[str, Any]    =       self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_model(*A      )





    def     lowerCamelCase__				(							self     : str      ):


      '''simple docstring'''




      a					:      Union[str, Any]    =       self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_for_masked_lm(*A      )





    def     lowerCamelCase__				(							self     : Any      ):


      '''simple docstring'''




      a					:      Any    =       self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_for_question_answering(*A      )





    def     lowerCamelCase__				(							self     : Any      ):


      '''simple docstring'''




      a					:      Tuple    =       self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_for_sequence_classification(*A      )





    def     lowerCamelCase__				(							self     : Optional[int]      ):


      '''simple docstring'''




      a					:      Dict    =       self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_for_token_classification(*A      )





    @slow
    def     lowerCamelCase__				(							self     : Dict      ):


      '''simple docstring'''




      a					:      Any    =       TFDebertaVaModel.from_pretrained('kamalkraj/deberta-v2-xlarge'      )
      self.assertIsNotNone(A      )






@require_tf
class 						snake_case  (  unittest.TestCase	):





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


      '''simple docstring'''




      pass





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


      '''simple docstring'''




      a					:      Any    =       TFDebertaVaModel.from_pretrained('kamalkraj/deberta-v2-xlarge'      )
      a					:      Union[str, Any]    =       tf.constant([[0, 3_1_4_1_4, 2_3_2, 3_2_8, 7_4_0, 1_1_4_0, 1_2_6_9_5, 6_9, 4_6_0_7_8, 1_5_8_8, 2]]      )
      a					:      List[Any]    =       tf.constant([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]      )
      a					:      Union[str, Any]    =       model(A    ,		attention_mask=A      )[0]

      a					:      List[Any]    =       tf.constant(
          [[[0.23_56, 0.19_48, 0.03_69], [-0.10_63, 0.35_86, -0.51_52], [-0.63_99, -0.02_59, -0.25_25]]]      )
      tf.debugging.assert_near(output[:, 1:4, 1:4]    ,		A    ,		atol=1E-4      )












"""simple docstring"""


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    :   int   			=						logging.get_logger(__name__)






class 						snake_case  (  UpperCAmelCase	):
    __magic_name__   					=					['''input_features''', '''attention_mask''']





    def __init__(							self     : Optional[int]    ,		A     : Optional[Any]=8_0    ,		A     : str=1_6_0_0_0    ,		A     : List[str]=0.0    ,		A     : Any=1_0    ,		A     : Union[str, Any]=2_5    ,		A     : str="hamming_window"    ,		A     : str=3_27_68.0    ,		A     : Union[str, Any]=0.97    ,		A     : Dict=1.0    ,		A     : Any=True    ,		A     : Union[str, Any]=True    ,		A     : List[Any]=False    ,		**A     : Tuple    ,		):


      '''simple docstring'''




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

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

      a					:      List[Any]    =       win_length * sampling_rate // 1_0_0_0
      a					:      Tuple    =       hop_length * sampling_rate // 1_0_0_0

      a					:      List[Any]    =       optimal_fft_length(self.sample_size      )
      a					:      Any    =       (self.n_fft // 2) + 1





    def     lowerCamelCase__				(							self     : List[Any]    ,		A     : np.array      ):


      '''simple docstring'''




      if self.win_function == "hamming_window":
        a					:      List[str]    =       window_function(window_length=self.sample_size    ,		name=self.win_function    ,		periodic=A      )
      else:
        a					:      Dict    =       window_function(window_length=self.sample_size    ,		name=self.win_function      )

      a					:      str    =       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    ,		)

      a					:      List[Any]    =       spectrogram(
          one_waveform * self.frame_signal_scale    ,		window=A    ,		frame_length=self.sample_size    ,		hop_length=self.sample_stride    ,		fft_length=self.n_fft    ,		center=A    ,		preemphasis=self.preemphasis_coeff    ,		mel_filters=A    ,		mel_floor=self.mel_floor    ,		log_mel='log'    ,		)
      return msfc_features.T





    def     lowerCamelCase__				(							self     : int    ,		A     : Tuple    ,		A     : int    ,		A     : Optional[int]      ):


      '''simple docstring'''




      if self.normalize_means:
        a					:      Any    =       x[:input_length].mean(axis=0      )
        a					:      Dict    =       np.subtract(A    ,		A      )
      if self.normalize_vars:
        a					:      Dict    =       x[:input_length].std(axis=0      )
        a					:      Dict    =       np.divide(A    ,		A      )

      if input_length < x.shape[0]:
        a					:      Dict    =       padding_value

      # make sure array is in float32
      a					:      Optional[int]    =       x.astype(np.floataa      )

      return x





    def     lowerCamelCase__				(							self     : str    ,		A     : List[np.ndarray]    ,		A     : Optional[np.ndarray] = None      ):


      '''simple docstring'''




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





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


      '''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.'      )

      a					:      Optional[int]    =       isinstance(A    ,		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}'''      )
      a					:      Dict    =       is_batched_numpy or (
          isinstance(A    ,		(list, tuple)      ) and (isinstance(raw_speech[0]    ,		(np.ndarray, tuple, list)      ))
      )

      if is_batched:
        a					:      str    =       [np.asarray(A    ,		dtype=np.floataa      ) for speech in raw_speech]
      elif not is_batched and not isinstance(A    ,		np.ndarray      ):
        a					:      List[str]    =       np.asarray(A    ,		dtype=np.floataa      )
      elif isinstance(A    ,		np.ndarray      ) and raw_speech.dtype is np.dtype(np.floataa      ):
        a					:      Optional[int]    =       raw_speech.astype(np.floataa      )

      # always return batch
      if not is_batched:
        a					:      Any    =       [raw_speech]

      # extract fbank features
      a					:      str    =       [self._extract_mfsc_features(A      ) for one_waveform in raw_speech]

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

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

      a					:      List[Any]    =       padded_inputs.get('attention_mask'      )
      if attention_mask is not None:
        a					:      int    =       [np.asarray(A    ,		dtype=np.intaa      ) for array in attention_mask]

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

      if return_tensors is not None:
        a					:      Optional[int]    =       padded_inputs.convert_to_tensors(A      )

      return padded_inputs




