




from ctypes import c_float, sizeof
from enum import Enum
from typing import TYPE_CHECKING, Optional, Union


if TYPE_CHECKING:
	from .. import AutoFeatureExtractor, AutoProcessor, AutoTokenizer  # tests_ignore


class       SCREAMING_SNAKE_CASE_							(					_UpperCamelCase      ):



	"""simple docstring"""


	__magic_name__      :					int			  =		c_float






	@property
	def 				lowerCamelCase__    (				self		:	str  )  ->					int:







						"""simple docstring"""







						return sizeof(self.value  )






def 							A__   (snake_case      :		int		,  snake_case      :		int		,  snake_case      :		int = 0							)    ->	int:
					# < 0 is possible if using a dynamic axis
					if dimension <= 0:
										__UpperCamelCase				:  Optional[Any]								=			fixed_dimension

					dimension -= num_token_to_add
					return dimension





def 							A__   (snake_case      :		int		,  snake_case      :		ParameterFormat							)    ->	int:
					return num_parameters * dtype.size







def 							A__   (snake_case      :		str							)    ->	Optional[Union["AutoTokenizer", "AutoFeatureExtractor", "AutoProcessor"]]:
					from .. import AutoFeatureExtractor, AutoProcessor, AutoTokenizer  # tests_ignore

					try:
										return AutoProcessor.from_pretrained(snake_case							)
					except (ValueError, OSError, KeyError):
										__UpperCamelCase			, __UpperCamelCase				:  Union[str, Any]								=			None, None
										try:
															__UpperCamelCase				:  str								=			AutoTokenizer.from_pretrained(snake_case							)
										except (OSError, KeyError):
															pass
										try:
															__UpperCamelCase				:  Optional[int]								=			AutoFeatureExtractor.from_pretrained(snake_case							)
										except (OSError, KeyError):
															pass

										if tokenizer is not None and feature_extractor is not None:
															raise ValueError(
															    F'''Couldn\'t auto-detect preprocessor for {model_name}. Found both a tokenizer and a feature extractor.'''							)
										elif tokenizer is None and feature_extractor is None:
															return None
										elif tokenizer is not None:
															return tokenizer
										else:
															return feature_extractor












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



	"""simple docstring"""


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







						"""simple docstring"""







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






	def 				lowerCamelCase__    (				self		:	Optional[Any]  )  ->					Dict:







						"""simple docstring"""







						__UpperCamelCase				:  Dict								=			ids_tensor([self.batch_size, self.seq_length]				,    self.vocab_size  )

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

						__UpperCamelCase				:  Optional[int]								=			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				:  List[Any]								=			None
						__UpperCamelCase				:  Tuple								=			None
						if self.use_labels:
											__UpperCamelCase				:  str								=			ids_tensor([self.batch_size]				,    self.type_sequence_label_size  )
											__UpperCamelCase				:  Dict								=			ids_tensor([self.batch_size, self.seq_length]				,    self.num_labels  )

						__UpperCamelCase				:  Optional[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=lowerCAmelCase				,    )

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






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







						"""simple docstring"""







						__UpperCamelCase				:  str								=			TFDebertaModel(config=lowerCAmelCase  )
						__UpperCamelCase				:  Optional[Any]								=			{"""input_ids""": input_ids, """attention_mask""": input_mask, """token_type_ids""": token_type_ids}

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

						__UpperCamelCase				:  int								=			model(lowerCAmelCase  )

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






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







						"""simple docstring"""







						__UpperCamelCase				:  Tuple								=			TFDebertaForMaskedLM(config=lowerCAmelCase  )
						__UpperCamelCase				:  Dict								=			{
						    """input_ids""": input_ids,
						    """attention_mask""": input_mask,
						    """token_type_ids""": token_type_ids,
						}
						__UpperCamelCase				:  Union[str, Any]								=			model(lowerCAmelCase  )
						self.parent.assertEqual(result.logits.shape				,    (self.batch_size, self.seq_length, self.vocab_size)  )






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







						"""simple docstring"""







						__UpperCamelCase				:  List[Any]								=			self.num_labels
						__UpperCamelCase				:  Optional[Any]								=			TFDebertaForSequenceClassification(config=lowerCAmelCase  )
						__UpperCamelCase				:  Any								=			{
						    """input_ids""": input_ids,
						    """attention_mask""": input_mask,
						    """token_type_ids""": token_type_ids,
						}

						__UpperCamelCase				:  Any								=			model(lowerCAmelCase  )
						self.parent.assertEqual(result.logits.shape				,    (self.batch_size, self.num_labels)  )






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







						"""simple docstring"""







						__UpperCamelCase				:  List[Any]								=			self.num_labels
						__UpperCamelCase				:  Optional[Any]								=			TFDebertaForTokenClassification(config=lowerCAmelCase  )
						__UpperCamelCase				:  List[Any]								=			{
						    """input_ids""": input_ids,
						    """attention_mask""": input_mask,
						    """token_type_ids""": token_type_ids,
						}
						__UpperCamelCase				:  int								=			model(lowerCAmelCase  )
						self.parent.assertEqual(result.logits.shape				,    (self.batch_size, self.seq_length, self.num_labels)  )






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







						"""simple docstring"""







						__UpperCamelCase				:  Optional[Any]								=			TFDebertaForQuestionAnswering(config=lowerCAmelCase  )
						__UpperCamelCase				:  int								=			{
						    """input_ids""": input_ids,
						    """attention_mask""": input_mask,
						    """token_type_ids""": token_type_ids,
						}

						__UpperCamelCase				:  Tuple								=			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		:	Optional[Any]  )  ->					Optional[int]:







						"""simple docstring"""







						__UpperCamelCase				:  Tuple								=			self.prepare_config_and_inputs()
						(
						    (
						    __UpperCamelCase
						)			, (
						    __UpperCamelCase
						)			, (
						    __UpperCamelCase
						)			, (
						    __UpperCamelCase
						)			, (
						    __UpperCamelCase
						)			, (
						    __UpperCamelCase
						)			, (
						    __UpperCamelCase
						)			, 
						)				:  int								=			config_and_inputs
						__UpperCamelCase				:  str								=			{"""input_ids""": input_ids, """token_type_ids""": token_type_ids, """attention_mask""": input_mask}
						return config, inputs_dict






@require_tf
class       SCREAMING_SNAKE_CASE_							(					_UpperCamelCase  ,     _UpperCamelCase  ,     unittest.TestCase      ):



	"""simple docstring"""


	__magic_name__      :					str			  =		(
	    (
	        TFDebertaModel,
	        TFDebertaForMaskedLM,
	        TFDebertaForQuestionAnswering,
	        TFDebertaForSequenceClassification,
	        TFDebertaForTokenClassification,
	    )
	    if is_tf_available()
	    else ()
	)
	__magic_name__      :					Optional[Any]			  =		(
	    {
	        'feature-extraction': TFDebertaModel,
	        'fill-mask': TFDebertaForMaskedLM,
	        'question-answering': TFDebertaForQuestionAnswering,
	        'text-classification': TFDebertaForSequenceClassification,
	        'token-classification': TFDebertaForTokenClassification,
	        'zero-shot': TFDebertaForSequenceClassification,
	    }
	    if is_tf_available()
	    else {}
	)

	__magic_name__      :					int			  =		False
	__magic_name__      :					List[str]			  =		False






	def 				lowerCamelCase__    (				self		:	Tuple  )  ->					int:







						"""simple docstring"""







						__UpperCamelCase				:  str								=			TFDebertaModelTester(self  )
						__UpperCamelCase				:  Union[str, Any]								=			ConfigTester(self				,    config_class=lowerCAmelCase				,    hidden_size=37  )






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







						"""simple docstring"""







						self.config_tester.run_common_tests()






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







						"""simple docstring"""







						__UpperCamelCase				:  List[Any]								=			self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_model(*lowerCAmelCase  )






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







						"""simple docstring"""







						__UpperCamelCase				:  Dict								=			self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_for_masked_lm(*lowerCAmelCase  )






	def 				lowerCamelCase__    (				self		:	Dict  )  ->					int:







						"""simple docstring"""







						__UpperCamelCase				:  Any								=			self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_for_question_answering(*lowerCAmelCase  )






	def 				lowerCamelCase__    (				self		:	Tuple  )  ->					Any:







						"""simple docstring"""







						__UpperCamelCase				:  Dict								=			self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_for_sequence_classification(*lowerCAmelCase  )






	def 				lowerCamelCase__    (				self		:	List[str]  )  ->					List[str]:







						"""simple docstring"""







						__UpperCamelCase				:  str								=			self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_for_token_classification(*lowerCAmelCase  )






	@slow
	def 				lowerCamelCase__    (				self		:	Union[str, Any]  )  ->					Tuple:







						"""simple docstring"""







						__UpperCamelCase				:  List[str]								=			TFDebertaModel.from_pretrained("""kamalkraj/deberta-base"""  )
						self.assertIsNotNone(lowerCAmelCase  )






@require_tf
class       SCREAMING_SNAKE_CASE_							(					unittest.TestCase      ):



	"""simple docstring"""


	@unittest.skip(reason="""Model not available yet"""  )
	def 				lowerCamelCase__    (				self		:	Optional[int]  )  ->					Tuple:







						"""simple docstring"""







						pass






	@slow
	def 				lowerCamelCase__    (				self		:	Tuple  )  ->					List[Any]:







						"""simple docstring"""







						__UpperCamelCase				:  str								=			TFDebertaModel.from_pretrained("""kamalkraj/deberta-base"""  )
						__UpperCamelCase				:  Optional[int]								=			tf.constant([[0, 31414, 232, 328, 740, 1140, 12695, 69, 46078, 1588, 2]]  )
						__UpperCamelCase				:  int								=			tf.constant([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]  )
						__UpperCamelCase				:  Tuple								=			model(lowerCAmelCase				,    attention_mask=lowerCAmelCase  )[0]

						__UpperCamelCase				:  Any								=			tf.constant(
						    [
						        [
						            [-0.59_85_58_96, -0.80_55_25_66, -0.8_46_21_35],
						            [1.4_48_40_25, -0.93_48_37_94, -0.80_59_30_85],
						            [0.3_12_27_41, 0.00_31_60_59, -1.4_13_13_77],
						        ]
						    ]  )
						tf.debugging.assert_near(output[:, 1:4, 1:4]				,    lowerCAmelCase				,    atol=1E-4  )






