Lazy Construction
Lazy Construction
Tutorial: First Steps, Nested CompositionSee also: Lie-Typing, API: language
Eager vs Lazy
# Eager: object exists now, no config possible
model = nn.Linear(784, 10)
# Lazy: DictConfig exists now, object constructed on demand
model_cfg = L.call(nn.Linear)(in_features=784, out_features=10)
model = laco.instantiate(model_cfg)
The lazy version can be serialized, overridden, merged, and reproduced.
L.call
L.call(target, *, strict=True, convert="none", recursive=True, **meta)
Returns a callable that accepts keyword arguments and produces a DictConfig.
The wire format is a Hydra-compatible dict:
_target_: torch.nn.Linear
in_features: 784
out_features: 10
strict=True (default): validates kwargs against the target's signature at
config-construction time, catching typos before training starts.
strict=False: disables validation, useful for C-extension targets whose
signatures cannot be introspected.
convert: controls how Hydra converts structured configs on instantiation.
Defaults to "none" (preserve DictConfig wrappers).
recursive: if True (default), Hydra recursively instantiates nested
_target_ nodes.
L.partial
optimizer_cfg = L.partial(torch.optim.Adam)(lr=1e-3)
optimizer = laco.instantiate(optimizer_cfg)
# optimizer is functools.partial(Adam, lr=1e-3)
optimizer_with_params = optimizer(params=model.parameters())
Equivalent to L.call but sets _partial_: true in the wire format, so
laco.instantiate returns a functools.partial rather than calling the target.
Common pattern: store the partial in the config; bind params= at training time.
L.just
node = L.just(42)
laco.instantiate(node) # → 42
Wraps an already-constructed value in a DictConfig so it survives
serialization. Serializes to a _target_: laco.ops.identity node.
Note: non-serializable objects (tensors, file handles) will fail on
laco.dump. For those, store a path or descriptor instead.
L.required[T]()
@L.params
class hps:
vocab_size: int = L.required[int]()
Produces the OmegaConf MISSING sentinel (??? in YAML). Accessing an
unset required field raises MissingMandatoryValue. The generic subscript
[int] is for the type-checker; it has no runtime effect.
laco.instantiate
result = laco.instantiate(cfg)
Thin wrapper around hydra.utils.instantiate. Honors all Hydra knobs
(_target_, _args_, _partial_, _convert_, _recursive_).
Primitive top-level values (int, float, str, bool, None, set,
frozenset, bytes, type, functions) are returned unchanged: callers
do not need to check the type before passing a leaf.
LACO_TRACE=1 logs the full config tree before each instantiation.
Cyclic configuration graphs raise LacoCycleError. See API: instantiate.