OneTrainer icon indicating copy to clipboard operation
OneTrainer copied to clipboard

Diff2Flow: Aligning Diffusion Models with Flow-Matching

Open Koratahiu opened this issue 1 year ago • 12 comments

This PR integrates the Diff2Flow methodology, based on the paper "Diff2Flow: Training Flow Matching Models via Diffusion Model Alignment". This feature adapts a pre-trained diffusion model to a flow matching objective, unlocking significant performance improvements.

A new boolean option, diff2flow, has been added to the training configuration for both SD and SDXL models.

Key Benefits & Features

  • More Efficient Fine-Tuning: The model converges much faster during training, especially with parameter-efficient methods like LoRA. This is because the model's pre-trained knowledge is directly leveraged without needing to re-learn a new prediction objective from scratch.
  • Improved Sample Quality: Diff2Flow naturally resolves the zero-terminal SNR issue often seen in eps-prediction models, leading to images with true blacks and whites instead of a washed-out or gray bias.
  • Broad Compatibility: The implementation is compatible with both eps-pred and v-pred models, seamlessly converting their outputs to the required velocity field.

Implementation Details

  • The core logic is encapsulated in modules/util/convert/diff2flow_util.py.
  • The enable_diff2flow() function is called during model setup to dynamically add the necessary methods and buffers.
  • Samplers (StableDiffusionSampler and StableDiffusionXLSampler) now include a non-invasive dispatch to a dedicated Diff2Flow sampling loop that uses a simple Euler ODE solver.

References

The paper: https://arxiv.org/abs/2506.02221 The code: https://github.com/CompVis/diff2flow

Checklist

  • [x] Test on SD1.5
  • [x] Test on SDXL
  • [x] Test on v-pred model
  • [ ] Maybe support inpainting? It might need some work.
  • [ ] Remove or keep the loss weight function of diffusion (e.g., Min SNR). The referenced paper did not specify its usage.
  • [x] Minimal Changes: The changes are minimal in all files except BaseStableDiffusionSetup and BaseStableDiffusionXLSetup, which might need some simplification.
  • ~~Timestep sampling problem: The paper’s method samples a continuous time t uniformly from U[0,1] , then finds the corresponding diffusion timestep. However, we want to apply user input for timesteps. More tests are required.~~

Koratahiu avatar Oct 12 '25 19:10 Koratahiu

Stable Diffusion 1.5 Training Results

Base SD1.5 trained on 16K samples using this method:

Diagram showing the training method for Base SD1.5

Random Sample Outputs (Generated inside OT since there's no inference app that supports this method, yet.)

A real flow-matching vibe!

Trained model output sample 1 Trained model output sample 2 Trained model output sample 3
Trained model output sample 4 Trained model output sample 5 Trained model output sample 6
Trained model output sample 7

Koratahiu avatar Oct 13 '25 07:10 Koratahiu

Interesting I'll leave this up for a while without merging, because it needs testing and community support (Comfy support?)

Are the authors suggesting that this method is superior to having a small flow-matching model? Or are they only saying: we already have a small diffusion model, but currently lack a small efficient FM model - we can finetune the diffusion model into a FM model

dxqb avatar Oct 15 '25 03:10 dxqb

Yeah, it needs testing and more support. Regarding ComfyUI support, I might do it myself once I’m done with this PR.

An example of what the authors mean is this:
Instead of abandoning your language and learning a new one (switching from eps/v-prediction to flow matching), which would require a lot of time (thousands to millions of steps). You can instead translate between the two languages. You keep your original language (diffusion) while translating its “words” (predictions) into the other language (flow matching).

This way, the model retains its original knowledge and predictions while learning to operate in the flow-matching dimension through "translation".

For example, the SD1.5 model I trained using this method can still generate images using the eps prediction just fine, and it can also do so with flow matching.

Koratahiu avatar Oct 15 '25 03:10 Koratahiu

I've been testing this both on eps and vpred models with SDXL and it's showing promise at improving those models in their native predictions even without using flow matching support in inference.

For example the eps model was able to do solid black images which it normally would never be able to do, and vpred ztsnr model got better with composition and darkness. I encourage others to test as well.

Goldenkoron avatar Oct 15 '25 03:10 Goldenkoron

https://civitai.com/models/2068592/snakebite-2 It's an SDXL based, but from its description:

This model uses Flow Matching, so you must connect it to the ModelSamplingSD3 node in ComfyUI to get correct results.

miasik avatar Oct 30 '25 07:10 miasik

https://civitai.com/models/2068592/snakebite-2 It's an SDXL based, but from its description:

This model uses Flow Matching, so you must connect it to the ModelSamplingSD3 node in ComfyUI to get correct results.

This takes blocks from bigASP v2.0, which is trained for 15M samples in flowmatching. And unrelated.

Koratahiu avatar Oct 30 '25 09:10 Koratahiu

A custom ComfyUI node for Diff2flow is now available: https://github.com/Koratahiu/ComfyUI-Diff2Flow

It can be used even on an untrained model, yielding good results, but with training, it can be further improved.

Koratahiu avatar Nov 15 '25 02:11 Koratahiu

Maybe support inpainting? It might need some work. Remove or keep the loss weight function of diffusion (e.g., Min SNR). The referenced paper did not specify its usage.

Do you still think these need to occur?

O-J1 avatar Jan 01 '26 20:01 O-J1

I think inpainting is already supported in the latest commit, since we made it use the standard SDXL/SD path and only applied the diff2flow logic at the end (but still untested)

Regarding loss weights, they shouldn't be used with diff2flow (different loss, different logic)

Koratahiu avatar Jan 01 '26 20:01 Koratahiu

Is this still needed? It’s basically ready, but who uses SDXL anymore?

Koratahiu avatar May 03 '26 18:05 Koratahiu

Is this still needed? It’s basically ready, but who uses SDXL anymore?

I do

miasik avatar May 03 '26 20:05 miasik

Is this still needed? It’s basically ready, but who uses SDXL anymore?

Please do it

yamatazen avatar May 18 '26 04:05 yamatazen

@Koratahiu Any updates?

yamatazen avatar Jul 07 '26 01:07 yamatazen

Closed.

  • SDXL is outdated
  • It only supports t2i, with no support for i2i tasks However, anyone is welcome to resolve the conflicts and continue working on this if they’d like (it was already completed).

Koratahiu avatar Oct 10 '26 05:10 Koratahiu

RIP

yamatazen avatar Oct 10 '26 05:10 yamatazen