utils¶
Numerical stability¶
numerical
¶
Numerical stability utilities for mixed-precision training.
General-purpose helpers for preventing NaN/Inf values in deep learning computations, particularly under FP16/BF16 autocast.
StabilityConfig
¶
Default numerical stability bounds.
get_eps
¶
Return an appropriate epsilon for the given dtype.
safe_normalize
¶
L2-normalize with numerical stability (avoids division by ~zero norms).
Source code in deltaflow/utils/numerical.py
safe_sqrt
¶
Compute a square root, clamping the input to be non-negative first.
clamp_noise
¶
Clamp a noise tensor to prevent extreme values under FP16.
Source code in deltaflow/utils/numerical.py
clamp_prediction
¶
Clamp model predictions to prevent extreme values.
Source code in deltaflow/utils/numerical.py
clamp_loss
¶
Clamp a scalar loss to prevent gradient explosion.
Source code in deltaflow/utils/numerical.py
clamp_cosine_similarity
¶
Clamp cosine similarity to the valid range [-1, 1].
Tensor shapes¶
tensor
¶
Small tensor-shape helpers shared by interpolants, solvers, and inverse problems.
broadcast_time
¶
Reshape a time (or any per-sample scalar) so it broadcasts against x.
- A per-sample tensor of shape
(B,)becomes(B, 1, ..., 1)withx.dim()dimensions, e.g.(B,) -> (B, 1, 1, 1)for images. - A Python float or 0-d tensor becomes a 0-d tensor on
x's device.
Source code in deltaflow/utils/tensor.py
Optimal transport¶
ot
¶
Discrete optimal-transport assignment helpers.
The mini-batch OT coupling used by deltaflow.interpolants.ot.OTInterpolant
and deltaflow.trainer.coupling reduces to a single reusable primitive, a
squared-L2 assignment between two equal-size batches. It lives here so the
interpolant, the training-time coupling, and the visualization examples can
share one implementation.
Exact optimal assignment is computed with the Hungarian algorithm
(scipy.optimize.linear_sum_assignment).
batch_ot_permutation
¶
Return a permutation perm such that x0[perm] is OT-coupled to x1.
Costs are squared L2 distances on the flattened per-sample tensors.
Source code in deltaflow/utils/ot.py
Image I/O¶
image_io
¶
Image file I/O shared by the image datasets.
Pillow is an optional extra (pip install torchdeltaflow[images]), so it is
the only dependency resolved lazily. Every image-loading module goes through
this adapter rather than importing Pillow itself, which keeps the optional
dependency handling in exactly one place.
require_pillow
¶
Return the PIL.Image module, or raise an actionable ImportError.
list_images
¶
Recursively list image files under root in a deterministic order.
Source code in deltaflow/utils/image_io.py
open_image
¶
Open path as a PIL image in mode, optionally resized to size x size.