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

     """simple docstring"""

     def __init__(      self							,		lowerCAmelCase_							,		lowerCAmelCase_=13							,		lowerCAmelCase_=7							,		lowerCAmelCase_=True							,		lowerCAmelCase_=True							,		lowerCAmelCase_=True							,		lowerCAmelCase_=True							,		lowerCAmelCase_=99							,		lowerCAmelCase_=32							,		lowerCAmelCase_=5							,		lowerCAmelCase_=4							,		lowerCAmelCase_=37							,		lowerCAmelCase_="gelu"							,		lowerCAmelCase_=0.1							,		lowerCAmelCase_=0.1							,		lowerCAmelCase_=5_12							,		lowerCAmelCase_=16							,		lowerCAmelCase_=2							,		lowerCAmelCase_=0.02							,		lowerCAmelCase_=3							,		lowerCAmelCase_=4							,		lowerCAmelCase_=None							,		):

       '''simple docstring'''

       a_      :	Optional[Any]      =							parent
       a_      :	List[str]      =							batch_size
       a_      :	int      =							seq_length
       a_      :	Tuple      =							is_training
       a_      :	List[str]      =							use_input_mask
       a_      :	Tuple      =							use_token_type_ids
       a_      :	Any      =							use_labels
       a_      :	Dict      =							vocab_size
       a_      :	Union[str, Any]      =							hidden_size
       a_      :	List[str]      =							num_hidden_layers
       a_      :	List[Any]      =							num_attention_heads
       a_      :	Optional[Any]      =							intermediate_size
       a_      :	int      =							hidden_act
       a_      :	Optional[int]      =							hidden_dropout_prob
       a_      :	Any      =							attention_probs_dropout_prob
       a_      :	Optional[Any]      =							max_position_embeddings
       a_      :	Union[str, Any]      =							type_vocab_size
       a_      :	Any      =							type_sequence_label_size
       a_      :	str      =							initializer_range
       a_      :	Union[str, Any]      =							num_labels
       a_      :	Tuple      =							num_choices
       a_      :	str      =							scope


     def 							_lowerCAmelCase  (      self ):

       '''simple docstring'''

       a_      :	List[str]      =							ids_tensor([self.batch_size, self.seq_length]							,		self.vocab_size )

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

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

       a_      :	Any      =							None
       a_      :	int      =							None
       a_      :	Optional[Any]      =							None
       if self.use_labels:
         a_      :	Dict      =							ids_tensor([self.batch_size]							,		self.type_sequence_label_size )
         a_      :	Tuple      =							ids_tensor([self.batch_size, self.seq_length]							,		self.num_labels )
         a_      :	Optional[int]      =							ids_tensor([self.batch_size]							,		self.num_choices )

       a_      :	Tuple      =							self.get_config()

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


     def 							_lowerCAmelCase  (      self ):

       '''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 							_lowerCAmelCase  (      self							,		lowerCAmelCase_							,		lowerCAmelCase_							,		lowerCAmelCase_							,		lowerCAmelCase_							,		lowerCAmelCase_							,		lowerCAmelCase_							,		lowerCAmelCase_ ):

       '''simple docstring'''

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


     def 							_lowerCAmelCase  (      self							,		lowerCAmelCase_							,		lowerCAmelCase_							,		lowerCAmelCase_							,		lowerCAmelCase_							,		lowerCAmelCase_							,		lowerCAmelCase_							,		lowerCAmelCase_ ):

       '''simple docstring'''

       a_      :	List[str]      =							NystromformerForMaskedLM(config=__lowerCAmelCase )
       model.to(__lowerCAmelCase )
       model.eval()
       a_      :	Any      =							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 							_lowerCAmelCase  (      self							,		lowerCAmelCase_							,		lowerCAmelCase_							,		lowerCAmelCase_							,		lowerCAmelCase_							,		lowerCAmelCase_							,		lowerCAmelCase_							,		lowerCAmelCase_ ):

       '''simple docstring'''

       a_      :	Optional[Any]      =							NystromformerForQuestionAnswering(config=__lowerCAmelCase )
       model.to(__lowerCAmelCase )
       model.eval()
       a_      :	Dict      =							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 							_lowerCAmelCase  (      self							,		lowerCAmelCase_							,		lowerCAmelCase_							,		lowerCAmelCase_							,		lowerCAmelCase_							,		lowerCAmelCase_							,		lowerCAmelCase_							,		lowerCAmelCase_ ):

       '''simple docstring'''

       a_      :	Union[str, Any]      =							self.num_labels
       a_      :	str      =							NystromformerForSequenceClassification(__lowerCAmelCase )
       model.to(__lowerCAmelCase )
       model.eval()
       a_      :	Optional[Any]      =							model(__lowerCAmelCase							,		attention_mask=__lowerCAmelCase							,		token_type_ids=__lowerCAmelCase							,		labels=__lowerCAmelCase )
       self.parent.assertEqual(result.logits.shape							,		(self.batch_size, self.num_labels) )


     def 							_lowerCAmelCase  (      self							,		lowerCAmelCase_							,		lowerCAmelCase_							,		lowerCAmelCase_							,		lowerCAmelCase_							,		lowerCAmelCase_							,		lowerCAmelCase_							,		lowerCAmelCase_ ):

       '''simple docstring'''

       a_      :	List[Any]      =							self.num_labels
       a_      :	Dict      =							NystromformerForTokenClassification(config=__lowerCAmelCase )
       model.to(__lowerCAmelCase )
       model.eval()
       a_      :	Union[str, Any]      =							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 							_lowerCAmelCase  (      self							,		lowerCAmelCase_							,		lowerCAmelCase_							,		lowerCAmelCase_							,		lowerCAmelCase_							,		lowerCAmelCase_							,		lowerCAmelCase_							,		lowerCAmelCase_ ):

       '''simple docstring'''

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






     def 							_lowerCAmelCase  (      self ):

       '''simple docstring'''

       a_      :	int      =							self.prepare_config_and_inputs()
       (
           (
           a_  
       )      , (
           a_  
       )      , (
           a_  
       )      , (
           a_  
       )      , (
           a_  
       )      , (
           a_  
       )      , (
           a_  
       )      ,   
       )      :	List[Any]      =							config_and_inputs
       a_      :	Optional[int]      =							{"""input_ids""": input_ids, """token_type_ids""": token_type_ids, """attention_mask""": input_mask}
       return config, inputs_dict







@require_torch
class  _UpperCAmelCase ( UpperCAmelCase__	,UpperCAmelCase__	,unittest.TestCase    ):

     """simple docstring"""

     a_    	=					(
         (
             NystromformerModel,
             NystromformerForMaskedLM,
             NystromformerForMultipleChoice,
             NystromformerForQuestionAnswering,
             NystromformerForSequenceClassification,
             NystromformerForTokenClassification,
         )
         if is_torch_available()
         else ()
     )
     a_    	=					(
         {
             "feature-extraction": NystromformerModel,
             "fill-mask": NystromformerForMaskedLM,
             "question-answering": NystromformerForQuestionAnswering,
             "text-classification": NystromformerForSequenceClassification,
             "token-classification": NystromformerForTokenClassification,
             "zero-shot": NystromformerForSequenceClassification,
         }
         if is_torch_available()
         else {}
     )
     a_    	=					False
     a_    	=					False


     def 							_lowerCAmelCase  (      self ):

       '''simple docstring'''

       a_      :	Optional[Any]      =							NystromformerModelTester(self )
       a_      :	Dict      =							ConfigTester(self							,		config_class=__lowerCAmelCase							,		hidden_size=37 )


     def 							_lowerCAmelCase  (      self ):

       '''simple docstring'''

       self.config_tester.run_common_tests()


     def 							_lowerCAmelCase  (      self ):

       '''simple docstring'''

       a_      :	Tuple      =							self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_model(*__lowerCAmelCase )


     def 							_lowerCAmelCase  (      self ):

       '''simple docstring'''

       a_      :	Optional[int]      =							self.model_tester.prepare_config_and_inputs()
       for type in ["absolute", "relative_key", "relative_key_query"]:
         a_      :	Tuple      =							type
         self.model_tester.create_and_check_model(*__lowerCAmelCase )


     def 							_lowerCAmelCase  (      self ):

       '''simple docstring'''

       a_      :	Optional[int]      =							self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_for_masked_lm(*__lowerCAmelCase )


     def 							_lowerCAmelCase  (      self ):

       '''simple docstring'''

       a_      :	Any      =							self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_for_multiple_choice(*__lowerCAmelCase )


     def 							_lowerCAmelCase  (      self ):

       '''simple docstring'''

       a_      :	Dict      =							self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_for_question_answering(*__lowerCAmelCase )


     def 							_lowerCAmelCase  (      self ):

       '''simple docstring'''

       a_      :	Tuple      =							self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_for_sequence_classification(*__lowerCAmelCase )


     def 							_lowerCAmelCase  (      self ):

       '''simple docstring'''

       a_      :	Tuple      =							self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_for_token_classification(*__lowerCAmelCase )






     @slow
     def 							_lowerCAmelCase  (      self ):

       '''simple docstring'''

       for model_name in NYSTROMFORMER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
         a_      :	str      =							NystromformerModel.from_pretrained(__lowerCAmelCase )
         self.assertIsNotNone(__lowerCAmelCase )







@require_torch
class  _UpperCAmelCase ( unittest.TestCase    ):

     """simple docstring"""

     @slow
     def 							_lowerCAmelCase  (      self ):

       '''simple docstring'''

       a_      :	Optional[Any]      =							NystromformerModel.from_pretrained("""uw-madison/nystromformer-512""" )
       a_      :	List[Any]      =							torch.tensor([[0, 1, 2, 3, 4, 5]] )

       with torch.no_grad():
         a_      :	str      =							model(__lowerCAmelCase )[0]

       a_      :	List[Any]      =							torch.Size((1, 6, 7_68) )
       self.assertEqual(output.shape							,		__lowerCAmelCase )

       a_      :	List[str]      =							torch.tensor(
           [[[-0.4532, -0.0936, 0.5137], [-0.2676, 0.0628, 0.6186], [-0.3629, -0.1726, 0.4716]]] )

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






     @slow
     def 							_lowerCAmelCase  (      self ):

       '''simple docstring'''

       a_      :	int      =							"""the [MASK] of Belgium is Brussels"""

       a_      :	List[str]      =							AutoTokenizer.from_pretrained("""uw-madison/nystromformer-512""" )
       a_      :	Tuple      =							NystromformerForMaskedLM.from_pretrained("""uw-madison/nystromformer-512""" )

       a_      :	Optional[Any]      =							tokenizer(__lowerCAmelCase							,		return_tensors="""pt""" )

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

       a_      :	Union[str, Any]      =							token_logits[:, 2, :].argmax(-1 )[0]

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




'''simple docstring'''

def 	_snake_case						(      A_       :    Optional[int]   ):
  """simple docstring"""







  if not head:
    return True
  # split the list to two parts
  a_      , a_      :	Union[str, Any]      =							head.next, head
  while fast and fast.next:
    a_      :	Dict      =							fast.next.next
    a_      :	List[str]      =							slow.next
  a_      :	str      =							slow.next
  a_      :	Optional[int]      =							None  # Don't forget here! But forget still works!
  # reverse the second part
  a_      :	Dict      =							None
  while second:
    a_      :	Any      =							second.next
    a_      :	Any      =							node
    a_      :	Optional[Any]      =							second
    a_      :	Optional[Any]      =							nxt
  # compare two parts
  # second part has the same or one less node
  while node:
    if node.val != head.val:
      return False
    a_      :	List[str]      =							node.next
    a_      :	Dict      =							head.next
  return True






def 	_snake_case						(      A_       :    List[str]   ):
  """simple docstring"""







  if not head or not head.next:
    return True

  # 1. Get the midpoint (slow)
  a_      :	Optional[Any]      =							head
  while fast and fast.next:
    a_      , a_      :	Optional[int]      =							fast.next.next, slow.next

  # 2. Push the second half into the stack
  a_      :	List[str]      =							[slow.val]
  while slow.next:
    a_      :	List[str]      =							slow.next
    stack.append(slow.val   )

  # 3. Comparison
  while stack:
    if stack.pop() != cur.val:
      return False
    a_      :	Union[str, Any]      =							cur.next

  return True






def 	_snake_case						(      A_       :    Optional[int]   ):
  """simple docstring"""







  if not head or not head.next:
    return True
  a_      :	List[Any]      =							{}
  a_      :	int      =							0
  while head:
    if head.val in d:
      d[head.val].append(A_   )
    else:
      a_      :	int      =							[pos]
    a_      :	List[str]      =							head.next
    pos += 1
  a_      :	str      =							pos - 1
  a_      :	List[str]      =							0
  for v in d.values():
    if len(A_   ) % 2 != 0:
      middle += 1
    else:
      a_      :	Optional[int]      =							0
      for i in range(0     ,		len(A_   )   ):
        if v[i] + v[len(A_   ) - 1 - step] != checksum:
          return False
        step += 1
    if middle > 1:
      return False
  return True


