




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





       '''simple docstring'''






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







             '''simple docstring'''







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







       def        UpperCamelCase_							(      self  ):







             '''simple docstring'''







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

             SCREAMING_SNAKE_CASE   :      Optional[Any]    					=						None
             if self.use_input_mask:
                   SCREAMING_SNAKE_CASE   :      Optional[int]    					=						random_attention_mask([self.batch_size, self.seq_length]  )

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

             SCREAMING_SNAKE_CASE   :      Union[str, Any]    					=						None
             SCREAMING_SNAKE_CASE   :      Union[str, Any]    					=						None
             SCREAMING_SNAKE_CASE   :      Optional[Any]    					=						None
             if self.use_labels:
                   SCREAMING_SNAKE_CASE   :      str    					=						ids_tensor([self.batch_size],							self.type_sequence_label_size  )
                   SCREAMING_SNAKE_CASE   :      List[str]    					=						ids_tensor([self.batch_size, self.seq_length],							self.num_labels  )
                   SCREAMING_SNAKE_CASE   :      List[str]    					=						ids_tensor([self.batch_size],							self.num_choices  )

             SCREAMING_SNAKE_CASE   :      Dict    					=						self.get_config()

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







       def        UpperCamelCase_							(      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=A,							initializer_range=self.initializer_range,							)







       def        UpperCamelCase_							(      self,							A,							A,							A,							A,							A,							A,							A  ):







             '''simple docstring'''







             SCREAMING_SNAKE_CASE   :      Tuple    					=						NystromformerModel(config=A  )
             model.to(A  )
             model.eval()
             SCREAMING_SNAKE_CASE   :      Tuple    					=						model(A,							attention_mask=A,							token_type_ids=A  )
             SCREAMING_SNAKE_CASE   :      Any    					=						model(A,							token_type_ids=A  )
             SCREAMING_SNAKE_CASE   :      str    					=						model(A  )
             self.parent.assertEqual(result.last_hidden_state.shape,							(self.batch_size, self.seq_length, self.hidden_size)  )







       def        UpperCamelCase_							(      self,							A,							A,							A,							A,							A,							A,							A  ):







             '''simple docstring'''







             SCREAMING_SNAKE_CASE   :      Dict    					=						NystromformerForMaskedLM(config=A  )
             model.to(A  )
             model.eval()
             SCREAMING_SNAKE_CASE   :      str    					=						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        UpperCamelCase_							(      self,							A,							A,							A,							A,							A,							A,							A  ):







             '''simple docstring'''







             SCREAMING_SNAKE_CASE   :      str    					=						NystromformerForQuestionAnswering(config=A  )
             model.to(A  )
             model.eval()
             SCREAMING_SNAKE_CASE   :      int    					=						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        UpperCamelCase_							(      self,							A,							A,							A,							A,							A,							A,							A  ):







             '''simple docstring'''







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







       def        UpperCamelCase_							(      self,							A,							A,							A,							A,							A,							A,							A  ):







             '''simple docstring'''







             SCREAMING_SNAKE_CASE   :      str    					=						self.num_labels
             SCREAMING_SNAKE_CASE   :      List[str]    					=						NystromformerForTokenClassification(config=A  )
             model.to(A  )
             model.eval()
             SCREAMING_SNAKE_CASE   :      Optional[Any]    					=						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        UpperCamelCase_							(      self,							A,							A,							A,							A,							A,							A,							A  ):







             '''simple docstring'''







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







       def        UpperCamelCase_							(      self  ):







             '''simple docstring'''







             SCREAMING_SNAKE_CASE   :      Tuple    					=						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
             )					,    
             )   :      Union[str, Any]    					=						config_and_inputs
             SCREAMING_SNAKE_CASE   :      int    					=						{'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 ):





       '''simple docstring'''






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







       def        UpperCamelCase_							(      self  ):







             '''simple docstring'''







             SCREAMING_SNAKE_CASE   :      List[Any]    					=						NystromformerModelTester(self  )
             SCREAMING_SNAKE_CASE   :      Optional[Any]    					=						ConfigTester(self,							config_class=A,							hidden_size=37  )







       def        UpperCamelCase_							(      self  ):







             '''simple docstring'''







             self.config_tester.run_common_tests()







       def        UpperCamelCase_							(      self  ):







             '''simple docstring'''







             SCREAMING_SNAKE_CASE   :      Dict    					=						self.model_tester.prepare_config_and_inputs()
             self.model_tester.create_and_check_model(*A  )







       def        UpperCamelCase_							(      self  ):







             '''simple docstring'''







             SCREAMING_SNAKE_CASE   :      str    					=						self.model_tester.prepare_config_and_inputs()
             for type in ["absolute", "relative_key", "relative_key_query"]:
                   SCREAMING_SNAKE_CASE   :      List[str]    					=						type
                   self.model_tester.create_and_check_model(*A  )







       def        UpperCamelCase_							(      self  ):







             '''simple docstring'''







             SCREAMING_SNAKE_CASE   :      Optional[Any]    					=						self.model_tester.prepare_config_and_inputs()
             self.model_tester.create_and_check_for_masked_lm(*A  )







       def        UpperCamelCase_							(      self  ):







             '''simple docstring'''







             SCREAMING_SNAKE_CASE   :      List[str]    					=						self.model_tester.prepare_config_and_inputs()
             self.model_tester.create_and_check_for_multiple_choice(*A  )







       def        UpperCamelCase_							(      self  ):







             '''simple docstring'''







             SCREAMING_SNAKE_CASE   :      Dict    					=						self.model_tester.prepare_config_and_inputs()
             self.model_tester.create_and_check_for_question_answering(*A  )







       def        UpperCamelCase_							(      self  ):







             '''simple docstring'''







             SCREAMING_SNAKE_CASE   :      Any    					=						self.model_tester.prepare_config_and_inputs()
             self.model_tester.create_and_check_for_sequence_classification(*A  )







       def        UpperCamelCase_							(      self  ):







             '''simple docstring'''







             SCREAMING_SNAKE_CASE   :      Optional[int]    					=						self.model_tester.prepare_config_and_inputs()
             self.model_tester.create_and_check_for_token_classification(*A  )







       @slow
       def        UpperCamelCase_							(      self  ):







             '''simple docstring'''







             for model_name in NYSTROMFORMER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
                   SCREAMING_SNAKE_CASE   :      Optional[int]    					=						NystromformerModel.from_pretrained(A  )
                   self.assertIsNotNone(A  )






@require_torch
class 							_a				(			unittest.TestCase ):





       '''simple docstring'''






       @slow
       def        UpperCamelCase_							(      self  ):







             '''simple docstring'''







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

             with torch.no_grad():
                   SCREAMING_SNAKE_CASE   :      Optional[int]    					=						model(A  )[0]

             SCREAMING_SNAKE_CASE   :      Any    					=						torch.Size((1, 6, 768)  )
             self.assertEqual(output.shape,							A  )

             SCREAMING_SNAKE_CASE   :      str    					=						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],							A,							atol=1E-4  )  )







       @slow
       def        UpperCamelCase_							(      self  ):







             '''simple docstring'''







             SCREAMING_SNAKE_CASE   :      Union[str, Any]    					=						'the [MASK] of Belgium is Brussels'

             SCREAMING_SNAKE_CASE   :      Dict    					=						AutoTokenizer.from_pretrained('uw-madison/nystromformer-512'  )
             SCREAMING_SNAKE_CASE   :      Dict    					=						NystromformerForMaskedLM.from_pretrained('uw-madison/nystromformer-512'  )

             SCREAMING_SNAKE_CASE   :      Dict    					=						tokenizer(A,							return_tensors='pt'  )

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

             SCREAMING_SNAKE_CASE   :      Optional[int]    					=						token_logits[:, 2, :].argmax(-1  )[0]

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








'''simple docstring'''



from math import ceil
from typing import List, Optional, Union

import numpy as np

from ...audio_utils import mel_filter_bank, spectrogram, window_function
from ...feature_extraction_sequence_utils import BatchFeature, SequenceFeatureExtractor
from ...utils import TensorType, logging


UpperCamelCase_    	=				logging.get_logger(__name__)






class 							_a				(			SCREAMING_SNAKE_CASE ):





       '''simple docstring'''






       A						:				str        =						['''audio_values''', '''audio_mask''']







       def __init__(      self,							A=2_048,							A=1,							A=[16, 16],							A=128,							A=44_100,							A=86,							A=2_048,							A=0.0,							**A,							):







             '''simple docstring'''







             super().__init__(
                 feature_size=A,							sampling_rate=A,							padding_value=A,							**A,							)

             SCREAMING_SNAKE_CASE   :      str    					=						spectrogram_length
             SCREAMING_SNAKE_CASE   :      Optional[Any]    					=						num_channels
             SCREAMING_SNAKE_CASE   :      List[str]    					=						patch_size
             SCREAMING_SNAKE_CASE   :      Optional[int]    					=						feature_size // self.patch_size[1]
             SCREAMING_SNAKE_CASE   :      Dict    					=						n_fft
             SCREAMING_SNAKE_CASE   :      Tuple    					=						sampling_rate // hop_length_to_sampling_rate
             SCREAMING_SNAKE_CASE   :      str    					=						sampling_rate
             SCREAMING_SNAKE_CASE   :      int    					=						padding_value
             SCREAMING_SNAKE_CASE   :      Any    					=						mel_filter_bank(
                 num_frequency_bins=1 + n_fft // 2,							num_mel_filters=A,							min_frequency=0.0,							max_frequency=2_20_50.0,							sampling_rate=A,							norm='slaney',							mel_scale='slaney',							).T







       def        UpperCamelCase_							(      self,							A  ):







             '''simple docstring'''







             SCREAMING_SNAKE_CASE   :      Optional[int]    					=						spectrogram(
                 A,							window_function(self.n_fft,							'hann'  ),							frame_length=self.n_fft,							hop_length=self.hop_length,							power=2.0,							mel_filters=self.mel_filters.T,							log_mel='dB',							db_range=80.0,							)
             SCREAMING_SNAKE_CASE   :      Union[str, Any]    					=						log_spec[:, :-1]
             SCREAMING_SNAKE_CASE   :      List[Any]    					=						log_spec - 20.0
             SCREAMING_SNAKE_CASE   :      Optional[Any]    					=						np.clip(log_spec / 40.0,							-2.0,							0.0  ) + 1.0
             return log_spec







       def __call__(      self,							A,							A = None,							A = True,							A = None,							A = False,							A = False,							**A,							):







             '''simple docstring'''







             if sampling_rate is not None:
                   if sampling_rate != self.sampling_rate:
                         raise ValueError(
                             'This feature extractor is set to support sampling rate'
                             F" of {self.sampling_rate}. Please make sure that the provided `raw_speech` input was sampled"
                             F" with {self.sampling_rate} and not {sampling_rate}."  )
             else:
                   logger.warning(
                       'It is strongly recommended to pass the `sampling_rate` argument to this function. '
                       'Failing to do so can result in silent errors that might be hard to debug.'  )

             SCREAMING_SNAKE_CASE   :      List[Any]    					=						isinstance(A,							np.ndarray  ) and len(raw_speech.shape  ) > 1
             if is_batched_numpy and len(raw_speech.shape  ) > 2:
                   raise ValueError(F"Only mono-channel audio is supported for input to {self}"  )
             SCREAMING_SNAKE_CASE   :      int    					=						is_batched_numpy or (
                 isinstance(A,							(list, tuple)  ) and (isinstance(raw_speech[0],							(np.ndarray, tuple, list)  ))
             )
             if is_batched:
                   SCREAMING_SNAKE_CASE   :      Union[str, Any]    					=						[np.asarray([speech],							dtype=np.floataa  ).T for speech in raw_speech]
             elif not is_batched and not isinstance(A,							np.ndarray  ):
                   SCREAMING_SNAKE_CASE   :      Any    					=						np.asarray(A,							dtype=np.floataa  )
             elif isinstance(A,							np.ndarray  ) and raw_speech.dtype is np.dtype(np.floataa  ):
                   SCREAMING_SNAKE_CASE   :      Optional[Any]    					=						raw_speech.astype(np.floataa  )
             # always return batch
             if not is_batched:
                   SCREAMING_SNAKE_CASE   :      Union[str, Any]    					=						[np.asarray([raw_speech]  ).T]

             # Convert audio signals to log mel spectrograms, truncate by time axis
             SCREAMING_SNAKE_CASE   :      int    					=						[
                 self._np_extract_fbank_features(waveform.squeeze()  ).T[: self.spectrogram_length] for waveform in raw_speech
             ]
             if isinstance(audio_features[0],							A  ):
                   SCREAMING_SNAKE_CASE   :      Union[str, Any]    					=						[np.asarray(A,							dtype=np.floataa  ) for feature in audio_features]

             # Create audio attention mask
             SCREAMING_SNAKE_CASE   :      Tuple    					=						max(
                 [ceil(feature.shape[0] / self.patch_size[0]  ) * self.freq_len for feature in audio_features]  )  # The maximum number of audio patches in a batch
             if return_attention_mask:
                   SCREAMING_SNAKE_CASE   :      List[Any]    					=						[
                       (ceil(feature.shape[0] / self.patch_size[0]  ) * self.freq_len) * [1]
                       + (max_patch_len - ceil(feature.shape[0] / self.patch_size[0]  ) * self.freq_len) * [0]
                       for feature in audio_features
                   ]
                   SCREAMING_SNAKE_CASE   :      Tuple    					=						np.array(A  ).astype(np.floataa  )

             # convert into correct format for padding
             SCREAMING_SNAKE_CASE   :      Tuple    					=						max_patch_len // self.freq_len * self.patch_size[0]  # The maximum audio size in a batch
             SCREAMING_SNAKE_CASE   :      Optional[Any]    					=						np.ones([len(A  ), 1, max_time_len, self.feature_size]  ).astype(np.floataa  )
             SCREAMING_SNAKE_CASE   :      Optional[int]    					=						padded_audio_features * self.padding_value
             for i in range(len(A  )  ):
                   SCREAMING_SNAKE_CASE   :      Optional[int]    					=						audio_features[i]
                   SCREAMING_SNAKE_CASE   :      Union[str, Any]    					=						feature

             # return as BatchFeature
             if return_attention_mask:
                   SCREAMING_SNAKE_CASE   :      Any    					=						{'audio_values': padded_audio_features, 'audio_mask': audio_mask}
             else:
                   SCREAMING_SNAKE_CASE   :      Dict    					=						{'audio_values': padded_audio_features}

             SCREAMING_SNAKE_CASE   :      str    					=						BatchFeature(data=A,							tensor_type=A  )
             return encoded_inputs


