

'''simple docstring'''

import collections
import inspect
import unittest
from typing import Dict, List, Tuple

from transformers import MaskFormerSwinConfig
from transformers.testing_utils import require_torch, require_torch_multi_gpu, torch_device
from transformers.utils import is_torch_available

from ...test_backbone_common import BackboneTesterMixin
from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
				import torch
				from torch import nn

				from transformers import MaskFormerSwinBackbone
				from transformers.models.maskformer import MaskFormerSwinModel







class   UpperCamelCase__    :


		"""simple docstring"""

		def __init__( self    ,			snake_case    ,			snake_case=1_3    ,			snake_case=3_2    ,			snake_case=2    ,			snake_case=3    ,			snake_case=1_6    ,			snake_case=[1, 2, 1]    ,			snake_case=[2, 2, 4]    ,			snake_case=2    ,			snake_case=2.0    ,			snake_case=True    ,			snake_case=0.0    ,			snake_case=0.0    ,			snake_case=0.1    ,			snake_case="gelu"    ,			snake_case=False    ,			snake_case=True    ,			snake_case=0.02    ,			snake_case=1e-5    ,			snake_case=True    ,			snake_case=None    ,			snake_case=True    ,			snake_case=1_0    ,			snake_case=8    ,			snake_case=["stage1", "stage2", "stage3"]    ,			snake_case=[1, 2, 3]    ,			):


							'''simple docstring'''

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







		def       A_       ( self			):


							'''simple docstring'''

							UpperCAmelCase		:							Union[str, Any]   					=   floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]			)

							UpperCAmelCase		:							List[str]   					=   None
							if self.use_labels:
												UpperCAmelCase		:							Tuple   					=   ids_tensor([self.batch_size]    ,			self.type_sequence_label_size			)

							UpperCAmelCase		:							Optional[int]   					=   self.get_config()

							return config, pixel_values, labels







		def       A_       ( self			):


							'''simple docstring'''

							return MaskFormerSwinConfig(
							    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    ,			out_features=self.out_features    ,			out_indices=self.out_indices    ,			)







		def       A_       ( self    ,			snake_case    ,			snake_case    ,			snake_case			):


							'''simple docstring'''

							UpperCAmelCase		:							Optional[int]   					=   MaskFormerSwinModel(config=lowerCAmelCase__			)
							model.to(lowerCAmelCase__			)
							model.eval()
							UpperCAmelCase		:							Tuple   					=   model(lowerCAmelCase__			)

							UpperCAmelCase		:							Any   					=   ((config.image_size // config.patch_size) ** 2) // (4 ** (len(config.depths			) - 1))
							UpperCAmelCase		:							Dict   					=   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    ,			snake_case    ,			snake_case    ,			snake_case			):


							'''simple docstring'''

							UpperCAmelCase		:							List[Any]   					=   MaskFormerSwinBackbone(config=lowerCAmelCase__			)
							model.to(lowerCAmelCase__			)
							model.eval()
							UpperCAmelCase		:							List[Any]   					=   model(lowerCAmelCase__			)

							# verify feature maps
							self.parent.assertEqual(len(result.feature_maps			)    ,			len(config.out_features			)			)
							self.parent.assertListEqual(list(result.feature_maps[0].shape			)    ,			[1_3, 1_6, 1_6, 1_6]			)

							# verify channels
							self.parent.assertEqual(len(model.channels			)    ,			len(config.out_features			)			)
							self.parent.assertListEqual(model.channels    ,			[1_6, 3_2, 6_4]			)

							# verify ValueError
							with self.parent.assertRaises(lowerCAmelCase__			):
												UpperCAmelCase		:							Optional[Any]   					=   ["stem"]
												UpperCAmelCase		:							Dict   					=   MaskFormerSwinBackbone(config=lowerCAmelCase__			)







		def       A_       ( self			):


							'''simple docstring'''

							UpperCAmelCase		:							Tuple   					=   self.prepare_config_and_inputs()
							UpperCAmelCase		:							Optional[int]   					=   config_and_inputs
							UpperCAmelCase		:							int   					=   {"pixel_values": pixel_values}
							return config, inputs_dict







@require_torch
class   UpperCamelCase__    (   UpperCAmelCase_							,							UpperCAmelCase_							,							unittest.TestCase   ):


		"""simple docstring"""

		SCREAMING_SNAKE_CASE__  :	Optional[int]   =							(
		    (
		        MaskFormerSwinModel,
		        MaskFormerSwinBackbone,
		    )
		    if is_torch_available()
		    else ()
		)
		SCREAMING_SNAKE_CASE__  :	List[Any]   =							{'''feature-extraction''': MaskFormerSwinModel} if is_torch_available() else {}
		SCREAMING_SNAKE_CASE__  :	str   =							False
		SCREAMING_SNAKE_CASE__  :	Optional[Any]   =							False
		SCREAMING_SNAKE_CASE__  :	Union[str, Any]   =							False
		SCREAMING_SNAKE_CASE__  :	Optional[Any]   =							False
		SCREAMING_SNAKE_CASE__  :	Optional[int]   =							False







		def       A_       ( self			):


							'''simple docstring'''

							UpperCAmelCase		:							Dict   					=   MaskFormerSwinModelTester(self			)
							UpperCAmelCase		:							Tuple   					=   ConfigTester(self    ,			config_class=lowerCAmelCase__    ,			embed_dim=3_7			)







		@require_torch_multi_gpu
		@unittest.skip(
		    reason=(
		        "`MaskFormerSwinModel` outputs `hidden_states_spatial_dimensions` which doesn't work well with"
		        " `nn.DataParallel`"
		    )			)
		def       A_       ( self			):


							'''simple docstring'''

							pass







		def       A_       ( self			):


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


							'''simple docstring'''

							return







		def       A_       ( self			):


							'''simple docstring'''

							UpperCAmelCase		:							List[str]   					=   self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_model(*lowerCAmelCase__			)







		def       A_       ( self			):


							'''simple docstring'''

							UpperCAmelCase		:							List[str]   					=   self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_backbone(*lowerCAmelCase__			)







		@unittest.skip("Swin does not use inputs_embeds"			)
		def       A_       ( self			):


							'''simple docstring'''

							pass







		@unittest.skip("Swin does not support feedforward chunking"			)
		def       A_       ( self			):


							'''simple docstring'''

							pass







		def       A_       ( self			):


							'''simple docstring'''

							UpperCAmelCase		:							Optional[Any]   					=   self.model_tester.prepare_config_and_inputs_for_common()

							for model_class in self.all_model_classes:
												UpperCAmelCase		:							List[Any]   					=   model_class(lowerCAmelCase__			)
												self.assertIsInstance(model.get_input_embeddings()    ,			(nn.Module)			)
												UpperCAmelCase		:							Union[str, Any]   					=   model.get_output_embeddings()
												self.assertTrue(x is None or isinstance(lowerCAmelCase__    ,			nn.Linear			)			)







		def       A_       ( self			):


							'''simple docstring'''

							UpperCAmelCase		:							Any   					=   self.model_tester.prepare_config_and_inputs_for_common()

							for model_class in self.all_model_classes:
												UpperCAmelCase		:							Tuple   					=   model_class(lowerCAmelCase__			)
												UpperCAmelCase		:							Dict   					=   inspect.signature(model.forward			)
												# signature.parameters is an OrderedDict => so arg_names order is deterministic
												UpperCAmelCase		:							List[Any]   					=   [*signature.parameters.keys()]

												UpperCAmelCase		:							Dict   					=   ["pixel_values"]
												self.assertListEqual(arg_names[:1]    ,			lowerCAmelCase__			)







		@unittest.skip(reason="MaskFormerSwin is only used as backbone and doesn't support output_attentions"			)
		def       A_       ( self			):


							'''simple docstring'''

							pass







		@unittest.skip(reason="MaskFormerSwin is only used as an internal backbone"			)
		def       A_       ( self			):


							'''simple docstring'''

							pass







		def       A_       ( self    ,			snake_case    ,			snake_case    ,			snake_case    ,			snake_case			):


							'''simple docstring'''

							UpperCAmelCase		:							List[str]   					=   model_class(lowerCAmelCase__			)
							model.to(lowerCAmelCase__			)
							model.eval()

							with torch.no_grad():
												UpperCAmelCase		:							List[Any]   					=   model(**self._prepare_for_class(lowerCAmelCase__    ,			lowerCAmelCase__			)			)

							UpperCAmelCase		:							List[Any]   					=   outputs.hidden_states

							UpperCAmelCase		:							int   					=   getattr(
							    self.model_tester    ,			"expected_num_hidden_layers"    ,			len(self.model_tester.depths			) + 1			)
							self.assertEqual(len(lowerCAmelCase__			)    ,			lowerCAmelCase__			)

							# Swin has a different seq_length
							UpperCAmelCase		:							Dict   					=   (
							    config.patch_size
							    if isinstance(config.patch_size    ,			collections.abc.Iterable			)
							    else (config.patch_size, config.patch_size)
							)

							UpperCAmelCase		:							str   					=   (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]    ,			)







		def       A_       ( self			):


							'''simple docstring'''

							UpperCAmelCase		:							List[Any]   					=   self.model_tester.prepare_config_and_inputs_for_common()

							UpperCAmelCase		:							List[Any]   					=   (
							    self.model_tester.image_size
							    if isinstance(self.model_tester.image_size    ,			collections.abc.Iterable			)
							    else (self.model_tester.image_size, self.model_tester.image_size)
							)

							for model_class in self.all_model_classes:
												UpperCAmelCase		:							Optional[Any]   					=   True
												self.check_hidden_states_output(lowerCAmelCase__    ,			lowerCAmelCase__    ,			lowerCAmelCase__    ,			lowerCAmelCase__			)

												# check that output_hidden_states also work using config
												del inputs_dict["output_hidden_states"]
												UpperCAmelCase		:							Optional[Any]   					=   True

												self.check_hidden_states_output(lowerCAmelCase__    ,			lowerCAmelCase__    ,			lowerCAmelCase__    ,			lowerCAmelCase__			)







		def       A_       ( self			):


							'''simple docstring'''

							UpperCAmelCase		:							Optional[Any]   					=   self.model_tester.prepare_config_and_inputs_for_common()
							UpperCAmelCase		:							int   					=   3

							UpperCAmelCase		:							Tuple   					=   (
							    self.model_tester.image_size
							    if isinstance(self.model_tester.image_size    ,			collections.abc.Iterable			)
							    else (self.model_tester.image_size, self.model_tester.image_size)
							)
							UpperCAmelCase		:							Tuple   					=   (
							    config.patch_size
							    if isinstance(config.patch_size    ,			collections.abc.Iterable			)
							    else (config.patch_size, config.patch_size)
							)

							UpperCAmelCase		:							Dict   					=   image_size[0] + patch_size[0] - (image_size[0] % patch_size[0])
							UpperCAmelCase		:							Union[str, Any]   					=   image_size[1] + patch_size[1] - (image_size[1] % patch_size[1])

							for model_class in self.all_model_classes:
												UpperCAmelCase		:							Optional[Any]   					=   True
												self.check_hidden_states_output(lowerCAmelCase__    ,			lowerCAmelCase__    ,			lowerCAmelCase__    ,			(padded_height, padded_width)			)

												# check that output_hidden_states also work using config
												del inputs_dict["output_hidden_states"]
												UpperCAmelCase		:							Optional[Any]   					=   True
												self.check_hidden_states_output(lowerCAmelCase__    ,			lowerCAmelCase__    ,			lowerCAmelCase__    ,			(padded_height, padded_width)			)







		@unittest.skip(reason="MaskFormerSwin doesn't have pretrained checkpoints"			)
		def       A_       ( self			):


							'''simple docstring'''

							pass







		@unittest.skip(reason="This will be fixed once MaskFormerSwin is replaced by native Swin"			)
		def       A_       ( self			):


							'''simple docstring'''

							pass







		@unittest.skip(reason="This will be fixed once MaskFormerSwin is replaced by native Swin"			)
		def       A_       ( self			):


							'''simple docstring'''

							pass







		def       A_       ( self			):


							'''simple docstring'''

							UpperCAmelCase		:							Union[str, Any]   					=   self.model_tester.prepare_config_and_inputs_for_common()

							def set_nan_tensor_to_zero(snake_case			):
												UpperCAmelCase		:							Optional[int]   					=   0
												return t

							def check_equivalence(snake_case    ,			snake_case    ,			snake_case    ,			snake_case={}			):
												with torch.no_grad():
																	UpperCAmelCase		:							Optional[Any]   					=   model(**lowerCAmelCase__    ,			return_dict=lowerCAmelCase__    ,			**lowerCAmelCase__			)
																	UpperCAmelCase		:							Union[str, Any]   					=   model(**lowerCAmelCase__    ,			return_dict=lowerCAmelCase__    ,			**lowerCAmelCase__			).to_tuple()

																	def recursive_check(snake_case    ,			snake_case			):
																						if isinstance(lowerCAmelCase__    ,			(List, Tuple)			):
																											for tuple_iterable_value, dict_iterable_value in zip(lowerCAmelCase__    ,			lowerCAmelCase__			):
																																recursive_check(lowerCAmelCase__    ,			lowerCAmelCase__			)
																						elif isinstance(lowerCAmelCase__    ,			lowerCAmelCase__			):
																											for tuple_iterable_value, dict_iterable_value in zip(
																											    tuple_object.values()    ,			dict_object.values()			):
																																recursive_check(lowerCAmelCase__    ,			lowerCAmelCase__			)
																						elif tuple_object is None:
																											return
																						else:
																											self.assertTrue(
																											    torch.allclose(
																											        set_nan_tensor_to_zero(lowerCAmelCase__			)    ,			set_nan_tensor_to_zero(lowerCAmelCase__			)    ,			atol=1e-5			)    ,			msg=(
																											        "Tuple and dict output are not equal. Difference:"
																											        f" {torch.max(torch.abs(tuple_object - dict_object			)			)}. Tuple has `nan`:"
																											        f" {torch.isnan(lowerCAmelCase__			).any()} and `inf`: {torch.isinf(lowerCAmelCase__			)}. Dict has"
																											        f" `nan`: {torch.isnan(lowerCAmelCase__			).any()} and `inf`: {torch.isinf(lowerCAmelCase__			)}."
																											    )    ,			)

																	recursive_check(lowerCAmelCase__    ,			lowerCAmelCase__			)

							for model_class in self.all_model_classes:
												UpperCAmelCase		:							Optional[int]   					=   model_class(lowerCAmelCase__			)
												model.to(lowerCAmelCase__			)
												model.eval()

												UpperCAmelCase		:							Optional[int]   					=   self._prepare_for_class(lowerCAmelCase__    ,			lowerCAmelCase__			)
												UpperCAmelCase		:							str   					=   self._prepare_for_class(lowerCAmelCase__    ,			lowerCAmelCase__			)
												check_equivalence(lowerCAmelCase__    ,			lowerCAmelCase__    ,			lowerCAmelCase__			)

												UpperCAmelCase		:							Any   					=   self._prepare_for_class(lowerCAmelCase__    ,			lowerCAmelCase__    ,			return_labels=lowerCAmelCase__			)
												UpperCAmelCase		:							str   					=   self._prepare_for_class(lowerCAmelCase__    ,			lowerCAmelCase__    ,			return_labels=lowerCAmelCase__			)
												check_equivalence(lowerCAmelCase__    ,			lowerCAmelCase__    ,			lowerCAmelCase__			)

												UpperCAmelCase		:							Optional[Any]   					=   self._prepare_for_class(lowerCAmelCase__    ,			lowerCAmelCase__			)
												UpperCAmelCase		:							Dict   					=   self._prepare_for_class(lowerCAmelCase__    ,			lowerCAmelCase__			)
												check_equivalence(lowerCAmelCase__    ,			lowerCAmelCase__    ,			lowerCAmelCase__    ,			{"output_hidden_states": True}			)

												UpperCAmelCase		:							List[Any]   					=   self._prepare_for_class(lowerCAmelCase__    ,			lowerCAmelCase__    ,			return_labels=lowerCAmelCase__			)
												UpperCAmelCase		:							Any   					=   self._prepare_for_class(lowerCAmelCase__    ,			lowerCAmelCase__    ,			return_labels=lowerCAmelCase__			)
												check_equivalence(lowerCAmelCase__    ,			lowerCAmelCase__    ,			lowerCAmelCase__    ,			{"output_hidden_states": True}			)







@require_torch
class   UpperCamelCase__    (   unittest.TestCase							,							UpperCAmelCase_   ):


		"""simple docstring"""

		SCREAMING_SNAKE_CASE__  :	Union[str, Any]   =							(MaskFormerSwinBackbone,) if is_torch_available() else ()
		SCREAMING_SNAKE_CASE__  :	str   =							MaskFormerSwinConfig







		def       A_       ( self			):


							'''simple docstring'''

							UpperCAmelCase		:							List[Any]   					=   MaskFormerSwinModelTester(self			)







		def       A_       ( self			):


							'''simple docstring'''

							UpperCAmelCase		:							Tuple   					=   self.model_tester.prepare_config_and_inputs_for_common()
							UpperCAmelCase		:							Any   					=   inputs_dict["pixel_values"].shape[0]

							for backbone_class in self.all_model_classes:
												UpperCAmelCase		:							List[str]   					=   backbone_class(lowerCAmelCase__			)
												backbone.to(lowerCAmelCase__			)
												backbone.eval()

												UpperCAmelCase		:							Any   					=   backbone(**lowerCAmelCase__			)

												# Test default outputs and verify feature maps
												self.assertIsInstance(outputs.feature_maps    ,			lowerCAmelCase__			)
												self.assertTrue(len(outputs.feature_maps			) == len(backbone.channels			)			)
												for feature_map, n_channels in zip(outputs.feature_maps    ,			backbone.channels			):
																	self.assertTrue(feature_map.shape[:2]    ,			(batch_size, n_channels)			)
												self.assertIsNone(outputs.hidden_states			)
												self.assertIsNone(outputs.attentions			)

												# Test output_hidden_states=True
												UpperCAmelCase		:							str   					=   backbone(**lowerCAmelCase__    ,			output_hidden_states=lowerCAmelCase__			)
												self.assertIsNotNone(outputs.hidden_states			)
												self.assertTrue(len(outputs.hidden_states			)    ,			len(backbone.stage_names			)			)
												# We skip the stem layer
												for hidden_states, n_channels in zip(outputs.hidden_states[1:]    ,			backbone.channels			):
																	for hidden_state in hidden_states:
																						# Hidden states are in the format (batch_size, (height * width), n_channels)
																						UpperCAmelCase		:							List[Any]   					=   hidden_state.shape
																						self.assertTrue((h_batch_size, h_n_channels)    ,			(batch_size, n_channels)			)

            # Test output_attentions=True
												if self.has_attentions:
																	UpperCAmelCase		:							Dict   					=   backbone(**lowerCAmelCase__    ,			output_attentions=lowerCAmelCase__			)
																	self.assertIsNotNone(outputs.attentions			)



def 			A_      (    _UpperCAmelCase				,	_UpperCAmelCase = False    ):

						if not isinstance(_UpperCAmelCase				,	_UpperCAmelCase    ):
												SCREAMING_SNAKE_CASE_: str  		=       f"Expected string as input, found {type(_UpperCAmelCase    )}"
												raise ValueError(_UpperCAmelCase    )
						if not isinstance(_UpperCAmelCase				,	_UpperCAmelCase    ):
												SCREAMING_SNAKE_CASE_: Optional[Any]  		=       f"Expected boolean as use_pascal parameter, found {type(_UpperCAmelCase    )}"
												raise ValueError(_UpperCAmelCase    )

						SCREAMING_SNAKE_CASE_: Tuple  		=       input_str.split("_"    )

						SCREAMING_SNAKE_CASE_: str  		=       0 if use_pascal else 1

						SCREAMING_SNAKE_CASE_: int  		=       words[start_index:]

						SCREAMING_SNAKE_CASE_: List[str]  		=       [word[0].upper() + word[1:] for word in words_to_capitalize]

						SCREAMING_SNAKE_CASE_: List[Any]  		=       "" if use_pascal else words[0]

						return "".join([initial_word, *capitalized_words]    )


if __name__ == "__main__":
						from doctest import testmod

						testmod()






