


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 			A_   (			snake_case_       ):




      '''simple docstring'''


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




      def 	_snake_case      (							self:			List[Any]							):
       __lowerCamelCase						:				Tuple						   =		ids_tensor([self.batch_size, self.seq_length]		, self.vocab_size							)

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

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

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

       __lowerCamelCase						:				Tuple						   =		self.get_config()

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




      def 	_snake_case      (							self:			Optional[int]							):
       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:			Any		, a:			int							):
       self.parent.assertListEqual(list(result.loss.size()							)		, []							)




      def 	_snake_case      (							self:			Optional[Any]		, a:			Tuple		, a:			Any		, a:			Union[str, Any]		, a:			Tuple		, a:			Dict		, a:			List[str]		, a:			Any							):
       __lowerCamelCase						:				Any						   =		DebertaVaModel(config=lowercase__							)
       model.to(lowercase__							)
       model.eval()
       __lowerCamelCase						:				List[Any]						   =		model(lowercase__		, attention_mask=lowercase__		, token_type_ids=lowercase__							)[0]
       __lowerCamelCase						:				Union[str, Any]						   =		model(lowercase__		, token_type_ids=lowercase__							)[0]
       __lowerCamelCase						:				Tuple						   =		model(lowercase__							)[0]

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




      def 	_snake_case      (							self:			List[str]		, a:			List[Any]		, a:			Union[str, Any]		, a:			Optional[int]		, a:			Optional[int]		, a:			Union[str, Any]		, a:			str		, a:			Any							):
       __lowerCamelCase						:				Dict						   =		DebertaVaForMaskedLM(config=lowercase__							)
       model.to(lowercase__							)
       model.eval()
       __lowerCamelCase						:				Union[str, Any]						   =		model(lowercase__		, attention_mask=lowercase__		, token_type_ids=lowercase__		, labels=lowercase__							)

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




      def 	_snake_case      (							self:			str		, a:			Any		, a:			Tuple		, a:			Any		, a:			int		, a:			Tuple		, a:			Optional[Any]		, a:			Union[str, Any]							):
       __lowerCamelCase						:				Dict						   =		self.num_labels
       __lowerCamelCase						:				Union[str, Any]						   =		DebertaVaForSequenceClassification(lowercase__							)
       model.to(lowercase__							)
       model.eval()
       __lowerCamelCase						:				Any						   =		model(lowercase__		, attention_mask=lowercase__		, token_type_ids=lowercase__		, labels=lowercase__							)
       self.parent.assertListEqual(list(result.logits.size()							)		, [self.batch_size, self.num_labels]							)
       self.check_loss_output(lowercase__							)




      def 	_snake_case      (							self:			Any		, a:			List[Any]		, a:			Dict		, a:			Optional[int]		, a:			List[Any]		, a:			List[str]		, a:			str		, a:			str							):
       __lowerCamelCase						:				Dict						   =		self.num_labels
       __lowerCamelCase						:				Tuple						   =		DebertaVaForTokenClassification(config=lowercase__							)
       model.to(lowercase__							)
       model.eval()
       __lowerCamelCase						:				int						   =		model(lowercase__		, attention_mask=lowercase__		, token_type_ids=lowercase__		, labels=lowercase__							)
       self.parent.assertEqual(result.logits.shape		, (self.batch_size, self.seq_length, self.num_labels)							)




      def 	_snake_case      (							self:			int		, a:			List[Any]		, a:			Optional[Any]		, a:			int		, a:			str		, a:			Tuple		, a:			int		, a:			str							):
       __lowerCamelCase						:				Tuple						   =		DebertaVaForQuestionAnswering(config=lowercase__							)
       model.to(lowercase__							)
       model.eval()
       __lowerCamelCase						:				List[str]						   =		model(
           lowercase__		, attention_mask=lowercase__		, token_type_ids=lowercase__		, start_positions=lowercase__		, end_positions=lowercase__		, )
       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:			Tuple		, a:			Dict		, a:			Tuple		, a:			Dict		, a:			int		, a:			int		, a:			List[str]		, a:			Tuple							):
       __lowerCamelCase						:				Dict						   =		DebertaVaForMultipleChoice(config=lowercase__							)
       model.to(lowercase__							)
       model.eval()
       __lowerCamelCase						:				Dict						   =		input_ids.unsqueeze(1							).expand(-1		, self.num_choices		, -1							).contiguous()
       __lowerCamelCase						:				Optional[int]						   =		token_type_ids.unsqueeze(1							).expand(-1		, self.num_choices		, -1							).contiguous()
       __lowerCamelCase						:				Union[str, Any]						   =		input_mask.unsqueeze(1							).expand(-1		, self.num_choices		, -1							).contiguous()
       __lowerCamelCase						:				int						   =		model(
           lowercase__		, attention_mask=lowercase__		, token_type_ids=lowercase__		, labels=lowercase__		, )
       self.parent.assertEqual(result.logits.shape		, (self.batch_size, self.num_choices)							)




      def 	_snake_case      (							self:			Union[str, Any]							):
       __lowerCamelCase						:				Tuple						   =		self.prepare_config_and_inputs()
       (
           __lowerCamelCase     
       )						:				Optional[int]						   =		config_and_inputs
       __lowerCamelCase						:				List[str]						   =		{"""input_ids""": input_ids, """token_type_ids""": token_type_ids, """attention_mask""": input_mask}
       return config, inputs_dict



@require_torch
class 			A_   (			snake_case_   , snake_case_   , unittest.TestCase       ):




      '''simple docstring'''


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

      __snake_case   			=  True
      __snake_case   			=  False
      __snake_case   			=  False
      __snake_case   			=  False
      __snake_case   			=  False




      def 	_snake_case      (							self:			Dict							):
       __lowerCamelCase						:				int						   =		DebertaVaModelTester(self							)
       __lowerCamelCase						:				Union[str, Any]						   =		ConfigTester(self		, config_class=lowercase__		, hidden_size=37							)




      def 	_snake_case      (							self:			Dict							):
       self.config_tester.run_common_tests()




      def 	_snake_case      (							self:			Optional[int]							):
       __lowerCamelCase						:				int						   =		self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_deberta_model(*lowercase__							)




      def 	_snake_case      (							self:			Union[str, Any]							):
       __lowerCamelCase						:				int						   =		self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_deberta_for_sequence_classification(*lowercase__							)




      def 	_snake_case      (							self:			Tuple							):
       __lowerCamelCase						:				str						   =		self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_deberta_for_masked_lm(*lowercase__							)




      def 	_snake_case      (							self:			Any							):
       __lowerCamelCase						:				Any						   =		self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_deberta_for_question_answering(*lowercase__							)




      def 	_snake_case      (							self:			Optional[int]							):
       __lowerCamelCase						:				List[str]						   =		self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_deberta_for_token_classification(*lowercase__							)




      def 	_snake_case      (							self:			List[Any]							):
       __lowerCamelCase						:				Dict						   =		self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_deberta_for_multiple_choice(*lowercase__							)




      @slow
      def 	_snake_case      (							self:			Dict							):

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



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




      '''simple docstring'''


      @unittest.skip(reason='Model not available yet'							)
      def 	_snake_case      (							self:			Tuple							):
       pass




      @slow
      def 	_snake_case      (							self:			Dict							):
       __lowerCamelCase						:				Any						   =		DebertaVaModel.from_pretrained('microsoft/deberta-v2-xlarge'							)

       __lowerCamelCase						:				List[str]						   =		torch.tensor([[0, 3_1414, 232, 328, 740, 1140, 1_2695, 69, 4_6078, 1588, 2]]							)
       __lowerCamelCase						:				Tuple						   =		torch.tensor([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]							)
       with torch.no_grad():
        __lowerCamelCase						:				Dict						   =		model(lowercase__		, attention_mask=lowercase__							)[0]
       # compare the actual values for a slice.
       __lowerCamelCase						:				str						   =		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]		, lowercase__		, atol=1e-4							)		, F'{output[:, 1:4, 1:4]}'							)







'''simple docstring'''


from typing import TYPE_CHECKING

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


lowerCAmelCase_      :  Optional[Any]				     =  {
    'configuration_mobilevit': ['MOBILEVIT_PRETRAINED_CONFIG_ARCHIVE_MAP', 'MobileViTConfig', 'MobileViTOnnxConfig'],
}

try:
 if not is_vision_available():
  raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
 pass
else:
 lowerCAmelCase_      :  int				     =  ['MobileViTFeatureExtractor']
 lowerCAmelCase_      :  int				     =  ['MobileViTImageProcessor']

try:
 if not is_torch_available():
  raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
 pass
else:
 lowerCAmelCase_      :  str				     =  [
     'MOBILEVIT_PRETRAINED_MODEL_ARCHIVE_LIST',
     'MobileViTForImageClassification',
     'MobileViTForSemanticSegmentation',
     'MobileViTModel',
     'MobileViTPreTrainedModel',
 ]

try:
 if not is_tf_available():
  raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
 pass
else:
 lowerCAmelCase_      :  Optional[Any]				     =  [
     'TF_MOBILEVIT_PRETRAINED_MODEL_ARCHIVE_LIST',
     'TFMobileViTForImageClassification',
     'TFMobileViTForSemanticSegmentation',
     'TFMobileViTModel',
     'TFMobileViTPreTrainedModel',
 ]

if TYPE_CHECKING:
 from .configuration_mobilevit import MOBILEVIT_PRETRAINED_CONFIG_ARCHIVE_MAP, MobileViTConfig, MobileViTOnnxConfig

 try:
  if not is_vision_available():
   raise OptionalDependencyNotAvailable()
 except OptionalDependencyNotAvailable:
  pass
 else:
  from .feature_extraction_mobilevit import MobileViTFeatureExtractor
  from .image_processing_mobilevit import MobileViTImageProcessor

 try:
  if not is_torch_available():
   raise OptionalDependencyNotAvailable()
 except OptionalDependencyNotAvailable:
  pass
 else:
  from .modeling_mobilevit import (
      MOBILEVIT_PRETRAINED_MODEL_ARCHIVE_LIST,
      MobileViTForImageClassification,
      MobileViTForSemanticSegmentation,
      MobileViTModel,
      MobileViTPreTrainedModel,
  )

 try:
  if not is_tf_available():
   raise OptionalDependencyNotAvailable()
 except OptionalDependencyNotAvailable:
  pass
 else:
  from .modeling_tf_mobilevit import (
      TF_MOBILEVIT_PRETRAINED_MODEL_ARCHIVE_LIST,
      TFMobileViTForImageClassification,
      TFMobileViTForSemanticSegmentation,
      TFMobileViTModel,
      TFMobileViTPreTrainedModel,
  )


else:
 import sys

 lowerCAmelCase_      :  List[Any]				     =  _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
