Skip to main content

The Zen way to configure your Python projects, deep learning and others

Project description

ZenCFG

A Zen way to configure your Python packages.

Why ZenCFG

ZenCFG (for Zen ConFiGuration), is the result of many iterations of trying pretty much every existing approach to Python configuration management systems and being satisfied by none of them.

The key building principles of ZenCFG are:

  • Pure Python code
  • Minimal amound of code
  • Minimally intrusive
  • Intuitive

In practice, this means that you can define your configuration as Python classes (like dataclasses). You can specify the type of the parameters, and we use Pydantic to verify them. You can trivially override any of the parameters through the command-line, with an intuitive interface.

It was built originally to configure and manage scripts for Deep Learning experiments, but you can use it for any Python project. The examples I use are Deep Learning inspired.

Install

Just clone the repository and install it, here in editable mode:

git clone https://github.com/JeanKossaifi/zencfg
cd zencfg
python -m pip install -e .

Defining configurations

There are two main type of configurations: core configuration categories, and subcategories.

Core configurations categories

Core categories are defined by inheriting directly from ConfigBase:

# We define a Model core config
class ModelConfig(ConfigBase):
    version: str = "0.1.0"

# Another base class: optimizer configurations
class OptimizerConfig(ConfigBase):
    lr: float = 0.001

SubCategories

Now that you have core categories, you can optionally instantiate this as subcategories. For instance, the different type of models you have, optimizers, etc.

To do this, simply inherit from your core category:

class DiT(ModelConfig):
    layers: Union[int, List[int]] = 16

class Unet(ModelConfig):
    conv: str = "DISCO"

# Nested config.
class CompositeModel(ModelConfig):
    submodel: ModelConfig
    num_heads: int = 4

class AdamW(OptimizerConfig):
    weight_decay: float = 0.01

Composing categories

You can have configuration objects as parameters in your config: for instance, our main configuration will contain a model and an optimizer.

# Our main config is also a core category, and encapsulates a model and an optimizer
class Config(ConfigBase):
    model: ModelConfig
    opt: OptimizerConfig = OptimizerConfig(_name='adamw')

Note the _name="adamw": this indicates that the default will be the AdamW class. You can create a subcategory by passing to the main category class the class name of the subcategory you want to create, through _name.

The above is equivalent to explicitly creating an ADAMW optimizer:

class Config(ConfigBase):
    model: ModelConfig
    opt: OptimizerConfig = AdamW(_name='adamw')

Instantiating configurations

Your configurations are Python object: you can instantiate them:

config = Config(model = ModelConfig(name='DIT', layers=24))

Instantiating a configuration with optional values from the command line

The library also lets you override parameters from the configuration through the command line, using cfg_from_flat_dict.

For instance, you can create a script main.py containing:

from zencfg import cfg_from_flat_dict

from YOUR_CONFIG_FILE import Config

config = cfg_from_flat_dict(Config, strict=True)

You can then call your script via the command line. In that case, we simply use . to indicate nesting.

For instance, to instantiate the same config as above, you could simply do:

python main.py --model._name DIT --model.layers 24

Configuration to dictionary

You can directly get a Python dictionary from a configuration instance, simply use to_dict:

config_dict = config.to_dict()

model_cfg = config_dict['model']

# You can access values as attributes too
optimizer_cfg = config_dict.opt

Questions or issues

This is very much a project in development that I wrote for myself and decided to share so others could easily reuse it for multiple projects, while knowing it is tested and actively developed!

If you have any questions or find any bugs, please open an issue, or better yet, a pull-request!

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

zencfg-0.1.0.tar.gz (12.4 kB view details)

Uploaded Source

Built Distribution

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

zencfg-0.1.0-py3-none-any.whl (13.4 kB view details)

Uploaded Python 3

File details

Details for the file zencfg-0.1.0.tar.gz.

File metadata

  • Download URL: zencfg-0.1.0.tar.gz
  • Upload date:
  • Size: 12.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.8

File hashes

Hashes for zencfg-0.1.0.tar.gz
Algorithm Hash digest
SHA256 8966e29d2de20b12a3abafcf4d63929090feffdbe1f3edfa48da1831820f9fdf
MD5 bd089547ea16d70a1a2de1a1adfc9e25
BLAKE2b-256 2a3165722fc07ef921ea67aea763118698ab21aae476a83c976f1665e76beb6b

See more details on using hashes here.

Provenance

The following attestation bundles were made for zencfg-0.1.0.tar.gz:

Publisher: deploy_pypi.yml on JeanKossaifi/zencfg

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file zencfg-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: zencfg-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 13.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.8

File hashes

Hashes for zencfg-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 e9e3e88e2695337f5ad356351bfb3127338cde2914382441883883860179ab58
MD5 4238fb6118cf07cdf79a9f3231973ac0
BLAKE2b-256 830ff86194bb699076b35da81aee46a08c30558a860fb6fdb158ca7a93992702

See more details on using hashes here.

Provenance

The following attestation bundles were made for zencfg-0.1.0-py3-none-any.whl:

Publisher: deploy_pypi.yml on JeanKossaifi/zencfg

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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