text stringlengths 0 5.54k |
|---|
Gaudi2 |
1.70s |
0.925 images/s |
Stable Diffusion v2.1 (768x768 resolution): |
Latency (batch size = 1) |
Throughput |
first-generation Gaudi |
23.3s |
0.045 images/s (batch size = 2) |
Gaudi2 |
7.75s |
0.14 images/s (batch size = 5) |
Accelerate inference of text-to-image diffusion models Diffusion models are slower than their GAN counterparts because of the iterative and sequential reverse diffusion process. There are several techniques that can address this limitation such as progressive timestep distillation (LCM LoRA), model compression (SSD-1B)... |
# Load the pipeline in full-precision and place its model components on CUDA. |
pipe = StableDiffusionXLPipeline.from_pretrained( |
"stabilityai/stable-diffusion-xl-base-1.0" |
).to("cuda") |
# Run the attention ops without SDPA. |
pipe.unet.set_default_attn_processor() |
pipe.vae.set_default_attn_processor() |
prompt = "Astronaut in a jungle, cold color palette, muted colors, detailed, 8k" |
image = pipe(prompt, num_inference_steps=30).images[0] This default setup takes 7.36 seconds. bfloat16 Enable the first optimization, reduced precision or more specifically bfloat16. There are several benefits of using reduced precision: Using a reduced numerical precision (such as float16 or bfloat16) for inference ... |
import torch |
pipe = StableDiffusionXLPipeline.from_pretrained( |
"stabilityai/stable-diffusion-xl-base-1.0", torch_dtype=torch.bfloat16 |
).to("cuda") |
# Run the attention ops without SDPA. |
pipe.unet.set_default_attn_processor() |
pipe.vae.set_default_attn_processor() |
prompt = "Astronaut in a jungle, cold color palette, muted colors, detailed, 8k" |
image = pipe(prompt, num_inference_steps=30).images[0] bfloat16 reduces the latency from 7.36 seconds to 4.63 seconds. In our later experiments with float16, recent versions of torchao do not incur numerical problems from float16. Take a look at the Speed up inference guide to learn more about running inference with r... |
import torch |
pipe = StableDiffusionXLPipeline.from_pretrained( |
"stabilityai/stable-diffusion-xl-base-1.0", torch_dtype=torch.bfloat16 |
).to("cuda") |
prompt = "Astronaut in a jungle, cold color palette, muted colors, detailed, 8k" |
image = pipe(prompt, num_inference_steps=30).images[0] Scaled dot product attention improves the latency from 4.63 seconds to 3.31 seconds. torch.compile PyTorch 2 includes torch.compile which uses fast and optimized kernels. In Diffusers, the UNet and VAE are usually compiled because these are the most compute-inten... |
import torch |
torch._inductor.config.conv_1x1_as_mm = True |
torch._inductor.config.coordinate_descent_tuning = True |
torch._inductor.config.epilogue_fusion = False |
torch._inductor.config.coordinate_descent_check_all_directions = True It is also important to change the UNet and VAE’s memory layout to “channels_last” when compiling them to ensure maximum speed. Copied pipe.unet.to(memory_format=torch.channels_last) |
pipe.vae.to(memory_format=torch.channels_last) Now compile and perform inference: Copied # Compile the UNet and VAE. |
pipe.unet = torch.compile(pipe.unet, mode="max-autotune", fullgraph=True) |
pipe.vae.decode = torch.compile(pipe.vae.decode, mode="max-autotune", fullgraph=True) |
prompt = "Astronaut in a jungle, cold color palette, muted colors, detailed, 8k" |
# First call to `pipe` is slow, subsequent ones are faster. |
image = pipe(prompt, num_inference_steps=30).images[0] torch.compile offers different backends and modes. For maximum inference speed, use “max-autotune” for the inductor backend. “max-autotune” uses CUDA graphs and optimizes the compilation graph specifically for latency. CUDA graphs greatly reduces the overhead of la... |
- latents, timestep=timestep, encoder_hidden_states=prompt_embeds |
-).sample |
+ latents = unet( |
+ latents, timestep=timestep, encoder_hidden_states=prompt_embeds, return_dict=False |
+)[0] Remove GPU sync after compilation During the iterative reverse diffusion process, the step() function is called on the scheduler each time after the denoiser predicts the less noisy latent embeddings. Inside step(), the sigmas variable is indexed which when placed on the GPU, causes a communication sync between ... |
import torch |
# Notice the two new flags at the end. |
torch._inductor.config.conv_1x1_as_mm = True |
torch._inductor.config.coordinate_descent_tuning = True |
torch._inductor.config.epilogue_fusion = False |
torch._inductor.config.coordinate_descent_check_all_directions = True |
torch._inductor.config.force_fuse_int_mm_with_mul = True |
torch._inductor.config.use_mixed_mm = True Certain linear layers in the UNet and VAE don’t benefit from dynamic int8 quantization. You can filter out those layers with the dynamic_quant_filter_fn shown below. Copied def dynamic_quant_filter_fn(mod, *args): |
return ( |
isinstance(mod, torch.nn.Linear) |
and mod.in_features > 16 |
and (mod.in_features, mod.out_features) |
not in [ |
(1280, 640), |
(1920, 1280), |
(1920, 640), |
(2048, 1280), |
(2048, 2560), |
(2560, 1280), |
(256, 128), |
(2816, 1280), |
(320, 640), |
(512, 1536), |
(512, 256), |
(512, 512), |
(640, 1280), |
(640, 1920), |
(640, 320), |
(640, 5120), |
(640, 640), |
(960, 320), |
(960, 640), |
] |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.