

"""simple docstring"""


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

				def __init__(			self							:       Dict  ,						UpperCamelCase__							:       str  ,						UpperCamelCase__							:       Union[str, Any]=1_3  ,						UpperCamelCase__							:       Optional[int]=3_2  ,						UpperCamelCase__							:       str=2  ,						UpperCamelCase__							:       List[str]=3  ,						UpperCamelCase__							:       List[str]=1_6  ,						UpperCamelCase__							:       Tuple=[1, 2, 1]  ,						UpperCamelCase__							:       int=[2, 2, 4]  ,						UpperCamelCase__							:       Optional[Any]=2  ,						UpperCamelCase__							:       Union[str, Any]=2.0  ,						UpperCamelCase__							:       List[Any]=True  ,						UpperCamelCase__							:       Optional[int]=0.0  ,						UpperCamelCase__							:       int=0.0  ,						UpperCamelCase__							:       Optional[Any]=0.1  ,						UpperCamelCase__							:       int="gelu"  ,						UpperCamelCase__							:       int=False  ,						UpperCamelCase__							:       Tuple=True  ,						UpperCamelCase__							:       int=0.02  ,						UpperCamelCase__							:       Union[str, Any]=1e-5  ,						UpperCamelCase__							:       str=True  ,						UpperCamelCase__							:       List[str]=None  ,						UpperCamelCase__							:       Dict=True  ,						UpperCamelCase__							:       int=1_0  ,						UpperCamelCase__							:       Union[str, Any]=8  ,						)->     None:



						'''simple docstring'''


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

				def 							lowercase_  (			self							:       int)->     Tuple:



						'''simple docstring'''


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

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

						__lowerCAmelCase: Union[str, Any]					   =    self.get_config()

						return config, pixel_values, labels

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



						'''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 							lowercase_  (			self							:       Optional[Any]  ,						UpperCamelCase__							:       Union[str, Any]  ,						UpperCamelCase__							:       Optional[int]  ,						UpperCamelCase__							:       List[str])->     Optional[Any]:



						'''simple docstring'''


						__lowerCAmelCase: Union[str, Any]					   =    TFSwinModel(config=UpperCamelCase__)
						__lowerCAmelCase: Optional[int]					   =    model(UpperCamelCase__)

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



						'''simple docstring'''


						__lowerCAmelCase: Union[str, Any]					   =    TFSwinForMaskedImageModeling(config=UpperCamelCase__)
						__lowerCAmelCase: str					   =    model(UpperCamelCase__)
						self.parent.assertEqual(
						    result.logits.shape  ,						(self.batch_size, self.num_channels, self.image_size, self.image_size))

						# test greyscale images
						__lowerCAmelCase: Optional[Any]					   =    1
						__lowerCAmelCase: Optional[int]					   =    TFSwinForMaskedImageModeling(UpperCamelCase__)

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

				def 							lowercase_  (			self							:       int  ,						UpperCamelCase__							:       List[str]  ,						UpperCamelCase__							:       Tuple  ,						UpperCamelCase__							:       List[Any])->     Dict:



						'''simple docstring'''


						__lowerCAmelCase: Any					   =    self.type_sequence_label_size
						__lowerCAmelCase: Union[str, Any]					   =    TFSwinForImageClassification(UpperCamelCase__)
						__lowerCAmelCase: Any					   =    model(UpperCamelCase__  ,						labels=UpperCamelCase__)
						self.parent.assertEqual(result.logits.shape  ,						(self.batch_size, self.type_sequence_label_size))

						# test greyscale images
						__lowerCAmelCase: str					   =    1
						__lowerCAmelCase: Optional[int]					   =    TFSwinForImageClassification(UpperCamelCase__)
						__lowerCAmelCase: int					   =    floats_tensor([self.batch_size, 1, self.image_size, self.image_size])
						__lowerCAmelCase: List[str]					   =    model(UpperCamelCase__)
						self.parent.assertEqual(result.logits.shape  ,						(self.batch_size, self.type_sequence_label_size))




				def 							lowercase_  (			self							:       Tuple)->     Tuple:



						'''simple docstring'''


						__lowerCAmelCase: List[str]					   =    self.prepare_config_and_inputs()
						__lowerCAmelCase   ,				__lowerCAmelCase   ,				__lowerCAmelCase: Optional[Any]					   =    config_and_inputs
						__lowerCAmelCase: Tuple					   =    {"pixel_values": pixel_values}
						return config, inputs_dict





@require_tf
class 					snake_case			(       __snake_case,							__snake_case,							unittest.TestCase						):
				SCREAMING_SNAKE_CASE_     :   Tuple        =						(
				    (
				        TFSwinModel,
				        TFSwinForImageClassification,
				        TFSwinForMaskedImageModeling,
				    )
				    if is_tf_available()
				    else ()
				)
				SCREAMING_SNAKE_CASE_     :   str        =						(
				    {"""feature-extraction""": TFSwinModel, """image-classification""": TFSwinForImageClassification}
				    if is_tf_available()
				    else {}
				)

				SCREAMING_SNAKE_CASE_     :   str        =						False
				SCREAMING_SNAKE_CASE_     :   Optional[Any]        =						False
				SCREAMING_SNAKE_CASE_     :   str        =						False
				SCREAMING_SNAKE_CASE_     :   Any        =						False

				def 							lowercase_  (			self							:       List[Any])->     List[Any]:



						'''simple docstring'''


						__lowerCAmelCase: Any					   =    TFSwinModelTester(self)
						__lowerCAmelCase: List[str]					   =    ConfigTester(self  ,						config_class=UpperCamelCase__  ,						embed_dim=3_7)

				def 							lowercase_  (			self							:       Optional[int])->     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])->     int:



						'''simple docstring'''


						return

				def 							lowercase_  (			self							:       str)->     Optional[int]:



						'''simple docstring'''


						__lowerCAmelCase: Optional[int]					   =    self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_model(*UpperCamelCase__)

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



						'''simple docstring'''


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

				def 							lowercase_  (			self							:       Tuple)->     Tuple:



						'''simple docstring'''


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



						'''simple docstring'''


						pass

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



						'''simple docstring'''


						__lowerCAmelCase   ,				__lowerCAmelCase: List[str]					   =    self.model_tester.prepare_config_and_inputs_for_common()

						for model_class in self.all_model_classes:
								__lowerCAmelCase: Tuple					   =    model_class(UpperCamelCase__)
								self.assertIsInstance(model.get_input_embeddings()  ,						tf.keras.layers.Layer)
								__lowerCAmelCase: Union[str, Any]					   =    model.get_output_embeddings()
								self.assertTrue(x is None or isinstance(UpperCamelCase__  ,						tf.keras.layers.Dense))

				def 							lowercase_  (			self							:       str)->     str:



						'''simple docstring'''


						__lowerCAmelCase   ,				__lowerCAmelCase: List[str]					   =    self.model_tester.prepare_config_and_inputs_for_common()

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

								__lowerCAmelCase: Tuple					   =    ["pixel_values"]
								self.assertListEqual(arg_names[:1]  ,						UpperCamelCase__)

				def 							lowercase_  (			self							:       List[str])->     str:



						'''simple docstring'''


						__lowerCAmelCase   ,				__lowerCAmelCase: int					   =    self.model_tester.prepare_config_and_inputs_for_common()
						__lowerCAmelCase: str					   =    True

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

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

								# Check attention is always last and order is fine
								__lowerCAmelCase: Union[str, Any]					   =    True
								__lowerCAmelCase: Dict					   =    True
								__lowerCAmelCase: int					   =    model_class(UpperCamelCase__)
								__lowerCAmelCase: Tuple					   =    model(**self._prepare_for_class(UpperCamelCase__  ,						UpperCamelCase__))

								if hasattr(self.model_tester  ,						"num_hidden_states_types"):
										__lowerCAmelCase: Union[str, Any]					   =    self.model_tester.num_hidden_states_types
								else:
										# also another +1 for reshaped_hidden_states
										__lowerCAmelCase: int					   =    2
								self.assertEqual(out_len + added_hidden_states  ,						len(UpperCamelCase__))

								__lowerCAmelCase: 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 							lowercase_  (			self							:       Optional[Any]  ,						UpperCamelCase__							:       Union[str, Any]  ,						UpperCamelCase__							:       Union[str, Any]  ,						UpperCamelCase__							:       Union[str, Any]  ,						UpperCamelCase__							:       Optional[Any])->     Union[str, Any]:



						'''simple docstring'''


						__lowerCAmelCase: Dict					   =    model_class(UpperCamelCase__)
						__lowerCAmelCase: str					   =    model(**self._prepare_for_class(UpperCamelCase__  ,						UpperCamelCase__))
						__lowerCAmelCase: int					   =    outputs.hidden_states

						__lowerCAmelCase: Union[str, 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
						__lowerCAmelCase: Dict					   =    to_atuple(config.patch_size)

						__lowerCAmelCase: List[str]					   =    (image_size[1] // patch_size[1]) * (image_size[0] // patch_size[0])

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

						__lowerCAmelCase: Union[str, Any]					   =    outputs.reshaped_hidden_states
						self.assertEqual(len(UpperCamelCase__)  ,						UpperCamelCase__)

						__lowerCAmelCase   ,				__lowerCAmelCase   ,				__lowerCAmelCase   ,				__lowerCAmelCase: Optional[Any]					   =    reshaped_hidden_states[0].shape

						__lowerCAmelCase: Tuple					   =    tf.reshape(reshaped_hidden_states[0]  ,						(batch_size, num_channels, height * width))
						__lowerCAmelCase: Optional[Any]					   =    tf.transpose(UpperCamelCase__  ,						(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'''


						__lowerCAmelCase   ,				__lowerCAmelCase: List[str]					   =    self.model_tester.prepare_config_and_inputs_for_common()

						__lowerCAmelCase: Optional[int]					   =    to_atuple(self.model_tester.image_size)

						for model_class in self.all_model_classes:
								__lowerCAmelCase: Union[str, Any]					   =    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"]
								__lowerCAmelCase: Any					   =    True

								self.check_hidden_states_output(UpperCamelCase__  ,						UpperCamelCase__  ,						UpperCamelCase__  ,						UpperCamelCase__)

				def 							lowercase_  (			self							:       str)->     Dict:



						'''simple docstring'''


						__lowerCAmelCase   ,				__lowerCAmelCase: List[str]					   =    self.model_tester.prepare_config_and_inputs_for_common()
						__lowerCAmelCase: int					   =    3

						__lowerCAmelCase: Optional[int]					   =    to_atuple(self.model_tester.image_size)
						__lowerCAmelCase: int					   =    to_atuple(config.patch_size)

						__lowerCAmelCase: List[Any]					   =    image_size[0] + patch_size[0] - (image_size[0] % patch_size[0])
						__lowerCAmelCase: Optional[Any]					   =    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(UpperCamelCase__  ,						UpperCamelCase__  ,						UpperCamelCase__  ,						(padded_height, padded_width))

								# check that output_hidden_states also work using config
								del inputs_dict["output_hidden_states"]
								__lowerCAmelCase: Tuple					   =    True
								self.check_hidden_states_output(UpperCamelCase__  ,						UpperCamelCase__  ,						UpperCamelCase__  ,						(padded_height, padded_width))




				@slow
				def 							lowercase_  (			self							:       Dict)->     Union[str, Any]:



						'''simple docstring'''


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





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

				@cached_property
				def 							lowercase_  (			self							:       int)->     str:



						'''simple docstring'''


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




				@slow
				def 							lowercase_  (			self							:       Dict)->     Optional[int]:



						'''simple docstring'''


						__lowerCAmelCase: Dict					   =    TFSwinForImageClassification.from_pretrained("microsoft/swin-tiny-patch4-window7-224")
						__lowerCAmelCase: Optional[Any]					   =    self.default_image_processor

						__lowerCAmelCase: Tuple					   =    Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png")
						__lowerCAmelCase: Dict					   =    image_processor(images=UpperCamelCase__  ,						return_tensors="tf")

						# forward pass
						__lowerCAmelCase: Dict					   =    model(UpperCamelCase__)

						# verify the logits
						__lowerCAmelCase: List[Any]					   =    tf.TensorShape((1, 1_0_0_0))
						self.assertEqual(outputs.logits.shape  ,						UpperCamelCase__)
						__lowerCAmelCase: Optional[int]					   =    tf.constant([-0.0948, -0.6454, -0.0921])
						self.assertTrue(np.allclose(outputs.logits[0, :3]  ,						UpperCamelCase__  ,						atol=1e-4))





"""simple docstring"""


import math






def 					a__					(					__SCREAMING_SNAKE_CASE				)			->			list:
		if n <= 0:
				raise ValueError("Only positive integers have prime factors"				)
		__lowerCAmelCase: Tuple					   =    []
		while n % 2 == 0:
				pf.append(2				)
				__lowerCAmelCase: str					   =    int(n / 2				)
		for i in range(3      ,				int(math.sqrt(__SCREAMING_SNAKE_CASE				)				) + 1      ,				2				):
				while n % i == 0:
						pf.append(__SCREAMING_SNAKE_CASE				)
						__lowerCAmelCase: List[Any]					   =    int(n / i				)
		if n > 2:
				pf.append(__SCREAMING_SNAKE_CASE				)
		return pf






def 					a__					(					__SCREAMING_SNAKE_CASE				)			->			int:
		if n <= 0:
				raise ValueError("Only positive numbers are accepted"				)
		__lowerCAmelCase: int					   =    1
		__lowerCAmelCase: str					   =    1
		while n % 2 == 0:
				temp += 1
				__lowerCAmelCase: Dict					   =    int(n / 2				)
		div *= temp
		for i in range(3      ,				int(math.sqrt(__SCREAMING_SNAKE_CASE				)				) + 1      ,				2				):
				__lowerCAmelCase: int					   =    1
				while n % i == 0:
						temp += 1
						__lowerCAmelCase: Tuple					   =    int(n / i				)
				div *= temp
		if n > 1:
				div *= 2
		return div






def 					a__					(					__SCREAMING_SNAKE_CASE				)			->			int:
		if n <= 0:
				raise ValueError("Only positive numbers are accepted"				)
		__lowerCAmelCase: Dict					   =    1
		__lowerCAmelCase: int					   =    1
		while n % 2 == 0:
				temp += 1
				__lowerCAmelCase: Tuple					   =    int(n / 2				)
		if temp > 1:
				s *= (2**temp - 1) / (2 - 1)
		for i in range(3      ,				int(math.sqrt(__SCREAMING_SNAKE_CASE				)				) + 1      ,				2				):
				__lowerCAmelCase: List[Any]					   =    1
				while n % i == 0:
						temp += 1
						__lowerCAmelCase: List[str]					   =    int(n / i				)
				if temp > 1:
						s *= (i**temp - 1) / (i - 1)
		return int(__SCREAMING_SNAKE_CASE				)






def 					a__					(					__SCREAMING_SNAKE_CASE				)			->			int:
		__lowerCAmelCase: Optional[int]					   =    n
		for x in set(prime_factors(__SCREAMING_SNAKE_CASE				)				):
				s *= (x - 1) / x
		return int(__SCREAMING_SNAKE_CASE				)


if __name__ == "__main__":
			import doctest

			doctest.testmod()


