Skip to content

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

Hugging Face 9 min read How-to hardMaster

Fast LoRA inference for Flux with Diffusers and PEFT

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.

Assumes: LoRA fine-tuningPyTorch torch.compilediffusion modelsmodel quantization

Related posts

  • (LoRA) Fine-Tuning FLUX.1-dev on Consumer Hardware

    Hugging Face hardMaster

    FLUX.1-dev can be LoRA fine-tuned on a single consumer GPU by combining QLoRA (4-bit NF4 base transformer via bitsandbytes), 8-bit AdamW, gradient checkpointing, cached VAE latents and precomputed CLIP/T5 text embeddings, so that only the quantized transformer plus adapters sit in GPU memory. On an RTX 4090, a rank-4 LoRA at 512x768 peaked around 9 GB VRAM, compared with 26 GB for BF16 LoRA and an estimated ~120 GB for full fine-tuning, and 700 steps took about 41 minutes to learn Alphonse Mucha's style. The guide includes the diffusers training script settings, an embedding precompute script, FP8 training with torchao for faster runs on compatible hardware, and options for loading or merging the trained adapters at inference.

  • Goodbye cold boot - how we made LoRA Inference 300% faster

    Hugging Face mediumAdept

    Hugging Face's Inference API now keeps Stable Diffusion base models warm and swaps LoRA adapters per request, so hundreds of distinct LoRAs share a few GPUs instead of each getting a dedicated deployment. Requests carrying a base_model attribute and a lora tag are routed to a shared backend that uses Diffusers' load_lora_weights/fuse_lora and unfuse_lora/unload_lora_weights to swap adapters in 2-4 seconds. Warm-up dropped from 25s to 3s and cold request latency from 35s to 13s, with hundreds of LoRAs served on fewer than 5 A10G GPUs. Fusing weights cuts inference time by 30%, and batched LoRA inference was skipped because diffusion throughput rose only 25% at batch size 8 while latency grew sixfold.

  • Exploring Quantization Backends in Diffusers

    Hugging Face mediumAdept

    Hugging Face Diffusers supports several quantization backends (bitsandbytes, torchao, Quanto, GGUF and native FP8 layerwise casting) that shrink large diffusion models such as Flux-dev, whose full BF16 pipeline needs about 31 GB. Benchmarks on an H100 put BnB 4-bit NF4 at about 12.6 GB after loading with no inference slowdown, while torchao and Quanto INT4 cut memory further but take roughly 109 seconds per image versus 12 seconds for BF16. Code examples apply PipelineQuantizationConfig to the transformer and T5 encoder, load pre-quantized GGUF files, and combine quantization with CPU offloading, group offloading and torch.compile. The 8-bit quality differences are usually hard to see, and NF4 is suggested as the best tradeoff.

  • 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.

  • Optimizing your LLM in production

    Hugging Face mediumAdept

    Serving billion-parameter LLMs efficiently comes down to three levers: lower precision, Flash Attention, and inference-friendly architectures such as ALiBi, rotary embeddings, Multi-Query Attention and Grouped-Query Attention. Weights need roughly 2 GB of VRAM per billion parameters in bfloat16, and measurements on OctoCoder (~15B parameters) show peak memory dropping from about 29 GB in bfloat16 to 15 GB with 8-bit and 9.5 GB with 4-bit bitsandbytes quantization, with little output change but slightly slower inference. Flash Attention addresses self-attention's quadratic memory growth with sequence length by restructuring the computation to make better use of GPU memory. Code examples in Hugging Face Transformers accompany each technique.

The quest for software engineering knowledge.

Look:
Layout: