dew.diffusion
Forward processes, noise schedules and the parameterizations over them.
Process pairs a schedule with a prediction transform, and a run’s
objective and every solver read that process. presets holds the named
combinations, and discrete has the masked-token process.
Each name below is imported from its own module when first read, so a discrete text process reads none of the image schedules and presets.
| Name | Summary |
|---|---|
ConsistencyBoundary | The boundary parameterization of a latent consistency model, as in Diffusers’ LCMScheduler. |
ContinuousNoiseScheduler | A schedule whose time is a fraction of the trajectory, not an index. Documented in dew.diffusion.schedules. |
CosineNoiseScheduler | The cosine beta table of Nichol and Dhariwal 2021. Documented in dew.diffusion.schedules. |
Denoiser | Denoises with one model, its parameters and its conditions. Documented in dew.diffusion.process. |
DirectPredictionTransform | |
DiscreteNoiseScheduler | A variance preserving schedule tabulated from betas, DDPM style. Documented in dew.diffusion.schedules. |
EDMNoiseScheduler | Draws training sigmas from exp(N(P_mean, P_std^2)). Documented in dew.diffusion.schedules. |
EpsilonPredictionTransform | |
FlowMatchPredictionTransform | The model predicts the rectified flow velocity u = epsilon - x_0. |
FlowMatchingScheduler | Rectified flow / conditional flow matching on the linear path. Documented in dew.diffusion.schedules. |
GeneralizedNoiseScheduler | The variance-exploding schedule family of the EDM paper. Documented in dew.diffusion.schedules. |
KarrasPredictionTransform | The EDM preconditioning of the model’s input and output. |
KarrasVENoiseScheduler | Places sigmas along t with the rho spacing of the EDM paper. Documented in dew.diffusion.schedules. |
LinearNoiseScheduler | The linear beta table of DDPM, scaled to the step count. Documented in dew.diffusion.schedules. |
MinSNR | Weights the loss with min-SNR-gamma. |
NoiseScheduler | Defines a forward process x_t = alpha(t) x_0 + sigma(t) eps on the time domain [0, T]. Documented in dew.diffusion.schedules. |
PredictionTransform | Defines what the model predicts and how x_0 and epsilon are recovered from its output. |
Process | Pairs a noise schedule with what the model predicts on it and how the loss is weighted. Documented in dew.diffusion.process. |
ScheduleWeighting | Weights the loss with the schedule’s own weight. |
SourceLimitedPrediction | Limits x_0 the way a published scheduler’s step does. |
SqrtContinuousNoiseScheduler | Square-root schedule from Diffusion-LM. Documented in dew.diffusion.schedules. |
VPredictionTransform | The model predicts v = alpha eps - sigma x_0, divided by sqrt(alpha^2 + sigma^2). |
VelocityLoss | Scores a clean-sample prediction in velocity space on the linear path, as JiT’s loss does. |
Weighting | Weights a per-example loss, given the schedule and the prediction transform. |
broadcast_rates | Return the schedule’s rates (alpha, sigma) at t, shaped to broadcast against x. |
cosine_beta_schedule | Return the cosine beta table of Nichol and Dhariwal 2021. Documented in dew.diffusion.schedules. |
discrete | Masked (absorbing-state) discrete diffusion, with the interface of the Gaussian one. |
expand | Return a per-example coefficient [B] reshaped to broadcast against x [B, ...]. Documented in dew.diffusion.schedules. |
linear_beta_schedule | Return DDPM’s betas, linear from beta_start to beta_end over 1000 steps. Documented in dew.diffusion.schedules. |
presets | Named diffusion conventions, stored as dataclasses in a run’s run.json. |
ConsistencyBoundary
Section titled “ConsistencyBoundary”class ConsistencyBoundary( inner: PredictionTransform, timestep_scaling: float = 10.0, sigma_data: float = 0.5,)The boundary parameterization of a latent consistency model, as in Diffusers’ LCMScheduler.
The method is from Luo et al. 2023 (arXiv 2310.04378). inner converts
the model’s output to x_0, and the consistency function is
f = c_skip x_t + c_out x_0, with
c_skip = sigma_data^2 / (s^2 + sigma_data^2) and
c_out = s / sqrt(s^2 + sigma_data^2) at the scaled time
s = timestep_scaling t. So f is x_t itself at t = 0. The returned x_0
is f, and epsilon is recovered from it.
ConsistencyBoundary.pred_transform
Section titled “ConsistencyBoundary.pred_transform”def pred_transform(x_t, prediction, rates, t)ConsistencyBoundary.backward_diffusion
Section titled “ConsistencyBoundary.backward_diffusion”def backward_diffusion(x_t, prediction, rates)ConsistencyBoundary.get_input_scale
Section titled “ConsistencyBoundary.get_input_scale”def get_input_scale(rates)DirectPredictionTransform
Section titled “DirectPredictionTransform”class DirectPredictionTransform(PredictionTransform)DirectPredictionTransform.backward_diffusion
Section titled “DirectPredictionTransform.backward_diffusion”def backward_diffusion(x_t, prediction, rates)EpsilonPredictionTransform
Section titled “EpsilonPredictionTransform”class EpsilonPredictionTransform(PredictionTransform)EpsilonPredictionTransform.backward_diffusion
Section titled “EpsilonPredictionTransform.backward_diffusion”def backward_diffusion(x_t, prediction, rates)EpsilonPredictionTransform.get_target
Section titled “EpsilonPredictionTransform.get_target”def get_target(x_0, epsilon, rates)EpsilonPredictionTransform.target_error_scale
Section titled “EpsilonPredictionTransform.target_error_scale”def target_error_scale(snr)FlowMatchPredictionTransform
Section titled “FlowMatchPredictionTransform”class FlowMatchPredictionTransform(PredictionTransform)The model predicts the rectified flow velocity u = epsilon - x_0.
This is the constant velocity of the linear path, so x_0 and epsilon are each one step from x_t along it.
FlowMatchPredictionTransform.backward_diffusion
Section titled “FlowMatchPredictionTransform.backward_diffusion”def backward_diffusion(x_t, prediction, rates)FlowMatchPredictionTransform.get_target
Section titled “FlowMatchPredictionTransform.get_target”def get_target(x_0, epsilon, rates)FlowMatchPredictionTransform.target_error_scale
Section titled “FlowMatchPredictionTransform.target_error_scale”def target_error_scale(snr)KarrasPredictionTransform
Section titled “KarrasPredictionTransform”class KarrasPredictionTransform(sigma_data: float = 0.5, *, velocity: bool = False)The EDM preconditioning of the model’s input and output.
This is Table 1 of Karras et al. 2022. The model’s input is c_in x_t, and its raw output F gives x_0 = c_skip x_t + c_out F. The loss compares that x_0 with the clean sample, so the target is x_0, and a weight defined on the x_0 loss, such as min-SNR’s, applies without conversion. EDM’s lambda = 1 / c_out^2 is the schedule’s own weight on this loss.
velocity matches Diffusers 0.34.0’s EDM
prediction_type="v_prediction". Its precondition_outputs negates
c_out, so the model outputs the velocity of the preconditioned path
instead of the offset to its endpoint.
KarrasPredictionTransform.backward_diffusion
Section titled “KarrasPredictionTransform.backward_diffusion”def backward_diffusion(x_t, prediction, rates)KarrasPredictionTransform.pred_transform
Section titled “KarrasPredictionTransform.pred_transform”def pred_transform(x_t, prediction, rates, t)KarrasPredictionTransform.get_input_scale
Section titled “KarrasPredictionTransform.get_input_scale”def get_input_scale(rates)MinSNR
Section titled “MinSNR”class MinSNR(gamma: float)Weights the loss with min-SNR-gamma.
min-SNR-gamma is from Hang et al. 2023. The weight is min(SNR, gamma) on the x_0 loss, converted into the space the model trains in, and it replaces the schedule’s own weight. At zero SNR (a table whose last step keeps no signal) the epsilon conversion is 0 / 0, so the weight there is one, as the authors’ code sets it.
PredictionTransform
Section titled “PredictionTransform”class PredictionTransform(*, normalize_input: bool = False)Defines what the model predicts and how x_0 and epsilon are recovered from its output.
The base class provides the x_0 target and the identity output
transform. A subclass must implement backward_diffusion, because the
base raises NotImplementedError. With normalize_input, the model’s
input is scaled by 1 / sqrt(alpha^2 + sigma^2).
rates in every method is the schedule’s (alpha, sigma) pair, already
shaped to broadcast against the batch.
divides_by_signal: bool-
Whether recovering
(x_0, epsilon)from a prediction divides by the signal rate alpha, and so is undefined where alpha is 0, as at a rectified flow’s pure-noise end.
PredictionTransform.pred_transform
Section titled “PredictionTransform.pred_transform”def pred_transform(x_t, prediction, rates, t) -> jax.ArrayReturn the model’s raw output at (x_t, t) as a prediction in target space.
t is passed for a parameterization whose scaling depends on the
time itself and not on the rates, such as ConsistencyBoundary.
PredictionTransform.forward_diffusion
Section titled “PredictionTransform.forward_diffusion”def forward_diffusion(x_0, epsilon, rates) -> tuple[jax.Array, ArrayLike, jax.Array]Return (x_t, c_in, target) for a clean sample and its noise.
x_t is the noised sample alpha x_0 + sigma epsilon, c_in is the scale applied to the model’s input, and target is what the model should output for x_t.
PredictionTransform.backward_diffusion
Section titled “PredictionTransform.backward_diffusion”def backward_diffusion(x_t, prediction, rates) -> tuple[jax.Array, jax.Array]Return (x_0, epsilon) recovered from a prediction in target space.
PredictionTransform.get_target
Section titled “PredictionTransform.get_target”def get_target(x_0, epsilon, rates) -> jax.ArrayPredictionTransform.get_input_scale
Section titled “PredictionTransform.get_input_scale”def get_input_scale(rates) -> ArrayLikePredictionTransform.target_error_scale
Section titled “PredictionTransform.target_error_scale”def target_error_scale(snr) -> ArrayLikeReturn ||target error||^2 / ||x_0 error||^2 at the given SNR.
min-SNR-gamma and the other loss weights are defined on the x_0 loss. Dividing such a weight by this ratio converts it into the space the model trains in.
ScheduleWeighting
Section titled “ScheduleWeighting”class ScheduleWeighting()Weights the loss with the schedule’s own weight.
SourceLimitedPrediction
Section titled “SourceLimitedPrediction”class SourceLimitedPrediction( inner: PredictionTransform, *, clip: float | None = None, threshold: tuple[float, float] | None = None, recompute_epsilon: bool = True,)Limits x_0 the way a published scheduler’s step does.
inner recovers x_0 from the model’s output, and then either dynamic
thresholding or a plain clamp to clip limits it. Thresholding, given
as threshold=(ratio, maximum), follows Saharia et al. 2022. Each
sample’s level is its own ratio quantile of |x_0|, kept between 1 and
maximum, and x_0 is clamped to that level and then divided by it. When
a source declares both limits, thresholding is used, because that is the
order in which the source’s step tests them. Giving neither raises
ValueError.
recompute_epsilon is whether the source recomputes epsilon from the
limited x_0. DDPM’s posterior, DEIS and the noise-prediction DPM-Solver
algorithms do, so the limit reaches their update through epsilon as
well. DDIM keeps the model’s own output as its epsilon, so only its x_0
term is limited.
The limit is not linear in the model’s output. So a guided walk applies it once, when it converts the combined output, and not to each guidance branch.
SourceLimitedPrediction.pred_transform
Section titled “SourceLimitedPrediction.pred_transform”def pred_transform(x_t, prediction, rates, t)SourceLimitedPrediction.backward_diffusion
Section titled “SourceLimitedPrediction.backward_diffusion”def backward_diffusion(x_t, prediction, rates)SourceLimitedPrediction.get_target
Section titled “SourceLimitedPrediction.get_target”def get_target(x_0, epsilon, rates)SourceLimitedPrediction.get_input_scale
Section titled “SourceLimitedPrediction.get_input_scale”def get_input_scale(rates)SourceLimitedPrediction.target_error_scale
Section titled “SourceLimitedPrediction.target_error_scale”def target_error_scale(snr)VPredictionTransform
Section titled “VPredictionTransform”class VPredictionTransform(PredictionTransform)The model predicts v = alpha eps - sigma x_0, divided by sqrt(alpha^2 + sigma^2).
VPredictionTransform.backward_diffusion
Section titled “VPredictionTransform.backward_diffusion”def backward_diffusion(x_t, prediction, rates)VPredictionTransform.get_target
Section titled “VPredictionTransform.get_target”def get_target(x_0, epsilon, rates)VPredictionTransform.target_error_scale
Section titled “VPredictionTransform.target_error_scale”def target_error_scale(snr)VelocityLoss
Section titled “VelocityLoss”class VelocityLoss(t_eps: float = 0.05)Scores a clean-sample prediction in velocity space on the linear path, as JiT’s loss does.
JiT is Li & He 2025, “Back to Basics: Let Denoising Generative Models
Denoise”. With x_t = (1 - sigma) x_0 + sigma eps, the velocity is
(x_0 - x_t) / sigma, so its squared error is the squared x_0 error over
sigma^2. sigma is floored at t_eps in both the target and the
prediction, as the reference clamps it. The weight replaces the
schedule’s own weight. Calling it on a process that does not predict
with DirectPredictionTransform raises ValueError.
Weighting
Section titled “Weighting”class Weighting(Protocol)Weights a per-example loss, given the schedule and the prediction transform.
broadcast_rates
Section titled “broadcast_rates”def broadcast_rates(schedule: NoiseScheduler, t, x) -> tuple[jax.Array, jax.Array]Return the schedule’s rates (alpha, sigma) at t, shaped to broadcast against x.
discrete
Section titled “discrete”Masked (absorbing-state) discrete diffusion, with the interface of the Gaussian one.
The forward process replaces each token with a mask id independently, with
a probability that grows with t in [0, 1]. MaskingSchedule.alpha(t) is
the fraction of tokens still visible at t. Training minimizes the
continuous-time negative ELBO of MDLM (Sahoo et al. 2024, “Simple and
Effective Masked Diffusion Language Models”), which is the cross entropy of
the model’s prediction at the masked positions, weighted by
-alpha’(t) / (1 - alpha(t)).
Sampling reverses the process one interval at a time. A masked token is
revealed with probability (alpha(s) - alpha(t)) / (1 - alpha(t)), and a
revealed token is drawn from the model’s categorical distribution. This is
MDLM’s _ddpm_update.
DiscreteProcess provides what dew.sampling.sample needs from a process:
a time grid, an initial state and a denoiser. The denoiser’s two outputs
sit where a Gaussian denoiser returns x_0 and epsilon. They are the model’s
argmax fill of the masked positions and the log-probabilities the solver
draws from.
presets
Section titled “presets”Named diffusion conventions, stored as dataclasses in a run’s run.json.
A convention is a choice of noise schedule, prediction target, loss
weighting and sampling grid. A preset is a frozen dataclass of the numbers
that define one convention. An objective builds the preset’s Process when
it is constructed. You can also call a preset to build its process yourself,
to inspect the schedule or to sample at a low level. A record that holds a
preset’s fields rebuilds the preset exactly.