



'''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 	_lowerCAmelCase     :






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







									'''simple docstring'''





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







									'''simple docstring'''





									_lowercase							:		Optional[Any]				=   floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]		)

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

									_lowercase							:		Dict				=   self.get_config()

									return config, pixel_values, labels
		def 						__lowercase      (						self							:       List[Any]		)      ->			Tuple:







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







									'''simple docstring'''





									_lowercase							:		str				=   SwinModel(config=UpperCamelCase_		)
									model.to(UpperCamelCase_		)
									model.eval()
									_lowercase							:		str				=   model(UpperCamelCase_		)

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







									'''simple docstring'''





									_lowercase							:		int				=   SwinBackbone(config=UpperCamelCase_		)
									model.to(UpperCamelCase_		)
									model.eval()
									_lowercase							:		int				=   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
									_lowercase							:		Dict				=   None
									_lowercase							:		Dict				=   SwinBackbone(config=UpperCamelCase_		)
									model.to(UpperCamelCase_		)
									model.eval()
									_lowercase							:		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 						__lowercase      (						self							:       Dict      ,		UpperCamelCase_							:       Dict      ,		UpperCamelCase_							:       List[str]      ,		UpperCamelCase_							:       Optional[Any]		)      ->			int:







									'''simple docstring'''





									_lowercase							:		Optional[Any]				=   SwinForMaskedImageModeling(config=UpperCamelCase_		)
									model.to(UpperCamelCase_		)
									model.eval()
									_lowercase							:		Optional[Any]				=   model(UpperCamelCase_		)
									self.parent.assertEqual(
									    result.logits.shape      ,		(self.batch_size, self.num_channels, self.image_size, self.image_size)		)

									# test greyscale images
									_lowercase							:		str				=   1
									_lowercase							:		Tuple				=   SwinForMaskedImageModeling(UpperCamelCase_		)
									model.to(UpperCamelCase_		)
									model.eval()

									_lowercase							:		Optional[int]				=   floats_tensor([self.batch_size, 1, self.image_size, self.image_size]		)
									_lowercase							:		List[Any]				=   model(UpperCamelCase_		)
									self.parent.assertEqual(result.logits.shape      ,		(self.batch_size, 1, self.image_size, self.image_size)		)
		def 						__lowercase      (						self							:       Any      ,		UpperCamelCase_							:       List[str]      ,		UpperCamelCase_							:       Tuple      ,		UpperCamelCase_							:       Tuple		)      ->			Union[str, Any]:







									'''simple docstring'''





									_lowercase							:		Optional[int]				=   self.type_sequence_label_size
									_lowercase							:		Optional[int]				=   SwinForImageClassification(UpperCamelCase_		)
									model.to(UpperCamelCase_		)
									model.eval()
									_lowercase							:		Optional[Any]				=   model(UpperCamelCase_      ,		labels=UpperCamelCase_		)
									self.parent.assertEqual(result.logits.shape      ,		(self.batch_size, self.type_sequence_label_size)		)

									# test greyscale images
									_lowercase							:		Any				=   1
									_lowercase							:		Dict				=   SwinForImageClassification(UpperCamelCase_		)
									model.to(UpperCamelCase_		)
									model.eval()

									_lowercase							:		List[Any]				=   floats_tensor([self.batch_size, 1, self.image_size, self.image_size]		)
									_lowercase							:		Optional[Any]				=   model(UpperCamelCase_		)
									self.parent.assertEqual(result.logits.shape      ,		(self.batch_size, self.type_sequence_label_size)		)





		def 						__lowercase      (						self							:       Dict		)      ->			int:







									'''simple docstring'''





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




@require_torch
class 	_lowerCAmelCase     (    __A     ,		__A     ,		unittest.TestCase       ):






		'''simple docstring'''
		snake_case_							= (
		    (
		        SwinModel,
		        SwinBackbone,
		        SwinForImageClassification,
		        SwinForMaskedImageModeling,
		    )
		    if is_torch_available()
		    else ()
		)
		snake_case_							= (
		    {'feature-extraction': SwinModel, 'image-classification': SwinForImageClassification}
		    if is_torch_available()
		    else {}
		)
		snake_case_							= True

		snake_case_							= False
		snake_case_							= False
		snake_case_							= False
		def 						__lowercase      (						self							:       Optional[int]		)      ->			Tuple:







									'''simple docstring'''





									_lowercase							:		List[str]				=   SwinModelTester(self		)
									_lowercase							:		List[str]				=   ConfigTester(self      ,		config_class=UpperCamelCase_      ,		embed_dim=37		)
		def 						__lowercase      (						self							:       Optional[Any]		)      ->			Any:







									'''simple docstring'''





									self.create_and_test_config_common_properties()
									self.config_tester.create_and_test_config_to_json_string()
									self.config_tester.create_and_test_config_to_json_file()
									self.config_tester.create_and_test_config_from_and_save_pretrained()
									self.config_tester.create_and_test_config_with_num_labels()
									self.config_tester.check_config_can_be_init_without_params()
									self.config_tester.check_config_arguments_init()
		def 						__lowercase      (						self							:       Union[str, Any]		)      ->			Optional[int]:







									'''simple docstring'''





									return
		def 						__lowercase      (						self							:       List[Any]		)      ->			Optional[int]:







									'''simple docstring'''





									_lowercase							:		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 						__lowercase      (						self							:       Any		)      ->			Any:







									'''simple docstring'''





									pass
		def 						__lowercase      (						self							:       List[Any]		)      ->			Optional[int]:







									'''simple docstring'''





									super().test_training_gradient_checkpointing()
		def 						__lowercase      (						self							:       str		)      ->			Dict:







									'''simple docstring'''





									_lowercase							:		Optional[int]				=   self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_backbone(*UpperCamelCase_		)
		def 						__lowercase      (						self							:       List[Any]		)      ->			Union[str, Any]:







									'''simple docstring'''





									_lowercase							:		int				=   self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_for_masked_image_modeling(*UpperCamelCase_		)
		def 						__lowercase      (						self							:       Union[str, Any]		)      ->			Dict:







									'''simple docstring'''





									_lowercase							:		Dict				=   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 						__lowercase      (						self							:       str		)      ->			Optional[int]:







									'''simple docstring'''





									pass
		@unittest.skip(reason='''Swin Transformer does not use feedforward chunking'''		)
		def 						__lowercase      (						self							:       Optional[int]		)      ->			str:







									'''simple docstring'''





									pass
		def 						__lowercase      (						self							:       List[Any]		)      ->			Optional[Any]:







									'''simple docstring'''





									_lowercase			, _lowercase							:		Any				=   self.model_tester.prepare_config_and_inputs_for_common()

									for model_class in self.all_model_classes:
																_lowercase							:		int				=   model_class(UpperCamelCase_		)
																self.assertIsInstance(model.get_input_embeddings()      ,		(nn.Module)		)
																_lowercase							:		Tuple				=   model.get_output_embeddings()
																self.assertTrue(x is None or isinstance(UpperCamelCase_      ,		nn.Linear		)		)
		def 						__lowercase      (						self							:       List[str]		)      ->			Dict:







									'''simple docstring'''





									_lowercase			, _lowercase							:		int				=   self.model_tester.prepare_config_and_inputs_for_common()

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

																_lowercase							:		Any				=   ['''pixel_values''']
																self.assertListEqual(arg_names[:1]      ,		UpperCamelCase_		)
		def 						__lowercase      (						self							:       Any		)      ->			int:







									'''simple docstring'''





									_lowercase			, _lowercase							:		Dict				=   self.model_tester.prepare_config_and_inputs_for_common()
									_lowercase							:		Union[str, Any]				=   True

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

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

																# Check attention is always last and order is fine
																_lowercase							:		Tuple				=   True
																_lowercase							:		Tuple				=   True
																_lowercase							:		Tuple				=   model_class(UpperCamelCase_		)
																model.to(UpperCamelCase_		)
																model.eval()
																with torch.no_grad():
																							_lowercase							:		Any				=   model(**self._prepare_for_class(UpperCamelCase_      ,		UpperCamelCase_		)		)

																# also another +1 for reshaped_hidden_states
																_lowercase							:		Dict				=   1 if model_class.__name__ == '''SwinBackbone''' else 2
																self.assertEqual(out_len + added_hidden_states      ,		len(UpperCamelCase_		)		)

																_lowercase							:		str				=   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 						__lowercase      (						self							:       List[Any]      ,		UpperCamelCase_							:       Optional[Any]      ,		UpperCamelCase_							:       Any      ,		UpperCamelCase_							:       int      ,		UpperCamelCase_							:       List[Any]		)      ->			List[Any]:







									'''simple docstring'''





									_lowercase							:		Union[str, Any]				=   model_class(UpperCamelCase_		)
									model.to(UpperCamelCase_		)
									model.eval()

									with torch.no_grad():
																_lowercase							:		Any				=   model(**self._prepare_for_class(UpperCamelCase_      ,		UpperCamelCase_		)		)

									_lowercase							:		Dict				=   outputs.hidden_states

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

									_lowercase							:		int				=   (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":
																_lowercase							:		Dict				=   outputs.reshaped_hidden_states
																self.assertEqual(len(UpperCamelCase_		)      ,		UpperCamelCase_		)

																_lowercase			, _lowercase			, _lowercase			, _lowercase							:		Tuple				=   reshaped_hidden_states[0].shape
																_lowercase							:		Optional[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 						__lowercase      (						self							:       str		)      ->			List[str]:







									'''simple docstring'''





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

									_lowercase							:		Dict				=   (
									    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:
																_lowercase							:		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"]
																_lowercase							:		Optional[Any]				=   True

																self.check_hidden_states_output(UpperCamelCase_      ,		UpperCamelCase_      ,		UpperCamelCase_      ,		UpperCamelCase_		)
		def 						__lowercase      (						self							:       Optional[Any]		)      ->			Optional[int]:







									'''simple docstring'''





									_lowercase			, _lowercase							:		str				=   self.model_tester.prepare_config_and_inputs_for_common()
									_lowercase							:		int				=   3

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

									_lowercase							:		Dict				=   image_size[0] + patch_size[0] - (image_size[0] % patch_size[0])
									_lowercase							:		List[Any]				=   image_size[1] + patch_size[1] - (image_size[1] % patch_size[1])

									for model_class in self.all_model_classes:
																_lowercase							:		int				=   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"]
																_lowercase							:		Any				=   True
																self.check_hidden_states_output(UpperCamelCase_      ,		UpperCamelCase_      ,		UpperCamelCase_      ,		(padded_height, padded_width)		)
		@slow
		def 						__lowercase      (						self							:       Dict		)      ->			Optional[Any]:







									'''simple docstring'''





									for model_name in SWIN_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																_lowercase							:		List[Any]				=   SwinModel.from_pretrained(UpperCamelCase_		)
																self.assertIsNotNone(UpperCamelCase_		)





		def 						__lowercase      (						self							:       Any		)      ->			Dict:







									'''simple docstring'''





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

									_lowercase							:		int				=   _config_zero_init(UpperCamelCase_		)
									for model_class in self.all_model_classes:
																_lowercase							:		Union[str, 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 	_lowerCAmelCase     (    unittest.TestCase       ):






		'''simple docstring'''
		@cached_property
		def 						__lowercase      (						self							:       str		)      ->			List[str]:







									'''simple docstring'''





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





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







									'''simple docstring'''





									_lowercase							:		int				=   SwinForImageClassification.from_pretrained('''microsoft/swin-tiny-patch4-window7-224'''		).to(UpperCamelCase_		)
									_lowercase							:		Any				=   self.default_image_processor

									_lowercase							:		Optional[int]				=   Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''		)
									_lowercase							:		Optional[int]				=   image_processor(images=UpperCamelCase_      ,		return_tensors='''pt'''		).to(UpperCamelCase_		)

									# forward pass
									with torch.no_grad():
																_lowercase							:		Optional[int]				=   model(**UpperCamelCase_		)

									# verify the logits
									_lowercase							:		str				=   torch.Size((1, 1_000)		)
									self.assertEqual(outputs.logits.shape      ,		UpperCamelCase_		)
									_lowercase							:		List[str]				=   torch.tensor([-0.0_948, -0.6_454, -0.0_921]		).to(UpperCamelCase_		)
									self.assertTrue(torch.allclose(outputs.logits[0, :3]      ,		UpperCamelCase_      ,		atol=1e-4		)		)




@require_torch
class 	_lowerCAmelCase     (    unittest.TestCase     ,		__A       ):






		'''simple docstring'''
		snake_case_							= (SwinBackbone,) if is_torch_available() else ()
		snake_case_							= SwinConfig
		def 						__lowercase      (						self							:       Any		)      ->			Tuple:







									'''simple docstring'''





									_lowercase							:		Optional[int]				=   SwinModelTester(self		)








'''simple docstring'''






import math as m





def 				_SCREAMING_SNAKE_CASE(					snake_case_    :     Union[str, Any] ,       snake_case_    :     int ,       snake_case_    :     Optional[Any]=0.0_0_1  )		->Tuple:






							'''simple docstring'''
							return (f(a + h  ) - f(a - h  )) / (2 * h)





def 				_SCREAMING_SNAKE_CASE(					snake_case_    :     Dict ,       snake_case_    :     List[Any]=0 ,       snake_case_    :     Dict=1_00 ,       snake_case_    :     List[str]=0.0_0_0_1 ,       snake_case_    :     str=1E-6 ,       snake_case_    :     Tuple=False  )		->List[Any]:






							'''simple docstring'''
							_lowercase							:		Tuple				=   xa  # set the initial guess
							_lowercase							:		List[str]				=   [a]
							_lowercase							:		str				=   abs(f(snake_case_  )  )
							_lowercase							:		Dict				=   lambda snake_case_  : calc_derivative(snake_case_ ,       snake_case_ ,       h=snake_case_  )  # noqa: E731  Derivative of f(x)
							for _ in range(snake_case_  ):
														if fa(snake_case_  ) == 0:
																					raise ValueError('''No converging solution found'''  )
														_lowercase							:		Tuple				=   a - f(snake_case_  ) / fa(snake_case_  )  # Calculate the next estimate
														if logsteps:
																					steps.append(snake_case_  )
														if error < maxerror:
																					break
							else:
														raise ValueError('''Iteration limit reached, no converging solution found'''  )
							if logsteps:
														# If logstep is true, then log intermediate steps
														return a, error, steps
							return a, error


if __name__ == "__main__":
						from matplotlib import pyplot as plt

						lowerCamelCase__	  =		lambda x: m.tanh(x) ** 2 - m.exp(3 * x)  # noqa: E731
						lowerCamelCase__  ,      lowerCamelCase__  ,      lowerCamelCase__	  =		newton_raphson(
						    f, xa=10, maxiter=10_00, step=1e-6, logsteps=True
						)
						plt.plot([abs(f(x)) for x in steps])
						plt.xlabel('step')
						plt.ylabel('error')
						plt.show()
						print(f'''solution = {{{solution:f}}}, error = {{{error:f}}}''')



