Skip to content

Commit

Permalink
Strip torch (#246)
Browse files Browse the repository at this point in the history
* have users install torch separately
Co-authored-by: Karla Saur <[email protected]>
  • Loading branch information
interesaaat authored Aug 21, 2020
1 parent 46c35fc commit fee38ab
Show file tree
Hide file tree
Showing 9 changed files with 49 additions and 61 deletions.
14 changes: 13 additions & 1 deletion .github/workflows/pythonapp.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,18 @@ jobs:
uses: actions/setup-python@v2
with:
python-version: ${{ matrix.python-version }}
- name: Install pytorch for mac
if: ${{ matrix.os == 'macos-latest' }}
run: |
pip install torch
- name: Install pytorch 1.5.1+cpu if python 3.5 (not mac)
if: ${{ matrix.python-version == '3.5' && matrix.os != 'macos-latest' }}
run: |
pip install torch==1.5.1+cpu -f https://download.pytorch.org/whl/torch_stable.html
- name: Install pytorch 1.6.0+cpu if python > 3.5 (not mac)
if: ${{ matrix.python-version != '3.5' && matrix.os != 'macos-latest' }}
run: |
pip install torch==1.6.0+cpu -f https://download.pytorch.org/whl/torch_stable.html
- name: Install basic dependencies
run: |
python -m pip install --upgrade pip
Expand Down Expand Up @@ -67,4 +79,4 @@ jobs:
uses: codecov/codecov-action@v1
with:
file: ./coverage.xml
flags: unittests
flags: unittests
7 changes: 4 additions & 3 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,22 +30,23 @@ For more information see the [Code of Conduct FAQ](https://opensource.microsoft.
contact [[email protected]](mailto:[email protected]) with any additional questions or comments.

## Developing
The simplest setup is:
The simplest setup (assuming you have PyTorch already installed) is:
```
mkdir hummingbird
cd hummingbird
git clone https://github.com/microsoft/hummingbird.git .
pip install -e .[docs,tests,extra]
```
On Windows, the last line above must also contain `-f https://download.pytorch.org/whl/torch_stable.html`. (This is required because PyTorch version for Windows is not up to date.)

If you do not have pytorch installed, please [install it](https://pytorch.org/get-started/locally/) first.

### Docker
We provide a simple [Dockerfile](https://github.com/microsoft/hummingbird/blob/master/Dockerfile) that you can customize to your preferred development environment.
```
docker build git://github.com/microsoft/hummingbird -t hb-jupy
docker run -it hb-dev
```
### Codespases
### Codespaces
For a light-weight, web-based experience, we provide the configuration ([.devcontainer](https://github.com/microsoft/hummingbird/tree/master/.devcontainer)) for [Codespaces](https://online.visualstudio.com/environments). More information on this setup can be found [here]( https://docs.microsoft.com/en-us/visualstudio/online/reference/configuring).

### Tools
Expand Down
12 changes: 5 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,22 @@
## Introduction
*Hummingbird* is a library for compiling trained traditional ML models into tensor computations. *Hummingbird* allows users to seamlessly leverage neural network frameworks (such as [PyTorch](https://pytorch.org/)) to accelerate traditional ML models. Thanks to *Hummingbird*, 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 use *Hummingbird* to convert your trained traditional ML models into [PyTorch](https://pytorch.org/), [TorchScript](https://pytorch.org/docs/stable/jit.html), and [ONNX](https://onnx.ai/). *Hummingbird* [supports](https://github.com/microsoft/hummingbird/wiki/Supported-Operators) a variety of tree-based classifiers and regressors. These models include
Currently, you can use *Hummingbird* to convert your trained traditional ML models into [PyTorch](https://pytorch.org/), [TorchScript](https://pytorch.org/docs/stable/jit.html), and [ONNX](https://onnx.ai/). *Hummingbird* [supports](https://github.com/microsoft/hummingbird/wiki/Supported-Operators) a variety of ML models and featurizers. These models include
[scikit-learn](https://scikit-learn.org/stable/) Decision Trees and Random Forest, and also [LightGBM](https://github.com/Microsoft/LightGBM) and [XGBoost](https://github.com/dmlc/xgboost) Classifiers/Regressors. Support for other neural network backends (e.g., [TVM](https://docs.tvm.ai/)) and models is on our [roadmap](https://github.com/microsoft/hummingbird/wiki/Roadmap-for-Upcoming-Features-and-Support).

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.

## Installation

Hummingbird was tested on Python >= 3.5 on Linux, Windows and MacOS machines. It is recommended to use a virtual environment (See: [python3 venv doc](https://docs.python.org/3/tutorial/venv.html) or [Using Python environments in VS Code](https://code.visualstudio.com/docs/python/environments).)

Hummingbird requires PyTorch >= 1.4.0. Please go [here](https://pytorch.org/) for instructions on how to install PyTorch based on your platform and hardware.

Install the Hummingbird package in Linux or Mac
Once PyTorch is installed, you can get Hummingbird from pip with:
```
pip install hummingbird-ml
```

To install the package in Windows instead:
```
pip install hummingbird-ml -f https://download.pytorch.org/whl/torch_stable.html
```

If you require the optional dependencies lightgbm and xgboost, you can use:
```
pip install hummingbird-ml[extra]
Expand Down
2 changes: 1 addition & 1 deletion doc/html/hummingbird/ml/convert.html
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ <h1 class="title">Module <code>hummingbird.ml.convert</code></h1>
&#34;&#34;&#34;
Function used to check whether the specified backend and configuration pair is supported or not.
&#34;&#34;&#34;
assert torch_installed()
assert torch_installed(), &#34;To use Hummingbird you need to install torch.&#34;
import onnx
import torch

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -327,18 +327,10 @@ <h3>Ancestors</h3>
<h3>Methods</h3>
<dl>
<dt id="hummingbird.ml.operator_converters.onnx.onnx_operator.Cast.forward"><code class="name flex">
<span>def <span class="ident">forward</span></span>(<span>self, x)</span>
<span>def <span class="ident">forward</span></span>(<span>self, x) -> Callable[..., Any]</span>
</code></dt>
<dd>
<div class="desc"><p>Defines the computation performed at every call.</p>
<p>Should be overridden by all subclasses.</p>
<div class="admonition note">
<p class="admonition-title">Note</p>
<p>Although the recipe for forward pass needs to be defined within
this function, one should call the :class:<code>Module</code> instance afterwards
instead of this since the former takes care of running the
registered hooks while the latter silently ignores them.</p>
</div></div>
<div class="desc"></div>
<details class="source">
<summary>
<span>Expand source code</span>
Expand Down Expand Up @@ -377,18 +369,10 @@ <h3>Ancestors</h3>
<h3>Methods</h3>
<dl>
<dt id="hummingbird.ml.operator_converters.onnx.onnx_operator.Concat.forward"><code class="name flex">
<span>def <span class="ident">forward</span></span>(<span>self, *x)</span>
<span>def <span class="ident">forward</span></span>(<span>self, *x) -> Callable[..., Any]</span>
</code></dt>
<dd>
<div class="desc"><p>Defines the computation performed at every call.</p>
<p>Should be overridden by all subclasses.</p>
<div class="admonition note">
<p class="admonition-title">Note</p>
<p>Although the recipe for forward pass needs to be defined within
this function, one should call the :class:<code>Module</code> instance afterwards
instead of this since the former takes care of running the
registered hooks while the latter silently ignores them.</p>
</div></div>
<div class="desc"></div>
<details class="source">
<summary>
<span>Expand source code</span>
Expand Down Expand Up @@ -429,18 +413,10 @@ <h3>Ancestors</h3>
<h3>Methods</h3>
<dl>
<dt id="hummingbird.ml.operator_converters.onnx.onnx_operator.Reshape.forward"><code class="name flex">
<span>def <span class="ident">forward</span></span>(<span>self, x)</span>
<span>def <span class="ident">forward</span></span>(<span>self, x) -> Callable[..., Any]</span>
</code></dt>
<dd>
<div class="desc"><p>Defines the computation performed at every call.</p>
<p>Should be overridden by all subclasses.</p>
<div class="admonition note">
<p class="admonition-title">Note</p>
<p>Although the recipe for forward pass needs to be defined within
this function, one should call the :class:<code>Module</code> instance afterwards
instead of this since the former takes care of running the
registered hooks while the latter silently ignores them.</p>
</div></div>
<div class="desc"></div>
<details class="source">
<summary>
<span>Expand source code</span>
Expand Down
12 changes: 2 additions & 10 deletions doc/html/hummingbird/ml/operator_converters/sklearn/skl_sv.html
Original file line number Diff line number Diff line change
Expand Up @@ -304,18 +304,10 @@ <h3>Ancestors</h3>
<h3>Methods</h3>
<dl>
<dt id="hummingbird.ml.operator_converters.sklearn.skl_sv.SVC.forward"><code class="name flex">
<span>def <span class="ident">forward</span></span>(<span>self, x)</span>
<span>def <span class="ident">forward</span></span>(<span>self, x) -> Callable[..., Any]</span>
</code></dt>
<dd>
<div class="desc"><p>Defines the computation performed at every call.</p>
<p>Should be overridden by all subclasses.</p>
<div class="admonition note">
<p class="admonition-title">Note</p>
<p>Although the recipe for forward pass needs to be defined within
this function, one should call the :class:<code>Module</code> instance afterwards
instead of this since the former takes care of running the
registered hooks while the latter silently ignores them.</p>
</div></div>
<div class="desc"></div>
<details class="source">
<summary>
<span>Expand source code</span>
Expand Down
2 changes: 1 addition & 1 deletion hummingbird/ml/convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def _supported_backend_check_config(model, backend, extra_config):
"""
Function used to check whether the specified backend and configuration pair is supported or not.
"""
assert torch_installed()
assert torch_installed(), "To use Hummingbird you need to install torch."
import onnx
import torch

Expand Down
9 changes: 1 addition & 8 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,7 @@
if start_pos >= 0:
long_description = long_description[start_pos:]

install_requires = ["numpy>=1.15", "onnxconverter-common>=1.6.0", "scikit-learn==0.22.1"]
if sys.platform == "darwin":
install_requires.append("torch")
else:
if sys.version_info[1] == 5:
install_requires.append("torch==1.5.1+cpu")
else:
install_requires.append("torch==1.6.0+cpu")
install_requires = ["numpy>=1.15", "onnxconverter-common>=1.6.0", "scikit-learn>=0.22.1", "torch>=1.4.*"]
setup(
name="hummingbird-ml",
version=version_str,
Expand Down
16 changes: 16 additions & 0 deletions tests/test_onnxml_lightgbm_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ def _test_classifier(self, X, model, rtol=1e-06, atol=1e-06, extra_config={}):
@unittest.skipIf(
not (onnx_ml_tools_installed() and onnx_runtime_installed()), reason="ONNXML test require ONNX, ORT and ONNXMLTOOLS"
)
@unittest.skipIf(not lightgbm_installed(), reason="LightGBM test requires LightGBM installed")
def test_lightgbm_pytorch(self):
warnings.filterwarnings("ignore")
X = [[0, 1], [1, 1], [2, 0]]
Expand All @@ -97,6 +98,7 @@ def test_lightgbm_pytorch(self):
@unittest.skipIf(
not (onnx_ml_tools_installed() and onnx_runtime_installed()), reason="ONNXML test require ONNX, ORT and ONNXMLTOOLS"
)
@unittest.skipIf(not lightgbm_installed(), reason="LightGBM test requires LightGBM installed")
def test_lightgbm_onnx_pytorch(self):
warnings.filterwarnings("ignore")
X = [[0, 1], [1, 1], [2, 0]]
Expand Down Expand Up @@ -126,6 +128,7 @@ def test_lightgbm_onnx_pytorch(self):
@unittest.skipIf(
not (onnx_ml_tools_installed() and onnx_runtime_installed()), reason="ONNXML test require ONNX, ORT and ONNXMLTOOLS"
)
@unittest.skipIf(not lightgbm_installed(), reason="LightGBM test requires LightGBM installed")
def test_lightgbm_pytorch_extra_config(self):
warnings.filterwarnings("ignore")
X = [[0, 1], [1, 1], [2, 0]]
Expand All @@ -152,6 +155,7 @@ def test_lightgbm_pytorch_extra_config(self):
@unittest.skipIf(
not (onnx_ml_tools_installed() and onnx_runtime_installed()), reason="ONNXML test require ONNX, ORT and ONNXMLTOOLS"
)
@unittest.skipIf(not lightgbm_installed(), reason="LightGBM test requires LightGBM installed")
def test_lgbm_onnxml_model_regressor(self):
warnings.filterwarnings("ignore")
n_features = 28
Expand All @@ -170,6 +174,7 @@ def test_lgbm_onnxml_model_regressor(self):
@unittest.skipIf(
not (onnx_ml_tools_installed() and onnx_runtime_installed()), reason="ONNXML test require ONNX, ORT and ONNXMLTOOLS"
)
@unittest.skipIf(not lightgbm_installed(), reason="LightGBM test requires LightGBM installed")
def test_lightgbm_regressor(self):
warnings.filterwarnings("ignore")
X = [[0, 1], [1, 1], [2, 0]]
Expand All @@ -183,6 +188,7 @@ def test_lightgbm_regressor(self):
@unittest.skipIf(
not (onnx_ml_tools_installed() and onnx_runtime_installed()), reason="ONNXML test require ONNX, ORT and ONNXMLTOOLS"
)
@unittest.skipIf(not lightgbm_installed(), reason="LightGBM test requires LightGBM installed")
def test_lightgbm_regressor1(self):
warnings.filterwarnings("ignore")
model = lgb.LGBMRegressor(n_estimators=1, min_child_samples=1)
Expand All @@ -196,6 +202,7 @@ def test_lightgbm_regressor1(self):
@unittest.skipIf(
not (onnx_ml_tools_installed() and onnx_runtime_installed()), reason="ONNXML test require ONNX, ORT and ONNXMLTOOLS"
)
@unittest.skipIf(not lightgbm_installed(), reason="LightGBM test requires LightGBM installed")
def test_lightgbm_regressor2(self):
warnings.filterwarnings("ignore")
model = lgb.LGBMRegressor(n_estimators=2, max_depth=1, min_child_samples=1)
Expand All @@ -209,6 +216,7 @@ def test_lightgbm_regressor2(self):
@unittest.skipIf(
not (onnx_ml_tools_installed() and onnx_runtime_installed()), reason="ONNXML test require ONNX, ORT and ONNXMLTOOLS"
)
@unittest.skipIf(not lightgbm_installed(), reason="LightGBM test requires LightGBM installed")
def test_lightgbm_booster_regressor(self):
warnings.filterwarnings("ignore")
X = [[0, 1], [1, 1], [2, 0]]
Expand All @@ -225,6 +233,7 @@ def test_lightgbm_booster_regressor(self):
@unittest.skipIf(
not (onnx_ml_tools_installed() and onnx_runtime_installed()), reason="ONNXML test require ONNX, ORT and ONNXMLTOOLS"
)
@unittest.skipIf(not lightgbm_installed(), reason="LightGBM test requires LightGBM installed")
def test_lgbm_onnxml_model_binary(self):
warnings.filterwarnings("ignore")
n_features = 28
Expand All @@ -243,6 +252,7 @@ def test_lgbm_onnxml_model_binary(self):
@unittest.skipIf(
not (onnx_ml_tools_installed() and onnx_runtime_installed()), reason="ONNXML test require ONNX, ORT and ONNXMLTOOLS"
)
@unittest.skipIf(not lightgbm_installed(), reason="LightGBM test requires LightGBM installed")
def test_lightgbm_classifier(self):
warnings.filterwarnings("ignore")
model = lgb.LGBMClassifier(n_estimators=3, min_child_samples=1)
Expand All @@ -256,6 +266,7 @@ def test_lightgbm_classifier(self):
@unittest.skipIf(
not (onnx_ml_tools_installed() and onnx_runtime_installed()), reason="ONNXML test require ONNX, ORT and ONNXMLTOOLS"
)
@unittest.skipIf(not lightgbm_installed(), reason="LightGBM test requires LightGBM installed")
def test_lightgbm_classifier_zipmap(self):
warnings.filterwarnings("ignore")
X = [[0, 1], [1, 1], [2, 0], [1, 2]]
Expand All @@ -269,6 +280,7 @@ def test_lightgbm_classifier_zipmap(self):
@unittest.skipIf(
not (onnx_ml_tools_installed() and onnx_runtime_installed()), reason="ONNXML test require ONNX, ORT and ONNXMLTOOLS"
)
@unittest.skipIf(not lightgbm_installed(), reason="LightGBM test requires LightGBM installed")
def test_lightgbm_booster_classifier(self):
warnings.filterwarnings("ignore")
X = [[0, 1], [1, 1], [2, 0], [1, 2]]
Expand All @@ -282,6 +294,7 @@ def test_lightgbm_booster_classifier(self):
@unittest.skipIf(
not (onnx_ml_tools_installed() and onnx_runtime_installed()), reason="ONNXML test require ONNX, ORT and ONNXMLTOOLS"
)
@unittest.skipIf(not lightgbm_installed(), reason="LightGBM test requires LightGBM installed")
def test_lightgbm_booster_classifier_zipmap(self):
warnings.filterwarnings("ignore")
X = [[0, 1], [1, 1], [2, 0], [1, 2]]
Expand All @@ -295,6 +308,7 @@ def test_lightgbm_booster_classifier_zipmap(self):
@unittest.skipIf(
not (onnx_ml_tools_installed() and onnx_runtime_installed()), reason="ONNXML test require ONNX, ORT and ONNXMLTOOLS"
)
@unittest.skipIf(not lightgbm_installed(), reason="LightGBM test requires LightGBM installed")
def test_lgbm_onnxml_model_multi(self):
warnings.filterwarnings("ignore")
n_features = 28
Expand All @@ -313,6 +327,7 @@ def test_lgbm_onnxml_model_multi(self):
@unittest.skipIf(
not (onnx_ml_tools_installed() and onnx_runtime_installed()), reason="ONNXML test require ONNX, ORT and ONNXMLTOOLS"
)
@unittest.skipIf(not lightgbm_installed(), reason="LightGBM test requires LightGBM installed")
def test_lightgbm_classifier_multi(self):
warnings.filterwarnings("ignore")
model = lgb.LGBMClassifier(n_estimators=3, min_child_samples=1)
Expand All @@ -326,6 +341,7 @@ def test_lightgbm_classifier_multi(self):
@unittest.skipIf(
not (onnx_ml_tools_installed() and onnx_runtime_installed()), reason="ONNXML test require ONNX, ORT and ONNXMLTOOLS"
)
@unittest.skipIf(not lightgbm_installed(), reason="LightGBM test requires LightGBM installed")
def test_lightgbm_booster_multi_classifier(self):
warnings.filterwarnings("ignore")
X = [[0, 1], [1, 1], [2, 0], [1, 2], [-1, 2], [1, -2]]
Expand Down

0 comments on commit fee38ab

Please sign in to comment.