



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					:



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



						"""simple docstring"""






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



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



						"""simple docstring"""






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

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

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

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

						_UpperCAmelCase	 =     self.get_config()

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



			def 					_lowerCamelCase (		self     :						Tuple)     ->					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     :						Any				,	A     :						Optional[int]				,	A     :						int				,	A     :						List[Any]				,	A     :						Union[str, Any]				,	A     :						Any				,	A     :						List[str]				,	A     :						str)     ->					Union[str, Any]:



						"""simple docstring"""






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



			def 					_lowerCamelCase (		self     :						Optional[Any]				,	A     :						int				,	A     :						Any				,	A     :						Dict				,	A     :						Union[str, Any]				,	A     :						List[Any]				,	A     :						List[Any]				,	A     :						str)     ->					int:



						"""simple docstring"""






						_UpperCAmelCase	 =     NystromformerForMaskedLM(config=A)
						model.to(A)
						model.eval()
						_UpperCAmelCase	 =     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     :						Tuple				,	A     :						List[Any]				,	A     :						int				,	A     :						Union[str, Any]				,	A     :						int				,	A     :						str				,	A     :						Tuple				,	A     :						str)     ->					Dict:



						"""simple docstring"""






						_UpperCAmelCase	 =     NystromformerForQuestionAnswering(config=A)
						model.to(A)
						model.eval()
						_UpperCAmelCase	 =     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]				,	A     :						Optional[Any]				,	A     :						List[Any]				,	A     :						Union[str, Any]				,	A     :						str				,	A     :						List[str]				,	A     :						str				,	A     :						Any)     ->					int:



						"""simple docstring"""






						_UpperCAmelCase	 =     self.num_labels
						_UpperCAmelCase	 =     NystromformerForSequenceClassification(A)
						model.to(A)
						model.eval()
						_UpperCAmelCase	 =     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     :						List[Any]				,	A     :						Optional[Any]				,	A     :						List[Any]				,	A     :						Optional[int]				,	A     :						Tuple				,	A     :						int				,	A     :						int				,	A     :						List[str])     ->					List[Any]:



						"""simple docstring"""






						_UpperCAmelCase	 =     self.num_labels
						_UpperCAmelCase	 =     NystromformerForTokenClassification(config=A)
						model.to(A)
						model.eval()
						_UpperCAmelCase	 =     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     :						List[Any]				,	A     :						str				,	A     :						Tuple				,	A     :						Optional[int]				,	A     :						int				,	A     :						str				,	A     :						List[str]				,	A     :						Any)     ->					str:



						"""simple docstring"""






						_UpperCAmelCase	 =     self.num_choices
						_UpperCAmelCase	 =     NystromformerForMultipleChoice(config=A)
						model.to(A)
						model.eval()
						_UpperCAmelCase	 =     input_ids.unsqueeze(1).expand(-1				,	self.num_choices				,	-1).contiguous()
						_UpperCAmelCase	 =     token_type_ids.unsqueeze(1).expand(-1				,	self.num_choices				,	-1).contiguous()
						_UpperCAmelCase	 =     input_mask.unsqueeze(1).expand(-1				,	self.num_choices				,	-1).contiguous()
						_UpperCAmelCase	 =     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)     ->					Optional[Any]:



						"""simple docstring"""






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







@require_torch
class     __lowerCAmelCase					(    A       ,		A       ,		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     :						str)     ->					Union[str, Any]:



						"""simple docstring"""






						_UpperCAmelCase	 =     NystromformerModelTester(self)
						_UpperCAmelCase	 =     ConfigTester(self				,	config_class=A				,	hidden_size=37)



			def 					_lowerCamelCase (		self     :						Optional[int])     ->					Tuple:



						"""simple docstring"""






						self.config_tester.run_common_tests()



			def 					_lowerCamelCase (		self     :						List[Any])     ->					Optional[int]:



						"""simple docstring"""






						_UpperCAmelCase	 =     self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_model(*A)



			def 					_lowerCamelCase (		self     :						Any)     ->					Tuple:



						"""simple docstring"""






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



			def 					_lowerCamelCase (		self     :						Optional[int])     ->					int:



						"""simple docstring"""






						_UpperCAmelCase	 =     self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_for_masked_lm(*A)



			def 					_lowerCamelCase (		self     :						Union[str, Any])     ->					List[str]:



						"""simple docstring"""






						_UpperCAmelCase	 =     self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_for_multiple_choice(*A)



			def 					_lowerCamelCase (		self     :						int)     ->					int:



						"""simple docstring"""






						_UpperCAmelCase	 =     self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_for_question_answering(*A)



			def 					_lowerCamelCase (		self     :						Tuple)     ->					List[Any]:



						"""simple docstring"""






						_UpperCAmelCase	 =     self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_for_sequence_classification(*A)



			def 					_lowerCamelCase (		self     :						Optional[int])     ->					Tuple:



						"""simple docstring"""






						_UpperCAmelCase	 =     self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_for_token_classification(*A)



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



						"""simple docstring"""






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







@require_torch
class     __lowerCAmelCase					(    unittest.TestCase		):



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



						"""simple docstring"""






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

						with torch.no_grad():
									_UpperCAmelCase	 =     model(A)[0]

						_UpperCAmelCase	 =     torch.Size((1, 6, 7_68))
						self.assertEqual(output.shape				,	A)

						_UpperCAmelCase	 =     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]				,	A				,	atol=1E-4))



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



						"""simple docstring"""






						_UpperCAmelCase	 =     'the [MASK] of Belgium is Brussels'

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

						_UpperCAmelCase	 =     tokenizer(A				,	return_tensors='pt')

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

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

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





import json
import os
import unittest

from transformers import MgpstrTokenizer
from transformers.models.mgp_str.tokenization_mgp_str import VOCAB_FILES_NAMES
from transformers.testing_utils import require_tokenizers

from ...test_tokenization_common import TokenizerTesterMixin







@require_tokenizers
class     __lowerCAmelCase					(    A       ,		unittest.TestCase		):
			UpperCamelCase           =  MgpstrTokenizer
			UpperCamelCase           =  False
			UpperCamelCase           =  {}
			UpperCamelCase           =  False



			def 					_lowerCamelCase (		self     :						int)     ->					List[Any]:



						"""simple docstring"""






						super().setUp()

						# fmt: off
						_UpperCAmelCase	 =     ['[GO]', '[s]', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
						# fmt: on
						_UpperCAmelCase	 =     dict(zip(A				,	range(len(A))))

						_UpperCAmelCase	 =     os.path.join(self.tmpdirname				,	VOCAB_FILES_NAMES['vocab_file'])
						with open(self.vocab_file				,	'w'				,	encoding='utf-8') as fp:
									fp.write(json.dumps(A) + '\n')



			def 					_lowerCamelCase (		self     :						Dict				,	**A     :						List[Any])     ->					Optional[Any]:



						"""simple docstring"""






						return MgpstrTokenizer.from_pretrained(self.tmpdirname				,	**A)



			def 					_lowerCamelCase (		self     :						List[str]				,	A     :						Optional[int])     ->					Dict:



						"""simple docstring"""






						_UpperCAmelCase	 =     'tester'
						_UpperCAmelCase	 =     'tester'
						return input_text, output_text



			@unittest.skip('MGP-STR always lower cases letters.')
			def 					_lowerCamelCase (		self     :						Optional[Any])     ->					int:



						"""simple docstring"""






						pass



			def 					_lowerCamelCase (		self     :						Optional[int])     ->					Optional[int]:



						"""simple docstring"""






						_UpperCAmelCase	 =     self.get_tokenizers(do_lower_case=A)
						for tokenizer in tokenizers:
									with self.subTest(F"{tokenizer.__class__.__name__}"):
												_UpperCAmelCase	 =     '[SPECIAL_TOKEN]'

												tokenizer.add_special_tokens({'cls_token': special_token})
												_UpperCAmelCase	 =     tokenizer.encode([special_token]				,	add_special_tokens=A)
												self.assertEqual(len(A)				,	1)

												_UpperCAmelCase	 =     tokenizer.decode(A				,	skip_special_tokens=A)
												self.assertTrue(special_token not in decoded)



			def 					_lowerCamelCase (		self     :						Any)     ->					str:



						"""simple docstring"""






						_UpperCAmelCase	 =     self.get_tokenizers()
						for tokenizer in tokenizers:
									with self.subTest(F"{tokenizer.__class__.__name__}"):
												_UpperCAmelCase ,  _UpperCAmelCase	 =     self.get_input_output_texts(A)

												_UpperCAmelCase	 =     tokenizer.tokenize(A)
												_UpperCAmelCase	 =     tokenizer.convert_tokens_to_ids(A)
												_UpperCAmelCase	 =     tokenizer.encode(A				,	add_special_tokens=A)
												self.assertListEqual(A				,	A)

												_UpperCAmelCase	 =     tokenizer.convert_ids_to_tokens(A)
												self.assertNotEqual(len(A)				,	0)
												_UpperCAmelCase	 =     tokenizer.decode(A)
												self.assertIsInstance(A				,	A)

												self.assertEqual(text_a.replace(' '				,	'')				,	A)



			@unittest.skip('MGP-STR tokenizer only handles one sequence.')
			def 					_lowerCamelCase (		self     :						str)     ->					List[str]:



						"""simple docstring"""






						pass



			@unittest.skip('inputs cannot be pretokenized in MgpstrTokenizer')
			def 					_lowerCamelCase (		self     :						Dict)     ->					Tuple:



						"""simple docstring"""






						pass
