Skip to content

Commit

Permalink
sci-misc/llama-cpp: support OpenBLAS or BLIS backends
Browse files Browse the repository at this point in the history
A significant speed up in some workflows when running on a CPU

Signed-off-by: Alexey Korepanov <[email protected]>
  • Loading branch information
khumarahn committed Feb 9, 2025
1 parent 72c1099 commit a48cf7a
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 6 deletions.
16 changes: 16 additions & 0 deletions sci-misc/llama-cpp/files/blas-ld.diff
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
cmake does not properly configure linking against OpenBLAS or BLIS
https://github.com/ggerganov/llama.cpp/pull/11741

diff --git a/ggml/src/ggml-blas/CMakeLists.txt b/ggml/src/ggml-blas/CMakeLists.txt
index 0bf3c05d93a89..117416b988665 100644
--- a/ggml/src/ggml-blas/CMakeLists.txt
+++ b/ggml/src/ggml-blas/CMakeLists.txt
@@ -54,6 +54,8 @@ if (BLAS_FOUND)
endif()
if (DepBLAS_FOUND)
set(BLAS_INCLUDE_DIRS ${DepBLAS_INCLUDE_DIRS})
+ set(BLAS_LIBRARIES ${DepBLAS_LIBRARIES})
+ set(BLAS_LINKER_FLAGS ${DepBLAS_LINKER_FLAGS})
else()
message(WARNING "BLAS_INCLUDE_DIRS neither been provided nor been automatically"
" detected by pkgconfig, trying to find cblas.h from possible paths...")
25 changes: 22 additions & 3 deletions sci-misc/llama-cpp/llama-cpp-0_pre4576.ebuild
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2023 Gentoo Authors
# Copyright 2025 Gentoo Authors
# Distributed under the terms of the GNU General Public License v2

EAPI=8
Expand All @@ -21,14 +21,20 @@ HOMEPAGE="https://github.com/ggerganov/llama.cpp"
LICENSE="MIT"
SLOT="0"
CPU_FLAGS_X86=( avx avx2 f16c )
IUSE="curl"
IUSE="curl openblas blis"
REQUIRED_USE="?? ( openblas blis )"

# curl is needed for pulling models from huggingface
# numpy is used by convert_hf_to_gguf.py
DEPEND="curl? ( net-misc/curl:= )"
DEPEND="
curl? ( net-misc/curl:= )
openblas? ( sci-libs/openblas:= )
blis? ( sci-libs/blis:= )
"
RDEPEND="${DEPEND}
dev-python/numpy
"
PATCHES=( "${FILESDIR}/blas-ld.diff" )

src_configure() {
local mycmakeargs=(
Expand All @@ -39,5 +45,18 @@ src_configure() {
-DLLAMA_CURL=$(usex curl ON OFF)
-DBUILD_NUMBER="1"
)

if use openblas ; then
mycmakeargs+=(
-DGGML_BLAS=ON -DGGML_BLAS_VENDOR=OpenBLAS
)
fi

if use blis ; then
mycmakeargs+=(
-DGGML_BLAS=ON -DGGML_BLAS_VENDOR=FLAME
)
fi

cmake_src_configure
}
25 changes: 22 additions & 3 deletions sci-misc/llama-cpp/llama-cpp-9999.ebuild
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2023 Gentoo Authors
# Copyright 2025 Gentoo Authors
# Distributed under the terms of the GNU General Public License v2

EAPI=8
Expand All @@ -21,14 +21,20 @@ HOMEPAGE="https://github.com/ggerganov/llama.cpp"
LICENSE="MIT"
SLOT="0"
CPU_FLAGS_X86=( avx avx2 f16c )
IUSE="curl"
IUSE="curl openblas blis"
REQUIRED_USE="?? ( openblas blis )"

# curl is needed for pulling models from huggingface
# numpy is used by convert_hf_to_gguf.py
DEPEND="curl? ( net-misc/curl:= )"
DEPEND="
curl? ( net-misc/curl:= )
openblas? ( sci-libs/openblas:= )
blis? ( sci-libs/blis:= )
"
RDEPEND="${DEPEND}
dev-python/numpy
"
PATCHES=( "${FILESDIR}/blas-ld.diff" )

src_configure() {
local mycmakeargs=(
Expand All @@ -39,5 +45,18 @@ src_configure() {
-DLLAMA_CURL=$(usex curl ON OFF)
-DBUILD_NUMBER="1"
)

if use openblas ; then
mycmakeargs+=(
-DGGML_BLAS=ON -DGGML_BLAS_VENDOR=OpenBLAS
)
fi

if use blis ; then
mycmakeargs+=(
-DGGML_BLAS=ON -DGGML_BLAS_VENDOR=FLAME
)
fi

cmake_src_configure
}
4 changes: 4 additions & 0 deletions sci-misc/llama-cpp/metadata.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
<upstream>
<remote-id type="github">ggerganov/llama.cpp</remote-id>
</upstream>
<use>
<flag name="blis">Build a BLIS backend</flag>
<flag name="openblas">Build an OpenBLAS backend</flag>
</use>
<maintainer type="person">
<email>[email protected]</email>
<name>Sergey Alirzaev</name>
Expand Down

0 comments on commit a48cf7a

Please sign in to comment.