
from __future__ import annotations

import unittest

from transformers import DebertaVaConfig, 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 (
		    TFDebertaVaForMaskedLM,
		    TFDebertaVaForQuestionAnswering,
		    TFDebertaVaForSequenceClassification,
		    TFDebertaVaForTokenClassification,
		    TFDebertaVaModel,
		)




class 					lowerCamelCase__				:




	'''simple docstring'''




	def __init__(   self						:List[Any]	,  a						:Optional[Any]	,  a						:Any=1_3	,  a						:Dict=7	,  a						:str=True	,  a						:List[str]=True	,  a						:Optional[Any]=True	,  a						:Optional[Any]=True	,  a						:Optional[int]=9_9	,  a						:List[str]=3_2	,  a						:Optional[int]=2	,  a						:Any=4	,  a						:Optional[int]=3_7	,  a						:Dict="gelu"	,  a						:Union[str, Any]=0.1	,  a						:int=0.1	,  a						:Dict=5_1_2	,  a						:Optional[int]=1_6	,  a						:int=2	,  a						:str=0.02	,  a						:List[Any]=False	,  a						:Dict=True	,  a						:Optional[int]="None"	,  a						:Dict=3	,  a						:str=4	,  a						:int=None	,  )						->					Tuple:
								__UpperCamelCase      :  Optional[int]       =   parent
								__UpperCamelCase      :  Tuple       =   batch_size
								__UpperCamelCase      :  List[str]       =   seq_length
								__UpperCamelCase      :  List[Any]       =   is_training
								__UpperCamelCase      :  Tuple       =   use_input_mask
								__UpperCamelCase      :  int       =   use_token_type_ids
								__UpperCamelCase      :  List[str]       =   use_labels
								__UpperCamelCase      :  Dict       =   vocab_size
								__UpperCamelCase      :  Optional[Any]       =   hidden_size
								__UpperCamelCase      :  str       =   num_hidden_layers
								__UpperCamelCase      :  List[str]       =   num_attention_heads
								__UpperCamelCase      :  List[Any]       =   intermediate_size
								__UpperCamelCase      :  List[Any]       =   hidden_act
								__UpperCamelCase      :  List[str]       =   hidden_dropout_prob
								__UpperCamelCase      :  Optional[int]       =   attention_probs_dropout_prob
								__UpperCamelCase      :  List[str]       =   max_position_embeddings
								__UpperCamelCase      :  int       =   type_vocab_size
								__UpperCamelCase      :  int       =   type_sequence_label_size
								__UpperCamelCase      :  Dict       =   initializer_range
								__UpperCamelCase      :  Union[str, Any]       =   num_labels
								__UpperCamelCase      :  Dict       =   num_choices
								__UpperCamelCase      :  Optional[Any]       =   relative_attention
								__UpperCamelCase      :  Dict       =   position_biased_input
								__UpperCamelCase      :  Optional[Any]       =   pos_att_type
								__UpperCamelCase      :  Optional[Any]       =   scope







	def   _lowerCamelCase							(   self						:str		)						->					Any:
								__UpperCamelCase      :  Union[str, Any]       =   ids_tensor([self.batch_size, self.seq_length]	,  self.vocab_size		)

								__UpperCamelCase      :  List[Any]       =   None
								if self.use_input_mask:
															__UpperCamelCase      :  Optional[int]       =   random_attention_mask([self.batch_size, self.seq_length]		)

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

								__UpperCamelCase      :  Union[str, Any]       =   None
								__UpperCamelCase      :  Any       =   None
								__UpperCamelCase      :  str       =   None
								if self.use_labels:
															__UpperCamelCase      :  Tuple       =   ids_tensor([self.batch_size]	,  self.type_sequence_label_size		)
															__UpperCamelCase      :  int       =   ids_tensor([self.batch_size, self.seq_length]	,  self.num_labels		)

								__UpperCamelCase      :  Any       =   DebertaVaConfig(
								    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	,  position_biased_input=self.position_biased_input	,  initializer_range=self.initializer_range	,  return_dict=a	,  )

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







	def   _lowerCamelCase							(   self						:Optional[Any]	,  a						:Any	,  a						:Optional[int]	,  a						:Optional[Any]	,  a						:List[str]	,  a						:Optional[int]	,  a						:str	,  a						:Union[str, Any]		)						->					Dict:
								__UpperCamelCase      :  Union[str, Any]       =   TFDebertaVaModel(config=a		)
								__UpperCamelCase      :  Optional[Any]       =   {"input_ids": input_ids, "attention_mask": input_mask, "token_type_ids": token_type_ids}

								__UpperCamelCase      :  int       =   [input_ids, input_mask]
								__UpperCamelCase      :  Optional[int]       =   model(a		)

								__UpperCamelCase      :  int       =   model(a		)

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







	def   _lowerCamelCase							(   self						:str	,  a						:Tuple	,  a						:Optional[int]	,  a						:Tuple	,  a						:Union[str, Any]	,  a						:List[Any]	,  a						:Tuple	,  a						:str		)						->					Union[str, Any]:
								__UpperCamelCase      :  Optional[Any]       =   TFDebertaVaForMaskedLM(config=a		)
								__UpperCamelCase      :  Union[str, Any]       =   {
								    "input_ids": input_ids,
								    "attention_mask": input_mask,
								    "token_type_ids": token_type_ids,
								}
								__UpperCamelCase      :  str       =   model(a		)
								self.parent.assertEqual(result.logits.shape	,  (self.batch_size, self.seq_length, self.vocab_size)		)







	def   _lowerCamelCase							(   self						:Dict	,  a						:List[str]	,  a						:Optional[int]	,  a						:Dict	,  a						:List[Any]	,  a						:Union[str, Any]	,  a						:int	,  a						:Optional[int]		)						->					Union[str, Any]:
								__UpperCamelCase      :  List[Any]       =   self.num_labels
								__UpperCamelCase      :  Tuple       =   TFDebertaVaForSequenceClassification(config=a		)
								__UpperCamelCase      :  Optional[Any]       =   {
								    "input_ids": input_ids,
								    "attention_mask": input_mask,
								    "token_type_ids": token_type_ids,
								}

								__UpperCamelCase      :  Optional[int]       =   model(a		)
								self.parent.assertEqual(result.logits.shape	,  (self.batch_size, self.num_labels)		)







	def   _lowerCamelCase							(   self						:Any	,  a						:Optional[Any]	,  a						:Any	,  a						:Optional[Any]	,  a						:int	,  a						:List[str]	,  a						:List[Any]	,  a						:Union[str, Any]		)						->					List[Any]:
								__UpperCamelCase      :  Optional[int]       =   self.num_labels
								__UpperCamelCase      :  Optional[int]       =   TFDebertaVaForTokenClassification(config=a		)
								__UpperCamelCase      :  str       =   {
								    "input_ids": input_ids,
								    "attention_mask": input_mask,
								    "token_type_ids": token_type_ids,
								}
								__UpperCamelCase      :  Any       =   model(a		)
								self.parent.assertEqual(result.logits.shape	,  (self.batch_size, self.seq_length, self.num_labels)		)







	def   _lowerCamelCase							(   self						:int	,  a						:int	,  a						:Any	,  a						:Optional[int]	,  a						:List[Any]	,  a						:Optional[int]	,  a						:int	,  a						:Optional[Any]		)						->					List[Any]:
								__UpperCamelCase      :  Dict       =   TFDebertaVaForQuestionAnswering(config=a		)
								__UpperCamelCase      :  Optional[Any]       =   {
								    "input_ids": input_ids,
								    "attention_mask": input_mask,
								    "token_type_ids": token_type_ids,
								}

								__UpperCamelCase      :  List[Any]       =   model(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   _lowerCamelCase							(   self						:str		)						->					Optional[Any]:
								__UpperCamelCase      :  int       =   self.prepare_config_and_inputs()
								(
								    (
								    __UpperCamelCase
								)  ,		(
								    __UpperCamelCase
								)  ,		(
								    __UpperCamelCase
								)  ,		(
								    __UpperCamelCase
								)  ,		(
								    __UpperCamelCase
								)  ,		(
								    __UpperCamelCase
								)  ,		(
								    __UpperCamelCase
								)  ,		
								)      :  Union[str, Any]       =   config_and_inputs
								__UpperCamelCase      :  Union[str, Any]       =   {"input_ids": input_ids, "token_type_ids": token_type_ids, "attention_mask": input_mask}
								return config, inputs_dict




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




	'''simple docstring'''




	_A								=	(
	    (
	        TFDebertaVaModel,
	        TFDebertaVaForMaskedLM,
	        TFDebertaVaForQuestionAnswering,
	        TFDebertaVaForSequenceClassification,
	        TFDebertaVaForTokenClassification,
	    )
	    if is_tf_available()
	    else ()
	)
	_A								=	(
	    {
	        'feature-extraction': TFDebertaVaModel,
	        'fill-mask': TFDebertaVaForMaskedLM,
	        'question-answering': TFDebertaVaForQuestionAnswering,
	        'text-classification': TFDebertaVaForSequenceClassification,
	        'token-classification': TFDebertaVaForTokenClassification,
	        'zero-shot': TFDebertaVaForSequenceClassification,
	    }
	    if is_tf_available()
	    else {}
	)

	_A								=	False
	_A								=	False







	def   _lowerCamelCase							(   self						:Union[str, Any]		)						->					Any:
								__UpperCamelCase      :  int       =   TFDebertaVaModelTester(self		)
								__UpperCamelCase      :  int       =   ConfigTester(self	,  config_class=a	,  hidden_size=3_7		)







	def   _lowerCamelCase							(   self						:int		)						->					List[str]:
								self.config_tester.run_common_tests()







	def   _lowerCamelCase							(   self						:List[str]		)						->					List[Any]:
								__UpperCamelCase      :  Tuple       =   self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_model(*a		)







	def   _lowerCamelCase							(   self						:List[str]		)						->					Any:
								__UpperCamelCase      :  Tuple       =   self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_for_masked_lm(*a		)







	def   _lowerCamelCase							(   self						:Tuple		)						->					Any:
								__UpperCamelCase      :  List[Any]       =   self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_for_question_answering(*a		)







	def   _lowerCamelCase							(   self						:Optional[int]		)						->					Union[str, Any]:
								__UpperCamelCase      :  Optional[Any]       =   self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_for_sequence_classification(*a		)







	def   _lowerCamelCase							(   self						:Tuple		)						->					Optional[Any]:
								__UpperCamelCase      :  str       =   self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_for_token_classification(*a		)







	@slow
	def   _lowerCamelCase							(   self						:Union[str, Any]		)						->					Optional[Any]:
								__UpperCamelCase      :  Dict       =   TFDebertaVaModel.from_pretrained("kamalkraj/deberta-v2-xlarge"		)
								self.assertIsNotNone(a		)




@require_tf
class 					lowerCamelCase__				(							unittest.TestCase):




	'''simple docstring'''




	@unittest.skip(reason="Model not available yet"		)
	def   _lowerCamelCase							(   self						:List[Any]		)						->					int:
								pass







	@slow
	def   _lowerCamelCase							(   self						:List[Any]		)						->					Union[str, Any]:
								__UpperCamelCase      :  str       =   TFDebertaVaModel.from_pretrained("kamalkraj/deberta-v2-xlarge"		)
								__UpperCamelCase      :  Optional[int]       =   tf.constant([[0, 3_1_4_1_4, 2_3_2, 3_2_8, 7_4_0, 1_1_4_0, 1_2_6_9_5, 6_9, 4_6_0_7_8, 1_5_8_8, 2]]		)
								__UpperCamelCase      :  Optional[int]       =   tf.constant([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]		)
								__UpperCamelCase      :  str       =   model(a	,  attention_mask=a		)[0]

								__UpperCamelCase      :  int       =   tf.constant(
								    [[[0.2356, 0.1948, 0.0369], [-0.1063, 0.3586, -0.5152], [-0.6399, -0.0259, -0.2525]]]		)
								tf.debugging.assert_near(output[:, 1:4, 1:4]	,  a	,  atol=1E-4		)

import os
from shutil import copyfile
from typing import List, Optional, Tuple

from ...tokenization_utils import AddedToken
from ...tokenization_utils_fast import PreTrainedTokenizerFast
from ...utils import is_sentencepiece_available, logging


if is_sentencepiece_available():
		from .tokenization_fnet import FNetTokenizer
else:
		lowercase    :   List[Any]						=			None

lowercase    :   Tuple						=			logging.get_logger(__name__)
lowercase    :   Optional[Any]						=			{'vocab_file': 'spiece.model', 'tokenizer_file': 'tokenizer.json'}

lowercase    :   str						=			{
    'vocab_file': {
        'google/fnet-base': 'https://huggingface.co/google/fnet-base/resolve/main/spiece.model',
        'google/fnet-large': 'https://huggingface.co/google/fnet-large/resolve/main/spiece.model',
    },
    'tokenizer_file': {
        'google/fnet-base': 'https://huggingface.co/google/fnet-base/resolve/main/tokenizer.json',
        'google/fnet-large': 'https://huggingface.co/google/fnet-large/resolve/main/tokenizer.json',
    },
}

lowercase    :   int						=			{
    'google/fnet-base': 512,
    'google/fnet-large': 512,
}

lowercase    :   str						=			'▁'




class 					lowerCamelCase__				(							__lowercase):




	'''simple docstring'''




	_A								=	VOCAB_FILES_NAMES
	_A								=	PRETRAINED_VOCAB_FILES_MAP
	_A								=	PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
	_A								=	['input_ids', 'token_type_ids']
	_A								=	FNetTokenizer







	def __init__(   self						:Optional[int]	,  a						:Union[str, Any]=None	,  a						:Optional[int]=None	,  a						:str=False	,  a						:Tuple=True	,  a						:Optional[Any]=True	,  a						:Optional[int]="<unk>"	,  a						:List[str]="[SEP]"	,  a						:Optional[Any]="<pad>"	,  a						:int="[CLS]"	,  a						:Dict="[MASK]"	,  **a						:Optional[int]	,  )						->					List[str]:
								# Mask token behave like a normal word, i.e. include the space before it and
								# is included in the raw text, there should be a match in a non-normalized sentence.
								__UpperCamelCase      :  List[Any]       =   (
								    AddedToken(a	,  lstrip=a	,  rstrip=a	,  normalized=a		)
								    if isinstance(a	,  a		)
								    else mask_token
								)

								super().__init__(
								    a	,  tokenizer_file=a	,  do_lower_case=a	,  remove_space=a	,  keep_accents=a	,  unk_token=a	,  sep_token=a	,  pad_token=a	,  cls_token=a	,  mask_token=a	,  **a	,  )

								__UpperCamelCase      :  List[Any]       =   do_lower_case
								__UpperCamelCase      :  str       =   remove_space
								__UpperCamelCase      :  Dict       =   keep_accents
								__UpperCamelCase      :  Union[str, Any]       =   vocab_file
								__UpperCamelCase      :  List[str]       =   False if not self.vocab_file else True







	def   _lowerCamelCase							(   self						:Dict	,  a						:List[int]	,  a						:Optional[List[int]] = None		)						->					List[int]:
								__UpperCamelCase      :  List[str]       =   [self.sep_token_id]
								__UpperCamelCase      :  List[str]       =   [self.cls_token_id]
								if token_ids_a is None:
															return cls + token_ids_a + sep
								return cls + token_ids_a + sep + token_ids_a + sep







	def   _lowerCamelCase							(   self						:Union[str, Any]	,  a						:List[int]	,  a						:Optional[List[int]] = None		)						->					List[int]:
								__UpperCamelCase      :  List[Any]       =   [self.sep_token_id]
								__UpperCamelCase      :  List[Any]       =   [self.cls_token_id]

								if token_ids_a is None:
															return len(cls + token_ids_a + sep		) * [0]
								return len(cls + token_ids_a + sep		) * [0] + len(token_ids_a + sep		) * [1]







	def   _lowerCamelCase							(   self						:int	,  a						:str	,  a						:Optional[str] = None		)						->					Tuple[str]:
								if not os.path.isdir(a		):
															logger.error(f'Vocabulary path ({save_directory}) should be a directory'		)
															return
								__UpperCamelCase      :  str       =   os.path.join(
								    a	,  (filename_prefix + "-" if filename_prefix else "") + VOCAB_FILES_NAMES["vocab_file"]		)

								if os.path.abspath(self.vocab_file		) != os.path.abspath(a		):
															copyfile(self.vocab_file	,  a		)

								return (out_vocab_file,)