
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		(  __snake_case  ):





     def __init__(self				:   Optional[int]    ,   _A				:   Dict    ,   _A				:   int=1_3    ,   _A				:   Optional[Any]=7    ,   _A				:   Any=True    ,   _A				:   str=True    ,   _A				:   List[Any]=True    ,   _A				:   Any=True    ,   _A				:   str=9_9    ,   _A				:   Optional[int]=3_2    ,   _A				:   Dict=5    ,   _A				:   Any=4    ,   _A				:   Optional[Any]=3_7    ,   _A				:   Optional[int]="gelu"    ,   _A				:   List[Any]=0.1    ,   _A				:   List[Any]=0.1    ,   _A				:   Optional[Any]=5_1_2    ,   _A				:   Tuple=1_6    ,   _A				:   List[Any]=2    ,   _A				:   Optional[int]=0.02    ,   _A				:   List[str]=False    ,   _A				:   Union[str, Any]=True    ,   _A				:   Tuple="None"    ,   _A				:   Tuple=3    ,   _A				:   Optional[Any]=4    ,   _A				:   str=None    ,   )						->						Union[str, 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 		UpperCAmelCase(self				:   Any       )						->						Union[str, Any]:
         snake_case								= ids_tensor([self.batch_size, self.seq_length]    ,   self.vocab_size       )

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

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

         snake_case								= self.get_config()

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





     def 		UpperCAmelCase(self				:   Dict       )						->						Dict:
         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 		UpperCAmelCase(self				:   List[str]       )						->						Tuple:
         snake_case								= self.get_config()
         snake_case								= 3_0_0
         return config





     def 		UpperCAmelCase(self				:   str    ,   _A				:   Any       )						->						Optional[int]:
         self.parent.assertListEqual(list(result.loss.size()       )    ,   []       )





     def 		UpperCAmelCase(self				:   Tuple    ,   _A				:   Optional[Any]    ,   _A				:   List[Any]    ,   _A				:   Dict    ,   _A				:   List[str]    ,   _A				:   Optional[Any]    ,   _A				:   Any    ,   _A				:   List[Any]       )						->						Dict:
         snake_case								= DebertaModel(config=a_       )
         model.to(a_       )
         model.eval()
         snake_case								= model(a_    ,   attention_mask=a_    ,   token_type_ids=a_       )[0]
         snake_case								= model(a_    ,   token_type_ids=a_       )[0]
         snake_case								= model(a_       )[0]

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





     def 		UpperCAmelCase(self				:   int    ,   _A				:   int    ,   _A				:   Optional[int]    ,   _A				:   List[Any]    ,   _A				:   Union[str, Any]    ,   _A				:   Any    ,   _A				:   List[Any]    ,   _A				:   Any       )						->						List[str]:
         snake_case								= DebertaForMaskedLM(config=a_       )
         model.to(a_       )
         model.eval()
         snake_case								= 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 		UpperCAmelCase(self				:   List[Any]    ,   _A				:   Dict    ,   _A				:   Optional[int]    ,   _A				:   Union[str, Any]    ,   _A				:   Dict    ,   _A				:   Optional[int]    ,   _A				:   str    ,   _A				:   Tuple       )						->						Optional[int]:
         snake_case								= self.num_labels
         snake_case								= DebertaForSequenceClassification(a_       )
         model.to(a_       )
         model.eval()
         snake_case								= 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 		UpperCAmelCase(self				:   str    ,   _A				:   Tuple    ,   _A				:   Optional[Any]    ,   _A				:   List[str]    ,   _A				:   Dict    ,   _A				:   Optional[int]    ,   _A				:   List[Any]    ,   _A				:   List[Any]       )						->						int:
         snake_case								= self.num_labels
         snake_case								= DebertaForTokenClassification(config=a_       )
         model.to(a_       )
         model.eval()
         snake_case								= 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 		UpperCAmelCase(self				:   int    ,   _A				:   Optional[int]    ,   _A				:   Optional[int]    ,   _A				:   Dict    ,   _A				:   int    ,   _A				:   Optional[int]    ,   _A				:   Dict    ,   _A				:   Dict       )						->						int:
         snake_case								= DebertaForQuestionAnswering(config=a_       )
         model.to(a_       )
         model.eval()
         snake_case								= 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 		UpperCAmelCase(self				:   Dict       )						->						int:
         snake_case								= self.prepare_config_and_inputs()
         (
             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_torch
class 					lowerCamelCase		(  __snake_case							,			__snake_case							,			unittest.TestCase  ):
     UpperCAmelCase__    :							str									=		(
         (
             DebertaModel,
             DebertaForMaskedLM,
             DebertaForSequenceClassification,
             DebertaForTokenClassification,
             DebertaForQuestionAnswering,
         )
         if is_torch_available()
         else ()
     )
     UpperCAmelCase__    :							List[str]									=		(
         {
             "feature-extraction": DebertaModel,
             "fill-mask": DebertaForMaskedLM,
             "question-answering": DebertaForQuestionAnswering,
             "text-classification": DebertaForSequenceClassification,
             "token-classification": DebertaForTokenClassification,
             "zero-shot": DebertaForSequenceClassification,
         }
         if is_torch_available()
         else {}
     )

     UpperCAmelCase__    :							Union[str, Any]									=		True
     UpperCAmelCase__    :							Optional[int]									=		False
     UpperCAmelCase__    :							Any									=		False
     UpperCAmelCase__    :							Dict									=		False
     UpperCAmelCase__    :							str									=		False





     def 		UpperCAmelCase(self				:   Optional[int]       )						->						List[Any]:
         snake_case								= DebertaModelTester(self       )
         snake_case								= ConfigTester(self    ,   config_class=a_    ,   hidden_size=3_7       )





     def 		UpperCAmelCase(self				:   List[Any]       )						->						int:
         self.config_tester.run_common_tests()





     def 		UpperCAmelCase(self				:   List[str]       )						->						str:
         snake_case								= self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_deberta_model(*a_       )





     def 		UpperCAmelCase(self				:   Any       )						->						Optional[int]:
         snake_case								= self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_deberta_for_sequence_classification(*a_       )





     def 		UpperCAmelCase(self				:   str       )						->						Tuple:
         snake_case								= self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_deberta_for_masked_lm(*a_       )





     def 		UpperCAmelCase(self				:   List[str]       )						->						Union[str, Any]:
         snake_case								= self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_deberta_for_question_answering(*a_       )





     def 		UpperCAmelCase(self				:   str       )						->						Any:
         snake_case								= self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_deberta_for_token_classification(*a_       )





     @slow
     def 		UpperCAmelCase(self				:   List[Any]       )						->						Optional[int]:

         for model_name in DEBERTA_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
             snake_case								= 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 		UpperCAmelCase(self				:   Dict       )						->						Dict:
         pass





     @slow
     def 		UpperCAmelCase(self				:   List[Any]       )						->						str:
         snake_case								= DebertaModel.from_pretrained("microsoft/deberta-base"       )

         snake_case								= torch.tensor([[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								= torch.tensor([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]       )
         with torch.no_grad():
             snake_case								= model(a_    ,   attention_mask=a_       )[0]
         # compare the actual values for a slice.
         snake_case								= torch.tensor(
             [[[-0.59_86, -0.80_55, -0.84_62], [1.44_84, -0.93_48, -0.80_59], [0.31_23, 0.00_32, -1.41_31]]]       )
         self.assertTrue(torch.allclose(output[:, 1:4, 1:4]    ,   a_    ,   atol=1E-4       )    ,   f'{output[:, 1:4, 1:4]}'       )



import warnings
from typing import List, Optional, Union

from ...processing_utils import ProcessorMixin
from ...tokenization_utils_base import BatchEncoding, PaddingStrategy, PreTokenizedInput, TextInput, TruncationStrategy
from ...utils import TensorType







class 					lowerCamelCase		(  A_  ):
     UpperCAmelCase__    :							Dict									=		["image_processor", "tokenizer"]
     UpperCAmelCase__    :							Dict									=		"LayoutLMv2ImageProcessor"
     UpperCAmelCase__    :							Optional[Any]									=		("LayoutXLMTokenizer", "LayoutXLMTokenizerFast")





     def __init__(self				:   str    ,   _A				:   Any=None    ,   _A				:   Tuple=None    ,   **_A				:   Optional[Any]       )						->						Optional[int]:
         if "feature_extractor" in kwargs:
             warnings.warn(
                 "The `feature_extractor` argument is deprecated and will be removed in v5, use `image_processor`"
                 " instead."    ,   _A    ,   )
             snake_case								= kwargs.pop("feature_extractor"       )

         snake_case								= image_processor if image_processor is not None else feature_extractor
         if image_processor is None:
             raise ValueError("You need to specify an `image_processor`."       )
         if tokenizer is None:
             raise ValueError("You need to specify a `tokenizer`."       )

         super().__init__(_A    ,   _A       )





     def __call__(self				:   int    ,   _A				:   List[str]    ,   _A				:   Union[TextInput, PreTokenizedInput, List[TextInput], List[PreTokenizedInput]] = None    ,   _A				:   Optional[Union[PreTokenizedInput, List[PreTokenizedInput]]] = None    ,   _A				:   Union[List[List[int]], List[List[List[int]]]] = None    ,   _A				:   Optional[Union[List[int], List[List[int]]]] = None    ,   _A				:   bool = True    ,   _A				:   Union[bool, str, PaddingStrategy] = False    ,   _A				:   Union[bool, str, TruncationStrategy] = None    ,   _A				:   Optional[int] = None    ,   _A				:   int = 0    ,   _A				:   Optional[int] = None    ,   _A				:   Optional[bool] = None    ,   _A				:   Optional[bool] = None    ,   _A				:   bool = False    ,   _A				:   bool = False    ,   _A				:   bool = False    ,   _A				:   bool = False    ,   _A				:   bool = True    ,   _A				:   Optional[Union[str, TensorType]] = None    ,   **_A				:   Dict    ,   )						->						BatchEncoding:
         # verify input
         if self.image_processor.apply_ocr and (boxes is not None):
             raise ValueError(
                 "You cannot provide bounding boxes "
                 "if you initialized the image processor with apply_ocr set to True."       )

         if self.image_processor.apply_ocr and (word_labels is not None):
             raise ValueError(
                 "You cannot provide word labels if you initialized the image processor with apply_ocr set to True."       )

         if return_overflowing_tokens is True and return_offsets_mapping is False:
             raise ValueError("You cannot return overflowing tokens without returning the offsets mapping."       )

         # first, apply the image processor
         snake_case								= self.image_processor(images=_A    ,   return_tensors=_A       )

         # second, apply the tokenizer
         if text is not None and self.image_processor.apply_ocr and text_pair is None:
             if isinstance(_A    ,   _A       ):
                 snake_case								= [text]  # add batch dimension (as the image processor always adds a batch dimension)
             snake_case								= features["words"]

         snake_case								= self.tokenizer(
             text=text if text is not None else features["words"]    ,   text_pair=text_pair if text_pair is not None else None    ,   boxes=boxes if boxes is not None else features["boxes"]    ,   word_labels=_A    ,   add_special_tokens=_A    ,   padding=_A    ,   truncation=_A    ,   max_length=_A    ,   stride=_A    ,   pad_to_multiple_of=_A    ,   return_token_type_ids=_A    ,   return_attention_mask=_A    ,   return_overflowing_tokens=_A    ,   return_special_tokens_mask=_A    ,   return_offsets_mapping=_A    ,   return_length=_A    ,   verbose=_A    ,   return_tensors=_A    ,   **_A    ,   )

         # add pixel values
         snake_case								= features.pop("pixel_values"       )
         if return_overflowing_tokens is True:
             snake_case								= self.get_overflowing_images(_A    ,   encoded_inputs["overflow_to_sample_mapping"]       )
         snake_case								= images

         return encoded_inputs





     def 		UpperCAmelCase(self				:   Dict    ,   _A				:   Dict    ,   _A				:   List[str]       )						->						Optional[int]:
         # in case there's an overflow, ensure each `input_ids` sample is mapped to its corresponding image
         snake_case								= []
         for sample_idx in overflow_to_sample_mapping:
             images_with_overflow.append(images[sample_idx]       )

         if len(_A       ) != len(_A       ):
             raise ValueError(
                 "Expected length of images to be the same as the length of `overflow_to_sample_mapping`, but got"
                 f' {len(_A       )} and {len(_A       )}'       )

         return images_with_overflow





     def 		UpperCAmelCase(self				:   Tuple    ,   *_A				:   int    ,   **_A				:   Dict       )						->						str:
         return self.tokenizer.batch_decode(*_A    ,   **_A       )





     def 		UpperCAmelCase(self				:   str    ,   *_A				:   List[Any]    ,   **_A				:   List[Any]       )						->						Optional[Any]:
         return self.tokenizer.decode(*_A    ,   **_A       )





     @property
     def 		UpperCAmelCase(self				:   Tuple       )						->						Optional[int]:
         return ["input_ids", "bbox", "attention_mask", "image"]





     @property
     def 		UpperCAmelCase(self				:   List[Any]       )						->						int:
         warnings.warn(
             "`feature_extractor_class` is deprecated and will be removed in v5. Use `image_processor_class` instead."    ,   _A    ,   )
         return self.image_processor_class





     @property
     def 		UpperCAmelCase(self				:   Dict       )						->						Union[str, Any]:
         warnings.warn(
             "`feature_extractor` is deprecated and will be removed in v5. Use `image_processor` instead."    ,   _A    ,   )
         return self.image_processor

