ranzen.hydra¶
Classes:
Helper for registering a group in hydra. |
|
Configuration option. |
|
Abstract class for orchestrating runs with |
|
Register hydra schemas. |
Functions:
Convert dataclass to a pretty dictionary. |
|
Takes a hydra config dict and makes it prettier for logging. |
|
Reconstruct the python command that was used to start this program. |
|
Check the given config and store everything in the ConfigStore. |
- class GroupRegistration(cs, *, group_name, package)¶
Bases:
object
Helper for registering a group in hydra.
- Parameters:
cs (ConfigStore)
group_name (str)
package (str)
- add_option(config_class, *, name)¶
Register a schema as an option for this group.
- Parameters:
config_class (type)
name (str)
- Return type:
None
- class Option(class_, name=None)¶
Bases:
Generic
[T
]Configuration option.
- Parameters:
class_ (type[T])
name (str | None)
- property name: str¶
Name of the option.
- class Relay¶
Bases:
object
Abstract class for orchestrating runs with
hydra
.This class does away with the hassle of needing to define config-stores, initialise config directories, and manually run neoconfigen on classes to convert them into valid schemas. Regular non-hydra compatible, classes can be passed to
with_hydra()
and neoconfigen will be run on them automatically (ifclear_cache=True
or a cached version of the schemas can’t be found), with the resulting schemas cached in the config directory.Subclasses must implement a
run()
method and will themselves be converted into the primary config classes used to initialise and validate hydra. With the launching of hydra, the subclasses will be instantiated and the run method called on the raw config.- Example:
>>> Relay.with_hydra( >>> root="conf", >>> model=[Option(MoCoV2), DINO], >>> datamodule=[Option(ColoredMNISTDataModule, "cmnist")], >>> )
- abstract run(raw_config=None)¶
Run the relay.
- Parameters:
raw_config (dict[str, Any] | None) – Dictionary containing the configuration used to instantiate the relay.
- Returns:
Any value that the experiment wishes to return.
- Return type:
Any
- classmethod with_hydra(root, *, clear_cache=False, instantiate_recursively=True, **options)¶
Run the relay with hydra.
- Parameters:
root (Path | str) – Root directory to look for the config directory in.
clear_cache (bool) – Whether to clear the cached schemas and generate the schemas anew with neoconfigen.
instantiate_recursively (bool) – Whether to recursively instantiate the relay instance.
**options (Option | type[T] | Sequence[type[T]] | Sequence[Option[T]] | Sequence[type[T] | Option[T]]) – Option or sequence of options (value) to register for each group (key). If an option is a type or is an
Option
withOption.name
asNone
, then a name will be generated based on the class name and used to register the option, else, the specified value forOption.name
will be used.
- Return type:
None
- class SchemaRegistration¶
Bases:
object
Register hydra schemas.
- Example:
>>> sr = SchemaRegistration() >>> sr.register(Config, path="experiment_schema") >>> sr.register(TrainerConf, path="trainer/trainer_schema") >>> >>> with sr.new_group("schema/data", target_path="data") as group: >>> group.add_option(CelebaDataConf, name="celeba") >>> group.add_option(WaterbirdsDataConf, name="waterbirds")
- new_group(group_name, *, target_path)¶
Return a context manager for a new group.
- Parameters:
group_name (str)
target_path (str)
- Return type:
Iterator[GroupRegistration]
- register(config_class, *, path)¶
Register a schema.
- Parameters:
config_class (type)
path (str)
- Return type:
None
- as_pretty_dict(data_class)¶
Convert dataclass to a pretty dictionary.
- Parameters:
data_class (DataclassInstance)
- Return type:
dict
- prepare_for_logging(hydra_config, *, enum_to_str=True)¶
Takes a hydra config dict and makes it prettier for logging.
Things this function does: turn enums to strings, resolve any references, mark entries with their type.
- Parameters:
hydra_config (DictConfig)
enum_to_str (bool)
- Return type:
dict[str, Any]
- reconstruct_cmd()¶
Reconstruct the python command that was used to start this program.
- Return type:
str
- register_hydra_config(main_cls, groups, schema_name='config_schema')¶
Check the given config and store everything in the ConfigStore.
This function performs two tasks: 1) make the necessary calls to ConfigStore and 2) run some checks over the given config and if there are problems, try to give a nice error message.
- Parameters:
main_cls (type) – The main config class; can be dataclass or attrs.
groups (dict[str, dict[str, type]]) – A dictionary that defines all the variants. The keys of top level of the dictionary should corresponds to the group names, and the keys in the nested dictionaries should correspond to the names of the options.
schema_name (str) – Name of the main schema. This name has to appear in the defaults list in the main config file.
- Raises:
ValueError – If the config is malformed in some way.
RuntimeError – If hydra itself is throwing an error.
- Example:
@dataclass class DataModule: root: Path = Path() @dataclass class LinearModel: dim: int = 256 @dataclass class CNNModel: kernel: int = 3 @dataclass class Config: dm: DataModule = dataclasses.field(default_factory=DataModule) model: Any groups = {"model": {"linear": LinearModel, "cnn": CNNModel}} register_hydra_config(Config, groups)
- Return type:
None