
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       ,					__lowerCAmelCase       ,					__lowerCAmelCase=1_3       ,					__lowerCAmelCase=7       ,					__lowerCAmelCase=True       ,					__lowerCAmelCase=True       ,					__lowerCAmelCase=True       ,					__lowerCAmelCase=True       ,					__lowerCAmelCase=9_9       ,					__lowerCAmelCase=3_2       ,					__lowerCAmelCase=5       ,					__lowerCAmelCase=4       ,					__lowerCAmelCase=3_7       ,					__lowerCAmelCase="gelu"       ,					__lowerCAmelCase=0.1       ,					__lowerCAmelCase=0.1       ,					__lowerCAmelCase=5_1_2       ,					__lowerCAmelCase=1_6       ,					__lowerCAmelCase=2       ,					__lowerCAmelCase=0.02       ,					__lowerCAmelCase=3       ,					__lowerCAmelCase=4       ,					__lowerCAmelCase=None       ,					):
								"""simple docstring"""
								__magic_name__		:Any        =      parent
								__magic_name__		:int        =      batch_size
								__magic_name__		:List[Any]        =      seq_length
								__magic_name__		:List[str]        =      is_training
								__magic_name__		:Union[str, Any]        =      use_input_mask
								__magic_name__		:Union[str, Any]        =      use_token_type_ids
								__magic_name__		:Union[str, Any]        =      use_labels
								__magic_name__		:int        =      vocab_size
								__magic_name__		:Optional[int]        =      hidden_size
								__magic_name__		:List[Any]        =      num_hidden_layers
								__magic_name__		:Any        =      num_attention_heads
								__magic_name__		:Union[str, Any]        =      intermediate_size
								__magic_name__		:List[str]        =      hidden_act
								__magic_name__		:List[Any]        =      hidden_dropout_prob
								__magic_name__		:Any        =      attention_probs_dropout_prob
								__magic_name__		:str        =      max_position_embeddings
								__magic_name__		:Any        =      type_vocab_size
								__magic_name__		:Any        =      type_sequence_label_size
								__magic_name__		:Dict        =      initializer_range
								__magic_name__		:Any        =      num_labels
								__magic_name__		:Dict        =      num_choices
								__magic_name__		:str        =      scope







				def 			A	(						self     ):
								"""simple docstring"""
								__magic_name__		:Tuple        =      ids_tensor([self.batch_size, self.seq_length]       ,					self.vocab_size     )

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

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

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

								__magic_name__		:Any        =      self.get_config()

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







				def 			A	(						self     ):
								"""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 			A	(						self       ,					__lowerCAmelCase       ,					__lowerCAmelCase       ,					__lowerCAmelCase       ,					__lowerCAmelCase       ,					__lowerCAmelCase       ,					__lowerCAmelCase       ,					__lowerCAmelCase     ):
								"""simple docstring"""
								__magic_name__		:Tuple        =      NystromformerModel(config=a_     )
								model.to(a_     )
								model.eval()
								__magic_name__		:List[str]        =      model(a_       ,					attention_mask=a_       ,					token_type_ids=a_     )
								__magic_name__		:str        =      model(a_       ,					token_type_ids=a_     )
								__magic_name__		:int        =      model(a_     )
								self.parent.assertEqual(result.last_hidden_state.shape       ,					(self.batch_size, self.seq_length, self.hidden_size)     )







				def 			A	(						self       ,					__lowerCAmelCase       ,					__lowerCAmelCase       ,					__lowerCAmelCase       ,					__lowerCAmelCase       ,					__lowerCAmelCase       ,					__lowerCAmelCase       ,					__lowerCAmelCase     ):
								"""simple docstring"""
								__magic_name__		:int        =      NystromformerForMaskedLM(config=a_     )
								model.to(a_     )
								model.eval()
								__magic_name__		:str        =      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 			A	(						self       ,					__lowerCAmelCase       ,					__lowerCAmelCase       ,					__lowerCAmelCase       ,					__lowerCAmelCase       ,					__lowerCAmelCase       ,					__lowerCAmelCase       ,					__lowerCAmelCase     ):
								"""simple docstring"""
								__magic_name__		:Tuple        =      NystromformerForQuestionAnswering(config=a_     )
								model.to(a_     )
								model.eval()
								__magic_name__		:Optional[int]        =      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 			A	(						self       ,					__lowerCAmelCase       ,					__lowerCAmelCase       ,					__lowerCAmelCase       ,					__lowerCAmelCase       ,					__lowerCAmelCase       ,					__lowerCAmelCase       ,					__lowerCAmelCase     ):
								"""simple docstring"""
								__magic_name__		:int        =      self.num_labels
								__magic_name__		:str        =      NystromformerForSequenceClassification(a_     )
								model.to(a_     )
								model.eval()
								__magic_name__		:Optional[int]        =      model(a_       ,					attention_mask=a_       ,					token_type_ids=a_       ,					labels=a_     )
								self.parent.assertEqual(result.logits.shape       ,					(self.batch_size, self.num_labels)     )







				def 			A	(						self       ,					__lowerCAmelCase       ,					__lowerCAmelCase       ,					__lowerCAmelCase       ,					__lowerCAmelCase       ,					__lowerCAmelCase       ,					__lowerCAmelCase       ,					__lowerCAmelCase     ):
								"""simple docstring"""
								__magic_name__		:int        =      self.num_labels
								__magic_name__		:List[Any]        =      NystromformerForTokenClassification(config=a_     )
								model.to(a_     )
								model.eval()
								__magic_name__		:Tuple        =      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 			A	(						self       ,					__lowerCAmelCase       ,					__lowerCAmelCase       ,					__lowerCAmelCase       ,					__lowerCAmelCase       ,					__lowerCAmelCase       ,					__lowerCAmelCase       ,					__lowerCAmelCase     ):
								"""simple docstring"""
								__magic_name__		:Any        =      self.num_choices
								__magic_name__		:Dict        =      NystromformerForMultipleChoice(config=a_     )
								model.to(a_     )
								model.eval()
								__magic_name__		:int        =      input_ids.unsqueeze(1     ).expand(-1       ,					self.num_choices       ,					-1     ).contiguous()
								__magic_name__		:Optional[int]        =      token_type_ids.unsqueeze(1     ).expand(-1       ,					self.num_choices       ,					-1     ).contiguous()
								__magic_name__		:int        =      input_mask.unsqueeze(1     ).expand(-1       ,					self.num_choices       ,					-1     ).contiguous()
								__magic_name__		:int        =      model(
								    a_       ,					attention_mask=a_       ,					token_type_ids=a_       ,					labels=a_       ,					)
								self.parent.assertEqual(result.logits.shape       ,					(self.batch_size, self.num_choices)     )







				def 			A	(						self     ):
								"""simple docstring"""
								__magic_name__		:List[Any]        =      self.prepare_config_and_inputs()
								(
								    __magic_name__      
								)		:List[Any]        =      config_and_inputs
								__magic_name__		:str        =      {"""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 ):
				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 			A	(						self     ):
								"""simple docstring"""
								__magic_name__		:int        =      NystromformerModelTester(self     )
								__magic_name__		:str        =      ConfigTester(self       ,					config_class=a_       ,					hidden_size=3_7     )







				def 			A	(						self     ):
								"""simple docstring"""
								self.config_tester.run_common_tests()







				def 			A	(						self     ):
								"""simple docstring"""
								__magic_name__		:List[str]        =      self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_model(*a_     )







				def 			A	(						self     ):
								"""simple docstring"""
								__magic_name__		:int        =      self.model_tester.prepare_config_and_inputs()
								for type in ["absolute", "relative_key", "relative_key_query"]:
												__magic_name__		:Any        =      type
												self.model_tester.create_and_check_model(*a_     )







				def 			A	(						self     ):
								"""simple docstring"""
								__magic_name__		:List[Any]        =      self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_for_masked_lm(*a_     )







				def 			A	(						self     ):
								"""simple docstring"""
								__magic_name__		:str        =      self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_for_multiple_choice(*a_     )







				def 			A	(						self     ):
								"""simple docstring"""
								__magic_name__		:int        =      self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_for_question_answering(*a_     )







				def 			A	(						self     ):
								"""simple docstring"""
								__magic_name__		:str        =      self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_for_sequence_classification(*a_     )







				def 			A	(						self     ):
								"""simple docstring"""
								__magic_name__		:List[Any]        =      self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_for_token_classification(*a_     )







				@slow
				def 			A	(						self     ):
								"""simple docstring"""



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







@require_torch
class 						lowerCamelCase_				(      unittest.TestCase ):







				@slow
				def 			A	(						self     ):
								"""simple docstring"""
								__magic_name__		:Tuple        =      NystromformerModel.from_pretrained('''uw-madison/nystromformer-512'''     )
								__magic_name__		:List[str]        =      torch.tensor([[0, 1, 2, 3, 4, 5]]     )

								with torch.no_grad():
												__magic_name__		:Optional[Any]        =      model(a_     )[0]

								__magic_name__		:int        =      torch.Size((1, 6, 7_6_8)     )
								self.assertEqual(output.shape       ,					a_     )

								__magic_name__		:Optional[Any]        =      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 			A	(						self     ):
								"""simple docstring"""
								__magic_name__		:List[Any]        =      """the [MASK] of Belgium is Brussels"""

								__magic_name__		:Optional[Any]        =      AutoTokenizer.from_pretrained('''uw-madison/nystromformer-512'''     )
								__magic_name__		:Optional[Any]        =      NystromformerForMaskedLM.from_pretrained('''uw-madison/nystromformer-512'''     )

								__magic_name__		:Optional[Any]        =      tokenizer(a_       ,					return_tensors='''pt'''     )

								with torch.no_grad():
												__magic_name__		:List[Any]        =      model(encoding.input_ids     ).logits

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

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














import unittest

import numpy as np
import torch

from .utils_summarization import build_mask, compute_token_type_ids, process_story, truncate_or_pad
class 						lowerCamelCase_				(      unittest.TestCase ):







				def 			A	(						self     ):
								"""simple docstring"""
								__magic_name__		:Dict        =      1_0







				def 			A	(						self     ):
								"""simple docstring"""
								__magic_name__		:Dict        =      [1, 2, 3, 4]
								__magic_name__		:Union[str, Any]        =      [1, 2, 3, 4, 0, 0, 0, 0, 0, 0]
								self.assertEqual(truncate_or_pad(__lowerCAmelCase       ,					self.block_size       ,					0     )       ,					__lowerCAmelCase     )







				def 			A	(						self     ):
								"""simple docstring"""
								__magic_name__		:Dict        =      [1, 2, 3, 4, 5, 6, 7, 8, 9, 1_0]
								__magic_name__		:Optional[Any]        =      [1, 2, 3, 4, 5, 6, 7, 8, 9, 1_0]
								self.assertEqual(truncate_or_pad(__lowerCAmelCase       ,					self.block_size       ,					0     )       ,					__lowerCAmelCase     )







				def 			A	(						self     ):
								"""simple docstring"""
								__magic_name__		:Any        =      [1, 2, 3, 4, 5, 6, 7, 8, 9, 1_0, 1_1, 1_2, 1_3]
								__magic_name__		:str        =      [1, 2, 3, 4, 5, 6, 7, 8, 9, 1_0]
								self.assertEqual(truncate_or_pad(__lowerCAmelCase       ,					self.block_size       ,					0     )       ,					__lowerCAmelCase     )







				def 			A	(						self     ):
								"""simple docstring"""
								__magic_name__		:Tuple        =      '''It was the year of Our Lord one thousand seven hundred and
        seventy-five.\n\nSpiritual revelations were conceded to England at that
        favoured period, as at this.'''
								__magic_name__     ,				__magic_name__		:Optional[int]        =      process_story(__lowerCAmelCase     )
								self.assertEqual(__lowerCAmelCase       ,					[]     )







				def 			A	(						self     ):
								"""simple docstring"""
								__magic_name__		:Union[str, Any]        =      ''''''
								__magic_name__     ,				__magic_name__		:Optional[int]        =      process_story(__lowerCAmelCase     )
								self.assertEqual(__lowerCAmelCase       ,					[]     )
								self.assertEqual(__lowerCAmelCase       ,					[]     )







				def 			A	(						self     ):
								"""simple docstring"""
								__magic_name__		:List[Any]        =      (
								    '''It was the year of Our Lord one thousand seven hundred and '''
								    '''seventy-five\n\nSpiritual revelations were conceded to England '''
								    '''at that favoured period, as at this.\n@highlight\n\nIt was the best of times'''
								)
								__magic_name__     ,				__magic_name__		:str        =      process_story(__lowerCAmelCase     )

								__magic_name__		:Optional[int]        =      [
								    '''It was the year of Our Lord one thousand seven hundred and seventy-five.''',
								    '''Spiritual revelations were conceded to England at that favoured period, as at this.''',
								]
								self.assertEqual(__lowerCAmelCase       ,					__lowerCAmelCase     )

								__magic_name__		:Union[str, Any]        =      ['''It was the best of times.''']
								self.assertEqual(__lowerCAmelCase       ,					__lowerCAmelCase     )







				def 			A	(						self     ):
								"""simple docstring"""
								__magic_name__		:Any        =      torch.tensor([1, 2, 3, 4]     )
								__magic_name__		:List[str]        =      torch.tensor([1, 1, 1, 1]     )
								np.testing.assert_array_equal(build_mask(__lowerCAmelCase       ,					0     ).numpy()       ,					expected.numpy()     )







				def 			A	(						self     ):
								"""simple docstring"""
								__magic_name__		:List[str]        =      torch.tensor([1, 2, 3, 4, 2_3, 2_3, 2_3]     )
								__magic_name__		:Union[str, Any]        =      torch.tensor([1, 1, 1, 1, 0, 0, 0]     )
								np.testing.assert_array_equal(build_mask(__lowerCAmelCase       ,					2_3     ).numpy()       ,					expected.numpy()     )







				def 			A	(						self     ):
								"""simple docstring"""
								__magic_name__		:Any        =      torch.tensor([8, 2, 3, 4, 1, 1, 1]     )
								__magic_name__		:Dict        =      torch.tensor([1, 1, 1, 1, 0, 0, 0]     )
								np.testing.assert_array_equal(build_mask(__lowerCAmelCase       ,					1     ).numpy()       ,					expected.numpy()     )







				def 			A	(						self     ):
								"""simple docstring"""
								__magic_name__		:Dict        =      1_0_1
								__magic_name__		:List[Any]        =      torch.tensor([[1, 2, 3, 4, 5, 6], [1, 2, 3, 1_0_1, 5, 6], [1, 1_0_1, 3, 4, 1_0_1, 6]]     )
								__magic_name__		:str        =      torch.tensor([[1, 1, 1, 1, 1, 1], [1, 1, 1, 0, 0, 0], [1, 0, 0, 0, 1, 1]]     )

								__magic_name__		:Tuple        =      compute_token_type_ids(__lowerCAmelCase       ,					__lowerCAmelCase     )
								np.testing.assert_array_equal(__lowerCAmelCase       ,					__lowerCAmelCase     )






