Help Needed: How to Integrate AMD BLIS & AMD LibFLAME into PyTorch CPU Backend?

kksspoi: <p>Hello,</p>
<p>I have built <code>libtorch (torch_no_python)</code> using a specific CMake configuration and installed it locally. After successfully completing the build with <code>BUILD_LIBTORCH_WHL=1 python setup.py bdist_wheel</code>, I installed the resulting wheel file into a Python 3.11.11 virtual environment. After installation, I copied the <code>lib</code>, <code>include</code>, <code>bin</code>, and <code>share</code> directories from <code>libtorch</code> into the virtual environment’s Python directory located at <code>/home/s/bt/lib/python3.11/site-packages/torch/</code>.<br>
cmake -B build -S . -G Ninja <br>
-DCMAKE_BUILD_TYPE=Debug <br>
-DBUILD_TEST=OFF <br>
-DBUILD_EXECUTORCH=OFF <br>
-DBUILD_FUNCTORCH=ON <br>
-DUSE_MPI=OFF <br>
-DBUILD_ONEDNN_GRAPH=OFF <br>
-DINTERN_BUILD_MOBILE=OFF <br>
-DCAFFE2_USE_EIGEN_FOR_BLAS=OFF <br>
-DUSE_EIGEN_FOR_BLAS=OFF <br>
-DCAFFE2_USE_MKL=OFF <br>
-DCAFFE2_PERF_WITH_AVX2=ON <br>
-DINTERN_BUILD_ATEN_OPS=ON <br>
-DBLAS=BLIS <br>
-DBLAS_blis_LIBRARY=“/home/s/.local/amd-blis/lib/libblis-mt.so” <br>
-DBLIS_INCLUDE_DIR=“/home/s/.local/amd-blis/include;/home/s/.local/amd-libflame/include” <br>
-DBLIS_LIB=“/home/s/.local/amd-blis/lib/libblis-mt.so;/home/s/.local/amd-libflame/lib/libflame.so” <br>
-DLAPACK_LIBRARIES=“/home/s/.local/amd-libflame/lib/libflame.so” <br>
-DLAPACK_INFO=FLAME <br>
-DMAGMA_INCLUDE_DIR=“/home/s/.local/magma/include” <br>
-DMAGMA_LIBRARIES=“/home/s/.local/magma/lib/libmagma.so;/home/s/.local/magma/lib/libmagma_sparse.so” <br>
-DUSE_ROCM=OFF <br>
-DUSE_CUDA=ON <br>
-DUSE_XPU=OFF <br>
-DUSE_MKL=OFF <br>
-DUSE_FBGEMM=ON <br>
-DUSE_FAKELOWP=ON <br>
-DCMAKE_C_IMPLICIT_LINK_DIRECTORIES=“/lib/x86_64-linux-gnu/libgfortran.so.5” <br>
-DUSE_MKLDNN=OFF <br>
-DUSE_OPENMP=ON <br>
-DUSE_NCCL=OFF <br>
-DUSE_ITT=OFF <br>
-DUSE_MIMALLOC=ON <br>
-DUSE_XNNPACK=OFF <br>
-DTORCH_CUDA_ARCH_LIST=“8.0;8.6;8.9” <br>
-DCMAKE_CUDA_ARCHITECTURES=“80;86;89” <br>
-DCMAKE_CUDA_COMPILER=“/usr/local/cuda-12.8/bin/nvcc” <br>
-DUSE_CUDNN=ON <br>
-DCUDNN_INCLUDE_DIR=“/usr/include” <br>
-DCUDNN_LIBRARY=“/usr/lib/x86_64-linux-gnu/libcudnn.so” <br>
-DCUDA_TOOLKIT_ROOT_DIR=“/usr/local/cuda-12.8” <br>
-DUSE_CUSPARSELT=ON <br>
-DUSE_CUFILE=ON <br>
-DUSE_CUDSS=ON <br>
-DUSE_NVRTC=ON <br>
-DUSE_NUMA=ON <br>
-DONNX_BUILD_SHARED_LIBS=ON <br>
-DBUILD_NVFUSER=ON <br>
-DBUILD_ONNX_PYTHON=ON <br>
-DPYTHON_EXECUTABLE=“$(which python3)” <br>
-DPYTHON_INCLUDE_DIR=“/home/s/bt/include/python3.11” <br>
-DPYTHON_LIBRARY=“/usr/local/python3.11.11/lib/libpython3.11.so” <br>
-DPYTHON_LIBRARIES=“/usr/local/python3.11.11/lib/libpython3.11.so” <br>
-DCMAKE_C_COMPILER=“/usr/bin/x86_64-linux-gnu-gcc-13” <br>
-DCMAKE_CXX_COMPILER=“/usr/bin/x86_64-linux-gnu-g+±13” <br>
-DCMAKE_INSTALL_PREFIX=“$HOME/local/libtorch2.6.0” <br>
-DCMAKE_PREFIX_PATH=“/home/s/bt/lib/python3.11/site-packages;/home/s/.local/amd-blis/include;/home/s/.local/amd-libflame/include” <br>
-DCMAKE_INCLUDE_PATH=“/home/s/.local/amd-blis/include;/home/s/.local/amd-libflame/include” <br>
-DCMAKE_LIBRARY_PATH=“/home/s/.local/amd-blis/lib;/home/s/.local/amd-libflame/lib” <br>
-DUSE_MAGMA=ON <br>
-DMAGMA_V2=ON <br>
-DCMAKE_VERBOSE_MAKEFILE=ON <br>
-DCUDA_NVRTC_LIB=“/usr/local/cuda-12.8/targets/x86_64-linux/lib/libnvrtc.so” <br>
-DCUDA_NVRTC_SHORTHASH=“3e858d13” <br>
-DUSE_TENSORRT=ON <br>
-DTENSORRT_LIBRARY=“/usr/lib/x86_64-linux-gnu/libnvinfer.so” <br>
-DTENSORRT_INCLUDE_DIR=“/usr/include/x86_64-linux-gnu” <br>
-DCMAKE_BUILD_WITH_INSTALL_RPATH=ON <br>
-DBUILD_SHARED_LIBS=ON <br>
-DBUILD_PYTHON=ON <br>
-DHAVE_SOVERSION=ON <br>
-DBUILD_BUNDLE_PTXAS=ON <br>
-DPYTHON_VERSION_STRING=“3.11” <br>
-DPYTHON_SIX_SOURCE_DIR=“/home/sk/bt/lib/python3.11/site-packages” <br>
-DCMAKE_EXPORT_COMPILE_COMMANDS=ON <br>
-DLIBGMP=“/usr/lib/x86_64-linux-gnu/libgmp.so” <br>
-DLIBFFTW3=“/usr/lib/x86_64-linux-gnu/libfftw3.so” <br>
-DOpenMP_C_LIBRARIES=“/usr/lib/gcc/x86_64-linux-gnu/13/libgomp.so” <br>
-DOpenMP_CXX_LIBRARIES=“/usr/lib/gcc/x86_64-linux-gnu/13/libgomp.so” <br>
-DOpenMP_C_INCLUDE_DIRS=“/usr/lib/gcc/x86_64-linux-gnu/13/include” <br>
-DOpenMP_CXX_INCLUDE_DIRS=“/usr/lib/gcc/x86_64-linux-gnu/13/include” <br>
-DCMAKE_C_FLAGS=“-march=znver4 -O3 -ffast-math -mavx512f -mavx512dq -mavx512vnni -mavx512bf16 -mavx512vl -mavx512bw -mavx2 -mfma -pthread -fopenmp -funroll-loops -Wno-error” <br>
-DCMAKE_CXX_FLAGS=“-march=znver4 -O3 -ffast-math -mavx512f -mavx512dq -mavx512vnni -mavx512bf16 -mavx512vl -mavx512bw -mavx2 -mfma -pthread -fopenmp -funroll-loops -Wno-error”<br>
Next, I cleaned the PyTorch source directory by removing the build directory and running <code>python setup.py clean</code>. Then, I re-ran the same CMake command used for building <code>libtorch</code>, ensuring that AMD BLIS and AMD LibFLAME were correctly recognized. After that, I proceeded to build the main PyTorch package using the command:</p>
<p><code>BUILD_PYTHON_ONLY=1 LDFLAGS="-L/home/s/bt/lib/python3.11/site-packages/torch/lib -Wl,-rpath,/home/s/bt/lib/python3.11/site-packages/torch/lib" python setup.py bdist_wheel</code>.</p>
<p>The build process completes successfully. After installing the newly built PyTorch package into my virtual environment, CUDA and cuDNN are recognized correctly. However, when I check the BLAS and LAPACK configuration by running <code>import torch</code> followed by <code>print(torch.__config__.show())</code>, AMD BLIS and AMD LibFLAME are not recognized as the BLAS and LAPACK backends.</p>
<h3><a name="p-466066-question-1" class="anchor" href="#p-466066-question-1"></a><strong>Question:</strong></h3>
<p>How can I correctly integrate AMD BLIS (specified as <code>blas=blis</code>) and AMD LibFLAME (specified as <code>lapack=flame</code>) into PyTorch’s CPU backend? Are there any specific build flags or configuration steps that I need to modify to achieve this integration?</p>
<p>Any guidance on the proper way to build PyTorch with these libraries would be greatly appreciated.</p>
<p>Thank you!</p>

kksspoi: <p>I was able to set blas = bils (amd blis) and lapack = amd libflame and this problem was solved.</p>