

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    :       int     ,lowercase_    :       List[str]     ,lowercase_    :       int=1_3     ,lowercase_    :       Dict=7     ,lowercase_    :       str=True     ,lowercase_    :       str=True     ,lowercase_    :       Dict=True     ,lowercase_    :       str=True     ,lowercase_    :       List[Any]=9_9     ,lowercase_    :       Tuple=3_2     ,lowercase_    :       List[str]=2     ,lowercase_    :       List[Any]=4     ,lowercase_    :       Optional[int]=3_7     ,lowercase_    :       Dict="gelu"     ,lowercase_    :       Union[str, Any]=0.1     ,lowercase_    :       List[str]=0.1     ,lowercase_    :       int=5_1_2     ,lowercase_    :       Optional[int]=1_6     ,lowercase_    :       Any=2     ,lowercase_    :       int=0.02     ,lowercase_    :       Optional[Any]=3     ,lowercase_    :       Tuple=4     ,lowercase_    :       List[Any]=None     ,):
						lowerCAmelCase__			:		Any						       =			parent
						lowerCAmelCase__			:		Optional[Any]						       =			1_3
						lowerCAmelCase__			:		Dict						       =			7
						lowerCAmelCase__			:		Any						       =			True
						lowerCAmelCase__			:		Optional[Any]						       =			True
						lowerCAmelCase__			:		str						       =			True
						lowerCAmelCase__			:		Dict						       =			True
						lowerCAmelCase__			:		Dict						       =			9_9
						lowerCAmelCase__			:		Optional[Any]						       =			3_2
						lowerCAmelCase__			:		Optional[int]						       =			2
						lowerCAmelCase__			:		Optional[int]						       =			4
						lowerCAmelCase__			:		str						       =			3_7
						lowerCAmelCase__			:		Dict						       =			'''gelu'''
						lowerCAmelCase__			:		Tuple						       =			0.1
						lowerCAmelCase__			:		Optional[int]						       =			0.1
						lowerCAmelCase__			:		Optional[Any]						       =			5_1_2
						lowerCAmelCase__			:		List[str]						       =			1_6
						lowerCAmelCase__			:		str						       =			False
						lowerCAmelCase__			:		str						       =			-1
						lowerCAmelCase__			:		Union[str, Any]						       =			True
						lowerCAmelCase__			:		int						       =			2
						lowerCAmelCase__			:		Tuple						       =			0.02
						lowerCAmelCase__			:		Optional[Any]						       =			3
						lowerCAmelCase__			:		Any						       =			4
						lowerCAmelCase__			:		Optional[Any]						       =			None







					def  __lowerCAmelCase				( self    :       Any	):
						lowerCAmelCase__			:		Optional[int]						       =			ids_tensor([self.batch_size, self.seq_length]     ,self.vocab_size	)

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

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

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

						lowerCAmelCase__			:		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     ,lowercase_    :       Optional[int]     ,lowercase_    :       Any     ,lowercase_    :       str     ,lowercase_    :       int     ,lowercase_    :       Dict     ,lowercase_    :       Dict     ,lowercase_    :       Optional[Any]	):
						lowerCAmelCase__			:		Tuple						       =			TFDebertaModel(config=UpperCamelCase__	)
						lowerCAmelCase__			:		Optional[Any]						       =			{'''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids}

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

						lowerCAmelCase__			:		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]     ,lowercase_    :       List[str]     ,lowercase_    :       Dict     ,lowercase_    :       List[Any]     ,lowercase_    :       List[Any]     ,lowercase_    :       Tuple     ,lowercase_    :       List[str]     ,lowercase_    :       Any	):
						lowerCAmelCase__			:		Dict						       =			TFDebertaForMaskedLM(config=UpperCamelCase__	)
						lowerCAmelCase__			:		str						       =			{
						    '''input_ids''': input_ids,
						    '''attention_mask''': input_mask,
						    '''token_type_ids''': token_type_ids,
						}
						lowerCAmelCase__			:		Any						       =			model(UpperCamelCase__	)
						self.parent.assertEqual(result.logits.shape     ,(self.batch_size, self.seq_length, self.vocab_size)	)







					def  __lowerCAmelCase				( self    :       Tuple     ,lowercase_    :       Dict     ,lowercase_    :       str     ,lowercase_    :       Optional[int]     ,lowercase_    :       List[str]     ,lowercase_    :       List[str]     ,lowercase_    :       str     ,lowercase_    :       Any	):
						lowerCAmelCase__			:		Optional[int]						       =			self.num_labels
						lowerCAmelCase__			:		Optional[int]						       =			TFDebertaForSequenceClassification(config=UpperCamelCase__	)
						lowerCAmelCase__			:		str						       =			{
						    '''input_ids''': input_ids,
						    '''attention_mask''': input_mask,
						    '''token_type_ids''': token_type_ids,
						}

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







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







					def  __lowerCAmelCase				( self    :       List[Any]     ,lowercase_    :       List[str]     ,lowercase_    :       Union[str, Any]     ,lowercase_    :       Optional[int]     ,lowercase_    :       Dict     ,lowercase_    :       Optional[int]     ,lowercase_    :       Optional[Any]     ,lowercase_    :       Dict	):
						lowerCAmelCase__			:		List[Any]						       =			TFDebertaForQuestionAnswering(config=UpperCamelCase__	)
						lowerCAmelCase__			:		Optional[Any]						       =			{
						    '''input_ids''': input_ids,
						    '''attention_mask''': input_mask,
						    '''token_type_ids''': token_type_ids,
						}

						lowerCAmelCase__			:		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]	):
						lowerCAmelCase__			:		Optional[Any]						       =			self.prepare_config_and_inputs()
						(
						    lowerCAmelCase__
						)			:		Optional[int]						       =			config_and_inputs
						lowerCAmelCase__			:		int						       =			{'''input_ids''': input_ids, '''token_type_ids''': token_type_ids, '''attention_mask''': input_mask}
						return config, inputs_dict







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






					"""simple docstring"""



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

					lowercase__       				= False
					lowercase__       				= False







					def  __lowerCAmelCase				( self    :       Optional[Any]	):
						lowerCAmelCase__			:		Tuple						       =			TFDebertaModelTester(self	)
						lowerCAmelCase__			:		Tuple						       =			ConfigTester(self     ,config_class=UpperCamelCase__     ,hidden_size=3_7	)







					def  __lowerCAmelCase				( self    :       Any	):
						self.config_tester.run_common_tests()







					def  __lowerCAmelCase				( self    :       Optional[Any]	):
						lowerCAmelCase__			:		str						       =			self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_model(*UpperCamelCase__	)







					def  __lowerCAmelCase				( self    :       str	):
						lowerCAmelCase__			:		Optional[int]						       =			self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_for_masked_lm(*UpperCamelCase__	)







					def  __lowerCAmelCase				( self    :       List[str]	):
						lowerCAmelCase__			:		List[str]						       =			self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_for_question_answering(*UpperCamelCase__	)







					def  __lowerCAmelCase				( self    :       List[str]	):
						lowerCAmelCase__			:		List[Any]						       =			self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_for_sequence_classification(*UpperCamelCase__	)







					def  __lowerCAmelCase				( self    :       Tuple	):
						lowerCAmelCase__			:		Dict						       =			self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_for_token_classification(*UpperCamelCase__	)







					@slow
					def  __lowerCAmelCase				( self    :       Tuple	):
						lowerCAmelCase__			:		List[str]						       =			TFDebertaModel.from_pretrained('''kamalkraj/deberta-base'''	)
						self.assertIsNotNone(UpperCamelCase__	)







@require_tf
class        SCREAMING_SNAKE_CASE      (	unittest.TestCase ):






					"""simple docstring"""



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







					@slow
					def  __lowerCAmelCase				( self    :       str	):
						lowerCAmelCase__			:		Union[str, Any]						       =			TFDebertaModel.from_pretrained('''kamalkraj/deberta-base'''	)
						lowerCAmelCase__			:		List[Any]						       =			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]]	)
						lowerCAmelCase__			:		str						       =			tf.constant([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]	)
						lowerCAmelCase__			:		Tuple						       =			model(UpperCamelCase__     ,attention_mask=UpperCamelCase__	)[0]

						lowerCAmelCase__			:		Optional[int]						       =			tf.constant(
						    [
						        [
						            [-0.5985_5896, -0.8055_2566, -0.846_2135],
						            [1.448_4025, -0.9348_3794, -0.8059_3085],
						            [0.312_2741, 0.0031_6059, -1.413_1377],
						        ]
						    ]	)
						tf.debugging.assert_near(output[:, 1:4, 1:4]     ,UpperCamelCase__     ,atol=1E-4	)







"""simple docstring"""





from typing import TYPE_CHECKING

from ...utils import (
    OptionalDependencyNotAvailable,
    _LazyModule,
    is_tf_available,
    is_tokenizers_available,
    is_torch_available,
)


__UpperCamelCase						:   Union[str, Any]							   =	{
    '''configuration_layoutlm''': ['''LAYOUTLM_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''LayoutLMConfig''', '''LayoutLMOnnxConfig'''],
    '''tokenization_layoutlm''': ['''LayoutLMTokenizer'''],
}

try:
							if not is_tokenizers_available():
														raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
							pass
else:
							__UpperCamelCase						:   Union[str, Any]							   =	['''LayoutLMTokenizerFast''']

try:
							if not is_torch_available():
														raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
							pass
else:
							__UpperCamelCase						:   Dict							   =	[
							    '''LAYOUTLM_PRETRAINED_MODEL_ARCHIVE_LIST''',
							    '''LayoutLMForMaskedLM''',
							    '''LayoutLMForSequenceClassification''',
							    '''LayoutLMForTokenClassification''',
							    '''LayoutLMForQuestionAnswering''',
							    '''LayoutLMModel''',
							    '''LayoutLMPreTrainedModel''',
							]

try:
							if not is_tf_available():
														raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
							pass
else:
							__UpperCamelCase						:   str							   =	[
							    '''TF_LAYOUTLM_PRETRAINED_MODEL_ARCHIVE_LIST''',
							    '''TFLayoutLMForMaskedLM''',
							    '''TFLayoutLMForSequenceClassification''',
							    '''TFLayoutLMForTokenClassification''',
							    '''TFLayoutLMForQuestionAnswering''',
							    '''TFLayoutLMMainLayer''',
							    '''TFLayoutLMModel''',
							    '''TFLayoutLMPreTrainedModel''',
							]


if TYPE_CHECKING:
							from .configuration_layoutlm import LAYOUTLM_PRETRAINED_CONFIG_ARCHIVE_MAP, LayoutLMConfig, LayoutLMOnnxConfig
							from .tokenization_layoutlm import LayoutLMTokenizer

							try:
														if not is_tokenizers_available():
																					raise OptionalDependencyNotAvailable()
							except OptionalDependencyNotAvailable:
														pass
							else:
														from .tokenization_layoutlm_fast import LayoutLMTokenizerFast

							try:
														if not is_torch_available():
																					raise OptionalDependencyNotAvailable()
							except OptionalDependencyNotAvailable:
														pass
							else:
														from .modeling_layoutlm import (
														    LAYOUTLM_PRETRAINED_MODEL_ARCHIVE_LIST,
														    LayoutLMForMaskedLM,
														    LayoutLMForQuestionAnswering,
														    LayoutLMForSequenceClassification,
														    LayoutLMForTokenClassification,
														    LayoutLMModel,
														    LayoutLMPreTrainedModel,
														)
							try:
														if not is_tf_available():
																					raise OptionalDependencyNotAvailable()
							except OptionalDependencyNotAvailable:
														pass
							else:
														from .modeling_tf_layoutlm import (
														    TF_LAYOUTLM_PRETRAINED_MODEL_ARCHIVE_LIST,
														    TFLayoutLMForMaskedLM,
														    TFLayoutLMForQuestionAnswering,
														    TFLayoutLMForSequenceClassification,
														    TFLayoutLMForTokenClassification,
														    TFLayoutLMMainLayer,
														    TFLayoutLMModel,
														    TFLayoutLMPreTrainedModel,
														)

else:
							import sys

							__UpperCamelCase						:   int							   =	_LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)




