



"""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    SCREAMING_SNAKE_CASE__ (							__UpperCAmelCase   ):

    def __init__(				self			:				Optional[int] ,  lowerCAmelCase_			:				Optional[int] ,  lowerCAmelCase_			:				List[str]=1_3 ,  lowerCAmelCase_			:				str=7 ,  lowerCAmelCase_			:				Any=True ,  lowerCAmelCase_			:				Optional[Any]=True ,  lowerCAmelCase_			:				Optional[Any]=True ,  lowerCAmelCase_			:				Any=True ,  lowerCAmelCase_			:				Optional[int]=9_9 ,  lowerCAmelCase_			:				Any=3_2 ,  lowerCAmelCase_			:				Union[str, Any]=5 ,  lowerCAmelCase_			:				Dict=4 ,  lowerCAmelCase_			:				Union[str, Any]=3_7 ,  lowerCAmelCase_			:				List[Any]="gelu" ,  lowerCAmelCase_			:				str=0.1 ,  lowerCAmelCase_			:				List[Any]=0.1 ,  lowerCAmelCase_			:				List[Any]=5_1_2 ,  lowerCAmelCase_			:				str=1_6 ,  lowerCAmelCase_			:				Union[str, Any]=2 ,  lowerCAmelCase_			:				List[Any]=0.02 ,  lowerCAmelCase_			:				int=False ,  lowerCAmelCase_			:				Union[str, Any]=True ,  lowerCAmelCase_			:				Tuple="None" ,  lowerCAmelCase_			:				Any=3 ,  lowerCAmelCase_			:				Any=4 ,  lowerCAmelCase_			:				int=None ,  ):







      """simple docstring"""







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

    def 					_UpperCAmelCase	(				self			:				Optional[int]):







      """simple docstring"""







      lowercase_								= ids_tensor([self.batch_size, self.seq_length] ,  self.vocab_size)

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

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

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

      lowercase_								= 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			:				List[str] ,  lowerCAmelCase_			:				Dict):







      """simple docstring"""







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

    def 					_UpperCAmelCase	(				self			:				Union[str, Any] ,  lowerCAmelCase_			:				str ,  lowerCAmelCase_			:				int ,  lowerCAmelCase_			:				List[str] ,  lowerCAmelCase_			:				int ,  lowerCAmelCase_			:				Optional[int] ,  lowerCAmelCase_			:				Tuple ,  lowerCAmelCase_			:				Optional[Any]):







      """simple docstring"""







      lowercase_								= DebertaVaModel(config=__UpperCamelCase)
      model.to(__UpperCamelCase)
      model.eval()
      lowercase_								= model(__UpperCamelCase ,  attention_mask=__UpperCamelCase ,  token_type_ids=__UpperCamelCase)[0]
      lowercase_								= model(__UpperCamelCase ,  token_type_ids=__UpperCamelCase)[0]
      lowercase_								= model(__UpperCamelCase)[0]

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

    def 					_UpperCAmelCase	(				self			:				List[Any] ,  lowerCAmelCase_			:				int ,  lowerCAmelCase_			:				Dict ,  lowerCAmelCase_			:				Optional[int] ,  lowerCAmelCase_			:				Dict ,  lowerCAmelCase_			:				List[Any] ,  lowerCAmelCase_			:				Optional[Any] ,  lowerCAmelCase_			:				Optional[Any]):







      """simple docstring"""







      lowercase_								= DebertaVaForMaskedLM(config=__UpperCamelCase)
      model.to(__UpperCamelCase)
      model.eval()
      lowercase_								= 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 					_UpperCAmelCase	(				self			:				str ,  lowerCAmelCase_			:				Tuple ,  lowerCAmelCase_			:				Union[str, Any] ,  lowerCAmelCase_			:				int ,  lowerCAmelCase_			:				Tuple ,  lowerCAmelCase_			:				int ,  lowerCAmelCase_			:				List[Any] ,  lowerCAmelCase_			:				List[str]):







      """simple docstring"""







      lowercase_								= self.num_labels
      lowercase_								= DebertaVaForSequenceClassification(__UpperCamelCase)
      model.to(__UpperCamelCase)
      model.eval()
      lowercase_								= 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 					_UpperCAmelCase	(				self			:				List[Any] ,  lowerCAmelCase_			:				Optional[int] ,  lowerCAmelCase_			:				int ,  lowerCAmelCase_			:				Any ,  lowerCAmelCase_			:				List[str] ,  lowerCAmelCase_			:				Tuple ,  lowerCAmelCase_			:				Optional[int] ,  lowerCAmelCase_			:				List[Any]):







      """simple docstring"""







      lowercase_								= self.num_labels
      lowercase_								= DebertaVaForTokenClassification(config=__UpperCamelCase)
      model.to(__UpperCamelCase)
      model.eval()
      lowercase_								= 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 					_UpperCAmelCase	(				self			:				int ,  lowerCAmelCase_			:				str ,  lowerCAmelCase_			:				Dict ,  lowerCAmelCase_			:				str ,  lowerCAmelCase_			:				str ,  lowerCAmelCase_			:				Optional[int] ,  lowerCAmelCase_			:				str ,  lowerCAmelCase_			:				str):







      """simple docstring"""







      lowercase_								= DebertaVaForQuestionAnswering(config=__UpperCamelCase)
      model.to(__UpperCamelCase)
      model.eval()
      lowercase_								= 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 					_UpperCAmelCase	(				self			:				Union[str, Any] ,  lowerCAmelCase_			:				Optional[int] ,  lowerCAmelCase_			:				Optional[int] ,  lowerCAmelCase_			:				Optional[Any] ,  lowerCAmelCase_			:				Any ,  lowerCAmelCase_			:				Union[str, Any] ,  lowerCAmelCase_			:				List[Any] ,  lowerCAmelCase_			:				Optional[int]):







      """simple docstring"""







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




    def 					_UpperCAmelCase	(				self			:				Any):







      """simple docstring"""







      lowercase_								= self.prepare_config_and_inputs()
      (
          (
          lowercase_						
      )					,    (
          lowercase_						
      )					,    (
          lowercase_						
      )					,    (
          lowercase_						
      )					,    (
          lowercase_						
      )					,    (
          lowercase_						
      )					,    (
          lowercase_						
      )					,    						
      )								= config_and_inputs
      lowercase_								= {"""input_ids""": input_ids, """token_type_ids""": token_type_ids, """attention_mask""": input_mask}
      return config, inputs_dict







@require_torch
class    SCREAMING_SNAKE_CASE__ (							__UpperCAmelCase      ,  __UpperCAmelCase      ,  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 					_UpperCAmelCase	(				self			:				str):







      """simple docstring"""







      lowercase_								= DebertaVaModelTester(self)
      lowercase_								= ConfigTester(self ,  config_class=__UpperCamelCase ,  hidden_size=3_7)

    def 					_UpperCAmelCase	(				self			:				Any):







      """simple docstring"""







      self.config_tester.run_common_tests()

    def 					_UpperCAmelCase	(				self			:				List[Any]):







      """simple docstring"""







      lowercase_								= self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_deberta_model(*__UpperCamelCase)

    def 					_UpperCAmelCase	(				self			:				List[Any]):







      """simple docstring"""







      lowercase_								= self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_deberta_for_sequence_classification(*__UpperCamelCase)

    def 					_UpperCAmelCase	(				self			:				Tuple):







      """simple docstring"""







      lowercase_								= self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_deberta_for_masked_lm(*__UpperCamelCase)

    def 					_UpperCAmelCase	(				self			:				Dict):







      """simple docstring"""







      lowercase_								= self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_deberta_for_question_answering(*__UpperCamelCase)

    def 					_UpperCAmelCase	(				self			:				str):







      """simple docstring"""







      lowercase_								= self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_deberta_for_token_classification(*__UpperCamelCase)

    def 					_UpperCAmelCase	(				self			:				Optional[int]):







      """simple docstring"""







      lowercase_								= self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_deberta_for_multiple_choice(*__UpperCamelCase)




    @slow
    def 					_UpperCAmelCase	(				self			:				Union[str, Any]):







      """simple docstring"""







      for model_name in DEBERTA_V2_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
        lowercase_								= DebertaVaModel.from_pretrained(__UpperCamelCase)
        self.assertIsNotNone(__UpperCamelCase)







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

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







      """simple docstring"""







      pass




    @slow
    def 					_UpperCAmelCase	(				self			:				Any):







      """simple docstring"""







      lowercase_								= DebertaVaModel.from_pretrained("""microsoft/deberta-v2-xlarge""")

      lowercase_								= torch.tensor([[0, 3_1_4_1_4, 2_3_2, 3_2_8, 7_4_0, 1_1_4_0, 1_2_6_9_5, 6_9, 4_6_0_7_8, 1_5_8_8, 2]])
      lowercase_								= torch.tensor([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]])
      with torch.no_grad():
        lowercase_								= model(__UpperCamelCase ,  attention_mask=__UpperCamelCase)[0]
      # compare the actual values for a slice.
      lowercase_								= 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] ,  __UpperCamelCase ,  atol=1E-4) ,  F'''{output[:, 1:4, 1:4]}''')





"""simple docstring"""
def 			_SCREAMING_SNAKE_CASE						(__lowerCAmelCase							) ->						list[list[int]]:







  '''simple docstring'''


  lowercase_								= []
  if len(__lowerCAmelCase							) == 1:
    return [nums.copy()]
  for _ in range(len(__lowerCAmelCase							)							):
    lowercase_								= nums.pop(0							)
    lowercase_								= permute(__lowerCAmelCase							)
    for perm in permutations:
      perm.append(__lowerCAmelCase							)
    result.extend(__lowerCAmelCase							)
    nums.append(__lowerCAmelCase							)
  return result


def 			_SCREAMING_SNAKE_CASE						(__lowerCAmelCase							) ->						Tuple:







  '''simple docstring'''


  def backtrack(__lowerCAmelCase							):
    if start == len(__lowerCAmelCase							) - 1:
      output.append(nums[:]							)
    else:
      for i in range(__lowerCAmelCase ,  len(__lowerCAmelCase							)							):
        lowercase_					,    lowercase_								= nums[i], nums[start]
        backtrack(start + 1							)
        lowercase_					,    lowercase_								= nums[i], nums[start]  # backtrack

  lowercase_								= []
  backtrack(0							)
  return output


if __name__ == "__main__":
       import doctest

       # use res to print the data in permute2 function
       UpperCAmelCase						:		Optional[Any]			= permutea([1, 2, 3])
       print(res)
       doctest.testmod()

