Home

Awesome

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?

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.

batchsize110100
LightGBM52.31μs84.46μs441.15μs
ONNX Runtime11.00μs36.74μs190.87μs
Treelite28.03μs40.81μs94.14μs
lleaves9.61μs14.06μs31.88μs

Dataset: MTPL2

mix of categorical and numerical features.

batchsize10,000100,000678,000
LightGBM95.14ms992.47ms7034.65ms
ONNX Runtime38.83ms381.40ms2849.42ms
Treelite38.15ms414.15ms2854.10ms
lleaves5.90ms56.96ms388.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},
}