Skip to content

How to activate BLAS? #627

New issue

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

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

Already on GitHub? # to your account

Closed
BadisG opened this issue Mar 30, 2023 · 19 comments
Closed

How to activate BLAS? #627

BadisG opened this issue Mar 30, 2023 · 19 comments
Labels
need more info The OP should provide more details about the issue stale

Comments

@BadisG
Copy link

BadisG commented Mar 30, 2023

Hello,

I've heard that I could get BLAS activated through my intel i7 10700k by installing this library.

Unfortunatly, nothing happened, after compiling again with Clung I still have no BLAS in llama.cpp

system_info: n_threads = 14 / 16 | AVX = 1 | AVX2 = 1 | AVX512 = 0 | FMA = 1 | NEON = 0 | ARM_FMA = 0 | F16C = 1 | FP16_VA = 0 | WASM_SIMD = 0 | BLAS = 0 | SSE3 = 1 | VSX = 0 |

Maybe it's just not possible I don't know, I need someone to tell me the truth 😅

@gjmulder
Copy link
Collaborator

Please review and use the issue template before submitting new issues

@gjmulder gjmulder added the need more info The OP should provide more details about the issue label Mar 30, 2023
@hungerf3
Copy link

You'll need to edit the Makefile; look at the LLAMA_OPENBLAS section. Make sure the correct library and include paths are set for the BLAS library you want to use.

You'll also need to set LLAMA_OPENBLAS when you build; for example, add LLAMA_OPENBLAS=yes to the command line when you run make.

You might not see much improvement; the limit is likely memory bandwidth rather than processing power, and shuffling data between memory and the GPU might slow things down, but it's worth trying.

@KerfuffleV2
Copy link
Collaborator

@hungerf3

You might not see much improvement; the limit is likely memory bandwidth rather than processing power, and shuffling data between memory and the GPU might slow things down, but it's worth trying.

OpenBLAS just uses the CPU. I think there are other, separate BLAS implementations that may use GPU but that wouldn't apply in this case.

I gave it a try. You'll probably need both openblas and cblas installed. You may also need to add -lcblas to the link flags in the Makefile (#634). At least on my system, using OpenBLAS seemed a little bit faster but it was a small difference, probably less than 5%.

@cmp-nct
Copy link
Contributor

cmp-nct commented Mar 31, 2023

On Windows I just gave up, wasted 3 hours of my life trying to get that BLASmonster somehow working.
Barely anything to be found online, most references are for linux.

The best amount of chaos I had with that:

set(OpenBLAS_INCLUDE_DIR "C:/Program Files (x86)/OpenBLAS/include/openblas")
set(OpenBLAS_LIBRARIES "C:/Program Files (x86)/OpenBLAS/bin/openblas.dll")
set(BLAS_LIBRARIES "C:/Program Files (x86)/OpenBLAS/bin/openblas.dll")
include_directories(${OpenBLAS_INCLUDE_DIR})
add_compile_definitions(GGML_USE_OPENBLAS)
add_library(openblas SHARED IMPORTED)
add_link_options("/DELAYLOAD:openblas.dll")
add_link_options("/OPT:REF")

--
the code to find blas is not working (find_package() is not locating it) so the above part manually adds it
The above code was added to the CMakeLists.txt manually.

Though that still doesn't properly load the DLL
"unresolved external symbol cblas_sgemm referenced in function ggml_compute_forward_mul_mat_f16_f32"
Must be something wrong in linking the dll, for another day/year

@BadisG
Copy link
Author

BadisG commented Mar 31, 2023

@cmp-nct it's ok I found how to make it work.

1 - Download this https://github.com/xianyi/OpenBLAS/releases/tag/v0.3.22
2 - Put the files on a folder (I'll call it OpenBlasFolder)
3 - On the windows searsh write "Edit the system environnement variables"
4 - Click on "Environnement variables"
5 - On "System Variables" find PATH and click edit
6 - Add a new path that has OpenBlasFolder\bin in it (in my case it was something like D:\Large Language Models\OpenBlasFolder\bin)
7 - On your CMakeList.txt enable OpenBLAS

option(LLAMA_OPENBLAS               "llama: use OpenBLAS"                                   ON)

8 - On your CMakeList.txt, add this line just after "# Build libraries"

include_directories("D:/Large Language Models/OpenBlasFolder/include") <- For you the path will be something different of course

And there you go... gosh why is it so complicated to install a library seriously 😞

@sicp-dnegvp
Copy link

@BadisG adding the folder to the %PATH% makes cmake find OpenBLAS? It doesn't change anything for me.

-- Could NOT find BLAS (missing: BLAS_LIBRARIES)

Switching the LLAMA_OPENBLAS option to ON just enables the broken cmake find_package() check for OpenBLAS, which keeps failing, no matter what I do.
If you want to bypass that and just manually add the paths and flags, you also need to add this after your include_directories:

add_compile_definitions(GGML_USE_OPENBLAS)
add_link_options("path_to_your_openblas_/lib/libopenblas.lib")

That will actually make it build with openblas (it actually depends on libopenblas.dll now).
It just crashes on startup for me though. I think I'm giving up on this, it doesn't seem to be worth the effort in Windows.

@cmp-nct
Copy link
Contributor

cmp-nct commented Mar 31, 2023

@cmp-nct it's ok I found how to make it work.

1 - Download this https://github.com/xianyi/OpenBLAS/releases/tag/v0.3.22 2 - Put the files on a folder (I'll call it OpenBlasFolder) 3 - On the windows searsh write "Edit the system environnement variables" 4 - Click on "Environnement variables" 5 - On "System Variables" find PATH and click edit 6 - Add a new path that has OpenBlasFolder\bin in it (in my case it was something like D:\Large Language Models\OpenBlasFolder\bin) 7 - On your CMakeList.txt enable OpenBLAS

option(LLAMA_OPENBLAS               "llama: use OpenBLAS"                                   ON)

8 - On your CMakeList.txt, add this line just after "# Build libraries"

include_directories("D:/Large Language Models/OpenBlasFolder/include") <- For you the path will be something different of course

And there you go... gosh why is it so complicated to install a library seriously 😞

In addition to that I had to add the -DBLAS_LIBRARIES= flag into settings.json as configurationArgs entry.

Now it indeed is compiled, also detected as BLAS = 1
The speed is exactly the same so it was useless.

I guess it might help to use the Intel BLAS lib, I just dislike adding 3.5 GB of libraries for a math multiplication.

@BadisG
Copy link
Author

BadisG commented Mar 31, 2023

@cmp-nct If I had to guess I don't think adding the Intel BLAS lib will change anything, I'm pretty sure it's similar to the github BLAS I found.

So same, I didn't see significant increase of performance I also wasted my time 😅

@cmp-nct
Copy link
Contributor

cmp-nct commented Mar 31, 2023

@cmp-nct If I had to guess I don't think adding the Intel BLAS lib will change anything, I'm pretty sure it's similar to the github BLAS I found.

So same, I didn't see significant increase of performance I also wasted my time 😅

Intel has a couple benchmarks up, significant differences to the open blas.
But it's 3.5 gig to install.
People these days don't know what efficiency means.

The same crap with the makefileon VScode I need to get llama compiled (llama.cpp is written awesomely).
That cmake stuff is more complex than the source code and it depends on more files than the source code.
It should be 10 lines to compile and link those 5 binaries but we've kilobytes of Make config to work through.
So I'll skip on Intel Blas

@tomsnunes
Copy link

@BadisG adding the folder to the %PATH% makes cmake find OpenBLAS? It doesn't change anything for me.

-- Could NOT find BLAS (missing: BLAS_LIBRARIES)

Switching the LLAMA_OPENBLAS option to ON just enables the broken cmake find_package() check for OpenBLAS, which keeps failing, no matter what I do. If you want to bypass that and just manually add the paths and flags, you also need to add this after your include_directories:

add_compile_definitions(GGML_USE_OPENBLAS)
add_link_options("path_to_your_openblas_/lib/libopenblas.lib")

That will actually make it build with openblas (it actually depends on libopenblas.dll now). It just crashes on startup for me though. I think I'm giving up on this, it doesn't seem to be worth the effort in Windows.

I was able to compile on Windows adding the following to CMakeLists.txt:

include_directories("C:/libs/OpenBLAS/include")
add_compile_definitions(GGML_USE_OPENBLAS)
add_link_options("C:/libs/OpenBLAS/lib/libopenblas.dll.a")

Now talking about potential performance gains, it may be my impression, but I notice some gain in the response time of the model in relation to the binary compiled without OpenBLAS, but I will still do more tests.

For now, using the llama 7b template using llama.cpp (master) my initial result is:
OpenBLAS enabled: 3.0843
OpenBLAS disabled: 3.1641

@rbrisita
Copy link
Contributor

rbrisita commented May 2, 2023

I was able to turn it on using the following for Windows 11 WSL2 Ubuntu 20.04:

git clone https://github.com/xianyi/OpenBLAS.git
cd OpenBLAS
make
make PREFIX=/usr/local/include/openblas install # The directory is in llama.cpp's Makefile.
cd ..
git clone https://github.com/ggerganov/llama.cpp.git
cd llama.cpp
make LLAMA_OPENBLAS=1

Next time you run llama.cpp you'll have BLAS turned on.

@rbrisita
Copy link
Contributor

It was a little different on Debian 11. There are different versions available: libopenblas[0 | 64[-openmp | -pthread | -serial]]-dev.
For this example, let's choose libopenblas-dev:

  1. sudo apt install libopenblas-dev
  2. Find include path and the lib file path of the installed library with dpkg -L libopenblas-dev.
  3. cd llama.cpp
  4. Modify Makefile to point to the include path, -I, in the CFLAGS variable.
  5. Modify Makefile to point to the lib .so file in the LDFLAGS variable.
  6. make clean
  7. make LLAMA_OPENBLAS=1

Next time you run llama.cpp you'll have BLAS turned on.

@countzero
Copy link

countzero commented Jun 28, 2023

Hi,

first of all @ggerganov thank you for this awesome project!

Problem

As a Windows user I also struggled to build llama.cpp with CMake and BLAS acceleration via OpenBLAS. The underlying problem seems to be that find_package(BLAS) does not find the OpenBLAS library. Even if it is located within the ./build directory. The error message is "Could NOT find BLAS (missing: BLAS_LIBRARIES)".

I debugged the CMake problem a bit further and described my findings there:

Workaround

Based on the description of @tomsnunes I have found a reliable way to build llama.cpp with an OpenBLAS library binding on Windows with CMake.

  1. Download and extract the latest OpenBLAS release
  2. Copy the libopenblas.dll file into the ./build/bin/Release directory next to the main.exe
  3. Add the following code on top of the CMakeLists.txt
# This is a workaround for a CMake bug on Windows to build llama.cpp
# with OpenBLAS. The find_package(BLAS) call fails to find OpenBLAS,
# so we have to link the 'libopenblas.dll' shared library manually.
# 
# @see https://github.com/ggerganov/llama.cpp/issues/627
# @see https://discourse.cmake.org/t/8414
# 
if (LLAMA_BLAS AND DEFINED LLAMA_BLAS_VENDOR)
    if (${LLAMA_BLAS_VENDOR} MATCHES "OpenBLAS")
        set(LLAMA_EXTRA_INCLUDES ${LLAMA_EXTRA_INCLUDES} "C:/absolute/path/to/OpenBLAS/include")
        set(LLAMA_EXTRA_LIBS ${LLAMA_EXTRA_LIBS} "C:/absolute/path/to/OpenBLAS/lib/libopenblas.dll.a")
        add_compile_definitions(GGML_USE_OPENBLAS)
    endif()
endif()

You can then build llama.cpp with OpenBLAS acceleration on Windows via CMake:

cmake -DLLAMA_BLAS=ON -DLLAMA_BLAS_VENDOR=OpenBLAS ..

Automation

Since llama.cpp changes frequently, I automated the complete build process with PowerShell:

This enables you to simply execute the following to keep up to date with the llama.cpp project:

./rebuild_llama.cpp.ps1 -blasAccelerator "OpenBLAS"

Next Steps

I think eventually this problem should be fixed in the scope of CMake to not clutter the CMakeLists.txt. A temporary workaround in this project scope might be a new optional option parameter to inject the path to the OpenBLAS library for Windows users.

@ggerganov what do you think?

@loretoparisi
Copy link

libopenblas-dev

It was a little different on Debian 11. There are different versions available: libopenblas[0 | 64[-openmp | -pthread | -serial]]-dev. For this example, let's choose libopenblas-dev:

  1. sudo apt install libopenblas-dev
  2. Find include path and the lib file path of the installed library with dpkg -L libopenblas-dev.
  3. cd llama.cpp
  4. Modify Makefile to point to the include path, -I, in the CFLAGS variable.
  5. Modify Makefile to point to the lib .so file in the LDFLAGS variable.
  6. make clean
  7. make LLAMA_OPENBLAS=1

Next time you run llama.cpp you'll have BLAS turned on.

I do not see the library files here

dpkg -L libopenblas-dev
/.
/usr
/usr/share
/usr/share/doc
/usr/share/doc/libopenblas-dev
/usr/share/doc/libopenblas-dev/BACKERS.md
/usr/share/doc/libopenblas-dev/CONTRIBUTORS.md.gz
/usr/share/doc/libopenblas-dev/README.md.gz
/usr/share/doc/libopenblas-dev/USAGE.md.gz
/usr/share/doc/libopenblas-dev/changelog.Debian.gz
/usr/share/doc/libopenblas-dev/changelog.gz
/usr/share/doc/libopenblas-dev/copyright
/usr/share/lintian
/usr/share/lintian/overrides
/usr/share/lintian/overrides/libopenblas-dev

@rbrisita
Copy link
Contributor

libopenblas-dev

It was a little different on Debian 11. There are different versions available: libopenblas[0 | 64[-openmp | -pthread | -serial]]-dev. For this example, let's choose libopenblas-dev:

  1. sudo apt install libopenblas-dev
  2. Find include path and the lib file path of the installed library with dpkg -L libopenblas-dev.
  3. cd llama.cpp
  4. Modify Makefile to point to the include path, -I, in the CFLAGS variable.
  5. Modify Makefile to point to the lib .so file in the LDFLAGS variable.
  6. make clean
  7. make LLAMA_OPENBLAS=1

Next time you run llama.cpp you'll have BLAS turned on.

I do not see the library files here

dpkg -L libopenblas-dev
/.
/usr
/usr/share
/usr/share/doc
/usr/share/doc/libopenblas-dev
/usr/share/doc/libopenblas-dev/BACKERS.md
/usr/share/doc/libopenblas-dev/CONTRIBUTORS.md.gz
/usr/share/doc/libopenblas-dev/README.md.gz
/usr/share/doc/libopenblas-dev/USAGE.md.gz
/usr/share/doc/libopenblas-dev/changelog.Debian.gz
/usr/share/doc/libopenblas-dev/changelog.gz
/usr/share/doc/libopenblas-dev/copyright
/usr/share/lintian
/usr/share/lintian/overrides
/usr/share/lintian/overrides/libopenblas-dev

Try installing one of the other versions like libopenblas-pthread-dev.

@github-actions github-actions bot removed the stale label Mar 26, 2024
@github-actions github-actions bot added the stale label Apr 26, 2024
Copy link
Contributor

This issue was closed because it has been inactive for 14 days since being marked as stale.

@themanyone
Copy link

I was compiling whisper.cpp and got the same error. I looked at CMakeLists.txt and reviewed documentation for FindBLAS. Tried supplying -DOpenBLAS to the build line. That didn't work. But grep BLAS revealed more options to try.

cmake -B build -DGGML_BLAS=1 -DGGML_BLAS_VENDOR=OpenBLAS
cmake --build build --config Release -j7

@katmandoo212
Copy link

I seem to have found an incantation that works. I hope it helps and I hope it works for everyone. But, YMMV!!! As with the Trolls, and the Whimsy Wasps, sometimes its best to be serious and sometimes its best to be whimsical:

D:\home\user\github\llama.cpp>cmake -B build --fresh -DGGML_BLAS=ON -DGGML_BLAS_VENDOR=OpenBLAS 
-DBLAS_LIBRARIES=D:\home\user\github\llama.cpp\OpenBLAS\libopenblas.lib 
-DBLAS_INCLUDE_DIRS=D:\home\user\github\llama.cpp\OpenBLAS\include 
-DPKG_CONFIG_EXECUTABLE=D:\home\user\github\vcpkg\vcpkg 
-DBLA_PREFER_PKGCONFIG=ON -DBLA_SIZEOF_INTEGER=8

-- Building for: Visual Studio 17 2022
-- Selecting Windows SDK version 10.0.22621.0 to target Windows 10.0.19045.
-- The C compiler identification is MSVC 19.43.34809.0
-- The CXX compiler identification is MSVC 19.43.34809.0
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: C:/Program Files/Microsoft Visual Studio/2022/Community/VC/Tools/MSVC/14.43.34808/bin/Hostx64/x64/cl.exe - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: C:/Program Files/Microsoft Visual Studio/2022/Community/VC/Tools/MSVC/14.43.34808/bin/Hostx64/x64/cl.exe - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Found Git: C:/Program Files/Git/cmd/git.exe (found version "2.48.1.windows.1")
-- Performing Test CMAKE_HAVE_LIBC_PTHREAD
-- Performing Test CMAKE_HAVE_LIBC_PTHREAD - Failed
-- Looking for pthread_create in pthreads
-- Looking for pthread_create in pthreads - not found
-- Looking for pthread_create in pthread
-- Looking for pthread_create in pthread - not found
-- Found Threads: TRUE
-- Warning: ccache not found - consider installing it for faster compilation or disable this warning with GGML_CCACHE=OFF
-- CMAKE_SYSTEM_PROCESSOR: AMD64
-- CMAKE_GENERATOR_PLATFORM:
-- Including CPU backend
-- Found OpenMP_C: -openmp (found version "2.0")
-- Found OpenMP_CXX: -openmp (found version "2.0")
-- Found OpenMP: TRUE (found version "2.0")
-- x86 detected
-- Performing Test HAS_AVX_1
-- Performing Test HAS_AVX_1 - Success
-- Performing Test HAS_AVX2_1
-- Performing Test HAS_AVX2_1 - Success
-- Performing Test HAS_FMA_1
-- Performing Test HAS_FMA_1 - Success
-- Performing Test HAS_AVX512_1
-- Performing Test HAS_AVX512_1 - Failed
-- Performing Test HAS_AVX512_2
-- Performing Test HAS_AVX512_2 - Failed
-- Adding CPU backend variant ggml-cpu: /arch:AVX2 GGML_AVX2;GGML_FMA;GGML_F16C
-- Found BLAS: D:\home\user\github\llama.cpp\OpenBLAS\libopenblas.lib
-- BLAS found, Libraries: D:\home\user\github\llama.cpp\OpenBLAS\libopenblas.lib
-- BLAS found, Includes: D:\home\user\github\llama.cpp\OpenBLAS\include
-- Including BLAS backend
-- Configuring done (49.6s)
-- Generating done (19.7s)
-- Build files have been written to: D:/home/user/github/llama.cpp/build

D:\home\user\github\llama.cpp>cmake --build build --config Release -j 8
MSBuild version 17.13.19+0d9f5a35a for .NET Framework

  build_info.vcxproj -> D:\home\user\github\llama.cpp\build\common\build_info.dir\Release\build_info.lib
  sha1.vcxproj -> D:\home\user\github\llama.cpp\build\examples\gguf-hash\sha1.dir\Release\sha1.lib
  sha256.vcxproj -> D:\home\user\github\llama.cpp\build\examples\gguf-hash\sha256.dir\Release\sha256.lib
  ggml-base.vcxproj -> D:\home\user\github\llama.cpp\build\bin\Release\ggml-base.dll
  xxhash.vcxproj -> D:\home\user\github\llama.cpp\build\examples\gguf-hash\xxhash.dir\Release\xxhash.lib
  ggml-cpu.vcxproj -> D:\home\user\github\llama.cpp\build\bin\Release\ggml-cpu.dll
  ggml-blas.cpp

@themanyone
Copy link

themanyone commented Mar 27, 2025

I thought -DBLA_SIZEOF_INTEGER=8 might offer a little boost with quantized models on non-GPU-accl. systems.
But no, it didn't make a difference. In fact, it seems to be about 3% slower, but it's within a margin of error.

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
need more info The OP should provide more details about the issue stale
Projects
None yet
Development

No branches or pull requests