Skip to content

Commit 9d4425d

Browse files
authored
Merge pull request #14227 from baojun-nervana/intel/ngraph_cmake
Adding Intel nGraph build
2 parents 8f9bfad + 5d20c42 commit 9d4425d

File tree

4 files changed

+110
-0
lines changed

4 files changed

+110
-0
lines changed

CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ option(WITH_GPU "Compile PaddlePaddle with NVIDIA GPU" ${CUDA_F
4141
option(WITH_AMD_GPU "Compile PaddlePaddle with AMD GPU" OFF)
4242
option(WITH_AVX "Compile PaddlePaddle with AVX intrinsics" ${AVX_FOUND})
4343
option(WITH_MKL "Compile PaddlePaddle with MKL support." ${AVX_FOUND})
44+
option(WITH_NGRAPH "Compile PaddlePaddle with nGraph support." OFF)
4445
option(WITH_DSO "Compile PaddlePaddle with dynamic linked CUDA" ON)
4546
option(WITH_TESTING "Compile PaddlePaddle with unit testing" OFF)
4647
option(WITH_SWIG_PY "Compile PaddlePaddle with inference api" ON)
@@ -103,6 +104,8 @@ if(ANDROID OR IOS)
103104
"Disable RDMA when cross-compiling for Android and iOS" FORCE)
104105
set(WITH_MKL OFF CACHE STRING
105106
"Disable MKL when cross-compiling for Android and iOS" FORCE)
107+
set(WITH_NGRAPH OFF CACHE STRING
108+
"Disable nGraph when cross-compiling for Android and iOS" FORCE)
106109
set(WITH_GOLANG OFF CACHE STRING
107110
"Disable golang when cross-compiling for Android and iOS" FORCE)
108111

@@ -171,6 +174,7 @@ include(external/protobuf) # download, build, install protobuf
171174
include(external/python) # download, build, install python
172175
include(external/openblas) # download, build, install openblas
173176
include(external/mkldnn) # download, build, install mkldnn
177+
include(external/ngraph) # download, build, install nGraph
174178
include(external/swig) # download, build, install swig
175179
include(external/boost) # download boost
176180
include(external/any) # download libn::any

cmake/external/ngraph.cmake

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
# Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
add_library(ngraph INTERFACE)
16+
17+
IF(WIN32 OR APPLE)
18+
MESSAGE(WARNING
19+
"Windows or Mac is not supported with nGraph in Paddle yet."
20+
"Force WITH_NGRAPH=OFF")
21+
SET(WITH_NGRAPH OFF CACHE STRING "Disable nGraph in Windows and MacOS" FORCE)
22+
ENDIF()
23+
24+
IF(${WITH_NGRAPH} AND NOT ${WITH_MKLDNN})
25+
MESSAGE(WARNING
26+
"nGraph needs mkl-dnn to be enabled."
27+
"Force WITH_NGRAPH=OFF")
28+
SET(WITH_NGRAPH OFF CACHE STRING "Disable nGraph if mkl-dnn is disabled" FORCE)
29+
ENDIF()
30+
31+
IF(NOT ${WITH_NGRAPH})
32+
return()
33+
ENDIF()
34+
35+
INCLUDE(ExternalProject)
36+
37+
SET(NGRAPH_PROJECT "extern_ngraph")
38+
SET(NGRAPH_VERSION "0.9")
39+
SET(NGRAPH_GIT_TAG "f9fd9d4cc318dc59dd4b68448e7fbb5f67a28bd0")
40+
SET(NGRAPH_SOURCES_DIR ${THIRD_PARTY_PATH}/ngraph)
41+
SET(NGRAPH_INSTALL_DIR ${THIRD_PARTY_PATH}/install/ngraph)
42+
SET(NGRAPH_INC_DIR ${NGRAPH_INSTALL_DIR}/include)
43+
SET(NGRAPH_SHARED_LIB_NAME libngraph.so.${NGRAPH_VERSION})
44+
SET(NGRAPH_CPU_LIB_NAME libcpu_backend.so)
45+
SET(NGRAPH_TBB_LIB_NAME libtbb.so.2)
46+
SET(NGRAPH_GIT_REPO "https://github.com/NervanaSystems/ngraph.git")
47+
48+
ExternalProject_Add(
49+
${NGRAPH_PROJECT}
50+
${EXTERNAL_PROJECT_LOG_ARGS}
51+
DEPENDS ${MKLDNN_PROJECT} ${MKLML_PROJECT}
52+
GIT_REPOSITORY ${NGRAPH_GIT_REPO}
53+
GIT_TAG ${NGRAPH_GIT_TAG}
54+
PREFIX ${NGRAPH_SOURCES_DIR}
55+
UPDATE_COMMAND ""
56+
CMAKE_ARGS -DCMAKE_INSTALL_PREFIX=${NGRAPH_INSTALL_DIR}
57+
CMAKE_ARGS -DNGRAPH_UNIT_TEST_ENABLE=FALSE
58+
CMAKE_ARGS -DNGRAPH_TOOLS_ENABLE=FALSE
59+
CMAKE_ARGS -DNGRAPH_INTERPRETER_ENABLE=FALSE
60+
CMAKE_ARGS -DNGRAPH_DEX_ONLY=TRUE
61+
CMAKE_ARGS -DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE}
62+
CMAKE_ARGS -DMKLDNN_INCLUDE_DIR=${MKLDNN_INC_DIR}
63+
CMAKE_ARGS -DMKLDNN_LIB_DIR=${MKLDNN_INSTALL_DIR}/lib
64+
)
65+
66+
if(UNIX AND NOT APPLE)
67+
include(GNUInstallDirs)
68+
SET(NGRAPH_LIB_DIR ${NGRAPH_INSTALL_DIR}/${CMAKE_INSTALL_LIBDIR})
69+
else()
70+
SET(NGRAPH_LIB_DIR ${NGRAPH_INSTALL_DIR}/lib)
71+
endif()
72+
MESSAGE(STATUS "nGraph lib will be installed at: ${NGRAPH_LIB_DIR}")
73+
74+
SET(NGRAPH_SHARED_LIB ${NGRAPH_LIB_DIR}/${NGRAPH_SHARED_LIB_NAME})
75+
SET(NGRAPH_CPU_LIB ${NGRAPH_LIB_DIR}/${NGRAPH_CPU_LIB_NAME})
76+
SET(NGRAPH_TBB_LIB ${NGRAPH_LIB_DIR}/${NGRAPH_TBB_LIB_NAME})
77+
78+
# Workaround for nGraph expecting mklml to be in mkldnn install directory.
79+
ExternalProject_Add_Step(
80+
${NGRAPH_PROJECT}
81+
PrepareMKL
82+
COMMAND ${CMAKE_COMMAND} -E create_symlink ${MKLML_LIB} ${MKLDNN_INSTALL_DIR}/lib/libmklml_intel.so
83+
COMMAND ${CMAKE_COMMAND} -E create_symlink ${MKLML_IOMP_LIB} ${MKLDNN_INSTALL_DIR}/lib/libiomp5.so
84+
DEPENDEES download
85+
DEPENDERS configure
86+
)
87+
88+
add_dependencies(ngraph ${NGRAPH_PROJECT})
89+
target_compile_definitions(ngraph INTERFACE -DPADDLE_WITH_NGRAPH)
90+
target_include_directories(ngraph INTERFACE ${NGRAPH_INC_DIR})
91+
target_link_libraries(ngraph INTERFACE ${NGRAPH_SHARED_LIB})
92+
LIST(APPEND external_project_dependencies ngraph)

paddle/scripts/paddle_build.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@ function cmake_gen() {
139139
-DWITH_AMD_GPU=${WITH_AMD_GPU:-OFF}
140140
-DWITH_DISTRIBUTE=${WITH_DISTRIBUTE:-OFF}
141141
-DWITH_MKL=${WITH_MKL:-ON}
142+
-DWITH_NGRAPH=${WITH_NGRAPH:-OFF}
142143
-DWITH_AVX=${WITH_AVX:-OFF}
143144
-DWITH_GOLANG=${WITH_GOLANG:-OFF}
144145
-DCUDA_ARCH_NAME=${CUDA_ARCH_NAME:-All}
@@ -171,6 +172,7 @@ EOF
171172
-DWITH_AMD_GPU=${WITH_AMD_GPU:-OFF} \
172173
-DWITH_DISTRIBUTE=${WITH_DISTRIBUTE:-OFF} \
173174
-DWITH_MKL=${WITH_MKL:-ON} \
175+
-DWITH_NGRAPH=${WITH_NGRAPH:-OFF} \
174176
-DWITH_AVX=${WITH_AVX:-OFF} \
175177
-DWITH_GOLANG=${WITH_GOLANG:-OFF} \
176178
-DCUDA_ARCH_NAME=${CUDA_ARCH_NAME:-All} \

python/setup.py.in

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,18 @@ if '${CMAKE_BUILD_TYPE}' == 'Release':
174174
raise Exception("patch libmkldnn.so failed, command: %s" % command)
175175
package_data['paddle.libs']+=['libmkldnn.so.0']
176176
shutil.copy('${MKLDNN_SHARED_LIB}', libs_path)
177+
if '${WITH_NGRAPH}' == 'ON':
178+
if '${CMAKE_BUILD_TYPE}' == 'Release':
179+
# only change rpath in Release mode.
180+
command = "patchelf --set-rpath '$ORIGIN/' ${NGRAPH_SHARED_LIB}"
181+
if os.system(command) != 0:
182+
raise Exception("patch ${NGRAPH_SHARED_LIB_NAME} failed, command: %s" % command)
183+
shutil.copy('${NGRAPH_SHARED_LIB}', libs_path)
184+
shutil.copy('${NGRAPH_CPU_LIB}', libs_path)
185+
shutil.copy('${NGRAPH_TBB_LIB}', libs_path)
186+
package_data['paddle.libs']+=['${NGRAPH_SHARED_LIB_NAME}',
187+
'${NGRAPH_CPU_LIB_NAME}',
188+
'${NGRAPH_TBB_LIB_NAME}']
177189
# remove unused paddle/libs/__init__.py
178190
os.remove(libs_path+'/__init__.py')
179191
package_dir['paddle.libs']=libs_path

0 commit comments

Comments
 (0)