

"""simple docstring"""

from __future__ import annotations

import unittest

from transformers import DebertaConfig, 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 (
        TFDebertaForMaskedLM,
        TFDebertaForQuestionAnswering,
        TFDebertaForSequenceClassification,
        TFDebertaForTokenClassification,
        TFDebertaModel,
    )






class  __a				:





       """simple docstring"""


       def __init__(      self ,				snake_case ,				snake_case=13 ,				snake_case=7 ,				snake_case=True ,				snake_case=True ,				snake_case=True ,				snake_case=True ,				snake_case=99 ,				snake_case=32 ,				snake_case=2 ,				snake_case=4 ,				snake_case=37 ,				snake_case="gelu" ,				snake_case=0.1 ,				snake_case=0.1 ,				snake_case=512 ,				snake_case=16 ,				snake_case=2 ,				snake_case=0.02 ,				snake_case=3 ,				snake_case=4 ,				snake_case=None ,				):





        """simple docstring"""

        lowerCAmelCase__      :							int    =							parent
        lowerCAmelCase__      :							Optional[Any]    =							13
        lowerCAmelCase__      :							Optional[Any]    =							7
        lowerCAmelCase__      :							int    =							True
        lowerCAmelCase__      :							Any    =							True
        lowerCAmelCase__      :							str    =							True
        lowerCAmelCase__      :							Any    =							True
        lowerCAmelCase__      :							str    =							99
        lowerCAmelCase__      :							List[Any]    =							32
        lowerCAmelCase__      :							int    =							2
        lowerCAmelCase__      :							Optional[int]    =							4
        lowerCAmelCase__      :							Tuple    =							37
        lowerCAmelCase__      :							Any    =							"gelu"
        lowerCAmelCase__      :							int    =							0.1
        lowerCAmelCase__      :							List[str]    =							0.1
        lowerCAmelCase__      :							Tuple    =							512
        lowerCAmelCase__      :							Tuple    =							16
        lowerCAmelCase__      :							Optional[Any]    =							False
        lowerCAmelCase__      :							Any    =							-1
        lowerCAmelCase__      :							Union[str, Any]    =							True
        lowerCAmelCase__      :							str    =							2
        lowerCAmelCase__      :							Union[str, Any]    =							0.02
        lowerCAmelCase__      :							Any    =							3
        lowerCAmelCase__      :							Any    =							4
        lowerCAmelCase__      :							str    =							None






       def  SCREAMING_SNAKE_CASE_  (      self      ):





        """simple docstring"""

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

        lowerCAmelCase__      :							int    =							None
        if self.use_input_mask:
         lowerCAmelCase__      :							Union[str, Any]    =							random_attention_mask([self.batch_size, self.seq_length]      )

        lowerCAmelCase__      :							int    =							None
        if self.use_token_type_ids:
         lowerCAmelCase__      :							List[Any]    =							ids_tensor([self.batch_size, self.seq_length] ,				self.type_vocab_size      )

        lowerCAmelCase__      :							Any    =							None
        lowerCAmelCase__      :							List[Any]    =							None
        lowerCAmelCase__      :							List[Any]    =							None
        if self.use_labels:
         lowerCAmelCase__      :							Tuple    =							ids_tensor([self.batch_size] ,				self.type_sequence_label_size      )
         lowerCAmelCase__      :							Dict    =							ids_tensor([self.batch_size, self.seq_length] ,				self.num_labels      )

        lowerCAmelCase__      :							Dict    =							DebertaConfig(
            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 ,				max_relative_positions=self.max_relative_positions ,				position_biased_input=self.position_biased_input ,				initializer_range=self.initializer_range ,				return_dict=snake_case ,				)

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






       def  SCREAMING_SNAKE_CASE_  (      self ,				snake_case ,				snake_case ,				snake_case ,				snake_case ,				snake_case ,				snake_case ,				snake_case      ):





        """simple docstring"""

        lowerCAmelCase__      :							Any    =							TFDebertaModel(config=snake_case      )
        lowerCAmelCase__      :							Optional[int]    =							{"input_ids": input_ids, "attention_mask": input_mask, "token_type_ids": token_type_ids}

        lowerCAmelCase__      :							str    =							[input_ids, input_mask]
        lowerCAmelCase__      :							str    =							model(snake_case      )

        lowerCAmelCase__      :							Tuple    =							model(snake_case      )

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






       def  SCREAMING_SNAKE_CASE_  (      self ,				snake_case ,				snake_case ,				snake_case ,				snake_case ,				snake_case ,				snake_case ,				snake_case      ):





        """simple docstring"""

        lowerCAmelCase__      :							Tuple    =							TFDebertaForMaskedLM(config=snake_case      )
        lowerCAmelCase__      :							List[Any]    =							{
            "input_ids": input_ids,
            "attention_mask": input_mask,
            "token_type_ids": token_type_ids,
        }
        lowerCAmelCase__      :							Optional[int]    =							model(snake_case      )
        self.parent.assertEqual(result.logits.shape ,				(self.batch_size, self.seq_length, self.vocab_size)      )






       def  SCREAMING_SNAKE_CASE_  (      self ,				snake_case ,				snake_case ,				snake_case ,				snake_case ,				snake_case ,				snake_case ,				snake_case      ):





        """simple docstring"""

        lowerCAmelCase__      :							Tuple    =							self.num_labels
        lowerCAmelCase__      :							Optional[int]    =							TFDebertaForSequenceClassification(config=snake_case      )
        lowerCAmelCase__      :							Dict    =							{
            "input_ids": input_ids,
            "attention_mask": input_mask,
            "token_type_ids": token_type_ids,
        }

        lowerCAmelCase__      :							Any    =							model(snake_case      )
        self.parent.assertEqual(result.logits.shape ,				(self.batch_size, self.num_labels)      )






       def  SCREAMING_SNAKE_CASE_  (      self ,				snake_case ,				snake_case ,				snake_case ,				snake_case ,				snake_case ,				snake_case ,				snake_case      ):





        """simple docstring"""

        lowerCAmelCase__      :							Dict    =							self.num_labels
        lowerCAmelCase__      :							int    =							TFDebertaForTokenClassification(config=snake_case      )
        lowerCAmelCase__      :							Optional[Any]    =							{
            "input_ids": input_ids,
            "attention_mask": input_mask,
            "token_type_ids": token_type_ids,
        }
        lowerCAmelCase__      :							Optional[Any]    =							model(snake_case      )
        self.parent.assertEqual(result.logits.shape ,				(self.batch_size, self.seq_length, self.num_labels)      )






       def  SCREAMING_SNAKE_CASE_  (      self ,				snake_case ,				snake_case ,				snake_case ,				snake_case ,				snake_case ,				snake_case ,				snake_case      ):





        """simple docstring"""

        lowerCAmelCase__      :							str    =							TFDebertaForQuestionAnswering(config=snake_case      )
        lowerCAmelCase__      :							str    =							{
            "input_ids": input_ids,
            "attention_mask": input_mask,
            "token_type_ids": token_type_ids,
        }

        lowerCAmelCase__      :							List[str]    =							model(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  SCREAMING_SNAKE_CASE_  (      self      ):





        """simple docstring"""

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






@require_tf
class  __a				(	__magic_name__					,  __magic_name__					,  unittest.TestCase					):





       """simple docstring"""


       __UpperCamelCase				:			Union[str, Any]          =	(
           (
               TFDebertaModel,
               TFDebertaForMaskedLM,
               TFDebertaForQuestionAnswering,
               TFDebertaForSequenceClassification,
               TFDebertaForTokenClassification,
           )
           if is_tf_available()
           else ()
       )
       __UpperCamelCase				:			Union[str, Any]          =	(
           {
               'feature-extraction': TFDebertaModel,
               'fill-mask': TFDebertaForMaskedLM,
               'question-answering': TFDebertaForQuestionAnswering,
               'text-classification': TFDebertaForSequenceClassification,
               'token-classification': TFDebertaForTokenClassification,
               'zero-shot': TFDebertaForSequenceClassification,
           }
           if is_tf_available()
           else {}
       )

       __UpperCamelCase				:			int          =	False
       __UpperCamelCase				:			Optional[Any]          =	False






       def  SCREAMING_SNAKE_CASE_  (      self      ):





        """simple docstring"""

        lowerCAmelCase__      :							int    =							TFDebertaModelTester(self      )
        lowerCAmelCase__      :							Optional[int]    =							ConfigTester(self ,				config_class=snake_case ,				hidden_size=37      )






       def  SCREAMING_SNAKE_CASE_  (      self      ):





        """simple docstring"""

        self.config_tester.run_common_tests()






       def  SCREAMING_SNAKE_CASE_  (      self      ):





        """simple docstring"""

        lowerCAmelCase__      :							List[Any]    =							self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_model(*snake_case      )






       def  SCREAMING_SNAKE_CASE_  (      self      ):





        """simple docstring"""

        lowerCAmelCase__      :							int    =							self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_masked_lm(*snake_case      )






       def  SCREAMING_SNAKE_CASE_  (      self      ):





        """simple docstring"""

        lowerCAmelCase__      :							Union[str, Any]    =							self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_question_answering(*snake_case      )






       def  SCREAMING_SNAKE_CASE_  (      self      ):





        """simple docstring"""

        lowerCAmelCase__      :							Union[str, Any]    =							self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_sequence_classification(*snake_case      )






       def  SCREAMING_SNAKE_CASE_  (      self      ):





        """simple docstring"""

        lowerCAmelCase__      :							Union[str, Any]    =							self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_token_classification(*snake_case      )






       @slow
       def  SCREAMING_SNAKE_CASE_  (      self      ):





        """simple docstring"""

        lowerCAmelCase__      :							str    =							TFDebertaModel.from_pretrained("kamalkraj/deberta-base"      )
        self.assertIsNotNone(snake_case      )






@require_tf
class  __a				(	unittest.TestCase					):





       """simple docstring"""


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





        """simple docstring"""

        pass






       @slow
       def  SCREAMING_SNAKE_CASE_  (      self      ):





        """simple docstring"""

        lowerCAmelCase__      :							Optional[Any]    =							TFDebertaModel.from_pretrained("kamalkraj/deberta-base"      )
        lowerCAmelCase__      :							List[str]    =							tf.constant([[0, 31_414, 232, 328, 740, 1_140, 12_695, 69, 46_078, 1_588, 2]]      )
        lowerCAmelCase__      :							str    =							tf.constant([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]      )
        lowerCAmelCase__      :							Tuple    =							model(snake_case ,				attention_mask=snake_case      )[0]

        lowerCAmelCase__      :							List[Any]    =							tf.constant(
            [
                [
                    [-0.59_855_896, -0.80_552_566, -0.8_462_135],
                    [1.4_484_025, -0.93_483_794, -0.80_593_085],
                    [0.3_122_741, 0.00_316_059, -1.4_131_377],
                ]
            ]      )
        tf.debugging.assert_near(output[:, 1:4, 1:4] ,				snake_case ,				atol=1e-4      )









"""simple docstring"""

from typing import List, Optional, Union

import numpy as np

from ...feature_extraction_sequence_utils import SequenceFeatureExtractor
from ...feature_extraction_utils import BatchFeature
from ...utils import PaddingStrategy, TensorType, logging


_UpperCamelCase		    =						logging.get_logger(__name__)






class  __a				(	__magic_name__					):





       """simple docstring"""


       __UpperCamelCase				:			Tuple          =	['input_values', 'attention_mask']






       def __init__(      self ,				snake_case=1 ,				snake_case=16_000 ,				snake_case=0.0 ,				snake_case=False ,				snake_case=True ,				**snake_case ,				):





        """simple docstring"""

        super().__init__(feature_size=snake_case ,				sampling_rate=snake_case ,				padding_value=snake_case ,				**snake_case      )
        lowerCAmelCase__      :							Dict    =							return_attention_mask
        lowerCAmelCase__      :							List[str]    =							do_normalize






       @staticmethod
       def  SCREAMING_SNAKE_CASE_  (      snake_case ,				snake_case ,				snake_case = 0.0      ):





        """simple docstring"""

        if attention_mask is not None:
         lowerCAmelCase__      :							List[str]    =							np.array(snake_case ,				np.intaa      )
         lowerCAmelCase__      :							str    =							[]

         for vector, length in zip(snake_case ,				attention_mask.sum(-1      )      ):
          lowerCAmelCase__      :							Any    =							(vector - vector[:length].mean()) / np.sqrt(vector[:length].var() + 1e-7      )
          if length < normed_slice.shape[0]:
           lowerCAmelCase__      :							List[Any]    =							padding_value

          normed_input_values.append(snake_case      )
        else:
         lowerCAmelCase__      :							List[Any]    =							[(x - x.mean()) / np.sqrt(x.var() + 1e-7      ) for x in input_values]

        return normed_input_values






       def __call__(      self ,				snake_case ,				snake_case = False ,				snake_case = None ,				snake_case = False ,				snake_case = None ,				snake_case = None ,				snake_case = None ,				snake_case = None ,				**snake_case ,				):





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

        lowerCAmelCase__      :							List[str]    =							isinstance(snake_case ,				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}"""      )
        lowerCAmelCase__      :							Tuple    =							is_batched_numpy or (
            isinstance(snake_case ,				(list, tuple)      ) and (isinstance(raw_speech[0] ,				(np.ndarray, tuple, list)      ))
        )

        # always return batch
        if not is_batched:
         lowerCAmelCase__      :							Tuple    =							[raw_speech]

        # convert into correct format for padding
        lowerCAmelCase__      :							Tuple    =							BatchFeature({"input_values": raw_speech}      )

        lowerCAmelCase__      :							Any    =							self.pad(
            snake_case ,				padding=snake_case ,				max_length=snake_case ,				truncation=snake_case ,				pad_to_multiple_of=snake_case ,				return_attention_mask=snake_case ,				)

        # convert input values to correct format
        lowerCAmelCase__      :							List[Any]    =							padded_inputs["input_values"]
        if not isinstance(input_values[0] ,				np.ndarray      ):
         lowerCAmelCase__      :							Any    =							[np.asarray(snake_case ,				dtype=np.floataa      ) for array in input_values]
        elif (
            not isinstance(snake_case ,				np.ndarray      )
            and isinstance(input_values[0] ,				np.ndarray      )
            and input_values[0].dtype is np.dtype(np.floataa      )
        ):
         lowerCAmelCase__      :							str    =							[array.astype(np.floataa      ) for array in input_values]
        elif isinstance(snake_case ,				np.ndarray      ) and input_values.dtype is np.dtype(np.floataa      ):
         lowerCAmelCase__      :							List[Any]    =							input_values.astype(np.floataa      )

        # convert attention_mask to correct format
        lowerCAmelCase__      :							int    =							padded_inputs.get("attention_mask"      )
        if attention_mask is not None:
         lowerCAmelCase__      :							Tuple    =							[np.asarray(snake_case ,				dtype=np.intaa      ) for array in attention_mask]

        # zero-mean and unit-variance normalization
        if self.do_normalize:
         lowerCAmelCase__      :							List[Any]    =							(
             attention_mask
             if self._get_padding_strategies(snake_case ,				max_length=snake_case      ) is not PaddingStrategy.DO_NOT_PAD
             else None
         )
         lowerCAmelCase__      :							Union[str, Any]    =							self.zero_mean_unit_var_norm(
             padded_inputs["input_values"] ,				attention_mask=snake_case ,				padding_value=self.padding_value      )

        if return_tensors is not None:
         lowerCAmelCase__      :							Optional[Any]    =							padded_inputs.convert_to_tensors(snake_case      )

        return padded_inputs






