Skip to content

Instantly share code, notes, and snippets.

@razhangwei
Last active December 18, 2024 23:38
Show Gist options
  • Save razhangwei/09770282c8018fb75643b7691d25ad99 to your computer and use it in GitHub Desktop.
Save razhangwei/09770282c8018fb75643b7691d25ad99 to your computer and use it in GitHub Desktop.
hydra cheatsheet

Basic Structure

Hydra uses YAML-like syntax for configuration files, typically with a .yaml extension.

key: value
nested:
  key: value
list:
  - item1
  - item2

Key Features

  1. Composition: Combine multiple config files
  2. Command-line overrides: Override config values from CLI
  3. Config groups: Organize related configs
  4. Interpolation: Reference other config values

Configuration File

defaults: # reserved keyword
  - base_config
  - environment: production
  - _self_

database:
  host: localhost
  port: 5432

app:
  _target_: my_package.App
  name: MyApp
  version: 1.0
  • _target_: target can be both class and function.

Python Usage

import hydra
from omegaconf import DictConfig

@hydra.main(version_base=None, config_path="conf", config_name="config")
def my_app(cfg: DictConfig) -> None:
    print(cfg.database.host)
    print(cfg.app.name)

if __name__ == "__main__":
    my_app()

Command-line Overrides

python my_app.py database.host=127.0.0.1 app.name=NewName

Config Groups

Organize related configs in directories:

conf/
  config.yaml
  database/
    mysql.yaml
    postgresql.yaml
  environment/
    development.yaml
    production.yaml

Interpolation

Reference other config values:

app:
  name: MyApp
  log_file: ${app.name}.log

Multirun

Run with multiple configurations:

python my_app.py --multirun database=mysql,postgresql

Advanced Features

  • Sweeping: Systematically explore config variations
  • Plugins: Extend Hydra's functionality
  • Custom resolvers: Define custom interpolation functions

Remember, Hydra is highly flexible and offers many more features for complex configuration management in Python applications[1][3][4].

Citations: [1] https://hydra.cc/docs/intro/ [2] https://python.plainenglish.io/managing-configuration-in-python-with-hydra-a-beginners-guide-cedebf9533ca?gi=ea764b7c7752 [3] https://hydra.cc/docs/tutorials/basic/your_first_app/config_file/ [4] https://hydra.cc/docs/configure_hydra/intro/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment