






from __future__ import annotations

import inspect
import unittest

import numpy as np

from transformers import SwinConfig
from transformers.testing_utils import require_tf, require_vision, slow, to_atuple
from transformers.utils import cached_property, is_tf_available, is_vision_available

from ...test_configuration_common import ConfigTester
from ...test_modeling_tf_common import TFModelTesterMixin, floats_tensor, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_tf_available():
				import tensorflow as tf

				from transformers.models.swin.modeling_tf_swin import (
				    TF_SWIN_PRETRAINED_MODEL_ARCHIVE_LIST,
				    TFSwinForImageClassification,
				    TFSwinForMaskedImageModeling,
				    TFSwinModel,
				)


if is_vision_available():
				from PIL import Image

				from transformers import AutoImageProcessor





class   SCREAMING_SNAKE_CASE :





							"""simple docstring"""

							def __init__(     self:      str						,   __A:      List[str]						,   __A:      Union[str, Any]=13						,   __A:      List[str]=32						,   __A:      Optional[int]=2						,   __A:      Optional[int]=3						,   __A:      List[Any]=16						,   __A:      List[str]=[1, 2, 1]						,   __A:      Any=[2, 2, 4]						,   __A:      str=2						,   __A:      Dict=2.0						,   __A:      Union[str, Any]=True						,   __A:      Tuple=0.0						,   __A:      List[Any]=0.0						,   __A:      Tuple=0.1						,   __A:      List[str]="gelu"						,   __A:      Optional[Any]=False						,   __A:      Union[str, Any]=True						,   __A:      Dict=0.02						,   __A:      List[Any]=1e-5						,   __A:      Optional[Any]=True						,   __A:      Tuple=None						,   __A:      str=True						,   __A:      str=10						,   __A:      List[Any]=8						,   )			-> None:
												_A									=       parent
												_A									=       batch_size
												_A									=       image_size
												_A									=       patch_size
												_A									=       num_channels
												_A									=       embed_dim
												_A									=       depths
												_A									=       num_heads
												_A									=       window_size
												_A									=       mlp_ratio
												_A									=       qkv_bias
												_A									=       hidden_dropout_prob
												_A									=       attention_probs_dropout_prob
												_A									=       drop_path_rate
												_A									=       hidden_act
												_A									=       use_absolute_embeddings
												_A									=       patch_norm
												_A									=       layer_norm_eps
												_A									=       initializer_range
												_A									=       is_training
												_A									=       scope
												_A									=       use_labels
												_A									=       type_sequence_label_size
												_A									=       encoder_stride

							def 					__A       (     self:      Union[str, Any]      )			-> Dict:
												_A									=       floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]      )

												_A									=       None
												if self.use_labels:
																	_A									=       ids_tensor([self.batch_size]						,   self.type_sequence_label_size      )

												_A									=       self.get_config()

												return config, pixel_values, labels

							def 					__A       (     self:      Dict      )			-> List[str]:
												return SwinConfig(
												    image_size=self.image_size						,   patch_size=self.patch_size						,   num_channels=self.num_channels						,   embed_dim=self.embed_dim						,   depths=self.depths						,   num_heads=self.num_heads						,   window_size=self.window_size						,   mlp_ratio=self.mlp_ratio						,   qkv_bias=self.qkv_bias						,   hidden_dropout_prob=self.hidden_dropout_prob						,   attention_probs_dropout_prob=self.attention_probs_dropout_prob						,   drop_path_rate=self.drop_path_rate						,   hidden_act=self.hidden_act						,   use_absolute_embeddings=self.use_absolute_embeddings						,   path_norm=self.patch_norm						,   layer_norm_eps=self.layer_norm_eps						,   initializer_range=self.initializer_range						,   encoder_stride=self.encoder_stride						,   )

							def 					__A       (     self:      List[Any]						,   __A:      Any						,   __A:      str						,   __A:      Any      )			-> int:
												_A									=       TFSwinModel(config=__A      )
												_A									=       model(__A      )

												_A									=       ((config.image_size // config.patch_size) ** 2) // (4 ** (len(config.depths      ) - 1))
												_A									=       int(config.embed_dim * 2 ** (len(config.depths      ) - 1)      )

												self.parent.assertEqual(result.last_hidden_state.shape						,   (self.batch_size, expected_seq_len, expected_dim)      )

							def 					__A       (     self:      List[str]						,   __A:      Dict						,   __A:      List[Any]						,   __A:      Optional[int]      )			-> str:
												_A									=       TFSwinForMaskedImageModeling(config=__A      )
												_A									=       model(__A      )
												self.parent.assertEqual(
												    result.logits.shape						,   (self.batch_size, self.num_channels, self.image_size, self.image_size)      )

												# test greyscale images
												_A									=       1
												_A									=       TFSwinForMaskedImageModeling(__A      )

												_A									=       floats_tensor([self.batch_size, 1, self.image_size, self.image_size]      )
												_A									=       model(__A      )
												self.parent.assertEqual(result.logits.shape						,   (self.batch_size, 1, self.image_size, self.image_size)      )

							def 					__A       (     self:      str						,   __A:      Union[str, Any]						,   __A:      Optional[int]						,   __A:      int      )			-> Optional[Any]:
												_A									=       self.type_sequence_label_size
												_A									=       TFSwinForImageClassification(__A      )
												_A									=       model(__A						,   labels=__A      )
												self.parent.assertEqual(result.logits.shape						,   (self.batch_size, self.type_sequence_label_size)      )

												# test greyscale images
												_A									=       1
												_A									=       TFSwinForImageClassification(__A      )
												_A									=       floats_tensor([self.batch_size, 1, self.image_size, self.image_size]      )
												_A									=       model(__A      )
												self.parent.assertEqual(result.logits.shape						,   (self.batch_size, self.type_sequence_label_size)      )



							def 					__A       (     self:      Optional[int]      )			-> Union[str, Any]:
												_A									=       self.prepare_config_and_inputs()
												_A							,_A							,_A									=       config_and_inputs
												_A									=       {'''pixel_values''': pixel_values}
												return config, inputs_dict





@require_tf
class   SCREAMING_SNAKE_CASE (       snake_case     ,		snake_case     ,		unittest.TestCase    ):





							"""simple docstring"""

							A_       =    (
							    (
							        TFSwinModel,
							        TFSwinForImageClassification,
							        TFSwinForMaskedImageModeling,
							    )
							    if is_tf_available()
							    else ()
							)
							A_       =    (
							    {"feature-extraction": TFSwinModel, "image-classification": TFSwinForImageClassification}
							    if is_tf_available()
							    else {}
							)

							A_       =    False
							A_       =    False
							A_       =    False
							A_       =    False

							def 					__A       (     self:      Dict      )			-> Dict:
												_A									=       TFSwinModelTester(self      )
												_A									=       ConfigTester(self						,   config_class=__A						,   embed_dim=37      )

							def 					__A       (     self:      str      )			-> Tuple:
												self.create_and_test_config_common_properties()
												self.config_tester.create_and_test_config_to_json_string()
												self.config_tester.create_and_test_config_to_json_file()
												self.config_tester.create_and_test_config_from_and_save_pretrained()
												self.config_tester.create_and_test_config_with_num_labels()
												self.config_tester.check_config_can_be_init_without_params()
												self.config_tester.check_config_arguments_init()

							def 					__A       (     self:      str      )			-> Any:
												return

							def 					__A       (     self:      Union[str, Any]      )			-> Dict:
												_A									=       self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_model(*__A      )

							def 					__A       (     self:      List[str]      )			-> List[str]:
												_A									=       self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_for_masked_image_modeling(*__A      )

							def 					__A       (     self:      Any      )			-> Optional[int]:
												_A									=       self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_for_image_classification(*__A      )

							@unittest.skip(reason='''Swin does not use inputs_embeds'''      )
							def 					__A       (     self:      int      )			-> Union[str, Any]:
												pass

							def 					__A       (     self:      Tuple      )			-> Tuple:
												_A							,_A									=       self.model_tester.prepare_config_and_inputs_for_common()

												for model_class in self.all_model_classes:
																	_A									=       model_class(__A      )
																	self.assertIsInstance(model.get_input_embeddings()						,   tf.keras.layers.Layer      )
																	_A									=       model.get_output_embeddings()
																	self.assertTrue(x is None or isinstance(__A						,   tf.keras.layers.Dense      )      )

							def 					__A       (     self:      List[Any]      )			-> Optional[Any]:
												_A							,_A									=       self.model_tester.prepare_config_and_inputs_for_common()

												for model_class in self.all_model_classes:
																	_A									=       model_class(__A      )
																	_A									=       inspect.signature(model.call      )
																	# signature.parameters is an OrderedDict => so arg_names order is deterministic
																	_A									=       [*signature.parameters.keys()]

																	_A									=       ['''pixel_values''']
																	self.assertListEqual(arg_names[:1]						,   __A      )

							def 					__A       (     self:      Union[str, Any]      )			-> Union[str, Any]:
												_A							,_A									=       self.model_tester.prepare_config_and_inputs_for_common()
												_A									=       True

												for model_class in self.all_model_classes:
																	_A									=       True
																	_A									=       False
																	_A									=       True
																	_A									=       model_class(__A      )
																	_A									=       model(**self._prepare_for_class(__A						,   __A      )      )
																	_A									=       outputs.attentions
																	_A									=       len(self.model_tester.depths      )
																	self.assertEqual(len(__A      )						,   __A      )

																	# check that output_attentions also work using config
																	del inputs_dict["output_attentions"]
																	_A									=       True
																	_A									=       config.window_size**2
																	_A									=       model_class(__A      )
																	_A									=       model(**self._prepare_for_class(__A						,   __A      )      )
																	_A									=       outputs.attentions
																	self.assertEqual(len(__A      )						,   __A      )

																	self.assertListEqual(
																	    list(attentions[0].shape[-3:]      )						,   [self.model_tester.num_heads[0], window_size_squared, window_size_squared]						,   )
																	_A									=       len(__A      )

																	# Check attention is always last and order is fine
																	_A									=       True
																	_A									=       True
																	_A									=       model_class(__A      )
																	_A									=       model(**self._prepare_for_class(__A						,   __A      )      )

																	if hasattr(self.model_tester						,   '''num_hidden_states_types'''      ):
																						_A									=       self.model_tester.num_hidden_states_types
																	else:
																						# also another +1 for reshaped_hidden_states
																						_A									=       2
																	self.assertEqual(out_len + added_hidden_states						,   len(__A      )      )

																	_A									=       outputs.attentions

																	self.assertEqual(len(__A      )						,   __A      )

																	self.assertListEqual(
																	    list(self_attentions[0].shape[-3:]      )						,   [self.model_tester.num_heads[0], window_size_squared, window_size_squared]						,   )

							def 					__A       (     self:      List[str]						,   __A:      str						,   __A:      Tuple						,   __A:      Optional[int]						,   __A:      List[str]      )			-> List[str]:
												_A									=       model_class(__A      )
												_A									=       model(**self._prepare_for_class(__A						,   __A      )      )
												_A									=       outputs.hidden_states

												_A									=       getattr(
												    self.model_tester						,   '''expected_num_hidden_layers'''						,   len(self.model_tester.depths      ) + 1      )
												self.assertEqual(len(__A      )						,   __A      )

												# Swin has a different seq_length
												_A									=       to_atuple(config.patch_size      )

												_A									=       (image_size[1] // patch_size[1]) * (image_size[0] // patch_size[0])

												self.assertListEqual(
												    list(hidden_states[0].shape[-2:]      )						,   [num_patches, self.model_tester.embed_dim]						,   )

												_A									=       outputs.reshaped_hidden_states
												self.assertEqual(len(__A      )						,   __A      )

												_A							,_A							,_A							,_A									=       reshaped_hidden_states[0].shape

												_A									=       tf.reshape(reshaped_hidden_states[0]						,   (batch_size, num_channels, height * width)      )
												_A									=       tf.transpose(__A						,   (0, 2, 1)      )

												self.assertListEqual(
												    list(reshaped_hidden_states.shape[-2:]      )						,   [num_patches, self.model_tester.embed_dim]						,   )

							def 					__A       (     self:      List[str]      )			-> Optional[int]:
												_A							,_A									=       self.model_tester.prepare_config_and_inputs_for_common()

												_A									=       to_atuple(self.model_tester.image_size      )

												for model_class in self.all_model_classes:
																	_A									=       True
																	self.check_hidden_states_output(__A						,   __A						,   __A						,   __A      )

																	# check that output_hidden_states also work using config
																	del inputs_dict["output_hidden_states"]
																	_A									=       True

																	self.check_hidden_states_output(__A						,   __A						,   __A						,   __A      )

							def 					__A       (     self:      Optional[int]      )			-> List[str]:
												_A							,_A									=       self.model_tester.prepare_config_and_inputs_for_common()
												_A									=       3

												_A									=       to_atuple(self.model_tester.image_size      )
												_A									=       to_atuple(config.patch_size      )

												_A									=       image_size[0] + patch_size[0] - (image_size[0] % patch_size[0])
												_A									=       image_size[1] + patch_size[1] - (image_size[1] % patch_size[1])

												for model_class in self.all_model_classes:
																	_A									=       True
																	self.check_hidden_states_output(__A						,   __A						,   __A						,   (padded_height, padded_width)      )

																	# check that output_hidden_states also work using config
																	del inputs_dict["output_hidden_states"]
																	_A									=       True
																	self.check_hidden_states_output(__A						,   __A						,   __A						,   (padded_height, padded_width)      )



							@slow
							def 					__A       (     self:      int      )			-> Tuple:
												for model_name in TF_SWIN_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																	_A									=       TFSwinModel.from_pretrained(__A      )
																	self.assertIsNotNone(__A      )





@require_vision
@require_tf
class   SCREAMING_SNAKE_CASE (       unittest.TestCase    ):





							"""simple docstring"""

							@cached_property
							def 					__A       (     self:      Tuple      )			-> Optional[Any]:
												return (
												    AutoImageProcessor.from_pretrained('''microsoft/swin-tiny-patch4-window7-224'''      )
												    if is_vision_available()
												    else None
												)



							@slow
							def 					__A       (     self:      Optional[Any]      )			-> List[str]:
												_A									=       TFSwinForImageClassification.from_pretrained('''microsoft/swin-tiny-patch4-window7-224'''      )
												_A									=       self.default_image_processor

												_A									=       Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''      )
												_A									=       image_processor(images=__A						,   return_tensors='''tf'''      )

												# forward pass
												_A									=       model(__A      )

												# verify the logits
												_A									=       tf.TensorShape((1, 10_00)      )
												self.assertEqual(outputs.logits.shape						,   __A      )
												_A									=       tf.constant([-0.0_948, -0.6_454, -0.0_921]      )
												self.assertTrue(np.allclose(outputs.logits[0, :3]						,   __A						,   atol=1e-4      )      )











from ...configuration_utils import PretrainedConfig
from ...utils import logging


__A          = logging.get_logger(__name__)

__A          = {
    'uw-madison/nystromformer-512': 'https://huggingface.co/uw-madison/nystromformer-512/resolve/main/config.json',
    # See all Nystromformer models at https://huggingface.co/models?filter=nystromformer
}





class   SCREAMING_SNAKE_CASE (       snake_case    ):





							"""simple docstring"""

							A_       =    "nystromformer"

							def __init__(     self:      List[str]						,   __A:      Dict=3_00_00						,   __A:      int=7_68						,   __A:      int=12						,   __A:      Union[str, Any]=12						,   __A:      List[str]=30_72						,   __A:      Tuple="gelu_new"						,   __A:      Optional[Any]=0.1						,   __A:      Union[str, Any]=0.1						,   __A:      int=5_10						,   __A:      List[Any]=2						,   __A:      List[Any]=64						,   __A:      Optional[Any]=64						,   __A:      str=65						,   __A:      List[str]=False						,   __A:      Tuple=0.02						,   __A:      Dict=1e-5						,   __A:      int=1						,   __A:      str=0						,   __A:      int=2						,   **__A:      Union[str, Any]						,   )			-> int:
												_A									=       vocab_size
												_A									=       max_position_embeddings
												_A									=       hidden_size
												_A									=       num_hidden_layers
												_A									=       num_attention_heads
												_A									=       intermediate_size
												_A									=       hidden_act
												_A									=       hidden_dropout_prob
												_A									=       attention_probs_dropout_prob
												_A									=       initializer_range
												_A									=       type_vocab_size
												_A									=       segment_means_seq_len
												_A									=       num_landmarks
												_A									=       conv_kernel_size
												_A									=       inv_coeff_init_option
												_A									=       layer_norm_eps
												super().__init__(pad_token_id=__A						,   bos_token_id=__A						,   eos_token_id=__A						,   **__A      )



