






"""simple docstring"""



# This model implementation is heavily inspired by https://github.com/haofanwang/ControlNet-for-Diffusers/

import gc
import random
import tempfile
import unittest

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

from diffusers import (
    AutoencoderKL,
    ControlNetModel,
    DDIMScheduler,
    StableDiffusionControlNetImgaImgPipeline,
    UNetaDConditionModel,
)
from diffusers.pipelines.stable_diffusion.pipeline_stable_diffusion_controlnet import MultiControlNetModel
from diffusers.utils import floats_tensor, load_image, load_numpy, randn_tensor, slow, torch_device
from diffusers.utils.import_utils import is_xformers_available
from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu

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


enable_full_determinism()







class        lowercase__      (							snake_case__,       snake_case__,       snake_case__,       unittest.TestCase       ):
			_UpperCAmelCase		:Dict             =			StableDiffusionControlNetImgaImgPipeline
			_UpperCAmelCase		:Optional[int]             =			TEXT_GUIDED_IMAGE_VARIATION_PARAMS - {"height", "width"}
			_UpperCAmelCase		:Tuple             =			TEXT_GUIDED_IMAGE_VARIATION_BATCH_PARAMS
			_UpperCAmelCase		:Tuple             =			IMAGE_TO_IMAGE_IMAGE_PARAMS.union({"control_image"}       )
			_UpperCAmelCase		:Union[str, Any]             =			IMAGE_TO_IMAGE_IMAGE_PARAMS


			def      UpperCAmelCase__						(				self					:  Dict ):
										torch.manual_seed(0 )
										lowerCamelCase_	:							Tuple									=UNetaDConditionModel(
										    block_out_channels=(32, 64)       ,	layers_per_block=2       ,	sample_size=32       ,	in_channels=4       ,	out_channels=4       ,	down_block_types=("DownBlock2D", "CrossAttnDownBlock2D")       ,	up_block_types=("CrossAttnUpBlock2D", "UpBlock2D")       ,	cross_attention_dim=32       ,	)
										torch.manual_seed(0 )
										lowerCamelCase_	:							int									=ControlNetModel(
										    block_out_channels=(32, 64)       ,	layers_per_block=2       ,	in_channels=4       ,	down_block_types=("DownBlock2D", "CrossAttnDownBlock2D")       ,	cross_attention_dim=32       ,	conditioning_embedding_out_channels=(16, 32)       ,	)
										torch.manual_seed(0 )
										lowerCamelCase_	:							List[str]									=DDIMScheduler(
										    beta_start=0.00_085       ,	beta_end=0.012       ,	beta_schedule="scaled_linear"       ,	clip_sample=snake_case__       ,	set_alpha_to_one=snake_case__       ,	)
										torch.manual_seed(0 )
										lowerCamelCase_	:							str									=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_	:							Dict									=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_	:							List[str]									=CLIPTextModel(snake_case__ )
										lowerCamelCase_	:							List[Any]									=CLIPTokenizer.from_pretrained("hf-internal-testing/tiny-random-clip" )

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


			def      UpperCAmelCase__						(				self					:  Tuple       ,	snake_case__					:  Union[str, Any]       ,	snake_case__					:  List[str]=0 ):
										if str(snake_case__ ).startswith("mps" ):
																	lowerCamelCase_	:							Any									=torch.manual_seed(snake_case__ )
										else:
																	lowerCamelCase_	:							List[str]									=torch.Generator(device=snake_case__ ).manual_seed(snake_case__ )

										lowerCamelCase_	:							Any									=2
										lowerCamelCase_	:							Optional[Any]									=randn_tensor(
										    (1, 3, 32 * controlnet_embedder_scale_factor, 32 * controlnet_embedder_scale_factor)       ,	generator=snake_case__       ,	device=torch.device(snake_case__ )       ,	)
										lowerCamelCase_	:							str									=floats_tensor(control_image.shape       ,	rng=random.Random(snake_case__ ) ).to(snake_case__ )
										lowerCamelCase_	:							List[str]									=image.cpu().permute(0       ,	2       ,	3       ,	1 )[0]
										lowerCamelCase_	:							Optional[int]									=Image.fromarray(np.uinta(snake_case__ ) ).convert("RGB" ).resize((64, 64) )
										lowerCamelCase_	:							Optional[Any]									={
										    "prompt": "A painting of a squirrel eating a burger",
										    "generator": generator,
										    "num_inference_steps": 2,
										    "guidance_scale": 6.0,
										    "output_type": "numpy",
										    "image": image,
										    "control_image": control_image,
										}

										return inputs


			def      UpperCAmelCase__						(				self					:  Dict ):
										return self._test_attention_slicing_forward_pass(expected_max_diff=2E-3 )


			@unittest.skipIf(
			    torch_device != "cuda" or not is_xformers_available()       ,	reason="XFormers attention is only available with CUDA and `xformers` installed"       ,	)
			def      UpperCAmelCase__						(				self					:  Tuple ):
										self._test_xformers_attention_forwardGenerator_pass(expected_max_diff=2E-3 )


			def      UpperCAmelCase__						(				self					:  str ):
										self._test_inference_batch_single_identical(expected_max_diff=2E-3 )







class        lowercase__      (							snake_case__,       snake_case__,       unittest.TestCase       ):
			_UpperCAmelCase		:List[str]             =			StableDiffusionControlNetImgaImgPipeline
			_UpperCAmelCase		:str             =			TEXT_GUIDED_IMAGE_VARIATION_PARAMS - {"height", "width"}
			_UpperCAmelCase		:Dict             =			TEXT_GUIDED_IMAGE_VARIATION_BATCH_PARAMS
			_UpperCAmelCase		:Optional[int]             =			frozenset([]       )  # TO_DO: add image_params once refactored VaeImageProcessor.preprocess


			def      UpperCAmelCase__						(				self					:  str ):
										torch.manual_seed(0 )
										lowerCamelCase_	:							Optional[int]									=UNetaDConditionModel(
										    block_out_channels=(32, 64)       ,	layers_per_block=2       ,	sample_size=32       ,	in_channels=4       ,	out_channels=4       ,	down_block_types=("DownBlock2D", "CrossAttnDownBlock2D")       ,	up_block_types=("CrossAttnUpBlock2D", "UpBlock2D")       ,	cross_attention_dim=32       ,	)
										torch.manual_seed(0 )

										def init_weights(snake_case__					:  Optional[int] ):
																	if isinstance(snake_case__       ,	torch.nn.Convad ):
																								torch.nn.init.normal(m.weight )
																								m.bias.data.fill_(1.0 )

										lowerCamelCase_	:							List[str]									=ControlNetModel(
										    block_out_channels=(32, 64)       ,	layers_per_block=2       ,	in_channels=4       ,	down_block_types=("DownBlock2D", "CrossAttnDownBlock2D")       ,	cross_attention_dim=32       ,	conditioning_embedding_out_channels=(16, 32)       ,	)
										controlneta.controlnet_down_blocks.apply(snake_case__ )

										torch.manual_seed(0 )
										lowerCamelCase_	:							Any									=ControlNetModel(
										    block_out_channels=(32, 64)       ,	layers_per_block=2       ,	in_channels=4       ,	down_block_types=("DownBlock2D", "CrossAttnDownBlock2D")       ,	cross_attention_dim=32       ,	conditioning_embedding_out_channels=(16, 32)       ,	)
										controlneta.controlnet_down_blocks.apply(snake_case__ )

										torch.manual_seed(0 )
										lowerCamelCase_	:							Optional[int]									=DDIMScheduler(
										    beta_start=0.00_085       ,	beta_end=0.012       ,	beta_schedule="scaled_linear"       ,	clip_sample=snake_case__       ,	set_alpha_to_one=snake_case__       ,	)
										torch.manual_seed(0 )
										lowerCamelCase_	:							Any									=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_	:							List[str]									=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(snake_case__ )
										lowerCamelCase_	:							str									=CLIPTokenizer.from_pretrained("hf-internal-testing/tiny-random-clip" )

										lowerCamelCase_	:							List[str]									=MultiControlNetModel([controlneta, controlneta] )

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


			def      UpperCAmelCase__						(				self					:  int       ,	snake_case__					:  Dict       ,	snake_case__					:  Optional[int]=0 ):
										if str(snake_case__ ).startswith("mps" ):
																	lowerCamelCase_	:							Optional[int]									=torch.manual_seed(snake_case__ )
										else:
																	lowerCamelCase_	:							Optional[int]									=torch.Generator(device=snake_case__ ).manual_seed(snake_case__ )

										lowerCamelCase_	:							List[str]									=2

										lowerCamelCase_	:							Any									=[
										    randn_tensor(
										        (1, 3, 32 * controlnet_embedder_scale_factor, 32 * controlnet_embedder_scale_factor)       ,	generator=snake_case__       ,	device=torch.device(snake_case__ )       ,	),
										    randn_tensor(
										        (1, 3, 32 * controlnet_embedder_scale_factor, 32 * controlnet_embedder_scale_factor)       ,	generator=snake_case__       ,	device=torch.device(snake_case__ )       ,	),
										]

										lowerCamelCase_	:							Any									=floats_tensor(control_image[0].shape       ,	rng=random.Random(snake_case__ ) ).to(snake_case__ )
										lowerCamelCase_	:							int									=image.cpu().permute(0       ,	2       ,	3       ,	1 )[0]
										lowerCamelCase_	:							Optional[Any]									=Image.fromarray(np.uinta(snake_case__ ) ).convert("RGB" ).resize((64, 64) )
										lowerCamelCase_	:							Dict									={
										    "prompt": "A painting of a squirrel eating a burger",
										    "generator": generator,
										    "num_inference_steps": 2,
										    "guidance_scale": 6.0,
										    "output_type": "numpy",
										    "image": image,
										    "control_image": control_image,
										}

										return inputs


			def      UpperCAmelCase__						(				self					:  Union[str, Any] ):
										lowerCamelCase_	:							str									=self.get_dummy_components()
										lowerCamelCase_	:							Any									=self.pipeline_class(**snake_case__ )
										pipe.to(snake_case__ )

										lowerCamelCase_	:							int									=10.0
										lowerCamelCase_	:							Optional[int]									=4

										lowerCamelCase_	:							Optional[int]									=self.get_dummy_inputs(snake_case__ )
										lowerCamelCase_	:							str									=steps
										lowerCamelCase_	:							List[Any]									=scale
										lowerCamelCase_	:							Any									=pipe(**snake_case__ )[0]

										lowerCamelCase_	:							Tuple									=self.get_dummy_inputs(snake_case__ )
										lowerCamelCase_	:							Union[str, Any]									=steps
										lowerCamelCase_	:							Union[str, Any]									=scale
										lowerCamelCase_	:							Optional[Any]									=pipe(**snake_case__       ,	control_guidance_start=0.1       ,	control_guidance_end=0.2 )[0]

										lowerCamelCase_	:							Optional[Any]									=self.get_dummy_inputs(snake_case__ )
										lowerCamelCase_	:							str									=steps
										lowerCamelCase_	:							Optional[int]									=scale
										lowerCamelCase_	:							Union[str, Any]									=pipe(**snake_case__       ,	control_guidance_start=[0.1, 0.3]       ,	control_guidance_end=[0.2, 0.7] )[0]

										lowerCamelCase_	:							str									=self.get_dummy_inputs(snake_case__ )
										lowerCamelCase_	:							Optional[int]									=steps
										lowerCamelCase_	:							Union[str, Any]									=scale
										lowerCamelCase_	:							List[str]									=pipe(**snake_case__       ,	control_guidance_start=0.4       ,	control_guidance_end=[0.5, 0.8] )[0]

										# make sure that all outputs are different
										assert np.sum(np.abs(output_a - output_a ) ) > 1E-3
										assert np.sum(np.abs(output_a - output_a ) ) > 1E-3
										assert np.sum(np.abs(output_a - output_a ) ) > 1E-3


			def      UpperCAmelCase__						(				self					:  str ):
										return self._test_attention_slicing_forward_pass(expected_max_diff=2E-3 )


			@unittest.skipIf(
			    torch_device != "cuda" or not is_xformers_available()       ,	reason="XFormers attention is only available with CUDA and `xformers` installed"       ,	)
			def      UpperCAmelCase__						(				self					:  Optional[Any] ):
										self._test_xformers_attention_forwardGenerator_pass(expected_max_diff=2E-3 )


			def      UpperCAmelCase__						(				self					:  Optional[Any] ):
										self._test_inference_batch_single_identical(expected_max_diff=2E-3 )


			def      UpperCAmelCase__						(				self					:  Optional[int] ):
										lowerCamelCase_	:							List[str]									=self.get_dummy_components()
										lowerCamelCase_	:							str									=self.pipeline_class(**snake_case__ )
										pipe.to(snake_case__ )
										pipe.set_progress_bar_config(disable=snake_case__ )
										with tempfile.TemporaryDirectory() as tmpdir:
																	try:
																								# save_pretrained is not implemented for Multi-ControlNet
																								pipe.save_pretrained(snake_case__ )
																	except NotImplementedError:
																								pass







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


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


			def      UpperCAmelCase__						(				self					:  Union[str, Any] ):
										lowerCamelCase_	:							List[str]									=ControlNetModel.from_pretrained("lllyasviel/sd-controlnet-canny" )

										lowerCamelCase_	:							Union[str, Any]									=StableDiffusionControlNetImgaImgPipeline.from_pretrained(
										    "runwayml/stable-diffusion-v1-5"       ,	safety_checker=snake_case__       ,	controlnet=snake_case__ )
										pipe.enable_model_cpu_offload()
										pipe.set_progress_bar_config(disable=snake_case__ )

										lowerCamelCase_	:							Union[str, Any]									=torch.Generator(device="cpu" ).manual_seed(0 )
										lowerCamelCase_	:							Dict									="evil space-punk bird"
										lowerCamelCase_	:							Any									=load_image(
										    "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/sd_controlnet/bird_canny.png" ).resize((512, 512) )
										lowerCamelCase_	:							List[str]									=load_image(
										    "https://huggingface.co/lllyasviel/sd-controlnet-canny/resolve/main/images/bird.png" ).resize((512, 512) )

										lowerCamelCase_	:							Tuple									=pipe(
										    snake_case__       ,	snake_case__       ,	control_image=snake_case__       ,	generator=snake_case__       ,	output_type="np"       ,	num_inference_steps=50       ,	strength=0.6       ,	)

										lowerCamelCase_	:							str									=output.images[0]

										assert image.shape == (512, 512, 3)

										lowerCamelCase_	:							str									=load_numpy(
										    "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/sd_controlnet/img2img.npy" )

										assert np.abs(expected_image - image ).max() < 9E-2











"""simple docstring"""



import gc
import random
import unittest

import numpy as np
import torch
from transformers import XLMRobertaTokenizer

from diffusers import (
    AltDiffusionImgaImgPipeline,
    AutoencoderKL,
    PNDMScheduler,
    UNetaDConditionModel,
)
from diffusers.image_processor import VaeImageProcessor
from diffusers.pipelines.alt_diffusion.modeling_roberta_series import (
    RobertaSeriesConfig,
    RobertaSeriesModelWithTransformation,
)
from diffusers.utils import floats_tensor, load_image, load_numpy, slow, torch_device
from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu


enable_full_determinism()







class        lowercase__      (							unittest.TestCase       ):


			def      UpperCAmelCase__						(				self					:  Any ):
										# clean up the VRAM after each test
										super().tearDown()
										gc.collect()
										torch.cuda.empty_cache()


			@property
			def      UpperCAmelCase__						(				self					:  Dict ):
										lowerCamelCase_	:							Optional[Any]									=1
										lowerCamelCase_	:							Union[str, Any]									=3
										lowerCamelCase_	:							Dict									=(32, 32)

										lowerCamelCase_	:							List[Any]									=floats_tensor((batch_size, num_channels) + sizes       ,	rng=random.Random(0 ) ).to(snake_case__ )
										return image


			@property
			def      UpperCAmelCase__						(				self					:  Union[str, Any] ):
										torch.manual_seed(0 )
										lowerCamelCase_	:							Dict									=UNetaDConditionModel(
										    block_out_channels=(32, 64)       ,	layers_per_block=2       ,	sample_size=32       ,	in_channels=4       ,	out_channels=4       ,	down_block_types=("DownBlock2D", "CrossAttnDownBlock2D")       ,	up_block_types=("CrossAttnUpBlock2D", "UpBlock2D")       ,	cross_attention_dim=32       ,	)
										return model


			@property
			def      UpperCAmelCase__						(				self					:  List[Any] ):
										torch.manual_seed(0 )
										lowerCamelCase_	:							Union[str, Any]									=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       ,	)
										return model


			@property
			def      UpperCAmelCase__						(				self					:  Optional[Any] ):
										torch.manual_seed(0 )
										lowerCamelCase_	:							Any									=RobertaSeriesConfig(
										    hidden_size=32       ,	project_dim=32       ,	intermediate_size=37       ,	layer_norm_eps=1E-05       ,	num_attention_heads=4       ,	num_hidden_layers=5       ,	pad_token_id=1       ,	vocab_size=5006       ,	)
										return RobertaSeriesModelWithTransformation(snake_case__ )


			@property
			def      UpperCAmelCase__						(				self					:  int ):
										def extract(*snake_case__					:  Dict       ,	**snake_case__					:  int ):







																	class        lowercase__      :


																				def __init__(				self					:  Optional[Any] ):
																											lowerCamelCase_	:							Union[str, Any]									=torch.ones([0] )


																				def      UpperCAmelCase__						(				self					:  List[Any]       ,	snake_case__					:  Any ):
																											self.pixel_values.to(snake_case__ )
																											return self


																	return Out()

										return extract


			def      UpperCAmelCase__						(				self					:  Optional[Any] ):
										lowerCamelCase_	:							Dict									="cpu"  # ensure determinism for the device-dependent torch.Generator
										lowerCamelCase_	:							Dict									=self.dummy_cond_unet
										lowerCamelCase_	:							List[str]									=PNDMScheduler(skip_prk_steps=snake_case__ )
										lowerCamelCase_	:							List[Any]									=self.dummy_vae
										lowerCamelCase_	:							Any									=self.dummy_text_encoder
										lowerCamelCase_	:							List[Any]									=XLMRobertaTokenizer.from_pretrained("hf-internal-testing/tiny-xlm-roberta" )
										lowerCamelCase_	:							List[str]									=77

										lowerCamelCase_	:							Optional[int]									=self.dummy_image.to(snake_case__ )
										lowerCamelCase_	:							Any									=init_image / 2 + 0.5

										# make sure here that pndm scheduler skips prk
										lowerCamelCase_	:							List[Any]									=AltDiffusionImgaImgPipeline(
										    unet=snake_case__       ,	scheduler=snake_case__       ,	vae=snake_case__       ,	text_encoder=snake_case__       ,	tokenizer=snake_case__       ,	safety_checker=snake_case__       ,	feature_extractor=self.dummy_extractor       ,	)
										lowerCamelCase_	:							int									=VaeImageProcessor(vae_scale_factor=alt_pipe.vae_scale_factor       ,	do_normalize=snake_case__ )
										lowerCamelCase_	:							Optional[int]									=alt_pipe.to(snake_case__ )
										alt_pipe.set_progress_bar_config(disable=snake_case__ )

										lowerCamelCase_	:							Any									="A painting of a squirrel eating a burger"
										lowerCamelCase_	:							Union[str, Any]									=torch.Generator(device=snake_case__ ).manual_seed(0 )
										lowerCamelCase_	:							str									=alt_pipe(
										    [prompt]       ,	generator=snake_case__       ,	guidance_scale=6.0       ,	num_inference_steps=2       ,	output_type="np"       ,	image=snake_case__       ,	)

										lowerCamelCase_	:							List[Any]									=output.images

										lowerCamelCase_	:							str									=torch.Generator(device=snake_case__ ).manual_seed(0 )
										lowerCamelCase_	:							Optional[Any]									=alt_pipe(
										    [prompt]       ,	generator=snake_case__       ,	guidance_scale=6.0       ,	num_inference_steps=2       ,	output_type="np"       ,	image=snake_case__       ,	return_dict=snake_case__       ,	)[0]

										lowerCamelCase_	:							Tuple									=image[0, -3:, -3:, -1]
										lowerCamelCase_	:							Union[str, Any]									=image_from_tuple[0, -3:, -3:, -1]

										assert image.shape == (1, 32, 32, 3)
										lowerCamelCase_	:							List[str]									=np.array([0.4_427, 0.3_731, 0.4_249, 0.4_941, 0.4_546, 0.4_148, 0.4_193, 0.4_666, 0.4_499] )

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


			@unittest.skipIf(torch_device != "cuda"       ,	"This test requires a GPU" )
			def      UpperCAmelCase__						(				self					:  Tuple ):
										lowerCamelCase_	:							Dict									=self.dummy_cond_unet
										lowerCamelCase_	:							Any									=PNDMScheduler(skip_prk_steps=snake_case__ )
										lowerCamelCase_	:							Tuple									=self.dummy_vae
										lowerCamelCase_	:							Union[str, Any]									=self.dummy_text_encoder
										lowerCamelCase_	:							Optional[Any]									=XLMRobertaTokenizer.from_pretrained("hf-internal-testing/tiny-xlm-roberta" )
										lowerCamelCase_	:							Tuple									=77

										lowerCamelCase_	:							str									=self.dummy_image.to(snake_case__ )

										# put models in fp16
										lowerCamelCase_	:							Optional[Any]									=unet.half()
										lowerCamelCase_	:							Dict									=vae.half()
										lowerCamelCase_	:							str									=bert.half()

										# make sure here that pndm scheduler skips prk
										lowerCamelCase_	:							Optional[int]									=AltDiffusionImgaImgPipeline(
										    unet=snake_case__       ,	scheduler=snake_case__       ,	vae=snake_case__       ,	text_encoder=snake_case__       ,	tokenizer=snake_case__       ,	safety_checker=snake_case__       ,	feature_extractor=self.dummy_extractor       ,	)
										lowerCamelCase_	:							List[Any]									=VaeImageProcessor(vae_scale_factor=alt_pipe.vae_scale_factor       ,	do_normalize=snake_case__ )
										lowerCamelCase_	:							int									=alt_pipe.to(snake_case__ )
										alt_pipe.set_progress_bar_config(disable=snake_case__ )

										lowerCamelCase_	:							Tuple									="A painting of a squirrel eating a burger"
										lowerCamelCase_	:							Tuple									=torch.manual_seed(0 )
										lowerCamelCase_	:							List[Any]									=alt_pipe(
										    [prompt]       ,	generator=snake_case__       ,	num_inference_steps=2       ,	output_type="np"       ,	image=snake_case__       ,	).images

										assert image.shape == (1, 32, 32, 3)


			@unittest.skipIf(torch_device != "cuda"       ,	"This test requires a GPU" )
			def      UpperCAmelCase__						(				self					:  Union[str, Any] ):
										lowerCamelCase_	:							int									=load_image(
										    "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main"
										    "/img2img/sketch-mountains-input.jpg" )
										# resize to resolution that is divisible by 8 but not 16 or 32
										lowerCamelCase_	:							Any									=init_image.resize((760, 504) )

										lowerCamelCase_	:							List[str]									="BAAI/AltDiffusion"
										lowerCamelCase_	:							List[str]									=AltDiffusionImgaImgPipeline.from_pretrained(
										    snake_case__       ,	safety_checker=snake_case__       ,	)
										pipe.to(snake_case__ )
										pipe.set_progress_bar_config(disable=snake_case__ )
										pipe.enable_attention_slicing()

										lowerCamelCase_	:							Optional[int]									="A fantasy landscape, trending on artstation"

										lowerCamelCase_	:							List[Any]									=torch.manual_seed(0 )
										lowerCamelCase_	:							Optional[int]									=pipe(
										    prompt=snake_case__       ,	image=snake_case__       ,	strength=0.75       ,	guidance_scale=7.5       ,	generator=snake_case__       ,	output_type="np"       ,	)
										lowerCamelCase_	:							Optional[int]									=output.images[0]

										lowerCamelCase_	:							Tuple									=image[255:258, 383:386, -1]

										assert image.shape == (504, 760, 3)
										lowerCamelCase_	:							Optional[int]									=np.array([0.9_358, 0.9_397, 0.9_599, 0.9_901, 1.0_000, 1.0_000, 0.9_882, 1.0_000, 1.0_000] )

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







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


			def      UpperCAmelCase__						(				self					:  str ):
										# clean up the VRAM after each test
										super().tearDown()
										gc.collect()
										torch.cuda.empty_cache()


			def      UpperCAmelCase__						(				self					:  List[Any] ):
										lowerCamelCase_	:							str									=load_image(
										    "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main"
										    "/img2img/sketch-mountains-input.jpg" )
										lowerCamelCase_	:							Any									=init_image.resize((768, 512) )
										lowerCamelCase_	:							Optional[int]									=load_numpy(
										    "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/img2img/fantasy_landscape_alt.npy" )

										lowerCamelCase_	:							Dict									="BAAI/AltDiffusion"
										lowerCamelCase_	:							List[str]									=AltDiffusionImgaImgPipeline.from_pretrained(
										    snake_case__       ,	safety_checker=snake_case__       ,	)
										pipe.to(snake_case__ )
										pipe.set_progress_bar_config(disable=snake_case__ )
										pipe.enable_attention_slicing()

										lowerCamelCase_	:							Optional[Any]									="A fantasy landscape, trending on artstation"

										lowerCamelCase_	:							Dict									=torch.manual_seed(0 )
										lowerCamelCase_	:							int									=pipe(
										    prompt=snake_case__       ,	image=snake_case__       ,	strength=0.75       ,	guidance_scale=7.5       ,	generator=snake_case__       ,	output_type="np"       ,	)
										lowerCamelCase_	:							int									=output.images[0]

										assert image.shape == (512, 768, 3)
										# img2img is flaky across GPUs even in fp32, so using MAE here
										assert np.abs(expected_image - image ).max() < 1E-2



