


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 	UpperCAmelCase_					(					UpperCamelCase    ):

   '''simple docstring'''




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






     """simple docstring"""





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






   def _snake_case    (		self ):






     """simple docstring"""





     lowerCamelCase :					Optional[Any]    =				ids_tensor([self.batch_size, self.seq_length]						,				self.vocab_size )

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

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

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

     lowerCamelCase :					Tuple    =				self.get_config()

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






   def _snake_case    (		self ):






     """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 _snake_case    (		self ):






     """simple docstring"""





     lowerCamelCase :					List[Any]    =				self.get_config()
     lowerCamelCase :					Tuple    =				300
     return config






   def _snake_case    (		self						,				__A ):






     """simple docstring"""





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






   def _snake_case    (		self						,				__A						,				__A						,				__A						,				__A						,				__A						,				__A						,				__A ):






     """simple docstring"""





     lowerCamelCase :					Optional[Any]    =				DebertaModel(config=__A )
     model.to(__A )
     model.eval()
     lowerCamelCase :					Any    =				model(__A						,				attention_mask=__A						,				token_type_ids=__A )[0]
     lowerCamelCase :					int    =				model(__A						,				token_type_ids=__A )[0]
     lowerCamelCase :					Optional[Any]    =				model(__A )[0]

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






   def _snake_case    (		self						,				__A						,				__A						,				__A						,				__A						,				__A						,				__A						,				__A ):






     """simple docstring"""





     lowerCamelCase :					Optional[int]    =				DebertaForMaskedLM(config=__A )
     model.to(__A )
     model.eval()
     lowerCamelCase :					Dict    =				model(__A						,				attention_mask=__A						,				token_type_ids=__A						,				labels=__A )

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






   def _snake_case    (		self						,				__A						,				__A						,				__A						,				__A						,				__A						,				__A						,				__A ):






     """simple docstring"""





     lowerCamelCase :					Dict    =				self.num_labels
     lowerCamelCase :					str    =				DebertaForSequenceClassification(__A )
     model.to(__A )
     model.eval()
     lowerCamelCase :					Optional[int]    =				model(__A						,				attention_mask=__A						,				token_type_ids=__A						,				labels=__A )
     self.parent.assertListEqual(list(result.logits.size() )						,				[self.batch_size, self.num_labels] )
     self.check_loss_output(__A )






   def _snake_case    (		self						,				__A						,				__A						,				__A						,				__A						,				__A						,				__A						,				__A ):






     """simple docstring"""





     lowerCamelCase :					Optional[Any]    =				self.num_labels
     lowerCamelCase :					List[str]    =				DebertaForTokenClassification(config=__A )
     model.to(__A )
     model.eval()
     lowerCamelCase :					Any    =				model(__A						,				attention_mask=__A						,				token_type_ids=__A						,				labels=__A )
     self.parent.assertEqual(result.logits.shape						,				(self.batch_size, self.seq_length, self.num_labels) )






   def _snake_case    (		self						,				__A						,				__A						,				__A						,				__A						,				__A						,				__A						,				__A ):






     """simple docstring"""





     lowerCamelCase :					Tuple    =				DebertaForQuestionAnswering(config=__A )
     model.to(__A )
     model.eval()
     lowerCamelCase :					Tuple    =				model(
         __A						,				attention_mask=__A						,				token_type_ids=__A						,				start_positions=__A						,				end_positions=__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 _snake_case    (		self ):






     """simple docstring"""





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



@require_torch
class 	UpperCAmelCase_					(					UpperCamelCase		, UpperCamelCase		, unittest.TestCase    ):

   '''simple docstring'''




   __A :      Tuple     		=		(
       (
           DebertaModel,
           DebertaForMaskedLM,
           DebertaForSequenceClassification,
           DebertaForTokenClassification,
           DebertaForQuestionAnswering,
       )
       if is_torch_available()
       else ()
   )
   __A :      Dict     		=		(
       {
           "feature-extraction": DebertaModel,
           "fill-mask": DebertaForMaskedLM,
           "question-answering": DebertaForQuestionAnswering,
           "text-classification": DebertaForSequenceClassification,
           "token-classification": DebertaForTokenClassification,
           "zero-shot": DebertaForSequenceClassification,
       }
       if is_torch_available()
       else {}
   )

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






   def _snake_case    (		self ):






     """simple docstring"""





     lowerCamelCase :					Optional[Any]    =				DebertaModelTester(self )
     lowerCamelCase :					Dict    =				ConfigTester(self						,				config_class=__A						,				hidden_size=37 )






   def _snake_case    (		self ):






     """simple docstring"""





     self.config_tester.run_common_tests()






   def _snake_case    (		self ):






     """simple docstring"""





     lowerCamelCase :					List[Any]    =				self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_deberta_model(*__A )






   def _snake_case    (		self ):






     """simple docstring"""





     lowerCamelCase :					Tuple    =				self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_deberta_for_sequence_classification(*__A )






   def _snake_case    (		self ):






     """simple docstring"""





     lowerCamelCase :					Optional[Any]    =				self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_deberta_for_masked_lm(*__A )






   def _snake_case    (		self ):






     """simple docstring"""





     lowerCamelCase :					Optional[Any]    =				self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_deberta_for_question_answering(*__A )






   def _snake_case    (		self ):






     """simple docstring"""





     lowerCamelCase :					Optional[int]    =				self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_deberta_for_token_classification(*__A )






   @slow
   def _snake_case    (		self ):






     """simple docstring"""





     for model_name in DEBERTA_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
       lowerCamelCase :					Optional[Any]    =				DebertaModel.from_pretrained(__A )
       self.assertIsNotNone(__A )



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

   '''simple docstring'''




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






     """simple docstring"""





     pass






   @slow
   def _snake_case    (		self ):






     """simple docstring"""





     lowerCamelCase :					Tuple    =				DebertaModel.from_pretrained("microsoft/deberta-base" )

     lowerCamelCase :					int    =				torch.tensor([[0, 3_1414, 232, 328, 740, 1140, 1_2695, 69, 4_6078, 1588, 2]] )
     lowerCamelCase :					int    =				torch.tensor([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]] )
     with torch.no_grad():
       lowerCamelCase :					str    =				model(__A						,				attention_mask=__A )[0]
     # compare the actual values for a slice.
     lowerCamelCase :					List[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]						,				__A						,				atol=1e-4 )						,				F"""{output[:, 1:4, 1:4]}""" )







from typing import TYPE_CHECKING

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


_snake_case             =     {
    '''configuration_squeezebert''': [
        '''SQUEEZEBERT_PRETRAINED_CONFIG_ARCHIVE_MAP''',
        '''SqueezeBertConfig''',
        '''SqueezeBertOnnxConfig''',
    ],
    '''tokenization_squeezebert''': ['''SqueezeBertTokenizer'''],
}

try:
   if not is_tokenizers_available():
      raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
   pass
else:
   _snake_case             =     ['''SqueezeBertTokenizerFast''']

try:
   if not is_torch_available():
      raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
   pass
else:
   _snake_case             =     [
       '''SQUEEZEBERT_PRETRAINED_MODEL_ARCHIVE_LIST''',
       '''SqueezeBertForMaskedLM''',
       '''SqueezeBertForMultipleChoice''',
       '''SqueezeBertForQuestionAnswering''',
       '''SqueezeBertForSequenceClassification''',
       '''SqueezeBertForTokenClassification''',
       '''SqueezeBertModel''',
       '''SqueezeBertModule''',
       '''SqueezeBertPreTrainedModel''',
   ]


if TYPE_CHECKING:
   from .configuration_squeezebert import (
       SQUEEZEBERT_PRETRAINED_CONFIG_ARCHIVE_MAP,
       SqueezeBertConfig,
       SqueezeBertOnnxConfig,
   )
   from .tokenization_squeezebert import SqueezeBertTokenizer

   try:
      if not is_tokenizers_available():
         raise OptionalDependencyNotAvailable()
   except OptionalDependencyNotAvailable:
      pass
   else:
      from .tokenization_squeezebert_fast import SqueezeBertTokenizerFast

   try:
      if not is_torch_available():
         raise OptionalDependencyNotAvailable()
   except OptionalDependencyNotAvailable:
      pass
   else:
      from .modeling_squeezebert import (
          SQUEEZEBERT_PRETRAINED_MODEL_ARCHIVE_LIST,
          SqueezeBertForMaskedLM,
          SqueezeBertForMultipleChoice,
          SqueezeBertForQuestionAnswering,
          SqueezeBertForSequenceClassification,
          SqueezeBertForTokenClassification,
          SqueezeBertModel,
          SqueezeBertModule,
          SqueezeBertPreTrainedModel,
      )

else:
   import sys

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



