Skip to content

Commit 3bbd26c

Browse files
leezuMoisesHer
authored andcommitted
Refactor cpp-package CMakeLists.txt & add missing inference/imagenet_inference (apache#17835)
missing inference/imagenet_inference target caused nightly tests to fail.
1 parent 896d3af commit 3bbd26c

File tree

5 files changed

+118
-83
lines changed

5 files changed

+118
-83
lines changed

CMakeLists.txt

+4-7
Original file line numberDiff line numberDiff line change
@@ -298,10 +298,6 @@ if(USE_MKLDNN)
298298
set(INSTALL_MKLDNN ON)
299299
endif()
300300

301-
if(USE_CPP_PACKAGE)
302-
add_definitions(-DMXNET_USE_CPP_PACKAGE=1)
303-
endif()
304-
305301
# Allow Cuda compiles outside of src tree to find things in 'src' and 'include'
306302
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/include)
307303
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/src)
@@ -927,11 +923,12 @@ endif()
927923

928924
if(USE_CPP_PACKAGE)
929925
add_subdirectory(cpp-package)
926+
target_compile_definitions(mxnet PUBLIC MXNET_USE_CPP_PACKAGE=1)
927+
if(BUILD_CPP_EXAMPLES)
928+
add_subdirectory(example/image-classification/predict-cpp)
929+
endif()
930930
endif()
931931

932-
if(BUILD_CPP_EXAMPLES)
933-
add_subdirectory(example/image-classification/predict-cpp)
934-
endif()
935932
add_subdirectory(tests)
936933

937934
# ---[ Linter target

cpp-package/CMakeLists.txt

+45-18
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,52 @@
1-
if(USE_CPP_PACKAGE)
1+
# Licensed to the Apache Software Foundation (ASF) under one
2+
# or more contributor license agreements. See the NOTICE file
3+
# distributed with this work for additional information
4+
# regarding copyright ownership. The ASF licenses this file
5+
# to you under the Apache License, Version 2.0 (the
6+
# "License"); you may not use this file except in compliance
7+
# with the License. You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing,
12+
# software distributed under the License is distributed on an
13+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
# KIND, either express or implied. See the License for the
15+
# specific language governing permissions and limitations
16+
# under the License.
217

3-
set(CPP_PACKAGE_OP_H_HEADER ${CMAKE_CURRENT_LIST_DIR}/include/mxnet-cpp/op.h)
18+
cmake_minimum_required(VERSION 3.13)
19+
project(mxnet_cpp C CXX)
420

5-
if(MSVC)
6-
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /utf-8")
7-
endif(MSVC)
21+
add_library(mxnet_cpp INTERFACE)
822

9-
add_custom_target(
10-
cpp_package_op_h ALL
11-
BYPRODUCTS ${CPP_PACKAGE_OP_H_HEADER}
12-
MAIN_DEPENDENCY mxnet
13-
DEPENDS mxnet ${CMAKE_CURRENT_SOURCE_DIR}/scripts/OpWrapperGenerator.py
14-
COMMAND echo "Running: OpWrapperGenerator.py"
15-
COMMAND python OpWrapperGenerator.py $<TARGET_FILE:mxnet>
16-
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/scripts
17-
)
23+
set(CPP_PACKAGE_INCLUDE_DIR ${CMAKE_CURRENT_LIST_DIR}/include/)
24+
target_include_directories(mxnet_cpp INTERFACE "${CPP_PACKAGE_INCLUDE_DIR}")
25+
file(GLOB_RECURSE CPP_PACKAGE_HEADERS
26+
"${CPP_PACKAGE_INCLUDE_DIR}/*.h"
27+
"${CPP_PACKAGE_INCLUDE_DIR}/*.hpp")
28+
set(CPP_PACKAGE_OP_H_HEADER ${CMAKE_CURRENT_LIST_DIR}/include/mxnet-cpp/op.h)
29+
target_sources(mxnet_cpp INTERFACE ${CPP_PACKAGE_HEADERS} ${CPP_PACKAGE_OP_H_HEADER})
30+
target_link_libraries(mxnet_cpp INTERFACE mxnet ${mxnet_LINKER_LIBS})
1831

19-
if(BUILD_CPP_EXAMPLES)
20-
add_subdirectory(example)
21-
endif()
32+
add_custom_target(
33+
cpp_package_op_h ALL
34+
BYPRODUCTS ${CPP_PACKAGE_OP_H_HEADER}
35+
MAIN_DEPENDENCY mxnet
36+
DEPENDS mxnet ${CMAKE_CURRENT_SOURCE_DIR}/scripts/OpWrapperGenerator.py
37+
COMMAND echo "Running: OpWrapperGenerator.py"
38+
COMMAND python OpWrapperGenerator.py $<TARGET_FILE:mxnet>
39+
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/scripts
40+
)
41+
add_dependencies(mxnet_cpp cpp_package_op_h)
2242

23-
install(DIRECTORY include/ DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})
43+
if(MSVC)
44+
target_compile_options(mxnet_cpp INTERFACE "/utf-8")
45+
endif(MSVC)
2446

47+
if(BUILD_CPP_EXAMPLES)
48+
add_subdirectory(example)
49+
add_subdirectory(example/inference)
2550
endif()
51+
52+
install(DIRECTORY include/ DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})

cpp-package/example/CMakeLists.txt

+45-58
Original file line numberDiff line numberDiff line change
@@ -1,72 +1,59 @@
1-
if(NOT MSVC)
2-
set(UNITTEST_STATIC_LINK ON)
3-
endif()
4-
5-
set(CPP_EXAMPLE_LIBS
6-
${BEGIN_WHOLE_ARCHIVE} mxnet_static ${END_WHOLE_ARCHIVE}
7-
${BEGIN_WHOLE_ARCHIVE} dmlc ${END_WHOLE_ARCHIVE}
8-
${mxnet_LINKER_LIBS}
9-
)
10-
11-
set(CPP_PACKAGE_INCLUDE_DIR ${CMAKE_CURRENT_LIST_DIR}/../include/mxnet-cpp/)
1+
# Licensed to the Apache Software Foundation (ASF) under one
2+
# or more contributor license agreements. See the NOTICE file
3+
# distributed with this work for additional information
4+
# regarding copyright ownership. The ASF licenses this file
5+
# to you under the Apache License, Version 2.0 (the
6+
# "License"); you may not use this file except in compliance
7+
# with the License. You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing,
12+
# software distributed under the License is distributed on an
13+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
# KIND, either express or implied. See the License for the
15+
# specific language governing permissions and limitations
16+
# under the License.
1217

13-
set(CPPEX_DEPS cpp_package_op_h)
18+
# Explicitly set GENERATED property https://gitlab.kitware.com/cmake/cmake/issues/18399
19+
set_property(SOURCE ${CMAKE_CURRENT_LIST_DIR}/../include/mxnet-cpp/op.h PROPERTY GENERATED 1)
1420

15-
file(GLOB_RECURSE CPP_PACKAGE_HEADERS
16-
"${CPP_PACKAGE_INCLUDE_DIR}/*.h"
17-
"${CPP_PACKAGE_INCLUDE_DIR}/*.hpp"
18-
)
21+
add_executable(test_regress_label test_regress_label.cpp)
22+
target_link_libraries(test_regress_label mxnet_cpp)
1923

20-
if (MSVC)
21-
add_custom_target(
22-
cpp_package_deploy_library ALL
23-
DEPENDS mxnet
24-
COMMAND ${CMAKE_COMMAND} -E copy $<TARGET_FILE:mxnet> $<TARGET_FILE_DIR:mlp>
25-
)
26-
endif()
27-
28-
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/../include)
24+
add_executable(lenet lenet.cpp)
25+
target_link_libraries(lenet mxnet_cpp)
2926

30-
add_executable(test_regress_label test_regress_label.cpp ${CPP_PACKAGE_HAEDERS})
31-
target_link_libraries(test_regress_label ${CPP_EXAMPLE_LIBS})
32-
add_dependencies(test_regress_label ${CPPEX_DEPS})
27+
add_executable(lenet_with_mxdataiter lenet_with_mxdataiter.cpp)
28+
target_link_libraries(lenet_with_mxdataiter mxnet_cpp)
3329

34-
add_executable(lenet lenet.cpp ${CPP_PACKAGE_HEADERS})
35-
target_link_libraries(lenet ${CPP_EXAMPLE_LIBS})
36-
add_dependencies(lenet ${CPPEX_DEPS})
30+
add_executable(alexnet alexnet.cpp)
31+
target_link_libraries(alexnet mxnet_cpp)
3732

38-
add_executable(lenet_with_mxdataiter lenet_with_mxdataiter.cpp ${CPP_PACKAGE_HEADERS})
39-
target_link_libraries(lenet_with_mxdataiter ${CPP_EXAMPLE_LIBS})
40-
add_dependencies(lenet_with_mxdataiter ${CPPEX_DEPS})
33+
add_executable(charRNN charRNN.cpp)
34+
target_link_libraries(charRNN mxnet_cpp)
4135

42-
add_executable(alexnet alexnet.cpp ${CPP_PACKAGE_HEADERS})
43-
target_link_libraries(alexnet ${CPP_EXAMPLE_LIBS})
44-
add_dependencies(alexnet ${CPPEX_DEPS})
36+
add_executable(googlenet googlenet.cpp)
37+
target_link_libraries(googlenet mxnet_cpp)
4538

46-
add_executable(charRNN charRNN.cpp ${CPP_PACKAGE_HEADERS})
47-
target_link_libraries(charRNN ${CPP_EXAMPLE_LIBS})
48-
add_dependencies(charRNN ${CPPEX_DEPS})
39+
add_executable(inception_bn inception_bn.cpp)
40+
target_link_libraries(inception_bn mxnet_cpp)
4941

50-
add_executable(googlenet googlenet.cpp ${CPP_PACKAGE_HEADERS})
51-
target_link_libraries(googlenet ${CPP_EXAMPLE_LIBS})
52-
add_dependencies(googlenet ${CPPEX_DEPS})
42+
add_executable(mlp mlp.cpp)
43+
target_link_libraries(mlp mxnet_cpp)
5344

54-
add_executable(inception_bn inception_bn.cpp ${CPP_PACKAGE_HEADERS})
55-
target_link_libraries(inception_bn ${CPP_EXAMPLE_LIBS})
56-
add_dependencies(inception_bn ${CPPEX_DEPS})
45+
add_executable(mlp_cpu mlp_cpu.cpp)
46+
target_link_libraries(mlp_cpu mxnet_cpp)
5747

58-
add_executable(mlp mlp.cpp ${CPP_PACKAGE_HEADERS})
59-
target_link_libraries(mlp ${CPP_EXAMPLE_LIBS})
60-
add_dependencies(mlp ${CPPEX_DEPS})
48+
add_executable(mlp_gpu mlp_gpu.cpp)
49+
target_link_libraries(mlp_gpu mxnet_cpp)
6150

62-
add_executable(mlp_cpu mlp_cpu.cpp ${CPP_PACKAGE_HEADERS})
63-
target_link_libraries(mlp_cpu ${CPP_EXAMPLE_LIBS})
64-
add_dependencies(mlp_cpu ${CPPEX_DEPS})
51+
add_executable(resnet resnet.cpp)
52+
target_link_libraries(resnet mxnet_cpp)
6553

66-
add_executable(mlp_gpu mlp_gpu.cpp ${CPP_PACKAGE_HEADERS})
67-
target_link_libraries(mlp_gpu ${CPP_EXAMPLE_LIBS})
68-
add_dependencies(mlp_gpu ${CPPEX_DEPS})
6954

70-
add_executable(resnet resnet.cpp ${CPP_PACKAGE_HEADERS})
71-
target_link_libraries(resnet ${CPP_EXAMPLE_LIBS})
72-
add_dependencies(resnet ${CPPEX_DEPS})
55+
if(MSVC)
56+
add_custom_target(cpp_package_deploy_library ALL
57+
DEPENDS mxnet
58+
COMMAND ${CMAKE_COMMAND} -E copy $<TARGET_FILE:mxnet> $<TARGET_FILE_DIR:mlp>)
59+
endif()

cpp-package/example/charRNN.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,9 @@
2929
* Rename params file epoch number starts from zero.
3030
*/
3131

32+
#if _MSC_VER
3233
#pragma warning(disable: 4996) // VS2015 complains on 'std::copy' ...
34+
#endif
3335
#include <cstring>
3436
#include <iostream>
3537
#include <fstream>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Licensed to the Apache Software Foundation (ASF) under one
2+
# or more contributor license agreements. See the NOTICE file
3+
# distributed with this work for additional information
4+
# regarding copyright ownership. The ASF licenses this file
5+
# to you under the Apache License, Version 2.0 (the
6+
# "License"); you may not use this file except in compliance
7+
# with the License. You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing,
12+
# software distributed under the License is distributed on an
13+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
# KIND, either express or implied. See the License for the
15+
# specific language governing permissions and limitations
16+
# under the License.
17+
18+
# Explicitly set GENERATED property https://gitlab.kitware.com/cmake/cmake/issues/18399
19+
set_property(SOURCE ${CMAKE_CURRENT_LIST_DIR}/../../include/mxnet-cpp/op.h PROPERTY GENERATED 1)
20+
21+
add_executable(imagenet_inference "imagenet_inference.cpp")
22+
target_link_libraries(imagenet_inference mxnet_cpp)

0 commit comments

Comments
 (0)