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.0.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.0-py3-none-any.whl (16.5 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for sws_config-0.1.0.tar.gz
Algorithm Hash digest
SHA256 9429e4215f86358f563a2e34a339b09da51ccf897a7a43464becaf4f57456809
MD5 c5c015293690979e6006564a6a2d2027
BLAKE2b-256 7f4329e4584227a89571797a47ebcc7b92d681dcecd8c589428874c4a96ed88e

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for sws_config-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 dd97d560bcf3d2902363e0ada1a94835b370ce8c6636168af7499bba1f53e01d
MD5 ba33e4d511a8bb54693ca5086d6cd0c3
BLAKE2b-256 0322f021e6f3fa983cb16b485b52d383cf8dbdb0ff430c7ab801f2a4180c6b8b

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