


"""simple docstring"""


import unittest

from diffusers.models.unet_ad_blocks import *  # noqa F403
from diffusers.utils import torch_device

from .test_unet_blocks_common import UNetBlockTesterMixin
class   _UpperCamelCase  (   __lowercase       ,unittest.TestCase		):
							'''simple docstring'''







							__UpperCAmelCase						:    Dict				      =DownBlockaD  # noqa F405
							__UpperCAmelCase						:    List[str]				      ='''down'''

							def 			snake_case				(     self	):
										__lowerCAmelCase				    =			[-0.0_2_3_2, -0.9_8_6_9, 0.8_0_5_4, -0.0_6_3_7, -0.1_6_8_8, -1.4_2_6_4, 0.4_4_7_0, -1.3_3_9_4, 0.0_9_0_4]
										super().test_output(UpperCAmelCase__	)
class   _UpperCamelCase  (   __lowercase       ,unittest.TestCase		):
							'''simple docstring'''







							__UpperCAmelCase						:    int				      =ResnetDownsampleBlockaD  # noqa F405
							__UpperCAmelCase						:    int				      ='''down'''

							def 			snake_case				(     self	):
										__lowerCAmelCase				    =			[0.0_7_1_0, 0.2_4_1_0, -0.7_3_2_0, -1.0_7_5_7, -1.1_3_4_3, 0.3_5_4_0, -0.0_1_3_3, -0.2_5_7_6, 0.0_9_4_8]
										super().test_output(UpperCAmelCase__	)
class   _UpperCamelCase  (   __lowercase       ,unittest.TestCase		):
							'''simple docstring'''







							__UpperCAmelCase						:    Optional[int]				      =AttnDownBlockaD  # noqa F405
							__UpperCAmelCase						:    Union[str, Any]				      ='''down'''

							def 			snake_case				(     self	):
										__lowerCAmelCase				    =			[0.0_6_3_6, 0.8_9_6_4, -0.6_2_3_4, -1.0_1_3_1, 0.0_8_4_4, 0.4_9_3_5, 0.3_4_3_7, 0.0_9_1_1, -0.2_9_5_7]
										super().test_output(UpperCAmelCase__	)
class   _UpperCamelCase  (   __lowercase       ,unittest.TestCase		):
							'''simple docstring'''







							__UpperCAmelCase						:    str				      =CrossAttnDownBlockaD  # noqa F405
							__UpperCAmelCase						:    List[str]				      ='''down'''

							def 			snake_case				(     self	):
										__lowerCAmelCase    ,	__lowerCAmelCase				    =			super().prepare_init_args_and_inputs_for_common()
										__lowerCAmelCase				    =			32
										return init_dict, inputs_dict



							def 			snake_case				(     self	):
										__lowerCAmelCase				    =			[0.2_2_3_8, -0.7_3_9_6, -0.2_2_5_5, -0.3_8_2_9, 0.1_9_2_5, 1.1_6_6_5, 0.0_6_0_3, -0.7_2_9_5, 0.1_9_8_3]
										super().test_output(UpperCAmelCase__	)
class   _UpperCamelCase  (   __lowercase       ,unittest.TestCase		):
							'''simple docstring'''







							__UpperCAmelCase						:    Optional[int]				      =SimpleCrossAttnDownBlockaD  # noqa F405
							__UpperCAmelCase						:    str				      ='''down'''

							@property
							def 			snake_case				(     self	):
										return super().get_dummy_input(include_encoder_hidden_states=UpperCAmelCase__	)

							def 			snake_case				(     self	):
										__lowerCAmelCase    ,	__lowerCAmelCase				    =			super().prepare_init_args_and_inputs_for_common()
										__lowerCAmelCase				    =			32
										return init_dict, inputs_dict



							@unittest.skipIf(torch_device == "mps"    ,   "MPS result is not consistent"	)
							def 			snake_case				(     self	):
										__lowerCAmelCase				    =			[0.7_9_2_1, -0.0_9_9_2, -0.1_9_6_2, -0.7_6_9_5, -0.4_2_4_2, 0.7_8_0_4, 0.4_7_3_7, 0.2_7_6_5, 0.3_3_3_8]
										super().test_output(UpperCAmelCase__	)
class   _UpperCamelCase  (   __lowercase       ,unittest.TestCase		):
							'''simple docstring'''







							__UpperCAmelCase						:    List[str]				      =SkipDownBlockaD  # noqa F405
							__UpperCAmelCase						:    int				      ='''down'''

							@property
							def 			snake_case				(     self	):
										return super().get_dummy_input(include_skip_sample=UpperCAmelCase__	)



							def 			snake_case				(     self	):
										__lowerCAmelCase				    =			[-0.0_8_4_5, -0.2_0_8_7, -0.2_4_6_5, 0.0_9_7_1, 0.1_9_0_0, -0.0_4_8_4, 0.2_6_6_4, 0.4_1_7_9, 0.5_0_6_9]
										super().test_output(UpperCAmelCase__	)
class   _UpperCamelCase  (   __lowercase       ,unittest.TestCase		):
							'''simple docstring'''







							__UpperCAmelCase						:    Union[str, Any]				      =AttnSkipDownBlockaD  # noqa F405
							__UpperCAmelCase						:    str				      ='''down'''

							@property
							def 			snake_case				(     self	):
										return super().get_dummy_input(include_skip_sample=UpperCAmelCase__	)



							def 			snake_case				(     self	):
										__lowerCAmelCase				    =			[0.5_5_3_9, 0.1_6_0_9, 0.4_9_2_4, 0.0_5_3_7, -0.1_9_9_5, 0.4_0_5_0, 0.0_9_7_9, -0.2_7_2_1, -0.0_6_4_2]
										super().test_output(UpperCAmelCase__	)
class   _UpperCamelCase  (   __lowercase       ,unittest.TestCase		):
							'''simple docstring'''







							__UpperCAmelCase						:    Optional[Any]				      =DownEncoderBlockaD  # noqa F405
							__UpperCAmelCase						:    Optional[Any]				      ='''down'''

							@property
							def 			snake_case				(     self	):
										return super().get_dummy_input(include_temb=UpperCAmelCase__	)

							def 			snake_case				(     self	):
										__lowerCAmelCase				    =			{
										    "in_channels": 32,
										    "out_channels": 32,
										}
										__lowerCAmelCase				    =			self.dummy_input
										return init_dict, inputs_dict



							def 			snake_case				(     self	):
										__lowerCAmelCase				    =			[1.1_1_0_2, 0.5_3_0_2, 0.4_8_7_2, -0.0_0_2_3, -0.8_0_4_2, 0.0_4_8_3, -0.3_4_8_9, -0.5_6_3_2, 0.7_6_2_6]
										super().test_output(UpperCAmelCase__	)
class   _UpperCamelCase  (   __lowercase       ,unittest.TestCase		):
							'''simple docstring'''







							__UpperCAmelCase						:    List[Any]				      =AttnDownEncoderBlockaD  # noqa F405
							__UpperCAmelCase						:    Dict				      ='''down'''

							@property
							def 			snake_case				(     self	):
										return super().get_dummy_input(include_temb=UpperCAmelCase__	)

							def 			snake_case				(     self	):
										__lowerCAmelCase				    =			{
										    "in_channels": 32,
										    "out_channels": 32,
										}
										__lowerCAmelCase				    =			self.dummy_input
										return init_dict, inputs_dict



							def 			snake_case				(     self	):
										__lowerCAmelCase				    =			[0.8_9_6_6, -0.1_4_8_6, 0.8_5_6_8, 0.8_1_4_1, -0.9_0_4_6, -0.1_3_4_2, -0.0_9_7_2, -0.7_4_1_7, 0.1_5_3_8]
										super().test_output(UpperCAmelCase__	)
class   _UpperCamelCase  (   __lowercase       ,unittest.TestCase		):
							'''simple docstring'''







							__UpperCAmelCase						:    Union[str, Any]				      =UNetMidBlockaD  # noqa F405
							__UpperCAmelCase						:    Union[str, Any]				      ='''mid'''

							def 			snake_case				(     self	):
										__lowerCAmelCase				    =			{
										    "in_channels": 32,
										    "temb_channels": 1_28,
										}
										__lowerCAmelCase				    =			self.dummy_input
										return init_dict, inputs_dict



							def 			snake_case				(     self	):
										__lowerCAmelCase				    =			[-0.1_0_6_2, 1.7_2_4_8, 0.3_4_9_4, 1.4_5_6_9, -0.0_9_1_0, -1.2_4_2_1, -0.9_9_8_4, 0.6_7_3_6, 1.0_0_2_8]
										super().test_output(UpperCAmelCase__	)
class   _UpperCamelCase  (   __lowercase       ,unittest.TestCase		):
							'''simple docstring'''







							__UpperCAmelCase						:    List[Any]				      =UNetMidBlockaDCrossAttn  # noqa F405
							__UpperCAmelCase						:    List[Any]				      ='''mid'''

							def 			snake_case				(     self	):
										__lowerCAmelCase    ,	__lowerCAmelCase				    =			super().prepare_init_args_and_inputs_for_common()
										__lowerCAmelCase				    =			32
										return init_dict, inputs_dict



							def 			snake_case				(     self	):
										__lowerCAmelCase				    =			[0.0_1_8_7, 2.4_2_2_0, 0.4_4_8_4, 1.1_2_0_3, -0.6_1_2_1, -1.5_1_2_2, -0.8_2_7_0, 0.7_8_5_1, 1.8_3_3_5]
										super().test_output(UpperCAmelCase__	)
class   _UpperCamelCase  (   __lowercase       ,unittest.TestCase		):
							'''simple docstring'''







							__UpperCAmelCase						:    int				      =UNetMidBlockaDSimpleCrossAttn  # noqa F405
							__UpperCAmelCase						:    List[str]				      ='''mid'''

							@property
							def 			snake_case				(     self	):
										return super().get_dummy_input(include_encoder_hidden_states=UpperCAmelCase__	)

							def 			snake_case				(     self	):
										__lowerCAmelCase    ,	__lowerCAmelCase				    =			super().prepare_init_args_and_inputs_for_common()
										__lowerCAmelCase				    =			32
										return init_dict, inputs_dict



							def 			snake_case				(     self	):
										__lowerCAmelCase				    =			[0.7_1_4_3, 1.9_9_7_4, 0.5_4_4_8, 1.3_9_7_7, 0.1_2_8_2, -1.1_2_3_7, -1.4_2_3_8, 0.5_5_3_0, 0.8_8_8_0]
										super().test_output(UpperCAmelCase__	)
class   _UpperCamelCase  (   __lowercase       ,unittest.TestCase		):
							'''simple docstring'''







							__UpperCAmelCase						:    Optional[int]				      =UpBlockaD  # noqa F405
							__UpperCAmelCase						:    int				      ='''up'''

							@property
							def 			snake_case				(     self	):
										return super().get_dummy_input(include_res_hidden_states_tuple=UpperCAmelCase__	)



							def 			snake_case				(     self	):
										__lowerCAmelCase				    =			[-0.2_0_4_1, -0.4_1_6_5, -0.3_0_2_2, 0.0_0_4_1, -0.6_6_2_8, -0.7_0_5_3, 0.1_9_2_8, -0.0_3_2_5, 0.0_5_2_3]
										super().test_output(UpperCAmelCase__	)
class   _UpperCamelCase  (   __lowercase       ,unittest.TestCase		):
							'''simple docstring'''







							__UpperCAmelCase						:    Any				      =ResnetUpsampleBlockaD  # noqa F405
							__UpperCAmelCase						:    List[str]				      ='''up'''

							@property
							def 			snake_case				(     self	):
										return super().get_dummy_input(include_res_hidden_states_tuple=UpperCAmelCase__	)



							def 			snake_case				(     self	):
										__lowerCAmelCase				    =			[0.2_2_8_7, 0.3_5_4_9, -0.1_3_4_6, 0.4_7_9_7, -0.1_7_1_5, -0.9_6_4_9, 0.7_3_0_5, -0.5_8_6_4, -0.6_2_4_4]
										super().test_output(UpperCAmelCase__	)
class   _UpperCamelCase  (   __lowercase       ,unittest.TestCase		):
							'''simple docstring'''







							__UpperCAmelCase						:    List[str]				      =CrossAttnUpBlockaD  # noqa F405
							__UpperCAmelCase						:    Tuple				      ='''up'''

							@property
							def 			snake_case				(     self	):
										return super().get_dummy_input(include_res_hidden_states_tuple=UpperCAmelCase__	)

							def 			snake_case				(     self	):
										__lowerCAmelCase    ,	__lowerCAmelCase				    =			super().prepare_init_args_and_inputs_for_common()
										__lowerCAmelCase				    =			32
										return init_dict, inputs_dict



							def 			snake_case				(     self	):
										__lowerCAmelCase				    =			[-0.1_4_0_3, -0.3_5_1_5, -0.0_4_2_0, -0.1_4_2_5, 0.3_1_6_7, 0.5_0_9_4, -0.2_1_8_1, 0.5_9_3_1, 0.5_5_8_2]
										super().test_output(UpperCAmelCase__	)
class   _UpperCamelCase  (   __lowercase       ,unittest.TestCase		):
							'''simple docstring'''







							__UpperCAmelCase						:    Tuple				      =SimpleCrossAttnUpBlockaD  # noqa F405
							__UpperCAmelCase						:    Dict				      ='''up'''

							@property
							def 			snake_case				(     self	):
										return super().get_dummy_input(include_res_hidden_states_tuple=UpperCAmelCase__    ,   include_encoder_hidden_states=UpperCAmelCase__	)

							def 			snake_case				(     self	):
										__lowerCAmelCase    ,	__lowerCAmelCase				    =			super().prepare_init_args_and_inputs_for_common()
										__lowerCAmelCase				    =			32
										return init_dict, inputs_dict



							def 			snake_case				(     self	):
										__lowerCAmelCase				    =			[0.2_6_4_5, 0.1_4_8_0, 0.0_9_0_9, 0.8_0_4_4, -0.9_7_5_8, -0.9_0_8_3, 0.0_9_9_4, -1.1_4_5_3, -0.7_4_0_2]
										super().test_output(UpperCAmelCase__	)
class   _UpperCamelCase  (   __lowercase       ,unittest.TestCase		):
							'''simple docstring'''







							__UpperCAmelCase						:    int				      =AttnUpBlockaD  # noqa F405
							__UpperCAmelCase						:    List[str]				      ='''up'''

							@property
							def 			snake_case				(     self	):
										return super().get_dummy_input(include_res_hidden_states_tuple=UpperCAmelCase__	)



							@unittest.skipIf(torch_device == "mps"    ,   "MPS result is not consistent"	)
							def 			snake_case				(     self	):
										__lowerCAmelCase				    =			[0.0_9_7_9, 0.1_3_2_6, 0.0_0_2_1, 0.0_6_5_9, 0.2_2_4_9, 0.0_0_5_9, 0.1_1_3_2, 0.5_9_5_2, 0.1_0_3_3]
										super().test_output(UpperCAmelCase__	)
class   _UpperCamelCase  (   __lowercase       ,unittest.TestCase		):
							'''simple docstring'''







							__UpperCAmelCase						:    Tuple				      =SkipUpBlockaD  # noqa F405
							__UpperCAmelCase						:    str				      ='''up'''

							@property
							def 			snake_case				(     self	):
										return super().get_dummy_input(include_res_hidden_states_tuple=UpperCAmelCase__	)



							def 			snake_case				(     self	):
										__lowerCAmelCase				    =			[-0.0_8_9_3, -0.1_2_3_4, -0.1_5_0_6, -0.0_3_3_2, 0.0_1_2_3, -0.0_2_1_1, 0.0_5_6_6, 0.0_1_4_3, 0.0_3_6_2]
										super().test_output(UpperCAmelCase__	)
class   _UpperCamelCase  (   __lowercase       ,unittest.TestCase		):
							'''simple docstring'''







							__UpperCAmelCase						:    Optional[Any]				      =AttnSkipUpBlockaD  # noqa F405
							__UpperCAmelCase						:    List[Any]				      ='''up'''

							@property
							def 			snake_case				(     self	):
										return super().get_dummy_input(include_res_hidden_states_tuple=UpperCAmelCase__	)



							def 			snake_case				(     self	):
										__lowerCAmelCase				    =			[0.0_3_6_1, 0.0_6_1_7, 0.2_7_8_7, -0.0_3_5_0, 0.0_3_4_2, 0.3_4_2_1, -0.0_8_4_3, 0.0_9_1_3, 0.3_0_1_5]
										super().test_output(UpperCAmelCase__	)
class   _UpperCamelCase  (   __lowercase       ,unittest.TestCase		):
							'''simple docstring'''







							__UpperCAmelCase						:    str				      =UpDecoderBlockaD  # noqa F405
							__UpperCAmelCase						:    List[str]				      ='''up'''

							@property
							def 			snake_case				(     self	):
										return super().get_dummy_input(include_temb=UpperCAmelCase__	)

							def 			snake_case				(     self	):
										__lowerCAmelCase				    =			{"in_channels": 32, "out_channels": 32}

										__lowerCAmelCase				    =			self.dummy_input
										return init_dict, inputs_dict



							def 			snake_case				(     self	):
										__lowerCAmelCase				    =			[0.4_4_0_4, 0.1_9_9_8, -0.9_8_8_6, -0.3_3_2_0, -0.3_1_2_8, -0.7_0_3_4, -0.6_9_5_5, -0.2_3_3_8, -0.3_1_3_7]
										super().test_output(UpperCAmelCase__	)
class   _UpperCamelCase  (   __lowercase       ,unittest.TestCase		):
							'''simple docstring'''







							__UpperCAmelCase						:    Dict				      =AttnUpDecoderBlockaD  # noqa F405
							__UpperCAmelCase						:    Tuple				      ='''up'''

							@property
							def 			snake_case				(     self	):
										return super().get_dummy_input(include_temb=UpperCAmelCase__	)

							def 			snake_case				(     self	):
										__lowerCAmelCase				    =			{"in_channels": 32, "out_channels": 32}

										__lowerCAmelCase				    =			self.dummy_input
										return init_dict, inputs_dict



							def 			snake_case				(     self	):
										__lowerCAmelCase				    =			[0.6_7_3_8, 0.4_4_9_1, 0.1_0_5_5, 1.0_7_1_0, 0.7_3_1_6, 0.3_3_3_9, 0.3_3_5_2, 0.1_0_2_3, 0.3_5_6_8]
										super().test_output(UpperCAmelCase__	)








'''simple docstring'''




import gc
import unittest

import numpy as np
import torch

from diffusers import StableDiffusionKDiffusionPipeline
from diffusers.utils import slow, torch_device
from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu


enable_full_determinism()

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





     def 							__UpperCAmelCase      ( self       :      Optional[int]    )				->  int:
        # clean up the VRAM after each test
        super().tearDown()
        gc.collect()
        torch.cuda.empty_cache()





     def 							__UpperCAmelCase      ( self       :      Tuple    )				->  Any:
        lowerCAmelCase     =			StableDiffusionKDiffusionPipeline.from_pretrained('CompVis/stable-diffusion-v1-4'    )
        lowerCAmelCase     =			sd_pipe.to(UpperCAmelCase__    )
        sd_pipe.set_progress_bar_config(disable=UpperCAmelCase__    )

        sd_pipe.set_scheduler('sample_euler'    )

        lowerCAmelCase     =			'A painting of a squirrel eating a burger'
        lowerCAmelCase     =			torch.manual_seed(0    )
        lowerCAmelCase     =			sd_pipe([prompt]    ,      generator=UpperCAmelCase__    ,      guidance_scale=9.0    ,      num_inference_steps=2_0    ,      output_type='np'    )

        lowerCAmelCase     =			output.images

        lowerCAmelCase     =			image[0, -3:, -3:, -1]

        assert image.shape == (1, 5_1_2, 5_1_2, 3)
        lowerCAmelCase     =			np.array([0.0_447, 0.0_492, 0.0_468, 0.0_408, 0.0_383, 0.0_408, 0.0_354, 0.0_380, 0.0_339]    )

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





     def 							__UpperCAmelCase      ( self       :      List[str]    )				->  Dict:
        lowerCAmelCase     =			StableDiffusionKDiffusionPipeline.from_pretrained('stabilityai/stable-diffusion-2-1-base'    )
        lowerCAmelCase     =			sd_pipe.to(UpperCAmelCase__    )
        sd_pipe.set_progress_bar_config(disable=UpperCAmelCase__    )

        sd_pipe.set_scheduler('sample_euler'    )

        lowerCAmelCase     =			'A painting of a squirrel eating a burger'
        lowerCAmelCase     =			torch.manual_seed(0    )
        lowerCAmelCase     =			sd_pipe([prompt]    ,      generator=UpperCAmelCase__    ,      guidance_scale=9.0    ,      num_inference_steps=2_0    ,      output_type='np'    )

        lowerCAmelCase     =			output.images

        lowerCAmelCase     =			image[0, -3:, -3:, -1]

        assert image.shape == (1, 5_1_2, 5_1_2, 3)
        lowerCAmelCase     =			np.array([0.1_237, 0.1_320, 0.1_438, 0.1_359, 0.1_390, 0.1_132, 0.1_277, 0.1_175, 0.1_112]    )

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





     def 							__UpperCAmelCase      ( self       :      Optional[Any]    )				->  List[str]:
        lowerCAmelCase     =			StableDiffusionKDiffusionPipeline.from_pretrained('stabilityai/stable-diffusion-2-1-base'    )
        lowerCAmelCase     =			sd_pipe.to(UpperCAmelCase__    )
        sd_pipe.set_progress_bar_config(disable=UpperCAmelCase__    )

        sd_pipe.set_scheduler('sample_dpmpp_2m'    )

        lowerCAmelCase     =			'A painting of a squirrel eating a burger'
        lowerCAmelCase     =			torch.manual_seed(0    )
        lowerCAmelCase     =			sd_pipe(
            [prompt]    ,      generator=UpperCAmelCase__    ,      guidance_scale=7.5    ,      num_inference_steps=1_5    ,      output_type='np'    ,      use_karras_sigmas=UpperCAmelCase__    ,      )

        lowerCAmelCase     =			output.images

        lowerCAmelCase     =			image[0, -3:, -3:, -1]

        assert image.shape == (1, 5_1_2, 5_1_2, 3)
        lowerCAmelCase     =			np.array(
            [0.11_381_689, 0.12_112_921, 0.1_389_457, 0.12_549_606, 0.1_244_964, 0.10_831_517, 0.11_562_866, 0.10_867_816, 0.10_499_048]    )

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





