

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







			'''simple docstring'''

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







			def 				snake_case__(     self :				Dict							)      ->Optional[int]:
									snake_case_   =	ids_tensor([self.batch_size, self.seq_length]							,   self.vocab_size							)

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

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

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

									snake_case_   =	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=_UpperCamelCase							,   )

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







			def 				snake_case__(     self :				Tuple							,   _UpperCamelCase :				List[Any]							,   _UpperCamelCase :				Tuple							,   _UpperCamelCase :				int							,   _UpperCamelCase :				Tuple							,   _UpperCamelCase :				List[str]							,   _UpperCamelCase :				Optional[Any]							,   _UpperCamelCase :				Any							)      ->List[Any]:
									snake_case_   =	TFDebertaVaModel(config=_UpperCamelCase							)
									snake_case_   =	{'''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids}

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

									snake_case_   =	model(_UpperCamelCase							)

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







			def 				snake_case__(     self :				List[Any]							,   _UpperCamelCase :				Union[str, Any]							,   _UpperCamelCase :				Union[str, Any]							,   _UpperCamelCase :				Any							,   _UpperCamelCase :				str							,   _UpperCamelCase :				List[Any]							,   _UpperCamelCase :				Dict							,   _UpperCamelCase :				List[str]							)      ->Optional[int]:
									snake_case_   =	TFDebertaVaForMaskedLM(config=_UpperCamelCase							)
									snake_case_   =	{
									    '''input_ids''': input_ids,
									    '''attention_mask''': input_mask,
									    '''token_type_ids''': token_type_ids,
									}
									snake_case_   =	model(_UpperCamelCase							)
									self.parent.assertEqual(result.logits.shape							,   (self.batch_size, self.seq_length, self.vocab_size)							)







			def 				snake_case__(     self :				Any							,   _UpperCamelCase :				Dict							,   _UpperCamelCase :				int							,   _UpperCamelCase :				Union[str, Any]							,   _UpperCamelCase :				Union[str, Any]							,   _UpperCamelCase :				List[str]							,   _UpperCamelCase :				Any							,   _UpperCamelCase :				Dict							)      ->str:
									snake_case_   =	self.num_labels
									snake_case_   =	TFDebertaVaForSequenceClassification(config=_UpperCamelCase							)
									snake_case_   =	{
									    '''input_ids''': input_ids,
									    '''attention_mask''': input_mask,
									    '''token_type_ids''': token_type_ids,
									}

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







			def 				snake_case__(     self :				Any							,   _UpperCamelCase :				str							,   _UpperCamelCase :				Optional[Any]							,   _UpperCamelCase :				Optional[Any]							,   _UpperCamelCase :				Union[str, Any]							,   _UpperCamelCase :				str							,   _UpperCamelCase :				Tuple							,   _UpperCamelCase :				str							)      ->List[Any]:
									snake_case_   =	self.num_labels
									snake_case_   =	TFDebertaVaForTokenClassification(config=_UpperCamelCase							)
									snake_case_   =	{
									    '''input_ids''': input_ids,
									    '''attention_mask''': input_mask,
									    '''token_type_ids''': token_type_ids,
									}
									snake_case_   =	model(_UpperCamelCase							)
									self.parent.assertEqual(result.logits.shape							,   (self.batch_size, self.seq_length, self.num_labels)							)







			def 				snake_case__(     self :				int							,   _UpperCamelCase :				str							,   _UpperCamelCase :				Tuple							,   _UpperCamelCase :				Union[str, Any]							,   _UpperCamelCase :				Tuple							,   _UpperCamelCase :				List[str]							,   _UpperCamelCase :				Optional[Any]							,   _UpperCamelCase :				List[str]							)      ->List[str]:
									snake_case_   =	TFDebertaVaForQuestionAnswering(config=_UpperCamelCase							)
									snake_case_   =	{
									    '''input_ids''': input_ids,
									    '''attention_mask''': input_mask,
									    '''token_type_ids''': token_type_ids,
									}

									snake_case_   =	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 				snake_case__(     self :				Any							)      ->Tuple:
									snake_case_   =	self.prepare_config_and_inputs()
									(
									    (
									    snake_case_
									),    (
									    snake_case_
									),    (
									    snake_case_
									),    (
									    snake_case_
									),    (
									    snake_case_
									),    (
									    snake_case_
									),    (
									    snake_case_
									),    
									)   =	config_and_inputs
									snake_case_   =	{'''input_ids''': input_ids, '''token_type_ids''': token_type_ids, '''attention_mask''': input_mask}
									return config, inputs_dict






@require_tf
class 	snake_case_   (		__A	,       __A	,       unittest.TestCase					):







			'''simple docstring'''

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

			SCREAMING_SNAKE_CASE     :   Any									=				False
			SCREAMING_SNAKE_CASE     :   int									=				False







			def 				snake_case__(     self :				Union[str, Any]							)      ->int:
									snake_case_   =	TFDebertaVaModelTester(self							)
									snake_case_   =	ConfigTester(self							,   config_class=_UpperCamelCase							,   hidden_size=3_7							)







			def 				snake_case__(     self :				str							)      ->Dict:
									self.config_tester.run_common_tests()







			def 				snake_case__(     self :				Optional[int]							)      ->List[Any]:
									snake_case_   =	self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_model(*_UpperCamelCase							)







			def 				snake_case__(     self :				Union[str, Any]							)      ->List[Any]:
									snake_case_   =	self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_for_masked_lm(*_UpperCamelCase							)







			def 				snake_case__(     self :				Union[str, Any]							)      ->Tuple:
									snake_case_   =	self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_for_question_answering(*_UpperCamelCase							)







			def 				snake_case__(     self :				Tuple							)      ->int:
									snake_case_   =	self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_for_sequence_classification(*_UpperCamelCase							)







			def 				snake_case__(     self :				Any							)      ->Dict:
									snake_case_   =	self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_for_token_classification(*_UpperCamelCase							)







			@slow
			def 				snake_case__(     self :				Any							)      ->List[str]:
									snake_case_   =	TFDebertaVaModel.from_pretrained('''kamalkraj/deberta-v2-xlarge'''							)
									self.assertIsNotNone(_UpperCamelCase							)






@require_tf
class 	snake_case_   (		unittest.TestCase					):







			'''simple docstring'''

			@unittest.skip(reason='''Model not available yet'''							)
			def 				snake_case__(     self :				Tuple							)      ->int:
									pass







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

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


import functools
import gc
import inspect

import torch

from .imports import is_npu_available, is_xpu_available


def   __SCREAMING_SNAKE_CASE					(*SCREAMING_SNAKE_CASE__       ):
						if not isinstance(SCREAMING_SNAKE_CASE__  ,		SCREAMING_SNAKE_CASE__       ):
												snake_case_   =	list(SCREAMING_SNAKE_CASE__       )
						for i in range(len(SCREAMING_SNAKE_CASE__       )       ):
												snake_case_   =	None
						gc.collect()
						if is_xpu_available():
												torch.xpu.empty_cache()
						elif is_npu_available():
												torch.npu.empty_cache()
						else:
												torch.cuda.empty_cache()
						return objects


def   __SCREAMING_SNAKE_CASE					(SCREAMING_SNAKE_CASE__       ):
						snake_case_   =	[
						    '''CUDA out of memory.''',  # CUDA OOM
						    '''cuDNN error: CUDNN_STATUS_NOT_SUPPORTED.''',  # CUDNN SNAFU
						    '''DefaultCPUAllocator: can\'t allocate memory''',  # CPU OOM
						]
						if isinstance(SCREAMING_SNAKE_CASE__  ,		SCREAMING_SNAKE_CASE__       ) and len(exception.args       ) == 1:
												return any(err in exception.args[0] for err in _statements       )
						return False


def   __SCREAMING_SNAKE_CASE					(SCREAMING_SNAKE_CASE__ = None  ,		SCREAMING_SNAKE_CASE__ = 128       ):
						if function is None:
												return functools.partial(SCREAMING_SNAKE_CASE__  ,		starting_batch_size=SCREAMING_SNAKE_CASE__       )

						snake_case_   =	starting_batch_size

						def decorator(*SCREAMING_SNAKE_CASE__  ,		**SCREAMING_SNAKE_CASE__       ):
												nonlocal batch_size
												gc.collect()
												if is_xpu_available():
																		torch.xpu.empty_cache()
												elif is_npu_available():
																		torch.npu.empty_cache()
												else:
																		torch.cuda.empty_cache()
												snake_case_   =	list(inspect.signature(SCREAMING_SNAKE_CASE__       ).parameters.keys()       )
												# Guard against user error
												if len(SCREAMING_SNAKE_CASE__       ) < (len(SCREAMING_SNAKE_CASE__       ) + 1):
																		snake_case_   =	''', '''.join([F'''{arg}={value}''' for arg, value in zip(params[1:]  ,		args[1:]       )]       )
																		raise TypeError(
																		    F'''Batch size was passed into `{function.__name__}` as the first argument when called.'''
																		    F'''Remove this as the decorator already does so: `{function.__name__}({arg_str})`'''       )
												while True:
																		if batch_size == 0:
																								raise RuntimeError('''No executable batch size found, reached zero.'''       )
																		try:
																								return function(SCREAMING_SNAKE_CASE__  ,		*SCREAMING_SNAKE_CASE__  ,		**SCREAMING_SNAKE_CASE__       )
																		except Exception as e:
																								if should_reduce_batch_size(SCREAMING_SNAKE_CASE__       ):
																														gc.collect()
																														if is_xpu_available():
																																				torch.xpu.empty_cache()
																														elif is_npu_available():
																																				torch.npu.empty_cache()
																														else:
																																				torch.cuda.empty_cache()
																														batch_size //= 2
																								else:
																														raise

						return decorator