Incubator-mxnet: How to compile Amalgamation for android with NNPACK?

Created on 10 Feb 2017  路  2Comments  路  Source: apache/incubator-mxnet

Environment Info

Operating System: Ubuntu 14.04 64bit Desktop
Compiler: arm-linux-androideabi-clang++
MXNet version: v0.9.3
NDK vesion: android-ndk-r13b

Error Description

I have successfully compiled the amalgamation for android with NNPACK. The file jni_libmxnet_predict.so is copied to WhatsThis Android project properly.

however, I had an error when running the app

dlopen failed: cannot locate symbol "_ZN5mxnet2op16nnpackinitializeE"


Compiling Steps

here is what I have done:
1.build android standalone toolchain
cd android-ndk-r13b/build/tools
python make_standalone_toolchain.py --arch arm --api 21 --install-dir /home/pallas/android-toolchain --stl=libc++

2.compile OPENBLAS for android
cd ~/OpenBLAS
make clean
export PATH=$PATH:/home/pallas/android-toolchain/bin
make TARGET=ARMV7 HOSTCC=gcc CC=arm-linux-androideabi-gcc NOFORTRAN=1

3.compile NNPACK
in MY_NNPACK_ROOT/jni/Application.mk set APP_PLATFORM := android-21
run ndk-build

4.add these to the makefile in amalgamation, follows https://github.com/dmlc/mxnet/issues/4419
export OPENBLAS_ROOT=MY_OPENBLAS_ROOT
export NNPACK_ROOT=MY_NNPACK_ROOT

mxnet itself

CFLAGS += -I${MXNET_ROOT} #mxnetroot
CFLAGS += -I${MXNET_ROOT}/dmlc-core/include #mxnetroot/dmlc-core/include
CFLAGS += -I${MXNET_ROOT}/include #mxnetroot/include
CFLAGS += -I${MXNET_ROOT}/mshadow #mxnetroot/mshadow
CFLAGS += -I${MXNET_ROOT}/nnvm/include

nnpack:

CFLAGS += -DMXNET_USE_NNPACK=1
CFLAGS += -DMXNET_USE_NNPACK_NUM_THREADS=8
CFLAGS += -I${NNPACK_ROOT}/include
LDFLAGS += -L${NNPACK_ROOT}/obj/local/armeabi-v7a

LDFLAGS += -lnnpack -lpthreadpool -lnnpack_ukernels -lnnpack_reference -lgtest -lfp16_utils -lbench_utils -lcpufeatures

LDFLAGS += -lnnpack -lpthreadpool -lnnpack_ukernels -lcpufeatures

nnpack dependence googletest:

CFLAGS += -I${NNPACK_ROOT}/third-party/gtest-1.7.0/include
CFLAGS += -I${NNPACK_ROOT}/third-party/gtest-1.7.0

nnpack dependence pthreadpool:

CFLAGS += -I${NNPACK_ROOT}/third-party/pthreadpool/include
CFLAGS += -I${NNPACK_ROOT}/third-party/FXdiv/include

other define used

CFLAGS += -DMSHADOW_STAND_ALONE=1

5.add these to Amalgamation.py the line 123

if MXNET_USE_NNPACK == 1

include "src/operator/nnpack/nnpack_convolution-inl.h"

endif // MXNET_USE_NNPACK

define fopen64 std::fopen

6.nnvm issues, follows https://github.com/dmlc/mxnet/issues/4783
nnvm/amalgamation/Makefile
line 2: change to
DEFS+=-DMSHADOW_USE_CUDA=0 -DMSHADOW_USE_MKL=0 -DMSHADOW_RABIT_PS=0 -DMSHADOW_DIST_PS=0 -DMSHADOW_USE_SSE=0 -DDMLC_LOG_STACK_TRACE=0 -DMSHADOW_FORCE_STREAM -DMXNET_USE_OPENCV=0 -DMXNET_PREDICT_ONLY=1 -DDISABLE_OPENMP=1
export CFLAGS = -std=c++11 -Wall -O3 -Wno-unknown-pragmas -funroll-loops -Iinclude -fPIC $(DEFS)

7.compile amalgamation
export PATH=$PATH:my-android-toolchain/bin
export CC=arm-linux-androideabi-clang
export CXX=arm-linux-androideabi-clang++
make clean
make ANDROID=1

8.copy jni_libmxnet_predict.so and libc++_shared.so to android project


Is this a ndk version mismatch problem?
The libmxnet_predict.so works fine without NNPACK speedup, if I do not add step 3 and 4.
I confirm the ndk-build is run from android-ndk-r13b
my os is Ubuntu14.04.

Most helpful comment

I fixed this problem by adding a line in mxnet_predict0.cc

include "src/operator/nnpack/nnpack_util.cc"

Hope this message may help someone.
mxnet version:


commit 3d78151a5078aa4d11eded4a79c86f61de926756
Author: Eric Junyuan Xie piiswrong@users.noreply.github.com
Date: Sun Feb 12 23:14:59 2017 -0800

All 2 comments

mxnet::op::nnpackinitialize is defined at mxnet/src/operator/nnpack/nnpack_util.h.
It seems it is not included. How to fix that?

I fixed this problem by adding a line in mxnet_predict0.cc

include "src/operator/nnpack/nnpack_util.cc"

Hope this message may help someone.
mxnet version:


commit 3d78151a5078aa4d11eded4a79c86f61de926756
Author: Eric Junyuan Xie piiswrong@users.noreply.github.com
Date: Sun Feb 12 23:14:59 2017 -0800

Was this page helpful?
0 / 5 - 0 ratings