Skip to main content

A hyper-parameter library for researchers, data scientists and machine learning engineers.

Project description

Hyperparameter

ENGLISH | 中文文档

Hyperparameter, Make configurable AI applications. Build for Python/Rust hackers.

Hyperparameter is a versatile library designed to streamline the management and control of hyperparameters in machine learning algorithms and system development. Tailored for AI researchers and Machine Learning Systems (MLSYS) developers, Hyperparameter offers a unified solution with a focus on ease of use in Python, high-performance access in Rust and C++, and a set of macros for seamless hyperparameter management.

Key Features

For Python Users

  • Pythonic Syntax: Define hyperparameters using keyword argument syntax;

  • Intuitive Scoping: Control parameter scope through with statement;

  • Configuration File: Easy to load parameters from config files;

For Rust and C++ Users

  • High-Performance Backend: Hyperparameter is implemented in Rust, providing a robust and high-performance backend for hyperparameter management. Access hyperparameters in Rust and C++ with minimal overhead, making it ideal for ML and system developers who prioritize performance.

  • Macro-Based Parameter Management: Hyperparameter provides a set of macros for both Rust and C++ users. These macros mimic Python's with statements and adhere to language-specific scoping rules.

  • Compile-Time Hashing: Both Rust and C++ interfaces utilize compile-time hashing of hyperparameter names, reducing runtime hash computation overhead.

Quick Start

Installation

pip install hyperparameter

Python

from hyperparameter import auto_param, param_scope

@auto_param("foo")
def foo(x=1, y="a"):
    return f"x={x}, y={y}"

foo()  # x=1, y='a'

with param_scope(**{"foo.x": 2}):
    foo()  # x=2, y='a'

Rust

fn foo() -> i32 {
    with_params! {
        get x = foo.x or 1i32; // Read hyperparameter with default value

        println!("x={}", x);
    }
}

fn main() {
    foo(); // x=1

    with_params! {
        set foo.x = 2i32; // Set hyperparameter

        foo(); // x=2
    }

    foo(); // x=1
}

C++

ASSERT(1 == GET_PARAM(a.b, 1), "get undefined param");
{
  auto guard = WITH_PARAMS(a, 1,        //
                            a.b, 2.0,    //
                            a.b.c, true, //
                            a.b.c.d, "str");
  ASSERT(1 == GET_PARAM(a, 0), "get int value");
  ASSERT(1 == GET_PARAM(a, 0), "get int value");
}

Detailed Usage Examples

Support for Default Values

Python

x = param_scope.foo.x | "default value"

Rust

get x = foo.x or "default value";

Scope Control of Parameter Values

Python

with param_scope() as ps: # 1st scope start
    ps.foo.x=1
    with param_scope() as ps2: # 2nd scope start
        ps.foo.y=2
    # 2nd scope end
# 1st scope end

Rust

with_params!{ // 1st scope start
    set foo.x=1;

    with_params!{ //2nd scope start
        set foo.y=2

        ...
    } // 2nd scope end
} // 1st scope end

Thread Isolation/Thread Safety

Python

@auto_param("foo")
def foo(x=1): # Print hyperparameter foo.x
    print(f"foo.x={x}")

with param_scope() as ps:
    ps.foo.x=2 # Modify foo.x in the current thread
    
    foo() # foo.x=2
    threading.Thread(target=foo).start() # foo.x=1, new thread's hyperparameter value is not affected by the main thread

Rust

fn foo() { // Print hyperparameter foo.x
    with_params!{
        get x = foo.x or 1;

        println!("foo.x={}", x);
    }
}

fn main() {
    with_params!{
        set foo.x = 2; // Modify foo.x in the current thread
        
        foo(); // foo.x=2
        thread::spawn(foo); // foo.x=1, new thread's hyperparameter value is not affected by the main thread
    }
}

Command Line Application

In command line applications, it's common to define hyperparameters using command line arguments (e.g., -D, --define) and control hyperparameters on the command line. Here's an example in Python and Rust:

Python

# example.py
from hyperparameter import param_scope, auto_param

@auto_param("example")
def main(a=0, b=1):
    print(f"example.a={a}, example.b={b}")

if __name__ == "__main__":
    import argparse
    parser = argparse.ArgumentParser()

    parser.add_argument("-D", "--define", nargs="*", default=[], action="extend")
    args = parser.parse_args()

    with param_scope(*args.define):
        main()

Rust

// example.rs
use hyperparameter::*;
use hyperparameter_derive::Parser;

fn main() {
    #[derive(Parser, Debug)]
    struct DeriveArgs {
        #[arg(short = 'D', long)]
        define: Vec<String>,
    }

    let args = DeriveArgs::parse();

    with_params! {
        params ParamScope::from(&args.define);

        foo()
    }
}

fn foo() {
    with_params! {
        get a = example.a or 0;
        get b = example.b or 1;
        
        println!("example.a={}, example.b={}",a ,b);
    }
}

More Examples

parameter tunning for researchers

This example demonstrates how to use hyperparameter in research projects, and make experiments reproducible.

experiment tracing for data scientists

This example showcases experiment management with hyperparameter and result tracing with mlflow.tracing.

Behavior Guarantees (Semantic Contract)

  • Keys & hashing: keys use . for nesting, case is preserved, and hashing uses the same UTF-8 input and seed across Python/Rust/C++; invalid characters are an error.
  • Read precedence: current thread’s innermost scope > parent scopes outward > frozen global snapshot > user default. Writes only affect the current scope and rollback on exit.
  • Defaults vs. missing: only missing keys fall back to defaults; explicit None/False/0 are treated as existing values. Type conversion rules (bool/int/float/str) are consistent across languages; invalid values use a best-effort conversion and otherwise fall back to the provided default (no silent random values).
  • Threads & frozen(): each thread starts from the frozen global snapshot; mutations stay in-thread unless frozen() is called, which atomically updates the global snapshot. Global mutations are lock-protected in the Python backend, matching Rust semantics.
  • Error model: reading an undefined key without a default raises a key error; backend load failure falls back to the Python backend without noisy tracebacks; no silent failure on type errors.
  • Multiprocess notice: cross-process consistency requires a shared backend (e.g., Rust backend or user-provided storage adapter); the built-in Python backend only guards threads, not processes.

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

hyperparameter-0.5.12.tar.gz (32.9 kB view details)

Uploaded Source

Built Distributions

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

hyperparameter-0.5.12-cp37-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (402.5 kB view details)

Uploaded CPython 3.7+manylinux: glibc 2.17+ x86-64

hyperparameter-0.5.12-cp37-abi3-macosx_11_0_arm64.whl (338.3 kB view details)

Uploaded CPython 3.7+macOS 11.0+ ARM64

hyperparameter-0.5.12-cp37-abi3-macosx_10_7_x86_64.whl (351.2 kB view details)

Uploaded CPython 3.7+macOS 10.7+ x86-64

File details

Details for the file hyperparameter-0.5.12.tar.gz.

File metadata

  • Download URL: hyperparameter-0.5.12.tar.gz
  • Upload date:
  • Size: 32.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for hyperparameter-0.5.12.tar.gz
Algorithm Hash digest
SHA256 5a34b0bed483e8bd261c82f3b3ea1de392ec04df5a2751db208a39e9b11a9873
MD5 428eb58365a0b03d2f2663b73829f6ca
BLAKE2b-256 2dc7e1e911a87efb63eeae4d647bf674810aa85d5d0de67e50fa2c91723a3aeb

See more details on using hashes here.

File details

Details for the file hyperparameter-0.5.12-cp37-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for hyperparameter-0.5.12-cp37-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 afca74b87779742d7fef877575ead40366dc5d331d4051050885876e496ee0aa
MD5 dfb643d2b9efbb884c7fec4f72059e0a
BLAKE2b-256 483ed23e6742e5a61022fb07d8195bebb9a8f67b32dc57a21060b9f3d6f7c78d

See more details on using hashes here.

File details

Details for the file hyperparameter-0.5.12-cp37-abi3-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for hyperparameter-0.5.12-cp37-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 2d62463826d81d651911e8ef688b3b9912a7d091654193454f60cf8d972315b6
MD5 da011461facdb16e0dd46420ee420edc
BLAKE2b-256 542fc6366688ad8b6c21510488404f87f1b3b458171ed373c7608eaa9d4f7688

See more details on using hashes here.

File details

Details for the file hyperparameter-0.5.12-cp37-abi3-macosx_10_7_x86_64.whl.

File metadata

File hashes

Hashes for hyperparameter-0.5.12-cp37-abi3-macosx_10_7_x86_64.whl
Algorithm Hash digest
SHA256 bc1cccfba02de0e38a087ebfff76d4f41cba9a40c81b96cd47ea82f8624e7246
MD5 364555c6cb8b12ac29a06e3505e73ee1
BLAKE2b-256 5649b0fbbbede39ca0366bfef3c1d682b47ad1d4af96b901a4c23582c688dcc3

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