README & Reference
README
主要是英文记录(大多是抄的),然后如果很难表达或者理解比较困难会切换成中文
真的要看以下内容务必结合reference原文
Reference
[2506.02070] An Introduction to Flow Matching and Diffusion Models 👍
非常好课程,非常好材料
Flow and Diffusion Models
Flow Models
Introduction
We modeling the reverse process by ordinary differential equations(ODEs).
Generally, A solution to an ODE is defined by a trajectory
is a function that map time t to some location in space (d维向量)
The formulation of ODE is
rewrite as . In perspective of physics, is called vector field and define an ODE.
It is we need an ODE that its trajectory X follows along the lines of vector field
And the trajectory should also strat from point
Given the ODE and vector field above, we can solve the ODE and obtain its solution. The solution is called flow
given a timestep t and the original location . flow recover the trajectory
In a word, we define ODE by vector field and its solution is flow
Theorem 1 Flow existence and uniqueness
If is continuously differentiable with a bounded derivative, then the ODE has a unique solution given by a flow In this case, is a diffeomorpism(微分同构) for all , ( is continuously differentiable with a continuously differentiable inverse )
The neural networks are always bounded derivative, so we can assume that all the ODE discussed has unique solution(flow)
Simulating ODE
It is difficult to compute flow explicitly if is not as simple as a linear function
We could use numerical methods instead
One of the simplest and most intuitive methods is Euler method
Where is the timestep gap
A more complicated method: Heun’s method
Heun method take a initial step to get a guess state, and correct the direction via an updated step
We can define the flow model
Given an ODE:
flow model paramterizes the vector field , and with ODE solver we can obtain the trajectory, and thus reach the target distribution
Actually, we do not care about what the trajectory looks like. We just want a efficient and high-quality path(defined by vector field) to map source distribution to target distribution. So flow models learn vector field instead of flow
Not that precise
By now, we only talk about the sample process(we have obtained the ODE)(trajectory→vector field→ODE)
The Pseudo-algorithm can be writen as

Diffuision Models
Stochastic differential equations(SDEs) extend the ODE with stochastic trajectories. A stochastic trajectory is called a stochastic process
The stochastic process is given by
Simulate stochastic process twice, we might get different outcomes
Brownian Motion
A Brownian motion is a stochastic process such that
The brownian motion define a stochastic trajectories , the trajectories are continuous and the following conditions are hold
- Normal increments:
- for all
- Increments have a Gaussian distribution with variance increasing linearly in time ( is the identity matrix)
- Independent increments:
- for , the increments are independent random variables
Brownian motion is also called Wiener process. We can easily simulate a Brownian motion approximately with step size by setting and updating
From ODEs to SDEs
Then we try to transform ODEs to SDEs by adding brownian motion
Since brownian is totally stochastic(continuous but indifferentiable), we need a equivalent formulation of ODEs that does not use derivatives
We use a differential form instead, where is a negligible error function/term
Then we add brownian motion
describes the diffusion coefficient and describes a stochastic error term. ( for ). This equation is called stochastic differential equation. It can be denoted as
keep in mind that notation is informal
SDEs always don’t have analytical solution, so we need a numerical method(like Euler method in ODE). The corresponding method is Euler-Maruyama method.
It can be write in a general form
Or
The tutorial use for denote , the term here may be easier to understand by denoted as . where is the coefficient and is a Gaussian noise
One may ask why here?
An easy answer of the question would be: the variance of brownian motion is , so that is its std. For , std becomes . We need a Gaussian distribution which has variance , so we multiply here.
Diffusion Models
diffusion model aims to model SDE, a simple way is to parameterize also the vector field.
This is still a sample process: initialize a from simple distribution, simulate the SDE from 0 to 1, and obtain the goal
Constructing the Training Target
We have learned about how to sample via vector field, which is parameterized by network. To train a neural network that perform as the actual vector field, we need to find a training target
target should itself be a vector field that converts into . But if we can directly obtain that target vector field, using neural network and train a alternative is just waste of time.
We can’t find the exact vector field, but we can derive a process that have the same performance: converts into
Conditional and Marginal Probability Path
A probability path is specifis a gradual interpolation between two distribution
Given noise and data , let’s say, the data point belong to Dirac delta “distribution” , which means that samling from , the results are always the same (deterministic)
Given data point , the Dirac delta distribution is , always return
The conditional(interpolating) probability path is a set of distribution such that:
in the axes of time step, is the conditional probability path, but given a time step , is the conditional ditribution
A conditional probability path gradually converts a single data point into the distribution . Every conditional probability path induces a marginal probability path
we know how to sample from , but we don’t know the density values as the integral is intractable
we can only sample from the distribution/path and we can not compute the from a specific
In practice, we always define a simple conditional path as long as they fullfill the requirements:
like Gaussain conditional path

这里展示的是conditional path和marginal path的区别,实际上是基础数学理解的问题,conditonal path是一个给定 data point z 和 具体的path 形式之后得到的 随时间的分布,可视化中是具体的某个图像的去噪/加噪过程,可视化中是具体图像的原因,是作为随机性的部分(噪声可视化中每个时刻已经被确定) marginal path即使给定了具体的path形式,整体仍然是分布,这里的toy demo是将这个分布做成了棋牌桌的样子 所以这里是 固定某个数据样本, 和整个数据集之间的区别
Conditional and Marginal Vector Fields
Theorem 10
Based on the notations mentioned above, we can derive the target vector field:
denote a conditional vector field
defined so that the corresponding ODE yields the conditional probability path :
the marginal vector field defined by
the mariginal probability path:
Why it useful
before prove it, we first figure out why it useful:
The article said, construct the marginal vector field from a conditional vector field simplifies the the problem of finding a formula for a training target significantly
we can find a conditional vector field and derive the target vector field
It can be illustrated by the Gaussain example
let
define conditional flow
Given satisfy
Remember that flow is the solution of a ODE, and vector field define a ODE
We found a conditional vector field that satisfy theorem 10
How to prove it
切换中文解释比较复杂的内容 theorem 10 can be proved via the continuity equation
define divergence operator div
continuity equation:
这里的连续性方程可以被理解为: 左边是概率密度随时间的变化,右边是x位置净流入率(散度在物理中经常描述净流出率) 对于左边而言,计算单纯是对这个位置计算随时间t的密度变化,那么自然会等于 密度乘以速度得到质量之后的对位置的导数
只要vector field满足连续性方程,根据某个定理就可以证明这个vector field就是我们想要的target vector field(充要条件),所以给定定理10前提(对应ii转化),可以得到以下推导证明定理10
Conditional and Marginal Score Functions
Now we extend the reasoning to SDEs
define the marginal score function of as
remember that SDE is denoted as
Theorem 13
for diffusion coefficient , we can construct an SDE which follows the same probability path:
the same identity holds if we change marginal expression to conditional expression
Similar to before, the theorem 13 is useful because it is way more easier to find a conditional score function.
How to prove it
依旧切换中文
定理13可以通过Fokker-Planck equation证明,首先同样定义一个特殊算子
Laplacian operator
有定理15(Fokker-Planck Equation)
给定一个SDE
属于分布 当且仅当满足Fokker-Planck equation
Fokker-Planck equation比较复杂,没有太直觉的解释,直接将定理13的前提代入可以得到
iv和v用到了div算子的特殊性质(log和线性计算)
Langevin dynamics
if the probability path is static, i.e. for a fixed distribution
vector field should be 0 and the SDE is
this is known as Langevin dynamics
Langevin dynamics有一些特性
因为p固定,p是这个Langevin dynamics的稳定分布
- 且 时,随着演化,最终也会得到分布 p
Training the Generative Model
Flow Matching
since we have derive target vector field, we can use MSE loss for training
The loss means that we first sample timestep, and then sample a data point z, and compute x (sample from )
But the integral is intractable, we cannot directly compute the value of target vector field. Instead we using conditonal flow matching loss
Training on conditional vector field makes no sense if we only care about marginal vector field unless they are the same
Theorem 18
The marginal flow matching loss equals the conditional flow matching loss up to a constant
so that their gradient are the same
Proof:
the second summand is
It means that, using the same derivation, CFM can be convey by a same first summand, a same second summand and a constan, sothat we proof the theorem 18
Score Matching
The SDE if defined by
loss can be desined as
And similarily, conditional score function is available objective for score matching.
marginal score function looks the same as marginal vector field and therefore they has the same theorem and proof
Theorem 20
Gaussian example
ODE 的Gaussian example没有什么好说的,需要再回来看就行,SDE实际上需要训练两个模型,分别拟合 vector field 和 score function
如果真的这么做了,一方面比较麻烦(两个模型或者两个输出),另一方面有些误差存在(当然flow matching和score matching都有误差)
对于score matching来说,生成结果受模型精度影响,它有随机噪声 ,这个的数值会很大影响结果,可以作为误差的调整,也可能导致误差变大
对于Gaussian的例子,实际上不需要训练两个模型或者两个head
对于Gaussain的例子,首先conditional score function是
这个损失也叫 denoising score matching
但是这里如果 接近0的话,数值不稳定
DDPM中是直接去掉 ,同时把 改成noise predictor network
除此之外,Gaussian除了能够直接得到conditional score function之外,score funciton 和 vector field 可以相互转化
设
有
marginal vector对应得这个ODE称为 probability flow ODE
证明为
i就是从式子中凑出score function
代入marginal flow vector的积分可以直接得到另一个式子
如果通过vector field计算score function的话
因此,denoising score matching 和 conditional flow matching 是一致的(也就是在Gaussian probability path下两个损失等价)
如果训练denoising score matching之后希望通过SDE进行采样
A Guide to the Diffusion Model Literature
- Discrete time and continuous time
- Discrete time came from the early diffusion paper. Using discrete time should choose a time discretization before training. The loss function is approximated via an evidence lower bound(ELBO), which is only a lower bound of the loss.
- Song showed that actually discrete constructions were essentially an approximation of a time-continuous SDEs and the ELBO loss is the tight loss in continuous case
- Forward process & probability paths
- probability path in this document means a set of ditribution that satisfy . it indicate a trajectory between noise and data point, but in term of ditribution across time.
- early diffusion use forward process to describe this process, add noise to the data point, and for ,
- The difference is: forward process describe a specific path in probability path (how to convert a data point into noise), but probability path is more general and do not care how to convert data.
- In practice, we need to know the distribution of in closed form in order to train our models to avoid simulating the SDE. vector field in forward process are always of the affine form where is a continuous function
- Time-Reversals & Solving the Fokker-Planck equation
- early diffusion model use time reversals (reverse the forward process) to derive the training objective. But we do not care about how to obtain data point (through time reversal), we just care about . A true reversal is not necessary
- Flow Matching and Stochastic Interpolants
- The framework that we present is most closely related to the frameworks of flow matching and stochastic interpolants (SIs)
- Stochastic interpolants included both the pure flow and the SDE extension via “Langevin dynamics”
文中提到了Flow Matching 的框架下,不需要定义forward过程(随时间的变化过程),只需要定义概率路径(init和data之间的中间随机变量 ),求解ODE的过程是确定性,不依赖于随机过程
大概的意思应该是 之前的diffusion或者SDE的做法,需要构建复杂的前向和反向过程,对于FM来说,可以直接定义简单的概率路径,例如线性插值,直接得到速度场得到target,同时是scalable的
Summary
方便理解使用中文
有点一知半解,梳理一下概念
- Flow Matching 和 Score Matching都是训练方法,根据probability flow ODE,训练结果都可以用于SDE或者ODE的求解
- probability flow ODE和SDE拥有相同边缘概率分布,但是probability flow ODE是ODE,给定初始点轨迹确定,SDE给定初始点仍然存在随机过程,二者轨迹不同
- flow model 和 diffusion model分别对应了ODE建模和SDE建模
实际上还差一些相关工作,不过需要的时候再回来进一步梳理吧