
from __future__ import annotations

import inspect
import unittest

import numpy as np

from transformers import SwinConfig
from transformers.testing_utils import require_tf, require_vision, slow, to_atuple
from transformers.utils import cached_property, is_tf_available, is_vision_available

from ...test_configuration_common import ConfigTester
from ...test_modeling_tf_common import TFModelTesterMixin, floats_tensor, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_tf_available():
					import tensorflow as tf

					from transformers.models.swin.modeling_tf_swin import (
					    TF_SWIN_PRETRAINED_MODEL_ARCHIVE_LIST,
					    TFSwinForImageClassification,
					    TFSwinForMaskedImageModeling,
					    TFSwinModel,
					)


if is_vision_available():
					from PIL import Image

					from transformers import AutoImageProcessor







class 	A__				:






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






							'''simple docstring'''







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






				def 				_lowerCamelCase						(		self   : Optional[int]  ):






							'''simple docstring'''







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

							lowerCAmelCase__ : int   =						None
							if self.use_labels:
										lowerCAmelCase__ : Union[str, Any]   =						ids_tensor([self.batch_size]       ,	self.type_sequence_label_size  )

							lowerCAmelCase__ : str   =						self.get_config()

							return config, pixel_values, labels






				def 				_lowerCamelCase						(		self   : Dict  ):






							'''simple docstring'''







							return SwinConfig(
							    image_size=self.image_size       ,	patch_size=self.patch_size       ,	num_channels=self.num_channels       ,	embed_dim=self.embed_dim       ,	depths=self.depths       ,	num_heads=self.num_heads       ,	window_size=self.window_size       ,	mlp_ratio=self.mlp_ratio       ,	qkv_bias=self.qkv_bias       ,	hidden_dropout_prob=self.hidden_dropout_prob       ,	attention_probs_dropout_prob=self.attention_probs_dropout_prob       ,	drop_path_rate=self.drop_path_rate       ,	hidden_act=self.hidden_act       ,	use_absolute_embeddings=self.use_absolute_embeddings       ,	path_norm=self.patch_norm       ,	layer_norm_eps=self.layer_norm_eps       ,	initializer_range=self.initializer_range       ,	encoder_stride=self.encoder_stride       ,	)






				def 				_lowerCamelCase						(		self   : List[Any]       ,	a   : Optional[int]       ,	a   : Any       ,	a   : Any  ):






							'''simple docstring'''







							lowerCAmelCase__ : Optional[int]   =						TFSwinModel(config=a  )
							lowerCAmelCase__ : str   =						model(a  )

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






							'''simple docstring'''







							lowerCAmelCase__ : List[Any]   =						TFSwinForMaskedImageModeling(config=a  )
							lowerCAmelCase__ : str   =						model(a  )
							self.parent.assertEqual(
							    result.logits.shape       ,	(self.batch_size, self.num_channels, self.image_size, self.image_size)  )

							# test greyscale images
							lowerCAmelCase__ : Any   =						1
							lowerCAmelCase__ : List[str]   =						TFSwinForMaskedImageModeling(a  )

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






				def 				_lowerCamelCase						(		self   : List[Any]       ,	a   : List[str]       ,	a   : Union[str, Any]       ,	a   : Any  ):






							'''simple docstring'''







							lowerCAmelCase__ : Optional[int]   =						self.type_sequence_label_size
							lowerCAmelCase__ : Tuple   =						TFSwinForImageClassification(a  )
							lowerCAmelCase__ : Tuple   =						model(a       ,	labels=a  )
							self.parent.assertEqual(result.logits.shape       ,	(self.batch_size, self.type_sequence_label_size)  )

							# test greyscale images
							lowerCAmelCase__ : List[Any]   =						1
							lowerCAmelCase__ : List[str]   =						TFSwinForImageClassification(a  )
							lowerCAmelCase__ : Dict   =						floats_tensor([self.batch_size, 1, self.image_size, self.image_size]  )
							lowerCAmelCase__ : Union[str, Any]   =						model(a  )
							self.parent.assertEqual(result.logits.shape       ,	(self.batch_size, self.type_sequence_label_size)  )






				def 				_lowerCamelCase						(		self   : Optional[int]  ):






							'''simple docstring'''







							lowerCAmelCase__ : Union[str, Any]   =						self.prepare_config_and_inputs()
							lowerCAmelCase__ ,							lowerCAmelCase__ ,							lowerCAmelCase__ : Dict   =						config_and_inputs
							lowerCAmelCase__ : Optional[int]   =						{'pixel_values': pixel_values}
							return config, inputs_dict







@require_tf
class 	A__				(	__magic_name__		,  __magic_name__		,  unittest.TestCase      ):
				lowercase            =    (
				    (
				        TFSwinModel,
				        TFSwinForImageClassification,
				        TFSwinForMaskedImageModeling,
				    )
				    if is_tf_available()
				    else ()
				)
				lowercase            =    (
				    {'feature-extraction': TFSwinModel, 'image-classification': TFSwinForImageClassification}
				    if is_tf_available()
				    else {}
				)

				lowercase            =    False
				lowercase            =    False
				lowercase            =    False
				lowercase            =    False






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






							'''simple docstring'''







							lowerCAmelCase__ : Optional[Any]   =						TFSwinModelTester(self  )
							lowerCAmelCase__ : List[Any]   =						ConfigTester(self       ,	config_class=a       ,	embed_dim=37  )






				def 				_lowerCamelCase						(		self   : 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 				_lowerCamelCase						(		self   : Dict  ):






							'''simple docstring'''







							return






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






							'''simple docstring'''







							lowerCAmelCase__ : Union[str, Any]   =						self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_model(*a  )






				def 				_lowerCamelCase						(		self   : Optional[int]  ):






							'''simple docstring'''







							lowerCAmelCase__ : Union[str, Any]   =						self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_for_masked_image_modeling(*a  )






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






							'''simple docstring'''







							lowerCAmelCase__ : List[str]   =						self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_for_image_classification(*a  )






				@unittest.skip(reason='Swin does not use inputs_embeds'  )
				def 				_lowerCamelCase						(		self   : Union[str, Any]  ):






							'''simple docstring'''







							pass






				def 				_lowerCamelCase						(		self   : Optional[int]  ):






							'''simple docstring'''







							lowerCAmelCase__ ,							lowerCAmelCase__ : Dict   =						self.model_tester.prepare_config_and_inputs_for_common()

							for model_class in self.all_model_classes:
										lowerCAmelCase__ : int   =						model_class(a  )
										self.assertIsInstance(model.get_input_embeddings()       ,	tf.keras.layers.Layer  )
										lowerCAmelCase__ : Optional[int]   =						model.get_output_embeddings()
										self.assertTrue(x is None or isinstance(a       ,	tf.keras.layers.Dense  )  )






				def 				_lowerCamelCase						(		self   : Any  ):






							'''simple docstring'''







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

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

										lowerCAmelCase__ : Dict   =						['pixel_values']
										self.assertListEqual(arg_names[:1]       ,	a  )






				def 				_lowerCamelCase						(		self   : Any  ):






							'''simple docstring'''







							lowerCAmelCase__ ,							lowerCAmelCase__ : Dict   =						self.model_tester.prepare_config_and_inputs_for_common()
							lowerCAmelCase__ : Dict   =						True

							for model_class in self.all_model_classes:
										lowerCAmelCase__ : Union[str, Any]   =						True
										lowerCAmelCase__ : Union[str, Any]   =						False
										lowerCAmelCase__ : List[Any]   =						True
										lowerCAmelCase__ : List[str]   =						model_class(a  )
										lowerCAmelCase__ : List[str]   =						model(**self._prepare_for_class(a       ,	a  )  )
										lowerCAmelCase__ : Union[str, Any]   =						outputs.attentions
										lowerCAmelCase__ : Optional[int]   =						len(self.model_tester.depths  )
										self.assertEqual(len(a  )       ,	a  )

										# check that output_attentions also work using config
										del inputs_dict["output_attentions"]
										lowerCAmelCase__ : Any   =						True
										lowerCAmelCase__ : List[Any]   =						config.window_size**2
										lowerCAmelCase__ : Dict   =						model_class(a  )
										lowerCAmelCase__ : Any   =						model(**self._prepare_for_class(a       ,	a  )  )
										lowerCAmelCase__ : str   =						outputs.attentions
										self.assertEqual(len(a  )       ,	a  )

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

										# Check attention is always last and order is fine
										lowerCAmelCase__ : List[Any]   =						True
										lowerCAmelCase__ : Any   =						True
										lowerCAmelCase__ : Optional[int]   =						model_class(a  )
										lowerCAmelCase__ : Tuple   =						model(**self._prepare_for_class(a       ,	a  )  )

										if hasattr(self.model_tester       ,	'num_hidden_states_types'  ):
													lowerCAmelCase__ : str   =						self.model_tester.num_hidden_states_types
										else:
													# also another +1 for reshaped_hidden_states
													lowerCAmelCase__ : Dict   =						2
										self.assertEqual(out_len + added_hidden_states       ,	len(a  )  )

										lowerCAmelCase__ : List[str]   =						outputs.attentions

										self.assertEqual(len(a  )       ,	a  )

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






				def 				_lowerCamelCase						(		self   : str       ,	a   : List[Any]       ,	a   : str       ,	a   : str       ,	a   : int  ):






							'''simple docstring'''







							lowerCAmelCase__ : Union[str, Any]   =						model_class(a  )
							lowerCAmelCase__ : Tuple   =						model(**self._prepare_for_class(a       ,	a  )  )
							lowerCAmelCase__ : Dict   =						outputs.hidden_states

							lowerCAmelCase__ : int   =						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
							lowerCAmelCase__ : Dict   =						to_atuple(config.patch_size  )

							lowerCAmelCase__ : Dict   =						(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]       ,	)

							lowerCAmelCase__ : Tuple   =						outputs.reshaped_hidden_states
							self.assertEqual(len(a  )       ,	a  )

							lowerCAmelCase__ ,							lowerCAmelCase__ ,							lowerCAmelCase__ ,							lowerCAmelCase__ : List[Any]   =						reshaped_hidden_states[0].shape

							lowerCAmelCase__ : List[Any]   =						tf.reshape(reshaped_hidden_states[0]       ,	(batch_size, num_channels, height * width)  )
							lowerCAmelCase__ : List[str]   =						tf.transpose(a       ,	(0, 2, 1)  )

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






				def 				_lowerCamelCase						(		self   : str  ):






							'''simple docstring'''







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

							lowerCAmelCase__ : Union[str, Any]   =						to_atuple(self.model_tester.image_size  )

							for model_class in self.all_model_classes:
										lowerCAmelCase__ : 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"]
										lowerCAmelCase__ : str   =						True

										self.check_hidden_states_output(a       ,	a       ,	a       ,	a  )






				def 				_lowerCamelCase						(		self   : Optional[int]  ):






							'''simple docstring'''







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

							lowerCAmelCase__ : Union[str, Any]   =						to_atuple(self.model_tester.image_size  )
							lowerCAmelCase__ : Optional[Any]   =						to_atuple(config.patch_size  )

							lowerCAmelCase__ : Tuple   =						image_size[0] + patch_size[0] - (image_size[0] % patch_size[0])
							lowerCAmelCase__ : Optional[int]   =						image_size[1] + patch_size[1] - (image_size[1] % patch_size[1])

							for model_class in self.all_model_classes:
										lowerCAmelCase__ : 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"]
										lowerCAmelCase__ : List[str]   =						True
										self.check_hidden_states_output(a       ,	a       ,	a       ,	(padded_height, padded_width)  )






				@slow
				def 				_lowerCamelCase						(		self   : Tuple  ):






							'''simple docstring'''







							for model_name in TF_SWIN_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
										lowerCAmelCase__ : List[Any]   =						TFSwinModel.from_pretrained(a  )
										self.assertIsNotNone(a  )







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






				@cached_property
				def 				_lowerCamelCase						(		self   : Optional[int]  ):






							'''simple docstring'''







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






				@slow
				def 				_lowerCamelCase						(		self   : Tuple  ):






							'''simple docstring'''







							lowerCAmelCase__ : Dict   =						TFSwinForImageClassification.from_pretrained('microsoft/swin-tiny-patch4-window7-224'  )
							lowerCAmelCase__ : Dict   =						self.default_image_processor

							lowerCAmelCase__ : str   =						Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png'  )
							lowerCAmelCase__ : List[str]   =						image_processor(images=a       ,	return_tensors='tf'  )

							# forward pass
							lowerCAmelCase__ : Optional[int]   =						model(a  )

							# verify the logits
							lowerCAmelCase__ : Tuple   =						tf.TensorShape((1, 1_000)  )
							self.assertEqual(outputs.logits.shape       ,	a  )
							lowerCAmelCase__ : Dict   =						tf.constant([-0.0_9_4_8, -0.6_4_5_4, -0.0_9_2_1]  )
							self.assertTrue(np.allclose(outputs.logits[0, :3]       ,	a       ,	atol=1E-4  )  )

import math
from decimal import Decimal, getcontext
def 					lowerCAmelCase__     (    SCREAMING_SNAKE_CASE_ = 4_000_000	)							->						int:

			try:
						lowerCAmelCase__ : str   =						int(SCREAMING_SNAKE_CASE_	)
			except (TypeError, ValueError):
						raise TypeError('Parameter n must be int or castable to int.'	)
			if n <= 0:
						raise ValueError('Parameter n must be greater than or equal to one.'	)
			lowerCAmelCase__ : Union[str, Any]   =						100
			lowerCAmelCase__ : List[Any]   =						(Decimal(5	) ** Decimal(0.5	) + 1) / Decimal(2	)

			lowerCAmelCase__ : Tuple   =						(math.floor(math.log(n * (phi + 2)			,				SCREAMING_SNAKE_CASE_	) - 1	) // 3) * 3 + 2
			lowerCAmelCase__ : Optional[int]   =						Decimal(round(phi ** Decimal(index + 1	)	)	) / (phi + 2)
			lowerCAmelCase__ : Tuple   =						num // 2
			return int(SCREAMING_SNAKE_CASE_	)


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