forked from tensorflow/ngraph-bridge
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCMakeLists.txt
328 lines (282 loc) · 11.2 KB
/
CMakeLists.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
# Copyright 2018-2019 Nervana Systems Inc.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# http://www.apache.org/licenses/LICENSE-2.0
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
cmake_minimum_required(VERSION 3.4)
project (ngraph_tensorflow_bridge CXX)
# set directory where the custom finders live
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake")
# set(CMAKE_CXX_COMPILER "clang++")
include(ExternalProject)
include(CMakeDependentOption)
include(cmake/sdl.cmake)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -Wall -Wno-comment -Wno-sign-compare")
# In order to compile ngraph-tf with memory leak detection, run `cmake` with `-DCMAKE_BUILD_TYPE=Sanitize`.
# N.B.: This *will* crash python unit tests because ngraph-tf will be loaded "too late" via `dlopen`,
# so only use this with C++ tests.
# (In theory using `LD_PRELOAD` should address the python issue, but it doesn't appear to work on OS X, at least.)
# If there are any memory leaks, then upon running the binary a report will be automatically generated.
SET(CMAKE_CXX_FLAGS_SANITIZE
"${CMAKE_CXX_FLAGS_DEBUG} -fsanitize=address -D_LIBCPP_HAS_NO_ASAN -fsanitize-address-use-after-scope"
CACHE STRING "Flags used by the C++ compiler during sanitized builds."
FORCE )
SET(CMAKE_C_FLAGS_SANITIZE
"${CMAKE_C_FLAGS_DEBUG} -fsanitize=address -D_LIBCPP_HAS_NO_ASAN -fsanitize-address-use-after-scope"
CACHE STRING "Flags used by the C compiler during sanitized builds."
FORCE )
SET(CMAKE_EXE_LINKER_FLAGS_SANITIZE
"${CMAKE_EXE_LINKER_FLAGS_DEBUG} -fsanitize=address -D_LIBCPP_HAS_NO_ASAN -fsanitize-address-use-after-scope"
CACHE STRING "Flags used for linking binaries during sanitized builds."
FORCE )
SET(CMAKE_SHARED_LINKER_FLAGS_SANITIZE
"${CMAKE_SHARED_LINKER_FLAGS_DEBUG} -fsanitize=address -D_LIBCPP_HAS_NO_ASAN -fsanitize-address-use-after-scope"
CACHE STRING "Flags used by the shared libraries linker during coverage builds."
FORCE )
MARK_AS_ADVANCED(
CMAKE_CXX_FLAGS_SANITIZE
CMAKE_C_FLAGS_SANITIZE
CMAKE_EXE_LINKER_FLAGS_SANITIZE
CMAKE_SHARED_LINKER_FLAGS_SANITIZE)
# These variables are undocumented but useful.
set(CMAKE_DISABLE_SOURCE_CHANGES ON)
set(CMAKE_DISABLE_IN_SOURCE_BUILD ON)
# Invoke a command to determine how many CPU cores we have, and set
# NUM_MAKE_PROCESSES accordingly so we know what number to pass to make -j.
if(APPLE)
set (PROCESSOR_COUNT_COMMAND sysctl -n hw.physicalcpu)
else()
set (PROCESSOR_COUNT_COMMAND nproc)
endif()
execute_process(
COMMAND ${PROCESSOR_COUNT_COMMAND}
RESULT_VARIABLE NPROC_RESULT
OUTPUT_VARIABLE NUM_MAKE_PROCESSES
OUTPUT_STRIP_TRAILING_WHITESPACE
)
if(NOT APPLE)
# FIXME: Doesn't work for Ubuntu
execute_process(COMMAND cat /etc/os-release
OUTPUT_VARIABLE LSB_RELEASE_ID_SHORT
OUTPUT_STRIP_TRAILING_WHITESPACE
)
string(REGEX MATCH "ID=\"([a-z])+\"" OS_VERSION "${LSB_RELEASE_ID_SHORT}")
string(REGEX MATCH "\"([a-z])+\"" OS_VERSION "${OS_VERSION}")
message("OS version is ${OS_VERSION}")
else()
# Handle the case for MacOS
# TBD
endif()
# Default to four jobs if the command fails.
if(NPROC_RESULT)
message (WARNING "Unable to detect number of processors. Building nGraph with make -j4.")
set(NUM_MAKE_PROCESSES 4)
endif()
# Need to setup the RPATH here - else it won't work.
# During installation, a Python pip package is created which when
# installed is located in the same level as the tensorflow directory
# site-packages/
# /ngraph
# libngraph_bridge.so
# ...
# /tensorflow
# libtensorflow_framework.so.1
# python/
# _pywrap....so
# Therefore we are setting two entries in the RPATH:
# 1. $ORIGIN/.
# 2. $ORIGIN/../tensorflow/
#
set(CMAKE_MACOSX_RPATH 1)
if(APPLE)
set(CMAKE_INSTALL_RPATH "@loader_path/;@loader_path/../tensorflow;")
elseif(DEFINED NGRAPH_TF_RPATH)
set(CMAKE_INSTALL_RPATH "\$ORIGIN:\$ORIGIN/../tensorflow:${NGRAPH_TF_RPATH}")
else()
set(CMAKE_INSTALL_RPATH "\$ORIGIN:\$ORIGIN/../tensorflow")
endif()
# Find TensorFlow
find_package(TensorFlow REQUIRED)
if (CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
set(NGRAPH_TF_CXX11_ABI "${TensorFlow_CXX_ABI}")
message( STATUS "nGraph-TensorFlow using CXX11 ABI: ${NGRAPH_TF_CXX11_ABI}" )
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -D_GLIBCXX_USE_CXX11_ABI=${NGRAPH_TF_CXX11_ABI}")
endif()
if(APPLE)
set(LIBNGRAPH_SO "libngraph.dylib")
else()
set(LIBNGRAPH_SO "libngraph.so")
endif(APPLE)
# Build options
option(USE_PRE_BUILT_NGRAPH "Use pre-built nGraph located in NGRAPH_ARTIFACTS_DIR" FALSE)
option(NGRAPH_ARTIFACTS_DIR "Where would nGraph be installed after build" FALSE)
option(UNIT_TEST_ENABLE "Control the building of unit tests" FALSE)
option(UNIT_TEST_TF_CC_DIR "Location where TensorFlow CC library is located" FALSE)
option(NGRAPH_DISTRIBUTED_ENABLE "Add distributed mode to the CPU backend" FALSE)
find_package(PlaidML CONFIG)
option(NGRAPH_PLAIDML_ENABLE "Build PlaidML backend" ${PLAIDML_FOUND})
# Validate the options
if (NGRAPH_PLAIDML_ENABLE)
if (NOT PLAIDML_FOUND)
message(FATAL_ERROR "Unable to find PlaidML; use \"pip install plaidml\" to install it.")
endif()
endif()
if(USE_PRE_BUILT_NGRAPH)
if(NOT NGRAPH_ARTIFACTS_DIR)
message(
FATAL_ERROR
"USE_PRE_BUILT_NGRAPH is ON but NGRAPH_ARTIFACTS_DIR is missing"
)
endif()
# Check if the path specified is ABSOLUTE or RELATVE
if (NOT IS_ABSOLUTE ${NGRAPH_ARTIFACTS_DIR})
set(NGRAPH_ARTIFACTS_DIR ${CMAKE_CURRENT_BINARY_DIR}/${NGRAPH_ARTIFACTS_DIR})
endif()
# Create absolute path for the directory
get_filename_component(
NGRAPH_ARTIFACTS_DIR
"${NGRAPH_ARTIFACTS_DIR}"
ABSOLUTE
)
if (NOT EXISTS ${NGRAPH_ARTIFACTS_DIR})
message(FATAL_ERROR
"nGraph artifacts directory doesn't exist: " ${NGRAPH_ARTIFACTS_DIR} )
endif()
else()
set(NGRAPH_ARTIFACTS_DIR ${CMAKE_CURRENT_BINARY_DIR}/ngraph/ngraph_dist)
endif()
message(STATUS "UNIT_TEST_TF_CC_DIR: ${TF_PRE_BUILT_LOCATION}")
if(UNIT_TEST_TF_CC_DIR)
# Check if the path specified is ABSOLUTE or RELATVE
if (NOT IS_ABSOLUTE ${UNIT_TEST_TF_CC_DIR})
set(UNIT_TEST_TF_CC_DIR ${CMAKE_CURRENT_BINARY_DIR}/${UNIT_TEST_TF_CC_DIR})
endif()
# Create absolute path for the directory
get_filename_component(
TF_PRE_BUILT_LOCATION
"${UNIT_TEST_TF_CC_DIR}"
ABSOLUTE
)
if (NOT EXISTS ${TF_PRE_BUILT_LOCATION})
message(FATAL_ERROR
"TensorFlow pre-built directory doesn't exist: " ${TF_PRE_BUILT_LOCATION} )
endif()
endif()
# Enable build target CPU features
if (NOT DEFINED NGRAPH_TARGET_ARCH)
set(
NGRAPH_TARGET_ARCH
native
CACHE STRING "Target CPU architecture to build for.")
endif()
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -march=${NGRAPH_TARGET_ARCH}")
if (NOT DEFINED NGRAPH_TUNE_ARCH)
set(
NGRAPH_TUNE_ARCH
native
CACHE STRING "Target CPU architecture to build for.")
endif()
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -mtune=${NGRAPH_TARGET_ARCH}")
message(STATUS "UNIT_TEST_ENABLE: ${UNIT_TEST_ENABLE}")
message(STATUS "NGRAPH_ARTIFACTS_DIR: ${NGRAPH_ARTIFACTS_DIR}")
message(STATUS "USE_PRE_BUILT_NGRAPH: ${USE_PRE_BUILT_NGRAPH}")
message(STATUS "NGRAPH_DISTRIBUTED_ENABLE: ${NGRAPH_DISTRIBUTED_ENABLE}")
message(STATUS "NGRAPH_PLAIDML_ENABLE: ${NGRAPH_PLAIDML_ENABLE}")
message(STATUS "NGRAPH_TARGET_ARCH: ${NGRAPH_TARGET_ARCH}")
message(STATUS "NGRAPH_TUNE_ARCH: ${NGRAPH_TUNE_ARCH}")
if(NGRAPH_DISTRIBUTED_ENABLE)
find_package(MPI REQUIRED)
add_definitions(-DNGRAPH_DISTRIBUTED)
include_directories(SYSTEM ${MPI_C_INCLUDE_PATH} ${MPI_CXX_INCLUDE_PATH})
link_directories(${MPI_C_LIBRARIES} ${MPI_CXX_LIBRARIES})
endif()
# Find and build ngraph - if not using pre-built one
if (NOT USE_PRE_BUILT_NGRAPH)
ExternalProject_Add(
ext_ngraph
GIT_REPOSITORY https://github.com/NervanaSystems/ngraph
GIT_TAG v0.25.0-rc.3
CMAKE_ARGS
-DNGRAPH_DISTRIBUTED_ENABLE=${NGRAPH_DISTRIBUTED_ENABLE}
-DNGRAPH_INSTALL_PREFIX=${NGRAPH_ARTIFACTS_DIR}
-DNGRAPH_USE_CXX_ABI=${TensorFlow_CXX_ABI}
-DNGRAPH_UNIT_TEST_ENABLE=FALSE
-DNGRAPH_TOOLS_ENABLE=${NGRAPH_TOOLS_ENABLE}
-DNGRAPH_DEX_ONLY=TRUE
-DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE}
-DCMAKE_CXX_COMPILER=${CMAKE_CXX_COMPILER}
-DNGRAPH_GPU_ENABLE=${NGRAPH_GPU_ENABLE}
-DNGRAPH_DEBUG_ENABLE=${NGRAPH_DEBUG_ENABLE}
-DNGRAPH_PLAIDML_ENABLE=${NGRAPH_PLAIDML_ENABLE}
-DNGRAPH_TARGET_ARCH=${NGRAPH_TARGET_ARCH}
-DNGRAPH_TUNE_ARCH=${NGRAPH_TUNE_ARCH}
TMP_DIR "${CMAKE_CURRENT_BINARY_DIR}/ngraph/tmp"
STAMP_DIR "${CMAKE_CURRENT_BINARY_DIR}/ngraph/stamp"
DOWNLOAD_DIR "${CMAKE_CURRENT_BINARY_DIR}/ngraph/download"
SOURCE_DIR "${CMAKE_CURRENT_BINARY_DIR}/ngraph/src"
BINARY_DIR "${CMAKE_CURRENT_BINARY_DIR}/ngraph/build"
BUILD_COMMAND ${CMAKE_MAKE_PROGRAM} -j ${NUM_MAKE_PROCESSES} ngraph
UPDATE_COMMAND ""
INSTALL_DIR "${CMAKE_INSTALL_PREFIX}"
)
endif()
set(NGRAPH_INSTALL_DIR ${NGRAPH_ARTIFACTS_DIR})
if(OS_VERSION STREQUAL "\"centos\"")
set(NGRAPH_IMPORTED_LOCATION ${NGRAPH_INSTALL_DIR}/lib64/${LIBNGRAPH_SO})
else()
set(NGRAPH_IMPORTED_LOCATION ${NGRAPH_INSTALL_DIR}/lib/${LIBNGRAPH_SO})
endif()
add_library(ngraph_lib SHARED IMPORTED)
set_target_properties(
ngraph_lib
PROPERTIES IMPORTED_LOCATION
${NGRAPH_IMPORTED_LOCATION}
)
add_dependencies(ngraph_lib ext_ngraph)
SET(BASEPATH "${CMAKE_SOURCE_DIR}")
INCLUDE_DIRECTORIES("${BASEPATH}")
# Add the directories to be built
add_subdirectory(third-party)
add_subdirectory(logging)
add_subdirectory(ngraph_bridge)
add_subdirectory(tools)
# The following targets depend on the Tensorflow source code directory
# Get the absolute file name for the source
get_filename_component(
TensorFlow_SRC_DIR
"${TF_SRC_DIR}"
ABSOLUTE
)
add_subdirectory(examples)
if (DEFINED TF_SRC_DIR)
message(STATUS "TensorFlow_SRC_DIR: ${TensorFlow_SRC_DIR}")
add_subdirectory(examples/cpp)
else()
message(
STATUS
"TensorFlow source directory not provided. "
"C++ Examples won't be built"
)
endif()
if (UNIT_TEST_ENABLE)
if (NOT DEFINED TF_SRC_DIR)
message(FATAL_ERROR "Provide TensorFlow source directory: -DTF_SRC_DIR=<directory>")
endif()
if (NOT EXISTS ${TensorFlow_SRC_DIR})
message(
STATUS
"TensorFlow source directory doesn't exist"
)
endif()
# Check if the path specified is ABSOLUTE or RELATVE
if (NOT IS_ABSOLUTE ${TF_SRC_DIR})
set(TF_SRC_DIR ${CMAKE_CURRENT_BINARY_DIR}/${TF_SRC_DIR})
endif()
add_subdirectory(test)
message(STATUS "unit tests enabled")
endif()