Skip to content

Commit

Permalink
add cuda build options
Browse files Browse the repository at this point in the history
  • Loading branch information
royshil committed Sep 12, 2023
1 parent b60451f commit b92bf4d
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 24 deletions.
6 changes: 4 additions & 2 deletions .github/scripts/Build-Windows.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ param(
[string] $Configuration = 'RelWithDebInfo',
[switch] $SkipAll,
[switch] $SkipBuild,
[switch] $SkipDeps
[switch] $SkipDeps,
[string] $ExtraCmakeArgs
)

$ErrorActionPreference = 'Stop'
Expand Down Expand Up @@ -56,7 +57,8 @@ function Build {
if ( ! ( ( $SkipAll ) -or ( $SkipBuild ) ) ) {
Ensure-Location $ProjectRoot

$CmakeArgs = @()
# take cmake args from $ExtraCmakeArgs
$CmakeArgs = @($ExtraCmakeArgs)
$CmakeBuildArgs = @()
$CmakeInstallArgs = @()

Expand Down
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ project(${_name} VERSION ${_version})
option(ENABLE_FRONTEND_API "Use obs-frontend-api for UI functionality" ON)
option(ENABLE_QT "Use Qt functionality" ON)

option(LOCALVOCAL_WITH_CUDA "Build with CUDA support. (Windows, CUDA toolkit required)" OFF)

include(compilerconfig)
include(defaults)
include(helpers)
Expand Down
62 changes: 40 additions & 22 deletions cmake/BuildWhispercpp.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,34 @@ endif()

# if on Windows - download OpenBLAS prebuilt binaries
if(WIN32)
set(OpenBLAS_URL "https://github.com/xianyi/OpenBLAS/releases/download/v0.3.24/OpenBLAS-0.3.24-x64.zip")
set(OpenBLAS_SHA256 "8E777E406BA7030D21ADB18683D6175E4FA5ADACFBC09982C01E81245B348132")
ExternalProject_Add(
OpenBLAS
URL ${OpenBLAS_URL}
URL_HASH SHA256=${OpenBLAS_SHA256}
DOWNLOAD_NO_PROGRESS true
CONFIGURE_COMMAND ""
BUILD_COMMAND ""
INSTALL_COMMAND ${CMAKE_COMMAND} -E copy_directory <SOURCE_DIR> <INSTALL_DIR>)
ExternalProject_Get_Property(OpenBLAS INSTALL_DIR)
set(OpenBLAS_DIR ${INSTALL_DIR})
if(LOCALVOCAL_WITH_CUDA)
## Build with CUDA
# Check that CUDA_TOOLKIT_ROOT_DIR is set
if(NOT DEFINED CUDA_TOOLKIT_ROOT_DIR)
message(FATAL_ERROR "CUDA_TOOLKIT_ROOT_DIR is not set. Please set it to the root directory of your CUDA "
"installation.")
endif(NOT DEFINED CUDA_TOOLKIT_ROOT_DIR)

set(WHISPER_ADDITIONAL_ENV "CUDAToolkit_ROOT=${CUDA_TOOLKIT_ROOT_DIR}")
set(WHISPER_ADDITIONAL_CMAKE_ARGS -DWHISPER_CUBLAS=ON -DCMAKE_GENERATOR_TOOLSET=cuda=${CUDA_TOOLKIT_ROOT_DIR})
else(LOCALVOCAL_WITH_CUDA)
## Build with OpenBLAS
set(OpenBLAS_URL "https://github.com/xianyi/OpenBLAS/releases/download/v0.3.24/OpenBLAS-0.3.24-x64.zip")
set(OpenBLAS_SHA256 "8E777E406BA7030D21ADB18683D6175E4FA5ADACFBC09982C01E81245B348132")
ExternalProject_Add(
OpenBLAS
URL ${OpenBLAS_URL}
URL_HASH SHA256=${OpenBLAS_SHA256}
DOWNLOAD_NO_PROGRESS true
CONFIGURE_COMMAND ""
BUILD_COMMAND ""
INSTALL_COMMAND ${CMAKE_COMMAND} -E copy_directory <SOURCE_DIR> <INSTALL_DIR>)
ExternalProject_Get_Property(OpenBLAS INSTALL_DIR)
set(OpenBLAS_DIR ${INSTALL_DIR})
set(WHISPER_ADDITIONAL_ENV "OPENBLAS_PATH=${OpenBLAS_DIR}")
set(WHISPER_ADDITIONAL_CMAKE_ARGS -DWHISPER_BLAS=ON -DWHISPER_CUBLAS=OFF)
endif(LOCALVOCAL_WITH_CUDA)

# On Windows add the `-DWHISPER_BLAS=ON` flag to the cmake command as well as building a shared Whisper library
ExternalProject_Add(
Whispercpp_Build
DOWNLOAD_EXTRACT_TIMESTAMP true
Expand All @@ -43,14 +57,16 @@ if(WIN32)
INSTALL_COMMAND ${CMAKE_COMMAND} --install <BINARY_DIR> --config ${Whispercpp_BUILD_TYPE} && ${CMAKE_COMMAND} -E
copy <BINARY_DIR>/${Whispercpp_BUILD_TYPE}/whisper.lib <INSTALL_DIR>/lib
CONFIGURE_COMMAND
${CMAKE_COMMAND} -E env OPENBLAS_PATH=${OpenBLAS_DIR} ${CMAKE_COMMAND} <SOURCE_DIR> -B <BINARY_DIR> -G
${CMAKE_COMMAND} -E env ${WHISPER_ADDITIONAL_ENV} ${CMAKE_COMMAND} <SOURCE_DIR> -B <BINARY_DIR> -G
${CMAKE_GENERATOR} -DCMAKE_INSTALL_PREFIX=<INSTALL_DIR> -DCMAKE_BUILD_TYPE=${Whispercpp_BUILD_TYPE}
-DCMAKE_GENERATOR_PLATFORM=${CMAKE_GENERATOR_PLATFORM} -DCMAKE_OSX_DEPLOYMENT_TARGET=10.13
-DCMAKE_OSX_ARCHITECTURES=${CMAKE_OSX_ARCHITECTURES_} -DCMAKE_CXX_FLAGS=${WHISPER_EXTRA_CXX_FLAGS}
-DCMAKE_C_FLAGS=${WHISPER_EXTRA_CXX_FLAGS} -DBUILD_SHARED_LIBS=ON -DWHISPER_BUILD_TESTS=OFF
-DWHISPER_BUILD_EXAMPLES=OFF -DWHISPER_BLAS=ON)
-DWHISPER_BUILD_EXAMPLES=OFF ${WHISPER_ADDITIONAL_CMAKE_ARGS})

add_dependencies(Whispercpp_Build OpenBLAS)
if(NOT LOCALVOCAL_WITH_CUDA)
add_dependencies(Whispercpp_Build OpenBLAS)
endif(NOT LOCALVOCAL_WITH_CUDA)
else()
# On Linux and MacOS build a static Whisper library
ExternalProject_Add(
Expand Down Expand Up @@ -86,10 +102,12 @@ if(WIN32)
install(FILES ${INSTALL_DIR}/bin/${CMAKE_SHARED_LIBRARY_PREFIX}whisper${CMAKE_SHARED_LIBRARY_SUFFIX}
DESTINATION "obs-plugins/64bit")

# add openblas to the link line
add_library(Whispercpp::OpenBLAS STATIC IMPORTED)
set_target_properties(Whispercpp::OpenBLAS PROPERTIES IMPORTED_LOCATION ${OpenBLAS_DIR}/lib/libopenblas.dll.a)
install(FILES ${OpenBLAS_DIR}/bin/libopenblas.dll DESTINATION "obs-plugins/64bit")
if(NOT LOCALVOCAL_WITH_CUDA)
# add openblas to the link line
add_library(Whispercpp::OpenBLAS STATIC IMPORTED)
set_target_properties(Whispercpp::OpenBLAS PROPERTIES IMPORTED_LOCATION ${OpenBLAS_DIR}/lib/libopenblas.dll.a)
install(FILES ${OpenBLAS_DIR}/bin/libopenblas.dll DESTINATION "obs-plugins/64bit")
endif(NOT LOCALVOCAL_WITH_CUDA)
else()
# on Linux and MacOS add the static Whisper library to the link line
add_library(Whispercpp::Whisper STATIC IMPORTED)
Expand All @@ -102,9 +120,9 @@ endif(WIN32)
add_library(Whispercpp INTERFACE)
add_dependencies(Whispercpp Whispercpp_Build)
target_link_libraries(Whispercpp INTERFACE Whispercpp::Whisper)
if(WIN32)
if(WIN32 AND NOT LOCALVOCAL_WITH_CUDA)
target_link_libraries(Whispercpp INTERFACE Whispercpp::OpenBLAS)
endif(WIN32)
endif()
set_target_properties(Whispercpp::Whisper PROPERTIES INTERFACE_INCLUDE_DIRECTORIES ${INSTALL_DIR}/include)
if(APPLE)
target_link_libraries(Whispercpp INTERFACE "-framework Accelerate")
Expand Down
1 change: 1 addition & 0 deletions src/whisper-processing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ struct whisper_context *init_whisper_context(const std::string &model_path)
obs_log(LOG_ERROR, "Failed to load whisper model");
return nullptr;
}
obs_log(LOG_INFO, "Whisper model loaded: %s", whisper_print_system_info());
return ctx;
}

Expand Down

0 comments on commit b92bf4d

Please sign in to comment.