From 16a0601123804e5b281df251c3c2461abe222cc1 Mon Sep 17 00:00:00 2001 From: Ryo Yamashita Date: Tue, 27 Aug 2024 00:23:15 +0900 Subject: [PATCH 1/3] change: liberate VOICEVOX CORE --- src/environment.rs | 4 ++-- src/session/builder.rs | 5 +++++ 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/src/environment.rs b/src/environment.rs index eb56862..83c2e8b 100644 --- a/src/environment.rs +++ b/src/environment.rs @@ -293,8 +293,8 @@ extern_system_fn! { ); match severity { - // TODO: https://github.com/VOICEVOX/voicevox_project/issues/24 をやる際に、libonnxruntime側で`WARNING`未満のログを遮断する - ort_sys::OrtLoggingLevel::ORT_LOGGING_LEVEL_VERBOSE | ort_sys::OrtLoggingLevel::ORT_LOGGING_LEVEL_INFO => {} + ort_sys::OrtLoggingLevel::ORT_LOGGING_LEVEL_VERBOSE => tracing::event!(parent: &span, Level::DEBUG, "{message}"), + ort_sys::OrtLoggingLevel::ORT_LOGGING_LEVEL_INFO => tracing::event!(parent: &span, Level::INFO, "{message}"), ort_sys::OrtLoggingLevel::ORT_LOGGING_LEVEL_WARNING => tracing::event!(parent: &span, Level::WARN, "{message}"), ort_sys::OrtLoggingLevel::ORT_LOGGING_LEVEL_ERROR | ort_sys::OrtLoggingLevel::ORT_LOGGING_LEVEL_FATAL => { tracing::event!(parent: &span, Level::ERROR, "{message}"); diff --git a/src/session/builder.rs b/src/session/builder.rs index 458c6ad..55ce22c 100644 --- a/src/session/builder.rs +++ b/src/session/builder.rs @@ -445,6 +445,11 @@ impl SessionBuilder { }; Ok(session) } + + pub fn commit_from_vv_bin(self, bin: &[u8]) -> Result { + ortsys![unsafe AddSessionConfigEntry(self.session_options_ptr.as_ptr(), c"session.decrypt_vv_model".as_ptr(), c"1".as_ptr())]; + self.commit_from_memory(bin) + } } /// ONNX Runtime provides various graph optimizations to improve performance. Graph optimizations are essentially From 26b59a810817393ad01f9e7b3fb2178e478981a2 Mon Sep 17 00:00:00 2001 From: Ryo Yamashita Date: Sun, 1 Sep 2024 04:40:30 +0900 Subject: [PATCH 2/3] `session.use_vv_bin` --- src/session/builder.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/session/builder.rs b/src/session/builder.rs index 55ce22c..35f4600 100644 --- a/src/session/builder.rs +++ b/src/session/builder.rs @@ -447,7 +447,7 @@ impl SessionBuilder { } pub fn commit_from_vv_bin(self, bin: &[u8]) -> Result { - ortsys![unsafe AddSessionConfigEntry(self.session_options_ptr.as_ptr(), c"session.decrypt_vv_model".as_ptr(), c"1".as_ptr())]; + ortsys![unsafe AddSessionConfigEntry(self.session_options_ptr.as_ptr(), c"session.use_vv_bin".as_ptr(), c"1".as_ptr())]; self.commit_from_memory(bin) } } From 5b5b22a135bd5257b31df9156d1cb6a51fbf92f0 Mon Sep 17 00:00:00 2001 From: Ryo Yamashita Date: Sun, 1 Sep 2024 04:47:55 +0900 Subject: [PATCH 3/3] =?UTF-8?q?VOICEVOX/voicevox=5Fcore#722=20=E7=94=A8?= =?UTF-8?q?=E3=82=A8=E3=83=A9=E3=83=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/error.rs | 4 +++- src/lib.rs | 25 +++++++++++++++++++++++-- src/session/builder.rs | 4 ++++ 3 files changed, 30 insertions(+), 3 deletions(-) diff --git a/src/error.rs b/src/error.rs index 2e84580..2017c57 100644 --- a/src/error.rs +++ b/src/error.rs @@ -263,7 +263,9 @@ pub enum Error { #[error("Could't get device ID from memory info: {0}")] GetDeviceId(ErrorInternal), #[error("Training API is not enabled in this build of ONNX Runtime.")] - TrainingNotEnabled + TrainingNotEnabled, + #[error("This ONNX Runtime does not support \"vv-bin\" format (note: load/link `voicevox_onnxruntime` instead of `onnxruntime`)")] + VvBinNotSupported } impl Error { diff --git a/src/lib.rs b/src/lib.rs index 8e83d6d..f5e6ed6 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -185,6 +185,7 @@ thread_local! { #[cfg_attr(docsrs, doc(cfg(feature = "__init-for-voicevox")))] #[derive(Debug)] pub struct EnvHandle { + is_voicevox_onnxruntime: bool, _env: std::sync::Arc, api: AssertSendSync>, #[cfg(feature = "load-dynamic")] @@ -265,7 +266,14 @@ pub fn try_init_from(filename: &std::ffi::OsStr, tp_options: Option) -> anyho let _env = create_env(api.0, tp_options)?; - Ok(EnvHandle { _env, api }) + let is_voicevox_onnxruntime = is_voicevox_onnxruntime(api.0); + + Ok(EnvHandle { is_voicevox_onnxruntime, _env, api }) }) } @@ -317,6 +327,17 @@ fn create_env(api: NonNull, tp_options: Option) -> bool { + unsafe { + let build_info = api.as_ref().GetBuildInfoString.expect("`GetBuildInfoString` must be present")(); + CStr::from_ptr(build_info) + .to_str() + .expect("should be UTF-8") + .starts_with("VOICEVOX ORT Build Info: ") + } +} + pub(crate) static G_ORT_API: OnceLock> = OnceLock::new(); /// Returns a pointer to the global [`ort_sys::OrtApi`] object. diff --git a/src/session/builder.rs b/src/session/builder.rs index 35f4600..f2b2576 100644 --- a/src/session/builder.rs +++ b/src/session/builder.rs @@ -446,7 +446,11 @@ impl SessionBuilder { Ok(session) } + #[cfg(feature = "__init-for-voicevox")] pub fn commit_from_vv_bin(self, bin: &[u8]) -> Result { + if !crate::EnvHandle::get().expect("should be present").is_voicevox_onnxruntime { + return Err(Error::VvBinNotSupported); + } ortsys![unsafe AddSessionConfigEntry(self.session_options_ptr.as_ptr(), c"session.use_vv_bin".as_ptr(), c"1".as_ptr())]; self.commit_from_memory(bin) }