Foundations to Frontier
Tests
C. Programming, Tools and HPC › C2 Software Engineering for Research

Configuration Management with Hydra

Research code runs in many variants: different environments, vehicles, models, hyperparameters, seeds. Hard-coded constants and ad-hoc argparse flags don't scale. Hydra (built on OmegaConf) composes a run's configuration from a base file plus selected options from config groups and command-line overrides, resolves ${...} interpolations, validates against structured (dataclass) schemas, launches sweeps (multirun), and saves the resolved config with each run's outputs. In the exercises you implement this machinery (deep merge, group selection, override grammar, typed interpolation with cycle detection), then expand sweeps, deduplicate runs by hashing resolved configs, and measure how many silent typos a schema catches.

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.

Compose a run configuration from groups and overrides
env: vehicle:
try:
$ 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
✅ run directory runs/mars/lander/dt=0.005
Configuration lives in small composable files: one per environment, one per vehicle, plus a base. A run is "base + chosen options + overrides", and the fully resolved config is saved next to the results. Rejecting unknown keys (a typo like sim.dtt would otherwise silently do nothing) and validating against a schema turn silent mistakes into immediate errors.
Why engineers care

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.

📝 Quick check (Level 0)
0/3 answered
  1. 1. Why keep settings out of the code?
  2. 2. A sweep runs…
  3. 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.

📝 Quick check (Level 1)
0/3 answered
  1. 1. `env=moon` on the command line…
  2. 2. ${env.name} in a value is…
  3. 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: 1 is an int, 1.0 a float, true a bool, null None.

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.

🐍 Exercise 1: compose configurations like Hydra
Implement a deep merge, YAML-ish value parsing, config-group selection ('env=moon'), the override grammar ('a.b=v' must exist, '+a.b=v' adds, '~a.b' deletes) and recursive ${a.b} interpolation (typed when the whole value is one reference, with cycle detection).
📝 Quick check (Level 2)
0/3 answered
  1. 1. sim.dtt=0.001 (typo) with Hydra's default grammar…
  2. 2. -m a=1,2 b=x,y,z launches…
  3. 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.

📝 Quick check (Level 3)
0/3 answered
  1. 1. Which resolution needs cycle detection?
  2. 2. Why hash the resolved config rather than the override string?
  3. 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.

🐍 Exercise 2 (research): sweeps, duplicate runs and silent typos
Expand multirun specs into the Cartesian product of overrides, compose each run, deduplicate by hashing the resolved config, and quantify how many bad overrides a schema-less config silently accepts versus what a dataclass schema with range checks catches.

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

  1. O. Yadan (2019). “Hydra: a framework for elegantly configuring complex applications.” GitHub (facebookresearch/hydra); documentation at hydra.cc.
  2. OmegaConf documentation (omegaconf.readthedocs.io): interpolation, structured configs.
  3. Hydra documentation: Override grammar, Multi-run, Structured configs.
  4. Pydantic documentation (docs.pydantic.dev) for runtime validation.
  5. ROS 2 documentation: Parameters.