Package: concrete-ml-extensions-hb
IntroductionHummingbirdis a library for compiling trained traditional ML models into tensor computations.Hummingbirdallows 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 useHummingbirdto convert your trained traditional ML models intoPyTorch,TorchScript,ONNX, andTVM).Hummingbirdsupportsa variety of ML models and featurizers.  These models includescikit-learnDecision Trees and Random Forest, and alsoLightGBMandXGBoostClassifiers/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.How Hummingbird WorksHummingbird 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.)Simple decision treeIn 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 tensorwith tensorA(computed from the decision tree model above) that captures the relationship between input features and internal nodes. Then compare it with tensorBwhich is set to the value of each internal node (orange) to create the tensorinput paththat 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 tensorAis 5x4 and tensor B is 1x4.Step 2:Theinput pathtensor will be multiplied with tensorCthat 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 tensorDthat captures the count of the left child of its parent in the path from a leaf node to the tree root to create the tenor 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 tensorCis 4x5 and tensorDis 1x5.Step 3:Theoutput pathwill be multiplied with tensorEthat captures the mapping between leaf nodes to infer the final prediction. In this case, tree model has 5 outputs, therefore, shape of tensorEis 5x1.And now Hummingbird has compiled a tree-based model using the GEMM strategy!  For more details, please seeFigure 3of our paper.Thank you toChien Vufor contributing the graphics and descriptions in hisblogfor this example!InstallationHummingbird was tested on Python >= 3.7 on Linux, Windows and MacOS machines.  (Python 3.6 is supported up tohummingbird-ml==0.4.2.)  It is recommended to use a virtual environment (See:python3 venv docorUsing Python environments in VS Code.)Hummingbird requires PyTorch >= 1.6.0. Please goherefor instructions on how to install PyTorch based on your platform and hardware.Once PyTorch is installed, you can get Hummingbird from pip with:pip install hummingbird-mlIf you require the optional dependencies lightgbm and xgboost, you can use:pip install hummingbird-ml[extra]See alsoTroubleshootingfor common problems.ExamplesSee thenotebookssection 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.mland addconvert(model, 'dnn_framework')to your code. Below is an example using ascikit-learn random forestmodel andPyTorchas 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')DocumentationThe 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 2019ContributingWe welcome contributions! Please see the guide onContributing.Also, see ourroadmapof planned features.CommunityJoin our community!For more formal enquiries, you cancontact us.AuthorsSupun NakandalaMatteo InterlandiKarla SaurLicenseMIT License