Using Custom BLAS/LAPACK for Pytorch C++ Frontend

username: talnish
cooked: <p>I am using Pytroch-C++ front-end on an AMD Threadripper machine.</p>
<p>I built libtorch by following the instructions listed here:<br>
<a href="https://pytorch.org/cppdocs/installing.html" class="onebox" target="_blank" rel="nofollow noopener">https://pytorch.org/cppdocs/installing.html</a></p>
<p>I want to use an optimized BLAS/LAPACK  library for AMD platform (<a href="https://developer.amd.com/amd-aocl/blas-library/" rel="nofollow noopener">https://developer.amd.com/amd-aocl/blas-library/</a>) and wondering how can I link it to build libtorch with this library.</p>

username: copythatpasta
cooked: <p>Did you actually build pytorch from source? or just used the prebuilt  libtorch? It should be as simple as having that amd-blas library within your main directory for libs, and running the build process. Pytorch will find the BLAS package as long as it is there in the main usr libs.</p>
<p>Follow the build process here to build from source<br>
</p><aside class="onebox whitelistedgeneric">
  <header class="source">
      <img src="https://github.githubassets.com/favicons/favicon.svg" class="site-icon" width="16" height="16">
      <a href="https://github.com/pytorch/pytorch" target="_blank" rel="nofollow noopener">GitHub</a>
  </header>
  <article class="onebox-body">
    <img src="https://avatars2.githubusercontent.com/u/21003710?s=400&amp;v=4" class="thumbnail onebox-avatar" width="60" height="60">

<h3><a href="https://github.com/pytorch/pytorch" target="_blank" rel="nofollow noopener">pytorch/pytorch</a></h3>

<p>Tensors and Dynamic neural networks in Python with strong GPU acceleration - pytorch/pytorch</p>


  </article>
  <div class="onebox-metadata">
    
    
  </div>
  <div style="clear: both"></div>
</aside>


username: talnish
cooked: <p>Thanks for your answer.<br>
I was able to make the build system detect BLIS (AMD-BLAS) installation based on the following message that it prints while building from source:</p>
<pre><code class="lang-auto">-- Checking for [blis]
--   Library blis: /usr/local/lib/libblis.so
-- Looking for sgemm_
-- Looking for sgemm_ - found
-- Performing Test BLAS_F2C_DOUBLE_WORKS
-- Performing Test BLAS_F2C_DOUBLE_WORKS - Failed
-- Performing Test BLAS_F2C_FLOAT_WORKS
-- Performing Test BLAS_F2C_FLOAT_WORKS - Success
-- Performing Test BLAS_USE_CBLAS_DOT
-- Performing Test BLAS_USE_CBLAS_DOT - Failed
-- Found a library with BLAS API (FLAME).
</code></pre>
<p>However, when I use the following command</p>
<pre><code class="lang-auto">&gt;&gt;&gt; import torch 
&gt;&gt;&gt; print(*torch.__config__.show().split("\n"), sep="\n")
</code></pre>
<p>it prints this:</p>
<pre><code class="lang-auto">PyTorch built with:
  - GCC 7.5
  - C++ Version: 201402
  - Intel(R) MKL-DNN v1.5.0 (Git Hash e2ac1fac44c5078ca927cb9b90e1b3066a0b2ed0)
  - OpenMP 201511 (a.k.a. OpenMP 4.5)
  - NNPACK is enabled
  - CPU capability usage: AVX2
  - Build settings: BLAS=MKL, BUILD_TYPE=Release, CXX_FLAGS= -Wno-deprecated -fvisibility-inlines-hidden -DUSE_PTHREADPOOL -fopenmp -DNDEBUG -DUSE_FBGEMM -DUSE_QNNPACK -DUSE_PYTORCH_QNNPACK -DUSE_XNNPACK -DUSE_VULKAN_WRAPPER -O2 -fPIC -Wno-narrowing -Wall -Wextra -Werror=return-type -Wno-missing-field-initializers -Wno-type-limits -Wno-array-bounds -Wno-unknown-pragmas -Wno-sign-compare -Wno-unused-parameter -Wno-unused-variable -Wno-unused-function -Wno-unused-result -Wno-unused-local-typedefs -Wno-strict-overflow -Wno-strict-aliasing -Wno-error=deprecated-declarations -Wno-stringop-overflow -Wno-psabi -Wno-error=pedantic -Wno-error=redundant-decls -Wno-error=old-style-cast -fdiagnostics-color=always -faligned-new -Wno-unused-but-set-variable -Wno-maybe-uninitialized -fno-math-errno -fno-trapping-math -Werror=format -Wno-stringop-overflow, PERF_WITH_AVX=1, PERF_WITH_AVX2=1, PERF_WITH_AVX512=1, USE_CUDA=OFF, USE_EIGEN_FOR_BLAS=ON, USE_EXCEPTION_PTR=1, USE_GFLAGS=OFF, USE_GLOG=OFF, USE_MKL=OFF, USE_MKLDNN=ON, USE_MPI=OFF, USE_NCCL=OFF, USE_NNPACK=ON, USE_OPENMP=ON, USE_STATIC_DISPATCH=OFF,
</code></pre>
<p>I am not sure how to interpret this, it shows <code>MKL-DNN</code>, <code>BLAS=MKL</code> (I was hoping to see <code>BLAS=FLAME</code>), and <code>USE_MKL=OFF</code>.</p>
<p>Am I missing something here? I am not sure if it’s still using BLIS or MKL.</p>