



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 SCREAMING_SNAKE_CASE__	:




    '''simple docstring'''






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






    def      _lowerCAmelCase   (			self						):
        A					:			str		=  ids_tensor([self.batch_size, self.seq_length],						self.vocab_size						)

        A					:			Dict		=  None
        if self.use_input_mask:
            A					:			Optional[int]		=  random_attention_mask([self.batch_size, self.seq_length]						)

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

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

        A					:			List[str]		=  self.get_config()

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






    def      _lowerCAmelCase   (			self						):
        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,						lowerCamelCase__,						lowerCamelCase__,						lowerCamelCase__,						lowerCamelCase__,						lowerCamelCase__,						lowerCamelCase__,						lowerCamelCase__						):
        A					:			List[Any]		=  NystromformerModel(config=_lowerCamelCase						)
        model.to(_lowerCamelCase						)
        model.eval()
        A					:			List[Any]		=  model(_lowerCamelCase,						attention_mask=_lowerCamelCase,						token_type_ids=_lowerCamelCase						)
        A					:			Union[str, Any]		=  model(_lowerCamelCase,						token_type_ids=_lowerCamelCase						)
        A					:			Any		=  model(_lowerCamelCase						)
        self.parent.assertEqual(result.last_hidden_state.shape,						(self.batch_size, self.seq_length, self.hidden_size)						)






    def      _lowerCAmelCase   (			self,						lowerCamelCase__,						lowerCamelCase__,						lowerCamelCase__,						lowerCamelCase__,						lowerCamelCase__,						lowerCamelCase__,						lowerCamelCase__						):
        A					:			Optional[int]		=  NystromformerForMaskedLM(config=_lowerCamelCase						)
        model.to(_lowerCamelCase						)
        model.eval()
        A					:			Tuple		=  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,						lowerCamelCase__,						lowerCamelCase__,						lowerCamelCase__,						lowerCamelCase__,						lowerCamelCase__,						lowerCamelCase__,						lowerCamelCase__						):
        A					:			Dict		=  NystromformerForQuestionAnswering(config=_lowerCamelCase						)
        model.to(_lowerCamelCase						)
        model.eval()
        A					:			Optional[Any]		=  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,						lowerCamelCase__,						lowerCamelCase__,						lowerCamelCase__,						lowerCamelCase__,						lowerCamelCase__,						lowerCamelCase__,						lowerCamelCase__						):
        A					:			List[str]		=  self.num_labels
        A					:			Any		=  NystromformerForSequenceClassification(_lowerCamelCase						)
        model.to(_lowerCamelCase						)
        model.eval()
        A					:			Optional[int]		=  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,						lowerCamelCase__,						lowerCamelCase__,						lowerCamelCase__,						lowerCamelCase__,						lowerCamelCase__,						lowerCamelCase__,						lowerCamelCase__						):
        A					:			List[str]		=  self.num_labels
        A					:			str		=  NystromformerForTokenClassification(config=_lowerCamelCase						)
        model.to(_lowerCamelCase						)
        model.eval()
        A					:			Union[str, 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      _lowerCAmelCase   (			self,						lowerCamelCase__,						lowerCamelCase__,						lowerCamelCase__,						lowerCamelCase__,						lowerCamelCase__,						lowerCamelCase__,						lowerCamelCase__						):
        A					:			Optional[Any]		=  self.num_choices
        A					:			Optional[Any]		=  NystromformerForMultipleChoice(config=_lowerCamelCase						)
        model.to(_lowerCamelCase						)
        model.eval()
        A					:			Optional[Any]		=  input_ids.unsqueeze(1						).expand(-1,						self.num_choices,						-1						).contiguous()
        A					:			List[Any]		=  token_type_ids.unsqueeze(1						).expand(-1,						self.num_choices,						-1						).contiguous()
        A					:			int		=  input_mask.unsqueeze(1						).expand(-1,						self.num_choices,						-1						).contiguous()
        A					:			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      _lowerCAmelCase   (			self						):
        A					:			List[Any]		=  self.prepare_config_and_inputs()
        (
            A							
        )					:			Any		=  config_and_inputs
        A					:			Tuple		=  {'''input_ids''': input_ids, '''token_type_ids''': token_type_ids, '''attention_mask''': input_mask}
        return config, inputs_dict






@require_torch
class SCREAMING_SNAKE_CASE__	(						_snake_case					,		_snake_case					,		unittest.TestCase       ):




    '''simple docstring'''






    __lowerCamelCase     :					Tuple   = (
        (
            NystromformerModel,
            NystromformerForMaskedLM,
            NystromformerForMultipleChoice,
            NystromformerForQuestionAnswering,
            NystromformerForSequenceClassification,
            NystromformerForTokenClassification,
        )
        if is_torch_available()
        else ()
    )
    __lowerCamelCase     :					Optional[int]   = (
        {
            "feature-extraction": NystromformerModel,
            "fill-mask": NystromformerForMaskedLM,
            "question-answering": NystromformerForQuestionAnswering,
            "text-classification": NystromformerForSequenceClassification,
            "token-classification": NystromformerForTokenClassification,
            "zero-shot": NystromformerForSequenceClassification,
        }
        if is_torch_available()
        else {}
    )
    __lowerCamelCase     :					str   = False
    __lowerCamelCase     :					Optional[int]   = False






    def      _lowerCAmelCase   (			self						):
        A					:			Any		=  NystromformerModelTester(self						)
        A					:			Tuple		=  ConfigTester(self,						config_class=_lowerCamelCase,						hidden_size=37						)






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






    def      _lowerCAmelCase   (			self						):
        A					:			Tuple		=  self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_model(*_lowerCamelCase						)






    def      _lowerCAmelCase   (			self						):
        A					:			Dict		=  self.model_tester.prepare_config_and_inputs()
        for type in ["absolute", "relative_key", "relative_key_query"]:
            A					:			Union[str, Any]		=  type
            self.model_tester.create_and_check_model(*_lowerCamelCase						)






    def      _lowerCAmelCase   (			self						):
        A					:			Tuple		=  self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_masked_lm(*_lowerCamelCase						)






    def      _lowerCAmelCase   (			self						):
        A					:			List[Any]		=  self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_multiple_choice(*_lowerCamelCase						)






    def      _lowerCAmelCase   (			self						):
        A					:			Any		=  self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_question_answering(*_lowerCamelCase						)






    def      _lowerCAmelCase   (			self						):
        A					:			Optional[int]		=  self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_sequence_classification(*_lowerCamelCase						)






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






    @slow
    def      _lowerCAmelCase   (			self						):
        for model_name in NYSTROMFORMER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
            A					:			str		=  NystromformerModel.from_pretrained(_lowerCamelCase						)
            self.assertIsNotNone(_lowerCamelCase						)






@require_torch
class SCREAMING_SNAKE_CASE__	(						unittest.TestCase       ):




    '''simple docstring'''






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

        with torch.no_grad():
            A					:			Optional[int]		=  model(_lowerCamelCase						)[0]

        A					:			Optional[Any]		=  torch.Size((1, 6, 768)						)
        self.assertEqual(output.shape,						_lowerCamelCase						)

        A					:			Dict		=  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						):
        A					:			Optional[int]		=  '''the [MASK] of Belgium is Brussels'''

        A					:			Dict		=  AutoTokenizer.from_pretrained("""uw-madison/nystromformer-512"""						)
        A					:			str		=  NystromformerForMaskedLM.from_pretrained("""uw-madison/nystromformer-512"""						)

        A					:			Dict		=  tokenizer(_lowerCamelCase,						return_tensors="""pt"""						)

        with torch.no_grad():
            A					:			Union[str, Any]		=  model(encoding.input_ids						).logits

        A					:			Optional[Any]		=  token_logits[:, 2, :].argmax(-1						)[0]

        self.assertEqual(tokenizer.decode(_lowerCamelCase						),						"""capital"""						)






snake_case							:       Any										=	{0: [2, 3], 1: [0], 2: [1], 3: [4], 4: []}

snake_case							:       Any										=	{0: [1, 2, 3], 1: [2], 2: [0], 3: [4], 4: [5], 5: [3]}
def 							__lowerCamelCase		(					UpperCAmelCase_		:						dict[int, list[int]]					,    UpperCAmelCase_		:						int					,    UpperCAmelCase_		:						list[bool]		):




      """simple docstring"""





      a		:Any									=			True
      a		:Any									=			[]

      for neighbour in graph[vert]:
            if not visited[neighbour]:
                  order += topology_sort(UpperCAmelCase_					,    UpperCAmelCase_					,    UpperCAmelCase_		)

      order.append(UpperCAmelCase_		)

      return order
def 							__lowerCamelCase		(					UpperCAmelCase_		:						dict[int, list[int]]					,    UpperCAmelCase_		:						int					,    UpperCAmelCase_		:						list[bool]		):




      """simple docstring"""





      a		:Optional[Any]									=			True
      a		:int									=			[vert]

      for neighbour in reversed_graph[vert]:
            if not visited[neighbour]:
                  component += find_components(UpperCAmelCase_					,    UpperCAmelCase_					,    UpperCAmelCase_		)

      return component






def 							__lowerCamelCase		(					UpperCAmelCase_		:						dict[int, list[int]]		):




      """simple docstring"""





      a		:int									=			len(UpperCAmelCase_		) * [False]
      a		:dict[int, list[int]]									=			{vert: [] for vert in range(len(UpperCAmelCase_		)		)}

      for vert, neighbours in graph.items():
            for neighbour in neighbours:
                  reversed_graph[neighbour].append(UpperCAmelCase_		)

      a		:Optional[Any]									=			[]
      for i, was_visited in enumerate(UpperCAmelCase_		):
            if not was_visited:
                  order += topology_sort(UpperCAmelCase_					,    UpperCAmelCase_					,    UpperCAmelCase_		)

      a		:Tuple									=			[]
      a		:int									=			len(UpperCAmelCase_		) * [False]

      for i in range(len(UpperCAmelCase_		)		):
            a		:Union[str, Any]									=			order[len(UpperCAmelCase_		) - i - 1]
            if not visited[vert]:
                  a		:Union[str, Any]									=			find_components(UpperCAmelCase_					,    UpperCAmelCase_					,    UpperCAmelCase_		)
                  components_list.append(UpperCAmelCase_		)

      return components_list





