


from typing import Dict, List, Optional

from ...tokenization_utils import AddedToken, PreTrainedTokenizer
from ...utils import logging


__UpperCamelCase				:				Optional[int]  				= logging.get_logger(__name__)


__UpperCamelCase				:				Optional[int]  				= {
    "nielsr/canine-s": 2048,
}

# Unicode defines 1,114,112 total “codepoints”
__UpperCamelCase				:				List[str]  				= 111_4112

# Below: Constants defining canonical codepoints for special, pseudo-characters.
# Copied from https://github.com/google-research/language/blob/master/language/canine/special_codepoints.py
__UpperCamelCase				:				Optional[int]  				= 0

__UpperCamelCase				:				int  				= 0xe_0_0_0
__UpperCamelCase				:				List[Any]  				= 0xe_0_0_1
__UpperCamelCase				:				Dict  				= 0xe_0_0_2
__UpperCamelCase				:				Optional[Any]  				= 0xe_0_0_3
__UpperCamelCase				:				Optional[int]  				= 0xe_0_0_4

# Maps special codepoints to human-readable names.
__UpperCamelCase				:				Dict[int, str]  				= {
    # Special symbols are represented using codepoints values that are valid,
    # but designated as "Private Use", meaning that they will never be assigned
    # characters by the Unicode Consortium, and are thus safe for use here.
    #
    # NOTE: Do *NOT* add any sort of [UNK_CHAR] here. They are explicitly
    # excluded and should fail with a hard error.
    CLS: "[CLS]",
    SEP: "[SEP]",
    BOS: "[BOS]",
    MASK: "[MASK]",
    PAD: "[PAD]",
    RESERVED: "[RESERVED]",
}

# Maps special codepoint human-readable names to their codepoint values.
__UpperCamelCase				:				Dict[str, int]  				= {name: codepoint for codepoint, name in SPECIAL_CODEPOINTS.items()}

class 					__magic_name__		(   _UpperCAmelCase):
     A:						List[Any]								=					PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES


     def __init__(					self							:   str							,		lowerCamelCase__							:   Optional[Any]=chr(lowercase_	)							,		lowerCamelCase__							:   Tuple=chr(lowercase_	)							,		lowerCamelCase__							:   Dict=chr(lowercase_	)							,		lowerCamelCase__							:   Dict=chr(lowercase_	)							,		lowerCamelCase__							:   List[str]=chr(lowercase_	)							,		lowerCamelCase__							:   Union[str, Any]=chr(lowercase_	)							,		lowerCamelCase__							:   Union[str, Any]=False							,		lowerCamelCase__							:   List[str]=2048							,		**lowerCamelCase__							:   str							,		)     ->					List[Any]:





      '''simple docstring'''






      UpperCamelCase__    :			Any      				=      AddedToken(lowercase_							,		lstrip=lowercase_							,		rstrip=lowercase_	) if isinstance(lowercase_							,		lowercase_	) else bos_token
      UpperCamelCase__    :			Dict      				=      AddedToken(lowercase_							,		lstrip=lowercase_							,		rstrip=lowercase_	) if isinstance(lowercase_							,		lowercase_	) else eos_token
      UpperCamelCase__    :			int      				=      AddedToken(lowercase_							,		lstrip=lowercase_							,		rstrip=lowercase_	) if isinstance(lowercase_							,		lowercase_	) else sep_token
      UpperCamelCase__    :			List[str]      				=      AddedToken(lowercase_							,		lstrip=lowercase_							,		rstrip=lowercase_	) if isinstance(lowercase_							,		lowercase_	) else cls_token
      UpperCamelCase__    :			List[Any]      				=      AddedToken(lowercase_							,		lstrip=lowercase_							,		rstrip=lowercase_	) if isinstance(lowercase_							,		lowercase_	) else pad_token

      # Mask token behave like a normal word, i.e. include the space before it
      UpperCamelCase__    :			int      				=      AddedToken(lowercase_							,		lstrip=lowercase_							,		rstrip=lowercase_	) if isinstance(lowercase_							,		lowercase_	) else mask_token

      super().__init__(
          bos_token=lowercase_							,		eos_token=lowercase_							,		sep_token=lowercase_							,		cls_token=lowercase_							,		pad_token=lowercase_							,		mask_token=lowercase_							,		add_prefix_space=lowercase_							,		model_max_length=lowercase_							,		**lowercase_							,		)

      # Creates a mapping for looking up the IDs of special symbols.
      UpperCamelCase__    :			Dict[str, int]      				=      {}
      for codepoint, name in SPECIAL_CODEPOINTS.items():
       UpperCamelCase__    :			List[str]      				=      codepoint

      # Creates a mapping for looking up the string forms of special symbol IDs.
      UpperCamelCase__    :			Dict[int, str]      				=      {
          codepoint: name for name, codepoint in self._special_codepoints.items()
      }

      UpperCamelCase__    :			str      				=      UNICODE_VOCAB_SIZE
      UpperCamelCase__    :			str      				=      len(self._special_codepoints	)


     @property
     def    UpperCAmelCase__		(					self							:   List[str]	)     ->					str:





      '''simple docstring'''






      return self._unicode_vocab_size


     def    UpperCAmelCase__		(					self							:   int							,		lowerCamelCase__							:   str	)     ->					Optional[int]:





      '''simple docstring'''






      return list(lowercase_	)


     def    UpperCAmelCase__		(					self							:   Optional[Any]							,		lowerCamelCase__							:   str	)     ->					Any:





      '''simple docstring'''






      try:
       return ord(lowercase_	)
      except TypeError:
       raise ValueError(F"invalid token: \'{token}\'"	)


     def    UpperCAmelCase__		(					self							:   Optional[Any]							,		lowerCamelCase__							:   int	)     ->					str:





      '''simple docstring'''






      try:
       if index in SPECIAL_CODEPOINTS:
        return SPECIAL_CODEPOINTS[index]
       return chr(lowercase_	)
      except TypeError:
       raise ValueError(F"invalid id: {index}"	)


     def    UpperCAmelCase__		(					self							:   Any							,		lowerCamelCase__							:   str	)     ->					Union[str, Any]:





      '''simple docstring'''






      return "".join(lowercase_	)


     def    UpperCAmelCase__		(					self							:   Optional[int]							,		lowerCamelCase__							:   List[int]							,		lowerCamelCase__							:   Optional[List[int]] = None	)     ->					List[str]:





      '''simple docstring'''






      UpperCamelCase__    :			Optional[Any]      				=      [self.sep_token_id]
      UpperCamelCase__    :			Any      				=      [self.cls_token_id]

      UpperCamelCase__    :			List[Any]      				=      cls + token_ids_a + sep
      if token_ids_a is not None:
       result += token_ids_a + sep
      return result


     def    UpperCAmelCase__		(					self							:   Tuple							,		lowerCamelCase__							:   List[int]							,		lowerCamelCase__							:   Optional[List[int]] = None							,		lowerCamelCase__							:   bool = False	)     ->					Optional[int]:





      '''simple docstring'''






      if already_has_special_tokens:
       return super().get_special_tokens_mask(
           token_ids_a=lowercase_							,		token_ids_a=lowercase_							,		already_has_special_tokens=lowercase_	)

      UpperCamelCase__    :			Optional[int]      				=      [1] + ([0] * len(lowercase_	)) + [1]
      if token_ids_a is not None:
       result += ([0] * len(lowercase_	)) + [1]
      return result


     def    UpperCAmelCase__		(					self							:   Tuple							,		lowerCamelCase__							:   List[int]							,		lowerCamelCase__							:   Optional[List[int]] = None	)     ->					List[Any]:





      '''simple docstring'''






      UpperCamelCase__    :			List[Any]      				=      [self.sep_token_id]
      UpperCamelCase__    :			List[str]      				=      [self.cls_token_id]

      UpperCamelCase__    :			int      				=      len(cls + token_ids_a + sep	) * [0]
      if token_ids_a is not None:
       result += len(token_ids_a + sep	) * [1]
      return result


     def    UpperCAmelCase__		(					self							:   Optional[Any]							,		lowerCamelCase__							:   str							,		lowerCamelCase__							:   Optional[str] = None	)     ->					Optional[int]:





      '''simple docstring'''






      return ()



'''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     __magic_name__						:







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







			def 	SCREAMING_SNAKE_CASE_     (	self				:  Tuple    ):
										lowercase_					:			int										=       ids_tensor([self.batch_size, self.seq_length]				,			self.vocab_size    )

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

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

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

										lowercase_					:			Tuple										=       self.get_config()

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







			def 	SCREAMING_SNAKE_CASE_     (	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=lowercase_				,			initializer_range=self.initializer_range				,			)







			def 	SCREAMING_SNAKE_CASE_     (	self				:  Optional[Any]				,			lowercase_				:  Any				,			lowercase_				:  List[str]				,			lowercase_				:  Union[str, Any]				,			lowercase_				:  Union[str, Any]				,			lowercase_				:  Tuple				,			lowercase_				:  Tuple				,			lowercase_				:  Optional[int]    ):
										lowercase_					:			Optional[Any]										=       NystromformerModel(config=lowercase_    )
										model.to(lowercase_    )
										model.eval()
										lowercase_					:			Optional[int]										=       model(lowercase_				,			attention_mask=lowercase_				,			token_type_ids=lowercase_    )
										lowercase_					:			Optional[Any]										=       model(lowercase_				,			token_type_ids=lowercase_    )
										lowercase_					:			Union[str, Any]										=       model(lowercase_    )
										self.parent.assertEqual(result.last_hidden_state.shape				,			(self.batch_size, self.seq_length, self.hidden_size)    )







			def 	SCREAMING_SNAKE_CASE_     (	self				:  int				,			lowercase_				:  Tuple				,			lowercase_				:  List[Any]				,			lowercase_				:  Union[str, Any]				,			lowercase_				:  List[str]				,			lowercase_				:  List[Any]				,			lowercase_				:  Optional[int]				,			lowercase_				:  Any    ):
										lowercase_					:			List[Any]										=       NystromformerForMaskedLM(config=lowercase_    )
										model.to(lowercase_    )
										model.eval()
										lowercase_					:			Optional[int]										=       model(lowercase_				,			attention_mask=lowercase_				,			token_type_ids=lowercase_				,			labels=lowercase_    )
										self.parent.assertEqual(result.logits.shape				,			(self.batch_size, self.seq_length, self.vocab_size)    )







			def 	SCREAMING_SNAKE_CASE_     (	self				:  Tuple				,			lowercase_				:  int				,			lowercase_				:  List[Any]				,			lowercase_				:  Any				,			lowercase_				:  str				,			lowercase_				:  Dict				,			lowercase_				:  Any				,			lowercase_				:  Tuple    ):
										lowercase_					:			Any										=       NystromformerForQuestionAnswering(config=lowercase_    )
										model.to(lowercase_    )
										model.eval()
										lowercase_					:			Union[str, Any]										=       model(
										    lowercase_				,			attention_mask=lowercase_				,			token_type_ids=lowercase_				,			start_positions=lowercase_				,			end_positions=lowercase_				,			)
										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 	SCREAMING_SNAKE_CASE_     (	self				:  Tuple				,			lowercase_				:  List[str]				,			lowercase_				:  List[Any]				,			lowercase_				:  Union[str, Any]				,			lowercase_				:  str				,			lowercase_				:  Optional[Any]				,			lowercase_				:  str				,			lowercase_				:  int    ):
										lowercase_					:			Any										=       self.num_labels
										lowercase_					:			Union[str, Any]										=       NystromformerForSequenceClassification(lowercase_    )
										model.to(lowercase_    )
										model.eval()
										lowercase_					:			Any										=       model(lowercase_				,			attention_mask=lowercase_				,			token_type_ids=lowercase_				,			labels=lowercase_    )
										self.parent.assertEqual(result.logits.shape				,			(self.batch_size, self.num_labels)    )







			def 	SCREAMING_SNAKE_CASE_     (	self				:  List[Any]				,			lowercase_				:  List[str]				,			lowercase_				:  int				,			lowercase_				:  List[str]				,			lowercase_				:  Dict				,			lowercase_				:  Tuple				,			lowercase_				:  Union[str, Any]				,			lowercase_				:  List[str]    ):
										lowercase_					:			int										=       self.num_labels
										lowercase_					:			int										=       NystromformerForTokenClassification(config=lowercase_    )
										model.to(lowercase_    )
										model.eval()
										lowercase_					:			Tuple										=       model(lowercase_				,			attention_mask=lowercase_				,			token_type_ids=lowercase_				,			labels=lowercase_    )
										self.parent.assertEqual(result.logits.shape				,			(self.batch_size, self.seq_length, self.num_labels)    )







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







			def 	SCREAMING_SNAKE_CASE_     (	self				:  Dict    ):
										lowercase_					:			Union[str, Any]										=       self.prepare_config_and_inputs()
										(
										    (
										    lowercase_
										)						,						(
										    lowercase_
										)						,						(
										    lowercase_
										)						,						(
										    lowercase_
										)						,						(
										    lowercase_
										)						,						(
										    lowercase_
										)						,						(
										    lowercase_
										)						,						
										)					:			Tuple										=       config_and_inputs
										lowercase_					:			Optional[Any]										=       {"""input_ids""": input_ids, """token_type_ids""": token_type_ids, """attention_mask""": input_mask}
										return config, inputs_dict





@require_torch
class     __magic_name__						(    _UpperCAmelCase,     _UpperCAmelCase,     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 	SCREAMING_SNAKE_CASE_     (	self				:  int    ):
										lowercase_					:			Any										=       NystromformerModelTester(self    )
										lowercase_					:			Optional[Any]										=       ConfigTester(self				,			config_class=lowercase_				,			hidden_size=37    )







			def 	SCREAMING_SNAKE_CASE_     (	self				:  Tuple    ):
										self.config_tester.run_common_tests()







			def 	SCREAMING_SNAKE_CASE_     (	self				:  Dict    ):
										lowercase_					:			Dict										=       self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_model(*lowercase_    )







			def 	SCREAMING_SNAKE_CASE_     (	self				:  List[Any]    ):
										lowercase_					:			Union[str, Any]										=       self.model_tester.prepare_config_and_inputs()
										for type in ["absolute", "relative_key", "relative_key_query"]:
																	lowercase_					:			int										=       type
																	self.model_tester.create_and_check_model(*lowercase_    )







			def 	SCREAMING_SNAKE_CASE_     (	self				:  Optional[Any]    ):
										lowercase_					:			Tuple										=       self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_for_masked_lm(*lowercase_    )







			def 	SCREAMING_SNAKE_CASE_     (	self				:  Optional[int]    ):
										lowercase_					:			int										=       self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_for_multiple_choice(*lowercase_    )







			def 	SCREAMING_SNAKE_CASE_     (	self				:  Optional[Any]    ):
										lowercase_					:			Union[str, Any]										=       self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_for_question_answering(*lowercase_    )







			def 	SCREAMING_SNAKE_CASE_     (	self				:  List[Any]    ):
										lowercase_					:			Any										=       self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_for_sequence_classification(*lowercase_    )







			def 	SCREAMING_SNAKE_CASE_     (	self				:  List[Any]    ):
										lowercase_					:			Optional[int]										=       self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_for_token_classification(*lowercase_    )







			@slow
			def 	SCREAMING_SNAKE_CASE_     (	self				:  Optional[int]    ):
										for model_name in NYSTROMFORMER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																	lowercase_					:			List[Any]										=       NystromformerModel.from_pretrained(lowercase_    )
																	self.assertIsNotNone(lowercase_    )





@require_torch
class     __magic_name__						(    unittest.TestCase):







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

										with torch.no_grad():
																	lowercase_					:			Tuple										=       model(lowercase_    )[0]

										lowercase_					:			Tuple										=       torch.Size((1, 6, 768)    )
										self.assertEqual(output.shape				,			lowercase_    )

										lowercase_					:			Dict										=       torch.tensor(
										    [[[-0.45_32, -0.09_36, 0.51_37], [-0.26_76, 0.06_28, 0.61_86], [-0.36_29, -0.17_26, 0.47_16]]]    )

										self.assertTrue(torch.allclose(output[:, :3, :3]				,			lowercase_				,			atol=1E-4    )    )







			@slow
			def 	SCREAMING_SNAKE_CASE_     (	self				:  Optional[int]    ):
										lowercase_					:			Optional[int]										=       """the [MASK] of Belgium is Brussels"""

										lowercase_					:			Optional[Any]										=       AutoTokenizer.from_pretrained("""uw-madison/nystromformer-512"""    )
										lowercase_					:			List[Any]										=       NystromformerForMaskedLM.from_pretrained("""uw-madison/nystromformer-512"""    )

										lowercase_					:			str										=       tokenizer(lowercase_				,			return_tensors="""pt"""    )

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

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

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





