




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


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


    def      lowerCAmelCase_  (					self	:  Optional[int]					):
      _UpperCAmelCase						 =      ids_tensor([self.batch_size, self.seq_length]   ,				self.vocab_size					)

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

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

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

      _UpperCAmelCase						 =      self.get_config()

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


    def      lowerCAmelCase_  (					self	:  List[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      lowerCAmelCase_  (					self	:  str   ,				__lowerCAmelCase	:  List[str]   ,				__lowerCAmelCase	:  List[str]   ,				__lowerCAmelCase	:  Optional[Any]   ,				__lowerCAmelCase	:  Optional[Any]   ,				__lowerCAmelCase	:  List[str]   ,				__lowerCAmelCase	:  List[str]   ,				__lowerCAmelCase	:  List[Any]					):
      _UpperCAmelCase						 =      NystromformerModel(config=_a					)
      model.to(_a					)
      model.eval()
      _UpperCAmelCase						 =      model(_a   ,				attention_mask=_a   ,				token_type_ids=_a					)
      _UpperCAmelCase						 =      model(_a   ,				token_type_ids=_a					)
      _UpperCAmelCase						 =      model(_a					)
      self.parent.assertEqual(result.last_hidden_state.shape   ,				(self.batch_size, self.seq_length, self.hidden_size)					)


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


    def      lowerCAmelCase_  (					self	:  Optional[int]   ,				__lowerCAmelCase	:  List[Any]   ,				__lowerCAmelCase	:  int   ,				__lowerCAmelCase	:  Tuple   ,				__lowerCAmelCase	:  Optional[Any]   ,				__lowerCAmelCase	:  Any   ,				__lowerCAmelCase	:  Union[str, Any]   ,				__lowerCAmelCase	:  Tuple					):
      _UpperCAmelCase						 =      self.num_labels
      _UpperCAmelCase						 =      NystromformerForTokenClassification(config=_a					)
      model.to(_a					)
      model.eval()
      _UpperCAmelCase						 =      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      lowerCAmelCase_  (					self	:  int   ,				__lowerCAmelCase	:  Tuple   ,				__lowerCAmelCase	:  List[Any]   ,				__lowerCAmelCase	:  Optional[Any]   ,				__lowerCAmelCase	:  Any   ,				__lowerCAmelCase	:  Optional[int]   ,				__lowerCAmelCase	:  Tuple   ,				__lowerCAmelCase	:  Any					):
      _UpperCAmelCase						 =      self.num_choices
      _UpperCAmelCase						 =      NystromformerForMultipleChoice(config=_a					)
      model.to(_a					)
      model.eval()
      _UpperCAmelCase						 =      input_ids.unsqueeze(1					).expand(-1   ,				self.num_choices   ,				-1					).contiguous()
      _UpperCAmelCase						 =      token_type_ids.unsqueeze(1					).expand(-1   ,				self.num_choices   ,				-1					).contiguous()
      _UpperCAmelCase						 =      input_mask.unsqueeze(1					).expand(-1   ,				self.num_choices   ,				-1					).contiguous()
      _UpperCAmelCase						 =      model(
          _a   ,				attention_mask=_a   ,				token_type_ids=_a   ,				labels=_a   ,				)
      self.parent.assertEqual(result.logits.shape   ,				(self.batch_size, self.num_choices)					)


    def      lowerCAmelCase_  (					self	:  List[str]					):
      _UpperCAmelCase						 =      self.prepare_config_and_inputs()
      (
          _UpperCAmelCase					
      )						 =      config_and_inputs
      _UpperCAmelCase						 =      {"""input_ids""": input_ids, """token_type_ids""": token_type_ids, """attention_mask""": input_mask}
      return config, inputs_dict



@require_torch
class        a				(		__SCREAMING_SNAKE_CASE	,							__SCREAMING_SNAKE_CASE	,							unittest.TestCase   ):
    _snake_case     :  Optional[int]     							=							(
        (
            NystromformerModel,
            NystromformerForMaskedLM,
            NystromformerForMultipleChoice,
            NystromformerForQuestionAnswering,
            NystromformerForSequenceClassification,
            NystromformerForTokenClassification,
        )
        if is_torch_available()
        else ()
    )
    _snake_case     :  Dict     							=							(
        {
            'feature-extraction': NystromformerModel,
            'fill-mask': NystromformerForMaskedLM,
            'question-answering': NystromformerForQuestionAnswering,
            'text-classification': NystromformerForSequenceClassification,
            'token-classification': NystromformerForTokenClassification,
            'zero-shot': NystromformerForSequenceClassification,
        }
        if is_torch_available()
        else {}
    )
    _snake_case     :  Any     							=							False
    _snake_case     :  Dict     							=							False


    def      lowerCAmelCase_  (					self	:  int					):
      _UpperCAmelCase						 =      NystromformerModelTester(self					)
      _UpperCAmelCase						 =      ConfigTester(self   ,				config_class=_a   ,				hidden_size=37					)


    def      lowerCAmelCase_  (					self	:  List[str]					):
      self.config_tester.run_common_tests()


    def      lowerCAmelCase_  (					self	:  List[Any]					):
      _UpperCAmelCase						 =      self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_model(*_a					)


    def      lowerCAmelCase_  (					self	:  Optional[int]					):
      _UpperCAmelCase						 =      self.model_tester.prepare_config_and_inputs()
      for type in ["absolute", "relative_key", "relative_key_query"]:
        _UpperCAmelCase						 =      type
        self.model_tester.create_and_check_model(*_a					)


    def      lowerCAmelCase_  (					self	:  str					):
      _UpperCAmelCase						 =      self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_for_masked_lm(*_a					)


    def      lowerCAmelCase_  (					self	:  Tuple					):
      _UpperCAmelCase						 =      self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_for_multiple_choice(*_a					)


    def      lowerCAmelCase_  (					self	:  Dict					):
      _UpperCAmelCase						 =      self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_for_question_answering(*_a					)


    def      lowerCAmelCase_  (					self	:  Any					):
      _UpperCAmelCase						 =      self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_for_sequence_classification(*_a					)


    def      lowerCAmelCase_  (					self	:  int					):
      _UpperCAmelCase						 =      self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_for_token_classification(*_a					)


    @slow
    def      lowerCAmelCase_  (					self	:  int					):




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



@require_torch
class        a				(		unittest.TestCase   ):


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

      with torch.no_grad():
        _UpperCAmelCase						 =      model(_a					)[0]

      _UpperCAmelCase						 =      torch.Size((1, 6, 768)					)
      self.assertEqual(output.shape   ,				_a					)

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


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

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

      _UpperCAmelCase						 =      tokenizer(_a   ,				return_tensors="""pt"""					)

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

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

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

"""simple docstring"""


from __future__ import annotations






def     __UpperCAmelCase					(			lowercase		):







  """simple docstring"""
  return [ord(lowercase		) - 96 for elem in plain]






def     __UpperCAmelCase					(			lowercase		):







  """simple docstring"""
  return "".join(chr(elem + 96		) for elem in encoded		)






def     __UpperCAmelCase					(			):







  """simple docstring"""
  _UpperCAmelCase						 =      encode(input("""-> """		).strip().lower()		)
  print("""Encoded: """   ,lowercase		)
  print("""Decoded:"""   ,decode(lowercase		)		)


if __name__ == "__main__":
    main()
