

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   __lowercase      (_UpperCAmelCase ):
 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.02      ,							A_=False      ,							A_=True      ,							A_="None"      ,							A_=3      ,							A_=4      ,							A_=None      ,							)					->Dict:



       '''simple docstring'''





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



       '''simple docstring'''





       __lowerCAmelCase					:						Any					=					ids_tensor([self.batch_size, self.seq_length]      ,							self.vocab_size					)

       __lowerCAmelCase					:						Optional[Any]					=					None
       if self.use_input_mask:
             __lowerCAmelCase					:						Tuple					=					ids_tensor([self.batch_size, self.seq_length]      ,							vocab_size=2					)

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

       __lowerCAmelCase					:						Dict					=					None
       __lowerCAmelCase					:						int					=					None
       __lowerCAmelCase					:						Tuple					=					None
       if self.use_labels:
             __lowerCAmelCase					:						Any					=					ids_tensor([self.batch_size]      ,							self.type_sequence_label_size					)
             __lowerCAmelCase					:						Dict					=					ids_tensor([self.batch_size, self.seq_length]      ,							self.num_labels					)
             __lowerCAmelCase					:						List[Any]					=					ids_tensor([self.batch_size]      ,							self.num_choices					)

       __lowerCAmelCase					:						List[Any]					=					self.get_config()

       return config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels
 def 	UpperCamelCase__  (							self					)					->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 	UpperCamelCase__  (							self      ,							A_					)					->str:



       '''simple docstring'''





       self.parent.assertListEqual(list(result.loss.size()					)      ,							[]					)
 def 	UpperCamelCase__  (							self      ,							A_      ,							A_      ,							A_      ,							A_      ,							A_      ,							A_      ,							A_					)					->str:



       '''simple docstring'''





       __lowerCAmelCase					:						Optional[int]					=					DebertaVaModel(config=A_					)
       model.to(A_					)
       model.eval()
       __lowerCAmelCase					:						str					=					model(A_      ,							attention_mask=A_      ,							token_type_ids=A_					)[0]
       __lowerCAmelCase					:						int					=					model(A_      ,							token_type_ids=A_					)[0]
       __lowerCAmelCase					:						List[str]					=					model(A_					)[0]

       self.parent.assertListEqual(list(sequence_output.size()					)      ,							[self.batch_size, self.seq_length, self.hidden_size]					)
 def 	UpperCamelCase__  (							self      ,							A_      ,							A_      ,							A_      ,							A_      ,							A_      ,							A_      ,							A_					)					->int:



       '''simple docstring'''





       __lowerCAmelCase					:						Tuple					=					DebertaVaForMaskedLM(config=A_					)
       model.to(A_					)
       model.eval()
       __lowerCAmelCase					:						Optional[int]					=					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 	UpperCamelCase__  (							self      ,							A_      ,							A_      ,							A_      ,							A_      ,							A_      ,							A_      ,							A_					)					->Union[str, Any]:



       '''simple docstring'''





       __lowerCAmelCase					:						Tuple					=					self.num_labels
       __lowerCAmelCase					:						int					=					DebertaVaForSequenceClassification(A_					)
       model.to(A_					)
       model.eval()
       __lowerCAmelCase					:						str					=					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 	UpperCamelCase__  (							self      ,							A_      ,							A_      ,							A_      ,							A_      ,							A_      ,							A_      ,							A_					)					->List[str]:



       '''simple docstring'''





       __lowerCAmelCase					:						List[str]					=					self.num_labels
       __lowerCAmelCase					:						int					=					DebertaVaForTokenClassification(config=A_					)
       model.to(A_					)
       model.eval()
       __lowerCAmelCase					:						Dict					=					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 	UpperCamelCase__  (							self      ,							A_      ,							A_      ,							A_      ,							A_      ,							A_      ,							A_      ,							A_					)					->Any:



       '''simple docstring'''





       __lowerCAmelCase					:						Union[str, Any]					=					DebertaVaForQuestionAnswering(config=A_					)
       model.to(A_					)
       model.eval()
       __lowerCAmelCase					:						str					=					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 	UpperCamelCase__  (							self      ,							A_      ,							A_      ,							A_      ,							A_      ,							A_      ,							A_      ,							A_					)					->Tuple:



       '''simple docstring'''





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





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



       '''simple docstring'''





       __lowerCAmelCase					:						Any					=					self.prepare_config_and_inputs()
       (
           (
           __lowerCAmelCase
       ),							(
           __lowerCAmelCase
       ),							(
           __lowerCAmelCase
       ),							(
           __lowerCAmelCase
       ),							(
           __lowerCAmelCase
       ),							(
           __lowerCAmelCase
       ),							(
           __lowerCAmelCase
       ),							
       )					:						Optional[Any]					=					config_and_inputs
       __lowerCAmelCase					:						Tuple					=					{'''input_ids''': input_ids, '''token_type_ids''': token_type_ids, '''attention_mask''': input_mask}
       return config, inputs_dict







@require_torch
class   __lowercase      (_UpperCAmelCase		,   _UpperCAmelCase		,   unittest.TestCase ):
 _UpperCamelCase			  =  (
     (
         DebertaVaModel,
         DebertaVaForMaskedLM,
         DebertaVaForSequenceClassification,
         DebertaVaForTokenClassification,
         DebertaVaForQuestionAnswering,
         DebertaVaForMultipleChoice,
     )
     if is_torch_available()
     else ()
 )
 _UpperCamelCase			  =  (
     {
         """feature-extraction""": DebertaVaModel,
         """fill-mask""": DebertaVaForMaskedLM,
         """question-answering""": DebertaVaForQuestionAnswering,
         """text-classification""": DebertaVaForSequenceClassification,
         """token-classification""": DebertaVaForTokenClassification,
         """zero-shot""": DebertaVaForSequenceClassification,
     }
     if is_torch_available()
     else {}
 )

 _UpperCamelCase			  =  True
 _UpperCamelCase			  =  False
 _UpperCamelCase			  =  False
 _UpperCamelCase			  =  False
 _UpperCamelCase			  =  False
 def 	UpperCamelCase__  (							self					)					->int:



       '''simple docstring'''





       __lowerCAmelCase					:						Dict					=					DebertaVaModelTester(self					)
       __lowerCAmelCase					:						Optional[Any]					=					ConfigTester(self      ,							config_class=A_      ,							hidden_size=37					)
 def 	UpperCamelCase__  (							self					)					->Union[str, Any]:



       '''simple docstring'''





       self.config_tester.run_common_tests()
 def 	UpperCamelCase__  (							self					)					->Tuple:



       '''simple docstring'''





       __lowerCAmelCase					:						int					=					self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_deberta_model(*A_					)
 def 	UpperCamelCase__  (							self					)					->Any:



       '''simple docstring'''





       __lowerCAmelCase					:						Optional[int]					=					self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_deberta_for_sequence_classification(*A_					)
 def 	UpperCamelCase__  (							self					)					->Tuple:



       '''simple docstring'''





       __lowerCAmelCase					:						Any					=					self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_deberta_for_masked_lm(*A_					)
 def 	UpperCamelCase__  (							self					)					->List[Any]:



       '''simple docstring'''





       __lowerCAmelCase					:						Union[str, Any]					=					self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_deberta_for_question_answering(*A_					)
 def 	UpperCamelCase__  (							self					)					->Dict:



       '''simple docstring'''





       __lowerCAmelCase					:						Tuple					=					self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_deberta_for_token_classification(*A_					)
 def 	UpperCamelCase__  (							self					)					->Dict:



       '''simple docstring'''





       __lowerCAmelCase					:						Union[str, Any]					=					self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_deberta_for_multiple_choice(*A_					)





 @slow
 def 	UpperCamelCase__  (							self					)					->List[Any]:



       '''simple docstring'''





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







@require_torch
@require_sentencepiece
@require_tokenizers
class   __lowercase      (unittest.TestCase ):
 @unittest.skip(reason='''Model not available yet'''					)
 def 	UpperCamelCase__  (							self					)					->Optional[int]:



       '''simple docstring'''





       pass





 @slow
 def 	UpperCamelCase__  (							self					)					->Tuple:



       '''simple docstring'''





       __lowerCAmelCase					:						Union[str, Any]					=					DebertaVaModel.from_pretrained('''microsoft/deberta-v2-xlarge'''					)

       __lowerCAmelCase					:						Any					=					torch.tensor([[0, 3_1414, 232, 328, 740, 1140, 1_2695, 69, 4_6078, 1588, 2]]					)
       __lowerCAmelCase					:						Union[str, Any]					=					torch.tensor([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]					)
       with torch.no_grad():
             __lowerCAmelCase					:						Optional[Any]					=					model(A_      ,							attention_mask=A_					)[0]
       # compare the actual values for a slice.
       __lowerCAmelCase					:						Tuple					=					torch.tensor(
           [[[0.2_356, 0.1_948, 0.0_369], [-0.1_063, 0.3_586, -0.5_152], [-0.6_399, -0.0_259, -0.2_525]]]					)
       self.assertTrue(torch.allclose(output[:, 1:4, 1:4]      ,							A_      ,							atol=1e-4					)      ,							f"""{output[:, 1:4, 1:4]}"""					)





from typing import Dict

import numpy as np
import torch

from . import residue_constants as rc
from .tensor_utils import tensor_tree_map, tree_map







def 		_lowercase (     lowercase__						):
      __lowerCAmelCase					:						str					=					[]
      __lowerCAmelCase					:						List[Any]					=					[]
      __lowerCAmelCase					:						str					=					[]

      for rt in rc.restypes:
            __lowerCAmelCase					:						List[Any]					=					rc.restype_name_to_atomaa_names[rc.restype_atoa[rt]]
            restype_atomaa_to_atomaa_list.append([(rc.atom_order[name] if name else 0) for name in atom_names]						)
            __lowerCAmelCase					:						List[str]					=					{name: i for i, name in enumerate(lowercase__						)}
            restype_atomaa_to_atomaa_list.append(
                [(atom_name_to_idxaa[name] if name in atom_name_to_idxaa else 0) for name in rc.atom_types]						)

            restype_atomaa_mask_list.append([(1.0 if name else 0.0) for name in atom_names]						)

      # Add dummy mapping for restype 'UNK'
      restype_atomaa_to_atomaa_list.append([0] * 1_4						)
      restype_atomaa_to_atomaa_list.append([0] * 3_7						)
      restype_atomaa_mask_list.append([0.0] * 1_4						)

      __lowerCAmelCase					:						List[Any]					=					torch.tensor(
          lowercase__   ,	dtype=torch.intaa   ,	device=protein['''aatype'''].device   ,	)
      __lowerCAmelCase					:						Optional[Any]					=					torch.tensor(
          lowercase__   ,	dtype=torch.intaa   ,	device=protein['''aatype'''].device   ,	)
      __lowerCAmelCase					:						Tuple					=					torch.tensor(
          lowercase__   ,	dtype=torch.floataa   ,	device=protein['''aatype'''].device   ,	)
      __lowerCAmelCase					:						List[Any]					=					protein['''aatype'''].to(torch.long						)

      # create the mapping for (residx, atom14) --> atom37, i.e. an array
      # with shape (num_res, 14) containing the atom37 indices for this protein
      __lowerCAmelCase					:						Any					=					restype_atomaa_to_atomaa[protein_aatype]
      __lowerCAmelCase					:						Union[str, Any]					=					restype_atomaa_mask[protein_aatype]

      __lowerCAmelCase					:						int					=					residx_atomaa_mask
      __lowerCAmelCase					:						List[str]					=					residx_atomaa_to_atomaa.long()

      # create the gather indices for mapping back
      __lowerCAmelCase					:						int					=					restype_atomaa_to_atomaa[protein_aatype]
      __lowerCAmelCase					:						Union[str, Any]					=					residx_atomaa_to_atomaa.long()

      # create the corresponding mask
      __lowerCAmelCase					:						str					=					torch.zeros([2_1, 3_7]   ,	dtype=torch.floataa   ,	device=protein['''aatype'''].device						)
      for restype, restype_letter in enumerate(rc.restypes						):
            __lowerCAmelCase					:						Optional[int]					=					rc.restype_atoa[restype_letter]
            __lowerCAmelCase					:						Optional[Any]					=					rc.residue_atoms[restype_name]
            for atom_name in atom_names:
                  __lowerCAmelCase					:						str					=					rc.atom_order[atom_name]
                  __lowerCAmelCase					:						List[Any]					=					1

      __lowerCAmelCase					:						Union[str, Any]					=					restype_atomaa_mask[protein_aatype]
      __lowerCAmelCase					:						Any					=					residx_atomaa_mask

      return protein







def 		_lowercase (     lowercase__						):
      __lowerCAmelCase					:						Dict					=					tree_map(lambda lowercase__						: torch.tensor(lowercase__   ,	device=batch['''aatype'''].device						)   ,	lowercase__   ,	np.ndarray						)
      __lowerCAmelCase					:						Tuple					=					tensor_tree_map(lambda lowercase__						: np.array(lowercase__						)   ,	make_atomaa_masks(lowercase__						)						)
      return out


