

"""simple docstring"""



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



    """simple docstring"""



    def __init__(   self:       int			,  UpperCamelCase:       Optional[int]			,  UpperCamelCase:       Any=13			,  UpperCamelCase:       List[str]=7			,  UpperCamelCase:       List[Any]=True			,  UpperCamelCase:       str=True			,  UpperCamelCase:       Dict=True			,  UpperCamelCase:       List[str]=True			,  UpperCamelCase:       List[str]=99			,  UpperCamelCase:       Dict=32			,  UpperCamelCase:       List[Any]=5			,  UpperCamelCase:       List[str]=4			,  UpperCamelCase:       Dict=37			,  UpperCamelCase:       List[Any]="gelu"			,  UpperCamelCase:       Dict=0.1			,  UpperCamelCase:       Any=0.1			,  UpperCamelCase:       int=5_12			,  UpperCamelCase:       Tuple=16			,  UpperCamelCase:       str=2			,  UpperCamelCase:       Optional[Any]=0.02			,  UpperCamelCase:       Any=3			,  UpperCamelCase:       Any=4			,  UpperCamelCase:       Dict=None			,  ):






         """simple docstring"""


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

    def  UpperCamelCase		(   self:       Tuple				):






         """simple docstring"""


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

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

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

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

         A__								=     self.get_config()

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

    def  UpperCamelCase		(   self:       int				):






         """simple docstring"""


         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=lowercase_			,  initializer_range=self.initializer_range			,  )

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






         """simple docstring"""


         A__								=     NystromformerModel(config=lowercase_				)
         model.to(lowercase_				)
         model.eval()
         A__								=     model(lowercase_			,  attention_mask=lowercase_			,  token_type_ids=lowercase_				)
         A__								=     model(lowercase_			,  token_type_ids=lowercase_				)
         A__								=     model(lowercase_				)
         self.parent.assertEqual(result.last_hidden_state.shape			,  (self.batch_size, self.seq_length, self.hidden_size)				)

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






         """simple docstring"""


         A__								=     NystromformerForMaskedLM(config=lowercase_				)
         model.to(lowercase_				)
         model.eval()
         A__								=     model(lowercase_			,  attention_mask=lowercase_			,  token_type_ids=lowercase_			,  labels=lowercase_				)
         self.parent.assertEqual(result.logits.shape			,  (self.batch_size, self.seq_length, self.vocab_size)				)

    def  UpperCamelCase		(   self:       Tuple			,  UpperCamelCase:       int			,  UpperCamelCase:       List[Any]			,  UpperCamelCase:       Any			,  UpperCamelCase:       str			,  UpperCamelCase:       Dict			,  UpperCamelCase:       Any			,  UpperCamelCase:       Tuple				):






         """simple docstring"""


         A__								=     NystromformerForQuestionAnswering(config=lowercase_				)
         model.to(lowercase_				)
         model.eval()
         A__								=     model(
             lowercase_			,  attention_mask=lowercase_			,  token_type_ids=lowercase_			,  start_positions=lowercase_			,  end_positions=lowercase_			,  )
         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:       Tuple			,  UpperCamelCase:       List[str]			,  UpperCamelCase:       List[Any]			,  UpperCamelCase:       Union[str, Any]			,  UpperCamelCase:       str			,  UpperCamelCase:       Optional[Any]			,  UpperCamelCase:       str			,  UpperCamelCase:       int				):






         """simple docstring"""


         A__								=     self.num_labels
         A__								=     NystromformerForSequenceClassification(lowercase_				)
         model.to(lowercase_				)
         model.eval()
         A__								=     model(lowercase_			,  attention_mask=lowercase_			,  token_type_ids=lowercase_			,  labels=lowercase_				)
         self.parent.assertEqual(result.logits.shape			,  (self.batch_size, self.num_labels)				)

    def  UpperCamelCase		(   self:       List[Any]			,  UpperCamelCase:       List[str]			,  UpperCamelCase:       int			,  UpperCamelCase:       List[str]			,  UpperCamelCase:       Dict			,  UpperCamelCase:       Tuple			,  UpperCamelCase:       Union[str, Any]			,  UpperCamelCase:       List[str]				):






         """simple docstring"""


         A__								=     self.num_labels
         A__								=     NystromformerForTokenClassification(config=lowercase_				)
         model.to(lowercase_				)
         model.eval()
         A__								=     model(lowercase_			,  attention_mask=lowercase_			,  token_type_ids=lowercase_			,  labels=lowercase_				)
         self.parent.assertEqual(result.logits.shape			,  (self.batch_size, self.seq_length, self.num_labels)				)

    def  UpperCamelCase		(   self:       Optional[Any]			,  UpperCamelCase:       Tuple			,  UpperCamelCase:       Dict			,  UpperCamelCase:       int			,  UpperCamelCase:       List[str]			,  UpperCamelCase:       Optional[Any]			,  UpperCamelCase:       str			,  UpperCamelCase:       Union[str, Any]				):






         """simple docstring"""


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





    def  UpperCamelCase		(   self:       Dict				):






         """simple docstring"""


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



@require_torch
class     a  (				_UpperCAmelCase,		_UpperCAmelCase,		unittest.TestCase ):



    """simple docstring"""



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

    def  UpperCamelCase		(   self:       int				):






         """simple docstring"""


         A__								=     NystromformerModelTester(self				)
         A__								=     ConfigTester(self			,  config_class=lowercase_			,  hidden_size=37				)

    def  UpperCamelCase		(   self:       Tuple				):






         """simple docstring"""


         self.config_tester.run_common_tests()

    def  UpperCamelCase		(   self:       Dict				):






         """simple docstring"""


         A__								=     self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_model(*lowercase_				)

    def  UpperCamelCase		(   self:       List[Any]				):






         """simple docstring"""


         A__								=     self.model_tester.prepare_config_and_inputs()
         for type in ["absolute", "relative_key", "relative_key_query"]:
              A__								=     type
              self.model_tester.create_and_check_model(*lowercase_				)

    def  UpperCamelCase		(   self:       Optional[Any]				):






         """simple docstring"""


         A__								=     self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_for_masked_lm(*lowercase_				)

    def  UpperCamelCase		(   self:       Optional[int]				):






         """simple docstring"""


         A__								=     self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_for_multiple_choice(*lowercase_				)

    def  UpperCamelCase		(   self:       Optional[Any]				):






         """simple docstring"""


         A__								=     self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_for_question_answering(*lowercase_				)

    def  UpperCamelCase		(   self:       List[Any]				):






         """simple docstring"""


         A__								=     self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_for_sequence_classification(*lowercase_				)

    def  UpperCamelCase		(   self:       List[Any]				):






         """simple docstring"""


         A__								=     self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_for_token_classification(*lowercase_				)





    @slow
    def  UpperCamelCase		(   self:       Optional[int]				):






         """simple docstring"""


         for model_name in NYSTROMFORMER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
              A__								=     NystromformerModel.from_pretrained(lowercase_				)
              self.assertIsNotNone(lowercase_				)



@require_torch
class     a  (				unittest.TestCase ):



    """simple docstring"""



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






         """simple docstring"""


         A__								=     NystromformerModel.from_pretrained("""uw-madison/nystromformer-512"""				)
         A__								=     torch.tensor([[0, 1, 2, 3, 4, 5]]				)

         with torch.no_grad():
              A__								=     model(lowercase_				)[0]

         A__								=     torch.Size((1, 6, 7_68)				)
         self.assertEqual(output.shape			,  lowercase_				)

         A__								=     torch.tensor(
             [[[-0.4_532, -0.0_936, 0.5_137], [-0.2_676, 0.0_628, 0.6_186], [-0.3_629, -0.1_726, 0.4_716]]]				)

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





    @slow
    def  UpperCamelCase		(   self:       Optional[int]				):






         """simple docstring"""


         A__								=     """the [MASK] of Belgium is Brussels"""

         A__								=     AutoTokenizer.from_pretrained("""uw-madison/nystromformer-512"""				)
         A__								=     NystromformerForMaskedLM.from_pretrained("""uw-madison/nystromformer-512"""				)

         A__								=     tokenizer(lowercase_			,  return_tensors="""pt"""				)

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

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

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







'''simple docstring'''




_lowercase						: str						=						tuple[float, float, float]
_lowercase						: List[Any]						=						tuple[float, float, float]




def 							lowerCamelCase	(      UpperCAmelCase__       :					Pointad      ,      UpperCAmelCase__       :					Pointad )			->      Vectorad:
							lowercase_					:			List[str]										=       end_pointa[0] - end_pointa[0]
							lowercase_					:			Union[str, Any]										=       end_pointa[1] - end_pointa[1]
							lowercase_					:			List[Any]										=       end_pointa[2] - end_pointa[2]
							return (x, y, z)




def 							lowerCamelCase	(      UpperCAmelCase__       :					Vectorad      ,      UpperCAmelCase__       :					Vectorad )			->      Vectorad:
							lowercase_					:			List[Any]										=       ab[1] * ac[2] - ab[2] * ac[1]  # *i
							lowercase_					:			Union[str, Any]										=       (ab[0] * ac[2] - ab[2] * ac[0]) * -1  # *j
							lowercase_					:			List[str]										=       ab[0] * ac[1] - ab[1] * ac[0]  # *k
							return (x, y, z)




def 							lowerCamelCase	(      UpperCAmelCase__       :					Vectorad      ,      UpperCAmelCase__       :					int )			->      bool:
							return tuple(round(UpperCAmelCase__      ,      UpperCAmelCase__ ) for x in vector ) == (0, 0, 0)




def 							lowerCamelCase	(      UpperCAmelCase__       :					Pointad      ,      UpperCAmelCase__       :					Pointad      ,      UpperCAmelCase__       :					Pointad      ,      UpperCAmelCase__       :					int = 10 )			->      bool:
							lowercase_					:			Dict										=       create_vector(UpperCAmelCase__      ,      UpperCAmelCase__ )
							lowercase_					:			Optional[int]										=       create_vector(UpperCAmelCase__      ,      UpperCAmelCase__ )
							return is_zero_vector(get_ad_vectors_cross(UpperCAmelCase__      ,      UpperCAmelCase__ )      ,      UpperCAmelCase__ )





