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

Finetune Stable Diffusion Models with DDPO via TRL

Denoising Diffusion Policy Optimization (DDPO) fine-tunes diffusion models with reinforcement learning by treating the full denoising process as a multistep MDP. Because each step's policy is an isotropic Gaussian, it can use exact per-step likelihoods, unlike reward-weighted regression, which stacks approximations, and it then applies PPO with customized trajectory collection. TRL's new DDPOTrainer lets you fine-tune Stable Diffusion against a CLIP-based aesthetic reward model, needs at least an A100, and ships recommended single-GPU hyperparameters. LoRA training is stable and generalizes across prompts even with a small prompt set; full fine-tuning can give more intricate images but needs a low learning rate (~1e-5) and careful tuning.

Related posts

  • How Diffusion Controller unifies and simplifies AI image generation

    Google Research hardMaster

    Diffusion Controller reframes diffusion denoising as a continuous control problem, unifying inference-time guidance and fine-tuning methods such as LoRA, reward-weighted regression and policy gradients under one framework. A lightweight trainable side network attached to a frozen backbone adds small corrections to the base model's score using the intermediate reverse mean, so it also works with gray-box, access-restricted models. On a Stable Diffusion v1.4 backbone across SFT, reward-weighted loss and PPO regimes, the gray-box controller beat LoRA on HPS-v2 win rates while touching fewer layers. The white-box variants reached about a 90% win rate over the baseline, and a single inference-time guidance-strength parameter dials alignment up or down.

  • Preference Optimization for Vision Language Models

    Hugging Face hardMaster

    TRL now supports direct preference optimization (DPO) for vision language models, trained on chosen/rejected answer pairs rather than fixed labels. A walkthrough fine-tunes Idefics2-8b on the RLAIF-V dataset, formatting image-plus-question prompts with the processor's chat template. Full float32 DPO training would need about 160 GB (policy, reference model, gradients, AdamW states), but bfloat16 plus LoRA cuts trainable parameters to 55M and memory to roughly 32 GB. Batch size 2 with 32 gradient accumulation steps and gradient checkpointing fits activations, and training is judged by rising accuracy and reward margin.

  • Fine-tune Llama 2 with DPO

    Hugging Face hardMaster

    Direct Preference Optimization (DPO) replaces the reward-model and RL steps of RLHF with a simple binary cross-entropy loss over preference pairs, using a frozen reference model to provide an implicit reward. With TRL's DPOTrainer, data is formatted as prompt/chosen/rejected triples and training is controlled by a beta temperature, typically 0.1 to 0.5. The walkthrough fine-tunes Llama 2 7B on the stack-exchange preference dataset: first SFT with QLoRA (4-bit NF4 plus LoRA adapters), then DPO using the SFT model as both policy and reference. Progress is monitored with logged reward metrics (chosen, rejected, accuracies, margins), where margins should grow and accuracies should approach 1.0.

  • Using LoRA for Efficient Stable Diffusion Fine-Tuning

    Hugging Face mediumAdept

    LoRA freezes pretrained weights and trains small rank-decomposition matrices, and diffusers now supports applying it to Stable Diffusion's cross-attention layers for both full fine-tuning and Dreambooth. Training fits in 11 GB of VRAM (a 2080 Ti) without 8-bit optimizers, uses a much higher learning rate (1e-4 vs ~1e-6), and produces ~3 MB weight files, roughly 1000x smaller than the UNet. A walkthrough covers the training script on a Pokémon dataset, looking up the base model from Hub metadata, and loading LoRA weights at inference with load_attn_procs. LoRA is also compared with Textual Inversion and Pivotal Tuning.

  • Instruction-tuning Stable Diffusion with InstructPix2Pix

    Hugging Face hardMaster

    Fine-tuning InstructPix2Pix on FLAN-style instruction-prompted datasets teaches Stable Diffusion to follow specific image transformation instructions such as cartoonization and low-level image processing (deraining, denoising, deblurring, low-light enhancement). The cartoonization dataset pairs 5,000 Imagenette images with Whitebox CartoonGAN outputs and 50 ChatGPT-generated instruction paraphrases, and the low-level dataset mixes samples from REDS, Rain13k, SIDD and LOL into a single multi-task set. Starting from an InstructPix2Pix checkpoint adapted faster than starting from base Stable Diffusion. Cartoonization and deraining results clearly beat the pre-trained model, while low-light enhancement and deblurring stayed weak, likely because those tasks had very few training samples. The results are preliminary, and open questions on dataset scale, task mixture and ControlNet alternatives remain.

The quest for software engineering knowledge.

Look:
Layout: