




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




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


									'''simple docstring'''


									__lowerCAmelCase					=  parent
									__lowerCAmelCase					=  13
									__lowerCAmelCase					=  7
									__lowerCAmelCase					=  True
									__lowerCAmelCase					=  True
									__lowerCAmelCase					=  True
									__lowerCAmelCase					=  True
									__lowerCAmelCase					=  99
									__lowerCAmelCase					=  32
									__lowerCAmelCase					=  2
									__lowerCAmelCase					=  4
									__lowerCAmelCase					=  37
									__lowerCAmelCase					=  """gelu"""
									__lowerCAmelCase					=  0.1
									__lowerCAmelCase					=  0.1
									__lowerCAmelCase					=  512
									__lowerCAmelCase					=  16
									__lowerCAmelCase					=  False
									__lowerCAmelCase					=  -1
									__lowerCAmelCase					=  True
									__lowerCAmelCase					=  2
									__lowerCAmelCase					=  0.02
									__lowerCAmelCase					=  3
									__lowerCAmelCase					=  4
									__lowerCAmelCase					=  None




					def     _lowercase						(   self:		Any):


									'''simple docstring'''


									__lowerCAmelCase					=  ids_tensor([self.batch_size, self.seq_length],     self.vocab_size)

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

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

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

									__lowerCAmelCase					=  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=_lowercase,     )

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




					def     _lowercase						(   self:		List[str],     _lowercase:		Any,     _lowercase:		List[Any],     _lowercase:		Dict,     _lowercase:		List[Any],     _lowercase:		Optional[Any],     _lowercase:		List[str],     _lowercase:		Any):


									'''simple docstring'''


									__lowerCAmelCase					=  TFDebertaModel(config=_lowercase)
									__lowerCAmelCase					=  {"""input_ids""": input_ids, """attention_mask""": input_mask, """token_type_ids""": token_type_ids}

									__lowerCAmelCase					=  [input_ids, input_mask]
									__lowerCAmelCase					=  model(_lowercase)

									__lowerCAmelCase					=  model(_lowercase)

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




					def     _lowercase						(   self:		List[str],     _lowercase:		Any,     _lowercase:		Dict,     _lowercase:		Union[str, Any],     _lowercase:		List[str],     _lowercase:		int,     _lowercase:		Optional[int],     _lowercase:		Optional[Any]):


									'''simple docstring'''


									__lowerCAmelCase					=  TFDebertaForMaskedLM(config=_lowercase)
									__lowerCAmelCase					=  {
									    """input_ids""": input_ids,
									    """attention_mask""": input_mask,
									    """token_type_ids""": token_type_ids,
									}
									__lowerCAmelCase					=  model(_lowercase)
									self.parent.assertEqual(result.logits.shape,     (self.batch_size, self.seq_length, self.vocab_size))




					def     _lowercase						(   self:		Optional[Any],     _lowercase:		Optional[int],     _lowercase:		Optional[Any],     _lowercase:		Union[str, Any],     _lowercase:		Tuple,     _lowercase:		List[Any],     _lowercase:		Dict,     _lowercase:		Any):


									'''simple docstring'''


									__lowerCAmelCase					=  self.num_labels
									__lowerCAmelCase					=  TFDebertaForSequenceClassification(config=_lowercase)
									__lowerCAmelCase					=  {
									    """input_ids""": input_ids,
									    """attention_mask""": input_mask,
									    """token_type_ids""": token_type_ids,
									}

									__lowerCAmelCase					=  model(_lowercase)
									self.parent.assertEqual(result.logits.shape,     (self.batch_size, self.num_labels))




					def     _lowercase						(   self:		Tuple,     _lowercase:		int,     _lowercase:		Optional[Any],     _lowercase:		int,     _lowercase:		List[str],     _lowercase:		Dict,     _lowercase:		Dict,     _lowercase:		List[Any]):


									'''simple docstring'''


									__lowerCAmelCase					=  self.num_labels
									__lowerCAmelCase					=  TFDebertaForTokenClassification(config=_lowercase)
									__lowerCAmelCase					=  {
									    """input_ids""": input_ids,
									    """attention_mask""": input_mask,
									    """token_type_ids""": token_type_ids,
									}
									__lowerCAmelCase					=  model(_lowercase)
									self.parent.assertEqual(result.logits.shape,     (self.batch_size, self.seq_length, self.num_labels))




					def     _lowercase						(   self:		Tuple,     _lowercase:		Dict,     _lowercase:		Optional[Any],     _lowercase:		Tuple,     _lowercase:		Union[str, Any],     _lowercase:		Optional[int],     _lowercase:		List[str],     _lowercase:		Any):


									'''simple docstring'''


									__lowerCAmelCase					=  TFDebertaForQuestionAnswering(config=_lowercase)
									__lowerCAmelCase					=  {
									    """input_ids""": input_ids,
									    """attention_mask""": input_mask,
									    """token_type_ids""": token_type_ids,
									}

									__lowerCAmelCase					=  model(_lowercase)
									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     _lowercase						(   self:		Optional[Any]):


									'''simple docstring'''


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





@require_tf
class 		lowercase_       (	lowerCAmelCase__						,							lowerCAmelCase__						,							unittest.TestCase				):
					__UpperCamelCase     	= (
					    (
					        TFDebertaModel,
					        TFDebertaForMaskedLM,
					        TFDebertaForQuestionAnswering,
					        TFDebertaForSequenceClassification,
					        TFDebertaForTokenClassification,
					    )
					    if is_tf_available()
					    else ()
					)
					__UpperCamelCase     	= (
					    {
					        "feature-extraction": TFDebertaModel,
					        "fill-mask": TFDebertaForMaskedLM,
					        "question-answering": TFDebertaForQuestionAnswering,
					        "text-classification": TFDebertaForSequenceClassification,
					        "token-classification": TFDebertaForTokenClassification,
					        "zero-shot": TFDebertaForSequenceClassification,
					    }
					    if is_tf_available()
					    else {}
					)

					__UpperCamelCase     	= False
					__UpperCamelCase     	= False




					def     _lowercase						(   self:		Dict):


									'''simple docstring'''


									__lowerCAmelCase					=  TFDebertaModelTester(self)
									__lowerCAmelCase					=  ConfigTester(self,     config_class=_lowercase,     hidden_size=37)




					def     _lowercase						(   self:		Union[str, Any]):


									'''simple docstring'''


									self.config_tester.run_common_tests()




					def     _lowercase						(   self:		List[Any]):


									'''simple docstring'''


									__lowerCAmelCase					=  self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_model(*_lowercase)




					def     _lowercase						(   self:		List[Any]):


									'''simple docstring'''


									__lowerCAmelCase					=  self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_for_masked_lm(*_lowercase)




					def     _lowercase						(   self:		List[str]):


									'''simple docstring'''


									__lowerCAmelCase					=  self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_for_question_answering(*_lowercase)




					def     _lowercase						(   self:		Union[str, Any]):


									'''simple docstring'''


									__lowerCAmelCase					=  self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_for_sequence_classification(*_lowercase)




					def     _lowercase						(   self:		str):


									'''simple docstring'''


									__lowerCAmelCase					=  self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_for_token_classification(*_lowercase)







					@slow
					def     _lowercase						(   self:		Any):


									'''simple docstring'''


									__lowerCAmelCase					=  TFDebertaModel.from_pretrained("""kamalkraj/deberta-base""")
									self.assertIsNotNone(_lowercase)





@require_tf
class 		lowercase_       (	unittest.TestCase				):




					@unittest.skip(reason="""Model not available yet""")
					def     _lowercase						(   self:		Optional[Any]):


									'''simple docstring'''


									pass







					@slow
					def     _lowercase						(   self:		List[str]):


									'''simple docstring'''


									__lowerCAmelCase					=  TFDebertaModel.from_pretrained("""kamalkraj/deberta-base""")
									__lowerCAmelCase					=  tf.constant([[0, 31414, 232, 328, 740, 1140, 12695, 69, 46078, 1588, 2]])
									__lowerCAmelCase					=  tf.constant([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]])
									__lowerCAmelCase					=  model(_lowercase,     attention_mask=_lowercase)[0]

									__lowerCAmelCase					=  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],     _lowercase,     atol=1e-4)











import os
from typing import List, Union

import tensorflow as tf
from tensorflow_text import BertTokenizer as BertTokenizerLayer
from tensorflow_text import FastBertTokenizer, ShrinkLongestTrimmer, case_fold_utfa, combine_segments, pad_model_inputs

from .tokenization_bert import BertTokenizer







class __snake_case     (     tf.keras.layers.Layer						):







   def __init__( self						:		Optional[int]   ,		_snake_case						:		List   ,		_snake_case						:		bool   ,		_snake_case						:		int = None   ,		_snake_case						:		int = None   ,		_snake_case						:		int = None   ,		_snake_case						:		str = "longest"   ,		_snake_case						:		bool = True   ,		_snake_case						:		int = 512   ,		_snake_case						:		int = None   ,		_snake_case						:		bool = True   ,		_snake_case						:		bool = True   ,		_snake_case						:		bool = True   ,		**_snake_case						:		Any   ,		):

        """simple docstring"""


        super().__init__()
        if use_fast_bert_tokenizer:
             UpperCAmelCase_		=				FastBertTokenizer(
                 _snake_case   ,		token_out_type=tf.intaa   ,		lower_case_nfd_strip_accents=_snake_case   ,		**_snake_case)
        else:
             UpperCAmelCase_		=				tf.lookup.StaticVocabularyTable(
                 tf.lookup.KeyValueTensorInitializer(
                     keys=_snake_case   ,		key_dtype=tf.string   ,		values=tf.range(tf.size(_snake_case   ,		out_type=tf.intaa)   ,		dtype=tf.intaa)   ,		value_dtype=tf.intaa   ,		)   ,		num_oov_buckets=1   ,		)
             UpperCAmelCase_		=				BertTokenizerLayer(
                 _snake_case   ,		token_out_type=tf.intaa   ,		lower_case=_snake_case   ,		**_snake_case)

        UpperCAmelCase_		=				vocab_list
        UpperCAmelCase_		=				do_lower_case
        UpperCAmelCase_		=				cls_token_id or vocab_list.index('''[CLS]''')
        UpperCAmelCase_		=				sep_token_id or vocab_list.index('''[SEP]''')
        UpperCAmelCase_		=				pad_token_id or vocab_list.index('''[PAD]''')
        UpperCAmelCase_		=				ShrinkLongestTrimmer(max_length - 3   ,		axis=1)  # Allow room for special tokens
        UpperCAmelCase_		=				max_length
        UpperCAmelCase_		=				padding
        UpperCAmelCase_		=				truncation
        UpperCAmelCase_		=				pad_to_multiple_of
        UpperCAmelCase_		=				return_token_type_ids
        UpperCAmelCase_		=				return_attention_mask







   @classmethod
   def lowerCamelCase		( cls						:		Optional[Any]   ,		_snake_case						:		"PreTrainedTokenizerBase"   ,		**_snake_case						:		str):  # noqa: F821

        """simple docstring"""


        UpperCAmelCase_		=				kwargs.pop('''do_lower_case'''   ,		_snake_case)
        UpperCAmelCase_		=				tokenizer.do_lower_case if do_lower_case is None else do_lower_case
        UpperCAmelCase_		=				kwargs.pop('''cls_token_id'''   ,		_snake_case)
        UpperCAmelCase_		=				tokenizer.cls_token_id if cls_token_id is None else cls_token_id
        UpperCAmelCase_		=				kwargs.pop('''sep_token_id'''   ,		_snake_case)
        UpperCAmelCase_		=				tokenizer.sep_token_id if sep_token_id is None else sep_token_id
        UpperCAmelCase_		=				kwargs.pop('''pad_token_id'''   ,		_snake_case)
        UpperCAmelCase_		=				tokenizer.pad_token_id if pad_token_id is None else pad_token_id

        UpperCAmelCase_		=				tokenizer.get_vocab()
        UpperCAmelCase_		=				sorted([(wordpiece, idx) for wordpiece, idx in vocab.items()]   ,		key=lambda _snake_case: x[1])
        UpperCAmelCase_		=				[entry[0] for entry in vocab]
        return cls(
            vocab_list=_snake_case   ,		do_lower_case=_snake_case   ,		cls_token_id=_snake_case   ,		sep_token_id=_snake_case   ,		pad_token_id=_snake_case   ,		**_snake_case   ,		)







   @classmethod
   def lowerCamelCase		( cls						:		List[str]   ,		_snake_case						:		Union[str, os.PathLike]   ,		*_snake_case						:		Union[str, Any]   ,		**_snake_case						:		Union[str, Any]):

        """simple docstring"""


        try:
             UpperCAmelCase_		=				BertTokenizer.from_pretrained(_snake_case   ,		*_snake_case   ,		**_snake_case)
        except:  # noqa: E722
             from .tokenization_bert_fast import BertTokenizerFast

             UpperCAmelCase_		=				BertTokenizerFast.from_pretrained(_snake_case   ,		*_snake_case   ,		**_snake_case)
        return cls.from_tokenizer(_snake_case   ,		**_snake_case)







   def lowerCamelCase		( self						:		int   ,		_snake_case						:		str):

        """simple docstring"""


        if self.do_lower_case:
             UpperCAmelCase_		=				case_fold_utfa(_snake_case)
        UpperCAmelCase_		=				self.tf_tokenizer.tokenize(_snake_case)
        return tokens.merge_dims(1   ,		-1)







   def lowerCamelCase		( self						:		int   ,		_snake_case						:		str   ,		_snake_case						:		str=None   ,		_snake_case						:		Union[str, Any]=None   ,		_snake_case						:		str=None   ,		_snake_case						:		int=None   ,		_snake_case						:		Optional[int]=None   ,		_snake_case						:		Optional[Any]=None   ,		_snake_case						:		List[str]=None   ,		):

        """simple docstring"""


        if padding is None:
             UpperCAmelCase_		=				self.padding
        if padding not in ("longest", "max_length"):
             raise ValueError('''Padding must be either \'longest\' or \'max_length\'!''')
        if max_length is not None and text_pair is not None:
             # Because we have to instantiate a Trimmer to do it properly
             raise ValueError('''max_length cannot be overridden at call time when truncating paired texts!''')
        if max_length is None:
             UpperCAmelCase_		=				self.max_length
        if truncation is None:
             UpperCAmelCase_		=				self.truncation
        if pad_to_multiple_of is None:
             UpperCAmelCase_		=				self.pad_to_multiple_of
        if return_token_type_ids is None:
             UpperCAmelCase_		=				self.return_token_type_ids
        if return_attention_mask is None:
             UpperCAmelCase_		=				self.return_attention_mask
        if not isinstance(_snake_case   ,		tf.Tensor):
             UpperCAmelCase_		=				tf.convert_to_tensor(_snake_case)
        if text_pair is not None and not isinstance(_snake_case   ,		tf.Tensor):
             UpperCAmelCase_		=				tf.convert_to_tensor(_snake_case)
        if text_pair is not None:
             if text.shape.rank > 1:
                  raise ValueError('''text argument should not be multidimensional when a text pair is supplied!''')
             if text_pair.shape.rank > 1:
                  raise ValueError('''text_pair should not be multidimensional!''')
        if text.shape.rank == 2:
             UpperCAmelCase_	,			UpperCAmelCase_		=				text[:, 0], text[:, 1]
        UpperCAmelCase_		=				self.unpaired_tokenize(_snake_case)
        if text_pair is None:  # Unpaired text
             if truncation:
                  UpperCAmelCase_		=				text[:, : max_length - 2]  # Allow room for special tokens
             UpperCAmelCase_	,			UpperCAmelCase_		=				combine_segments(
                 (text,)   ,		start_of_sequence_id=self.cls_token_id   ,		end_of_segment_id=self.sep_token_id)
        else:  # Paired text
             UpperCAmelCase_		=				self.unpaired_tokenize(_snake_case)
             if truncation:
                  UpperCAmelCase_	,			UpperCAmelCase_		=				self.paired_trimmer.trim([text, text_pair])
             UpperCAmelCase_	,			UpperCAmelCase_		=				combine_segments(
                 (text, text_pair)   ,		start_of_sequence_id=self.cls_token_id   ,		end_of_segment_id=self.sep_token_id)
        if padding == "longest":
             UpperCAmelCase_		=				input_ids.bounding_shape(axis=1)
             if pad_to_multiple_of is not None:
                  # No ceiling division in tensorflow, so we negate floordiv instead
                  UpperCAmelCase_		=				pad_to_multiple_of * (-tf.math.floordiv(-pad_length   ,		_snake_case))
        else:
             UpperCAmelCase_		=				max_length

        UpperCAmelCase_	,			UpperCAmelCase_		=				pad_model_inputs(_snake_case   ,		max_seq_length=_snake_case   ,		pad_value=self.pad_token_id)
        UpperCAmelCase_		=				{'''input_ids''': input_ids}
        if return_attention_mask:
             UpperCAmelCase_		=				attention_mask
        if return_token_type_ids:
             UpperCAmelCase_	,			UpperCAmelCase_		=				pad_model_inputs(
                 _snake_case   ,		max_seq_length=_snake_case   ,		pad_value=self.pad_token_id)
             UpperCAmelCase_		=				token_type_ids
        return output







   def lowerCamelCase		( self						:		Optional[Any]):

        """simple docstring"""


        return {
            "vocab_list": self.vocab_list,
            "do_lower_case": self.do_lower_case,
            "cls_token_id": self.cls_token_id,
            "sep_token_id": self.sep_token_id,
            "pad_token_id": self.pad_token_id,
        }






