

'''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        A__   (   _snake_case      ):



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





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



    def        snake_case_      (       self							)    ->   List[str]:





           '''simple docstring'''
           A_						       =   ids_tensor([self.batch_size, self.seq_length]		,					self.vocab_size							)

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

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

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

           A_						       =   self.get_config()

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



    def        snake_case_      (       self							)    ->   Optional[Any]:





           '''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        snake_case_      (       self		,					UpperCamelCase__							)    ->   Dict:





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



    def        snake_case_      (       self		,					UpperCamelCase__		,					UpperCamelCase__		,					UpperCamelCase__		,					UpperCamelCase__		,					UpperCamelCase__		,					UpperCamelCase__		,					UpperCamelCase__							)    ->   str:





           '''simple docstring'''
           A_						       =   DebertaVaModel(config=UpperCamelCase__							)
           model.to(UpperCamelCase__							)
           model.eval()
           A_						       =   model(UpperCamelCase__		,					attention_mask=UpperCamelCase__		,					token_type_ids=UpperCamelCase__							)[0]
           A_						       =   model(UpperCamelCase__		,					token_type_ids=UpperCamelCase__							)[0]
           A_						       =   model(UpperCamelCase__							)[0]

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



    def        snake_case_      (       self		,					UpperCamelCase__		,					UpperCamelCase__		,					UpperCamelCase__		,					UpperCamelCase__		,					UpperCamelCase__		,					UpperCamelCase__		,					UpperCamelCase__							)    ->   Optional[int]:





           '''simple docstring'''
           A_						       =   DebertaVaForMaskedLM(config=UpperCamelCase__							)
           model.to(UpperCamelCase__							)
           model.eval()
           A_						       =   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        snake_case_      (       self		,					UpperCamelCase__		,					UpperCamelCase__		,					UpperCamelCase__		,					UpperCamelCase__		,					UpperCamelCase__		,					UpperCamelCase__		,					UpperCamelCase__							)    ->   Tuple:





           '''simple docstring'''
           A_						       =   self.num_labels
           A_						       =   DebertaVaForSequenceClassification(UpperCamelCase__							)
           model.to(UpperCamelCase__							)
           model.eval()
           A_						       =   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        snake_case_      (       self		,					UpperCamelCase__		,					UpperCamelCase__		,					UpperCamelCase__		,					UpperCamelCase__		,					UpperCamelCase__		,					UpperCamelCase__		,					UpperCamelCase__							)    ->   Optional[Any]:





           '''simple docstring'''
           A_						       =   self.num_labels
           A_						       =   DebertaVaForTokenClassification(config=UpperCamelCase__							)
           model.to(UpperCamelCase__							)
           model.eval()
           A_						       =   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        snake_case_      (       self		,					UpperCamelCase__		,					UpperCamelCase__		,					UpperCamelCase__		,					UpperCamelCase__		,					UpperCamelCase__		,					UpperCamelCase__		,					UpperCamelCase__							)    ->   str:





           '''simple docstring'''
           A_						       =   DebertaVaForQuestionAnswering(config=UpperCamelCase__							)
           model.to(UpperCamelCase__							)
           model.eval()
           A_						       =   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        snake_case_      (       self		,					UpperCamelCase__		,					UpperCamelCase__		,					UpperCamelCase__		,					UpperCamelCase__		,					UpperCamelCase__		,					UpperCamelCase__		,					UpperCamelCase__							)    ->   Optional[Any]:





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







    def        snake_case_      (       self							)    ->   Any:





           '''simple docstring'''
           A_						       =   self.prepare_config_and_inputs()
           (
               (
               A_
           )       ,      (
               A_
           )       ,      (
               A_
           )       ,      (
               A_
           )       ,      (
               A_
           )       ,      (
               A_
           )       ,      (
               A_
           )       ,      
           )						       =   config_and_inputs
           A_						       =   {"""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      ):
    lowercase						       =		(
        (
            DebertaVaModel,
            DebertaVaForMaskedLM,
            DebertaVaForSequenceClassification,
            DebertaVaForTokenClassification,
            DebertaVaForQuestionAnswering,
            DebertaVaForMultipleChoice,
        )
        if is_torch_available()
        else ()
    )
    lowercase						       =		(
        {
            "feature-extraction": DebertaVaModel,
            "fill-mask": DebertaVaForMaskedLM,
            "question-answering": DebertaVaForQuestionAnswering,
            "text-classification": DebertaVaForSequenceClassification,
            "token-classification": DebertaVaForTokenClassification,
            "zero-shot": DebertaVaForSequenceClassification,
        }
        if is_torch_available()
        else {}
    )

    lowercase						       =		True
    lowercase						       =		False
    lowercase						       =		False
    lowercase						       =		False
    lowercase						       =		False



    def        snake_case_      (       self							)    ->   str:





           '''simple docstring'''
           A_						       =   DebertaVaModelTester(self							)
           A_						       =   ConfigTester(self		,					config_class=UpperCamelCase__		,					hidden_size=37							)



    def        snake_case_      (       self							)    ->   Any:





           '''simple docstring'''
           self.config_tester.run_common_tests()



    def        snake_case_      (       self							)    ->   Optional[int]:





           '''simple docstring'''
           A_						       =   self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_deberta_model(*UpperCamelCase__							)



    def        snake_case_      (       self							)    ->   Tuple:





           '''simple docstring'''
           A_						       =   self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_deberta_for_sequence_classification(*UpperCamelCase__							)



    def        snake_case_      (       self							)    ->   Union[str, Any]:





           '''simple docstring'''
           A_						       =   self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_deberta_for_masked_lm(*UpperCamelCase__							)



    def        snake_case_      (       self							)    ->   Any:





           '''simple docstring'''
           A_						       =   self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_deberta_for_question_answering(*UpperCamelCase__							)



    def        snake_case_      (       self							)    ->   Optional[int]:





           '''simple docstring'''
           A_						       =   self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_deberta_for_token_classification(*UpperCamelCase__							)



    def        snake_case_      (       self							)    ->   List[Any]:





           '''simple docstring'''
           A_						       =   self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_deberta_for_multiple_choice(*UpperCamelCase__							)







    @slow
    def        snake_case_      (       self							)    ->   List[str]:





           '''simple docstring'''
           for model_name in DEBERTA_V2_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
                  A_						       =   DebertaVaModel.from_pretrained(UpperCamelCase__							)
                  self.assertIsNotNone(UpperCamelCase__							)


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



    @unittest.skip(reason="""Model not available yet"""							)
    def        snake_case_      (       self							)    ->   List[str]:





           '''simple docstring'''
           pass







    @slow
    def        snake_case_      (       self							)    ->   Optional[Any]:





           '''simple docstring'''
           A_						       =   DebertaVaModel.from_pretrained("""microsoft/deberta-v2-xlarge"""							)

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



'''simple docstring'''







import os
import sys
import unittest


__lowerCamelCase									=     os.path.abspath(os.path.dirname(os.path.dirname(os.path.dirname(__file__))))
sys.path.append(os.path.join(git_repo_path, '''utils'''))

import check_dummies  # noqa: E402
from check_dummies import create_dummy_files, create_dummy_object, find_backend, read_init  # noqa: E402


# Align TRANSFORMERS_PATH in check_dummies with the current path
__lowerCamelCase									=     os.path.join(git_repo_path, '''src''', '''transformers''')

__lowerCamelCase									=     '''
{0} = None
'''

__lowerCamelCase									=     '''
class {0}(metaclass=DummyObject):
    _backends = {1}

    def __init__(self, *args, **kwargs):
        requires_backends(self, {1})
'''


__lowerCamelCase									=     '''
def {0}(*args, **kwargs):
    requires_backends({0}, {1})
'''


class        A__   (   unittest.TestCase      ):



    def        snake_case_      (       self							)    ->   Dict:





           '''simple docstring'''
           A_						       =   find_backend("""    _import_structure[\"models.albert\"].append(\"AlbertTokenizerFast\")"""							)
           self.assertIsNone(UpperCamelCase__							)

           A_						       =   find_backend("""    if not is_tokenizers_available():"""							)
           self.assertEqual(UpperCamelCase__		,					"""tokenizers"""							)

           A_						       =   find_backend("""    if not is_tensorflow_text_available():"""							)
           self.assertEqual(UpperCamelCase__		,					"""tensorflow_text"""							)

           A_						       =   find_backend("""    if not (is_sentencepiece_available() and is_tokenizers_available()):"""							)
           self.assertEqual(UpperCamelCase__		,					"""sentencepiece_and_tokenizers"""							)

           A_						       =   find_backend(
               """    if not (is_sentencepiece_available() and is_tensorflow_text_available()):"""							)
           self.assertEqual(UpperCamelCase__		,					"""sentencepiece_and_tensorflow_text"""							)

           A_						       =   find_backend(
               """    if not (is_sentencepiece_available() and is_tokenizers_available() and is_vision_available()):"""							)
           self.assertEqual(UpperCamelCase__		,					"""sentencepiece_and_tokenizers_and_vision"""							)



    def        snake_case_      (       self							)    ->   int:





           '''simple docstring'''
           A_						       =   read_init()
           # We don't assert on the exact list of keys to allow for smooth grow of backend-specific objects
           self.assertIn("""torch"""		,					UpperCamelCase__							)
           self.assertIn("""tensorflow_text"""		,					UpperCamelCase__							)
           self.assertIn("""sentencepiece_and_tokenizers"""		,					UpperCamelCase__							)

           # Likewise, we can't assert on the exact content of a key
           self.assertIn("""BertModel"""		,					objects["""torch"""]							)
           self.assertIn("""TFBertModel"""		,					objects["""tf"""]							)
           self.assertIn("""FlaxBertModel"""		,					objects["""flax"""]							)
           self.assertIn("""BertModel"""		,					objects["""torch"""]							)
           self.assertIn("""TFBertTokenizer"""		,					objects["""tensorflow_text"""]							)
           self.assertIn("""convert_slow_tokenizer"""		,					objects["""sentencepiece_and_tokenizers"""]							)



    def        snake_case_      (       self							)    ->   str:





           '''simple docstring'''
           A_						       =   create_dummy_object("""CONSTANT"""		,					"""'torch'"""							)
           self.assertEqual(UpperCamelCase__		,					"""\nCONSTANT = None\n"""							)

           A_						       =   create_dummy_object("""function"""		,					"""'torch'"""							)
           self.assertEqual(
               UpperCamelCase__		,					"""\ndef function(*args, **kwargs):\n    requires_backends(function, 'torch')\n"""							)

           A_						       =   """
class FakeClass(metaclass=DummyObject):
    _backends = 'torch'

    def __init__(self, *args, **kwargs):
        requires_backends(self, 'torch')
"""
           A_						       =   create_dummy_object("""FakeClass"""		,					"""'torch'"""							)
           self.assertEqual(UpperCamelCase__		,					UpperCamelCase__							)







    def        snake_case_      (       self							)    ->   str:





           '''simple docstring'''
           A_						       =   """# This file is autogenerated by the command `make fix-copies`, do not edit.
from ..utils import DummyObject, requires_backends


CONSTANT = None


def function(*args, **kwargs):
    requires_backends(function, [\"torch\"])


class FakeClass(metaclass=DummyObject):
    _backends = [\"torch\"]

    def __init__(self, *args, **kwargs):
        requires_backends(self, [\"torch\"])
"""
           A_						       =   create_dummy_files({"""torch""": ["""CONSTANT""", """function""", """FakeClass"""]}							)
           self.assertEqual(dummy_files["""torch"""]		,					UpperCamelCase__							)
