Skip to content

Hydra utils

Utility functions related to working with Hydra.

get_attr #

get_attr(obj: Any, *attributes: str)

Recursive version of getattr when the attribute is like 'a.b.c'.

register_instance_attr_resolver #

register_instance_attr_resolver(
    instantiated_objects_cache: dict[str, Any]
) -> None

Registers the instance_attr custom resolver with OmegaConf.

resolve_dictconfig #

resolve_dictconfig(dict_config: DictConfig) -> Config

Resolve all interpolations in the DictConfig.

Returns a Config object, which is a simple dataclass used to give nicer type hints for the contents of an experiment config.

instance_attr #

instance_attr(
    *attributes: str,
    _instantiated_objects_cache: (
        MutableMapping[str, Any] | None
    ) = None
)

Allows interpolations of the instantiated objects attributes (rather than configs).

This is very hacky

This is quite hacky and very dependent on the code of Hydra / OmegaConf not changing too much in the future. For this reason, consider pinning the versions of these libraries in your project if you intend do use this feature.

This works during a call to hydra.utils.instantiate, by looking at the stack trace to find the instantiated objects, which are in a variable in that function.

If there is a ${instance_attr:datamodule.num_classes} interpolation in a config, this will:

  1. instantiate the datamodule config
  2. store it at the key 'datamodule' in the instantiated objects cache dict (if passed).

    (This is useful since it makes it possible for us to later reuse this instantiated datamodule instead of re-instantiating it.)

  3. Retrieve the value of the attribute (getattr(datamodule, 'num_classes')) and return it.

make_config_and_store #

make_config_and_store(
    target: Callable[..., Target],
    *,
    store: ZenStore,
    **overrides
)

Creates a config dataclass for the given target and stores it in the config store.

This uses hydra_zen.builds to create the config dataclass and stores it at the name config_name, or target.__name__.