TL2cgen: model compiler for decision trees

TL2cgen (TreeLite 2 C GENerator) is a model compiler for decision tree models. You can convert any decision tree models (random forests, gradient boosting models) into C code and distribute it as a native binary.

TL2cgen seamlessly integrates with Treelite. Any tree models that are supported by Treelite can be converted into C via TL2cgen.

Quick start

Install TL2cgen from PyPI:

pip install tl2cgen

Or from Conda-forge:

conda install -c conda-forge tl2cgen

To use TL2cgen, first import your tree ensemble model:

import treelite  # Used for importing tree model
model = treelite.Model.load("my_model.json", model_format="xgboost_json")

Now use TL2cgen to generate C code:

import tl2cgen
tl2cgen.generate_c_code(model, dirpath="./src")

TL2cgen also provides a convenient wrapper for building native shared libraries:

tl2cgen.export_lib(model, toolchain="gcc", libpath="./predictor.so")

You can also build a source archive, with a CMakeLists.txt:

# Run `cmake` on the target machine
tl2cgen.export_srcpkg(model, toolchain="cmake", pkgpath="./archive.zip",
                      libname="predictor")

Finally, make predictions with the native library using the Predictor class:

predictor = tl2cgen.Predictor("./predictor.so")
dmat = tl2cgen.DMatrix(X)
out_pred = predictor.predict(dmat)

Contents

Indices