mlx-optiq
Fine-tuning · Step 3

DPO on top of an SFT adapter

SFT teaches the model what an answer looks like. DPO (Direct Preference Optimization) then teaches it which of two answers you prefer.

It is optional. Reach for it when SFT got the format right but the model still picks the worse of two reasonable answers: too long, too formal, the wrong habit. The usual order is SFT first, then DPO starting from the SFT weights.

DPO trains a new adapter on top of a frozen SFT adapter; the reference model is base plus SFT; merge gives one adapter.Basequantized, frozen+SFT adaptermounted, frozen+DPO adapterthe only part trainedreference: base + SFTpolicy: base + SFT + DPOoptiq lora merge sft dpo = one adapter
With --mount-adapter, the SFT adapter is loaded frozen and a fresh adapter trains beside it. The reference the loss is measured against is your SFT model, not the raw base.

Preference data

Each row has a prompt, a chosen answer and a rejected one. Multi-turn and agent data can use full message lists instead.

preferences.jsonljson
// single turn
{"prompt": "...", "chosen": "...", "rejected": "..."}
// multi-turn: chosen and rejected are full message lists
{"chosen": [{"role": "user", ...}, ...],
 "rejected": [{"role": "user", ...}, ...]}

Both answers must be plausible replies to the same prompt. If the chosen answer responds to a different context, the two reward terms move together, the margin stays near zero, and the loss falls to 0 while the rewards drift to large negative numbers. The trainer warns once if the first validation pass shows that pattern.

The Lab's DPO from preference pairs template turns a CSV with a prompt,chosen,rejected header into this format.

Train it

dpo.shbash
$ optiq lora train mlx-community/Qwen3.5-4B-OptiQ-4bit \
    --method dpo \
    --data ./my_preferences \
    --mount-adapter ./my_sft_adapter \
    -o ./my_dpo_adapter

The defaults are set for preference tuning, not copied from SFT:

  • Learning rate 5e-5, four times lower than SFT. Running DPO at the SFT rate is the most common reason it collapses.
  • One epoch. DPO is a nudge; more passes invite the collapse above.
  • --dpo-beta 0.1: how hard the model is held to the reference. Higher stays closer to your SFT model.
  • Warmup, then cosine decay. --dpo-warmup-iters defaults to a tenth of the run (at least 10 steps), and --dpo-lr-schedule cosine anneals after it.

For a small or noisy set, --dpo-loss ipo or --dpo-label-smoothing 0.1 (0.1 to 0.3) keeps the loss from collapsing on pairs that are too easy to tell apart. For examples longer than about 4k tokens on a 24 GB Mac, add --fused-dpo, which computes the log-probabilities in chunks.

Combine the two adapters

The DPO adapter holds only the change on top of SFT, so the two are used together. Merge them into one adapter, or bundle both with the base in one step:

combine.shbash
# One adapter, exact (ranks add up, nothing is approximated)
$ optiq lora merge ./my_sft_adapter ./my_dpo_adapter -o ./my_merged_adapter

# Or base + both adapters in one servable folder
$ optiq lora export mlx-community/Qwen3.5-4B-OptiQ-4bit \
    --adapter ./my_sft_adapter --adapter ./my_dpo_adapter \
    -o ./my-model

In the Lab, choose the dpo objective on the hyperparameter step. When the run finishes, Combine with another adapter merges it with your SFT adapter.

A worked example

The 1B humanizer is this recipe: an SFT adapter and a DPO adapter stacked on MiniCPM5-1B-OptiQ-4bit, trained on a 24 GB Mac. SFT alone barely moved the AI-detector score (0.51 to 0.50). With DPO on top it reached 0.37, the same score as the human reference set.