Skip to main content

DualSessionEngine

Struct DualSessionEngine 

Source
pub struct DualSessionEngine { /* private fields */ }
Expand description

2セッション分割型推論エンジン (DualSessionEngine)。

state_session で State をエンコードし、得られた隠れ状態をキャッシュした上で、 query_session の入力スロットへ IoBinding でゼロコピーバインドして超低遅延推論を実行する。

内部にセッションプール(query_sessions、state_sessions)を保持し、 複数スレッドからの並行評価要求に対してロック競合を回避し、並列推論を実行する。

Implementations§

Source§

impl DualSessionEngine

Source

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: キャッシュマネージャー。
Source

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: キャッシュマネージャー。
Source

pub fn from_sessions( state_session: Session, query_session: Session, active_provider: ExecutionProvider, cache_manager: Arc<StateCacheManager>, ) -> Self

構築済みのセッションからエンジンインスタンスを生成する。

Source

pub fn cache_manager(&self) -> &Arc<StateCacheManager> ⓘ

保持しているキャッシュマネージャーへの参照を取得する。

Source

pub fn active_provider(&self) -> ExecutionProvider

稼働中の Execution Provider を取得する。

Source

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] の次元配列。
Source

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>>)。

Source

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 を再利用して リピートテンソルの再生成アロケーションをスキップするために使用する。

Source

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: 質問側の入力次元情報。
Source

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>)。

Source

pub fn forward_query_raw( &self, cached_state: &CachedStateFeatures, tokenized: &TokenizedQuestion, ) -> Result<Vec<f64>>

単一の質問クエリに対する各選択肢の生ロジット列 (Vec<f64>) を抽出する。

§引数
  • cached_state: エンコード済みの中間特徴量キャッシュ。
  • tokenized: トークナイズ済みの質問クエリ。
§戻り値

該当質問の各選択肢 ([OP]) に対応する生ロジット列。

Source

pub fn forward_question_raw( &self, tokenizer: &JevTokenizer, cached_state: &CachedStateFeatures, question: &Question, ) -> Result<Vec<f64>>

単一の質問定義とキャッシュ済み State から各選択肢の未正規化生ロジット列 (Vec<f64>) を直接抽出する。

§引数
  • tokenizer: Jev 高速トークナイザー。
  • cached_state: エンコード済みの中間特徴量キャッシュ。
  • question: 質問仕様。
§戻り値

各選択肢の未正規化生ロジット列。

Source

pub fn predict_single_question( &self, tokenizer: &JevTokenizer, cached_state: &CachedStateFeatures, state_text: Option<&str>, question: &Question, calib_config: &CalibrationConfig, ) -> Result<Answer>

単一の質問をキャッシュ済み State に対して推論し、決定結果 (Answer) を返却する。

§引数
  • tokenizer: Jev 高速トークナイザー。
  • cached_state: エンコード済みの中間特徴量キャッシュ。
  • state_text: 任意で渡す文脈テキスト (ゲーティングメタデータ用)。
  • question: 質問仕様。
  • calib_config: 較正温度設定。
§戻り値

確率較正済みの Answer。

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T> Instrument for T

§

fn instrument(self, span: Span) -> Instrumented<Self> ⓘ

Instruments this type with the provided [Span], returning an Instrumented wrapper. Read more
§

fn in_current_span(self) -> Instrumented<Self> ⓘ

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
where F: FnOnce(&Self) -> bool,

Converts 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
§

impl<T> Pointable for T

§

const ALIGN: usize

The alignment of pointer.
§

type Init = T

The type for initializers.
§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

§

fn vzip(self) -> V

§

impl<T> WithSubscriber for T

§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> ⓘ
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

fn with_current_subscriber(self) -> WithDispatch<Self> ⓘ

Attaches the current default Subscriber to this type, returning a [WithDispatch] wrapper. Read more