
'''simple docstring'''
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   snake_case__					:







		"""simple docstring"""

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






				"""simple docstring"""







				snake_case     :					Any     =	parent
				snake_case     :					Optional[Any]     =	13
				snake_case     :					Dict     =	7
				snake_case     :					Any     =	True
				snake_case     :					Optional[Any]     =	True
				snake_case     :					str     =	True
				snake_case     :					Dict     =	True
				snake_case     :					Dict     =	99
				snake_case     :					Optional[Any]     =	32
				snake_case     :					Optional[int]     =	2
				snake_case     :					Optional[int]     =	4
				snake_case     :					str     =	37
				snake_case     :					Dict     =	'''gelu'''
				snake_case     :					Tuple     =	0.1
				snake_case     :					Optional[int]     =	0.1
				snake_case     :					Optional[Any]     =	512
				snake_case     :					List[str]     =	16
				snake_case     :					str     =	False
				snake_case     :					str     =	-1
				snake_case     :					Union[str, Any]     =	True
				snake_case     :					int     =	2
				snake_case     :					Tuple     =	0.02
				snake_case     :					Optional[Any]     =	3
				snake_case     :					Any     =	4
				snake_case     :					Optional[Any]     =	None



		def 			lowerCAmelCase							(			self							: Any			)	-> List[str]:






				"""simple docstring"""







				snake_case     :					Optional[int]     =	ids_tensor([self.batch_size, self.seq_length]     ,  self.vocab_size			)

				snake_case     :					Dict     =	None
				if self.use_input_mask:
						snake_case     :					Union[str, Any]     =	random_attention_mask([self.batch_size, self.seq_length]			)

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

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

				snake_case     :					List[Any]     =	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 			lowerCAmelCase							(			self							: int     ,  UpperCamelCase__							: Optional[int]     ,  UpperCamelCase__							: Any     ,  UpperCamelCase__							: str     ,  UpperCamelCase__							: int     ,  UpperCamelCase__							: Dict     ,  UpperCamelCase__							: Dict     ,  UpperCamelCase__							: Optional[Any]			)	-> List[str]:






				"""simple docstring"""







				snake_case     :					Tuple     =	TFDebertaModel(config=UpperCamelCase__			)
				snake_case     :					Optional[Any]     =	{'''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids}

				snake_case     :					Dict     =	[input_ids, input_mask]
				snake_case     :					Optional[Any]     =	model(UpperCamelCase__			)

				snake_case     :					List[str]     =	model(UpperCamelCase__			)

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



		def 			lowerCAmelCase							(			self							: Optional[Any]     ,  UpperCamelCase__							: List[str]     ,  UpperCamelCase__							: Dict     ,  UpperCamelCase__							: List[Any]     ,  UpperCamelCase__							: List[Any]     ,  UpperCamelCase__							: Tuple     ,  UpperCamelCase__							: List[str]     ,  UpperCamelCase__							: Any			)	-> int:






				"""simple docstring"""







				snake_case     :					Dict     =	TFDebertaForMaskedLM(config=UpperCamelCase__			)
				snake_case     :					str     =	{
				    '''input_ids''': input_ids,
				    '''attention_mask''': input_mask,
				    '''token_type_ids''': token_type_ids,
				}
				snake_case     :					Any     =	model(UpperCamelCase__			)
				self.parent.assertEqual(result.logits.shape     ,  (self.batch_size, self.seq_length, self.vocab_size)			)



		def 			lowerCAmelCase							(			self							: Tuple     ,  UpperCamelCase__							: Dict     ,  UpperCamelCase__							: str     ,  UpperCamelCase__							: Optional[int]     ,  UpperCamelCase__							: List[str]     ,  UpperCamelCase__							: List[str]     ,  UpperCamelCase__							: str     ,  UpperCamelCase__							: Any			)	-> Optional[int]:






				"""simple docstring"""







				snake_case     :					Optional[int]     =	self.num_labels
				snake_case     :					Optional[int]     =	TFDebertaForSequenceClassification(config=UpperCamelCase__			)
				snake_case     :					str     =	{
				    '''input_ids''': input_ids,
				    '''attention_mask''': input_mask,
				    '''token_type_ids''': token_type_ids,
				}

				snake_case     :					List[Any]     =	model(UpperCamelCase__			)
				self.parent.assertEqual(result.logits.shape     ,  (self.batch_size, self.num_labels)			)



		def 			lowerCAmelCase							(			self							: Any     ,  UpperCamelCase__							: Optional[Any]     ,  UpperCamelCase__							: Union[str, Any]     ,  UpperCamelCase__							: Any     ,  UpperCamelCase__							: str     ,  UpperCamelCase__							: List[str]     ,  UpperCamelCase__							: Union[str, Any]     ,  UpperCamelCase__							: Optional[int]			)	-> Optional[Any]:






				"""simple docstring"""







				snake_case     :					Dict     =	self.num_labels
				snake_case     :					int     =	TFDebertaForTokenClassification(config=UpperCamelCase__			)
				snake_case     :					List[Any]     =	{
				    '''input_ids''': input_ids,
				    '''attention_mask''': input_mask,
				    '''token_type_ids''': token_type_ids,
				}
				snake_case     :					int     =	model(UpperCamelCase__			)
				self.parent.assertEqual(result.logits.shape     ,  (self.batch_size, self.seq_length, self.num_labels)			)



		def 			lowerCAmelCase							(			self							: List[Any]     ,  UpperCamelCase__							: List[str]     ,  UpperCamelCase__							: Union[str, Any]     ,  UpperCamelCase__							: Optional[int]     ,  UpperCamelCase__							: Dict     ,  UpperCamelCase__							: Optional[int]     ,  UpperCamelCase__							: Optional[Any]     ,  UpperCamelCase__							: Dict			)	-> Optional[Any]:






				"""simple docstring"""







				snake_case     :					List[Any]     =	TFDebertaForQuestionAnswering(config=UpperCamelCase__			)
				snake_case     :					Optional[Any]     =	{
				    '''input_ids''': input_ids,
				    '''attention_mask''': input_mask,
				    '''token_type_ids''': token_type_ids,
				}

				snake_case     :					List[Any]     =	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 			lowerCAmelCase							(			self							: List[Any]			)	-> str:






				"""simple docstring"""







				snake_case     :					Optional[Any]     =	self.prepare_config_and_inputs()
				(
				    (
				    snake_case
				) ,(
				    snake_case
				) ,(
				    snake_case
				) ,(
				    snake_case
				) ,(
				    snake_case
				) ,(
				    snake_case
				) ,(
				    snake_case
				) ,
				)     :					Optional[int]     =	config_and_inputs
				snake_case     :					int     =	{'''input_ids''': input_ids, '''token_type_ids''': token_type_ids, '''attention_mask''': input_mask}
				return config, inputs_dict







@require_tf
class   snake_case__					(			__SCREAMING_SNAKE_CASE						,				__SCREAMING_SNAKE_CASE						,				unittest.TestCase				):







		"""simple docstring"""

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

		lowerCamelCase           =	False
		lowerCamelCase           =	False



		def 			lowerCAmelCase							(			self							: Optional[Any]			)	-> Optional[Any]:






				"""simple docstring"""







				snake_case     :					Tuple     =	TFDebertaModelTester(self			)
				snake_case     :					Tuple     =	ConfigTester(self     ,  config_class=UpperCamelCase__     ,  hidden_size=37			)



		def 			lowerCAmelCase							(			self							: Any			)	-> Dict:






				"""simple docstring"""







				self.config_tester.run_common_tests()



		def 			lowerCAmelCase							(			self							: Optional[Any]			)	-> List[Any]:






				"""simple docstring"""







				snake_case     :					str     =	self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_model(*UpperCamelCase__			)



		def 			lowerCAmelCase							(			self							: str			)	-> str:






				"""simple docstring"""







				snake_case     :					Optional[int]     =	self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_for_masked_lm(*UpperCamelCase__			)



		def 			lowerCAmelCase							(			self							: List[str]			)	-> Optional[int]:






				"""simple docstring"""







				snake_case     :					List[str]     =	self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_for_question_answering(*UpperCamelCase__			)



		def 			lowerCAmelCase							(			self							: List[str]			)	-> Union[str, Any]:






				"""simple docstring"""







				snake_case     :					List[Any]     =	self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_for_sequence_classification(*UpperCamelCase__			)



		def 			lowerCAmelCase							(			self							: Tuple			)	-> Tuple:






				"""simple docstring"""







				snake_case     :					Dict     =	self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_for_token_classification(*UpperCamelCase__			)



		@slow
		def 			lowerCAmelCase							(			self							: Tuple			)	-> Dict:






				"""simple docstring"""







				snake_case     :					List[str]     =	TFDebertaModel.from_pretrained('''kamalkraj/deberta-base'''			)
				self.assertIsNotNone(UpperCamelCase__			)







@require_tf
class   snake_case__					(			unittest.TestCase				):







		"""simple docstring"""

		@unittest.skip(reason='''Model not available yet'''			)
		def 			lowerCAmelCase							(			self							: Tuple			)	-> List[str]:






				"""simple docstring"""







				pass



		@slow
		def 			lowerCAmelCase							(			self							: str			)	-> List[Any]:






				"""simple docstring"""







				snake_case     :					Union[str, Any]     =	TFDebertaModel.from_pretrained('''kamalkraj/deberta-base'''			)
				snake_case     :					List[Any]     =	tf.constant([[0, 3_1414, 232, 328, 740, 1140, 1_2695, 69, 4_6078, 1588, 2]]			)
				snake_case     :					str     =	tf.constant([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]			)
				snake_case     :					Tuple     =	model(UpperCamelCase__     ,  attention_mask=UpperCamelCase__			)[0]

				snake_case     :					Optional[int]     =	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			)




'''simple docstring'''
import torch

from transformers import AutoTokenizer






class   snake_case__					(			__SCREAMING_SNAKE_CASE				):







		"""simple docstring"""

		def __init__(			self							: Union[str, Any]     ,  UpperCamelCase__							: List[str]			)	-> List[str]:






				"""simple docstring"""







				snake_case     :					List[Any]     =	AutoTokenizer.from_pretrained(UpperCamelCase__			)



		def 			lowerCAmelCase							(			self							: Any     ,  UpperCamelCase__							: str			)	-> Dict:






				"""simple docstring"""







				if isinstance(UpperCamelCase__     ,  UpperCamelCase__			) and all(isinstance(_x     ,  UpperCamelCase__			) for _x in x			):
						snake_case     :					Any     =	None
						for l in x:
								snake_case     :					str     =	self.tokenizer(
								    UpperCamelCase__     ,  padding='''max_length'''     ,  max_length=384     ,  truncation=UpperCamelCase__     ,  return_tensors='''pt'''     ,  )
								snake_case     :					Tuple     =	torch.tensor([len(UpperCamelCase__			)]			)
								if d is not None:
										for k in d.keys():
												snake_case     :					int     =	torch.cat((d[k], t[k])     ,  0			)
								else:
										snake_case     :					List[str]     =	t

						snake_case     :					Union[str, Any]     =	torch.tensor(self.tokenizer.convert_tokens_to_ids('''[E]'''			)			)
						snake_case     :					int     =	torch.tensor(self.tokenizer.convert_tokens_to_ids('''[/E]'''			)			)

				elif isinstance(UpperCamelCase__     ,  UpperCamelCase__			) and all(isinstance(_x     ,  UpperCamelCase__			) for _x in x			):
						snake_case     :					Optional[int]     =	self.tokenizer(
						    UpperCamelCase__     ,  padding='''max_length'''     ,  max_length=384     ,  truncation=UpperCamelCase__     ,  return_tensors='''pt'''     ,  )

				else:
						raise Exception(
						    '''Type of parameter x was not recognized! Only `list of strings` for query or `list of lists of'''
						    ''' strings` for supports are supported.'''			)

				return d



		def 			lowerCAmelCase							(			self							: int     ,  UpperCamelCase__							: List[Any]     ,  UpperCamelCase__							: List[Any]     ,  UpperCamelCase__							: Any     ,  UpperCamelCase__							: Tuple     ,  UpperCamelCase__							: Any=0.70			)	-> Dict:






				"""simple docstring"""







				snake_case     :					str     =	[]
				for idx in range(len(W_query['''input_ids''']			)			):
						snake_case     :					Optional[int]     =	range(p_start.shape[1]			)

						snake_case     :					str     =	[]
						for start_id in start_indexes:
								for end_id in end_indexes:
										if start_id < end_id:
												output.append(
												    (
												        start_id,
												        end_id,
												        p_start[idx][start_id].item(),
												        p_end[idx][end_id].item(),
												    )			)

						output.sort(key=lambda UpperCamelCase__			: (tup[2] * tup[3])     ,  reverse=UpperCamelCase__			)
						snake_case     :					int     =	[]
						for k in range(len(UpperCamelCase__			)			):
								if output[k][2] * output[k][3] >= thresh:
										snake_case ,snake_case     :					Tuple     =	output[k][0], output[k][1]
										snake_case     :					str     =	self.tokenizer.decode(W_query['''input_ids'''][idx][c_start_pos:c_end_pos]			)
										temp.append((decoded, output[k][2] * output[k][3])			)

						final_outputs.append(UpperCamelCase__			)

				return final_outputs


