






"""simple docstring"""



import unittest

from transformers import DebertaConfig, 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 (
        DebertaForMaskedLM,
        DebertaForQuestionAnswering,
        DebertaForSequenceClassification,
        DebertaForTokenClassification,
        DebertaModel,
    )
    from transformers.models.deberta.modeling_deberta import DEBERTA_PRETRAINED_MODEL_ARCHIVE_LIST

class        a     (   a_	):







    def __init__(			self							,		_lowerCamelCase							,		_lowerCamelCase=1_3							,		_lowerCamelCase=7							,		_lowerCamelCase=True							,		_lowerCamelCase=True							,		_lowerCamelCase=True							,		_lowerCamelCase=True							,		_lowerCamelCase=9_9							,		_lowerCamelCase=3_2							,		_lowerCamelCase=5							,		_lowerCamelCase=4							,		_lowerCamelCase=3_7							,		_lowerCamelCase="gelu"							,		_lowerCamelCase=0.1							,		_lowerCamelCase=0.1							,		_lowerCamelCase=5_1_2							,		_lowerCamelCase=1_6							,		_lowerCamelCase=2							,		_lowerCamelCase=0.0_2							,		_lowerCamelCase=False							,		_lowerCamelCase=True							,		_lowerCamelCase="None"							,		_lowerCamelCase=3							,		_lowerCamelCase=4							,		_lowerCamelCase=None							,		):
      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       ):
      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       ):
      return DebertaConfig(
          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       ):
      lowercase							    =       self.get_config()
      lowercase							    =       3_0_0
      return config







    def 			UpperCamelCase_      (			self							,		_lowerCamelCase       ):
      self.parent.assertListEqual(list(result.loss.size()       )							,		[]       )







    def 			UpperCamelCase_      (			self							,		_lowerCamelCase							,		_lowerCamelCase							,		_lowerCamelCase							,		_lowerCamelCase							,		_lowerCamelCase							,		_lowerCamelCase							,		_lowerCamelCase       ):
      lowercase							    =       DebertaModel(config=_lowerCamelCase       )
      model.to(_lowerCamelCase       )
      model.eval()
      lowercase							    =       model(_lowerCamelCase							,		attention_mask=_lowerCamelCase							,		token_type_ids=_lowerCamelCase       )[0]
      lowercase							    =       model(_lowerCamelCase							,		token_type_ids=_lowerCamelCase       )[0]
      lowercase							    =       model(_lowerCamelCase       )[0]

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







    def 			UpperCamelCase_      (			self							,		_lowerCamelCase							,		_lowerCamelCase							,		_lowerCamelCase							,		_lowerCamelCase							,		_lowerCamelCase							,		_lowerCamelCase							,		_lowerCamelCase       ):
      lowercase							    =       DebertaForMaskedLM(config=_lowerCamelCase       )
      model.to(_lowerCamelCase       )
      model.eval()
      lowercase							    =       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 			UpperCamelCase_      (			self							,		_lowerCamelCase							,		_lowerCamelCase							,		_lowerCamelCase							,		_lowerCamelCase							,		_lowerCamelCase							,		_lowerCamelCase							,		_lowerCamelCase       ):
      lowercase							    =       self.num_labels
      lowercase							    =       DebertaForSequenceClassification(_lowerCamelCase       )
      model.to(_lowerCamelCase       )
      model.eval()
      lowercase							    =       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 			UpperCamelCase_      (			self							,		_lowerCamelCase							,		_lowerCamelCase							,		_lowerCamelCase							,		_lowerCamelCase							,		_lowerCamelCase							,		_lowerCamelCase							,		_lowerCamelCase       ):
      lowercase							    =       self.num_labels
      lowercase							    =       DebertaForTokenClassification(config=_lowerCamelCase       )
      model.to(_lowerCamelCase       )
      model.eval()
      lowercase							    =       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 			UpperCamelCase_      (			self							,		_lowerCamelCase							,		_lowerCamelCase							,		_lowerCamelCase							,		_lowerCamelCase							,		_lowerCamelCase							,		_lowerCamelCase							,		_lowerCamelCase       ):
      lowercase							    =       DebertaForQuestionAnswering(config=_lowerCamelCase       )
      model.to(_lowerCamelCase       )
      model.eval()
      lowercase							    =       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 			UpperCamelCase_      (			self       ):
      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        a     (   a_,				a_,				unittest.TestCase	):
    UpperCAmelCase_ :     Optional[Any]	     =(
        (
            DebertaModel,
            DebertaForMaskedLM,
            DebertaForSequenceClassification,
            DebertaForTokenClassification,
            DebertaForQuestionAnswering,
        )
        if is_torch_available()
        else ()
    )
    UpperCAmelCase_ :     Optional[int]	     =(
        {
            "feature-extraction": DebertaModel,
            "fill-mask": DebertaForMaskedLM,
            "question-answering": DebertaForQuestionAnswering,
            "text-classification": DebertaForSequenceClassification,
            "token-classification": DebertaForTokenClassification,
            "zero-shot": DebertaForSequenceClassification,
        }
        if is_torch_available()
        else {}
    )

    UpperCAmelCase_ :     List[str]	     =True
    UpperCAmelCase_ :     List[Any]	     =False
    UpperCAmelCase_ :     List[str]	     =False
    UpperCAmelCase_ :     List[str]	     =False
    UpperCAmelCase_ :     Union[str, Any]	     =False







    def 			UpperCamelCase_      (			self       ):
      lowercase							    =       DebertaModelTester(self       )
      lowercase							    =       ConfigTester(self							,		config_class=_lowerCamelCase							,		hidden_size=3_7       )







    def 			UpperCamelCase_      (			self       ):
      self.config_tester.run_common_tests()







    def 			UpperCamelCase_      (			self       ):
      lowercase							    =       self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_deberta_model(*_lowerCamelCase       )







    def 			UpperCamelCase_      (			self       ):
      lowercase							    =       self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_deberta_for_sequence_classification(*_lowerCamelCase       )







    def 			UpperCamelCase_      (			self       ):
      lowercase							    =       self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_deberta_for_masked_lm(*_lowerCamelCase       )







    def 			UpperCamelCase_      (			self       ):
      lowercase							    =       self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_deberta_for_question_answering(*_lowerCamelCase       )







    def 			UpperCamelCase_      (			self       ):
      lowercase							    =       self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_deberta_for_token_classification(*_lowerCamelCase       )







    @slow
    def 			UpperCamelCase_      (			self       ):
      for model_name in DEBERTA_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
        lowercase							    =       DebertaModel.from_pretrained(_lowerCamelCase       )
        self.assertIsNotNone(_lowerCamelCase       )


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







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







    @slow
    def 			UpperCamelCase_      (			self       ):
      lowercase							    =       DebertaModel.from_pretrained('microsoft/deberta-base'       )

      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(_lowerCamelCase							,		attention_mask=_lowerCamelCase       )[0]
      # compare the actual values for a slice.
      lowercase							    =       torch.tensor(
          [[[-0.5_9_8_6, -0.8_0_5_5, -0.8_4_6_2], [1.4_4_8_4, -0.9_3_4_8, -0.8_0_5_9], [0.3_1_2_3, 0.0_0_3_2, -1.4_1_3_1]]]       )
      self.assertTrue(torch.allclose(output[:, 1:4, 1:4]							,		_lowerCamelCase							,		atol=1e-4       )							,		F'{output[:, 1:4, 1:4]}'       )










"""simple docstring"""



# Author: OMKAR PATHAK, Nwachukwu Chidiebere

# Use a Python dictionary to construct the graph.
from __future__ import annotations

from pprint import pformat
from typing import Generic, TypeVar

_UpperCamelCase		:  Optional[Any]						=		TypeVar('T')

class        a     (   Generic[T]	):







    def __init__(			self							,		_lowerCamelCase = True       ):
      lowercase							    =       {}  # dictionary of lists
      lowercase							    =       directed







    def 			UpperCamelCase_      (			self							,		_lowerCamelCase							,		_lowerCamelCase       ):

      if not self.directed:  # For undirected graphs
        # if both source vertex and destination vertex are both present in the
        # adjacency list, add destination vertex to source vertex list of adjacent
        # vertices and add source vertex to destination vertex list of adjacent
        # vertices.
        if source_vertex in self.adj_list and destination_vertex in self.adj_list:
          self.adj_list[source_vertex].append(_lowerCamelCase       )
          self.adj_list[destination_vertex].append(_lowerCamelCase       )
        # if only source vertex is present in adjacency list, add destination vertex
        # to source vertex list of adjacent vertices, then create a new vertex with
        # destination vertex as key and assign a list containing the source vertex
        # as it's first adjacent vertex.
        elif source_vertex in self.adj_list:
          self.adj_list[source_vertex].append(_lowerCamelCase       )
          lowercase							    =       [source_vertex]
        # if only destination vertex is present in adjacency list, add source vertex
        # to destination vertex list of adjacent vertices, then create a new vertex
        # with source vertex as key and assign a list containing the source vertex
        # as it's first adjacent vertex.
        elif destination_vertex in self.adj_list:
          self.adj_list[destination_vertex].append(_lowerCamelCase       )
          lowercase							    =       [destination_vertex]
        # if both source vertex and destination vertex are not present in adjacency
        # list, create a new vertex with source vertex as key and assign a list
        # containing the destination vertex as it's first adjacent vertex also
        # create a new vertex with destination vertex as key and assign a list
        # containing the source vertex as it's first adjacent vertex.
        else:
          lowercase							    =       [destination_vertex]
          lowercase							    =       [source_vertex]
      else:  # For directed graphs
        # if both source vertex and destination vertex are present in adjacency
        # list, add destination vertex to source vertex list of adjacent vertices.
        if source_vertex in self.adj_list and destination_vertex in self.adj_list:
          self.adj_list[source_vertex].append(_lowerCamelCase       )
        # if only source vertex is present in adjacency list, add destination
        # vertex to source vertex list of adjacent vertices and create a new vertex
        # with destination vertex as key, which has no adjacent vertex
        elif source_vertex in self.adj_list:
          self.adj_list[source_vertex].append(_lowerCamelCase       )
          lowercase							    =       []
        # if only destination vertex is present in adjacency list, create a new
        # vertex with source vertex as key and assign a list containing destination
        # vertex as first adjacent vertex
        elif destination_vertex in self.adj_list:
          lowercase							    =       [destination_vertex]
        # if both source vertex and destination vertex are not present in adjacency
        # list, create a new vertex with source vertex as key and a list containing
        # destination vertex as it's first adjacent vertex. Then create a new vertex
        # with destination vertex as key, which has no adjacent vertex
        else:
          lowercase							    =       [destination_vertex]
          lowercase							    =       []

      return self







    def __repr__(			self       ):
      return pformat(self.adj_list       )


