Initial support for CUDA and OpenCL

This commit is contained in:
Zero 2023-05-05 21:17:59 -06:00
parent 330024a8af
commit 9b3bebf4b5
No known key found for this signature in database
GPG key ID: 3861E636EA1E0E2B
3 changed files with 46 additions and 42 deletions

View file

@ -21,8 +21,12 @@ dashmap = "5"
hound = "3.5.0" hound = "3.5.0"
[features] [features]
default = []
simd = [] simd = []
coreml = ["whisper-rs-sys/coreml"] coreml = ["whisper-rs-sys/coreml"]
cuda = ["whisper-rs-sys/cuda"]
opencl = ["whisper-rs-sys/opencl"]
test-with-tiny-model = [] test-with-tiny-model = []
[package.metadata.docs.rs] [package.metadata.docs.rs]

View file

@ -27,8 +27,11 @@ include = [
[features] [features]
coreml = [] coreml = []
cuda = []
opencl = []
[dependencies] [dependencies]
[build-dependencies] [build-dependencies]
bindgen = "0.64" bindgen = "0.64"
cfg-if = "1"

View file

@ -2,6 +2,7 @@
extern crate bindgen; extern crate bindgen;
use cfg_if::cfg_if;
use std::env; use std::env;
use std::path::PathBuf; use std::path::PathBuf;
@ -70,32 +71,27 @@ fn main() {
_ = std::fs::create_dir("build"); _ = std::fs::create_dir("build");
env::set_current_dir("build").expect("Unable to change directory to whisper.cpp build"); env::set_current_dir("build").expect("Unable to change directory to whisper.cpp build");
#[cfg(feature = "coreml")] let mut cmd = std::process::Command::new("cmake");
let code = std::process::Command::new("cmake") cmd.arg("..")
.arg("..")
.arg("-DCMAKE_BUILD_TYPE=Release") .arg("-DCMAKE_BUILD_TYPE=Release")
.arg("-DBUILD_SHARED_LIBS=OFF") .arg("-DBUILD_SHARED_LIBS=OFF")
.arg("-DWHISPER_ALL_WARNINGS=OFF") .arg("-DWHISPER_ALL_WARNINGS=OFF")
.arg("-DWHISPER_ALL_WARNINGS_3RD_PARTY=OFF") .arg("-DWHISPER_ALL_WARNINGS_3RD_PARTY=OFF")
.arg("-DWHISPER_BUILD_TESTS=OFF") .arg("-DWHISPER_BUILD_TESTS=OFF")
.arg("-DWHISPER_BUILD_EXAMPLES=OFF") .arg("-DWHISPER_BUILD_EXAMPLES=OFF");
.arg("-DWHISPER_COREML=1")
.status()
.expect("Failed to generate build script");
#[cfg(not(feature = "coreml"))] #[cfg(feature = "coreml")]
let code = std::process::Command::new("cmake") cmd.arg("-DWHISPER_COREML=1");
.arg("..")
.arg("-DCMAKE_BUILD_TYPE=Release") #[cfg(feature = "cuda")]
.arg("-DBUILD_SHARED_LIBS=OFF") cmd.arg("-DWHISPER_CUBLAS=1");
.arg("-DWHISPER_ALL_WARNINGS=OFF")
.arg("-DWHISPER_ALL_WARNINGS_3RD_PARTY=OFF") #[cfg(feature = "opencl")]
.arg("-DWHISPER_BUILD_TESTS=OFF") cmd.arg("-DWHISPER_CLBLAST=1");
.arg("-DWHISPER_BUILD_EXAMPLES=OFF")
.status() let code = cmd.status().expect("Failed to run `cmake`");
.expect("Failed to generate build script");
if code.code() != Some(0) { if code.code() != Some(0) {
panic!("Failed to generate build script"); panic!("Failed to run `cmake`");
} }
let code = std::process::Command::new("cmake") let code = std::process::Command::new("cmake")
@ -110,25 +106,25 @@ fn main() {
} }
// move libwhisper.a to where Cargo expects it (OUT_DIR) // move libwhisper.a to where Cargo expects it (OUT_DIR)
#[cfg(target_os = "windows")] cfg_if! {
{ if #[cfg(target_os = "windows")] {
std::fs::copy( std::fs::copy(
"Release/whisper.lib", "Release/whisper.lib",
format!("{}/whisper.lib", env::var("OUT_DIR").unwrap()), format!("{}/whisper.lib", env::var("OUT_DIR").unwrap()),
) )
.expect("Failed to copy libwhisper.lib"); .expect("Failed to copy libwhisper.lib");
} } else {
#[cfg(not(target_os = "windows"))]
{
std::fs::copy( std::fs::copy(
"libwhisper.a", "libwhisper.a",
format!("{}/libwhisper.a", env::var("OUT_DIR").unwrap()), format!("{}/libwhisper.a", env::var("OUT_DIR").unwrap()),
) )
.expect("Failed to copy libwhisper.a"); .expect("Failed to copy libwhisper.a");
} }
#[cfg(feature = "coreml")] }
#[cfg(not(target_os = "windows"))]
// 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( std::fs::copy(
"libwhisper.coreml.a", "libwhisper.coreml.a",
@ -136,6 +132,7 @@ fn main() {
) )
.expect("Failed to copy libwhisper.coreml.a"); .expect("Failed to copy libwhisper.coreml.a");
} }
}
// clean the whisper build directory to prevent Cargo from complaining during crate publish // clean the whisper build directory to prevent Cargo from complaining during crate publish
env::set_current_dir("..").expect("Unable to change directory to whisper.cpp"); env::set_current_dir("..").expect("Unable to change directory to whisper.cpp");