




'''simple docstring'''

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






						def __init__(   self						:						Dict					,   __UpperCamelCase						:						List[Any]					,   __UpperCamelCase						:						Optional[Any]=1_3					,   __UpperCamelCase						:						int=3_2					,   __UpperCamelCase						:						str=2					,   __UpperCamelCase						:						Optional[Any]=3					,   __UpperCamelCase						:						Union[str, Any]=1_6					,   __UpperCamelCase						:						int=[1, 2, 1]					,   __UpperCamelCase						:						Optional[int]=[2, 2, 4]					,   __UpperCamelCase						:						Any=2					,   __UpperCamelCase						:						Union[str, Any]=2.0					,   __UpperCamelCase						:						str=True					,   __UpperCamelCase						:						Dict=0.0					,   __UpperCamelCase						:						Union[str, Any]=0.0					,   __UpperCamelCase						:						int=0.1					,   __UpperCamelCase						:						Optional[Any]="gelu"					,   __UpperCamelCase						:						Tuple=False					,   __UpperCamelCase						:						Optional[Any]=True					,   __UpperCamelCase						:						Tuple=0.02					,   __UpperCamelCase						:						List[Any]=1E-5					,   __UpperCamelCase						:						str=True					,   __UpperCamelCase						:						Dict=None					,   __UpperCamelCase						:						Optional[int]=True					,   __UpperCamelCase						:						Dict=1_0					,   __UpperCamelCase						:						Dict=8					,   ):



									'''simple docstring'''






									snake_case__          =						parent
									snake_case__          =						batch_size
									snake_case__          =						image_size
									snake_case__          =						patch_size
									snake_case__          =						num_channels
									snake_case__          =						embed_dim
									snake_case__          =						depths
									snake_case__          =						num_heads
									snake_case__          =						window_size
									snake_case__          =						mlp_ratio
									snake_case__          =						qkv_bias
									snake_case__          =						hidden_dropout_prob
									snake_case__          =						attention_probs_dropout_prob
									snake_case__          =						drop_path_rate
									snake_case__          =						hidden_act
									snake_case__          =						use_absolute_embeddings
									snake_case__          =						patch_norm
									snake_case__          =						layer_norm_eps
									snake_case__          =						initializer_range
									snake_case__          =						is_training
									snake_case__          =						scope
									snake_case__          =						use_labels
									snake_case__          =						type_sequence_label_size
									snake_case__          =						encoder_stride






						def       __lowerCAmelCase(   self						:						Any       ):



									'''simple docstring'''






									snake_case__          =						floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]       )

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

									snake_case__          =						self.get_config()

									return config, pixel_values, labels






						def       __lowerCAmelCase(   self						:						str       ):



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



									'''simple docstring'''






									snake_case__          =						TFSwinModel(config=__UpperCamelCase       )
									snake_case__          =						model(__UpperCamelCase       )

									snake_case__          =						((config.image_size // config.patch_size) ** 2) // (4 ** (len(config.depths       ) - 1))
									snake_case__          =						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       __lowerCAmelCase(   self						:						Optional[Any]					,   __UpperCamelCase						:						int					,   __UpperCamelCase						:						Union[str, Any]					,   __UpperCamelCase						:						List[str]       ):



									'''simple docstring'''






									snake_case__          =						TFSwinForMaskedImageModeling(config=__UpperCamelCase       )
									snake_case__          =						model(__UpperCamelCase       )
									self.parent.assertEqual(
									    result.logits.shape					,   (self.batch_size, self.num_channels, self.image_size, self.image_size)       )

									# test greyscale images
									snake_case__          =						1
									snake_case__          =						TFSwinForMaskedImageModeling(__UpperCamelCase       )

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






						def       __lowerCAmelCase(   self						:						Dict					,   __UpperCamelCase						:						List[Any]					,   __UpperCamelCase						:						Union[str, Any]					,   __UpperCamelCase						:						int       ):



									'''simple docstring'''






									snake_case__          =						self.type_sequence_label_size
									snake_case__          =						TFSwinForImageClassification(__UpperCamelCase       )
									snake_case__          =						model(__UpperCamelCase					,   labels=__UpperCamelCase       )
									self.parent.assertEqual(result.logits.shape					,   (self.batch_size, self.type_sequence_label_size)       )

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







						def       __lowerCAmelCase(   self						:						Dict       ):



									'''simple docstring'''






									snake_case__          =						self.prepare_config_and_inputs()
									snake_case__    ,			snake_case__    ,			snake_case__          =						config_and_inputs
									snake_case__          =						{"""pixel_values""": pixel_values}
									return config, inputs_dict







@require_tf
class __magic_name__(						__lowerCAmelCase    ,		__lowerCAmelCase    ,		unittest.TestCase      ):
						UpperCAmelCase_		:					int    =  (
						    (
						        TFSwinModel,
						        TFSwinForImageClassification,
						        TFSwinForMaskedImageModeling,
						    )
						    if is_tf_available()
						    else ()
						)
						UpperCAmelCase_		:					Dict    =  (
						    {"""feature-extraction""": TFSwinModel, """image-classification""": TFSwinForImageClassification}
						    if is_tf_available()
						    else {}
						)

						UpperCAmelCase_		:					Tuple    =  False
						UpperCAmelCase_		:					Union[str, Any]    =  False
						UpperCAmelCase_		:					Dict    =  False
						UpperCAmelCase_		:					List[Any]    =  False






						def       __lowerCAmelCase(   self						:						Any       ):



									'''simple docstring'''






									snake_case__          =						TFSwinModelTester(self       )
									snake_case__          =						ConfigTester(self					,   config_class=__UpperCamelCase					,   embed_dim=3_7       )






						def       __lowerCAmelCase(   self						:						List[str]       ):



									'''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       __lowerCAmelCase(   self						:						str       ):



									'''simple docstring'''






									return






						def       __lowerCAmelCase(   self						:						Union[str, Any]       ):



									'''simple docstring'''






									snake_case__          =						self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_model(*__UpperCamelCase       )






						def       __lowerCAmelCase(   self						:						Union[str, Any]       ):



									'''simple docstring'''






									snake_case__          =						self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_for_masked_image_modeling(*__UpperCamelCase       )






						def       __lowerCAmelCase(   self						:						Union[str, Any]       ):



									'''simple docstring'''






									snake_case__          =						self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_for_image_classification(*__UpperCamelCase       )






						@unittest.skip(reason="""Swin does not use inputs_embeds"""       )
						def       __lowerCAmelCase(   self						:						List[str]       ):



									'''simple docstring'''






									pass






						def       __lowerCAmelCase(   self						:						str       ):



									'''simple docstring'''






									snake_case__    ,			snake_case__          =						self.model_tester.prepare_config_and_inputs_for_common()

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






						def       __lowerCAmelCase(   self						:						Union[str, Any]       ):



									'''simple docstring'''






									snake_case__    ,			snake_case__          =						self.model_tester.prepare_config_and_inputs_for_common()

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

												snake_case__          =						["""pixel_values"""]
												self.assertListEqual(arg_names[:1]					,   __UpperCamelCase       )






						def       __lowerCAmelCase(   self						:						Tuple       ):



									'''simple docstring'''






									snake_case__    ,			snake_case__          =						self.model_tester.prepare_config_and_inputs_for_common()
									snake_case__          =						True

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

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

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

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

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

												snake_case__          =						outputs.attentions

												self.assertEqual(len(__UpperCamelCase       )					,   __UpperCamelCase       )

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






						def       __lowerCAmelCase(   self						:						List[str]					,   __UpperCamelCase						:						Tuple					,   __UpperCamelCase						:						List[Any]					,   __UpperCamelCase						:						Optional[Any]					,   __UpperCamelCase						:						List[str]       ):



									'''simple docstring'''






									snake_case__          =						model_class(__UpperCamelCase       )
									snake_case__          =						model(**self._prepare_for_class(__UpperCamelCase					,   __UpperCamelCase       )       )
									snake_case__          =						outputs.hidden_states

									snake_case__          =						getattr(
									    self.model_tester					,   """expected_num_hidden_layers"""					,   len(self.model_tester.depths       ) + 1       )
									self.assertEqual(len(__UpperCamelCase       )					,   __UpperCamelCase       )

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

									snake_case__          =						(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__          =						outputs.reshaped_hidden_states
									self.assertEqual(len(__UpperCamelCase       )					,   __UpperCamelCase       )

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

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

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






						def       __lowerCAmelCase(   self						:						str       ):



									'''simple docstring'''






									snake_case__    ,			snake_case__          =						self.model_tester.prepare_config_and_inputs_for_common()

									snake_case__          =						to_atuple(self.model_tester.image_size       )

									for model_class in self.all_model_classes:
												snake_case__          =						True
												self.check_hidden_states_output(__UpperCamelCase					,   __UpperCamelCase					,   __UpperCamelCase					,   __UpperCamelCase       )

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

												self.check_hidden_states_output(__UpperCamelCase					,   __UpperCamelCase					,   __UpperCamelCase					,   __UpperCamelCase       )






						def       __lowerCAmelCase(   self						:						str       ):



									'''simple docstring'''






									snake_case__    ,			snake_case__          =						self.model_tester.prepare_config_and_inputs_for_common()
									snake_case__          =						3

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

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

									for model_class in self.all_model_classes:
												snake_case__          =						True
												self.check_hidden_states_output(__UpperCamelCase					,   __UpperCamelCase					,   __UpperCamelCase					,   (padded_height, padded_width)       )

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







						@slow
						def       __lowerCAmelCase(   self						:						List[Any]       ):



									'''simple docstring'''






									for model_name in TF_SWIN_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
												snake_case__          =						TFSwinModel.from_pretrained(__UpperCamelCase       )
												self.assertIsNotNone(__UpperCamelCase       )







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






						@cached_property
						def       __lowerCAmelCase(   self						:						List[str]       ):



									'''simple docstring'''






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







						@slow
						def       __lowerCAmelCase(   self						:						Optional[int]       ):



									'''simple docstring'''






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

									snake_case__          =						Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png"""       )
									snake_case__          =						image_processor(images=__UpperCamelCase					,   return_tensors="""tf"""       )

									# forward pass
									snake_case__          =						model(__UpperCamelCase       )

									# verify the logits
									snake_case__          =						tf.TensorShape((1, 1_0_0_0)       )
									self.assertEqual(outputs.logits.shape					,   __UpperCamelCase       )
									snake_case__          =						tf.constant([-0.09_48, -0.64_54, -0.09_21]       )
									self.assertTrue(np.allclose(outputs.logits[0, :3]					,   __UpperCamelCase					,   atol=1E-4       )       )
"""simple docstring"""






from collections import OrderedDict
from typing import Mapping

from packaging import version

from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfig
from ...utils import logging


lowerCAmelCase__    :    List[Any]			=      logging.get_logger(__name__)

lowerCAmelCase__    :    Dict			=      {
    'apple/mobilevit-small': 'https://huggingface.co/apple/mobilevit-small/resolve/main/config.json',
    'apple/mobilevit-x-small': 'https://huggingface.co/apple/mobilevit-x-small/resolve/main/config.json',
    'apple/mobilevit-xx-small': 'https://huggingface.co/apple/mobilevit-xx-small/resolve/main/config.json',
    'apple/deeplabv3-mobilevit-small': (
        'https://huggingface.co/apple/deeplabv3-mobilevit-small/resolve/main/config.json'
    ),
    'apple/deeplabv3-mobilevit-x-small': (
        'https://huggingface.co/apple/deeplabv3-mobilevit-x-small/resolve/main/config.json'
    ),
    'apple/deeplabv3-mobilevit-xx-small': (
        'https://huggingface.co/apple/deeplabv3-mobilevit-xx-small/resolve/main/config.json'
    ),
    # See all MobileViT models at https://huggingface.co/models?filter=mobilevit
}
class 							snake_case			(					__UpperCAmelCase							):




					"""simple docstring"""

					snake_case__							       =					"mobilevit"







					def __init__(							self  :      Union[str, Any]       ,lowerCamelCase__  :      Any=3       ,lowerCamelCase__  :      str=256       ,lowerCamelCase__  :      Optional[int]=2       ,lowerCamelCase__  :      Union[str, Any]=[144, 192, 240]       ,lowerCamelCase__  :      Optional[int]=[16, 32, 64, 96, 128, 160, 640]       ,lowerCamelCase__  :      Dict=4       ,lowerCamelCase__  :      int=2.0       ,lowerCamelCase__  :      Optional[Any]=4.0       ,lowerCamelCase__  :      Optional[int]="silu"       ,lowerCamelCase__  :      List[str]=3       ,lowerCamelCase__  :      int=32       ,lowerCamelCase__  :      int=0.1       ,lowerCamelCase__  :      List[str]=0.0       ,lowerCamelCase__  :      List[str]=0.1       ,lowerCamelCase__  :      Tuple=0.0_2       ,lowerCamelCase__  :      Union[str, Any]=1e-5       ,lowerCamelCase__  :      Tuple=True       ,lowerCamelCase__  :      Dict=256       ,lowerCamelCase__  :      Any=[6, 12, 18]       ,lowerCamelCase__  :      Optional[int]=0.1       ,lowerCamelCase__  :      List[str]=255       ,**lowerCamelCase__  :      List[Any]       ,):
									super().__init__(**lowerCamelCase__     )

									UpperCAmelCase__			 =				num_channels
									UpperCAmelCase__			 =				image_size
									UpperCAmelCase__			 =				patch_size
									UpperCAmelCase__			 =				hidden_sizes
									UpperCAmelCase__			 =				neck_hidden_sizes
									UpperCAmelCase__			 =				num_attention_heads
									UpperCAmelCase__			 =				mlp_ratio
									UpperCAmelCase__			 =				expand_ratio
									UpperCAmelCase__			 =				hidden_act
									UpperCAmelCase__			 =				conv_kernel_size
									UpperCAmelCase__			 =				output_stride
									UpperCAmelCase__			 =				hidden_dropout_prob
									UpperCAmelCase__			 =				attention_probs_dropout_prob
									UpperCAmelCase__			 =				classifier_dropout_prob
									UpperCAmelCase__			 =				initializer_range
									UpperCAmelCase__			 =				layer_norm_eps
									UpperCAmelCase__			 =				qkv_bias

									# decode head attributes for semantic segmentation
									UpperCAmelCase__			 =				aspp_out_channels
									UpperCAmelCase__			 =				atrous_rates
									UpperCAmelCase__			 =				aspp_dropout_prob
									UpperCAmelCase__			 =				semantic_loss_ignore_index






class 							snake_case			(					__UpperCAmelCase							):




					"""simple docstring"""

					snake_case__							       =					version.parse("1.11"							)







					@property
					def 						__lowerCAmelCase		(							self  :      str     ):
									return OrderedDict([('pixel_values', {0: 'batch', 1: 'num_channels', 2: 'height', 3: 'width'})]     )







					@property
					def 						__lowerCAmelCase		(							self  :      Dict     ):
									if self.task == "image-classification":
													return OrderedDict([('logits', {0: 'batch'})]     )
									else:
													return OrderedDict([('last_hidden_state', {0: 'batch'}), ('pooler_output', {0: 'batch'})]     )







					@property
					def 						__lowerCAmelCase		(							self  :      str     ):
									return 1e-4



