diff --git a/CMakeLists.txt b/CMakeLists.txt index 8aff0f83078..c7562025b85 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -74,6 +74,7 @@ else() option(WHISPER_BLAS "whisper: use BLAS libraries" OFF) option(WHISPER_BLAS_VENDOR "whisper: BLAS library vendor" Generic) option(WHISPER_OPENBLAS "whisper: prefer OpenBLAS" OFF) + option(WHISPER_OPENBLAS_INTERFACE64 "whisper: use OpenBLAS w/ 64-bit interface" OFF) option(WHISPER_CUDA "whisper: support for CUDA" OFF) option(WHISPER_CUBLAS "whisper: support for CUDA (deprecated)" OFF) option(WHISPER_HIPBLAS "whisper: support for hipBLAS" OFF) @@ -207,30 +208,82 @@ endif() if (WHISPER_OPENBLAS) set(WHISPER_BLAS_VENDOR "OpenBLAS") set(WHISPER_BLAS ON) + # BLA_PKGCONFIG_BLAS is supported since CMake 3.25. + # FindBLAS.cmake pkg-config logic seems incomplete, because when + # BLA_SIZEOF_INTEGER is 8, then it should search for blas64 instead of blas. + # blas.pc/blas64.pc are not always provided, so let's be more specific + # and go with openblas.pc/openblas64.pc if WHISPER_OPENBLAS is on. + if (WHISPER_OPENBLAS_INTERFACE64) + set(WHISPER_BLAS_LIB "openblas64") + else () + set(WHISPER_BLAS_LIB "openblas") + endif () + set(BLA_PKGCONFIG_BLAS ${WHISPER_BLAS_LIB}) + # OpenBLAS prebuilt libraries for Windows do not have "64" suffix in filename. + # (But .pc file has "64" suffix in filename for USE_64BITINT=1 Windows build.) + if (MSVC) + set(WHISPER_BLAS_LIB "openblas") + endif () endif() if (WHISPER_BLAS) - if (WIN32) - if(DEFINED ENV{OPENBLAS_PATH}) - set(BLAS_LIBRARIES $ENV{OPENBLAS_PATH}/lib/libopenblas.dll.a) - message(STATUS "Libraries ${BLAS_LIBRARIES}") - set(WHISPER_EXTRA_FLAGS ${WHISPER_EXTRA_FLAGS} -DGGML_USE_OPENBLAS) - include_directories($ENV{OPENBLAS_PATH}/include) - set(WHISPER_EXTRA_LIBS ${WHISPER_EXTRA_LIBS} ${BLAS_LIBRARIES}) + if (NOT "$ENV{OPENBLAS_PATH}" STREQUAL "") + if (WHISPER_STATIC) + set(WHISPER_BLAS_LIB_PREFIX ${CMAKE_STATIC_LIBRARY_PREFIX}) + set(WHISPER_BLAS_LIB_SUFFIX ${CMAKE_STATIC_LIBRARY_SUFFIX}) else () - message(FATAL_ERROR "BLAS library was not found. Environment variable OPENBLAS_PATH not defined.") + if (CMAKE_IMPORT_LIBRARY_SUFFIX) + set(WHISPER_BLAS_LIB_PREFIX ${CMAKE_IMPORT_LIBRARY_PREFIX}) + set(WHISPER_BLAS_LIB_SUFFIX ${CMAKE_IMPORT_LIBRARY_SUFFIX}) + else () + set(WHISPER_BLAS_LIB_PREFIX ${CMAKE_SHARED_LIBRARY_PREFIX}) + set(WHISPER_BLAS_LIB_SUFFIX ${CMAKE_SHARED_LIBRARY_SUFFIX}) + endif () + endif () + # OpenBLAS prebuilt libraries hardcode "lib" prefix in filename even on Windows + if (WHISPER_OPENBLAS) + set(WHISPER_BLAS_LIB_PREFIX "lib") endif () + message(STATUS "BLAS compatible library path provided") + set(BLAS_LIBRARIES "$ENV{OPENBLAS_PATH}/lib/${WHISPER_BLAS_LIB_PREFIX}${WHISPER_BLAS_LIB}${WHISPER_BLAS_LIB_SUFFIX}") + message(STATUS "Libraries ${BLAS_LIBRARIES}") + set(BLAS_INCLUDE_DIRS "$ENV{OPENBLAS_PATH}/include") + message(STATUS "Include dirs ${BLAS_INCLUDE_DIRS}") + if (NOT EXISTS "${BLAS_LIBRARIES}") + message(FATAL_ERROR "BLAS library was not found. Environment variable OPENBLAS_PATH misdefined.") + endif () + set(WHISPER_EXTRA_FLAGS ${WHISPER_EXTRA_FLAGS} -DGGML_USE_OPENBLAS) + include_directories(${BLAS_INCLUDE_DIRS}) + set(WHISPER_EXTRA_LIBS ${WHISPER_EXTRA_LIBS} ${BLAS_LIBRARIES}) else () - set(BLA_STATIC 1) + if (WHISPER_STATIC) + # FindBLAS.cmake pkg-config logic seems incomplete, because when + # BLA_STATIC is on, then it should use pkg_check_modules_static + # instead of pkg_check_modules. + # Some manual variable overriding may be necessary if you don't + # achieve desired results. + set(BLA_STATIC 1) + endif () set(BLA_VENDOR ${WHISPER_BLAS_VENDOR}) - set(BLA_SIZEOF_INTEGER 8) + if (WHISPER_OPENBLAS_INTERFACE64) + set(BLA_SIZEOF_INTEGER 8) + else () + set(BLA_SIZEOF_INTEGER 4) + endif() set(BLA_PREFER_PKGCONFIG 1) find_package(BLAS) if(BLAS_FOUND) message(STATUS "BLAS compatible library found") message(STATUS "Libraries ${BLAS_LIBRARIES}") - find_path(BLAS_INCLUDE_DIRS cblas.h /usr/include/openblas /usr/local/include/openblas $ENV{BLAS_HOME}/include) + if (NOT DEFINED BLAS_INCLUDE_DIRS) + if (PKGC_BLAS_FOUND) + set(BLAS_INCLUDE_DIRS "${PKGC_BLAS_INCLUDE_DIRS}") + else () + find_path(BLAS_INCLUDE_DIRS cblas.h /usr/include/openblas) + endif() + endif() + message(STATUS "Include dirs ${BLAS_INCLUDE_DIRS}") set(WHISPER_EXTRA_FLAGS ${WHISPER_EXTRA_FLAGS} -DGGML_USE_OPENBLAS) include_directories(${BLAS_INCLUDE_DIRS}) set(WHISPER_EXTRA_LIBS ${WHISPER_EXTRA_LIBS} ${BLAS_LIBRARIES}) diff --git a/Makefile b/Makefile index 4cf76c6a8be..e255c52121a 100644 --- a/Makefile +++ b/Makefile @@ -210,9 +210,28 @@ ifndef WHISPER_NO_METAL endif endif -ifdef WHISPER_OPENBLAS - CFLAGS += -DGGML_USE_OPENBLAS -I/usr/local/include/openblas -I/usr/include/openblas - LDFLAGS += -lopenblas +ifneq ($(filter-out 0,$(WHISPER_OPENBLAS)),) # OpenBLAS + WHISPER_OPENBLAS_INTERFACE64 ?= 0 # use 32-bit interface by default + ifneq ($(filter-out 0,$(WHISPER_OPENBLAS_INTERFACE64)),) + WHISPER_BLAS_LIB := openblas64 + else + WHISPER_BLAS_LIB := openblas + endif + ifneq ($(OPENBLAS_PATH),) + WHISPER_BLAS_CFLAGS := -I$(OPENBLAS_PATH)/include + WHISPER_BLAS_LDFLAGS := -L$(OPENBLAS_PATH)/lib -l$(WHISPER_BLAS_LIB) + else + WHISPER_BLAS_LIB_PC_EXISTS := $(shell pkg-config --exists $(WHISPER_BLAS_LIB) && echo 1) + ifneq ($(filter-out 0,$(WHISPER_BLAS_LIB_PC_EXISTS)),) + WHISPER_BLAS_CFLAGS := $(shell pkg-config --cflags $(WHISPER_BLAS_LIB)) + WHISPER_BLAS_LDFLAGS := $(shell pkg-config --libs $(WHISPER_BLAS_LIB)) + else + WHISPER_BLAS_CFLAGS := -I/usr/include/openblas + WHISPER_BLAS_LDFLAGS := -l$(WHISPER_BLAS_LIB) + endif + endif + CFLAGS += $(WHISPER_BLAS_CFLAGS) -DGGML_USE_OPENBLAS + LDFLAGS += $(WHISPER_BLAS_LDFLAGS) endif ifdef WHISPER_CUBLAS