



import gc
import random
import tempfile
import unittest

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

from diffusers import AutoencoderKL, DDIMScheduler, LMSDiscreteScheduler, PNDMScheduler, UNetaDConditionModel
from diffusers.pipelines.semantic_stable_diffusion import SemanticStableDiffusionPipeline as StableDiffusionPipeline
from diffusers.utils import floats_tensor, nightly, torch_device
from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu


enable_full_determinism()


class 	lowerCamelCase__ (		unittest.TestCase   ):


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


						@property
						def   _lowerCamelCase			( self						:  str			):
													a__:       str											=1
													a__:       Optional[Any]											=3
													a__:       int											=(3_2, 3_2)

													a__:       Union[str, Any]											=floats_tensor((batch_size, num_channels) + sizes      ,       rng=random.Random(0			)			).to(_a			)
													return image


						@property
						def   _lowerCamelCase			( self						:  Optional[Any]			):
													torch.manual_seed(0			)
													a__:       str											=UNetaDConditionModel(
													    block_out_channels=(3_2, 6_4)      ,       layers_per_block=2      ,       sample_size=3_2      ,       in_channels=4      ,       out_channels=4      ,       down_block_types=("DownBlock2D", "CrossAttnDownBlock2D")      ,       up_block_types=("CrossAttnUpBlock2D", "UpBlock2D")      ,       cross_attention_dim=3_2      ,       )
													return model


						@property
						def   _lowerCamelCase			( self						:  int			):
													torch.manual_seed(0			)
													a__:       Dict											=AutoencoderKL(
													    block_out_channels=[3_2, 6_4]      ,       in_channels=3      ,       out_channels=3      ,       down_block_types=["DownEncoderBlock2D", "DownEncoderBlock2D"]      ,       up_block_types=["UpDecoderBlock2D", "UpDecoderBlock2D"]      ,       latent_channels=4      ,       )
													return model


						@property
						def   _lowerCamelCase			( self						:  Optional[Any]			):
													torch.manual_seed(0			)
													a__:       Dict											=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      ,       )
													return CLIPTextModel(_a			)


						@property
						def   _lowerCamelCase			( self						:  Dict			):
													def extract(*_a						:  List[str]      ,       **_a						:  Dict			):


																				class 	lowerCamelCase__ :


																										def __init__( self						:  Dict			):
																																	a__:       Dict											=torch.ones([0]			)







																										def   _lowerCamelCase			( self						:  int      ,       _a						:  Optional[Any]			):
																																	self.pixel_values.to(_a			)
																																	return self


																				return Out()

													return extract


						def   _lowerCamelCase			( self						:  int			):
													a__:       List[Any]											="cpu"  # ensure determinism for the device-dependent torch.Generator
													a__:       int											=self.dummy_cond_unet
													a__:       Optional[Any]											=DDIMScheduler(
													    beta_start=0.0_0_0_8_5      ,       beta_end=0.0_1_2      ,       beta_schedule="scaled_linear"      ,       clip_sample=_a      ,       set_alpha_to_one=_a      ,       )

													a__:       Optional[Any]											=self.dummy_vae
													a__:       Optional[Any]											=self.dummy_text_encoder
													a__:       str											=CLIPTokenizer.from_pretrained("hf-internal-testing/tiny-random-clip"			)

													# make sure here that pndm scheduler skips prk
													a__:       int											=StableDiffusionPipeline(
													    unet=_a      ,       scheduler=_a      ,       vae=_a      ,       text_encoder=_a      ,       tokenizer=_a      ,       safety_checker=_a      ,       feature_extractor=self.dummy_extractor      ,       )
													a__:       Union[str, Any]											=sd_pipe.to(_a			)
													sd_pipe.set_progress_bar_config(disable=_a			)

													a__:       Optional[int]											="A painting of a squirrel eating a burger"

													a__:       Tuple											=torch.Generator(device=_a			).manual_seed(0			)
													a__:       List[Any]											=sd_pipe([prompt]      ,       generator=_a      ,       guidance_scale=6.0      ,       num_inference_steps=2      ,       output_type="np"			)
													a__:       List[Any]											=output.images

													a__:       List[Any]											=torch.Generator(device=_a			).manual_seed(0			)
													a__:       str											=sd_pipe(
													    [prompt]      ,       generator=_a      ,       guidance_scale=6.0      ,       num_inference_steps=2      ,       output_type="np"      ,       return_dict=_a      ,       )[0]

													a__:       Optional[int]											=image[0, -3:, -3:, -1]
													a__:       Any											=image_from_tuple[0, -3:, -3:, -1]

													assert image.shape == (1, 6_4, 6_4, 3)
													a__:       Union[str, Any]											=np.array([0.5_7_5_3, 0.6_1_1_4, 0.5_0_0_1, 0.5_0_3_4, 0.5_4_7_0, 0.4_7_2_9, 0.4_9_7_1, 0.4_8_6_7, 0.4_8_6_7]			)

													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   _lowerCamelCase			( self						:  int			):
													a__:       Optional[Any]											="cpu"  # ensure determinism for the device-dependent torch.Generator
													a__:       Optional[int]											=self.dummy_cond_unet
													a__:       Optional[Any]											=PNDMScheduler(skip_prk_steps=_a			)
													a__:       Optional[int]											=self.dummy_vae
													a__:       str											=self.dummy_text_encoder
													a__:       Optional[int]											=CLIPTokenizer.from_pretrained("hf-internal-testing/tiny-random-clip"			)

													# make sure here that pndm scheduler skips prk
													a__:       Dict											=StableDiffusionPipeline(
													    unet=_a      ,       scheduler=_a      ,       vae=_a      ,       text_encoder=_a      ,       tokenizer=_a      ,       safety_checker=_a      ,       feature_extractor=self.dummy_extractor      ,       )
													a__:       Optional[Any]											=sd_pipe.to(_a			)
													sd_pipe.set_progress_bar_config(disable=_a			)

													a__:       str											="A painting of a squirrel eating a burger"
													a__:       Any											=torch.Generator(device=_a			).manual_seed(0			)
													a__:       str											=sd_pipe([prompt]      ,       generator=_a      ,       guidance_scale=6.0      ,       num_inference_steps=2      ,       output_type="np"			)

													a__:       Tuple											=output.images

													a__:       Tuple											=torch.Generator(device=_a			).manual_seed(0			)
													a__:       Optional[Any]											=sd_pipe(
													    [prompt]      ,       generator=_a      ,       guidance_scale=6.0      ,       num_inference_steps=2      ,       output_type="np"      ,       return_dict=_a      ,       )[0]

													a__:       Dict											=image[0, -3:, -3:, -1]
													a__:       Optional[int]											=image_from_tuple[0, -3:, -3:, -1]

													assert image.shape == (1, 6_4, 6_4, 3)
													a__:       Tuple											=np.array([0.5_1_2_2, 0.5_7_1_2, 0.4_8_2_5, 0.5_0_5_3, 0.5_6_4_6, 0.4_7_6_9, 0.5_1_7_9, 0.4_8_9_4, 0.4_9_9_4]			)

													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   _lowerCamelCase			( self						:  List[Any]			):
													a__:       Union[str, Any]											=StableDiffusionPipeline.from_pretrained(
													    "hf-internal-testing/tiny-stable-diffusion-lms-pipe"      ,       safety_checker=_a			)
													assert isinstance(_a      ,       _a			)
													assert isinstance(pipe.scheduler      ,       _a			)
													assert pipe.safety_checker is None

													a__:       int											=pipe("example prompt"      ,       num_inference_steps=2			).images[0]
													assert image is not None

													# check that there's no error when saving a pipeline with one of the models being None
													with tempfile.TemporaryDirectory() as tmpdirname:
																				pipe.save_pretrained(_a			)
																				a__:       Optional[int]											=StableDiffusionPipeline.from_pretrained(_a			)

													# sanity check that the pipeline still works
													assert pipe.safety_checker is None
													a__:       List[Any]											=pipe("example prompt"      ,       num_inference_steps=2			).images[0]
													assert image is not None







						@unittest.skipIf(torch_device != "cuda"      ,       "This test requires a GPU"			)
						def   _lowerCamelCase			( self						:  Optional[int]			):
													a__:       Any											=self.dummy_cond_unet
													a__:       Dict											=PNDMScheduler(skip_prk_steps=_a			)
													a__:       str											=self.dummy_vae
													a__:       Union[str, Any]											=self.dummy_text_encoder
													a__:       Dict											=CLIPTokenizer.from_pretrained("hf-internal-testing/tiny-random-clip"			)

													# put models in fp16
													a__:       List[str]											=unet.half()
													a__:       Union[str, Any]											=vae.half()
													a__:       int											=bert.half()

													# make sure here that pndm scheduler skips prk
													a__:       List[str]											=StableDiffusionPipeline(
													    unet=_a      ,       scheduler=_a      ,       vae=_a      ,       text_encoder=_a      ,       tokenizer=_a      ,       safety_checker=_a      ,       feature_extractor=self.dummy_extractor      ,       )
													a__:       Optional[int]											=sd_pipe.to(_a			)
													sd_pipe.set_progress_bar_config(disable=_a			)

													a__:       str											="A painting of a squirrel eating a burger"
													a__:       Optional[int]											=sd_pipe([prompt]      ,       num_inference_steps=2      ,       output_type="np"			).images

													assert image.shape == (1, 6_4, 6_4, 3)


@nightly
@require_torch_gpu
class 	lowerCamelCase__ (		unittest.TestCase   ):


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


						def   _lowerCamelCase			( self						:  int			):
													a__:       Any											="cuda"
													a__:       Optional[Any]											=StableDiffusionPipeline.from_pretrained("runwayml/stable-diffusion-v1-5"			)
													a__:       int											=pipe.to(_a			)
													pipe.set_progress_bar_config(disable=_a			)

													a__:       List[str]											="a photo of a cat"
													a__:       Union[str, Any]											={
													    "editing_prompt": ["sunglasses"],
													    "reverse_editing_direction": [False],
													    "edit_warmup_steps": 1_0,
													    "edit_guidance_scale": 6,
													    "edit_threshold": 0.9_5,
													    "edit_momentum_scale": 0.5,
													    "edit_mom_beta": 0.6,
													}

													a__:       Union[str, Any]											=3
													a__:       Tuple											=7

													# no sega enabled
													a__:       List[Any]											=torch.Generator(_a			)
													generator.manual_seed(_a			)
													a__:       Optional[Any]											=pipe(
													    [prompt]      ,       generator=_a      ,       guidance_scale=_a      ,       num_inference_steps=5_0      ,       output_type="np"      ,       width=5_1_2      ,       height=5_1_2      ,       )

													a__:       int											=output.images
													a__:       List[Any]											=image[0, -3:, -3:, -1]
													a__:       Union[str, Any]											=[
													    0.3_4_6_7_3_1_1_3,
													    0.3_8_4_9_2_7_3_3,
													    0.3_7_5_9_7_3_5_2,
													    0.3_4_0_8_6_3_3_5,
													    0.3_5_6_5_0_7_4_8,
													    0.3_5_5_7_9_2_0_5,
													    0.3_3_8_4_7_6_3,
													    0.3_4_3_4_0_2_3_6,
													    0.3_5_7_3_2_7_1,
													]

													assert image.shape == (1, 5_1_2, 5_1_2, 3)

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

													# with sega enabled
													# generator = torch.manual_seed(seed)
													generator.manual_seed(_a			)
													a__:       Any											=pipe(
													    [prompt]      ,       generator=_a      ,       guidance_scale=_a      ,       num_inference_steps=5_0      ,       output_type="np"      ,       width=5_1_2      ,       height=5_1_2      ,       **_a      ,       )

													a__:       Union[str, Any]											=output.images
													a__:       List[str]											=image[0, -3:, -3:, -1]
													a__:       List[str]											=[
													    0.4_1_8_8_7_8_2_6,
													    0.3_7_7_2_8_7_6_6,
													    0.3_0_1_3_8_2_7_2,
													    0.4_1_4_1_6_3_3_5,
													    0.4_1_6_6_4_9_8_5,
													    0.3_6_2_8_3_3_9_2,
													    0.3_6_1_9_1_2_4_6,
													    0.4_3_3_6_4_4_6_5,
													    0.4_3_0_0_1_7_3_2,
													]

													assert image.shape == (1, 5_1_2, 5_1_2, 3)

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


						def   _lowerCamelCase			( self						:  int			):
													a__:       Optional[Any]											="cuda"
													a__:       Optional[Any]											=StableDiffusionPipeline.from_pretrained("runwayml/stable-diffusion-v1-5"			)
													a__:       Optional[Any]											=pipe.to(_a			)
													pipe.set_progress_bar_config(disable=_a			)

													a__:       Optional[Any]											="an image of a crowded boulevard, realistic, 4k"
													a__:       Tuple											={
													    "editing_prompt": "crowd, crowded, people",
													    "reverse_editing_direction": True,
													    "edit_warmup_steps": 1_0,
													    "edit_guidance_scale": 8.3,
													    "edit_threshold": 0.9,
													    "edit_momentum_scale": 0.5,
													    "edit_mom_beta": 0.6,
													}

													a__:       Tuple											=9
													a__:       int											=7

													# no sega enabled
													a__:       int											=torch.Generator(_a			)
													generator.manual_seed(_a			)
													a__:       str											=pipe(
													    [prompt]      ,       generator=_a      ,       guidance_scale=_a      ,       num_inference_steps=5_0      ,       output_type="np"      ,       width=5_1_2      ,       height=5_1_2      ,       )

													a__:       List[Any]											=output.images
													a__:       List[Any]											=image[0, -3:, -3:, -1]
													a__:       int											=[
													    0.4_3_4_9_7_9_9_8,
													    0.9_1_8_1_4_0_6_5,
													    0.7_5_4_0_7_3_9,
													    0.5_5_5_8_0_2_0_5,
													    0.8_4_6_7_2_6_5,
													    0.5_3_8_9_6_9_1,
													    0.6_2_5_7_4_5_0_6,
													    0.5_8_8_9_7_7_6_3,
													    0.5_0_9_2_6_7_5_7,
													]

													assert image.shape == (1, 5_1_2, 5_1_2, 3)

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

													# with sega enabled
													# generator = torch.manual_seed(seed)
													generator.manual_seed(_a			)
													a__:       str											=pipe(
													    [prompt]      ,       generator=_a      ,       guidance_scale=_a      ,       num_inference_steps=5_0      ,       output_type="np"      ,       width=5_1_2      ,       height=5_1_2      ,       **_a      ,       )

													a__:       Union[str, Any]											=output.images
													a__:       str											=image[0, -3:, -3:, -1]
													a__:       Optional[Any]											=[
													    0.3_0_8_9_7_1_9,
													    0.3_0_5_0_0_1_4_4,
													    0.2_9_0_1_6_0_4_2,
													    0.3_0_6_3_0_9_6_4,
													    0.3_2_5_6_8_7,
													    0.2_9_4_1_9_2_2_5,
													    0.2_9_0_8_0_9_1,
													    0.2_8_7_2_3_5_9_8,
													    0.2_7_6_9_6_2_9_4,
													]

													assert image.shape == (1, 5_1_2, 5_1_2, 3)

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


						def   _lowerCamelCase			( self						:  Union[str, Any]			):
													a__:       Any											="cuda"
													a__:       str											=StableDiffusionPipeline.from_pretrained("runwayml/stable-diffusion-v1-5"			)
													a__:       Dict											=pipe.to(_a			)
													pipe.set_progress_bar_config(disable=_a			)

													a__:       Tuple											="a castle next to a river"
													a__:       Union[str, Any]											={
													    "editing_prompt": ["boat on a river, boat", "monet, impression, sunrise"],
													    "reverse_editing_direction": False,
													    "edit_warmup_steps": [1_5, 1_8],
													    "edit_guidance_scale": 6,
													    "edit_threshold": [0.9, 0.8],
													    "edit_momentum_scale": 0.5,
													    "edit_mom_beta": 0.6,
													}

													a__:       Optional[Any]											=4_8
													a__:       Tuple											=7

													# no sega enabled
													a__:       int											=torch.Generator(_a			)
													generator.manual_seed(_a			)
													a__:       Dict											=pipe(
													    [prompt]      ,       generator=_a      ,       guidance_scale=_a      ,       num_inference_steps=5_0      ,       output_type="np"      ,       width=5_1_2      ,       height=5_1_2      ,       )

													a__:       Dict											=output.images
													a__:       Tuple											=image[0, -3:, -3:, -1]
													a__:       str											=[
													    0.7_5_1_6_3_5_5_5,
													    0.7_6_0_3_7_1_4_5,
													    0.6_1_7_8_5,
													    0.9_1_8_9_6_7_3,
													    0.8_6_2_7_7_0_1,
													    0.8_5_1_8_9_6_9_4,
													    0.8_5_1_2_8_1_3,
													    0.8_7_0_1_2_0_7_6,
													    0.8_3_1_2_8_5_7,
													]

													assert image.shape == (1, 5_1_2, 5_1_2, 3)

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

													# with sega enabled
													# generator = torch.manual_seed(seed)
													generator.manual_seed(_a			)
													a__:       str											=pipe(
													    [prompt]      ,       generator=_a      ,       guidance_scale=_a      ,       num_inference_steps=5_0      ,       output_type="np"      ,       width=5_1_2      ,       height=5_1_2      ,       **_a      ,       )

													a__:       Union[str, Any]											=output.images
													a__:       Optional[Any]											=image[0, -3:, -3:, -1]
													a__:       List[Any]											=[
													    0.7_3_5_5_3_3_6_5,
													    0.7_5_3_7_2_7_1,
													    0.7_4_3_4_1_9_0_5,
													    0.6_6_4_8_0_3_5_6,
													    0.6_4_7_2_9_2_5,
													    0.6_3_0_3_9_4_1_6,
													    0.6_4_8_1_2_9_0_5,
													    0.6_7_4_9_7_1_7,
													    0.6_5_1_7_1_0_2,
													]

													assert image.shape == (1, 5_1_2, 5_1_2, 3)

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







						def   _lowerCamelCase			( self						:  List[Any]			):
													a__:       Optional[Any]											="cuda"
													a__:       int											=StableDiffusionPipeline.from_pretrained("runwayml/stable-diffusion-v1-5"      ,       torch_dtype=torch.floataa			)
													a__:       List[Any]											=pipe.to(_a			)
													pipe.set_progress_bar_config(disable=_a			)

													a__:       Union[str, Any]											="a photo of a cat"
													a__:       int											={
													    "editing_prompt": ["sunglasses"],
													    "reverse_editing_direction": [False],
													    "edit_warmup_steps": 1_0,
													    "edit_guidance_scale": 6,
													    "edit_threshold": 0.9_5,
													    "edit_momentum_scale": 0.5,
													    "edit_mom_beta": 0.6,
													}

													a__:       Optional[int]											=3
													a__:       Optional[int]											=7

													# no sega enabled
													a__:       List[Any]											=torch.Generator(_a			)
													generator.manual_seed(_a			)
													a__:       Dict											=pipe(
													    [prompt]      ,       generator=_a      ,       guidance_scale=_a      ,       num_inference_steps=5_0      ,       output_type="np"      ,       width=5_1_2      ,       height=5_1_2      ,       )

													a__:       str											=output.images
													a__:       Any											=image[0, -3:, -3:, -1]
													a__:       Any											=[
													    0.3_4_8_8_7_6_9_5,
													    0.3_8_7_6_9_5_3,
													    0.3_7_5,
													    0.3_4_4_2_3_8_2_8,
													    0.3_5_8_1_5_4_3,
													    0.3_5_7_1_7_7_7_3,
													    0.3_3_8_3_7_8_9,
													    0.3_4_5_7_0_3_1_2,
													    0.3_5_9_3_7_5,
													]

													assert image.shape == (1, 5_1_2, 5_1_2, 3)

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

													# with sega enabled
													# generator = torch.manual_seed(seed)
													generator.manual_seed(_a			)
													a__:       Dict											=pipe(
													    [prompt]      ,       generator=_a      ,       guidance_scale=_a      ,       num_inference_steps=5_0      ,       output_type="np"      ,       width=5_1_2      ,       height=5_1_2      ,       **_a      ,       )

													a__:       Tuple											=output.images
													a__:       int											=image[0, -3:, -3:, -1]
													a__:       Union[str, Any]											=[
													    0.4_2_2_8_5_1_5_6,
													    0.3_6_9_1_4_0_6_2,
													    0.2_9_0_7_7_1_4_8,
													    0.4_2_0_4_1_0_1_6,
													    0.4_1_9_1_8_9_4_5,
													    0.3_5_4_9_8_0_4_7,
													    0.3_6_1_8_1_6_4,
													    0.4_4_2_3_8_2_8,
													    0.4_3_1_1_5_2_3_4,
													]

													assert image.shape == (1, 5_1_2, 5_1_2, 3)

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










from typing import Callable, List, Optional, Union

import torch

from ...models import UNetaDModel
from ...schedulers import CMStochasticIterativeScheduler
from ...utils import (
    is_accelerate_available,
    is_accelerate_version,
    logging,
    randn_tensor,
    replace_example_docstring,
)
from ..pipeline_utils import DiffusionPipeline, ImagePipelineOutput


__UpperCAmelCase						  =     logging.get_logger(__name__)  # pylint: disable=invalid-name


__UpperCAmelCase						  =     '''
    Examples:
        ```py
        >>> import torch

        >>> from diffusers import ConsistencyModelPipeline

        >>> device = "cuda"
        >>> # Load the cd_imagenet64_l2 checkpoint.
        >>> model_id_or_path = "openai/diffusers-cd_imagenet64_l2"
        >>> pipe = ConsistencyModelPipeline.from_pretrained(model_id_or_path, torch_dtype=torch.float16)
        >>> pipe.to(device)

        >>> # Onestep Sampling
        >>> image = pipe(num_inference_steps=1).images[0]
        >>> image.save("cd_imagenet64_l2_onestep_sample.png")

        >>> # Onestep sampling, class-conditional image generation
        >>> # ImageNet-64 class label 145 corresponds to king penguins
        >>> image = pipe(num_inference_steps=1, class_labels=145).images[0]
        >>> image.save("cd_imagenet64_l2_onestep_sample_penguin.png")

        >>> # Multistep sampling, class-conditional image generation
        >>> # Timesteps can be explicitly specified; the particular timesteps below are from the original Github repo:
        >>> # https://github.com/openai/consistency_models/blob/main/scripts/launch.sh#L77
        >>> image = pipe(num_inference_steps=None, timesteps=[22, 0], class_labels=145).images[0]
        >>> image.save("cd_imagenet64_l2_multistep_sample_penguin.png")
        ```
'''


class 	lowerCamelCase__ (		_a   ):


						def __init__( self						:  str      ,       _a						:  UNetaDModel      ,       _a						:  CMStochasticIterativeScheduler			):
													super().__init__()

													self.register_modules(
													    unet=_a      ,       scheduler=_a      ,       )

													a__:       Union[str, Any]											=None


						def   _lowerCamelCase			( self						:  List[Any]      ,       _a						:  Any=0			):
													if is_accelerate_available() and is_accelerate_version(">="      ,       "0.14.0"			):
																				from accelerate import cpu_offload
													else:
																				raise ImportError("`enable_sequential_cpu_offload` requires `accelerate v0.14.0` or higher"			)

													a__:       Optional[Any]											=torch.device(F"cuda:{gpu_id}"			)

													if self.device.type != "cpu":
																				self.to("cpu"      ,       silence_dtype_warnings=_a			)
																				torch.cuda.empty_cache()  # otherwise we don't see the memory savings (but they probably exist)

													for cpu_offloaded_model in [self.unet]:
																				cpu_offload(_a      ,       _a			)

													if self.safety_checker is not None:
																				cpu_offload(self.safety_checker      ,       execution_device=_a      ,       offload_buffers=_a			)


						def   _lowerCamelCase			( self						:  List[str]      ,       _a						:  Optional[int]=0			):
													if is_accelerate_available() and is_accelerate_version(">="      ,       "0.17.0.dev0"			):
																				from accelerate import cpu_offload_with_hook
													else:
																				raise ImportError("`enable_model_cpu_offload` requires `accelerate v0.17.0` or higher."			)

													a__:       Union[str, Any]											=torch.device(F"cuda:{gpu_id}"			)

													if self.device.type != "cpu":
																				self.to("cpu"      ,       silence_dtype_warnings=_a			)
																				torch.cuda.empty_cache()  # otherwise we don't see the memory savings (but they probably exist)

													a__:       Optional[Any]											=None
													for cpu_offloaded_model in [self.unet]:
																				a__							,			a__:       Optional[Any]											=cpu_offload_with_hook(_a      ,       _a      ,       prev_module_hook=_a			)

													if self.safety_checker is not None:
																				a__							,			a__:       Optional[Any]											=cpu_offload_with_hook(self.safety_checker      ,       _a      ,       prev_module_hook=_a			)

													# We'll offload the last model manually.
													a__:       Optional[Any]											=hook


						@property
						# Copied from diffusers.pipelines.stable_diffusion.pipeline_stable_diffusion.StableDiffusionPipeline._execution_device
						def   _lowerCamelCase			( self						:  Tuple			):
													if not hasattr(self.unet      ,       "_hf_hook"			):
																				return self.device
													for module in self.unet.modules():
																				if (
																				    hasattr(_a      ,       "_hf_hook"			)
																				    and hasattr(module._hf_hook      ,       "execution_device"			)
																				    and module._hf_hook.execution_device is not None
																				):
																											return torch.device(module._hf_hook.execution_device			)
													return self.device


						def   _lowerCamelCase			( self						:  Dict      ,       _a						:  List[Any]      ,       _a						:  Optional[int]      ,       _a						:  Dict      ,       _a						:  List[Any]      ,       _a						:  List[str]      ,       _a						:  Union[str, Any]      ,       _a						:  str      ,       _a						:  int=None			):
													a__:       List[str]											=(batch_size, num_channels, height, width)
													if isinstance(_a      ,       _a			) and len(_a			) != batch_size:
																				raise ValueError(
																				    F"You have passed a list of generators of length {len(_a			)}, but requested an effective batch"
																				    F" size of {batch_size}. Make sure the batch size matches the length of the generators."			)

													if latents is None:
																				a__:       Dict											=randn_tensor(_a      ,       generator=_a      ,       device=_a      ,       dtype=_a			)
													else:
																				a__:       List[Any]											=latents.to(device=_a      ,       dtype=_a			)

													# scale the initial noise by the standard deviation required by the scheduler
													a__:       int											=latents * self.scheduler.init_noise_sigma
													return latents


						def   _lowerCamelCase			( self						:  Dict      ,       _a						:  torch.FloatTensor      ,       _a						:  str = "pil"			):
													if output_type not in ["pt", "np", "pil"]:
																				raise ValueError(
																				    F"output_type={output_type} is not supported. Make sure to choose one of ['pt', 'np', or 'pil']"			)

													# Equivalent to diffusers.VaeImageProcessor.denormalize
													a__:       Tuple											=(sample / 2 + 0.5).clamp(0      ,       1			)
													if output_type == "pt":
																				return sample

													# Equivalent to diffusers.VaeImageProcessor.pt_to_numpy
													a__:       Tuple											=sample.cpu().permute(0      ,       2      ,       3      ,       1			).numpy()
													if output_type == "np":
																				return sample

													# Output_type must be 'pil'
													a__:       List[Any]											=self.numpy_to_pil(_a			)
													return sample


						def   _lowerCamelCase			( self						:  Tuple      ,       _a						:  Dict      ,       _a						:  Optional[Any]      ,       _a						:  List[str]=None			):
													if self.unet.config.num_class_embeds is not None:
																				if isinstance(_a      ,       _a			):
																											a__:       List[str]											=torch.tensor(_a      ,       dtype=torch.int			)
																				elif isinstance(_a      ,       _a			):
																											assert batch_size == 1, "Batch size must be 1 if classes is an int"
																											a__:       List[str]											=torch.tensor([class_labels]      ,       dtype=torch.int			)
																				elif class_labels is None:
																											# Randomly generate batch_size class labels
																											# TODO: should use generator here? int analogue of randn_tensor is not exposed in ...utils
																											a__:       Union[str, Any]											=torch.randint(0      ,       self.unet.config.num_class_embeds      ,       size=(batch_size,)			)
																				a__:       Dict											=class_labels.to(_a			)
													else:
																				a__:       List[str]											=None
													return class_labels


						def   _lowerCamelCase			( self						:  Union[str, Any]      ,       _a						:  Optional[Any]      ,       _a						:  Optional[int]      ,       _a						:  List[Any]      ,       _a						:  List[str]      ,       _a						:  Optional[int]      ,       _a						:  Optional[Any]			):
													if num_inference_steps is None and timesteps is None:
																				raise ValueError("Exactly one of `num_inference_steps` or `timesteps` must be supplied."			)

													if num_inference_steps is not None and timesteps is not None:
																				logger.warning(
																				    F"Both `num_inference_steps`: {num_inference_steps} and `timesteps`: {timesteps} are supplied;"
																				    " `timesteps` will be used over `num_inference_steps`."			)

													if latents is not None:
																				a__:       Tuple											=(batch_size, 3, img_size, img_size)
																				if latents.shape != expected_shape:
																											raise ValueError(F"The shape of latents is {latents.shape} but is expected to be {expected_shape}."			)

													if (callback_steps is None) or (
													    callback_steps is not None and (not isinstance(_a      ,       _a			) or callback_steps <= 0)
													):
																				raise ValueError(
																				    F"`callback_steps` has to be a positive integer but is {callback_steps} of type"
																				    F" {type(_a			)}."			)







						@torch.no_grad()
						@replace_example_docstring(_a			)
						def __call__( self						:  Dict      ,       _a						:  int = 1      ,       _a						:  Optional[Union[torch.Tensor, List[int], int]] = None      ,       _a						:  int = 1      ,       _a						:  List[int] = None      ,       _a						:  Optional[Union[torch.Generator, List[torch.Generator]]] = None      ,       _a						:  Optional[torch.FloatTensor] = None      ,       _a						:  Optional[str] = "pil"      ,       _a						:  bool = True      ,       _a						:  Optional[Callable[[int, int, torch.FloatTensor], None]] = None      ,       _a						:  int = 1      ,       ):
													a__:       int											=self.unet.config.sample_size
													a__:       Union[str, Any]											=self._execution_device

													# 1. Check inputs
													self.check_inputs(_a      ,       _a      ,       _a      ,       _a      ,       _a      ,       _a			)

													# 2. Prepare image latents
													# Sample image latents x_0 ~ N(0, sigma_0^2 * I)
													a__:       Tuple											=self.prepare_latents(
													    batch_size=_a      ,       num_channels=self.unet.config.in_channels      ,       height=_a      ,       width=_a      ,       dtype=self.unet.dtype      ,       device=_a      ,       generator=_a      ,       latents=_a      ,       )

													# 3. Handle class_labels for class-conditional models
													a__:       int											=self.prepare_class_labels(_a      ,       _a      ,       class_labels=_a			)

													# 4. Prepare timesteps
													if timesteps is not None:
																				self.scheduler.set_timesteps(timesteps=_a      ,       device=_a			)
																				a__:       str											=self.scheduler.timesteps
																				a__:       List[str]											=len(_a			)
													else:
																				self.scheduler.set_timesteps(_a			)
																				a__:       Optional[int]											=self.scheduler.timesteps

													# 5. Denoising loop
													# Multistep sampling: implements Algorithm 1 in the paper
													with self.progress_bar(total=_a			) as progress_bar:
																				for i, t in enumerate(_a			):
																											a__:       Any											=self.scheduler.scale_model_input(_a      ,       _a			)
																											a__:       Dict											=self.unet(_a      ,       _a      ,       class_labels=_a      ,       return_dict=_a			)[0]

																											a__:       int											=self.scheduler.step(_a      ,       _a      ,       _a      ,       generator=_a			)[0]

																											# call the callback, if provided
																											progress_bar.update()
																											if callback is not None and i % callback_steps == 0:
																																		callback(_a      ,       _a      ,       _a			)

        # 6. Post-process image sample
													a__:       List[str]											=self.postprocess_image(_a      ,       output_type=_a			)

													# Offload last model to CPU
													if hasattr(self      ,       "final_offload_hook"			) and self.final_offload_hook is not None:
																				self.final_offload_hook.offload()

													if not return_dict:
																				return (image,)

													return ImagePipelineOutput(images=_a			)





