


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 lowerCAmelCase_		:

 '''simple docstring'''

 def __init__(      self						, _UpperCAmelCase						, _UpperCAmelCase=13						, _UpperCAmelCase=7						, _UpperCAmelCase=True						, _UpperCAmelCase=True						, _UpperCAmelCase=True						, _UpperCAmelCase=True						, _UpperCAmelCase=99						, _UpperCAmelCase=32						, _UpperCAmelCase=2						, _UpperCAmelCase=4						, _UpperCAmelCase=37						, _UpperCAmelCase="gelu"						, _UpperCAmelCase=0.1						, _UpperCAmelCase=0.1						, _UpperCAmelCase=5_12						, _UpperCAmelCase=16						, _UpperCAmelCase=2						, _UpperCAmelCase=0.02						, _UpperCAmelCase=3						, _UpperCAmelCase=4						, _UpperCAmelCase=None						, ):
       snake_case_			= parent
       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_			= 5_12
       snake_case_			= 16
       snake_case_			= False
       snake_case_			= -1
       snake_case_			= True
       snake_case_			= 2
       snake_case_			= 0.02
       snake_case_			= 3
       snake_case_			= 4
       snake_case_			= None



 def 		UpperCamelCase__	(      self   ):
       snake_case_			= ids_tensor([self.batch_size, self.seq_length]						, self.vocab_size   )

       snake_case_			= None
       if self.use_input_mask:
             snake_case_			= random_attention_mask([self.batch_size, self.seq_length]   )

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

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

       snake_case_			= 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=_UpperCAmelCase						, )

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



 def 		UpperCamelCase__	(      self						, _UpperCAmelCase						, _UpperCAmelCase						, _UpperCAmelCase						, _UpperCAmelCase						, _UpperCAmelCase						, _UpperCAmelCase						, _UpperCAmelCase   ):
       snake_case_			= TFDebertaModel(config=_UpperCAmelCase   )
       snake_case_			= {'''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids}

       snake_case_			= [input_ids, input_mask]
       snake_case_			= model(_UpperCAmelCase   )

       snake_case_			= model(_UpperCAmelCase   )

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



 def 		UpperCamelCase__	(      self						, _UpperCAmelCase						, _UpperCAmelCase						, _UpperCAmelCase						, _UpperCAmelCase						, _UpperCAmelCase						, _UpperCAmelCase						, _UpperCAmelCase   ):
       snake_case_			= TFDebertaForMaskedLM(config=_UpperCAmelCase   )
       snake_case_			= {
           '''input_ids''': input_ids,
           '''attention_mask''': input_mask,
           '''token_type_ids''': token_type_ids,
       }
       snake_case_			= model(_UpperCAmelCase   )
       self.parent.assertEqual(result.logits.shape						, (self.batch_size, self.seq_length, self.vocab_size)   )



 def 		UpperCamelCase__	(      self						, _UpperCAmelCase						, _UpperCAmelCase						, _UpperCAmelCase						, _UpperCAmelCase						, _UpperCAmelCase						, _UpperCAmelCase						, _UpperCAmelCase   ):
       snake_case_			= self.num_labels
       snake_case_			= TFDebertaForSequenceClassification(config=_UpperCAmelCase   )
       snake_case_			= {
           '''input_ids''': input_ids,
           '''attention_mask''': input_mask,
           '''token_type_ids''': token_type_ids,
       }

       snake_case_			= model(_UpperCAmelCase   )
       self.parent.assertEqual(result.logits.shape						, (self.batch_size, self.num_labels)   )



 def 		UpperCamelCase__	(      self						, _UpperCAmelCase						, _UpperCAmelCase						, _UpperCAmelCase						, _UpperCAmelCase						, _UpperCAmelCase						, _UpperCAmelCase						, _UpperCAmelCase   ):
       snake_case_			= self.num_labels
       snake_case_			= TFDebertaForTokenClassification(config=_UpperCAmelCase   )
       snake_case_			= {
           '''input_ids''': input_ids,
           '''attention_mask''': input_mask,
           '''token_type_ids''': token_type_ids,
       }
       snake_case_			= model(_UpperCAmelCase   )
       self.parent.assertEqual(result.logits.shape						, (self.batch_size, self.seq_length, self.num_labels)   )



 def 		UpperCamelCase__	(      self						, _UpperCAmelCase						, _UpperCAmelCase						, _UpperCAmelCase						, _UpperCAmelCase						, _UpperCAmelCase						, _UpperCAmelCase						, _UpperCAmelCase   ):
       snake_case_			= TFDebertaForQuestionAnswering(config=_UpperCAmelCase   )
       snake_case_			= {
           '''input_ids''': input_ids,
           '''attention_mask''': input_mask,
           '''token_type_ids''': token_type_ids,
       }

       snake_case_			= model(_UpperCAmelCase   )
       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 		UpperCamelCase__	(      self   ):
       snake_case_			= self.prepare_config_and_inputs()
       (
           (
           snake_case_
       )			,       (
           snake_case_
       )			,       (
           snake_case_
       )			,       (
           snake_case_
       )			,       (
           snake_case_
       )			,       (
           snake_case_
       )			,       (
           snake_case_
       )			,       
       )			= config_and_inputs
       snake_case_			= {'''input_ids''': input_ids, '''token_type_ids''': token_type_ids, '''attention_mask''': input_mask}
       return config, inputs_dict







@require_tf
class lowerCAmelCase_		(					lowerCamelCase__   ,		lowerCamelCase__   ,		unittest.TestCase			):

 '''simple docstring'''

 __snake_case				=    (
     (
         TFDebertaModel,
         TFDebertaForMaskedLM,
         TFDebertaForQuestionAnswering,
         TFDebertaForSequenceClassification,
         TFDebertaForTokenClassification,
     )
     if is_tf_available()
     else ()
 )
 __snake_case				=    (
     {
         "feature-extraction": TFDebertaModel,
         "fill-mask": TFDebertaForMaskedLM,
         "question-answering": TFDebertaForQuestionAnswering,
         "text-classification": TFDebertaForSequenceClassification,
         "token-classification": TFDebertaForTokenClassification,
         "zero-shot": TFDebertaForSequenceClassification,
     }
     if is_tf_available()
     else {}
 )

 __snake_case				=    False
 __snake_case				=    False



 def 		UpperCamelCase__	(      self   ):
       snake_case_			= TFDebertaModelTester(self   )
       snake_case_			= ConfigTester(self						, config_class=_UpperCAmelCase						, hidden_size=37   )



 def 		UpperCamelCase__	(      self   ):
       self.config_tester.run_common_tests()



 def 		UpperCamelCase__	(      self   ):
       snake_case_			= self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_model(*_UpperCAmelCase   )



 def 		UpperCamelCase__	(      self   ):
       snake_case_			= self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_for_masked_lm(*_UpperCAmelCase   )



 def 		UpperCamelCase__	(      self   ):
       snake_case_			= self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_for_question_answering(*_UpperCAmelCase   )



 def 		UpperCamelCase__	(      self   ):
       snake_case_			= self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_for_sequence_classification(*_UpperCAmelCase   )



 def 		UpperCamelCase__	(      self   ):
       snake_case_			= self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_for_token_classification(*_UpperCAmelCase   )





 @slow
 def 		UpperCamelCase__	(      self   ):
       snake_case_			= TFDebertaModel.from_pretrained('''kamalkraj/deberta-base'''   )
       self.assertIsNotNone(_UpperCAmelCase   )







@require_tf
class lowerCAmelCase_		(					unittest.TestCase			):

 '''simple docstring'''

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





 @slow
 def 		UpperCamelCase__	(      self   ):
       snake_case_			= TFDebertaModel.from_pretrained('''kamalkraj/deberta-base'''   )
       snake_case_			= tf.constant([[0, 3_14_14, 2_32, 3_28, 7_40, 11_40, 1_26_95, 69, 4_60_78, 15_88, 2]]   )
       snake_case_			= tf.constant([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]   )
       snake_case_			= model(_UpperCAmelCase						, attention_mask=_UpperCAmelCase   )[0]

       snake_case_			= 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]						, _UpperCAmelCase						, atol=1E-4   )



from typing import Dict, List, Optional, Tuple

from ...tokenization_utils import AddedToken, PreTrainedTokenizer
from ...utils import logging


UpperCAmelCase     =       logging.get_logger(__name__)







class lowerCAmelCase_		(					lowerCamelCase__			):

 '''simple docstring'''

 __snake_case				=    ["input_ids", "attention_mask"]



 def __init__(      self						, _UpperCAmelCase="[PAD]"						, _UpperCAmelCase="[BOS]"						, _UpperCAmelCase="[EOS]"						, _UpperCAmelCase="[MASK]"						, _UpperCAmelCase="[CLS]"						, _UpperCAmelCase="[SEP]"						, _UpperCAmelCase=20_48						, **_UpperCAmelCase						, ):
       snake_case_			= AddedToken(_UpperCAmelCase						, lstrip=_UpperCAmelCase						, rstrip=_UpperCAmelCase   ) if isinstance(_UpperCAmelCase						, _UpperCAmelCase   ) else pad_token
       snake_case_			= AddedToken(_UpperCAmelCase						, lstrip=_UpperCAmelCase						, rstrip=_UpperCAmelCase   ) if isinstance(_UpperCAmelCase						, _UpperCAmelCase   ) else bos_token
       snake_case_			= AddedToken(_UpperCAmelCase						, lstrip=_UpperCAmelCase						, rstrip=_UpperCAmelCase   ) if isinstance(_UpperCAmelCase						, _UpperCAmelCase   ) else eos_token
       snake_case_			= AddedToken(_UpperCAmelCase						, lstrip=_UpperCAmelCase						, rstrip=_UpperCAmelCase   ) if isinstance(_UpperCAmelCase						, _UpperCAmelCase   ) else mask_token
       snake_case_			= AddedToken(_UpperCAmelCase						, lstrip=_UpperCAmelCase						, rstrip=_UpperCAmelCase   ) if isinstance(_UpperCAmelCase						, _UpperCAmelCase   ) else cls_token
       snake_case_			= AddedToken(_UpperCAmelCase						, lstrip=_UpperCAmelCase						, rstrip=_UpperCAmelCase   ) if isinstance(_UpperCAmelCase						, _UpperCAmelCase   ) else sep_token

       super().__init__(
           pad_token=_UpperCAmelCase						, bos_token=_UpperCAmelCase						, eos_token=_UpperCAmelCase						, mask_token=_UpperCAmelCase						, cls_token=_UpperCAmelCase						, sep_token=_UpperCAmelCase						, model_max_length=_UpperCAmelCase						, **_UpperCAmelCase						, )

       snake_case_			= 2**8  # utf is 8 bits

       # define special tokens dict
       snake_case_			= {
           self.pad_token: 0,
           self.bos_token: 1,
           self.eos_token: 2,
           self.mask_token: 3,
           self.cls_token: 4,
           self.sep_token: 5,
       }
       snake_case_			= len(self.special_tokens_encoder   )
       snake_case_			= {v: k for k, v in self.special_tokens_encoder.items()}



 def 		UpperCamelCase__	(      self   ):
       snake_case_			= self.special_tokens_encoder.copy()
       vocab.update(self.added_tokens_encoder   )
       for i in range(self._utf_vocab_size   ):
             snake_case_			= chr(_UpperCAmelCase   )
             snake_case_			= i + len(self.special_tokens_encoder   )
       return vocab



 @property
 def 		UpperCamelCase__	(      self   ):
       return self._utf_vocab_size + self._num_special_tokens



 def 		UpperCamelCase__	(      self						, _UpperCAmelCase						, _UpperCAmelCase = None						, _UpperCAmelCase = False   ):
       if already_has_special_tokens:
             return super().get_special_tokens_mask(
                 token_ids_a=_UpperCAmelCase						, token_ids_a=_UpperCAmelCase						, already_has_special_tokens=_UpperCAmelCase   )

       # normal case: some special tokens
       if token_ids_a is None:
             return [1] + [0] * len(_UpperCAmelCase   ) + [1]
       return [1] + ([0] * len(_UpperCAmelCase   )) + [1] + ([0] * len(_UpperCAmelCase   )) + [1]



 def 		UpperCamelCase__	(      self						, _UpperCAmelCase						, _UpperCAmelCase = None   ):
       if token_ids_a is None:
             return [self.cls_token_id] + token_ids_a + [self.sep_token_id]
       else:
             return [self.cls_token_id] + token_ids_a + [self.sep_token_id] + token_ids_a + [self.sep_token_id]



 def 		UpperCamelCase__	(      self						, _UpperCAmelCase   ):
       snake_case_			= [chr(_UpperCAmelCase   ) for i in text.encode('''utf-8'''   )]
       return tokens



 def 		UpperCamelCase__	(      self						, _UpperCAmelCase   ):
       if token in self.special_tokens_encoder:
             snake_case_			= self.special_tokens_encoder[token]
       elif token in self.added_tokens_encoder:
             snake_case_			= self.added_tokens_encoder[token]
       elif len(_UpperCAmelCase   ) != 1:
             snake_case_			= self.unk_token_id
       else:
             snake_case_			= ord(_UpperCAmelCase   ) + self._num_special_tokens
       return token_id



 def 		UpperCamelCase__	(      self						, _UpperCAmelCase   ):
       if index in self.special_tokens_decoder:
             snake_case_			= self.special_tokens_decoder[index]
       elif index in self.added_tokens_decoder:
             snake_case_			= self.added_tokens_decoder[index]
       else:
             snake_case_			= chr(index - self._num_special_tokens   )
       return token



 def 		UpperCamelCase__	(      self						, _UpperCAmelCase   ):
       snake_case_			= b''''''
       for token in tokens:
             if token in self.special_tokens_decoder:
                   snake_case_			= self.special_tokens_decoder[token].encode('''utf-8'''   )
             elif token in self.added_tokens_decoder:
                   snake_case_			= self.special_tokens_decoder[token].encode('''utf-8'''   )
             elif token in self.special_tokens_encoder:
                   snake_case_			= token.encode('''utf-8'''   )
             elif token in self.added_tokens_encoder:
                   snake_case_			= token.encode('''utf-8'''   )
             else:
                   snake_case_			= bytes([ord(_UpperCAmelCase   )]   )
             bstring += tok_string
       snake_case_			= bstring.decode('''utf-8'''						, errors='''replace'''   )
       return string





 def 		UpperCamelCase__	(      self						, _UpperCAmelCase						, _UpperCAmelCase = None   ):
       return ()