Add OT-CFM: mini-batch optimal transport coupling for flow matching#6
Open
isNeil wants to merge 1 commit into
Open
Add OT-CFM: mini-batch optimal transport coupling for flow matching#6isNeil wants to merge 1 commit into
isNeil wants to merge 1 commit into
Conversation
Implements Tong et al. 2023 (OT-CFM): each training step permutes the noise batch to minimise total squared distance to x_start, reducing path crossings and producing a straighter learned vector field. Changes: - flow_matching.py: _ot_permute() (Hungarian algorithm via scipy.optimize.linear_sum_assignment), FlowMatching.use_ot_coupling flag (default False, opt-in), applied in training_losses() - diffusion/__init__.py: use_ot_coupling kwarg forwarded to FlowMatching (default False) - train_experiment.py: reads use_ot_coupling from diffusion config (default False) Non-flow experiments are unaffected: use_ot_coupling is only consumed by the FlowMatching code path. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Implements mini-batch optimal transport (OT) coupling for the flow matching training path as an opt-in feature (
use_ot_coupling=Falseby default, preserving existing FM behaviour).OT-CFM
Standard conditional flow matching pairs each clean sample
x_0with an independently drawn noise sampleε. These random pairings produce straight-line paths that cross each other in latent space, forcing the model to learn an averaged, curved vector field that requires more Euler steps to integrate accurately at inference.OT-CFM (Tong et al., NeurIPS 2023) instead finds the assignment that minimises total squared distance across the mini-batch:
Non-crossing paths yield a straighter learned vector field that requires fewer inference steps — which is the primary motivation.
Implementation:
_ot_permute()inflow_matching.pysolves the linear assignment problem viascipy.optimize.linear_sum_assignment(Hungarian algorithm, O(B³), negligible at B ≤ 64; scipy is already a project dependency). One call is inserted intraining_losses()beforeq_sample. Inference is completely unchanged.Enabling OT:
Non-flow experiments are completely unaffected.
Changes
src/diffusion/flow_matching.py—_ot_permute()helper,use_ot_couplingflag onFlowMatching.__init__(defaultFalse), called intraining_losses()src/diffusion/__init__.py—use_ot_couplingkwarg forwarded toFlowMatching(defaultFalse)src/experiments/train_experiment.py— readsuse_ot_couplingfrom diffusion config (defaultFalse)Results
Training quality — RT-1 Fractal (30k steps)
NanoWM-S/2, 12,515 episodes, RTX 4090, bf16-mixed AMP.
*= winner at that step. Bold = that model's best across all checkpoints.PSNR ↑
SSIM ↑
LPIPS ↓
MSE ↓
OT-CFM leads clearly at 5k–10k (faster early convergence), noOT catches up and leads around 15k–20k, then results are mixed at 25k–30k. Final quality is essentially tied — all differences are within eval noise at 32 val samples.
Inference steps — RT-1 30k (mean ± std, 3 seeds × 32 samples)
Both models were trained with
diffusion_steps=1000; inference uses Euler integration with varyingnum_sampling_steps. The primary claim of OT-CFM is that straighter paths require fewer steps to integrate accurately. Bold * = that model's best across all step counts.PSNR ↑
SSIM ↑
LPIPS ↓
Takeaway: FM+OT peaks at 10 steps (LPIPS 0.1820); FM-noOT peaks at 20 steps (LPIPS 0.1859) and never reaches OT's best. On PSNR/SSIM both models plateau by step 2–5. In practice OT-CFM lets you run at 10 steps instead of 20+ — a 2× inference speedup at strictly better perceptual quality, with larger gains at very low step budgets (1–5 steps).
Test plan
python main.py experiment=dino_wm_pusht_flow ++training.max_steps=100use_ot_coupling=false(default) reproduces standard FM — no behaviour changeuse_ot_coupling=trueOT-CFM run on RT-1 30k; inference step ablation above🤖 Generated with Claude Code