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]. Existing RF conversions use
float64 intermediates and cast back to the input dtype. PDD grid and interval helpers
instead preserve float32-or-higher outputs because their result remains in the decoding path.
Functions
Forward process under rectified flow: |
|
Return step-width-normalized coefficients for a contiguous block |
|
Integrate per-sample velocity heads over half-open blocks |
|
Construct the fixed decreasing shifted rectified-flow grid. |
|
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
- fusion_coefficients(grid, start, end)#
Return step-width-normalized coefficients for a contiguous block
[start, end).- Parameters:
grid (Tensor)
start (int)
end (int)
- Return type:
Tensor
- integrate_interval_velocities(state, velocities, grid, start, end)#
Integrate per-sample velocity heads over half-open blocks
[start, end).stateis batch-first with shape[B, ...]andvelocitieshas shape[B, N, ...].startandendmay be scalar integers or integer tensors of shape[B]. The empty blockstart == endreturnsstatepromoted to the PDD math dtype.- Parameters:
state (Tensor)
velocities (Tensor)
grid (Tensor)
start (int | Tensor)
end (int | Tensor)
- Return type:
Tensor
- make_shifted_flow_grid(grid_size, shift, *, max_t, device=None, dtype=torch.float32)#
Construct the fixed decreasing shifted rectified-flow grid.
Schedule construction and upper clamps use float64, matching FastGen’s RF scheduler. The completed grid is cast to the requested PDD math dtype. Low precision requests are promoted to float32 so distinct early intervals do not collapse for the canonical 128-node, shift-5 schedule.
- Parameters:
grid_size (int)
shift (float)
max_t (float)
device (device | str | None)
dtype (dtype)
- 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