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



			def __init__(				self   :   List[str]			,	A__   :   Union[str, Any]			,	A__   :   Union[str, Any]=13			,	A__   :   Union[str, Any]=32			,	A__   :   Union[str, Any]=2			,	A__   :   Optional[int]=3			,	A__   :   List[Any]=16			,	A__   :   Tuple=[1, 2, 1]			,	A__   :   Tuple=[2, 2, 4]			,	A__   :   Optional[int]=2			,	A__   :   Optional[int]=2.0			,	A__   :   Optional[int]=True			,	A__   :   Tuple=0.0			,	A__   :   Dict=0.0			,	A__   :   Optional[int]=0.1			,	A__   :   Optional[int]="gelu"			,	A__   :   Any=False			,	A__   :   Dict=True			,	A__   :   List[Any]=0.02			,	A__   :   Union[str, Any]=1E-5			,	A__   :   Tuple=True			,	A__   :   Tuple=None			,	A__   :   List[Any]=True			,	A__   :   Optional[Any]=10			,	A__   :   Optional[int]=8			,	)							->     None:




									'''simple docstring'''




									snake_case_     :			Union[str, Any]       =       parent
									snake_case_     :			List[Any]       =       batch_size
									snake_case_     :			Union[str, Any]       =       image_size
									snake_case_     :			Tuple       =       patch_size
									snake_case_     :			int       =       num_channels
									snake_case_     :			Optional[int]       =       embed_dim
									snake_case_     :			str       =       depths
									snake_case_     :			Union[str, Any]       =       num_heads
									snake_case_     :			Union[str, Any]       =       window_size
									snake_case_     :			int       =       mlp_ratio
									snake_case_     :			Tuple       =       qkv_bias
									snake_case_     :			Dict       =       hidden_dropout_prob
									snake_case_     :			List[Any]       =       attention_probs_dropout_prob
									snake_case_     :			Tuple       =       drop_path_rate
									snake_case_     :			Union[str, Any]       =       hidden_act
									snake_case_     :			Optional[int]       =       use_absolute_embeddings
									snake_case_     :			Any       =       patch_norm
									snake_case_     :			str       =       layer_norm_eps
									snake_case_     :			List[Any]       =       initializer_range
									snake_case_     :			int       =       is_training
									snake_case_     :			Tuple       =       scope
									snake_case_     :			List[str]       =       use_labels
									snake_case_     :			Optional[Any]       =       type_sequence_label_size
									snake_case_     :			Optional[int]       =       encoder_stride



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




									'''simple docstring'''




									snake_case_     :			Optional[int]       =       floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]   )

									snake_case_     :			Tuple       =       None
									if self.use_labels:
															snake_case_     :			Optional[Any]       =       ids_tensor([self.batch_size]			,	self.type_sequence_label_size   )

									snake_case_     :			Any       =       self.get_config()

									return config, pixel_values, labels



			def 					UpperCAmelCase__       (				self   :   Tuple   )							->     Optional[int]:




									'''simple docstring'''




									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 					UpperCAmelCase__       (				self   :   Tuple			,	A__   :   Union[str, Any]			,	A__   :   Optional[int]			,	A__   :   Any   )							->     Tuple:




									'''simple docstring'''




									snake_case_     :			Optional[int]       =       TFSwinModel(config=A__   )
									snake_case_     :			Dict       =       model(A__   )

									snake_case_     :			List[str]       =       ((config.image_size // config.patch_size) ** 2) // (4 ** (len(config.depths   ) - 1))
									snake_case_     :			List[Any]       =       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 					UpperCAmelCase__       (				self   :   List[Any]			,	A__   :   Any			,	A__   :   int			,	A__   :   Dict   )							->     List[Any]:




									'''simple docstring'''




									snake_case_     :			int       =       TFSwinForMaskedImageModeling(config=A__   )
									snake_case_     :			int       =       model(A__   )
									self.parent.assertEqual(
									    result.logits.shape			,	(self.batch_size, self.num_channels, self.image_size, self.image_size)   )

									# test greyscale images
									snake_case_     :			List[Any]       =       1
									snake_case_     :			Union[str, Any]       =       TFSwinForMaskedImageModeling(A__   )

									snake_case_     :			Union[str, Any]       =       floats_tensor([self.batch_size, 1, self.image_size, self.image_size]   )
									snake_case_     :			List[Any]       =       model(A__   )
									self.parent.assertEqual(result.logits.shape			,	(self.batch_size, 1, self.image_size, self.image_size)   )



			def 					UpperCAmelCase__       (				self   :   Optional[int]			,	A__   :   List[str]			,	A__   :   Optional[Any]			,	A__   :   List[str]   )							->     List[Any]:




									'''simple docstring'''




									snake_case_     :			Optional[Any]       =       self.type_sequence_label_size
									snake_case_     :			Dict       =       TFSwinForImageClassification(A__   )
									snake_case_     :			int       =       model(A__			,	labels=A__   )
									self.parent.assertEqual(result.logits.shape			,	(self.batch_size, self.type_sequence_label_size)   )

									# test greyscale images
									snake_case_     :			Union[str, Any]       =       1
									snake_case_     :			int       =       TFSwinForImageClassification(A__   )
									snake_case_     :			Any       =       floats_tensor([self.batch_size, 1, self.image_size, self.image_size]   )
									snake_case_     :			str       =       model(A__   )
									self.parent.assertEqual(result.logits.shape			,	(self.batch_size, self.type_sequence_label_size)   )



			def 					UpperCAmelCase__       (				self   :   Any   )							->     Optional[Any]:




									'''simple docstring'''




									snake_case_     :			List[str]       =       self.prepare_config_and_inputs()
									snake_case_     :			Any       =       config_and_inputs
									snake_case_     :			List[str]       =       {"pixel_values": pixel_values}
									return config, inputs_dict



@require_tf
class 					snake_case__       (				_UpperCamelCase					,						_UpperCamelCase					,						unittest.TestCase							):
			_SCREAMING_SNAKE_CASE	: Optional[int]						    =      (
			    (
			        TFSwinModel,
			        TFSwinForImageClassification,
			        TFSwinForMaskedImageModeling,
			    )
			    if is_tf_available()
			    else ()
			)
			_SCREAMING_SNAKE_CASE	: Dict						    =      (
			    {"feature-extraction": TFSwinModel, "image-classification": TFSwinForImageClassification}
			    if is_tf_available()
			    else {}
			)

			_SCREAMING_SNAKE_CASE	: Tuple						    =      False
			_SCREAMING_SNAKE_CASE	: Dict						    =      False
			_SCREAMING_SNAKE_CASE	: List[str]						    =      False
			_SCREAMING_SNAKE_CASE	: Optional[int]						    =      False



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




									'''simple docstring'''




									snake_case_     :			Optional[int]       =       TFSwinModelTester(self   )
									snake_case_     :			List[str]       =       ConfigTester(self			,	config_class=A__			,	embed_dim=37   )



			def 					UpperCAmelCase__       (				self   :   Optional[Any]   )							->     Optional[Any]:




									'''simple docstring'''




									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 					UpperCAmelCase__       (				self   :   Union[str, Any]   )							->     Any:




									'''simple docstring'''




									return



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




									'''simple docstring'''




									snake_case_     :			Optional[Any]       =       self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_model(*A__   )



			def 					UpperCAmelCase__       (				self   :   List[str]   )							->     Dict:




									'''simple docstring'''




									snake_case_     :			Optional[int]       =       self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_for_masked_image_modeling(*A__   )



			def 					UpperCAmelCase__       (				self   :   Dict   )							->     Optional[int]:




									'''simple docstring'''




									snake_case_     :			Dict       =       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 					UpperCAmelCase__       (				self   :   List[str]   )							->     Optional[int]:




									'''simple docstring'''




									pass



			def 					UpperCAmelCase__       (				self   :   str   )							->     Tuple:




									'''simple docstring'''




									snake_case_     :			Tuple       =       self.model_tester.prepare_config_and_inputs_for_common()

									for model_class in self.all_model_classes:
															snake_case_     :			List[str]       =       model_class(A__   )
															self.assertIsInstance(model.get_input_embeddings()			,	tf.keras.layers.Layer   )
															snake_case_     :			int       =       model.get_output_embeddings()
															self.assertTrue(x is None or isinstance(A__			,	tf.keras.layers.Dense   )   )



			def 					UpperCAmelCase__       (				self   :   Any   )							->     str:




									'''simple docstring'''




									snake_case_     :			Optional[int]       =       self.model_tester.prepare_config_and_inputs_for_common()

									for model_class in self.all_model_classes:
															snake_case_     :			Optional[Any]       =       model_class(A__   )
															snake_case_     :			Union[str, Any]       =       inspect.signature(model.call   )
															# signature.parameters is an OrderedDict => so arg_names order is deterministic
															snake_case_     :			List[str]       =       [*signature.parameters.keys()]

															snake_case_     :			str       =       ["pixel_values"]
															self.assertListEqual(arg_names[:1]			,	A__   )



			def 					UpperCAmelCase__       (				self   :   int   )							->     Optional[Any]:




									'''simple docstring'''




									snake_case_     :			int       =       self.model_tester.prepare_config_and_inputs_for_common()
									snake_case_     :			Any       =       True

									for model_class in self.all_model_classes:
															snake_case_     :			str       =       True
															snake_case_     :			int       =       False
															snake_case_     :			List[Any]       =       True
															snake_case_     :			Any       =       model_class(A__   )
															snake_case_     :			List[Any]       =       model(**self._prepare_for_class(A__			,	A__   )   )
															snake_case_     :			Union[str, Any]       =       outputs.attentions
															snake_case_     :			Tuple       =       len(self.model_tester.depths   )
															self.assertEqual(len(A__   )			,	A__   )

															# check that output_attentions also work using config
															del inputs_dict["output_attentions"]
															snake_case_     :			Optional[int]       =       True
															snake_case_     :			Optional[int]       =       config.window_size**2
															snake_case_     :			int       =       model_class(A__   )
															snake_case_     :			Tuple       =       model(**self._prepare_for_class(A__			,	A__   )   )
															snake_case_     :			Dict       =       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]			,	)
															snake_case_     :			int       =       len(A__   )

															# Check attention is always last and order is fine
															snake_case_     :			List[str]       =       True
															snake_case_     :			Optional[Any]       =       True
															snake_case_     :			Dict       =       model_class(A__   )
															snake_case_     :			Dict       =       model(**self._prepare_for_class(A__			,	A__   )   )

															if hasattr(self.model_tester			,	"num_hidden_states_types"   ):
																					snake_case_     :			str       =       self.model_tester.num_hidden_states_types
															else:
																					# also another +1 for reshaped_hidden_states
																					snake_case_     :			Union[str, Any]       =       2
															self.assertEqual(out_len + added_hidden_states			,	len(A__   )   )

															snake_case_     :			List[Any]       =       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 					UpperCAmelCase__       (				self   :   Any			,	A__   :   Dict			,	A__   :   Dict			,	A__   :   List[str]			,	A__   :   str   )							->     Optional[int]:




									'''simple docstring'''




									snake_case_     :			Union[str, Any]       =       model_class(A__   )
									snake_case_     :			str       =       model(**self._prepare_for_class(A__			,	A__   )   )
									snake_case_     :			Union[str, Any]       =       outputs.hidden_states

									snake_case_     :			Tuple       =       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
									snake_case_     :			int       =       to_atuple(config.patch_size   )

									snake_case_     :			Any       =       (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]			,	)

									snake_case_     :			List[str]       =       outputs.reshaped_hidden_states
									self.assertEqual(len(A__   )			,	A__   )

									snake_case_     :			Tuple       =       reshaped_hidden_states[0].shape

									snake_case_     :			Dict       =       tf.reshape(reshaped_hidden_states[0]			,	(batch_size, num_channels, height * width)   )
									snake_case_     :			Optional[Any]       =       tf.transpose(A__			,	(0, 2, 1)   )

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



			def 					UpperCAmelCase__       (				self   :   Optional[int]   )							->     List[str]:




									'''simple docstring'''




									snake_case_     :			Any       =       self.model_tester.prepare_config_and_inputs_for_common()

									snake_case_     :			Optional[Any]       =       to_atuple(self.model_tester.image_size   )

									for model_class in self.all_model_classes:
															snake_case_     :			Any       =       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"]
															snake_case_     :			Tuple       =       True

															self.check_hidden_states_output(A__			,	A__			,	A__			,	A__   )



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




									'''simple docstring'''




									snake_case_     :			int       =       self.model_tester.prepare_config_and_inputs_for_common()
									snake_case_     :			List[str]       =       3

									snake_case_     :			Dict       =       to_atuple(self.model_tester.image_size   )
									snake_case_     :			List[Any]       =       to_atuple(config.patch_size   )

									snake_case_     :			int       =       image_size[0] + patch_size[0] - (image_size[0] % patch_size[0])
									snake_case_     :			Tuple       =       image_size[1] + patch_size[1] - (image_size[1] % patch_size[1])

									for model_class in self.all_model_classes:
															snake_case_     :			Union[str, Any]       =       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"]
															snake_case_     :			Tuple       =       True
															self.check_hidden_states_output(A__			,	A__			,	A__			,	(padded_height, padded_width)   )



			@slow
			def 					UpperCAmelCase__       (				self   :   str   )							->     Dict:




									'''simple docstring'''




									for model_name in TF_SWIN_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
															snake_case_     :			List[str]       =       TFSwinModel.from_pretrained(A__   )
															self.assertIsNotNone(A__   )



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



			@cached_property
			def 					UpperCAmelCase__       (				self   :   Tuple   )							->     int:




									'''simple docstring'''




									return (
									    AutoImageProcessor.from_pretrained("microsoft/swin-tiny-patch4-window7-224"   )
									    if is_vision_available()
									    else None
									)



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




									'''simple docstring'''




									snake_case_     :			int       =       TFSwinForImageClassification.from_pretrained("microsoft/swin-tiny-patch4-window7-224"   )
									snake_case_     :			int       =       self.default_image_processor

									snake_case_     :			str       =       Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png"   )
									snake_case_     :			List[Any]       =       image_processor(images=A__			,	return_tensors="tf"   )

									# forward pass
									snake_case_     :			int       =       model(A__   )

									# verify the logits
									snake_case_     :			Any       =       tf.TensorShape((1, 10_00)   )
									self.assertEqual(outputs.logits.shape			,	A__   )
									snake_case_     :			List[str]       =       tf.constant([-0.0948, -0.6454, -0.0921]   )
									self.assertTrue(np.allclose(outputs.logits[0, :3]			,	A__			,	atol=1E-4   )   )




from typing import TYPE_CHECKING

from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tokenizers_available, is_torch_available


UpperCAmelCase							=       {
    "configuration_nystromformer": ["NYSTROMFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP", "NystromformerConfig"],
}

try:
     if not is_torch_available():
          raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
     pass
else:
     UpperCAmelCase							=       [
         "NYSTROMFORMER_PRETRAINED_MODEL_ARCHIVE_LIST",
         "NystromformerForMaskedLM",
         "NystromformerForMultipleChoice",
         "NystromformerForQuestionAnswering",
         "NystromformerForSequenceClassification",
         "NystromformerForTokenClassification",
         "NystromformerLayer",
         "NystromformerModel",
         "NystromformerPreTrainedModel",
     ]


if TYPE_CHECKING:
     from .configuration_nystromformer import NYSTROMFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP, NystromformerConfig

     try:
          if not is_torch_available():
               raise OptionalDependencyNotAvailable()
     except OptionalDependencyNotAvailable:
          pass
     else:
          from .modeling_nystromformer import (
              NYSTROMFORMER_PRETRAINED_MODEL_ARCHIVE_LIST,
              NystromformerForMaskedLM,
              NystromformerForMultipleChoice,
              NystromformerForQuestionAnswering,
              NystromformerForSequenceClassification,
              NystromformerForTokenClassification,
              NystromformerLayer,
              NystromformerModel,
              NystromformerPreTrainedModel,
          )


else:
     import sys

     UpperCAmelCase							=       _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)



