Skip to main content

Hackable hyperparameter configuration with decorators.

Project description

Ornamentalist

Ornamentalist is a tiny library for configuring functions with fixed hyperparameters in Python. The goal is to allow research code to be more flexible and hackable, without losing readability.

The core thing ornamentalist does is it allows you to specify the parameters of a function as Configurable. You can then use the ornamentalist.configure() decorator to replace the function with a partial version of itself. The new partial function has all configurable parameters fixed to values supplied by you at the start of the program. This pattern allows you to avoid the work of plumbing hyperparameters around your code, without resorting to global variables or config God-objects.

You can use ornamentalist alongside your favourite configuration libraries like argparse or hydra. We also provide the optional ornamentalist.cli() feature, which automatically generates a CLI for your program.

I encourage you to read the short blog post to better understand the motivation behind this libary and why I think ornamentalist is a good solution. For worked examples of how to use ornamentalist with other tools such as hydra, argparse, or submitit, check out the examples/ directory.

You can install ornamentalist with pip:

pip install ornamentalist

Ornamentalist is only 1-file, so feel free to copy-paste it into your projects if you prefer.

Usage

Using ornamentalist is straightforward:

  1. Mark hyperparameters as configurable by setting their default value to ornamentalist.Configurable.
  2. Decorate the function with @ornamentalist.configure().
  3. Create a config dictionary at the start of your program (either with ornamentalist.cli() or your favourite configuration tool).
  4. Call ornamentalist.setup(config) before running any configurable functions.

Quickstart

Tip: You can find this file in examples/basics.py. Download and play with it to get a feel for ornamentalist :).

import ornamentalist
from ornamentalist import Configurable


# basic usage of ornamentalist...
# setting verbose=True is useful for debugging
@ornamentalist.configure(verbose=True)
def add_n(x: int, n: int = Configurable):
    print(x + n)


# by default, ornamentalist looks for parameters
# in CONFIG_DICT[func.__name__],
# you can override this with a custom key like so
@ornamentalist.configure(name="greeting_config")
def greet(name: str = Configurable):
    print(f"Hello, {name}")


# you can even use ornamentalist on classes!
class MyClass:
    # you probably want to give constructors custom
    # names, else they will just be "__init__"
    @ornamentalist.configure(name="myclass.init")
    def __init__(self, a: float = Configurable):
        print(a)


if __name__ == "__main__":
    # you can manually supply config with argparse, hydra etc.
    # we also provide ornamentalist.cli() to automatically
    # generate a basic CLI.
    # But we will hardcode it for this example...
    config = {
        "add_n": {"n": 5},
        # greeting_config and myclass_init are the
        # custom names we specified earlier
        "greeting_config": {"name": "Alice"},
        "myclass.init": {"a": 4.5},
    }
    ornamentalist.setup(config)

    add_n(10)
    greet()
    MyClass()

    # you can access the config dict anywhere in your program
    # through `ornamentalist.get_config()`
    assert ornamentalist.get_config() == config

Examples

Ornamentalist is a simple library! You can learn the whole thing by reading through these examples:

  • examples/basics.py teaches you the basic usage of ornamentalist (as seen above).
  • examples/submitit_basic.py shows you a simple pattern for launching ornamentalist jobs with submitit.
  • examples/cli.py demonstrates how to use the automatic CLI generation feature (with example outputs).
  • examples/diffusion_transformer is an example of a full research codebase using ornamentalist.

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

ornamentalist-2.2.0.tar.gz (6.6 kB view details)

Uploaded Source

Built Distribution

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

ornamentalist-2.2.0-py3-none-any.whl (7.4 kB view details)

Uploaded Python 3

File details

Details for the file ornamentalist-2.2.0.tar.gz.

File metadata

  • Download URL: ornamentalist-2.2.0.tar.gz
  • Upload date:
  • Size: 6.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.7.2

File hashes

Hashes for ornamentalist-2.2.0.tar.gz
Algorithm Hash digest
SHA256 6a42debd32461fd354df75920c22f0dcbc1888e4d1a66dddf74b3d6a584bbe65
MD5 70c72a2e242083db84f1498428387cae
BLAKE2b-256 7fc448773de9a8fb88d5c635274aa4f73db4a3ac1056dae93b1251837ad68726

See more details on using hashes here.

File details

Details for the file ornamentalist-2.2.0-py3-none-any.whl.

File metadata

File hashes

Hashes for ornamentalist-2.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 0582ee829503a312ca79e89247124891080ed18b51f91cc338e35b6baeeb3ef8
MD5 4cd321bc1e18409cf90a697681195401
BLAKE2b-256 3e9875f8eb76a8f878e25e79116e70b5f9274665858b7bcf2c654cc64a0b6a1f

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