CJD-Flows 1.0 — Normalizing Flows with a Constant Jacobian Determinant
Latent representations coloured by data density: a uniformly scaling flow (left) keeps density level sets aligned with latent norm balls, a general flow (right) does not.I’m happy to announce CJD-Flows 1.0, the first stable release of our PyTorch library for normalizing flows with a constant Jacobian determinant. The library was previously called USFlows; the new name says directly what the models have in common, while uniformly scaling flows (US flows) stays the name of the model class.
pip install cjd-flows · GitHub: USFlows/cjd-flows · Docs: usflows.github.io/cjd-flows
Why a constant determinant?
If log |det J_f(x)| does not depend on x, the flow changes the density of its
base distribution only by one global factor. Density level sets in the data
space are then exactly the pre-images of level sets in the latent space. With a
radial base whose density decreases with the radius, this means:
- 🔎 for anomaly detection, the latent norm ranks inputs exactly like the model density, and density thresholds become norm balls in the latent space;
- ✅ for verification, “the density is at least t” is a norm-ball constraint on the latents — for an L1 or L∞ base a polytope, which together with a piecewise-affine flow can be encoded exactly in MILP or SMT.
The price is expressivity: with a fixed base, volume-preserving flows are not universal (Draxler et al., ICML 2024). The library’s main ingredient, the learnable radial base distribution we first presented with VeriFlow at AISoLA 2023, removes exactly this limitation: an Lp-radial base whose 1-D norm distribution is learned jointly with the flow, so the latent level-set geometry stays intact.
What’s new in 1.0
🧱 A new core. The library has a new, pyro-free core that depends only on
torch and numpy. Every building block is a plain nn.Module, so .to(),
.double(), state_dict and torch.compile work as usual. Every layer returns
its log-determinant as a constant, so the constant determinant is now part of
the API contract instead of a property of particular layer combinations, and
cjd_flows.testing.check_bijection verifies it for your own layers.
🏗️ Many new layers. Masked and split additive couplings, GIN couplings, autoregressive layers (MADE shifts, causal convolutions, and a causal transformer over tokens with a KV cache), LU, Householder and orthogonal mixing, ActNorm, a global scale, Squeeze, Haar wavelets and Patchify, with MLP, ConvNet, MADE and ViT-style conditioners. With ReLU conditioners, most of them are piecewise affine.
📉 Learnable bases with a guaranteed monotone density. Radial bases now work
for any Lp norm with p > 0, with learnable norm distributions — generalized
Gamma mixtures, a Khinchine construction, a monotone profile, splines and more.
Several of them are guaranteed to have a radially decreasing density while
being trained, and profile_is_monotone() checks it. For these bases, the NLL
and the latent norm give exactly the same anomaly ranking.
🔎 Anomaly detection. Ready-made scores (NLL, latent norm, typicality, HDR
p-value), metrics (AUROC, AUPR, FPR@95) and a one-class protocol, plus the
evaluation suite RadialFlowEvaluator, which checks how well the latents of a
trained flow match the radial structure of its base.
✅ Verification. The VeriFlow(...) preset combines ReLU conditioners,
masked additive couplings in adjoint LU blocks and an L1-radial base with a
monotone learnable profile. Level-set utilities compute the latent radius for a
given density threshold or probability mass. merge_linear collapses
consecutive linear layers into one, and every flow exports to ONNX (forward,
inverse, log_prob and sample) for use in external verifiers. Optional
spectral normalization comes with Lipschitz upper bounds for the conditioners.
🛠️ Everything around it. A training loop with warmup and cosine schedule, EMA, early stopping and mixed precision; the MAF tabular benchmarks, image datasets and dequantization; saving and loading flows together with their configuration; and a documentation site with an API reference and tutorials.
🐛 Fixes in the old API. The pyro-based 0.9 API stays available through the
legacy extra and received several bug fixes. The experiment framework explib has been removed; paper experiments now live in
their own repositories.
A quick taste
Train a flow with a monotone learnable base and use it as an anomaly detector — the density and the latent norm agree on the ranking:
import torch
from cjd_flows import USFlow, fit
from cjd_flows.data.toy import make_toy
from cjd_flows.scores import anomaly_scores, auroc
x = make_toy("two_moons", 20_000)
flow = USFlow((2,), blocks=8, base="ggmix-monotone") # learnable, guaranteed monotone radial base
fit(flow, x, epochs=30, lr=1e-3)
x_in, x_out = make_toy("two_moons", 2_000), 2 * torch.randn(2_000, 2)
s_in, s_out = anomaly_scores(flow, x_in), anomaly_scores(flow, x_out)
for kind in ("nll", "latent_norm"):
print(kind, auroc(s_in[kind], s_out[kind])) # the same ranking, up to float rounding
And for verification: a piecewise-affine flow whose density level sets are pre-images of L1 balls, exported for an external tool:
import torch
from cjd_flows import VeriFlow
from cjd_flows.export import to_onnx, verify_onnx
from cjd_flows.radial import density_threshold_radius
flow = VeriFlow((10,)) # ReLU conditioners, adjoint LU blocks, monotone L1 base
assert flow.is_piecewise_affine()
# {x : log p(x) >= t} = f^{-1}( L1 ball of radius rho )
t = -15.0
rho = density_threshold_radius(flow.base, t - flow.log_abs_det())
to_onnx(flow, "veriflow.onnx", mode="forward") # the piecewise-affine map f, for your verifier
verify_onnx(flow, "veriflow.onnx", torch.randn(64, 10))
Built with an AI pair programmer
Like the recent AutStr releases, CJD-Flows 1.0 was developed in pair-programming sessions inside Claude Code: the new core, most of the new layers, the tests and the documentation were written by the model, while the mathematical direction, the design decisions and the review stayed human.
Install with pip install cjd-flows and see the project page
and the documentation to get started.