instantiate#
- instantiate(config, *, recursive_instantiate=False, **kwargs)#
Instantiate an object from a
LazyConfigor__target__dict.Resolution pipeline
If
configis aLazyConfig, call it with any**kwargsto produce aDictConfig.If the config has no
__target__key, recursively instantiate each value and return aDictConfig(useful for nested non-lazy sub-configs).Resolve
__target__to the actual class/callable viaimportlib.import_module().Deep-copy, resolve OmegaConf interpolations, merge
**kwargsoverrides, recursively instantiate nested configs, and evaluate arithmetic strings (e.g."128 * 4"→512).Call
target(**processed_args)and return the result.
Placeholder handling
Nested configs that still contain
PLACEHOLDERvalues are left as config dicts rather than instantiated.- Parameters:
config (Dict[str, Any] | omegaconf.DictConfig | LazyConfig) – A
LazyConfiginstance, an OmegaConfDictConfig, or a plaindictcontaining at least a"__target__"key.recursive_instantiate (bool) – If
True, recursively instantiate all nested configs found in argument values. IfFalse(default), nested configs are left asDictConfigobjects.**kwargs – Extra keyword arguments merged into (and overriding) the config’s stored arguments before instantiation. Commonly used to inject per-block arguments such as
drop_path_rate.
- Returns:
The instantiated object (result of calling the resolved
__target__with the processed arguments).- Raises:
TypeError – If
target(**processed_args)fails; re-raised with the target’s signature in the error message for easier debugging.- Return type:
Example:
cfg = LazyConfig(torch.nn.LayerNorm)(normalized_shape=768) norm = instantiate(cfg, eps=1e-5) # override default eps