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






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






       def       _snake_case    (   self   ):
              __UpperCAmelCase	:					Any												=		ids_tensor([self.batch_size, self.seq_length]					,  self.vocab_size   )

              __UpperCAmelCase	:					Union[str, Any]												=		None
              if self.use_input_mask:
                     __UpperCAmelCase	:					Optional[Any]												=		random_attention_mask([self.batch_size, self.seq_length]   )

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

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

              __UpperCAmelCase	:					int												=		self.get_config()

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






       def       _snake_case    (   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=UpperCamelCase_					,  initializer_range=self.initializer_range					,  )






       def       _snake_case    (   self					,  UpperCamelCase_					,  UpperCamelCase_					,  UpperCamelCase_					,  UpperCamelCase_					,  UpperCamelCase_					,  UpperCamelCase_					,  UpperCamelCase_   ):
              __UpperCAmelCase	:					str												=		NystromformerModel(config=UpperCamelCase_   )
              model.to(UpperCamelCase_   )
              model.eval()
              __UpperCAmelCase	:					Union[str, Any]												=		model(UpperCamelCase_					,  attention_mask=UpperCamelCase_					,  token_type_ids=UpperCamelCase_   )
              __UpperCAmelCase	:					List[str]												=		model(UpperCamelCase_					,  token_type_ids=UpperCamelCase_   )
              __UpperCAmelCase	:					List[str]												=		model(UpperCamelCase_   )
              self.parent.assertEqual(result.last_hidden_state.shape					,  (self.batch_size, self.seq_length, self.hidden_size)   )






       def       _snake_case    (   self					,  UpperCamelCase_					,  UpperCamelCase_					,  UpperCamelCase_					,  UpperCamelCase_					,  UpperCamelCase_					,  UpperCamelCase_					,  UpperCamelCase_   ):
              __UpperCAmelCase	:					Union[str, Any]												=		NystromformerForMaskedLM(config=UpperCamelCase_   )
              model.to(UpperCamelCase_   )
              model.eval()
              __UpperCAmelCase	:					Union[str, Any]												=		model(UpperCamelCase_					,  attention_mask=UpperCamelCase_					,  token_type_ids=UpperCamelCase_					,  labels=UpperCamelCase_   )
              self.parent.assertEqual(result.logits.shape					,  (self.batch_size, self.seq_length, self.vocab_size)   )






       def       _snake_case    (   self					,  UpperCamelCase_					,  UpperCamelCase_					,  UpperCamelCase_					,  UpperCamelCase_					,  UpperCamelCase_					,  UpperCamelCase_					,  UpperCamelCase_   ):
              __UpperCAmelCase	:					List[str]												=		NystromformerForQuestionAnswering(config=UpperCamelCase_   )
              model.to(UpperCamelCase_   )
              model.eval()
              __UpperCAmelCase	:					Dict												=		model(
                  UpperCamelCase_					,  attention_mask=UpperCamelCase_					,  token_type_ids=UpperCamelCase_					,  start_positions=UpperCamelCase_					,  end_positions=UpperCamelCase_					,  )
              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       _snake_case    (   self					,  UpperCamelCase_					,  UpperCamelCase_					,  UpperCamelCase_					,  UpperCamelCase_					,  UpperCamelCase_					,  UpperCamelCase_					,  UpperCamelCase_   ):
              __UpperCAmelCase	:					Union[str, Any]												=		self.num_labels
              __UpperCAmelCase	:					int												=		NystromformerForSequenceClassification(UpperCamelCase_   )
              model.to(UpperCamelCase_   )
              model.eval()
              __UpperCAmelCase	:					str												=		model(UpperCamelCase_					,  attention_mask=UpperCamelCase_					,  token_type_ids=UpperCamelCase_					,  labels=UpperCamelCase_   )
              self.parent.assertEqual(result.logits.shape					,  (self.batch_size, self.num_labels)   )






       def       _snake_case    (   self					,  UpperCamelCase_					,  UpperCamelCase_					,  UpperCamelCase_					,  UpperCamelCase_					,  UpperCamelCase_					,  UpperCamelCase_					,  UpperCamelCase_   ):
              __UpperCAmelCase	:					Tuple												=		self.num_labels
              __UpperCAmelCase	:					int												=		NystromformerForTokenClassification(config=UpperCamelCase_   )
              model.to(UpperCamelCase_   )
              model.eval()
              __UpperCAmelCase	:					Union[str, Any]												=		model(UpperCamelCase_					,  attention_mask=UpperCamelCase_					,  token_type_ids=UpperCamelCase_					,  labels=UpperCamelCase_   )
              self.parent.assertEqual(result.logits.shape					,  (self.batch_size, self.seq_length, self.num_labels)   )






       def       _snake_case    (   self					,  UpperCamelCase_					,  UpperCamelCase_					,  UpperCamelCase_					,  UpperCamelCase_					,  UpperCamelCase_					,  UpperCamelCase_					,  UpperCamelCase_   ):
              __UpperCAmelCase	:					int												=		self.num_choices
              __UpperCAmelCase	:					Any												=		NystromformerForMultipleChoice(config=UpperCamelCase_   )
              model.to(UpperCamelCase_   )
              model.eval()
              __UpperCAmelCase	:					Optional[Any]												=		input_ids.unsqueeze(1   ).expand(-1					,  self.num_choices					,  -1   ).contiguous()
              __UpperCAmelCase	:					List[str]												=		token_type_ids.unsqueeze(1   ).expand(-1					,  self.num_choices					,  -1   ).contiguous()
              __UpperCAmelCase	:					Dict												=		input_mask.unsqueeze(1   ).expand(-1					,  self.num_choices					,  -1   ).contiguous()
              __UpperCAmelCase	:					str												=		model(
                  UpperCamelCase_					,  attention_mask=UpperCamelCase_					,  token_type_ids=UpperCamelCase_					,  labels=UpperCamelCase_					,  )
              self.parent.assertEqual(result.logits.shape					,  (self.batch_size, self.num_choices)   )







       def       _snake_case    (   self   ):
              __UpperCAmelCase	:					Tuple												=		self.prepare_config_and_inputs()
              (
                  __UpperCAmelCase    
              )	:					Union[str, Any]												=		config_and_inputs
              __UpperCAmelCase	:					Optional[int]												=		{"""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 ):
       snake_case							:str					     =       (
           (
               NystromformerModel,
               NystromformerForMaskedLM,
               NystromformerForMultipleChoice,
               NystromformerForQuestionAnswering,
               NystromformerForSequenceClassification,
               NystromformerForTokenClassification,
           )
           if is_torch_available()
           else ()
       )
       snake_case							:Tuple					     =       (
           {
               "feature-extraction": NystromformerModel,
               "fill-mask": NystromformerForMaskedLM,
               "question-answering": NystromformerForQuestionAnswering,
               "text-classification": NystromformerForSequenceClassification,
               "token-classification": NystromformerForTokenClassification,
               "zero-shot": NystromformerForSequenceClassification,
           }
           if is_torch_available()
           else {}
       )
       snake_case							:Optional[int]					     =       False
       snake_case							:int					     =       False






       def       _snake_case    (   self   ):
              __UpperCAmelCase	:					Union[str, Any]												=		NystromformerModelTester(self   )
              __UpperCAmelCase	:					List[Any]												=		ConfigTester(self					,  config_class=UpperCamelCase_					,  hidden_size=37   )






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






       def       _snake_case    (   self   ):
              __UpperCAmelCase	:					Optional[int]												=		self.model_tester.prepare_config_and_inputs()
              self.model_tester.create_and_check_model(*UpperCamelCase_   )






       def       _snake_case    (   self   ):
              __UpperCAmelCase	:					List[Any]												=		self.model_tester.prepare_config_and_inputs()
              for type in ["absolute", "relative_key", "relative_key_query"]:
                     __UpperCAmelCase	:					int												=		type
                     self.model_tester.create_and_check_model(*UpperCamelCase_   )






       def       _snake_case    (   self   ):
              __UpperCAmelCase	:					List[str]												=		self.model_tester.prepare_config_and_inputs()
              self.model_tester.create_and_check_for_masked_lm(*UpperCamelCase_   )






       def       _snake_case    (   self   ):
              __UpperCAmelCase	:					str												=		self.model_tester.prepare_config_and_inputs()
              self.model_tester.create_and_check_for_multiple_choice(*UpperCamelCase_   )






       def       _snake_case    (   self   ):
              __UpperCAmelCase	:					List[str]												=		self.model_tester.prepare_config_and_inputs()
              self.model_tester.create_and_check_for_question_answering(*UpperCamelCase_   )






       def       _snake_case    (   self   ):
              __UpperCAmelCase	:					str												=		self.model_tester.prepare_config_and_inputs()
              self.model_tester.create_and_check_for_sequence_classification(*UpperCamelCase_   )






       def       _snake_case    (   self   ):
              __UpperCAmelCase	:					List[Any]												=		self.model_tester.prepare_config_and_inputs()
              self.model_tester.create_and_check_for_token_classification(*UpperCamelCase_   )







       @slow
       def       _snake_case    (   self   ):
              for model_name in NYSTROMFORMER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
                     __UpperCAmelCase	:					Dict												=		NystromformerModel.from_pretrained(UpperCamelCase_   )
                     self.assertIsNotNone(UpperCamelCase_   )






@require_torch
class 		__A							(unittest.TestCase ):






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

              with torch.no_grad():
                     __UpperCAmelCase	:					List[Any]												=		model(UpperCamelCase_   )[0]

              __UpperCAmelCase	:					Any												=		torch.Size((1, 6, 7_68)   )
              self.assertEqual(output.shape					,  UpperCamelCase_   )

              __UpperCAmelCase	:					List[str]												=		torch.tensor(
                  [[[-0.4_5_3_2, -0.0_9_3_6, 0.5_1_3_7], [-0.2_6_7_6, 0.0_6_2_8, 0.6_1_8_6], [-0.3_6_2_9, -0.1_7_2_6, 0.4_7_1_6]]]   )

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







       @slow
       def       _snake_case    (   self   ):
              __UpperCAmelCase	:					Optional[Any]												=		"""the [MASK] of Belgium is Brussels"""

              __UpperCAmelCase	:					Optional[Any]												=		AutoTokenizer.from_pretrained("uw-madison/nystromformer-512"   )
              __UpperCAmelCase	:					List[Any]												=		NystromformerForMaskedLM.from_pretrained("uw-madison/nystromformer-512"   )

              __UpperCAmelCase	:					str												=		tokenizer(UpperCamelCase_					,  return_tensors="pt"   )

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

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

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










"""simple docstring"""
from __future__ import annotations

from numpy import array, cos, cross, floataa, radians, sin
from numpy.typing import NDArray

def 				_snake_case      (   __snake_case							:      float						, __snake_case							:      float						, __snake_case							:      bool = False   ):




   """simple docstring"""







   if radian_mode:
      return [magnitude * cos(__snake_case   ), magnitude * sin(__snake_case   )]
   return [magnitude * cos(radians(__snake_case   )   ), magnitude * sin(radians(__snake_case   )   )]

def 				_snake_case      (   __snake_case							:      NDArray[floataa]						, __snake_case							:      NDArray[floataa]						, __snake_case							:      float = 10**-1   ):




   """simple docstring"""







   _lowerCamelCase	:					NDArray[floataa]   =		cross(__snake_case						, __snake_case   )
   _lowerCamelCase	:					float   =		sum(__snake_case   )
   return abs(__snake_case   ) < eps


if __name__ == "__main__":
 # Test to check if it works
 UpperCAmelCase				=						array(
     [
         polar_force(718.4, 180 - 30),
         polar_force(879.54, 45),
         polar_force(100, -90),
     ]
 )

 UpperCAmelCase				=						array([[0, 0], [0, 0], [0, 0]])

 assert in_static_equilibrium(forces, location)

 # Problem 1 in image_data/2D_problems.jpg
 UpperCAmelCase				=						array(
     [
         polar_force(30 * 9.81, 15),
         polar_force(215, 180 - 45),
         polar_force(264, 90 - 30),
     ]
 )

 UpperCAmelCase				=						array([[0, 0], [0, 0], [0, 0]])

 assert in_static_equilibrium(forces, location)

 # Problem in image_data/2D_problems_1.jpg
 UpperCAmelCase				=						array([[0, -2000], [0, -1200], [0, 1_5600], [0, -1_2400]])

 UpperCAmelCase				=						array([[0, 0], [6, 0], [10, 0], [12, 0]])

 assert in_static_equilibrium(forces, location)

 import doctest

 doctest.testmod()

