flow_matching
Rectified-flow (RF) helpers: forward process, inversions, timestep sampling.
This module intentionally does not define a NoiseScheduler class. It exposes the
handful of primitives that DMD2 actually needs as plain functions, so callers can plug
fastgen into any training stack without adopting a new scheduler object.
RF convention used throughout: alpha_t = 1 - t and sigma_t = t, so
x_t = (1 - t) * x_0 + t * eps with t in [0, 1]. Internally all arithmetic is in
float64 for numerical stability, and the result is cast back to the input dtype.
Functions
Forward process under rectified flow: |
|
Convert an |
|
Convert a flow-parameterized prediction ( |
|
Rectified-flow data coefficient |
|
Rectified-flow noise coefficient |
|
Sample |
|
Sample |
|
Invert the RF forward process: |
|
Convert an |
- add_noise(x0, eps, t)
Forward process under rectified flow:
x_t = (1 - t) * x_0 + t * eps.tis broadcast across the spatial axes ofx_0viaexpand_like(). Computation is performed infloat64for numerical stability and the output is cast back tox_0’s dtype.- Parameters:
x0 (Tensor)
eps (Tensor)
t (Tensor)
- Return type:
Tensor
- pred_noise_to_pred_x0(pred_noise, noisy_latents, t)
Convert an
eps-parameterized prediction to anx_0prediction under RF.Solves
x_t = (1 - t) * x_0 + t * epsforx_0:x_0 = (x_t - t * eps) / (1 - t).- Parameters:
pred_noise (Tensor)
noisy_latents (Tensor)
t (Tensor)
- Return type:
Tensor
- pred_x0_from_flow(pred_flow, noisy_latents, t)
Convert a flow-parameterized prediction (
v = eps - x_0) to anx_0prediction.Under RF
x_t = (1 - t) * x_0 + t * epsandv = eps - x_0combine tox_t = x_0 + t * v, sox_0 = x_t - t * v.- Parameters:
pred_flow (Tensor)
noisy_latents (Tensor)
t (Tensor)
- Return type:
Tensor
- rf_alpha(t)
Rectified-flow data coefficient
alpha_t = 1 - t.- Parameters:
t (Tensor)
- Return type:
Tensor
- rf_sigma(t)
Rectified-flow noise coefficient
sigma_t = t.- Parameters:
t (Tensor)
- Return type:
Tensor
- sample_from_t_list(n, t_list, *, device, dtype=torch.float32)
Sample
nstarting timesteps uniformly fromt_list[:-1].Used for multi-step student training:
t_listencodes the inference trajectory (t_list[-1]must be0), and a random intermediate timestep is sampled so the student is trained at every rung of the trajectory.- Parameters:
n (int)
t_list (list[float])
device (device)
dtype (dtype)
- Return type:
Tensor
- sample_timesteps(n, cfg, *, device, dtype=torch.float32)
Sample
ntraining timesteps according tocfg.Supports
uniform,logitnormal,lognormal,shifted, andpolynomialdistributions.polynomialfor RF degenerates to discrete uniform sampling from alinspace(min_t, max_t, 1000)grid (EDM’s polynomial-spaced_sigmasis EDM-specific and not applicable under RF).- Parameters:
n (int)
cfg (SampleTimestepConfig)
device (torch.device)
dtype (torch.dtype)
- Return type:
torch.Tensor
- x0_to_eps(x0, x_t, t)
Invert the RF forward process:
eps = (x_t - (1 - t) * x_0) / t.Used when unrolling the student in ODE mode — given the current
x_tand the student’sx_0prediction, we can recover the impliedepsdeterministically.- Parameters:
x0 (Tensor)
x_t (Tensor)
t (Tensor)
- Return type:
Tensor
- x0_to_flow(x0, x_t, t)
Convert an
x_0prediction back into a flow-parameterized prediction under RF.Under RF
x_t = (1 - t) * x_0 + t * epsandv = eps - x_0, sox_t - x_0 = t * (eps - x_0) = t * vand thereforev = (x_t - x_0) / t.Used when the fake score is flow-native but the DSM loss is computed in a different target parameterization: convert raw flow → x_0 via
pred_x0_from_flow(), then back to the loss space (which may coincide with flow, in which case the round-trip is identity up to fp64 round-off).- Parameters:
x0 (Tensor)
x_t (Tensor)
t (Tensor)
- Return type:
Tensor