


import gc
import random
import unittest

import numpy as np
import torch
from PIL import Image
from transformers import CLIPTextConfig, CLIPTextModel, CLIPTokenizer

from diffusers import AutoencoderKL, DDIMScheduler, DDPMScheduler, StableDiffusionUpscalePipeline, UNetaDConditionModel
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


enable_full_determinism()


class   a__	(						unittest.TestCase    ):

       def 						UpperCAmelCase(   self      :     Any    ):
            super().tearDown()
            gc.collect()
            torch.cuda.empty_cache()

       @property
       def 						UpperCAmelCase(   self      :     Dict    ):
            a_    :     Optional[Any]										=    1
            a_    :     Union[str, Any]										=    3
            a_    :     List[Any]										=    (3_2, 3_2)

            a_    :     Dict										=    floats_tensor((batch_size, num_channels) + sizes      ,							rng=random.Random(0    )    ).to(lowercase__    )
            return image

       @property
       def 						UpperCAmelCase(   self      :     Tuple    ):
            torch.manual_seed(0    )
            a_    :     List[str]										=    UNetaDConditionModel(
                block_out_channels=(3_2, 3_2, 6_4)      ,							layers_per_block=2      ,							sample_size=3_2      ,							in_channels=7      ,							out_channels=4      ,							down_block_types=("""DownBlock2D""", """CrossAttnDownBlock2D""", """CrossAttnDownBlock2D""")      ,							up_block_types=("""CrossAttnUpBlock2D""", """CrossAttnUpBlock2D""", """UpBlock2D""")      ,							cross_attention_dim=3_2      ,							attention_head_dim=8      ,							use_linear_projection=lowercase__      ,							only_cross_attention=(True, True, False)      ,							num_class_embeds=1_0_0      ,							)
            return model

       @property
       def 						UpperCAmelCase(   self      :     Union[str, Any]    ):
            torch.manual_seed(0    )
            a_    :     Any										=    AutoencoderKL(
                block_out_channels=[3_2, 3_2, 6_4]      ,							in_channels=3      ,							out_channels=3      ,							down_block_types=["""DownEncoderBlock2D""", """DownEncoderBlock2D""", """DownEncoderBlock2D"""]      ,							up_block_types=["""UpDecoderBlock2D""", """UpDecoderBlock2D""", """UpDecoderBlock2D"""]      ,							latent_channels=4      ,							)
            return model

       @property
       def 						UpperCAmelCase(   self      :     Dict    ):
            torch.manual_seed(0    )
            a_    :     Union[str, Any]										=    CLIPTextConfig(
                bos_token_id=0      ,							eos_token_id=2      ,							hidden_size=3_2      ,							intermediate_size=3_7      ,							layer_norm_eps=1E-05      ,							num_attention_heads=4      ,							num_hidden_layers=5      ,							pad_token_id=1      ,							vocab_size=1_0_0_0      ,							hidden_act="""gelu"""      ,							projection_dim=5_1_2      ,							)
            return CLIPTextModel(lowercase__    )

       def 						UpperCAmelCase(   self      :     List[Any]    ):
            a_    :     List[str]										=    '''cpu'''  # ensure determinism for the device-dependent torch.Generator
            a_    :     Union[str, Any]										=    self.dummy_cond_unet_upscale
            a_    :     List[Any]										=    DDPMScheduler()
            a_    :     Dict										=    DDIMScheduler(prediction_type="""v_prediction"""    )
            a_    :     Union[str, Any]										=    self.dummy_vae
            a_    :     Optional[Any]										=    self.dummy_text_encoder
            a_    :     List[Any]										=    CLIPTokenizer.from_pretrained("""hf-internal-testing/tiny-random-clip"""    )

            a_    :     int										=    self.dummy_image.cpu().permute(0      ,							2      ,							3      ,							1    )[0]
            a_    :     List[str]										=    Image.fromarray(np.uinta(lowercase__    )    ).convert("""RGB"""    ).resize((6_4, 6_4)    )

            # make sure here that pndm scheduler skips prk
            a_    :     int										=    StableDiffusionUpscalePipeline(
                unet=lowercase__      ,							low_res_scheduler=lowercase__      ,							scheduler=lowercase__      ,							vae=lowercase__      ,							text_encoder=lowercase__      ,							tokenizer=lowercase__      ,							max_noise_level=3_5_0      ,							)
            a_    :     Tuple										=    sd_pipe.to(lowercase__    )
            sd_pipe.set_progress_bar_config(disable=lowercase__    )

            a_    :     Union[str, Any]										=    '''A painting of a squirrel eating a burger'''
            a_    :     Any										=    torch.Generator(device=lowercase__    ).manual_seed(0    )
            a_    :     int										=    sd_pipe(
                [prompt]      ,							image=lowercase__      ,							generator=lowercase__      ,							guidance_scale=6.0      ,							noise_level=2_0      ,							num_inference_steps=2      ,							output_type="""np"""      ,							)

            a_    :     Dict										=    output.images

            a_    :     Optional[int]										=    torch.Generator(device=lowercase__    ).manual_seed(0    )
            a_    :     List[str]										=    sd_pipe(
                [prompt]      ,							image=lowercase__      ,							generator=lowercase__      ,							guidance_scale=6.0      ,							noise_level=2_0      ,							num_inference_steps=2      ,							output_type="""np"""      ,							return_dict=lowercase__      ,							)[0]

            a_    :     Optional[Any]										=    image[0, -3:, -3:, -1]
            a_    :     Union[str, Any]										=    image_from_tuple[0, -3:, -3:, -1]

            a_    :     Dict										=    low_res_image.size[0] * 4
            assert image.shape == (1, expected_height_width, expected_height_width, 3)
            a_    :     int										=    np.array([0.3_1_1_3, 0.3_9_1_0, 0.4_2_7_2, 0.4_8_5_9, 0.5_0_6_1, 0.4_6_5_2, 0.5_3_6_2, 0.5_7_1_5, 0.5_6_6_1]    )

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

       def 						UpperCAmelCase(   self      :     str    ):
            a_    :     Any										=    '''cpu'''  # ensure determinism for the device-dependent torch.Generator
            a_    :     Dict										=    self.dummy_cond_unet_upscale
            a_    :     Any										=    DDPMScheduler()
            a_    :     List[str]										=    DDIMScheduler(prediction_type="""v_prediction"""    )
            a_    :     Optional[Any]										=    self.dummy_vae
            a_    :     List[str]										=    self.dummy_text_encoder
            a_    :     Optional[Any]										=    CLIPTokenizer.from_pretrained("""hf-internal-testing/tiny-random-clip"""    )

            a_    :     str										=    self.dummy_image.cpu().permute(0      ,							2      ,							3      ,							1    )[0]
            a_    :     Optional[Any]										=    Image.fromarray(np.uinta(lowercase__    )    ).convert("""RGB"""    ).resize((6_4, 6_4)    )

            # make sure here that pndm scheduler skips prk
            a_    :     str										=    StableDiffusionUpscalePipeline(
                unet=lowercase__      ,							low_res_scheduler=lowercase__      ,							scheduler=lowercase__      ,							vae=lowercase__      ,							text_encoder=lowercase__      ,							tokenizer=lowercase__      ,							max_noise_level=3_5_0      ,							)
            a_    :     List[Any]										=    sd_pipe.to(lowercase__    )
            sd_pipe.set_progress_bar_config(disable=lowercase__    )

            a_    :     str										=    '''A painting of a squirrel eating a burger'''
            a_    :     Optional[Any]										=    sd_pipe(
                2 * [prompt]      ,							image=2 * [low_res_image]      ,							guidance_scale=6.0      ,							noise_level=2_0      ,							num_inference_steps=2      ,							output_type="""np"""      ,							)
            a_    :     Any										=    output.images
            assert image.shape[0] == 2

            a_    :     str										=    torch.Generator(device=lowercase__    ).manual_seed(0    )
            a_    :     Optional[Any]										=    sd_pipe(
                [prompt]      ,							image=lowercase__      ,							generator=lowercase__      ,							num_images_per_prompt=2      ,							guidance_scale=6.0      ,							noise_level=2_0      ,							num_inference_steps=2      ,							output_type="""np"""      ,							)
            a_    :     str										=    output.images
            assert image.shape[0] == 2

       @unittest.skipIf(torch_device != """cuda"""      ,							"""This test requires a GPU"""    )
       def 						UpperCAmelCase(   self      :     Tuple    ):
            a_    :     str										=    self.dummy_cond_unet_upscale
            a_    :     Dict										=    DDPMScheduler()
            a_    :     int										=    DDIMScheduler(prediction_type="""v_prediction"""    )
            a_    :     Dict										=    self.dummy_vae
            a_    :     Optional[int]										=    self.dummy_text_encoder
            a_    :     List[Any]										=    CLIPTokenizer.from_pretrained("""hf-internal-testing/tiny-random-clip"""    )

            a_    :     Dict										=    self.dummy_image.cpu().permute(0      ,							2      ,							3      ,							1    )[0]
            a_    :     Dict										=    Image.fromarray(np.uinta(lowercase__    )    ).convert("""RGB"""    ).resize((6_4, 6_4)    )

            # put models in fp16, except vae as it overflows in fp16
            a_    :     Tuple										=    unet.half()
            a_    :     int										=    text_encoder.half()

            # make sure here that pndm scheduler skips prk
            a_    :     Optional[Any]										=    StableDiffusionUpscalePipeline(
                unet=lowercase__      ,							low_res_scheduler=lowercase__      ,							scheduler=lowercase__      ,							vae=lowercase__      ,							text_encoder=lowercase__      ,							tokenizer=lowercase__      ,							max_noise_level=3_5_0      ,							)
            a_    :     List[Any]										=    sd_pipe.to(lowercase__    )
            sd_pipe.set_progress_bar_config(disable=lowercase__    )

            a_    :     List[Any]										=    '''A painting of a squirrel eating a burger'''
            a_    :     int										=    torch.manual_seed(0    )
            a_    :     Tuple										=    sd_pipe(
                [prompt]      ,							image=lowercase__      ,							generator=lowercase__      ,							num_inference_steps=2      ,							output_type="""np"""      ,							).images

            a_    :     List[Any]										=    low_res_image.size[0] * 4
            assert image.shape == (1, expected_height_width, expected_height_width, 3)


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

       def 						UpperCAmelCase(   self      :     Optional[Any]    ):
            super().tearDown()
            gc.collect()
            torch.cuda.empty_cache()

       def 						UpperCAmelCase(   self      :     Optional[Any]    ):
            a_    :     Optional[Any]										=    load_image(
                """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main"""
                """/sd2-upscale/low_res_cat.png"""    )
            a_    :     Optional[Any]										=    load_numpy(
                """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/sd2-upscale"""
                """/upsampled_cat.npy"""    )

            a_    :     Union[str, Any]										=    '''stabilityai/stable-diffusion-x4-upscaler'''
            a_    :     Tuple										=    StableDiffusionUpscalePipeline.from_pretrained(lowercase__    )
            pipe.to(lowercase__    )
            pipe.set_progress_bar_config(disable=lowercase__    )
            pipe.enable_attention_slicing()

            a_    :     Union[str, Any]										=    '''a cat sitting on a park bench'''

            a_    :     Any										=    torch.manual_seed(0    )
            a_    :     Tuple										=    pipe(
                prompt=lowercase__      ,							image=lowercase__      ,							generator=lowercase__      ,							output_type="""np"""      ,							)
            a_    :     Optional[Any]										=    output.images[0]

            assert image.shape == (5_1_2, 5_1_2, 3)
            assert np.abs(expected_image - image    ).max() < 1E-3

       def 						UpperCAmelCase(   self      :     int    ):
            a_    :     str										=    load_image(
                """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main"""
                """/sd2-upscale/low_res_cat.png"""    )
            a_    :     Optional[Any]										=    load_numpy(
                """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/sd2-upscale"""
                """/upsampled_cat_fp16.npy"""    )

            a_    :     int										=    '''stabilityai/stable-diffusion-x4-upscaler'''
            a_    :     Union[str, Any]										=    StableDiffusionUpscalePipeline.from_pretrained(
                lowercase__      ,							torch_dtype=torch.floataa      ,							)
            pipe.to(lowercase__    )
            pipe.set_progress_bar_config(disable=lowercase__    )
            pipe.enable_attention_slicing()

            a_    :     List[str]										=    '''a cat sitting on a park bench'''

            a_    :     List[str]										=    torch.manual_seed(0    )
            a_    :     str										=    pipe(
                prompt=lowercase__      ,							image=lowercase__      ,							generator=lowercase__      ,							output_type="""np"""      ,							)
            a_    :     Optional[int]										=    output.images[0]

            assert image.shape == (5_1_2, 5_1_2, 3)
            assert np.abs(expected_image - image    ).max() < 5E-1

       def 						UpperCAmelCase(   self      :     Union[str, Any]    ):
            torch.cuda.empty_cache()
            torch.cuda.reset_max_memory_allocated()
            torch.cuda.reset_peak_memory_stats()

            a_    :     List[Any]										=    load_image(
                """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main"""
                """/sd2-upscale/low_res_cat.png"""    )

            a_    :     Optional[int]										=    '''stabilityai/stable-diffusion-x4-upscaler'''
            a_    :     Union[str, Any]										=    StableDiffusionUpscalePipeline.from_pretrained(
                lowercase__      ,							torch_dtype=torch.floataa      ,							)
            pipe.to(lowercase__    )
            pipe.set_progress_bar_config(disable=lowercase__    )
            pipe.enable_attention_slicing(1    )
            pipe.enable_sequential_cpu_offload()

            a_    :     str										=    '''a cat sitting on a park bench'''

            a_    :     Tuple										=    torch.manual_seed(0    )
            a_    :     List[str]										=    pipe(
                prompt=lowercase__      ,							image=lowercase__      ,							generator=lowercase__      ,							num_inference_steps=5      ,							output_type="""np"""      ,							)

            a_    :     Any										=    torch.cuda.max_memory_allocated()
            # make sure that less than 2.9 GB is allocated
            assert mem_bytes < 2.9 * 1_0**9







from typing import TYPE_CHECKING

from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tokenizers_available, is_torch_available


__lowerCamelCase        =     {
    '''configuration_altclip''': [
        '''ALTCLIP_PRETRAINED_CONFIG_ARCHIVE_MAP''',
        '''AltCLIPConfig''',
        '''AltCLIPTextConfig''',
        '''AltCLIPVisionConfig''',
    ],
    '''processing_altclip''': ['''AltCLIPProcessor'''],
}

try:
       if not is_torch_available():
              raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
       pass
else:
       __lowerCamelCase        =     [
           '''ALTCLIP_PRETRAINED_MODEL_ARCHIVE_LIST''',
           '''AltCLIPPreTrainedModel''',
           '''AltCLIPModel''',
           '''AltCLIPTextModel''',
           '''AltCLIPVisionModel''',
       ]


if TYPE_CHECKING:
       from .configuration_altclip import (
           ALTCLIP_PRETRAINED_CONFIG_ARCHIVE_MAP,
           AltCLIPConfig,
           AltCLIPTextConfig,
           AltCLIPVisionConfig,
       )
       from .processing_altclip import AltCLIPProcessor

       try:
              if not is_torch_available():
                     raise OptionalDependencyNotAvailable()
       except OptionalDependencyNotAvailable:
              pass
       else:
              from .modeling_altclip import (
                  ALTCLIP_PRETRAINED_MODEL_ARCHIVE_LIST,
                  AltCLIPModel,
                  AltCLIPPreTrainedModel,
                  AltCLIPTextModel,
                  AltCLIPVisionModel,
              )


else:
       import sys

       __lowerCamelCase        =     _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)



