




import unittest

from transformers import AutoTokenizer, NystromformerConfig, is_torch_available
from transformers.testing_utils import require_torch, slow, torch_device

from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
   import torch

   from transformers import (
       NystromformerForMaskedLM,
       NystromformerForMultipleChoice,
       NystromformerForQuestionAnswering,
       NystromformerForSequenceClassification,
       NystromformerForTokenClassification,
       NystromformerModel,
   )
   from transformers.models.nystromformer.modeling_nystromformer import NYSTROMFORMER_PRETRAINED_MODEL_ARCHIVE_LIST





class        lowerCamelCase_							:


 '''simple docstring'''







 def __init__(							self  :		List[str]				,				_lowerCAmelCase  :		Union[str, Any]				,				_lowerCAmelCase  :		Optional[Any]=13				,				_lowerCAmelCase  :		Dict=7				,				_lowerCAmelCase  :		Tuple=True				,				_lowerCAmelCase  :		Optional[Any]=True				,				_lowerCAmelCase  :		Optional[Any]=True				,				_lowerCAmelCase  :		Union[str, Any]=True				,				_lowerCAmelCase  :		Dict=99				,				_lowerCAmelCase  :		Dict=32				,				_lowerCAmelCase  :		Optional[int]=5				,				_lowerCAmelCase  :		List[str]=4				,				_lowerCAmelCase  :		Dict=37				,				_lowerCAmelCase  :		List[str]="gelu"				,				_lowerCAmelCase  :		int=0.1				,				_lowerCAmelCase  :		List[Any]=0.1				,				_lowerCAmelCase  :		Union[str, Any]=512				,				_lowerCAmelCase  :		Any=16				,				_lowerCAmelCase  :		Dict=2				,				_lowerCAmelCase  :		Any=0.02				,				_lowerCAmelCase  :		Tuple=3				,				_lowerCAmelCase  :		List[str]=4				,				_lowerCAmelCase  :		Tuple=None				,				):
       SCREAMING_SNAKE_CASE_							=			parent
       SCREAMING_SNAKE_CASE_							=			batch_size
       SCREAMING_SNAKE_CASE_							=			seq_length
       SCREAMING_SNAKE_CASE_							=			is_training
       SCREAMING_SNAKE_CASE_							=			use_input_mask
       SCREAMING_SNAKE_CASE_							=			use_token_type_ids
       SCREAMING_SNAKE_CASE_							=			use_labels
       SCREAMING_SNAKE_CASE_							=			vocab_size
       SCREAMING_SNAKE_CASE_							=			hidden_size
       SCREAMING_SNAKE_CASE_							=			num_hidden_layers
       SCREAMING_SNAKE_CASE_							=			num_attention_heads
       SCREAMING_SNAKE_CASE_							=			intermediate_size
       SCREAMING_SNAKE_CASE_							=			hidden_act
       SCREAMING_SNAKE_CASE_							=			hidden_dropout_prob
       SCREAMING_SNAKE_CASE_							=			attention_probs_dropout_prob
       SCREAMING_SNAKE_CASE_							=			max_position_embeddings
       SCREAMING_SNAKE_CASE_							=			type_vocab_size
       SCREAMING_SNAKE_CASE_							=			type_sequence_label_size
       SCREAMING_SNAKE_CASE_							=			initializer_range
       SCREAMING_SNAKE_CASE_							=			num_labels
       SCREAMING_SNAKE_CASE_							=			num_choices
       SCREAMING_SNAKE_CASE_							=			scope



 def       lowerCAmelCase_  (							self  :		List[str]						):
       SCREAMING_SNAKE_CASE_							=			ids_tensor([self.batch_size, self.seq_length]				,				self.vocab_size						)

       SCREAMING_SNAKE_CASE_							=			None
       if self.use_input_mask:
             SCREAMING_SNAKE_CASE_							=			random_attention_mask([self.batch_size, self.seq_length]						)

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

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

       SCREAMING_SNAKE_CASE_							=			self.get_config()

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



 def       lowerCAmelCase_  (							self  :		int						):
       return NystromformerConfig(
           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				,				is_decoder=_lowerCAmelCase				,				initializer_range=self.initializer_range				,				)



 def       lowerCAmelCase_  (							self  :		int				,				_lowerCAmelCase  :		Tuple				,				_lowerCAmelCase  :		Optional[int]				,				_lowerCAmelCase  :		Optional[int]				,				_lowerCAmelCase  :		Any				,				_lowerCAmelCase  :		Optional[int]				,				_lowerCAmelCase  :		Optional[Any]				,				_lowerCAmelCase  :		List[str]						):
       SCREAMING_SNAKE_CASE_							=			NystromformerModel(config=_lowerCAmelCase						)
       model.to(_lowerCAmelCase						)
       model.eval()
       SCREAMING_SNAKE_CASE_							=			model(_lowerCAmelCase				,				attention_mask=_lowerCAmelCase				,				token_type_ids=_lowerCAmelCase						)
       SCREAMING_SNAKE_CASE_							=			model(_lowerCAmelCase				,				token_type_ids=_lowerCAmelCase						)
       SCREAMING_SNAKE_CASE_							=			model(_lowerCAmelCase						)
       self.parent.assertEqual(result.last_hidden_state.shape				,				(self.batch_size, self.seq_length, self.hidden_size)						)



 def       lowerCAmelCase_  (							self  :		str				,				_lowerCAmelCase  :		Dict				,				_lowerCAmelCase  :		int				,				_lowerCAmelCase  :		Dict				,				_lowerCAmelCase  :		Optional[Any]				,				_lowerCAmelCase  :		List[str]				,				_lowerCAmelCase  :		Optional[int]				,				_lowerCAmelCase  :		Union[str, Any]						):
       SCREAMING_SNAKE_CASE_							=			NystromformerForMaskedLM(config=_lowerCAmelCase						)
       model.to(_lowerCAmelCase						)
       model.eval()
       SCREAMING_SNAKE_CASE_							=			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       lowerCAmelCase_  (							self  :		Any				,				_lowerCAmelCase  :		Optional[Any]				,				_lowerCAmelCase  :		List[Any]				,				_lowerCAmelCase  :		Any				,				_lowerCAmelCase  :		Dict				,				_lowerCAmelCase  :		Any				,				_lowerCAmelCase  :		Dict				,				_lowerCAmelCase  :		str						):
       SCREAMING_SNAKE_CASE_							=			NystromformerForQuestionAnswering(config=_lowerCAmelCase						)
       model.to(_lowerCAmelCase						)
       model.eval()
       SCREAMING_SNAKE_CASE_							=			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       lowerCAmelCase_  (							self  :		Union[str, Any]				,				_lowerCAmelCase  :		Union[str, Any]				,				_lowerCAmelCase  :		Optional[Any]				,				_lowerCAmelCase  :		str				,				_lowerCAmelCase  :		str				,				_lowerCAmelCase  :		Any				,				_lowerCAmelCase  :		Tuple				,				_lowerCAmelCase  :		List[str]						):
       SCREAMING_SNAKE_CASE_							=			self.num_labels
       SCREAMING_SNAKE_CASE_							=			NystromformerForSequenceClassification(_lowerCAmelCase						)
       model.to(_lowerCAmelCase						)
       model.eval()
       SCREAMING_SNAKE_CASE_							=			model(_lowerCAmelCase				,				attention_mask=_lowerCAmelCase				,				token_type_ids=_lowerCAmelCase				,				labels=_lowerCAmelCase						)
       self.parent.assertEqual(result.logits.shape				,				(self.batch_size, self.num_labels)						)



 def       lowerCAmelCase_  (							self  :		Tuple				,				_lowerCAmelCase  :		Any				,				_lowerCAmelCase  :		Dict				,				_lowerCAmelCase  :		List[Any]				,				_lowerCAmelCase  :		List[str]				,				_lowerCAmelCase  :		List[str]				,				_lowerCAmelCase  :		Union[str, Any]				,				_lowerCAmelCase  :		int						):
       SCREAMING_SNAKE_CASE_							=			self.num_labels
       SCREAMING_SNAKE_CASE_							=			NystromformerForTokenClassification(config=_lowerCAmelCase						)
       model.to(_lowerCAmelCase						)
       model.eval()
       SCREAMING_SNAKE_CASE_							=			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       lowerCAmelCase_  (							self  :		Union[str, Any]				,				_lowerCAmelCase  :		str				,				_lowerCAmelCase  :		str				,				_lowerCAmelCase  :		Any				,				_lowerCAmelCase  :		List[Any]				,				_lowerCAmelCase  :		List[str]				,				_lowerCAmelCase  :		int				,				_lowerCAmelCase  :		Dict						):
       SCREAMING_SNAKE_CASE_							=			self.num_choices
       SCREAMING_SNAKE_CASE_							=			NystromformerForMultipleChoice(config=_lowerCAmelCase						)
       model.to(_lowerCAmelCase						)
       model.eval()
       SCREAMING_SNAKE_CASE_							=			input_ids.unsqueeze(1						).expand(-1				,				self.num_choices				,				-1						).contiguous()
       SCREAMING_SNAKE_CASE_							=			token_type_ids.unsqueeze(1						).expand(-1				,				self.num_choices				,				-1						).contiguous()
       SCREAMING_SNAKE_CASE_							=			input_mask.unsqueeze(1						).expand(-1				,				self.num_choices				,				-1						).contiguous()
       SCREAMING_SNAKE_CASE_							=			model(
           _lowerCAmelCase				,				attention_mask=_lowerCAmelCase				,				token_type_ids=_lowerCAmelCase				,				labels=_lowerCAmelCase				,				)
       self.parent.assertEqual(result.logits.shape				,				(self.batch_size, self.num_choices)						)




 def       lowerCAmelCase_  (							self  :		Any						):
       SCREAMING_SNAKE_CASE_							=			self.prepare_config_and_inputs()
       (
           (
           SCREAMING_SNAKE_CASE_
       )						, (
           SCREAMING_SNAKE_CASE_
       )						, (
           SCREAMING_SNAKE_CASE_
       )						, (
           SCREAMING_SNAKE_CASE_
       )						, (
           SCREAMING_SNAKE_CASE_
       )						, (
           SCREAMING_SNAKE_CASE_
       )						, (
           SCREAMING_SNAKE_CASE_
       )						, 
       )							=			config_and_inputs
       SCREAMING_SNAKE_CASE_							=			{'input_ids': input_ids, 'token_type_ids': token_type_ids, 'attention_mask': input_mask}
       return config, inputs_dict





@require_torch
class        lowerCamelCase_							(      _SCREAMING_SNAKE_CASE       ,   _SCREAMING_SNAKE_CASE       ,   unittest.TestCase					):


 '''simple docstring'''







 lowercase_				    =    (
     (
         NystromformerModel,
         NystromformerForMaskedLM,
         NystromformerForMultipleChoice,
         NystromformerForQuestionAnswering,
         NystromformerForSequenceClassification,
         NystromformerForTokenClassification,
     )
     if is_torch_available()
     else ()
 )
 lowercase_				    =    (
     {
         "feature-extraction": NystromformerModel,
         "fill-mask": NystromformerForMaskedLM,
         "question-answering": NystromformerForQuestionAnswering,
         "text-classification": NystromformerForSequenceClassification,
         "token-classification": NystromformerForTokenClassification,
         "zero-shot": NystromformerForSequenceClassification,
     }
     if is_torch_available()
     else {}
 )
 lowercase_				    =    False
 lowercase_				    =    False



 def       lowerCAmelCase_  (							self  :		Union[str, Any]						):
       SCREAMING_SNAKE_CASE_							=			NystromformerModelTester(self						)
       SCREAMING_SNAKE_CASE_							=			ConfigTester(self				,				config_class=_lowerCAmelCase				,				hidden_size=37						)



 def       lowerCAmelCase_  (							self  :		Dict						):
       self.config_tester.run_common_tests()



 def       lowerCAmelCase_  (							self  :		str						):
       SCREAMING_SNAKE_CASE_							=			self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_model(*_lowerCAmelCase						)



 def       lowerCAmelCase_  (							self  :		Any						):
       SCREAMING_SNAKE_CASE_							=			self.model_tester.prepare_config_and_inputs()
       for type in ["absolute", "relative_key", "relative_key_query"]:
             SCREAMING_SNAKE_CASE_							=			type
             self.model_tester.create_and_check_model(*_lowerCAmelCase						)



 def       lowerCAmelCase_  (							self  :		str						):
       SCREAMING_SNAKE_CASE_							=			self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_for_masked_lm(*_lowerCAmelCase						)



 def       lowerCAmelCase_  (							self  :		Optional[Any]						):
       SCREAMING_SNAKE_CASE_							=			self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_for_multiple_choice(*_lowerCAmelCase						)



 def       lowerCAmelCase_  (							self  :		Dict						):
       SCREAMING_SNAKE_CASE_							=			self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_for_question_answering(*_lowerCAmelCase						)



 def       lowerCAmelCase_  (							self  :		List[str]						):
       SCREAMING_SNAKE_CASE_							=			self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_for_sequence_classification(*_lowerCAmelCase						)



 def       lowerCAmelCase_  (							self  :		Optional[Any]						):
       SCREAMING_SNAKE_CASE_							=			self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_for_token_classification(*_lowerCAmelCase						)




 @slow
 def       lowerCAmelCase_  (							self  :		Optional[Any]						):
       for model_name in NYSTROMFORMER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
             SCREAMING_SNAKE_CASE_							=			NystromformerModel.from_pretrained(_lowerCAmelCase						)
             self.assertIsNotNone(_lowerCAmelCase						)





@require_torch
class        lowerCamelCase_							(      unittest.TestCase					):


 '''simple docstring'''







 @slow
 def       lowerCAmelCase_  (							self  :		Any						):
       SCREAMING_SNAKE_CASE_							=			NystromformerModel.from_pretrained('uw-madison/nystromformer-512'						)
       SCREAMING_SNAKE_CASE_							=			torch.tensor([[0, 1, 2, 3, 4, 5]]						)

       with torch.no_grad():
             SCREAMING_SNAKE_CASE_							=			model(_lowerCAmelCase						)[0]

       SCREAMING_SNAKE_CASE_							=			torch.Size((1, 6, 768)						)
       self.assertEqual(output.shape				,				_lowerCAmelCase						)

       SCREAMING_SNAKE_CASE_							=			torch.tensor(
           [[[-0.4532, -0.0936, 0.5137], [-0.2676, 0.0628, 0.6186], [-0.3629, -0.1726, 0.4716]]]						)

       self.assertTrue(torch.allclose(output[:, :3, :3]				,				_lowerCAmelCase				,				atol=1E-4						)						)




 @slow
 def       lowerCAmelCase_  (							self  :		str						):
       SCREAMING_SNAKE_CASE_							=			'the [MASK] of Belgium is Brussels'

       SCREAMING_SNAKE_CASE_							=			AutoTokenizer.from_pretrained('uw-madison/nystromformer-512'						)
       SCREAMING_SNAKE_CASE_							=			NystromformerForMaskedLM.from_pretrained('uw-madison/nystromformer-512'						)

       SCREAMING_SNAKE_CASE_							=			tokenizer(_lowerCAmelCase				,				return_tensors='pt'						)

       with torch.no_grad():
             SCREAMING_SNAKE_CASE_							=			model(encoding.input_ids						).logits

       SCREAMING_SNAKE_CASE_							=			token_logits[:, 2, :].argmax(-1						)[0]

       self.assertEqual(tokenizer.decode(_lowerCAmelCase						)				,				'capital'						)





class        lowerCamelCase_							:


 '''simple docstring'''







 def __init__(							self  :		List[Any]						):
       SCREAMING_SNAKE_CASE_							=			{}



 def       lowerCAmelCase_  (							self  :		List[str]						):
       print(self.vertex						)
       for i in self.vertex:
             print(_lowerCAmelCase				,				' -> '				,				' -> '.join([str(_lowerCAmelCase						) for j in self.vertex[i]]						)						)



 def       lowerCAmelCase_  (							self  :		List[Any]				,				_lowerCAmelCase  :		int				,				_lowerCAmelCase  :		int						):
       # check if vertex is already present,
       if from_vertex in self.vertex:
             self.vertex[from_vertex].append(_lowerCAmelCase						)
       else:
             # else make a new vertex
             SCREAMING_SNAKE_CASE_							=			[to_vertex]



 def       lowerCAmelCase_  (							self  :		Dict						):
       # visited array for storing already visited nodes
       SCREAMING_SNAKE_CASE_							=			[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       lowerCAmelCase_  (							self  :		Union[str, Any]				,				_lowerCAmelCase  :		int				,				_lowerCAmelCase  :		list						):
       # mark start vertex as visited
       SCREAMING_SNAKE_CASE_							=			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__":
   lowerCamelCase__ :   List[Any]					=							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