From 9b3bebf4b521a2ffda433b8024a500790a9a9834 Mon Sep 17 00:00:00 2001 From: Zero Date: Fri, 5 May 2023 21:17:59 -0600 Subject: [PATCH 1/4] Initial support for CUDA and OpenCL --- Cargo.toml | 4 +++ sys/Cargo.toml | 3 ++ sys/build.rs | 81 ++++++++++++++++++++++++-------------------------- 3 files changed, 46 insertions(+), 42 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 5cac3fb..8232f7d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -21,8 +21,12 @@ dashmap = "5" hound = "3.5.0" [features] +default = [] + simd = [] coreml = ["whisper-rs-sys/coreml"] +cuda = ["whisper-rs-sys/cuda"] +opencl = ["whisper-rs-sys/opencl"] test-with-tiny-model = [] [package.metadata.docs.rs] diff --git a/sys/Cargo.toml b/sys/Cargo.toml index f73213a..80533a8 100644 --- a/sys/Cargo.toml +++ b/sys/Cargo.toml @@ -27,8 +27,11 @@ include = [ [features] coreml = [] +cuda = [] +opencl = [] [dependencies] [build-dependencies] bindgen = "0.64" +cfg-if = "1" diff --git a/sys/build.rs b/sys/build.rs index 89f1b9b..b5f2e89 100644 --- a/sys/build.rs +++ b/sys/build.rs @@ -2,6 +2,7 @@ extern crate bindgen; +use cfg_if::cfg_if; use std::env; use std::path::PathBuf; @@ -70,32 +71,27 @@ fn main() { _ = std::fs::create_dir("build"); env::set_current_dir("build").expect("Unable to change directory to whisper.cpp build"); - #[cfg(feature = "coreml")] - let code = std::process::Command::new("cmake") - .arg("..") + let mut cmd = std::process::Command::new("cmake"); + cmd.arg("..") .arg("-DCMAKE_BUILD_TYPE=Release") .arg("-DBUILD_SHARED_LIBS=OFF") .arg("-DWHISPER_ALL_WARNINGS=OFF") .arg("-DWHISPER_ALL_WARNINGS_3RD_PARTY=OFF") .arg("-DWHISPER_BUILD_TESTS=OFF") - .arg("-DWHISPER_BUILD_EXAMPLES=OFF") - .arg("-DWHISPER_COREML=1") - .status() - .expect("Failed to generate build script"); + .arg("-DWHISPER_BUILD_EXAMPLES=OFF"); - #[cfg(not(feature = "coreml"))] - let code = std::process::Command::new("cmake") - .arg("..") - .arg("-DCMAKE_BUILD_TYPE=Release") - .arg("-DBUILD_SHARED_LIBS=OFF") - .arg("-DWHISPER_ALL_WARNINGS=OFF") - .arg("-DWHISPER_ALL_WARNINGS_3RD_PARTY=OFF") - .arg("-DWHISPER_BUILD_TESTS=OFF") - .arg("-DWHISPER_BUILD_EXAMPLES=OFF") - .status() - .expect("Failed to generate build script"); + #[cfg(feature = "coreml")] + cmd.arg("-DWHISPER_COREML=1"); + + #[cfg(feature = "cuda")] + cmd.arg("-DWHISPER_CUBLAS=1"); + + #[cfg(feature = "opencl")] + cmd.arg("-DWHISPER_CLBLAST=1"); + + let code = cmd.status().expect("Failed to run `cmake`"); if code.code() != Some(0) { - panic!("Failed to generate build script"); + panic!("Failed to run `cmake`"); } let code = std::process::Command::new("cmake") @@ -110,31 +106,32 @@ fn main() { } // move libwhisper.a to where Cargo expects it (OUT_DIR) - #[cfg(target_os = "windows")] - { - std::fs::copy( - "Release/whisper.lib", - format!("{}/whisper.lib", env::var("OUT_DIR").unwrap()), - ) - .expect("Failed to copy libwhisper.lib"); + cfg_if! { + if #[cfg(target_os = "windows")] { + std::fs::copy( + "Release/whisper.lib", + format!("{}/whisper.lib", env::var("OUT_DIR").unwrap()), + ) + .expect("Failed to copy libwhisper.lib"); + } else { + std::fs::copy( + "libwhisper.a", + format!("{}/libwhisper.a", env::var("OUT_DIR").unwrap()), + ) + .expect("Failed to copy libwhisper.a"); + } } - #[cfg(not(target_os = "windows"))] - { - std::fs::copy( - "libwhisper.a", - format!("{}/libwhisper.a", env::var("OUT_DIR").unwrap()), - ) - .expect("Failed to copy libwhisper.a"); - } - #[cfg(feature = "coreml")] - #[cfg(not(target_os = "windows"))] - { - std::fs::copy( - "libwhisper.coreml.a", - format!("{}/libwhisper.coreml.a", env::var("OUT_DIR").unwrap()), - ) - .expect("Failed to copy libwhisper.coreml.a"); + // if on iOS or macOS, with coreml feature enabled, copy libwhisper.coreml.a as well + cfg_if! { + if #[cfg(all(feature = "coreml", any(target_os = "ios", target_os = "macos")))] + { + std::fs::copy( + "libwhisper.coreml.a", + format!("{}/libwhisper.coreml.a", env::var("OUT_DIR").unwrap()), + ) + .expect("Failed to copy libwhisper.coreml.a"); + } } // clean the whisper build directory to prevent Cargo from complaining during crate publish From e2f39c9c257ee93a2b02cc3ac94f4e22a871cd4c Mon Sep 17 00:00:00 2001 From: Zero Date: Fri, 5 May 2023 22:38:30 -0600 Subject: [PATCH 2/4] fix OpenCL build --- sys/build.rs | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/sys/build.rs b/sys/build.rs index b5f2e89..9d3d658 100644 --- a/sys/build.rs +++ b/sys/build.rs @@ -26,6 +26,11 @@ fn main() { println!("cargo:rustc-link-lib=static=whisper"); #[cfg(feature = "coreml")] println!("cargo:rustc-link-lib=static=whisper.coreml"); + #[cfg(feature = "opencl")] + { + println!("cargo:rustc-link-lib=clblast"); + println!("cargo:rustc-link-lib=OpenCL"); + } println!("cargo:rerun-if-changed=wrapper.h"); if env::var("WHISPER_DONT_GENERATE_BINDINGS").is_ok() { @@ -81,13 +86,13 @@ fn main() { .arg("-DWHISPER_BUILD_EXAMPLES=OFF"); #[cfg(feature = "coreml")] - cmd.arg("-DWHISPER_COREML=1"); + cmd.arg("-DWHISPER_COREML=ON"); #[cfg(feature = "cuda")] - cmd.arg("-DWHISPER_CUBLAS=1"); + cmd.arg("-DWHISPER_CUBLAS=ON"); #[cfg(feature = "opencl")] - cmd.arg("-DWHISPER_CLBLAST=1"); + cmd.arg("-DWHISPER_CLBLAST=ON"); let code = cmd.status().expect("Failed to run `cmake`"); if code.code() != Some(0) { From 9dd66a3b4bc69e1670c7937ba305b880dc41e8ec Mon Sep 17 00:00:00 2001 From: Zero Date: Fri, 5 May 2023 22:45:08 -0600 Subject: [PATCH 3/4] add libraries to CUDA search path --- sys/build.rs | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/sys/build.rs b/sys/build.rs index 9d3d658..e10e32f 100644 --- a/sys/build.rs +++ b/sys/build.rs @@ -31,6 +31,15 @@ fn main() { println!("cargo:rustc-link-lib=clblast"); println!("cargo:rustc-link-lib=OpenCL"); } + #[cfg(feature = "cuda")] + { + println!("cargo:rustc-link-lib=cublas"); + println!("cargo:rustc-link-lib=culibos"); + println!("cargo:rustc-link-lib=cudart"); + println!("cargo:rustc-link-lib=cublasLt"); + println!("cargo:rustc-link-search=/usr/local/cuda/lib64"); + println!("cargo:rustc-link-search=/opt/cuda/lib64"); + } println!("cargo:rerun-if-changed=wrapper.h"); if env::var("WHISPER_DONT_GENERATE_BINDINGS").is_ok() { From a089bb71f9c0b1bc6be82ee1ab9c24285389390f Mon Sep 17 00:00:00 2001 From: Zero Date: Sun, 14 May 2023 14:01:38 -0600 Subject: [PATCH 4/4] fix broken things from update --- sys/build.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/sys/build.rs b/sys/build.rs index e10e32f..4d63555 100644 --- a/sys/build.rs +++ b/sys/build.rs @@ -95,7 +95,8 @@ fn main() { .arg("-DWHISPER_BUILD_EXAMPLES=OFF"); #[cfg(feature = "coreml")] - cmd.arg("-DWHISPER_COREML=ON"); + cmd.arg("-DWHISPER_COREML=ON") + .arg("-DWHISPER_COREML_ALLOW_FALLBACK=1"); #[cfg(feature = "cuda")] cmd.arg("-DWHISPER_CUBLAS=ON");