
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       a		:







							def __init__(						self  :  Union[str, Any]      ,				snake_case__  :  List[str]      ,				snake_case__  :  List[str]=13      ,				snake_case__  :  Any=7      ,				snake_case__  :  Dict=True      ,				snake_case__  :  int=True      ,				snake_case__  :  str=True      ,				snake_case__  :  int=True      ,				snake_case__  :  int=99      ,				snake_case__  :  Any=32      ,				snake_case__  :  Optional[int]=5      ,				snake_case__  :  Union[str, Any]=4      ,				snake_case__  :  int=37      ,				snake_case__  :  List[Any]="gelu"      ,				snake_case__  :  Any=0.1      ,				snake_case__  :  List[str]=0.1      ,				snake_case__  :  str=512      ,				snake_case__  :  Union[str, Any]=16      ,				snake_case__  :  List[str]=2      ,				snake_case__  :  int=0.0_2      ,				snake_case__  :  Tuple=3      ,				snake_case__  :  Optional[Any]=4      ,				snake_case__  :  Union[str, Any]=None      ,				):
								"""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       UpperCAmelCase__					(						self  :  Optional[Any] ):
								"""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       UpperCAmelCase__					(						self  :  int ):
								"""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=snake_case__      ,				initializer_range=self.initializer_range      ,				)







							def       UpperCAmelCase__					(						self  :  int      ,				snake_case__  :  Optional[Any]      ,				snake_case__  :  Dict      ,				snake_case__  :  Optional[Any]      ,				snake_case__  :  Tuple      ,				snake_case__  :  str      ,				snake_case__  :  List[Any]      ,				snake_case__  :  Dict ):
								"""simple docstring"""







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







							def       UpperCAmelCase__					(						self  :  Optional[Any]      ,				snake_case__  :  Union[str, Any]      ,				snake_case__  :  Any      ,				snake_case__  :  int      ,				snake_case__  :  List[Any]      ,				snake_case__  :  Tuple      ,				snake_case__  :  Optional[Any]      ,				snake_case__  :  Tuple ):
								"""simple docstring"""







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







							def       UpperCAmelCase__					(						self  :  str      ,				snake_case__  :  Optional[Any]      ,				snake_case__  :  List[str]      ,				snake_case__  :  Union[str, Any]      ,				snake_case__  :  Any      ,				snake_case__  :  int      ,				snake_case__  :  List[Any]      ,				snake_case__  :  Union[str, Any] ):
								"""simple docstring"""







								__lowerCAmelCase         =							NystromformerForQuestionAnswering(config=snake_case__ )
								model.to(snake_case__ )
								model.eval()
								__lowerCAmelCase         =							model(
								    snake_case__      ,				attention_mask=snake_case__      ,				token_type_ids=snake_case__      ,				start_positions=snake_case__      ,				end_positions=snake_case__      ,				)
								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       UpperCAmelCase__					(						self  :  Tuple      ,				snake_case__  :  int      ,				snake_case__  :  Any      ,				snake_case__  :  List[Any]      ,				snake_case__  :  int      ,				snake_case__  :  Optional[int]      ,				snake_case__  :  Tuple      ,				snake_case__  :  Optional[int] ):
								"""simple docstring"""







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







							def       UpperCAmelCase__					(						self  :  Optional[Any]      ,				snake_case__  :  List[str]      ,				snake_case__  :  Any      ,				snake_case__  :  int      ,				snake_case__  :  Dict      ,				snake_case__  :  str      ,				snake_case__  :  Dict      ,				snake_case__  :  Tuple ):
								"""simple docstring"""







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







							def       UpperCAmelCase__					(						self  :  List[str]      ,				snake_case__  :  int      ,				snake_case__  :  str      ,				snake_case__  :  Optional[int]      ,				snake_case__  :  List[Any]      ,				snake_case__  :  Dict      ,				snake_case__  :  Optional[Any]      ,				snake_case__  :  Union[str, Any] ):
								"""simple docstring"""







								__lowerCAmelCase         =							self.num_choices
								__lowerCAmelCase         =							NystromformerForMultipleChoice(config=snake_case__ )
								model.to(snake_case__ )
								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(
								    snake_case__      ,				attention_mask=snake_case__      ,				token_type_ids=snake_case__      ,				labels=snake_case__      ,				)
								self.parent.assertEqual(result.logits.shape      ,				(self.batch_size, self.num_choices) )







							def       UpperCAmelCase__					(						self  :  Dict ):
								"""simple docstring"""







								__lowerCAmelCase         =							self.prepare_config_and_inputs()
								(
								    (
								    __lowerCAmelCase
								)   ,						(
								    __lowerCAmelCase
								)   ,						(
								    __lowerCAmelCase
								)   ,						(
								    __lowerCAmelCase
								)   ,						(
								    __lowerCAmelCase
								)   ,						(
								    __lowerCAmelCase
								)   ,						(
								    __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       a		(   __UpperCAmelCase      ,	__UpperCAmelCase      ,	unittest.TestCase					):
							lowercase_		:						int   							=  (
							    (
							        NystromformerModel,
							        NystromformerForMaskedLM,
							        NystromformerForMultipleChoice,
							        NystromformerForQuestionAnswering,
							        NystromformerForSequenceClassification,
							        NystromformerForTokenClassification,
							    )
							    if is_torch_available()
							    else ()
							)
							lowercase_		:						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 {}
							)
							lowercase_		:						Any   							=  False
							lowercase_		:						Union[str, Any]   							=  False







							def       UpperCAmelCase__					(						self  :  Optional[Any] ):
								"""simple docstring"""







								__lowerCAmelCase         =							NystromformerModelTester(self )
								__lowerCAmelCase         =							ConfigTester(self      ,				config_class=snake_case__      ,				hidden_size=37 )







							def       UpperCAmelCase__					(						self  :  List[Any] ):
								"""simple docstring"""







								self.config_tester.run_common_tests()







							def       UpperCAmelCase__					(						self  :  int ):
								"""simple docstring"""







								__lowerCAmelCase         =							self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_model(*snake_case__ )







							def       UpperCAmelCase__					(						self  :  List[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(*snake_case__ )







							def       UpperCAmelCase__					(						self  :  Optional[Any] ):
								"""simple docstring"""







								__lowerCAmelCase         =							self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_for_masked_lm(*snake_case__ )







							def       UpperCAmelCase__					(						self  :  Union[str, Any] ):
								"""simple docstring"""







								__lowerCAmelCase         =							self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_for_multiple_choice(*snake_case__ )







							def       UpperCAmelCase__					(						self  :  str ):
								"""simple docstring"""







								__lowerCAmelCase         =							self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_for_question_answering(*snake_case__ )







							def       UpperCAmelCase__					(						self  :  Optional[Any] ):
								"""simple docstring"""







								__lowerCAmelCase         =							self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_for_sequence_classification(*snake_case__ )







							def       UpperCAmelCase__					(						self  :  Any ):
								"""simple docstring"""







								__lowerCAmelCase         =							self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_for_token_classification(*snake_case__ )







							@slow
							def       UpperCAmelCase__					(						self  :  Dict ):
								"""simple docstring"""







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

@require_torch
class       a		(   unittest.TestCase					):







							@slow
							def       UpperCAmelCase__					(						self  :  Optional[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(snake_case__ )[0]

								__lowerCAmelCase         =							torch.Size((1, 6, 768) )
								self.assertEqual(output.shape      ,				snake_case__ )

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







							@slow
							def       UpperCAmelCase__					(						self  :  Optional[int] ):
								"""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(snake_case__      ,				return_tensors="pt" )

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

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

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








import os

import pytest

from datasets import (
    get_dataset_config_info,
    get_dataset_config_names,
    get_dataset_infos,
    get_dataset_split_names,
    inspect_dataset,
    inspect_metric,
)


UpperCamelCase_   	=	pytest.mark.integration






@pytest.mark.parametrize("path"			,							["paws", "csv"]  )
def 		_UpperCAmelCase   (	UpperCamelCase:     Optional[int]			,							UpperCamelCase:     Tuple  ):






	"""simple docstring"""


	inspect_dataset(UpperCamelCase			,							UpperCamelCase  )
	__lowerCAmelCase         =							path + ".py"
	assert script_name in os.listdir(UpperCamelCase  )
	assert "__pycache__" not in os.listdir(UpperCamelCase  )






@pytest.mark.filterwarnings("ignore:inspect_metric is deprecated:FutureWarning"  )
@pytest.mark.filterwarnings("ignore:metric_module_factory is deprecated:FutureWarning"  )
@pytest.mark.parametrize("path"			,							["accuracy"]  )
def 		_UpperCAmelCase   (	UpperCamelCase:     List[Any]			,							UpperCamelCase:     Any  ):






	"""simple docstring"""


	inspect_metric(UpperCamelCase			,							UpperCamelCase  )
	__lowerCAmelCase         =							path + ".py"
	assert script_name in os.listdir(UpperCamelCase  )
	assert "__pycache__" not in os.listdir(UpperCamelCase  )






@pytest.mark.parametrize(
    "path, config_name, expected_splits"			,							[
        ("squad", "plain_text", ["train", "validation"]),
        ("dalle-mini/wit", "dalle-mini--wit", ["train"]),
        ("paws", "labeled_final", ["train", "test", "validation"]),
    ]			,							)
def 		_UpperCAmelCase   (	UpperCamelCase:     Optional[Any]			,							UpperCamelCase:     int			,							UpperCamelCase:     Tuple  ):






	"""simple docstring"""


	__lowerCAmelCase         =							get_dataset_config_info(UpperCamelCase			,							config_name=UpperCamelCase  )
	assert info.config_name == config_name
	assert list(info.splits.keys()  ) == expected_splits






@pytest.mark.parametrize(
    "path, config_name, expected_exception"			,							[
        ("paws", None, ValueError),
    ]			,							)
def 		_UpperCAmelCase   (	UpperCamelCase:     Dict			,							UpperCamelCase:     Any			,							UpperCamelCase:     List[str]  ):






	"""simple docstring"""


	with pytest.raises(UpperCamelCase  ):
		get_dataset_config_info(UpperCamelCase			,							config_name=UpperCamelCase  )






@pytest.mark.parametrize(
    "path, expected"			,							[
        ("squad", "plain_text"),
        ("acronym_identification", "default"),
        ("lhoestq/squad", "plain_text"),
        ("lhoestq/test", "default"),
        ("lhoestq/demo1", "lhoestq--demo1"),
        ("dalle-mini/wit", "dalle-mini--wit"),
    ]			,							)
def 		_UpperCAmelCase   (	UpperCamelCase:     List[str]			,							UpperCamelCase:     Optional[int]  ):






	"""simple docstring"""


	__lowerCAmelCase         =							get_dataset_config_names(UpperCamelCase  )
	assert expected in config_names






@pytest.mark.parametrize(
    "path, expected_configs, expected_splits_in_first_config"			,							[
        ("squad", ["plain_text"], ["train", "validation"]),
        ("dalle-mini/wit", ["dalle-mini--wit"], ["train"]),
        ("paws", ["labeled_final", "labeled_swap", "unlabeled_final"], ["train", "test", "validation"]),
    ]			,							)
def 		_UpperCAmelCase   (	UpperCamelCase:     str			,							UpperCamelCase:     Tuple			,							UpperCamelCase:     List[Any]  ):






	"""simple docstring"""


	__lowerCAmelCase         =							get_dataset_infos(UpperCamelCase  )
	assert list(infos.keys()  ) == expected_configs
	__lowerCAmelCase         =							expected_configs[0]
	assert expected_config in infos
	__lowerCAmelCase         =							infos[expected_config]
	assert info.config_name == expected_config
	assert list(info.splits.keys()  ) == expected_splits_in_first_config






@pytest.mark.parametrize(
    "path, expected_config, expected_splits"			,							[
        ("squad", "plain_text", ["train", "validation"]),
        ("dalle-mini/wit", "dalle-mini--wit", ["train"]),
        ("paws", "labeled_final", ["train", "test", "validation"]),
    ]			,							)
def 		_UpperCAmelCase   (	UpperCamelCase:     Optional[int]			,							UpperCamelCase:     int			,							UpperCamelCase:     Optional[Any]  ):






	"""simple docstring"""


	__lowerCAmelCase         =							get_dataset_infos(UpperCamelCase  )
	assert expected_config in infos
	__lowerCAmelCase         =							infos[expected_config]
	assert info.config_name == expected_config
	assert list(info.splits.keys()  ) == expected_splits







@pytest.mark.parametrize(
    "path, config_name, expected_exception"			,							[
        ("paws", None, ValueError),
    ]			,							)
def 		_UpperCAmelCase   (	UpperCamelCase:     Dict			,							UpperCamelCase:     List[str]			,							UpperCamelCase:     List[Any]  ):






	"""simple docstring"""


	with pytest.raises(UpperCamelCase  ):
		get_dataset_split_names(UpperCamelCase			,							config_name=UpperCamelCase  )






