@@ -152,28 +152,89 @@ if (APPLE AND NOT GGML_NO_ACCELERATE)
152152 endif ()
153153endif ()
154154
155- if (GGML_OPENBLAS)
156- set (OPENBLAS_INCLUDE_SEARCH_PATHS
157- /usr/include
158- /usr/include /openblas
159- /usr/include /openblas-base
160- /usr/local/include
161- /usr/local/include /openblas
162- /usr/local/include /openblas-base
163- /opt/OpenBLAS/include
164- $ENV{OpenBLAS_HOME}
165- $ENV{OpenBLAS_HOME} /include
166- )
167- find_path (OPENBLAS_INC NAMES cblas.h PATHS ${OPENBLAS_INCLUDE_SEARCH_PATHS} )
168- find_library (OPENBLAS_LIB NAMES openblas libopenblas)
169- if (OPENBLAS_LIB)
170- message (STATUS "OpenBLAS found" )
171-
172- set (GGML_EXTRA_LIBS ${GGML_EXTRA_LIBS} ${OPENBLAS_LIB} )
173- set (GGML_EXTRA_INCS ${GGML_EXTRA_INCS} ${OPENBLAS_INC} )
174- set (GGML_EXTRA_FLAGS ${GGML_EXTRA_FLAGS} -DGGML_USE_OPENBLAS)
155+ if (GGML_BLAS)
156+ if (GGML_STATIC)
157+ set (BLA_STATIC ON )
158+ endif ()
159+ #if (CMAKE_VERSION VERSION_GREATER_EQUAL 3.22)
160+ # set(BLA_SIZEOF_INTEGER 8)
161+ #endif()
162+
163+ set (BLA_VENDOR ${GGML_BLAS_VENDOR} )
164+ find_package (BLAS)
165+
166+ if (BLAS_FOUND)
167+ message (STATUS "BLAS found, Libraries: ${BLAS_LIBRARIES} " )
168+
169+ if (("${BLAS_INCLUDE_DIRS} " STREQUAL "" ) AND NOT (${GGML_BLAS_VENDOR} MATCHES "Apple" ))
170+ # BLAS_INCLUDE_DIRS is missing in FindBLAS.cmake.
171+ # see https://gitlab.kitware.com/cmake/cmake/-/issues/20268
172+ find_package (PkgConfig REQUIRED)
173+ if (${GGML_BLAS_VENDOR} MATCHES "Generic" )
174+ pkg_check_modules(DepBLAS REQUIRED blas)
175+ elseif (${GGML_BLAS_VENDOR} MATCHES "OpenBLAS" )
176+ # As of openblas v0.3.22, the 64-bit is named openblas64.pc
177+ pkg_check_modules(DepBLAS openblas64)
178+ if (NOT DepBLAS_FOUND)
179+ pkg_check_modules(DepBLAS REQUIRED openblas)
180+ endif ()
181+ elseif (${GGML_BLAS_VENDOR} MATCHES "FLAME" )
182+ pkg_check_modules(DepBLAS REQUIRED blis)
183+ elseif (${GGML_BLAS_VENDOR} MATCHES "ATLAS" )
184+ pkg_check_modules(DepBLAS REQUIRED blas-atlas)
185+ elseif (${GGML_BLAS_VENDOR} MATCHES "FlexiBLAS" )
186+ pkg_check_modules(DepBLAS REQUIRED flexiblas_api)
187+ elseif (${GGML_BLAS_VENDOR} MATCHES "Intel" )
188+ # all Intel* libraries share the same include path
189+ pkg_check_modules(DepBLAS REQUIRED mkl-sdl)
190+ elseif (${GGML_BLAS_VENDOR} MATCHES "NVHPC" )
191+ # this doesn't provide pkg-config
192+ # suggest to assign BLAS_INCLUDE_DIRS on your own
193+ if ("${NVHPC_VERSION} " STREQUAL "" )
194+ message (WARNING "Better to set NVHPC_VERSION" )
195+ else ()
196+ set (DepBLAS_FOUND ON )
197+ set (DepBLAS_INCLUDE_DIRS "/opt/nvidia/hpc_sdk/${CMAKE_SYSTEM_NAME} _${CMAKE_SYSTEM_PROCESSOR} /${NVHPC_VERSION} /math_libs/include" )
198+ endif ()
199+ endif ()
200+ if (DepBLAS_FOUND)
201+ set (BLAS_INCLUDE_DIRS ${DepBLAS_INCLUDE_DIRS} )
202+ else ()
203+ message (WARNING "BLAS_INCLUDE_DIRS neither been provided nor been automatically"
204+ " detected by pkgconfig, trying to find cblas.h from possible paths..." )
205+ find_path (BLAS_INCLUDE_DIRS
206+ NAMES cblas.h
207+ HINTS
208+ /usr/include
209+ /usr/local/include
210+ /usr/include /openblas
211+ /opt/homebrew/opt/openblas/include
212+ /usr/local/opt/openblas/include
213+ /usr/include /x86_64-linux-gnu/openblas/include
214+ )
215+ endif ()
216+ endif ()
217+
218+ message (STATUS "BLAS found, Includes: ${BLAS_INCLUDE_DIRS} " )
219+
220+ add_compile_options (${BLAS_LINKER_FLAGS} )
221+
222+ add_compile_definitions (GGML_USE_BLAS)
223+
224+ if (${BLAS_INCLUDE_DIRS} MATCHES "mkl" AND (${GGML_BLAS_VENDOR} MATCHES "Generic" OR ${GGML_BLAS_VENDOR} MATCHES "Intel" ))
225+ add_compile_definitions (GGML_BLAS_USE_MKL)
226+ endif ()
227+
228+ set (GGML_HEADERS_BLAS ggml-blas.h)
229+ set (GGML_SOURCES_BLAS ggml-blas.cpp)
230+
231+ set (GGML_EXTRA_LIBS ${GGML_EXTRA_LIBS} ${BLAS_LIBRARIES} )
232+ set (GGML_EXTRA_INCLUDES ${GGML_EXTRA_INCLUDES} ${BLAS_INCLUDE_DIRS} )
233+ set (GGML_EXTRA_FLAGS ${GGML_EXTRA_FLAGS} -DGGML_USE_BLAS)
175234 else ()
176- message (WARNING "OpenBLAS not found" )
235+ message (WARNING "BLAS not found, please refer to "
236+ "https://cmake.org/cmake/help/latest/module/FindBLAS.html#blas-lapack-vendors"
237+ " to set correct GGML_BLAS_VENDOR" )
177238 endif ()
178239endif ()
179240
@@ -513,9 +574,10 @@ add_library(${TARGET}
513574 ../include /ggml/ggml.h
514575 ../include /ggml/ggml-alloc.h
515576 ../include /ggml/ggml-backend.h
516- ${GGML_SOURCES_CUDA}
517- ${GGML_SOURCES_METAL}
518- ${GGML_SOURCES_RPC}
577+ ${GGML_SOURCES_CUDA} ${GGML_HEADERS_CUDA}
578+ ${GGML_SOURCES_METAL} ${GGML_HEADERS_METAL}
579+ ${GGML_SOURCES_RPC} ${GGML_HEADERS_RPC}
580+ ${GGML_SOURCES_BLAS} ${GGML_HEADERS_BLAS}
519581 )
520582
521583target_include_directories (${TARGET} PUBLIC
0 commit comments