




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       a					(       _A      ):

     '''simple docstring'''





     def __init__(     self       :      Any ,   __snake_case       :      Union[str, Any] ,   __snake_case       :      Optional[int]=13 ,   __snake_case       :      str=7 ,   __snake_case       :      Optional[Any]=True ,   __snake_case       :      List[Any]=True ,   __snake_case       :      Dict=True ,   __snake_case       :      Optional[int]=True ,   __snake_case       :      str=99 ,   __snake_case       :      Optional[Any]=32 ,   __snake_case       :      str=5 ,   __snake_case       :      List[Any]=4 ,   __snake_case       :      str=37 ,   __snake_case       :      Union[str, Any]="gelu" ,   __snake_case       :      Dict=0.1 ,   __snake_case       :      Optional[Any]=0.1 ,   __snake_case       :      int=5_12 ,   __snake_case       :      Tuple=16 ,   __snake_case       :      List[str]=2 ,   __snake_case       :      Optional[int]=0.02 ,   __snake_case       :      str=False ,   __snake_case       :      Tuple=True ,   __snake_case       :      Optional[Any]="None" ,   __snake_case       :      Union[str, Any]=3 ,   __snake_case       :      Union[str, Any]=4 ,   __snake_case       :      Optional[int]=None ,   ):
         UpperCAmelCase_										=					parent
         UpperCAmelCase_										=					batch_size
         UpperCAmelCase_										=					seq_length
         UpperCAmelCase_										=					is_training
         UpperCAmelCase_										=					use_input_mask
         UpperCAmelCase_										=					use_token_type_ids
         UpperCAmelCase_										=					use_labels
         UpperCAmelCase_										=					vocab_size
         UpperCAmelCase_										=					hidden_size
         UpperCAmelCase_										=					num_hidden_layers
         UpperCAmelCase_										=					num_attention_heads
         UpperCAmelCase_										=					intermediate_size
         UpperCAmelCase_										=					hidden_act
         UpperCAmelCase_										=					hidden_dropout_prob
         UpperCAmelCase_										=					attention_probs_dropout_prob
         UpperCAmelCase_										=					max_position_embeddings
         UpperCAmelCase_										=					type_vocab_size
         UpperCAmelCase_										=					type_sequence_label_size
         UpperCAmelCase_										=					initializer_range
         UpperCAmelCase_										=					num_labels
         UpperCAmelCase_										=					num_choices
         UpperCAmelCase_										=					relative_attention
         UpperCAmelCase_										=					position_biased_input
         UpperCAmelCase_										=					pos_att_type
         UpperCAmelCase_										=					scope
     def   lowerCamelCase_       (     self       :      str    ):
         UpperCAmelCase_										=					ids_tensor([self.batch_size, self.seq_length] ,   self.vocab_size    )

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

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

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

         UpperCAmelCase_										=					self.get_config()

         return config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels
     def   lowerCamelCase_       (     self       :      Optional[int]    ):
         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       :      Tuple    ):
         UpperCAmelCase_										=					self.get_config()
         UpperCAmelCase_										=					3_00
         return config
     def   lowerCamelCase_       (     self       :      Any ,   __snake_case       :      str    ):
         self.parent.assertListEqual(list(result.loss.size()    ) ,   []    )
     def   lowerCamelCase_       (     self       :      int ,   __snake_case       :      Optional[Any] ,   __snake_case       :      Tuple ,   __snake_case       :      Any ,   __snake_case       :      Tuple ,   __snake_case       :      Union[str, Any] ,   __snake_case       :      Tuple ,   __snake_case       :      Optional[int]    ):
         UpperCAmelCase_										=					DebertaModel(config=__snake_case    )
         model.to(__snake_case    )
         model.eval()
         UpperCAmelCase_										=					model(__snake_case ,   attention_mask=__snake_case ,   token_type_ids=__snake_case    )[0]
         UpperCAmelCase_										=					model(__snake_case ,   token_type_ids=__snake_case    )[0]
         UpperCAmelCase_										=					model(__snake_case    )[0]

         self.parent.assertListEqual(list(sequence_output.size()    ) ,   [self.batch_size, self.seq_length, self.hidden_size]    )
     def   lowerCamelCase_       (     self       :      Union[str, Any] ,   __snake_case       :      Any ,   __snake_case       :      Tuple ,   __snake_case       :      Optional[int] ,   __snake_case       :      List[Any] ,   __snake_case       :      Optional[Any] ,   __snake_case       :      Any ,   __snake_case       :      Optional[int]    ):
         UpperCAmelCase_										=					DebertaForMaskedLM(config=__snake_case    )
         model.to(__snake_case    )
         model.eval()
         UpperCAmelCase_										=					model(__snake_case ,   attention_mask=__snake_case ,   token_type_ids=__snake_case ,   labels=__snake_case    )

         self.parent.assertEqual(result.logits.shape ,   (self.batch_size, self.seq_length, self.vocab_size)    )
     def   lowerCamelCase_       (     self       :      List[Any] ,   __snake_case       :      Dict ,   __snake_case       :      str ,   __snake_case       :      Any ,   __snake_case       :      Optional[Any] ,   __snake_case       :      str ,   __snake_case       :      List[str] ,   __snake_case       :      str    ):
         UpperCAmelCase_										=					self.num_labels
         UpperCAmelCase_										=					DebertaForSequenceClassification(__snake_case    )
         model.to(__snake_case    )
         model.eval()
         UpperCAmelCase_										=					model(__snake_case ,   attention_mask=__snake_case ,   token_type_ids=__snake_case ,   labels=__snake_case    )
         self.parent.assertListEqual(list(result.logits.size()    ) ,   [self.batch_size, self.num_labels]    )
         self.check_loss_output(__snake_case    )
     def   lowerCamelCase_       (     self       :      str ,   __snake_case       :      Optional[int] ,   __snake_case       :      Dict ,   __snake_case       :      List[Any] ,   __snake_case       :      Dict ,   __snake_case       :      Any ,   __snake_case       :      str ,   __snake_case       :      Optional[int]    ):
         UpperCAmelCase_										=					self.num_labels
         UpperCAmelCase_										=					DebertaForTokenClassification(config=__snake_case    )
         model.to(__snake_case    )
         model.eval()
         UpperCAmelCase_										=					model(__snake_case ,   attention_mask=__snake_case ,   token_type_ids=__snake_case ,   labels=__snake_case    )
         self.parent.assertEqual(result.logits.shape ,   (self.batch_size, self.seq_length, self.num_labels)    )
     def   lowerCamelCase_       (     self       :      List[str] ,   __snake_case       :      Optional[int] ,   __snake_case       :      List[Any] ,   __snake_case       :      Tuple ,   __snake_case       :      Any ,   __snake_case       :      Dict ,   __snake_case       :      Union[str, Any] ,   __snake_case       :      Tuple    ):
         UpperCAmelCase_										=					DebertaForQuestionAnswering(config=__snake_case    )
         model.to(__snake_case    )
         model.eval()
         UpperCAmelCase_										=					model(
             __snake_case ,   attention_mask=__snake_case ,   token_type_ids=__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   lowerCamelCase_       (     self       :      List[str]    ):
         UpperCAmelCase_										=					self.prepare_config_and_inputs()
         (
             (
             UpperCAmelCase_
         )			,							(
             UpperCAmelCase_
         )			,							(
             UpperCAmelCase_
         )			,							(
             UpperCAmelCase_
         )			,							(
             UpperCAmelCase_
         )			,							(
             UpperCAmelCase_
         )			,							(
             UpperCAmelCase_
         )			,							
         )										=					config_and_inputs
         UpperCAmelCase_										=					{'''input_ids''': input_ids, '''token_type_ids''': token_type_ids, '''attention_mask''': input_mask}
         return config, inputs_dict



@require_torch
class       a					(       _A     ,       _A     ,       unittest.TestCase      ):

     '''simple docstring'''





     lowerCAmelCase    :	str       		=  (
         (
             DebertaModel,
             DebertaForMaskedLM,
             DebertaForSequenceClassification,
             DebertaForTokenClassification,
             DebertaForQuestionAnswering,
         )
         if is_torch_available()
         else ()
     )
     lowerCAmelCase    :	List[Any]       		=  (
         {
             'feature-extraction': DebertaModel,
             'fill-mask': DebertaForMaskedLM,
             'question-answering': DebertaForQuestionAnswering,
             'text-classification': DebertaForSequenceClassification,
             'token-classification': DebertaForTokenClassification,
             'zero-shot': DebertaForSequenceClassification,
         }
         if is_torch_available()
         else {}
     )

     lowerCAmelCase    :	Optional[int]       		=  True
     lowerCAmelCase    :	str       		=  False
     lowerCAmelCase    :	str       		=  False
     lowerCAmelCase    :	Any       		=  False
     lowerCAmelCase    :	int       		=  False
     def   lowerCamelCase_       (     self       :      Dict    ):
         UpperCAmelCase_										=					DebertaModelTester(self    )
         UpperCAmelCase_										=					ConfigTester(self ,   config_class=__snake_case ,   hidden_size=37    )
     def   lowerCamelCase_       (     self       :      Tuple    ):
         self.config_tester.run_common_tests()
     def   lowerCamelCase_       (     self       :      int    ):
         UpperCAmelCase_										=					self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_deberta_model(*__snake_case    )
     def   lowerCamelCase_       (     self       :      Optional[Any]    ):
         UpperCAmelCase_										=					self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_deberta_for_sequence_classification(*__snake_case    )
     def   lowerCamelCase_       (     self       :      List[str]    ):
         UpperCAmelCase_										=					self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_deberta_for_masked_lm(*__snake_case    )
     def   lowerCamelCase_       (     self       :      str    ):
         UpperCAmelCase_										=					self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_deberta_for_question_answering(*__snake_case    )
     def   lowerCamelCase_       (     self       :      Dict    ):
         UpperCAmelCase_										=					self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_deberta_for_token_classification(*__snake_case    )




     @slow
     def   lowerCamelCase_       (     self       :      Optional[int]    ):
         for model_name in DEBERTA_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
             UpperCAmelCase_										=					DebertaModel.from_pretrained(__snake_case    )
             self.assertIsNotNone(__snake_case    )



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

     '''simple docstring'''





     @unittest.skip(reason='''Model not available yet'''    )
     def   lowerCamelCase_       (     self       :      Tuple    ):
         pass




     @slow
     def   lowerCamelCase_       (     self       :      Any    ):
         UpperCAmelCase_										=					DebertaModel.from_pretrained('''microsoft/deberta-base'''    )

         UpperCAmelCase_										=					torch.tensor([[0, 3_14_14, 2_32, 3_28, 7_40, 11_40, 1_26_95, 69, 4_60_78, 15_88, 2]]    )
         UpperCAmelCase_										=					torch.tensor([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]    )
         with torch.no_grad():
             UpperCAmelCase_										=					model(__snake_case ,   attention_mask=__snake_case    )[0]
         # compare the actual values for a slice.
         UpperCAmelCase_										=					torch.tensor(
             [[[-0.5_986, -0.8_055, -0.8_462], [1.4_484, -0.9_348, -0.8_059], [0.3_123, 0.0_032, -1.4_131]]]    )
         self.assertTrue(torch.allclose(output[:, 1:4, 1:4] ,   __snake_case ,   atol=1E-4    ) ,   F'{output[:, 1:4, 1:4]}'    )











from typing import TYPE_CHECKING

from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available, is_vision_available


_lowerCamelCase      			=						{
    'configuration_conditional_detr': [
        'CONDITIONAL_DETR_PRETRAINED_CONFIG_ARCHIVE_MAP',
        'ConditionalDetrConfig',
        'ConditionalDetrOnnxConfig',
    ]
}

try:
 if not is_vision_available():
  raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
 pass
else:
 _lowerCamelCase      			=						['ConditionalDetrFeatureExtractor']
 _lowerCamelCase      			=						['ConditionalDetrImageProcessor']

try:
 if not is_torch_available():
  raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
 pass
else:
 _lowerCamelCase      			=						[
     'CONDITIONAL_DETR_PRETRAINED_MODEL_ARCHIVE_LIST',
     'ConditionalDetrForObjectDetection',
     'ConditionalDetrForSegmentation',
     'ConditionalDetrModel',
     'ConditionalDetrPreTrainedModel',
 ]


if TYPE_CHECKING:
 from .configuration_conditional_detr import (
     CONDITIONAL_DETR_PRETRAINED_CONFIG_ARCHIVE_MAP,
     ConditionalDetrConfig,
     ConditionalDetrOnnxConfig,
 )

 try:
  if not is_vision_available():
   raise OptionalDependencyNotAvailable()
 except OptionalDependencyNotAvailable:
  pass
 else:
  from .feature_extraction_conditional_detr import ConditionalDetrFeatureExtractor
  from .image_processing_conditional_detr import ConditionalDetrImageProcessor

 try:
  if not is_torch_available():
   raise OptionalDependencyNotAvailable()
 except OptionalDependencyNotAvailable:
  pass
 else:
  from .modeling_conditional_detr import (
      CONDITIONAL_DETR_PRETRAINED_MODEL_ARCHIVE_LIST,
      ConditionalDetrForObjectDetection,
      ConditionalDetrForSegmentation,
      ConditionalDetrModel,
      ConditionalDetrPreTrainedModel,
  )

else:
 import sys

 _lowerCamelCase      			=						_LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)





