From 66f8aaeb86c88bf811f83c200bee9b2465351a8d Mon Sep 17 00:00:00 2001 From: Zero Date: Mon, 28 Aug 2023 15:58:28 -0600 Subject: [PATCH 1/9] Update whisper.cpp to 8e46ba80d3c1dcf532a0029f9bcdf99ce9ce7d40 --- src/standalone.rs | 18 ------------------ src/whisper_ctx.rs | 17 +++++++++++++++++ sys/whisper.cpp | 2 +- 3 files changed, 18 insertions(+), 19 deletions(-) diff --git a/src/standalone.rs b/src/standalone.rs index 318e99f6..ef7373a7 100644 --- a/src/standalone.rs +++ b/src/standalone.rs @@ -1,6 +1,5 @@ //! Standalone functions that have no associated type. -use crate::WhisperToken; use std::ffi::{c_int, CStr, CString}; /// Return the id of the specified language, returns -1 if not found @@ -54,23 +53,6 @@ pub fn get_lang_str(id: i32) -> Option<&'static str> { } } -// task tokens -/// Get the ID of the translate task token. -/// -/// # C++ equivalent -/// `whisper_token whisper_token_translate ()` -pub fn token_translate() -> WhisperToken { - unsafe { whisper_rs_sys::whisper_token_translate() } -} - -/// Get the ID of the transcribe task token. -/// -/// # C++ equivalent -/// `whisper_token whisper_token_transcribe()` -pub fn token_transcribe() -> WhisperToken { - unsafe { whisper_rs_sys::whisper_token_transcribe() } -} - /// Print system information. /// /// # C++ equivalent diff --git a/src/whisper_ctx.rs b/src/whisper_ctx.rs index c8c906c4..e035c078 100644 --- a/src/whisper_ctx.rs +++ b/src/whisper_ctx.rs @@ -432,6 +432,23 @@ impl WhisperContext { pub fn reset_timings(&self) { unsafe { whisper_rs_sys::whisper_reset_timings(self.ctx) } } + + // task tokens + /// Get the ID of the translate task token. + /// + /// # C++ equivalent + /// `whisper_token whisper_token_translate ()` + pub fn token_translate(&self) -> WhisperToken { + unsafe { whisper_rs_sys::whisper_token_translate(self.ctx) } + } + + /// Get the ID of the transcribe task token. + /// + /// # C++ equivalent + /// `whisper_token whisper_token_transcribe()` + pub fn token_transcribe(&self) -> WhisperToken { + unsafe { whisper_rs_sys::whisper_token_transcribe(self.ctx) } + } } impl Drop for WhisperContext { diff --git a/sys/whisper.cpp b/sys/whisper.cpp index a5defbc1..8e46ba80 160000 --- a/sys/whisper.cpp +++ b/sys/whisper.cpp @@ -1 +1 @@ -Subproject commit a5defbc1b98bea0f070331ce1e8b62d947b0443d +Subproject commit 8e46ba80d3c1dcf532a0029f9bcdf99ce9ce7d40 From 776abc3c91919341c691e6d941839e114b486807 Mon Sep 17 00:00:00 2001 From: Zero Date: Mon, 28 Aug 2023 16:12:00 -0600 Subject: [PATCH 2/9] Fix typo --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 38a26d3c..49ab4812 100644 --- a/README.md +++ b/README.md @@ -67,7 +67,7 @@ See [BUILDING.md](BUILDING.md) for instructions for building whisper-rs on Windo ## Troubleshooting * Something other than Windows/macOS/Linux isn't working! - * I don't have a way to test this platforms, so I can't really help you. + * I don't have a way to test these platforms, so I can't really help you. * If you can get it working, please open a PR with any changes to make it work and build instructions in BUILDING.md! * I get a panic during binding generation build! * You can attempt to fix it yourself, or you can set the `WHISPER_DONT_GENERATE_BINDINGS` environment variable. From 5c140c14d42abd4cd01b2b67a59375785ef5beaf Mon Sep 17 00:00:00 2001 From: Zero Date: Mon, 28 Aug 2023 16:28:43 -0600 Subject: [PATCH 3/9] Add OpenVINO support --- BUILDING.md | 35 ++++++++++++++++++++++++++++++++++- Cargo.toml | 1 + README.md | 4 +++- src/whisper_ctx.rs | 38 ++++++++++++++++++++++++++++++++++++++ sys/Cargo.toml | 1 + sys/build.rs | 23 +++++++++++++++++++++++ 6 files changed, 100 insertions(+), 2 deletions(-) diff --git a/BUILDING.md b/BUILDING.md index 0820aabf..0a169039 100644 --- a/BUILDING.md +++ b/BUILDING.md @@ -44,4 +44,37 @@ You also need to have CMake installed. You can obtain this using homebrew: brew install cmake ``` -CMake can also be installed from https://cmake.org/download/ but `cmake` binary needs to be in your PATH. \ No newline at end of file +CMake can also be installed from https://cmake.org/download/ but `cmake` binary needs to be in your PATH. + +# OpenVINO support + +## Development Tools +OpenVINO support requires the OpenVINO Development Tools to be installed. You can find +instructions for installing the OpenVINO Development Tools here: +https://docs.openvino.ai/2023.0/openvino_docs_install_guides_install_dev_tools.html#for-c-developers + +On Arch Linux, you can install the OpenVINO Development Tools with the following command: +``` +paru -S openvino +``` +This build may take a significant amount of time, but can save massive headaches later on. + +## Building +First, the `openvino` feature must be enabled in your Cargo.toml. + +Next, you must set the `OpenVINO_DIR` environment variable to the path where CMake can find +`OpenVINOConfig.cmake`. +This is usually in the `cmake` directory of the OpenVINO installation. + +If you used the AUR package to install OpenVINO, the location of this file is `/opt/intel/openvino/runtime/cmake`. + +``` +export OpenVINO_DIR=/opt/intel/openvino/runtime/cmake +``` + +Finally, you can build whisper-rs as normal. + +## Tested platforms +- Arch Linux + +If you have successfully built whisper-rs with OpenVINO on another platform, please open a PR to document it here! \ No newline at end of file diff --git a/Cargo.toml b/Cargo.toml index 85cabd72..ffeff0c5 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -26,6 +26,7 @@ simd = [] coreml = ["whisper-rs-sys/coreml"] cuda = ["whisper-rs-sys/cuda"] opencl = ["whisper-rs-sys/opencl"] +openvino = ["whisper-rs-sys/openvino"] test-with-tiny-model = [] [package.metadata.docs.rs] diff --git a/README.md b/README.md index 49ab4812..f7725382 100644 --- a/README.md +++ b/README.md @@ -62,7 +62,9 @@ See the docs: https://docs.rs/whisper-rs/ for more details. ## Building -See [BUILDING.md](BUILDING.md) for instructions for building whisper-rs on Windows and OSX M1. Linux builds should just work out of the box. +See [BUILDING.md](BUILDING.md) for instructions for building whisper-rs on Windows and OSX M1, +or with OpenVINO on any OS. +Besides OpenVINO, Linux builds should just work out of the box. ## Troubleshooting diff --git a/src/whisper_ctx.rs b/src/whisper_ctx.rs index e035c078..29f36858 100644 --- a/src/whisper_ctx.rs +++ b/src/whisper_ctx.rs @@ -56,6 +56,44 @@ impl WhisperContext { // we don't implement `whisper_init()` here since i have zero clue what `whisper_model_loader` does + /// Using this context, enable use of OpenVINO for encoder inference. + /// + /// # Arguments + /// * `model_path`: An optional path to the OpenVINO encoder IR model. + /// If set to `None`, + /// the path will be generated from the ggml model path + /// that was passed in to whisper_init_from_file. + /// For example, if the model path was "/path/to/ggml-base.en.bin", + /// then the OpenVINO IR model path will be assumed as "/path/to/ggml-base.en-encoder-openvino.xml". + /// + /// * `device`: The OpenVINO device to use for inference (e.g. "CPU", "GPU") + /// + /// * `cache_dir`: Optional cache directory that can speed up init time, + /// especially for GPU, by caching compiled 'blobs' there. + /// Set to nullptr if not used. + /// + /// # Returns + /// `true` on success, `false` if OpenVINO was not enabled at compile time + /// (enable the `openvino` feature flag in your Cargo.toml). + /// + /// # C++ equivalent + /// `int whisper_ctx_init_openvino_encoder(struct whisper_context * ctx, const char * model_path, const char * device, const char * cache_dir);` + #[cfg(feature = "openvino")] + pub fn init_openvino_encoder(&mut self, model_path: Option<&str>, device: &str, cache_dir: Option<&str>) -> bool { + let model_path = model_path.map(|s| CString::new(s).unwrap()); + let device = CString::new(device).unwrap(); + let cache_dir = cache_dir.map(|s| CString::new(s).unwrap()); + let ret = unsafe { + whisper_rs_sys::whisper_ctx_init_openvino_encoder( + self.ctx, + model_path.map(|s| s.as_ptr()).unwrap_or(std::ptr::null()), + device.as_ptr(), + cache_dir.map(|s| s.as_ptr()).unwrap_or(std::ptr::null()), + ) + }; + ret != 0 + } + /// Create a new state object, ready for use. /// /// # Returns diff --git a/sys/Cargo.toml b/sys/Cargo.toml index 9f0db7e3..0a284ca8 100644 --- a/sys/Cargo.toml +++ b/sys/Cargo.toml @@ -33,6 +33,7 @@ include = [ coreml = [] cuda = [] opencl = [] +openvino = [] [dependencies] diff --git a/sys/build.rs b/sys/build.rs index 2b6b1a1a..0f9b596d 100644 --- a/sys/build.rs +++ b/sys/build.rs @@ -7,6 +7,26 @@ use std::env; use std::path::PathBuf; fn main() { + // Fail-fast test for OpenVINO + #[cfg(feature = "openvino")] + if let Ok(openvino_dir) = env::var("OpenVINO_DIR") { + // see if we can find OpenVINOConfig.cmake + let openvino_config_path = PathBuf::from(&openvino_dir).join("OpenVINOConfig.cmake"); + if !openvino_config_path.exists() { + panic!( + "Couldn't find OpenVINOConfig.cmake in OpenVINO_DIR. Please set it to the path where `OpenVINOConfig.cmake` can be found.\n\ + On Arch Linux, if you installed the AUR package, this path is `/opt/intel/openvino/runtime/cmake/`.\n\ + Note the `/cmake/` at the end of the path." + ); + } + } else { + panic!( + "Couldn't find the OpenVINO_DIR environment variable. Please set it to the path where `OpenVINOConfig.cmake` can be found.\n\ + On Arch Linux, if you installed the AUR package, this path is `/opt/intel/openvino/runtime/cmake/`." + ); + } + + let target = env::var("TARGET").unwrap(); // Link C++ standard library if let Some(cpp_stdlib) = get_cpp_link_stdlib(&target) { @@ -112,6 +132,9 @@ fn main() { #[cfg(feature = "opencl")] cmd.arg("-DWHISPER_CLBLAST=ON"); + #[cfg(feature = "openvino")] + cmd.arg("-DWHISPER_OPENVINO=1"); + cmd.arg("-DCMAKE_POSITION_INDEPENDENT_CODE=ON"); let code = cmd From 851fca36d7ea525289ed02a305e47ae9b76936e5 Mon Sep 17 00:00:00 2001 From: Zero Date: Mon, 28 Aug 2023 17:04:08 -0600 Subject: [PATCH 4/9] Add new nosp token --- src/whisper_ctx.rs | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/src/whisper_ctx.rs b/src/whisper_ctx.rs index 29f36858..a144a965 100644 --- a/src/whisper_ctx.rs +++ b/src/whisper_ctx.rs @@ -405,6 +405,15 @@ impl WhisperContext { unsafe { whisper_rs_sys::whisper_token_sot(self.ctx) } } + /// Get the ID of the solm token. + /// + /// # C++ equivalent + /// `whisper_token whisper_token_solm(struct whisper_context * ctx)` + #[inline] + pub fn token_solm(&self) -> WhisperToken { + unsafe { whisper_rs_sys::whisper_token_solm(self.ctx) } + } + /// Get the ID of the prev token. /// /// # C++ equivalent @@ -414,13 +423,13 @@ impl WhisperContext { unsafe { whisper_rs_sys::whisper_token_prev(self.ctx) } } - /// Get the ID of the solm token. + /// Get the ID of the nosp token. /// /// # C++ equivalent - /// `whisper_token whisper_token_solm(struct whisper_context * ctx)` + /// `whisper_token whisper_token_nosp(struct whisper_context * ctx)` #[inline] - pub fn token_solm(&self) -> WhisperToken { - unsafe { whisper_rs_sys::whisper_token_solm(self.ctx) } + pub fn token_nosp(&self) -> WhisperToken { + unsafe { whisper_rs_sys::whisper_token_nosp(self.ctx) } } /// Get the ID of the not token. From 6668c4e2aae2f0e4e2f5a2c026f8cbc55c5210a8 Mon Sep 17 00:00:00 2001 From: Zero Date: Mon, 28 Aug 2023 17:04:38 -0600 Subject: [PATCH 5/9] Add support for tdrz and debug mode --- src/whisper_params.rs | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/src/whisper_params.rs b/src/whisper_params.rs index 923f6463..f9a2c6e0 100644 --- a/src/whisper_params.rs +++ b/src/whisper_params.rs @@ -206,6 +206,15 @@ impl<'a, 'b> FullParams<'a, 'b> { self.fp.speed_up = speed_up; } + /// # EXPERIMENTAL + /// + /// Enables debug mode, such as dumping the log mel spectrogram. + /// + /// Defaults to false. + pub fn set_debug_mode(&mut self, debug: bool) { + self.fp.debug_mode = debug; + } + /// # EXPERIMENTAL /// /// Overwrite the audio context size. 0 = default. @@ -216,6 +225,16 @@ impl<'a, 'b> FullParams<'a, 'b> { self.fp.audio_ctx = audio_ctx; } + /// # EXPERIMENTAL + /// + /// Enable tinydiarize support. + /// Experimental speaker turn detection. + /// + /// Defaults to false. + pub fn set_tdrz_enable(&mut self, tdrz_enable: bool) { + self.fp.tdrz_enable = tdrz_enable; + } + /// Set tokens to provide the model as initial input. /// /// These tokens are prepended to any existing text content from a previous call. From 749c18cfeefdc7e284c4f02b29d28b647bdbb692 Mon Sep 17 00:00:00 2001 From: Zero Date: Mon, 28 Aug 2023 17:09:46 -0600 Subject: [PATCH 6/9] `cargo fmt` --- src/whisper_ctx.rs | 7 ++++++- sys/build.rs | 1 - 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/src/whisper_ctx.rs b/src/whisper_ctx.rs index a144a965..572e57a0 100644 --- a/src/whisper_ctx.rs +++ b/src/whisper_ctx.rs @@ -79,7 +79,12 @@ impl WhisperContext { /// # C++ equivalent /// `int whisper_ctx_init_openvino_encoder(struct whisper_context * ctx, const char * model_path, const char * device, const char * cache_dir);` #[cfg(feature = "openvino")] - pub fn init_openvino_encoder(&mut self, model_path: Option<&str>, device: &str, cache_dir: Option<&str>) -> bool { + pub fn init_openvino_encoder( + &mut self, + model_path: Option<&str>, + device: &str, + cache_dir: Option<&str>, + ) -> bool { let model_path = model_path.map(|s| CString::new(s).unwrap()); let device = CString::new(device).unwrap(); let cache_dir = cache_dir.map(|s| CString::new(s).unwrap()); diff --git a/sys/build.rs b/sys/build.rs index 0f9b596d..0861f157 100644 --- a/sys/build.rs +++ b/sys/build.rs @@ -26,7 +26,6 @@ fn main() { ); } - let target = env::var("TARGET").unwrap(); // Link C++ standard library if let Some(cpp_stdlib) = get_cpp_link_stdlib(&target) { From e6cbc75430c7e3452dac5125702c02aad584a60e Mon Sep 17 00:00:00 2001 From: Niko Date: Mon, 25 Sep 2023 08:55:09 -0600 Subject: [PATCH 7/9] fix broken build --- BUILDING.md | 6 ++++-- sys/build.rs | 45 ++++++++++++++++++++++++++++++++++----------- 2 files changed, 38 insertions(+), 13 deletions(-) diff --git a/BUILDING.md b/BUILDING.md index 0a169039..fca481e8 100644 --- a/BUILDING.md +++ b/BUILDING.md @@ -63,8 +63,10 @@ This build may take a significant amount of time, but can save massive headaches First, the `openvino` feature must be enabled in your Cargo.toml. Next, you must set the `OpenVINO_DIR` environment variable to the path where CMake can find -`OpenVINOConfig.cmake`. -This is usually in the `cmake` directory of the OpenVINO installation. +`OpenVINOConfig.cmake`, and the `OpenVINO_SO_DIR` tothe path where the OpenVINO shared +libraries are located. +The former is usually in the `runtime/cmake` directory of the OpenVINO installation, and the latter +is usually in `runtime/lib/intel64`. If you used the AUR package to install OpenVINO, the location of this file is `/opt/intel/openvino/runtime/cmake`. diff --git a/sys/build.rs b/sys/build.rs index 0861f157..df72e780 100644 --- a/sys/build.rs +++ b/sys/build.rs @@ -9,21 +9,39 @@ use std::path::PathBuf; fn main() { // Fail-fast test for OpenVINO #[cfg(feature = "openvino")] - if let Ok(openvino_dir) = env::var("OpenVINO_DIR") { - // see if we can find OpenVINOConfig.cmake - let openvino_config_path = PathBuf::from(&openvino_dir).join("OpenVINOConfig.cmake"); - if !openvino_config_path.exists() { - panic!( - "Couldn't find OpenVINOConfig.cmake in OpenVINO_DIR. Please set it to the path where `OpenVINOConfig.cmake` can be found.\n\ + { + if let Ok(openvino_dir) = env::var("OpenVINO_DIR") { + // see if we can find OpenVINOConfig.cmake + let openvino_config_path = PathBuf::from(&openvino_dir).join("OpenVINOConfig.cmake"); + if !openvino_config_path.exists() { + panic!( + "Couldn't find OpenVINOConfig.cmake in OpenVINO_DIR. Please set it to the path where `OpenVINOConfig.cmake` can be found.\n\ On Arch Linux, if you installed the AUR package, this path is `/opt/intel/openvino/runtime/cmake/`.\n\ Note the `/cmake/` at the end of the path." + ); + } + } else { + panic!( + "Couldn't find the OpenVINO_DIR environment variable. Please set it to the path where `OpenVINOConfig.cmake` can be found.\n\ + On Arch Linux, if you installed the AUR package, this path is `/opt/intel/openvino/runtime/cmake/`." ); } - } else { - panic!( - "Couldn't find the OpenVINO_DIR environment variable. Please set it to the path where `OpenVINOConfig.cmake` can be found.\n\ - On Arch Linux, if you installed the AUR package, this path is `/opt/intel/openvino/runtime/cmake/`." - ); + + if let Ok(openvino_so_dir) = env::var("OpenVINO_SO_DIR") { + // check if `libopenvino.so` exists in the directory + let openvino_so_path = PathBuf::from(&openvino_so_dir).join("libopenvino.so"); + if !openvino_so_path.exists() { + panic!( + "Couldn't find libopenvino.so in OpenVINO_SO_DIR. Please set it to the path where `libopenvino.so` can be found.\n\ + On Arch Linux, if you installed the AUR package, this path is `/opt/intel/openvino/runtime/lib/intel64/`." + ); + } + } else { + panic!( + "Couldn't find the OpenVINO_SO_DIR environment variable. Please set it to the path where `libopenvino.so` can be found.\n\ + On Arch Linux, if you installed the AUR package, this path is `/opt/intel/openvino/runtime/lib/intel64/`." + ) + } } let target = env::var("TARGET").unwrap(); @@ -66,6 +84,11 @@ fn main() { } } } + #[cfg(feature = "openvino")] + { + println!("cargo:rustc-link-lib=openvino"); + println!("cargo:rustc-link-search={}", env::var("OpenVINO_SO_DIR").unwrap()); + } println!("cargo:rerun-if-changed=wrapper.h"); if env::var("WHISPER_DONT_GENERATE_BINDINGS").is_ok() { From d6da605de11dfffb4b950307cc2b084287853109 Mon Sep 17 00:00:00 2001 From: Niko Date: Fri, 6 Oct 2023 21:09:23 -0600 Subject: [PATCH 8/9] Revert "Add OpenVINO support" This reverts commit 5c140c14 --- BUILDING.md | 37 +------------------------------------ Cargo.toml | 1 - README.md | 4 +--- src/whisper_ctx.rs | 43 ------------------------------------------- sys/Cargo.toml | 1 - sys/build.rs | 45 --------------------------------------------- sys/whisper.cpp | 2 +- 7 files changed, 3 insertions(+), 130 deletions(-) diff --git a/BUILDING.md b/BUILDING.md index fca481e8..0820aabf 100644 --- a/BUILDING.md +++ b/BUILDING.md @@ -44,39 +44,4 @@ You also need to have CMake installed. You can obtain this using homebrew: brew install cmake ``` -CMake can also be installed from https://cmake.org/download/ but `cmake` binary needs to be in your PATH. - -# OpenVINO support - -## Development Tools -OpenVINO support requires the OpenVINO Development Tools to be installed. You can find -instructions for installing the OpenVINO Development Tools here: -https://docs.openvino.ai/2023.0/openvino_docs_install_guides_install_dev_tools.html#for-c-developers - -On Arch Linux, you can install the OpenVINO Development Tools with the following command: -``` -paru -S openvino -``` -This build may take a significant amount of time, but can save massive headaches later on. - -## Building -First, the `openvino` feature must be enabled in your Cargo.toml. - -Next, you must set the `OpenVINO_DIR` environment variable to the path where CMake can find -`OpenVINOConfig.cmake`, and the `OpenVINO_SO_DIR` tothe path where the OpenVINO shared -libraries are located. -The former is usually in the `runtime/cmake` directory of the OpenVINO installation, and the latter -is usually in `runtime/lib/intel64`. - -If you used the AUR package to install OpenVINO, the location of this file is `/opt/intel/openvino/runtime/cmake`. - -``` -export OpenVINO_DIR=/opt/intel/openvino/runtime/cmake -``` - -Finally, you can build whisper-rs as normal. - -## Tested platforms -- Arch Linux - -If you have successfully built whisper-rs with OpenVINO on another platform, please open a PR to document it here! \ No newline at end of file +CMake can also be installed from https://cmake.org/download/ but `cmake` binary needs to be in your PATH. \ No newline at end of file diff --git a/Cargo.toml b/Cargo.toml index ffeff0c5..85cabd72 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -26,7 +26,6 @@ simd = [] coreml = ["whisper-rs-sys/coreml"] cuda = ["whisper-rs-sys/cuda"] opencl = ["whisper-rs-sys/opencl"] -openvino = ["whisper-rs-sys/openvino"] test-with-tiny-model = [] [package.metadata.docs.rs] diff --git a/README.md b/README.md index f7725382..49ab4812 100644 --- a/README.md +++ b/README.md @@ -62,9 +62,7 @@ See the docs: https://docs.rs/whisper-rs/ for more details. ## Building -See [BUILDING.md](BUILDING.md) for instructions for building whisper-rs on Windows and OSX M1, -or with OpenVINO on any OS. -Besides OpenVINO, Linux builds should just work out of the box. +See [BUILDING.md](BUILDING.md) for instructions for building whisper-rs on Windows and OSX M1. Linux builds should just work out of the box. ## Troubleshooting diff --git a/src/whisper_ctx.rs b/src/whisper_ctx.rs index 572e57a0..350e5349 100644 --- a/src/whisper_ctx.rs +++ b/src/whisper_ctx.rs @@ -56,49 +56,6 @@ impl WhisperContext { // we don't implement `whisper_init()` here since i have zero clue what `whisper_model_loader` does - /// Using this context, enable use of OpenVINO for encoder inference. - /// - /// # Arguments - /// * `model_path`: An optional path to the OpenVINO encoder IR model. - /// If set to `None`, - /// the path will be generated from the ggml model path - /// that was passed in to whisper_init_from_file. - /// For example, if the model path was "/path/to/ggml-base.en.bin", - /// then the OpenVINO IR model path will be assumed as "/path/to/ggml-base.en-encoder-openvino.xml". - /// - /// * `device`: The OpenVINO device to use for inference (e.g. "CPU", "GPU") - /// - /// * `cache_dir`: Optional cache directory that can speed up init time, - /// especially for GPU, by caching compiled 'blobs' there. - /// Set to nullptr if not used. - /// - /// # Returns - /// `true` on success, `false` if OpenVINO was not enabled at compile time - /// (enable the `openvino` feature flag in your Cargo.toml). - /// - /// # C++ equivalent - /// `int whisper_ctx_init_openvino_encoder(struct whisper_context * ctx, const char * model_path, const char * device, const char * cache_dir);` - #[cfg(feature = "openvino")] - pub fn init_openvino_encoder( - &mut self, - model_path: Option<&str>, - device: &str, - cache_dir: Option<&str>, - ) -> bool { - let model_path = model_path.map(|s| CString::new(s).unwrap()); - let device = CString::new(device).unwrap(); - let cache_dir = cache_dir.map(|s| CString::new(s).unwrap()); - let ret = unsafe { - whisper_rs_sys::whisper_ctx_init_openvino_encoder( - self.ctx, - model_path.map(|s| s.as_ptr()).unwrap_or(std::ptr::null()), - device.as_ptr(), - cache_dir.map(|s| s.as_ptr()).unwrap_or(std::ptr::null()), - ) - }; - ret != 0 - } - /// Create a new state object, ready for use. /// /// # Returns diff --git a/sys/Cargo.toml b/sys/Cargo.toml index 0a284ca8..9f0db7e3 100644 --- a/sys/Cargo.toml +++ b/sys/Cargo.toml @@ -33,7 +33,6 @@ include = [ coreml = [] cuda = [] opencl = [] -openvino = [] [dependencies] diff --git a/sys/build.rs b/sys/build.rs index df72e780..2b6b1a1a 100644 --- a/sys/build.rs +++ b/sys/build.rs @@ -7,43 +7,6 @@ use std::env; use std::path::PathBuf; fn main() { - // Fail-fast test for OpenVINO - #[cfg(feature = "openvino")] - { - if let Ok(openvino_dir) = env::var("OpenVINO_DIR") { - // see if we can find OpenVINOConfig.cmake - let openvino_config_path = PathBuf::from(&openvino_dir).join("OpenVINOConfig.cmake"); - if !openvino_config_path.exists() { - panic!( - "Couldn't find OpenVINOConfig.cmake in OpenVINO_DIR. Please set it to the path where `OpenVINOConfig.cmake` can be found.\n\ - On Arch Linux, if you installed the AUR package, this path is `/opt/intel/openvino/runtime/cmake/`.\n\ - Note the `/cmake/` at the end of the path." - ); - } - } else { - panic!( - "Couldn't find the OpenVINO_DIR environment variable. Please set it to the path where `OpenVINOConfig.cmake` can be found.\n\ - On Arch Linux, if you installed the AUR package, this path is `/opt/intel/openvino/runtime/cmake/`." - ); - } - - if let Ok(openvino_so_dir) = env::var("OpenVINO_SO_DIR") { - // check if `libopenvino.so` exists in the directory - let openvino_so_path = PathBuf::from(&openvino_so_dir).join("libopenvino.so"); - if !openvino_so_path.exists() { - panic!( - "Couldn't find libopenvino.so in OpenVINO_SO_DIR. Please set it to the path where `libopenvino.so` can be found.\n\ - On Arch Linux, if you installed the AUR package, this path is `/opt/intel/openvino/runtime/lib/intel64/`." - ); - } - } else { - panic!( - "Couldn't find the OpenVINO_SO_DIR environment variable. Please set it to the path where `libopenvino.so` can be found.\n\ - On Arch Linux, if you installed the AUR package, this path is `/opt/intel/openvino/runtime/lib/intel64/`." - ) - } - } - let target = env::var("TARGET").unwrap(); // Link C++ standard library if let Some(cpp_stdlib) = get_cpp_link_stdlib(&target) { @@ -84,11 +47,6 @@ fn main() { } } } - #[cfg(feature = "openvino")] - { - println!("cargo:rustc-link-lib=openvino"); - println!("cargo:rustc-link-search={}", env::var("OpenVINO_SO_DIR").unwrap()); - } println!("cargo:rerun-if-changed=wrapper.h"); if env::var("WHISPER_DONT_GENERATE_BINDINGS").is_ok() { @@ -154,9 +112,6 @@ fn main() { #[cfg(feature = "opencl")] cmd.arg("-DWHISPER_CLBLAST=ON"); - #[cfg(feature = "openvino")] - cmd.arg("-DWHISPER_OPENVINO=1"); - cmd.arg("-DCMAKE_POSITION_INDEPENDENT_CODE=ON"); let code = cmd diff --git a/sys/whisper.cpp b/sys/whisper.cpp index 8e46ba80..54735be8 160000 --- a/sys/whisper.cpp +++ b/sys/whisper.cpp @@ -1 +1 @@ -Subproject commit 8e46ba80d3c1dcf532a0029f9bcdf99ce9ce7d40 +Subproject commit 54735be80f85fec2b08b09b44641b2698dbdef79 From 4b45810dcacd2ce0dca59ec268e86dda2c51927b Mon Sep 17 00:00:00 2001 From: Niko Date: Fri, 6 Oct 2023 21:49:40 -0600 Subject: [PATCH 9/9] Update whisper.cpp --- src/lib.rs | 2 ++ src/standalone.rs | 8 ++++++++ src/whisper_params.rs | 25 +++++++++++++++++++++++++ sys/whisper.cpp | 2 +- 4 files changed, 36 insertions(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index 4d30fee6..8c3ac3c0 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -24,3 +24,5 @@ pub type WhisperNewSegmentCallback = whisper_rs_sys::whisper_new_segment_callbac pub type WhisperStartEncoderCallback = whisper_rs_sys::whisper_encoder_begin_callback; pub type WhisperProgressCallback = whisper_rs_sys::whisper_progress_callback; pub type WhisperLogitsFilterCallback = whisper_rs_sys::whisper_logits_filter_callback; +pub type WhisperAbortCallback = whisper_rs_sys::whisper_abort_callback; +pub type WhisperLogCallback = whisper_rs_sys::whisper_log_callback; diff --git a/src/standalone.rs b/src/standalone.rs index ef7373a7..4928785e 100644 --- a/src/standalone.rs +++ b/src/standalone.rs @@ -53,6 +53,14 @@ pub fn get_lang_str(id: i32) -> Option<&'static str> { } } +/// Callback to control logging output: default behaviour is to print to stderr. +/// +/// # C++ equivalent +/// `void whisper_set_log_callback(whisper_log_callback callback);` +pub unsafe fn set_log_callback(callback: whisper_rs_sys::whisper_log_callback) { + unsafe { whisper_rs_sys::whisper_set_log_callback(callback) } +} + /// Print system information. /// /// # C++ equivalent diff --git a/src/whisper_params.rs b/src/whisper_params.rs index 68870f40..73e57be1 100644 --- a/src/whisper_params.rs +++ b/src/whisper_params.rs @@ -504,6 +504,31 @@ impl<'a, 'b> FullParams<'a, 'b> { ) { self.fp.logits_filter_callback_user_data = user_data; } + + /// Set the callback that is called each time before ggml computation starts. + /// + /// Note that this callback has not been Rustified yet (and likely never will be, unless someone else feels the need to do so). + /// It is still a C callback. + /// + /// # Safety + /// Do not use this function unless you know what you are doing. + /// * Be careful not to mutate the state of the whisper_context pointer returned in the callback. + /// This could cause undefined behavior, as this violates the thread-safety guarantees of the underlying C library. + /// + /// Defaults to None. + pub unsafe fn set_abort_callback(&mut self, abort_callback: crate::WhisperAbortCallback) { + self.fp.abort_callback = abort_callback; + } + + /// Set the user data to be passed to the abort callback. + /// + /// # Safety + /// See the safety notes for `set_abort_callback`. + /// + /// Defaults to None. + pub unsafe fn set_abort_callback_user_data(&mut self, user_data: *mut std::ffi::c_void) { + self.fp.abort_callback_user_data = user_data; + } } // following implementations are safe diff --git a/sys/whisper.cpp b/sys/whisper.cpp index 54735be8..91c0b233 160000 --- a/sys/whisper.cpp +++ b/sys/whisper.cpp @@ -1 +1 @@ -Subproject commit 54735be80f85fec2b08b09b44641b2698dbdef79 +Subproject commit 91c0b23384fc3725013c4b6d3b35c45ad92dea0a