Würstchen: Efficient Pretraining of Text-to-Image Models is by Pablo Pernias, Dominic Rampas, Mats L. Richter and Christopher Pal and Marc Aubreville.
The abstract from the paper is:
We introduce Würstchen, a novel technique for text-to-image synthesis that unites competitive performance with unprecedented cost-effectiveness and ease of training on constrained hardware. Building on recent advancements in machine learning, our approach, which utilizes latent diffusion strategies at strong latent image compression rates, significantly reduces the computational burden, typically associated with state-of-the-art models, while preserving, if not enhancing, the quality of generated images. Wuerstchen achieves notable speed improvements at inference time, thereby rendering real-time applications more viable. One of the key advantages of our method lies in its modest training requirements of only 9,200 GPU hours, slashing the usual costs significantly without compromising the end performance. In a comparison against the state-of-the-art, we found the approach to yield strong competitiveness. This paper opens the door to a new line of research that prioritizes both performance and computational accessibility, hence democratizing the use of sophisticated AI technologies. Through Wuerstchen, we demonstrate a compelling stride forward in the realm of text-to-image synthesis, offering an innovative path to explore in future research.
Würstchen is a diffusion model, whose text-conditional model works in a highly compressed latent space of images. Why is this important? Compressing data can reduce computational costs for both training and inference by magnitudes. Training on 1024x1024 images is way more expensive than training on 32x32. Usually, other works make use of a relatively small compression, in the range of 4x - 8x spatial compression. Würstchen takes this to an extreme. Through its novel design, we achieve a 42x spatial compression. This was unseen before because common methods fail to faithfully reconstruct detailed images after 16x spatial compression. Würstchen employs a two-stage compression, what we call Stage A and Stage B. Stage A is a VQGAN, and Stage B is a Diffusion Autoencoder (more details can be found in the paper ). A third model, Stage C, is learned in that highly compressed latent space. This training requires fractions of the compute used for current top-performing models, while also allowing cheaper and faster inference.
After the initial paper release, we have improved numerous things in the architecture, training and sampling, making Würstchen competitive to current state-of-the-art models in many ways. We are excited to release this new version together with Diffusers. Here is a list of the improvements.
We are releasing 3 checkpoints for the text-conditional image generation model (Stage C). Those are:
We recommend using v2-interpolated, as it has a nice touch of both photorealism and aesthetics. Use v2-base for finetunings as it does not have a style bias and use v2-aesthetic for very artistic generations. A comparison can be seen here:
For the sake of usability, Würstchen can be used with a single pipeline. This pipeline can be used as follows:
import torch
from diffusers import AutoPipelineForText2Image
from diffusers.pipelines.wuerstchen import DEFAULT_STAGE_C_TIMESTEPS
pipe = AutoPipelineForText2Image.from_pretrained("warp-ai/wuerstchen", torch_dtype=torch.float16).to("cuda")
caption = "Anthropomorphic cat dressed as a fire fighter"
images = pipe(
caption,
width=1024,
height=1536,
prior_timesteps=DEFAULT_STAGE_C_TIMESTEPS,
prior_guidance_scale=4.0,
num_images_per_prompt=2,
).images
For explanation purposes, we can also initialize the two main pipelines of Würstchen individually. Würstchen consists of 3 stages: Stage C, Stage B, Stage A. They all have different jobs and work only together. When generating text-conditional images, Stage C will first generate the latents in a very compressed latent space. This is what happens in the prior_pipeline
. Afterwards, the generated latents will be passed to Stage B, which decompresses the latents into a bigger latent space of a VQGAN. These latents can then be decoded by Stage A, which is a VQGAN, into the pixel-space. Stage B & Stage A are both encapsulated in the decoder_pipeline
. For more details, take a look at the paper.
import torch
from diffusers import WuerstchenDecoderPipeline, WuerstchenPriorPipeline
from diffusers.pipelines.wuerstchen import DEFAULT_STAGE_C_TIMESTEPS
device = "cuda"
dtype = torch.float16
num_images_per_prompt = 2
prior_pipeline = WuerstchenPriorPipeline.from_pretrained(
"warp-ai/wuerstchen-prior", torch_dtype=dtype
).to(device)
decoder_pipeline = WuerstchenDecoderPipeline.from_pretrained(
"warp-ai/wuerstchen", torch_dtype=dtype
).to(device)
caption = "Anthropomorphic cat dressed as a fire fighter"
negative_prompt = ""
prior_output = prior_pipeline(
prompt=caption,
height=1024,
width=1536,
timesteps=DEFAULT_STAGE_C_TIMESTEPS,
negative_prompt=negative_prompt,
guidance_scale=4.0,
num_images_per_prompt=num_images_per_prompt,
)
decoder_output = decoder_pipeline(
image_embeddings=prior_output.image_embeddings,
prompt=caption,
negative_prompt=negative_prompt,
guidance_scale=0.0,
output_type="pil",
).images
You can make use of torch.compile
function and gain a speed-up of about 2-3x:
prior_pipeline.prior = torch.compile(prior_pipeline.prior, mode="reduce-overhead", fullgraph=True)
decoder_pipeline.decoder = torch.compile(decoder_pipeline.decoder, mode="reduce-overhead", fullgraph=True)
The original codebase, as well as experimental ideas, can be found at dome272/Wuerstchen.
( tokenizer: CLIPTokenizer text_encoder: CLIPTextModel decoder: WuerstchenDiffNeXt scheduler: DDPMWuerstchenScheduler vqgan: PaellaVQModel prior_tokenizer: CLIPTokenizer prior_text_encoder: CLIPTextModel prior_prior: WuerstchenPrior prior_scheduler: DDPMWuerstchenScheduler )
Parameters
CLIPTokenizer
) —
The decoder tokenizer to be used for text inputs. CLIPTextModel
) —
The decoder text encoder to be used for text inputs. WuerstchenDiffNeXt
) —
The decoder model to be used for decoder image generation pipeline. DDPMWuerstchenScheduler
) —
The scheduler to be used for decoder image generation pipeline. PaellaVQModel
) —
The VQGAN model to be used for decoder image generation pipeline. CLIPTokenizer
) —
The prior tokenizer to be used for text inputs. CLIPTextModel
) —
The prior text encoder to be used for text inputs. WuerstchenPrior
) —
The prior model to be used for prior pipeline. DDPMWuerstchenScheduler
) —
The scheduler to be used for prior pipeline. Combined Pipeline for text-to-image generation using Wuerstchen
This model inherits from DiffusionPipeline. Check the superclass documentation for the generic methods the library implements for all the pipelines (such as downloading or saving, running on a particular device, etc.)
( prompt: typing.Union[str, typing.List[str], NoneType] = None height: int = 512 width: int = 512 prior_num_inference_steps: int = 60 prior_timesteps: typing.Optional[typing.List[float]] = None prior_guidance_scale: float = 4.0 num_inference_steps: int = 12 decoder_timesteps: typing.Optional[typing.List[float]] = None decoder_guidance_scale: float = 0.0 negative_prompt: typing.Union[str, typing.List[str], NoneType] = None prompt_embeds: typing.Optional[torch.FloatTensor] = None negative_prompt_embeds: typing.Optional[torch.FloatTensor] = None num_images_per_prompt: int = 1 generator: typing.Union[torch._C.Generator, typing.List[torch._C.Generator], NoneType] = None latents: typing.Optional[torch.FloatTensor] = None output_type: typing.Optional[str] = 'pil' return_dict: bool = True prior_callback: typing.Union[typing.Callable[[int, int, torch.FloatTensor], NoneType], NoneType] = None prior_callback_steps: int = 1 callback: typing.Union[typing.Callable[[int, int, torch.FloatTensor], NoneType], NoneType] = None callback_steps: int = 1 )
Parameters
str
or List[str]
) —
The prompt or prompts to guide the image generation for the prior and decoder. str
or List[str]
, optional) —
The prompt or prompts not to guide the image generation. Ignored when not using guidance (i.e., ignored
if guidance_scale
is less than 1
). torch.FloatTensor
, optional) —
Pre-generated text embeddings for the prior. Can be used to easily tweak text inputs, e.g. prompt
weighting. If not provided, text embeddings will be generated from prompt
input argument. torch.FloatTensor
, optional) —
Pre-generated negative text embeddings for the prior. Can be used to easily tweak text inputs, e.g.
prompt weighting. If not provided, negative_prompt_embeds will be generated from negative_prompt
input argument. int
, optional, defaults to 1) —
The number of images to generate per prompt. int
, optional, defaults to 512) —
The height in pixels of the generated image. int
, optional, defaults to 512) —
The width in pixels of the generated image. float
, optional, defaults to 4.0) —
Guidance scale as defined in Classifier-Free Diffusion Guidance.
prior_guidance_scale
is defined as w
of equation 2. of Imagen
Paper. Guidance scale is enabled by setting
prior_guidance_scale > 1
. Higher guidance scale encourages to generate images that are closely linked
to the text prompt
, usually at the expense of lower image quality. Union[int, Dict[float, int]]
, optional, defaults to 60) —
The number of prior denoising steps. More denoising steps usually lead to a higher quality image at the
expense of slower inference. For more specific timestep spacing, you can pass customized
prior_timesteps
int
, optional, defaults to 12) —
The number of decoder denoising steps. More denoising steps usually lead to a higher quality image at
the expense of slower inference. For more specific timestep spacing, you can pass customized
timesteps
List[float]
, optional) —
Custom timesteps to use for the denoising process for the prior. If not defined, equal spaced
prior_num_inference_steps
timesteps are used. Must be in descending order. List[float]
, optional) —
Custom timesteps to use for the denoising process for the decoder. If not defined, equal spaced
num_inference_steps
timesteps are used. Must be in descending order. float
, optional, defaults to 0.0) —
Guidance scale as defined in Classifier-Free Diffusion Guidance.
guidance_scale
is defined as w
of equation 2. of Imagen
Paper. Guidance scale is enabled by setting guidance_scale > 1
. Higher guidance scale encourages to generate images that are closely linked to the text prompt
,
usually at the expense of lower image quality. torch.Generator
or List[torch.Generator]
, optional) —
One or a list of torch generator(s)
to make generation deterministic. 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 will ge generated by sampling using the supplied random generator
. str
, optional, defaults to "pil"
) —
The output format of the generate image. Choose between: "pil"
(PIL.Image.Image
), "np"
(np.array
) or "pt"
(torch.Tensor
). bool
, optional, defaults to True
) —
Whether or not to return a ImagePipelineOutput instead of a plain tuple. Callable
, optional) —
A function that will be called every prior_callback_steps
steps during inference. The function will
be called with the following arguments: prior_callback(step: int, timestep: int, latents: torch.FloatTensor)
. int
, optional, defaults to 1) —
The frequency at which the callback
function will be called. If not specified, the callback will be
called at every step. Callable
, optional) —
A function that will be called every callback_steps
steps during inference. The function will be
called with the following arguments: callback(step: int, timestep: int, latents: torch.FloatTensor)
. int
, optional, defaults to 1) —
The frequency at which the callback
function will be called. If not specified, the callback will be
called at every step. Function invoked when calling the pipeline for generation.
Offloads all models to CPU using accelerate, reducing memory usage with a low impact on performance. Compared
to enable_sequential_cpu_offload
, this method moves one whole model at a time to the GPU when its forward
method is called, and the model remains in GPU until the next model runs. Memory savings are lower than with
enable_sequential_cpu_offload
, but performance is much better due to the iterative execution of the unet
.
Offloads all models (unet
, text_encoder
, vae
, and safety checker
state dicts) to CPU using 🤗
Accelerate, significantly reducing memory usage. Models are moved to a torch.device('meta')
and loaded on a
GPU only when their specific submodule’s forward
method is called. Offloading happens on a submodule basis.
Memory savings are higher than using enable_model_cpu_offload
, but performance is lower.
( tokenizer: CLIPTokenizer text_encoder: CLIPTextModel prior: WuerstchenPrior scheduler: DDPMWuerstchenScheduler latent_mean: float = 42.0 latent_std: float = 1.0 resolution_multiple: float = 42.67 )
Parameters
Prior
) —
The canonical unCLIP prior to approximate the image embedding from the text embedding. CLIPTextModelWithProjection
) —
Frozen text-encoder. CLIPTokenizer
) —
Tokenizer of class
CLIPTokenizer. DDPMWuerstchenScheduler
) —
A scheduler to be used in combination with prior
to generate image embedding. Pipeline for generating image prior for Wuerstchen.
This model inherits from DiffusionPipeline. Check the superclass documentation for the generic methods the library implements for all the pipelines (such as downloading or saving, running on a particular device, etc.)
( prompt: typing.Union[str, typing.List[str], NoneType] = None height: int = 1024 width: int = 1024 num_inference_steps: int = 60 timesteps: typing.List[float] = None guidance_scale: float = 8.0 negative_prompt: typing.Union[str, typing.List[str], NoneType] = None prompt_embeds: typing.Optional[torch.FloatTensor] = None negative_prompt_embeds: typing.Optional[torch.FloatTensor] = None num_images_per_prompt: typing.Optional[int] = 1 generator: typing.Union[torch._C.Generator, typing.List[torch._C.Generator], NoneType] = None latents: typing.Optional[torch.FloatTensor] = None output_type: typing.Optional[str] = 'pt' return_dict: bool = True callback: typing.Union[typing.Callable[[int, int, torch.FloatTensor], NoneType], NoneType] = None callback_steps: int = 1 )
Parameters
str
or List[str]
) —
The prompt or prompts to guide the image generation. int
, optional, defaults to 1024) —
The height in pixels of the generated image. int
, optional, defaults to 1024) —
The width in pixels of the generated image. int
, optional, defaults to 60) —
The number of denoising steps. More denoising steps usually lead to a higher quality image at the
expense of slower inference. 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. float
, optional, defaults to 8.0) —
Guidance scale as defined in Classifier-Free Diffusion Guidance.
decoder_guidance_scale
is defined as w
of equation 2. of Imagen
Paper. Guidance scale is enabled by setting
decoder_guidance_scale > 1
. Higher guidance scale encourages to generate images that are closely
linked to the text prompt
, usually at the expense of lower image quality. str
or List[str]
, optional) —
The prompt or prompts not to guide the image generation. Ignored when not using guidance (i.e., ignored
if decoder_guidance_scale
is less than 1
). torch.FloatTensor
, optional) —
Pre-generated text embeddings. Can be used to easily tweak text inputs, e.g. prompt weighting. If not
provided, text embeddings will be generated from prompt
input argument. torch.FloatTensor
, optional) —
Pre-generated negative text embeddings. Can be used to easily tweak text inputs, e.g. prompt
weighting. If not provided, negative_prompt_embeds will be generated from negative_prompt
input
argument. int
, optional, defaults to 1) —
The number of images to generate per prompt. torch.Generator
or List[torch.Generator]
, optional) —
One or a list of torch generator(s)
to make generation deterministic. 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 will ge generated by sampling using the supplied random generator
. str
, optional, defaults to "pil"
) —
The output format of the generate image. Choose between: "pil"
(PIL.Image.Image
), "np"
(np.array
) or "pt"
(torch.Tensor
). bool
, optional, defaults to True
) —
Whether or not to return a ImagePipelineOutput instead of a plain tuple. Callable
, optional) —
A function that will be called every callback_steps
steps during inference. The function will be
called with the following arguments: callback(step: int, timestep: int, latents: torch.FloatTensor)
. int
, optional, defaults to 1) —
The frequency at which the callback
function will be called. If not specified, the callback will be
called at every step. Function invoked when calling the pipeline for generation.
Examples:
>>> import torch
>>> from diffusers import WuerstchenPriorPipeline
>>> prior_pipe = WuerstchenPriorPipeline.from_pretrained(
... "warp-ai/wuerstchen-prior", torch_dtype=torch.float16
... ).to("cuda")
>>> prompt = "an image of a shiba inu, donning a spacesuit and helmet"
>>> prior_output = pipe(prompt)
( image_embeddings: typing.Union[torch.FloatTensor, numpy.ndarray] )
Output class for WuerstchenPriorPipeline.
( tokenizer: CLIPTokenizer text_encoder: CLIPTextModel decoder: WuerstchenDiffNeXt scheduler: DDPMWuerstchenScheduler vqgan: PaellaVQModel latent_dim_scale: float = 10.67 )
Parameters
CLIPTokenizer
) —
The CLIP tokenizer. CLIPTextModel
) —
The CLIP text encoder. WuerstchenDiffNeXt
) —
The WuerstchenDiffNeXt unet decoder. PaellaVQModel
) —
The VQGAN model. DDPMWuerstchenScheduler
) —
A scheduler to be used in combination with prior
to generate image embedding. optional
, defaults to 10.67) —
Multiplier to determine the VQ latent space size from the image embeddings. If the image embeddings are
height=24 and width=24, the VQ latent shape needs to be height=int(2410.67)=256 and
width=int(2410.67)=256 in order to match the training conditions. Pipeline for generating images from the Wuerstchen model.
This model inherits from DiffusionPipeline. Check the superclass documentation for the generic methods the library implements for all the pipelines (such as downloading or saving, running on a particular device, etc.)
( image_embeddings: typing.Union[torch.FloatTensor, typing.List[torch.FloatTensor]] prompt: typing.Union[str, typing.List[str]] = None num_inference_steps: int = 12 timesteps: typing.Optional[typing.List[float]] = None guidance_scale: float = 0.0 negative_prompt: typing.Union[str, typing.List[str], NoneType] = None num_images_per_prompt: int = 1 generator: typing.Union[torch._C.Generator, typing.List[torch._C.Generator], NoneType] = None latents: typing.Optional[torch.FloatTensor] = None output_type: typing.Optional[str] = 'pil' return_dict: bool = True callback: typing.Union[typing.Callable[[int, int, torch.FloatTensor], NoneType], NoneType] = None callback_steps: int = 1 )
Parameters
torch.FloatTensor
or List[torch.FloatTensor]
) —
Image Embeddings either extracted from an image or generated by a Prior Model. str
or List[str]
) —
The prompt or prompts to guide the image generation. int
, optional, defaults to 12) —
The number of denoising steps. More denoising steps usually lead to a higher quality image at the
expense of slower inference. 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. float
, optional, defaults to 0.0) —
Guidance scale as defined in Classifier-Free Diffusion Guidance.
decoder_guidance_scale
is defined as w
of equation 2. of Imagen
Paper. Guidance scale is enabled by setting
decoder_guidance_scale > 1
. Higher guidance scale encourages to generate images that are closely
linked to the text prompt
, usually at the expense of lower image quality. str
or List[str]
, optional) —
The prompt or prompts not to guide the image generation. Ignored when not using guidance (i.e., ignored
if decoder_guidance_scale
is less than 1
). int
, optional, defaults to 1) —
The number of images to generate per prompt. torch.Generator
or List[torch.Generator]
, optional) —
One or a list of torch generator(s)
to make generation deterministic. 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 will ge generated by sampling using the supplied random generator
. str
, optional, defaults to "pil"
) —
The output format of the generate image. Choose between: "pil"
(PIL.Image.Image
), "np"
(np.array
) or "pt"
(torch.Tensor
). bool
, optional, defaults to True
) —
Whether or not to return a ImagePipelineOutput instead of a plain tuple. Callable
, optional) —
A function that will be called every callback_steps
steps during inference. The function will be
called with the following arguments: callback(step: int, timestep: int, latents: torch.FloatTensor)
. int
, optional, defaults to 1) —
The frequency at which the callback
function will be called. If not specified, the callback will be
called at every step. Function invoked when calling the pipeline for generation.
Examples:
>>> import torch
>>> from diffusers import WuerstchenPriorPipeline, WuerstchenDecoderPipeline
>>> prior_pipe = WuerstchenPriorPipeline.from_pretrained(
... "warp-ai/wuerstchen-prior", torch_dtype=torch.float16
... ).to("cuda")
>>> gen_pipe = WuerstchenDecoderPipeline.from_pretrain("warp-ai/wuerstchen", torch_dtype=torch.float16).to(
... "cuda"
... )
>>> prompt = "an image of a shiba inu, donning a spacesuit and helmet"
>>> prior_output = pipe(prompt)
>>> images = gen_pipe(prior_output.image_embeddings, prompt=prompt)
@misc{pernias2023wuerstchen,
title={Wuerstchen: Efficient Pretraining of Text-to-Image Models},
author={Pablo Pernias and Dominic Rampas and Mats L. Richter and Christopher Pal and Marc Aubreville},
year={2023},
eprint={2306.00637},
archivePrefix={arXiv},
primaryClass={cs.CV}
}