


'''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 							__magic_name__	(       lowerCAmelCase ):

     """simple docstring"""






     def __init__(   self			,     lowerCamelCase			,     lowerCamelCase=13			,     lowerCamelCase=7			,     lowerCamelCase=True			,     lowerCamelCase=True			,     lowerCamelCase=True			,     lowerCamelCase=True			,     lowerCamelCase=99			,     lowerCamelCase=32			,     lowerCamelCase=5			,     lowerCamelCase=4			,     lowerCamelCase=37			,     lowerCamelCase="gelu"			,     lowerCamelCase=0.1			,     lowerCamelCase=0.1			,     lowerCamelCase=512			,     lowerCamelCase=16			,     lowerCamelCase=2			,     lowerCamelCase=0.02			,     lowerCamelCase=False			,     lowerCamelCase=True			,     lowerCamelCase="None"			,     lowerCamelCase=3			,     lowerCamelCase=4			,     lowerCamelCase=None			,     ):


          '''simple docstring'''





          __A	:       List[Any]      =							parent
          __A	:       List[str]      =							batch_size
          __A	:       List[str]      =							seq_length
          __A	:       Dict      =							is_training
          __A	:       Tuple      =							use_input_mask
          __A	:       Optional[Any]      =							use_token_type_ids
          __A	:       List[Any]      =							use_labels
          __A	:       Union[str, Any]      =							vocab_size
          __A	:       Union[str, Any]      =							hidden_size
          __A	:       List[Any]      =							num_hidden_layers
          __A	:       str      =							num_attention_heads
          __A	:       Optional[int]      =							intermediate_size
          __A	:       Tuple      =							hidden_act
          __A	:       Optional[int]      =							hidden_dropout_prob
          __A	:       Optional[int]      =							attention_probs_dropout_prob
          __A	:       List[Any]      =							max_position_embeddings
          __A	:       int      =							type_vocab_size
          __A	:       Optional[int]      =							type_sequence_label_size
          __A	:       Tuple      =							initializer_range
          __A	:       List[str]      =							num_labels
          __A	:       Tuple      =							num_choices
          __A	:       List[Any]      =							relative_attention
          __A	:       Dict      =							position_biased_input
          __A	:       Dict      =							pos_att_type
          __A	:       str      =							scope


     def lowerCAmelCase__  (   self		):


          '''simple docstring'''





          __A	:       Dict      =							ids_tensor([self.batch_size, self.seq_length]			,     self.vocab_size		)

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

          __A	:       int      =							None
          if self.use_token_type_ids:
               __A	:       Union[str, Any]      =							ids_tensor([self.batch_size, self.seq_length]			,     self.type_vocab_size		)

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

          __A	:       Tuple      =							self.get_config()

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


     def lowerCAmelCase__  (   self		):


          '''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 lowerCAmelCase__  (   self			,     lowerCamelCase		):


          '''simple docstring'''





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


     def lowerCAmelCase__  (   self			,     lowerCamelCase			,     lowerCamelCase			,     lowerCamelCase			,     lowerCamelCase			,     lowerCamelCase			,     lowerCamelCase			,     lowerCamelCase		):


          '''simple docstring'''





          __A	:       Any      =							DebertaVaModel(config=lowerCamelCase		)
          model.to(lowerCamelCase		)
          model.eval()
          __A	:       Dict      =							model(lowerCamelCase			,     attention_mask=lowerCamelCase			,     token_type_ids=lowerCamelCase		)[0]
          __A	:       Optional[int]      =							model(lowerCamelCase			,     token_type_ids=lowerCamelCase		)[0]
          __A	:       Dict      =							model(lowerCamelCase		)[0]

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


     def lowerCAmelCase__  (   self			,     lowerCamelCase			,     lowerCamelCase			,     lowerCamelCase			,     lowerCamelCase			,     lowerCamelCase			,     lowerCamelCase			,     lowerCamelCase		):


          '''simple docstring'''





          __A	:       List[str]      =							DebertaVaForMaskedLM(config=lowerCamelCase		)
          model.to(lowerCamelCase		)
          model.eval()
          __A	:       Optional[int]      =							model(lowerCamelCase			,     attention_mask=lowerCamelCase			,     token_type_ids=lowerCamelCase			,     labels=lowerCamelCase		)

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


     def lowerCAmelCase__  (   self			,     lowerCamelCase			,     lowerCamelCase			,     lowerCamelCase			,     lowerCamelCase			,     lowerCamelCase			,     lowerCamelCase			,     lowerCamelCase		):


          '''simple docstring'''





          __A	:       List[str]      =							self.num_labels
          __A	:       Any      =							DebertaVaForSequenceClassification(lowerCamelCase		)
          model.to(lowerCamelCase		)
          model.eval()
          __A	:       str      =							model(lowerCamelCase			,     attention_mask=lowerCamelCase			,     token_type_ids=lowerCamelCase			,     labels=lowerCamelCase		)
          self.parent.assertListEqual(list(result.logits.size()		)			,     [self.batch_size, self.num_labels]		)
          self.check_loss_output(lowerCamelCase		)


     def lowerCAmelCase__  (   self			,     lowerCamelCase			,     lowerCamelCase			,     lowerCamelCase			,     lowerCamelCase			,     lowerCamelCase			,     lowerCamelCase			,     lowerCamelCase		):


          '''simple docstring'''





          __A	:       Union[str, Any]      =							self.num_labels
          __A	:       Union[str, Any]      =							DebertaVaForTokenClassification(config=lowerCamelCase		)
          model.to(lowerCamelCase		)
          model.eval()
          __A	:       Optional[Any]      =							model(lowerCamelCase			,     attention_mask=lowerCamelCase			,     token_type_ids=lowerCamelCase			,     labels=lowerCamelCase		)
          self.parent.assertEqual(result.logits.shape			,     (self.batch_size, self.seq_length, self.num_labels)		)


     def lowerCAmelCase__  (   self			,     lowerCamelCase			,     lowerCamelCase			,     lowerCamelCase			,     lowerCamelCase			,     lowerCamelCase			,     lowerCamelCase			,     lowerCamelCase		):


          '''simple docstring'''





          __A	:       Optional[int]      =							DebertaVaForQuestionAnswering(config=lowerCamelCase		)
          model.to(lowerCamelCase		)
          model.eval()
          __A	:       Dict      =							model(
              lowerCamelCase			,     attention_mask=lowerCamelCase			,     token_type_ids=lowerCamelCase			,     start_positions=lowerCamelCase			,     end_positions=lowerCamelCase			,     )
          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 lowerCAmelCase__  (   self			,     lowerCamelCase			,     lowerCamelCase			,     lowerCamelCase			,     lowerCamelCase			,     lowerCamelCase			,     lowerCamelCase			,     lowerCamelCase		):


          '''simple docstring'''





          __A	:       Dict      =							DebertaVaForMultipleChoice(config=lowerCamelCase		)
          model.to(lowerCamelCase		)
          model.eval()
          __A	:       str      =							input_ids.unsqueeze(1		).expand(-1			,     self.num_choices			,     -1		).contiguous()
          __A	:       List[Any]      =							token_type_ids.unsqueeze(1		).expand(-1			,     self.num_choices			,     -1		).contiguous()
          __A	:       Union[str, Any]      =							input_mask.unsqueeze(1		).expand(-1			,     self.num_choices			,     -1		).contiguous()
          __A	:       Optional[Any]      =							model(
              lowerCamelCase			,     attention_mask=lowerCamelCase			,     token_type_ids=lowerCamelCase			,     labels=lowerCamelCase			,     )
          self.parent.assertEqual(result.logits.shape			,     (self.batch_size, self.num_choices)		)


     def lowerCAmelCase__  (   self		):


          '''simple docstring'''





          __A	:       str      =							self.prepare_config_and_inputs()
          (
              (
              __A
          )  ,(
              __A
          )  ,(
              __A
          )  ,(
              __A
          )  ,(
              __A
          )  ,(
              __A
          )  ,(
              __A
          )  ,
          )	:       Dict      =							config_and_inputs
          __A	:       int      =							{"input_ids": input_ids, "token_type_ids": token_type_ids, "attention_mask": input_mask}
          return config, inputs_dict







@require_torch
class 							__magic_name__	(       lowerCAmelCase    ,		lowerCAmelCase    ,		unittest.TestCase ):

     """simple docstring"""






     lowerCamelCase__       						=				(
         (
             DebertaVaModel,
             DebertaVaForMaskedLM,
             DebertaVaForSequenceClassification,
             DebertaVaForTokenClassification,
             DebertaVaForQuestionAnswering,
             DebertaVaForMultipleChoice,
         )
         if is_torch_available()
         else ()
     )
     lowerCamelCase__       						=				(
         {
             'feature-extraction': DebertaVaModel,
             'fill-mask': DebertaVaForMaskedLM,
             'question-answering': DebertaVaForQuestionAnswering,
             'text-classification': DebertaVaForSequenceClassification,
             'token-classification': DebertaVaForTokenClassification,
             'zero-shot': DebertaVaForSequenceClassification,
         }
         if is_torch_available()
         else {}
     )

     lowerCamelCase__       						=				True
     lowerCamelCase__       						=				False
     lowerCamelCase__       						=				False
     lowerCamelCase__       						=				False
     lowerCamelCase__       						=				False


     def lowerCAmelCase__  (   self		):


          '''simple docstring'''





          __A	:       Union[str, Any]      =							DebertaVaModelTester(self		)
          __A	:       Optional[Any]      =							ConfigTester(self			,     config_class=lowerCamelCase			,     hidden_size=37		)


     def lowerCAmelCase__  (   self		):


          '''simple docstring'''





          self.config_tester.run_common_tests()


     def lowerCAmelCase__  (   self		):


          '''simple docstring'''





          __A	:       Any      =							self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_deberta_model(*lowerCamelCase		)


     def lowerCAmelCase__  (   self		):


          '''simple docstring'''





          __A	:       List[str]      =							self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_deberta_for_sequence_classification(*lowerCamelCase		)


     def lowerCAmelCase__  (   self		):


          '''simple docstring'''





          __A	:       Union[str, Any]      =							self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_deberta_for_masked_lm(*lowerCamelCase		)


     def lowerCAmelCase__  (   self		):


          '''simple docstring'''





          __A	:       Optional[Any]      =							self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_deberta_for_question_answering(*lowerCamelCase		)


     def lowerCAmelCase__  (   self		):


          '''simple docstring'''





          __A	:       Tuple      =							self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_deberta_for_token_classification(*lowerCamelCase		)


     def lowerCAmelCase__  (   self		):


          '''simple docstring'''





          __A	:       List[str]      =							self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_deberta_for_multiple_choice(*lowerCamelCase		)


     @slow
     def lowerCAmelCase__  (   self		):


          '''simple docstring'''





          for model_name in DEBERTA_V2_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
               __A	:       Any      =							DebertaVaModel.from_pretrained(lowerCamelCase		)
               self.assertIsNotNone(lowerCamelCase		)







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

     """simple docstring"""






     @unittest.skip(reason="Model not available yet"		)
     def lowerCAmelCase__  (   self		):


          '''simple docstring'''





          pass


     @slow
     def lowerCAmelCase__  (   self		):


          '''simple docstring'''





          __A	:       str      =							DebertaVaModel.from_pretrained("microsoft/deberta-v2-xlarge"		)

          __A	:       str      =							torch.tensor([[0, 3_1414, 232, 328, 740, 1140, 1_2695, 69, 4_6078, 1588, 2]]		)
          __A	:       Optional[Any]      =							torch.tensor([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]		)
          with torch.no_grad():
               __A	:       Optional[Any]      =							model(lowerCamelCase			,     attention_mask=lowerCamelCase		)[0]
          # compare the actual values for a slice.
          __A	:       List[Any]      =							torch.tensor(
              [[[0.2356, 0.1948, 0.0369], [-0.1063, 0.3586, -0.5152], [-0.6399, -0.0259, -0.2525]]]		)
          self.assertTrue(torch.allclose(output[:, 1:4, 1:4]			,     lowerCamelCase			,     atol=1E-4		)			,     f"{output[:, 1:4, 1:4]}"		)





'''simple docstring'''


from typing import TYPE_CHECKING

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


_UpperCamelCase 							=  {"""configuration_vit""": ["""VIT_PRETRAINED_CONFIG_ARCHIVE_MAP""", """ViTConfig""", """ViTOnnxConfig"""]}

try:
      if not is_vision_available():
            raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
      pass
else:
      _UpperCamelCase 							=  ["""ViTFeatureExtractor"""]
      _UpperCamelCase 							=  ["""ViTImageProcessor"""]

try:
      if not is_torch_available():
            raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
      pass
else:
      _UpperCamelCase 							=  [
          """VIT_PRETRAINED_MODEL_ARCHIVE_LIST""",
          """ViTForImageClassification""",
          """ViTForMaskedImageModeling""",
          """ViTModel""",
          """ViTPreTrainedModel""",
      ]

try:
      if not is_tf_available():
            raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
      pass
else:
      _UpperCamelCase 							=  [
          """TFViTForImageClassification""",
          """TFViTModel""",
          """TFViTPreTrainedModel""",
      ]

try:
      if not is_flax_available():
            raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
      pass
else:
      _UpperCamelCase 							=  [
          """FlaxViTForImageClassification""",
          """FlaxViTModel""",
          """FlaxViTPreTrainedModel""",
      ]

if TYPE_CHECKING:
      from .configuration_vit import VIT_PRETRAINED_CONFIG_ARCHIVE_MAP, ViTConfig, ViTOnnxConfig

      try:
            if not is_vision_available():
                  raise OptionalDependencyNotAvailable()
      except OptionalDependencyNotAvailable:
            pass
      else:
            from .feature_extraction_vit import ViTFeatureExtractor
            from .image_processing_vit import ViTImageProcessor

      try:
            if not is_torch_available():
                  raise OptionalDependencyNotAvailable()
      except OptionalDependencyNotAvailable:
            pass
      else:
            from .modeling_vit import (
                VIT_PRETRAINED_MODEL_ARCHIVE_LIST,
                ViTForImageClassification,
                ViTForMaskedImageModeling,
                ViTModel,
                ViTPreTrainedModel,
            )

      try:
            if not is_tf_available():
                  raise OptionalDependencyNotAvailable()
      except OptionalDependencyNotAvailable:
            pass
      else:
            from .modeling_tf_vit import TFViTForImageClassification, TFViTModel, TFViTPreTrainedModel

      try:
            if not is_flax_available():
                  raise OptionalDependencyNotAvailable()
      except OptionalDependencyNotAvailable:
            pass
      else:
            from .modeling_flax_vit import FlaxViTForImageClassification, FlaxViTModel, FlaxViTPreTrainedModel


else:
      import sys

      _UpperCamelCase 							=  _LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)

