



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


      '''simple docstring'''



      def __init__( self     ,	A_     ,	A_=13     ,	A_=7     ,	A_=True     ,	A_=True     ,	A_=True     ,	A_=True     ,	A_=99     ,	A_=32     ,	A_=5     ,	A_=4     ,	A_=37     ,	A_="gelu"     ,	A_=0.1     ,	A_=0.1     ,	A_=512     ,	A_=16     ,	A_=2     ,	A_=0.0_2     ,	A_=False     ,	A_=True     ,	A_="None"     ,	A_=3     ,	A_=4     ,	A_=None     ,	)     ->     Union[str, Any]:
       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     __snake_case   ( self				)     ->     Optional[Any]:
       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     __snake_case   ( self				)     ->     Union[str, Any]:
       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     __snake_case   ( self     ,	A_				)     ->     Tuple:
       self.parent.assertListEqual(list(result.loss.size()				)     ,	[]				)


      def     __snake_case   ( self     ,	A_     ,	A_     ,	A_     ,	A_     ,	A_     ,	A_     ,	A_				)     ->     Optional[int]:
       lowerCAmelCase   =     DebertaVaModel(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     __snake_case   ( self     ,	A_     ,	A_     ,	A_     ,	A_     ,	A_     ,	A_     ,	A_				)     ->     Union[str, Any]:
       lowerCAmelCase   =     DebertaVaForMaskedLM(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     __snake_case   ( self     ,	A_     ,	A_     ,	A_     ,	A_     ,	A_     ,	A_     ,	A_				)     ->     Optional[Any]:
       lowerCAmelCase   =     self.num_labels
       lowerCAmelCase   =     DebertaVaForSequenceClassification(__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     __snake_case   ( self     ,	A_     ,	A_     ,	A_     ,	A_     ,	A_     ,	A_     ,	A_				)     ->     Optional[Any]:
       lowerCAmelCase   =     self.num_labels
       lowerCAmelCase   =     DebertaVaForTokenClassification(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     __snake_case   ( self     ,	A_     ,	A_     ,	A_     ,	A_     ,	A_     ,	A_     ,	A_				)     ->     Dict:
       lowerCAmelCase   =     DebertaVaForQuestionAnswering(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     __snake_case   ( self     ,	A_     ,	A_     ,	A_     ,	A_     ,	A_     ,	A_     ,	A_				)     ->     List[Any]:
       lowerCAmelCase   =     DebertaVaForMultipleChoice(config=__A				)
       model.to(__A				)
       model.eval()
       lowerCAmelCase   =     input_ids.unsqueeze(1				).expand(-1     ,	self.num_choices     ,	-1				).contiguous()
       lowerCAmelCase   =     token_type_ids.unsqueeze(1				).expand(-1     ,	self.num_choices     ,	-1				).contiguous()
       lowerCAmelCase   =     input_mask.unsqueeze(1				).expand(-1     ,	self.num_choices     ,	-1				).contiguous()
       lowerCAmelCase   =     model(
           __A     ,	attention_mask=__A     ,	token_type_ids=__A     ,	labels=__A     ,	)
       self.parent.assertEqual(result.logits.shape     ,	(self.batch_size, self.num_choices)				)




      def     __snake_case   ( self				)     ->     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 __snake_case(				lowerCamelCase__		,							lowerCamelCase__		,							unittest.TestCase ):


      '''simple docstring'''



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

      UpperCAmelCase							:   Dict									=		True
      UpperCAmelCase							:   Optional[int]									=		False
      UpperCAmelCase							:   List[str]									=		False
      UpperCAmelCase							:   Optional[int]									=		False
      UpperCAmelCase							:   Optional[int]									=		False


      def     __snake_case   ( self				)     ->     Union[str, Any]:
       lowerCAmelCase   =     DebertaVaModelTester(self				)
       lowerCAmelCase   =     ConfigTester(self     ,	config_class=__A     ,	hidden_size=37				)


      def     __snake_case   ( self				)     ->     Optional[Any]:
       self.config_tester.run_common_tests()


      def     __snake_case   ( self				)     ->     Dict:
       lowerCAmelCase   =     self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_deberta_model(*__A				)


      def     __snake_case   ( self				)     ->     int:
       lowerCAmelCase   =     self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_deberta_for_sequence_classification(*__A				)


      def     __snake_case   ( self				)     ->     Optional[int]:
       lowerCAmelCase   =     self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_deberta_for_masked_lm(*__A				)


      def     __snake_case   ( self				)     ->     str:
       lowerCAmelCase   =     self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_deberta_for_question_answering(*__A				)


      def     __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				)


      def     __snake_case   ( self				)     ->     str:
       lowerCAmelCase   =     self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_deberta_for_multiple_choice(*__A				)




      @slow
      def     __snake_case   ( self				)     ->     int:
       for model_name in DEBERTA_V2_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
        lowerCAmelCase   =     DebertaVaModel.from_pretrained(__A				)
        self.assertIsNotNone(__A				)







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


      '''simple docstring'''



      @unittest.skip(reason="""Model not available yet"""				)
      def     __snake_case   ( self				)     ->     Tuple:
       pass




      @slow
      def     __snake_case   ( self				)     ->     List[str]:
       lowerCAmelCase   =     DebertaVaModel.from_pretrained("""microsoft/deberta-v2-xlarge"""				)

       lowerCAmelCase   =     torch.tensor([[0, 3_1414, 232, 328, 740, 1140, 1_2695, 69, 4_6078, 1588, 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.2_3_5_6, 0.1_9_4_8, 0.0_3_6_9], [-0.1_0_6_3, 0.3_5_8_6, -0.5_1_5_2], [-0.6_3_9_9, -0.0_2_5_9, -0.2_5_2_5]]]				)
       self.assertTrue(torch.allclose(output[:, 1:4, 1:4]     ,	__A     ,	atol=1e-4				)     ,	f'{output[:, 1:4, 1:4]}'				)




'''simple docstring'''

import unittest

from transformers import is_torch_available, is_vision_available
from transformers.testing_utils import require_torch, require_vision, slow, torch_device


if is_torch_available():
				import torch

				from transformers import AutoModelForImageClassification

if is_vision_available():
				from transformers import AutoImageProcessor

@require_torch
@require_vision
class __snake_case(				unittest.TestCase ):


						'''simple docstring'''



						@slow
						def     __snake_case   ( self				)     ->     List[str]:
							lowerCAmelCase   =     AutoImageProcessor.from_pretrained("""microsoft/dit-base-finetuned-rvlcdip"""				)
							lowerCAmelCase   =     AutoModelForImageClassification.from_pretrained("""microsoft/dit-base-finetuned-rvlcdip"""				)
							model.to(A_				)

							from datasets import load_dataset

							lowerCAmelCase   =     load_dataset("""nielsr/rvlcdip-demo"""				)

							lowerCAmelCase   =     dataset["""train"""][0]["""image"""].convert("""RGB"""				)

							lowerCAmelCase   =     image_processor(A_     ,	return_tensors="""pt"""				).to(A_				)

							# forward pass
							with torch.no_grad():
								lowerCAmelCase   =     model(**A_				)
								lowerCAmelCase   =     outputs.logits

							lowerCAmelCase   =     torch.Size((1, 16)				)
							self.assertEqual(logits.shape     ,	A_				)

							lowerCAmelCase   =     torch.tensor(
							    [-0.4_1_5_8, -0.4_0_9_2, -0.4_3_4_7]     ,	device=A_     ,	dtype=torch.float     ,	)
							self.assertTrue(torch.allclose(logits[0, :3]     ,	A_     ,	atol=1e-4				)				)