Diff2Flow: Aligning Diffusion Models with Flow-Matching
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-predandv-predmodels, 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 (
StableDiffusionSamplerandStableDiffusionXLSampler) 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.~~
Stable Diffusion 1.5 Training Results
Base SD1.5 trained on 16K samples using this method:
Random Sample Outputs (Generated inside OT since there's no inference app that supports this method, yet.)
A real flow-matching vibe!
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
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.
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.
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.
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.
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.
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?
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)
Is this still needed? It’s basically ready, but who uses SDXL anymore?
Is this still needed? It’s basically ready, but who uses SDXL anymore?
I do
Is this still needed? It’s basically ready, but who uses SDXL anymore?
Please do it
@Koratahiu Any updates?
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).
RIP