| from typing import Optional, Union, List, Tuple, Callable |
|
|
| import torch |
| from diffusers.utils.torch_utils import randn_tensor |
| from diffusers.pipelines.pipeline_utils import DiffusionPipeline, ImagePipelineOutput |
|
|
| class KarrasEDMConditionalPipeline(DiffusionPipeline): |
| r""" |
| Pipeline for unconditional or class-conditional image generation based on the EDM model from [1]. |
| This model inherits from [`DiffusionPipeline`]. Check the superclass documentation for the generic methods |
| implemented for all pipelines (downloading, saving, running on a particular device, etc.). |
| [1] Karras, Tero, et al. "Elucidating the Design Space of Diffusion-Based Generative Models." |
| https://arxiv.org/abs/2206.00364 |
| Args: |
| unet ([`UNet2DModel`]): |
| A `UNet2DModel` to denoise the encoded image latents. |
| scheduler ([`SchedulerMixin`]): |
| A scheduler to be used in combination with `unet` to denoise the encoded image latents. Currently only |
| supports KarrasEDMScheduler. |
| """ |
|
|
| model_cpu_offload_seq = "unet" |
|
|
| def __init__(self, unet, scheduler) -> None: |
| super().__init__() |
|
|
| self.register_modules( |
| unet=unet, |
| scheduler=scheduler, |
| ) |
|
|
| self.safety_checker = None |
|
|
| def prepare_latents(self, batch_size, num_channels, height, width, dtype, device, generator, latents=None): |
| shape = (batch_size, num_channels, height, width) |
| if isinstance(generator, list) and len(generator) != batch_size: |
| raise ValueError( |
| f"You have passed a list of generators of length {len(generator)}, 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: |
| latents = randn_tensor(shape, generator=generator, device=device, dtype=dtype) |
| else: |
| latents = latents.to(device=device, dtype=dtype) |
|
|
| |
| latents = latents * self.scheduler.init_noise_sigma |
| return latents |
|
|
| |
| def postprocess_image(self, sample: torch.FloatTensor, output_type: 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']" |
| ) |
|
|
| |
| sample = (sample / 2 + 0.5).clamp(0, 1) |
| if output_type == "pt": |
| return sample |
|
|
| |
| sample = sample.cpu().permute(0, 2, 3, 1).numpy() |
| if output_type == "np": |
| return sample |
|
|
| |
| sample = self.numpy_to_pil(sample) |
| return sample |
|
|
| def check_inputs(self, num_inference_steps, timesteps, latents, batch_size, img_size, callback_steps): |
| 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: |
| expected_shape = (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(callback_steps, int) or callback_steps <= 0) |
| ): |
| raise ValueError( |
| f"`callback_steps` has to be a positive integer but is {callback_steps} of type" |
| f" {type(callback_steps)}." |
| ) |
|
|
| @torch.no_grad() |
| def __call__( |
| self, |
| batch_size: int = 1, |
| num_inference_steps: int = 1, |
| timesteps: List[int] = None, |
| class_labels: Optional[torch.Tensor] = None, |
| generator: Optional[Union[torch.Generator, List[torch.Generator]]] = None, |
| latents: Optional[torch.FloatTensor] = None, |
| output_type: Optional[str] = "pil", |
| return_dict: bool = True, |
| callback: Optional[Callable[[int, int, torch.FloatTensor], None]] = None, |
| callback_steps: int = 1, |
| ): |
| r""" |
| Args: |
| batch_size (`int`, *optional*, defaults to 1): |
| The number of images to generate. |
| class_labels (`torch.Tensor` or `List[int]` or `int`, *optional*): |
| Optional class labels for conditioning class-conditional consistency models. Not used if the model is |
| not class-conditional. |
| num_inference_steps (`int`, *optional*, defaults to 1): |
| The number of denoising steps. More denoising steps usually lead to a higher quality image at the |
| expense of slower inference. |
| timesteps (`List[int]`, *optional*): |
| Custom timesteps to use for the denoising process. If not defined, equal spaced `num_inference_steps` |
| timesteps are used. Must be in descending order. |
| generator (`torch.Generator`, *optional*): |
| A [`torch.Generator`](https://pytorch.org/docs/stable/generated/torch.Generator.html) to make |
| generation deterministic. |
| latents (`torch.FloatTensor`, *optional*): |
| Pre-generated noisy latents sampled from a Gaussian distribution, to be used as inputs for image |
| generation. Can be used to tweak the same generation with different prompts. If not provided, a latents |
| tensor is generated by sampling using the supplied random `generator`. |
| output_type (`str`, *optional*, defaults to `"pil"`): |
| The output format of the generated image. Choose between `PIL.Image` or `np.array`. |
| return_dict (`bool`, *optional*, defaults to `True`): |
| Whether or not to return a [`~pipelines.ImagePipelineOutput`] instead of a plain tuple. |
| callback (`Callable`, *optional*): |
| A function that calls every `callback_steps` steps during inference. The function is called with the |
| following arguments: `callback(step: int, timestep: int, latents: torch.FloatTensor)`. |
| callback_steps (`int`, *optional*, defaults to 1): |
| The frequency at which the `callback` function is called. If not specified, the callback is called at |
| every step. |
| Examples: |
| Returns: |
| [`~pipelines.ImagePipelineOutput`] or `tuple`: |
| If `return_dict` is `True`, [`~pipelines.ImagePipelineOutput`] is returned, otherwise a `tuple` is |
| returned where the first element is a list with the generated images. |
| """ |
| |
| img_size = self.unet.config.sample_size |
| device = self._execution_device |
|
|
|
|
|
|
| |
| |
| sample = self.prepare_latents( |
| batch_size=batch_size, |
| num_channels=self.unet.config.in_channels, |
| height=img_size, |
| width=img_size, |
| dtype=self.unet.dtype, |
| device=device, |
| generator=generator, |
| latents=latents, |
| ) |
|
|
| |
| if timesteps is not None: |
| self.scheduler.set_timesteps(timesteps=timesteps, device=device) |
| timesteps = self.scheduler.timesteps |
| num_inference_steps = len(timesteps) |
| else: |
| self.scheduler.set_timesteps(num_inference_steps) |
| timesteps = self.scheduler.timesteps |
|
|
| |
| |
| num_warmup_steps = len(timesteps) - num_inference_steps * self.scheduler.order |
|
|
| |
| t_init = timesteps[0] |
| scaled_sample_first_step = self.scheduler.scale_model_input(sample / self.scheduler.init_noise_sigma, t_init) |
| if self.scheduler.step_index is None: |
| self.scheduler._init_step_index(t_init) |
| sigma = self.scheduler.sigmas[self.scheduler.step_index] |
| sigma_input = self.scheduler.precondition_noise(sigma) |
| model_output = self.unet(scaled_sample_first_step, sigma_input.squeeze(), class_labels, return_dict=False)[0] |
| sample = self.scheduler.step(model_output, t_init, sample).prev_sample |
| |
|
|
| with self.progress_bar(total=num_inference_steps) as progress_bar: |
| for i, t in enumerate(timesteps[1:]): |
| |
| scaled_sample = self.scheduler.scale_model_input(sample, t) |
|
|
| if self.scheduler.step_index is None: |
| self.scheduler._init_step_index(t) |
|
|
| sigma = self.scheduler.sigmas[self.scheduler.step_index] |
|
|
| sigma_input = self.scheduler.precondition_noise(sigma) |
|
|
| |
| model_output = self.unet(scaled_sample, sigma_input.squeeze(), class_labels, return_dict=False)[0] |
|
|
| |
| |
| sample = self.scheduler.step(model_output, t, sample).prev_sample |
|
|
| |
| if i == len(timesteps) - 1 or ((i + 1) > num_warmup_steps and (i + 1) % self.scheduler.order == 0): |
| progress_bar.update() |
| if callback is not None and i % callback_steps == 0: |
| callback(i, t, latents) |
|
|
| |
| image = self.postprocess_image(sample, output_type=output_type) |
|
|
| |
| self.maybe_free_model_hooks() |
|
|
| if not return_dict: |
| return (image,) |
|
|
| return ImagePipelineOutput(images=image) |