






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


   '''simple docstring'''

   def __init__(	self :     Any,       _lowerCamelCase :     List[str],       _lowerCamelCase :     Optional[Any]=13,       _lowerCamelCase :     Dict=7,       _lowerCamelCase :     Tuple=True,       _lowerCamelCase :     Optional[int]=True,       _lowerCamelCase :     Union[str, Any]=True,       _lowerCamelCase :     Union[str, Any]=True,       _lowerCamelCase :     List[Any]=99,       _lowerCamelCase :     Any=32,       _lowerCamelCase :     Optional[int]=5,       _lowerCamelCase :     List[Any]=4,       _lowerCamelCase :     Dict=37,       _lowerCamelCase :     Union[str, Any]="gelu",       _lowerCamelCase :     str=0.1,       _lowerCamelCase :     Optional[Any]=0.1,       _lowerCamelCase :     Tuple=5_12,       _lowerCamelCase :     List[str]=16,       _lowerCamelCase :     List[str]=2,       _lowerCamelCase :     Dict=0.02,       _lowerCamelCase :     str=3,       _lowerCamelCase :     Dict=4,       _lowerCamelCase :     Union[str, Any]=None,       ):

     '''simple docstring'''
     __A									= parent
     __A									= batch_size
     __A									= seq_length
     __A									= is_training
     __A									= use_input_mask
     __A									= use_token_type_ids
     __A									= use_labels
     __A									= vocab_size
     __A									= hidden_size
     __A									= num_hidden_layers
     __A									= num_attention_heads
     __A									= intermediate_size
     __A									= hidden_act
     __A									= hidden_dropout_prob
     __A									= attention_probs_dropout_prob
     __A									= max_position_embeddings
     __A									= type_vocab_size
     __A									= type_sequence_label_size
     __A									= initializer_range
     __A									= num_labels
     __A									= num_choices
     __A									= scope


   def 				_SCREAMING_SNAKE_CASE				(	self :     Optional[int]  ):

     '''simple docstring'''
     __A									= ids_tensor([self.batch_size, self.seq_length],       self.vocab_size  )

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

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

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

     __A									= self.get_config()

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


   def 				_SCREAMING_SNAKE_CASE				(	self :     List[Any]  ):

     '''simple docstring'''
     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=_lowerCamelCase,       initializer_range=self.initializer_range,       )


   def 				_SCREAMING_SNAKE_CASE				(	self :     Tuple,       _lowerCamelCase :     str,       _lowerCamelCase :     Optional[int],       _lowerCamelCase :     List[str],       _lowerCamelCase :     Optional[Any],       _lowerCamelCase :     str,       _lowerCamelCase :     List[Any],       _lowerCamelCase :     Any  ):

     '''simple docstring'''
     __A									= NystromformerModel(config=_lowerCamelCase  )
     model.to(_lowerCamelCase  )
     model.eval()
     __A									= model(_lowerCamelCase,       attention_mask=_lowerCamelCase,       token_type_ids=_lowerCamelCase  )
     __A									= model(_lowerCamelCase,       token_type_ids=_lowerCamelCase  )
     __A									= model(_lowerCamelCase  )
     self.parent.assertEqual(result.last_hidden_state.shape,       (self.batch_size, self.seq_length, self.hidden_size)  )


   def 				_SCREAMING_SNAKE_CASE				(	self :     Optional[Any],       _lowerCamelCase :     Union[str, Any],       _lowerCamelCase :     Optional[Any],       _lowerCamelCase :     Optional[Any],       _lowerCamelCase :     Any,       _lowerCamelCase :     Dict,       _lowerCamelCase :     Optional[Any],       _lowerCamelCase :     Tuple  ):

     '''simple docstring'''
     __A									= NystromformerForMaskedLM(config=_lowerCamelCase  )
     model.to(_lowerCamelCase  )
     model.eval()
     __A									= model(_lowerCamelCase,       attention_mask=_lowerCamelCase,       token_type_ids=_lowerCamelCase,       labels=_lowerCamelCase  )
     self.parent.assertEqual(result.logits.shape,       (self.batch_size, self.seq_length, self.vocab_size)  )


   def 				_SCREAMING_SNAKE_CASE				(	self :     Dict,       _lowerCamelCase :     Optional[int],       _lowerCamelCase :     List[str],       _lowerCamelCase :     Optional[Any],       _lowerCamelCase :     Optional[int],       _lowerCamelCase :     List[Any],       _lowerCamelCase :     Optional[Any],       _lowerCamelCase :     List[Any]  ):

     '''simple docstring'''
     __A									= NystromformerForQuestionAnswering(config=_lowerCamelCase  )
     model.to(_lowerCamelCase  )
     model.eval()
     __A									= model(
         _lowerCamelCase,       attention_mask=_lowerCamelCase,       token_type_ids=_lowerCamelCase,       start_positions=_lowerCamelCase,       end_positions=_lowerCamelCase,       )
     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 :     List[Any],       _lowerCamelCase :     Optional[int],       _lowerCamelCase :     Union[str, Any],       _lowerCamelCase :     Any,       _lowerCamelCase :     List[Any],       _lowerCamelCase :     Any,       _lowerCamelCase :     Union[str, Any],       _lowerCamelCase :     Optional[int]  ):

     '''simple docstring'''
     __A									= self.num_labels
     __A									= NystromformerForSequenceClassification(_lowerCamelCase  )
     model.to(_lowerCamelCase  )
     model.eval()
     __A									= model(_lowerCamelCase,       attention_mask=_lowerCamelCase,       token_type_ids=_lowerCamelCase,       labels=_lowerCamelCase  )
     self.parent.assertEqual(result.logits.shape,       (self.batch_size, self.num_labels)  )


   def 				_SCREAMING_SNAKE_CASE				(	self :     Union[str, Any],       _lowerCamelCase :     str,       _lowerCamelCase :     List[str],       _lowerCamelCase :     str,       _lowerCamelCase :     Dict,       _lowerCamelCase :     Any,       _lowerCamelCase :     Any,       _lowerCamelCase :     Any  ):

     '''simple docstring'''
     __A									= self.num_labels
     __A									= NystromformerForTokenClassification(config=_lowerCamelCase  )
     model.to(_lowerCamelCase  )
     model.eval()
     __A									= model(_lowerCamelCase,       attention_mask=_lowerCamelCase,       token_type_ids=_lowerCamelCase,       labels=_lowerCamelCase  )
     self.parent.assertEqual(result.logits.shape,       (self.batch_size, self.seq_length, self.num_labels)  )


   def 				_SCREAMING_SNAKE_CASE				(	self :     Optional[Any],       _lowerCamelCase :     Union[str, Any],       _lowerCamelCase :     str,       _lowerCamelCase :     Optional[int],       _lowerCamelCase :     Tuple,       _lowerCamelCase :     List[str],       _lowerCamelCase :     List[Any],       _lowerCamelCase :     Optional[int]  ):

     '''simple docstring'''
     __A									= self.num_choices
     __A									= NystromformerForMultipleChoice(config=_lowerCamelCase  )
     model.to(_lowerCamelCase  )
     model.eval()
     __A									= input_ids.unsqueeze(1  ).expand(-1,       self.num_choices,       -1  ).contiguous()
     __A									= token_type_ids.unsqueeze(1  ).expand(-1,       self.num_choices,       -1  ).contiguous()
     __A									= input_mask.unsqueeze(1  ).expand(-1,       self.num_choices,       -1  ).contiguous()
     __A									= model(
         _lowerCamelCase,       attention_mask=_lowerCamelCase,       token_type_ids=_lowerCamelCase,       labels=_lowerCamelCase,       )
     self.parent.assertEqual(result.logits.shape,       (self.batch_size, self.num_choices)  )






   def 				_SCREAMING_SNAKE_CASE				(	self :     List[str]  ):

     '''simple docstring'''
     __A									= self.prepare_config_and_inputs()
     (
         (
         __A
     )			,    (
         __A
     )			,    (
         __A
     )			,    (
         __A
     )			,    (
         __A
     )			,    (
         __A
     )			,    (
         __A
     )			,    
     )									= config_and_inputs
     __A									= {'''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						):


   '''simple docstring'''

   A_						:	List[Any]						 = (
       (
           NystromformerModel,
           NystromformerForMaskedLM,
           NystromformerForMultipleChoice,
           NystromformerForQuestionAnswering,
           NystromformerForSequenceClassification,
           NystromformerForTokenClassification,
       )
       if is_torch_available()
       else ()
   )
   A_						:	Union[str, Any]						 = (
       {
           "feature-extraction": NystromformerModel,
           "fill-mask": NystromformerForMaskedLM,
           "question-answering": NystromformerForQuestionAnswering,
           "text-classification": NystromformerForSequenceClassification,
           "token-classification": NystromformerForTokenClassification,
           "zero-shot": NystromformerForSequenceClassification,
       }
       if is_torch_available()
       else {}
   )
   A_						:	Any						 = False
   A_						:	Optional[int]						 = False


   def 				_SCREAMING_SNAKE_CASE				(	self :     Optional[Any]  ):

     '''simple docstring'''
     __A									= NystromformerModelTester(self  )
     __A									= ConfigTester(self,       config_class=_lowerCamelCase,       hidden_size=37  )


   def 				_SCREAMING_SNAKE_CASE				(	self :     List[str]  ):

     '''simple docstring'''
     self.config_tester.run_common_tests()


   def 				_SCREAMING_SNAKE_CASE				(	self :     Tuple  ):

     '''simple docstring'''
     __A									= self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_model(*_lowerCamelCase  )


   def 				_SCREAMING_SNAKE_CASE				(	self :     str  ):

     '''simple docstring'''
     __A									= self.model_tester.prepare_config_and_inputs()
     for type in ["absolute", "relative_key", "relative_key_query"]:
       __A									= type
       self.model_tester.create_and_check_model(*_lowerCamelCase  )


   def 				_SCREAMING_SNAKE_CASE				(	self :     Dict  ):

     '''simple docstring'''
     __A									= self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_for_masked_lm(*_lowerCamelCase  )


   def 				_SCREAMING_SNAKE_CASE				(	self :     Union[str, Any]  ):

     '''simple docstring'''
     __A									= self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_for_multiple_choice(*_lowerCamelCase  )


   def 				_SCREAMING_SNAKE_CASE				(	self :     Union[str, Any]  ):

     '''simple docstring'''
     __A									= self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_for_question_answering(*_lowerCamelCase  )


   def 				_SCREAMING_SNAKE_CASE				(	self :     Union[str, Any]  ):

     '''simple docstring'''
     __A									= self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_for_sequence_classification(*_lowerCamelCase  )


   def 				_SCREAMING_SNAKE_CASE				(	self :     Optional[Any]  ):

     '''simple docstring'''
     __A									= self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_for_token_classification(*_lowerCamelCase  )






   @slow
   def 				_SCREAMING_SNAKE_CASE				(	self :     Dict  ):

     '''simple docstring'''
     for model_name in NYSTROMFORMER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
       __A									= NystromformerModel.from_pretrained(_lowerCamelCase  )
       self.assertIsNotNone(_lowerCamelCase  )





@require_torch
class 					snake_case		(							unittest.TestCase						):


   '''simple docstring'''

   @slow
   def 				_SCREAMING_SNAKE_CASE				(	self :     List[Any]  ):

     '''simple docstring'''
     __A									= NystromformerModel.from_pretrained('''uw-madison/nystromformer-512'''  )
     __A									= torch.tensor([[0, 1, 2, 3, 4, 5]]  )

     with torch.no_grad():
       __A									= model(_lowerCamelCase  )[0]

     __A									= torch.Size((1, 6, 7_68)  )
     self.assertEqual(output.shape,       _lowerCamelCase  )

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






   @slow
   def 				_SCREAMING_SNAKE_CASE				(	self :     str  ):

     '''simple docstring'''
     __A									= '''the [MASK] of Belgium is Brussels'''

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

     __A									= tokenizer(_lowerCamelCase,       return_tensors='''pt'''  )

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

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

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












"""simple docstring"""


from __future__ import annotations

from typing import Any



def 					lowerCAmelCase				(    __UpperCamelCase ):




  """simple docstring"""







  if not postfix_notation:
    return 0

  __A									= {'''+''', '''-''', '''*''', '''/'''}
  __A									= []

  for token in postfix_notation:
    if token in operations:
      __A			,    __A									= stack.pop(), stack.pop()
      if token == "+":
        stack.append(a + b )
      elif token == "-":
        stack.append(a - b )
      elif token == "*":
        stack.append(a * b )
      else:
        if a * b < 0 and a % b != 0:
          stack.append(a // b + 1 )
        else:
          stack.append(a // b )
    else:
      stack.append(int(__UpperCamelCase ) )

  return stack.pop()


if __name__ == "__main__":
       import doctest

       doctest.testmod()




