code: 
  
  
  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				,    UpperCamelCase_				,    UpperCamelCase_=13				,    UpperCamelCase_=32				,    UpperCamelCase_=2				,    UpperCamelCase_=3				,    UpperCamelCase_=16				,    UpperCamelCase_=[1, 2, 1]				,    UpperCamelCase_=[2, 2, 4]				,    UpperCamelCase_=2				,    UpperCamelCase_=2.0				,    UpperCamelCase_=True				,    UpperCamelCase_=0.0				,    UpperCamelCase_=0.0				,    UpperCamelCase_=0.1				,    UpperCamelCase_="gelu"				,    UpperCamelCase_=False				,    UpperCamelCase_=True				,    UpperCamelCase_=0.02				,    UpperCamelCase_=1e-5				,    UpperCamelCase_=True				,    UpperCamelCase_=None				,    UpperCamelCase_=True				,    UpperCamelCase_=10				,    UpperCamelCase_=8				,    )		->			None:
  								"""simple docstring"""
  
  
  
  
  
  
  								a_       :   Optional[Any]			=	parent
  								a_       :   List[Any]			=	batch_size
  								a_       :   Optional[Any]			=	image_size
  								a_       :   Dict			=	patch_size
  								a_       :   Optional[Any]			=	num_channels
  								a_       :   Union[str, Any]			=	embed_dim
  								a_       :   Dict			=	depths
  								a_       :   Optional[Any]			=	num_heads
  								a_       :   Tuple			=	window_size
  								a_       :   List[str]			=	mlp_ratio
  								a_       :   Optional[int]			=	qkv_bias
  								a_       :   Optional[int]			=	hidden_dropout_prob
  								a_       :   List[str]			=	attention_probs_dropout_prob
  								a_       :   Any			=	drop_path_rate
  								a_       :   Union[str, Any]			=	hidden_act
  								a_       :   Tuple			=	use_absolute_embeddings
  								a_       :   Tuple			=	patch_norm
  								a_       :   List[str]			=	layer_norm_eps
  								a_       :   Optional[Any]			=	initializer_range
  								a_       :   Optional[Any]			=	is_training
  								a_       :   str			=	scope
  								a_       :   Optional[Any]			=	use_labels
  								a_       :   Optional[int]			=	type_sequence_label_size
  								a_       :   List[str]			=	encoder_stride
  
  
  
  
  
  	def     A				(  self							)		->			List[str]:
  								"""simple docstring"""
  
  
  
  
  
  
  								a_       :   List[Any]			=	floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]							)
  
  								a_       :   Dict			=	None
  								if self.use_labels:
  															a_       :   List[str]			=	ids_tensor([self.batch_size]				,    self.type_sequence_label_size							)
  
  								a_       :   Tuple			=	self.get_config()
  
  								return config, pixel_values, labels
  
  
  
  
  
  	def     A				(  self							)		->			int:
  								"""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     A				(  self				,    UpperCamelCase_				,    UpperCamelCase_				,    UpperCamelCase_							)		->			Union[str, Any]:
  								"""simple docstring"""
  
  
  
  
  
  
  								a_       :   Dict			=	TFSwinModel(config=UpperCamelCase_							)
  								a_       :   Optional[Any]			=	model(UpperCamelCase_							)
  
  								a_       :   List[Any]			=	((config.image_size // config.patch_size) ** 2) // (4 ** (len(config.depths							) - 1))
  								a_       :   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     A				(  self				,    UpperCamelCase_				,    UpperCamelCase_				,    UpperCamelCase_							)		->			List[str]:
  								"""simple docstring"""
  
  
  
  
  
  
  								a_       :   List[Any]			=	TFSwinForMaskedImageModeling(config=UpperCamelCase_							)
  								a_       :   Tuple			=	model(UpperCamelCase_							)
  								self.parent.assertEqual(
  								    result.logits.shape				,    (self.batch_size, self.num_channels, self.image_size, self.image_size)							)
  
  								# test greyscale images
  								a_       :   List[str]			=	1
  								a_       :   List[str]			=	TFSwinForMaskedImageModeling(UpperCamelCase_							)
  
  								a_       :   List[str]			=	floats_tensor([self.batch_size, 1, self.image_size, self.image_size]							)
  								a_       :   Optional[int]			=	model(UpperCamelCase_							)
  								self.parent.assertEqual(result.logits.shape				,    (self.batch_size, 1, self.image_size, self.image_size)							)
  
  
  
  
  
  	def     A				(  self				,    UpperCamelCase_				,    UpperCamelCase_				,    UpperCamelCase_							)		->			Dict:
  								"""simple docstring"""
  
  
  
  
  
  
  								a_       :   Optional[int]			=	self.type_sequence_label_size
  								a_       :   List[str]			=	TFSwinForImageClassification(UpperCamelCase_							)
  								a_       :   Union[str, Any]			=	model(UpperCamelCase_				,    labels=UpperCamelCase_							)
  								self.parent.assertEqual(result.logits.shape				,    (self.batch_size, self.type_sequence_label_size)							)
  
  								# test greyscale images
  								a_       :   Optional[Any]			=	1
  								a_       :   Tuple			=	TFSwinForImageClassification(UpperCamelCase_							)
  								a_       :   List[Any]			=	floats_tensor([self.batch_size, 1, self.image_size, self.image_size]							)
  								a_       :   int			=	model(UpperCamelCase_							)
  								self.parent.assertEqual(result.logits.shape				,    (self.batch_size, self.type_sequence_label_size)							)
  
  
  
  
  
  	def     A				(  self							)		->			Dict:
  								"""simple docstring"""
  
  
  
  
  
  
  								a_       :   Dict			=	self.prepare_config_and_inputs()
  								a_       :   Optional[int]			=	config_and_inputs
  								a_       :   Tuple			=	{"""pixel_values""": pixel_values}
  								return config, inputs_dict
  
  
  
  
  
  @require_tf
  class 					snake_case__    ( __A      ,    __A      ,    unittest.TestCase       ):
  	UpperCAmelCase   :							int         = (
  	    (
  	        TFSwinModel,
  	        TFSwinForImageClassification,
  	        TFSwinForMaskedImageModeling,
  	    )
  	    if is_tf_available()
  	    else ()
  	)
  	UpperCAmelCase   :							List[Any]         = (
  	    {"""feature-extraction""": TFSwinModel, """image-classification""": TFSwinForImageClassification}
  	    if is_tf_available()
  	    else {}
  	)
  
  	UpperCAmelCase   :							Optional[int]         = False
  	UpperCAmelCase   :							Dict         = False
  	UpperCAmelCase   :							List[str]         = False
  	UpperCAmelCase   :							List[str]         = False
  
  
  
  
  
  	def     A				(  self							)		->			List[Any]:
  								"""simple docstring"""
  
  
  
  
  
  
  								a_       :   str			=	TFSwinModelTester(self							)
  								a_       :   Dict			=	ConfigTester(self				,    config_class=UpperCamelCase_				,    embed_dim=37							)
  
  
  
  
  
  	def     A				(  self							)		->			Dict:
  								"""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     A				(  self							)		->			Dict:
  								"""simple docstring"""
  
  
  
  
  
  
  								return
  
  
  
  
  
  	def     A				(  self							)		->			int:
  								"""simple docstring"""
  
  
  
  
  
  
  								a_       :   Tuple			=	self.model_tester.prepare_config_and_inputs()
  								self.model_tester.create_and_check_model(*UpperCamelCase_							)
  
  
  
  
  
  	def     A				(  self							)		->			Optional[Any]:
  								"""simple docstring"""
  
  
  
  
  
  
  								a_       :   Optional[Any]			=	self.model_tester.prepare_config_and_inputs()
  								self.model_tester.create_and_check_for_masked_image_modeling(*UpperCamelCase_							)
  
  
  
  
  
  	def     A				(  self							)		->			Any:
  								"""simple docstring"""
  
  
  
  
  
  
  								a_       :   Optional[Any]			=	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     A				(  self							)		->			Optional[Any]:
  								"""simple docstring"""
  
  
  
  
  
  
  								pass
  
  
  
  
  
  	def     A				(  self							)		->			List[Any]:
  								"""simple docstring"""
  
  
  
  
  
  
  								a_       :   Optional[int]			=	self.model_tester.prepare_config_and_inputs_for_common()
  
  								for model_class in self.all_model_classes:
  															a_       :   Dict			=	model_class(UpperCamelCase_							)
  															self.assertIsInstance(model.get_input_embeddings()				,    tf.keras.layers.Layer							)
  															a_       :   Any			=	model.get_output_embeddings()
  															self.assertTrue(x is None or isinstance(UpperCamelCase_				,    tf.keras.layers.Dense							)							)
  
  
  
  
  
  	def     A				(  self							)		->			List[Any]:
  								"""simple docstring"""
  
  
  
  
  
  
  								a_       :   int			=	self.model_tester.prepare_config_and_inputs_for_common()
  
  								for model_class in self.all_model_classes:
  															a_       :   Union[str, Any]			=	model_class(UpperCamelCase_							)
  															a_       :   List[Any]			=	inspect.signature(model.call							)
  															# signature.parameters is an OrderedDict => so arg_names order is deterministic
  															a_       :   Tuple			=	[*signature.parameters.keys()]
  
  															a_       :   List[str]			=	["""pixel_values"""]
  															self.assertListEqual(arg_names[:1]				,    UpperCamelCase_							)
  
  
  
  
  
  	def     A				(  self							)		->			Tuple:
  								"""simple docstring"""
  
  
  
  
  
  
  								a_       :   int			=	self.model_tester.prepare_config_and_inputs_for_common()
  								a_       :   int			=	True
  
  								for model_class in self.all_model_classes:
  															a_       :   Union[str, Any]			=	True
  															a_       :   Dict			=	False
  															a_       :   List[Any]			=	True
  															a_       :   str			=	model_class(UpperCamelCase_							)
  															a_       :   Union[str, Any]			=	model(**self._prepare_for_class(UpperCamelCase_				,    UpperCamelCase_							)							)
  															a_       :   List[str]			=	outputs.attentions
  															a_       :   Dict			=	len(self.model_tester.depths							)
  															self.assertEqual(len(UpperCamelCase_							)				,    UpperCamelCase_							)
  
  															# check that output_attentions also work using config
  															del inputs_dict["output_attentions"]
  															a_       :   Dict			=	True
  															a_       :   Union[str, Any]			=	config.window_size**2
  															a_       :   List[str]			=	model_class(UpperCamelCase_							)
  															a_       :   int			=	model(**self._prepare_for_class(UpperCamelCase_				,    UpperCamelCase_							)							)
  															a_       :   int			=	outputs.attentions
  															self.assertEqual(len(UpperCamelCase_							)				,    UpperCamelCase_							)
  
  															self.assertListEqual(
  															    list(attentions[0].shape[-3:]							)				,    [self.model_tester.num_heads[0], window_size_squared, window_size_squared]				,    )
  															a_       :   Union[str, Any]			=	len(UpperCamelCase_							)
  
  															# Check attention is always last and order is fine
  															a_       :   Optional[Any]			=	True
  															a_       :   Union[str, Any]			=	True
  															a_       :   int			=	model_class(UpperCamelCase_							)
  															a_       :   Dict			=	model(**self._prepare_for_class(UpperCamelCase_				,    UpperCamelCase_							)							)
  
  															if hasattr(self.model_tester				,    """num_hidden_states_types"""							):
  																						a_       :   List[Any]			=	self.model_tester.num_hidden_states_types
  															else:
  																						# also another +1 for reshaped_hidden_states
  																						a_       :   Dict			=	2
  															self.assertEqual(out_len + added_hidden_states				,    len(UpperCamelCase_							)							)
  
  															a_       :   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     A				(  self				,    UpperCamelCase_				,    UpperCamelCase_				,    UpperCamelCase_				,    UpperCamelCase_							)		->			Optional[Any]:
  								"""simple docstring"""
  
  
  
  
  
  
  								a_       :   str			=	model_class(UpperCamelCase_							)
  								a_       :   str			=	model(**self._prepare_for_class(UpperCamelCase_				,    UpperCamelCase_							)							)
  								a_       :   List[Any]			=	outputs.hidden_states
  
  								a_       :   Dict			=	getattr(
  								    self.model_tester				,    """expected_num_hidden_layers"""				,    len(self.model_tester.depths							) + 1							)
  								self.assertEqual(len(UpperCamelCase_							)				,    UpperCamelCase_							)
  
  								# Swin has a different seq_length
  								a_       :   Optional[Any]			=	to_atuple(config.patch_size							)
  
  								a_       :   Any			=	(image_size[1] // patch_size[1]) * (image_size[0] // patch_size[0])
  
  								self.assertListEqual(
  								    list(hidden_states[0].shape[-2:]							)				,    [num_patches, self.model_tester.embed_dim]				,    )
  
  								a_       :   int			=	outputs.reshaped_hidden_states
  								self.assertEqual(len(UpperCamelCase_							)				,    UpperCamelCase_							)
  
  								a_       :   List[str]			=	reshaped_hidden_states[0].shape
  
  								a_       :   Union[str, Any]			=	tf.reshape(reshaped_hidden_states[0]				,    (batch_size, num_channels, height * width)							)
  								a_       :   List[str]			=	tf.transpose(UpperCamelCase_				,    (0, 2, 1)							)
  
  								self.assertListEqual(
  								    list(reshaped_hidden_states.shape[-2:]							)				,    [num_patches, self.model_tester.embed_dim]				,    )
  
  
  
  
  
  	def     A				(  self							)		->			Optional[int]:
  								"""simple docstring"""
  
  
  
  
  
  
  								a_       :   Dict			=	self.model_tester.prepare_config_and_inputs_for_common()
  
  								a_       :   Optional[int]			=	to_atuple(self.model_tester.image_size							)
  
  								for model_class in self.all_model_classes:
  															a_       :   List[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"]
  															a_       :   Any			=	True
  
  															self.check_hidden_states_output(UpperCamelCase_				,    UpperCamelCase_				,    UpperCamelCase_				,    UpperCamelCase_							)
  
  
  
  
  
  	def     A				(  self							)		->			List[str]:
  								"""simple docstring"""
  
  
  
  
  
  
  								a_       :   int			=	self.model_tester.prepare_config_and_inputs_for_common()
  								a_       :   Union[str, Any]			=	3
  
  								a_       :   Union[str, Any]			=	to_atuple(self.model_tester.image_size							)
  								a_       :   Optional[Any]			=	to_atuple(config.patch_size							)
  
  								a_       :   Union[str, Any]			=	image_size[0] + patch_size[0] - (image_size[0] % patch_size[0])
  								a_       :   int			=	image_size[1] + patch_size[1] - (image_size[1] % patch_size[1])
  
  								for model_class in self.all_model_classes:
  															a_       :   List[str]			=	True
  															self.check_hidden_states_output(UpperCamelCase_				,    UpperCamelCase_				,    UpperCamelCase_				,    (padded_height, padded_width)							)
  
  															# check that output_hidden_states also work using config
  															del inputs_dict["output_hidden_states"]
  															a_       :   Optional[int]			=	True
  															self.check_hidden_states_output(UpperCamelCase_				,    UpperCamelCase_				,    UpperCamelCase_				,    (padded_height, padded_width)							)
  
  
  
  
  
  	@slow
  	def     A				(  self							)		->			Optional[int]:
  								"""simple docstring"""
  
  
  
  
  
  
  								for model_name in TF_SWIN_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
  															a_       :   Tuple			=	TFSwinModel.from_pretrained(UpperCamelCase_							)
  															self.assertIsNotNone(UpperCamelCase_							)
  
  
  
  
  
  @require_vision
  @require_tf
  class 					snake_case__    ( unittest.TestCase       ):
  
  
  
  
  
  	@cached_property
  	def     A				(  self							)		->			List[Any]:
  								"""simple docstring"""
  
  
  
  
  
  
  								return (
  								    AutoImageProcessor.from_pretrained("""microsoft/swin-tiny-patch4-window7-224"""							)
  								    if is_vision_available()
  								    else None
  								)
  
  
  
  
  
  	@slow
  	def     A				(  self							)		->			Tuple:
  								"""simple docstring"""
  
  
  
  
  
  
  								a_       :   Union[str, Any]			=	TFSwinForImageClassification.from_pretrained("""microsoft/swin-tiny-patch4-window7-224"""							)
  								a_       :   List[str]			=	self.default_image_processor
  
  								a_       :   Any			=	Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png"""							)
  								a_       :   Tuple			=	image_processor(images=UpperCamelCase_				,    return_tensors="""tf"""							)
  
  								# forward pass
  								a_       :   int			=	model(UpperCamelCase_							)
  
  								# verify the logits
  								a_       :   Union[str, Any]			=	tf.TensorShape((1, 1000)							)
  								self.assertEqual(outputs.logits.shape				,    UpperCamelCase_							)
  								a_       :   str			=	tf.constant([-0.0948, -0.6454, -0.0921]							)
  								self.assertTrue(np.allclose(outputs.logits[0, :3]				,    UpperCamelCase_				,    atol=1e-4							)							)
  
  
  
style_context: 
  
  
  from ...utils import is_transformers_available
  from .pipeline_latent_diffusion_superresolution import LDMSuperResolutionPipeline
  
  
  if is_transformers_available():
  	from .pipeline_latent_diffusion import LDMBertModel, LDMTextToImagePipeline
  
  
  