
'''simple docstring'''







import collections
import inspect
import unittest

from transformers import SwinConfig
from transformers.testing_utils import require_torch, require_vision, slow, torch_device
from transformers.utils import cached_property, is_torch_available, is_vision_available

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


if is_torch_available():
					import torch
					from torch import nn

					from transformers import SwinBackbone, SwinForImageClassification, SwinForMaskedImageModeling, SwinModel
					from transformers.models.swin.modeling_swin import SWIN_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
					from PIL import Image

					from transformers import AutoImageProcessor
class  SCREAMING_SNAKE_CASE__       :
	def __init__(				self  ,    __UpperCamelCase  ,    __UpperCamelCase=13  ,    __UpperCamelCase=32  ,    __UpperCamelCase=2  ,    __UpperCamelCase=3  ,    __UpperCamelCase=16  ,    __UpperCamelCase=[1, 2, 1]  ,    __UpperCamelCase=[2, 2, 4]  ,    __UpperCamelCase=2  ,    __UpperCamelCase=2.0  ,    __UpperCamelCase=True  ,    __UpperCamelCase=0.0  ,    __UpperCamelCase=0.0  ,    __UpperCamelCase=0.1  ,    __UpperCamelCase="gelu"  ,    __UpperCamelCase=False  ,    __UpperCamelCase=True  ,    __UpperCamelCase=0.0_2  ,    __UpperCamelCase=1E-5  ,    __UpperCamelCase=True  ,    __UpperCamelCase=None  ,    __UpperCamelCase=True  ,    __UpperCamelCase=10  ,    __UpperCamelCase=8  ,    __UpperCamelCase=["stage1", "stage2"]  ,    __UpperCamelCase=[1, 2]  ,    ):



					'''simple docstring'''






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



					'''simple docstring'''






					__a       :     List[Any]								=	floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]			)

					__a       :     Optional[Any]								=	None
					if self.use_labels:
									__a       :     List[str]								=	ids_tensor([self.batch_size]  ,    self.type_sequence_label_size			)

					__a       :     int								=	self.get_config()

					return config, pixel_values, labels
	def __lowerCamelCase    (				self			):



					'''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  ,    out_features=self.out_features  ,    out_indices=self.out_indices  ,    )
	def __lowerCamelCase    (				self  ,    __UpperCamelCase  ,    __UpperCamelCase  ,    __UpperCamelCase			):



					'''simple docstring'''






					__a       :     List[Any]								=	SwinModel(config=__UpperCamelCase			)
					model.to(__UpperCamelCase			)
					model.eval()
					__a       :     Dict								=	model(__UpperCamelCase			)

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



					'''simple docstring'''






					__a       :     Dict								=	SwinBackbone(config=__UpperCamelCase			)
					model.to(__UpperCamelCase			)
					model.eval()
					__a       :     List[str]								=	model(__UpperCamelCase			)

					# verify hidden states
					self.parent.assertEqual(len(result.feature_maps			)  ,    len(config.out_features			)			)
					self.parent.assertListEqual(list(result.feature_maps[0].shape			)  ,    [self.batch_size, model.channels[0], 16, 16]			)

					# verify channels
					self.parent.assertEqual(len(model.channels			)  ,    len(config.out_features			)			)

					# verify backbone works with out_features=None
					__a       :     List[str]								=	None
					__a       :     Any								=	SwinBackbone(config=__UpperCamelCase			)
					model.to(__UpperCamelCase			)
					model.eval()
					__a       :     int								=	model(__UpperCamelCase			)

					# verify feature maps
					self.parent.assertEqual(len(result.feature_maps			)  ,    1			)
					self.parent.assertListEqual(list(result.feature_maps[0].shape			)  ,    [self.batch_size, model.channels[-1], 4, 4]			)

					# verify channels
					self.parent.assertEqual(len(model.channels			)  ,    1			)
	def __lowerCamelCase    (				self  ,    __UpperCamelCase  ,    __UpperCamelCase  ,    __UpperCamelCase			):



					'''simple docstring'''






					__a       :     Tuple								=	SwinForMaskedImageModeling(config=__UpperCamelCase			)
					model.to(__UpperCamelCase			)
					model.eval()
					__a       :     str								=	model(__UpperCamelCase			)
					self.parent.assertEqual(
					    result.logits.shape  ,    (self.batch_size, self.num_channels, self.image_size, self.image_size)			)

					# test greyscale images
					__a       :     str								=	1
					__a       :     Optional[Any]								=	SwinForMaskedImageModeling(__UpperCamelCase			)
					model.to(__UpperCamelCase			)
					model.eval()

					__a       :     Any								=	floats_tensor([self.batch_size, 1, self.image_size, self.image_size]			)
					__a       :     Union[str, Any]								=	model(__UpperCamelCase			)
					self.parent.assertEqual(result.logits.shape  ,    (self.batch_size, 1, self.image_size, self.image_size)			)
	def __lowerCamelCase    (				self  ,    __UpperCamelCase  ,    __UpperCamelCase  ,    __UpperCamelCase			):



					'''simple docstring'''






					__a       :     Dict								=	self.type_sequence_label_size
					__a       :     Any								=	SwinForImageClassification(__UpperCamelCase			)
					model.to(__UpperCamelCase			)
					model.eval()
					__a       :     List[Any]								=	model(__UpperCamelCase  ,    labels=__UpperCamelCase			)
					self.parent.assertEqual(result.logits.shape  ,    (self.batch_size, self.type_sequence_label_size)			)

					# test greyscale images
					__a       :     Optional[int]								=	1
					__a       :     List[Any]								=	SwinForImageClassification(__UpperCamelCase			)
					model.to(__UpperCamelCase			)
					model.eval()

					__a       :     str								=	floats_tensor([self.batch_size, 1, self.image_size, self.image_size]			)
					__a       :     List[Any]								=	model(__UpperCamelCase			)
					self.parent.assertEqual(result.logits.shape  ,    (self.batch_size, self.type_sequence_label_size)			)

	def __lowerCamelCase    (				self			):



					'''simple docstring'''






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







@require_torch
class  SCREAMING_SNAKE_CASE__       (  __UpperCamelCase			,					__UpperCamelCase			,					unittest.TestCase   ):
	lowercase__	    =							(
	    (
	        SwinModel,
	        SwinBackbone,
	        SwinForImageClassification,
	        SwinForMaskedImageModeling,
	    )
	    if is_torch_available()
	    else ()
	)
	lowercase__	    =							(
	    {"feature-extraction": SwinModel, "image-classification": SwinForImageClassification}
	    if is_torch_available()
	    else {}
	)
	lowercase__	    =							True

	lowercase__	    =							False
	lowercase__	    =							False
	lowercase__	    =							False
	def __lowerCamelCase    (				self			):



					'''simple docstring'''






					__a       :     Tuple								=	SwinModelTester(self			)
					__a       :     List[str]								=	ConfigTester(self  ,    config_class=__UpperCamelCase  ,    embed_dim=37			)
	def __lowerCamelCase    (				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 __lowerCamelCase    (				self			):



					'''simple docstring'''






					return
	def __lowerCamelCase    (				self			):



					'''simple docstring'''






					__a       :     Tuple								=	self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_model(*__UpperCamelCase			)
	@unittest.skip(reason="""Got `CUDA error: misaligned address` with PyTorch 2.0.0."""			)
	def __lowerCamelCase    (				self			):



					'''simple docstring'''






					pass
	def __lowerCamelCase    (				self			):



					'''simple docstring'''






					super().test_training_gradient_checkpointing()
	def __lowerCamelCase    (				self			):



					'''simple docstring'''






					__a       :     Tuple								=	self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_backbone(*__UpperCamelCase			)
	def __lowerCamelCase    (				self			):



					'''simple docstring'''






					__a       :     Dict								=	self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_for_masked_image_modeling(*__UpperCamelCase			)
	def __lowerCamelCase    (				self			):



					'''simple docstring'''






					__a       :     int								=	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			):



					'''simple docstring'''






					pass
	@unittest.skip(reason="""Swin Transformer does not use feedforward chunking"""			)
	def __lowerCamelCase    (				self			):



					'''simple docstring'''






					pass
	def __lowerCamelCase    (				self			):



					'''simple docstring'''






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

					for model_class in self.all_model_classes:
									__a       :     int								=	model_class(__UpperCamelCase			)
									self.assertIsInstance(model.get_input_embeddings()  ,    (nn.Module)			)
									__a       :     Union[str, Any]								=	model.get_output_embeddings()
									self.assertTrue(x is None or isinstance(__UpperCamelCase  ,    nn.Linear			)			)
	def __lowerCamelCase    (				self			):



					'''simple docstring'''






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

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

									__a       :     int								=	["""pixel_values"""]
									self.assertListEqual(arg_names[:1]  ,    __UpperCamelCase			)
	def __lowerCamelCase    (				self			):



					'''simple docstring'''






					__a							,	__a       :     int								=	self.model_tester.prepare_config_and_inputs_for_common()
					__a       :     Dict								=	True

					for model_class in self.all_model_classes:
									__a       :     Any								=	True
									__a       :     str								=	False
									__a       :     Any								=	True
									__a       :     Dict								=	model_class(__UpperCamelCase			)
									model.to(__UpperCamelCase			)
									model.eval()
									with torch.no_grad():
													__a       :     Optional[Any]								=	model(**self._prepare_for_class(__UpperCamelCase  ,    __UpperCamelCase			)			)
									__a       :     str								=	outputs.attentions
									__a       :     Optional[Any]								=	len(self.model_tester.depths			)
									self.assertEqual(len(__UpperCamelCase			)  ,    __UpperCamelCase			)

									# check that output_attentions also work using config
									del inputs_dict["output_attentions"]
									__a       :     Optional[Any]								=	True
									__a       :     Dict								=	config.window_size**2
									__a       :     Any								=	model_class(__UpperCamelCase			)
									model.to(__UpperCamelCase			)
									model.eval()
									with torch.no_grad():
													__a       :     Optional[int]								=	model(**self._prepare_for_class(__UpperCamelCase  ,    __UpperCamelCase			)			)
									__a       :     str								=	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]  ,    )
									__a       :     int								=	len(__UpperCamelCase			)

									# Check attention is always last and order is fine
									__a       :     str								=	True
									__a       :     Union[str, Any]								=	True
									__a       :     Tuple								=	model_class(__UpperCamelCase			)
									model.to(__UpperCamelCase			)
									model.eval()
									with torch.no_grad():
													__a       :     Optional[int]								=	model(**self._prepare_for_class(__UpperCamelCase  ,    __UpperCamelCase			)			)

									# also another +1 for reshaped_hidden_states
									__a       :     List[Any]								=	1 if model_class.__name__ == """SwinBackbone""" else 2
									self.assertEqual(out_len + added_hidden_states  ,    len(__UpperCamelCase			)			)

									__a       :     Optional[int]								=	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  ,    __UpperCamelCase  ,    __UpperCamelCase  ,    __UpperCamelCase  ,    __UpperCamelCase			):



					'''simple docstring'''






					__a       :     Union[str, Any]								=	model_class(__UpperCamelCase			)
					model.to(__UpperCamelCase			)
					model.eval()

					with torch.no_grad():
									__a       :     Optional[Any]								=	model(**self._prepare_for_class(__UpperCamelCase  ,    __UpperCamelCase			)			)

					__a       :     Tuple								=	outputs.hidden_states

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

					__a       :     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]  ,    )

					if not model_class.__name__ == "SwinBackbone":
									__a       :     Union[str, Any]								=	outputs.reshaped_hidden_states
									self.assertEqual(len(__UpperCamelCase			)  ,    __UpperCamelCase			)

									__a							,	__a							,	__a							,	__a       :     Union[str, Any]								=	reshaped_hidden_states[0].shape
									__a       :     Union[str, Any]								=	(
									    reshaped_hidden_states[0].view(__UpperCamelCase  ,    __UpperCamelCase  ,    height * width			).permute(0  ,    2  ,    1			)
									)
									self.assertListEqual(
									    list(reshaped_hidden_states.shape[-2:]			)  ,    [num_patches, self.model_tester.embed_dim]  ,    )
	def __lowerCamelCase    (				self			):



					'''simple docstring'''






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

					__a       :     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)
					)

					for model_class in self.all_model_classes:
									__a       :     Optional[int]								=	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"]
									__a       :     int								=	True

									self.check_hidden_states_output(__UpperCamelCase  ,    __UpperCamelCase  ,    __UpperCamelCase  ,    __UpperCamelCase			)
	def __lowerCamelCase    (				self			):



					'''simple docstring'''






					__a							,	__a       :     List[Any]								=	self.model_tester.prepare_config_and_inputs_for_common()
					__a       :     Optional[int]								=	3

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

					__a       :     Any								=	image_size[0] + patch_size[0] - (image_size[0] % patch_size[0])
					__a       :     int								=	image_size[1] + patch_size[1] - (image_size[1] % patch_size[1])

					for model_class in self.all_model_classes:
									__a       :     List[Any]								=	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"]
									__a       :     List[Any]								=	True
									self.check_hidden_states_output(__UpperCamelCase  ,    __UpperCamelCase  ,    __UpperCamelCase  ,    (padded_height, padded_width)			)
	@slow
	def __lowerCamelCase    (				self			):



					'''simple docstring'''






					for model_name in SWIN_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
									__a       :     Union[str, Any]								=	SwinModel.from_pretrained(__UpperCamelCase			)
									self.assertIsNotNone(__UpperCamelCase			)

	def __lowerCamelCase    (				self			):



					'''simple docstring'''






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

					__a       :     str								=	_config_zero_init(__UpperCamelCase			)
					for model_class in self.all_model_classes:
									__a       :     Any								=	model_class(config=__UpperCamelCase			)
									for name, param in model.named_parameters():
													if "embeddings" not in name and param.requires_grad:
																	self.assertIn(
																	    ((param.data.mean() * 1E9).round() / 1E9).item()  ,    [0.0, 1.0]  ,    msg=f"""Parameter {name} of model {model_class} seems not properly initialized"""  ,    )







@require_vision
@require_torch
class  SCREAMING_SNAKE_CASE__       (  unittest.TestCase   ):
	@cached_property
	def __lowerCamelCase    (				self			):



					'''simple docstring'''






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

	@slow
	def __lowerCamelCase    (				self			):



					'''simple docstring'''






					__a       :     str								=	SwinForImageClassification.from_pretrained("""microsoft/swin-tiny-patch4-window7-224"""			).to(__UpperCamelCase			)
					__a       :     Any								=	self.default_image_processor

					__a       :     Optional[Any]								=	Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png"""			)
					__a       :     List[Any]								=	image_processor(images=__UpperCamelCase  ,    return_tensors="""pt"""			).to(__UpperCamelCase			)

					# forward pass
					with torch.no_grad():
									__a       :     Tuple								=	model(**__UpperCamelCase			)

					# verify the logits
					__a       :     List[str]								=	torch.Size((1, 1000)			)
					self.assertEqual(outputs.logits.shape  ,    __UpperCamelCase			)
					__a       :     List[str]								=	torch.tensor([-0.0_9_4_8, -0.6_4_5_4, -0.0_9_2_1]			).to(__UpperCamelCase			)
					self.assertTrue(torch.allclose(outputs.logits[0, :3]  ,    __UpperCamelCase  ,    atol=1E-4			)			)







@require_torch
class  SCREAMING_SNAKE_CASE__       (  unittest.TestCase			,					__UpperCamelCase   ):
	lowercase__	    =							(SwinBackbone,) if is_torch_available() else ()
	lowercase__	    =							SwinConfig
	def __lowerCamelCase    (				self			):



					'''simple docstring'''






					__a       :     Dict								=	SwinModelTester(self			)

'''simple docstring'''







def 				_snake_case (		lowercase = 1_0_0_0						)       ->       int:
				__a       :     Any								=	[]
				__a       :     List[Any]								=	[]
				__a       :     Any								=	3
				__a       :     Optional[Any]								=	5
				__a       :     Optional[Any]								=	1
				while True:
								__a       :     List[str]								=	z * temp
								if result < n:
												zmulti.append(lowercase						)
												temp += 1
								else:
												__a       :     Any								=	1
												break
				while True:
								__a       :     Optional[int]								=	x * temp
								if result < n:
												xmulti.append(lowercase						)
												temp += 1
								else:
												break
				__a       :     Any								=	list(set(xmulti + zmulti						)						)
				return sum(lowercase						)


if __name__ == "__main__":
					print(f'''{solution() = }''')