Skip to content

Commit

Permalink
Add docs
Browse files Browse the repository at this point in the history
  • Loading branch information
hcho3 committed May 4, 2023
1 parent 694fbb2 commit a8eeaef
Show file tree
Hide file tree
Showing 4 changed files with 269 additions and 0 deletions.
4 changes: 4 additions & 0 deletions docs/_static/css/custom.css
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,7 @@ div.wy-side-nav-search {
.wy-table-responsive table td,.wy-table-responsive table th {
white-space: normal;
}

.rst-content .code-block-caption {
text-align: left;
}
2 changes: 2 additions & 0 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ Contents
:maxdepth: 2
:titlesonly:

install
treelite-migration
api
compiler_param
tl2cgen-doxygen
Expand Down
146 changes: 146 additions & 0 deletions docs/install.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
==================
Installation Guide
==================

You may choose one of two methods to install TL2cgen on your system:

.. contents::
:local:
:depth: 1

.. _install-pip:

Download binary releases from PyPI (Recommended)
================================================
This is probably the most convenient method. Simply type

.. code-block:: console
pip install tl2cgen
to install the TL2cgen package. The command will locate the binary release that is compatible with
your current platform. Check the installation by running

.. code-block:: python
import tl2cgen
in an interactive Python session. This method is available for only Windows, MacOS, and Linux.
For other operating systems, see the next section.

.. note:: Windows users need to install Visual C++ Redistributable

TL2cgen requires DLLs from `Visual C++ Redistributable
<https://www.microsoft.com/en-us/download/details.aspx?id=48145>`_
in order to function, so make sure to install it. Exception: If
you have Visual Studio installed, you already have access to
necessary libraries and thus don't need to install Visual C++
Redistributable.

.. note:: Installing OpenMP runtime on MacOS

TL2cgen requires the presence of OpenMP runtime. To install OpenMP runtime on a Mac OSX system,
run the following command:

.. code-block:: bash
brew install libomp
.. _install-conda:

Download binary releases from Conda
===================================

TL2cgen is also available on Conda.

.. code-block:: console
conda install -c conda-forge tl2cgen
to install the TL2cgen package. See https://anaconda.org/conda-forge/tl2cgen to check the
available platforms.

.. _install-source:

Compile TL2cgen from the source
===============================
Installation consists of two steps:

1. Build the native library from C++ code.
2. Install the Python package.

.. note:: Name of the native library

================== =====================
Operating System Shared library
================== =====================
Windows ``tl2cgen.dll``
Mac OS X ``libtl2cgen.dylib``
Linux / other UNIX ``libtl2cgen.so``
================== =====================

To get started, clone TL2cgen repo from GitHub.

.. code-block:: bash
git clone https://github.com/dmlc/tl2cgen.git
cd tl2cgen
The next step is to build the native library.

1-1. Compiling the native library on Linux and MacOS
----------------------------------------------------
Here, we use CMake to generate a Makefile:

.. code-block:: bash
mkdir build
cd build
cmake ..
Once CMake finished running, simply invoke GNU Make to obtain the native
libraries.

.. code-block:: bash
make
The compiled library will be under the ``build/`` directory.

.. note:: Compiling TL2cgen with multithreading on MacOS

TL2cgen requires the presence of OpenMP runtime. To install OpenMP runtime on a MacOS system,
run the following command:

.. code-block:: bash
brew install libomp
1-2. Compiling native libraries on Windows
------------------------------------------
We can use CMake to generate a Visual Studio project. The following snippet assumes that Visual
Studio 2022 is installed. Adjust the version depending on the copy that's installed on your system.

.. code-block:: dosbatch
mkdir build
cd build
cmake .. -G"Visual Studio 17 2022" -A x64
.. note:: Visual Studio 2019 or newer is required

TL2cgen uses the C++17 standard. Ensure that you have Visual Studio version 2019 or newer.

Once CMake finished running, open the generated solution file (``tl2cgen.sln``) in Visual Studio.
From the top menu, select **Build > Build Solution**.

2. Installing Python package
----------------------------
The Python package is located at the ``python`` subdirectory. Run Pip to install the Python
package. The Python package will re-use the native library built in Step 1.

.. code-block:: bash
cd python
pip install . # will re-use libtl2cgen.so
117 changes: 117 additions & 0 deletions docs/treelite-migration.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
=================================================
Migration Guide: How to migrate from Treelite 3.x
=================================================

Starting from 4.0 version, Treelite no longer supports compiling tree models into
C code. That part of Treelite has been migrated to TL2cgen.

In this guide, we will show you how to migrate your code to use TL2cgen instead of
Treelite version 3.x.

.. contents::
:local:

Model loading
=============
The logic for loading tree model into memory remains identical. Keep using Treelite
for model loading.

.. code-block:: python
:caption: Load XGBoost model from files
model = treelite.Model.load("xgboost_model.bin", model_format="xgboost")
# Or
model = treelite.Model.load("xgboost_model.json", model_format="xgboost_json")
# Or
model = treelite.Model.from_xgboost(bst) # bst is xgboost.Booster type
.. code-block:: python
:caption: Load LightGBM model
model = treelite.Model.load("lightgbm_model.txt", model_format="lightgbm")
# Or
model = treelite.Model.from_lightgbm(bst) # bst is lightgbm.Booster type
.. code-block:: python
:caption: Load scikit-learn model
model = treelite.sklearn.import_model(clf) # clf is a scikit-learn model object
.. code-block:: python
:caption: Load a tree model using the model builder interface
builder = treelite.ModelBuilder(num_feature=3)
tree = treelite.ModelBuilder.Tree()
tree[0].set_numerical_test_node(
feature_id=0,
opname="<",
threshold=5.0,
default_left=True,
left_child_key=1,
right_child_key=2
)
tree[1].set_leaf_node(0.6)
tree[2].set_leaf_node(-0.4)
tree[0].set_root()
builder.append(tree)
model = builder.commit() # Obtain treelite.Model object
Generating C code
=================

For generating C code from tree models, replace :py:meth:`treelite.Model.compile`
with :py:meth:`tl2cgen.generate_c_code`

.. code-block:: python
# Before: Treelite 3.x
model.compile(dirpath="./code_dir", params={})
# After: TL2cgen. Note the model object being passed as the first argument
tl2cgen.generate_c_code(model, dirpath="./code_dir", params={})
Exporting libraries
===================
Replace :py:meth:`treelite.Model.export_lib` with :py:meth:`tl2cgen.export_lib`:

.. code-block:: python
# Before: Treelite 3.x
model.export_lib(toolchain="msvc", libpath="./mymodel.dll", params={})
# After: TL2cgen. The model object is passed as the first argument
tl2cgen.export_lib(model, toolchain="msvc", libpath="./mymodel.dll", params={})
:py:meth:`treelite.Model.export_srcpkg` is replaced with :py:meth:`tl2cgen.export_srcpkg`.
Note that the parameter ``platform`` was removed in :py:meth:`tl2cgen.export_srcpkg`.

.. code-block:: python
# Before: Treelite 3.x
model.export_srcpkg(platform="unix", toolchain="gcc", pkgpath="./mymodel_pkg.zip",
libname="mymodel.so", params={})
# After: TL2cgen. The model object is passed as the first argument
# 'platform' parameter is removed.
tl2cgen.export_srcpkg(model, toolchain="gcc", pkgpath="./mymodel_pkg.zip",
libname="mymodel.so", params={})
Annotating branches
===================
Replace :py:class:`treelite.Annotator` with :py:meth:`tl2cgen.annotate_branch`.
Instead of calling two methods :py:meth:`treelite.Annotator.annotate_branch` and
:py:meth:`treelite.Annotator.save`, you only need to call one,
:py:meth:`tl2cgen.annotate_branch`:

.. code-block:: python
# Before: Treelite 3.x
annotator = treelite.Annotator()
annotator.annotate_branch(model, dmat)
annotator.save(path="mymodel-annotation.json")
# After: TL2cgen. Only one method call is needed.
tl2cgen.annotate_branch(model, dmat, path="mymodel-annotation.json")

0 comments on commit a8eeaef

Please sign in to comment.