




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






       def __init__(   self:					int       ,					_lowerCAmelCase:					int       ,					_lowerCAmelCase:					Union[str, Any]=13       ,					_lowerCAmelCase:					List[Any]=7       ,					_lowerCAmelCase:					Tuple=True       ,					_lowerCAmelCase:					Optional[int]=True       ,					_lowerCAmelCase:					List[Any]=True       ,					_lowerCAmelCase:					Union[str, Any]=True       ,					_lowerCAmelCase:					Optional[int]=99       ,					_lowerCAmelCase:					int=32       ,					_lowerCAmelCase:					Tuple=2       ,					_lowerCAmelCase:					int=4       ,					_lowerCAmelCase:					Dict=37       ,					_lowerCAmelCase:					Optional[Any]="gelu"       ,					_lowerCAmelCase:					Optional[int]=0.1       ,					_lowerCAmelCase:					Optional[int]=0.1       ,					_lowerCAmelCase:					Dict=5_12       ,					_lowerCAmelCase:					Dict=16       ,					_lowerCAmelCase:					str=2       ,					_lowerCAmelCase:					Optional[Any]=0.02       ,					_lowerCAmelCase:					Any=False       ,					_lowerCAmelCase:					Dict=True       ,					_lowerCAmelCase:					str="None"       ,					_lowerCAmelCase:					List[str]=3       ,					_lowerCAmelCase:					Optional[Any]=4       ,					_lowerCAmelCase:					Dict=None       ,					):
         lowercase    :Union[str, Any]      	=						parent
         lowercase    :Union[str, Any]      	=						batch_size
         lowercase    :List[str]      	=						seq_length
         lowercase    :List[Any]      	=						is_training
         lowercase    :Dict      	=						use_input_mask
         lowercase    :Dict      	=						use_token_type_ids
         lowercase    :Optional[int]      	=						use_labels
         lowercase    :Tuple      	=						vocab_size
         lowercase    :str      	=						hidden_size
         lowercase    :Any      	=						num_hidden_layers
         lowercase    :List[Any]      	=						num_attention_heads
         lowercase    :Union[str, Any]      	=						intermediate_size
         lowercase    :str      	=						hidden_act
         lowercase    :List[str]      	=						hidden_dropout_prob
         lowercase    :Optional[int]      	=						attention_probs_dropout_prob
         lowercase    :List[Any]      	=						max_position_embeddings
         lowercase    :List[Any]      	=						type_vocab_size
         lowercase    :List[Any]      	=						type_sequence_label_size
         lowercase    :Tuple      	=						initializer_range
         lowercase    :List[Any]      	=						num_labels
         lowercase    :List[Any]      	=						num_choices
         lowercase    :List[str]      	=						relative_attention
         lowercase    :List[Any]      	=						position_biased_input
         lowercase    :str      	=						pos_att_type
         lowercase    :List[str]      	=						scope






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

         lowercase    :Dict      	=						None
         if self.use_input_mask:
           lowercase    :List[str]      	=						random_attention_mask([self.batch_size, self.seq_length]				)

         lowercase    :List[str]      	=						None
         if self.use_token_type_ids:
           lowercase    :Optional[Any]      	=						ids_tensor([self.batch_size, self.seq_length]       ,					self.type_vocab_size				)

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

         lowercase    :str      	=						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=snake_case_       ,					)

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






       def 					SCREAMING_SNAKE_CASE    (   self:					Optional[int]       ,					_lowerCAmelCase:					str       ,					_lowerCAmelCase:					Any       ,					_lowerCAmelCase:					Tuple       ,					_lowerCAmelCase:					Tuple       ,					_lowerCAmelCase:					int       ,					_lowerCAmelCase:					List[str]       ,					_lowerCAmelCase:					Tuple				):
         lowercase    :Dict      	=						TFDebertaVaModel(config=snake_case_				)
         lowercase    :Optional[int]      	=						{"input_ids": input_ids, "attention_mask": input_mask, "token_type_ids": token_type_ids}

         lowercase    :Dict      	=						[input_ids, input_mask]
         lowercase    :int      	=						model(snake_case_				)

         lowercase    :str      	=						model(snake_case_				)

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






       def 					SCREAMING_SNAKE_CASE    (   self:					Tuple       ,					_lowerCAmelCase:					Union[str, Any]       ,					_lowerCAmelCase:					Dict       ,					_lowerCAmelCase:					Tuple       ,					_lowerCAmelCase:					str       ,					_lowerCAmelCase:					Tuple       ,					_lowerCAmelCase:					Optional[int]       ,					_lowerCAmelCase:					str				):
         lowercase    :List[str]      	=						TFDebertaVaForMaskedLM(config=snake_case_				)
         lowercase    :List[str]      	=						{
             "input_ids": input_ids,
             "attention_mask": input_mask,
             "token_type_ids": token_type_ids,
         }
         lowercase    :List[str]      	=						model(snake_case_				)
         self.parent.assertEqual(result.logits.shape       ,					(self.batch_size, self.seq_length, self.vocab_size)				)






       def 					SCREAMING_SNAKE_CASE    (   self:					List[Any]       ,					_lowerCAmelCase:					Optional[int]       ,					_lowerCAmelCase:					Any       ,					_lowerCAmelCase:					int       ,					_lowerCAmelCase:					Union[str, Any]       ,					_lowerCAmelCase:					Optional[int]       ,					_lowerCAmelCase:					Optional[Any]       ,					_lowerCAmelCase:					Tuple				):
         lowercase    :Optional[int]      	=						self.num_labels
         lowercase    :str      	=						TFDebertaVaForSequenceClassification(config=snake_case_				)
         lowercase    :List[Any]      	=						{
             "input_ids": input_ids,
             "attention_mask": input_mask,
             "token_type_ids": token_type_ids,
         }

         lowercase    :List[str]      	=						model(snake_case_				)
         self.parent.assertEqual(result.logits.shape       ,					(self.batch_size, self.num_labels)				)






       def 					SCREAMING_SNAKE_CASE    (   self:					Any       ,					_lowerCAmelCase:					List[Any]       ,					_lowerCAmelCase:					List[Any]       ,					_lowerCAmelCase:					str       ,					_lowerCAmelCase:					Optional[int]       ,					_lowerCAmelCase:					List[str]       ,					_lowerCAmelCase:					int       ,					_lowerCAmelCase:					List[Any]				):
         lowercase    :Tuple      	=						self.num_labels
         lowercase    :Tuple      	=						TFDebertaVaForTokenClassification(config=snake_case_				)
         lowercase    :Dict      	=						{
             "input_ids": input_ids,
             "attention_mask": input_mask,
             "token_type_ids": token_type_ids,
         }
         lowercase    :str      	=						model(snake_case_				)
         self.parent.assertEqual(result.logits.shape       ,					(self.batch_size, self.seq_length, self.num_labels)				)






       def 					SCREAMING_SNAKE_CASE    (   self:					List[Any]       ,					_lowerCAmelCase:					str       ,					_lowerCAmelCase:					Optional[int]       ,					_lowerCAmelCase:					Dict       ,					_lowerCAmelCase:					int       ,					_lowerCAmelCase:					Optional[Any]       ,					_lowerCAmelCase:					Optional[int]       ,					_lowerCAmelCase:					int				):
         lowercase    :Tuple      	=						TFDebertaVaForQuestionAnswering(config=snake_case_				)
         lowercase    :Dict      	=						{
             "input_ids": input_ids,
             "attention_mask": input_mask,
             "token_type_ids": token_type_ids,
         }

         lowercase    :Union[str, Any]      	=						model(snake_case_				)
         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:					str				):
         lowercase    :str      	=						self.prepare_config_and_inputs()
         (
             lowercase					
         )    :List[Any]      	=						config_and_inputs
         lowercase    :List[Any]      	=						{"input_ids": input_ids, "token_type_ids": token_type_ids, "attention_mask": input_mask}
         return config, inputs_dict







@require_tf
class    a__       (							_snake_case      , _snake_case      , unittest.TestCase):
       _a 					=						(
           (
               TFDebertaVaModel,
               TFDebertaVaForMaskedLM,
               TFDebertaVaForQuestionAnswering,
               TFDebertaVaForSequenceClassification,
               TFDebertaVaForTokenClassification,
           )
           if is_tf_available()
           else ()
       )
       _a 					=						(
           {
               """feature-extraction""": TFDebertaVaModel,
               """fill-mask""": TFDebertaVaForMaskedLM,
               """question-answering""": TFDebertaVaForQuestionAnswering,
               """text-classification""": TFDebertaVaForSequenceClassification,
               """token-classification""": TFDebertaVaForTokenClassification,
               """zero-shot""": TFDebertaVaForSequenceClassification,
           }
           if is_tf_available()
           else {}
       )

       _a 					=						False
       _a 					=						False






       def 					SCREAMING_SNAKE_CASE    (   self:					Optional[Any]				):
         lowercase    :Any      	=						TFDebertaVaModelTester(self				)
         lowercase    :Dict      	=						ConfigTester(self       ,					config_class=snake_case_       ,					hidden_size=37				)






       def 					SCREAMING_SNAKE_CASE    (   self:					int				):
         self.config_tester.run_common_tests()






       def 					SCREAMING_SNAKE_CASE    (   self:					List[str]				):
         lowercase    :List[str]      	=						self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_model(*snake_case_				)






       def 					SCREAMING_SNAKE_CASE    (   self:					Union[str, Any]				):
         lowercase    :int      	=						self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_for_masked_lm(*snake_case_				)






       def 					SCREAMING_SNAKE_CASE    (   self:					Tuple				):
         lowercase    :Dict      	=						self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_for_question_answering(*snake_case_				)






       def 					SCREAMING_SNAKE_CASE    (   self:					Tuple				):
         lowercase    :Union[str, Any]      	=						self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_for_sequence_classification(*snake_case_				)






       def 					SCREAMING_SNAKE_CASE    (   self:					Union[str, Any]				):
         lowercase    :Optional[int]      	=						self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_for_token_classification(*snake_case_				)






       @slow
       def 					SCREAMING_SNAKE_CASE    (   self:					Any				):
         lowercase    :Dict      	=						TFDebertaVaModel.from_pretrained("kamalkraj/deberta-v2-xlarge"				)
         self.assertIsNotNone(snake_case_				)







@require_tf
class    a__       (							unittest.TestCase):






       @unittest.skip(reason="Model not available yet"				)
       def 					SCREAMING_SNAKE_CASE    (   self:					Optional[Any]				):
         pass






       @slow
       def 					SCREAMING_SNAKE_CASE    (   self:					List[Any]				):
         lowercase    :Union[str, Any]      	=						TFDebertaVaModel.from_pretrained("kamalkraj/deberta-v2-xlarge"				)
         lowercase    :Optional[int]      	=						tf.constant([[0, 3_14_14, 2_32, 3_28, 7_40, 11_40, 1_26_95, 69, 4_60_78, 15_88, 2]]				)
         lowercase    :List[str]      	=						tf.constant([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]				)
         lowercase    :str      	=						model(snake_case_       ,					attention_mask=snake_case_				)[0]

         lowercase    :Optional[Any]      	=						tf.constant(
             [[[0.23_56, 0.19_48, 0.03_69], [-0.10_63, 0.35_86, -0.51_52], [-0.63_99, -0.02_59, -0.25_25]]]				)
         tf.debugging.assert_near(output[:, 1:4, 1:4]       ,					snake_case_       ,					atol=1e-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       (							lowerCAmelCase):
       _a 					=						['''image_processor''', '''tokenizer''']
       _a 					=						'''LayoutLMv2ImageProcessor'''
       _a 					=						('''LayoutXLMTokenizer''', '''LayoutXLMTokenizerFast''')






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

         lowercase    :Union[str, Any]      	=						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__(_lowerCAmelCase       ,					_lowerCAmelCase				)






       def __call__(   self:					List[str]       ,					_lowerCAmelCase:					Optional[Any]       ,					_lowerCAmelCase:					Union[TextInput, PreTokenizedInput, List[TextInput], List[PreTokenizedInput]] = None       ,					_lowerCAmelCase:					Optional[Union[PreTokenizedInput, List[PreTokenizedInput]]] = None       ,					_lowerCAmelCase:					Union[List[List[int]], List[List[List[int]]]] = None       ,					_lowerCAmelCase:					Optional[Union[List[int], List[List[int]]]] = None       ,					_lowerCAmelCase:					bool = True       ,					_lowerCAmelCase:					Union[bool, str, PaddingStrategy] = False       ,					_lowerCAmelCase:					Union[bool, str, TruncationStrategy] = None       ,					_lowerCAmelCase:					Optional[int] = None       ,					_lowerCAmelCase:					int = 0       ,					_lowerCAmelCase:					Optional[int] = None       ,					_lowerCAmelCase:					Optional[bool] = None       ,					_lowerCAmelCase:					Optional[bool] = None       ,					_lowerCAmelCase:					bool = False       ,					_lowerCAmelCase:					bool = False       ,					_lowerCAmelCase:					bool = False       ,					_lowerCAmelCase:					bool = False       ,					_lowerCAmelCase:					bool = True       ,					_lowerCAmelCase:					Optional[Union[str, TensorType]] = None       ,					**_lowerCAmelCase:					Tuple       ,					):
         # 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
         lowercase    :int      	=						self.image_processor(images=_lowerCAmelCase       ,					return_tensors=_lowerCAmelCase				)

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

         lowercase    :Optional[Any]      	=						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=_lowerCAmelCase       ,					add_special_tokens=_lowerCAmelCase       ,					padding=_lowerCAmelCase       ,					truncation=_lowerCAmelCase       ,					max_length=_lowerCAmelCase       ,					stride=_lowerCAmelCase       ,					pad_to_multiple_of=_lowerCAmelCase       ,					return_token_type_ids=_lowerCAmelCase       ,					return_attention_mask=_lowerCAmelCase       ,					return_overflowing_tokens=_lowerCAmelCase       ,					return_special_tokens_mask=_lowerCAmelCase       ,					return_offsets_mapping=_lowerCAmelCase       ,					return_length=_lowerCAmelCase       ,					verbose=_lowerCAmelCase       ,					return_tensors=_lowerCAmelCase       ,					**_lowerCAmelCase       ,					)

         # add pixel values
         lowercase    :Any      	=						features.pop("pixel_values"				)
         if return_overflowing_tokens is True:
           lowercase    :Any      	=						self.get_overflowing_images(_lowerCAmelCase       ,					encoded_inputs["overflow_to_sample_mapping"]				)
         lowercase    :Optional[int]      	=						images

         return encoded_inputs






       def 					SCREAMING_SNAKE_CASE    (   self:					Tuple       ,					_lowerCAmelCase:					Tuple       ,					_lowerCAmelCase:					List[Any]				):
         # in case there's an overflow, ensure each `input_ids` sample is mapped to its corresponding image
         lowercase    :Dict      	=						[]
         for sample_idx in overflow_to_sample_mapping:
           images_with_overflow.append(images[sample_idx]				)

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

         return images_with_overflow






       def 					SCREAMING_SNAKE_CASE    (   self:					Dict       ,					*_lowerCAmelCase:					str       ,					**_lowerCAmelCase:					List[Any]				):
         return self.tokenizer.batch_decode(*_lowerCAmelCase       ,					**_lowerCAmelCase				)






       def 					SCREAMING_SNAKE_CASE    (   self:					str       ,					*_lowerCAmelCase:					Tuple       ,					**_lowerCAmelCase:					Union[str, Any]				):
         return self.tokenizer.decode(*_lowerCAmelCase       ,					**_lowerCAmelCase				)






       @property
       def 					SCREAMING_SNAKE_CASE    (   self:					Optional[Any]				):
         return ["input_ids", "bbox", "attention_mask", "image"]






       @property
       def 					SCREAMING_SNAKE_CASE    (   self:					List[str]				):
         warnings.warn(
             "`feature_extractor_class` is deprecated and will be removed in v5. Use `image_processor_class` instead."       ,					_lowerCAmelCase       ,					)
         return self.image_processor_class






       @property
       def 					SCREAMING_SNAKE_CASE    (   self:					List[Any]				):
         warnings.warn(
             "`feature_extractor` is deprecated and will be removed in v5. Use `image_processor` instead."       ,					_lowerCAmelCase       ,					)
         return self.image_processor



