






'''simple docstring'''

import gc
import random
import unittest

import numpy as np
import torch

from diffusers import (
    DDIMScheduler,
    KandinskyVaaControlnetPipeline,
    KandinskyVaaPriorPipeline,
    UNetaDConditionModel,
    VQModel,
)
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

from ..test_pipelines_common import PipelineTesterMixin, assert_mean_pixel_difference


enable_full_determinism()




class      A     (   SCREAMING_SNAKE_CASE__, unittest.TestCase    ):






 """simple docstring"""





 __a      :    List[str]			  =    KandinskyVaaControlnetPipeline
 __a      :    str			  =    ['''image_embeds''', '''negative_image_embeds''', '''hint''']
 __a      :    Union[str, Any]			  =    ['''image_embeds''', '''negative_image_embeds''', '''hint''']
 __a      :    Any			  =    [
     '''generator''',
     '''height''',
     '''width''',
     '''latents''',
     '''guidance_scale''',
     '''num_inference_steps''',
     '''return_dict''',
     '''guidance_scale''',
     '''num_images_per_prompt''',
     '''output_type''',
     '''return_dict''',
 ]
 __a      :    List[Any]			  =    False
 @property
 def 	_UpperCAmelCase							(				self			):
       return 32
 @property
 def 	_UpperCAmelCase							(				self			):
       return 32
 @property
 def 	_UpperCAmelCase							(				self			):
       return self.time_input_dim
 @property
 def 	_UpperCAmelCase							(				self			):
       return self.time_input_dim * 4
 @property
 def 	_UpperCAmelCase							(				self			):
       return 1_00
 @property
 def 	_UpperCAmelCase							(				self			):
       torch.manual_seed(0			)

       UpperCamelCase_					: Any            =				{
           """in_channels""": 8,
           # Out channels is double in channels because predicts mean and variance
           """out_channels""": 8,
           """addition_embed_type""": """image_hint""",
           """down_block_types""": ("""ResnetDownsampleBlock2D""", """SimpleCrossAttnDownBlock2D"""),
           """up_block_types""": ("""SimpleCrossAttnUpBlock2D""", """ResnetUpsampleBlock2D"""),
           """mid_block_type""": """UNetMidBlock2DSimpleCrossAttn""",
           """block_out_channels""": (self.block_out_channels_a, self.block_out_channels_a * 2),
           """layers_per_block""": 1,
           """encoder_hid_dim""": self.text_embedder_hidden_size,
           """encoder_hid_dim_type""": """image_proj""",
           """cross_attention_dim""": self.cross_attention_dim,
           """attention_head_dim""": 4,
           """resnet_time_scale_shift""": """scale_shift""",
           """class_embed_type""": None,
       }

       UpperCamelCase_					: Dict            =				UNetaDConditionModel(**__lowerCAmelCase			)
       return model
 @property
 def 	_UpperCAmelCase							(				self			):
       return {
           "block_out_channels": [32, 32, 64, 64],
           "down_block_types": [
               "DownEncoderBlock2D",
               "DownEncoderBlock2D",
               "DownEncoderBlock2D",
               "AttnDownEncoderBlock2D",
           ],
           "in_channels": 3,
           "latent_channels": 4,
           "layers_per_block": 1,
           "norm_num_groups": 8,
           "norm_type": "spatial",
           "num_vq_embeddings": 12,
           "out_channels": 3,
           "up_block_types": ["AttnUpDecoderBlock2D", "UpDecoderBlock2D", "UpDecoderBlock2D", "UpDecoderBlock2D"],
           "vq_embed_dim": 4,
       }
 @property
 def 	_UpperCAmelCase							(				self			):
       torch.manual_seed(0			)
       UpperCamelCase_					: Optional[Any]            =				VQModel(**self.dummy_movq_kwargs			)
       return model
 def 	_UpperCAmelCase							(				self			):
       UpperCamelCase_					: Dict            =				self.dummy_unet
       UpperCamelCase_					: Tuple            =				self.dummy_movq

       UpperCamelCase_					: int            =				DDIMScheduler(
           num_train_timesteps=10_00      ,  beta_schedule="""linear"""      ,  beta_start=0.0_00_85      ,  beta_end=0.0_12      ,  clip_sample=__lowerCAmelCase      ,  set_alpha_to_one=__lowerCAmelCase      ,  steps_offset=1      ,  prediction_type="""epsilon"""      ,  thresholding=__lowerCAmelCase      ,  )

       UpperCamelCase_					: str            =				{
           """unet""": unet,
           """scheduler""": scheduler,
           """movq""": movq,
       }
       return components
 def 	_UpperCAmelCase							(				self      ,  __lowerCAmelCase      ,  __lowerCAmelCase=0			):
       UpperCamelCase_					: Dict            =				floats_tensor((1, self.text_embedder_hidden_size)      ,  rng=random.Random(__lowerCAmelCase			)			).to(__lowerCAmelCase			)
       UpperCamelCase_					: Optional[int]            =				floats_tensor((1, self.text_embedder_hidden_size)      ,  rng=random.Random(seed + 1			)			).to(
           __lowerCAmelCase			)

       # create hint
       UpperCamelCase_					: List[str]            =				floats_tensor((1, 3, 64, 64)      ,  rng=random.Random(__lowerCAmelCase			)			).to(__lowerCAmelCase			)

       if str(__lowerCAmelCase			).startswith("""mps"""			):
             UpperCamelCase_					: Optional[Any]            =				torch.manual_seed(__lowerCAmelCase			)
       else:
             UpperCamelCase_					: List[Any]            =				torch.Generator(device=__lowerCAmelCase			).manual_seed(__lowerCAmelCase			)
       UpperCamelCase_					: List[Any]            =				{
           """image_embeds""": image_embeds,
           """negative_image_embeds""": negative_image_embeds,
           """hint""": hint,
           """generator""": generator,
           """height""": 64,
           """width""": 64,
           """guidance_scale""": 4.0,
           """num_inference_steps""": 2,
           """output_type""": """np""",
       }
       return inputs





 def 	_UpperCAmelCase							(				self			):
       UpperCamelCase_					: Any            =				"""cpu"""

       UpperCamelCase_					: Union[str, Any]            =				self.get_dummy_components()

       UpperCamelCase_					: List[Any]            =				self.pipeline_class(**__lowerCAmelCase			)
       UpperCamelCase_					: List[Any]            =				pipe.to(__lowerCAmelCase			)

       pipe.set_progress_bar_config(disable=__lowerCAmelCase			)

       UpperCamelCase_					: Optional[int]            =				pipe(**self.get_dummy_inputs(__lowerCAmelCase			)			)
       UpperCamelCase_					: int            =				output.images

       UpperCamelCase_					: Optional[int]            =				pipe(
           **self.get_dummy_inputs(__lowerCAmelCase			)      ,  return_dict=__lowerCAmelCase      ,  )[0]

       UpperCamelCase_					: Optional[Any]            =				image[0, -3:, -3:, -1]
       UpperCamelCase_					: Tuple            =				image_from_tuple[0, -3:, -3:, -1]

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

       UpperCamelCase_					: List[Any]            =				np.array(
           [0.6_95_98_26, 0.86_82_79, 0.7_55_80_92, 0.68_76_94_67, 0.85_80_58_04, 0.65_97_74_96, 0.44_88_53_02, 0.5_95_91_11, 0.4_25_15_95]			)

       assert (
           np.abs(image_slice.flatten() - expected_slice			).max() < 1E-2
       ), F" expected_slice {expected_slice}, but got {image_slice.flatten()}"

       assert (
           np.abs(image_from_tuple_slice.flatten() - expected_slice			).max() < 1E-2
       ), F" expected_slice {expected_slice}, but got {image_from_tuple_slice.flatten()}"






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






 """simple docstring"""





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





 def 	_UpperCAmelCase							(				self			):
       UpperCamelCase_					: Dict            =				load_numpy(
           """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main"""
           """/kandinskyv22/kandinskyv22_controlnet_robotcat_fp16.npy"""			)

       UpperCamelCase_					: Dict            =				load_image(
           """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main"""
           """/kandinskyv22/hint_image_cat.png"""			)
       UpperCamelCase_					: Tuple            =				torch.from_numpy(np.array(__lowerCAmelCase			)			).float() / 2_55.0
       UpperCamelCase_					: Optional[int]            =				hint.permute(2      ,  0      ,  1			).unsqueeze(0			)

       UpperCamelCase_					: int            =				KandinskyVaaPriorPipeline.from_pretrained(
           """kandinsky-community/kandinsky-2-2-prior"""      ,  torch_dtype=torch.floataa			)
       pipe_prior.to(__lowerCAmelCase			)

       UpperCamelCase_					: int            =				KandinskyVaaControlnetPipeline.from_pretrained(
           """kandinsky-community/kandinsky-2-2-controlnet-depth"""      ,  torch_dtype=torch.floataa			)
       UpperCamelCase_					: Tuple            =				pipeline.to(__lowerCAmelCase			)
       pipeline.set_progress_bar_config(disable=__lowerCAmelCase			)

       UpperCamelCase_					: Tuple            =				"""A robot, 4k photo"""

       UpperCamelCase_					: Optional[Any]            =				torch.Generator(device="""cuda"""			).manual_seed(0			)
       UpperCamelCase_     ,      UpperCamelCase_					: Tuple            =				pipe_prior(
           __lowerCAmelCase      ,  generator=__lowerCAmelCase      ,  num_inference_steps=5      ,  negative_prompt=""""""      ,  ).to_tuple()

       UpperCamelCase_					: str            =				torch.Generator(device="""cuda"""			).manual_seed(0			)
       UpperCamelCase_					: Optional[int]            =				pipeline(
           image_embeds=__lowerCAmelCase      ,  negative_image_embeds=__lowerCAmelCase      ,  hint=__lowerCAmelCase      ,  generator=__lowerCAmelCase      ,  num_inference_steps=1_00      ,  output_type="""np"""      ,  )

       UpperCamelCase_					: str            =				output.images[0]

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

       assert_mean_pixel_difference(__lowerCAmelCase      ,  __lowerCAmelCase			)














'''simple docstring'''

from ...utils import (
    OptionalDependencyNotAvailable,
    is_flax_available,
    is_torch_available,
    is_transformers_available,
)


try:
      if not (is_transformers_available() and is_torch_available()):
            raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
      from ...utils.dummy_torch_and_transformers_objects import *  # noqa F403
else:
      from .multicontrolnet import MultiControlNetModel
      from .pipeline_controlnet import StableDiffusionControlNetPipeline
      from .pipeline_controlnet_imgaimg import StableDiffusionControlNetImgaImgPipeline
      from .pipeline_controlnet_inpaint import StableDiffusionControlNetInpaintPipeline


if is_transformers_available() and is_flax_available():
      from .pipeline_flax_controlnet import FlaxStableDiffusionControlNetPipeline






