CJD-Flows v1 design¶
This document fixes the architecture of the pyro-free v1 core. All new code follows it.
Contract¶
See src/cjd_flows/core.py:
Bijection(nn.Module)withforward(x, context=None) -> z— normalizing direction (data → latent),inverse(z, context=None) -> x— generative direction,log_abs_det() -> Tensor— a 0-dim tensor; constant inxandcontext.Class flags:
piecewise_affineandvolume_preserving.
Chain,InverseBijection(b.inv),Identity.BaseDistribution(nn.Module)withevent_shape,log_prob(z) -> (B,)andsample(sample_shape).Flow(bijection, base, context_dim=0, default_context=0.0)withlog_prob,sample,rsample,transform,generate,log_abs_detandfit.
Conventions¶
Tensors are
(B, *event_shape). Vector data is(B, D). Spatial data is(B, C, *spatial)(1–3 spatial dims). The channel dimension is dim 1; for vectors, D is the channel dimension.No hidden caches and no custom
.to()overrides. Masks, permutations and constant matrices are buffers. Create every tensor with thedevice/dtypeof the parameters or the input (x.new_*,torch.eye(n, device=..., dtype=...)).log_abs_det()must be differentiable w.r.t. parameters and must not depend on the input. Layers whose determinant depends on the spatial size receiveevent_shapeat construction.Every bijection must pass
cjd_flows.testing.check_bijectionin float64.Conditioners are plain
nn.Modules(B, C, *spatial)[, context (B, ctx)] -> (B, C_out, *spatial). Their last layer is zero-initialized, so every coupling starts as the identity.Docstrings are Google style with type hints. No bare
except. Do not import pyro anywhere in the new modules.Optional dependencies (scipy, sklearn, onnx, torchvision, matplotlib) are imported lazily inside functions.
Package layout (new modules)¶
Module |
Content |
|---|---|
|
Contract, |
|
|
|
LU (free / SL(n) unit-det), Householder (efficient), orthogonal (Cayley / matrix-exp), ActNorm (free or zero-sum log-scale), |
|
Additive masked coupling (checkerboard / channel / custom mask), split coupling (channel/spatial, Jet-style), GIN coupling (zero-sum log-scale, bounded) |
|
Autoregressive layers with det = 1: MADE shifts (vectors), causal convolutions, causal transformer over tokens |
|
Squeeze (space-to-depth), Haar wavelet (orthonormal), Patchify |
|
|
|
Lp-radial base distributions with learnable 1-D norm distributions ( |
|
|
|
Preprocessing (uniform dequantization, affine scaling, bpd), toy 2-D datasets, MAF tabular benchmarks, image datasets |
|
Anomaly / OOD scores (NLL, latent norm, typicality, HDR p-value) and metrics (AUROC, AUPR, FPR@95) |
|
|
|
|
|
ONNX export wrappers (log_prob / sample / forward / inverse), |
|
High-level builders ( |
Legacy pyro-based modules (flows.py, transforms.py, distributions.py,
networks.py) stay importable for backward compatibility. pyro
becomes an optional [legacy] extra. The experiment framework explib/ was
removed in 1.0 (tag archive/explib-final).
Theory notes for implementers¶
A constant-Jacobian flow cannot change the distribution of density values of its base (
μ(t) = vol{p>t}is invariant up to the global scale). The learnable 1-D norm distribution of the radial base (introduced with VeriFlow) removes this limitation. Draxler et al. (ICML 2024) prove the corresponding non-universality of volume-preserving flows with a fixed base. The choice ofpin the Lp norm does not change expressivity.Radial profile:
h(r) = p_R(r) / (d V_p r^{d-1}), withV_p = (2Γ(1+1/p))^d / Γ(1+d/p)andV_∞ = 2^d. Uselgamma; never formr^{d-1}.Volume coordinate
t = V_p r^d:p_T(t) = h(r). A radially decreasing base corresponds to a decreasing 1-D density int.Monotonicity: Gamma(k) norms are monotone iff
k ≤ dand bounded iffk ≥ d. LogNormal norms are always “shell” profiles (non-monotone). The Khinchine constructionR = C·U^{1/d}gives monotone profiles for any law ofC.