- Notifications
You must be signed in to change notification settings - Fork280
Hummingbird compiles trained ML models into tensor computation for faster inference.
License
microsoft/hummingbird
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
Hummingbird is a library for compiling trained traditional ML models into tensor computations.Hummingbird allows users to seamlessly leverage neural network frameworks (such asPyTorch) to accelerate traditional ML models. Thanks toHummingbird, users can benefit from: (1) all the current and future optimizations implemented in neural network frameworks; (2) native hardware acceleration; (3) having a unique platform to support for both traditional and neural network models; and have all of this (4) without having to re-engineer their models.
Currently, you can useHummingbird to convert your trained traditional ML models intoPyTorch,TorchScript,ONNX, andTVM).Hummingbirdsupports a variety of ML models and featurizers. These models includescikit-learn Decision Trees and Random Forest, and alsoLightGBM andXGBoost Classifiers/Regressors. Support for other neural network backends and models is on ourroadmap.
Hummingbird also provides a convenient uniform "inference" API following the Sklearn API. This allows swapping Sklearn models with Hummingbird-generated ones without having to change the inference code. By converting the models to PyTorch and TorchScript it also becomes possible to serve them usingTorchServe.
Hummingbird works by reconfiguring algorithmic operators such that we can perform more regular computations which are amenable to vectorized and GPU execution. Each operator is slightly different, and we incorporate multiple strategies. This example explains one of Hummingbird's strategies for translating a decision tree into tensors involving GEMM (GEneric Matrix Multiplication), where we implement the traversal of the tree using matrix multiplications. (GEMM is one of the three tree conversion strategies we currently support.)
In this example, the decision tree has four decision nodes (orange), and five leaf nodes (blue). The tree takes a feature vector with five elements as input. For example, assume that we want to calculate the output of this observation:
Step 1: Multiply theinput tensor
with tensorA
(computed from the decision tree model above) that captures the relationship between input features and internal nodes. Then compare it with tensorB
which is set to the value of each internal node (orange) to create the tensorinput path
that represents the path from input to node. In this case, the tree model has 4 conditions and the input vector is 5, therefore, the shape of tensorA
is 5x4 and tensor B is 1x4.
Step 2: Theinput path
tensor will be multiplied with tensorC
that captures whether the internal node is a parent of that internal node, and if so, whether it is in the left or right sub-tree (left = 1, right =-1, otherwise =0) and then check the equals with tensorD
that captures the count of the left child of its parent in the path from a leaf node to the tree root to create the tensor output path that represents the path from node to output. In this case, this tree model has 5 outputs with 4 conditions, therefore, the shape of tensorC
is 4x5 and tensorD
is 1x5.
Step 3: Theoutput path
will be multiplied with tensorE
that captures the mapping between leaf nodes to infer the final prediction. In this case, tree model has 5 outputs, therefore, shape of tensorE
is 5x1.
And now Hummingbird has compiled a tree-based model using the GEMM strategy! For more details, please seeFigure 3 of our paper.
Thank you toChien Vu for contributing the graphics and descriptions in hisblog for this example!
Hummingbird was tested on Python 3.9, 3.10 and 3.11 on Linux, Windows and MacOS machines. (TVM only works through Python3.10.) It is recommended to use a virtual environment (See:python3 venv doc orUsing Python environments in VS Code.)
Hummingbird requires PyTorch >= 1.6.0. Please gohere for instructions on how to install PyTorch based on your platform and hardware.
Once PyTorch is installed, you can get Hummingbird from pip with:
python -m pip install hummingbird-ml
If you require the optional dependencies lightgbm and xgboost, you can use:
python -m pip install hummingbird-ml[extra]
See alsoTroubleshooting for common problems.
See thenotebooks section for examples that demonstrate use and speedups.
In general, Hummingbird syntax is very intuitive and minimal. To run your traditional ML model on DNN frameworks, you only need toimport hummingbird.ml
and addconvert(model, 'dnn_framework')
to your code. Below is an example using ascikit-learn random forest model andPyTorch as target framework.
importnumpyasnpfromsklearn.ensembleimportRandomForestClassifierfromhummingbird.mlimportconvert,load# Create some random data for binary classificationnum_classes=2X=np.random.rand(100000,28)y=np.random.randint(num_classes,size=100000)# Create and train a model (scikit-learn RandomForestClassifier in this case)skl_model=RandomForestClassifier(n_estimators=10,max_depth=10)skl_model.fit(X,y)# Use Hummingbird to convert the model to PyTorchmodel=convert(skl_model,'pytorch')# Run predictions on CPUmodel.predict(X)# Run predictions on GPUmodel.to('cuda')model.predict(X)# Save the modelmodel.save('hb_model')# Load the model backmodel=load('hb_model')
The API documentation ishere.
You can also read about Hummingbird in our blog posthere.
For more details on the vision and on the technical details related to Hummingbird, please check our papers:
Tensors: An abstraction for general data processing. Dimitrios Koutsoukos, Supun Nakandala, Konstantinos Karanasos, Karla Saur, Gustavo Alonso, Matteo Interlandi. PVLDB 2021.
A Tensor Compiler for Unified Machine Learning Prediction Serving. Supun Nakandala, Karla Saur, Gyeong-In Yu, Konstantinos Karanasos, Carlo Curino, Markus Weimer, Matteo Interlandi. OSDI 2020.
Compiling Classical ML Pipelines into Tensor Computations for One-size-fits-all Prediction Serving. Supun Nakandala, Gyeong-In Yu, Markus Weimer, Matteo Interlandi. System for ML Workshop. NeurIPS 2019
We welcome contributions! Please see the guide onContributing.
Also, see ourroadmap of planned features.
- Supun Nakandala (@scnakandala)
- Matteo Interlandi (@interesaaat)
- Karla Saur (@ksaur)
About
Hummingbird compiles trained ML models into tensor computation for faster inference.