Yet Another Configuration System (YACS)

0.1.8 · active · verified Sat Apr 11

YACS is a lightweight Python library for defining and managing system configurations, particularly useful in scientific experimentation for reproducibility. It provides a hierarchical CfgNode object for structured configuration and supports loading configurations from both Python files and YAML files. The current version is 0.1.8, with a relatively active release cadence.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to define a default configuration using `CfgNode`, retrieve a mutable copy, and then merge overrides from a YAML file and a list of command-line style options. It also shows the recommended practice of freezing the configuration to ensure reproducibility.

from yacs.config import CfgNode as CN
import os

# 1. Define your default configuration in a Python file (e.g., config.py)
_C = CN()

_C.SYSTEM = CN()
_C.SYSTEM.NUM_GPUS = 8
_C.SYSTEM.NUM_WORKERS = 4

_C.TRAIN = CN()
_C.TRAIN.HYPERPARAMETER_1 = 0.1
_C.TRAIN.SCALES = (2, 4, 8, 16)

def get_cfg_defaults():
    """Get a yacs CfgNode object with default values."""
    return _C.clone()

# 2. Example usage in your main application
# Simulate an experiment.yaml file
# This content would typically be in a separate file
# with only the overrides.
experiment_yaml_content = """
SYSTEM:
  NUM_GPUS: 2
TRAIN:
  SCALES: (1, 2, 3, 4)
"""

# Create a dummy experiment.yaml for demonstration
with open("experiment.yaml", "w") as f:
    f.write(experiment_yaml_content)


if __name__ == "__main__":
    cfg = get_cfg_defaults()

    # Merge from an experiment-specific YAML file
    cfg.merge_from_file("experiment.yaml")

    # Override from a list of key-value pairs (e.g., from command line)
    opts = ["SYSTEM.NUM_GPUS", 1, "TRAIN.HYPERPARAMETER_1", 0.5]
    cfg.merge_from_list(opts)

    # Freeze the configuration to prevent further modification
    cfg.freeze()

    print("Final Configuration:")
    print(cfg)
    print(f"Number of GPUs: {cfg.SYSTEM.NUM_GPUS}")
    print(f"Training Hyperparameter 1: {cfg.TRAIN.HYPERPARAMETER_1}")
    print(f"Training Scales: {cfg.TRAIN.SCALES}")

    # Clean up dummy file
    os.remove("experiment.yaml")

view raw JSON →