






"""simple docstring"""

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							:	Optional[Any]						,			a_							:	Dict						,			a_							:	Any=13						,			a_							:	Tuple=7						,			a_							:	Union[str, Any]=True						,			a_							:	Optional[int]=True						,			a_							:	str=True						,			a_							:	Any=True						,			a_							:	Union[str, Any]=99						,			a_							:	Tuple=32						,			a_							:	Any=2						,			a_							:	int=4						,			a_							:	List[str]=37						,			a_							:	Optional[int]="gelu"						,			a_							:	Union[str, Any]=0.1						,			a_							:	Any=0.1						,			a_							:	Optional[int]=5_12						,			a_							:	Union[str, Any]=16						,			a_							:	Optional[int]=2						,			a_							:	str=0.02						,			a_							:	Optional[Any]=False						,			a_							:	Tuple=True						,			a_							:	Any="None"						,			a_							:	List[Any]=3						,			a_							:	Optional[Any]=4						,			a_							:	Any=None						,			):
				lowerCAmelCase_   :							List[Any]									=			parent
				lowerCAmelCase_   :							Tuple									=			batch_size
				lowerCAmelCase_   :							Union[str, Any]									=			seq_length
				lowerCAmelCase_   :							Dict									=			is_training
				lowerCAmelCase_   :							Dict									=			use_input_mask
				lowerCAmelCase_   :							Union[str, Any]									=			use_token_type_ids
				lowerCAmelCase_   :							Any									=			use_labels
				lowerCAmelCase_   :							int									=			vocab_size
				lowerCAmelCase_   :							Tuple									=			hidden_size
				lowerCAmelCase_   :							int									=			num_hidden_layers
				lowerCAmelCase_   :							List[Any]									=			num_attention_heads
				lowerCAmelCase_   :							List[str]									=			intermediate_size
				lowerCAmelCase_   :							List[Any]									=			hidden_act
				lowerCAmelCase_   :							Any									=			hidden_dropout_prob
				lowerCAmelCase_   :							str									=			attention_probs_dropout_prob
				lowerCAmelCase_   :							Union[str, Any]									=			max_position_embeddings
				lowerCAmelCase_   :							Any									=			type_vocab_size
				lowerCAmelCase_   :							Tuple									=			type_sequence_label_size
				lowerCAmelCase_   :							str									=			initializer_range
				lowerCAmelCase_   :							Tuple									=			num_labels
				lowerCAmelCase_   :							int									=			num_choices
				lowerCAmelCase_   :							Optional[int]									=			relative_attention
				lowerCAmelCase_   :							List[Any]									=			position_biased_input
				lowerCAmelCase_   :							Optional[Any]									=			pos_att_type
				lowerCAmelCase_   :							Dict									=			scope





		def    lowerCamelCase       (   self							:	Dict	):
				lowerCAmelCase_   :							Union[str, Any]									=			ids_tensor([self.batch_size, self.seq_length]						,			self.vocab_size	)

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

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

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

				lowerCAmelCase_   :							Tuple									=			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=__lowerCamelCase						,			)

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





		def    lowerCamelCase       (   self							:	Tuple						,			a_							:	str						,			a_							:	Optional[int]						,			a_							:	Optional[int]						,			a_							:	List[Any]						,			a_							:	Any						,			a_							:	Optional[int]						,			a_							:	List[Any]	):
				lowerCAmelCase_   :							Optional[int]									=			TFDebertaVaModel(config=__lowerCamelCase	)
				lowerCAmelCase_   :							List[str]									=			{"""input_ids""": input_ids, """attention_mask""": input_mask, """token_type_ids""": token_type_ids}

				lowerCAmelCase_   :							Optional[int]									=			[input_ids, input_mask]
				lowerCAmelCase_   :							str									=			model(__lowerCamelCase	)

				lowerCAmelCase_   :							Optional[Any]									=			model(__lowerCamelCase	)

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





		def    lowerCamelCase       (   self							:	Optional[int]						,			a_							:	List[str]						,			a_							:	str						,			a_							:	Optional[Any]						,			a_							:	Any						,			a_							:	Dict						,			a_							:	List[str]						,			a_							:	Dict	):
				lowerCAmelCase_   :							Optional[Any]									=			TFDebertaVaForMaskedLM(config=__lowerCamelCase	)
				lowerCAmelCase_   :							Optional[int]									=			{
				    """input_ids""": input_ids,
				    """attention_mask""": input_mask,
				    """token_type_ids""": token_type_ids,
				}
				lowerCAmelCase_   :							Union[str, Any]									=			model(__lowerCamelCase	)
				self.parent.assertEqual(result.logits.shape						,			(self.batch_size, self.seq_length, self.vocab_size)	)





		def    lowerCamelCase       (   self							:	List[str]						,			a_							:	Union[str, Any]						,			a_							:	Optional[int]						,			a_							:	List[Any]						,			a_							:	Union[str, Any]						,			a_							:	str						,			a_							:	Any						,			a_							:	Dict	):
				lowerCAmelCase_   :							Optional[Any]									=			self.num_labels
				lowerCAmelCase_   :							Optional[int]									=			TFDebertaVaForSequenceClassification(config=__lowerCamelCase	)
				lowerCAmelCase_   :							Optional[int]									=			{
				    """input_ids""": input_ids,
				    """attention_mask""": input_mask,
				    """token_type_ids""": token_type_ids,
				}

				lowerCAmelCase_   :							Tuple									=			model(__lowerCamelCase	)
				self.parent.assertEqual(result.logits.shape						,			(self.batch_size, self.num_labels)	)





		def    lowerCamelCase       (   self							:	Union[str, Any]						,			a_							:	int						,			a_							:	Tuple						,			a_							:	Optional[int]						,			a_							:	Tuple						,			a_							:	Dict						,			a_							:	Union[str, Any]						,			a_							:	List[str]	):
				lowerCAmelCase_   :							int									=			self.num_labels
				lowerCAmelCase_   :							Optional[Any]									=			TFDebertaVaForTokenClassification(config=__lowerCamelCase	)
				lowerCAmelCase_   :							Dict									=			{
				    """input_ids""": input_ids,
				    """attention_mask""": input_mask,
				    """token_type_ids""": token_type_ids,
				}
				lowerCAmelCase_   :							int									=			model(__lowerCamelCase	)
				self.parent.assertEqual(result.logits.shape						,			(self.batch_size, self.seq_length, self.num_labels)	)





		def    lowerCamelCase       (   self							:	Tuple						,			a_							:	List[str]						,			a_							:	int						,			a_							:	Optional[int]						,			a_							:	str						,			a_							:	Optional[int]						,			a_							:	str						,			a_							:	Dict	):
				lowerCAmelCase_   :							Optional[Any]									=			TFDebertaVaForQuestionAnswering(config=__lowerCamelCase	)
				lowerCAmelCase_   :							Optional[int]									=			{
				    """input_ids""": input_ids,
				    """attention_mask""": input_mask,
				    """token_type_ids""": token_type_ids,
				}

				lowerCAmelCase_   :							Dict									=			model(__lowerCamelCase	)
				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							:	Any	):
				lowerCAmelCase_   :							List[Any]									=			self.prepare_config_and_inputs()
				(
				    lowerCAmelCase_      
				)   :							str									=			config_and_inputs
				lowerCAmelCase_   :							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				( _a ,							_a ,							unittest.TestCase			):





		'''simple docstring'''






		a_	:				List[str]            =							(
		    (
		        TFDebertaVaModel,
		        TFDebertaVaForMaskedLM,
		        TFDebertaVaForQuestionAnswering,
		        TFDebertaVaForSequenceClassification,
		        TFDebertaVaForTokenClassification,
		    )
		    if is_tf_available()
		    else ()
		)
		a_	:				Optional[Any]            =							(
		    {
		        """feature-extraction""": TFDebertaVaModel,
		        """fill-mask""": TFDebertaVaForMaskedLM,
		        """question-answering""": TFDebertaVaForQuestionAnswering,
		        """text-classification""": TFDebertaVaForSequenceClassification,
		        """token-classification""": TFDebertaVaForTokenClassification,
		        """zero-shot""": TFDebertaVaForSequenceClassification,
		    }
		    if is_tf_available()
		    else {}
		)

		a_	:				Any            =							False
		a_	:				Union[str, Any]            =							False





		def    lowerCamelCase       (   self							:	Any	):
				lowerCAmelCase_   :							Tuple									=			TFDebertaVaModelTester(self	)
				lowerCAmelCase_   :							Optional[int]									=			ConfigTester(self						,			config_class=__lowerCamelCase						,			hidden_size=37	)





		def    lowerCamelCase       (   self							:	Optional[int]	):
				self.config_tester.run_common_tests()





		def    lowerCamelCase       (   self							:	str	):
				lowerCAmelCase_   :							List[str]									=			self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_model(*__lowerCamelCase	)





		def    lowerCamelCase       (   self							:	Optional[Any]	):
				lowerCAmelCase_   :							int									=			self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_for_masked_lm(*__lowerCamelCase	)





		def    lowerCamelCase       (   self							:	Tuple	):
				lowerCAmelCase_   :							Optional[Any]									=			self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_for_question_answering(*__lowerCamelCase	)





		def    lowerCamelCase       (   self							:	Optional[int]	):
				lowerCAmelCase_   :							Dict									=			self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_for_sequence_classification(*__lowerCamelCase	)





		def    lowerCamelCase       (   self							:	Tuple	):
				lowerCAmelCase_   :							int									=			self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_for_token_classification(*__lowerCamelCase	)





		@slow
		def    lowerCamelCase       (   self							:	Tuple	):
				lowerCAmelCase_   :							Optional[int]									=			TFDebertaVaModel.from_pretrained("kamalkraj/deberta-v2-xlarge"	)
				self.assertIsNotNone(__lowerCamelCase	)







@require_tf
class 		__lowerCamelCase				( unittest.TestCase			):





		'''simple docstring'''






		@unittest.skip(reason="Model not available yet"	)
		def    lowerCamelCase       (   self							:	Union[str, Any]	):
				pass





		@slow
		def    lowerCamelCase       (   self							:	Optional[Any]	):
				lowerCAmelCase_   :							Dict									=			TFDebertaVaModel.from_pretrained("kamalkraj/deberta-v2-xlarge"	)
				lowerCAmelCase_   :							Any									=			tf.constant([[0, 3_14_14, 2_32, 3_28, 7_40, 11_40, 1_26_95, 69, 4_60_78, 15_88, 2]]	)
				lowerCAmelCase_   :							Optional[Any]									=			tf.constant([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]	)
				lowerCAmelCase_   :							Optional[Any]									=			model(__lowerCamelCase						,			attention_mask=__lowerCamelCase	)[0]

				lowerCAmelCase_   :							Tuple									=			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]						,			__lowerCamelCase						,			atol=1e-4	)








import os
import unittest

from transformers.models.transfo_xl.tokenization_transfo_xl import VOCAB_FILES_NAMES, TransfoXLTokenizer

from ...test_tokenization_common import TokenizerTesterMixin

class     _SCREAMING_SNAKE_CASE       (			_a		,   unittest.TestCase      ):
	snake_case__							:     Optional[Any]     	=  TransfoXLTokenizer
	snake_case__							:     List[Any]     	=  False
	snake_case__							:     Tuple     	=  False





	def 						_A (		self				:					str	):
		super().setUp()

		UpperCamelCase     :Dict          =	[
		    """<unk>""",
		    """[CLS]""",
		    """[SEP]""",
		    """want""",
		    """unwanted""",
		    """wa""",
		    """un""",
		    """running""",
		    """,""",
		    """low""",
		    """l""",
		]
		UpperCamelCase     :str          =	os.path.join(self.tmpdirname ,  VOCAB_FILES_NAMES["""vocab_file"""]	)
		with open(self.vocab_file ,  """w""" ,  encoding="""utf-8"""	) as vocab_writer:
			vocab_writer.write("""""".join([x + """\n""" for x in vocab_tokens]	)	)





	def 						_A (		self				:					List[str] ,  **__lowerCamelCase				:					Any	):
		UpperCamelCase     :Any          =	True
		return TransfoXLTokenizer.from_pretrained(self.tmpdirname ,  **__lowerCamelCase	)





	def 						_A (		self				:					Any ,  __lowerCamelCase				:					int	):
		UpperCamelCase     :List[Any]          =	"""<unk> UNwanted , running"""
		UpperCamelCase     :int          =	"""<unk> unwanted, running"""
		return input_text, output_text





	def 						_A (		self				:					Tuple	):
		UpperCamelCase     :List[str]          =	TransfoXLTokenizer(vocab_file=self.vocab_file ,  lower_case=__lowerCamelCase	)

		UpperCamelCase     :Any          =	tokenizer.tokenize("""<unk> UNwanted , running"""	)
		self.assertListEqual(__lowerCamelCase ,  ["""<unk>""", """unwanted""", """,""", """running"""]	)

		self.assertListEqual(tokenizer.convert_tokens_to_ids(__lowerCamelCase	) ,  [0, 4, 8, 7]	)





	def 						_A (		self				:					Optional[Any]	):
		UpperCamelCase     :List[Any]          =	TransfoXLTokenizer(lower_case=__lowerCamelCase	)

		self.assertListEqual(
		    tokenizer.tokenize(""" \tHeLLo ! how  \n Are yoU ?  """	) ,  ["""hello""", """!""", """how""", """are""", """you""", """?"""]	)





	def 						_A (		self				:					Union[str, Any]	):
		UpperCamelCase     :int          =	TransfoXLTokenizer(lower_case=__lowerCamelCase	)

		self.assertListEqual(
		    tokenizer.tokenize(""" \tHeLLo ! how  \n Are yoU ?  """	) ,  ["""HeLLo""", """!""", """how""", """Are""", """yoU""", """?"""]	)





	def 						_A (		self				:					Tuple	):
		UpperCamelCase     :Any          =	TransfoXLTokenizer(lower_case=__lowerCamelCase	)
		UpperCamelCase     :Optional[int]          =	"""Hello (bracket) and side-scrolled [and] Henry's $5,000 with 3.34 m. What's up!?"""
		UpperCamelCase     :Optional[int]          =	[
		    """Hello""",
		    """(""",
		    """bracket""",
		    """)""",
		    """and""",
		    """side""",
		    """@-@""",
		    """scrolled""",
		    """[""",
		    """and""",
		    """]""",
		    """Henry""",
		    """'s""",
		    """$""",
		    """5""",
		    """@,@""",
		    """000""",
		    """with""",
		    """3""",
		    """@.@""",
		    """34""",
		    """m""",
		    """.""",
		    """What""",
		    """'s""",
		    """up""",
		    """!""",
		    """?""",
		]

		self.assertListEqual(tokenizer.tokenize(__lowerCamelCase	) ,  __lowerCamelCase	)

		self.assertEqual(tokenizer.convert_tokens_to_string(__lowerCamelCase	) ,  __lowerCamelCase	)





	def 						_A (		self				:					List[Any]	):
		UpperCamelCase     :Any          =	self.get_tokenizer()
		UpperCamelCase     :List[str]          =	len(__lowerCamelCase	)

		tokenizer.add_tokens(["""new1""", """new2"""]	)
		tokenizer.move_added_token("""new1""" ,  1	)

		# Check that moved token is not copied (duplicate)
		self.assertEqual(len(__lowerCamelCase	) ,  original_len + 2	)
		# Check that token is moved to specified id
		self.assertEqual(tokenizer.encode("""new1"""	) ,  [1]	)
		self.assertEqual(tokenizer.decode([1]	) ,  """new1"""	)
