Skip to content

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

Hugging Face 11 min read How-to hardMaster

Preference Optimization for Vision Language Models

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.

Assumes: LLM fine-tuning basicsPyTorch and Hugging Face TransformersGPU memory constraints

Related posts

  • Vision Language Model Alignment in TRL ⚡️

    Hugging Face hardMaster

    TRL adds vision language model support for several alignment methods. New are Mixed Preference Optimization (MPO), Group Relative Policy Optimization (GRPO) and its sequence-level variant GSPO, alongside multimodal RLOO, Online DPO and native SFTTrainer support. MPO is implemented in DPOTrainer as a weighted combination of DPO sigmoid, BCO and SFT losses, which the MPO paper reports gives a 6.2-point MathVista gain. GRPO uses custom format and accuracy reward functions. GSPO is enabled through GRPOConfig with sequence-level importance sampling, which is mainly useful for MoE models. Code snippets show each trainer config, and a vibe-check comparison on Qwen2.5-VL-3B shows the GRPO and GSPO runs reaching the correct geometry answer where the base and MPO models do not.

  • Vision Language Models Explained

    Hugging Face mediumAdept

    Vision language models combine an image encoder, a projector that aligns image features with text embeddings, and a text decoder that generates text from both inputs. LLaVA trains in two stages: first only the projector with the encoder and decoder frozen, then the projector together with an unfrozen decoder. KOSMOS-2 instead trains end to end, and Fuyu-8B skips the image encoder entirely. The guide compares open VLMs by license, size and resolution, explains model selection via Vision Arena, the Open VLM Leaderboard and benchmarks like MMMU and MMBench, then shows inference with transformers' LlavaNext and supervised fine-tuning of Llava 1.5 using TRL's new experimental VLM support in SFTTrainer.

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

  • LoRA training scripts of the world, unite!

    Hugging Face hardMaster

    Combining Pivotal Tuning (textual inversion of newly inserted tokens, then LoRA fine-tuning) with the adaptive Prodigy optimizer produces strong SDXL Dreambooth LoRA results, and both are now available in a diffusers training script. The guide covers the relevant flags and recommended values: stopping textual inversion halfway through training, Prodigy with learning_rate=1.0 and bias correction, and separate text encoder and UNet learning rates. It also covers custom BLIP captions, Min-SNR gamma of 5.0, image repeats, prior preservation loss and dataset curation tips for faces and objects. Experiments on a 31-image Huggy mascot dataset compare batch sizes, optimizers and text encoder training modes.

  • Finetune Stable Diffusion Models with DDPO via TRL

    Hugging Face hardMaster

    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.

The quest for software engineering knowledge.

Look:
Layout: