





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







     def __init__( self    ,   A__    ,   A__=13    ,   A__=7    ,   A__=True    ,   A__=True    ,   A__=True    ,   A__=True    ,   A__=99    ,   A__=32    ,   A__=5    ,   A__=4    ,   A__=37    ,   A__="gelu"    ,   A__=0.1    ,   A__=0.1    ,   A__=5_12    ,   A__=16    ,   A__=2    ,   A__=0.0_2    ,   A__=3    ,   A__=4    ,   A__=None    ,   ) ->	Optional[Any]:
      snake_case				=			parent
      snake_case				=			batch_size
      snake_case				=			seq_length
      snake_case				=			is_training
      snake_case				=			use_input_mask
      snake_case				=			use_token_type_ids
      snake_case				=			use_labels
      snake_case				=			vocab_size
      snake_case				=			hidden_size
      snake_case				=			num_hidden_layers
      snake_case				=			num_attention_heads
      snake_case				=			intermediate_size
      snake_case				=			hidden_act
      snake_case				=			hidden_dropout_prob
      snake_case				=			attention_probs_dropout_prob
      snake_case				=			max_position_embeddings
      snake_case				=			type_vocab_size
      snake_case				=			type_sequence_label_size
      snake_case				=			initializer_range
      snake_case				=			num_labels
      snake_case				=			num_choices
      snake_case				=			scope







     def     UpperCamelCase			( self    ) ->	Optional[int]:
      snake_case				=			ids_tensor([self.batch_size, self.seq_length]    ,   self.vocab_size    )

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

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

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

      snake_case				=			self.get_config()

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







     def     UpperCamelCase			( self    ) ->	str:
      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     UpperCamelCase			( self    ,   A__    ,   A__    ,   A__    ,   A__    ,   A__    ,   A__    ,   A__    ) ->	str:
      snake_case				=			NystromformerModel(config=A__    )
      model.to(A__    )
      model.eval()
      snake_case				=			model(A__    ,   attention_mask=A__    ,   token_type_ids=A__    )
      snake_case				=			model(A__    ,   token_type_ids=A__    )
      snake_case				=			model(A__    )
      self.parent.assertEqual(result.last_hidden_state.shape    ,   (self.batch_size, self.seq_length, self.hidden_size)    )







     def     UpperCamelCase			( self    ,   A__    ,   A__    ,   A__    ,   A__    ,   A__    ,   A__    ,   A__    ) ->	Optional[int]:
      snake_case				=			NystromformerForMaskedLM(config=A__    )
      model.to(A__    )
      model.eval()
      snake_case				=			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     UpperCamelCase			( self    ,   A__    ,   A__    ,   A__    ,   A__    ,   A__    ,   A__    ,   A__    ) ->	List[Any]:
      snake_case				=			NystromformerForQuestionAnswering(config=A__    )
      model.to(A__    )
      model.eval()
      snake_case				=			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     UpperCamelCase			( self    ,   A__    ,   A__    ,   A__    ,   A__    ,   A__    ,   A__    ,   A__    ) ->	Dict:
      snake_case				=			self.num_labels
      snake_case				=			NystromformerForSequenceClassification(A__    )
      model.to(A__    )
      model.eval()
      snake_case				=			model(A__    ,   attention_mask=A__    ,   token_type_ids=A__    ,   labels=A__    )
      self.parent.assertEqual(result.logits.shape    ,   (self.batch_size, self.num_labels)    )







     def     UpperCamelCase			( self    ,   A__    ,   A__    ,   A__    ,   A__    ,   A__    ,   A__    ,   A__    ) ->	Any:
      snake_case				=			self.num_labels
      snake_case				=			NystromformerForTokenClassification(config=A__    )
      model.to(A__    )
      model.eval()
      snake_case				=			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     UpperCamelCase			( self    ,   A__    ,   A__    ,   A__    ,   A__    ,   A__    ,   A__    ,   A__    ) ->	int:
      snake_case				=			self.num_choices
      snake_case				=			NystromformerForMultipleChoice(config=A__    )
      model.to(A__    )
      model.eval()
      snake_case				=			input_ids.unsqueeze(1    ).expand(-1    ,   self.num_choices    ,   -1    ).contiguous()
      snake_case				=			token_type_ids.unsqueeze(1    ).expand(-1    ,   self.num_choices    ,   -1    ).contiguous()
      snake_case				=			input_mask.unsqueeze(1    ).expand(-1    ,   self.num_choices    ,   -1    ).contiguous()
      snake_case				=			model(
          A__    ,   attention_mask=A__    ,   token_type_ids=A__    ,   labels=A__    ,   )
      self.parent.assertEqual(result.logits.shape    ,   (self.batch_size, self.num_choices)    )







     def     UpperCamelCase			( self    ) ->	List[Any]:
      snake_case				=			self.prepare_config_and_inputs()
      (
          (
          snake_case
      )				,		(
          snake_case
      )				,		(
          snake_case
      )				,		(
          snake_case
      )				,		(
          snake_case
      )				,		(
          snake_case
      )				,		(
          snake_case
      )				,		
      )				=			config_and_inputs
      snake_case				=			{'''input_ids''': input_ids, '''token_type_ids''': token_type_ids, '''attention_mask''': input_mask}
      return config, inputs_dict




@require_torch
class 			_lowercase						(       __a				,		__a				,		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     UpperCamelCase			( self    ) ->	Union[str, Any]:
      snake_case				=			NystromformerModelTester(self    )
      snake_case				=			ConfigTester(self    ,   config_class=A__    ,   hidden_size=37    )







     def     UpperCamelCase			( self    ) ->	Any:
      self.config_tester.run_common_tests()







     def     UpperCamelCase			( self    ) ->	Union[str, Any]:
      snake_case				=			self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_model(*A__    )







     def     UpperCamelCase			( self    ) ->	str:
      snake_case				=			self.model_tester.prepare_config_and_inputs()
      for type in ["absolute", "relative_key", "relative_key_query"]:
       snake_case				=			type
       self.model_tester.create_and_check_model(*A__    )







     def     UpperCamelCase			( self    ) ->	List[Any]:
      snake_case				=			self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_for_masked_lm(*A__    )







     def     UpperCamelCase			( self    ) ->	Optional[Any]:
      snake_case				=			self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_for_multiple_choice(*A__    )







     def     UpperCamelCase			( self    ) ->	Tuple:
      snake_case				=			self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_for_question_answering(*A__    )







     def     UpperCamelCase			( self    ) ->	Optional[Any]:
      snake_case				=			self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_for_sequence_classification(*A__    )







     def     UpperCamelCase			( self    ) ->	Dict:
      snake_case				=			self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_for_token_classification(*A__    )







     @slow
     def     UpperCamelCase			( self    ) ->	Tuple:
      for model_name in NYSTROMFORMER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
       snake_case				=			NystromformerModel.from_pretrained(A__    )
       self.assertIsNotNone(A__    )




@require_torch
class 			_lowercase						(       unittest.TestCase					):







     @slow
     def     UpperCamelCase			( self    ) ->	List[str]:
      snake_case				=			NystromformerModel.from_pretrained('''uw-madison/nystromformer-512'''    )
      snake_case				=			torch.tensor([[0, 1, 2, 3, 4, 5]]    )

      with torch.no_grad():
       snake_case				=			model(A__    )[0]

      snake_case				=			torch.Size((1, 6, 7_68)    )
      self.assertEqual(output.shape    ,   A__    )

      snake_case				=			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]    ,   A__    ,   atol=1e-4    )    )







     @slow
     def     UpperCamelCase			( self    ) ->	Union[str, Any]:
      snake_case				=			'''the [MASK] of Belgium is Brussels'''

      snake_case				=			AutoTokenizer.from_pretrained('''uw-madison/nystromformer-512'''    )
      snake_case				=			NystromformerForMaskedLM.from_pretrained('''uw-madison/nystromformer-512'''    )

      snake_case				=			tokenizer(A__    ,   return_tensors='''pt'''    )

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

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

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








'''simple docstring'''







_lowercase       						=			{
    'Pillow': 'Pillow',
    'accelerate': 'accelerate>=0.11.0',
    'compel': 'compel==0.1.8',
    'black': 'black~=23.1',
    'datasets': 'datasets',
    'filelock': 'filelock',
    'flax': 'flax>=0.4.1',
    'hf-doc-builder': 'hf-doc-builder>=0.3.0',
    'huggingface-hub': 'huggingface-hub>=0.13.2',
    'requests-mock': 'requests-mock==1.10.0',
    'importlib_metadata': 'importlib_metadata',
    'invisible-watermark': 'invisible-watermark',
    'isort': 'isort>=5.5.4',
    'jax': 'jax>=0.2.8,!=0.3.2',
    'jaxlib': 'jaxlib>=0.1.65',
    'Jinja2': 'Jinja2',
    'k-diffusion': 'k-diffusion>=0.0.12',
    'torchsde': 'torchsde',
    'note_seq': 'note_seq',
    'librosa': 'librosa',
    'numpy': 'numpy',
    'omegaconf': 'omegaconf',
    'parameterized': 'parameterized',
    'protobuf': 'protobuf>=3.20.3,<4',
    'pytest': 'pytest',
    'pytest-timeout': 'pytest-timeout',
    'pytest-xdist': 'pytest-xdist',
    'ruff': 'ruff>=0.0.241',
    'safetensors': 'safetensors',
    'sentencepiece': 'sentencepiece>=0.1.91,!=0.1.92',
    'scipy': 'scipy',
    'onnx': 'onnx',
    'regex': 'regex!=2019.12.17',
    'requests': 'requests',
    'tensorboard': 'tensorboard',
    'torch': 'torch>=1.4',
    'torchvision': 'torchvision',
    'transformers': 'transformers>=4.25.1',
    'urllib3': 'urllib3<=2.0.0',
}

