Skip to content
This repository has been archived by the owner on Nov 17, 2023. It is now read-only.

[WIP] CMake NNPack support #9860

Closed
wants to merge 94 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
94 commits
Select commit Hold shift + click to select a range
d61e408
Added two functions to the C api:
Jan 26, 2018
af19a0a
update to latest version
Jan 29, 2018
dcf94e2
Merge branch 'master' into master
dabraude Feb 1, 2018
5782eed
changed to us spelling
dabraude Feb 1, 2018
b987833
Update c_api.cc
dabraude Feb 1, 2018
a206dc7
removing the initialise function
dabraude Feb 6, 2018
3c65037
removed initialize function and fixed bug with symbol loading
dabraude Feb 6, 2018
32987f1
sync with apache
dabraude Feb 13, 2018
fdff10b
removed extra function
dabraude Feb 20, 2018
226755c
Update c_api.h
dabraude Feb 20, 2018
b3ed2ba
changed submodule versions to match apache
dabraude Feb 21, 2018
44e9759
added NNPack and dependencies
dabraude Feb 21, 2018
0810fbf
added missing dependency
dabraude Feb 21, 2018
25e04c0
incremental update
dabraude Feb 21, 2018
db05b81
NNPack now compiling
dabraude Feb 21, 2018
6e9bfbb
enabled NNPack define, and fixed import paths
dabraude Feb 22, 2018
b2d7eb5
changed to reflect apache master
dabraude Feb 22, 2018
2f8ac5b
Update CONTRIBUTORS.md
dabraude Feb 22, 2018
3486c23
Merge branch 'master' into nnpack
dabraude Feb 22, 2018
e2cc244
Added two functions to the C api:
Jan 26, 2018
06363f5
changed to us spelling
dabraude Feb 1, 2018
3fffa33
Update c_api.cc
dabraude Feb 1, 2018
29ed230
removing the initialise function
dabraude Feb 6, 2018
695b88f
removed initialize function and fixed bug with symbol loading
dabraude Feb 6, 2018
e301fbd
removed extra function
dabraude Feb 20, 2018
1e688b3
Update c_api.h
dabraude Feb 20, 2018
a704907
added NNPack and dependencies
dabraude Feb 21, 2018
5c2e889
added missing dependency
dabraude Feb 21, 2018
83a527a
incremental update
dabraude Feb 21, 2018
d619f54
NNPack now compiling
dabraude Feb 21, 2018
104c3ea
enabled NNPack define, and fixed import paths
dabraude Feb 22, 2018
ea74a2d
changed to reflect apache master
dabraude Feb 22, 2018
e13ed8a
fixed merge issues
dabraude Feb 22, 2018
5959de5
removed NNPack dependency submodules
dabraude Feb 24, 2018
b1cd82b
Merge branch 'master' of https://github.com/dabraude/incubator-mxnet …
dabraude Feb 24, 2018
9866ec4
moved files to be similar to other backends
dabraude Feb 24, 2018
cd8e5a2
now compiling
dabraude Feb 25, 2018
31796cd
incremental update
dabraude Feb 25, 2018
a9b18a5
Added two functions to the C api:
Jan 26, 2018
c9ec707
changed to us spelling
dabraude Feb 1, 2018
9dcddd4
Update c_api.cc
dabraude Feb 1, 2018
383b42c
removing the initialise function
dabraude Feb 6, 2018
a5a0ee9
removed initialize function and fixed bug with symbol loading
dabraude Feb 6, 2018
886658a
removed extra function
dabraude Feb 20, 2018
7f79e66
Update c_api.h
dabraude Feb 20, 2018
ec724cf
changed to reflect apache master
dabraude Feb 22, 2018
57c0a33
removed initialize function and fixed bug with symbol loading
dabraude Feb 6, 2018
bf8dbc3
changed to reflect apache master
dabraude Feb 22, 2018
14f9f48
moved files to be similar to other backends
dabraude Feb 24, 2018
733114e
fixed conflicts
dabraude Feb 26, 2018
34572d2
Added two functions to the C api:
Jan 26, 2018
0eee65f
changed to us spelling
dabraude Feb 1, 2018
8119136
Update c_api.cc
dabraude Feb 1, 2018
5711e21
removing the initialise function
dabraude Feb 6, 2018
0a5288f
removed initialize function and fixed bug with symbol loading
dabraude Feb 6, 2018
79ade33
removed extra function
dabraude Feb 20, 2018
9c4df00
Update c_api.h
dabraude Feb 20, 2018
791995d
added NNPack and dependencies
dabraude Feb 21, 2018
8a105c7
added missing dependency
dabraude Feb 21, 2018
80e8141
incremental update
dabraude Feb 21, 2018
5b92773
NNPack now compiling
dabraude Feb 21, 2018
8d95a04
enabled NNPack define, and fixed import paths
dabraude Feb 22, 2018
d015d92
changed to reflect apache master
dabraude Feb 22, 2018
b071542
Added two functions to the C api:
Jan 26, 2018
dc570f4
changed to us spelling
dabraude Feb 1, 2018
d6d579f
Update c_api.cc
dabraude Feb 1, 2018
fb61233
removing the initialise function
dabraude Feb 6, 2018
f68546c
removed initialize function and fixed bug with symbol loading
dabraude Feb 6, 2018
c68f82c
removed extra function
dabraude Feb 20, 2018
0c82493
Update c_api.h
dabraude Feb 20, 2018
902e042
incremental update
dabraude Feb 21, 2018
7dd8fe5
enabled NNPack define, and fixed import paths
dabraude Feb 22, 2018
1a948a1
changed to reflect apache master
dabraude Feb 22, 2018
e04a594
removed NNPack dependency submodules
dabraude Feb 24, 2018
b427d51
moved files to be similar to other backends
dabraude Feb 24, 2018
8c1e6b3
now compiling
dabraude Feb 25, 2018
bc95032
incremental update
dabraude Feb 25, 2018
0d607d7
Added two functions to the C api:
Jan 26, 2018
c68e3f7
changed to us spelling
dabraude Feb 1, 2018
6edebcc
Update c_api.cc
dabraude Feb 1, 2018
f616b9a
removing the initialise function
dabraude Feb 6, 2018
15261ec
removed initialize function and fixed bug with symbol loading
dabraude Feb 6, 2018
0a1ec6a
removed extra function
dabraude Feb 20, 2018
d9efcda
Update c_api.h
dabraude Feb 20, 2018
3469537
changed to reflect apache master
dabraude Feb 22, 2018
a466d02
removed initialize function and fixed bug with symbol loading
dabraude Feb 6, 2018
4c29ec8
changed to reflect apache master
dabraude Feb 22, 2018
fcd5ec0
moved files to be similar to other backends
dabraude Feb 24, 2018
847bb18
merging
dabraude Feb 26, 2018
9c78f01
fixed merge
dabraude Feb 26, 2018
69adaca
Update CONTRIBUTORS.md
dabraude Feb 26, 2018
070fe2e
stripped down files that won't be used
dabraude Feb 26, 2018
05e176f
stripped down files that won't be used
dabraude Feb 26, 2018
c17a987
updated to latest NNPack and incorporated CMake suggestions
dabraude Feb 28, 2018
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
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,11 @@ CMakeFiles
cmake_install.cmake
lib

# Kate / Kdevelop files
*.kate-swp
*.kdev4


# Visual Studio Code
.vscode

Expand Down
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,6 @@
[submodule "3rdparty/cub"]
path = 3rdparty/cub
url = https://github.com/dmlc/cub
[submodule "3rdparty/nnpack/NNPACK"]
path = 3rdparty/NNPACK
url = https://github.com/Maratyszcza/NNPACK
1 change: 1 addition & 0 deletions 3rdparty/NNPACK
Submodule NNPACK added at 83af25
47 changes: 47 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ mxnet_option(USE_VTUNE "Enable use of Intel Amplifier XE (VTune)" OFF
mxnet_option(ENABLE_CUDA_RTC "Build with CUDA runtime compilation support" ON)
mxnet_option(INSTALL_EXAMPLES "Install the example source files." OFF)
mxnet_option(USE_SIGNAL_HANDLER "Print stack traces on segfaults." OFF)
mxnet_option(USE_NNPACK "Build with NNPack support." OFF)

if(USE_CUDA AND NOT USE_OLDCMAKECUDA)
message(STATUS "CMake version '${CMAKE_VERSION}' using generator '${CMAKE_GENERATOR}'")
Expand Down Expand Up @@ -551,6 +552,52 @@ if(NOT EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/nnvm/CMakeLists.txt")
list(APPEND mxnet_LINKER_LIBS ${nnvm_LINKER_LIBS})
endif()

# ---[ NNPack
if(USE_NNPACK)
if (USE_MKLDNN)
message(FATAL_ERROR "Either MKLDNN or NNPack can be enabled but not both.")
endif()
# Add in NNPack and its dependencies
set(NNPACK_SOURCE_DIR "${CMAKE_SOURCE_DIR}/3rdparty/NNPACK")
if (EXISTS "${NNPACK_SOURCE_DIR}")
if (GOOGLETEST_SOURCE_DIR AND EXISTS "${GOOGLETEST_SOURCE_DIR}")
set(GOOGLETEST_SOURCE_DIR "${GTEST_ROOT}" CACHE STRING "Google Test source directory")
endif()

# Disable NNPack internal testing
set(NNPACK_BUILD_TESTS OFF CACHE BOOL "")
set(NNPACK_BUILD_BENCHMARKS OFF CACHE BOOL "")

# Compile statically
set(NNPACK_LIBRARY_TYPE "static" CACHE STRING "")
set(PTHREADPOOL_LIBRARY_TYPE "static" CACHE STRING "")
set(CPUINFO_LIBRARY_TYPE "static" CACHE STRING "")

# put NNPack dependencies in appropriate folders, see NNPack
# for other options
set(CONFU_DEPENDENCIES_SOURCE_DIR "${NNPACK_SOURCE_DIR}/deps"
CACHE PATH "Confu-style dependencies source directory")
set(CONFU_DEPENDENCIES_BINARY_DIR "${CMAKE_BINARY_DIR}/3rdparty/NNPACK/deps"
CACHE PATH "Confu-style dependencies source directory")

add_subdirectory("${NNPACK_SOURCE_DIR}")
Copy link

@Maratyszcza Maratyszcza Feb 27, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd suggest to do set the following variables:

set(NNPACK_BUILD_TESTS OFF CACHE BOOL "")
set(NNPACK_BUILD_BENCHMARKS OFF CACHE BOOL "")
set(NNPACK_LIBRARY_TYPE "static" CACHE STRING "")
set(PTHREADPOOL_LIBRARY_TYPE "static" CACHE STRING "")
set(CPUINFO_LIBRARY_TYPE "static" CACHE STRING "")

NNPACK_BUILD_TESTS and NNPACK_BUILD_BENCHMARKS disabled building NNPACK's own tests and benchmarks (which are enabled by default), and setting NNPACK_LIBRARY_TYPE, PTHREADPOOL_LIBRARY_TYPE and CPUINFO_LIBRARY_TYPE as "static" would build these projects as static libraries even if BUILD_SHARED_LIBS is ON.

After add_subdirectory, you'd likely want to add

set_property(TARGET nnpack PROPERTY POSITION_INDEPENDENT_CODE ON)
set_property(TARGET pthreadpool PROPERTY POSITION_INDEPENDENT_CODE ON)
set_property(TARGET cpuinfo PROPERTY POSITION_INDEPENDENT_CODE ON)

to build nnpack, pthreadpool, and cpuinfo with -fPIC even through they are static libraries.


# compile with -fPIC
set_property(TARGET nnpack PROPERTY POSITION_INDEPENDENT_CODE ON)
set_property(TARGET pthreadpool PROPERTY POSITION_INDEPENDENT_CODE ON)
set_property(TARGET cpuinfo PROPERTY POSITION_INDEPENDENT_CODE ON)

include_directories(${NNPACK_SOURCE_DIR}/include)
#PTHREADPOOL_SOURCE_DIR is set in cache by NNPack
include_directories(${PTHREADPOOL_SOURCE_DIR}/include)
add_definitions(-DMXNET_USE_NNPACK=1)
set(NNPack_LINKER_LIBS nnpack)
list(APPEND mxnet_LINKER_LIBS ${NNPack_LINKER_LIBS})
else()
message("NNPack submodule not found.")
endif()
endif()

if(NOT MSVC)
# Only add c++11 flags and definitions after cuda compiling
add_definitions(-DDMLC_USE_CXX11)
Expand Down
3 changes: 0 additions & 3 deletions src/operator/convolution_v1.cc
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,6 @@
*/

#include "./convolution_v1-inl.h"
#if MXNET_USE_NNPACK == 1
#include "./nnpack/nnpack_convolution-inl.h"
#endif // MXNET_USE_NNPACK

namespace mxnet {
namespace op {
Expand Down
4 changes: 1 addition & 3 deletions src/operator/nn/convolution.cc
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,7 @@
#include "../elemwise_op_common.h"
#include "./mkldnn/mkldnn_ops-inl.h"
#include "./mkldnn/mkldnn_base-inl.h"
#if MXNET_USE_NNPACK == 1
#include "./nnpack/nnpack_convolution-inl.h"
#endif // MXNET_USE_NNPACK
#include "./nnpack/nnpack_ops-inl.h"

namespace mxnet {
namespace op {
Expand Down
4 changes: 1 addition & 3 deletions src/operator/nn/fully_connected.cc
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,7 @@
#include "./fully_connected-inl.h"
#include "./mkldnn/mkldnn_ops-inl.h"
#include "./mkldnn/mkldnn_base-inl.h"
#if MXNET_USE_NNPACK == 1
#include "./nnpack/nnpack_fully_connected-inl.h"
#endif // MXNET_USE_NNPACK
#include "./nnpack/nnpack_ops-inl.h"

namespace mxnet {
namespace op {
Expand Down
68 changes: 68 additions & 0 deletions src/operator/nn/nnpack/nnpack_ops-inl.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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.
*/

/*!
* \file nnpack_ops-inl.h
* \brief
* \author David Braude
*/

#ifndef MXNET_OPERATOR_NN_NNPACK_NNPACK_OPS_INL_H_
#define MXNET_OPERATOR_NN_NNPACK_NNPACK_OPS_INL_H_

#if MXNET_USE_NNPACK == 1

#include <mxnet/io.h>
#include <mxnet/base.h>
#include <mxnet/ndarray.h>
#include <mxnet/operator.h>
#include <mxnet/operator_util.h>
#include <dmlc/logging.h>
#include <dmlc/optional.h>
#include <vector>
#include <nnpack.h>

// TODO:
// Convolutional layer
// Inference-optimized forward propagation (nnp_convolution_inference)
// Training-optimized forward propagation (nnp_convolution_output)
// Training-optimized backward input gradient update (nnp_convolution_input_gradient)
// Training-optimized backward kernel gradient update (nnp_convolution_kernel_gradient)
// Fully-connected layer
// Inference-optimized forward propagation (nnp_fully_connected_inference and nnp_fully_connected_inference_f16f32 version for FP16 weights)
// Training-optimized forward propagation (nnp_fully_connected_output)
// Max pooling layer
// Forward propagation, both for training and inference, (nnp_max_pooling_output)
// ReLU layer (with parametrized negative slope)
// Forward propagation, both for training and inference, optionally in-place, (nnp_relu_output)
// Backward input gradient update (nnp_relu_input_gradient)

namespace mxnet {
namespace op {

/* For softmax */
void NNPACKSoftmaxForward(const nnvm::NodeAttrs& attrs, const OpContext &ctx,
const NDArray &in_data, const OpReqType &req,
const NDArray &out_data);

} // namespace op
} // namespace mxnet
#endif // MXNET_USE_MKLDNN == 1

#endif // MXNET_OPERATOR_NN_MKLDNN_MKLDNN_OPS_INL_H_
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,31 @@
*/

/*!
* Copyright (c) 2016 by Contributors
* \file nnpack_util.cc
* \file nnpack_softmax.cc
* \brief
* \author Wei Wu
* \author David Braude
*/

#if MXNET_USE_NNPACK == 1
#include "nnpack_util.h"
#include "../softmax-inl.h"
#include "./nnpack_ops-inl.h"


#if MXNET_USE_NNPACK == 1
namespace mxnet {
namespace op {

NNPACKInitialize nnpackinitialize;
void NNPACKSoftmaxForward(const nnvm::NodeAttrs& attrs, const OpContext &ctx,
const NDArray &in_data, const OpReqType &req,
const NDArray &out_data) {
const SoftmaxParam& param = nnvm::get<SoftmaxParam>(attrs.parsed);
// enum nnp_status nnp_softmax_output(
// size_t batch_size,
// size_t channels,
// const float input[],
// float output[],
// pthreadpool_t threadpool);
}

} // namespace op
} // namespace mxnet
#endif // MXNET_USE_NNPACK
} // namespace op
} // namespace mxnet
#endif
6 changes: 1 addition & 5 deletions src/operator/nn/pooling.cc
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,8 @@
*/
#include "../elemwise_op_common.h"
#include "./pooling-inl.h"
#if MXNET_USE_NNPACK == 1
#include "./nnpack/nnpack_pooling-inl.h"
#endif // MXNET_USE_NNPACK
#if MXNET_USE_MKLDNN == 1
// #include "./nnpack/nnpack_pooling-inl.h"
#include "./mkldnn/mkldnn_pooling-inl.h"
#endif // MXNET_USE_MKLDNN

namespace mxnet {
namespace op {
Expand Down
124 changes: 0 additions & 124 deletions src/operator/nnpack/nnpack_convolution-inl.h

This file was deleted.

Loading