
"""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        _SCREAMING_SNAKE_CASE  :






				'''simple docstring'''



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







											"""simple docstring"""




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


				def 				__lowerCamelCase	(					self	:			Dict  )		->  int:







											"""simple docstring"""




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

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

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

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

											_lowerCAmelCase  	=			self.get_config()

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


				def 				__lowerCamelCase	(					self	:			List[str]  )		->  Any:







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


				def 				__lowerCamelCase	(					self	:			Tuple				,     UpperCAmelCase_	:			List[Any]				,     UpperCAmelCase_	:			Any				,     UpperCAmelCase_	:			Dict				,     UpperCAmelCase_	:			Dict				,     UpperCAmelCase_	:			Any				,     UpperCAmelCase_	:			Dict				,     UpperCAmelCase_	:			Tuple  )		->  Dict:







											"""simple docstring"""




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


				def 				__lowerCamelCase	(					self	:			Any				,     UpperCAmelCase_	:			int				,     UpperCAmelCase_	:			str				,     UpperCAmelCase_	:			Any				,     UpperCAmelCase_	:			List[Any]				,     UpperCAmelCase_	:			Union[str, Any]				,     UpperCAmelCase_	:			Union[str, Any]				,     UpperCAmelCase_	:			str  )		->  Union[str, Any]:







											"""simple docstring"""




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


				def 				__lowerCamelCase	(					self	:			Optional[Any]				,     UpperCAmelCase_	:			List[Any]				,     UpperCAmelCase_	:			Any				,     UpperCAmelCase_	:			int				,     UpperCAmelCase_	:			Union[str, Any]				,     UpperCAmelCase_	:			List[str]				,     UpperCAmelCase_	:			Any				,     UpperCAmelCase_	:			Tuple  )		->  str:







											"""simple docstring"""




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







											"""simple docstring"""




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


				def 				__lowerCamelCase	(					self	:			Optional[Any]				,     UpperCAmelCase_	:			str				,     UpperCAmelCase_	:			Dict				,     UpperCAmelCase_	:			Dict				,     UpperCAmelCase_	:			List[Any]				,     UpperCAmelCase_	:			List[Any]				,     UpperCAmelCase_	:			str				,     UpperCAmelCase_	:			Union[str, Any]  )		->  Any:







											"""simple docstring"""




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


				def 				__lowerCamelCase	(					self	:			Optional[Any]				,     UpperCAmelCase_	:			Tuple				,     UpperCAmelCase_	:			str				,     UpperCAmelCase_	:			Optional[int]				,     UpperCAmelCase_	:			Any				,     UpperCAmelCase_	:			Optional[Any]				,     UpperCAmelCase_	:			int				,     UpperCAmelCase_	:			Dict  )		->  Union[str, Any]:







											"""simple docstring"""




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







				def 				__lowerCamelCase	(					self	:			str  )		->  List[Any]:







											"""simple docstring"""




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






@require_torch
class        _SCREAMING_SNAKE_CASE  (  __a      ,				__a      ,				unittest.TestCase							):






				'''simple docstring'''



				SCREAMING_SNAKE_CASE_:							Union[str, Any]							=					(
				    (
				        NystromformerModel,
				        NystromformerForMaskedLM,
				        NystromformerForMultipleChoice,
				        NystromformerForQuestionAnswering,
				        NystromformerForSequenceClassification,
				        NystromformerForTokenClassification,
				    )
				    if is_torch_available()
				    else ()
				)
				SCREAMING_SNAKE_CASE_:							List[Any]							=					(
				    {
				        "feature-extraction": NystromformerModel,
				        "fill-mask": NystromformerForMaskedLM,
				        "question-answering": NystromformerForQuestionAnswering,
				        "text-classification": NystromformerForSequenceClassification,
				        "token-classification": NystromformerForTokenClassification,
				        "zero-shot": NystromformerForSequenceClassification,
				    }
				    if is_torch_available()
				    else {}
				)
				SCREAMING_SNAKE_CASE_:							List[str]							=					False
				SCREAMING_SNAKE_CASE_:							str							=					False


				def 				__lowerCamelCase	(					self	:			str  )		->  Any:







											"""simple docstring"""




											_lowerCAmelCase  	=			NystromformerModelTester(self  )
											_lowerCAmelCase  	=			ConfigTester(self				,     config_class=a_				,     hidden_size=37  )


				def 				__lowerCamelCase	(					self	:			List[str]  )		->  Dict:







											"""simple docstring"""




											self.config_tester.run_common_tests()


				def 				__lowerCamelCase	(					self	:			int  )		->  Tuple:







											"""simple docstring"""




											_lowerCAmelCase  	=			self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_model(*a_  )


				def 				__lowerCamelCase	(					self	:			Dict  )		->  Union[str, Any]:







											"""simple docstring"""




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


				def 				__lowerCamelCase	(					self	:			Optional[Any]  )		->  Dict:







											"""simple docstring"""




											_lowerCAmelCase  	=			self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_for_masked_lm(*a_  )


				def 				__lowerCamelCase	(					self	:			Dict  )		->  int:







											"""simple docstring"""




											_lowerCAmelCase  	=			self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_for_multiple_choice(*a_  )


				def 				__lowerCamelCase	(					self	:			Optional[Any]  )		->  List[str]:







											"""simple docstring"""




											_lowerCAmelCase  	=			self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_for_question_answering(*a_  )


				def 				__lowerCamelCase	(					self	:			Dict  )		->  Tuple:







											"""simple docstring"""




											_lowerCAmelCase  	=			self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_for_sequence_classification(*a_  )


				def 				__lowerCamelCase	(					self	:			str  )		->  Any:







											"""simple docstring"""




											_lowerCAmelCase  	=			self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_for_token_classification(*a_  )







				@slow
				def 				__lowerCamelCase	(					self	:			int  )		->  Optional[int]:







											"""simple docstring"""




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






@require_torch
class        _SCREAMING_SNAKE_CASE  (  unittest.TestCase							):






				'''simple docstring'''



				@slow
				def 				__lowerCamelCase	(					self	:			List[Any]  )		->  List[Any]:







											"""simple docstring"""




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

											with torch.no_grad():
																		_lowerCAmelCase  	=			model(a_  )[0]

											_lowerCAmelCase  	=			torch.Size((1, 6, 768)  )
											self.assertEqual(output.shape				,     a_  )

											_lowerCAmelCase  	=			torch.tensor(
											    [[[-0.4532, -0.0936, 0.5137], [-0.2676, 0.0628, 0.6186], [-0.3629, -0.1726, 0.4716]]]  )

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







				@slow
				def 				__lowerCamelCase	(					self	:			Any  )		->  List[str]:







											"""simple docstring"""




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

											_lowerCAmelCase  	=			AutoTokenizer.from_pretrained('uw-madison/nystromformer-512'  )
											_lowerCAmelCase  	=			NystromformerForMaskedLM.from_pretrained('uw-madison/nystromformer-512'  )

											_lowerCAmelCase  	=			tokenizer(a_				,     return_tensors='pt'  )

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

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

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








"""simple docstring"""



def 	__snake_case  (    SCREAMING_SNAKE_CASE:					str						):






							"""simple docstring"""



							_lowerCAmelCase  	=			[0 for i in range(len(SCREAMING_SNAKE_CASE						)						)]

							# initialize interval's left pointer and right pointer
							_lowerCAmelCase	,		_lowerCAmelCase  	=			0, 0

							for i in range(1     ,  len(SCREAMING_SNAKE_CASE						)						):
														# case when current index is inside the interval
														if i <= right_pointer:
																					_lowerCAmelCase  	=			min(right_pointer - i + 1     ,  z_result[i - left_pointer]						)
																					_lowerCAmelCase  	=			min_edge

														while go_next(SCREAMING_SNAKE_CASE     ,  SCREAMING_SNAKE_CASE     ,  SCREAMING_SNAKE_CASE						):
																					z_result[i] += 1

														# if new index's result gives us more right interval,
														# we've to update left_pointer and right_pointer
														if i + z_result[i] - 1 > right_pointer:
																					_lowerCAmelCase	,		_lowerCAmelCase  	=			i, i + z_result[i] - 1

							return z_result






def 	__snake_case  (    SCREAMING_SNAKE_CASE:					int     ,  SCREAMING_SNAKE_CASE:					list[int]     ,  SCREAMING_SNAKE_CASE:					str						):






							"""simple docstring"""



							return i + z_result[i] < len(SCREAMING_SNAKE_CASE						) and s[z_result[i]] == s[i + z_result[i]]






def 	__snake_case  (    SCREAMING_SNAKE_CASE:					str     ,  SCREAMING_SNAKE_CASE:					str						):






							"""simple docstring"""



							_lowerCAmelCase  	=			0
							# concatenate 'pattern' and 'input_str' and call z_function
							# with concatenated string
							_lowerCAmelCase  	=			z_function(pattern + input_str						)

							for val in z_result:
														# if value is greater then length of the pattern string
														# that means this index is starting position of substring
														# which is equal to pattern string
														if val >= len(SCREAMING_SNAKE_CASE						):
																					answer += 1

							return answer


if __name__ == "__main__":
	import doctest

	doctest.testmod()






