Treelite: Universal model exchange format for decision tree forests
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
- breaking Starting from Treelite 4.0, the tree compiler and prediction runtime functionality (e.g., `treelite.Model.compile`, `treelite.Model.export_lib`, and the `treelite_runtime` module) were migrated to a separate project called TL2cgen. Older code relying on these features will break.
- gotcha Treelite's primary role is model exchange and serialization; it does not train decision tree models. You must use other libraries like XGBoost, LightGBM, or scikit-learn to train models before importing them into Treelite.
- gotcha On macOS, Treelite requires the OpenMP runtime. On Windows, Visual C++ Redistributable might be necessary for the generated shared libraries to function correctly.
Install
-
pip install treelite -
pip install tl2cgen
Imports
- treelite
import treelite
- treelite.frontend
import treelite.frontend
- tl2cgen
import tl2cgen
- treelite_runtime
import treelite_runtime
import tl2cgen
Quickstart
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)