Skip to content

In development. Expect rough edges and changes. The source code is not public yet.

Hugging Face 8 min read How-to mediumAdept

Exploring simple optimizations for SDXL

Running SDXL out of the box in fp32 on an A100 takes about 28GB and 72 seconds for 4 images. A series of Diffusers and PyTorch 2 optimizations, each benchmarked on that setup, cuts both. Switching to fp16 brings latency to 14.8s, SDPA attention (on by default) to 11.4s, and torch.compile to 10.3s. Memory drops with model or sequential CPU offloading (sequential is much slower), VAE slicing (15.4GB, or 11.5GB combined with sequential offload), precomputed text embeddings, and the Tiny Autoencoder (15.5GB and faster, at some cost in fine detail). The gains depend on hardware; torch.compile, for example, only appears to help modern GPUs.

Assumes: PyTorch basicsdiffusion modelsGPU memory concepts

Related posts

  • Run FLUX.1-dev three times faster

    Modal mediumAdept

    FLUX.1-dev image generation on Modal drops from ~6.75s to under three seconds per 1024x1024 image. About 1.5x comes from standard optimizations: torch.compile with max-autotune, fused QKV projections and channels-last memory layout. Another 2x comes from First Block Caching (via ParaAttention, based on TEACache), which skips denoising steps whose early-block residual change falls below a threshold. A threshold of 0.12 gave a good quality/speed tradeoff. The tens of minutes of compile time added to cold starts were cut roughly 30x by persisting Inductor, Triton and CUDA caches on a volume and using Modal memory snapshots.

  • Make your ZeroGPU Spaces go brrr with ahead-of-time compilation

    Hugging Face mediumAdept

    PyTorch ahead-of-time compilation (torch.export plus AOTInductor) suits ZeroGPU Spaces, where each GPU task runs in a short-lived forked process and torch.compile's just-in-time caching costs tens of seconds to minutes per task. The workflow captures example inputs with spaces.aoti_capture, exports and compiles the transformer inside a @spaces.GPU function, then patches it into the pipeline with spaces.aoti_apply, giving 1.3–1.8× speedups on Flux, Wan and LTX (1.7× on FLUX.1-dev). Further gains come from FP8 dynamic quantization via torchao (about 1.2× more on H200), dynamic shapes via torch.export.Dim, per-resolution multi-compile with shared weights, and prebuilt FlashAttention-3 kernels from the kernels library.

  • Fast LoRA inference for Flux with Diffusers and PEFT

    Hugging Face hardMaster

    Combining Flash Attention 3, torch.compile and TorchAO FP8 quantization with PEFT's LoRA hotswapping speeds up Flux.1-Dev LoRA inference by 2.23x on an H100, and swapping adapters does not trigger recompilation. Hotswapping avoids recompiles by turning LoRA scaling factors into tensors and zero-padding adapter weights to a declared max rank. The limits are that later LoRAs must target the same layers as the first one, or a subset of them, and text encoder LoRAs are not supported. On a 24GB RTX 4090, FA3 is unavailable, so the recipe adds NF4 quantization of the T5 text encoder to fit FP8 without CPU offloading, which gives a 2.04x speedup.

  • Accelerating Stable Diffusion Inference on Intel CPUs

    Hugging Face mediumAdept

    Stable Diffusion v1.5 inference on Sapphire Rapids Xeon CPUs (EC2 r7iz) can be cut from a 32.3s baseline to a few seconds per image with a stack of optimizations. OpenVINO via Optimum Intel gives a 2x speedup through automatic bfloat16, and reshaping the pipeline to a static 512x512 resolution brings it to 4.7s. Without OpenVINO, jemalloc, Intel OpenMP and numactl core pinning reach 11.8s with no code change. Adding IPEX with channels-last, bfloat16 and AMX gets to 5.4s, and the DPMSolverMultistep scheduler reaches 5.05s, about 6.5x faster than the baseline.

  • Accelerating SD Turbo and SDXL Turbo Inference with ONNX Runtime and Olive

    Hugging Face mediumAdept

    ONNX Runtime's CUDA and TensorRT execution providers run SD Turbo and SDXL Turbo faster than PyTorch on NVIDIA A100 and RTX 4090 GPUs for every batch-size and step combination tested. Throughput gains reach 229% for SDXL Turbo and 120% for SD Turbo. The speedups come from CUDA graphs for static shapes, Flash Attention V2, trimming unused text-encoder outputs and SkipGroupNorm fusion. TensorRT does slightly better with static shapes but needs a separate engine per batch and image size, while the CUDA provider is often the better pick for dynamic shapes on A100. Olive-optimized models, a usage example, and C# and Java community ports are provided.

The quest for software engineering knowledge.

Look:
Layout: