"""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 (						lowerCAmelCase_		,							unittest.TestCase      ):
    _snake_case   :     Tuple   							=							KandinskyVaaControlnetPipeline
    _snake_case   :     Optional[Any]   							=							['image_embeds', 'negative_image_embeds', 'hint']
    _snake_case   :     Any   							=							['image_embeds', 'negative_image_embeds', 'hint']
    _snake_case   :     Any   							=							[
        'generator',
        'height',
        'width',
        'latents',
        'guidance_scale',
        'num_inference_steps',
        'return_dict',
        'guidance_scale',
        'num_images_per_prompt',
        'output_type',
        'return_dict',
    ]
    _snake_case   :     Tuple   							=							False


    @property
    def    lowerCAmelCase_     (    self		:     List[Any]    ):
      return 32


    @property
    def    lowerCAmelCase_     (    self		:     Optional[int]    ):
      return 32


    @property
    def    lowerCAmelCase_     (    self		:     Dict    ):
      return self.time_input_dim


    @property
    def    lowerCAmelCase_     (    self		:     Optional[Any]    ):
      return self.time_input_dim * 4


    @property
    def    lowerCAmelCase_     (    self		:     List[str]    ):
      return 100


    @property
    def    lowerCAmelCase_     (    self		:     int    ):
      torch.manual_seed(0    )

      _UpperCAmelCase         =					{
          """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         =					UNetaDConditionModel(**__lowerCAmelCase    )
      return model


    @property
    def    lowerCAmelCase_     (    self		:     Dict    ):
      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    lowerCAmelCase_     (    self		:     Union[str, Any]    ):
      torch.manual_seed(0    )
      _UpperCAmelCase         =					VQModel(**self.dummy_movq_kwargs    )
      return model


    def    lowerCAmelCase_     (    self		:     Any    ):
      _UpperCAmelCase         =					self.dummy_unet
      _UpperCAmelCase         =					self.dummy_movq

      _UpperCAmelCase         =					DDIMScheduler(
          num_train_timesteps=1000      , beta_schedule="""linear"""      , beta_start=0.00_085      , beta_end=0.012      , clip_sample=__lowerCAmelCase      , set_alpha_to_one=__lowerCAmelCase      , steps_offset=1      , prediction_type="""epsilon"""      , thresholding=__lowerCAmelCase      , )

      _UpperCAmelCase         =					{
          """unet""": unet,
          """scheduler""": scheduler,
          """movq""": movq,
      }
      return components


    def    lowerCAmelCase_     (    self		:     Union[str, Any]      , __lowerCAmelCase		:     Union[str, Any]      , __lowerCAmelCase		:     str=0    ):
      _UpperCAmelCase         =					floats_tensor((1, self.text_embedder_hidden_size)      , rng=random.Random(__lowerCAmelCase    )    ).to(__lowerCAmelCase    )
      _UpperCAmelCase         =					floats_tensor((1, self.text_embedder_hidden_size)      , rng=random.Random(seed + 1    )    ).to(
          __lowerCAmelCase    )

      # create hint
      _UpperCAmelCase         =					floats_tensor((1, 3, 64, 64)      , rng=random.Random(__lowerCAmelCase    )    ).to(__lowerCAmelCase    )

      if str(__lowerCAmelCase    ).startswith("""mps"""    ):
        _UpperCAmelCase         =					torch.manual_seed(__lowerCAmelCase    )
      else:
        _UpperCAmelCase         =					torch.Generator(device=__lowerCAmelCase    ).manual_seed(__lowerCAmelCase    )
      _UpperCAmelCase         =					{
          """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    lowerCAmelCase_     (    self		:     Dict    ):
      _UpperCAmelCase         =					"""cpu"""

      _UpperCAmelCase         =					self.get_dummy_components()

      _UpperCAmelCase         =					self.pipeline_class(**__lowerCAmelCase    )
      _UpperCAmelCase         =					pipe.to(__lowerCAmelCase    )

      pipe.set_progress_bar_config(disable=__lowerCAmelCase    )

      _UpperCAmelCase         =					pipe(**self.get_dummy_inputs(__lowerCAmelCase    )    )
      _UpperCAmelCase         =					output.images

      _UpperCAmelCase         =					pipe(
          **self.get_dummy_inputs(__lowerCAmelCase    )      , return_dict=__lowerCAmelCase      , )[0]

      _UpperCAmelCase         =					image[0, -3:, -3:, -1]
      _UpperCAmelCase         =					image_from_tuple[0, -3:, -3:, -1]

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

      _UpperCAmelCase         =					np.array(
          [0.6_959_826, 0.868_279, 0.7_558_092, 0.68_769_467, 0.85_805_804, 0.65_977_496, 0.44_885_302, 0.5_959_111, 0.4_251_595]    )

      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      ):


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


    def    lowerCAmelCase_     (    self		:     List[str]    ):
      _UpperCAmelCase         =					load_numpy(
          """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main"""
          """/kandinskyv22/kandinskyv22_controlnet_robotcat_fp16.npy"""    )

      _UpperCAmelCase         =					load_image(
          """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main"""
          """/kandinskyv22/hint_image_cat.png"""    )
      _UpperCAmelCase         =					torch.from_numpy(np.array(__lowerCAmelCase    )    ).float() / 255.0
      _UpperCAmelCase         =					hint.permute(2      , 0      , 1    ).unsqueeze(0    )

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

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

      _UpperCAmelCase         =					"""A robot, 4k photo"""

      _UpperCAmelCase         =					torch.Generator(device="""cuda"""    ).manual_seed(0    )
      _UpperCAmelCase       , _UpperCAmelCase         =					pipe_prior(
          __lowerCAmelCase      , generator=__lowerCAmelCase      , num_inference_steps=5      , negative_prompt=""""""      , ).to_tuple()

      _UpperCAmelCase         =					torch.Generator(device="""cuda"""    ).manual_seed(0    )
      _UpperCAmelCase         =					pipeline(
          image_embeds=__lowerCAmelCase      , negative_image_embeds=__lowerCAmelCase      , hint=__lowerCAmelCase      , generator=__lowerCAmelCase      , num_inference_steps=100      , output_type="""np"""      , )

      _UpperCAmelCase         =					output.images[0]

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

      assert_mean_pixel_difference(__lowerCAmelCase      , __lowerCAmelCase    )

"""simple docstring"""


import gc
import math
import unittest

import torch

from diffusers import UNetaDModel
from diffusers.utils import floats_tensor, logging, slow, torch_all_close, torch_device
from diffusers.utils.testing_utils import enable_full_determinism

from .test_modeling_common import ModelTesterMixin, UNetTesterMixin


UpperCAmelCase__     						=  logging.get_logger(__name__)

enable_full_determinism()


class 				a (						lowerCAmelCase_		,							lowerCAmelCase_		,							unittest.TestCase      ):
    _snake_case   :     Optional[int]   							=							UNetaDModel
    _snake_case   :     List[str]   							=							'sample'


    @property
    def    lowerCAmelCase_     (    self		:     List[str]    ):
      _UpperCAmelCase         =					4
      _UpperCAmelCase         =					3
      _UpperCAmelCase         =					(32, 32)

      _UpperCAmelCase         =					floats_tensor((batch_size, num_channels) + sizes    ).to(__lowerCAmelCase    )
      _UpperCAmelCase         =					torch.tensor([10]    ).to(__lowerCAmelCase    )

      return {"sample": noise, "timestep": time_step}


    @property
    def    lowerCAmelCase_     (    self		:     List[Any]    ):
      return (3, 32, 32)


    @property
    def    lowerCAmelCase_     (    self		:     Optional[Any]    ):
      return (3, 32, 32)


    def    lowerCAmelCase_     (    self		:     Any    ):
      _UpperCAmelCase         =					{
          """block_out_channels""": (32, 64),
          """down_block_types""": ("""DownBlock2D""", """AttnDownBlock2D"""),
          """up_block_types""": ("""AttnUpBlock2D""", """UpBlock2D"""),
          """attention_head_dim""": 3,
          """out_channels""": 3,
          """in_channels""": 3,
          """layers_per_block""": 2,
          """sample_size""": 32,
      }
      _UpperCAmelCase         =					self.dummy_input
      return init_dict, inputs_dict



class 				a (						lowerCAmelCase_		,							lowerCAmelCase_		,							unittest.TestCase      ):
    _snake_case   :     int   							=							UNetaDModel
    _snake_case   :     Optional[Any]   							=							'sample'


    @property
    def    lowerCAmelCase_     (    self		:     Optional[Any]    ):
      _UpperCAmelCase         =					4
      _UpperCAmelCase         =					4
      _UpperCAmelCase         =					(32, 32)

      _UpperCAmelCase         =					floats_tensor((batch_size, num_channels) + sizes    ).to(__lowerCAmelCase    )
      _UpperCAmelCase         =					torch.tensor([10]    ).to(__lowerCAmelCase    )

      return {"sample": noise, "timestep": time_step}


    @property
    def    lowerCAmelCase_     (    self		:     Optional[Any]    ):
      return (4, 32, 32)


    @property
    def    lowerCAmelCase_     (    self		:     Dict    ):
      return (4, 32, 32)


    def    lowerCAmelCase_     (    self		:     List[Any]    ):
      _UpperCAmelCase         =					{
          """sample_size""": 32,
          """in_channels""": 4,
          """out_channels""": 4,
          """layers_per_block""": 2,
          """block_out_channels""": (32, 64),
          """attention_head_dim""": 32,
          """down_block_types""": ("""DownBlock2D""", """DownBlock2D"""),
          """up_block_types""": ("""UpBlock2D""", """UpBlock2D"""),
      }
      _UpperCAmelCase         =					self.dummy_input
      return init_dict, inputs_dict


    def    lowerCAmelCase_     (    self		:     List[str]    ):
      _UpperCAmelCase       , _UpperCAmelCase         =					UNetaDModel.from_pretrained("""fusing/unet-ldm-dummy-update"""      , output_loading_info=__lowerCAmelCase    )

      self.assertIsNotNone(__lowerCAmelCase    )
      self.assertEqual(len(loading_info["""missing_keys"""]    )      , 0    )

      model.to(__lowerCAmelCase    )
      _UpperCAmelCase         =					model(**self.dummy_input    ).sample

      assert image is not None, "Make sure output is not None"


    @unittest.skipIf(torch_device != """cuda"""      , """This test is supposed to run on GPU"""    )
    def    lowerCAmelCase_     (    self		:     Optional[int]    ):
      _UpperCAmelCase       , _UpperCAmelCase         =					UNetaDModel.from_pretrained("""fusing/unet-ldm-dummy-update"""      , output_loading_info=__lowerCAmelCase    )
      model.to(__lowerCAmelCase    )
      _UpperCAmelCase         =					model(**self.dummy_input    ).sample

      assert image is not None, "Make sure output is not None"


    @unittest.skipIf(torch_device != """cuda"""      , """This test is supposed to run on GPU"""    )
    def    lowerCAmelCase_     (    self		:     str    ):
      # by defautl model loading will use accelerate as `low_cpu_mem_usage=True`
      _UpperCAmelCase       , _UpperCAmelCase         =					UNetaDModel.from_pretrained("""fusing/unet-ldm-dummy-update"""      , output_loading_info=__lowerCAmelCase    )
      model_accelerate.to(__lowerCAmelCase    )
      model_accelerate.eval()

      _UpperCAmelCase         =					torch.randn(
          1      , model_accelerate.config.in_channels      , model_accelerate.config.sample_size      , model_accelerate.config.sample_size      , generator=torch.manual_seed(0    )      , )
      _UpperCAmelCase         =					noise.to(__lowerCAmelCase    )
      _UpperCAmelCase         =					torch.tensor([10] * noise.shape[0]    ).to(__lowerCAmelCase    )

      _UpperCAmelCase         =					model_accelerate(__lowerCAmelCase      , __lowerCAmelCase    )["""sample"""]

      # two models don't need to stay in the device at the same time
      del model_accelerate
      torch.cuda.empty_cache()
      gc.collect()

      _UpperCAmelCase       , _UpperCAmelCase         =					UNetaDModel.from_pretrained(
          """fusing/unet-ldm-dummy-update"""      , output_loading_info=__lowerCAmelCase      , low_cpu_mem_usage=__lowerCAmelCase    )
      model_normal_load.to(__lowerCAmelCase    )
      model_normal_load.eval()
      _UpperCAmelCase         =					model_normal_load(__lowerCAmelCase      , __lowerCAmelCase    )["""sample"""]

      assert torch_all_close(__lowerCAmelCase      , __lowerCAmelCase      , rtol=1e-3    )


    def    lowerCAmelCase_     (    self		:     Tuple    ):
      _UpperCAmelCase         =					UNetaDModel.from_pretrained("""fusing/unet-ldm-dummy-update"""    )
      model.eval()
      model.to(__lowerCAmelCase    )

      _UpperCAmelCase         =					torch.randn(
          1      , model.config.in_channels      , model.config.sample_size      , model.config.sample_size      , generator=torch.manual_seed(0    )      , )
      _UpperCAmelCase         =					noise.to(__lowerCAmelCase    )
      _UpperCAmelCase         =					torch.tensor([10] * noise.shape[0]    ).to(__lowerCAmelCase    )

      with torch.no_grad():
        _UpperCAmelCase         =					model(__lowerCAmelCase      , __lowerCAmelCase    ).sample

      _UpperCAmelCase         =					output[0, -1, -3:, -3:].flatten().cpu()
      # fmt: off
      _UpperCAmelCase         =					torch.tensor([-13.3_258, -20.1_100, -15.9_873, -17.6_617, -23.0_596, -17.9_419, -13.3_675, -16.1_889, -12.3_800]    )
      # fmt: on

      self.assertTrue(torch_all_close(__lowerCAmelCase      , __lowerCAmelCase      , rtol=1e-3    )    )



class 				a (						lowerCAmelCase_		,							lowerCAmelCase_		,							unittest.TestCase      ):
    _snake_case   :     Optional[Any]   							=							UNetaDModel
    _snake_case   :     str   							=							'sample'


    @property
    def    lowerCAmelCase_     (    self		:     Optional[Any]      , __lowerCAmelCase		:     str=(32, 32)    ):
      _UpperCAmelCase         =					4
      _UpperCAmelCase         =					3

      _UpperCAmelCase         =					floats_tensor((batch_size, num_channels) + sizes    ).to(__lowerCAmelCase    )
      _UpperCAmelCase         =					torch.tensor(batch_size * [10]    ).to(dtype=torch.intaa      , device=__lowerCAmelCase    )

      return {"sample": noise, "timestep": time_step}


    @property
    def    lowerCAmelCase_     (    self		:     Any    ):
      return (3, 32, 32)


    @property
    def    lowerCAmelCase_     (    self		:     Union[str, Any]    ):
      return (3, 32, 32)


    def    lowerCAmelCase_     (    self		:     Union[str, Any]    ):
      _UpperCAmelCase         =					{
          """block_out_channels""": [32, 64, 64, 64],
          """in_channels""": 3,
          """layers_per_block""": 1,
          """out_channels""": 3,
          """time_embedding_type""": """fourier""",
          """norm_eps""": 1e-6,
          """mid_block_scale_factor""": math.sqrt(2.0    ),
          """norm_num_groups""": None,
          """down_block_types""": [
              """SkipDownBlock2D""",
              """AttnSkipDownBlock2D""",
              """SkipDownBlock2D""",
              """SkipDownBlock2D""",
          ],
          """up_block_types""": [
              """SkipUpBlock2D""",
              """SkipUpBlock2D""",
              """AttnSkipUpBlock2D""",
              """SkipUpBlock2D""",
          ],
      }
      _UpperCAmelCase         =					self.dummy_input
      return init_dict, inputs_dict


    @slow
    def    lowerCAmelCase_     (    self		:     Optional[Any]    ):
      _UpperCAmelCase       , _UpperCAmelCase         =					UNetaDModel.from_pretrained("""google/ncsnpp-celebahq-256"""      , output_loading_info=__lowerCAmelCase    )
      self.assertIsNotNone(__lowerCAmelCase    )
      self.assertEqual(len(loading_info["""missing_keys"""]    )      , 0    )

      model.to(__lowerCAmelCase    )
      _UpperCAmelCase         =					self.dummy_input
      _UpperCAmelCase         =					floats_tensor((4, 3) + (256, 256)    ).to(__lowerCAmelCase    )
      _UpperCAmelCase         =					noise
      _UpperCAmelCase         =					model(**__lowerCAmelCase    )

      assert image is not None, "Make sure output is not None"


    @slow
    def    lowerCAmelCase_     (    self		:     Union[str, Any]    ):
      _UpperCAmelCase         =					UNetaDModel.from_pretrained("""google/ncsnpp-celebahq-256"""    )
      model.to(__lowerCAmelCase    )

      _UpperCAmelCase         =					4
      _UpperCAmelCase         =					3
      _UpperCAmelCase         =					(256, 256)

      _UpperCAmelCase         =					torch.ones((batch_size, num_channels) + sizes    ).to(__lowerCAmelCase    )
      _UpperCAmelCase         =					torch.tensor(batch_size * [1e-4]    ).to(__lowerCAmelCase    )

      with torch.no_grad():
        _UpperCAmelCase         =					model(__lowerCAmelCase      , __lowerCAmelCase    ).sample

      _UpperCAmelCase         =					output[0, -3:, -3:, -1].flatten().cpu()
      # fmt: off
      _UpperCAmelCase         =					torch.tensor([-4_842.8_691, -6_499.6_631, -3_800.1_953, -7_978.2_686, -10_980.7_129, -20_028.8_535, 8_148.2_822, 2_342.2_905, 567.7_608]    )
      # fmt: on

      self.assertTrue(torch_all_close(__lowerCAmelCase      , __lowerCAmelCase      , rtol=1e-2    )    )


    def    lowerCAmelCase_     (    self		:     str    ):
      _UpperCAmelCase         =					UNetaDModel.from_pretrained("""fusing/ncsnpp-ffhq-ve-dummy-update"""    )
      model.to(__lowerCAmelCase    )

      _UpperCAmelCase         =					4
      _UpperCAmelCase         =					3
      _UpperCAmelCase         =					(32, 32)

      _UpperCAmelCase         =					torch.ones((batch_size, num_channels) + sizes    ).to(__lowerCAmelCase    )
      _UpperCAmelCase         =					torch.tensor(batch_size * [1e-4]    ).to(__lowerCAmelCase    )

      with torch.no_grad():
        _UpperCAmelCase         =					model(__lowerCAmelCase      , __lowerCAmelCase    ).sample

      _UpperCAmelCase         =					output[0, -3:, -3:, -1].flatten().cpu()
      # fmt: off
      _UpperCAmelCase         =					torch.tensor([-0.0_325, -0.0_900, -0.0_869, -0.0_332, -0.0_725, -0.0_270, -0.0_101, 0.0_227, 0.0_256]    )
      # fmt: on

      self.assertTrue(torch_all_close(__lowerCAmelCase      , __lowerCAmelCase      , rtol=1e-2    )    )


    def    lowerCAmelCase_     (    self		:     List[str]    ):
      # not required for this model
      pass
