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

username: yingbo
cooked: <p>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?</p>
<p>More details on what I’m facing. In my <code>build.gradle</code>, I tried both <code>org.pytorch:pytorch_android_lite:1.9.0</code> and <code>org.pytorch:pytorch_android_torchvision:1.9.0</code>. But I got the same error when run <code>forward</code>:</p>
<pre><code class="lang-auto">RuntimeError: inverse: LAPACK library not found in compilation
    
        at org.pytorch.NativePeer.forward(Native Method)
        at org.pytorch.Module.forward(Module.java:52)
</code></pre>
<p>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)?</p>
<p>I created an issue in GitHub too and hope we can get some response: <a href="https://github.com/pytorch/pytorch/issues/61693" class="inline-onebox" rel="noopener nofollow ugc">Pytorch Mobile on Android, LAPACK library not found in compilation · Issue #61693 · pytorch/pytorch · GitHub</a></p>
<p>Thanks~!</p>

username: kimishpatel
cooked: <p>Yes, it seems that in pytorch we dont build with LAPACK for mobile. Hence the jar does not have lapack.</p>
<p>There are two alternatives I see.</p>
<ol>
<li>If your inverse is on small matrix like 3x3 you can do python implementation of it as part of your model.</li>
<li>Build from source for mobile and add <code>USE_LAPACK=1</code>.</li>
</ol>
<p>I think 1 can unblock you faster probably.</p>
<p><a class="mention" href="/u/ivankobzarev">@IvanKobzarev</a> who is our android side expert can add more context.</p>

username: yingbo
cooked: <p>Thanks <a class="mention" href="/u/kimishpatel">@kimishpatel</a> . Unfortunately the matrix is not small and option 1 does not fit.</p>
<p>Is there any documentation on option 2? I can try but I guess we need to prepare a LAPACK library before build the PyTorch?</p>

username: kimishpatel
cooked: <p>I would expect that you do not need to prepare anything. You can build pytorch from source for android but specify <code>USE_LAPACK=1</code>. This can be done in one of the scripts for building android package. I dont honestly know which one though. <a class="mention" href="/u/ivankobzarev">@IvanKobzarev</a> can suggest more, or I can try to find it later.</p>

username: yingbo
cooked: <p>How to specify <code>USE_LAPACK=1</code>? Should I add it to ENV, e.g. <code>USE_LAPACK=1 ./scripts/build_pytorch_android.sh armeabi-v7a  </code>?</p>
<p>I tried the above, used the two .aar files in lib, and still got the same LAPACK error:</p>
<pre><code class="lang-auto"> com.facebook.jni.CppException: inverse: LAPACK library not found in compilation
  
      debug_handle:-1
      
    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(Module.java:52)
</code></pre>
<p>If I added changes as in <a href="https://github.com/pytorch/pytorch/pull/35200/files" class="inline-onebox" rel="noopener nofollow ugc">[NOT_FOR_COMMIT][android] Pytorch android build with USE_LAPACK and QML lib by IvanKobzarev · Pull Request #35200 · pytorch/pytorch · GitHub</a></p>
<p>then I got an error</p>
<pre><code class="lang-auto">  [1/1] Linking CXX shared library ../../../../build/intermediates/cmake/release/obj/armeabi-v7a/libpytorch_jni_lite.so                                                                 
  FAILED: ../../../../build/intermediates/cmake/release/obj/armeabi-v7a/libpytorch_jni_lite.so                                                  
  : &amp;&amp; /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,libpytorch_jni_lite.so -o ../../../../build/intermediates/cmake/release/obj/armeabi-v7a/libpytorch_jni_lite.so 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
bjni.so -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 &amp;&amp; :
  /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&lt;c10::complex&lt;double&gt; &gt;(int, int, c10::comp
lex&lt;double&gt;*, 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&lt;c10::complex&lt;float&gt; &gt;(int, int, c10::compl
ex&lt;float&gt;*, int, int*, int*): error: undefined reference to 'cgetrf_'
.....
</code></pre>
<p>Would you please help on how to build from source with the LAPACK support? <a class="mention" href="/u/kimishpatel">@kimishpatel</a>  <a class="mention" href="/u/ivankobzarev">@IvanKobzarev</a></p>

username: yingbo
cooked: <p>Forgot mentioning in the above, the source are checked out from tag v1.9.0.</p>

username: girgink
cooked: <p>I’m not sure if it is the case also with Java version, but during compilation PyTorch (1.9.0) BLAS auto detection selects BLIS first, although OpenBLAS exists. BLIS doesn’t have LAPACK support, therefore torch gives “LAPACK library not found in compilation” error. You can check this by looking at the BLAS section of the configuration summary and see if it is indicated as “BLAS: blis”. If it is the case, you can correct it by setting WITH_BLAS=open, or by editing cmake/Modules/FindBLAS.cmake and setting default value to “open” before compilation.</p>
<p>I think OpenBLAS should be checked before BLIS, or should overwrite BLIS if detected.</p>

username: Paran0
cooked: <p>Is it possible for main developers to publish a build with LAPACK support?</p>
<p>It’s been about week or two since I’m stuck building the pytorch_android with LAPACK …</p>

username: kimishpatel
cooked: <p><a class="mention" href="/u/ivankobzarev">@IvanKobzarev</a> can we support this?</p>
<p><a class="mention" href="/u/paran0">@Paran0</a> my understanding is that you want prebuilt android package with LAPACK that is released from pytorch?</p>

username: Paran0
cooked: <p>Yeah, we tried with multiple BLAS backend APIs (OpenBLAS, ATLAS, Eigen, … ) All failed in some stage of the building, as I said and you can check from my previous posts we’re trying to build it for about a week or so with no success …</p>
<p>One of my main concerns is when we checked the <code>Dependencies.cmake</code> the <code>LAPACK</code> option is contained in the <code>NOT INTERN_BUILD_MOBILE</code>. Is it possible that the operations depending on LAPACK isn’t supported at all in Mobile device?<br>
There should be some reason in which you guys disabled this option in the first place …</p>

username: kimishpatel
cooked: <p><a class="mention" href="/u/paran0">@Paran0</a>, so I think LAPACK is not built with mobile at the moment. I dont have the historical context as to why. It maybe size related.</p>
<p>Digging deeper into this I see that we use Eigen for BLAS. Eigen does support LAPACK but pytorch build system does not even build for LAPACK as you pointed out. <a href="https://github.com/pytorch/pytorch/blob/master/cmake/Dependencies.cmake#L1758" class="inline-onebox" rel="noopener nofollow ugc">pytorch/Dependencies.cmake at master · pytorch/pytorch · GitHub</a></p>
<p>In order to change this we likely have to make some changes here: <a href="https://github.com/pytorch/pytorch/blob/master/cmake/Modules/FindLAPACK.cmaketo" rel="noopener nofollow ugc">https://github.com/pytorch/pytorch/blob/master/cmake/Modules/FindLAPACK.cmaketo</a> account for eigen, or just build eigen with LAPACK and set USE_LAPACK to 1 here: <a href="https://github.com/pytorch/pytorch/blob/master/cmake/Dependencies.cmake#L223" class="inline-onebox" rel="noopener nofollow ugc">pytorch/Dependencies.cmake at master · pytorch/pytorch · GitHub</a></p>
<p>Either way this will take some time to do on our end. <a class="mention" href="/u/ivankobzarev">@IvanKobzarev</a> can you help with this?</p>
<p>Alternatively, you can also try to follow this. The earlier undefined symbol error you got is because of not building eigen with LAPACK, I think.</p>