






from ..utils import deprecate
from .attention_processor import (  # noqa: F401
    Attention,
    AttentionProcessor,
    AttnAddedKVProcessor,
    AttnProcessor2_0,
    LoRAAttnProcessor,
    LoRALinearLayer,
    LoRAXFormersAttnProcessor,
    SlicedAttnAddedKVProcessor,
    SlicedAttnProcessor,
    XFormersAttnProcessor,
)
from .attention_processor import AttnProcessor as AttnProcessorRename  # noqa: F401


deprecate(
    'cross_attention',
    '0.20.0',
    'Importing from cross_attention is deprecated. Please import from diffusers.models.attention_processor instead.',
    standard_warn=False,
)


_UpperCAmelCase				:	List[Any]    						=    AttentionProcessor


class 		lowerCAmelCase_							(     A__   ):





					def __init__(  self       ,   *_UpperCAmelCase       ,   **_UpperCAmelCase     )		->							int:
								lowerCamelCase											=F'''{self.__class__.__name__} is deprecated and will be removed in `0.20.0`. Please use `from diffusers.models.attention_processor import {"".join(self.__class__.__name__.split("Cross"     )     )} instead.'''
								deprecate("""cross_attention"""       ,   """0.20.0"""       ,   _lowerCAmelCase       ,   standard_warn=_lowerCAmelCase     )
								super().__init__(*_lowerCAmelCase       ,   **_lowerCAmelCase     )




class 		lowerCAmelCase_							(     A__   ):





					def __init__(  self       ,   *_UpperCAmelCase       ,   **_UpperCAmelCase     )		->							List[str]:
								lowerCamelCase											=F'''{self.__class__.__name__} is deprecated and will be removed in `0.20.0`. Please use `from diffusers.models.attention_processor import {"".join(self.__class__.__name__.split("Cross"     )     )} instead.'''
								deprecate("""cross_attention"""       ,   """0.20.0"""       ,   _lowerCAmelCase       ,   standard_warn=_lowerCAmelCase     )
								super().__init__(*_lowerCAmelCase       ,   **_lowerCAmelCase     )




class 		lowerCAmelCase_							(     A__   ):





					def __init__(  self       ,   *_UpperCAmelCase       ,   **_UpperCAmelCase     )		->							Any:
								lowerCamelCase											=F'''{self.__class__.__name__} is deprecated and will be removed in `0.20.0`. Please use `from diffusers.models.attention_processor import {"".join(self.__class__.__name__.split("Cross"     )     )} instead.'''
								deprecate("""cross_attention"""       ,   """0.20.0"""       ,   _lowerCAmelCase       ,   standard_warn=_lowerCAmelCase     )
								super().__init__(*_lowerCAmelCase       ,   **_lowerCAmelCase     )




class 		lowerCAmelCase_							(     A__   ):





					def __init__(  self       ,   *_UpperCAmelCase       ,   **_UpperCAmelCase     )		->							Union[str, Any]:
								lowerCamelCase											=F'''{self.__class__.__name__} is deprecated and will be removed in `0.20.0`. Please use `from diffusers.models.attention_processor import {"".join(self.__class__.__name__.split("Cross"     )     )} instead.'''
								deprecate("""cross_attention"""       ,   """0.20.0"""       ,   _lowerCAmelCase       ,   standard_warn=_lowerCAmelCase     )
								super().__init__(*_lowerCAmelCase       ,   **_lowerCAmelCase     )




class 		lowerCAmelCase_							(     A__   ):





					def __init__(  self       ,   *_UpperCAmelCase       ,   **_UpperCAmelCase     )		->							Optional[Any]:
								lowerCamelCase											=F'''{self.__class__.__name__} is deprecated and will be removed in `0.20.0`. Please use `from diffusers.models.attention_processor import {"".join(self.__class__.__name__.split("Cross"     )     )} instead.'''
								deprecate("""cross_attention"""       ,   """0.20.0"""       ,   _lowerCAmelCase       ,   standard_warn=_lowerCAmelCase     )
								super().__init__(*_lowerCAmelCase       ,   **_lowerCAmelCase     )




class 		lowerCAmelCase_							(     A__   ):





					def __init__(  self       ,   *_UpperCAmelCase       ,   **_UpperCAmelCase     )		->							Optional[int]:
								lowerCamelCase											=F'''{self.__class__.__name__} is deprecated and will be removed in `0.20.0`. Please use `from diffusers.models.attention_processor import {"".join(self.__class__.__name__.split("Cross"     )     )} instead.'''
								deprecate("""cross_attention"""       ,   """0.20.0"""       ,   _lowerCAmelCase       ,   standard_warn=_lowerCAmelCase     )
								super().__init__(*_lowerCAmelCase       ,   **_lowerCAmelCase     )




class 		lowerCAmelCase_							(     A__   ):





					def __init__(  self       ,   *_UpperCAmelCase       ,   **_UpperCAmelCase     )		->							Union[str, Any]:
								lowerCamelCase											=F'''{self.__class__.__name__} is deprecated and will be removed in `0.20.0`. Please use `from diffusers.models.attention_processor import {"".join(self.__class__.__name__.split("Cross"     )     )} instead.'''
								deprecate("""cross_attention"""       ,   """0.20.0"""       ,   _lowerCAmelCase       ,   standard_warn=_lowerCAmelCase     )
								super().__init__(*_lowerCAmelCase       ,   **_lowerCAmelCase     )




class 		lowerCAmelCase_							(     A__   ):





					def __init__(  self       ,   *_UpperCAmelCase       ,   **_UpperCAmelCase     )		->							Dict:
								lowerCamelCase											=F'''{self.__class__.__name__} is deprecated and will be removed in `0.20.0`. Please use `from diffusers.models.attention_processor import {"".join(self.__class__.__name__.split("Cross"     )     )} instead.'''
								deprecate("""cross_attention"""       ,   """0.20.0"""       ,   _lowerCAmelCase       ,   standard_warn=_lowerCAmelCase     )
								super().__init__(*_lowerCAmelCase       ,   **_lowerCAmelCase     )







import random
import unittest

import numpy as np
import torch

from diffusers import DDIMScheduler, LDMSuperResolutionPipeline, UNetaDModel, VQModel
from diffusers.utils import PIL_INTERPOLATION, floats_tensor, load_image, slow, torch_device
from diffusers.utils.testing_utils import enable_full_determinism, require_torch


enable_full_determinism()


class 		lowerCAmelCase_							(     unittest.TestCase   ):





     @property
     def 	lowerCamelCase       (  self     )		->							Tuple:
        lowerCamelCase											=1
        lowerCamelCase											=3
        lowerCamelCase											=(32, 32)

        lowerCamelCase											=floats_tensor((batch_size, num_channels) + sizes       ,   rng=random.Random(0     )     ).to(_UpperCAmelCase     )
        return image





     @property
     def 	lowerCamelCase       (  self     )		->							Optional[int]:
        torch.manual_seed(0     )
        lowerCamelCase											=UNetaDModel(
            block_out_channels=(32, 64)       ,   layers_per_block=2       ,   sample_size=32       ,   in_channels=6       ,   out_channels=3       ,   down_block_types=("""DownBlock2D""", """AttnDownBlock2D""")       ,   up_block_types=("""AttnUpBlock2D""", """UpBlock2D""")       ,   )
        return model





     @property
     def 	lowerCamelCase       (  self     )		->							int:
        torch.manual_seed(0     )
        lowerCamelCase											=VQModel(
            block_out_channels=[32, 64]       ,   in_channels=3       ,   out_channels=3       ,   down_block_types=["""DownEncoderBlock2D""", """DownEncoderBlock2D"""]       ,   up_block_types=["""UpDecoderBlock2D""", """UpDecoderBlock2D"""]       ,   latent_channels=3       ,   )
        return model





     def 	lowerCamelCase       (  self     )		->							Any:
        lowerCamelCase											="""cpu"""
        lowerCamelCase											=self.dummy_uncond_unet
        lowerCamelCase											=DDIMScheduler()
        lowerCamelCase											=self.dummy_vq_model

        lowerCamelCase											=LDMSuperResolutionPipeline(unet=_UpperCAmelCase       ,   vqvae=_UpperCAmelCase       ,   scheduler=_UpperCAmelCase     )
        ldm.to(_UpperCAmelCase     )
        ldm.set_progress_bar_config(disable=_UpperCAmelCase     )

        lowerCamelCase											=self.dummy_image.to(_UpperCAmelCase     )

        lowerCamelCase											=torch.Generator(device=_UpperCAmelCase     ).manual_seed(0     )
        lowerCamelCase											=ldm(image=_UpperCAmelCase       ,   generator=_UpperCAmelCase       ,   num_inference_steps=2       ,   output_type="""numpy"""     ).images

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

        assert image.shape == (1, 64, 64, 3)
        lowerCamelCase											=np.array([0.8678, 0.8245, 0.6381, 0.6830, 0.4385, 0.5599, 0.4641, 0.6201, 0.5150]     )

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





     @unittest.skipIf(torch_device != """cuda"""       ,   """This test requires a GPU"""     )
     def 	lowerCamelCase       (  self     )		->							int:
        lowerCamelCase											=self.dummy_uncond_unet
        lowerCamelCase											=DDIMScheduler()
        lowerCamelCase											=self.dummy_vq_model

        # put models in fp16
        lowerCamelCase											=unet.half()
        lowerCamelCase											=vqvae.half()

        lowerCamelCase											=LDMSuperResolutionPipeline(unet=_UpperCAmelCase       ,   vqvae=_UpperCAmelCase       ,   scheduler=_UpperCAmelCase     )
        ldm.to(_UpperCAmelCase     )
        ldm.set_progress_bar_config(disable=_UpperCAmelCase     )

        lowerCamelCase											=self.dummy_image.to(_UpperCAmelCase     )

        lowerCamelCase											=ldm(_UpperCAmelCase       ,   num_inference_steps=2       ,   output_type="""numpy"""     ).images

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




@slow
@require_torch
class 		lowerCAmelCase_							(     unittest.TestCase   ):





     def 	lowerCamelCase       (  self     )		->							Optional[int]:
        lowerCamelCase											=load_image(
            """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main"""
            """/vq_diffusion/teddy_bear_pool.png"""     )
        lowerCamelCase											=init_image.resize((64, 64)       ,   resample=PIL_INTERPOLATION["""lanczos"""]     )

        lowerCamelCase											=LDMSuperResolutionPipeline.from_pretrained("""duongna/ldm-super-resolution"""       ,   device_map="""auto"""     )
        ldm.set_progress_bar_config(disable=_UpperCAmelCase     )

        lowerCamelCase											=torch.manual_seed(0     )
        lowerCamelCase											=ldm(image=_UpperCAmelCase       ,   generator=_UpperCAmelCase       ,   num_inference_steps=20       ,   output_type="""numpy"""     ).images

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

        assert image.shape == (1, 256, 256, 3)
        lowerCamelCase											=np.array([0.7644, 0.7679, 0.7642, 0.7633, 0.7666, 0.7560, 0.7425, 0.7257, 0.6907]     )

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