



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







						"""simple docstring"""




						def __init__(       self ,       __lowercase ,       __lowercase=1_3 ,       __lowercase=7 ,       __lowercase=True ,       __lowercase=True ,       __lowercase=True ,       __lowercase=True ,       __lowercase=9_9 ,       __lowercase=3_2 ,       __lowercase=5 ,       __lowercase=4 ,       __lowercase=3_7 ,       __lowercase="gelu" ,       __lowercase=0.1 ,       __lowercase=0.1 ,       __lowercase=5_1_2 ,       __lowercase=1_6 ,       __lowercase=2 ,       __lowercase=0.0_2 ,       __lowercase=3 ,       __lowercase=4 ,       __lowercase=None ,       ) ->				Tuple:
									"""simple docstring"""




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

						def 							SCREAMING_SNAKE_CASE__(       self  ) ->				Optional[int]:
									"""simple docstring"""




									a__       :						Any							=  ids_tensor([self.batch_size, self.seq_length] ,       self.vocab_size  )

									a__       :						List[str]							=  None
									if self.use_input_mask:
												a__       :						Any							=  random_attention_mask([self.batch_size, self.seq_length]  )

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

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

									a__       :						Any							=  self.get_config()

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

						def 							SCREAMING_SNAKE_CASE__(       self  ) ->				Tuple:
									"""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 							SCREAMING_SNAKE_CASE__(       self ,       __lowercase ,       __lowercase ,       __lowercase ,       __lowercase ,       __lowercase ,       __lowercase ,       __lowercase  ) ->				Dict:
									"""simple docstring"""




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

						def 							SCREAMING_SNAKE_CASE__(       self ,       __lowercase ,       __lowercase ,       __lowercase ,       __lowercase ,       __lowercase ,       __lowercase ,       __lowercase  ) ->				Dict:
									"""simple docstring"""




									a__       :						Optional[int]							=  NystromformerForMaskedLM(config=__lowercase  )
									model.to(__lowercase  )
									model.eval()
									a__       :						List[Any]							=  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 							SCREAMING_SNAKE_CASE__(       self ,       __lowercase ,       __lowercase ,       __lowercase ,       __lowercase ,       __lowercase ,       __lowercase ,       __lowercase  ) ->				Optional[Any]:
									"""simple docstring"""




									a__       :						Optional[Any]							=  NystromformerForQuestionAnswering(config=__lowercase  )
									model.to(__lowercase  )
									model.eval()
									a__       :						List[Any]							=  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 							SCREAMING_SNAKE_CASE__(       self ,       __lowercase ,       __lowercase ,       __lowercase ,       __lowercase ,       __lowercase ,       __lowercase ,       __lowercase  ) ->				str:
									"""simple docstring"""




									a__       :						Any							=  self.num_labels
									a__       :						List[Any]							=  NystromformerForSequenceClassification(__lowercase  )
									model.to(__lowercase  )
									model.eval()
									a__       :						Tuple							=  model(__lowercase ,       attention_mask=__lowercase ,       token_type_ids=__lowercase ,       labels=__lowercase  )
									self.parent.assertEqual(result.logits.shape ,       (self.batch_size, self.num_labels)  )

						def 							SCREAMING_SNAKE_CASE__(       self ,       __lowercase ,       __lowercase ,       __lowercase ,       __lowercase ,       __lowercase ,       __lowercase ,       __lowercase  ) ->				Tuple:
									"""simple docstring"""




									a__       :						Any							=  self.num_labels
									a__       :						int							=  NystromformerForTokenClassification(config=__lowercase  )
									model.to(__lowercase  )
									model.eval()
									a__       :						Tuple							=  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 							SCREAMING_SNAKE_CASE__(       self ,       __lowercase ,       __lowercase ,       __lowercase ,       __lowercase ,       __lowercase ,       __lowercase ,       __lowercase  ) ->				Tuple:
									"""simple docstring"""




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

						def 							SCREAMING_SNAKE_CASE__(       self  ) ->				Any:
									"""simple docstring"""




									a__       :						List[Any]							=  self.prepare_config_and_inputs()
									(
									    (
									    a__
									)	,						(
									    a__
									)	,						(
									    a__
									)	,						(
									    a__
									)	,						(
									    a__
									)	,						(
									    a__
									)	,						(
									    a__
									)	,						
									)       :						str							=  config_and_inputs
									a__       :						Any							=  {"""input_ids""": input_ids, """token_type_ids""": token_type_ids, """attention_mask""": input_mask}
									return config, inputs_dict




@require_torch
class        snake_case__    (A__ ,     A__ ,     unittest.TestCase		):







						"""simple docstring"""




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

						def 							SCREAMING_SNAKE_CASE__(       self  ) ->				List[str]:
									"""simple docstring"""




									a__       :						Union[str, Any]							=  NystromformerModelTester(self  )
									a__       :						Optional[int]							=  ConfigTester(self ,       config_class=__lowercase ,       hidden_size=3_7  )

						def 							SCREAMING_SNAKE_CASE__(       self  ) ->				Union[str, Any]:
									"""simple docstring"""




									self.config_tester.run_common_tests()

						def 							SCREAMING_SNAKE_CASE__(       self  ) ->				Dict:
									"""simple docstring"""




									a__       :						Optional[Any]							=  self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_model(*__lowercase  )

						def 							SCREAMING_SNAKE_CASE__(       self  ) ->				Optional[int]:
									"""simple docstring"""




									a__       :						int							=  self.model_tester.prepare_config_and_inputs()
									for type in ["absolute", "relative_key", "relative_key_query"]:
												a__       :						Optional[int]							=  type
												self.model_tester.create_and_check_model(*__lowercase  )

						def 							SCREAMING_SNAKE_CASE__(       self  ) ->				List[str]:
									"""simple docstring"""




									a__       :						Any							=  self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_for_masked_lm(*__lowercase  )

						def 							SCREAMING_SNAKE_CASE__(       self  ) ->				Union[str, Any]:
									"""simple docstring"""




									a__       :						Union[str, Any]							=  self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_for_multiple_choice(*__lowercase  )

						def 							SCREAMING_SNAKE_CASE__(       self  ) ->				Tuple:
									"""simple docstring"""




									a__       :						str							=  self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_for_question_answering(*__lowercase  )

						def 							SCREAMING_SNAKE_CASE__(       self  ) ->				Optional[int]:
									"""simple docstring"""




									a__       :						Tuple							=  self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_for_sequence_classification(*__lowercase  )

						def 							SCREAMING_SNAKE_CASE__(       self  ) ->				Tuple:
									"""simple docstring"""




									a__       :						Union[str, Any]							=  self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_for_token_classification(*__lowercase  )

						@slow
						def 							SCREAMING_SNAKE_CASE__(       self  ) ->				Any:
									"""simple docstring"""




									for model_name in NYSTROMFORMER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
												a__       :						Union[str, Any]							=  NystromformerModel.from_pretrained(__lowercase  )
												self.assertIsNotNone(__lowercase  )




@require_torch
class        snake_case__    (unittest.TestCase		):







						"""simple docstring"""




						@slow
						def 							SCREAMING_SNAKE_CASE__(       self  ) ->				Any:
									"""simple docstring"""




									a__       :						Optional[Any]							=  NystromformerModel.from_pretrained("""uw-madison/nystromformer-512"""  )
									a__       :						List[str]							=  torch.tensor([[0, 1, 2, 3, 4, 5]]  )

									with torch.no_grad():
												a__       :						List[str]							=  model(__lowercase  )[0]

									a__       :						Optional[int]							=  torch.Size((1, 6, 7_6_8)  )
									self.assertEqual(output.shape ,       __lowercase  )

									a__       :						Optional[Any]							=  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] ,       __lowercase ,       atol=1E-4  )  )

						@slow
						def 							SCREAMING_SNAKE_CASE__(       self  ) ->				List[Any]:
									"""simple docstring"""




									a__       :						Dict							=  """the [MASK] of Belgium is Brussels"""

									a__       :						Union[str, Any]							=  AutoTokenizer.from_pretrained("""uw-madison/nystromformer-512"""  )
									a__       :						List[Any]							=  NystromformerForMaskedLM.from_pretrained("""uw-madison/nystromformer-512"""  )

									a__       :						Optional[Any]							=  tokenizer(__lowercase ,       return_tensors="""pt"""  )

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

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

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









from __future__ import annotations

def      lowerCAmelCase_					(  _lowercase    :       float						,       _lowercase    :       float						,       _lowercase    :       float						,       )							->	tuple[str, float]:







			"""simple docstring"""
			if (stress, tangential_force, area).count(0) != 1:
						raise ValueError("""You cannot supply more or less than 2 values""")
			elif stress < 0:
						raise ValueError("""Stress cannot be negative""")
			elif tangential_force < 0:
						raise ValueError("""Tangential Force cannot be negative""")
			elif area < 0:
						raise ValueError("""Area cannot be negative""")
			elif stress == 0:
						return (
						    "stress",
						    tangential_force / area,
						)
			elif tangential_force == 0:
						return (
						    "tangential_force",
						    stress * area,
						)
			else:
						return (
						    "area",
						    tangential_force / stress,
						)


if __name__ == "__main__":
		import doctest

		doctest.testmod()




