Maratyszcza / NNPACK

Acceleration package for neural networks on multi-core CPUs
BSD 2-Clause "Simplified" License
1.68k stars 316 forks source link

cross compile for android on mac os #119

Closed NHZlX closed 6 years ago

NHZlX commented 7 years ago

the log is

xingzhaolong@xzl:~/third_party/SOURCE/NNPACK$ ndk-build 
[x86] Compile++      : convolution-inference-alexnet-test <= alexnet.cc
......

[x86] Compile        : nnpack_test_ukernels <= winograd-f6k3.c
[x86] StaticLibrary  : libnnpack_test_ukernels.a
[x86] Compile        : fourier_reference <= aos.c
In file included from jni/../jni/../src/ref/fft/aos.c:3:
jni/../include/nnpack/complex.h:3:10: error: 'complex.h' file not found with <angled> include; use "quotes" instead
#include <complex.h>
         ^~~~~~~~~~~
         "complex.h"
jni/../jni/../src/ref/fft/aos.c:12:22: error: use of undeclared identifier '_Complex_I'
        float _Complex w0 = CMPLXF(t[0 * t_stride], t[1 * t_stride]);
                            ^
jni/../include/nnpack/complex.h:6:39: note: expanded from macro 'CMPLXF'
        #define CMPLXF(real, imag) ((real) + _Complex_I * (imag))
                                             ^
jni/../jni/../src/ref/fft/aos.c:13:22: error: use of undeclared identifier '_Complex_I'
        float _Complex w1 = CMPLXF(t[2 * t_stride], t[3 * t_stride]);
                            ^
jni/../include/nnpack/complex.h:6:39: note: expanded from macro 'CMPLXF'
        #define CMPLXF(real, imag) ((real) + _Complex_I * (imag))
                                             ^
jni/../jni/../src/ref/fft/aos.c:18:20: warning: implicitly declaring library function 'crealf' with type 'float (_Complex float)'
      [-Wimplicit-function-declaration]
        f[0 * f_stride] = crealf(w0);
                          ^
jni/../jni/../src/ref/fft/aos.c:18:20: note: include the header <complex.h> or explicitly provide a declaration for 'crealf'
jni/../jni/../src/ref/fft/aos.c:19:20: warning: implicitly declaring library function 'cimagf' with type 'float (_Complex float)'
      [-Wimplicit-function-declaration]
        f[1 * f_stride] = cimagf(w0);
                          ^
jni/../jni/../src/ref/fft/aos.c:19:20: note: include the header <complex.h> or explicitly provide a declaration for 'cimagf'
jni/../jni/../src/ref/fft/aos.c:29:22: error: use of undeclared identifier '_Complex_I'
        float _Complex w0 = CMPLXF(t[0 * t_stride], t[1 * t_stride]);
                            ^
jni/../include/nnpack/complex.h:6:39: note: expanded from macro 'CMPLXF'
        #define CMPLXF(real, imag) ((real) + _Complex_I * (imag))
                                             ^
jni/../jni/../src/ref/fft/aos.c:30:22: error: use of undeclared identifier '_Complex_I'
        float _Complex w1 = CMPLXF(t[2 * t_stride], t[3 * t_stride]);
                            ^
jni/../include/nnpack/complex.h:6:39: note: expanded from macro 'CMPLXF'
        #define CMPLXF(real, imag) ((real) + _Complex_I * (imag))
                                             ^
........
                                             ^
fatal error: too many errors emitted, stopping now [-ferror-limit=]
2 warnings and 20 errors generated.
make: *** [obj/local/x86/objs/fourier_reference/jni/__/src/ref/fft/aos.o] Error 1

I feel that the way Ninja compiled is not transparent for me I meet the same problem in a docker linux env.

NHZlX commented 7 years ago

I try the cmake method.

Assume the nnpack directory is $NNPACK

step one: Download the android.toolchain.cmake to $NNPACK/

step two: Add the following code on the head of $NNAPCK/CMakeLists.txt

if(CMAKE_TOOLCHAIN_FILE)
get_filename_component(CMAKE_TOOLCHAIN_FILE_NAME ${CMAKE_TOOLCHAIN_FILE} NAME)
find_file(CMAKE_TOOLCHAIN_FILE ${CMAKE_TOOLCHAIN_FILE_NAME} PATHS ${CMAKE_SOURCE_DIR} NO_DEFAULT_PATH)
message(STATUS "CMAKE_TOOLCHAIN_FILE = ${CMAKE_TOOLCHAIN_FILE}")
endif()

step three(build armv7):

$ cd $NNPACK
$ mkdir android_build
$ cd android_build
$ cmake .. \
 -DCMAKE_TOOLCHAIN_FILE=../android.toolchain.cmake   \
-DANDROID_ABI="armeabi-v7a with NEON"  \
-DANDROID_NATIVE_API_LEVEL=21  \
-DANDROID_TOOLCHAIN_NAME=arm-linux-androideabi-4.9   \
-DANDROID_STL_FORCE_FEATURES=OFF \
-DCMAKE_INSTALL_PREFIX=./install  \

$ make -j `nproc`
$ make install
NHZlX commented 7 years ago

build armv8:

cmake .. \
 -DCMAKE_TOOLCHAIN_FILE=../android.toolchain.cmake   \
-DANDROID_ABI="arm64-v8a"  \
-DANDROID_NATIVE_API_LEVEL=21  \
-DANDROID_TOOLCHAIN_NAME=aarch64-linux-android-4.9   \
-DANDROID_STL_FORCE_FEATURES=OFF \
-DCMAKE_INSTALL_PREFIX=./install  \

but it has the following problem:

Scanning dependencies of target nnpack_reference_layers
[ 11%] Building C object CMakeFiles/nnpack_reference_layers.dir/src/ref/convolution-output.c.o
[ 13%] Building C object CMakeFiles/nnpack_reference_layers.dir/src/ref/convolution-input-gradient.c.o
[ 16%] Building C object CMakeFiles/nnpack_reference_layers.dir/src/ref/convolution-kernel.c.o
[ 18%] Building C object CMakeFiles/nnpack_reference_layers.dir/src/ref/fully-connected-output.c.o
[ 20%] Building C object CMakeFiles/nnpack_reference_layers.dir/src/ref/max-pooling-output.c.o
[ 23%] Building C object CMakeFiles/nnpack_reference_layers.dir/src/ref/softmax-output.c.o
[ 25%] Building C object CMakeFiles/nnpack_reference_layers.dir/src/ref/relu-output.c.o
In file included from /Users/xingzhaolong/third_party/SOURCE/NNPACK/include/nnpack/activations.h:25:0,
                 from /Users/xingzhaolong/third_party/SOURCE/NNPACK/src/ref/relu-output.c:3:
/Users/xingzhaolong/third_party/SOURCE/NNPACK/include/nnpack/arm_neon.h: In function 'vld1q_f32_f16':
/Users/xingzhaolong/third_party/SOURCE/NNPACK/include/nnpack/arm_neon.h:46:4: warning: implicit declaration of function 'vcvt_f32_f16' [-Wimplicit-function-declaration]
    return vcvt_f32_f16((float16x4_t) vld1_u16((const uint16_t*) address));
    ^
/Users/xingzhaolong/third_party/SOURCE/NNPACK/include/nnpack/arm_neon.h:46:25: error: 'float16x4_t' undeclared (first use in this function)
    return vcvt_f32_f16((float16x4_t) vld1_u16((const uint16_t*) address));
                         ^
/Users/xingzhaolong/third_party/SOURCE/NNPACK/include/nnpack/arm_neon.h:46:25: note: each undeclared identifier is reported only once for each function it appears in
/Users/xingzhaolong/third_party/SOURCE/NNPACK/include/nnpack/arm_neon.h:46:38: error: expected ')' before 'vld1_u16'
    return vcvt_f32_f16((float16x4_t) vld1_u16((const uint16_t*) address));
                                      ^
/Users/xingzhaolong/third_party/SOURCE/NNPACK/include/nnpack/arm_neon.h: In function 'vld1q_f32_f16_aligned':
/Users/xingzhaolong/third_party/SOURCE/NNPACK/include/nnpack/arm_neon.h:50:25: error: 'float16x4_t' undeclared (first use in this function)
    return vcvt_f32_f16((float16x4_t)
                         ^
/Users/xingzhaolong/third_party/SOURCE/NNPACK/include/nnpack/arm_neon.h:51:5: error: expected ')' before 'vld1_u16'
     vld1_u16((const uint16_t*) __builtin_assume_aligned(address, sizeof(float16x4_t))));
     ^
/Users/xingzhaolong/third_party/SOURCE/NNPACK/include/nnpack/arm_neon.h: In function 'vst1q_f16_f32':
/Users/xingzhaolong/third_party/SOURCE/NNPACK/include/nnpack/arm_neon.h:55:4: warning: implicit declaration of function 'vcvt_f16_f32' [-Wimplicit-function-declaration]
    vst1_u16((uint16_t*) address, (uint16x4_t) vcvt_f16_f32(vector));
    ^
/Users/xingzhaolong/third_party/SOURCE/NNPACK/include/nnpack/arm_neon.h:55:4: error: can't convert between vector values of different size
/Users/xingzhaolong/third_party/SOURCE/NNPACK/include/nnpack/arm_neon.h: In function 'vst1q_f16_f32_aligned':
/Users/xingzhaolong/third_party/SOURCE/NNPACK/include/nnpack/arm_neon.h:60:5: error: can't convert between vector values of different size
     (uint16x4_t) vcvt_f16_f32(vector));
     ^
make[2]: *** [CMakeFiles/nnpack_reference_layers.dir/src/ref/relu-output.c.o] Error 1
make[1]: *** [CMakeFiles/nnpack_reference_layers.dir/all] Error 2
make: *** [all] Error 2

help welcome.

Maratyszcza commented 7 years ago

Which NDK version do you use? Version r15c should work.

NHZlX commented 6 years ago

It's r14b, i will try r15c, thanks .

NHZlX commented 6 years ago

I test the r15c version NDK, the problem still exists.

Maratyszcza commented 6 years ago

The aarch64-linux-android-4.9 toolchain doesn't support NEON-FP16 intrinsics. Clang toolchain will work.

NHZlX commented 6 years ago

Clang toolchain works for me, many thanks