

"""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  lowerCamelCase    :

			def __init__(       self					:    Optional[int]     ,					__UpperCAmelCase					:    str     ,					__UpperCAmelCase					:    Optional[int]=1_3     ,					__UpperCAmelCase					:    Any=7     ,					__UpperCAmelCase					:    List[Any]=True     ,					__UpperCAmelCase					:    Tuple=True     ,					__UpperCAmelCase					:    Dict=True     ,					__UpperCAmelCase					:    str=True     ,					__UpperCAmelCase					:    int=9_9     ,					__UpperCAmelCase					:    int=3_2     ,					__UpperCAmelCase					:    List[str]=5     ,					__UpperCAmelCase					:    Optional[Any]=4     ,					__UpperCAmelCase					:    Optional[int]=3_7     ,					__UpperCAmelCase					:    Tuple="gelu"     ,					__UpperCAmelCase					:    Optional[int]=0.1     ,					__UpperCAmelCase					:    Optional[Any]=0.1     ,					__UpperCAmelCase					:    int=5_1_2     ,					__UpperCAmelCase					:    List[Any]=1_6     ,					__UpperCAmelCase					:    Tuple=2     ,					__UpperCAmelCase					:    Optional[int]=0.02     ,					__UpperCAmelCase					:    str=3     ,					__UpperCAmelCase					:    Optional[Any]=4     ,					__UpperCAmelCase					:    Union[str, Any]=None     ,					) ->     Optional[Any]:
						SCREAMING_SNAKE_CASE__										=   parent
						SCREAMING_SNAKE_CASE__										=   batch_size
						SCREAMING_SNAKE_CASE__										=   seq_length
						SCREAMING_SNAKE_CASE__										=   is_training
						SCREAMING_SNAKE_CASE__										=   use_input_mask
						SCREAMING_SNAKE_CASE__										=   use_token_type_ids
						SCREAMING_SNAKE_CASE__										=   use_labels
						SCREAMING_SNAKE_CASE__										=   vocab_size
						SCREAMING_SNAKE_CASE__										=   hidden_size
						SCREAMING_SNAKE_CASE__										=   num_hidden_layers
						SCREAMING_SNAKE_CASE__										=   num_attention_heads
						SCREAMING_SNAKE_CASE__										=   intermediate_size
						SCREAMING_SNAKE_CASE__										=   hidden_act
						SCREAMING_SNAKE_CASE__										=   hidden_dropout_prob
						SCREAMING_SNAKE_CASE__										=   attention_probs_dropout_prob
						SCREAMING_SNAKE_CASE__										=   max_position_embeddings
						SCREAMING_SNAKE_CASE__										=   type_vocab_size
						SCREAMING_SNAKE_CASE__										=   type_sequence_label_size
						SCREAMING_SNAKE_CASE__										=   initializer_range
						SCREAMING_SNAKE_CASE__										=   num_labels
						SCREAMING_SNAKE_CASE__										=   num_choices
						SCREAMING_SNAKE_CASE__										=   scope

			def        SCREAMING_SNAKE_CASE					(       self					:    int							) ->     List[Any]:
						SCREAMING_SNAKE_CASE__										=   ids_tensor([self.batch_size, self.seq_length]     ,					self.vocab_size							)

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

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

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

						SCREAMING_SNAKE_CASE__										=   self.get_config()

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

			def        SCREAMING_SNAKE_CASE					(       self					:    Optional[Any]							) ->     List[Any]:
						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=__UpperCAmelCase     ,					initializer_range=self.initializer_range     ,					)

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

			def        SCREAMING_SNAKE_CASE					(       self					:    Optional[int]     ,					__UpperCAmelCase					:    Dict     ,					__UpperCAmelCase					:    List[Any]     ,					__UpperCAmelCase					:    int     ,					__UpperCAmelCase					:    Optional[int]     ,					__UpperCAmelCase					:    Union[str, Any]     ,					__UpperCAmelCase					:    int     ,					__UpperCAmelCase					:    Optional[int]							) ->     str:
						SCREAMING_SNAKE_CASE__										=   NystromformerForMaskedLM(config=__UpperCAmelCase							)
						model.to(__UpperCAmelCase							)
						model.eval()
						SCREAMING_SNAKE_CASE__										=   model(__UpperCAmelCase     ,					attention_mask=__UpperCAmelCase     ,					token_type_ids=__UpperCAmelCase     ,					labels=__UpperCAmelCase							)
						self.parent.assertEqual(result.logits.shape     ,					(self.batch_size, self.seq_length, self.vocab_size)							)

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

			def        SCREAMING_SNAKE_CASE					(       self					:    Optional[Any]     ,					__UpperCAmelCase					:    Optional[int]     ,					__UpperCAmelCase					:    Any     ,					__UpperCAmelCase					:    Optional[Any]     ,					__UpperCAmelCase					:    int     ,					__UpperCAmelCase					:    List[str]     ,					__UpperCAmelCase					:    int     ,					__UpperCAmelCase					:    List[str]							) ->     str:
						SCREAMING_SNAKE_CASE__										=   self.num_labels
						SCREAMING_SNAKE_CASE__										=   NystromformerForTokenClassification(config=__UpperCAmelCase							)
						model.to(__UpperCAmelCase							)
						model.eval()
						SCREAMING_SNAKE_CASE__										=   model(__UpperCAmelCase     ,					attention_mask=__UpperCAmelCase     ,					token_type_ids=__UpperCAmelCase     ,					labels=__UpperCAmelCase							)
						self.parent.assertEqual(result.logits.shape     ,					(self.batch_size, self.seq_length, self.num_labels)							)

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





			def        SCREAMING_SNAKE_CASE					(       self					:    Dict							) ->     List[Any]:
						SCREAMING_SNAKE_CASE__										=   self.prepare_config_and_inputs()
						(
						    (
						    SCREAMING_SNAKE_CASE__
						)				,		(
						    SCREAMING_SNAKE_CASE__
						)				,		(
						    SCREAMING_SNAKE_CASE__
						)				,		(
						    SCREAMING_SNAKE_CASE__
						)				,		(
						    SCREAMING_SNAKE_CASE__
						)				,		(
						    SCREAMING_SNAKE_CASE__
						)				,		(
						    SCREAMING_SNAKE_CASE__
						)				,		
						)										=   config_and_inputs
						SCREAMING_SNAKE_CASE__										=   {"""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      ):
			lowerCamelCase__ :		List[str]       		=				(
			    (
			        NystromformerModel,
			        NystromformerForMaskedLM,
			        NystromformerForMultipleChoice,
			        NystromformerForQuestionAnswering,
			        NystromformerForSequenceClassification,
			        NystromformerForTokenClassification,
			    )
			    if is_torch_available()
			    else ()
			)
			lowerCamelCase__ :		List[str]       		=				(
			    {
			        'feature-extraction': NystromformerModel,
			        'fill-mask': NystromformerForMaskedLM,
			        'question-answering': NystromformerForQuestionAnswering,
			        'text-classification': NystromformerForSequenceClassification,
			        'token-classification': NystromformerForTokenClassification,
			        'zero-shot': NystromformerForSequenceClassification,
			    }
			    if is_torch_available()
			    else {}
			)
			lowerCamelCase__ :		List[Any]       		=				False
			lowerCamelCase__ :		Optional[Any]       		=				False

			def        SCREAMING_SNAKE_CASE					(       self					:    str							) ->     Any:
						SCREAMING_SNAKE_CASE__										=   NystromformerModelTester(self							)
						SCREAMING_SNAKE_CASE__										=   ConfigTester(self     ,					config_class=__UpperCAmelCase     ,					hidden_size=3_7							)

			def        SCREAMING_SNAKE_CASE					(       self					:    Dict							) ->     List[str]:
						self.config_tester.run_common_tests()

			def        SCREAMING_SNAKE_CASE					(       self					:    List[Any]							) ->     Any:
						SCREAMING_SNAKE_CASE__										=   self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_model(*__UpperCAmelCase							)

			def        SCREAMING_SNAKE_CASE					(       self					:    List[Any]							) ->     Any:
						SCREAMING_SNAKE_CASE__										=   self.model_tester.prepare_config_and_inputs()
						for type in ["absolute", "relative_key", "relative_key_query"]:
									SCREAMING_SNAKE_CASE__										=   type
									self.model_tester.create_and_check_model(*__UpperCAmelCase							)

			def        SCREAMING_SNAKE_CASE					(       self					:    Union[str, Any]							) ->     Optional[Any]:
						SCREAMING_SNAKE_CASE__										=   self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_for_masked_lm(*__UpperCAmelCase							)

			def        SCREAMING_SNAKE_CASE					(       self					:    List[str]							) ->     Union[str, Any]:
						SCREAMING_SNAKE_CASE__										=   self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_for_multiple_choice(*__UpperCAmelCase							)

			def        SCREAMING_SNAKE_CASE					(       self					:    Any							) ->     Dict:
						SCREAMING_SNAKE_CASE__										=   self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_for_question_answering(*__UpperCAmelCase							)

			def        SCREAMING_SNAKE_CASE					(       self					:    Optional[Any]							) ->     str:
						SCREAMING_SNAKE_CASE__										=   self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_for_sequence_classification(*__UpperCAmelCase							)

			def        SCREAMING_SNAKE_CASE					(       self					:    Tuple							) ->     List[str]:
						SCREAMING_SNAKE_CASE__										=   self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_for_token_classification(*__UpperCAmelCase							)





			@slow
			def        SCREAMING_SNAKE_CASE					(       self					:    Dict							) ->     Tuple:
						for model_name in NYSTROMFORMER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
									SCREAMING_SNAKE_CASE__										=   NystromformerModel.from_pretrained(__UpperCAmelCase							)
									self.assertIsNotNone(__UpperCAmelCase							)





@require_torch
class  lowerCamelCase    (unittest.TestCase      ):

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

						with torch.no_grad():
									SCREAMING_SNAKE_CASE__										=   model(__UpperCAmelCase							)[0]

						SCREAMING_SNAKE_CASE__										=   torch.Size((1, 6, 7_6_8)							)
						self.assertEqual(output.shape     ,					__UpperCAmelCase							)

						SCREAMING_SNAKE_CASE__										=   torch.tensor(
						    [[[-0.4_532, -0.0_936, 0.5_137], [-0.2_676, 0.0_628, 0.6_186], [-0.3_629, -0.1_726, 0.4_716]]]							)

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





			@slow
			def        SCREAMING_SNAKE_CASE					(       self					:    Optional[int]							) ->     Optional[Any]:
						SCREAMING_SNAKE_CASE__										=   """the [MASK] of Belgium is Brussels"""

						SCREAMING_SNAKE_CASE__										=   AutoTokenizer.from_pretrained("""uw-madison/nystromformer-512"""							)
						SCREAMING_SNAKE_CASE__										=   NystromformerForMaskedLM.from_pretrained("""uw-madison/nystromformer-512"""							)

						SCREAMING_SNAKE_CASE__										=   tokenizer(__UpperCAmelCase     ,					return_tensors="""pt"""							)

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

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

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





"""simple docstring"""




import os
import unittest

from transformers.models.phobert.tokenization_phobert import VOCAB_FILES_NAMES, PhobertTokenizer

from ...test_tokenization_common import TokenizerTesterMixin





class  lowerCamelCase    (A__      ,unittest.TestCase      ):
			lowerCamelCase__ :		List[str]       		=				PhobertTokenizer
			lowerCamelCase__ :		Union[str, Any]       		=				False

			def        SCREAMING_SNAKE_CASE					(       self					:    int							) ->     Any:
						super().setUp()

						# Adapted from Sennrich et al. 2015 and https://github.com/rsennrich/subword-nmt
						SCREAMING_SNAKE_CASE__										=   ["""T@@""", """i""", """I""", """R@@""", """r""", """e@@"""]
						SCREAMING_SNAKE_CASE__										=   dict(zip(__UpperCAmelCase     ,					range(len(__UpperCAmelCase							)							)							)							)
						SCREAMING_SNAKE_CASE__										=   ["""#version: 0.2""", """l à</w>"""]
						SCREAMING_SNAKE_CASE__										=   {"""unk_token""": """<unk>"""}

						SCREAMING_SNAKE_CASE__										=   os.path.join(self.tmpdirname     ,					VOCAB_FILES_NAMES["""vocab_file"""]							)
						SCREAMING_SNAKE_CASE__										=   os.path.join(self.tmpdirname     ,					VOCAB_FILES_NAMES["""merges_file"""]							)

						with open(self.vocab_file     ,					"""w"""     ,					encoding="""utf-8"""							) as fp:
									for token in vocab_tokens:
												fp.write(F"""{token} {vocab_tokens[token]}\n"""							)
						with open(self.merges_file     ,					"""w"""     ,					encoding="""utf-8"""							) as fp:
									fp.write("""\n""".join(__UpperCAmelCase							)							)

			def        SCREAMING_SNAKE_CASE					(       self					:    List[Any]     ,					**__UpperCAmelCase					:    int							) ->     Optional[int]:
						kwargs.update(self.special_tokens_map							)
						return PhobertTokenizer.from_pretrained(self.tmpdirname     ,					**__UpperCAmelCase							)

			def        SCREAMING_SNAKE_CASE					(       self					:    Dict     ,					__UpperCAmelCase					:    int							) ->     Optional[Any]:
						SCREAMING_SNAKE_CASE__										=   """Tôi là VinAI Research"""
						SCREAMING_SNAKE_CASE__										=   """T<unk> i <unk> <unk> <unk> <unk> <unk> <unk> I Re<unk> e<unk> <unk> <unk> <unk>"""
						return input_text, output_text





			def        SCREAMING_SNAKE_CASE					(       self					:    Optional[int]							) ->     int:
						SCREAMING_SNAKE_CASE__										=   PhobertTokenizer(self.vocab_file     ,					self.merges_file     ,					**self.special_tokens_map							)
						SCREAMING_SNAKE_CASE__										=   """Tôi là VinAI Research"""
						SCREAMING_SNAKE_CASE__										=   """T@@ ô@@ i l@@ à V@@ i@@ n@@ A@@ I R@@ e@@ s@@ e@@ a@@ r@@ c@@ h""".split()
						SCREAMING_SNAKE_CASE__										=   tokenizer.tokenize(__UpperCAmelCase							)
						print(__UpperCAmelCase							)
						self.assertListEqual(__UpperCAmelCase     ,					__UpperCAmelCase							)

						SCREAMING_SNAKE_CASE__										=   tokens + [tokenizer.unk_token]

						SCREAMING_SNAKE_CASE__										=   [4, 3, 5, 3, 3, 3, 3, 3, 3, 6, 7, 9, 3, 9, 3, 3, 3, 3, 3]
						self.assertListEqual(tokenizer.convert_tokens_to_ids(__UpperCAmelCase							)     ,					__UpperCAmelCase							)


