Skip to content

Commit 5b71d20

Browse files
committed
enabled OpenBLAS
1 parent b16fb0d commit 5b71d20

File tree

2 files changed

+99
-83
lines changed

2 files changed

+99
-83
lines changed

ggml/src/ggml-blas/CMakeLists.txt

Lines changed: 81 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -5,83 +5,96 @@ endif()
55
# set(BLA_SIZEOF_INTEGER 8)
66
#endif()
77

8-
set(BLA_VENDOR ${GGML_BLAS_VENDOR})
9-
find_package(BLAS)
10-
11-
if (BLAS_FOUND)
12-
message(STATUS "BLAS found, Libraries: ${BLAS_LIBRARIES}")
8+
if(${GGML_BLAS_VENDOR} MATCHES "LocalOpenBLAS")
9+
message(STATUS "Using local BLAS, Includes: ${BLAS_INCLUDE_DIRS}")
1310

1411
ggml_add_backend_library(ggml-blas
15-
ggml-blas.cpp
16-
)
12+
ggml-blas.cpp
13+
)
14+
15+
target_compile_options(ggml-blas PRIVATE ${BLAS_LINKER_FLAGS})
16+
17+
target_link_libraries (ggml-blas PRIVATE ${BLAS_LIBRARIES})
18+
target_include_directories(ggml-blas PRIVATE ${BLAS_INCLUDE_DIRS})
19+
else()
20+
set(BLA_VENDOR ${GGML_BLAS_VENDOR})
21+
find_package(BLAS)
22+
23+
if (BLAS_FOUND)
24+
message(STATUS "BLAS found, Libraries: ${BLAS_LIBRARIES}")
1725

18-
if (${GGML_BLAS_VENDOR} MATCHES "Apple")
19-
add_compile_definitions(ACCELERATE_NEW_LAPACK)
20-
add_compile_definitions(ACCELERATE_LAPACK_ILP64)
21-
add_compile_definitions(GGML_BLAS_USE_ACCELERATE)
22-
elseif ("${BLAS_INCLUDE_DIRS}" STREQUAL "")
23-
# BLAS_INCLUDE_DIRS is missing in FindBLAS.cmake.
24-
# see https://gitlab.kitware.com/cmake/cmake/-/issues/20268
25-
find_package(PkgConfig REQUIRED)
26-
if (${GGML_BLAS_VENDOR} MATCHES "Generic")
27-
pkg_check_modules(DepBLAS blas)
28-
elseif (${GGML_BLAS_VENDOR} MATCHES "OpenBLAS")
29-
# As of openblas v0.3.22, the 64-bit is named openblas64.pc
30-
pkg_check_modules(DepBLAS openblas64)
31-
if (NOT DepBLAS_FOUND)
32-
pkg_check_modules(DepBLAS openblas)
26+
ggml_add_backend_library(ggml-blas
27+
ggml-blas.cpp
28+
)
29+
30+
if (${GGML_BLAS_VENDOR} MATCHES "Apple")
31+
add_compile_definitions(ACCELERATE_NEW_LAPACK)
32+
add_compile_definitions(ACCELERATE_LAPACK_ILP64)
33+
add_compile_definitions(GGML_BLAS_USE_ACCELERATE)
34+
elseif ("${BLAS_INCLUDE_DIRS}" STREQUAL "")
35+
# BLAS_INCLUDE_DIRS is missing in FindBLAS.cmake.
36+
# see https://gitlab.kitware.com/cmake/cmake/-/issues/20268
37+
find_package(PkgConfig REQUIRED)
38+
if (${GGML_BLAS_VENDOR} MATCHES "Generic")
39+
pkg_check_modules(DepBLAS blas)
40+
elseif (${GGML_BLAS_VENDOR} MATCHES "OpenBLAS")
41+
# As of openblas v0.3.22, the 64-bit is named openblas64.pc
42+
pkg_check_modules(DepBLAS openblas64)
43+
if (NOT DepBLAS_FOUND)
44+
pkg_check_modules(DepBLAS openblas)
45+
endif()
46+
elseif (${GGML_BLAS_VENDOR} MATCHES "FLAME")
47+
add_compile_definitions(GGML_BLAS_USE_BLIS)
48+
pkg_check_modules(DepBLAS blis)
49+
elseif (${GGML_BLAS_VENDOR} MATCHES "ATLAS")
50+
pkg_check_modules(DepBLAS blas-atlas)
51+
elseif (${GGML_BLAS_VENDOR} MATCHES "FlexiBLAS")
52+
pkg_check_modules(DepBLAS flexiblas_api)
53+
elseif (${GGML_BLAS_VENDOR} MATCHES "Intel")
54+
add_compile_definitions(GGML_BLAS_USE_MKL)
55+
# all Intel* libraries share the same include path
56+
pkg_check_modules(DepBLAS mkl-sdl)
57+
elseif (${GGML_BLAS_VENDOR} MATCHES "NVHPC")
58+
# this doesn't provide pkg-config
59+
# suggest to assign BLAS_INCLUDE_DIRS on your own
60+
if ("${NVHPC_VERSION}" STREQUAL "")
61+
message(WARNING "Better to set NVHPC_VERSION")
62+
else()
63+
set(DepBLAS_FOUND ON)
64+
set(DepBLAS_INCLUDE_DIRS "/opt/nvidia/hpc_sdk/${CMAKE_SYSTEM_NAME}_${CMAKE_SYSTEM_PROCESSOR}/${NVHPC_VERSION}/math_libs/include")
65+
endif()
3366
endif()
34-
elseif (${GGML_BLAS_VENDOR} MATCHES "FLAME")
35-
add_compile_definitions(GGML_BLAS_USE_BLIS)
36-
pkg_check_modules(DepBLAS blis)
37-
elseif (${GGML_BLAS_VENDOR} MATCHES "ATLAS")
38-
pkg_check_modules(DepBLAS blas-atlas)
39-
elseif (${GGML_BLAS_VENDOR} MATCHES "FlexiBLAS")
40-
pkg_check_modules(DepBLAS flexiblas_api)
41-
elseif (${GGML_BLAS_VENDOR} MATCHES "Intel")
42-
add_compile_definitions(GGML_BLAS_USE_MKL)
43-
# all Intel* libraries share the same include path
44-
pkg_check_modules(DepBLAS mkl-sdl)
45-
elseif (${GGML_BLAS_VENDOR} MATCHES "NVHPC")
46-
# this doesn't provide pkg-config
47-
# suggest to assign BLAS_INCLUDE_DIRS on your own
48-
if ("${NVHPC_VERSION}" STREQUAL "")
49-
message(WARNING "Better to set NVHPC_VERSION")
67+
if (DepBLAS_FOUND)
68+
set(BLAS_INCLUDE_DIRS ${DepBLAS_INCLUDE_DIRS})
5069
else()
51-
set(DepBLAS_FOUND ON)
52-
set(DepBLAS_INCLUDE_DIRS "/opt/nvidia/hpc_sdk/${CMAKE_SYSTEM_NAME}_${CMAKE_SYSTEM_PROCESSOR}/${NVHPC_VERSION}/math_libs/include")
70+
message(WARNING "BLAS_INCLUDE_DIRS neither been provided nor been automatically"
71+
" detected by pkgconfig, trying to find cblas.h from possible paths...")
72+
find_path(BLAS_INCLUDE_DIRS
73+
NAMES cblas.h
74+
HINTS
75+
/usr/include
76+
/usr/local/include
77+
/usr/include/openblas
78+
/opt/homebrew/opt/openblas/include
79+
/usr/local/opt/openblas/include
80+
/usr/include/x86_64-linux-gnu/openblas/include
81+
)
5382
endif()
5483
endif()
55-
if (DepBLAS_FOUND)
56-
set(BLAS_INCLUDE_DIRS ${DepBLAS_INCLUDE_DIRS})
57-
else()
58-
message(WARNING "BLAS_INCLUDE_DIRS neither been provided nor been automatically"
59-
" detected by pkgconfig, trying to find cblas.h from possible paths...")
60-
find_path(BLAS_INCLUDE_DIRS
61-
NAMES cblas.h
62-
HINTS
63-
/usr/include
64-
/usr/local/include
65-
/usr/include/openblas
66-
/opt/homebrew/opt/openblas/include
67-
/usr/local/opt/openblas/include
68-
/usr/include/x86_64-linux-gnu/openblas/include
69-
)
70-
endif()
71-
endif()
7284

73-
message(STATUS "BLAS found, Includes: ${BLAS_INCLUDE_DIRS}")
85+
message(STATUS "BLAS found, Includes: ${BLAS_INCLUDE_DIRS}")
7486

75-
target_compile_options(ggml-blas PRIVATE ${BLAS_LINKER_FLAGS})
87+
target_compile_options(ggml-blas PRIVATE ${BLAS_LINKER_FLAGS})
7688

77-
if (${BLAS_INCLUDE_DIRS} MATCHES "mkl" AND (${GGML_BLAS_VENDOR} MATCHES "Generic" OR ${GGML_BLAS_VENDOR} MATCHES "Intel"))
78-
add_compile_definitions(GGML_BLAS_USE_MKL)
79-
endif()
89+
if (${BLAS_INCLUDE_DIRS} MATCHES "mkl" AND (${GGML_BLAS_VENDOR} MATCHES "Generic" OR ${GGML_BLAS_VENDOR} MATCHES "Intel"))
90+
add_compile_definitions(GGML_BLAS_USE_MKL)
91+
endif()
8092

81-
target_link_libraries (ggml-blas PRIVATE ${BLAS_LIBRARIES})
82-
target_include_directories(ggml-blas PRIVATE ${BLAS_INCLUDE_DIRS})
83-
else()
84-
message(ERROR "BLAS not found, please refer to "
85-
"https://cmake.org/cmake/help/latest/module/FindBLAS.html#blas-lapack-vendors"
86-
" to set correct GGML_BLAS_VENDOR")
93+
target_link_libraries (ggml-blas PRIVATE ${BLAS_LIBRARIES})
94+
target_include_directories(ggml-blas PRIVATE ${BLAS_INCLUDE_DIRS})
95+
else()
96+
message(ERROR "BLAS not found, please refer to "
97+
"https://cmake.org/cmake/help/latest/module/FindBLAS.html#blas-lapack-vendors"
98+
" to set correct GGML_BLAS_VENDOR")
99+
endif()
87100
endif()

ggml/src/ggml-cpu/CMakeLists.txt

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -141,21 +141,24 @@ function(ggml_add_cpu_backend_variant_impl tag_name)
141141
set(FEAT_INPUT_FILE "/dev/null")
142142
endif()
143143

144-
execute_process(
145-
COMMAND ${CMAKE_C_COMPILER} ${ARCH_FLAGS} -dM -E -
146-
INPUT_FILE ${FEAT_INPUT_FILE}
147-
OUTPUT_VARIABLE ARM_FEATURE
148-
RESULT_VARIABLE ARM_FEATURE_RESULT
149-
)
150-
if (ARM_FEATURE_RESULT)
151-
message(WARNING "Failed to get ARM features")
152-
else()
153-
foreach(feature DOTPROD SVE MATMUL_INT8 FMA FP16_VECTOR_ARITHMETIC)
154-
string(FIND "${ARM_FEATURE}" "__ARM_FEATURE_${feature} 1" feature_pos)
155-
if (NOT ${feature_pos} EQUAL -1)
156-
message(STATUS "ARM feature ${feature} enabled")
157-
endif()
158-
endforeach()
144+
# we cannot check for arm features dynamically if we are cross-compiling
145+
if(NOT CMAKE_CROSSCOMPILING)
146+
execute_process(
147+
COMMAND ${CMAKE_C_COMPILER} ${ARCH_FLAGS} -dM -E -
148+
INPUT_FILE ${FEAT_INPUT_FILE}
149+
OUTPUT_VARIABLE ARM_FEATURE
150+
RESULT_VARIABLE ARM_FEATURE_RESULT
151+
)
152+
if (ARM_FEATURE_RESULT)
153+
message(WARNING "Failed to get ARM features")
154+
else()
155+
foreach(feature DOTPROD SVE MATMUL_INT8 FMA FP16_VECTOR_ARITHMETIC)
156+
string(FIND "${ARM_FEATURE}" "__ARM_FEATURE_${feature} 1" feature_pos)
157+
if (NOT ${feature_pos} EQUAL -1)
158+
message(STATUS "ARM feature ${feature} enabled")
159+
endif()
160+
endforeach()
161+
endif()
159162
endif()
160163
endif()
161164
elseif (CMAKE_OSX_ARCHITECTURES STREQUAL "x86_64" OR CMAKE_GENERATOR_PLATFORM_LWR MATCHES "^(x86_64|i686|amd64|x64|win32)$" OR

0 commit comments

Comments
 (0)