




"""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   UpperCAmelCase_    :
		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				,						)						->       List[Any]:
					__lowercase			:  int								=       parent
					__lowercase			:  Optional[Any]								=       batch_size
					__lowercase			:  Union[str, Any]								=       seq_length
					__lowercase			:  Optional[int]								=       is_training
					__lowercase			:  Union[str, Any]								=       use_input_mask
					__lowercase			:  Any								=       use_token_type_ids
					__lowercase			:  Any								=       use_labels
					__lowercase			:  Optional[Any]								=       vocab_size
					__lowercase			:  Optional[Any]								=       hidden_size
					__lowercase			:  str								=       num_hidden_layers
					__lowercase			:  Any								=       num_attention_heads
					__lowercase			:  Dict								=       intermediate_size
					__lowercase			:  Dict								=       hidden_act
					__lowercase			:  List[str]								=       hidden_dropout_prob
					__lowercase			:  List[str]								=       attention_probs_dropout_prob
					__lowercase			:  Optional[int]								=       max_position_embeddings
					__lowercase			:  Tuple								=       type_vocab_size
					__lowercase			:  Tuple								=       type_sequence_label_size
					__lowercase			:  str								=       initializer_range
					__lowercase			:  List[str]								=       num_labels
					__lowercase			:  Dict								=       num_choices
					__lowercase			:  Dict								=       scope
		def 	_lowerCamelCase							(						self       )						->       Optional[Any]:
					__lowercase			:  Tuple								=       ids_tensor([self.batch_size, self.seq_length]				,						self.vocab_size       )

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

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

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

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

					return config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels
		def 	_lowerCamelCase							(						self       )						->       Tuple:
					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 	_lowerCamelCase							(						self				,						UpperCamelCase_				,						UpperCamelCase_				,						UpperCamelCase_				,						UpperCamelCase_				,						UpperCamelCase_				,						UpperCamelCase_				,						UpperCamelCase_       )						->       int:
					__lowercase			:  Dict								=       NystromformerModel(config=UpperCamelCase_       )
					model.to(UpperCamelCase_       )
					model.eval()
					__lowercase			:  Tuple								=       model(UpperCamelCase_				,						attention_mask=UpperCamelCase_				,						token_type_ids=UpperCamelCase_       )
					__lowercase			:  Dict								=       model(UpperCamelCase_				,						token_type_ids=UpperCamelCase_       )
					__lowercase			:  List[str]								=       model(UpperCamelCase_       )
					self.parent.assertEqual(result.last_hidden_state.shape				,						(self.batch_size, self.seq_length, self.hidden_size)       )
		def 	_lowerCamelCase							(						self				,						UpperCamelCase_				,						UpperCamelCase_				,						UpperCamelCase_				,						UpperCamelCase_				,						UpperCamelCase_				,						UpperCamelCase_				,						UpperCamelCase_       )						->       str:
					__lowercase			:  Tuple								=       NystromformerForMaskedLM(config=UpperCamelCase_       )
					model.to(UpperCamelCase_       )
					model.eval()
					__lowercase			:  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 	_lowerCamelCase							(						self				,						UpperCamelCase_				,						UpperCamelCase_				,						UpperCamelCase_				,						UpperCamelCase_				,						UpperCamelCase_				,						UpperCamelCase_				,						UpperCamelCase_       )						->       Dict:
					__lowercase			:  Union[str, Any]								=       NystromformerForQuestionAnswering(config=UpperCamelCase_       )
					model.to(UpperCamelCase_       )
					model.eval()
					__lowercase			:  Any								=       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 	_lowerCamelCase							(						self				,						UpperCamelCase_				,						UpperCamelCase_				,						UpperCamelCase_				,						UpperCamelCase_				,						UpperCamelCase_				,						UpperCamelCase_				,						UpperCamelCase_       )						->       Optional[int]:
					__lowercase			:  List[str]								=       self.num_labels
					__lowercase			:  Any								=       NystromformerForSequenceClassification(UpperCamelCase_       )
					model.to(UpperCamelCase_       )
					model.eval()
					__lowercase			:  List[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 	_lowerCamelCase							(						self				,						UpperCamelCase_				,						UpperCamelCase_				,						UpperCamelCase_				,						UpperCamelCase_				,						UpperCamelCase_				,						UpperCamelCase_				,						UpperCamelCase_       )						->       Optional[int]:
					__lowercase			:  Optional[int]								=       self.num_labels
					__lowercase			:  Optional[int]								=       NystromformerForTokenClassification(config=UpperCamelCase_       )
					model.to(UpperCamelCase_       )
					model.eval()
					__lowercase			:  List[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 	_lowerCamelCase							(						self				,						UpperCamelCase_				,						UpperCamelCase_				,						UpperCamelCase_				,						UpperCamelCase_				,						UpperCamelCase_				,						UpperCamelCase_				,						UpperCamelCase_       )						->       Optional[int]:
					__lowercase			:  Tuple								=       self.num_choices
					__lowercase			:  Dict								=       NystromformerForMultipleChoice(config=UpperCamelCase_       )
					model.to(UpperCamelCase_       )
					model.eval()
					__lowercase			:  Optional[Any]								=       input_ids.unsqueeze(1       ).expand(-1				,						self.num_choices				,						-1       ).contiguous()
					__lowercase			:  Optional[int]								=       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			:  Dict								=       model(
					    UpperCamelCase_				,						attention_mask=UpperCamelCase_				,						token_type_ids=UpperCamelCase_				,						labels=UpperCamelCase_				,						)
					self.parent.assertEqual(result.logits.shape				,						(self.batch_size, self.num_choices)       )
		def 	_lowerCamelCase							(						self       )						->       Union[str, Any]:
					__lowercase			:  Optional[int]								=       self.prepare_config_and_inputs()
					(
					    (
					    __lowercase
					)			,(
					    __lowercase
					)			,(
					    __lowercase
					)			,(
					    __lowercase
					)			,(
					    __lowercase
					)			,(
					    __lowercase
					)			,(
					    __lowercase
					)			,
					)			:  Union[str, Any]								=       config_and_inputs
					__lowercase			:  Optional[int]								=       {'''input_ids''': input_ids, '''token_type_ids''': token_type_ids, '''attention_mask''': input_mask}
					return config, inputs_dict


@require_torch
class   UpperCAmelCase_    (			snake_case   ,					snake_case   ,					unittest.TestCase		):
		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 	_lowerCamelCase							(						self       )						->       int:
					__lowercase			:  Dict								=       NystromformerModelTester(self       )
					__lowercase			:  Union[str, Any]								=       ConfigTester(self				,						config_class=UpperCamelCase_				,						hidden_size=37       )
		def 	_lowerCamelCase							(						self       )						->       Dict:
					self.config_tester.run_common_tests()
		def 	_lowerCamelCase							(						self       )						->       Any:
					__lowercase			:  str								=       self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_model(*UpperCamelCase_       )
		def 	_lowerCamelCase							(						self       )						->       Any:
					__lowercase			:  List[str]								=       self.model_tester.prepare_config_and_inputs()
					for type in ["absolute", "relative_key", "relative_key_query"]:
								__lowercase			:  Dict								=       type
								self.model_tester.create_and_check_model(*UpperCamelCase_       )
		def 	_lowerCamelCase							(						self       )						->       Tuple:
					__lowercase			:  int								=       self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_for_masked_lm(*UpperCamelCase_       )
		def 	_lowerCamelCase							(						self       )						->       Tuple:
					__lowercase			:  List[str]								=       self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_for_multiple_choice(*UpperCamelCase_       )
		def 	_lowerCamelCase							(						self       )						->       str:
					__lowercase			:  Any								=       self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_for_question_answering(*UpperCamelCase_       )
		def 	_lowerCamelCase							(						self       )						->       List[str]:
					__lowercase			:  str								=       self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_for_sequence_classification(*UpperCamelCase_       )
		def 	_lowerCamelCase							(						self       )						->       Tuple:
					__lowercase			:  int								=       self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_for_token_classification(*UpperCamelCase_       )
		@slow
		def 	_lowerCamelCase							(						self       )						->       Union[str, Any]:
					for model_name in NYSTROMFORMER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
								__lowercase			:  Dict								=       NystromformerModel.from_pretrained(UpperCamelCase_       )
								self.assertIsNotNone(UpperCamelCase_       )


@require_torch
class   UpperCAmelCase_    (			unittest.TestCase		):
		@slow
		def 	_lowerCamelCase							(						self       )						->       Tuple:
					__lowercase			:  Dict								=       NystromformerModel.from_pretrained('''uw-madison/nystromformer-512'''       )
					__lowercase			:  str								=       torch.tensor([[0, 1, 2, 3, 4, 5]]       )

					with torch.no_grad():
								__lowercase			:  Union[str, Any]								=       model(UpperCamelCase_       )[0]

					__lowercase			:  List[Any]								=       torch.Size((1, 6, 7_68)       )
					self.assertEqual(output.shape				,						UpperCamelCase_       )

					__lowercase			:  int								=       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 	_lowerCamelCase							(						self       )						->       Tuple:
					__lowercase			:  Tuple								=       '''the [MASK] of Belgium is Brussels'''

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

					__lowercase			:  int								=       tokenizer(UpperCamelCase_				,						return_tensors='''pt'''       )

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

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

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








'''simple docstring'''
def A__       (	UpperCAmelCase_    ,    UpperCAmelCase_				):
						return base * power(UpperCAmelCase_    ,    (exponent - 1)				) if exponent else 1


if __name__ == "__main__":
					print('Raise base to the power of exponent using recursion...')
					snake_case_					:      int    			=    int(input('Enter the base: ').strip())
					snake_case_					:      Optional[int]    			=    int(input('Enter the exponent: ').strip())
					snake_case_					:      Optional[int]    			=    power(base, abs(exponent))
					if exponent < 0:  # power() does not properly deal w/ negative exponents
										snake_case_					:      List[Any]    			=    1 / result
					print(F"""{base} to the power of {exponent} is {result}""")


