






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






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






	def  lowerCAmelCase_				(		self							)    ->     Union[str, Any]:
					snake_case__   :Optional[int]       = ids_tensor([self.batch_size, self.seq_length]       ,self.vocab_size							)

					snake_case__   :Any       = None
					if self.use_input_mask:
									snake_case__   :List[str]       = random_attention_mask([self.batch_size, self.seq_length]							)

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

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

					snake_case__   :Union[str, Any]       = self.get_config()

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






	def  lowerCAmelCase_				(		self							)    ->     int:
					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=_SCREAMING_SNAKE_CASE       ,initializer_range=self.initializer_range       ,)






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






	def  lowerCAmelCase_				(		self       ,UpperCamelCase       ,UpperCamelCase       ,UpperCamelCase       ,UpperCamelCase       ,UpperCamelCase       ,UpperCamelCase       ,UpperCamelCase							)    ->     Optional[Any]:
					snake_case__   :Optional[int]       = NystromformerForMaskedLM(config=_SCREAMING_SNAKE_CASE							)
					model.to(_SCREAMING_SNAKE_CASE							)
					model.eval()
					snake_case__   :List[str]       = model(_SCREAMING_SNAKE_CASE       ,attention_mask=_SCREAMING_SNAKE_CASE       ,token_type_ids=_SCREAMING_SNAKE_CASE       ,labels=_SCREAMING_SNAKE_CASE							)
					self.parent.assertEqual(result.logits.shape       ,(self.batch_size, self.seq_length, self.vocab_size)							)






	def  lowerCAmelCase_				(		self       ,UpperCamelCase       ,UpperCamelCase       ,UpperCamelCase       ,UpperCamelCase       ,UpperCamelCase       ,UpperCamelCase       ,UpperCamelCase							)    ->     Any:
					snake_case__   :Tuple       = NystromformerForQuestionAnswering(config=_SCREAMING_SNAKE_CASE							)
					model.to(_SCREAMING_SNAKE_CASE							)
					model.eval()
					snake_case__   :Tuple       = model(
					    _SCREAMING_SNAKE_CASE       ,attention_mask=_SCREAMING_SNAKE_CASE       ,token_type_ids=_SCREAMING_SNAKE_CASE       ,start_positions=_SCREAMING_SNAKE_CASE       ,end_positions=_SCREAMING_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  lowerCAmelCase_				(		self       ,UpperCamelCase       ,UpperCamelCase       ,UpperCamelCase       ,UpperCamelCase       ,UpperCamelCase       ,UpperCamelCase       ,UpperCamelCase							)    ->     Optional[int]:
					snake_case__   :Optional[int]       = self.num_labels
					snake_case__   :Optional[int]       = NystromformerForSequenceClassification(_SCREAMING_SNAKE_CASE							)
					model.to(_SCREAMING_SNAKE_CASE							)
					model.eval()
					snake_case__   :Optional[int]       = model(_SCREAMING_SNAKE_CASE       ,attention_mask=_SCREAMING_SNAKE_CASE       ,token_type_ids=_SCREAMING_SNAKE_CASE       ,labels=_SCREAMING_SNAKE_CASE							)
					self.parent.assertEqual(result.logits.shape       ,(self.batch_size, self.num_labels)							)






	def  lowerCAmelCase_				(		self       ,UpperCamelCase       ,UpperCamelCase       ,UpperCamelCase       ,UpperCamelCase       ,UpperCamelCase       ,UpperCamelCase       ,UpperCamelCase							)    ->     Optional[int]:
					snake_case__   :Optional[Any]       = self.num_labels
					snake_case__   :Any       = NystromformerForTokenClassification(config=_SCREAMING_SNAKE_CASE							)
					model.to(_SCREAMING_SNAKE_CASE							)
					model.eval()
					snake_case__   :int       = model(_SCREAMING_SNAKE_CASE       ,attention_mask=_SCREAMING_SNAKE_CASE       ,token_type_ids=_SCREAMING_SNAKE_CASE       ,labels=_SCREAMING_SNAKE_CASE							)
					self.parent.assertEqual(result.logits.shape       ,(self.batch_size, self.seq_length, self.num_labels)							)






	def  lowerCAmelCase_				(		self       ,UpperCamelCase       ,UpperCamelCase       ,UpperCamelCase       ,UpperCamelCase       ,UpperCamelCase       ,UpperCamelCase       ,UpperCamelCase							)    ->     List[Any]:
					snake_case__   :List[str]       = self.num_choices
					snake_case__   :Tuple       = NystromformerForMultipleChoice(config=_SCREAMING_SNAKE_CASE							)
					model.to(_SCREAMING_SNAKE_CASE							)
					model.eval()
					snake_case__   :Any       = input_ids.unsqueeze(1							).expand(-1       ,self.num_choices       ,-1							).contiguous()
					snake_case__   :int       = token_type_ids.unsqueeze(1							).expand(-1       ,self.num_choices       ,-1							).contiguous()
					snake_case__   :Any       = input_mask.unsqueeze(1							).expand(-1       ,self.num_choices       ,-1							).contiguous()
					snake_case__   :Optional[Any]       = model(
					    _SCREAMING_SNAKE_CASE       ,attention_mask=_SCREAMING_SNAKE_CASE       ,token_type_ids=_SCREAMING_SNAKE_CASE       ,labels=_SCREAMING_SNAKE_CASE       ,)
					self.parent.assertEqual(result.logits.shape       ,(self.batch_size, self.num_choices)							)






	def  lowerCAmelCase_				(		self							)    ->     Optional[int]:
					snake_case__   :Tuple       = self.prepare_config_and_inputs()
					(
					    (
					    snake_case__   
					)     ,    (
					    snake_case__   
					)     ,    (
					    snake_case__   
					)     ,    (
					    snake_case__   
					)     ,    (
					    snake_case__   
					)     ,    (
					    snake_case__   
					)     ,    (
					    snake_case__   
					)     ,       
					)   :Any       = config_and_inputs
					snake_case__   :Union[str, Any]       = {"input_ids": input_ids, "token_type_ids": token_type_ids, "attention_mask": input_mask}
					return config, inputs_dict




@require_torch
class        _snake_case    (      __lowerCAmelCase							,     __lowerCAmelCase							,     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  lowerCAmelCase_				(		self							)    ->     str:
					snake_case__   :Any       = NystromformerModelTester(self							)
					snake_case__   :Optional[Any]       = ConfigTester(self       ,config_class=_SCREAMING_SNAKE_CASE       ,hidden_size=37							)






	def  lowerCAmelCase_				(		self							)    ->     Dict:
					self.config_tester.run_common_tests()






	def  lowerCAmelCase_				(		self							)    ->     str:
					snake_case__   :Any       = self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_model(*_SCREAMING_SNAKE_CASE							)






	def  lowerCAmelCase_				(		self							)    ->     Dict:
					snake_case__   :Union[str, Any]       = self.model_tester.prepare_config_and_inputs()
					for type in ["absolute", "relative_key", "relative_key_query"]:
									snake_case__   :str       = type
									self.model_tester.create_and_check_model(*_SCREAMING_SNAKE_CASE							)






	def  lowerCAmelCase_				(		self							)    ->     str:
					snake_case__   :Any       = self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_for_masked_lm(*_SCREAMING_SNAKE_CASE							)






	def  lowerCAmelCase_				(		self							)    ->     int:
					snake_case__   :str       = self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_for_multiple_choice(*_SCREAMING_SNAKE_CASE							)






	def  lowerCAmelCase_				(		self							)    ->     Union[str, Any]:
					snake_case__   :Optional[int]       = self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_for_question_answering(*_SCREAMING_SNAKE_CASE							)






	def  lowerCAmelCase_				(		self							)    ->     List[Any]:
					snake_case__   :Union[str, Any]       = self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_for_sequence_classification(*_SCREAMING_SNAKE_CASE							)






	def  lowerCAmelCase_				(		self							)    ->     Dict:
					snake_case__   :str       = self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_for_token_classification(*_SCREAMING_SNAKE_CASE							)






	@slow
	def  lowerCAmelCase_				(		self							)    ->     Optional[Any]:
					for model_name in NYSTROMFORMER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
									snake_case__   :Optional[Any]       = NystromformerModel.from_pretrained(_SCREAMING_SNAKE_CASE							)
									self.assertIsNotNone(_SCREAMING_SNAKE_CASE							)




@require_torch
class        _snake_case    (      unittest.TestCase			):






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

					with torch.no_grad():
									snake_case__   :List[str]       = model(_SCREAMING_SNAKE_CASE							)[0]

					snake_case__   :Optional[Any]       = torch.Size((1, 6, 768)							)
					self.assertEqual(output.shape       ,_SCREAMING_SNAKE_CASE							)

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






	@slow
	def  lowerCAmelCase_				(		self							)    ->     str:
					snake_case__   :int       = "the [MASK] of Belgium is Brussels"

					snake_case__   :Any       = AutoTokenizer.from_pretrained("uw-madison/nystromformer-512"							)
					snake_case__   :str       = NystromformerForMaskedLM.from_pretrained("uw-madison/nystromformer-512"							)

					snake_case__   :str       = tokenizer(_SCREAMING_SNAKE_CASE       ,return_tensors="pt"							)

					with torch.no_grad():
									snake_case__   :List[str]       = model(encoding.input_ids							).logits

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

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





'''simple docstring'''





from typing import Optional
from urllib.parse import quote

import huggingface_hub as hfh
from packaging import version





def       __a      (  _UpperCamelCase:       str   ,		_UpperCamelCase:       str   ,		_UpperCamelCase:       Optional[str] = None       )		->					str:

     """simple docstring"""




     if version.parse(hfh.__version__       ).release < version.parse("0.11.0"       ).release:
          # old versions of hfh don't url-encode the file path
          _snake_case				= quote(_UpperCamelCase       )
     return hfh.hf_hub_url(_UpperCamelCase   ,		_UpperCamelCase   ,		repo_type="dataset"   ,		revision=_UpperCamelCase       )


