Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Workflow:Huggingface Diffusers LoRA Finetuning

From Leeroopedia
Revision as of 11:05, 16 February 2026 by Admin (talk | contribs) (Auto-imported from workflows/Huggingface_Diffusers_LoRA_Finetuning.md)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Knowledge Sources
Domains Diffusion_Models, Fine_Tuning, LoRA
Last Updated 2026-02-13 21:00 GMT

Overview

End-to-end process for parameter-efficient fine-tuning of diffusion models on custom image-caption datasets using Low-Rank Adaptation (LoRA).

Description

This workflow describes how to fine-tune a pretrained text-to-image diffusion model on a custom dataset using LoRA adapters. LoRA injects small, trainable low-rank matrices into the frozen base model's attention layers, enabling adaptation with a fraction of the parameters and memory required for full fine-tuning. The workflow covers dataset preparation with image-caption pairs, model loading with frozen weights, LoRA adapter injection targeting attention projection layers, training loop execution with noise prediction loss, and saving the resulting lightweight adapter weights. The trained LoRA adapter can then be loaded at inference time to steer generation toward the training distribution.

Usage

Execute this workflow when you have a dataset of image-caption pairs and want to adapt a base diffusion model to generate images in a specific style, domain, or subject matter. This is appropriate when full fine-tuning is too expensive or when you want to maintain the base model's capabilities while adding specialized knowledge. Typical use cases include adapting to a particular artistic style, training on domain-specific imagery (medical, satellite, product photos), or teaching the model new visual concepts.

Execution Steps

Step 1: Environment Setup

Initialize the training environment with distributed training support via the Accelerate library. Configure logging, output directories, random seeds for reproducibility, and experiment tracking backends (TensorBoard, Weights & Biases). Validate that all required dependencies including PEFT are installed.

Key considerations:

  • Accelerate handles multi-GPU and mixed-precision training automatically
  • Set a fixed seed for reproducible training runs
  • Configure gradient accumulation steps to simulate larger batch sizes on limited VRAM
  • Enable experiment tracking for monitoring training progress

Step 2: Model Loading

Load the pretrained diffusion model components from the Hugging Face Hub: the text tokenizer, the text encoder (CLIP or T5), the VAE autoencoder, the UNet or Transformer denoising model, and the noise scheduler. Freeze all parameters across all components to prevent any weight updates to the base model.

Key considerations:

  • Load in the appropriate precision (float16/bfloat16 for non-trainable components)
  • The VAE and text encoder remain completely frozen throughout training
  • Move frozen components to the target device and appropriate dtype
  • The noise scheduler defines the noise schedule used during training

Step 3: LoRA Adapter Injection

Create and inject LoRA adapter layers into the denoising model's attention mechanisms. Define the LoRA configuration specifying the rank, alpha scaling factor, target modules, and initialization method. The adapter adds pairs of small matrices (down-projection and up-projection) alongside the frozen attention weights.

Key considerations:

  • Default target modules are the query, key, value, and output projection layers
  • Rank (r) controls adapter capacity: typical values range from 4 to 128
  • Alpha parameter scales the LoRA contribution (usually set equal to rank)
  • Cast trainable parameters to float32 for numerical stability in mixed-precision training
  • Only LoRA parameters are marked as requiring gradients

Step 4: Dataset Preparation

Load and preprocess the training dataset of image-caption pairs. Apply image transformations including resizing, cropping (center or random), optional horizontal flipping, and normalization. Tokenize text captions with the model's tokenizer, applying truncation and padding to the maximum token length.

Key considerations:

  • Dataset can be loaded from the Hugging Face Hub or a local folder in imagefolder format
  • Image resolution must match the model's expected input size (512, 768, or 1024 depending on model)
  • Random cropping and flipping provide data augmentation during training
  • Pre-computing text embeddings can save memory if the text encoder is not being trained

Step 5: Training Configuration

Set up the optimizer, learning rate scheduler, and training hyperparameters. Prepare all trainable components, the data loader, and schedulers through the Accelerate library for distributed training compatibility.

Key considerations:

  • AdamW optimizer with optional 8-bit variant for memory savings
  • Learning rate typically between 1e-4 and 1e-5 for LoRA training
  • Learning rate scaling can be applied based on batch size and number of processes
  • Warmup steps help stabilize early training
  • Cosine or constant learning rate schedules are commonly used

Step 6: Training Loop

Execute the main training loop. For each batch: encode images into latent space through the frozen VAE, sample random noise and timesteps, apply forward diffusion to create noisy latents, obtain text embeddings from the frozen text encoder, predict the noise with the LoRA-adapted denoising model, compute the mean squared error loss between predicted and actual noise, backpropagate through only the LoRA parameters, and update weights.

Key considerations:

  • The VAE encodes images once and the latents are cached (no gradient needed)
  • Timesteps are sampled uniformly from the scheduler's range
  • SNR-weighted loss can improve training stability and output quality
  • Gradient clipping prevents training instability
  • Checkpoint saving at regular intervals enables training resumption

Step 7: Validation

Periodically generate sample images using the current LoRA weights to monitor training progress. Construct a temporary inference pipeline, load the in-progress LoRA adapter, and run generation with fixed validation prompts. Log generated images to the experiment tracker.

Key considerations:

  • Validation should use a fixed seed for consistent comparison across training
  • Run validation every N epochs or steps as configured
  • Compare validation outputs against the training distribution to detect overfitting
  • The validation pipeline is constructed temporarily and freed after use

Step 8: Export and Save

Save the trained LoRA adapter weights in the standard diffusers format. Convert the PEFT state dictionary to the diffusers-compatible LoRA format and write the adapter weights file. Optionally push the trained adapter to the Hugging Face Hub with a generated model card containing training metadata.

Key considerations:

  • Only the LoRA adapter weights are saved (typically a few MB vs. GB for the full model)
  • The saved adapter can be loaded into any compatible base model at inference time
  • Model card generation includes training hyperparameters and dataset information
  • Multiple LoRA adapters can be combined at inference time with different scales

Execution Diagram

GitHub URL

Workflow Repository