Problems with LAPACK on Android

Hello! I’m trying to build libtorch for android with linear algebra support LAPACK. For building I’ve used this guide: https://github.com/pytorch/pytorch/tree/master/android and similar solution for iOS Problems with LAPACK on iOS

After adding flag CMAKE_ARGS+=("-DUSE_LAPACK=ON")
The compiler results in error linking LAPACK (some external function are not found)

> Task :fbjni:compileReleaseJavaWithJavac
Note: Some input files use unchecked or unsafe operations.
....
> Task :pytorch_android:externalNativeBuildRelease
Build multiple targets pytorch_jni_arm64-v8a fbjni_arm64-v8a
ninja: Entering directory 
....
[17/17] Linking CXX shared library ../../../../build/intermediates/cmake/release/obj/arm64-v8a/libpytorch_jni.so
FAILED: ../../../../build/intermediates/cmake/release/obj/arm64-v8a/libpytorch_jni.so
: && /home/dmitryv/Android/Sdk/ndk/20.0.5594570/toolchains/llvm/prebuilt/linux-x86_64/bin/clang++ --target=aarch64-none-linux-android21 --gcc-toolchain=/home/dmitryv/Android/Sdk/ndk/20.0.5594570/toolchains/llvm/prebuilt/linux-x86_64 --sysroot=/home/dmitryv/Android/Sdk/ndk/20.0.5594570/toolchains/llvm/prebuilt/linux-x86_64/sysroot -fPIC -g -DANDROID -fdata-sections -ffunction-sections -funwind-tables -fstack-protector-strong -no-canonical-prefixes -fno-addrsig -Wa,--noexecstack -Wformat -Werror=format-security   -O2 -DNDEBUG  -Wl,--exclude-libs,libgcc.a -Wl,--exclude-libs,libatomic.a -static-libstdc++ -Wl,--build-id -Wl,--warn-shared-textrel -Wl,--fatal-warnings -Wl,--no-undefined -Qunused-arguments -Wl,-z,noexecstack -shared -Wl,-soname,libpytorch_jni.so -o ../../../../build/intermediates/cmake/release/obj/arm64-v8a/libpytorch_jni.so CMakeFiles/pytorch_jni.dir/src/main/cpp/pytorch_jni_jit.cpp.o CMakeFiles/pytorch_jni.dir/src/main/cpp/pytorch_jni_common.cpp.o  ../../../../build/intermediates/cmake/release/obj/arm64-v8a/libfbjni.so -Wl,--gc-sections -Wl,--whole-archive ../../../../src/main/jniLibs/arm64-v8a/libtorch.a ../../../../src/main/jniLibs/arm64-v8a/libtorch_cpu.a -Wl,--no-whole-archive ../../../../src/main/jniLibs/arm64-v8a/libc10.a ../../../../src/main/jniLibs/arm64-v8a/libnnpack.a ../../../../src/main/jniLibs/arm64-v8a/libXNNPACK.a ../../../../src/main/jniLibs/arm64-v8a/libpytorch_qnnpack.a ../../../../src/main/jniLibs/arm64-v8a/libeigen_blas.a ../../../../src/main/jniLibs/arm64-v8a/libcpuinfo.a ../../../../src/main/jniLibs/arm64-v8a/libclog.a -landroid -llog -latomic -lm && :
../../../../src/main/jniLibs/arm64-v8a/libtorch_cpu.a(BatchLinearAlgebra.cpp.o): In function `void at::native::lapackSolve<std::__ndk1::complex<double> >(int, int, std::__ndk1::complex<double>*, int, int*, std::__ndk1::complex<double>*, int, int*)':
/storage/pytorch/build_android/../aten/src/ATen/native/BatchLinearAlgebra.cpp:131: undefined reference to `zgesv_'

Which library do I have to link?
Any thoughts would be appreciated!

Hello @kulikovv,
Sorry for delay with reply.

If you are ok with supporting at the moment only armeabi-v7a, arm64-v8a, you can try to use LAPACK implementation from Qualcomm Math Library.

I experimented with linking it to pytorch_android with CMAKE_ARGS+=("-DUSE_LAPACK=ON")
It linked successfully for me.

I prepared PR with small instructions and changes in android/pytorch_android/CMakeLists.txt:

Instructions from PR that I will duplicate here:

  1. Download Qualcomm Math Library https://developer.qualcomm.com/software/qualcomm-math-library (You might need to register new user to download it)
  2. Unpack it in ${PYTORCH_ROOT}/android/libs

QML can be used for armeabi-v7a, arm64-v8a android abis

  1. sh ./scripts/build_pytorch_android.sh arm64-v8a,armeabi-v7a

I have not checked runtime using of it, if you test it, please reply here if it works or not.

2 Likes

Thanks for your answer! I’ve tried your approach, you need also to include (in my case manually libQML.so into arr archive) For devices with arm32 architecture (armeabi-v7a) libQML works fine, but for arm64 the loading of libtorch.so hangs.

Currently we try to compile and statically link CLAPACK for linear algebra support. And also with arm32 everything is good, but in the case of arm64 - incompatible function arguments.

About the project: we use a PCA model inside or neural network as an regulariser it works pretty well, and right now we are deploying it on mobiles.

1 Like

Hi~ unfortunately, this web page for qualcomm math library seems to have been removed, do you know where can I get qualcomm math library somewhere else? thanks.

We end-up by fixing CLAPACK and linking it statically. There are a lot of errors in function arguments.