From ac6ca3f5a4936d89715a423d2c72f247ab2eca0f Mon Sep 17 00:00:00 2001 From: Ryo Yamashita Date: Mon, 9 Dec 2024 14:10:26 +0900 Subject: [PATCH] Minor refactor --- crates/voicevox_core/src/infer.rs | 18 ++++++++---------- .../src/infer/runtimes/onnxruntime.rs | 2 +- 2 files changed, 9 insertions(+), 11 deletions(-) diff --git a/crates/voicevox_core/src/infer.rs b/crates/voicevox_core/src/infer.rs index 16b246e36..7b81b7c5d 100644 --- a/crates/voicevox_core/src/infer.rs +++ b/crates/voicevox_core/src/infer.rs @@ -3,7 +3,7 @@ mod model_file; pub(crate) mod runtimes; pub(crate) mod session_set; -use std::{borrow::Cow, collections::BTreeSet, fmt::Debug, future::Future, ops::Index, sync::Arc}; +use std::{borrow::Cow, collections::BTreeSet, fmt::Debug, ops::Index, sync::Arc}; use derive_new::new; use duplicate::duplicate_item; @@ -27,7 +27,7 @@ impl AsyncExt for SingleTasked { async fn run_session( ctx: R::RunContext, ) -> anyhow::Result> { - R::run(ctx) + R::run_blocking(ctx) } } @@ -43,7 +43,7 @@ pub(crate) trait InferenceRuntime: 'static { // TODO: "session"とは何なのかを定め、ドキュメントを書く。`InferenceSessionSet`も同様。 type Session; - // 本当は`From<'_ Self::Session>`としたいが、 rust-lang/rust#100013 がある + // 本当は`From<&'_ Self::Session>`としたいが、 rust-lang/rust#100013 が立ち塞がる type RunContext: From> + PushInputTensor; /// 名前。 @@ -70,11 +70,9 @@ pub(crate) trait InferenceRuntime: 'static { Vec>, )>; - fn run(ctx: Self::RunContext) -> anyhow::Result>; + fn run_blocking(ctx: Self::RunContext) -> anyhow::Result>; - fn run_async( - ctx: Self::RunContext, - ) -> impl Future>> + Send; + async fn run_async(ctx: Self::RunContext) -> anyhow::Result>; } /// 共に扱われるべき推論操作の集合を示す。 @@ -115,7 +113,7 @@ pub(crate) trait InferenceOperation: Copy + Enum { /// `InferenceDomain`の推論操作を表す列挙型。 /// /// `::macros::InferenceOperation`により、具体型ごと生成される。 -pub(crate) trait InferenceSignature: Sized + Send + 'static { +pub(crate) trait InferenceSignature { type Domain: InferenceDomain; type Input: InferenceInputSignature; type Output: InferenceOutputSignature; @@ -125,7 +123,7 @@ pub(crate) trait InferenceSignature: Sized + Send + 'static { /// 推論操作の入力シグネチャ。 /// /// `::macros::InferenceInputSignature`により導出される。 -pub(crate) trait InferenceInputSignature: Send + 'static { +pub(crate) trait InferenceInputSignature { type Signature: InferenceSignature; const PARAM_INFOS: &'static [ParamInfo]; fn make_run_context( @@ -189,7 +187,7 @@ pub(crate) trait PushInputTensor { /// /// `::macros::InferenceOutputSignature`により、`TryFrom`も含めて導出される。 pub(crate) trait InferenceOutputSignature: - TryFrom, Error = anyhow::Error> + Send + TryFrom, Error = anyhow::Error> { const PARAM_INFOS: &'static [ParamInfo]; } diff --git a/crates/voicevox_core/src/infer/runtimes/onnxruntime.rs b/crates/voicevox_core/src/infer/runtimes/onnxruntime.rs index 8e457bbf0..f2cc4fac7 100644 --- a/crates/voicevox_core/src/infer/runtimes/onnxruntime.rs +++ b/crates/voicevox_core/src/infer/runtimes/onnxruntime.rs @@ -182,7 +182,7 @@ impl InferenceRuntime for self::blocking::Onnxruntime { Ok((sess.into(), input_param_infos, output_param_infos)) } - fn run( + fn run_blocking( OnnxruntimeRunContext { sess, inputs }: Self::RunContext, ) -> anyhow::Result> { extract_outputs(&sess.lock_blocking().run(inputs)?)