Przemysław Pawełczyk commited on
Commit
d5d466c
·
unverified ·
1 Parent(s): 7e6ea10

build : use pkg-config for OpenBLAS (#1778)

Browse files

* make : use pkg-config for finding CFLAGS & LDFLAGS needed by OpenBLAS

That way building on *nix like environments (including MSYS2 on Windows)
with WHISPER_OPENBLAS=1 works out of the box.

Fix handling of WHISPER_OPENBLAS, so that empty value or 0 won't be
misinterpreted by make as enabled. Mind that it's not intended to
detect CMake false constants (OFF NO FALSE N). make is not CMake.

By default OpenBLAS with 64-bit interface is used, but that can be
changed with `WHISPER_OPENBLAS_INTERFACE64=0` if 32-bit one is desired.

If OpenBLAS headers and library are respectively in include/ and lib/
subdirectories of given path, then you can specify it, e.g.
`OPENBLAS_PATH=/usr/local/openblas`, and this will take precedence over
any pkg-config file.

If there is no pkg-config file (.pc) for OpenBLAS and OPENBLAS_PATH is
empty, then headers are assumed to be in /usr/include/openblas and
library as assumed to be called 'openblas64' (or 'openblas' if
`WHISPER_OPENBLAS_INTERFACE64=0`). If different headers location should
be used, then it can be done, e.g.
`WHISPER_BLAS_CFLAGS=-I/usr/local/include/openblas`.
If different library should be used, it can be specified, e.g.
`WHISPER_BLAS_LIB=openblasp64` (pthreads version as seen on Fedora), or
you can provide LDFLAGS needed to link with OpenBLAS directly:
`WHISPER_BLAS_LDFLAGS="-L/usr/local/lib/openblas -lopenblas64"`.

Current solution is flexible enough to handle most cases out there
without needlessly hardcoding possible OpenBLAS installation details.

* cmake : fix how pkg-config is used for finding include dirs and libraries needed by OpenBLAS

That way building on *nix like environments (including MSYS2 on Windows)
with -DWHISPER_OPENBLAS=ON should work out of the box as long as you
have CMake 3.25 or newer.

Make OPENBLAS_PATH environment variable supported not only on Windows.
It sets OpenBLAS include dir to ${OPENBLAS_PATH}/include and library to
${WHISPER_BLAS_LIB} (name without prefixes and suffixes) in
${OPENBLAS_PATH}/lib and avoids further package finding.

By default OpenBLAS with 64-bit interface is used (equivalent to setting
`-DWHISPER_BLAS_LIB=openblas64`), but that can be changed with
`-DWHISPER_OPENBLAS_INTERFACE64=OFF` (equivalent to setting
`-DWHISPER_BLAS_LIB=openblas`) if 32-bit one is desired.

Turn on BLA_STATIC for FindBLAS only when WHISPER_STATIC is enabled.
BLA_STATIC may not work as expected for pkg-config based operation.

Get rid of supporting BLAS_HOME environment variable. If OPENBLAS_PATH
is insufficient in your case, there is no pkg-config file to rely on,
then you can manually specify include dir, e.g.
`-DBLAS_INCLUDE_DIRS=/usr/local/include/openblas`, and library, e.g.
`-DBLAS_LIBRARIES=/usr/local/lib/libopenblas.so`.

* make / cmake : use OpenBLAS with 32-bit interface by default.

OpenBLAS w/o INTERFACE64=1 vel USE_64BITINT=1 seems to be more common.

* cmake : hardcode "lib" prefix for OpenBLAS lib filename (even on Windows)

* cmake : hardcode OpenBLAS library name when building in MSVC (Windows)

Most *nix like environments (including MSYS2 on Windows) have OpenBLAS
packages that allow coexistence of OpenBLAS builds with 32-bit and
64-bit interface (w/o and w/ OPENBLAS_USE64BITINT defined) and they
differ by not having or having "64" suffix in their library filenames.
That's not the case for OpenBLAS prebuilt libraries for Windows.

Files changed (2) hide show
  1. CMakeLists.txt +64 -11
  2. Makefile +22 -3
CMakeLists.txt CHANGED
@@ -74,6 +74,7 @@ else()
74
  option(WHISPER_BLAS "whisper: use BLAS libraries" OFF)
75
  option(WHISPER_BLAS_VENDOR "whisper: BLAS library vendor" Generic)
76
  option(WHISPER_OPENBLAS "whisper: prefer OpenBLAS" OFF)
 
77
  option(WHISPER_CUDA "whisper: support for CUDA" OFF)
78
  option(WHISPER_CUBLAS "whisper: support for CUDA (deprecated)" OFF)
79
  option(WHISPER_HIPBLAS "whisper: support for hipBLAS" OFF)
@@ -207,30 +208,82 @@ endif()
207
  if (WHISPER_OPENBLAS)
208
  set(WHISPER_BLAS_VENDOR "OpenBLAS")
209
  set(WHISPER_BLAS ON)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
210
  endif()
211
 
212
  if (WHISPER_BLAS)
213
- if (WIN32)
214
- if(DEFINED ENV{OPENBLAS_PATH})
215
- set(BLAS_LIBRARIES $ENV{OPENBLAS_PATH}/lib/libopenblas.dll.a)
216
- message(STATUS "Libraries ${BLAS_LIBRARIES}")
217
- set(WHISPER_EXTRA_FLAGS ${WHISPER_EXTRA_FLAGS} -DGGML_USE_OPENBLAS)
218
- include_directories($ENV{OPENBLAS_PATH}/include)
219
- set(WHISPER_EXTRA_LIBS ${WHISPER_EXTRA_LIBS} ${BLAS_LIBRARIES})
220
  else ()
221
- message(FATAL_ERROR "BLAS library was not found. Environment variable OPENBLAS_PATH not defined.")
 
 
 
 
 
 
 
 
 
 
222
  endif ()
 
 
 
 
 
 
 
 
 
 
 
223
  else ()
224
- set(BLA_STATIC 1)
 
 
 
 
 
 
 
225
  set(BLA_VENDOR ${WHISPER_BLAS_VENDOR})
226
- set(BLA_SIZEOF_INTEGER 8)
 
 
 
 
227
  set(BLA_PREFER_PKGCONFIG 1)
228
  find_package(BLAS)
229
 
230
  if(BLAS_FOUND)
231
  message(STATUS "BLAS compatible library found")
232
  message(STATUS "Libraries ${BLAS_LIBRARIES}")
233
- find_path(BLAS_INCLUDE_DIRS cblas.h /usr/include/openblas /usr/local/include/openblas $ENV{BLAS_HOME}/include)
 
 
 
 
 
 
 
234
  set(WHISPER_EXTRA_FLAGS ${WHISPER_EXTRA_FLAGS} -DGGML_USE_OPENBLAS)
235
  include_directories(${BLAS_INCLUDE_DIRS})
236
  set(WHISPER_EXTRA_LIBS ${WHISPER_EXTRA_LIBS} ${BLAS_LIBRARIES})
 
74
  option(WHISPER_BLAS "whisper: use BLAS libraries" OFF)
75
  option(WHISPER_BLAS_VENDOR "whisper: BLAS library vendor" Generic)
76
  option(WHISPER_OPENBLAS "whisper: prefer OpenBLAS" OFF)
77
+ option(WHISPER_OPENBLAS_INTERFACE64 "whisper: use OpenBLAS w/ 64-bit interface" OFF)
78
  option(WHISPER_CUDA "whisper: support for CUDA" OFF)
79
  option(WHISPER_CUBLAS "whisper: support for CUDA (deprecated)" OFF)
80
  option(WHISPER_HIPBLAS "whisper: support for hipBLAS" OFF)
 
208
  if (WHISPER_OPENBLAS)
209
  set(WHISPER_BLAS_VENDOR "OpenBLAS")
210
  set(WHISPER_BLAS ON)
211
+ # BLA_PKGCONFIG_BLAS is supported since CMake 3.25.
212
+ # FindBLAS.cmake pkg-config logic seems incomplete, because when
213
+ # BLA_SIZEOF_INTEGER is 8, then it should search for blas64 instead of blas.
214
+ # blas.pc/blas64.pc are not always provided, so let's be more specific
215
+ # and go with openblas.pc/openblas64.pc if WHISPER_OPENBLAS is on.
216
+ if (WHISPER_OPENBLAS_INTERFACE64)
217
+ set(WHISPER_BLAS_LIB "openblas64")
218
+ else ()
219
+ set(WHISPER_BLAS_LIB "openblas")
220
+ endif ()
221
+ set(BLA_PKGCONFIG_BLAS ${WHISPER_BLAS_LIB})
222
+ # OpenBLAS prebuilt libraries for Windows do not have "64" suffix in filename.
223
+ # (But .pc file has "64" suffix in filename for USE_64BITINT=1 Windows build.)
224
+ if (MSVC)
225
+ set(WHISPER_BLAS_LIB "openblas")
226
+ endif ()
227
  endif()
228
 
229
  if (WHISPER_BLAS)
230
+ if (NOT "$ENV{OPENBLAS_PATH}" STREQUAL "")
231
+ if (WHISPER_STATIC)
232
+ set(WHISPER_BLAS_LIB_PREFIX ${CMAKE_STATIC_LIBRARY_PREFIX})
233
+ set(WHISPER_BLAS_LIB_SUFFIX ${CMAKE_STATIC_LIBRARY_SUFFIX})
 
 
 
234
  else ()
235
+ if (CMAKE_IMPORT_LIBRARY_SUFFIX)
236
+ set(WHISPER_BLAS_LIB_PREFIX ${CMAKE_IMPORT_LIBRARY_PREFIX})
237
+ set(WHISPER_BLAS_LIB_SUFFIX ${CMAKE_IMPORT_LIBRARY_SUFFIX})
238
+ else ()
239
+ set(WHISPER_BLAS_LIB_PREFIX ${CMAKE_SHARED_LIBRARY_PREFIX})
240
+ set(WHISPER_BLAS_LIB_SUFFIX ${CMAKE_SHARED_LIBRARY_SUFFIX})
241
+ endif ()
242
+ endif ()
243
+ # OpenBLAS prebuilt libraries hardcode "lib" prefix in filename even on Windows
244
+ if (WHISPER_OPENBLAS)
245
+ set(WHISPER_BLAS_LIB_PREFIX "lib")
246
  endif ()
247
+ message(STATUS "BLAS compatible library path provided")
248
+ set(BLAS_LIBRARIES "$ENV{OPENBLAS_PATH}/lib/${WHISPER_BLAS_LIB_PREFIX}${WHISPER_BLAS_LIB}${WHISPER_BLAS_LIB_SUFFIX}")
249
+ message(STATUS "Libraries ${BLAS_LIBRARIES}")
250
+ set(BLAS_INCLUDE_DIRS "$ENV{OPENBLAS_PATH}/include")
251
+ message(STATUS "Include dirs ${BLAS_INCLUDE_DIRS}")
252
+ if (NOT EXISTS "${BLAS_LIBRARIES}")
253
+ message(FATAL_ERROR "BLAS library was not found. Environment variable OPENBLAS_PATH misdefined.")
254
+ endif ()
255
+ set(WHISPER_EXTRA_FLAGS ${WHISPER_EXTRA_FLAGS} -DGGML_USE_OPENBLAS)
256
+ include_directories(${BLAS_INCLUDE_DIRS})
257
+ set(WHISPER_EXTRA_LIBS ${WHISPER_EXTRA_LIBS} ${BLAS_LIBRARIES})
258
  else ()
259
+ if (WHISPER_STATIC)
260
+ # FindBLAS.cmake pkg-config logic seems incomplete, because when
261
+ # BLA_STATIC is on, then it should use pkg_check_modules_static
262
+ # instead of pkg_check_modules.
263
+ # Some manual variable overriding may be necessary if you don't
264
+ # achieve desired results.
265
+ set(BLA_STATIC 1)
266
+ endif ()
267
  set(BLA_VENDOR ${WHISPER_BLAS_VENDOR})
268
+ if (WHISPER_OPENBLAS_INTERFACE64)
269
+ set(BLA_SIZEOF_INTEGER 8)
270
+ else ()
271
+ set(BLA_SIZEOF_INTEGER 4)
272
+ endif()
273
  set(BLA_PREFER_PKGCONFIG 1)
274
  find_package(BLAS)
275
 
276
  if(BLAS_FOUND)
277
  message(STATUS "BLAS compatible library found")
278
  message(STATUS "Libraries ${BLAS_LIBRARIES}")
279
+ if (NOT DEFINED BLAS_INCLUDE_DIRS)
280
+ if (PKGC_BLAS_FOUND)
281
+ set(BLAS_INCLUDE_DIRS "${PKGC_BLAS_INCLUDE_DIRS}")
282
+ else ()
283
+ find_path(BLAS_INCLUDE_DIRS cblas.h /usr/include/openblas)
284
+ endif()
285
+ endif()
286
+ message(STATUS "Include dirs ${BLAS_INCLUDE_DIRS}")
287
  set(WHISPER_EXTRA_FLAGS ${WHISPER_EXTRA_FLAGS} -DGGML_USE_OPENBLAS)
288
  include_directories(${BLAS_INCLUDE_DIRS})
289
  set(WHISPER_EXTRA_LIBS ${WHISPER_EXTRA_LIBS} ${BLAS_LIBRARIES})
Makefile CHANGED
@@ -210,9 +210,28 @@ ifndef WHISPER_NO_METAL
210
  endif
211
  endif
212
 
213
- ifdef WHISPER_OPENBLAS
214
- CFLAGS += -DGGML_USE_OPENBLAS -I/usr/local/include/openblas -I/usr/include/openblas
215
- LDFLAGS += -lopenblas
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
216
  endif
217
 
218
  ifdef WHISPER_CUBLAS
 
210
  endif
211
  endif
212
 
213
+ ifneq ($(filter-out 0,$(WHISPER_OPENBLAS)),) # OpenBLAS
214
+ WHISPER_OPENBLAS_INTERFACE64 ?= 0 # use 32-bit interface by default
215
+ ifneq ($(filter-out 0,$(WHISPER_OPENBLAS_INTERFACE64)),)
216
+ WHISPER_BLAS_LIB := openblas64
217
+ else
218
+ WHISPER_BLAS_LIB := openblas
219
+ endif
220
+ ifneq ($(OPENBLAS_PATH),)
221
+ WHISPER_BLAS_CFLAGS := -I$(OPENBLAS_PATH)/include
222
+ WHISPER_BLAS_LDFLAGS := -L$(OPENBLAS_PATH)/lib -l$(WHISPER_BLAS_LIB)
223
+ else
224
+ WHISPER_BLAS_LIB_PC_EXISTS := $(shell pkg-config --exists $(WHISPER_BLAS_LIB) && echo 1)
225
+ ifneq ($(filter-out 0,$(WHISPER_BLAS_LIB_PC_EXISTS)),)
226
+ WHISPER_BLAS_CFLAGS := $(shell pkg-config --cflags $(WHISPER_BLAS_LIB))
227
+ WHISPER_BLAS_LDFLAGS := $(shell pkg-config --libs $(WHISPER_BLAS_LIB))
228
+ else
229
+ WHISPER_BLAS_CFLAGS := -I/usr/include/openblas
230
+ WHISPER_BLAS_LDFLAGS := -l$(WHISPER_BLAS_LIB)
231
+ endif
232
+ endif
233
+ CFLAGS += $(WHISPER_BLAS_CFLAGS) -DGGML_USE_OPENBLAS
234
+ LDFLAGS += $(WHISPER_BLAS_LDFLAGS)
235
  endif
236
 
237
  ifdef WHISPER_CUBLAS