Skip to content

Commit 845710e

Browse files
authored
Merge pull request #12 from royshil/roy.add_openblas
Add OpenBLAS acceleration on Windows
2 parents 16b5a17 + 91b259e commit 845710e

File tree

6 files changed

+139
-28
lines changed

6 files changed

+139
-28
lines changed

.github/scripts/Build-Windows.ps1

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ param(
66
[string] $Configuration = 'RelWithDebInfo',
77
[switch] $SkipAll,
88
[switch] $SkipBuild,
9-
[switch] $SkipDeps
9+
[switch] $SkipDeps,
10+
[string] $ExtraCmakeArgs
1011
)
1112

1213
$ErrorActionPreference = 'Stop'
@@ -56,7 +57,8 @@ function Build {
5657
if ( ! ( ( $SkipAll ) -or ( $SkipBuild ) ) ) {
5758
Ensure-Location $ProjectRoot
5859

59-
$CmakeArgs = @()
60+
# take cmake args from $ExtraCmakeArgs
61+
$CmakeArgs = @($ExtraCmakeArgs)
6062
$CmakeBuildArgs = @()
6163
$CmakeInstallArgs = @()
6264

CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ project(${_name} VERSION ${_version})
77
option(ENABLE_FRONTEND_API "Use obs-frontend-api for UI functionality" ON)
88
option(ENABLE_QT "Use Qt functionality" ON)
99

10+
option(LOCALVOCAL_WITH_CUDA "Build with CUDA support. (Windows, CUDA toolkit required)" OFF)
11+
1012
include(compilerconfig)
1113
include(defaults)
1214
include(helpers)

README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,3 +77,22 @@ Use the CI scripts again, for example:
7777
```
7878

7979
The build should exist in the `./release` folder off the root. You can manually install the files in the OBS directory.
80+
81+
#### Building with CUDA support on Windows
82+
83+
To build with CUDA support on Windows, you need to install the CUDA toolkit from NVIDIA. The CUDA toolkit is available for download from [here](https://developer.nvidia.com/cuda-downloads).
84+
85+
After installing the CUDA toolkit, you need to set variables to point CMake to the CUDA toolkit installation directory. For example, if you have installed the CUDA toolkit in `C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.4`, you need to set `CUDA_TOOLKIT_ROOT_DIR` to `C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.4` and `LOCALVOCAL_WITH_CUDA` to `ON` when running `.github/scripts/Build-Windows.ps1`.
86+
87+
For example
88+
```powershell
89+
> .github/scripts/Build-Windows.ps1 -Target x64 -ExtraCmakeFlags "-D LOCALVOCAL_WITH_CUDA=ON -D CUDA_TOOLKIT_ROOT_DIR='C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.4'"
90+
```
91+
92+
You will need to copy a few CUDA .dll files to the location of the plugin .dll for it to run. The required .dll files from CUDA (which are located in the `bin` folder of the CUDA toolkit installation directory) are:
93+
94+
- `cudart64_NN.dll`
95+
- `cublas64_NN.dll`
96+
- `cublasLt64_NN.dll`
97+
98+
where `NN` is the CUDA version number. For example, if you have installed CUDA 11.4 then `NN` is likely `11`.

cmake/BuildMyCurl.cmake

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,18 @@ elseif(OS_LINUX)
1818
endif()
1919
set(BUILD_CURL_EXE OFF)
2020
set(BUILD_SHARED_LIBS OFF)
21-
set(HTTP_ONLY OFF)
21+
set(HTTP_ONLY ON)
2222
set(CURL_USE_LIBSSH2 OFF)
23+
set(CURL_DISABLE_FTP ON)
24+
set(CURL_DISABLE_LDAP ON)
25+
set(CURL_DISABLE_LDAPS ON)
26+
set(CURL_DISABLE_TELNET ON)
27+
set(CURL_DISABLE_MQTT ON)
28+
set(CURL_DISABLE_POP3 ON)
29+
set(CURL_DISABLE_RTMP ON)
30+
set(CURL_DISABLE_SMTP ON)
31+
set(CURL_DISABLE_GOPHER ON)
32+
2333
add_subdirectory(${LIBCURL_SOURCE_DIR} EXCLUDE_FROM_ALL)
2434
if(OS_MACOS)
2535
target_compile_options(

cmake/BuildWhispercpp.cmake

Lines changed: 102 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -13,38 +13,115 @@ if(UNIX AND NOT APPLE)
1313
set(WHISPER_EXTRA_CXX_FLAGS "-fPIC")
1414
endif()
1515

16-
ExternalProject_Add(
17-
Whispercpp_Build
18-
DOWNLOAD_EXTRACT_TIMESTAMP true
19-
GIT_REPOSITORY https://github.com/ggerganov/whisper.cpp.git
20-
GIT_TAG 7b374c9ac9b9861bb737eec060e4dfa29d229259
21-
BUILD_COMMAND ${CMAKE_COMMAND} --build <BINARY_DIR> --config ${Whispercpp_BUILD_TYPE}
22-
BUILD_BYPRODUCTS <INSTALL_DIR>/lib/static/${CMAKE_STATIC_LIBRARY_PREFIX}whisper${CMAKE_STATIC_LIBRARY_SUFFIX}
23-
CMAKE_GENERATOR ${CMAKE_GENERATOR}
24-
INSTALL_COMMAND ${CMAKE_COMMAND} --install <BINARY_DIR> --config ${Whispercpp_BUILD_TYPE}
25-
CMAKE_ARGS -DCMAKE_INSTALL_PREFIX=<INSTALL_DIR>
26-
-DCMAKE_BUILD_TYPE=${Whispercpp_BUILD_TYPE}
27-
-DCMAKE_GENERATOR_PLATFORM=${CMAKE_GENERATOR_PLATFORM}
28-
-DCMAKE_OSX_DEPLOYMENT_TARGET=10.13
29-
-DCMAKE_OSX_ARCHITECTURES=${CMAKE_OSX_ARCHITECTURES_}
30-
-DCMAKE_CXX_FLAGS=${WHISPER_EXTRA_CXX_FLAGS}
31-
-DCMAKE_C_FLAGS=${WHISPER_EXTRA_CXX_FLAGS}
32-
-DBUILD_SHARED_LIBS=OFF
33-
-DWHISPER_BUILD_TESTS=OFF
34-
-DWHISPER_BUILD_EXAMPLES=OFF
35-
-DWHISPER_OPENBLAS=ON)
16+
# if on Windows - download OpenBLAS prebuilt binaries
17+
if(WIN32)
18+
if(LOCALVOCAL_WITH_CUDA)
19+
# Build with CUDA Check that CUDA_TOOLKIT_ROOT_DIR is set
20+
if(NOT DEFINED CUDA_TOOLKIT_ROOT_DIR)
21+
message(FATAL_ERROR "CUDA_TOOLKIT_ROOT_DIR is not set. Please set it to the root directory of your CUDA "
22+
"installation.")
23+
endif(NOT DEFINED CUDA_TOOLKIT_ROOT_DIR)
24+
25+
set(WHISPER_ADDITIONAL_ENV "CUDAToolkit_ROOT=${CUDA_TOOLKIT_ROOT_DIR}")
26+
set(WHISPER_ADDITIONAL_CMAKE_ARGS -DWHISPER_CUBLAS=ON -DCMAKE_GENERATOR_TOOLSET=cuda=${CUDA_TOOLKIT_ROOT_DIR})
27+
else(LOCALVOCAL_WITH_CUDA)
28+
# Build with OpenBLAS
29+
set(OpenBLAS_URL "https://github.com/xianyi/OpenBLAS/releases/download/v0.3.24/OpenBLAS-0.3.24-x64.zip")
30+
set(OpenBLAS_SHA256 "8E777E406BA7030D21ADB18683D6175E4FA5ADACFBC09982C01E81245B348132")
31+
ExternalProject_Add(
32+
OpenBLAS
33+
URL ${OpenBLAS_URL}
34+
URL_HASH SHA256=${OpenBLAS_SHA256}
35+
DOWNLOAD_NO_PROGRESS true
36+
CONFIGURE_COMMAND ""
37+
BUILD_COMMAND ""
38+
INSTALL_COMMAND ${CMAKE_COMMAND} -E copy_directory <SOURCE_DIR> <INSTALL_DIR>)
39+
ExternalProject_Get_Property(OpenBLAS INSTALL_DIR)
40+
set(OpenBLAS_DIR ${INSTALL_DIR})
41+
set(WHISPER_ADDITIONAL_ENV "OPENBLAS_PATH=${OpenBLAS_DIR}")
42+
set(WHISPER_ADDITIONAL_CMAKE_ARGS -DWHISPER_BLAS=ON -DWHISPER_CUBLAS=OFF)
43+
endif(LOCALVOCAL_WITH_CUDA)
44+
45+
ExternalProject_Add(
46+
Whispercpp_Build
47+
DOWNLOAD_EXTRACT_TIMESTAMP true
48+
GIT_REPOSITORY https://github.com/ggerganov/whisper.cpp.git
49+
GIT_TAG 7b374c9ac9b9861bb737eec060e4dfa29d229259
50+
BUILD_COMMAND ${CMAKE_COMMAND} --build <BINARY_DIR> --config ${Whispercpp_BUILD_TYPE}
51+
BUILD_BYPRODUCTS
52+
<INSTALL_DIR>/lib/static/${CMAKE_STATIC_LIBRARY_PREFIX}whisper${CMAKE_STATIC_LIBRARY_SUFFIX}
53+
<INSTALL_DIR>/bin/${CMAKE_SHARED_LIBRARY_PREFIX}whisper${CMAKE_SHARED_LIBRARY_SUFFIX}
54+
<INSTALL_DIR>/lib/${CMAKE_IMPORT_LIBRARY_PREFIX}whisper${CMAKE_IMPORT_LIBRARY_SUFFIX}
55+
CMAKE_GENERATOR ${CMAKE_GENERATOR}
56+
INSTALL_COMMAND ${CMAKE_COMMAND} --install <BINARY_DIR> --config ${Whispercpp_BUILD_TYPE} && ${CMAKE_COMMAND} -E
57+
copy <BINARY_DIR>/${Whispercpp_BUILD_TYPE}/whisper.lib <INSTALL_DIR>/lib
58+
CONFIGURE_COMMAND
59+
${CMAKE_COMMAND} -E env ${WHISPER_ADDITIONAL_ENV} ${CMAKE_COMMAND} <SOURCE_DIR> -B <BINARY_DIR> -G
60+
${CMAKE_GENERATOR} -DCMAKE_INSTALL_PREFIX=<INSTALL_DIR> -DCMAKE_BUILD_TYPE=${Whispercpp_BUILD_TYPE}
61+
-DCMAKE_GENERATOR_PLATFORM=${CMAKE_GENERATOR_PLATFORM} -DCMAKE_OSX_DEPLOYMENT_TARGET=10.13
62+
-DCMAKE_OSX_ARCHITECTURES=${CMAKE_OSX_ARCHITECTURES_} -DCMAKE_CXX_FLAGS=${WHISPER_EXTRA_CXX_FLAGS}
63+
-DCMAKE_C_FLAGS=${WHISPER_EXTRA_CXX_FLAGS} -DBUILD_SHARED_LIBS=ON -DWHISPER_BUILD_TESTS=OFF
64+
-DWHISPER_BUILD_EXAMPLES=OFF ${WHISPER_ADDITIONAL_CMAKE_ARGS})
65+
66+
if(NOT LOCALVOCAL_WITH_CUDA)
67+
add_dependencies(Whispercpp_Build OpenBLAS)
68+
endif(NOT LOCALVOCAL_WITH_CUDA)
69+
else()
70+
# On Linux and MacOS build a static Whisper library
71+
ExternalProject_Add(
72+
Whispercpp_Build
73+
DOWNLOAD_EXTRACT_TIMESTAMP true
74+
GIT_REPOSITORY https://github.com/ggerganov/whisper.cpp.git
75+
GIT_TAG 7b374c9ac9b9861bb737eec060e4dfa29d229259
76+
BUILD_COMMAND ${CMAKE_COMMAND} --build <BINARY_DIR> --config ${Whispercpp_BUILD_TYPE}
77+
BUILD_BYPRODUCTS <INSTALL_DIR>/lib/static/${CMAKE_STATIC_LIBRARY_PREFIX}whisper${CMAKE_STATIC_LIBRARY_SUFFIX}
78+
CMAKE_GENERATOR ${CMAKE_GENERATOR}
79+
INSTALL_COMMAND ${CMAKE_COMMAND} --install <BINARY_DIR> --config ${Whispercpp_BUILD_TYPE}
80+
CONFIGURE_COMMAND
81+
${CMAKE_COMMAND} -E env OPENBLAS_PATH=${OpenBLAS_DIR} ${CMAKE_COMMAND} <SOURCE_DIR> -B <BINARY_DIR> -G
82+
${CMAKE_GENERATOR} -DCMAKE_INSTALL_PREFIX=<INSTALL_DIR> -DCMAKE_BUILD_TYPE=${Whispercpp_BUILD_TYPE}
83+
-DCMAKE_GENERATOR_PLATFORM=${CMAKE_GENERATOR_PLATFORM} -DCMAKE_OSX_DEPLOYMENT_TARGET=10.13
84+
-DCMAKE_OSX_ARCHITECTURES=${CMAKE_OSX_ARCHITECTURES_} -DCMAKE_CXX_FLAGS=${WHISPER_EXTRA_CXX_FLAGS}
85+
-DCMAKE_C_FLAGS=${WHISPER_EXTRA_CXX_FLAGS} -DBUILD_SHARED_LIBS=OFF -DWHISPER_BUILD_TESTS=OFF
86+
-DWHISPER_BUILD_EXAMPLES=OFF -DWHISPER_BLAS=OFF)
87+
endif(WIN32)
3688

3789
ExternalProject_Get_Property(Whispercpp_Build INSTALL_DIR)
3890

39-
add_library(Whispercpp::Whisper STATIC IMPORTED)
40-
set_target_properties(
41-
Whispercpp::Whisper
42-
PROPERTIES IMPORTED_LOCATION
43-
${INSTALL_DIR}/lib/static/${CMAKE_STATIC_LIBRARY_PREFIX}whisper${CMAKE_STATIC_LIBRARY_SUFFIX})
91+
# add the Whisper library to the link line
92+
if(WIN32)
93+
add_library(Whispercpp::Whisper SHARED IMPORTED)
94+
set_target_properties(
95+
Whispercpp::Whisper
96+
PROPERTIES IMPORTED_LOCATION ${INSTALL_DIR}/bin/${CMAKE_SHARED_LIBRARY_PREFIX}whisper${CMAKE_SHARED_LIBRARY_SUFFIX})
97+
set_target_properties(
98+
Whispercpp::Whisper
99+
PROPERTIES IMPORTED_IMPLIB ${INSTALL_DIR}/lib/${CMAKE_STATIC_LIBRARY_PREFIX}whisper${CMAKE_STATIC_LIBRARY_SUFFIX})
100+
101+
install(FILES ${INSTALL_DIR}/bin/${CMAKE_SHARED_LIBRARY_PREFIX}whisper${CMAKE_SHARED_LIBRARY_SUFFIX}
102+
DESTINATION "obs-plugins/64bit")
103+
104+
if(NOT LOCALVOCAL_WITH_CUDA)
105+
# add openblas to the link line
106+
add_library(Whispercpp::OpenBLAS STATIC IMPORTED)
107+
set_target_properties(Whispercpp::OpenBLAS PROPERTIES IMPORTED_LOCATION ${OpenBLAS_DIR}/lib/libopenblas.dll.a)
108+
install(FILES ${OpenBLAS_DIR}/bin/libopenblas.dll DESTINATION "obs-plugins/64bit")
109+
endif(NOT LOCALVOCAL_WITH_CUDA)
110+
else()
111+
# on Linux and MacOS add the static Whisper library to the link line
112+
add_library(Whispercpp::Whisper STATIC IMPORTED)
113+
set_target_properties(
114+
Whispercpp::Whisper
115+
PROPERTIES IMPORTED_LOCATION
116+
${INSTALL_DIR}/lib/static/${CMAKE_STATIC_LIBRARY_PREFIX}whisper${CMAKE_STATIC_LIBRARY_SUFFIX})
117+
endif(WIN32)
44118

45119
add_library(Whispercpp INTERFACE)
46120
add_dependencies(Whispercpp Whispercpp_Build)
47121
target_link_libraries(Whispercpp INTERFACE Whispercpp::Whisper)
122+
if(WIN32 AND NOT LOCALVOCAL_WITH_CUDA)
123+
target_link_libraries(Whispercpp INTERFACE Whispercpp::OpenBLAS)
124+
endif()
48125
set_target_properties(Whispercpp::Whisper PROPERTIES INTERFACE_INCLUDE_DIRECTORIES ${INSTALL_DIR}/include)
49126
if(APPLE)
50127
target_link_libraries(Whispercpp INTERFACE "-framework Accelerate")

src/whisper-processing.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ struct whisper_context *init_whisper_context(const std::string &model_path)
7878
obs_log(LOG_ERROR, "Failed to load whisper model");
7979
return nullptr;
8080
}
81+
obs_log(LOG_INFO, "Whisper model loaded: %s", whisper_print_system_info());
8182
return ctx;
8283
}
8384

0 commit comments

Comments
 (0)