





"""simple docstring"""
from collections import OrderedDict
from typing import Any, Mapping, Optional

from ... import PreTrainedTokenizer
from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfig, OnnxConfigWithPast, OnnxSeqaSeqConfigWithPast
from ...onnx.utils import compute_effective_axis_dimension
from ...utils import TensorType, is_torch_available, logging


lowerCamelCase_       =		logging.get_logger(__name__)

lowerCamelCase_       =		{
    "Helsinki-NLP/opus-mt-en-de": "https://huggingface.co/Helsinki-NLP/opus-mt-en-de/resolve/main/config.json",
    # See all Marian models at https://huggingface.co/models?filter=marian
}






class  _SCREAMING_SNAKE_CASE(       A  ):
       SCREAMING_SNAKE_CASE_       :				Any     						=  '''marian'''
       SCREAMING_SNAKE_CASE_       :				List[str]     						=  ['''past_key_values''']
       SCREAMING_SNAKE_CASE_       :				List[Any]     						=  {'''num_attention_heads''': '''encoder_attention_heads''', '''hidden_size''': '''d_model'''}




       def __init__(						self		,SCREAMING_SNAKE_CASE__=5_81_01		,SCREAMING_SNAKE_CASE__=None		,SCREAMING_SNAKE_CASE__=10_24		,SCREAMING_SNAKE_CASE__=12		,SCREAMING_SNAKE_CASE__=40_96		,SCREAMING_SNAKE_CASE__=16		,SCREAMING_SNAKE_CASE__=12		,SCREAMING_SNAKE_CASE__=40_96		,SCREAMING_SNAKE_CASE__=16		,SCREAMING_SNAKE_CASE__=0.0		,SCREAMING_SNAKE_CASE__=0.0		,SCREAMING_SNAKE_CASE__=True		,SCREAMING_SNAKE_CASE__=True		,SCREAMING_SNAKE_CASE__="gelu"		,SCREAMING_SNAKE_CASE__=10_24		,SCREAMING_SNAKE_CASE__=0.1		,SCREAMING_SNAKE_CASE__=0.0		,SCREAMING_SNAKE_CASE__=0.0		,SCREAMING_SNAKE_CASE__=0.0_2		,SCREAMING_SNAKE_CASE__=5_81_00		,SCREAMING_SNAKE_CASE__=False		,SCREAMING_SNAKE_CASE__=5_81_00		,SCREAMING_SNAKE_CASE__=0		,SCREAMING_SNAKE_CASE__=0		,SCREAMING_SNAKE_CASE__=True		,**SCREAMING_SNAKE_CASE__		,)      ->				Tuple:





             """simple docstring"""






             __SCREAMING_SNAKE_CASE       :List[str]		    =  vocab_size
             __SCREAMING_SNAKE_CASE       :Optional[int]		    =  decoder_vocab_size or vocab_size
             __SCREAMING_SNAKE_CASE       :Dict		    =  max_position_embeddings
             __SCREAMING_SNAKE_CASE       :List[str]		    =  d_model
             __SCREAMING_SNAKE_CASE       :Any		    =  encoder_ffn_dim
             __SCREAMING_SNAKE_CASE       :List[Any]		    =  encoder_layers
             __SCREAMING_SNAKE_CASE       :List[str]		    =  encoder_attention_heads
             __SCREAMING_SNAKE_CASE       :Optional[Any]		    =  decoder_ffn_dim
             __SCREAMING_SNAKE_CASE       :Dict		    =  decoder_layers
             __SCREAMING_SNAKE_CASE       :Tuple		    =  decoder_attention_heads
             __SCREAMING_SNAKE_CASE       :str		    =  dropout
             __SCREAMING_SNAKE_CASE       :int		    =  attention_dropout
             __SCREAMING_SNAKE_CASE       :str		    =  activation_dropout
             __SCREAMING_SNAKE_CASE       :Dict		    =  activation_function
             __SCREAMING_SNAKE_CASE       :Optional[Any]		    =  init_std
             __SCREAMING_SNAKE_CASE       :Optional[int]		    =  encoder_layerdrop
             __SCREAMING_SNAKE_CASE       :Dict		    =  decoder_layerdrop
             __SCREAMING_SNAKE_CASE       :Dict		    =  use_cache
             __SCREAMING_SNAKE_CASE       :Any		    =  encoder_layers
             __SCREAMING_SNAKE_CASE       :Tuple		    =  scale_embedding  # scale factor will be sqrt(d_model) if True
             __SCREAMING_SNAKE_CASE       :Union[str, Any]		    =  share_encoder_decoder_embeddings
             super().__init__(
                 pad_token_id=lowerCamelCase_		,eos_token_id=lowerCamelCase_		,is_encoder_decoder=lowerCamelCase_		,decoder_start_token_id=lowerCamelCase_		,forced_eos_token_id=lowerCamelCase_		,**lowerCamelCase_		,)






class  _SCREAMING_SNAKE_CASE(       A  ):




       @property
       # Copied from transformers.models.bart.configuration_bart.BartOnnxConfig.inputs
       def    _UpperCamelCase					(						self							)      ->				Mapping[str, Mapping[int, str]]:





             """simple docstring"""






             if self.task in ["default", "seq2seq-lm"]:
                   __SCREAMING_SNAKE_CASE       :int		    =  OrderedDict(
                       [
                           ('''input_ids''', {0: '''batch''', 1: '''encoder_sequence'''}),
                           ('''attention_mask''', {0: '''batch''', 1: '''encoder_sequence'''}),
                       ]							)

                   if self.use_past:
                         __SCREAMING_SNAKE_CASE       :Dict		    =  {0: '''batch'''}
                         __SCREAMING_SNAKE_CASE       :int		    =  {0: '''batch''', 1: '''past_decoder_sequence + sequence'''}
                   else:
                         __SCREAMING_SNAKE_CASE       :str		    =  {0: '''batch''', 1: '''decoder_sequence'''}
                         __SCREAMING_SNAKE_CASE       :Optional[int]		    =  {0: '''batch''', 1: '''decoder_sequence'''}

                   if self.use_past:
                         self.fill_with_past_key_values_(lowerCamelCase_		,direction='''inputs'''							)
             elif self.task == "causal-lm":
                   # TODO: figure this case out.
                   __SCREAMING_SNAKE_CASE       :Dict		    =  OrderedDict(
                       [
                           ('''input_ids''', {0: '''batch''', 1: '''encoder_sequence'''}),
                           ('''attention_mask''', {0: '''batch''', 1: '''encoder_sequence'''}),
                       ]							)
                   if self.use_past:
                         __SCREAMING_SNAKE_CASE    ,__SCREAMING_SNAKE_CASE       :Optional[Any]		    =  self.num_layers
                         for i in range(lowerCamelCase_							):
                               __SCREAMING_SNAKE_CASE       :List[Any]		    =  {0: '''batch''', 2: '''past_sequence + sequence'''}
                               __SCREAMING_SNAKE_CASE       :List[Any]		    =  {0: '''batch''', 2: '''past_sequence + sequence'''}
             else:
                   __SCREAMING_SNAKE_CASE       :List[str]		    =  OrderedDict(
                       [
                           ('''input_ids''', {0: '''batch''', 1: '''encoder_sequence'''}),
                           ('''attention_mask''', {0: '''batch''', 1: '''encoder_sequence'''}),
                           ('''decoder_input_ids''', {0: '''batch''', 1: '''decoder_sequence'''}),
                           ('''decoder_attention_mask''', {0: '''batch''', 1: '''decoder_sequence'''}),
                       ]							)

             return common_inputs




       @property
       # Copied from transformers.models.bart.configuration_bart.BartOnnxConfig.outputs
       def    _UpperCamelCase					(						self							)      ->				Mapping[str, Mapping[int, str]]:





             """simple docstring"""






             if self.task in ["default", "seq2seq-lm"]:
                   __SCREAMING_SNAKE_CASE       :Any		    =  super().outputs
             else:
                   __SCREAMING_SNAKE_CASE       :Tuple		    =  super(lowerCamelCase_		,self							).outputs
                   if self.use_past:
                         __SCREAMING_SNAKE_CASE    ,__SCREAMING_SNAKE_CASE       :Tuple		    =  self.num_layers
                         for i in range(lowerCamelCase_							):
                               __SCREAMING_SNAKE_CASE       :int		    =  {0: '''batch''', 2: '''past_sequence + sequence'''}
                               __SCREAMING_SNAKE_CASE       :Optional[int]		    =  {0: '''batch''', 2: '''past_sequence + sequence'''}
             return common_outputs




       def    _UpperCamelCase					(						self		,SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__ = -1		,SCREAMING_SNAKE_CASE__ = -1		,SCREAMING_SNAKE_CASE__ = False		,SCREAMING_SNAKE_CASE__ = None		,)      ->				Mapping[str, Any]:





             """simple docstring"""






             __SCREAMING_SNAKE_CASE       :Tuple		    =  self._generate_dummy_inputs_for_encoder_and_decoder(
                 lowerCamelCase_		,lowerCamelCase_		,lowerCamelCase_		,lowerCamelCase_		,lowerCamelCase_							)

             # Generate decoder inputs
             __SCREAMING_SNAKE_CASE       :Dict		    =  seq_length if not self.use_past else 1
             __SCREAMING_SNAKE_CASE       :Dict		    =  self._generate_dummy_inputs_for_encoder_and_decoder(
                 lowerCamelCase_		,lowerCamelCase_		,lowerCamelCase_		,lowerCamelCase_		,lowerCamelCase_							)
             __SCREAMING_SNAKE_CASE       :List[Any]		    =  {f'''decoder_{name}''': tensor for name, tensor in decoder_inputs.items()}
             __SCREAMING_SNAKE_CASE       :Any		    =  dict(**lowerCamelCase_		,**lowerCamelCase_							)

             if self.use_past:
                   if not is_torch_available():
                         raise ValueError('''Cannot generate dummy past_keys inputs without PyTorch installed.'''							)
                   else:
                         import torch
                   __SCREAMING_SNAKE_CASE    ,__SCREAMING_SNAKE_CASE       :Tuple		    =  common_inputs['''input_ids'''].shape
                   __SCREAMING_SNAKE_CASE       :Optional[int]		    =  common_inputs['''decoder_input_ids'''].shape[1]
                   __SCREAMING_SNAKE_CASE    ,__SCREAMING_SNAKE_CASE       :int		    =  self.num_attention_heads
                   __SCREAMING_SNAKE_CASE       :Optional[int]		    =  (
                       batch,
                       num_encoder_attention_heads,
                       encoder_seq_length,
                       self._config.hidden_size // num_encoder_attention_heads,
                   )
                   __SCREAMING_SNAKE_CASE       :List[str]		    =  decoder_seq_length + 3
                   __SCREAMING_SNAKE_CASE       :str		    =  (
                       batch,
                       num_decoder_attention_heads,
                       decoder_past_length,
                       self._config.hidden_size // num_decoder_attention_heads,
                   )

                   __SCREAMING_SNAKE_CASE       :Optional[Any]		    =  torch.cat(
                       [common_inputs['''decoder_attention_mask'''], torch.ones(lowerCamelCase_		,lowerCamelCase_							)]		,dim=1							)

                   __SCREAMING_SNAKE_CASE       :Tuple		    =  []
                   # If the number of encoder and decoder layers are present in the model configuration, both are considered
                   __SCREAMING_SNAKE_CASE    ,__SCREAMING_SNAKE_CASE       :str		    =  self.num_layers
                   __SCREAMING_SNAKE_CASE       :Any		    =  min(lowerCamelCase_		,lowerCamelCase_							)
                   __SCREAMING_SNAKE_CASE       :List[Any]		    =  max(lowerCamelCase_		,lowerCamelCase_							) - min_num_layers
                   __SCREAMING_SNAKE_CASE       :List[Any]		    =  '''encoder''' if num_encoder_layers > num_decoder_layers else '''decoder'''

                   for _ in range(lowerCamelCase_							):
                         common_inputs["past_key_values"].append(
                             (
                                 torch.zeros(lowerCamelCase_							),
                                 torch.zeros(lowerCamelCase_							),
                                 torch.zeros(lowerCamelCase_							),
                                 torch.zeros(lowerCamelCase_							),
                             )							)
                   # TODO: test this.
                   __SCREAMING_SNAKE_CASE       :Optional[Any]		    =  encoder_shape if remaining_side_name == '''encoder''' else decoder_shape
                   for _ in range(lowerCamelCase_		,lowerCamelCase_							):
                         common_inputs["past_key_values"].append((torch.zeros(lowerCamelCase_							), torch.zeros(lowerCamelCase_							))							)
             return common_inputs




       def    _UpperCamelCase					(						self		,SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__ = -1		,SCREAMING_SNAKE_CASE__ = -1		,SCREAMING_SNAKE_CASE__ = False		,SCREAMING_SNAKE_CASE__ = None		,)      ->				Mapping[str, Any]:





             """simple docstring"""






             __SCREAMING_SNAKE_CASE       :Tuple		    =  self._generate_dummy_inputs_for_encoder_and_decoder(
                 lowerCamelCase_		,lowerCamelCase_		,lowerCamelCase_		,lowerCamelCase_		,lowerCamelCase_							)

             if self.use_past:
                   if not is_torch_available():
                         raise ValueError('''Cannot generate dummy past_keys inputs without PyTorch installed.'''							)
                   else:
                         import torch
                   __SCREAMING_SNAKE_CASE    ,__SCREAMING_SNAKE_CASE       :Dict		    =  common_inputs['''input_ids'''].shape
                   # Not using the same length for past_key_values
                   __SCREAMING_SNAKE_CASE       :List[Any]		    =  seqlen + 2
                   __SCREAMING_SNAKE_CASE    ,__SCREAMING_SNAKE_CASE       :Tuple		    =  self.num_layers
                   __SCREAMING_SNAKE_CASE    ,__SCREAMING_SNAKE_CASE       :str		    =  self.num_attention_heads
                   __SCREAMING_SNAKE_CASE       :List[Any]		    =  (
                       batch,
                       num_encoder_attention_heads,
                       past_key_values_length,
                       self._config.hidden_size // num_encoder_attention_heads,
                   )

                   __SCREAMING_SNAKE_CASE       :Any		    =  common_inputs['''attention_mask'''].dtype
                   __SCREAMING_SNAKE_CASE       :Tuple		    =  torch.cat(
                       [common_inputs['''attention_mask'''], torch.ones(lowerCamelCase_		,lowerCamelCase_		,dtype=lowerCamelCase_							)]		,dim=1							)
                   __SCREAMING_SNAKE_CASE       :Any		    =  [
                       (torch.zeros(lowerCamelCase_							), torch.zeros(lowerCamelCase_							)) for _ in range(lowerCamelCase_							)
                   ]
             return common_inputs




       def    _UpperCamelCase					(						self		,SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__ = -1		,SCREAMING_SNAKE_CASE__ = -1		,SCREAMING_SNAKE_CASE__ = False		,SCREAMING_SNAKE_CASE__ = None		,)      ->				Mapping[str, Any]:





             """simple docstring"""






             __SCREAMING_SNAKE_CASE       :Optional[Any]		    =  compute_effective_axis_dimension(
                 lowerCamelCase_		,fixed_dimension=OnnxConfig.default_fixed_batch		,num_token_to_add=0							)

             # If dynamic axis (-1) we forward with a fixed dimension of 8 tokens to avoid optimizations made by ONNX
             __SCREAMING_SNAKE_CASE       :List[Any]		    =  tokenizer.num_special_tokens_to_add(lowerCamelCase_							)
             __SCREAMING_SNAKE_CASE       :List[Any]		    =  compute_effective_axis_dimension(
                 lowerCamelCase_		,fixed_dimension=OnnxConfig.default_fixed_sequence		,num_token_to_add=lowerCamelCase_							)

             # Generate dummy inputs according to compute batch and sequence
             __SCREAMING_SNAKE_CASE       :Tuple		    =  [''' '''.join([tokenizer.unk_token]							) * seq_length] * batch_size
             __SCREAMING_SNAKE_CASE       :str		    =  dict(tokenizer(lowerCamelCase_		,return_tensors=lowerCamelCase_							)							)
             return common_inputs




       def    _UpperCamelCase					(						self		,SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__ = -1		,SCREAMING_SNAKE_CASE__ = -1		,SCREAMING_SNAKE_CASE__ = False		,SCREAMING_SNAKE_CASE__ = None		,)      ->				Mapping[str, Any]:





             """simple docstring"""






             if self.task in ["default", "seq2seq-lm"]:
                   __SCREAMING_SNAKE_CASE       :List[Any]		    =  self._generate_dummy_inputs_for_default_and_seqaseq_lm(
                       lowerCamelCase_		,batch_size=lowerCamelCase_		,seq_length=lowerCamelCase_		,is_pair=lowerCamelCase_		,framework=lowerCamelCase_							)

             else:
                   __SCREAMING_SNAKE_CASE       :str		    =  self._generate_dummy_inputs_for_causal_lm(
                       lowerCamelCase_		,batch_size=lowerCamelCase_		,seq_length=lowerCamelCase_		,is_pair=lowerCamelCase_		,framework=lowerCamelCase_							)

             return common_inputs




       def    _UpperCamelCase					(						self		,SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__							)      ->				Dict:





             """simple docstring"""






             if self.task in ["default", "seq2seq-lm"]:
                   __SCREAMING_SNAKE_CASE       :Dict		    =  super()._flatten_past_key_values_(lowerCamelCase_		,lowerCamelCase_		,lowerCamelCase_		,lowerCamelCase_							)
             else:
                   __SCREAMING_SNAKE_CASE       :Optional[int]		    =  super(lowerCamelCase_		,self							)._flatten_past_key_values_(
                       lowerCamelCase_		,lowerCamelCase_		,lowerCamelCase_		,lowerCamelCase_							)




       @property
       def    _UpperCamelCase					(						self							)      ->				float:





             """simple docstring"""






             return 1E-4







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




       def __init__(						self		,SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__=13		,SCREAMING_SNAKE_CASE__=7		,SCREAMING_SNAKE_CASE__=True		,SCREAMING_SNAKE_CASE__=True		,SCREAMING_SNAKE_CASE__=True		,SCREAMING_SNAKE_CASE__=True		,SCREAMING_SNAKE_CASE__=99		,SCREAMING_SNAKE_CASE__=32		,SCREAMING_SNAKE_CASE__=5		,SCREAMING_SNAKE_CASE__=4		,SCREAMING_SNAKE_CASE__=37		,SCREAMING_SNAKE_CASE__="gelu"		,SCREAMING_SNAKE_CASE__=0.1		,SCREAMING_SNAKE_CASE__=0.1		,SCREAMING_SNAKE_CASE__=5_12		,SCREAMING_SNAKE_CASE__=16		,SCREAMING_SNAKE_CASE__=2		,SCREAMING_SNAKE_CASE__=0.0_2		,SCREAMING_SNAKE_CASE__=3		,SCREAMING_SNAKE_CASE__=4		,SCREAMING_SNAKE_CASE__=None		,)      ->				Dict:





             """simple docstring"""






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




       def    _UpperCamelCase					(						self							)      ->				Optional[int]:





             """simple docstring"""






             __SCREAMING_SNAKE_CASE       :Union[str, Any]		    =  ids_tensor([self.batch_size, self.seq_length]		,self.vocab_size							)

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

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

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

             __SCREAMING_SNAKE_CASE       :Tuple		    =  self.get_config()

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




       def    _UpperCamelCase					(						self							)      ->				Union[str, 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=SCREAMING_SNAKE_CASE__		,initializer_range=self.initializer_range		,)




       def    _UpperCamelCase					(						self		,SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__							)      ->				Any:





             """simple docstring"""






             __SCREAMING_SNAKE_CASE       :List[Any]		    =  NystromformerModel(config=SCREAMING_SNAKE_CASE__							)
             model.to(SCREAMING_SNAKE_CASE__							)
             model.eval()
             __SCREAMING_SNAKE_CASE       :Any		    =  model(SCREAMING_SNAKE_CASE__		,attention_mask=SCREAMING_SNAKE_CASE__		,token_type_ids=SCREAMING_SNAKE_CASE__							)
             __SCREAMING_SNAKE_CASE       :List[str]		    =  model(SCREAMING_SNAKE_CASE__		,token_type_ids=SCREAMING_SNAKE_CASE__							)
             __SCREAMING_SNAKE_CASE       :int		    =  model(SCREAMING_SNAKE_CASE__							)
             self.parent.assertEqual(result.last_hidden_state.shape		,(self.batch_size, self.seq_length, self.hidden_size)							)




       def    _UpperCamelCase					(						self		,SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__							)      ->				int:





             """simple docstring"""






             __SCREAMING_SNAKE_CASE       :Dict		    =  NystromformerForMaskedLM(config=SCREAMING_SNAKE_CASE__							)
             model.to(SCREAMING_SNAKE_CASE__							)
             model.eval()
             __SCREAMING_SNAKE_CASE       :Tuple		    =  model(SCREAMING_SNAKE_CASE__		,attention_mask=SCREAMING_SNAKE_CASE__		,token_type_ids=SCREAMING_SNAKE_CASE__		,labels=SCREAMING_SNAKE_CASE__							)
             self.parent.assertEqual(result.logits.shape		,(self.batch_size, self.seq_length, self.vocab_size)							)




       def    _UpperCamelCase					(						self		,SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__							)      ->				Tuple:





             """simple docstring"""






             __SCREAMING_SNAKE_CASE       :List[str]		    =  NystromformerForQuestionAnswering(config=SCREAMING_SNAKE_CASE__							)
             model.to(SCREAMING_SNAKE_CASE__							)
             model.eval()
             __SCREAMING_SNAKE_CASE       :Optional[Any]		    =  model(
                 SCREAMING_SNAKE_CASE__		,attention_mask=SCREAMING_SNAKE_CASE__		,token_type_ids=SCREAMING_SNAKE_CASE__		,start_positions=SCREAMING_SNAKE_CASE__		,end_positions=SCREAMING_SNAKE_CASE__		,)
             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		,SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__							)      ->				Union[str, Any]:





             """simple docstring"""






             __SCREAMING_SNAKE_CASE       :int		    =  self.num_labels
             __SCREAMING_SNAKE_CASE       :Any		    =  NystromformerForSequenceClassification(SCREAMING_SNAKE_CASE__							)
             model.to(SCREAMING_SNAKE_CASE__							)
             model.eval()
             __SCREAMING_SNAKE_CASE       :Dict		    =  model(SCREAMING_SNAKE_CASE__		,attention_mask=SCREAMING_SNAKE_CASE__		,token_type_ids=SCREAMING_SNAKE_CASE__		,labels=SCREAMING_SNAKE_CASE__							)
             self.parent.assertEqual(result.logits.shape		,(self.batch_size, self.num_labels)							)




       def    _UpperCamelCase					(						self		,SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__							)      ->				Any:





             """simple docstring"""






             __SCREAMING_SNAKE_CASE       :Optional[Any]		    =  self.num_labels
             __SCREAMING_SNAKE_CASE       :Tuple		    =  NystromformerForTokenClassification(config=SCREAMING_SNAKE_CASE__							)
             model.to(SCREAMING_SNAKE_CASE__							)
             model.eval()
             __SCREAMING_SNAKE_CASE       :Any		    =  model(SCREAMING_SNAKE_CASE__		,attention_mask=SCREAMING_SNAKE_CASE__		,token_type_ids=SCREAMING_SNAKE_CASE__		,labels=SCREAMING_SNAKE_CASE__							)
             self.parent.assertEqual(result.logits.shape		,(self.batch_size, self.seq_length, self.num_labels)							)




       def    _UpperCamelCase					(						self		,SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__							)      ->				Dict:





             """simple docstring"""






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




       def    _UpperCamelCase					(						self							)      ->				Any:





             """simple docstring"""






             __SCREAMING_SNAKE_CASE       :List[Any]		    =  self.prepare_config_and_inputs()
             (
                 (
                 __SCREAMING_SNAKE_CASE
             )    ,(
                 __SCREAMING_SNAKE_CASE
             )    ,(
                 __SCREAMING_SNAKE_CASE
             )    ,(
                 __SCREAMING_SNAKE_CASE
             )    ,(
                 __SCREAMING_SNAKE_CASE
             )    ,(
                 __SCREAMING_SNAKE_CASE
             )    ,(
                 __SCREAMING_SNAKE_CASE
             )    ,
             )       :Dict		    =  config_and_inputs
             __SCREAMING_SNAKE_CASE       :str		    =  {'''input_ids''': input_ids, '''token_type_ids''': token_type_ids, '''attention_mask''': input_mask}
             return config, inputs_dict






@require_torch
class  _SCREAMING_SNAKE_CASE(       A   ,     A   ,     unittest.TestCase  ):
       SCREAMING_SNAKE_CASE_       :				int     						=  (
           (
               NystromformerModel,
               NystromformerForMaskedLM,
               NystromformerForMultipleChoice,
               NystromformerForQuestionAnswering,
               NystromformerForSequenceClassification,
               NystromformerForTokenClassification,
           )
           if is_torch_available()
           else ()
       )
       SCREAMING_SNAKE_CASE_       :				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 {}
       )
       SCREAMING_SNAKE_CASE_       :				str     						=  False
       SCREAMING_SNAKE_CASE_       :				Tuple     						=  False




       def    _UpperCamelCase					(						self							)      ->				Tuple:





             """simple docstring"""






             __SCREAMING_SNAKE_CASE       :Any		    =  NystromformerModelTester(self							)
             __SCREAMING_SNAKE_CASE       :Optional[Any]		    =  ConfigTester(self		,config_class=SCREAMING_SNAKE_CASE__		,hidden_size=37							)




       def    _UpperCamelCase					(						self							)      ->				List[str]:





             """simple docstring"""






             self.config_tester.run_common_tests()




       def    _UpperCamelCase					(						self							)      ->				List[str]:





             """simple docstring"""






             __SCREAMING_SNAKE_CASE       :List[str]		    =  self.model_tester.prepare_config_and_inputs()
             self.model_tester.create_and_check_model(*SCREAMING_SNAKE_CASE__							)




       def    _UpperCamelCase					(						self							)      ->				Dict:





             """simple docstring"""






             __SCREAMING_SNAKE_CASE       :Any		    =  self.model_tester.prepare_config_and_inputs()
             for type in ["absolute", "relative_key", "relative_key_query"]:
                   __SCREAMING_SNAKE_CASE       :Any		    =  type
                   self.model_tester.create_and_check_model(*SCREAMING_SNAKE_CASE__							)




       def    _UpperCamelCase					(						self							)      ->				Any:





             """simple docstring"""






             __SCREAMING_SNAKE_CASE       :List[str]		    =  self.model_tester.prepare_config_and_inputs()
             self.model_tester.create_and_check_for_masked_lm(*SCREAMING_SNAKE_CASE__							)




       def    _UpperCamelCase					(						self							)      ->				int:





             """simple docstring"""






             __SCREAMING_SNAKE_CASE       :Optional[Any]		    =  self.model_tester.prepare_config_and_inputs()
             self.model_tester.create_and_check_for_multiple_choice(*SCREAMING_SNAKE_CASE__							)




       def    _UpperCamelCase					(						self							)      ->				Union[str, Any]:





             """simple docstring"""






             __SCREAMING_SNAKE_CASE       :Any		    =  self.model_tester.prepare_config_and_inputs()
             self.model_tester.create_and_check_for_question_answering(*SCREAMING_SNAKE_CASE__							)




       def    _UpperCamelCase					(						self							)      ->				Union[str, Any]:





             """simple docstring"""






             __SCREAMING_SNAKE_CASE       :Dict		    =  self.model_tester.prepare_config_and_inputs()
             self.model_tester.create_and_check_for_sequence_classification(*SCREAMING_SNAKE_CASE__							)




       def    _UpperCamelCase					(						self							)      ->				Optional[int]:





             """simple docstring"""






             __SCREAMING_SNAKE_CASE       :Optional[Any]		    =  self.model_tester.prepare_config_and_inputs()
             self.model_tester.create_and_check_for_token_classification(*SCREAMING_SNAKE_CASE__							)




       @slow
       def    _UpperCamelCase					(						self							)      ->				Union[str, Any]:





             """simple docstring"""






             for model_name in NYSTROMFORMER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
                   __SCREAMING_SNAKE_CASE       :Tuple		    =  NystromformerModel.from_pretrained(SCREAMING_SNAKE_CASE__							)
                   self.assertIsNotNone(SCREAMING_SNAKE_CASE__							)






@require_torch
class  _SCREAMING_SNAKE_CASE(       unittest.TestCase  ):




       @slow
       def    _UpperCamelCase					(						self							)      ->				List[Any]:





             """simple docstring"""






             __SCREAMING_SNAKE_CASE       :int		    =  NystromformerModel.from_pretrained('''uw-madison/nystromformer-512'''							)
             __SCREAMING_SNAKE_CASE       :Dict		    =  torch.tensor([[0, 1, 2, 3, 4, 5]]							)

             with torch.no_grad():
                   __SCREAMING_SNAKE_CASE       :str		    =  model(SCREAMING_SNAKE_CASE__							)[0]

             __SCREAMING_SNAKE_CASE       :Optional[int]		    =  torch.Size((1, 6, 7_68)							)
             self.assertEqual(output.shape		,SCREAMING_SNAKE_CASE__							)

             __SCREAMING_SNAKE_CASE       :int		    =  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]		,SCREAMING_SNAKE_CASE__		,atol=1E-4							)							)




       @slow
       def    _UpperCamelCase					(						self							)      ->				Optional[int]:





             """simple docstring"""






             __SCREAMING_SNAKE_CASE       :Optional[Any]		    =  '''the [MASK] of Belgium is Brussels'''

             __SCREAMING_SNAKE_CASE       :Optional[Any]		    =  AutoTokenizer.from_pretrained('''uw-madison/nystromformer-512'''							)
             __SCREAMING_SNAKE_CASE       :str		    =  NystromformerForMaskedLM.from_pretrained('''uw-madison/nystromformer-512'''							)

             __SCREAMING_SNAKE_CASE       :Union[str, Any]		    =  tokenizer(SCREAMING_SNAKE_CASE__		,return_tensors='''pt'''							)

             with torch.no_grad():
                   __SCREAMING_SNAKE_CASE       :Union[str, Any]		    =  model(encoding.input_ids							).logits

             __SCREAMING_SNAKE_CASE       :List[str]		    =  token_logits[:, 2, :].argmax(-1							)[0]

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