Skip to main content

lleaves 🍃

CI Documentation Status Downloads

A LLVM-based compiler for LightGBM decision trees.

lleaves converts trained LightGBM models to optimized machine code, speeding-up prediction by ≥10x.

Example

lgbm_model = lightgbm.Booster(model_file="NYC_taxi/model.txt")
%timeit lgbm_model.predict(df)
# 12.77s

llvm_model = lleaves.Model(model_file="NYC_taxi/model.txt")
llvm_model.compile()
%timeit llvm_model.predict(df)
# 0.90s 

Why lleaves?

  • Speed: Both low-latency single-row prediction and high-throughput batch-prediction.
  • Drop-in replacement: The interface of lleaves.Model is a subset of LightGBM.Booster.
  • Dependencies: llvmlite and numpy. LLVM comes statically linked.

Installation

conda install -c conda-forge lleaves or pip install lleaves (Linux and MacOS only).

Benchmarks

Ran on a dedicated Intel i7-4770 Haswell, 4 cores. Stated runtime is the minimum over 20.000 runs.

Dataset: NYC-taxi

mostly numerical features.

batchsize 1 10 100
LightGBM 52.31μs 84.46μs 441.15μs
ONNX Runtime 11.00μs 36.74μs 190.87μs
Treelite 28.03μs 40.81μs 94.14μs
lleaves 9.61μs 14.06μs 31.88μs

Dataset: MTPL2

mix of categorical and numerical features.

batchsize 10,000 100,000 678,000
LightGBM 95.14ms 992.47ms 7034.65ms
ONNX Runtime 38.83ms 381.40ms 2849.42ms
Treelite 38.15ms 414.15ms 2854.10ms
lleaves 5.90ms 56.96ms 388.88ms

Advanced Usage

To avoid expensive recompilation, you can call lleaves.Model.compile() and pass a cache=<filepath> argument. This will store an ELF (Linux) / Mach-O (macOS) file at the given path when the method is first called. Subsequent calls of compile(cache=<same filepath>) will skip compilation and load the stored binary file instead. For more info, see docs.

To eliminate any Python overhead during inference you can link against this generated binary. For an example of how to do this see benchmarks/c_bench/. The function signature might change between major versions.

Development

High-level explanation of the inner workings of the lleaves compiler: link

mamba env create
conda activate lleaves
pip install -e .
pre-commit install
./benchmarks/data/setup_data.sh
pytest -k "not benchmark"

Cite

If you're using lleaves for your research, I'd appreciate if you could cite it. Use:

@software{Boehm_lleaves,
  author = {Boehm, Simon},
  title = {lleaves},
  url = {https://github.com/siboehm/lleaves},
  license = {MIT},
}

Release files for lleaves 1.3.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for lleaves 1.3.0
File Size Uploaded
lleaves-1.3.0.tar.gz 1.5 MB Details

Built distribution (wheel)

Table of built distributions (wheels) for lleaves 1.3.0
File Interpreter ABI Platform
lleaves-1.3.0-py3-none-any.whl Python 3 none any Details

Total release size: 1.5 MB

Release files / lleaves-1.3.0.tar.gz

Download URL lleaves-1.3.0.tar.gz
Size 1.5 MB
Tags Source
SHA-256 checksum
How to use checksums
b9e3c0b9e6896aa5bfe32192aba039063e3869dbedc44f5bec7a133078597fb1
BLAKE2b-256 checksum
How to use checksums
1927aca65414fdc25737a5971b7c82ec1d1358cf49ef05adacf6303f84873230
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.0 CPython/3.12.7

Release files / lleaves-1.3.0-py3-none-any.whl

Download URL lleaves-1.3.0-py3-none-any.whl
Size 22.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
c25d37b3240f36daa8cf6b48847a63f7b37ec2f57749b3ce2997d2a9d5544eb5
BLAKE2b-256 checksum
How to use checksums
ca1e0f076823cc005e6d3569292e16a57c0cb21c9b0a2439c88fa67ec906ea18
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.0 CPython/3.12.7

Release history Release notifications | RSS feed

This release

1.3.0 This release

2 release files

1.2.3

2 release files

1.2.2

2 release files

1.2.1

2 release files

1.2.0

2 release files

1.1.0

2 release files

1.0.0

2 release files

0.2.6

2 release files

0.2.5

2 release files

0.2.4

2 release files

0.2.3

2 release files

0.2.2

2 release files

0.2.1

2 release files

0.2.0

2 release files

0.1.1

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page