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
flowtogether with its builder configuration.- Parameters:
flow (
Module) – a flow (anyModule). If it carries abuilder_config(built by a registered builder),load()can rebuild it; otherwise a warning is issued and the file has to be loaded withload(path, model=...).metadata (
Optional[dict]) – optional JSON-serializable dict stored alongside (e.g. training settings or metrics).
- Return type:
- 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:
model (
Optional[Module]) – module to load the weights into. IfNone, 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 toload_state_dict().
- Return type:
- 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
Shiftinitialized).
- cjd_flows.io.load_metadata(path)[source]¶
Returns the builder configuration, versions and user metadata of a saved file (no model is built).
- 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 asbuilder_config({"builder": name, "kwargs": {...}}) if they are JSON-serializable (numbers, strings,None, lists/tuples, dicts with string keys); otherwisebuilder_configisNoneand the reason is kept inbuilder_config_error. Tuples are recorded, and replayed, as lists.When a registered builder delegates to another one (e.g.
VeriFlowcallsUSFlow) 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_builderor@register_builder(name="MyFlow").