Level 0: Foundation
★☆☆☆☆ Introductory⏱ 5 minGeneral audience. No mathematics required.One program, many experiments
You have one simulation program and want to run it for Mars and the Moon, for a lander and a hopper, with three time steps and five seeds. Editing numbers inside the code each time is slow and error-prone. Instead, keep the settings in small, readable configuration files (“mars.yaml”, “lander.yaml”), choose which ones to combine when you start a run, and change any single value from the command line. A tool like Hydra assembles the final settings, checks them, runs the whole grid of experiments, and saves the exact settings next to each result.
$ python sim.py env=mars vehicle=lander sim.dt=0.005 seed=7 seed: 7 sim: dt: 0.005 duration: 60 output: dir: "runs/mars/lander/dt=0.005" env: name: "mars" gravity: 3.721 vehicle: name: "lander" mass: 1200 thrust: 8000
Trade studies, Monte Carlo campaigns and ML hyperparameter sweeps involve hundreds of runs. Knowing exactly which configuration produced each result (and being able to re-run it with one command) is the difference between a credible analysis and a pile of unexplained numbers.
- 1. Why keep settings out of the code?
- 2. A sweep runs…
- 3. Saving the final settings next to results lets you…
Level 1: Beginner
★★☆☆☆ Basic⏱ 25 minSecondary-school mathematics: arithmetic and elementary algebra.A Hydra project
conf/
config.yaml # defaults list + base values
env/mars.yaml env/moon.yaml
vehicle/lander.yaml vehicle/hopper.yaml
sim.py# conf/config.yaml
defaults:
- env: mars
- vehicle: lander
- _self_
seed: 0
sim: {dt: 0.01, duration: 60.0}
output_dir: runs/${env.name}/${vehicle.name}import hydra
from omegaconf import DictConfig
@hydra.main(config_path="conf", config_name="config", version_base=None)
def main(cfg: DictConfig):
print(cfg.env.gravity, cfg.sim.dt)
if __name__ == "__main__":
main()python sim.py env=moon sim.dt=0.005 picks the moon group option and overrides dt. Hydra writes the resolved config to outputs/<date>/<time>/.hydra/config.yaml.
- 1. `env=moon` on the command line…
- 2. ${env.name} in a value is…
- 3. Where does Hydra save the resolved config?
Level 2: Intermediate
★★★☆☆ Intermediate⏱ 35 minUndergraduate students and practising engineers.The override grammar
key=value: change an existing key (an error if it doesn’t exist, which catches typos).+key=value: add a new key.++key=value: add or override.~key: delete.group=option: choose a config group option.- Values are parsed YAML-style:
1is an int,1.0a float,truea bool,nullNone.
Structured configs
Register a dataclass schema (cs.store(name="sim_schema", node=SimConfig)) so OmegaConf type-checks values and rejects unknown keys at composition time. Add domain checks (0 < dt < duration) in __post_init__ or at program start.
Multirun
python sim.py -m env=mars,moon vehicle=lander,hopper sim.dt=0.01,0.005 launches the 8-run Cartesian product, locally or through launchers (joblib, SLURM via submitit), and sweeper plugins (Optuna, Ax) do smarter searches. Each run gets its own directory and saved config.
- 1. sim.dtt=0.001 (typo) with Hydra's default grammar…
- 2. -m a=1,2 b=x,y,z launches…
- 3. Structured configs add…
Level 3: Advanced
★★★★☆ Advanced⏱ 30 minGraduate level, with full derivations and proofs.Semantics of composition
Composition is a fold of deep merges in defaults-list order (later wins), then overrides in command-line order, then lazy interpolation. Interpolation forms a dependency graph; resolution is a depth-first traversal that must detect cycles (a: ${b}, b: ${a}). A value that is exactly one reference keeps its type (a float stays a float); references embedded in strings are stringified.
Identity of a run
Two override lists can denote the same run (dt=0.01 vs dt=1e-2, duplicated seeds). Hashing the resolved config (canonical JSON with sorted keys) gives a run identity that is insensitive to spelling: the basis for caching completed runs and for detecting accidental duplicates in a sweep.
Typos are the silent killer
With a free-form dict config, sim.duraton=600 creates a new, unused key and the run proceeds with the default duration: no error, wrong result. Schemas (unknown-key errors, type checks, range checks) convert these into immediate failures.
- 1. Which resolution needs cycle detection?
- 2. Why hash the resolved config rather than the override string?
- 3. 'x${a}' with a = 1 resolves to…
Level 4: Expert
★★★★★ Expert⏱ 30 minResearchers: original papers and open problems.Configuration, measured
Exercise 1. The default composition gives Mars gravity 3.721, a 1200 kg lander and output dir runs/mars/lander. env=moon vehicle=hopper sim.dt=0.005 seed=7 gives runs/moon/hopper with int seed 7. sim.dtt=0.1 is rejected, +sim.integrator=rk4 adds, ~env.rho0 deletes, 1e3 parses as a float, and a two-key interpolation cycle is detected.
Exercise 2. The sweep env × vehicle × dt yields 8 runs and 8 distinct configs. A careless sweep sim.dt=0.01,1e-2,0.010 seed=0,0 launches 6 runs but only 1 distinct resolved config: hashing saves 5 redundant runs. Of six overrides applied without key checking, the four bad ones (sim.dtt, sim.duraton, dt=-0.01, dt=fast) all compose “successfully”. A dataclass schema with range checks rejects exactly those four and accepts the two valid ones.
Configuration in AI, robotics and aerospace
- Trade studies and Monte Carlo campaigns as Hydra multiruns on clusters (SLURM launcher).
- ML training (Lightning + Hydra) with configs for data, model, optimizer and trainer.
- Robot deployment parameters (ROS 2 parameter files) validated against schemas.
- Provenance: saved resolved configs plus run hashes for every figure and model.
References
- O. Yadan (2019). “Hydra: a framework for elegantly configuring complex applications.” GitHub (facebookresearch/hydra); documentation at hydra.cc.
- OmegaConf documentation (omegaconf.readthedocs.io): interpolation, structured configs.
- Hydra documentation: Override grammar, Multi-run, Structured configs.
- Pydantic documentation (docs.pydantic.dev) for runtime validation.
- ROS 2 documentation: Parameters.