Hydra Configuration for Examples
Many examples within the Reward Kit, particularly those involving local evaluations (e.g.,examples/math_example/local_eval.py or CLI-driven runs like reward-kit run ...) and TRL integrations (e.g., examples/math_example/trl_grpo_integration.py), leverage Hydra for flexible and powerful configuration management. This guide explains how Hydra is used and how you can interact with it.
Why Hydra?
Hydra allows for:- Structured Configuration: Define configurations in YAML files, promoting clarity and organization.
- Composition: Combine and override configuration files easily.
- Command-Line Overrides: Change any configuration parameter directly from the command line without modifying code.
- Dynamic Output Directories: Automatically create organized, timestamped output directories for each run.
Typical Configuration Structure in Examples
An example using Hydra will typically have aconf/ subdirectory:
- Main Config File: This is the entry point for Hydra (e.g.,
run_math_eval.yamlforreward-kit run, ortrl_grpo_config.yamlfor a TRL script). It often includes defaults and references other configuration files or groups. - Dataset Configs: As detailed in the Dataset Configuration Guide, these YAML files define how data is loaded and processed.
- Other Configs: You might find separate YAML files for model parameters, training arguments, or other components, which are then composed into the main config.
Running Examples with Hydra
-
Activate Your Virtual Environment:
-
Navigate to the Repository Root: Most Hydra-based scripts are designed to be run from the root of the
reward-kitrepository. -
Execute the Script:
- For
reward-kit run(CLI-driven evaluation): Thereward-kit runcommand itself is integrated with Hydra. You specify the path to the configuration directory and the name of the main configuration file. - For Python scripts using Hydra directly (e.g., TRL integration):
Hydra automatically discovers the
conf/directory relative to the script if structured correctly, or uses the--config-pathand--config-namearguments if the script is designed to accept them likereward-kit run.
- For
Overriding Configuration Parameters
This is one of Hydra’s most powerful features. You can override any parameter defined in the YAML configuration files directly from the command line using akey=value syntax.
-
Simple Override:
-
Nested Parameter Override: Use dot notation to access nested parameters.
-
Changing Dataset:
If your main config allows choosing different dataset configurations (often via a
defaultslist or a parameter likedataset_config_name):(The exact way to switch datasets depends on how themain_config.yamlis structured, often using config groups.) -
Multiple Overrides:
Output Directory Management
Hydra automatically manages output directories for each run.- Default Location: By default, outputs (logs, results, saved models, etc.) are saved to a timestamped directory structure like
outputs/YYYY-MM-DD/HH-MM-SS/(relative to where the command is run, typically the repository root). - Configuration: The base output path can often be configured within the YAML files (e.g., via
hydra.run.diror a custom output directory parameter in the main config). - Contents: Inside the run-specific output directory, you’ll typically find:
- A
.hydra/subdirectory containing the complete configuration used for that run (including overrides), which is excellent for reproducibility. - Log files.
- Result files (e.g.,
*.jsonlfiles with evaluation scores or generated outputs).
- A
Key Takeaways
- Look for a
conf/directory within an example to understand its Hydra setup. - Use command-line overrides for quick experiments without changing YAML files.
- Check the
.hydra/directory in your outputs to see the exact configuration used for any given run.