Skip to main content

Python package exposing the LambdaHappy class

Project description

Lambda Happy

A high-performance CPU/GPU solver for estimating the lambda_happy factor in sparse linear models (≈99% sparsity) using C++/CUDA and PyTorch.

Installation

# Core functionality
pip install lambda-happy

# Benchmark GUI (PyQt5)
pip install lambda-happy[benchmark]

# Validation tools (PyQt5 + pandas)
pip install lambda-happy[validation]

# All extras (Benchmark + Validation tools)
pip install lambda-happy[all]

Then install torch (see: https://pytorch.org/get-started/locally/)

Here is the current command in August 2025 for Linux :

pip3 install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu118

What is lambda happy ?

In sparse regression, lambda_happy balances data fidelity against model sparsity.

Given:

  • X ∈ R -> The feature matrix
  • Z ∈ R -> A random Gaussian projection matrix
  • Z_centrer -> centered Z matrix

Lambda_happy is estimated as the 95th percentile of (norm of the transpose of X times the centered Z matrix, measured in Chebyshev norm) divided by (norm of the centered Z matrix measured in 2-norm), that is:

  • lambda_happy = Quantile_0.95 ( || X^T * Z_centrer ||_infinity / || Z_centrer ||_2 )

which requires:

  • p: The number of feature in The feature matrix X (affects only the matmul)
  • n: The number of row in The feature matrix x
  • m: number of projections (larger m ⇒ higher precision, but each step’s cost scales with m)

Quickstart

import torch
from lambda_happy import LambdaHappy

# Prepare data
X = torch.randn(1000, 5000, device="cuda")

# Initialize solver (auto‐select fastest backend)
solver = LambdaHappy(X, force_fastest=True)

# Single estimate
lambda_value = solver.compute(m=10000)
print(f"lambda_value ≈ {lambda_value:.4f}")

# Multiple runs
lambda_values = solver.compute_many(m=10000, nb_run=50)

# Aggregated (mean)
lambda_mean = solver.compute_agg(m=10000, nb_run=500, func=torch.median)

Performance Trade-Offs

Projection Dimension (m)

  • ↑ m → improves lambda_happy precision.
  • ↑ m → linearly increases compute time (all kernels scale with m).
  • Recommended: m = 10,000 provides good accuracy in most cases.

ℹ️ Use float16 on GPU only if the input matrix X is normalized.
Otherwise, lambda_happy estimation may be unstable or inconsistent.

Sample Dimension (n)

  • ↑ n → increases cost in all kernels (since Z ∈ R^(n × m)), except for the quantile post-processing step.

Feature Dimension (p)

  • ↑ p → only affects the X^T·Z matrix multiplication.

Recommended Settings

Context Data Type Notes
CPU float32 Stable, widely supported, and generally the fastest on CPU.
CUDA GPU float16 High performance if X is normalized; otherwise use float32.
Backend "AUTOMATIC" Selects the best available implementation based on hardware and dtype.

Extras

Benchmark

The lambda-happy-benchmark script measures and compares the performance of LambdaHappy on CPU and GPU. It offers various benchmarking options and displays live throughput plots. Example usage:

lambda-happy-benchmark --benchmark_2D --benchmark_3D --device cuda --dtype float16 -n 1000 -p 1000 -m 10000

This runs a 2D benchmark using CUDA with specified matrix dimensions and then run a 3D benchmark.

ℹ️ Note: Not all hyperparameters are used for every plot, but if provided, they will be applied when relevant.

Validation

The lambda-happy-validation script runs tests to validate lambda_happy estimation accuracy. It generates detailed reports and distribution plots using pandas and PyQt5.

Example usage:

lambda-happy-validation --distribution_small --device cuda --dtype float32 -n 1000 -p 1000

This plots small-scale lambda_happy distributions on cuda for the given parameters.

Results

Here are the results for a CUDA calculation :

Rang Mode Version Précision FPS Speed-up
1 Mono-GPU SMART_TENSOR Float32 449 1.00x
2 Mono-GPU GPU_DEDICATED Float32 501 1.12x
3 Multi-GPU SMART_TENSOR Float32 511 1.14x
4 Multi-GPU SMART_TENSOR Float16 664 1.48x
5 Multi-GPU GPU_DEDICATED Float32 911 2.03x
6 Mono-GPU SMART_TENSOR Float16 1'215 2.71x
7 Mono-GPU GPU_DEDICATED Float16 1'618 3.60x
8 Multi-GPU GPU_DEDICATED Float16 2'104 4.69x

The test server is equipped with an Intel Xeon E5-2699 v3 processor (2014) and three NVIDIA GeForce RTX 2080 Ti graphics cards (2018).

It uses the default parameters for the evaluation with X of size 1000x1000 and m=10000.

ℹ️ Note: Use device="cuda" when you create X.

About This Project

This package is developed as part of the Bachelor’s thesis by Yerly Sevan at HE-Arc, supervised by Cédric Bilat.

For questions or contact: sevan.yerly@he-arc.ch

Lambda Happy

A high-performance CPU/GPU solver for estimating the lambda_happy factor in sparse linear models (≈99% sparsity) using C++/CUDA and PyTorch, compatible with linux only and python 3.10.

Installation

# Core functionality
pip install lambda-happy

# Benchmark GUI (PyQt5)
pip install lambda-happy[benchmark]

# Validation tools (PyQt5 + pandas)
pip install lambda-happy[validation]

# All extras (Benchmark + Validation tools)
pip install lambda-happy[all]

Then install torch (see: https://pytorch.org/get-started/locally/)

pip3 install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu118

Quickstart

import torch
from lambda_happy import LambdaHappy

# Prepare data
X = torch.randn(1000, 5000, device="cuda")
# Initialize solver (auto‐select fastest backend)
solver = LambdaHappy(X, force_fastest=True)

# Single estimate
λ = solver.compute(m=5000)
print(f"λ ≈ {λ:.4f}")

# Multiple runs
λs = solver.compute_many(m=5000, nb_run=50)

# Aggregated (mean)
λ_mean = solver.compute_agg(m=5000, nb_run=50, func=torch.mean)

What is lambda happy ?

In sparse model, lambda_happy balances data fidelity against model sparsity.

Given:

  • X ∈ R -> The feature matrix
  • Z ∈ R -> A random Gaussian projection matrix
  • Z_centrer -> centered Z matrix

Lambda_happy is estimated as the 95th percentile of (norm of the transpose of X times the centered Z matrix, measured in Chebyshev norm) divided by (norm of the centered Z matrix measured in 2-norm), that is:

  • lambda_happy = Quantile_0.95 ( || X^T * Z_centrer ||_infinity / || Z_centrer ||_2 )

which requires:

  • p: The number of feature in The feature matrix X (affects only the matmul)
  • n: The number of row in The feature matrix x
  • m: number of projections (larger m ⇒ higher precision, but each step’s cost scales with m)

Performance Trade-Offs

Projection Dimension (m)

  • ↑ m → improves lambda_happy precision.
  • ↑ m → linearly increases compute time (all kernels scale with m).
  • Recommended: m = 10,000 provides good accuracy in most cases.

ℹ️ Use float16 on GPU only if the input matrix X is normalized.
Otherwise, lambda_happy estimation may be unstable or inconsistent.

Sample Dimension (n)

  • ↑ n → increases cost in all kernels (since Z ∈ R^(n × m)), except for the quantile post-processing step.

Feature Dimension (p)

  • ↑ p → only affects the X^T·Z matrix multiplication.

Recommended Settings

Context Data Type Notes
CPU float32 Stable, widely supported, and generally the fastest on CPU.
CUDA GPU float16 High performance if X is normalized; otherwise use float32.
Backend "AUTOMATIC" Selects the best available implementation based on hardware and dtype.

Extras

Benchmark

The lambda-happy-benchmark script measures and compares the performance of LambdaHappy on CPU and GPU. It offers various benchmarking options and displays live throughput plots. Example usage:

lambda-happy-benchmark --benchmark_2D --benchmark_3D --device cuda --dtype float16 -n 1000 -p 1000 -m 10000

This runs a 2D benchmark using CUDA with specified matrix dimensions and then run a 3D benchmark.

ℹ️ Note: Not all hyperparameters are used for every plot, but if provided, they will be applied when relevant.

Validation

The lambda-happy-validation script runs tests to validate lambda_happy estimation accuracy. It generates detailed reports and distribution plots using pandas and PyQt5.

Example usage:

lambda-happy-validation --distribution_small --device cuda --dtype float32 -n 1000 -p 1000

This plots small-scale lambda_happy distributions on cuda for the given parameters.

Results

Here are the results for a CUDA calculation :

Rang Mode Version Précision FPS Speed-up
1 Mono-GPU SMART_TENSOR Float32 449 1.00x
2 Mono-GPU GPU_DEDICATED Float32 501 1.12x
3 Multi-GPU SMART_TENSOR Float32 511 1.14x
4 Multi-GPU SMART_TENSOR Float16 664 1.48x
5 Multi-GPU GPU_DEDICATED Float32 911 2.03x
6 Mono-GPU SMART_TENSOR Float16 1'215 2.71x
7 Mono-GPU GPU_DEDICATED Float16 1'618 3.60x
8 Multi-GPU GPU_DEDICATED Float16 2'104 4.69x

ℹ️ FPS : number of times the lambda_happy factor is estimated per second.

The test server is equipped with an Intel Xeon E5-2699 v3 processor (2014) and three NVIDIA GeForce RTX 2080 Ti graphics cards (2018).

It uses the default parameters for the evaluation with X of size 1000x1000 and m=10000.

ℹ️ Note: Use device="cuda" when you create X.

About This Project

This package is developed as part of the Bachelor’s thesis by Yerly Sevan at HE-Arc, supervised by Cédric Bilat.

This package, including performance optimizations, was developed as part of a Bachelor’s thesis at HE-Arc by Yerly Sevan (sevan.yerly@he-arc.ch), under the supervision of Cédric Bilat (cedric.bilat@he-arc.ch). The mathematical foundations were developed by Sylvain Sardy (sylvain.sardy@unige.ch).

For questions or contact: sevan.yerly@he-arc.ch or cedric.bilat@he-arc.ch

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

lambda_happy-0.1.7.tar.gz (2.1 MB view details)

Uploaded Source

Built Distribution

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

lambda_happy-0.1.7-py3-none-any.whl (2.1 MB view details)

Uploaded Python 3

File details

Details for the file lambda_happy-0.1.7.tar.gz.

File metadata

  • Download URL: lambda_happy-0.1.7.tar.gz
  • Upload date:
  • Size: 2.1 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.6.1

File hashes

Hashes for lambda_happy-0.1.7.tar.gz
Algorithm Hash digest
SHA256 e67805bf999c31e0ef838c1f4f479b979ef826e013252e534a8ac40741d6c9c8
MD5 65a8069665d624d7bc8df669ac49a10e
BLAKE2b-256 28717bc40d62bbd525cb07f8dd43d288ae6bb2dc654e9ec6c2cd89d614f54a67

See more details on using hashes here.

File details

Details for the file lambda_happy-0.1.7-py3-none-any.whl.

File metadata

File hashes

Hashes for lambda_happy-0.1.7-py3-none-any.whl
Algorithm Hash digest
SHA256 6f84a6c0d70e4efa86e4bdea8a2e7c292f3944bd1aaac32923e851e530d2bf6d
MD5 9d8f6a767013844a3af500c30c824f72
BLAKE2b-256 552cafc74e8d9e4aba1f775206717a1f210926a28823aa10590cb34d9ec3a8a5

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