[P] configgle: Hierarchical configuration using dataclasses factories

I’ve been working on (yet another…) library for managing ML experiment configs and wanted to share it. This project is intended for production ML research and development, though might be useful elsewhere.

The basic idea is that a config is composed of nested dataclasses. Each nesting is defined in the class it configures and doubles as a factory. This keeps params “close” to their point of use and makes for more readable code.

from configgle import Fig, Makes class Model: class Config(Fig["Model"]): hidden_size: int = 256 num_layers: int = 4 def __init__(self, config: Config): self.config = config cfg = Model.Config() cfg.hidden_size = 512 model = cfg.make() 

Alternatively there is also aconfiggle.autofig decorator to auto-generate the Config from __init__.

The factory method make is built for you and automatically handles inheritance so you can also do:

class OtherModel: class Config(Makes["OtherModel"], Model.Config): hidden_size: int = 12 other_thing: float = 3.14 def __init__(self, config: Config): self.config = config other_model = OtherModel.Config().make() 

A key feature of this design is that although makeis auto-populated we still retain type tracking for both the Config and the class it makes. (And if pyright/ty/mypy etc eventually support Intersection then you won’t needFig["Model"]nor Makes and can just use Fig.)

Why another config library? There are great options out there (Hydra, Fiddle, gin-config, Sacred, Confugue, etc.), but they either focus more on YAML or wrapper objects and have various issues when it comes to typing. The goal here was a UX that’s just simple Python–standard dataclasses, hierarchical, and class-local. No external files, no new syntax to learn. In fact the provided Dataclassclass is just for brevity–you can still use dataclasses.dataclass decorators.

Learn more: https://pypi.org/project/configgle/

submitted by /u/Legal-Pop-1330
[link] [comments]

Liked Liked