



"""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 			__snake_case			(      _lowercase):




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






         """simple docstring"""






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




  def 		SCREAMING_SNAKE_CASE							(					self	:		Tuple						):






         """simple docstring"""






         _lowerCamelCase		:							str            =					ids_tensor([self.batch_size, self.seq_length]		,   self.vocab_size						)

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

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

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

         _lowerCamelCase		:							int            =					self.get_config()

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




  def 		SCREAMING_SNAKE_CASE							(					self	:		Union[str, 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 		SCREAMING_SNAKE_CASE							(					self	:		List[str]		,   __lowerCAmelCase	:		str						):






         """simple docstring"""






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




  def 		SCREAMING_SNAKE_CASE							(					self	:		Optional[int]		,   __lowerCAmelCase	:		str		,   __lowerCAmelCase	:		Tuple		,   __lowerCAmelCase	:		Any		,   __lowerCAmelCase	:		Tuple		,   __lowerCAmelCase	:		Union[str, Any]		,   __lowerCAmelCase	:		Optional[int]		,   __lowerCAmelCase	:		Any						):






         """simple docstring"""






         _lowerCamelCase		:							List[str]            =					DebertaVaModel(config=__lowerCAmelCase						)
         model.to(__lowerCAmelCase						)
         model.eval()
         _lowerCamelCase		:							Dict            =					model(__lowerCAmelCase		,   attention_mask=__lowerCAmelCase		,   token_type_ids=__lowerCAmelCase						)[0]
         _lowerCamelCase		:							str            =					model(__lowerCAmelCase		,   token_type_ids=__lowerCAmelCase						)[0]
         _lowerCamelCase		:							List[Any]            =					model(__lowerCAmelCase						)[0]

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




  def 		SCREAMING_SNAKE_CASE							(					self	:		Optional[Any]		,   __lowerCAmelCase	:		Any		,   __lowerCAmelCase	:		List[Any]		,   __lowerCAmelCase	:		Optional[int]		,   __lowerCAmelCase	:		Union[str, Any]		,   __lowerCAmelCase	:		List[Any]		,   __lowerCAmelCase	:		str		,   __lowerCAmelCase	:		Tuple						):






         """simple docstring"""






         _lowerCamelCase		:							Tuple            =					DebertaVaForMaskedLM(config=__lowerCAmelCase						)
         model.to(__lowerCAmelCase						)
         model.eval()
         _lowerCamelCase		:							Optional[int]            =					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 		SCREAMING_SNAKE_CASE							(					self	:		int		,   __lowerCAmelCase	:		Dict		,   __lowerCAmelCase	:		Dict		,   __lowerCAmelCase	:		Dict		,   __lowerCAmelCase	:		Optional[int]		,   __lowerCAmelCase	:		int		,   __lowerCAmelCase	:		List[Any]		,   __lowerCAmelCase	:		Any						):






         """simple docstring"""






         _lowerCamelCase		:							Optional[Any]            =					self.num_labels
         _lowerCamelCase		:							Dict            =					DebertaVaForSequenceClassification(__lowerCAmelCase						)
         model.to(__lowerCAmelCase						)
         model.eval()
         _lowerCamelCase		:							Dict            =					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 		SCREAMING_SNAKE_CASE							(					self	:		str		,   __lowerCAmelCase	:		List[str]		,   __lowerCAmelCase	:		str		,   __lowerCAmelCase	:		Optional[int]		,   __lowerCAmelCase	:		str		,   __lowerCAmelCase	:		Union[str, Any]		,   __lowerCAmelCase	:		Optional[Any]		,   __lowerCAmelCase	:		Optional[int]						):






         """simple docstring"""






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






         """simple docstring"""






         _lowerCamelCase		:							List[str]            =					DebertaVaForQuestionAnswering(config=__lowerCAmelCase						)
         model.to(__lowerCAmelCase						)
         model.eval()
         _lowerCamelCase		:							Tuple            =					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 		SCREAMING_SNAKE_CASE							(					self	:		str		,   __lowerCAmelCase	:		str		,   __lowerCAmelCase	:		Optional[int]		,   __lowerCAmelCase	:		Tuple		,   __lowerCAmelCase	:		Optional[int]		,   __lowerCAmelCase	:		str		,   __lowerCAmelCase	:		Dict		,   __lowerCAmelCase	:		Dict						):






         """simple docstring"""






         _lowerCamelCase		:							Optional[int]            =					DebertaVaForMultipleChoice(config=__lowerCAmelCase						)
         model.to(__lowerCAmelCase						)
         model.eval()
         _lowerCamelCase		:							Optional[Any]            =					input_ids.unsqueeze(1						).expand(-1		,   self.num_choices		,   -1						).contiguous()
         _lowerCamelCase		:							Optional[Any]            =					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		:							List[Any]            =					model(
             __lowerCAmelCase		,   attention_mask=__lowerCAmelCase		,   token_type_ids=__lowerCAmelCase		,   labels=__lowerCAmelCase		,   )
         self.parent.assertEqual(result.logits.shape		,   (self.batch_size, self.num_choices)						)




  def 		SCREAMING_SNAKE_CASE							(					self	:		Dict						):






         """simple docstring"""






         _lowerCamelCase		:							Any            =					self.prepare_config_and_inputs()
         (
             (
             _lowerCamelCase
         )     ,       (
             _lowerCamelCase
         )     ,       (
             _lowerCamelCase
         )     ,       (
             _lowerCamelCase
         )     ,       (
             _lowerCamelCase
         )     ,       (
             _lowerCamelCase
         )     ,       (
             _lowerCamelCase
         )     ,       
         )		:							Union[str, Any]            =					config_and_inputs
         _lowerCamelCase		:							Optional[int]            =					{'''input_ids''': input_ids, '''token_type_ids''': token_type_ids, '''attention_mask''': input_mask}
         return config, inputs_dict






@require_torch
class 			__snake_case			(      _lowercase    ,	_lowercase    ,	unittest.TestCase):
  snake_case__							:  int         =		(
      (
          DebertaVaModel,
          DebertaVaForMaskedLM,
          DebertaVaForSequenceClassification,
          DebertaVaForTokenClassification,
          DebertaVaForQuestionAnswering,
          DebertaVaForMultipleChoice,
      )
      if is_torch_available()
      else ()
  )
  snake_case__							:  int         =		(
      {
          "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__							:  List[str]         =		True
  snake_case__							:  List[Any]         =		False
  snake_case__							:  int         =		False
  snake_case__							:  Optional[Any]         =		False
  snake_case__							:  str         =		False




  def 		SCREAMING_SNAKE_CASE							(					self	:		int						):






         """simple docstring"""






         _lowerCamelCase		:							List[str]            =					DebertaVaModelTester(self						)
         _lowerCamelCase		:							Any            =					ConfigTester(self		,   config_class=__lowerCAmelCase		,   hidden_size=3_7						)




  def 		SCREAMING_SNAKE_CASE							(					self	:		Any						):






         """simple docstring"""






         self.config_tester.run_common_tests()




  def 		SCREAMING_SNAKE_CASE							(					self	:		int						):






         """simple docstring"""






         _lowerCamelCase		:							Dict            =					self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_deberta_model(*__lowerCAmelCase						)




  def 		SCREAMING_SNAKE_CASE							(					self	:		List[str]						):






         """simple docstring"""






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




  def 		SCREAMING_SNAKE_CASE							(					self	:		List[str]						):






         """simple docstring"""






         _lowerCamelCase		:							Any            =					self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_deberta_for_masked_lm(*__lowerCAmelCase						)




  def 		SCREAMING_SNAKE_CASE							(					self	:		str						):






         """simple docstring"""






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




  def 		SCREAMING_SNAKE_CASE							(					self	:		int						):






         """simple docstring"""






         _lowerCamelCase		:							List[Any]            =					self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_deberta_for_token_classification(*__lowerCAmelCase						)




  def 		SCREAMING_SNAKE_CASE							(					self	:		List[str]						):






         """simple docstring"""






         _lowerCamelCase		:							Dict            =					self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_deberta_for_multiple_choice(*__lowerCAmelCase						)




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






         """simple docstring"""






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






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




  @unittest.skip(reason='''Model not available yet'''						)
  def 		SCREAMING_SNAKE_CASE							(					self	:		Optional[Any]						):






         """simple docstring"""






         pass




  @slow
  def 		SCREAMING_SNAKE_CASE							(					self	:		Tuple						):






         """simple docstring"""






         _lowerCamelCase		:							Tuple            =					DebertaVaModel.from_pretrained('''microsoft/deberta-v2-xlarge'''						)

         _lowerCamelCase		:							List[str]            =					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]]						)
         _lowerCamelCase		:							Any            =					torch.tensor([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]						)
         with torch.no_grad():
                _lowerCamelCase		:							Tuple            =					model(__lowerCAmelCase		,   attention_mask=__lowerCAmelCase						)[0]
         # compare the actual values for a slice.
         _lowerCamelCase		:							Union[str, Any]            =					torch.tensor(
             [[[0.23_56, 0.19_48, 0.03_69], [-0.10_63, 0.35_86, -0.51_52], [-0.63_99, -0.02_59, -0.25_25]]]						)
         self.assertTrue(torch.allclose(output[:, 1:4, 1:4]		,   __lowerCAmelCase		,   atol=1E-4						)		,   f'''{output[:, 1:4, 1:4]}'''						)






class 	__lowercase					:





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






         """simple docstring"""







         UpperCAmelCase      					=  {}





      def 							_lowercase					(		self    :       str							)     -> None:






         """simple docstring"""







         print(self.vertex							)
         for i in self.vertex:
            print(__lowerCamelCase       ,							""" -> """       ,							""" -> """.join([str(__lowerCamelCase							) for j in self.vertex[i]]							)							)





      def 							_lowercase					(		self    :       Optional[Any]       ,							__lowerCamelCase    :       int       ,							__lowerCamelCase    :       int							)     -> None:






         """simple docstring"""







         if from_vertex in self.vertex:
            self.vertex[from_vertex].append(__lowerCamelCase							)
         else:
            # else make a new vertex
            UpperCAmelCase      					=  [to_vertex]





      def 							_lowercase					(		self    :       str							)     -> None:






         """simple docstring"""







         UpperCAmelCase      					=  [False] * len(self.vertex							)

         # call the recursive helper function
         for i in range(len(self.vertex							)							):
            if not visited[i]:
               self.dfs_recursive(__lowerCamelCase       ,							__lowerCamelCase							)





      def 							_lowercase					(		self    :       Optional[int]       ,							__lowerCamelCase    :       int       ,							__lowerCamelCase    :       list							)     -> None:






         """simple docstring"""







         UpperCAmelCase      					=  True

         print(__lowerCamelCase       ,							end=""" """							)

         # Recur for all the vertices that are adjacent to this node
         for i in self.vertex:
            if not visited[i]:
               self.dfs_recursive(__lowerCamelCase       ,							__lowerCamelCase							)


if __name__ == "__main__":
 __a	 =      Graph()
 g.add_edge(0, 1)
 g.add_edge(0, 2)
 g.add_edge(1, 2)
 g.add_edge(2, 0)
 g.add_edge(2, 3)
 g.add_edge(3, 3)

 g.print_graph()
 print("""DFS:""")
 g.dfs()

 # OUTPUT:
 # 0  ->  1 -> 2
 # 1  ->  2
 # 2  ->  0 -> 3
 # 3  ->  3
 # DFS:
 #  0 1 2 3



