Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CMake based build for pyngraph module #3080

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
57 commits
Select commit Hold shift + click to select a range
072a6ab
[MO] Add CMake install for Model Optimizer
slyubimt Oct 26, 2020
8fc1bad
[MO] Update test for version.py
slyubimt Oct 27, 2020
19793b3
[MO] Add CMake install for Model Optimizer
slyubimt Oct 26, 2020
3cb829d
[MO] Update test for version.py
slyubimt Oct 27, 2020
bac9036
Merge branch 'features/cmake_install' of https://github.com/slyubimt/…
slyubimt Oct 27, 2020
e99c831
[MO] Add CMake install for Model Optimizer
slyubimt Oct 26, 2020
023f8b9
[MO] Update test for version.py
slyubimt Oct 27, 2020
771d4b8
Merge branch 'features/cmake_install' of https://github.com/slyubimt/…
slyubimt Oct 28, 2020
0fc6f8d
Merge branch 'master' into features/cmake_install
slyubimt Oct 28, 2020
0311cc3
Merge branch 'features/cmake_install' of https://github.com/slyubimt/…
slyubimt Oct 28, 2020
74e9698
Merge remote-tracking branch 'upstream/master' into master
slyubimt Oct 30, 2020
30161c8
Merge branch 'master' of https://github.com/slyubimt/openvino into ma…
slyubimt Nov 2, 2020
dafb9ef
Merge remote-tracking branch 'upstream/master' into master
slyubimt Nov 3, 2020
28867d4
Merge remote-tracking branch 'upstream/master' into master
slyubimt Nov 3, 2020
ee54fa3
Merge remote-tracking branch 'upstream/master' into master
slyubimt Nov 6, 2020
e71385f
Merge remote-tracking branch 'upstream/master' into master
slyubimt Nov 11, 2020
78ec374
[nGraph] Python API should be compiled and installed via CMake (41857)
slyubimt Nov 11, 2020
784f1e4
Merge remote-tracking branch 'upstream/master' into master
slyubimt Nov 17, 2020
838cb7f
Merge remote-tracking branch 'upstream/master' into features/cmake_in…
slyubimt Nov 17, 2020
c94dc6f
Refactored wheel setup script to build module using CMake
slyubimt Nov 17, 2020
1393618
Update build instructions
slyubimt Nov 17, 2020
3831a7d
Merge remote-tracking branch 'upstream/master' into master
slyubimt Nov 18, 2020
867dd97
Merge remote-tracking branch 'upstream/master' into features/cmake_in…
slyubimt Nov 18, 2020
bdd2051
Added USE_SOURCE_PERMISSIONS to cmake install
slyubimt Nov 18, 2020
dfb3dd2
Adjust CMake compiler flags conditions
slyubimt Nov 19, 2020
873203f
Merge remote-tracking branch 'upstream/master' into master
slyubimt Nov 19, 2020
baf28cd
Merge branch 'master' of https://github.com/slyubimt/openvino into ma…
slyubimt Nov 19, 2020
2e14744
fix CPack issue for CI build pipeline
slyubimt Nov 20, 2020
6bb548d
case insensitive option check
slyubimt Nov 20, 2020
79a6033
build only python API if ngraph_DIR provided
slyubimt Nov 23, 2020
b91fddd
fix lib extension for macOS
slyubimt Nov 24, 2020
7422f3d
-fixed style (flake8)
slyubimt Nov 24, 2020
09fe1f4
fix flake8 B006 check
slyubimt Nov 24, 2020
b43b426
add ngraph_DIR & remove unsed env. variables.
slyubimt Nov 25, 2020
d0ac91f
Reworked build & test instructions to make it more straightforward
slyubimt Nov 26, 2020
5379d33
remove unused CMake arguments for setup.py
slyubimt Nov 26, 2020
bf30710
make source dir condition more general
slyubimt Nov 26, 2020
c1a8700
Merge remote-tracking branch 'upstream/master' into features/cmake_in…
slyubimt Nov 26, 2020
9299e34
Update BUILDING.md
andrew-zaytsev Nov 27, 2020
16b9451
Merge remote-tracking branch 'upstream/master' into features/cmake_in…
slyubimt Nov 27, 2020
60d956c
Merge branch 'features/cmake_install_pyngraph' of https://github.com/…
slyubimt Nov 27, 2020
db3b8aa
Update BUILDING.md
andrew-zaytsev Nov 27, 2020
b24f5cc
Update BUILDING.md
andrew-zaytsev Nov 27, 2020
5bdb3cb
beautified instructions wording
slyubimt Nov 30, 2020
72a3373
Merge remote-tracking branch 'upstream/master' into master
slyubimt Nov 30, 2020
5b1618d
Merge remote-tracking branch 'upstream/master' into master
slyubimt Dec 1, 2020
80db2cf
Merge remote-tracking branch 'upstream/master' into master
slyubimt Dec 2, 2020
0291edf
Merge remote-tracking branch 'upstream/master' into master
slyubimt Dec 10, 2020
edaf16b
Merge branch 'master' into features/cmake_install_pyngraph
slyubimt Dec 10, 2020
cae9057
fix wheel build issue after sourcing setupvars
slyubimt Dec 11, 2020
26ed1c5
Merge remote-tracking branch 'upstream/master' into master
slyubimt Dec 14, 2020
4388a0b
Extend user options to build, install and develop commands
slyubimt Dec 22, 2020
d0787ff
Merge remote-tracking branch 'upstream/master' into master
slyubimt Dec 22, 2020
de4cdbf
Merge branch 'master' into features/cmake_install_pyngraph
slyubimt Dec 22, 2020
e06ec8d
Merge branch 'features/cmake_install_pyngraph' of https://github.com/…
slyubimt Dec 22, 2020
2dea25c
Merge remote-tracking branch 'upstream/master' into master
slyubimt Dec 24, 2020
67f3255
Merge branch 'master' into features/cmake_install_pyngraph
slyubimt Dec 24, 2020
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
131 changes: 63 additions & 68 deletions ngraph/python/BUILDING.md
Original file line number Diff line number Diff line change
@@ -1,45 +1,44 @@
# Building the Python API for nGraph
# Building the nGraph Python* API

You can build the nGraph Python API from sources by following instructions in this document. A Python wheel is a
portable package which will allow you to install nGraph in your Python distribution, or dedicated virtual environment.
This document provides the instructions for building the nGraph Python API from source on Linux, macOS and Windows 10 platforms.

## Build nGraph Python Wheels on Linux or MacOS
For each platform, you can build and install the API as a part of OpenVINO™ Toolkit or as a Python wheel.
A Python wheel is a portable package that allows you to install nGraph in your Python distribution, or dedicated virtual environment.

## Linux* and macOS*

### Prerequisites

In order to build the nGraph Python wheel, you will need to install a few packages.
To build the nGraph Python API, you need to install a few additional packages.

On Ubuntu 20.04 LTS you can use the following instructions to install the required packages, including Python and Cython.
On Ubuntu* 20.04 LTS you can use the following instructions to install the required packages, including Python and Cython.

apt install git wget build-essential cmake
apt install python3 python3-dev python3-pip python3-virtualenv python-is-python3

You can see a full working example on an Ubuntu environment used in our continuous environment in this
[Dockerfile](https://github.com/openvinotoolkit/openvino/blob/master/.ci/openvino-onnx/Dockerfile).

On MacOS you can use [Homebrew](https://brew.sh) to install required packages:
On macOS, you can use [Homebrew](https://brew.sh) to install required packages:

brew install cmake
brew install automake
brew install libtool
brew install python3

Install Cython in the Python installation, or virtualenv which you are planning to use:
Install Cython in the Python installation, or virtualenv that you are planning to use:

pip3 install cython

### Configure, build and install OpenVINO
### Configure and Build as a part of OpenVINO™ Toolkit on Linux and macOS

The following section will illustrate how to download, build and install OpenVINO in a workspace directory specified
by the `${MY_OPENVINO_BASEDIR}` variable. Let's start by setting this variable to a directory of your choice:
The following section illustrates how to build and install OpenVINO in a workspace directory using CMake.
The workspace directory is specified by the `${OPENVINO_BASEDIR}` variable. Set this variable to a directory of your choice:

export MY_OPENVINO_BASEDIR=/path/to/my/workspace
export OPENVINO_BASEDIR=/path/to/my/workspace

Now we can clone OpenVINO, configure it using `cmake` and build using `make`. Please note that we're disabling
the building of a few modules by setting the `ENABLE_*` flag to `OFF`. In order to build the OpenVINO Python APIs
Now you can clone the OpenVINO™ repository, configure it using `cmake` and build using `make`. Please note that we're disabling
the building of a few modules by setting the `ENABLE_*` flag to `OFF`. In order to build the OpenVINO Python APIs
set the mentioned flags to `ON`. Note the `CMAKE_INSTALL_PREFIX`, which defaults to `/usr/local/` if not set.

cd "${MY_OPENVINO_BASEDIR}"
cd "${OPENVINO_BASEDIR}"
git clone --recursive https://github.com/openvinotoolkit/openvino.git
mkdir openvino/build
cd openvino/build
Expand All @@ -51,64 +50,64 @@ set the mentioned flags to `ON`. Note the `CMAKE_INSTALL_PREFIX`, which defaults
-DENABLE_PYTHON=ON \
-DNGRAPH_PYTHON_BUILD_ENABLE=ON \
-DNGRAPH_ONNX_IMPORT_ENABLE=ON \
-DCMAKE_INSTALL_PREFIX="${MY_OPENVINO_BASEDIR}/openvino_dist"
-DCMAKE_INSTALL_PREFIX="${OPENVINO_BASEDIR}/openvino_dist"

make -j 4
make install

If you would like to use a specific version of Python, or use a virtual environment you can set the `PYTHON_EXECUTABLE`
variable. Examples:
The Python module is installed in the `${OPENVINO_BASEDIR}/openvino_dist/python/python<version>/` folder.
Set up the OpenVINO™ environment in order to add the module path to `PYTHONPATH`:

source ${OPENVINO_BASEDIR}/openvino_dist/bin/setupvars.sh

If you would like to use a specific version of Python, or use a virtual environment, you can set the `PYTHON_EXECUTABLE`
variable. For example:

```
-DPYTHON_EXECUTABLE=/path/to/venv/bin/python
-DPYTHON_EXECUTABLE=$(which python3.8)
```
```

### Build nGraph Python wheel
### Build an nGraph Python Wheel on Linux and macOS

When OpenVINO is built and installed, we can build the Python wheel by issuing the following command:
You can build the Python wheel running the following command:
slyubimt marked this conversation as resolved.
Show resolved Hide resolved

make python_wheel
cd "${OPENVINO_BASEDIR}/openvino/ngraph/python"
python3 setup.py bdist_wheel

Once completed, the wheel package should be located under the following path:

$ ls "${MY_OPENVINO_BASEDIR}/openvino/ngraph/python/dist/"
$ ls "${OPENVINO_BASEDIR}/openvino/ngraph/python/dist/"
ngraph_core-0.0.0-cp38-cp38-linux_x86_64.whl

You can now install the wheel in your Python environment:

cd "${MY_OPENVINO_BASEDIR}/openvino/ngraph/python/dist/"
cd "${OPENVINO_BASEDIR}/openvino/ngraph/python/dist/"
pip3 install ngraph_core-0.0.0-cp38-cp38-linux_x86_64.whl

#### What does `make python_wheel` do?

The `python_wheel` target automates a few steps, required to build the wheel package. You can recreate the process
manually by issuing the following commands:

cd "${MY_OPENVINO_BASEDIR}/openvino/ngraph/python"
git clone --branch v2.5.0 https://github.com/pybind/pybind11.git
export NGRAPH_CPP_BUILD_PATH="${MY_OPENVINO_BASEDIR}/openvino_dist"
python3 setup.py bdist_wheel


## Build nGraph Python Wheels on Windows
## Windows* 10

### Prerequisites

In order to build OpenVINO and the nGraph Python wheel on Windows, you will need to install Visual Studio and Python.
In order to build OpenVINO™ and the nGraph Python wheel on Windows, you need to install Microsoft Visual Studio* and Python.

Once Python is installed, you also need to install Cython using `pip install cython`.

Once Python is installed, you will also need to install Cython using `pip install cython`.
### Configure and Build as a Part of OpenVINO™ Toolkit on Windows

### Configure, build and install OpenVINO
The following section illustrates how to build and install OpenVINO™ in a workspace directory using CMake.
The workspace directory is specified by the `OPENVINO_BASEDIR` variable. Set this variable to a directory of your choice:

set OPENVINO_BASEDIR=/path/to/my/workspace

Configure the build with a `cmake` invocation similar to the following. Note that you'll need to set the `-G` and
`-DCMAKE_CXX_COMPILER` to match the version and location of your Visual Studio installation.
Configure the build with a `cmake` invocation similar to the following. Note that need to set `-G` and
`-DCMAKE_CXX_COMPILER` to match the version and location of your Microsoft Visual Studio installation.

```
cmake .. ^
-G"Visual Studio 16 2019" ^
-DCMAKE_BUILD_TYPE=Release ^
-DCMAKE_INSTALL_PREFIX="C:\temporary_install_dir" ^
-DCMAKE_INSTALL_PREFIX="%OPENVINO_BASEDIR%/openvino_dist" ^
-DENABLE_CLDNN=OFF ^
-DENABLE_OPENCV=OFF ^
-DENABLE_VPU=OFF ^
Expand All @@ -120,67 +119,63 @@ cmake .. ^
```

There are a couple of things to notice here. One is that the full path to the x64 version of
MSVC compiler has to be specified. This is because DNNL requires a 64-bit version and cmake may
MSVC compiler has to be specified. This is because DNNL requires a 64-bit version and `cmake` may
fail to detect it correctly.

The other equally important thing to note is that the temporary directory where the build is to be installed can be specified.
If the installation directory is not specified, the default location is `C:\Program Files\OpenVINO`.
This examples uses `C:\temporary_install_dir` however, a subdirectory of `openvino\build` works as well.
The final Python wheel will contain the contents of this temporary directory so it's important to set it.

If you want to specify an exact Python version, use the following options:
```
-DPYTHON_EXECUTABLE="C:\Program Files\Python37\python.exe" ^
-DPYTHON_LIBRARY="C:\Program Files\Python37\libs\python37.lib" ^
-DPYTHON_INCLUDE_DIR="C:\Program Files\Python37\include" ^
```

In order to build and install OpenVINO, build the `install` target:
In order to build and install OpenVINO, build the `install` target:

cmake --build . --target install --config Release -j 8

In this step OpenVINO will be built and installed to the directory specified above. You can
In this step, OpenVINO™ is built and installed to the directory specified above. You can
adjust the number of threads used in the building process to your machine's capabilities.

Set up the OpenVINO™ environment in order to add a module path to `PYTHONPATH`:

%OPENVINO_BASEDIR%\openvino_dist\bin\setupvars.bat

### Build an nGraph Python Wheel on Windows

Build the Python wheel package:
slyubimt marked this conversation as resolved.
Show resolved Hide resolved

cmake --build . --target python_wheel --config Release -j 8
cd "%OPENVINO_BASEDIR%/openvino/ngraph/python"
python setup.py bdist_wheel

The final wheel should be located in `ngraph\python\dist` directory.
The final wheel should be located in the `ngraph\python\dist` directory.

dir openvino\ngraph\python\dist\
10/09/2020 04:06 PM 4,010,943 ngraph_core-0.0.0-cp38-cp38-win_amd64.whl

## Run Tests

## Run tests

### Using a virtualenv (optional)
### Use a virtualenv (Optional)

You may wish to use a virutualenv for your installation.

$ virtualenv -p $(which python3) venv
$ source venv/bin/activate
(venv) $

### Install the nGraph wheel and other requirements
### Install the nGraph Wheel and Other Requirements

(venv) $ cd "${MY_OPENVINO_BASEDIR}/openvino/ngraph/python"
(venv) $ cd "${OPENVINO_BASEDIR}/openvino/ngraph/python"
(venv) $ pip3 install -r requirements.txt
(venv) $ pip3 install -r requirements_test.txt
(venv) $ pip3 install dist/ngraph_core-0.0.0-cp38-cp38-linux_x86_64.whl

### Run tests
### Run Tests

You should now be able to run tests.

You may need to run the `setupvars` script from OpenVINO to set paths to OpenVINO components.

source ${MY_OPENVINO_BASEDIR}/openvino/scripts/setupvars/setupvars.sh

The minimum requirement is to set the `PYTHONPATH` to include the Inference Engine Python API:
You may need to run the `setupvars` script from the OpenVINO™ Toolkit to set paths to OpenVINO™ components.

export PYTHONPATH="${MY_OPENVINO_BASEDIR}/openvino/bin/intel64/Release/lib/python_api/python3.8/":${PYTHONPATH}
pytest tests/
source ${OPENVINO_BASEDIR}/openvino_dist/bin/setupvars.sh

Now you can run tests using `pytest`:

Expand Down
81 changes: 60 additions & 21 deletions ngraph/python/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,28 +18,67 @@ cmake_minimum_required (VERSION 3.13)

project (pyngraph)

include(ExternalProject)

ExternalProject_Add(
pybind11
GIT_REPOSITORY "https://github.com/pybind/pybind11.git"
GIT_TAG "v2.5.0"
SOURCE_DIR "${CMAKE_CURRENT_BINARY_DIR}/pybind11"
CONFIGURE_COMMAND ""
BUILD_COMMAND ""
INSTALL_COMMAND ""
)
if(NOT DEFINED OpenVINO_MAIN_SOURCE_DIR)
find_package(InferenceEngineDeveloperPackage)
find_package(ngraph REQUIRED)
endif()

if(ngraph_FOUND)
message("ngraph version = {${ngraph_VERSION}}")
endif()

set(BUILD_PY_IN "${CMAKE_CURRENT_SOURCE_DIR}/build_wheel.py.in")
set(BUILD_PY "${CMAKE_CURRENT_BINARY_DIR}/build_wheel.py")
configure_file(${BUILD_PY_IN} ${BUILD_PY} @ONLY)
include(FetchContent)

add_custom_command(
DEPENDS pybind11
OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/dist/
POST_BUILD
WORKING_DIR ${CMAKE_CURRENT_BINARY_DIR}
COMMAND python ${BUILD_PY}
FetchContent_Declare(
pybind11
GIT_REPOSITORY "https://github.com/pybind/pybind11.git"
GIT_TAG "v2.5.0"
)

add_custom_target(python_wheel DEPENDS ngraph ${CMAKE_CURRENT_BINARY_DIR}/dist/)
FetchContent_GetProperties(pybind11)
if(NOT pybind11_POPULATED)
FetchContent_Populate(pybind11)
add_subdirectory(${pybind11_SOURCE_DIR} ${pybind11_BINARY_DIR})
endif()

find_package(Python COMPONENTS Interpreter Development REQUIRED)

if(PYTHON_FOUND)
set(PYTHON_VERSION python${PYTHON_VERSION_MAJOR}.${PYTHON_VERSION_MINOR})
message("Python version={${Python_VERSION}}")
else()
message(FATAL_ERROR "Python was not found!")
endif()

if(CMAKE_COMPILER_IS_GNUCXX)
add_compile_options(-fPIC)
endif()

if (APPLE)
add_link_options(-stdlib=libc++)
endif()

if (CMAKE_CXX_COMPILER_ID STREQUAL "MSVC")
#disable warning: This operator was deprecated and will be removed with v0 operation.
add_compile_options(/wd4996)
endif()

file(GLOB_RECURSE SOURCES src/pyngraph/*.cpp)
pybind11_add_module(_${PROJECT_NAME} MODULE ${SOURCES})
target_include_directories(_${PROJECT_NAME} PRIVATE src)
target_link_libraries(_${PROJECT_NAME} PRIVATE ngraph::ngraph ngraph::onnx_importer)

if(OpenVINO_MAIN_SOURCE_DIR OR InferenceEngineDeveloperPackage_FOUND)
ie_cpack_add_component(pyngraph_${PYTHON_VERSION})

install(TARGETS _${PROJECT_NAME}
DESTINATION python/${PYTHON_VERSION}
COMPONENT pyngraph_${PYTHON_VERSION})

install(DIRECTORY ${CMAKE_CURRENT_LIST_DIR}/src/ngraph
DESTINATION python/${PYTHON_VERSION}
COMPONENT pyngraph_${PYTHON_VERSION}
USE_SOURCE_PERMISSIONS)

ie_cpack(pyngraph_${PYTHON_VERSION})
endif()
Loading