Skip to content

DLGModel

Bases: Module

Base class for DLG models.

Subclasses override :meth:build_model_graph to return the list of layers that make up the model. The layers are wired into a :class:torch.nn.Sequential and dtypes / quantization observers are stitched between consecutive layers automatically.

Subclass __init__ parameters must all be named keywords with default values (no *args / **kwargs) so they can be captured as self.config and round-tripped by :meth:save_pretrained / :func:dg.hub.from_pretrained.

build_model_graph

build_model_graph() -> list[BaseModule]

Return the ordered list of layers that form the model.

Subclasses must override this method.

post_init

post_init() -> None

Build :attr:model_graph from :meth:build_model_graph and wire layers.

Called automatically once the outermost subclass __init__ finishes.

save_pretrained

save_pretrained(path: str | Path) -> None

Serialize weights, source, config, and optional schema to path.

Writes model.pt (state dict), model.py (this class's source file), config.json (constructor kwargs) and, when a forward pass has already been run, schema.json. The saved directory is round-tripped through :func:dg.hub.from_pretrained to validate that model.py is self-contained.

Parameters:

Name Type Description Default
path str | Path

Destination directory. Created if missing.

required

Raises:

Type Description
RuntimeError

If the round-trip load fails. The destination directory is removed before re-raising.

to_schema

to_schema() -> dict[str, Any]

Export the model graph as a compiler-ready schema dictionary.

Raises:

Type Description
ValueError

If :meth:forward has not been run yet (layer shapes are recorded on the first forward pass).