
from ...utils import is_transformers_available
from .pipeline_latent_diffusion_superresolution import LDMSuperResolutionPipeline


if is_transformers_available():
      from .pipeline_latent_diffusion import LDMBertModel, LDMTextToImagePipeline



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

			"""simple docstring"""

			def __init__(				self  :       Any    ,	lowercase_  :       Union[str, Any]    ,	lowercase_  :       Optional[Any]=13    ,	lowercase_  :       List[str]=32    ,	lowercase_  :       int=2    ,	lowercase_  :       List[Any]=3    ,	lowercase_  :       List[Any]=16    ,	lowercase_  :       Union[str, Any]=[1, 2, 1]    ,	lowercase_  :       Optional[Any]=[2, 2, 4]    ,	lowercase_  :       List[str]=2    ,	lowercase_  :       List[Any]=2.0    ,	lowercase_  :       Optional[Any]=True    ,	lowercase_  :       Any=0.0    ,	lowercase_  :       Optional[int]=0.0    ,	lowercase_  :       str=0.1    ,	lowercase_  :       List[str]="gelu"    ,	lowercase_  :       int=False    ,	lowercase_  :       List[Any]=True    ,	lowercase_  :       Union[str, Any]=0.0_2    ,	lowercase_  :       Union[str, Any]=1e-5    ,	lowercase_  :       Dict=True    ,	lowercase_  :       int=None    ,	lowercase_  :       Dict=True    ,	lowercase_  :       Union[str, Any]=10    ,	lowercase_  :       int=8    ,	):
								UpperCamelCase__		:     Optional[Any]								=parent
								UpperCamelCase__		:     Any								=batch_size
								UpperCamelCase__		:     Tuple								=image_size
								UpperCamelCase__		:     str								=patch_size
								UpperCamelCase__		:     Tuple								=num_channels
								UpperCamelCase__		:     List[Any]								=embed_dim
								UpperCamelCase__		:     Optional[int]								=depths
								UpperCamelCase__		:     Optional[int]								=num_heads
								UpperCamelCase__		:     Dict								=window_size
								UpperCamelCase__		:     Dict								=mlp_ratio
								UpperCamelCase__		:     Optional[Any]								=qkv_bias
								UpperCamelCase__		:     int								=hidden_dropout_prob
								UpperCamelCase__		:     Optional[Any]								=attention_probs_dropout_prob
								UpperCamelCase__		:     Tuple								=drop_path_rate
								UpperCamelCase__		:     Dict								=hidden_act
								UpperCamelCase__		:     Union[str, Any]								=use_absolute_embeddings
								UpperCamelCase__		:     str								=patch_norm
								UpperCamelCase__		:     Optional[int]								=layer_norm_eps
								UpperCamelCase__		:     Optional[int]								=initializer_range
								UpperCamelCase__		:     List[Any]								=is_training
								UpperCamelCase__		:     Optional[int]								=scope
								UpperCamelCase__		:     Any								=use_labels
								UpperCamelCase__		:     Optional[Any]								=type_sequence_label_size
								UpperCamelCase__		:     Optional[int]								=encoder_stride






			def  _lowerCAmelCase      (				self  :       int    ):
								UpperCamelCase__		:     List[Any]								=floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]    )

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

								UpperCamelCase__		:     List[Any]								=self.get_config()

								return config, pixel_values, labels






			def  _lowerCAmelCase      (				self  :       Dict    ):
								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[str]    ,	lowercase_  :       str    ,	lowercase_  :       str    ,	lowercase_  :       Optional[Any]    ):
								UpperCamelCase__		:     Union[str, Any]								=TFSwinModel(config=lowercase_    )
								UpperCamelCase__		:     List[Any]								=model(lowercase_    )

								UpperCamelCase__		:     Optional[int]								=((config.image_size // config.patch_size) ** 2) // (4 ** (len(config.depths    ) - 1))
								UpperCamelCase__		:     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  :       Any    ,	lowercase_  :       int    ,	lowercase_  :       str    ,	lowercase_  :       Tuple    ):
								UpperCamelCase__		:     str								=TFSwinForMaskedImageModeling(config=lowercase_    )
								UpperCamelCase__		:     List[Any]								=model(lowercase_    )
								self.parent.assertEqual(
								    result.logits.shape    ,	(self.batch_size, self.num_channels, self.image_size, self.image_size)    )

								# test greyscale images
								UpperCamelCase__		:     Optional[Any]								=1
								UpperCamelCase__		:     Dict								=TFSwinForMaskedImageModeling(lowercase_    )

								UpperCamelCase__		:     Tuple								=floats_tensor([self.batch_size, 1, self.image_size, self.image_size]    )
								UpperCamelCase__		:     Any								=model(lowercase_    )
								self.parent.assertEqual(result.logits.shape    ,	(self.batch_size, 1, self.image_size, self.image_size)    )






			def  _lowerCAmelCase      (				self  :       Optional[Any]    ,	lowercase_  :       List[Any]    ,	lowercase_  :       List[Any]    ,	lowercase_  :       int    ):
								UpperCamelCase__		:     Any								=self.type_sequence_label_size
								UpperCamelCase__		:     Union[str, Any]								=TFSwinForImageClassification(lowercase_    )
								UpperCamelCase__		:     Optional[int]								=model(lowercase_    ,	labels=lowercase_    )
								self.parent.assertEqual(result.logits.shape    ,	(self.batch_size, self.type_sequence_label_size)    )

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






			def  _lowerCAmelCase      (				self  :       Tuple    ):
								UpperCamelCase__		:     int								=self.prepare_config_and_inputs()
								UpperCamelCase__	,      UpperCamelCase__	,      UpperCamelCase__		:     List[Any]								=config_and_inputs
								UpperCamelCase__		:     Tuple								={'''pixel_values''': pixel_values}
								return config, inputs_dict



@require_tf
class      __a (   snake_case__,     snake_case__,     unittest.TestCase     ):

			"""simple docstring"""

			SCREAMING_SNAKE_CASE_      =    (
			    (
			        TFSwinModel,
			        TFSwinForImageClassification,
			        TFSwinForMaskedImageModeling,
			    )
			    if is_tf_available()
			    else ()
			)
			SCREAMING_SNAKE_CASE_      =    (
			    {'feature-extraction': TFSwinModel, 'image-classification': TFSwinForImageClassification}
			    if is_tf_available()
			    else {}
			)

			SCREAMING_SNAKE_CASE_      =    False
			SCREAMING_SNAKE_CASE_      =    False
			SCREAMING_SNAKE_CASE_      =    False
			SCREAMING_SNAKE_CASE_      =    False






			def  _lowerCAmelCase      (				self  :       Dict    ):
								UpperCamelCase__		:     int								=TFSwinModelTester(self    )
								UpperCamelCase__		:     Optional[Any]								=ConfigTester(self    ,	config_class=lowercase_    ,	embed_dim=37    )






			def  _lowerCAmelCase      (				self  :       Dict    ):
								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  :       str    ):
								return






			def  _lowerCAmelCase      (				self  :       Any    ):
								UpperCamelCase__		:     Tuple								=self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_model(*lowercase_    )






			def  _lowerCAmelCase      (				self  :       List[str]    ):
								UpperCamelCase__		:     List[Any]								=self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_for_masked_image_modeling(*lowercase_    )






			def  _lowerCAmelCase      (				self  :       List[str]    ):
								UpperCamelCase__		:     Tuple								=self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_for_image_classification(*lowercase_    )






			@unittest.skip(reason='''Swin does not use inputs_embeds'''    )
			def  _lowerCAmelCase      (				self  :       int    ):
								pass






			def  _lowerCAmelCase      (				self  :       List[Any]    ):
								UpperCamelCase__	,      UpperCamelCase__		:     Tuple								=self.model_tester.prepare_config_and_inputs_for_common()

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






			def  _lowerCAmelCase      (				self  :       int    ):
								UpperCamelCase__	,      UpperCamelCase__		:     Optional[Any]								=self.model_tester.prepare_config_and_inputs_for_common()

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

													UpperCamelCase__		:     Optional[Any]								=['''pixel_values''']
													self.assertListEqual(arg_names[:1]    ,	lowercase_    )






			def  _lowerCAmelCase      (				self  :       Optional[Any]    ):
								UpperCamelCase__	,      UpperCamelCase__		:     int								=self.model_tester.prepare_config_and_inputs_for_common()
								UpperCamelCase__		:     Dict								=True

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

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

													self.assertListEqual(
													    list(attentions[0].shape[-3:]    )    ,	[self.model_tester.num_heads[0], window_size_squared, window_size_squared]    ,	)
													UpperCamelCase__		:     Any								=len(lowercase_    )

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

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

													UpperCamelCase__		:     Any								=outputs.attentions

													self.assertEqual(len(lowercase_    )    ,	lowercase_    )

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






			def  _lowerCAmelCase      (				self  :       List[str]    ,	lowercase_  :       Union[str, Any]    ,	lowercase_  :       Dict    ,	lowercase_  :       str    ,	lowercase_  :       List[str]    ):
								UpperCamelCase__		:     Any								=model_class(lowercase_    )
								UpperCamelCase__		:     Union[str, Any]								=model(**self._prepare_for_class(lowercase_    ,	lowercase_    )    )
								UpperCamelCase__		:     str								=outputs.hidden_states

								UpperCamelCase__		:     Dict								=getattr(
								    self.model_tester    ,	'''expected_num_hidden_layers'''    ,	len(self.model_tester.depths    ) + 1    )
								self.assertEqual(len(lowercase_    )    ,	lowercase_    )

								# Swin has a different seq_length
								UpperCamelCase__		:     List[Any]								=to_atuple(config.patch_size    )

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

								UpperCamelCase__		:     Optional[Any]								=outputs.reshaped_hidden_states
								self.assertEqual(len(lowercase_    )    ,	lowercase_    )

								UpperCamelCase__	,      UpperCamelCase__	,      UpperCamelCase__	,      UpperCamelCase__		:     int								=reshaped_hidden_states[0].shape

								UpperCamelCase__		:     Optional[int]								=tf.reshape(reshaped_hidden_states[0]    ,	(batch_size, num_channels, height * width)    )
								UpperCamelCase__		:     Optional[int]								=tf.transpose(lowercase_    ,	(0, 2, 1)    )

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






			def  _lowerCAmelCase      (				self  :       List[str]    ):
								UpperCamelCase__	,      UpperCamelCase__		:     Dict								=self.model_tester.prepare_config_and_inputs_for_common()

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

								for model_class in self.all_model_classes:
													UpperCamelCase__		:     Union[str, Any]								=True
													self.check_hidden_states_output(lowercase_    ,	lowercase_    ,	lowercase_    ,	lowercase_    )

													# check that output_hidden_states also work using config
													del inputs_dict["output_hidden_states"]
													UpperCamelCase__		:     List[str]								=True

													self.check_hidden_states_output(lowercase_    ,	lowercase_    ,	lowercase_    ,	lowercase_    )






			def  _lowerCAmelCase      (				self  :       List[Any]    ):
								UpperCamelCase__	,      UpperCamelCase__		:     Optional[Any]								=self.model_tester.prepare_config_and_inputs_for_common()
								UpperCamelCase__		:     List[str]								=3

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

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

								for model_class in self.all_model_classes:
													UpperCamelCase__		:     List[str]								=True
													self.check_hidden_states_output(lowercase_    ,	lowercase_    ,	lowercase_    ,	(padded_height, padded_width)    )

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






			@slow
			def  _lowerCAmelCase      (				self  :       List[Any]    ):
								for model_name in TF_SWIN_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
													UpperCamelCase__		:     str								=TFSwinModel.from_pretrained(lowercase_    )
													self.assertIsNotNone(lowercase_    )



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

			"""simple docstring"""

			@cached_property
			def  _lowerCAmelCase      (				self  :       Optional[int]    ):
								return (
								    AutoImageProcessor.from_pretrained('''microsoft/swin-tiny-patch4-window7-224'''    )
								    if is_vision_available()
								    else None
								)






			@slow
			def  _lowerCAmelCase      (				self  :       Any    ):
								UpperCamelCase__		:     List[str]								=TFSwinForImageClassification.from_pretrained('''microsoft/swin-tiny-patch4-window7-224'''    )
								UpperCamelCase__		:     Optional[Any]								=self.default_image_processor

								UpperCamelCase__		:     str								=Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''    )
								UpperCamelCase__		:     int								=image_processor(images=lowercase_    ,	return_tensors='''tf'''    )

								# forward pass
								UpperCamelCase__		:     str								=model(lowercase_    )

								# verify the logits
								UpperCamelCase__		:     Optional[int]								=tf.TensorShape((1, 1000)    )
								self.assertEqual(outputs.logits.shape    ,	lowercase_    )
								UpperCamelCase__		:     Tuple								=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]    ,	lowercase_    ,	atol=1e-4    )    )


