


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

		"""simple docstring"""

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

							'''simple docstring'''


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




		def        lowerCAmelCase__							(						self				):

							'''simple docstring'''


							UpperCamelCase__  :int				   =					ids_tensor([self.batch_size, self.seq_length]       ,  self.vocab_size				)

							UpperCamelCase__  :int				   =					None
							if self.use_input_mask:
												UpperCamelCase__  :Tuple				   =					random_attention_mask([self.batch_size, self.seq_length]				)

							UpperCamelCase__  :Dict				   =					None
							if self.use_token_type_ids:
												UpperCamelCase__  :Optional[int]				   =					ids_tensor([self.batch_size, self.seq_length]       ,  self.type_vocab_size				)

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

							UpperCamelCase__  :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=UpperCamelCase_       ,  initializer_range=self.initializer_range       ,  )




		def        lowerCAmelCase__							(						self       ,  UpperCamelCase_       ,  UpperCamelCase_       ,  UpperCamelCase_       ,  UpperCamelCase_       ,  UpperCamelCase_       ,  UpperCamelCase_       ,  UpperCamelCase_				):

							'''simple docstring'''


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




		def        lowerCAmelCase__							(						self       ,  UpperCamelCase_       ,  UpperCamelCase_       ,  UpperCamelCase_       ,  UpperCamelCase_       ,  UpperCamelCase_       ,  UpperCamelCase_       ,  UpperCamelCase_				):

							'''simple docstring'''


							UpperCamelCase__  :List[str]				   =					NystromformerForMaskedLM(config=UpperCamelCase_				)
							model.to(UpperCamelCase_				)
							model.eval()
							UpperCamelCase__  :int				   =					model(UpperCamelCase_       ,  attention_mask=UpperCamelCase_       ,  token_type_ids=UpperCamelCase_       ,  labels=UpperCamelCase_				)
							self.parent.assertEqual(result.logits.shape       ,  (self.batch_size, self.seq_length, self.vocab_size)				)




		def        lowerCAmelCase__							(						self       ,  UpperCamelCase_       ,  UpperCamelCase_       ,  UpperCamelCase_       ,  UpperCamelCase_       ,  UpperCamelCase_       ,  UpperCamelCase_       ,  UpperCamelCase_				):

							'''simple docstring'''


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

							'''simple docstring'''


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




		def        lowerCAmelCase__							(						self       ,  UpperCamelCase_       ,  UpperCamelCase_       ,  UpperCamelCase_       ,  UpperCamelCase_       ,  UpperCamelCase_       ,  UpperCamelCase_       ,  UpperCamelCase_				):

							'''simple docstring'''


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




		def        lowerCAmelCase__							(						self       ,  UpperCamelCase_       ,  UpperCamelCase_       ,  UpperCamelCase_       ,  UpperCamelCase_       ,  UpperCamelCase_       ,  UpperCamelCase_       ,  UpperCamelCase_				):

							'''simple docstring'''


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




		def        lowerCAmelCase__							(						self				):

							'''simple docstring'''


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



@require_torch
class   lowercase      (				A__      ,						A__      ,						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'''


							UpperCamelCase__  :Tuple				   =					NystromformerModelTester(self				)
							UpperCamelCase__  :Tuple				   =					ConfigTester(self       ,  config_class=UpperCamelCase_       ,  hidden_size=37				)




		def        lowerCAmelCase__							(						self				):

							'''simple docstring'''


							self.config_tester.run_common_tests()




		def        lowerCAmelCase__							(						self				):

							'''simple docstring'''


							UpperCamelCase__  :Any				   =					self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_model(*UpperCamelCase_				)




		def        lowerCAmelCase__							(						self				):

							'''simple docstring'''


							UpperCamelCase__  :Tuple				   =					self.model_tester.prepare_config_and_inputs()
							for type in ["absolute", "relative_key", "relative_key_query"]:
												UpperCamelCase__  :Optional[Any]				   =					type
												self.model_tester.create_and_check_model(*UpperCamelCase_				)




		def        lowerCAmelCase__							(						self				):

							'''simple docstring'''


							UpperCamelCase__  :Optional[int]				   =					self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_for_masked_lm(*UpperCamelCase_				)




		def        lowerCAmelCase__							(						self				):

							'''simple docstring'''


							UpperCamelCase__  :Tuple				   =					self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_for_multiple_choice(*UpperCamelCase_				)




		def        lowerCAmelCase__							(						self				):

							'''simple docstring'''


							UpperCamelCase__  :Optional[Any]				   =					self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_for_question_answering(*UpperCamelCase_				)




		def        lowerCAmelCase__							(						self				):

							'''simple docstring'''


							UpperCamelCase__  :Dict				   =					self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_for_sequence_classification(*UpperCamelCase_				)




		def        lowerCAmelCase__							(						self				):

							'''simple docstring'''


							UpperCamelCase__  :Any				   =					self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_for_token_classification(*UpperCamelCase_				)




		@slow
		def        lowerCAmelCase__							(						self				):

							'''simple docstring'''


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



@require_torch
class   lowercase      (				unittest.TestCase   ):

		"""simple docstring"""

		@slow
		def        lowerCAmelCase__							(						self				):

							'''simple docstring'''


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

							with torch.no_grad():
												UpperCamelCase__  :List[str]				   =					model(UpperCamelCase_				)[0]

							UpperCamelCase__  :Optional[int]				   =					torch.Size((1, 6, 768)				)
							self.assertEqual(output.shape       ,  UpperCamelCase_				)

							UpperCamelCase__  :Optional[Any]				   =					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]       ,  UpperCamelCase_       ,  atol=1e-4				)				)




		@slow
		def        lowerCAmelCase__							(						self				):

							'''simple docstring'''


							UpperCamelCase__  :Dict				   =					'''the [MASK] of Belgium is Brussels'''

							UpperCamelCase__  :Union[str, Any]				   =					AutoTokenizer.from_pretrained('''uw-madison/nystromformer-512'''				)
							UpperCamelCase__  :int				   =					NystromformerForMaskedLM.from_pretrained('''uw-madison/nystromformer-512'''				)

							UpperCamelCase__  :Dict				   =					tokenizer(UpperCamelCase_       ,  return_tensors='''pt'''				)

							with torch.no_grad():
												UpperCamelCase__  :Optional[int]				   =					model(encoding.input_ids				).logits

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

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



'''simple docstring'''
import warnings

from ...processing_utils import ProcessorMixin
from ...tokenization_utils_base import BatchEncoding
class   lowercase      (				A__   ):

		"""simple docstring"""

		_a       					=							['image_processor', 'tokenizer']
		_a       					=							'ChineseCLIPImageProcessor'
		_a       					=							('BertTokenizer', 'BertTokenizerFast')




		def __init__(						self       ,  UpperCamelCase_=None       ,  UpperCamelCase_=None       ,  **UpperCamelCase_				):

							'''simple docstring'''


							UpperCamelCase__  :List[Any]				   =					None
							if "feature_extractor" in kwargs:
												warnings.warn(
												    '''The `feature_extractor` argument is deprecated and will be removed in v5, use `image_processor`'''
												    ''' instead.'''       ,  UpperCamelCase_       ,  )
												UpperCamelCase__  :List[Any]				   =					kwargs.pop('''feature_extractor'''				)

							UpperCamelCase__  :List[str]				   =					image_processor if image_processor is not None else feature_extractor
							if image_processor is None:
												raise ValueError('''You need to specify an `image_processor`.'''				)
							if tokenizer is None:
												raise ValueError('''You need to specify a `tokenizer`.'''				)

							super().__init__(UpperCamelCase_       ,  UpperCamelCase_				)
							UpperCamelCase__  :Any				   =					self.image_processor




		def __call__(						self       ,  UpperCamelCase_=None       ,  UpperCamelCase_=None       ,  UpperCamelCase_=None       ,  **UpperCamelCase_				):

							'''simple docstring'''


							if text is None and images is None:
												raise ValueError('''You have to specify either text or images. Both cannot be none.'''				)

							if text is not None:
												UpperCamelCase__  :Tuple				   =					self.tokenizer(UpperCamelCase_       ,  return_tensors=UpperCamelCase_       ,  **UpperCamelCase_				)

							if images is not None:
												UpperCamelCase__  :Any				   =					self.image_processor(UpperCamelCase_       ,  return_tensors=UpperCamelCase_       ,  **UpperCamelCase_				)

							if text is not None and images is not None:
												UpperCamelCase__  :Optional[int]				   =					image_features.pixel_values
												return encoding
							elif text is not None:
												return encoding
							else:
												return BatchEncoding(data=dict(**UpperCamelCase_				)       ,  tensor_type=UpperCamelCase_				)




		def        lowerCAmelCase__							(						self       ,  *UpperCamelCase_       ,  **UpperCamelCase_				):

							'''simple docstring'''


							return self.tokenizer.batch_decode(*UpperCamelCase_       ,  **UpperCamelCase_				)




		def        lowerCAmelCase__							(						self       ,  *UpperCamelCase_       ,  **UpperCamelCase_				):

							'''simple docstring'''


							return self.tokenizer.decode(*UpperCamelCase_       ,  **UpperCamelCase_				)




		@property
		def        lowerCAmelCase__							(						self				):

							'''simple docstring'''


							UpperCamelCase__  :int				   =					self.tokenizer.model_input_names
							UpperCamelCase__  :Any				   =					self.image_processor.model_input_names
							return list(dict.fromkeys(tokenizer_input_names + image_processor_input_names				)				)




		@property
		def        lowerCAmelCase__							(						self				):

							'''simple docstring'''


							warnings.warn(
							    '''`feature_extractor_class` is deprecated and will be removed in v5. Use `image_processor_class` instead.'''       ,  UpperCamelCase_       ,  )
							return self.image_processor_class