cjd_flows.core

Core abstractions of CJD-Flows.

The central invariant of the library is that every bijection has a constant Jacobian determinant, i.e. log|det J_f(x)| does not depend on x (nor on an optional context). The contract therefore separates the map from its log-determinant:

  • Bijection.forward(x, context=None) -> z (normalizing direction: data -> latent)

  • Bijection.inverse(z, context=None) -> x (generative direction: latent -> data)

  • Bijection.log_abs_det() -> Tensor (0-dim tensor, depends only on parameters)

so that a flow’s log-density is

log p(x) = log p_Z(f(x)) + log|det J_f|

where the second summand is a single scalar shared by all inputs. Consequently, density level sets in data space are exactly the pre-images of density level sets of the base distribution (“density level-set preservation”).

Conventions

  • Tensors have exactly one leading batch dimension followed by the event dimensions, i.e. shape (B, *event_shape). Vector data has event_shape == (D,); image-like data has (C, H, W) (any number of spatial dimensions is allowed). The channel dimension is always dim 1.

  • Bijections are plain torch.nn.Module objects without hidden caches, so they work with torch.compile, state_dict, .to(), .double(), DDP, etc.

cjd_flows.core.as_shape(shape)[source]

Normalizes an int/sequence to a tuple of ints.

Return type:

Tuple[int, ...]

Parameters:

shape (int | Sequence[int] | Size)

class cjd_flows.core.Bijection[source]

Bases: Module

Base class for all bijections with constant Jacobian determinant.

Subclasses implement forward(), inverse() and (if the determinant is not one) log_abs_det().

piecewise_affine

True if the map is piecewise affine provided its conditioner networks are piecewise affine (e.g. ReLU MLPs/CNNs). Such flows admit exact SMT/MILP encodings.

Type:

bool

volume_preserving

True if |det J| == 1 by construction.

Type:

bool

forward(x, context=None)[source]

Define the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

Return type:

Tensor

Parameters:
log_abs_det()[source]

Constant log|det J_f| of the forward (normalizing) map as a 0-dim tensor.

Return type:

Tensor

forward_and_log_det(x, context=None)[source]

Convenience API compatible with nflows/normflows-style code.

Returns (z, ladj) where ladj has shape (B,).

Return type:

Tuple[Tensor, Tensor]

Parameters:
property inv: Bijection

The inverse bijection (a lightweight view sharing parameters).

is_piecewise_affine()[source]

Whether this bijection (including its submodules) is piecewise affine.

Return type:

bool

class cjd_flows.core.VolumePreserving[source]

Bases: Bijection

Marker base class for bijections with |det J| == 1.

log_abs_det()[source]

Constant log|det J_f| of the forward (normalizing) map as a 0-dim tensor.

Return type:

Tensor

class cjd_flows.core.InverseBijection[source]

Bases: Bijection

Swaps the directions of a bijection.

__init__(bijection)[source]

Initialize internal Module state, shared by both nn.Module and ScriptModule.

Parameters:

bijection (Bijection)

forward(x, context=None)[source]

Define the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

Return type:

Tensor

Parameters:
log_abs_det()[source]

Constant log|det J_f| of the forward (normalizing) map as a 0-dim tensor.

Return type:

Tensor

property inv: Bijection

The inverse bijection (a lightweight view sharing parameters).

is_piecewise_affine()[source]

Whether this bijection (including its submodules) is piecewise affine.

Return type:

bool

class cjd_flows.core.Chain[source]

Bases: Bijection

Composition f = f_n ∘ ... ∘ f_1 (applied in list order in the forward direction).

__init__(*bijections)[source]

Initialize internal Module state, shared by both nn.Module and ScriptModule.

Parameters:

bijections (Bijection | Iterable[Bijection])

forward(x, context=None)[source]

Define the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

Return type:

Tensor

Parameters:
log_abs_det()[source]

Constant log|det J_f| of the forward (normalizing) map as a 0-dim tensor.

Return type:

Tensor

is_piecewise_affine()[source]

Whether this bijection (including its submodules) is piecewise affine.

Return type:

bool

class cjd_flows.core.Identity[source]

Bases: VolumePreserving

forward(x, context=None)[source]

Define the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

Return type:

Tensor

Parameters:
class cjd_flows.core.BaseDistribution[source]

Bases: Module

Interface for (learnable) base distributions.

Subclasses provide event_shape, log_prob(z) -> (B,) and sample(sample_shape) -> (*sample_shape, *event_shape).

forward(z)[source]

Define the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

Return type:

Tensor

Parameters:

z (Tensor)

class cjd_flows.core.StandardNormal[source]

Bases: BaseDistribution

Isotropic standard normal base distribution (non-learnable).

__init__(event_shape)[source]

Initialize internal Module state, shared by both nn.Module and ScriptModule.

Parameters:

event_shape (int | Sequence[int] | Size)

class cjd_flows.core.Flow[source]

Bases: Module

A normalizing flow with constant Jacobian determinant.

Parameters:
  • bijection (Union[Bijection, Sequence[Bijection]]) – normalizing map f (data -> latent). Lists are wrapped in a Chain.

  • base (BaseDistribution) – base distribution p_Z over the latent space.

  • context_dim (int) – dimension of an optional context vector passed to all conditioners (e.g. class embeddings or the soft-training noise scale).

  • default_context (Optional[float]) – value used when context is omitted but context_dim > 0 (0.0 for soft training, where sigma=0 is the clean data distribution).

  • event_shape (Union[int, Sequence[int], Size, None]) – shape of a single data point. Defaults to the event_shape of the first layer of the bijection (e.g. the input shape of a leading Squeeze) and otherwise to the base distribution’s event shape. Pass it explicitly if the first layer does not declare its input shape but the bijection changes the shape.

The model computes log p(x) = log p_Z(f(x)) + log|det J_f|.

__init__(bijection, base, context_dim=0, default_context=0.0, event_shape=None)[source]

Initialize internal Module state, shared by both nn.Module and ScriptModule.

Parameters:
property event_shape: Tuple[int, ...]

Shape of a single data point.

property latent_shape: Tuple[int, ...]

Shape of a single latent point.

log_abs_det()[source]

The constant log|det J_f| of the normalizing map.

Return type:

Tensor

transform(x, context=None)[source]

Maps data to latent space, z = f(x).

Return type:

Tensor

Parameters:
generate(z, context=None)[source]

Maps latents to data space, x = f^{-1}(z).

Return type:

Tensor

Parameters:
log_prob(x, context=None)[source]

Exact log-density log p(x) of shape (B,).

Inputs with more than one leading batch dimension are flattened and reshaped back.

Return type:

Tensor

Parameters:
forward(x, context=None)[source]

Define the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

Return type:

Tensor

Parameters:
latent_log_prob(x, context=None)[source]

log p_Z(f(x)) – equals log p(x) up to the constant log|det J_f|.

Return type:

Tensor

Parameters:
sample(sample_shape=(), context=None)[source]

Draws samples x = f^{-1}(z) with z ~ p_Z.

Return type:

Tensor

Parameters:
rsample(sample_shape=(), context=None)[source]

Like sample() but differentiable w.r.t. the flow parameters.

Return type:

Tensor

Parameters:
fit(*args, **kwargs)[source]

Trains the flow; see cjd_flows.train.fit().