Treelite: Universal model exchange format for decision tree forests

4.7.0 · active · verified Wed Apr 15

Treelite is a universal model exchange and serialization format for decision tree forests. It enables C++ applications to efficiently exchange and store decision trees from various sources like XGBoost, LightGBM, and scikit-learn. The current version is 4.7.0. It is actively maintained with irregular but feature-rich releases, including a significant architectural shift with version 4.0.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to import a pre-trained XGBoost model into Treelite, compile it into a shared library using `tl2cgen`, and then use the compiled library for predictions. Note that Treelite itself does not train models.

import os
import numpy as np
import xgboost # Required to train a model to be imported
import treelite
import tl2cgen # The separate compiler and runtime library

# 1. Train a dummy XGBoost model (in a real scenario, you'd load a pre-trained model)
X = np.random.rand(100, 10).astype('float32')
y = np.random.rand(100).astype('float32')
dtrain = xgboost.DMatrix(X, label=y)
param = {'max_depth': 2, 'eta': 1, 'objective': 'reg:squarederror'}
bst = xgboost.train(param, dtrain, num_boost_round=10)

# 2. Import the XGBoost model into Treelite
model = treelite.frontend.from_xgboost(bst)

# 3. Compile the Treelite model into a shared library using TL2cgen
# Choose appropriate extension: .so for Linux, .dll for Windows, .dylib for macOS
libpath = "./predictor.so"
tl2cgen.export_lib(model, toolchain="gcc", libpath=libpath, verbose=True)

# 4. Load the compiled model with TL2cgen for prediction
predictor = tl2cgen.Predictor(libpath=libpath, verbose=True)

# 5. Make predictions
dtest = tl2cgen.DMatrix(X)
predictions = predictor.predict(dtest)

print("Predictions (first 5):", predictions[:5])

# Clean up the generated library
os.remove(libpath)

view raw JSON →