Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

blas build: use configuration from pkg_check_modules(DepBLAS openblas) and alike #11741

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

khumarahn
Copy link

@khumarahn khumarahn commented Feb 7, 2025

Hi. On my system (a gentoo), cmake build process with OpenBLAS or BLIS fails because linker options like -openblas or -lblis are missing.

With BLIS:

$ cmake -B build -DGGML_BLAS=ON -DGGML_BLAS_VENDOR=FLAME -DBUILD_NUMBER=1 && cmake --build build --config Release -v
...
[ 26%] Linking CXX executable ../bin/test-tokenizer-0
cd llama.cpp/build/tests && /usr/bin/cmake -E cmake_link_script CMakeFiles/test-tokenizer-0.dir/link.txt --verbose=1
/usr/bin/c++ -O3 -DNDEBUG "CMakeFiles/test-tokenizer-0.dir/test-tokenizer-0.cpp.o" -o ../bin/test-tokenizer-0  -Wl,-rpath,llama.cpp/build/bin: ../common/libcommon.a ../bin/libllama.so ../bin/libggml.so ../bin/libggml-cpu.so ../bin/libggml-blas.so ../bin/libggml-base.so
/usr/lib/gcc/x86_64-pc-linux-gnu/14/../../../../x86_64-pc-linux-gnu/bin/ld: ../bin/libggml-blas.so: undefined reference to `bli_thread_set_num_threads'
/usr/lib/gcc/x86_64-pc-linux-gnu/14/../../../../x86_64-pc-linux-gnu/bin/ld: ../bin/libggml-blas.so: undefined reference to `cblas_sgemm'
collect2: error: ld returned 1 exit status
gmake[2]: *** [tests/CMakeFiles/test-tokenizer-0.dir/build.make:103: bin/test-tokenizer-0] Error 1
gmake[2]: Leaving directory 'llama.cpp/build'
gmake[1]: *** [CMakeFiles/Makefile2:1906: tests/CMakeFiles/test-tokenizer-0.dir/all] Error 2
gmake[1]: Leaving directory 'llama.cpp/build'
gmake: *** [Makefile:146: all] Error 2

or with OpenBLAS:

$ cmake -B build -DGGML_BLAS=ON -DGGML_BLAS_VENDOR=OpenBLAS -DBUILD_NUMBER=1 && cmake --build build --config Release -v
[ 26%] Linking CXX executable ../bin/test-tokenizer-0
cd llama.cpp/build/tests && /usr/bin/cmake -E cmake_link_script CMakeFiles/test-tokenizer-0.dir/link.txt --verbose=1
/usr/bin/c++ -O3 -DNDEBUG "CMakeFiles/test-tokenizer-0.dir/test-tokenizer-0.cpp.o" -o ../bin/test-tokenizer-0  -Wl,-rpath,llama.cpp/build/bin: ../common/libcommon.a ../bin/libllama.so ../bin/libggml.so ../bin/libggml-cpu.so ../bin/libggml-blas.so ../bin/libggml-base.so
/usr/lib/gcc/x86_64-pc-linux-gnu/14/../../../../x86_64-pc-linux-gnu/bin/ld: ../bin/libggml-blas.so: undefined reference to `openblas_set_num_threads'
/usr/lib/gcc/x86_64-pc-linux-gnu/14/../../../../x86_64-pc-linux-gnu/bin/ld: ../bin/libggml-blas.so: undefined reference to `cblas_sgemm'
collect2: error: ld returned 1 exit status
gmake[2]: *** [tests/CMakeFiles/test-tokenizer-0.dir/build.make:103: bin/test-tokenizer-0] Error 1
gmake[2]: Leaving directory 'llama.cpp/build'
gmake[1]: *** [CMakeFiles/Makefile2:1906: tests/CMakeFiles/test-tokenizer-0.dir/all] Error 2
gmake[1]: Leaving directory 'llama.cpp/build'
gmake: *** [Makefile:146: all] Error 2

The problem seems to be quite basic: the build process is not using DepBLAS_LIBRARIES and DepBLAS_LINKER_FLAGS that come from pkg_check_modules(DepBLAS openblas) or similar. So I added a couple of lines to fix it. I'm not sure that this is the right fix, and I don't know cmake, but it seems sensible.

P.S.: Edited because at first I was wrong
P.P.S.: Added the error output so people who have the same problem are more likely to find it

@github-actions github-actions bot added the ggml changes relating to the ggml tensor library for machine learning label Feb 7, 2025
@khumarahn khumarahn marked this pull request as draft February 7, 2025 20:22
@khumarahn khumarahn marked this pull request as ready for review February 7, 2025 20:47
@khumarahn khumarahn changed the title blas build: use LDFLAGS from pkg-config blas build: use configuration from pkg_check_modules(DepBLAS openblas) and alike Feb 7, 2025
@slaren
Copy link
Collaborator

slaren commented Feb 9, 2025

BLAS_LIBRARIES and BLAS_LINKER_FLAGS should already be set by https://cmake.org/cmake/help/latest/module/FindBLAS.html. I believe pkgconfig is used only to find the include directories, since that's something that FindBLAS does not do.

@khumarahn
Copy link
Author

It makes sense, but I'm not sure why that isn't working. FindBLAS is pointing me to /usr/lib64/libblas.so which is installed by lapack, neither blis nor openblas.

A slightly modified cmake script to print out the variables gives:

-- BLAS found:
--     Libraries: /usr/lib64/libblas.so
--     Include dirs: 
--     Linker flags: 
-- Found PkgConfig: /usr/bin/pkg-config (found version "2.3.0")
-- Checking for module 'openblas64'
--   Package 'openblas64' not found
-- Checking for module 'openblas'
--   Found openblas, version 0.3.29
-- DepBLAS found:
--     Libraries: openblas
--     Include dirs: /usr/include/openblas
--     Linker flags: 

@khumarahn
Copy link
Author

khumarahn commented Feb 10, 2025

I think I understand this better now. But I'm lost even more.

To the best of my current understanding, this issue is a combination of two problems:

UPD: an old bug in gentoo: https://bugs.gentoo.org/736547

@khumarahn khumarahn marked this pull request as draft February 10, 2025 11:39
@slaren
Copy link
Collaborator

slaren commented Feb 10, 2025

On Ubuntu 24.04, it works as it is for both OpenBLAS and BLIS. Maybe we can add a workaround for distributions where FindBLAS is broken, but it should be optional.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ggml changes relating to the ggml tensor library for machine learning
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants