Skip to main content

Minimal pythonic config library for deep learning experiments

Project description

sws

Minimal, predictable, footgun-free configuration for deep learning experiments.

The remainder of this readme follows the CODE THEN EXPLAIN layout. Install instructions at the end.

Basics

from sws import Config, lazy

# Create the config and populate the fields with defaults
c = Config()
c.lr = 3e-4
c.wd = c.lr * 0.1  # ERROR, c is write-only. Instead, do:
c.wd = lazy(lambda c: c.lr * 0.1)

# Alternative convenience for short configs:
c = Config(lr=3e-4, wd=lazy(lambda c: c.lr * 0.1))

# Finalizing resolves all fields to plain values, and integrates CLI args:
c = c.finalize(argv=sys.argv[1:])
assert c.lr == 3e-4 and c.wd == 3e-5

train_agi(lr=c.lr, wd=c.wd)

Sws clearly separates two phases: config creation, and config use. At creation time, you build a (possibly nested) Config object, with the option to use lazy(...) to make some field's value depend on the values of other fields. Then, you finalize() the config, which resolves all fields (including lazy ones) into plain concrete values. If desired, finalize() also applies overrides from the commandline.

To avoid subtle bugs common in many config libraries I've used before, at creation time, the Config object is write-only; this forces use of lazy references that get correctly resolved later. At use-time (after finalize()), on the other hand, the FinalConfig object is read-only, which again avoids subtle bugs in complex code.

Nesting

TODO: Continue the doc rewrite from here!

Nested writes and reading after finalize

c = sws.Config()
c.model = {"width": 128}
c.model.depth = 4

f = c.finalize()
assert f.model.width == 128 and f.model.depth == 4

CLI-style overrides (left-to-right) and expression access to c

import sys

base = sws.Config(lr=1.0, model={"width": 128}, foo=0, bar=0)
f = base.finalize(sys.argv[1:])  # e.g.: lr=3 foo=c.lr bar=c.model.width

# Or pass an explicit list:
f = base.finalize(["lr", "3", "foo", "c.lr", "bar", "c.model.width"])  # foo=1.0, then lr=3, then bar=3
assert f.foo == 1.0 and f.lr == 3 and f.bar == 3

Containers and lazy inside containers

c = sws.Config(
    a=2,
    t=(1, lazy(lambda c: c.a + 1)),
    s={1, lazy(lambda c: c.a)},
)
f = c.finalize()
assert f.t == (1, 3) and f.s == frozenset({1, 2})

Cycles and errors

import pytest

c = sws.Config(a=lazy(lambda c: c.b), b=lazy(lambda c: c.a))
wic pytest.raises(sws.CycleError):
    c.finalize()

Install

pip install sws-config

Test

python -m pytest

Authors / License

  • Authors: see pyproject.toml.
  • License: MIT © Lucas Beyer. See LICENSE for details.

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

sws_config-0.1.4.tar.gz (17.9 kB view details)

Uploaded Source

Built Distribution

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

sws_config-0.1.4-py3-none-any.whl (16.5 kB view details)

Uploaded Python 3

File details

Details for the file sws_config-0.1.4.tar.gz.

File metadata

  • Download URL: sws_config-0.1.4.tar.gz
  • Upload date:
  • Size: 17.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for sws_config-0.1.4.tar.gz
Algorithm Hash digest
SHA256 e470659dca864bcb4620ec7076aad79ddd7ce5a12090dcbca503e0eae20e671f
MD5 ab56b1c8ba0dbd25adfd4649635f8143
BLAKE2b-256 20f2c10600491d923f73fa9e5e9e0328599342926eec4d45b2cd920dc7c80d0a

See more details on using hashes here.

Provenance

The following attestation bundles were made for sws_config-0.1.4.tar.gz:

Publisher: publish.yml on lucasb-eyer/sws

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

File details

Details for the file sws_config-0.1.4-py3-none-any.whl.

File metadata

  • Download URL: sws_config-0.1.4-py3-none-any.whl
  • Upload date:
  • Size: 16.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for sws_config-0.1.4-py3-none-any.whl
Algorithm Hash digest
SHA256 0519b50e836e08635ee293bdef01ab9272758a6baae4c4d2701151f5b8c86974
MD5 c6dfa9878651fd313092dbe54148aca6
BLAKE2b-256 007f89e2ab103d96e77dcdf1a7da9ec97c2f5725f32501d32c108123af48ef0d

See more details on using hashes here.

Provenance

The following attestation bundles were made for sws_config-0.1.4-py3-none-any.whl:

Publisher: publish.yml on lucasb-eyer/sws

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