cjd_flows.io

Saving and loading flows together with their builder configuration.

Flows built with a registered builder (USFlow(), VeriFlow(), or your own function decorated with register_builder()) remember the arguments they were built with (flow.builder_config). save() stores these arguments as JSON together with the weights, the dtype and the library version, so that load() can rebuild the flow without any code from the caller:

from cjd_flows import USFlow
from cjd_flows.io import load, save

flow = USFlow((2,), blocks=8, base="ggmix")
...  # train
save(flow, "flow.pt")
flow = load("flow.pt")           # rebuilt from the stored configuration

The file is a torch.save() archive of tensors, strings and numbers only, read with torch.load(..., weights_only=True); loading never executes pickled code. Flows that were not built by a registered builder, or whose builder arguments are not JSON-serializable (e.g. a custom conditioner factory or a base distribution instance), are saved without configuration and loaded into a model you construct yourself: load(path, model=my_flow).

Optimizer, scheduler and EMA state are not part of the file.

cjd_flows.io.save(flow, path, metadata=None)[source]

Saves the weights of flow together with its builder configuration.

Parameters:
  • flow (Module) – a flow (any Module). If it carries a builder_config (built by a registered builder), load() can rebuild it; otherwise a warning is issued and the file has to be loaded with load(path, model=...).

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

  • metadata (Optional[dict]) – optional JSON-serializable dict stored alongside (e.g. training settings or metrics).

Return type:

Path

Returns:

The path written to.

cjd_flows.io.load(path, model=None, map_location=None, strict=True)[source]

Loads a flow saved with save().

Parameters:
  • path (Union[str, Path]) – file written by save().

  • model (Optional[Module]) – module to load the weights into. If None, the flow is rebuilt from the stored builder configuration. A supplied model gets the saved dtype of every parameter and buffer (call e.g. .float() afterwards for another precision).

  • map_location (Union[str, device, None]) – device to move the flow to (default: CPU).

  • strict (bool) – passed to load_state_dict().

Return type:

Module

Returns:

The flow in eval mode, with the saved dtype of every parameter and buffer (when rebuilt, also for mixed precision) and the saved weights and buffers (including data-dependent initialization flags such as Shift initialized).

cjd_flows.io.load_metadata(path)[source]

Returns the builder configuration, versions and user metadata of a saved file (no model is built).

Return type:

dict

Parameters:

path (str | Path)

cjd_flows.io.register_builder(fn=None, *, name=None)[source]

Registers a model builder so that load() can rebuild its flows.

The decorated function must return an Module. Its call arguments, with defaults filled in, are recorded on the result as builder_config ({"builder": name, "kwargs": {...}}) if they are JSON-serializable (numbers, strings, None, lists/tuples, dicts with string keys); otherwise builder_config is None and the reason is kept in builder_config_error. Tuples are recorded, and replayed, as lists.

When a registered builder delegates to another one (e.g. VeriFlow calls USFlow) and returns its model, the outer call is recorded and the inner, fully resolved configuration is kept under "delegate". load() replays the outer builder and substitutes the saved inner arguments in its call of the delegated builder (when the explicitly passed arguments match), so the defaults of every builder in the chain are frozen at save time and everything the outer builder does is replayed as well.

Can be used as @register_builder or @register_builder(name="MyFlow").

Parameters:
cjd_flows.io.FORMAT_VERSION = 1

Version of the file layout written by save().