


'''simple docstring'''







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 					__a      (lowerCamelCase       ,		lowerCamelCase       ,		lowerCamelCase       ,		unittest.TestCase     ):
							__a					:   Dict									=       StableDiffusionControlNetImgaImgPipeline
							__a					:   Optional[int]									=       TEXT_GUIDED_IMAGE_VARIATION_PARAMS - {"height", "width"}
							__a					:   str									=       TEXT_GUIDED_IMAGE_VARIATION_BATCH_PARAMS
							__a					:   Union[str, Any]									=       IMAGE_TO_IMAGE_IMAGE_PARAMS.union({"control_image"}     )
							__a					:   str									=       IMAGE_TO_IMAGE_IMAGE_PARAMS

							def     UpperCAmelCase__	(							self							:     Tuple   )					->       int:







													"""simple docstring"""
													torch.manual_seed(0   )
													UpperCAmelCase_			:      Union[str, Any]     						=  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   )
													UpperCAmelCase_			:      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)       ,     )
													torch.manual_seed(0   )
													UpperCAmelCase_			:      str     						=  DDIMScheduler(
													    beta_start=0.0_0_0_8_5       ,     beta_end=0.0_1_2       ,     beta_schedule='''scaled_linear'''       ,     clip_sample=__magic_name__       ,     set_alpha_to_one=__magic_name__       ,     )
													torch.manual_seed(0   )
													UpperCAmelCase_			:      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   )
													UpperCAmelCase_			:      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=10_00       ,     )
													UpperCAmelCase_			:      List[Any]     						=  CLIPTextModel(__magic_name__   )
													UpperCAmelCase_			:      Any     						=  CLIPTokenizer.from_pretrained('''hf-internal-testing/tiny-random-clip'''   )

													UpperCAmelCase_			:      int     						=  {
													    '''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							:     Optional[int]       ,     __magic_name__							:     int       ,     __magic_name__							:     Union[str, Any]=0   )					->       List[str]:







													"""simple docstring"""
													if str(__magic_name__   ).startswith('''mps'''   ):
																			UpperCAmelCase_			:      str     						=  torch.manual_seed(__magic_name__   )
													else:
																			UpperCAmelCase_			:      Optional[int]     						=  torch.Generator(device=__magic_name__   ).manual_seed(__magic_name__   )

													UpperCAmelCase_			:      List[Any]     						=  2
													UpperCAmelCase_			:      Tuple     						=  randn_tensor(
													    (1, 3, 32 * controlnet_embedder_scale_factor, 32 * controlnet_embedder_scale_factor)       ,     generator=__magic_name__       ,     device=torch.device(__magic_name__   )       ,     )
													UpperCAmelCase_			:      List[str]     						=  floats_tensor(control_image.shape       ,     rng=random.Random(__magic_name__   )   ).to(__magic_name__   )
													UpperCAmelCase_			:      Dict     						=  image.cpu().permute(0       ,     2       ,     3       ,     1   )[0]
													UpperCAmelCase_			:      Optional[int]     						=  Image.fromarray(np.uinta(__magic_name__   )   ).convert('''RGB'''   ).resize((64, 64)   )
													UpperCAmelCase_			:      int     						=  {
													    '''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							:     List[str]   )					->       List[str]:







													"""simple docstring"""
													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]   )					->       str:







													"""simple docstring"""
													self._test_xformers_attention_forwardGenerator_pass(expected_max_diff=2E-3   )



							def     UpperCAmelCase__	(							self							:     int   )					->       Dict:







													"""simple docstring"""
													self._test_inference_batch_single_identical(expected_max_diff=2E-3   )





class 					__a      (lowerCamelCase       ,		lowerCamelCase       ,		unittest.TestCase     ):
							__a					:   str									=       StableDiffusionControlNetImgaImgPipeline
							__a					:   Tuple									=       TEXT_GUIDED_IMAGE_VARIATION_PARAMS - {"height", "width"}
							__a					:   Optional[Any]									=       TEXT_GUIDED_IMAGE_VARIATION_BATCH_PARAMS
							__a					:   List[Any]									=       frozenset([]     )  # TO_DO: add image_params once refactored VaeImageProcessor.preprocess

							def     UpperCAmelCase__	(							self							:     int   )					->       int:







													"""simple docstring"""
													torch.manual_seed(0   )
													UpperCAmelCase_			:      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(__magic_name__							:     str   ):
																			if isinstance(__magic_name__       ,     torch.nn.Convad   ):
																									torch.nn.init.normal(m.weight   )
																									m.bias.data.fill_(1.0   )

													UpperCAmelCase_			:      Optional[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(__magic_name__   )

													torch.manual_seed(0   )
													UpperCAmelCase_			:      Optional[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)       ,     )
													controlneta.controlnet_down_blocks.apply(__magic_name__   )

													torch.manual_seed(0   )
													UpperCAmelCase_			:      Optional[Any]     						=  DDIMScheduler(
													    beta_start=0.0_0_0_8_5       ,     beta_end=0.0_1_2       ,     beta_schedule='''scaled_linear'''       ,     clip_sample=__magic_name__       ,     set_alpha_to_one=__magic_name__       ,     )
													torch.manual_seed(0   )
													UpperCAmelCase_			:      Optional[int]     						=  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   )
													UpperCAmelCase_			:      Optional[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=10_00       ,     )
													UpperCAmelCase_			:      Any     						=  CLIPTextModel(__magic_name__   )
													UpperCAmelCase_			:      Tuple     						=  CLIPTokenizer.from_pretrained('''hf-internal-testing/tiny-random-clip'''   )

													UpperCAmelCase_			:      Tuple     						=  MultiControlNetModel([controlneta, controlneta]   )

													UpperCAmelCase_			:      Union[str, Any]     						=  {
													    '''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							:     List[str]       ,     __magic_name__							:     List[Any]       ,     __magic_name__							:     Optional[int]=0   )					->       List[Any]:







													"""simple docstring"""
													if str(__magic_name__   ).startswith('''mps'''   ):
																			UpperCAmelCase_			:      Any     						=  torch.manual_seed(__magic_name__   )
													else:
																			UpperCAmelCase_			:      Optional[Any]     						=  torch.Generator(device=__magic_name__   ).manual_seed(__magic_name__   )

													UpperCAmelCase_			:      Union[str, Any]     						=  2

													UpperCAmelCase_			:      Tuple     						=  [
													    randn_tensor(
													        (1, 3, 32 * controlnet_embedder_scale_factor, 32 * controlnet_embedder_scale_factor)       ,     generator=__magic_name__       ,     device=torch.device(__magic_name__   )       ,     ),
													    randn_tensor(
													        (1, 3, 32 * controlnet_embedder_scale_factor, 32 * controlnet_embedder_scale_factor)       ,     generator=__magic_name__       ,     device=torch.device(__magic_name__   )       ,     ),
													]

													UpperCAmelCase_			:      Any     						=  floats_tensor(control_image[0].shape       ,     rng=random.Random(__magic_name__   )   ).to(__magic_name__   )
													UpperCAmelCase_			:      Union[str, Any]     						=  image.cpu().permute(0       ,     2       ,     3       ,     1   )[0]
													UpperCAmelCase_			:      List[Any]     						=  Image.fromarray(np.uinta(__magic_name__   )   ).convert('''RGB'''   ).resize((64, 64)   )
													UpperCAmelCase_			:      List[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							:     List[str]   )					->       Optional[int]:







													"""simple docstring"""
													UpperCAmelCase_			:      Optional[int]     						=  self.get_dummy_components()
													UpperCAmelCase_			:      List[str]     						=  self.pipeline_class(**__magic_name__   )
													pipe.to(__magic_name__   )

													UpperCAmelCase_			:      List[Any]     						=  10.0
													UpperCAmelCase_			:      Optional[int]     						=  4

													UpperCAmelCase_			:      Optional[Any]     						=  self.get_dummy_inputs(__magic_name__   )
													UpperCAmelCase_			:      Tuple     						=  steps
													UpperCAmelCase_			:      List[Any]     						=  scale
													UpperCAmelCase_			:      str     						=  pipe(**__magic_name__   )[0]

													UpperCAmelCase_			:      Any     						=  self.get_dummy_inputs(__magic_name__   )
													UpperCAmelCase_			:      Dict     						=  steps
													UpperCAmelCase_			:      Dict     						=  scale
													UpperCAmelCase_			:      str     						=  pipe(**__magic_name__       ,     control_guidance_start=0.1       ,     control_guidance_end=0.2   )[0]

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

													UpperCAmelCase_			:      Union[str, Any]     						=  self.get_dummy_inputs(__magic_name__   )
													UpperCAmelCase_			:      Optional[int]     						=  steps
													UpperCAmelCase_			:      List[Any]     						=  scale
													UpperCAmelCase_			:      Union[str, Any]     						=  pipe(**__magic_name__       ,     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							:     int   )					->       Tuple:







													"""simple docstring"""
													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]   )					->       List[Any]:







													"""simple docstring"""
													self._test_xformers_attention_forwardGenerator_pass(expected_max_diff=2E-3   )

							def     UpperCAmelCase__	(							self							:     List[Any]   )					->       Dict:







													"""simple docstring"""
													self._test_inference_batch_single_identical(expected_max_diff=2E-3   )



							def     UpperCAmelCase__	(							self							:     Union[str, Any]   )					->       Dict:







													"""simple docstring"""
													UpperCAmelCase_			:      Optional[int]     						=  self.get_dummy_components()
													UpperCAmelCase_			:      str     						=  self.pipeline_class(**__magic_name__   )
													pipe.to(__magic_name__   )
													pipe.set_progress_bar_config(disable=__magic_name__   )
													with tempfile.TemporaryDirectory() as tmpdir:
																			try:
																									# save_pretrained is not implemented for Multi-ControlNet
																									pipe.save_pretrained(__magic_name__   )
																			except NotImplementedError:
																									pass





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

							def     UpperCAmelCase__	(							self							:     Optional[int]   )					->       Union[str, Any]:







													"""simple docstring"""
													super().tearDown()
													gc.collect()
													torch.cuda.empty_cache()



							def     UpperCAmelCase__	(							self							:     int   )					->       List[str]:







													"""simple docstring"""
													UpperCAmelCase_			:      Optional[int]     						=  ControlNetModel.from_pretrained('''lllyasviel/sd-controlnet-canny'''   )

													UpperCAmelCase_			:      Any     						=  StableDiffusionControlNetImgaImgPipeline.from_pretrained(
													    '''runwayml/stable-diffusion-v1-5'''       ,     safety_checker=__magic_name__       ,     controlnet=__magic_name__   )
													pipe.enable_model_cpu_offload()
													pipe.set_progress_bar_config(disable=__magic_name__   )

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

													UpperCAmelCase_			:      List[Any]     						=  pipe(
													    __magic_name__       ,     __magic_name__       ,     control_image=__magic_name__       ,     generator=__magic_name__       ,     output_type='''np'''       ,     num_inference_steps=50       ,     strength=0.6       ,     )

													UpperCAmelCase_			:      Tuple     						=  output.images[0]

													assert image.shape == (5_12, 5_12, 3)

													UpperCAmelCase_			:      Optional[int]     						=  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'''







class 					__a      :

							def __init__(							self							:     List[Any]       ,     __magic_name__							:     int   )					->       None:







													"""simple docstring"""
													UpperCAmelCase_			:      Optional[Any]     						=  size
													UpperCAmelCase_			:      Tuple     						=  [0] * size
													UpperCAmelCase_			:      Optional[Any]     						=  [0] * size

							@staticmethod
							def     UpperCAmelCase__	(							__magic_name__							:     int   )					->       int:







													"""simple docstring"""
													return index | (index + 1)

							@staticmethod
							def     UpperCAmelCase__	(							__magic_name__							:     int   )					->       int:







													"""simple docstring"""
													return (index & (index + 1)) - 1

							def     UpperCAmelCase__	(							self							:     Optional[int]       ,     __magic_name__							:     int       ,     __magic_name__							:     int   )					->       None:







													"""simple docstring"""
													UpperCAmelCase_			:      int     						=  value
													while index < self.size:
																			UpperCAmelCase_			:      str     						=  self.get_prev(__magic_name__   ) + 1
																			if current_left_border == index:
																									UpperCAmelCase_			:      List[str]     						=  value
																			else:
																									UpperCAmelCase_			:      Optional[int]     						=  max(__magic_name__       ,     __magic_name__       ,     __magic_name__   )
																			UpperCAmelCase_			:      Tuple     						=  self.get_next(__magic_name__   )



							def     UpperCAmelCase__	(							self							:     Any       ,     __magic_name__							:     int       ,     __magic_name__							:     int   )					->       int:







													"""simple docstring"""
													right -= 1  # Because of right is exclusive
													UpperCAmelCase_			:      List[str]     						=  0
													while left <= right:
																			UpperCAmelCase_			:      Optional[Any]     						=  self.get_prev(__magic_name__   )
																			if left <= current_left:
																									UpperCAmelCase_			:      Dict     						=  max(__magic_name__       ,     self.tree[right]   )
																									UpperCAmelCase_			:      Optional[Any]     						=  current_left
																			else:
																									UpperCAmelCase_			:      str     						=  max(__magic_name__       ,     self.arr[right]   )
																									right -= 1
													return result


if __name__ == "__main__":
					import doctest

					doctest.testmod()





