




"""simple docstring"""


import unittest

from transformers import DebertaConfig, is_torch_available
from transformers.testing_utils import require_sentencepiece, require_tokenizers, require_torch, slow, torch_device

from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
 import torch

 from transformers import (
     DebertaForMaskedLM,
     DebertaForQuestionAnswering,
     DebertaForSequenceClassification,
     DebertaForTokenClassification,
     DebertaModel,
 )
 from transformers.models.deberta.modeling_deberta import DEBERTA_PRETRAINED_MODEL_ARCHIVE_LIST


class _lowerCamelCase    (					lowerCAmelCase__						):






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

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






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

         """simple docstring"""
         lowerCAmelCase__      :			List[str]   =	ids_tensor([self.batch_size, self.seq_length]   ,     self.vocab_size   )

         lowerCAmelCase__      :			str   =	None
         if self.use_input_mask:
               lowerCAmelCase__      :			Dict   =	ids_tensor([self.batch_size, self.seq_length]   ,     vocab_size=2   )

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

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

         lowerCAmelCase__      :			int   =	self.get_config()

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






   def 	_lowerCAmelCase   (		self       :		str   )			->						Tuple:

         """simple docstring"""
         return 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   ,     initializer_range=self.initializer_range   ,     relative_attention=self.relative_attention   ,     position_biased_input=self.position_biased_input   ,     pos_att_type=self.pos_att_type   ,     )






   def 	_lowerCAmelCase   (		self       :		Dict   )			->						int:

         """simple docstring"""
         lowerCAmelCase__      :			Optional[int]   =	self.get_config()
         lowerCAmelCase__      :			Any   =	3_00
         return config






   def 	_lowerCAmelCase   (		self       :		str   ,     UpperCamelCase       :		Any   )			->						List[str]:

         """simple docstring"""
         self.parent.assertListEqual(list(result.loss.size()   )   ,     []   )






   def 	_lowerCAmelCase   (		self       :		Any   ,     UpperCamelCase       :		List[str]   ,     UpperCamelCase       :		str   ,     UpperCamelCase       :		Union[str, Any]   ,     UpperCamelCase       :		Optional[int]   ,     UpperCamelCase       :		int   ,     UpperCamelCase       :		str   ,     UpperCamelCase       :		Union[str, Any]   )			->						Union[str, Any]:

         """simple docstring"""
         lowerCAmelCase__      :			Tuple   =	DebertaModel(config=_SCREAMING_SNAKE_CASE   )
         model.to(_SCREAMING_SNAKE_CASE   )
         model.eval()
         lowerCAmelCase__      :			Dict   =	model(_SCREAMING_SNAKE_CASE   ,     attention_mask=_SCREAMING_SNAKE_CASE   ,     token_type_ids=_SCREAMING_SNAKE_CASE   )[0]
         lowerCAmelCase__      :			Optional[Any]   =	model(_SCREAMING_SNAKE_CASE   ,     token_type_ids=_SCREAMING_SNAKE_CASE   )[0]
         lowerCAmelCase__      :			Optional[Any]   =	model(_SCREAMING_SNAKE_CASE   )[0]

         self.parent.assertListEqual(list(sequence_output.size()   )   ,     [self.batch_size, self.seq_length, self.hidden_size]   )






   def 	_lowerCAmelCase   (		self       :		Tuple   ,     UpperCamelCase       :		Optional[Any]   ,     UpperCamelCase       :		List[str]   ,     UpperCamelCase       :		List[str]   ,     UpperCamelCase       :		List[str]   ,     UpperCamelCase       :		Dict   ,     UpperCamelCase       :		Tuple   ,     UpperCamelCase       :		Dict   )			->						Optional[int]:

         """simple docstring"""
         lowerCAmelCase__      :			Optional[int]   =	DebertaForMaskedLM(config=_SCREAMING_SNAKE_CASE   )
         model.to(_SCREAMING_SNAKE_CASE   )
         model.eval()
         lowerCAmelCase__      :			Union[str, Any]   =	model(_SCREAMING_SNAKE_CASE   ,     attention_mask=_SCREAMING_SNAKE_CASE   ,     token_type_ids=_SCREAMING_SNAKE_CASE   ,     labels=_SCREAMING_SNAKE_CASE   )

         self.parent.assertEqual(result.logits.shape   ,     (self.batch_size, self.seq_length, self.vocab_size)   )






   def 	_lowerCAmelCase   (		self       :		Tuple   ,     UpperCamelCase       :		Tuple   ,     UpperCamelCase       :		Dict   ,     UpperCamelCase       :		List[Any]   ,     UpperCamelCase       :		Tuple   ,     UpperCamelCase       :		Optional[Any]   ,     UpperCamelCase       :		Optional[Any]   ,     UpperCamelCase       :		Any   )			->						Optional[int]:

         """simple docstring"""
         lowerCAmelCase__      :			List[str]   =	self.num_labels
         lowerCAmelCase__      :			Union[str, Any]   =	DebertaForSequenceClassification(_SCREAMING_SNAKE_CASE   )
         model.to(_SCREAMING_SNAKE_CASE   )
         model.eval()
         lowerCAmelCase__      :			str   =	model(_SCREAMING_SNAKE_CASE   ,     attention_mask=_SCREAMING_SNAKE_CASE   ,     token_type_ids=_SCREAMING_SNAKE_CASE   ,     labels=_SCREAMING_SNAKE_CASE   )
         self.parent.assertListEqual(list(result.logits.size()   )   ,     [self.batch_size, self.num_labels]   )
         self.check_loss_output(_SCREAMING_SNAKE_CASE   )






   def 	_lowerCAmelCase   (		self       :		Any   ,     UpperCamelCase       :		Dict   ,     UpperCamelCase       :		Optional[Any]   ,     UpperCamelCase       :		List[Any]   ,     UpperCamelCase       :		List[Any]   ,     UpperCamelCase       :		Tuple   ,     UpperCamelCase       :		List[str]   ,     UpperCamelCase       :		str   )			->						List[Any]:

         """simple docstring"""
         lowerCAmelCase__      :			Union[str, Any]   =	self.num_labels
         lowerCAmelCase__      :			Optional[Any]   =	DebertaForTokenClassification(config=_SCREAMING_SNAKE_CASE   )
         model.to(_SCREAMING_SNAKE_CASE   )
         model.eval()
         lowerCAmelCase__      :			Optional[int]   =	model(_SCREAMING_SNAKE_CASE   ,     attention_mask=_SCREAMING_SNAKE_CASE   ,     token_type_ids=_SCREAMING_SNAKE_CASE   ,     labels=_SCREAMING_SNAKE_CASE   )
         self.parent.assertEqual(result.logits.shape   ,     (self.batch_size, self.seq_length, self.num_labels)   )






   def 	_lowerCAmelCase   (		self       :		List[str]   ,     UpperCamelCase       :		Any   ,     UpperCamelCase       :		int   ,     UpperCamelCase       :		str   ,     UpperCamelCase       :		Optional[Any]   ,     UpperCamelCase       :		Tuple   ,     UpperCamelCase       :		Optional[int]   ,     UpperCamelCase       :		Dict   )			->						str:

         """simple docstring"""
         lowerCAmelCase__      :			Dict   =	DebertaForQuestionAnswering(config=_SCREAMING_SNAKE_CASE   )
         model.to(_SCREAMING_SNAKE_CASE   )
         model.eval()
         lowerCAmelCase__      :			str   =	model(
             _SCREAMING_SNAKE_CASE   ,     attention_mask=_SCREAMING_SNAKE_CASE   ,     token_type_ids=_SCREAMING_SNAKE_CASE   ,     start_positions=_SCREAMING_SNAKE_CASE   ,     end_positions=_SCREAMING_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 	_lowerCAmelCase   (		self       :		int   )			->						Dict:

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






@require_torch
class _lowerCamelCase    (					lowerCAmelCase__       ,	lowerCAmelCase__       ,	unittest.TestCase						):
   _lowerCamelCase    :Optional[Any]     				=     (
       (
           DebertaModel,
           DebertaForMaskedLM,
           DebertaForSequenceClassification,
           DebertaForTokenClassification,
           DebertaForQuestionAnswering,
       )
       if is_torch_available()
       else ()
   )
   _lowerCamelCase    :Tuple     				=     (
       {
           "feature-extraction": DebertaModel,
           "fill-mask": DebertaForMaskedLM,
           "question-answering": DebertaForQuestionAnswering,
           "text-classification": DebertaForSequenceClassification,
           "token-classification": DebertaForTokenClassification,
           "zero-shot": DebertaForSequenceClassification,
       }
       if is_torch_available()
       else {}
   )

   _lowerCamelCase    :List[str]     				=     True
   _lowerCamelCase    :int     				=     False
   _lowerCamelCase    :Union[str, Any]     				=     False
   _lowerCamelCase    :Any     				=     False
   _lowerCamelCase    :Optional[int]     				=     False






   def 	_lowerCAmelCase   (		self       :		Any   )			->						int:

         """simple docstring"""
         lowerCAmelCase__      :			List[Any]   =	DebertaModelTester(self   )
         lowerCAmelCase__      :			Tuple   =	ConfigTester(self   ,     config_class=_SCREAMING_SNAKE_CASE   ,     hidden_size=37   )






   def 	_lowerCAmelCase   (		self       :		List[str]   )			->						str:

         """simple docstring"""
         self.config_tester.run_common_tests()






   def 	_lowerCAmelCase   (		self       :		Optional[Any]   )			->						Any:

         """simple docstring"""
         lowerCAmelCase__      :			List[str]   =	self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_deberta_model(*_SCREAMING_SNAKE_CASE   )






   def 	_lowerCAmelCase   (		self       :		List[str]   )			->						int:

         """simple docstring"""
         lowerCAmelCase__      :			Tuple   =	self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_deberta_for_sequence_classification(*_SCREAMING_SNAKE_CASE   )






   def 	_lowerCAmelCase   (		self       :		str   )			->						Optional[int]:

         """simple docstring"""
         lowerCAmelCase__      :			List[str]   =	self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_deberta_for_masked_lm(*_SCREAMING_SNAKE_CASE   )






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

         """simple docstring"""
         lowerCAmelCase__      :			List[Any]   =	self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_deberta_for_question_answering(*_SCREAMING_SNAKE_CASE   )






   def 	_lowerCAmelCase   (		self       :		Optional[int]   )			->						Dict:

         """simple docstring"""
         lowerCAmelCase__      :			str   =	self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_deberta_for_token_classification(*_SCREAMING_SNAKE_CASE   )






   @slow
   def 	_lowerCAmelCase   (		self       :		List[Any]   )			->						Union[str, Any]:

         """simple docstring"""
         for model_name in DEBERTA_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
               lowerCAmelCase__      :			Union[str, Any]   =	DebertaModel.from_pretrained(_SCREAMING_SNAKE_CASE   )
               self.assertIsNotNone(_SCREAMING_SNAKE_CASE   )






@require_torch
@require_sentencepiece
@require_tokenizers
class _lowerCamelCase    (					unittest.TestCase						):






   @unittest.skip(reason="""Model not available yet"""   )
   def 	_lowerCAmelCase   (		self       :		Optional[int]   )			->						Dict:

         """simple docstring"""
         pass






   @slow
   def 	_lowerCAmelCase   (		self       :		Optional[int]   )			->						Optional[int]:

         """simple docstring"""
         lowerCAmelCase__      :			Tuple   =	DebertaModel.from_pretrained("""microsoft/deberta-base"""   )

         lowerCAmelCase__      :			str   =	torch.tensor([[0, 3_14_14, 2_32, 3_28, 7_40, 11_40, 1_26_95, 69, 4_60_78, 15_88, 2]]   )
         lowerCAmelCase__      :			int   =	torch.tensor([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]   )
         with torch.no_grad():
               lowerCAmelCase__      :			str   =	model(_SCREAMING_SNAKE_CASE   ,     attention_mask=_SCREAMING_SNAKE_CASE   )[0]
         # compare the actual values for a slice.
         lowerCAmelCase__      :			Optional[Any]   =	torch.tensor(
             [[[-0.5986, -0.8055, -0.8462], [1.4484, -0.9348, -0.8059], [0.3123, 0.0032, -1.4131]]]   )
         self.assertTrue(torch.allclose(output[:, 1:4, 1:4]   ,     _SCREAMING_SNAKE_CASE   ,     atol=1E-4   )   ,     f"""{output[:, 1:4, 1:4]}"""   )











"""simple docstring"""


import copy
from dataclasses import dataclass, field
from typing import ClassVar, Dict

from ..features import Audio, ClassLabel, Features
from .base import TaskTemplate


@dataclass(frozen=a_						)
class _lowerCamelCase    (					a_						):
   _lowerCamelCase    :str     				=     field(default="audio-classification"       ,	metadata={"include_in_asdict_even_if_is_default": True}						)
   _lowerCamelCase    :ClassVar[Features]     				=     Features({"audio": Audio()}						)
   _lowerCamelCase    :ClassVar[Features]     				=     Features({"labels": ClassLabel}						)
   _lowerCamelCase    :str     				=     "audio"
   _lowerCamelCase    :str     				=     "labels"






   def 	_lowerCAmelCase   (		self       :		str   ,     UpperCamelCase       :		Optional[int]   )			->						Union[str, Any]:

         """simple docstring"""
         if self.label_column not in features:
               raise ValueError(f"""Column {self.label_column} is not present in features."""   )
         if not isinstance(features[self.label_column]   ,     UpperCamelCase   ):
               raise ValueError(f"""Column {self.label_column} is not a ClassLabel."""   )
         lowerCAmelCase__      :			str   =	copy.deepcopy(self   )
         lowerCAmelCase__      :			Optional[int]   =	self.label_schema.copy()
         lowerCAmelCase__      :			List[Any]   =	features[self.label_column]
         lowerCAmelCase__      :			Optional[int]   =	label_schema
         return task_template






   @property
   def 	_lowerCAmelCase   (		self       :		List[Any]   )			->						Dict[str, str]:

         """simple docstring"""
         return {
             self.audio_column: "audio",
             self.label_column: "labels",
         }





