Diffusion Models #
Config-style diffusion model constructors plus reusable, dataset-independent DDPM/DDIM helpers.
The runnable examples decide where data comes from (CIFAR-10, ImageNet-style folders, synthetic artifacts). The definitions here are shape-parametric and can be reused by tests, examples, and future proof layer specifications.
Configuration for a convolutional diffusion-noise predictor.
- dataChannels : ℕ
Number of channels in the denoised sample.
Size of each sample axis. Values such as
[32, 32]work directly.Radius of the same-padding convolution kernel on each axis.
A radius of
1gives the usual kernel size3; every residual branch therefore preserves the sample grid by construction.
Instances For
Implementation helper for the shape-preserving convolutions in the epsilon predictors.
Instances For
Build one independently seeded two-convolution residual branch.
Instances For
Build a minimal epsilon-predictor conv net:
conv -> relu -> conv -> relu -> conv -> relu -> conv.
The first three convolutions use hiddenChannels; the last returns dataChannels noise values
at every spatial position.
Instances For
Build a same-resolution residual epsilon predictor.
Architecture:
stem conv -> relu -> residual block -> relu -> residual block -> relu -> output conv
Each residual block preserves hiddenChannels :: spatial and computes
$x+\operatorname{conv}(\operatorname{relu}(\operatorname{conv}(x)))$. The skip adds the block's
input unchanged, while its branch learns a correction at the same resolution. This constructor
omits U-Net downsampling, upsampling, and multi-scale skip concatenation.
Instances For
Deterministic Gaussian epsilon tensor for an arbitrary diffusion shape.
The (seed, step) pair is turned into the runtime RNG key, so examples and artifact generation can
reproduce the same noising path without ambient randomness.
Instances For
Whether a loaded cumulative diffusion coefficient is finite and probabilistically valid.
Instances For
A validated, nonempty diffusion schedule.
The constructor is private so runnable code never carries a separate proof that the coefficient
tensor can be indexed. Use Schedule.from for loaded coefficients or Schedule.linear for the
standard linear beta schedule.
Cumulative coefficients indexed by diffusion timestep.
Internal invariant used to cycle natural-number training steps safely.
Instances For
Append a constant time channel to every sample in batchShape.
The input layout is batchShape × channels × spatial. The result preserves the batch and spatial
axes and changes only the channel count from c to c + 1.
Instances For
Build an epsilon-prediction training sample.
By default, (seed, step) determines reproducible Gaussian noise. Pass noise := ... to use
a supplied tensor instead, without changing the noising formula:
$x_t=\sqrt{\bar{\alpha}_t}\,x_0+\sqrt{1-\bar{\alpha}_t}\,\varepsilon$, with target $\varepsilon$.
Instances For
One deterministic DDIM reverse update ($\eta=0$).
Given $x_t$, predicted epsilon, and adjacent schedule values, this estimates $x_0$ and remixes it to the previous timestep.
The default postprocessor clamps the intermediate $x_0$ estimate to the training image range
$[-1,1]$. This stabilizes compact image models whose reconstruction can drift outside the data
range. Supply postprocess := id for an unclipped reconstruction, or a shape-preserving transform
for another data domain. Postprocessing happens before remixing and does not recompute epsilon.
The reconstruction denominator uses the explicit sqrtAb > denominatorFloor branch, with the
floor in the else branch. The default is 1e-12. For finite schedule coefficients, a positive
finite floor keeps the denominator finite and nonzero.