{"id":6925,"library":"treelite","title":"Treelite: Universal model exchange format for decision tree forests","description":"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.","status":"active","version":"4.7.0","language":"en","source_language":"en","source_url":"https://github.com/dmlc/treelite","tags":["machine learning","decision trees","model deployment","xgboost","lightgbm","scikit-learn","compiler"],"install":[{"cmd":"pip install treelite","lang":"bash","label":"Core library for model exchange"},{"cmd":"pip install tl2cgen","lang":"bash","label":"Separate library for model compilation and runtime (recommended for deployment)"}],"dependencies":[{"reason":"Used for numerical operations and data handling with models.","package":"numpy","optional":false},{"reason":"Optional: For importing models trained with XGBoost.","package":"xgboost","optional":true},{"reason":"Optional: For importing models trained with LightGBM.","package":"lightgbm","optional":true},{"reason":"Optional: For importing models trained with scikit-learn.","package":"scikit-learn","optional":true},{"reason":"Required: For compiling Treelite models into deployable shared libraries and for prediction runtime (since Treelite 4.0+).","package":"tl2cgen","optional":false}],"imports":[{"symbol":"treelite","correct":"import treelite"},{"note":"Contains functions like `from_xgboost`, `load_lightgbm_model` for importing external models.","symbol":"treelite.frontend","correct":"import treelite.frontend"},{"note":"The dedicated module for model compilation and runtime prediction (post-Treelite 4.0).","symbol":"tl2cgen","correct":"import tl2cgen"},{"note":"The `treelite_runtime` module was deprecated and its functionality migrated to `tl2cgen` in Treelite 4.0+. Use `tl2cgen.Predictor` and `tl2cgen.DMatrix` instead.","wrong":"import treelite_runtime","symbol":"treelite_runtime","correct":"import tl2cgen"}],"quickstart":{"code":"import os\nimport numpy as np\nimport xgboost # Required to train a model to be imported\nimport treelite\nimport tl2cgen # The separate compiler and runtime library\n\n# 1. Train a dummy XGBoost model (in a real scenario, you'd load a pre-trained model)\nX = np.random.rand(100, 10).astype('float32')\ny = np.random.rand(100).astype('float32')\ndtrain = xgboost.DMatrix(X, label=y)\nparam = {'max_depth': 2, 'eta': 1, 'objective': 'reg:squarederror'}\nbst = xgboost.train(param, dtrain, num_boost_round=10)\n\n# 2. Import the XGBoost model into Treelite\nmodel = treelite.frontend.from_xgboost(bst)\n\n# 3. Compile the Treelite model into a shared library using TL2cgen\n# Choose appropriate extension: .so for Linux, .dll for Windows, .dylib for macOS\nlibpath = \"./predictor.so\"\ntl2cgen.export_lib(model, toolchain=\"gcc\", libpath=libpath, verbose=True)\n\n# 4. Load the compiled model with TL2cgen for prediction\npredictor = tl2cgen.Predictor(libpath=libpath, verbose=True)\n\n# 5. Make predictions\ndtest = tl2cgen.DMatrix(X)\npredictions = predictor.predict(dtest)\n\nprint(\"Predictions (first 5):\", predictions[:5])\n\n# Clean up the generated library\nos.remove(libpath)\n","lang":"python","description":"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."},"warnings":[{"fix":"Install `tl2cgen` (e.g., `pip install tl2cgen`) and replace `treelite_runtime` imports with `tl2cgen`. Replace `treelite.Model.export_lib` with `tl2cgen.export_lib`, and `treelite_runtime.Predictor`/`DMatrix` with `tl2cgen.Predictor`/`DMatrix`.","message":"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.","severity":"breaking","affected_versions":">=4.0.0"},{"fix":"Ensure you have a pre-trained model object or file from a supported ML library before attempting to use Treelite for import and deployment.","message":"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.","severity":"gotcha","affected_versions":"All versions"},{"fix":"For macOS, run `brew install libomp`. For Windows, ensure Visual C++ Redistributable is installed.","message":"On macOS, Treelite requires the OpenMP runtime. On Windows, Visual C++ Redistributable might be necessary for the generated shared libraries to function correctly.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-15T00:00:00.000Z","next_check":"2026-07-14T00:00:00.000Z","problems":[]}