cjd_flows.export

ONNX export of CJD flows.

to_onnx() wraps the flow in a tiny stateless nn.Module for one of four computations and exports it:

mode

input

output

computation

log_prob

x

log_prob

log p(x)

forward

x

z

z = f(x) (normalizing direction)

inverse

z

x

x = f⁻¹(z)

sample

z

x

x = f⁻¹(z) with externally drawn z ~ p_Z

Sampling is exported as a deterministic graph taking the latent z as input (no RNG inside the graph); draw z with flow.base.sample. Conditional flows (context_dim > 0) get a second input context unless with_context=False (then flow.default_context is baked in).

The legacy TorchScript exporter (dynamo=False) is used if available (it needs only onnx); otherwise the dynamo exporter (needs onnxscript).

cjd_flows.export.export_module(flow, mode='log_prob', with_context=None, freeze_linear=False)[source]

The wrapper nn.Module that to_onnx() exports (useful for other exporters).

Parameters:
  • flow (Module) – the flow.

  • mode (str) – one of MODES.

  • with_context (Optional[bool]) – add a context input (default: iff flow.context_dim > 0).

  • freeze_linear (bool) – replace (runs of) channel-linear layers by frozen AffineFromMatrix layers (see to_onnx()).

Return type:

Module

cjd_flows.export.to_onnx(flow, path, mode='log_prob', batch_size=1, dynamic_batch=True, opset=None, with_context=None, example=None, dynamo=None, freeze_linear=True)[source]

Exports flow to ONNX.

Parameters:
  • flow (Module) – a Flow.

  • path (Union[str, Path]) – output .onnx file.

  • mode (str) – "log_prob", "sample", "forward" or "inverse" (see module doc).

  • batch_size (int) – batch size of the example input used for tracing.

  • dynamic_batch (bool) – make the batch dimension symbolic.

  • opset (Optional[int]) – ONNX opset (default 17 for the legacy exporter, exporter default otherwise).

  • with_context (Optional[bool]) – add a context input for conditional flows (default: True iff flow.context_dim > 0).

  • example (Optional[Tensor]) – optional example input (shape (B, *event_shape), or (B, *latent_shape) for "sample"/"inverse").

  • dynamo (Optional[bool]) – force the exporter (None: legacy TorchScript exporter if available, else dynamo).

  • freeze_linear (bool) – export the channel-linear layers (LU, orthogonal, Householder, ActNorm, …) as frozen matrices via merge_linear() (consecutive ones are merged). Required for parametrizations that ONNX cannot express, e.g. the triangular solves of LULinear.inverse or the Cayley map of OrthogonalLinear.

Return type:

Path

Returns:

The path of the written file.

cjd_flows.export.verify_onnx(flow, path, x, context=None, rtol=1e-4, atol=1e-4)[source]

Compares an exported model (via onnxruntime) with the torch flow.

The mode is inferred from the ONNX output name. x is the graph input (data for log_prob/forward, latents for sample/inverse).

Return type:

float

Returns:

The maximum absolute deviation. Raises AssertionError if it exceeds atol + rtol·|reference|; raises ImportError without onnxruntime.

Parameters: