Skip to main content

Slayer of Hydra

Project description

Heracls - Slayer of Hydra

heracls is a tiny utility package to instantiate typed dataclasses from flexible config sources (dictionary, OmegaConf, YAML, dotlist, CLI, ...). It is designed for projects that want strict, typed config objects while supporting the dynamic overrides commonly used in scripts and experiments.

Installation

The heracls package is available on PyPi and can be installed with pip.

pip install heracls

Alternatively, if you need the latest features, you can install it from source.

pip install git+https://github.com/francois-rozet/heracls

Getting started

The following example demonstrates how to declare a nested dataclass config, instantiate it from command line arguments, serialize it to YAML, and use it in a script. The heracls.ArgumentParser parser infers arguments from the dataclass structure and types.

from dataclasses import dataclass, field
from typing import Literal

import heracls

@dataclass
class ModelConfig:
    name: str = "mlp"
    depth: int = 3
    width: int = 256
    dropout: float | None = None

@dataclass
class AdamConfig:
    name: Literal["adam"] = "adam"
    betas: tuple[float, float] = (0.95, 0.95)
    learning_rate: float = 1e-3
    weight_decay: float = 0.0

@dataclass
class SGDConfig:
    name: Literal["sgd"] = "sgd"
    momentum: float = 0.0
    learning_rate: float = 1e-3
    weight_decay: float = 0.0
    nesterov: bool = False

@dataclass
class TrainConfig:
    model: ModelConfig = field(default_factory=ModelConfig)
    optimizer: AdamConfig | SGDConfig = heracls.choice(
        {"adam": AdamConfig, "sgd": SGDConfig},
        default="adam",
    )
    dataset: str = "mnist"
    data_splits: tuple[float, ...] = (0.8, 0.1)
    n_epochs: int = 1024
    n_steps_per_epoch: int = 256
    tasks: list[str] = field(default_factory=list)

def main() -> None:
    parser = heracls.ArgumentParser()
    parser.add_argument("--dry", action="store_true", help="dry run")
    parser.add_arguments(TrainConfig, dest="train", root=True)

    args = parser.parse_args()

    print(heracls.to_yaml(args.train))

    if args.dry:
        return

    trainset, validset, testset = load_dataset(args.train.data_splits)
    model = init_model(args.train.model)

    for epoch in range(args.train.n_epochs):
        ...

if __name__ == "__main__":
    main()
$ python examples/train.py --dry --model.dropout 0.1 --optimizer sgd --data_splits 0.7 0.2
model:
  name: mlp
  depth: 3
  width: 256
  dropout: 0.1
optimizer:
  name: sgd
  momentum: 0.0
  learning_rate: 0.001
  weight_decay: 0.0
  nesterov: false
dataset: mnist
data_splits:
- 0.7
- 0.2
n_epochs: 1024
n_steps_per_epoch: 256
tasks: []
$ python examples/train.py --help
usage: train.py [-h] [--dry] [--model.name str] [--model.depth int] [--model.width int]
                [--model.dropout float] [--optimizer {adam,sgd}] [--dataset str]
                [--data_splits float [float ...]] [--n_epochs int] [--n_steps_per_epoch int]
                [--tasks [str ...]] [--optimizer.name {adam}] [--optimizer.betas float float]
                [--optimizer.learning_rate float] [--optimizer.weight_decay float]

options:
  -h, --help                       show this help message and exit
  --dry                            dry run

  --optimizer {adam,sgd}           (default: adam)
  --dataset str                    (default: mnist)
  --data_splits float [float ...]  (default: (0.8, 0.1))
  --n_epochs int                   (default: 1024)
  --n_steps_per_epoch int          (default: 256)
  --tasks [str ...]                (default: [])

model:
  --model.name str                 (default: mlp)
  --model.depth int                (default: 3)
  --model.width int                (default: 256)
  --model.dropout float            (default: None)

optimizer:
  --optimizer.name {adam}          (default: adam)
  --optimizer.betas float float    (default: (0.95, 0.95))
  --optimizer.learning_rate float  (default: 0.001)
  --optimizer.weight_decay float   (default: 0.0)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

heracls-0.5.0.tar.gz (9.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

heracls-0.5.0-py3-none-any.whl (8.6 kB view details)

Uploaded Python 3

File details

Details for the file heracls-0.5.0.tar.gz.

File metadata

  • Download URL: heracls-0.5.0.tar.gz
  • Upload date:
  • Size: 9.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.22 {"installer":{"name":"uv","version":"0.9.22","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for heracls-0.5.0.tar.gz
Algorithm Hash digest
SHA256 2a6b961d3c12bd37a005a07cf30b15682964dd3cb6a8c21cccd37e25516af4e5
MD5 193ce5a149dd2feec7f08870ca198d62
BLAKE2b-256 146b44ce6b6e0bf1114807356fe9cd866ef19ecc2e0b5d7650ad318d69a0b07f

See more details on using hashes here.

File details

Details for the file heracls-0.5.0-py3-none-any.whl.

File metadata

  • Download URL: heracls-0.5.0-py3-none-any.whl
  • Upload date:
  • Size: 8.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.22 {"installer":{"name":"uv","version":"0.9.22","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for heracls-0.5.0-py3-none-any.whl
Algorithm Hash digest
SHA256 e359299e1e7866dbc4597df99d4d7e129d302c255275ef7d1f3ea190b843a1b5
MD5 e68ca112c11d17cebf90139d1ef52dfd
BLAKE2b-256 5ab5c9da9a45ef30746eb4246f930749316d6b8668921e583ddb491d9227bb0e

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page