pub struct DualSessionEngine { /* private fields */ }Expand description
2セッション分割型推論エンジン (DualSessionEngine)。
state_session で State をエンコードし、得られた隠れ状態をキャッシュした上で、
query_session の入力スロットへ IoBinding でゼロコピーバインドして超低遅延推論を実行する。
内部にセッションプール(query_sessions、state_sessions)を保持し、
複数スレッドからの並行評価要求に対してロック競合を回避し、並列推論を実行する。
Implementations§
Source§impl DualSessionEngine
impl DualSessionEngine
Sourcepub fn new(
state_model_path: impl AsRef<Path>,
query_model_path: impl AsRef<Path>,
config: SessionConfig,
cache_manager: Arc<StateCacheManager>,
) -> Result<Self>
pub fn new( state_model_path: impl AsRef<Path>, query_model_path: impl AsRef<Path>, config: SessionConfig, cache_manager: Arc<StateCacheManager>, ) -> Result<Self>
ファイルパスからデュアルモデルを読み込み、エンジンを初期化する。
§引数
state_model_path:state_encoder.onnxファイルパス。query_model_path:query_evaluator.onnxファイルパス。config: セッション構築設定。cache_manager: キャッシュマネージャー。
Sourcepub fn from_bytes(
state_model_bytes: &[u8],
query_model_bytes: &[u8],
config: SessionConfig,
cache_manager: Arc<StateCacheManager>,
) -> Result<Self>
pub fn from_bytes( state_model_bytes: &[u8], query_model_bytes: &[u8], config: SessionConfig, cache_manager: Arc<StateCacheManager>, ) -> Result<Self>
メモリ上のバイト列からデュアルモデルを読み込み、エンジンを初期化する。
§引数
state_model_bytes:state_encoder.onnxバイナリ。query_model_bytes:query_evaluator.onnxバイナリ。config: セッション構築設定。cache_manager: キャッシュマネージャー。
Sourcepub fn from_sessions(
state_session: Session,
query_session: Session,
active_provider: ExecutionProvider,
cache_manager: Arc<StateCacheManager>,
) -> Self
pub fn from_sessions( state_session: Session, query_session: Session, active_provider: ExecutionProvider, cache_manager: Arc<StateCacheManager>, ) -> Self
構築済みのセッションからエンジンインスタンスを生成する。
Sourcepub fn cache_manager(&self) -> &Arc<StateCacheManager> ⓘ
pub fn cache_manager(&self) -> &Arc<StateCacheManager> ⓘ
保持しているキャッシュマネージャーへの参照を取得する。
Sourcepub fn active_provider(&self) -> ExecutionProvider
pub fn active_provider(&self) -> ExecutionProvider
稼働中の Execution Provider を取得する。
Sourcepub fn encode_and_cache_state(
&self,
state_text: &str,
input_ids: &[i64],
attention_mask: &[i64],
dims: [usize; 2],
) -> Result<Arc<CachedStateFeatures>>
pub fn encode_and_cache_state( &self, state_text: &str, input_ids: &[i64], attention_mask: &[i64], dims: [usize; 2], ) -> Result<Arc<CachedStateFeatures>>
State をエンコードしてキャッシュへ格納し、キャッシュエントリを返す。
既にキャッシュに存在する場合は推論を省略し、即座にキャッシュエントリを返す。
§引数
state_text: キャッシュキー生成用のState原文テキスト。input_ids: トークンID列 ([batch_size, seq_len])。attention_mask: アテンションマスク列 ([batch_size, seq_len])。dims:[batch_size, seq_len]の次元配列。
Sourcepub fn evaluate_query_with_cache(
&self,
cached_state: &CachedStateFeatures,
state_mask: &[i64],
query_ids: &[i64],
query_mask: &[i64],
op_indices: &[i64],
query_dims: ModelInputDimensions,
) -> Result<Vec<Vec<f64>>>
pub fn evaluate_query_with_cache( &self, cached_state: &CachedStateFeatures, state_mask: &[i64], query_ids: &[i64], query_mask: &[i64], op_indices: &[i64], query_dims: ModelInputDimensions, ) -> Result<Vec<Vec<f64>>>
キャッシュされた State 特徴量をゼロコピーバインドして質問を推論する。
§引数
cached_state: キャッシュされたState特徴量。state_mask:Stateマスク列 ([batch_size, state_seq_len])。query_ids: 質問トークンID列 ([batch_size, query_seq_len])。query_mask: 質問アテンションマスク列 ([batch_size, query_seq_len])。op_indices: 候補アンカーインデックス ([batch_size, num_options])。query_dims: 質問側の入力次元情報。
§戻り値
各バッチサンプルのロジット配列 (Vec<Vec<f64>>)。
Sourcepub fn evaluate_query_with_state_value(
&self,
state_val: &Value,
state_seq_len: usize,
state_mask: &[i64],
query_ids: &[i64],
query_mask: &[i64],
op_indices: &[i64],
query_dims: ModelInputDimensions,
) -> Result<Vec<Vec<f64>>>
pub fn evaluate_query_with_state_value( &self, state_val: &Value, state_seq_len: usize, state_mask: &[i64], query_ids: &[i64], query_mask: &[i64], op_indices: &[i64], query_dims: ModelInputDimensions, ) -> Result<Vec<Vec<f64>>>
構築済みの State テンソル Value を直接バインドして質問を推論する。
複数チャンクのバッチ推論において、同一チャンク長の state_val を再利用して
リピートテンソルの再生成アロケーションをスキップするために使用する。
Sourcepub fn evaluate_query_async(
engine: Arc<Self>,
cached_state: Arc<CachedStateFeatures>,
state_mask: Vec<i64>,
query_ids: Vec<i64>,
query_mask: Vec<i64>,
op_indices: Vec<i64>,
query_dims: ModelInputDimensions,
) -> JoinHandle<Result<Vec<Vec<f64>>>> ⓘ
pub fn evaluate_query_async( engine: Arc<Self>, cached_state: Arc<CachedStateFeatures>, state_mask: Vec<i64>, query_ids: Vec<i64>, query_mask: Vec<i64>, op_indices: Vec<i64>, query_dims: ModelInputDimensions, ) -> JoinHandle<Result<Vec<Vec<f64>>>> ⓘ
Tokio ワーカースレッドの枯渇を防ぐため、tokio::task::spawn_blocking で隔離して推論を実行する。
§引数
engine: デュアルセッションエンジン。cached_state: キャッシュされたState特徴量。state_mask:Stateマスク列。query_ids: 質問トークンID列。query_mask: 質問アテンションマスク列。op_indices: 候補アンカーインデックス。query_dims: 質問側の入力次元情報。
Sourcepub fn evaluate_batch_questions_with_cache(
&self,
tokenizer: &JevTokenizer,
cached_state: &CachedStateFeatures,
state_text: Option<&str>,
questions: &IndexMap<String, Question>,
calib_config: &CalibrationConfig,
gating_config: Option<&GatingConfig>,
chunk_size: usize,
) -> Result<IndexMap<String, Answer>>
pub fn evaluate_batch_questions_with_cache( &self, tokenizer: &JevTokenizer, cached_state: &CachedStateFeatures, state_text: Option<&str>, questions: &IndexMap<String, Question>, calib_config: &CalibrationConfig, gating_config: Option<&GatingConfig>, chunk_size: usize, ) -> Result<IndexMap<String, Answer>>
キャッシュされた State 特徴量をゼロコピー参照し、複数の質問を一括またはマイクロバッチ評価する。
内部で各質問の Query 部分を事前トークナイズし、chunk_size 単位のマイクロバッチとして
evaluate_query_with_cache を実行することで、L3 キャッシュ保護と並列推論を両立する。
§引数
tokenizer: 高速トークナイザー。cached_state: キャッシュされたState特徴量。state_text:Stateテキスト (ゲーティングや OOD 判定用、任意)。questions: 評価対象質問群のマップ。calib_config: 事後較正設定。gating_config: 確信度ゲーティング設定 (任意)。chunk_size: マイクロバッチ分割サイズ (OOM 防止)。
§戻り値
各質問 ID に対応する決定結果 (IndexMap<String, Answer>)。
Sourcepub fn forward_query_raw(
&self,
cached_state: &CachedStateFeatures,
tokenized: &TokenizedQuestion,
) -> Result<Vec<f64>>
pub fn forward_query_raw( &self, cached_state: &CachedStateFeatures, tokenized: &TokenizedQuestion, ) -> Result<Vec<f64>>
Sourcepub fn forward_question_raw(
&self,
tokenizer: &JevTokenizer,
cached_state: &CachedStateFeatures,
question: &Question,
) -> Result<Vec<f64>>
pub fn forward_question_raw( &self, tokenizer: &JevTokenizer, cached_state: &CachedStateFeatures, question: &Question, ) -> Result<Vec<f64>>
Sourcepub fn predict_single_question(
&self,
tokenizer: &JevTokenizer,
cached_state: &CachedStateFeatures,
state_text: Option<&str>,
question: &Question,
calib_config: &CalibrationConfig,
) -> Result<Answer>
pub fn predict_single_question( &self, tokenizer: &JevTokenizer, cached_state: &CachedStateFeatures, state_text: Option<&str>, question: &Question, calib_config: &CalibrationConfig, ) -> Result<Answer>
Auto Trait Implementations§
impl !Freeze for DualSessionEngine
impl !RefUnwindSafe for DualSessionEngine
impl !UnwindSafe for DualSessionEngine
impl Send for DualSessionEngine
impl Sync for DualSessionEngine
impl Unpin for DualSessionEngine
impl UnsafeUnpin for DualSessionEngine
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more