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






					'''simple docstring'''


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



					def 			lowerCAmelCase_					(						self			:				Tuple		)							->       int:
									SCREAMING_SNAKE_CASE__		  =				ids_tensor([self.batch_size, self.seq_length]   , self.vocab_size		)

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

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

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

									SCREAMING_SNAKE_CASE__		  =				self.get_config()

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



					def 			lowerCAmelCase_					(						self			:				Any		)							->       List[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=UpperCAmelCase__   , initializer_range=self.initializer_range   , )



					def 			lowerCAmelCase_					(						self			:				Optional[Any]   , _snake_case			:				Optional[int]   , _snake_case			:				int   , _snake_case			:				Union[str, Any]   , _snake_case			:				Union[str, Any]   , _snake_case			:				Optional[Any]   , _snake_case			:				Optional[int]   , _snake_case			:				Any		)							->       Optional[Any]:
									SCREAMING_SNAKE_CASE__		  =				NystromformerModel(config=UpperCAmelCase__		)
									model.to(UpperCAmelCase__		)
									model.eval()
									SCREAMING_SNAKE_CASE__		  =				model(UpperCAmelCase__   , attention_mask=UpperCAmelCase__   , token_type_ids=UpperCAmelCase__		)
									SCREAMING_SNAKE_CASE__		  =				model(UpperCAmelCase__   , token_type_ids=UpperCAmelCase__		)
									SCREAMING_SNAKE_CASE__		  =				model(UpperCAmelCase__		)
									self.parent.assertEqual(result.last_hidden_state.shape   , (self.batch_size, self.seq_length, self.hidden_size)		)



					def 			lowerCAmelCase_					(						self			:				str   , _snake_case			:				Tuple   , _snake_case			:				List[Any]   , _snake_case			:				Union[str, Any]   , _snake_case			:				List[Any]   , _snake_case			:				Optional[Any]   , _snake_case			:				List[str]   , _snake_case			:				Any		)							->       str:
									SCREAMING_SNAKE_CASE__		  =				NystromformerForMaskedLM(config=UpperCAmelCase__		)
									model.to(UpperCAmelCase__		)
									model.eval()
									SCREAMING_SNAKE_CASE__		  =				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			:				List[Any]   , _snake_case			:				Union[str, Any]   , _snake_case			:				int   , _snake_case			:				Dict   , _snake_case			:				str   , _snake_case			:				str   , _snake_case			:				Dict   , _snake_case			:				List[Any]		)							->       Union[str, Any]:
									SCREAMING_SNAKE_CASE__		  =				NystromformerForQuestionAnswering(config=UpperCAmelCase__		)
									model.to(UpperCAmelCase__		)
									model.eval()
									SCREAMING_SNAKE_CASE__		  =				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			:				str   , _snake_case			:				Any   , _snake_case			:				List[Any]   , _snake_case			:				Optional[int]   , _snake_case			:				int   , _snake_case			:				List[Any]   , _snake_case			:				str   , _snake_case			:				List[Any]		)							->       int:
									SCREAMING_SNAKE_CASE__		  =				self.num_labels
									SCREAMING_SNAKE_CASE__		  =				NystromformerForSequenceClassification(UpperCAmelCase__		)
									model.to(UpperCAmelCase__		)
									model.eval()
									SCREAMING_SNAKE_CASE__		  =				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			:				List[Any]   , _snake_case			:				int   , _snake_case			:				List[str]   , _snake_case			:				Dict   , _snake_case			:				Tuple   , _snake_case			:				Union[str, Any]   , _snake_case			:				Tuple   , _snake_case			:				Optional[Any]		)							->       List[Any]:
									SCREAMING_SNAKE_CASE__		  =				self.num_labels
									SCREAMING_SNAKE_CASE__		  =				NystromformerForTokenClassification(config=UpperCAmelCase__		)
									model.to(UpperCAmelCase__		)
									model.eval()
									SCREAMING_SNAKE_CASE__		  =				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			:				Tuple   , _snake_case			:				Union[str, Any]   , _snake_case			:				Any   , _snake_case			:				str   , _snake_case			:				Dict   , _snake_case			:				Union[str, Any]   , _snake_case			:				Dict   , _snake_case			:				List[Any]		)							->       List[str]:
									SCREAMING_SNAKE_CASE__		  =				self.num_choices
									SCREAMING_SNAKE_CASE__		  =				NystromformerForMultipleChoice(config=UpperCAmelCase__		)
									model.to(UpperCAmelCase__		)
									model.eval()
									SCREAMING_SNAKE_CASE__		  =				input_ids.unsqueeze(1		).expand(-1   , self.num_choices   , -1		).contiguous()
									SCREAMING_SNAKE_CASE__		  =				token_type_ids.unsqueeze(1		).expand(-1   , self.num_choices   , -1		).contiguous()
									SCREAMING_SNAKE_CASE__		  =				input_mask.unsqueeze(1		).expand(-1   , self.num_choices   , -1		).contiguous()
									SCREAMING_SNAKE_CASE__		  =				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			:				Any		)							->       str:
									SCREAMING_SNAKE_CASE__		  =				self.prepare_config_and_inputs()
									(
									    SCREAMING_SNAKE_CASE__  
									)		  =				config_and_inputs
									SCREAMING_SNAKE_CASE__		  =				{'''input_ids''': input_ids, '''token_type_ids''': token_type_ids, '''attention_mask''': input_mask}
									return config, inputs_dict



@require_torch
class lowerCamelCase		(lowercase__    ,    lowercase__    ,    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 			lowerCAmelCase_					(						self			:				int		)							->       List[str]:
									SCREAMING_SNAKE_CASE__		  =				NystromformerModelTester(self		)
									SCREAMING_SNAKE_CASE__		  =				ConfigTester(self   , config_class=UpperCAmelCase__   , hidden_size=37		)



					def 			lowerCAmelCase_					(						self			:				Tuple		)							->       List[Any]:
									self.config_tester.run_common_tests()



					def 			lowerCAmelCase_					(						self			:				List[str]		)							->       int:
									SCREAMING_SNAKE_CASE__		  =				self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_model(*UpperCAmelCase__		)



					def 			lowerCAmelCase_					(						self			:				int		)							->       List[str]:
									SCREAMING_SNAKE_CASE__		  =				self.model_tester.prepare_config_and_inputs()
									for type in ["absolute", "relative_key", "relative_key_query"]:
													SCREAMING_SNAKE_CASE__		  =				type
													self.model_tester.create_and_check_model(*UpperCAmelCase__		)



					def 			lowerCAmelCase_					(						self			:				Optional[int]		)							->       Optional[Any]:
									SCREAMING_SNAKE_CASE__		  =				self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_for_masked_lm(*UpperCAmelCase__		)



					def 			lowerCAmelCase_					(						self			:				str		)							->       Any:
									SCREAMING_SNAKE_CASE__		  =				self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_for_multiple_choice(*UpperCAmelCase__		)



					def 			lowerCAmelCase_					(						self			:				str		)							->       int:
									SCREAMING_SNAKE_CASE__		  =				self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_for_question_answering(*UpperCAmelCase__		)



					def 			lowerCAmelCase_					(						self			:				Tuple		)							->       Union[str, Any]:
									SCREAMING_SNAKE_CASE__		  =				self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_for_sequence_classification(*UpperCAmelCase__		)



					def 			lowerCAmelCase_					(						self			:				Any		)							->       Tuple:
									SCREAMING_SNAKE_CASE__		  =				self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_for_token_classification(*UpperCAmelCase__		)



					@slow
					def 			lowerCAmelCase_					(						self			:				str		)							->       Union[str, Any]:
									for model_name in NYSTROMFORMER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
													SCREAMING_SNAKE_CASE__		  =				NystromformerModel.from_pretrained(UpperCAmelCase__		)
													self.assertIsNotNone(UpperCAmelCase__		)



@require_torch
class lowerCamelCase		(unittest.TestCase      ):






					'''simple docstring'''


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

									with torch.no_grad():
													SCREAMING_SNAKE_CASE__		  =				model(UpperCAmelCase__		)[0]

									SCREAMING_SNAKE_CASE__		  =				torch.Size((1, 6, 768)		)
									self.assertEqual(output.shape   , UpperCAmelCase__		)

									SCREAMING_SNAKE_CASE__		  =				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]   , UpperCAmelCase__   , atol=1e-4		)		)



					@slow
					def 			lowerCAmelCase_					(						self			:				Any		)							->       Tuple:
									SCREAMING_SNAKE_CASE__		  =				'''the [MASK] of Belgium is Brussels'''

									SCREAMING_SNAKE_CASE__		  =				AutoTokenizer.from_pretrained("uw-madison/nystromformer-512"		)
									SCREAMING_SNAKE_CASE__		  =				NystromformerForMaskedLM.from_pretrained("uw-madison/nystromformer-512"		)

									SCREAMING_SNAKE_CASE__		  =				tokenizer(UpperCAmelCase__   , return_tensors="pt"		)

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

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

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





"""simple docstring"""






def 						SCREAMING_SNAKE_CASE					(  __UpperCAmelCase       )							->   List[str]:
				if not head:
								return True
				# split the list to two parts
				SCREAMING_SNAKE_CASE__  ,       SCREAMING_SNAKE_CASE__		  =				head.next, head
				while fast and fast.next:
								SCREAMING_SNAKE_CASE__		  =				fast.next.next
								SCREAMING_SNAKE_CASE__		  =				slow.next
				SCREAMING_SNAKE_CASE__		  =				slow.next
				SCREAMING_SNAKE_CASE__		  =				None  # Don't forget here! But forget still works!
				# reverse the second part
				SCREAMING_SNAKE_CASE__		  =				None
				while second:
								SCREAMING_SNAKE_CASE__		  =				second.next
								SCREAMING_SNAKE_CASE__		  =				node
								SCREAMING_SNAKE_CASE__		  =				second
								SCREAMING_SNAKE_CASE__		  =				nxt
				# compare two parts
				# second part has the same or one less node
				while node:
								if node.val != head.val:
												return False
								SCREAMING_SNAKE_CASE__		  =				node.next
								SCREAMING_SNAKE_CASE__		  =				head.next
				return True




def 						SCREAMING_SNAKE_CASE					(  __UpperCAmelCase       )							->   str:
				if not head or not head.next:
								return True

				# 1. Get the midpoint (slow)
				SCREAMING_SNAKE_CASE__		  =				SCREAMING_SNAKE_CASE__		  =				SCREAMING_SNAKE_CASE__		  =				head
				while fast and fast.next:
								SCREAMING_SNAKE_CASE__  ,       SCREAMING_SNAKE_CASE__		  =				fast.next.next, slow.next

				# 2. Push the second half into the stack
				SCREAMING_SNAKE_CASE__		  =				[slow.val]
				while slow.next:
								SCREAMING_SNAKE_CASE__		  =				slow.next
								stack.append(slow.val       )

				# 3. Comparison
				while stack:
								if stack.pop() != cur.val:
												return False
								SCREAMING_SNAKE_CASE__		  =				cur.next

				return True




def 						SCREAMING_SNAKE_CASE					(  __UpperCAmelCase       )							->   str:
				if not head or not head.next:
								return True
				SCREAMING_SNAKE_CASE__		  =				{}
				SCREAMING_SNAKE_CASE__		  =				0
				while head:
								if head.val in d:
												d[head.val].append(__UpperCAmelCase       )
								else:
												SCREAMING_SNAKE_CASE__		  =				[pos]
								SCREAMING_SNAKE_CASE__		  =				head.next
								pos += 1
				SCREAMING_SNAKE_CASE__		  =				pos - 1
				SCREAMING_SNAKE_CASE__		  =				0
				for v in d.values():
								if len(__UpperCAmelCase       ) % 2 != 0:
												middle += 1
								else:
												SCREAMING_SNAKE_CASE__		  =				0
												for i in range(0  ,					len(__UpperCAmelCase       )       ):
																if v[i] + v[len(__UpperCAmelCase       ) - 1 - step] != checksum:
																				return False
																step += 1
								if middle > 1:
												return False
				return True




