instantiate#

instantiate(config, *, recursive_instantiate=False, **kwargs)#

Instantiate an object from a LazyConfig or __target__ dict.

Resolution pipeline

  1. If config is a LazyConfig, call it with any **kwargs to produce a DictConfig.

  2. If the config has no __target__ key, recursively instantiate each value and return a DictConfig (useful for nested non-lazy sub-configs).

  3. Resolve __target__ to the actual class/callable via importlib.import_module().

  4. Deep-copy, resolve OmegaConf interpolations, merge **kwargs overrides, recursively instantiate nested configs, and evaluate arithmetic strings (e.g. "128 * 4"512).

  5. Call target(**processed_args) and return the result.

Placeholder handling

Nested configs that still contain PLACEHOLDER values are left as config dicts rather than instantiated.

Parameters:
  • config (Dict[str, Any] | omegaconf.DictConfig | LazyConfig) – A LazyConfig instance, an OmegaConf DictConfig, or a plain dict containing at least a "__target__" key.

  • recursive_instantiate (bool) – If True, recursively instantiate all nested configs found in argument values. If False (default), nested configs are left as DictConfig objects.

  • **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:

Any

Example:

cfg = LazyConfig(torch.nn.LayerNorm)(normalized_shape=768)
norm = instantiate(cfg, eps=1e-5)   # override default eps