"""simple docstring"""




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							(			SCREAMING_SNAKE_CASE_   ,     SCREAMING_SNAKE_CASE_   ,     SCREAMING_SNAKE_CASE_   ,     unittest.TestCase		):
     _UpperCamelCase							:      Optional[Any]		 =		StableDiffusionInstructPixaPixPipeline
     _UpperCamelCase							:      Any		 =		TEXT_GUIDED_IMAGE_VARIATION_PARAMS - {"height", "width", "cross_attention_kwargs"}
     _UpperCamelCase							:      List[Any]		 =		TEXT_GUIDED_IMAGE_INPAINTING_BATCH_PARAMS
     _UpperCamelCase							:      Dict		 =		IMAGE_TO_IMAGE_IMAGE_PARAMS
     _UpperCamelCase							:      List[Any]		 =		IMAGE_TO_IMAGE_IMAGE_PARAMS






     def    __A  (		self  ):
       torch.manual_seed(0  )
       _lowerCAmelCase						:	List[str]        =					UNetaDConditionModel(
           block_out_channels=(32, 64)    ,					layers_per_block=2    ,					sample_size=32    ,					in_channels=8    ,					out_channels=4    ,					down_block_types=("""DownBlock2D""", """CrossAttnDownBlock2D""")    ,					up_block_types=("""CrossAttnUpBlock2D""", """UpBlock2D""")    ,					cross_attention_dim=32    ,					)
       _lowerCAmelCase						:	Tuple        =					PNDMScheduler(skip_prk_steps=a__  )
       torch.manual_seed(0  )
       _lowerCAmelCase						:	Dict        =					AutoencoderKL(
           block_out_channels=[32, 64]    ,					in_channels=3    ,					out_channels=3    ,					down_block_types=["""DownEncoderBlock2D""", """DownEncoderBlock2D"""]    ,					up_block_types=["""UpDecoderBlock2D""", """UpDecoderBlock2D"""]    ,					latent_channels=4    ,					)
       torch.manual_seed(0  )
       _lowerCAmelCase						:	Union[str, Any]        =					CLIPTextConfig(
           bos_token_id=0    ,					eos_token_id=2    ,					hidden_size=32    ,					intermediate_size=37    ,					layer_norm_eps=1e-05    ,					num_attention_heads=4    ,					num_hidden_layers=5    ,					pad_token_id=1    ,					vocab_size=1000    ,					)
       _lowerCAmelCase						:	Tuple        =					CLIPTextModel(a__  )
       _lowerCAmelCase						:	Tuple        =					CLIPTokenizer.from_pretrained("""hf-internal-testing/tiny-random-clip"""  )

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






     def    __A  (		self    ,					a__    ,					a__=0  ):
       _lowerCAmelCase						:	Optional[int]        =					floats_tensor((1, 3, 32, 32)    ,					rng=random.Random(a__  )  ).to(a__  )
       _lowerCAmelCase						:	List[str]        =					image.cpu().permute(0    ,					2    ,					3    ,					1  )[0]
       _lowerCAmelCase						:	Union[str, Any]        =					Image.fromarray(np.uinta(a__  )  ).convert("""RGB"""  )
       if str(a__  ).startswith("""mps"""  ):
         _lowerCAmelCase						:	int        =					torch.manual_seed(a__  )
       else:
         _lowerCAmelCase						:	List[Any]        =					torch.Generator(device=a__  ).manual_seed(a__  )
       _lowerCAmelCase						:	int        =					{
           """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    __A  (		self  ):
       _lowerCAmelCase						:	Union[str, Any]        =					"""cpu"""  # ensure determinism for the device-dependent torch.Generator
       _lowerCAmelCase						:	str        =					self.get_dummy_components()
       _lowerCAmelCase						:	Optional[int]        =					StableDiffusionInstructPixaPixPipeline(**a__  )
       _lowerCAmelCase						:	List[str]        =					sd_pipe.to(a__  )
       sd_pipe.set_progress_bar_config(disable=a__  )

       _lowerCAmelCase						:	Dict        =					self.get_dummy_inputs(a__  )
       _lowerCAmelCase						:	Dict        =					sd_pipe(**a__  ).images
       _lowerCAmelCase						:	Tuple        =					image[0, -3:, -3:, -1]
       assert image.shape == (1, 32, 32, 3)
       _lowerCAmelCase						:	Dict        =					np.array([0.7_5_2_6, 0.3_7_5_0, 0.4_5_4_7, 0.6_1_1_7, 0.5_8_6_6, 0.5_0_1_6, 0.4_3_2_7, 0.5_6_4_2, 0.4_8_1_5]  )

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






     def    __A  (		self  ):
       _lowerCAmelCase						:	str        =					"""cpu"""  # ensure determinism for the device-dependent torch.Generator
       _lowerCAmelCase						:	Tuple        =					self.get_dummy_components()
       _lowerCAmelCase						:	Any        =					StableDiffusionInstructPixaPixPipeline(**a__  )
       _lowerCAmelCase						:	List[str]        =					sd_pipe.to(a__  )
       sd_pipe.set_progress_bar_config(disable=a__  )

       _lowerCAmelCase						:	List[Any]        =					self.get_dummy_inputs(a__  )
       _lowerCAmelCase						:	Optional[Any]        =					"""french fries"""
       _lowerCAmelCase						:	List[Any]        =					sd_pipe(**a__    ,					negative_prompt=a__  )
       _lowerCAmelCase						:	int        =					output.images
       _lowerCAmelCase						:	str        =					image[0, -3:, -3:, -1]

       assert image.shape == (1, 32, 32, 3)
       _lowerCAmelCase						:	int        =					np.array([0.7_5_1_1, 0.3_6_4_2, 0.4_5_5_3, 0.6_2_3_6, 0.5_7_9_7, 0.5_0_1_3, 0.4_3_4_3, 0.5_6_1_1, 0.4_8_3_1]  )

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






     def    __A  (		self  ):
       _lowerCAmelCase						:	int        =					"""cpu"""  # ensure determinism for the device-dependent torch.Generator
       _lowerCAmelCase						:	List[str]        =					self.get_dummy_components()
       _lowerCAmelCase						:	Any        =					StableDiffusionInstructPixaPixPipeline(**a__  )
       _lowerCAmelCase						:	Optional[int]        =					sd_pipe.to(a__  )
       sd_pipe.set_progress_bar_config(disable=a__  )

       _lowerCAmelCase						:	List[Any]        =					self.get_dummy_inputs(a__  )
       _lowerCAmelCase						:	int        =					[inputs["""prompt"""]] * 2

       _lowerCAmelCase						:	Optional[Any]        =					np.array(inputs["""image"""]  ).astype(np.floataa  ) / 255.0
       _lowerCAmelCase						:	Tuple        =					torch.from_numpy(a__  ).unsqueeze(0  ).to(a__  )
       _lowerCAmelCase						:	List[str]        =					image / 2 + 0.5
       _lowerCAmelCase						:	Tuple        =					image.permute(0    ,					3    ,					1    ,					2  )
       _lowerCAmelCase						:	List[str]        =					image.repeat(2    ,					1    ,					1    ,					1  )

       _lowerCAmelCase						:	int        =					sd_pipe(**a__  ).images
       _lowerCAmelCase						:	Any        =					image[-1, -3:, -3:, -1]

       assert image.shape == (2, 32, 32, 3)
       _lowerCAmelCase						:	str        =					np.array([0.5_8_1_2, 0.5_7_4_8, 0.5_2_2_2, 0.5_9_0_8, 0.5_6_9_5, 0.7_1_7_4, 0.6_8_0_4, 0.5_5_2_3, 0.5_5_7_9]  )

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






     def    __A  (		self  ):
       _lowerCAmelCase						:	Tuple        =					"""cpu"""  # ensure determinism for the device-dependent torch.Generator
       _lowerCAmelCase						:	List[Any]        =					self.get_dummy_components()
       _lowerCAmelCase						:	Union[str, Any]        =					EulerAncestralDiscreteScheduler(
           beta_start=0.0_0_0_8_5    ,					beta_end=0.0_1_2    ,					beta_schedule="""scaled_linear"""  )
       _lowerCAmelCase						:	str        =					StableDiffusionInstructPixaPixPipeline(**a__  )
       _lowerCAmelCase						:	Optional[int]        =					sd_pipe.to(a__  )
       sd_pipe.set_progress_bar_config(disable=a__  )

       _lowerCAmelCase						:	str        =					self.get_dummy_inputs(a__  )
       _lowerCAmelCase						:	str        =					sd_pipe(**a__  ).images
       _lowerCAmelCase						:	Any        =					image[0, -3:, -3:, -1]

       _lowerCAmelCase						:	Optional[int]        =					[round(a__    ,					4  ) for x in image_slice.flatten().tolist()]
       print(""",""".join([str(a__  ) for x in slice]  )  )

       assert image.shape == (1, 32, 32, 3)
       _lowerCAmelCase						:	Optional[int]        =					np.array([0.7_4_1_7, 0.3_8_4_2, 0.4_7_3_2, 0.5_7_7_6, 0.5_8_9_1, 0.5_1_3_9, 0.4_0_5_2, 0.5_6_7_3, 0.4_9_8_6]  )

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






     def    __A  (		self  ):
       super().test_inference_batch_single_identical(expected_max_diff=3e-3  )






     def    __A  (		self  ):
       _lowerCAmelCase						:	Dict        =					self.get_dummy_components()
       _lowerCAmelCase						:	Dict        =					StableDiffusionInstructPixaPixPipeline(**a__  )
       _lowerCAmelCase						:	Dict        =					VaeImageProcessor(do_resize=a__    ,					do_normalize=a__  )
       _lowerCAmelCase						:	Dict        =					pipe.to(a__  )
       pipe.set_progress_bar_config(disable=a__  )

       _lowerCAmelCase						:	Any        =					pipe(**self.get_dummy_inputs_by_type(a__    ,					input_image_type="""pt"""  )  )[0]

       _lowerCAmelCase						:	int        =					components["""vae"""]
       _lowerCAmelCase						:	Optional[int]        =					self.get_dummy_inputs_by_type(a__    ,					input_image_type="""pt"""  )

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

       _lowerCAmelCase						:	Optional[Any]        =					pipe(**a__  )[0]

       _lowerCAmelCase						:	Union[str, Any]        =					np.abs(out - out_latents_inputs  ).max()
       self.assertLess(a__    ,					1e-4    ,					"""passing latents as image input generate different result from passing image"""  )







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






     def    __A  (		self  ):
       super().tearDown()
       gc.collect()
       torch.cuda.empty_cache()






     def    __A  (		self    ,					a__=0  ):
       _lowerCAmelCase						:	str        =					torch.manual_seed(a__  )
       _lowerCAmelCase						:	List[Any]        =					load_image(
           """https://huggingface.co/datasets/diffusers/test-arrays/resolve/main/stable_diffusion_pix2pix/example.jpg"""  )
       _lowerCAmelCase						:	Optional[int]        =					{
           """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    __A  (		self  ):
       _lowerCAmelCase						:	Any        =					StableDiffusionInstructPixaPixPipeline.from_pretrained(
           """timbrooks/instruct-pix2pix"""    ,					safety_checker=a__  )
       pipe.to(a__  )
       pipe.set_progress_bar_config(disable=a__  )
       pipe.enable_attention_slicing()

       _lowerCAmelCase						:	List[Any]        =					self.get_inputs()
       _lowerCAmelCase						:	List[str]        =					pipe(**a__  ).images
       _lowerCAmelCase						:	List[Any]        =					image[0, -3:, -3:, -1].flatten()

       assert image.shape == (1, 512, 512, 3)
       _lowerCAmelCase						:	str        =					np.array([0.5_9_0_2, 0.6_0_1_5, 0.6_0_2_7, 0.5_9_8_3, 0.6_0_9_2, 0.6_0_6_1, 0.5_7_6_5, 0.5_7_8_5, 0.5_5_5_5]  )

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






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

       _lowerCAmelCase						:	Optional[Any]        =					self.get_inputs()
       _lowerCAmelCase						:	int        =					pipe(**a__  ).images
       _lowerCAmelCase						:	Union[str, Any]        =					image[0, -3:, -3:, -1].flatten()

       assert image.shape == (1, 512, 512, 3)
       _lowerCAmelCase						:	str        =					np.array([0.6_5_7_8, 0.6_8_1_7, 0.6_9_7_2, 0.6_7_6_1, 0.6_8_5_6, 0.6_9_1_6, 0.6_4_2_8, 0.6_5_1_6, 0.6_3_0_1]  )

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






     def    __A  (		self  ):
       _lowerCAmelCase						:	Any        =					StableDiffusionInstructPixaPixPipeline.from_pretrained(
           """timbrooks/instruct-pix2pix"""    ,					safety_checker=a__  )
       _lowerCAmelCase						:	Union[str, Any]        =					DDIMScheduler.from_config(pipe.scheduler.config  )
       pipe.to(a__  )
       pipe.set_progress_bar_config(disable=a__  )
       pipe.enable_attention_slicing()

       _lowerCAmelCase						:	Optional[Any]        =					self.get_inputs()
       _lowerCAmelCase						:	Tuple        =					pipe(**a__  ).images
       _lowerCAmelCase						:	Tuple        =					image[0, -3:, -3:, -1].flatten()

       assert image.shape == (1, 512, 512, 3)
       _lowerCAmelCase						:	List[str]        =					np.array([0.3_8_2_8, 0.3_8_3_4, 0.3_8_1_8, 0.3_7_9_2, 0.3_8_6_5, 0.3_7_5_2, 0.3_7_9_2, 0.3_8_4_7, 0.3_7_5_3]  )

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






     def    __A  (		self  ):
       _lowerCAmelCase						:	Any        =					0

       def callback_fn(a__    ,					a__    ,					a__  ) -> None:
         _lowerCAmelCase						:	Any        =					True
         nonlocal number_of_steps
         number_of_steps += 1
         if step == 1:
           _lowerCAmelCase						:	str        =					latents.detach().cpu().numpy()
           assert latents.shape == (1, 4, 64, 64)
           _lowerCAmelCase						:	List[Any]        =					latents[0, -3:, -3:, -1]
           _lowerCAmelCase						:	str        =					np.array([-0.2_4_6_3, -0.4_6_4_4, -0.9_7_5_6, 1.5_1_7_6, 1.4_4_1_4, 0.7_8_6_6, 0.9_8_9_7, 0.8_5_2_1, 0.7_9_8_3]  )

           assert np.abs(latents_slice.flatten() - expected_slice  ).max() < 5e-2
         elif step == 2:
           _lowerCAmelCase						:	List[str]        =					latents.detach().cpu().numpy()
           assert latents.shape == (1, 4, 64, 64)
           _lowerCAmelCase						:	Optional[int]        =					latents[0, -3:, -3:, -1]
           _lowerCAmelCase						:	Tuple        =					np.array([-0.2_6_4_4, -0.4_6_2_6, -0.9_6_5_3, 1.5_1_7_6, 1.4_5_5_1, 0.7_6_8_6, 0.9_8_0_5, 0.8_4_5_2, 0.8_1_1_5]  )

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

       _lowerCAmelCase						:	Union[str, Any]        =					False

       _lowerCAmelCase						:	Any        =					StableDiffusionInstructPixaPixPipeline.from_pretrained(
           """timbrooks/instruct-pix2pix"""    ,					safety_checker=a__    ,					torch_dtype=torch.floataa  )
       _lowerCAmelCase						:	str        =					pipe.to(a__  )
       pipe.set_progress_bar_config(disable=a__  )
       pipe.enable_attention_slicing()

       _lowerCAmelCase						:	Union[str, Any]        =					self.get_inputs()
       pipe(**a__    ,					callback=a__    ,					callback_steps=1  )
       assert callback_fn.has_been_called
       assert number_of_steps == 3






     def    __A  (		self  ):
       torch.cuda.empty_cache()
       torch.cuda.reset_max_memory_allocated()
       torch.cuda.reset_peak_memory_stats()

       _lowerCAmelCase						:	Tuple        =					StableDiffusionInstructPixaPixPipeline.from_pretrained(
           """timbrooks/instruct-pix2pix"""    ,					safety_checker=a__    ,					torch_dtype=torch.floataa  )
       _lowerCAmelCase						:	int        =					pipe.to(a__  )
       pipe.set_progress_bar_config(disable=a__  )
       pipe.enable_attention_slicing(1  )
       pipe.enable_sequential_cpu_offload()

       _lowerCAmelCase						:	Dict        =					self.get_inputs()
       _lowerCAmelCase						:	Dict        =					pipe(**a__  )

       _lowerCAmelCase						:	Tuple        =					torch.cuda.max_memory_allocated()
       # make sure that less than 2.2 GB is allocated
       assert mem_bytes < 2.2 * 10**9






     def    __A  (		self  ):
       _lowerCAmelCase						:	Tuple        =					self.get_inputs()
       # resize to resolution that is divisible by 8 but not 16 or 32
       _lowerCAmelCase						:	Any        =					inputs["""image"""].resize((504, 504)  )

       _lowerCAmelCase						:	Dict        =					"""timbrooks/instruct-pix2pix"""
       _lowerCAmelCase						:	List[str]        =					StableDiffusionInstructPixaPixPipeline.from_pretrained(
           a__    ,					safety_checker=a__    ,					)
       pipe.to(a__  )
       pipe.set_progress_bar_config(disable=a__  )
       pipe.enable_attention_slicing()

       _lowerCAmelCase						:	Union[str, Any]        =					pipe(**a__  )
       _lowerCAmelCase						:	Any        =					output.images[0]

       _lowerCAmelCase						:	Optional[int]        =					image[255:258, 383:386, -1]

       assert image.shape == (504, 504, 3)
       _lowerCAmelCase						:	Dict        =					np.array([0.2_7_2_6, 0.2_5_2_9, 0.2_6_6_4, 0.2_6_5_5, 0.2_6_4_1, 0.2_6_4_2, 0.2_5_9_1, 0.2_6_4_9, 0.2_5_9_0]  )

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





"""simple docstring"""




from ...configuration_utils import PretrainedConfig
from ...utils import logging
from ...utils.backbone_utils import BackboneConfigMixin, get_aligned_output_features_output_indices


_a				:		List[Any]	   =       logging.get_logger(__name__)
class 						__A							(			SCREAMING_SNAKE_CASE_   ,     SCREAMING_SNAKE_CASE_		):
     _UpperCamelCase							:      Tuple		 =		"maskformer-swin"

     _UpperCamelCase							:      Union[str, Any]		 =		{
         "num_attention_heads": "num_heads",
         "num_hidden_layers": "num_layers",
     }






     def __init__(		self    ,					a__=224    ,					a__=4    ,					a__=3    ,					a__=96    ,					a__=[2, 2, 6, 2]    ,					a__=[3, 6, 12, 24]    ,					a__=7    ,					a__=4.0    ,					a__=True    ,					a__=0.0    ,					a__=0.0    ,					a__=0.1    ,					a__="gelu"    ,					a__=False    ,					a__=0.0_2    ,					a__=1e-5    ,					a__=None    ,					a__=None    ,					**a__    ,					):
       super().__init__(**a__  )

       _lowerCAmelCase						:	Dict        =					image_size
       _lowerCAmelCase						:	List[str]        =					patch_size
       _lowerCAmelCase						:	Any        =					num_channels
       _lowerCAmelCase						:	int        =					embed_dim
       _lowerCAmelCase						:	Optional[Any]        =					depths
       _lowerCAmelCase						:	List[str]        =					len(a__  )
       _lowerCAmelCase						:	List[Any]        =					num_heads
       _lowerCAmelCase						:	Tuple        =					window_size
       _lowerCAmelCase						:	List[Any]        =					mlp_ratio
       _lowerCAmelCase						:	Optional[Any]        =					qkv_bias
       _lowerCAmelCase						:	int        =					hidden_dropout_prob
       _lowerCAmelCase						:	Union[str, Any]        =					attention_probs_dropout_prob
       _lowerCAmelCase						:	Any        =					drop_path_rate
       _lowerCAmelCase						:	Optional[Any]        =					hidden_act
       _lowerCAmelCase						:	Tuple        =					use_absolute_embeddings
       _lowerCAmelCase						:	str        =					layer_norm_eps
       _lowerCAmelCase						:	Any        =					initializer_range
       # we set the hidden_size attribute in order to make Swin work with VisionEncoderDecoderModel
       # this indicates the channel dimension after the last stage of the model
       _lowerCAmelCase						:	Union[str, Any]        =					int(embed_dim * 2 ** (len(a__  ) - 1)  )
       _lowerCAmelCase						:	int        =					["""stem"""] + [F"stage{idx}" for idx in range(1    ,					len(a__  ) + 1  )]
       _lowerCAmelCase      ,     _lowerCAmelCase						:	int        =					get_aligned_output_features_output_indices(
           out_features=a__    ,					out_indices=a__    ,					stage_names=self.stage_names  )




