Commit 8a9cf8aa authored by Michał Karzyński's avatar Michał Karzyński Committed by Robert Kimball

[Py] Update README for PyPI (#2151)

* Update README for PyPI

* Update README for PyPI

* Remove redundant newlines

* Fix links
parent 8249bf9f
# Building the Python API for nGraph
## Building nGraph Python Wheels
[nGraph's build instructions][ngraph_build] give detailed instructions on building nGraph on different operating systems. Please make sure you specify the options `-DNGRAPH_PYTHON_BUILD_ENABLE=ON` and `-DNGRAPH_ONNX_IMPORT_ENABLE=ON` when building nGraph. Use the `make python_wheel` command to build nGraph and create Python packages.
Basic build procedure on an Ubuntu system:
# apt-get install build-essential cmake clang-3.9 clang-format-3.9 git curl zlib1g zlib1g-dev libtinfo-dev unzip autoconf automake libtool
# apt-get install python3 python3-dev python python-dev python-virtualenv
$ git clone https://github.com/NervanaSystems/ngraph.git
$ cd ngraph/
$ mkdir build
$ cd build/
$ cmake ../ -DNGRAPH_PYTHON_BUILD_ENABLE=ON -DNGRAPH_ONNX_IMPORT_ENABLE=ON -DNGRAPH_USE_PREBUILT_LLVM=ON
$ make python_wheel
After this procedure completes, the `ngraph/build/python/dist` directory should contain Python packages.
$ ls python/dist/
ngraph-core-0.10.0.tar.gz
ngraph_core-0.10.0-cp27-cp27mu-linux_x86_64.whl
ngraph_core-0.10.0-cp35-cp35m-linux_x86_64.whl
### Using a virtualenv (optional)
You may wish to use a virutualenv for your installation.
$ virtualenv -p $(which python3) venv
$ source venv/bin/activate
(venv) $
### Installing the wheel
You may wish to use a virutualenv for your installation.
(venv) $ pip install ngraph/build/python/dist/ngraph_core-0.10.0-cp35-cp35m-linux_x86_64.whl
## Running tests
Unit tests require additional packages be installed:
(venv) $ cd ngraph/python
(venv) $ pip install -r test_requirements.txt
Then run tests:
(venv) $ pytest test/ngraph/
[ngraph_build]: http://ngraph.nervanasys.com/docs/latest/buildlb.html
# Python binding for the nGraph Library
## nGraph Neural Network compiler
## Build the nGraph Library (required)
[nGraph][ngraph_github] is Intel's open-source graph compiler and runtime for Neural Network models. Frameworks using nGraph to execute workloads have shown up to [45X](https://ai.intel.com/ngraph-compiler-stack-beta-release/) performance boost compared to native implementations.
Follow the [build instructions] to build nGraph. When you get to the `cmake`
command, be sure to specify the option that enables ONNX support in the Library:
nGraph can be used directly thought it's [Python API][api_python] or [C++ API][api_cpp]. Alternatively it can be used through one of its frontends, e.g. [TensorFlow][frontend_tf], [MXNet][frontend_mxnet] and [ONNX][frontend_onnx].
$ cmake ../ -DNGRAPH_ONNX_IMPORT_ENABLE=ON
## Installation
nGraph is available as binary wheels you can install from PyPI. nGraph binary wheels are currently tested on Ubuntu 16.04 and require a CPU with AVX-512 instructions, if you're using a different system, you may want to [build][ngraph_building] nGraph from sources.
Next, clone the `pybind11` repository:
Installing nGraph Python API from PyPI is easy:
$ cd ngraph/python
$ git clone --recursive https://github.com/pybind/pybind11.git
pip install ngraph-core
## Usage example
Set the environment variables:
Using nGraph's Python API to construct a computation graph and execute a computation is simple. The following example shows how to create a simple `(A + B) * C` computation graph and calculate a result using 3 numpy arrays as input.
export NGRAPH_CPP_BUILD_PATH=$HOME/ngraph_dist
export LD_LIBRARY_PATH=$HOME/ngraph_dist/lib
export DYLD_LIBRARY_PATH=$HOME/ngraph_dist/lib # (Only needed on MacOS)
export PYBIND_HEADERS_PATH=pybind11
```python
import numpy as np
import ngraph as ng
A = ng.parameter(shape=[2, 2], name='A', dtype=np.float32)
B = ng.parameter(shape=[2, 2], name='B', dtype=np.float32)
C = ng.parameter(shape=[2, 2], name='C', dtype=np.float32)
# >>> print(A)
# <Parameter: 'A' ([2, 2], float)>
Install the wrapper (Python binding):
model = (A + B) * C
# >>> print(model)
# <Multiply: 'Multiply_14' ([2, 2])>
$ python setup.py install
runtime = ng.runtime(backend_name='CPU')
# >>> print(runtime)
# <Runtime: Backend='CPU'>
computation = runtime.computation(model, A, B, C)
# >>> print(computation)
# <Computation: Multiply_14(A, B, C)>
Unit tests require additional packages be installed:
value_a = np.array([[1, 2], [3, 4]], dtype=np.float32)
value_b = np.array([[5, 6], [7, 8]], dtype=np.float32)
value_c = np.array([[9, 10], [11, 12]], dtype=np.float32)
$ pip install -r test_requirements.txt
result = computation(value_a, value_b, value_c)
# >>> print(result)
# [[ 54. 80.]
# [110. 144.]]
print('Result = ', result)
```
Then run a test:
$ pytest test/test_ops.py
$ pytest test/ngraph/
## Running tests with tox
[Tox] is a Python [virtualenv] management and test command line tool. In our
project it automates:
* running of unit tests with [pytest]
* checking that code style is compliant with [PEP8] using [Flake8]
* static type checking using [MyPy]
* testing across Python 2 and 3
Installing and running test with Tox:
$ pip install tox
$ tox
You can run tests using only Python 3 or 2 using the `-e` (environment) switch:
$ tox -e py36
$ tox -e py27
You can check styles in a particular code directory by specifying the path:
$ tox ngraph/
If you run into any problems, try recreating the virtual environments by
deleting the `.tox` directory:
$ rm -rf .tox
$ tox
[build instructions]:http://ngraph.nervanasys.com/docs/latest/buildlb.html
[Tox]:https://tox.readthedocs.io/
[virtualenv]:https://virtualenv.pypa.io/
[pytest]:https://docs.pytest.org/
[PEP8]:https://www.python.org/dev/peps/pep-0008
[Flake8]:http://flake8.pycqa.org
[MyPy]:http://mypy.readthedocs.io
\ No newline at end of file
[frontend_onnx]: https://pypi.org/project/ngraph-onnx/
[frontend_mxnet]: https://pypi.org/project/ngraph-mxnet/
[frontend_tf]: https://pypi.org/project/ngraph-tensorflow-bridge/
[ngraph_github]: https://github.com/NervanaSystems/ngraph "nGraph on GitHub"
[ngraph_building]: https://github.com/NervanaSystems/ngraph/blob/master/python/BUILDING.md "Building nGraph"
[api_python]: https://ngraph.nervanasys.com/docs/latest/python_api/ "nGraph's Python API documentation"
[api_cpp]: https://ngraph.nervanasys.com/docs/latest/howto/
......@@ -49,5 +49,6 @@ if [ "${PYTHON3_DETECTED}" == 1 ]; then
source build/venv3/bin/activate
pip install --upgrade setuptools pip wheel
python ${SOURCE_DIR}/setup.py bdist_wheel
python ${SOURCE_DIR}/setup.py sdist
deactivate
fi
......@@ -293,7 +293,7 @@ ext_modules = [
),
]
if NGRAPH_ONNX_IMPORT_ENABLE == 'TRUE':
if NGRAPH_ONNX_IMPORT_ENABLE in ['TRUE', 'ON', True]:
onnx_sources = [
'pyngraph/pyngraph_onnx_import.cpp',
'pyngraph/onnx_import/onnx_import.cpp',
......@@ -370,13 +370,14 @@ with open(os.path.join(PYNGRAPH_ROOT_DIR, 'requirements.txt')) as req:
setup(
name='ngraph-core',
description=open(os.path.join(PYNGRAPH_ROOT_DIR, 'README.md')).read(),
description='nGraph - Intel\'s graph compiler and runtime for Neural Networks',
version=__version__,
author='Intel',
author_email='intelnervana@intel.com',
url='https://ai.intel.com/',
url='https://github.com/NervanaSystems/ngraph/',
license='License :: OSI Approved :: Apache Software License',
long_description='',
long_description=open(os.path.join(PYNGRAPH_ROOT_DIR, 'README.md')).read(),
long_description_content_type='text/markdown',
ext_modules=ext_modules,
package_dir=package_dir,
packages=packages,
......
......@@ -17,20 +17,25 @@
import os
import numpy as np
from ngraph.impl.onnx_import import load_onnx_model_file
from test.ngraph.util import get_runtime
try:
from ngraph.impl.onnx_import import load_onnx_model_file
def test_import_onnx_function():
model_path = os.path.join(os.path.dirname(__file__), 'models/add_abc.onnx')
ng_function = load_onnx_model_file(model_path)[0]
def test_import_onnx_function():
model_path = os.path.join(os.path.dirname(__file__), 'models/add_abc.onnx')
ng_function = load_onnx_model_file(model_path)[0]
dtype = np.float32
value_a = np.array([1.0], dtype=dtype)
value_b = np.array([2.0], dtype=dtype)
value_c = np.array([3.0], dtype=dtype)
dtype = np.float32
value_a = np.array([1.0], dtype=dtype)
value_b = np.array([2.0], dtype=dtype)
value_c = np.array([3.0], dtype=dtype)
runtime = get_runtime()
computation = runtime.computation(ng_function)
result = computation(value_a, value_b, value_c)
assert np.allclose(result, np.array([6], dtype=dtype))
runtime = get_runtime()
computation = runtime.computation(ng_function)
result = computation(value_a, value_b, value_c)
assert np.allclose(result, np.array([6], dtype=dtype))
except ImportError:
# Do not test load_onnx_model_file if nGraph was build without ONNX support
pass
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment