LAPACK library not found in compilation (Pytorch Mobile, Android, Java)

I think someone has the same issue, but the solution is not clear and the download link was removed. Is there an official document to solve the problem?

More details on what I’m facing. In my build.gradle, I tried both org.pytorch:pytorch_android_lite:1.9.0 and org.pytorch:pytorch_android_torchvision:1.9.0. But I got the same error when run forward:

RuntimeError: inverse: LAPACK library not found in compilation
        at org.pytorch.NativePeer.forward(Native Method)
        at org.pytorch.Module.forward(

Shouldn’t this problem be a bug? I don’t understand why the LAPACK is not in the official repo’s jar. Is the only way to solve it compiling PyTorch manually to include a LAPACK library (and the LAPACK library should be downloaded somewhere)?

I created an issue in GitHub too and hope we can get some response: Pytorch Mobile on Android, LAPACK library not found in compilation · Issue #61693 · pytorch/pytorch · GitHub


Yes, it seems that in pytorch we dont build with LAPACK for mobile. Hence the jar does not have lapack.

There are two alternatives I see.

  1. If your inverse is on small matrix like 3x3 you can do python implementation of it as part of your model.
  2. Build from source for mobile and add USE_LAPACK=1.

I think 1 can unblock you faster probably.

@IvanKobzarev who is our android side expert can add more context.

Thanks @kimishpatel . Unfortunately the matrix is not small and option 1 does not fit.

Is there any documentation on option 2? I can try but I guess we need to prepare a LAPACK library before build the PyTorch?

I would expect that you do not need to prepare anything. You can build pytorch from source for android but specify USE_LAPACK=1. This can be done in one of the scripts for building android package. I dont honestly know which one though. @IvanKobzarev can suggest more, or I can try to find it later.

How to specify USE_LAPACK=1? Should I add it to ENV, e.g. USE_LAPACK=1 ./scripts/ armeabi-v7a ?

I tried the above, used the two .aar files in lib, and still got the same LAPACK error:

 com.facebook.jni.CppException: inverse: LAPACK library not found in compilation
    Exception raised from apply_inverse at ../aten/src/ATen/native/BatchLinearAlgebra.cpp:958 (most recent call first):
    (no backtrace available)
        at org.pytorch.LiteNativePeer.forward(Native Method)
        at org.pytorch.Module.forward(

If I added changes as in [NOT_FOR_COMMIT][android] Pytorch android build with USE_LAPACK and QML lib by IvanKobzarev · Pull Request #35200 · pytorch/pytorch · GitHub

then I got an error

  [1/1] Linking CXX shared library ../../../../build/intermediates/cmake/release/obj/armeabi-v7a/                                                                 
  FAILED: ../../../../build/intermediates/cmake/release/obj/armeabi-v7a/                                                  
  : && /Users/ymiao/Library/Android/sdk/ndk/21.1.6352462/toolchains/llvm/prebuilt/darwin-x86_64/bin/clang++ --target=armv7-none-linux-androideabi21 --gcc-toolchain=/Users/ymiao/Library
/Android/sdk/ndk/21.1.6352462/toolchains/llvm/prebuilt/darwin-x86_64 --sysroot=/Users/ymiao/Library/Android/sdk/ndk/21.1.6352462/toolchains/llvm/prebuilt/darwin-x86_64/sysroot -fPIC -g
 -DANDROID -fdata-sections -ffunction-sections -funwind-tables -fstack-protector-strong -no-canonical-prefixes -D_FORTIFY_SOURCE=2 -march=armv7-a -mthumb -Wformat -Werror=format-securi
ty   -Oz -DNDEBUG  -Wl,--exclude-libs,libgcc.a -Wl,--exclude-libs,libgcc_real.a -Wl,--exclude-libs,libatomic.a -Wl,--build-id -Wl,--fatal-warnings -Wl,--exclude-libs,libunwind.a -Wl,--
no-undefined -Qunused-arguments -shared -Wl,-soname, -o ../../../../build/intermediates/cmake/release/obj/armeabi-v7a/ CMakeFiles/pytorch_jn
i_lite.dir/src/main/cpp/pytorch_jni_lite.cpp.o CMakeFiles/pytorch_jni_lite.dir/src/main/cpp/pytorch_jni_common.cpp.o  ../../../../build/intermediates/cmake/release/obj/armeabi-v7a/libf -Wl,--gc-sections -Wl,--whole-archive ../../../../src/main/jniLibs/armeabi-v7a/libtorch.a ../../../../src/main/jniLibs/armeabi-v7a/libtorch_cpu.a -Wl,--no-whole-archive ../../.
./../src/main/jniLibs/armeabi-v7a/libc10.a ../../../../src/main/jniLibs/armeabi-v7a/libnnpack.a ../../../../src/main/jniLibs/armeabi-v7a/libXNNPACK.a ../../../../src/main/jniLibs/armea
bi-v7a/libpytorch_qnnpack.a ../../../../src/main/jniLibs/armeabi-v7a/libpthreadpool.a ../../../../src/main/jniLibs/armeabi-v7a/libeigen_blas.a ../../../../src/main/jniLibs/armeabi-v7a/
libcpuinfo.a ../../../../src/main/jniLibs/armeabi-v7a/libclog.a -llibQML libVulkanWrapper.a -landroid -llog -ldl -latomic -lm && :
  /Users/ymiao/Library/Android/sdk/ndk/21.1.6352462/toolchains/llvm/prebuilt/darwin-x86_64/lib/gcc/arm-linux-androideabi/4.9.x/../../../../arm-linux-androideabi/bin/ld: error: cannot f
ind -llibQML
  ../../../../src/main/jniLibs/armeabi-v7a/libtorch_cpu.a(BatchLinearAlgebra.cpp.o):BatchLinearAlgebra.cpp:function void at::native::lapackLu<c10::complex<double> >(int, int, c10::comp
lex<double>*, int, int*, int*): error: undefined reference to 'zgetrf_'
  ../../../../src/main/jniLibs/armeabi-v7a/libtorch_cpu.a(BatchLinearAlgebra.cpp.o):BatchLinearAlgebra.cpp:function void at::native::lapackLu<c10::complex<float> >(int, int, c10::compl
ex<float>*, int, int*, int*): error: undefined reference to 'cgetrf_'

Would you please help on how to build from source with the LAPACK support? @kimishpatel @IvanKobzarev

Forgot mentioning in the above, the source are checked out from tag v1.9.0.