


'''simple docstring'''

import unittest

from transformers import DebertaVaConfig, 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 (
     DebertaVaForMaskedLM,
     DebertaVaForMultipleChoice,
     DebertaVaForQuestionAnswering,
     DebertaVaForSequenceClassification,
     DebertaVaForTokenClassification,
     DebertaVaModel,
 )
 from transformers.models.deberta_va.modeling_deberta_va import DEBERTA_V2_PRETRAINED_MODEL_ARCHIVE_LIST
class     __lowerCAmelCase		(lowercase_	):





      '''simple docstring'''






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

             '''simple docstring'''







             lowercase__      =					parent
             lowercase__      =					batch_size
             lowercase__      =					seq_length
             lowercase__      =					is_training
             lowercase__      =					use_input_mask
             lowercase__      =					use_token_type_ids
             lowercase__      =					use_labels
             lowercase__      =					vocab_size
             lowercase__      =					hidden_size
             lowercase__      =					num_hidden_layers
             lowercase__      =					num_attention_heads
             lowercase__      =					intermediate_size
             lowercase__      =					hidden_act
             lowercase__      =					hidden_dropout_prob
             lowercase__      =					attention_probs_dropout_prob
             lowercase__      =					max_position_embeddings
             lowercase__      =					type_vocab_size
             lowercase__      =					type_sequence_label_size
             lowercase__      =					initializer_range
             lowercase__      =					num_labels
             lowercase__      =					num_choices
             lowercase__      =					relative_attention
             lowercase__      =					position_biased_input
             lowercase__      =					pos_att_type
             lowercase__      =					scope



      def  UpperCamelCase__						(self       :       Any					):

             '''simple docstring'''







             lowercase__      =					ids_tensor([self.batch_size, self.seq_length]  ,		self.vocab_size					)

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

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

             lowercase__      =					None
             lowercase__      =					None
             lowercase__      =					None
             if self.use_labels:
                    lowercase__      =					ids_tensor([self.batch_size]  ,		self.type_sequence_label_size					)
                    lowercase__      =					ids_tensor([self.batch_size, self.seq_length]  ,		self.num_labels					)
                    lowercase__      =					ids_tensor([self.batch_size]  ,		self.num_choices					)

             lowercase__      =					self.get_config()

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



      def  UpperCamelCase__						(self       :       Optional[int]					):

             '''simple docstring'''







             return 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  ,		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       :       str  ,		UpperCamelCase       :       int					):

             '''simple docstring'''







             self.parent.assertListEqual(list(result.loss.size()					)  ,		[]					)



      def  UpperCamelCase__						(self       :       Union[str, Any]  ,		UpperCamelCase       :       List[str]  ,		UpperCamelCase       :       int  ,		UpperCamelCase       :       List[Any]  ,		UpperCamelCase       :       List[str]  ,		UpperCamelCase       :       Dict  ,		UpperCamelCase       :       Tuple  ,		UpperCamelCase       :       str					):

             '''simple docstring'''







             lowercase__      =					DebertaVaModel(config=UpperCamelCase					)
             model.to(UpperCamelCase					)
             model.eval()
             lowercase__      =					model(UpperCamelCase  ,		attention_mask=UpperCamelCase  ,		token_type_ids=UpperCamelCase					)[0]
             lowercase__      =					model(UpperCamelCase  ,		token_type_ids=UpperCamelCase					)[0]
             lowercase__      =					model(UpperCamelCase					)[0]

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



      def  UpperCamelCase__						(self       :       Optional[Any]  ,		UpperCamelCase       :       int  ,		UpperCamelCase       :       Any  ,		UpperCamelCase       :       Optional[int]  ,		UpperCamelCase       :       Any  ,		UpperCamelCase       :       int  ,		UpperCamelCase       :       int  ,		UpperCamelCase       :       Dict					):

             '''simple docstring'''







             lowercase__      =					DebertaVaForMaskedLM(config=UpperCamelCase					)
             model.to(UpperCamelCase					)
             model.eval()
             lowercase__      =					model(UpperCamelCase  ,		attention_mask=UpperCamelCase  ,		token_type_ids=UpperCamelCase  ,		labels=UpperCamelCase					)

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



      def  UpperCamelCase__						(self       :       str  ,		UpperCamelCase       :       Optional[int]  ,		UpperCamelCase       :       str  ,		UpperCamelCase       :       str  ,		UpperCamelCase       :       Tuple  ,		UpperCamelCase       :       Optional[int]  ,		UpperCamelCase       :       str  ,		UpperCamelCase       :       int					):

             '''simple docstring'''







             lowercase__      =					self.num_labels
             lowercase__      =					DebertaVaForSequenceClassification(UpperCamelCase					)
             model.to(UpperCamelCase					)
             model.eval()
             lowercase__      =					model(UpperCamelCase  ,		attention_mask=UpperCamelCase  ,		token_type_ids=UpperCamelCase  ,		labels=UpperCamelCase					)
             self.parent.assertListEqual(list(result.logits.size()					)  ,		[self.batch_size, self.num_labels]					)
             self.check_loss_output(UpperCamelCase					)



      def  UpperCamelCase__						(self       :       Tuple  ,		UpperCamelCase       :       List[Any]  ,		UpperCamelCase       :       Any  ,		UpperCamelCase       :       Optional[int]  ,		UpperCamelCase       :       Any  ,		UpperCamelCase       :       Dict  ,		UpperCamelCase       :       int  ,		UpperCamelCase       :       Tuple					):

             '''simple docstring'''







             lowercase__      =					self.num_labels
             lowercase__      =					DebertaVaForTokenClassification(config=UpperCamelCase					)
             model.to(UpperCamelCase					)
             model.eval()
             lowercase__      =					model(UpperCamelCase  ,		attention_mask=UpperCamelCase  ,		token_type_ids=UpperCamelCase  ,		labels=UpperCamelCase					)
             self.parent.assertEqual(result.logits.shape  ,		(self.batch_size, self.seq_length, self.num_labels)					)



      def  UpperCamelCase__						(self       :       Any  ,		UpperCamelCase       :       Union[str, Any]  ,		UpperCamelCase       :       Dict  ,		UpperCamelCase       :       List[str]  ,		UpperCamelCase       :       Union[str, Any]  ,		UpperCamelCase       :       int  ,		UpperCamelCase       :       List[str]  ,		UpperCamelCase       :       List[str]					):

             '''simple docstring'''







             lowercase__      =					DebertaVaForQuestionAnswering(config=UpperCamelCase					)
             model.to(UpperCamelCase					)
             model.eval()
             lowercase__      =					model(
                 UpperCamelCase  ,		attention_mask=UpperCamelCase  ,		token_type_ids=UpperCamelCase  ,		start_positions=UpperCamelCase  ,		end_positions=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  UpperCamelCase__						(self       :       List[Any]  ,		UpperCamelCase       :       Dict  ,		UpperCamelCase       :       List[Any]  ,		UpperCamelCase       :       str  ,		UpperCamelCase       :       Any  ,		UpperCamelCase       :       Any  ,		UpperCamelCase       :       Dict  ,		UpperCamelCase       :       List[str]					):

             '''simple docstring'''







             lowercase__      =					DebertaVaForMultipleChoice(config=UpperCamelCase					)
             model.to(UpperCamelCase					)
             model.eval()
             lowercase__      =					input_ids.unsqueeze(1					).expand(-1  ,		self.num_choices  ,		-1					).contiguous()
             lowercase__      =					token_type_ids.unsqueeze(1					).expand(-1  ,		self.num_choices  ,		-1					).contiguous()
             lowercase__      =					input_mask.unsqueeze(1					).expand(-1  ,		self.num_choices  ,		-1					).contiguous()
             lowercase__      =					model(
                 UpperCamelCase  ,		attention_mask=UpperCamelCase  ,		token_type_ids=UpperCamelCase  ,		labels=UpperCamelCase  ,		)
             self.parent.assertEqual(result.logits.shape  ,		(self.batch_size, self.num_choices)					)






      def  UpperCamelCase__						(self       :       Any					):

             '''simple docstring'''







             lowercase__      =					self.prepare_config_and_inputs()
             (
                 (
                 lowercase__
             )     ,(
                 lowercase__
             )     ,(
                 lowercase__
             )     ,(
                 lowercase__
             )     ,(
                 lowercase__
             )     ,(
                 lowercase__
             )     ,(
                 lowercase__
             )     ,
             )      =					config_and_inputs
             lowercase__      =					{'''input_ids''': input_ids, '''token_type_ids''': token_type_ids, '''attention_mask''': input_mask}
             return config, inputs_dict






@require_torch
class     __lowerCAmelCase		(lowercase_    ,	lowercase_    ,	unittest.TestCase	):





      '''simple docstring'''






      lowerCAmelCase__   :	List[str]       =				(
          (
              DebertaVaModel,
              DebertaVaForMaskedLM,
              DebertaVaForSequenceClassification,
              DebertaVaForTokenClassification,
              DebertaVaForQuestionAnswering,
              DebertaVaForMultipleChoice,
          )
          if is_torch_available()
          else ()
      )
      lowerCAmelCase__   :	Dict       =				(
          {
              """feature-extraction""": DebertaVaModel,
              """fill-mask""": DebertaVaForMaskedLM,
              """question-answering""": DebertaVaForQuestionAnswering,
              """text-classification""": DebertaVaForSequenceClassification,
              """token-classification""": DebertaVaForTokenClassification,
              """zero-shot""": DebertaVaForSequenceClassification,
          }
          if is_torch_available()
          else {}
      )

      lowerCAmelCase__   :	str       =				True
      lowerCAmelCase__   :	Dict       =				False
      lowerCAmelCase__   :	Tuple       =				False
      lowerCAmelCase__   :	str       =				False
      lowerCAmelCase__   :	Tuple       =				False



      def  UpperCamelCase__						(self       :       int					):

             '''simple docstring'''







             lowercase__      =					DebertaVaModelTester(self					)
             lowercase__      =					ConfigTester(self  ,		config_class=UpperCamelCase  ,		hidden_size=37					)



      def  UpperCamelCase__						(self       :       str					):

             '''simple docstring'''







             self.config_tester.run_common_tests()



      def  UpperCamelCase__						(self       :       Optional[int]					):

             '''simple docstring'''







             lowercase__      =					self.model_tester.prepare_config_and_inputs()
             self.model_tester.create_and_check_deberta_model(*UpperCamelCase					)



      def  UpperCamelCase__						(self       :       Dict					):

             '''simple docstring'''







             lowercase__      =					self.model_tester.prepare_config_and_inputs()
             self.model_tester.create_and_check_deberta_for_sequence_classification(*UpperCamelCase					)



      def  UpperCamelCase__						(self       :       int					):

             '''simple docstring'''







             lowercase__      =					self.model_tester.prepare_config_and_inputs()
             self.model_tester.create_and_check_deberta_for_masked_lm(*UpperCamelCase					)



      def  UpperCamelCase__						(self       :       Any					):

             '''simple docstring'''







             lowercase__      =					self.model_tester.prepare_config_and_inputs()
             self.model_tester.create_and_check_deberta_for_question_answering(*UpperCamelCase					)



      def  UpperCamelCase__						(self       :       str					):

             '''simple docstring'''







             lowercase__      =					self.model_tester.prepare_config_and_inputs()
             self.model_tester.create_and_check_deberta_for_token_classification(*UpperCamelCase					)



      def  UpperCamelCase__						(self       :       int					):

             '''simple docstring'''







             lowercase__      =					self.model_tester.prepare_config_and_inputs()
             self.model_tester.create_and_check_deberta_for_multiple_choice(*UpperCamelCase					)






      @slow
      def  UpperCamelCase__						(self       :       Optional[int]					):

             '''simple docstring'''







             for model_name in DEBERTA_V2_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
                    lowercase__      =					DebertaVaModel.from_pretrained(UpperCamelCase					)
                    self.assertIsNotNone(UpperCamelCase					)






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





      '''simple docstring'''






      @unittest.skip(reason='''Model not available yet'''					)
      def  UpperCamelCase__						(self       :       str					):

             '''simple docstring'''







             pass






      @slow
      def  UpperCamelCase__						(self       :       str					):

             '''simple docstring'''







             lowercase__      =					DebertaVaModel.from_pretrained('''microsoft/deberta-v2-xlarge'''					)

             lowercase__      =					torch.tensor([[0, 31414, 232, 328, 740, 1140, 12695, 69, 46078, 1588, 2]]					)
             lowercase__      =					torch.tensor([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]					)
             with torch.no_grad():
                    lowercase__      =					model(UpperCamelCase  ,		attention_mask=UpperCamelCase					)[0]
             # compare the actual values for a slice.
             lowercase__      =					torch.tensor(
                 [[[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]]]					)
             self.assertTrue(torch.allclose(output[:, 1:4, 1:4]  ,		UpperCamelCase  ,		atol=1E-4					)  ,		f"{output[:, 1:4, 1:4]}"					)








'''simple docstring'''

from typing import Optional, Union

import numpy as np

from ...image_processing_utils import BaseImageProcessor, BatchFeature
from ...image_transforms import get_image_size, pad, rescale, to_channel_dimension_format
from ...image_utils import ChannelDimension, ImageInput, make_list_of_images, to_numpy_array, valid_images
from ...utils import TensorType, logging


lowerCamelCase				:  Optional[int]    		=      logging.get_logger(__name__)
class     __lowerCAmelCase		(lowercase_	):





      '''simple docstring'''






      lowerCAmelCase__   :	Optional[int]       =				["""pixel_values"""]



      def __init__(self       :       str  ,		UpperCamelCase       :       bool = True  ,		UpperCamelCase       :       Union[int, float] = 1 / 255  ,		UpperCamelCase       :       bool = True  ,		UpperCamelCase       :       int = 8  ,		**UpperCamelCase       :       Dict  ,		):

             '''simple docstring'''







             super().__init__(**UpperCamelCase					)

             lowercase__      =					do_rescale
             lowercase__      =					rescale_factor
             lowercase__      =					do_pad
             lowercase__      =					pad_size



      def  UpperCamelCase__						(self       :       Dict  ,		UpperCamelCase       :       np.ndarray  ,		UpperCamelCase       :       float  ,		UpperCamelCase       :       Optional[Union[str, ChannelDimension]] = None  ,		**UpperCamelCase       :       Tuple					):

             '''simple docstring'''







             return rescale(UpperCamelCase  ,		scale=UpperCamelCase  ,		data_format=UpperCamelCase  ,		**UpperCamelCase					)



      def  UpperCamelCase__						(self       :       Tuple  ,		UpperCamelCase       :       np.ndarray  ,		UpperCamelCase       :       int  ,		UpperCamelCase       :       Optional[Union[str, ChannelDimension]] = None					):

             '''simple docstring'''







             lowercase__     ,lowercase__      =					get_image_size(UpperCamelCase					)
             lowercase__      =					(old_height // size + 1) * size - old_height
             lowercase__      =					(old_width // size + 1) * size - old_width

             return pad(UpperCamelCase  ,		((0, pad_height), (0, pad_width))  ,		mode='''symmetric'''  ,		data_format=UpperCamelCase					)






      def  UpperCamelCase__						(self       :       Optional[int]  ,		UpperCamelCase       :       ImageInput  ,		UpperCamelCase       :       Optional[bool] = None  ,		UpperCamelCase       :       Optional[float] = None  ,		UpperCamelCase       :       Optional[bool] = None  ,		UpperCamelCase       :       Optional[int] = None  ,		UpperCamelCase       :       Optional[Union[str, TensorType]] = None  ,		UpperCamelCase       :       Union[str, ChannelDimension] = ChannelDimension.FIRST  ,		**UpperCamelCase       :       int  ,		):

             '''simple docstring'''







             lowercase__      =					do_rescale if do_rescale is not None else self.do_rescale
             lowercase__      =					rescale_factor if rescale_factor is not None else self.rescale_factor
             lowercase__      =					do_pad if do_pad is not None else self.do_pad
             lowercase__      =					pad_size if pad_size is not None else self.pad_size

             lowercase__      =					make_list_of_images(UpperCamelCase					)

             if not valid_images(UpperCamelCase					):
                    raise ValueError(
                        '''Invalid image type. Must be of type PIL.Image.Image, numpy.ndarray, '''
                        '''torch.Tensor, tf.Tensor or jax.ndarray.'''					)

             if do_rescale and rescale_factor is None:
                    raise ValueError('''Rescale factor must be specified if do_rescale is True.'''					)

             # All transformations expect numpy arrays.
             lowercase__      =					[to_numpy_array(UpperCamelCase					) for image in images]

             if do_rescale:
                    lowercase__      =					[self.rescale(image=UpperCamelCase  ,		scale=UpperCamelCase					) for image in images]

             if do_pad:
                    lowercase__      =					[self.pad(UpperCamelCase  ,		size=UpperCamelCase					) for image in images]

             lowercase__      =					[to_channel_dimension_format(UpperCamelCase  ,		UpperCamelCase					) for image in images]

             lowercase__      =					{'''pixel_values''': images}
             return BatchFeature(data=UpperCamelCase  ,		tensor_type=UpperCamelCase					)




