





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






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







							"""simple docstring"""


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






						def UpperCAmelCase_			( self : Any		)							->      Dict:







							"""simple docstring"""


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

							snake_case_						:     Dict     =		None
							if self.use_labels:
								snake_case_						:     int     =		ids_tensor([self.batch_size]						,				self.type_sequence_label_size		)

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

							return config, pixel_values, labels






						def UpperCAmelCase_			( self : List[Any]		)							->      Tuple:







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







							"""simple docstring"""


							snake_case_						:     Dict     =		MaskFormerSwinModel(config=_A		)
							model.to(_A		)
							model.eval()
							snake_case_						:     Any     =		model(_A		)

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







							"""simple docstring"""


							snake_case_						:     Union[str, Any]     =		MaskFormerSwinBackbone(config=_A		)
							model.to(_A		)
							model.eval()
							snake_case_						:     str     =		model(_A		)

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

							# verify channels
							self.parent.assertEqual(len(model.channels		)						,				len(config.out_features		)		)
							self.parent.assertListEqual(model.channels						,				[16, 32, 64]		)

							# verify ValueError
							with self.parent.assertRaises(_A		):
								snake_case_						:     int     =		['stem']
								snake_case_						:     List[str]     =		MaskFormerSwinBackbone(config=_A		)






						def UpperCAmelCase_			( self : Tuple		)							->      str:







							"""simple docstring"""


							snake_case_						:     Dict     =		self.prepare_config_and_inputs()
							snake_case_  ,snake_case_  ,snake_case_						:     Union[str, Any]     =		config_and_inputs
							snake_case_						:     str     =		{'pixel_values': pixel_values}
							return config, inputs_dict
@require_torch
class SCREAMING_SNAKE_CASE_  (   snake_case_    ,			snake_case_    ,			unittest.TestCase   ):
						__magic_name__:     str	 =    (
						    (
						        MaskFormerSwinModel,
						        MaskFormerSwinBackbone,
						    )
						    if is_torch_available()
						    else ()
						)
						__magic_name__:     List[str]	 =    {"feature-extraction": MaskFormerSwinModel} if is_torch_available() else {}
						__magic_name__:     Optional[Any]	 =    False
						__magic_name__:     List[str]	 =    False
						__magic_name__:     Tuple	 =    False
						__magic_name__:     int	 =    False
						__magic_name__:     Optional[int]	 =    False






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







							"""simple docstring"""


							snake_case_						:     List[str]     =		MaskFormerSwinModelTester(self		)
							snake_case_						:     str     =		ConfigTester(self						,				config_class=_A						,				embed_dim=37		)






						@require_torch_multi_gpu
						@unittest.skip(
						    reason=(
						        '`MaskFormerSwinModel` outputs `hidden_states_spatial_dimensions` which doesn\'t work well with'
						        ' `nn.DataParallel`'
						    )		)
						def UpperCAmelCase_			( self : str		)							->      Dict:







							"""simple docstring"""


							pass






						def UpperCAmelCase_			( self : List[Any]		)							->      Dict:







							"""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 : Dict		)							->      Union[str, Any]:







							"""simple docstring"""


							return






						def UpperCAmelCase_			( self : Any		)							->      str:







							"""simple docstring"""


							snake_case_						:     Optional[int]     =		self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_model(*_A		)






						def UpperCAmelCase_			( self : Optional[Any]		)							->      Tuple:







							"""simple docstring"""


							snake_case_						:     Optional[int]     =		self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_backbone(*_A		)






						@unittest.skip('Swin does not use inputs_embeds'		)
						def UpperCAmelCase_			( self : str		)							->      Union[str, Any]:







							"""simple docstring"""


							pass






						@unittest.skip('Swin does not support feedforward chunking'		)
						def UpperCAmelCase_			( self : Union[str, Any]		)							->      str:







							"""simple docstring"""


							pass






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







							"""simple docstring"""


							snake_case_  ,snake_case_						:     str     =		self.model_tester.prepare_config_and_inputs_for_common()

							for model_class in self.all_model_classes:
								snake_case_						:     str     =		model_class(_A		)
								self.assertIsInstance(model.get_input_embeddings()						,				(nn.Module)		)
								snake_case_						:     int     =		model.get_output_embeddings()
								self.assertTrue(x is None or isinstance(_A						,				nn.Linear		)		)






						def UpperCAmelCase_			( self : Optional[Any]		)							->      Tuple:







							"""simple docstring"""


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

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

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






						@unittest.skip(reason='MaskFormerSwin is only used as backbone and doesn\'t support output_attentions'		)
						def UpperCAmelCase_			( self : Dict		)							->      str:







							"""simple docstring"""


							pass






						@unittest.skip(reason='MaskFormerSwin is only used as an internal backbone'		)
						def UpperCAmelCase_			( self : List[str]		)							->      str:







							"""simple docstring"""


							pass






						def UpperCAmelCase_			( self : Tuple						,				_A : Optional[Any]						,				_A : int						,				_A : Tuple						,				_A : List[str]		)							->      str:







							"""simple docstring"""


							snake_case_						:     List[str]     =		model_class(_A		)
							model.to(_A		)
							model.eval()

							with torch.no_grad():
								snake_case_						:     Any     =		model(**self._prepare_for_class(_A						,				_A		)		)

							snake_case_						:     str     =		outputs.hidden_states

							snake_case_						:     List[str]     =		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_						:     Union[str, Any]     =		(
							    config.patch_size
							    if isinstance(config.patch_size						,				collections.abc.Iterable		)
							    else (config.patch_size, config.patch_size)
							)

							snake_case_						:     List[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]						,				)






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







							"""simple docstring"""


							snake_case_  ,snake_case_						:     Tuple     =		self.model_tester.prepare_config_and_inputs_for_common()

							snake_case_						:     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:
								snake_case_						:     Tuple     =		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_						:     List[Any]     =		True

								self.check_hidden_states_output(_A						,				_A						,				_A						,				_A		)






						def UpperCAmelCase_			( self : str		)							->      str:







							"""simple docstring"""


							snake_case_  ,snake_case_						:     Tuple     =		self.model_tester.prepare_config_and_inputs_for_common()
							snake_case_						:     List[Any]     =		3

							snake_case_						:     Optional[int]     =		(
							    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)
							)
							snake_case_						:     Optional[Any]     =		(
							    config.patch_size
							    if isinstance(config.patch_size						,				collections.abc.Iterable		)
							    else (config.patch_size, config.patch_size)
							)

							snake_case_						:     Tuple     =		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_						:     Dict     =		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)		)






						@unittest.skip(reason='MaskFormerSwin doesn\'t have pretrained checkpoints'		)
						def UpperCAmelCase_			( self : Optional[Any]		)							->      List[Any]:







							"""simple docstring"""


							pass






						@unittest.skip(reason='This will be fixed once MaskFormerSwin is replaced by native Swin'		)
						def UpperCAmelCase_			( self : Optional[Any]		)							->      Tuple:







							"""simple docstring"""


							pass






						@unittest.skip(reason='This will be fixed once MaskFormerSwin is replaced by native Swin'		)
						def UpperCAmelCase_			( self : Union[str, Any]		)							->      Dict:







							"""simple docstring"""


							pass






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







							"""simple docstring"""


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

							def set_nan_tensor_to_zero(_A : Tuple		):
								snake_case_						:     List[Any]     =		0
								return t

							def check_equivalence(_A : Tuple						,				_A : int						,				_A : Tuple						,				_A : List[str]={}		):
								with torch.no_grad():
									snake_case_						:     str     =		model(**_A						,				return_dict=_A						,				**_A		)
									snake_case_						:     Tuple     =		model(**_A						,				return_dict=_A						,				**_A		).to_tuple()

									def recursive_check(_A : Optional[Any]						,				_A : str		):
										if isinstance(_A						,				(List, Tuple)		):
											for tuple_iterable_value, dict_iterable_value in zip(_A						,				_A		):
												recursive_check(_A						,				_A		)
										elif isinstance(_A						,				_A		):
											for tuple_iterable_value, dict_iterable_value in zip(
											    tuple_object.values()						,				dict_object.values()		):
												recursive_check(_A						,				_A		)
										elif tuple_object is None:
											return
										else:
											self.assertTrue(
											    torch.allclose(
											        set_nan_tensor_to_zero(_A		)						,				set_nan_tensor_to_zero(_A		)						,				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(_A		).any()} and `inf`: {torch.isinf(_A		)}. Dict has"""
											        F""" `nan`: {torch.isnan(_A		).any()} and `inf`: {torch.isinf(_A		)}."""
											    )						,				)

									recursive_check(_A						,				_A		)

							for model_class in self.all_model_classes:
								snake_case_						:     Any     =		model_class(_A		)
								model.to(_A		)
								model.eval()

								snake_case_						:     Dict     =		self._prepare_for_class(_A						,				_A		)
								snake_case_						:     List[str]     =		self._prepare_for_class(_A						,				_A		)
								check_equivalence(_A						,				_A						,				_A		)

								snake_case_						:     Union[str, Any]     =		self._prepare_for_class(_A						,				_A						,				return_labels=_A		)
								snake_case_						:     Any     =		self._prepare_for_class(_A						,				_A						,				return_labels=_A		)
								check_equivalence(_A						,				_A						,				_A		)

								snake_case_						:     str     =		self._prepare_for_class(_A						,				_A		)
								snake_case_						:     Union[str, Any]     =		self._prepare_for_class(_A						,				_A		)
								check_equivalence(_A						,				_A						,				_A						,				{'output_hidden_states': True}		)

								snake_case_						:     int     =		self._prepare_for_class(_A						,				_A						,				return_labels=_A		)
								snake_case_						:     Optional[Any]     =		self._prepare_for_class(_A						,				_A						,				return_labels=_A		)
								check_equivalence(_A						,				_A						,				_A						,				{'output_hidden_states': True}		)
@require_torch
class SCREAMING_SNAKE_CASE_  (   unittest.TestCase    ,			snake_case_   ):
						__magic_name__:     List[str]	 =    (MaskFormerSwinBackbone,) if is_torch_available() else ()
						__magic_name__:     int	 =    MaskFormerSwinConfig






						def UpperCAmelCase_			( self : int		)							->      Tuple:







							"""simple docstring"""


							snake_case_						:     Optional[Any]     =		MaskFormerSwinModelTester(self		)






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







							"""simple docstring"""


							snake_case_  ,snake_case_						:     int     =		self.model_tester.prepare_config_and_inputs_for_common()
							snake_case_						:     Any     =		inputs_dict['pixel_values'].shape[0]

							for backbone_class in self.all_model_classes:
								snake_case_						:     str     =		backbone_class(_A		)
								backbone.to(_A		)
								backbone.eval()

								snake_case_						:     List[str]     =		backbone(**_A		)

								# Test default outputs and verify feature maps
								self.assertIsInstance(outputs.feature_maps						,				_A		)
								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
								snake_case_						:     Any     =		backbone(**_A						,				output_hidden_states=_A		)
								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)
										snake_case_  ,snake_case_  ,snake_case_						:     Any     =		hidden_state.shape
										self.assertTrue((h_batch_size, h_n_channels)						,				(batch_size, n_channels)		)

            # Test output_attentions=True
								if self.has_attentions:
									snake_case_						:     Any     =		backbone(**_A						,				output_attentions=_A		)
									self.assertIsNotNone(outputs.attentions		)








def 							SCREAMING_SNAKE_CASE__  (      __a				,	__a = False				):

	if not isinstance(__a				,	__a				):
		snake_case_						:     str     =		f"""Expected string as input, found {type(__a				)}"""
		raise ValueError(__a				)
	if not isinstance(__a				,	__a				):
		snake_case_						:     int     =		f"""Expected boolean as use_pascal parameter, found {type(__a				)}"""
		raise ValueError(__a				)

	snake_case_						:     Union[str, Any]     =		input_str.split('_'				)

	snake_case_						:     int     =		0 if use_pascal else 1

	snake_case_						:     List[Any]     =		words[start_index:]

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

	snake_case_						:     Optional[Any]     =		'' if use_pascal else words[0]

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


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

						testmod()

