Problems with LAPACK on Android

kulikovv: <p>Hello! I’m trying to build libtorch for android with linear algebra support LAPACK. For building I’ve used this guide: <a href="https://github.com/pytorch/pytorch/tree/master/android" rel="nofollow noopener">https://github.com/pytorch/pytorch/tree/master/android</a> and similar solution for iOS <a href="https://discuss.pytorch.org/t/problems-with-lapack-on-ios/69240" class="inline-onebox">Problems with LAPACK on iOS</a></p>
<p>After adding flag  CMAKE_ARGS+=("-DUSE_LAPACK=ON")<br>
The compiler results in error linking LAPACK (some external function are not found)</p>
<pre><code class="lang-auto">&gt; Task :fbjni:compileReleaseJavaWithJavac
Note: Some input files use unchecked or unsafe operations.
....
&gt; 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
: &amp;&amp; /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 &amp;&amp; :
../../../../src/main/jniLibs/arm64-v8a/libtorch_cpu.a(BatchLinearAlgebra.cpp.o): In function `void at::native::lapackSolve&lt;std::__ndk1::complex&lt;double&gt; &gt;(int, int, std::__ndk1::complex&lt;double&gt;*, int, int*, std::__ndk1::complex&lt;double&gt;*, int, int*)':
/storage/pytorch/build_android/../aten/src/ATen/native/BatchLinearAlgebra.cpp:131: undefined reference to `zgesv_'
</code></pre>
<p>Which library do I have to link?<br>
Any thoughts would be appreciated!</p>

IvanKobzarev: <p>Hello <a class="mention" href="/u/kulikovv">@kulikovv</a>,<br>
Sorry for delay with reply.</p>
<p>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.</p>
<p>I experimented with linking it to pytorch_android with <code>CMAKE_ARGS+=("-DUSE_LAPACK=ON")</code><br>
It linked successfully for me.</p>
<p>I prepared PR with small instructions and changes in <code>android/pytorch_android/CMakeLists.txt</code>:<br>
<aside class="onebox githubpullrequest">
  <header class="source">
      <a href="https://github.com/pytorch/pytorch/pull/35200" target="_blank" rel="nofollow noopener">github.com/pytorch/pytorch</a>
  </header>
  <article class="onebox-body">
    <div class="github-row">
  <div class="github-icon-container" title="Pull Request">
    <svg width="60" height="60" class="github-icon" viewbox="0 0 12 16" aria-hidden="true"><path d="M11 11.28V5c-.03-.78-.34-1.47-.94-2.06C9.46 2.35 8.78 2.03 8 2H7V0L4 3l3 3V4h1c.27.02.48.11.69.31.21.2.3.42.31.69v6.28A1.993 1.993 0 0 0 10 15a1.993 1.993 0 0 0 1-3.72zm-1 2.92c-.66 0-1.2-.55-1.2-1.2 0-.65.55-1.2 1.2-1.2.65 0 1.2.55 1.2 1.2 0 .65-.55 1.2-1.2 1.2zM4 3c0-1.11-.89-2-2-2a1.993 1.993 0 0 0-1 3.72v6.56A1.993 1.993 0 0 0 2 15a1.993 1.993 0 0 0 1-3.72V4.72c.59-.34 1-.98 1-1.72zm-.8 10c0 .66-.55 1.2-1.2 1.2-.65 0-1.2-.55-1.2-1.2 0-.65.55-1.2 1.2-1.2.65 0 1.2.55 1.2 1.2zM2 4.2C1.34 4.2.8 3.65.8 3c0-.65.55-1.2 1.2-1.2.65 0 1.2.55 1.2 1.2 0 .65-.55 1.2-1.2 1.2z"></path></svg>
  </div>

  <div class="github-info-container">
    <h4>
      <a href="https://github.com/pytorch/pytorch/pull/35200" target="_blank" rel="nofollow noopener">[NOT_FOR_COMMIT][android] Pytorch android build with USE_LAPACK and QML lib</a>
    </h4>

    <div class="branches">
      <code>pytorch:master</code> ← <code>pytorch:ik_lapack_qml</code>
    </div>

    <div class="github-info">
      <div class="date">
        opened <span class="discourse-local-date" data-format="ll" data-date="2020-03-23" data-time="04:30:46" data-timezone="UTC">04:30AM - 23 Mar 20 UTC</span>
      </div>

      <div class="user">
        <a href="https://github.com/IvanKobzarev" target="_blank" rel="nofollow noopener">
          <img alt="IvanKobzarev" src="https://avatars3.githubusercontent.com/u/6638825?v=4" class="onebox-avatar-inline" width="20" height="20">
          IvanKobzarev
        </a>
      </div>

      <div class="lines" title="1 commits changed 2 files with 20 additions and 0 deletions">
        <a href="https://github.com/pytorch/pytorch/pull/35200/files" target="_blank" rel="nofollow noopener">
          <span class="added">+20</span>
          <span class="removed">-0</span>
        </a>
      </div>
    </div>

  </div>
</div>
  </article>
  <div class="onebox-metadata">
    
    
  </div>
  <div style="clear: both"></div>
</aside>
</p>
<p>Instructions from PR that I will duplicate here:</p>
<ol>
<li>Download Qualcomm Math Library <a href="https://developer.qualcomm.com/software/qualcomm-math-library" rel="nofollow noopener">https://developer.qualcomm.com/software/qualcomm-math-library</a> (You might need to register new user to download it)</li>
<li>Unpack it in  <code>${PYTORCH_ROOT}/android/libs</code>
</li>
</ol>
<p>QML can be used for armeabi-v7a, arm64-v8a android abis</p>
<ol start="3">
<li>sh ./scripts/build_pytorch_android.sh arm64-v8a,armeabi-v7a</li>
</ol>
<p>I have not checked runtime using of it, if you test it, please reply here if it works or not.</p>

kulikovv: <p>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.</p>
<p>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.</p>
<p>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.</p>

Jonson: <p>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.</p>

kulikovv: <p>We end-up by fixing CLAPACK and linking it statically. There are a lot of errors in function arguments.</p>