import unittest

from transformers import DebertaConfig, is_torch_available
from transformers.testing_utils import require_sentencepiece, require_tokenizers, require_torch, slow, torch_device

from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
				import torch

				from transformers import (
				    DebertaForMaskedLM,
				    DebertaForQuestionAnswering,
				    DebertaForSequenceClassification,
				    DebertaForTokenClassification,
				    DebertaModel,
				)
				from transformers.models.deberta.modeling_deberta import DEBERTA_PRETRAINED_MODEL_ARCHIVE_LIST



class        lowerCamelCase__				(   __SCREAMING_SNAKE_CASE				):





			def __init__(       self	:	Tuple  ,  lowercase__	:	Optional[int]  ,  lowercase__	:	Dict=13  ,  lowercase__	:	Optional[int]=7  ,  lowercase__	:	Any=True  ,  lowercase__	:	Dict=True  ,  lowercase__	:	List[str]=True  ,  lowercase__	:	Optional[Any]=True  ,  lowercase__	:	int=99  ,  lowercase__	:	int=32  ,  lowercase__	:	List[str]=5  ,  lowercase__	:	Tuple=4  ,  lowercase__	:	Optional[int]=37  ,  lowercase__	:	Any="gelu"  ,  lowercase__	:	List[str]=0.1  ,  lowercase__	:	Optional[Any]=0.1  ,  lowercase__	:	Dict=5_12  ,  lowercase__	:	Dict=16  ,  lowercase__	:	Dict=2  ,  lowercase__	:	List[str]=0.0_2  ,  lowercase__	:	List[Any]=False  ,  lowercase__	:	Dict=True  ,  lowercase__	:	Dict="None"  ,  lowercase__	:	Tuple=3  ,  lowercase__	:	int=4  ,  lowercase__	:	Dict=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   SCREAMING_SNAKE_CASE__     (       self	:	int						):
										_lowerCAmelCase							=							ids_tensor([self.batch_size, self.seq_length]  ,  self.vocab_size						)

										_lowerCAmelCase							=							None
										if self.use_input_mask:
																	_lowerCAmelCase							=							ids_tensor([self.batch_size, self.seq_length]  ,  vocab_size=2						)

										_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							=							ids_tensor([self.batch_size]  ,  self.num_choices						)

										_lowerCAmelCase							=							self.get_config()

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





			def   SCREAMING_SNAKE_CASE__     (       self	:	int						):
										return 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  ,  initializer_range=self.initializer_range  ,  relative_attention=self.relative_attention  ,  position_biased_input=self.position_biased_input  ,  pos_att_type=self.pos_att_type  ,  )





			def   SCREAMING_SNAKE_CASE__     (       self	:	str						):
										_lowerCAmelCase							=							self.get_config()
										_lowerCAmelCase							=							3_00
										return config





			def   SCREAMING_SNAKE_CASE__     (       self	:	int  ,  lowercase__	:	List[Any]						):
										self.parent.assertListEqual(list(result.loss.size()						)  ,  []						)





			def   SCREAMING_SNAKE_CASE__     (       self	:	Any  ,  lowercase__	:	Dict  ,  lowercase__	:	Union[str, Any]  ,  lowercase__	:	Dict  ,  lowercase__	:	Optional[Any]  ,  lowercase__	:	Dict  ,  lowercase__	:	Any  ,  lowercase__	:	List[Any]						):
										_lowerCAmelCase							=							DebertaModel(config=_A						)
										model.to(_A						)
										model.eval()
										_lowerCAmelCase							=							model(_A  ,  attention_mask=_A  ,  token_type_ids=_A						)[0]
										_lowerCAmelCase							=							model(_A  ,  token_type_ids=_A						)[0]
										_lowerCAmelCase							=							model(_A						)[0]

										self.parent.assertListEqual(list(sequence_output.size()						)  ,  [self.batch_size, self.seq_length, self.hidden_size]						)





			def   SCREAMING_SNAKE_CASE__     (       self	:	List[Any]  ,  lowercase__	:	Any  ,  lowercase__	:	Optional[int]  ,  lowercase__	:	str  ,  lowercase__	:	int  ,  lowercase__	:	List[str]  ,  lowercase__	:	Tuple  ,  lowercase__	:	Any						):
										_lowerCAmelCase							=							DebertaForMaskedLM(config=_A						)
										model.to(_A						)
										model.eval()
										_lowerCAmelCase							=							model(_A  ,  attention_mask=_A  ,  token_type_ids=_A  ,  labels=_A						)

										self.parent.assertEqual(result.logits.shape  ,  (self.batch_size, self.seq_length, self.vocab_size)						)





			def   SCREAMING_SNAKE_CASE__     (       self	:	str  ,  lowercase__	:	str  ,  lowercase__	:	Tuple  ,  lowercase__	:	List[str]  ,  lowercase__	:	List[str]  ,  lowercase__	:	Dict  ,  lowercase__	:	List[str]  ,  lowercase__	:	Tuple						):
										_lowerCAmelCase							=							self.num_labels
										_lowerCAmelCase							=							DebertaForSequenceClassification(_A						)
										model.to(_A						)
										model.eval()
										_lowerCAmelCase							=							model(_A  ,  attention_mask=_A  ,  token_type_ids=_A  ,  labels=_A						)
										self.parent.assertListEqual(list(result.logits.size()						)  ,  [self.batch_size, self.num_labels]						)
										self.check_loss_output(_A						)





			def   SCREAMING_SNAKE_CASE__     (       self	:	Tuple  ,  lowercase__	:	List[str]  ,  lowercase__	:	Any  ,  lowercase__	:	Optional[int]  ,  lowercase__	:	Any  ,  lowercase__	:	List[Any]  ,  lowercase__	:	Optional[Any]  ,  lowercase__	:	List[Any]						):
										_lowerCAmelCase							=							self.num_labels
										_lowerCAmelCase							=							DebertaForTokenClassification(config=_A						)
										model.to(_A						)
										model.eval()
										_lowerCAmelCase							=							model(_A  ,  attention_mask=_A  ,  token_type_ids=_A  ,  labels=_A						)
										self.parent.assertEqual(result.logits.shape  ,  (self.batch_size, self.seq_length, self.num_labels)						)





			def   SCREAMING_SNAKE_CASE__     (       self	:	Dict  ,  lowercase__	:	Any  ,  lowercase__	:	int  ,  lowercase__	:	int  ,  lowercase__	:	Tuple  ,  lowercase__	:	List[Any]  ,  lowercase__	:	Optional[Any]  ,  lowercase__	:	List[str]						):
										_lowerCAmelCase							=							DebertaForQuestionAnswering(config=_A						)
										model.to(_A						)
										model.eval()
										_lowerCAmelCase							=							model(
										    _A  ,  attention_mask=_A  ,  token_type_ids=_A  ,  start_positions=_A  ,  end_positions=_A  ,  )
										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   SCREAMING_SNAKE_CASE__     (       self	:	Union[str, Any]						):
										_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_torch
class        lowerCamelCase__				(   __SCREAMING_SNAKE_CASE       ,__SCREAMING_SNAKE_CASE       ,unittest.TestCase				):
			UpperCamelCase__    							=(
			    (
			        DebertaModel,
			        DebertaForMaskedLM,
			        DebertaForSequenceClassification,
			        DebertaForTokenClassification,
			        DebertaForQuestionAnswering,
			    )
			    if is_torch_available()
			    else ()
			)
			UpperCamelCase__    							=(
			    {
			        """feature-extraction""": DebertaModel,
			        """fill-mask""": DebertaForMaskedLM,
			        """question-answering""": DebertaForQuestionAnswering,
			        """text-classification""": DebertaForSequenceClassification,
			        """token-classification""": DebertaForTokenClassification,
			        """zero-shot""": DebertaForSequenceClassification,
			    }
			    if is_torch_available()
			    else {}
			)

			UpperCamelCase__    							=True
			UpperCamelCase__    							=False
			UpperCamelCase__    							=False
			UpperCamelCase__    							=False
			UpperCamelCase__    							=False





			def   SCREAMING_SNAKE_CASE__     (       self	:	List[str]						):
										_lowerCAmelCase							=							DebertaModelTester(self						)
										_lowerCAmelCase							=							ConfigTester(self  ,  config_class=_A  ,  hidden_size=37						)





			def   SCREAMING_SNAKE_CASE__     (       self	:	List[Any]						):
										self.config_tester.run_common_tests()





			def   SCREAMING_SNAKE_CASE__     (       self	:	int						):
										_lowerCAmelCase							=							self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_deberta_model(*_A						)





			def   SCREAMING_SNAKE_CASE__     (       self	:	int						):
										_lowerCAmelCase							=							self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_deberta_for_sequence_classification(*_A						)





			def   SCREAMING_SNAKE_CASE__     (       self	:	List[str]						):
										_lowerCAmelCase							=							self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_deberta_for_masked_lm(*_A						)





			def   SCREAMING_SNAKE_CASE__     (       self	:	Any						):
										_lowerCAmelCase							=							self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_deberta_for_question_answering(*_A						)





			def   SCREAMING_SNAKE_CASE__     (       self	:	Union[str, Any]						):
										_lowerCAmelCase							=							self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_deberta_for_token_classification(*_A						)





			@slow
			def   SCREAMING_SNAKE_CASE__     (       self	:	Dict						):







										for model_name in DEBERTA_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																	_lowerCAmelCase							=							DebertaModel.from_pretrained(_A						)
																	self.assertIsNotNone(_A						)







@require_torch
@require_sentencepiece
@require_tokenizers
class        lowerCamelCase__				(   unittest.TestCase				):





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





			@slow
			def   SCREAMING_SNAKE_CASE__     (       self	:	List[str]						):
										_lowerCAmelCase							=							DebertaModel.from_pretrained('microsoft/deberta-base'						)

										_lowerCAmelCase							=							torch.tensor([[0, 3_14_14, 2_32, 3_28, 7_40, 11_40, 1_26_95, 69, 4_60_78, 15_88, 2]]						)
										_lowerCAmelCase							=							torch.tensor([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]						)
										with torch.no_grad():
																	_lowerCAmelCase							=							model(_A  ,  attention_mask=_A						)[0]
										# compare the actual values for a slice.
										_lowerCAmelCase							=							torch.tensor(
										    [[[-0.5_9_8_6, -0.8_0_5_5, -0.8_4_6_2], [1.4_4_8_4, -0.9_3_4_8, -0.8_0_5_9], [0.3_1_2_3, 0.0_0_3_2, -1.4_1_3_1]]]						)
										self.assertTrue(torch.allclose(output[:, 1:4, 1:4]  ,  _A  ,  atol=1e-4						)  ,  f'{output[:, 1:4, 1:4]}'						)













"""simple docstring"""


from typing import TYPE_CHECKING

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


__magic_name__       :					Dict						=			{
    """configuration_layoutlmv3""": [
        """LAYOUTLMV3_PRETRAINED_CONFIG_ARCHIVE_MAP""",
        """LayoutLMv3Config""",
        """LayoutLMv3OnnxConfig""",
    ],
    """processing_layoutlmv3""": ["""LayoutLMv3Processor"""],
    """tokenization_layoutlmv3""": ["""LayoutLMv3Tokenizer"""],
}

try:
		if not is_tokenizers_available():
				raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
		pass
else:
		__magic_name__       :					Optional[Any]						=			["""LayoutLMv3TokenizerFast"""]

try:
		if not is_torch_available():
				raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
		pass
else:
		__magic_name__       :					int						=			[
		    """LAYOUTLMV3_PRETRAINED_MODEL_ARCHIVE_LIST""",
		    """LayoutLMv3ForQuestionAnswering""",
		    """LayoutLMv3ForSequenceClassification""",
		    """LayoutLMv3ForTokenClassification""",
		    """LayoutLMv3Model""",
		    """LayoutLMv3PreTrainedModel""",
		]

try:
		if not is_tf_available():
				raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
		pass
else:
		__magic_name__       :					Tuple						=			[
		    """TF_LAYOUTLMV3_PRETRAINED_MODEL_ARCHIVE_LIST""",
		    """TFLayoutLMv3ForQuestionAnswering""",
		    """TFLayoutLMv3ForSequenceClassification""",
		    """TFLayoutLMv3ForTokenClassification""",
		    """TFLayoutLMv3Model""",
		    """TFLayoutLMv3PreTrainedModel""",
		]

try:
		if not is_vision_available():
				raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
		pass
else:
		__magic_name__       :					Union[str, Any]						=			["""LayoutLMv3FeatureExtractor"""]
		__magic_name__       :					Any						=			["""LayoutLMv3ImageProcessor"""]


if TYPE_CHECKING:
		from .configuration_layoutlmva import (
		    LAYOUTLMV3_PRETRAINED_CONFIG_ARCHIVE_MAP,
		    LayoutLMvaConfig,
		    LayoutLMvaOnnxConfig,
		)
		from .processing_layoutlmva import LayoutLMvaProcessor
		from .tokenization_layoutlmva import LayoutLMvaTokenizer

		try:
				if not is_tokenizers_available():
						raise OptionalDependencyNotAvailable()
		except OptionalDependencyNotAvailable:
				pass
		else:
				from .tokenization_layoutlmva_fast import LayoutLMvaTokenizerFast

		try:
				if not is_torch_available():
						raise OptionalDependencyNotAvailable()
		except OptionalDependencyNotAvailable:
				pass
		else:
				from .modeling_layoutlmva import (
				    LAYOUTLMV3_PRETRAINED_MODEL_ARCHIVE_LIST,
				    LayoutLMvaForQuestionAnswering,
				    LayoutLMvaForSequenceClassification,
				    LayoutLMvaForTokenClassification,
				    LayoutLMvaModel,
				    LayoutLMvaPreTrainedModel,
				)

		try:
				if not is_tf_available():
						raise OptionalDependencyNotAvailable()
		except OptionalDependencyNotAvailable:
				pass
		else:
				from .modeling_tf_layoutlmva import (
				    TF_LAYOUTLMV3_PRETRAINED_MODEL_ARCHIVE_LIST,
				    TFLayoutLMvaForQuestionAnswering,
				    TFLayoutLMvaForSequenceClassification,
				    TFLayoutLMvaForTokenClassification,
				    TFLayoutLMvaModel,
				    TFLayoutLMvaPreTrainedModel,
				)

		try:
				if not is_vision_available():
						raise OptionalDependencyNotAvailable()
		except OptionalDependencyNotAvailable:
				pass
		else:
				from .feature_extraction_layoutlmva import LayoutLMvaFeatureExtractor
				from .image_processing_layoutlmva import LayoutLMvaImageProcessor

else:
		import sys

		__magic_name__       :					Union[str, Any]						=			_LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)



