




"""simple docstring"""





import collections
import inspect
import unittest

from transformers import FocalNetConfig
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 (
					    FocalNetBackbone,
					    FocalNetForImageClassification,
					    FocalNetForMaskedImageModeling,
					    FocalNetModel,
					)
					from transformers.models.focalnet.modeling_focalnet import FOCALNET_PRETRAINED_MODEL_ARCHIVE_LIST

if is_vision_available():
					from PIL import Image

					from transformers import AutoImageProcessor



class   a__					:


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


						"""simple docstring"""




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


			def    a_  (   self   :     Tuple):


						"""simple docstring"""




						__UpperCAmelCase						:			int												=       floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size])

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

						__UpperCAmelCase						:			str												=       self.get_config()

						return config, pixel_values, labels


			def    a_  (   self   :     Dict):


						"""simple docstring"""




						return FocalNetConfig(
						    image_size=self.image_size   ,	patch_size=self.patch_size   ,	num_channels=self.num_channels   ,	embed_dim=self.embed_dim   ,	hidden_sizes=self.hidden_sizes   ,	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    a_  (   self   :     List[str]   ,	UpperCamelCase_   :     Union[str, Any]   ,	UpperCamelCase_   :     Tuple   ,	UpperCamelCase_   :     Union[str, Any]):


						"""simple docstring"""




						__UpperCAmelCase						:			List[str]												=       FocalNetModel(config=__UpperCamelCase)
						model.to(__UpperCamelCase)
						model.eval()
						__UpperCAmelCase						:			Union[str, Any]												=       model(__UpperCamelCase)

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


						"""simple docstring"""




						__UpperCAmelCase						:			Any												=       FocalNetBackbone(config=__UpperCamelCase)
						model.to(__UpperCamelCase)
						model.eval()
						__UpperCAmelCase						:			str												=       model(__UpperCamelCase)

						# verify feature maps
						self.parent.assertEqual(len(result.feature_maps)   ,	len(config.out_features))
						self.parent.assertListEqual(list(result.feature_maps[0].shape)   ,	[self.batch_size, self.image_size, 8, 8])

						# verify channels
						self.parent.assertEqual(len(model.channels)   ,	len(config.out_features))
						self.parent.assertListEqual(model.channels   ,	config.hidden_sizes[:-1])

						# verify backbone works with out_features=None
						__UpperCAmelCase						:			str												=       None
						__UpperCAmelCase						:			str												=       FocalNetBackbone(config=__UpperCamelCase)
						model.to(__UpperCamelCase)
						model.eval()
						__UpperCAmelCase						:			Dict												=       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, self.image_size * 2, 4, 4])

						# verify channels
						self.parent.assertEqual(len(model.channels)   ,	1)
						self.parent.assertListEqual(model.channels   ,	[config.hidden_sizes[-1]])


			def    a_  (   self   :     Optional[Any]   ,	UpperCamelCase_   :     Union[str, Any]   ,	UpperCamelCase_   :     str   ,	UpperCamelCase_   :     Any):


						"""simple docstring"""




						__UpperCAmelCase						:			Optional[Any]												=       FocalNetForMaskedImageModeling(config=__UpperCamelCase)
						model.to(__UpperCamelCase)
						model.eval()
						__UpperCAmelCase						:			Tuple												=       model(__UpperCamelCase)
						self.parent.assertEqual(
						    result.reconstruction.shape   ,	(self.batch_size, self.num_channels, self.image_size, self.image_size))

						# test greyscale images
						__UpperCAmelCase						:			Dict												=       1
						__UpperCAmelCase						:			List[Any]												=       FocalNetForMaskedImageModeling(__UpperCamelCase)
						model.to(__UpperCamelCase)
						model.eval()

						__UpperCAmelCase						:			Union[str, Any]												=       floats_tensor([self.batch_size, 1, self.image_size, self.image_size])
						__UpperCAmelCase						:			Any												=       model(__UpperCamelCase)
						self.parent.assertEqual(result.reconstruction.shape   ,	(self.batch_size, 1, self.image_size, self.image_size))


			def    a_  (   self   :     str   ,	UpperCamelCase_   :     str   ,	UpperCamelCase_   :     Optional[int]   ,	UpperCamelCase_   :     Union[str, Any]):


						"""simple docstring"""




						__UpperCAmelCase						:			Optional[int]												=       self.type_sequence_label_size
						__UpperCAmelCase						:			List[str]												=       FocalNetForImageClassification(__UpperCamelCase)
						model.to(__UpperCamelCase)
						model.eval()
						__UpperCAmelCase						:			Union[str, Any]												=       model(__UpperCamelCase   ,	labels=__UpperCamelCase)
						self.parent.assertEqual(result.logits.shape   ,	(self.batch_size, self.type_sequence_label_size))

						# test greyscale images
						__UpperCAmelCase						:			Optional[Any]												=       1
						__UpperCAmelCase						:			Dict												=       FocalNetForImageClassification(__UpperCamelCase)
						model.to(__UpperCamelCase)
						model.eval()

						__UpperCAmelCase						:			str												=       floats_tensor([self.batch_size, 1, self.image_size, self.image_size])
						__UpperCAmelCase						:			List[str]												=       model(__UpperCamelCase)
						self.parent.assertEqual(result.logits.shape   ,	(self.batch_size, self.type_sequence_label_size))


			def    a_  (   self   :     Tuple):


						"""simple docstring"""




						__UpperCAmelCase						:			Tuple												=       self.prepare_config_and_inputs()

						__UpperCAmelCase						,  __UpperCAmelCase						,  __UpperCAmelCase						:			int												=       config_and_inputs
						__UpperCAmelCase						:			Tuple												=       {"pixel_values": pixel_values}
						return config, inputs_dict






@require_torch
class   a__					(						__magic_name__				,     __magic_name__				,     unittest.TestCase ):
			lowercase_		=						(
			    (
			        FocalNetModel,
			        FocalNetForImageClassification,
			        FocalNetForMaskedImageModeling,
			        FocalNetBackbone,
			    )
			    if is_torch_available()
			    else ()
			)
			lowercase_		=						(
			    {"feature-extraction": FocalNetModel, "image-classification": FocalNetForImageClassification}
			    if is_torch_available()
			    else {}
			)
			lowercase_		=						False

			lowercase_		=						False
			lowercase_		=						False
			lowercase_		=						False
			lowercase_		=						False


			def    a_  (   self   :     str):


						"""simple docstring"""




						__UpperCAmelCase						:			List[str]												=       FocalNetModelTester(self)
						__UpperCAmelCase						:			str												=       ConfigTester(self   ,	config_class=__UpperCamelCase   ,	embed_dim=37   ,	has_text_modality=__UpperCamelCase)


			def    a_  (   self   :     Optional[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    a_  (   self   :     Tuple):


						"""simple docstring"""




						return


			def    a_  (   self   :     Optional[int]):


						"""simple docstring"""




						__UpperCAmelCase						:			Dict												=       self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_model(*__UpperCamelCase)


			def    a_  (   self   :     Any):


						"""simple docstring"""




						__UpperCAmelCase						:			Optional[int]												=       self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_backbone(*__UpperCamelCase)


			def    a_  (   self   :     List[Any]):


						"""simple docstring"""




						__UpperCAmelCase						:			str												=       self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_for_masked_image_modeling(*__UpperCamelCase)


			def    a_  (   self   :     List[Any]):


						"""simple docstring"""




						__UpperCAmelCase						:			Tuple												=       self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_for_image_classification(*__UpperCamelCase)


			@unittest.skip(reason="FocalNet does not use inputs_embeds")
			def    a_  (   self   :     str):


						"""simple docstring"""




						pass


			@unittest.skip(reason="FocalNet does not use feedforward chunking")
			def    a_  (   self   :     List[str]):


						"""simple docstring"""




						pass


			def    a_  (   self   :     Optional[int]):


						"""simple docstring"""




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

						for model_class in self.all_model_classes[:-1]:
									__UpperCAmelCase						:			Optional[int]												=       model_class(__UpperCamelCase)
									self.assertIsInstance(model.get_input_embeddings()   ,	(nn.Module))
									__UpperCAmelCase						:			Union[str, Any]												=       model.get_output_embeddings()
									self.assertTrue(x is None or isinstance(__UpperCamelCase   ,	nn.Linear))


			def    a_  (   self   :     Tuple):


						"""simple docstring"""




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

						for model_class in self.all_model_classes[:-1]:
									__UpperCAmelCase						:			List[str]												=       model_class(__UpperCamelCase)
									__UpperCAmelCase						:			Union[str, Any]												=       inspect.signature(model.forward)
									# signature.parameters is an OrderedDict => so arg_names order is deterministic
									__UpperCAmelCase						:			Optional[Any]												=       [*signature.parameters.keys()]

									__UpperCAmelCase						:			Dict												=       ["pixel_values"]
									self.assertListEqual(arg_names[:1]   ,	__UpperCamelCase)


			def    a_  (   self   :     Dict   ,	UpperCamelCase_   :     Tuple   ,	UpperCamelCase_   :     List[str]   ,	UpperCamelCase_   :     str   ,	UpperCamelCase_   :     Optional[Any]):


						"""simple docstring"""




						__UpperCAmelCase						:			Union[str, Any]												=       model_class(__UpperCamelCase)
						model.to(__UpperCamelCase)
						model.eval()

						with torch.no_grad():
									__UpperCAmelCase						:			Optional[int]												=       model(**self._prepare_for_class(__UpperCamelCase   ,	__UpperCamelCase))

						__UpperCAmelCase						:			List[str]												=       outputs.hidden_states

						__UpperCAmelCase						:			Optional[int]												=       getattr(
						    self.model_tester   ,	"expected_num_hidden_layers"   ,	len(self.model_tester.depths) + 1)
						self.assertEqual(len(__UpperCamelCase)   ,	__UpperCamelCase)

						# FocalNet has a different seq_length
						__UpperCAmelCase						:			List[str]												=       (
						    config.patch_size
						    if isinstance(config.patch_size   ,	collections.abc.Iterable)
						    else (config.patch_size, config.patch_size)
						)

						__UpperCAmelCase						:			Optional[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]   ,	)

						__UpperCAmelCase						:			Tuple												=       outputs.reshaped_hidden_states
						self.assertEqual(len(__UpperCamelCase)   ,	__UpperCamelCase)

						__UpperCAmelCase						,  __UpperCAmelCase						,  __UpperCAmelCase						,  __UpperCAmelCase						:			Dict												=       reshaped_hidden_states[0].shape
						__UpperCAmelCase						:			Dict												=       (
						    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    a_  (   self   :     int):


						"""simple docstring"""




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

						__UpperCAmelCase						:			Optional[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)
						)

						for model_class in self.all_model_classes[:-1]:
									__UpperCAmelCase						:			Optional[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"]
									__UpperCAmelCase						:			Dict												=       True

									self.check_hidden_states_output(__UpperCamelCase   ,	__UpperCamelCase   ,	__UpperCamelCase   ,	__UpperCamelCase)


			def    a_  (   self   :     List[str]):


						"""simple docstring"""




						__UpperCAmelCase						,  __UpperCAmelCase						:			Dict												=       self.model_tester.prepare_config_and_inputs_for_common()
						__UpperCAmelCase						:			Tuple												=       3

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

						__UpperCAmelCase						:			List[str]												=       image_size[0] + patch_size[0] - (image_size[0] % patch_size[0])
						__UpperCAmelCase						:			List[Any]												=       image_size[1] + patch_size[1] - (image_size[1] % patch_size[1])

						for model_class in self.all_model_classes[:-1]:
									__UpperCAmelCase						:			List[Any]												=       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"]
									__UpperCAmelCase						:			Optional[Any]												=       True
									self.check_hidden_states_output(__UpperCamelCase   ,	__UpperCamelCase   ,	__UpperCamelCase   ,	(padded_height, padded_width))


			@slow
			def    a_  (   self   :     Dict):


						"""simple docstring"""




						for model_name in FOCALNET_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
									__UpperCAmelCase						:			List[Any]												=       FocalNetModel.from_pretrained(__UpperCamelCase)
									self.assertIsNotNone(__UpperCamelCase)


			def    a_  (   self   :     int):


						"""simple docstring"""




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

						__UpperCAmelCase						:			Any												=       _config_zero_init(__UpperCamelCase)
						for model_class in self.all_model_classes:
									__UpperCAmelCase						:			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   a__					(						unittest.TestCase ):


			@cached_property
			def    a_  (   self   :     int):


						"""simple docstring"""




						return AutoImageProcessor.from_pretrained("microsoft/focalnet-tiny") if is_vision_available() else None


			@slow
			def    a_  (   self   :     str):


						"""simple docstring"""




						__UpperCAmelCase						:			Optional[Any]												=       FocalNetForImageClassification.from_pretrained("microsoft/focalnet-tiny").to(__UpperCamelCase)
						__UpperCAmelCase						:			List[str]												=       self.default_image_processor

						__UpperCAmelCase						:			str												=       Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png")
						__UpperCAmelCase						:			str												=       image_processor(images=__UpperCamelCase   ,	return_tensors="pt").to(__UpperCamelCase)

						# forward pass
						with torch.no_grad():
									__UpperCAmelCase						:			Union[str, Any]												=       model(**__UpperCamelCase)

						# verify the logits
						__UpperCAmelCase						:			List[str]												=       torch.Size((1, 1000))
						self.assertEqual(outputs.logits.shape   ,	__UpperCamelCase)
						__UpperCAmelCase						:			Optional[int]												=       torch.tensor([0.2166, -0.4368, 0.2191]).to(__UpperCamelCase)
						self.assertTrue(torch.allclose(outputs.logits[0, :3]   ,	__UpperCamelCase   ,	atol=1e-4))
						self.assertTrue(outputs.logits.argmax(dim=-1).item()   ,	281)






@require_torch
class   a__					(						__magic_name__				,     unittest.TestCase ):
			lowercase_		=						(FocalNetBackbone,) if is_torch_available() else ()
			lowercase_		=						FocalNetConfig

			lowercase_		=						False


			def    a_  (   self   :     Any):


						"""simple docstring"""




						__UpperCAmelCase						:			Any												=       FocalNetModelTester(self)




import gc
import random
import unittest

import numpy as np
import torch
from PIL import Image
from transformers import CLIPTextConfig, CLIPTextModel, CLIPTokenizer

from diffusers import (
    AutoencoderKL,
    DDIMScheduler,
    EulerAncestralDiscreteScheduler,
    LMSDiscreteScheduler,
    PNDMScheduler,
    StableDiffusionInstructPixaPixPipeline,
    UNetaDConditionModel,
)
from diffusers.image_processor import VaeImageProcessor
from diffusers.utils import floats_tensor, load_image, slow, torch_device
from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu

from ..pipeline_params import (
    IMAGE_TO_IMAGE_IMAGE_PARAMS,
    TEXT_GUIDED_IMAGE_INPAINTING_BATCH_PARAMS,
    TEXT_GUIDED_IMAGE_VARIATION_PARAMS,
)
from ..test_pipelines_common import PipelineKarrasSchedulerTesterMixin, PipelineLatentTesterMixin, PipelineTesterMixin


enable_full_determinism()





class    __A(					UpperCAmelCase  ,		UpperCAmelCase  ,		UpperCAmelCase  ,		unittest.TestCase							):
					SCREAMING_SNAKE_CASE           =     StableDiffusionInstructPixaPixPipeline
					SCREAMING_SNAKE_CASE           =     TEXT_GUIDED_IMAGE_VARIATION_PARAMS - {'''height''', '''width''', '''cross_attention_kwargs'''}
					SCREAMING_SNAKE_CASE           =     TEXT_GUIDED_IMAGE_INPAINTING_BATCH_PARAMS
					SCREAMING_SNAKE_CASE           =     IMAGE_TO_IMAGE_IMAGE_PARAMS
					SCREAMING_SNAKE_CASE           =     IMAGE_TO_IMAGE_IMAGE_PARAMS

					def  lowercase__   (						self       :  List[Any] ):
												torch.manual_seed(0 )
												lowerCamelCase_					=       UNetaDConditionModel(
												    block_out_channels=(3_2, 6_4) ,       layers_per_block=2 ,       sample_size=3_2 ,       in_channels=8 ,       out_channels=4 ,       down_block_types=("""DownBlock2D""", """CrossAttnDownBlock2D""") ,       up_block_types=("""CrossAttnUpBlock2D""", """UpBlock2D""") ,       cross_attention_dim=3_2 ,       )
												lowerCamelCase_					=       PNDMScheduler(skip_prk_steps=__UpperCamelCase )
												torch.manual_seed(0 )
												lowerCamelCase_					=       AutoencoderKL(
												    block_out_channels=[3_2, 6_4] ,       in_channels=3 ,       out_channels=3 ,       down_block_types=["""DownEncoderBlock2D""", """DownEncoderBlock2D"""] ,       up_block_types=["""UpDecoderBlock2D""", """UpDecoderBlock2D"""] ,       latent_channels=4 ,       )
												torch.manual_seed(0 )
												lowerCamelCase_					=       CLIPTextConfig(
												    bos_token_id=0 ,       eos_token_id=2 ,       hidden_size=3_2 ,       intermediate_size=3_7 ,       layer_norm_eps=1E-05 ,       num_attention_heads=4 ,       num_hidden_layers=5 ,       pad_token_id=1 ,       vocab_size=1_0_0_0 ,       )
												lowerCamelCase_					=       CLIPTextModel(__UpperCamelCase )
												lowerCamelCase_					=       CLIPTokenizer.from_pretrained("""hf-internal-testing/tiny-random-clip""" )

												lowerCamelCase_					=       {
												    """unet""": unet,
												    """scheduler""": scheduler,
												    """vae""": vae,
												    """text_encoder""": text_encoder,
												    """tokenizer""": tokenizer,
												    """safety_checker""": None,
												    """feature_extractor""": None,
												}
												return components

					def  lowercase__   (						self       :  Optional[int] ,       __UpperCamelCase       :  Optional[int] ,       __UpperCamelCase       :  Any=0 ):
												lowerCamelCase_					=       floats_tensor((1, 3, 3_2, 3_2) ,       rng=random.Random(__UpperCamelCase ) ).to(__UpperCamelCase )
												lowerCamelCase_					=       image.cpu().permute(0 ,       2 ,       3 ,       1 )[0]
												lowerCamelCase_					=       Image.fromarray(np.uinta(__UpperCamelCase ) ).convert("""RGB""" )
												if str(__UpperCamelCase ).startswith("""mps""" ):
																			lowerCamelCase_					=       torch.manual_seed(__UpperCamelCase )
												else:
																			lowerCamelCase_					=       torch.Generator(device=__UpperCamelCase ).manual_seed(__UpperCamelCase )
												lowerCamelCase_					=       {
												    """prompt""": """A painting of a squirrel eating a burger""",
												    """image""": image,
												    """generator""": generator,
												    """num_inference_steps""": 2,
												    """guidance_scale""": 6.0,
												    """image_guidance_scale""": 1,
												    """output_type""": """numpy""",
												}
												return inputs

					def  lowercase__   (						self       :  Dict ):
												lowerCamelCase_					=       """cpu"""  # ensure determinism for the device-dependent torch.Generator
												lowerCamelCase_					=       self.get_dummy_components()
												lowerCamelCase_					=       StableDiffusionInstructPixaPixPipeline(**__UpperCamelCase )
												lowerCamelCase_					=       sd_pipe.to(__UpperCamelCase )
												sd_pipe.set_progress_bar_config(disable=__UpperCamelCase )

												lowerCamelCase_					=       self.get_dummy_inputs(__UpperCamelCase )
												lowerCamelCase_					=       sd_pipe(**__UpperCamelCase ).images
												lowerCamelCase_					=       image[0, -3:, -3:, -1]
												assert image.shape == (1, 3_2, 3_2, 3)
												lowerCamelCase_					=       np.array([0.7526, 0.3750, 0.4547, 0.6117, 0.5866, 0.5016, 0.4327, 0.5642, 0.4815] )

												assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-3

					def  lowercase__   (						self       :  Dict ):
												lowerCamelCase_					=       """cpu"""  # ensure determinism for the device-dependent torch.Generator
												lowerCamelCase_					=       self.get_dummy_components()
												lowerCamelCase_					=       StableDiffusionInstructPixaPixPipeline(**__UpperCamelCase )
												lowerCamelCase_					=       sd_pipe.to(__UpperCamelCase )
												sd_pipe.set_progress_bar_config(disable=__UpperCamelCase )

												lowerCamelCase_					=       self.get_dummy_inputs(__UpperCamelCase )
												lowerCamelCase_					=       """french fries"""
												lowerCamelCase_					=       sd_pipe(**__UpperCamelCase ,       negative_prompt=__UpperCamelCase )
												lowerCamelCase_					=       output.images
												lowerCamelCase_					=       image[0, -3:, -3:, -1]

												assert image.shape == (1, 3_2, 3_2, 3)
												lowerCamelCase_					=       np.array([0.7511, 0.3642, 0.4553, 0.6236, 0.5797, 0.5013, 0.4343, 0.5611, 0.4831] )

												assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-3

					def  lowercase__   (						self       :  Optional[int] ):
												lowerCamelCase_					=       """cpu"""  # ensure determinism for the device-dependent torch.Generator
												lowerCamelCase_					=       self.get_dummy_components()
												lowerCamelCase_					=       StableDiffusionInstructPixaPixPipeline(**__UpperCamelCase )
												lowerCamelCase_					=       sd_pipe.to(__UpperCamelCase )
												sd_pipe.set_progress_bar_config(disable=__UpperCamelCase )

												lowerCamelCase_					=       self.get_dummy_inputs(__UpperCamelCase )
												lowerCamelCase_					=       [inputs["""prompt"""]] * 2

												lowerCamelCase_					=       np.array(inputs["""image"""] ).astype(np.floataa ) / 255.0
												lowerCamelCase_					=       torch.from_numpy(__UpperCamelCase ).unsqueeze(0 ).to(__UpperCamelCase )
												lowerCamelCase_					=       image / 2 + 0.5
												lowerCamelCase_					=       image.permute(0 ,       3 ,       1 ,       2 )
												lowerCamelCase_					=       image.repeat(2 ,       1 ,       1 ,       1 )

												lowerCamelCase_					=       sd_pipe(**__UpperCamelCase ).images
												lowerCamelCase_					=       image[-1, -3:, -3:, -1]

												assert image.shape == (2, 3_2, 3_2, 3)
												lowerCamelCase_					=       np.array([0.5812, 0.5748, 0.5222, 0.5908, 0.5695, 0.7174, 0.6804, 0.5523, 0.5579] )

												assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-3

					def  lowercase__   (						self       :  Dict ):
												lowerCamelCase_					=       """cpu"""  # ensure determinism for the device-dependent torch.Generator
												lowerCamelCase_					=       self.get_dummy_components()
												lowerCamelCase_					=       EulerAncestralDiscreteScheduler(
												    beta_start=0.00085 ,       beta_end=0.012 ,       beta_schedule="""scaled_linear""" )
												lowerCamelCase_					=       StableDiffusionInstructPixaPixPipeline(**__UpperCamelCase )
												lowerCamelCase_					=       sd_pipe.to(__UpperCamelCase )
												sd_pipe.set_progress_bar_config(disable=__UpperCamelCase )

												lowerCamelCase_					=       self.get_dummy_inputs(__UpperCamelCase )
												lowerCamelCase_					=       sd_pipe(**__UpperCamelCase ).images
												lowerCamelCase_					=       image[0, -3:, -3:, -1]

												lowerCamelCase_					=       [round(__UpperCamelCase ,       4 ) for x in image_slice.flatten().tolist()]
												print(""",""".join([str(__UpperCamelCase ) for x in slice] ) )

												assert image.shape == (1, 3_2, 3_2, 3)
												lowerCamelCase_					=       np.array([0.7417, 0.3842, 0.4732, 0.5776, 0.5891, 0.5139, 0.4052, 0.5673, 0.4986] )

												assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-3

					def  lowercase__   (						self       :  str ):
												super().test_inference_batch_single_identical(expected_max_diff=3E-3 )



					def  lowercase__   (						self       :  Optional[int] ):
												lowerCamelCase_					=       self.get_dummy_components()
												lowerCamelCase_					=       StableDiffusionInstructPixaPixPipeline(**__UpperCamelCase )
												lowerCamelCase_					=       VaeImageProcessor(do_resize=__UpperCamelCase ,       do_normalize=__UpperCamelCase )
												lowerCamelCase_					=       pipe.to(__UpperCamelCase )
												pipe.set_progress_bar_config(disable=__UpperCamelCase )

												lowerCamelCase_					=       pipe(**self.get_dummy_inputs_by_type(__UpperCamelCase ,       input_image_type="""pt""" ) )[0]

												lowerCamelCase_					=       components["""vae"""]
												lowerCamelCase_					=       self.get_dummy_inputs_by_type(__UpperCamelCase ,       input_image_type="""pt""" )

												for image_param in self.image_latents_params:
																			if image_param in inputs.keys():
																										lowerCamelCase_					=       vae.encode(inputs[image_param] ).latent_dist.mode()

												lowerCamelCase_					=       pipe(**__UpperCamelCase )[0]

												lowerCamelCase_					=       np.abs(out - out_latents_inputs ).max()
												self.assertLess(__UpperCamelCase ,       1E-4 ,       """passing latents as image input generate different result from passing image""" )





@slow
@require_torch_gpu
class    __A(					unittest.TestCase							):

					def  lowercase__   (						self       :  List[Any] ):
												super().tearDown()
												gc.collect()
												torch.cuda.empty_cache()

					def  lowercase__   (						self       :  List[str] ,       __UpperCamelCase       :  Union[str, Any]=0 ):
												lowerCamelCase_					=       torch.manual_seed(__UpperCamelCase )
												lowerCamelCase_					=       load_image(
												    """https://huggingface.co/datasets/diffusers/test-arrays/resolve/main/stable_diffusion_pix2pix/example.jpg""" )
												lowerCamelCase_					=       {
												    """prompt""": """turn him into a cyborg""",
												    """image""": image,
												    """generator""": generator,
												    """num_inference_steps""": 3,
												    """guidance_scale""": 7.5,
												    """image_guidance_scale""": 1.0,
												    """output_type""": """numpy""",
												}
												return inputs

					def  lowercase__   (						self       :  Union[str, Any] ):
												lowerCamelCase_					=       StableDiffusionInstructPixaPixPipeline.from_pretrained(
												    """timbrooks/instruct-pix2pix""" ,       safety_checker=__UpperCamelCase )
												pipe.to(__UpperCamelCase )
												pipe.set_progress_bar_config(disable=__UpperCamelCase )
												pipe.enable_attention_slicing()

												lowerCamelCase_					=       self.get_inputs()
												lowerCamelCase_					=       pipe(**__UpperCamelCase ).images
												lowerCamelCase_					=       image[0, -3:, -3:, -1].flatten()

												assert image.shape == (1, 5_1_2, 5_1_2, 3)
												lowerCamelCase_					=       np.array([0.5902, 0.6015, 0.6027, 0.5983, 0.6092, 0.6061, 0.5765, 0.5785, 0.5555] )

												assert np.abs(expected_slice - image_slice ).max() < 1E-3

					def  lowercase__   (						self       :  List[str] ):
												lowerCamelCase_					=       StableDiffusionInstructPixaPixPipeline.from_pretrained(
												    """timbrooks/instruct-pix2pix""" ,       safety_checker=__UpperCamelCase )
												lowerCamelCase_					=       LMSDiscreteScheduler.from_config(pipe.scheduler.config )
												pipe.to(__UpperCamelCase )
												pipe.set_progress_bar_config(disable=__UpperCamelCase )
												pipe.enable_attention_slicing()

												lowerCamelCase_					=       self.get_inputs()
												lowerCamelCase_					=       pipe(**__UpperCamelCase ).images
												lowerCamelCase_					=       image[0, -3:, -3:, -1].flatten()

												assert image.shape == (1, 5_1_2, 5_1_2, 3)
												lowerCamelCase_					=       np.array([0.6578, 0.6817, 0.6972, 0.6761, 0.6856, 0.6916, 0.6428, 0.6516, 0.6301] )

												assert np.abs(expected_slice - image_slice ).max() < 1E-3

					def  lowercase__   (						self       :  List[Any] ):
												lowerCamelCase_					=       StableDiffusionInstructPixaPixPipeline.from_pretrained(
												    """timbrooks/instruct-pix2pix""" ,       safety_checker=__UpperCamelCase )
												lowerCamelCase_					=       DDIMScheduler.from_config(pipe.scheduler.config )
												pipe.to(__UpperCamelCase )
												pipe.set_progress_bar_config(disable=__UpperCamelCase )
												pipe.enable_attention_slicing()

												lowerCamelCase_					=       self.get_inputs()
												lowerCamelCase_					=       pipe(**__UpperCamelCase ).images
												lowerCamelCase_					=       image[0, -3:, -3:, -1].flatten()

												assert image.shape == (1, 5_1_2, 5_1_2, 3)
												lowerCamelCase_					=       np.array([0.3828, 0.3834, 0.3818, 0.3792, 0.3865, 0.3752, 0.3792, 0.3847, 0.3753] )

												assert np.abs(expected_slice - image_slice ).max() < 1E-3

					def  lowercase__   (						self       :  List[str] ):
												lowerCamelCase_					=       0

												def callback_fn(__UpperCamelCase       :  int ,       __UpperCamelCase       :  int ,       __UpperCamelCase       :  torch.FloatTensor ) -> None:
																			lowerCamelCase_					=       True
																			nonlocal number_of_steps
																			number_of_steps += 1
																			if step == 1:
																										lowerCamelCase_					=       latents.detach().cpu().numpy()
																										assert latents.shape == (1, 4, 6_4, 6_4)
																										lowerCamelCase_					=       latents[0, -3:, -3:, -1]
																										lowerCamelCase_					=       np.array([-0.2463, -0.4644, -0.9756, 1.5176, 1.4414, 0.7866, 0.9897, 0.8521, 0.7983] )

																										assert np.abs(latents_slice.flatten() - expected_slice ).max() < 5E-2
																			elif step == 2:
																										lowerCamelCase_					=       latents.detach().cpu().numpy()
																										assert latents.shape == (1, 4, 6_4, 6_4)
																										lowerCamelCase_					=       latents[0, -3:, -3:, -1]
																										lowerCamelCase_					=       np.array([-0.2644, -0.4626, -0.9653, 1.5176, 1.4551, 0.7686, 0.9805, 0.8452, 0.8115] )

																										assert np.abs(latents_slice.flatten() - expected_slice ).max() < 5E-2

												lowerCamelCase_					=       False

												lowerCamelCase_					=       StableDiffusionInstructPixaPixPipeline.from_pretrained(
												    """timbrooks/instruct-pix2pix""" ,       safety_checker=__UpperCamelCase ,       torch_dtype=torch.floataa )
												lowerCamelCase_					=       pipe.to(__UpperCamelCase )
												pipe.set_progress_bar_config(disable=__UpperCamelCase )
												pipe.enable_attention_slicing()

												lowerCamelCase_					=       self.get_inputs()
												pipe(**__UpperCamelCase ,       callback=__UpperCamelCase ,       callback_steps=1 )
												assert callback_fn.has_been_called
												assert number_of_steps == 3

					def  lowercase__   (						self       :  int ):
												torch.cuda.empty_cache()
												torch.cuda.reset_max_memory_allocated()
												torch.cuda.reset_peak_memory_stats()

												lowerCamelCase_					=       StableDiffusionInstructPixaPixPipeline.from_pretrained(
												    """timbrooks/instruct-pix2pix""" ,       safety_checker=__UpperCamelCase ,       torch_dtype=torch.floataa )
												lowerCamelCase_					=       pipe.to(__UpperCamelCase )
												pipe.set_progress_bar_config(disable=__UpperCamelCase )
												pipe.enable_attention_slicing(1 )
												pipe.enable_sequential_cpu_offload()

												lowerCamelCase_					=       self.get_inputs()
												lowerCamelCase_					=       pipe(**__UpperCamelCase )

												lowerCamelCase_					=       torch.cuda.max_memory_allocated()
												# make sure that less than 2.2 GB is allocated
												assert mem_bytes < 2.2 * 1_0**9



					def  lowercase__   (						self       :  Tuple ):
												lowerCamelCase_					=       self.get_inputs()
												# resize to resolution that is divisible by 8 but not 16 or 32
												lowerCamelCase_					=       inputs["""image"""].resize((5_0_4, 5_0_4) )

												lowerCamelCase_					=       """timbrooks/instruct-pix2pix"""
												lowerCamelCase_					=       StableDiffusionInstructPixaPixPipeline.from_pretrained(
												    __UpperCamelCase ,       safety_checker=__UpperCamelCase ,       )
												pipe.to(__UpperCamelCase )
												pipe.set_progress_bar_config(disable=__UpperCamelCase )
												pipe.enable_attention_slicing()

												lowerCamelCase_					=       pipe(**__UpperCamelCase )
												lowerCamelCase_					=       output.images[0]

												lowerCamelCase_					=       image[2_5_5:2_5_8, 3_8_3:3_8_6, -1]

												assert image.shape == (5_0_4, 5_0_4, 3)
												lowerCamelCase_					=       np.array([0.2726, 0.2529, 0.2664, 0.2655, 0.2641, 0.2642, 0.2591, 0.2649, 0.2590] )

												assert np.abs(image_slice.flatten() - expected_slice ).max() < 5E-3





