Interpolation
Interpolation
Tutorial: Params and RefsSee also: API: language
OmegaConf Interpolation Basics
OmegaConf resolves ${key} references at access time, not at load time.
This means a config can reference a value that is overridden after loading:
lr: 1e-3
optimizer:
_target_: torch.optim.Adam
lr: ${lr} # resolved when cfg.optimizer.lr is accessed
Override lr and the optimizer picks it up automatically.
@L.params
@L.params turns a plain Python class into a hyperparameter block whose
attribute access generates OmegaConf interpolation strings:
@L.params
class hps:
lr: float = 1e-3
weight_decay: float = 1e-4
optimizer = L.partial(torch.optim.Adam)(
lr=hps.lr, # → "${hps.lr}"
weight_decay=hps.weight_decay, # → "${hps.weight_decay}"
)
The class is decorated with dataclass_transform so IDEs understand its
attribute types. At runtime hps.lr returns the string "${hps.lr}", which
OmegaConf resolves when the config is accessed.
Override one place, all references update:
cfg = laco.load("configs/train.py?hps.lr=1e-4")
# cfg.optimizer.lr is now 1e-4
L.ref
L.ref("${key}") creates a typed interpolation reference for one-off
cross-config connections:
model_out = L.ref("${model.out_features}")
classifier = L.call(nn.Linear)(
in_features=model_out,
out_features=10,
)
The type annotation from L.ref[int]("${model.out_features}") helps the
type-checker treat model_out as int rather than Any.
L.r.*: Resolver Namespace
Laco ships 11 custom OmegaConf resolvers under the L.r namespace:
| Resolver | Example | Result |
|---|---|---|
L.r.sum(a, b) | L.r.sum(hps.a, hps.b) | ${sum:${hps.a},${hps.b}} |
L.r.min(a, b) | L.r.min(hps.a, hps.b) | ${min:${hps.a},${hps.b}} |
L.r.max(a, b) | — | ${max:…} |
L.r.div(a, b) | L.r.div(hps.lr, 10) | lr divided by 10 |
L.r.pow(a, b) | L.r.pow(2, hps.n) | 2^n |
L.r.mod(a, b) | — | a % b |
L.r.neg(a) | — | -a |
L.r.reciprocal(a) | — | 1/a |
L.r.abs(a) | — | abs(a) |
L.r.round(a, n) | — | round(a, n) |
L.r.math(expr) | L.r.math("sqrt(2)") | math.sqrt(2) |
These resolvers are registered automatically on import laco and are also
available to vanilla Hydra apps via the hydra_plugins.laco_resolvers plugin.
Resolution Order
- OmegaConf resolves
${key}references lazily (on access). - Hydra-style overrides (
key=val) are applied bylaco.loadbefore the config is returned. - Custom resolvers run inside OmegaConf's resolver chain.