





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:						Any,					lowerCamelCase_:						Tuple,					lowerCamelCase_:						int=13,					lowerCamelCase_:						Union[str, Any]=7,					lowerCamelCase_:						Union[str, Any]=True,					lowerCamelCase_:						Optional[int]=True,					lowerCamelCase_:						Optional[Any]=True,					lowerCamelCase_:						Dict=True,					lowerCamelCase_:						int=99,					lowerCamelCase_:						Optional[int]=32,					lowerCamelCase_:						List[Any]=5,					lowerCamelCase_:						Tuple=4,					lowerCamelCase_:						List[Any]=37,					lowerCamelCase_:						Tuple="gelu",					lowerCamelCase_:						List[str]=0.1,					lowerCamelCase_:						Optional[int]=0.1,					lowerCamelCase_:						Tuple=512,					lowerCamelCase_:						Optional[Any]=16,					lowerCamelCase_:						List[str]=2,					lowerCamelCase_:						List[Any]=0.0_2,					lowerCamelCase_:						List[str]=3,					lowerCamelCase_:						Optional[int]=4,					lowerCamelCase_:						List[Any]=None,					):
			lowercase__					:      str      =		parent
			lowercase__					:      str      =		batch_size
			lowercase__					:      Tuple      =		seq_length
			lowercase__					:      Any      =		is_training
			lowercase__					:      Union[str, Any]      =		use_input_mask
			lowercase__					:      List[str]      =		use_token_type_ids
			lowercase__					:      List[Any]      =		use_labels
			lowercase__					:      Dict      =		vocab_size
			lowercase__					:      int      =		hidden_size
			lowercase__					:      List[str]      =		num_hidden_layers
			lowercase__					:      Optional[Any]      =		num_attention_heads
			lowercase__					:      Optional[int]      =		intermediate_size
			lowercase__					:      Tuple      =		hidden_act
			lowercase__					:      int      =		hidden_dropout_prob
			lowercase__					:      List[Any]      =		attention_probs_dropout_prob
			lowercase__					:      Union[str, Any]      =		max_position_embeddings
			lowercase__					:      str      =		type_vocab_size
			lowercase__					:      str      =		type_sequence_label_size
			lowercase__					:      Dict      =		initializer_range
			lowercase__					:      List[Any]      =		num_labels
			lowercase__					:      Any      =		num_choices
			lowercase__					:      List[Any]      =		scope

	def    snake_case__(						self:						Union[str, Any]			):
			lowercase__					:      Dict      =		ids_tensor([self.batch_size, self.seq_length],					self.vocab_size			)

			lowercase__					:      List[str]      =		None
			if self.use_input_mask:
					lowercase__					:      Optional[int]      =		random_attention_mask([self.batch_size, self.seq_length]			)

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

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

			lowercase__					:      Union[str, Any]      =		self.get_config()

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

	def    snake_case__(						self:						Union[str, Any]			):
			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    snake_case__(						self:						Optional[int],					lowerCamelCase_:						List[str],					lowerCamelCase_:						str,					lowerCamelCase_:						int,					lowerCamelCase_:						str,					lowerCamelCase_:						Union[str, Any],					lowerCamelCase_:						Tuple,					lowerCamelCase_:						int			):
			lowercase__					:      Union[str, Any]      =		NystromformerModel(config=lowerCamelCase_			)
			model.to(lowerCamelCase_			)
			model.eval()
			lowercase__					:      Any      =		model(lowerCamelCase_,					attention_mask=lowerCamelCase_,					token_type_ids=lowerCamelCase_			)
			lowercase__					:      Any      =		model(lowerCamelCase_,					token_type_ids=lowerCamelCase_			)
			lowercase__					:      Union[str, Any]      =		model(lowerCamelCase_			)
			self.parent.assertEqual(result.last_hidden_state.shape,					(self.batch_size, self.seq_length, self.hidden_size)			)

	def    snake_case__(						self:						str,					lowerCamelCase_:						Optional[Any],					lowerCamelCase_:						Optional[int],					lowerCamelCase_:						Dict,					lowerCamelCase_:						Any,					lowerCamelCase_:						Optional[Any],					lowerCamelCase_:						Optional[int],					lowerCamelCase_:						Optional[Any]			):
			lowercase__					:      Optional[Any]      =		NystromformerForMaskedLM(config=lowerCamelCase_			)
			model.to(lowerCamelCase_			)
			model.eval()
			lowercase__					:      List[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.vocab_size)			)

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

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





	def    snake_case__(						self:						List[Any]			):
			lowercase__					:      List[Any]      =		self.prepare_config_and_inputs()
			(
			    (
			    lowercase__
			)   ,				(
			    lowercase__
			)   ,				(
			    lowercase__
			)   ,				(
			    lowercase__
			)   ,				(
			    lowercase__
			)   ,				(
			    lowercase__
			)   ,				(
			    lowercase__
			)   ,				
			)					:      Optional[int]      =		config_and_inputs
			lowercase__					:      Any      =		{'input_ids': input_ids, 'token_type_ids': token_type_ids, 'attention_mask': input_mask}
			return config, inputs_dict


@require_torch
class       __lowerCAmelCase					(       _UpperCamelCase   ,    _UpperCamelCase   ,    unittest.TestCase      ):


	'''simple docstring'''





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

	def    snake_case__(						self:						Any			):
			lowercase__					:      Optional[Any]      =		NystromformerModelTester(self			)
			lowercase__					:      Optional[Any]      =		ConfigTester(self,					config_class=lowerCamelCase_,					hidden_size=37			)

	def    snake_case__(						self:						Any			):
			self.config_tester.run_common_tests()

	def    snake_case__(						self:						List[str]			):
			lowercase__					:      List[Any]      =		self.model_tester.prepare_config_and_inputs()
			self.model_tester.create_and_check_model(*lowerCamelCase_			)

	def    snake_case__(						self:						Any			):
			lowercase__					:      Optional[Any]      =		self.model_tester.prepare_config_and_inputs()
			for type in ["absolute", "relative_key", "relative_key_query"]:
					lowercase__					:      Union[str, Any]      =		type
					self.model_tester.create_and_check_model(*lowerCamelCase_			)

	def    snake_case__(						self:						List[str]			):
			lowercase__					:      Any      =		self.model_tester.prepare_config_and_inputs()
			self.model_tester.create_and_check_for_masked_lm(*lowerCamelCase_			)

	def    snake_case__(						self:						Dict			):
			lowercase__					:      Union[str, Any]      =		self.model_tester.prepare_config_and_inputs()
			self.model_tester.create_and_check_for_multiple_choice(*lowerCamelCase_			)

	def    snake_case__(						self:						int			):
			lowercase__					:      List[Any]      =		self.model_tester.prepare_config_and_inputs()
			self.model_tester.create_and_check_for_question_answering(*lowerCamelCase_			)

	def    snake_case__(						self:						str			):
			lowercase__					:      Tuple      =		self.model_tester.prepare_config_and_inputs()
			self.model_tester.create_and_check_for_sequence_classification(*lowerCamelCase_			)

	def    snake_case__(						self:						Any			):
			lowercase__					:      Optional[int]      =		self.model_tester.prepare_config_and_inputs()
			self.model_tester.create_and_check_for_token_classification(*lowerCamelCase_			)





	@slow
	def    snake_case__(						self:						Optional[Any]			):
			for model_name in NYSTROMFORMER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
					lowercase__					:      Dict      =		NystromformerModel.from_pretrained(lowerCamelCase_			)
					self.assertIsNotNone(lowerCamelCase_			)


@require_torch
class       __lowerCAmelCase					(       unittest.TestCase      ):


	'''simple docstring'''





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

			with torch.no_grad():
					lowercase__					:      Optional[int]      =		model(lowerCamelCase_			)[0]

			lowercase__					:      Dict      =		torch.Size((1, 6, 768)			)
			self.assertEqual(output.shape,					lowerCamelCase_			)

			lowercase__					:      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],					lowerCamelCase_,					atol=1E-4			)			)





	@slow
	def    snake_case__(						self:						str			):
			lowercase__					:      int      =		'the [MASK] of Belgium is Brussels'

			lowercase__					:      str      =		AutoTokenizer.from_pretrained('uw-madison/nystromformer-512'			)
			lowercase__					:      List[Any]      =		NystromformerForMaskedLM.from_pretrained('uw-madison/nystromformer-512'			)

			lowercase__					:      Union[str, Any]      =		tokenizer(lowerCamelCase_,					return_tensors='pt'			)

			with torch.no_grad():
					lowercase__					:      Optional[int]      =		model(encoding.input_ids			).logits

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

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









def       SCREAMING_SNAKE_CASE__  (       _lowercase     : int   ,    _lowercase     : int			)   ->							Optional[int]:




		'''simple docstring'''
		if b == 0:
				return 1
		if (b % 2) == 0:
				return actual_power(_lowercase   ,    int(b / 2			)			) * actual_power(_lowercase   ,    int(b / 2			)			)
		else:
				return a * actual_power(_lowercase   ,    int(b / 2			)			) * actual_power(_lowercase   ,    int(b / 2			)			)


def       SCREAMING_SNAKE_CASE__  (       _lowercase     : int   ,    _lowercase     : int			)   ->							float:




		'''simple docstring'''
		if b < 0:
				return 1 / actual_power(_lowercase   ,    _lowercase			)
		return actual_power(_lowercase   ,    _lowercase			)


if __name__ == "__main__":
			print(power(-2, -3))


