



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



   def __init__(       self    :	Dict							,       __lowerCamelCase    :	Dict							,       __lowerCamelCase    :	Any=1_3							,       __lowerCamelCase    :	Union[str, Any]=7							,       __lowerCamelCase    :	str=True							,       __lowerCamelCase    :	int=True							,       __lowerCamelCase    :	Any=True							,       __lowerCamelCase    :	Optional[Any]=True							,       __lowerCamelCase    :	List[str]=9_9							,       __lowerCamelCase    :	Optional[int]=3_2							,       __lowerCamelCase    :	str=5							,       __lowerCamelCase    :	Tuple=4							,       __lowerCamelCase    :	List[str]=3_7							,       __lowerCamelCase    :	Any="gelu"							,       __lowerCamelCase    :	str=0.1							,       __lowerCamelCase    :	str=0.1							,       __lowerCamelCase    :	int=5_1_2							,       __lowerCamelCase    :	Tuple=1_6							,       __lowerCamelCase    :	List[Any]=2							,       __lowerCamelCase    :	str=0.0_2							,       __lowerCamelCase    :	Any=3							,       __lowerCamelCase    :	Optional[int]=4							,       __lowerCamelCase    :	Union[str, Any]=None							,       ):



     """simple docstring"""
     _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    :	str				):



     """simple docstring"""
     _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    :	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    __UpperCAmelCase    (       self    :	List[Any]							,       __lowerCamelCase    :	Optional[int]							,       __lowerCamelCase    :	str							,       __lowerCamelCase    :	Tuple							,       __lowerCamelCase    :	Tuple							,       __lowerCamelCase    :	Union[str, Any]							,       __lowerCamelCase    :	str							,       __lowerCamelCase    :	Optional[int]				):



     """simple docstring"""
     _snake_case   	=      NystromformerModel(config=__lowerCamelCase				)
     model.to(__lowerCamelCase				)
     model.eval()
     _snake_case   	=      model(__lowerCamelCase							,       attention_mask=__lowerCamelCase							,       token_type_ids=__lowerCamelCase				)
     _snake_case   	=      model(__lowerCamelCase							,       token_type_ids=__lowerCamelCase				)
     _snake_case   	=      model(__lowerCamelCase				)
     self.parent.assertEqual(result.last_hidden_state.shape							,       (self.batch_size, self.seq_length, self.hidden_size)				)



   def    __UpperCAmelCase    (       self    :	Any							,       __lowerCamelCase    :	Optional[int]							,       __lowerCamelCase    :	Tuple							,       __lowerCamelCase    :	str							,       __lowerCamelCase    :	Optional[int]							,       __lowerCamelCase    :	str							,       __lowerCamelCase    :	Any							,       __lowerCamelCase    :	int				):



     """simple docstring"""
     _snake_case   	=      NystromformerForMaskedLM(config=__lowerCamelCase				)
     model.to(__lowerCamelCase				)
     model.eval()
     _snake_case   	=      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    __UpperCAmelCase    (       self    :	Optional[int]							,       __lowerCamelCase    :	Dict							,       __lowerCamelCase    :	Optional[Any]							,       __lowerCamelCase    :	int							,       __lowerCamelCase    :	Tuple							,       __lowerCamelCase    :	Tuple							,       __lowerCamelCase    :	List[str]							,       __lowerCamelCase    :	Dict				):



     """simple docstring"""
     _snake_case   	=      NystromformerForQuestionAnswering(config=__lowerCamelCase				)
     model.to(__lowerCamelCase				)
     model.eval()
     _snake_case   	=      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    __UpperCAmelCase    (       self    :	Dict							,       __lowerCamelCase    :	Optional[Any]							,       __lowerCamelCase    :	Optional[Any]							,       __lowerCamelCase    :	Tuple							,       __lowerCamelCase    :	int							,       __lowerCamelCase    :	Union[str, Any]							,       __lowerCamelCase    :	int							,       __lowerCamelCase    :	List[Any]				):



     """simple docstring"""
     _snake_case   	=      self.num_labels
     _snake_case   	=      NystromformerForSequenceClassification(__lowerCamelCase				)
     model.to(__lowerCamelCase				)
     model.eval()
     _snake_case   	=      model(__lowerCamelCase							,       attention_mask=__lowerCamelCase							,       token_type_ids=__lowerCamelCase							,       labels=__lowerCamelCase				)
     self.parent.assertEqual(result.logits.shape							,       (self.batch_size, self.num_labels)				)



   def    __UpperCAmelCase    (       self    :	Dict							,       __lowerCamelCase    :	List[str]							,       __lowerCamelCase    :	List[Any]							,       __lowerCamelCase    :	Tuple							,       __lowerCamelCase    :	Optional[Any]							,       __lowerCamelCase    :	Dict							,       __lowerCamelCase    :	Any							,       __lowerCamelCase    :	Optional[int]				):



     """simple docstring"""
     _snake_case   	=      self.num_labels
     _snake_case   	=      NystromformerForTokenClassification(config=__lowerCamelCase				)
     model.to(__lowerCamelCase				)
     model.eval()
     _snake_case   	=      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    __UpperCAmelCase    (       self    :	Optional[Any]							,       __lowerCamelCase    :	Union[str, Any]							,       __lowerCamelCase    :	Union[str, Any]							,       __lowerCamelCase    :	Any							,       __lowerCamelCase    :	int							,       __lowerCamelCase    :	Optional[Any]							,       __lowerCamelCase    :	Union[str, Any]							,       __lowerCamelCase    :	Optional[Any]				):



     """simple docstring"""
     _snake_case   	=      self.num_choices
     _snake_case   	=      NystromformerForMultipleChoice(config=__lowerCamelCase				)
     model.to(__lowerCamelCase				)
     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(
         __lowerCamelCase							,       attention_mask=__lowerCamelCase							,       token_type_ids=__lowerCamelCase							,       labels=__lowerCamelCase							,       )
     self.parent.assertEqual(result.logits.shape							,       (self.batch_size, self.num_choices)				)





   def    __UpperCAmelCase    (       self    :	Optional[int]				):



     """simple docstring"""
     _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     UpperCAmelCase    (	__SCREAMING_SNAKE_CASE,__SCREAMING_SNAKE_CASE,unittest.TestCase  ):
   A__		:	Tuple           =  (
       (
           NystromformerModel,
           NystromformerForMaskedLM,
           NystromformerForMultipleChoice,
           NystromformerForQuestionAnswering,
           NystromformerForSequenceClassification,
           NystromformerForTokenClassification,
       )
       if is_torch_available()
       else ()
   )
   A__		:	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 {}
   )
   A__		:	Optional[int]           =  False
   A__		:	List[Any]           =  False



   def    __UpperCAmelCase    (       self    :	int				):



     """simple docstring"""
     _snake_case   	=      NystromformerModelTester(self				)
     _snake_case   	=      ConfigTester(self							,       config_class=__lowerCamelCase							,       hidden_size=3_7				)



   def    __UpperCAmelCase    (       self    :	int				):



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



   def    __UpperCAmelCase    (       self    :	Tuple				):



     """simple docstring"""
     _snake_case   	=      self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_model(*__lowerCamelCase				)



   def    __UpperCAmelCase    (       self    :	Optional[int]				):



     """simple docstring"""
     _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(*__lowerCamelCase				)



   def    __UpperCAmelCase    (       self    :	Dict				):



     """simple docstring"""
     _snake_case   	=      self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_for_masked_lm(*__lowerCamelCase				)



   def    __UpperCAmelCase    (       self    :	List[str]				):



     """simple docstring"""
     _snake_case   	=      self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_for_multiple_choice(*__lowerCamelCase				)



   def    __UpperCAmelCase    (       self    :	Tuple				):



     """simple docstring"""
     _snake_case   	=      self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_for_question_answering(*__lowerCamelCase				)



   def    __UpperCAmelCase    (       self    :	str				):



     """simple docstring"""
     _snake_case   	=      self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_for_sequence_classification(*__lowerCamelCase				)



   def    __UpperCAmelCase    (       self    :	Dict				):



     """simple docstring"""
     _snake_case   	=      self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_for_token_classification(*__lowerCamelCase				)





   @slow
   def    __UpperCAmelCase    (       self    :	Optional[int]				):



     """simple docstring"""
     for model_name in NYSTROMFORMER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
       _snake_case   	=      NystromformerModel.from_pretrained(__lowerCamelCase				)
       self.assertIsNotNone(__lowerCamelCase				)



@require_torch
class     UpperCAmelCase    (	unittest.TestCase  ):



   @slow
   def    __UpperCAmelCase    (       self    :	Optional[int]				):



     """simple docstring"""
     _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(__lowerCamelCase				)[0]

     _snake_case   	=      torch.Size((1, 6, 7_6_8)				)
     self.assertEqual(output.shape							,       __lowerCamelCase				)

     _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]							,       __lowerCamelCase							,       atol=1E-4				)				)





   @slow
   def    __UpperCAmelCase    (       self    :	Dict				):



     """simple docstring"""
     _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(__lowerCamelCase							,       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(__lowerCamelCase				)							,       '''capital'''				)







import re





def      lowerCamelCase_					(							lowerCamelCase__       ):
				return [char.split() for char in re.split(r"[^ a-z A-Z 0-9 \s]"      ,					str_       )]





def      lowerCamelCase_					(							lowerCamelCase__       ):
				lowerCamelCase_         =     split_input(str_       )
				return "".join(
				    ["".join([char.capitalize() for char in sub_str]       ) for sub_str in string_split]       )





def      lowerCamelCase_					(							lowerCamelCase__      ,					lowerCamelCase__      ,					lowerCamelCase__       ):
				try:
								lowerCamelCase_         =     split_input(lowerCamelCase__       )
								if upper:
												lowerCamelCase_         =     "".join(
												    [
												        separator.join([char.upper() for char in sub_str]       )
												        for sub_str in string_split
												    ]       )
								else:
												lowerCamelCase_         =     "".join(
												    [
												        separator.join([char.lower() for char in sub_str]       )
												        for sub_str in string_split
												    ]       )
								return res_str
				except IndexError:
								return "not valid string"





def      lowerCamelCase_					(							lowerCamelCase__       ):
				return to_simple_case(lowerCamelCase__       )





def      lowerCamelCase_					(							lowerCamelCase__       ):
				try:
								lowerCamelCase_         =     to_simple_case(lowerCamelCase__       )
								return res_str[0].lower() + res_str[1:]
				except IndexError:
								return "not valid string"





def      lowerCamelCase_					(							lowerCamelCase__      ,					lowerCamelCase__       ):
				return to_complex_case(lowerCamelCase__      ,					lowerCamelCase__      ,					"_"       )





def      lowerCamelCase_					(							lowerCamelCase__      ,					lowerCamelCase__       ):
				return to_complex_case(lowerCamelCase__      ,					lowerCamelCase__      ,					"-"       )


if __name__ == "__main__":
			__import__('''doctest''').testmod()


