


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







				"""simple docstring"""



				def __init__(self       ,			__lowercase       ,			__lowercase=13       ,			__lowercase=7       ,			__lowercase=True       ,			__lowercase=True       ,			__lowercase=True       ,			__lowercase=True       ,			__lowercase=99       ,			__lowercase=32       ,			__lowercase=2       ,			__lowercase=4       ,			__lowercase=37       ,			__lowercase="gelu"       ,			__lowercase=0.1       ,			__lowercase=0.1       ,			__lowercase=5_12       ,			__lowercase=16       ,			__lowercase=2       ,			__lowercase=0.0_2       ,			__lowercase=False       ,			__lowercase=True       ,			__lowercase="None"       ,			__lowercase=3       ,			__lowercase=4       ,			__lowercase=None       ,			):
										__lowerCAmelCase							       =				parent
										__lowerCAmelCase							       =				batch_size
										__lowerCAmelCase							       =				seq_length
										__lowerCAmelCase							       =				is_training
										__lowerCAmelCase							       =				use_input_mask
										__lowerCAmelCase							       =				use_token_type_ids
										__lowerCAmelCase							       =				use_labels
										__lowerCAmelCase							       =				vocab_size
										__lowerCAmelCase							       =				hidden_size
										__lowerCAmelCase							       =				num_hidden_layers
										__lowerCAmelCase							       =				num_attention_heads
										__lowerCAmelCase							       =				intermediate_size
										__lowerCAmelCase							       =				hidden_act
										__lowerCAmelCase							       =				hidden_dropout_prob
										__lowerCAmelCase							       =				attention_probs_dropout_prob
										__lowerCAmelCase							       =				max_position_embeddings
										__lowerCAmelCase							       =				type_vocab_size
										__lowerCAmelCase							       =				type_sequence_label_size
										__lowerCAmelCase							       =				initializer_range
										__lowerCAmelCase							       =				num_labels
										__lowerCAmelCase							       =				num_choices
										__lowerCAmelCase							       =				relative_attention
										__lowerCAmelCase							       =				position_biased_input
										__lowerCAmelCase							       =				pos_att_type
										__lowerCAmelCase							       =				scope




				def 					_snake_case  (self		):
										__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							       =				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=snake_case_       ,			)

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




				def 					_snake_case  (self       ,			__lowercase       ,			__lowercase       ,			__lowercase       ,			__lowercase       ,			__lowercase       ,			__lowercase       ,			__lowercase		):
										__lowerCAmelCase							       =				TFDebertaVaModel(config=snake_case_		)
										__lowerCAmelCase							       =				{"input_ids": input_ids, "attention_mask": input_mask, "token_type_ids": token_type_ids}

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

										__lowerCAmelCase							       =				model(snake_case_		)

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




				def 					_snake_case  (self       ,			__lowercase       ,			__lowercase       ,			__lowercase       ,			__lowercase       ,			__lowercase       ,			__lowercase       ,			__lowercase		):
										__lowerCAmelCase							       =				TFDebertaVaForMaskedLM(config=snake_case_		)
										__lowerCAmelCase							       =				{
										    "input_ids": input_ids,
										    "attention_mask": input_mask,
										    "token_type_ids": token_type_ids,
										}
										__lowerCAmelCase							       =				model(snake_case_		)
										self.parent.assertEqual(result.logits.shape       ,			(self.batch_size, self.seq_length, self.vocab_size)		)




				def 					_snake_case  (self       ,			__lowercase       ,			__lowercase       ,			__lowercase       ,			__lowercase       ,			__lowercase       ,			__lowercase       ,			__lowercase		):
										__lowerCAmelCase							       =				self.num_labels
										__lowerCAmelCase							       =				TFDebertaVaForSequenceClassification(config=snake_case_		)
										__lowerCAmelCase							       =				{
										    "input_ids": input_ids,
										    "attention_mask": input_mask,
										    "token_type_ids": token_type_ids,
										}

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




				def 					_snake_case  (self       ,			__lowercase       ,			__lowercase       ,			__lowercase       ,			__lowercase       ,			__lowercase       ,			__lowercase       ,			__lowercase		):
										__lowerCAmelCase							       =				self.num_labels
										__lowerCAmelCase							       =				TFDebertaVaForTokenClassification(config=snake_case_		)
										__lowerCAmelCase							       =				{
										    "input_ids": input_ids,
										    "attention_mask": input_mask,
										    "token_type_ids": token_type_ids,
										}
										__lowerCAmelCase							       =				model(snake_case_		)
										self.parent.assertEqual(result.logits.shape       ,			(self.batch_size, self.seq_length, self.num_labels)		)




				def 					_snake_case  (self       ,			__lowercase       ,			__lowercase       ,			__lowercase       ,			__lowercase       ,			__lowercase       ,			__lowercase       ,			__lowercase		):
										__lowerCAmelCase							       =				TFDebertaVaForQuestionAnswering(config=snake_case_		)
										__lowerCAmelCase							       =				{
										    "input_ids": input_ids,
										    "attention_mask": input_mask,
										    "token_type_ids": token_type_ids,
										}

										__lowerCAmelCase							       =				model(snake_case_		)
										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		):
										__lowerCAmelCase							       =				self.prepare_config_and_inputs()
										(
										    __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       a__				( _snake_case							,						_snake_case							,						unittest.TestCase      ):







				"""simple docstring"""



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

				__UpperCamelCase    :		Dict	=	False
				__UpperCamelCase    :		Optional[int]	=	False




				def 					_snake_case  (self		):
										__lowerCAmelCase							       =				TFDebertaVaModelTester(self		)
										__lowerCAmelCase							       =				ConfigTester(self       ,			config_class=snake_case_       ,			hidden_size=37		)




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




				def 					_snake_case  (self		):
										__lowerCAmelCase							       =				self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_model(*snake_case_		)




				def 					_snake_case  (self		):
										__lowerCAmelCase							       =				self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_for_masked_lm(*snake_case_		)




				def 					_snake_case  (self		):
										__lowerCAmelCase							       =				self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_for_question_answering(*snake_case_		)




				def 					_snake_case  (self		):
										__lowerCAmelCase							       =				self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_for_sequence_classification(*snake_case_		)




				def 					_snake_case  (self		):
										__lowerCAmelCase							       =				self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_for_token_classification(*snake_case_		)




				@slow
				def 					_snake_case  (self		):
										__lowerCAmelCase							       =				TFDebertaVaModel.from_pretrained('''kamalkraj/deberta-v2-xlarge'''		)
										self.assertIsNotNone(snake_case_		)


@require_tf
class       a__				( unittest.TestCase      ):







				"""simple docstring"""



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




				@slow
				def 					_snake_case  (self		):
										__lowerCAmelCase							       =				TFDebertaVaModel.from_pretrained('''kamalkraj/deberta-v2-xlarge'''		)
										__lowerCAmelCase							       =				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							       =				tf.constant([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]		)
										__lowerCAmelCase							       =				model(snake_case_       ,			attention_mask=snake_case_		)[0]

										__lowerCAmelCase							       =				tf.constant(
										    [[[0.2_3_5_6, 0.1_9_4_8, 0.0_3_6_9], [-0.1_0_6_3, 0.3_5_8_6, -0.5_1_5_2], [-0.6_3_9_9, -0.0_2_5_9, -0.2_5_2_5]]]		)
										tf.debugging.assert_near(output[:, 1:4, 1:4]       ,			snake_case_       ,			atol=1e-4		)





'''simple docstring'''

import json
import os
import unittest

from transformers import MgpstrTokenizer
from transformers.models.mgp_str.tokenization_mgp_str import VOCAB_FILES_NAMES
from transformers.testing_utils import require_tokenizers

from ...test_tokenization_common import TokenizerTesterMixin


@require_tokenizers
class       a__				( __A							,						unittest.TestCase      ):







				"""simple docstring"""



				__UpperCamelCase    :		int	=	MgpstrTokenizer
				__UpperCamelCase    :		Optional[Any]	=	False
				__UpperCamelCase    :		Optional[int]	=	{}
				__UpperCamelCase    :		List[Any]	=	False




				def 					_snake_case  (self		):
										super().setUp()

										# fmt: off
										__lowerCAmelCase							       =				['''[GO]''', '''[s]''', '''0''', '''1''', '''2''', '''3''', '''4''', '''5''', '''6''', '''7''', '''8''', '''9''', '''a''', '''b''', '''c''', '''d''', '''e''', '''f''', '''g''', '''h''', '''i''', '''j''', '''k''', '''l''', '''m''', '''n''', '''o''', '''p''', '''q''', '''r''', '''s''', '''t''', '''u''', '''v''', '''w''', '''x''', '''y''', '''z''']
										# fmt: on
										__lowerCAmelCase							       =				dict(zip(__lowercase       ,			range(len(__lowercase		)		)		)		)

										__lowerCAmelCase							       =				os.path.join(self.tmpdirname       ,			VOCAB_FILES_NAMES['''vocab_file''']		)
										with open(self.vocab_file       ,			'''w'''       ,			encoding='''utf-8'''		) as fp:
																fp.write(json.dumps(__lowercase		) + '''\n'''		)




				def 					_snake_case  (self       ,			**__lowercase		):
										return MgpstrTokenizer.from_pretrained(self.tmpdirname       ,			**__lowercase		)




				def 					_snake_case  (self       ,			__lowercase		):
										__lowerCAmelCase							       =				'''tester'''
										__lowerCAmelCase							       =				'''tester'''
										return input_text, output_text




				@unittest.skip('''MGP-STR always lower cases letters.'''		)
				def 					_snake_case  (self		):
										pass




				def 					_snake_case  (self		):
										__lowerCAmelCase							       =				self.get_tokenizers(do_lower_case=__lowercase		)
										for tokenizer in tokenizers:
																with self.subTest(F"""{tokenizer.__class__.__name__}"""		):
																						__lowerCAmelCase							       =				'''[SPECIAL_TOKEN]'''

																						tokenizer.add_special_tokens({'''cls_token''': special_token}		)
																						__lowerCAmelCase							       =				tokenizer.encode([special_token]       ,			add_special_tokens=__lowercase		)
																						self.assertEqual(len(__lowercase		)       ,			1		)

																						__lowerCAmelCase							       =				tokenizer.decode(__lowercase       ,			skip_special_tokens=__lowercase		)
																						self.assertTrue(special_token not in decoded		)




				def 					_snake_case  (self		):
										__lowerCAmelCase							       =				self.get_tokenizers()
										for tokenizer in tokenizers:
																with self.subTest(F"""{tokenizer.__class__.__name__}"""		):
																						__lowerCAmelCase       ,					__lowerCAmelCase							       =				self.get_input_output_texts(__lowercase		)

																						__lowerCAmelCase							       =				tokenizer.tokenize(__lowercase		)
																						__lowerCAmelCase							       =				tokenizer.convert_tokens_to_ids(__lowercase		)
																						__lowerCAmelCase							       =				tokenizer.encode(__lowercase       ,			add_special_tokens=__lowercase		)
																						self.assertListEqual(__lowercase       ,			__lowercase		)

																						__lowerCAmelCase							       =				tokenizer.convert_ids_to_tokens(__lowercase		)
																						self.assertNotEqual(len(__lowercase		)       ,			0		)
																						__lowerCAmelCase							       =				tokenizer.decode(__lowercase		)
																						self.assertIsInstance(__lowercase       ,			__lowercase		)

																						self.assertEqual(text_a.replace(''' '''       ,			''''''		)       ,			__lowercase		)




				@unittest.skip('''MGP-STR tokenizer only handles one sequence.'''		)
				def 					_snake_case  (self		):
										pass




				@unittest.skip('''inputs cannot be pretokenized in MgpstrTokenizer'''		)
				def 					_snake_case  (self		):
										pass

