Skip to main content

InferenceSession

Struct InferenceSession 

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

A loaded model ready to run inference (§20.2).

Implementations§

Source§

impl InferenceSession

Source

pub fn load(path: impl AsRef<Path>) -> Result<Self, SessionError>

Primary entry point: load a model with auto device detection.

Source

pub fn load_bytes(bytes: &[u8]) -> Result<Self, SessionError>

Load a model from an in-memory buffer.

Source

pub fn from_graph(graph: Graph) -> Result<Self, SessionError>

Build a session directly from an in-memory IR Graph.

Initializer bytes are read from the graph’s inline [WeightRef]s, so no on-disk model or weight store is required. Useful for programmatically constructed graphs and tests.

Source

pub fn builder() -> SessionBuilder

Start a configuration builder.

Source

pub fn run( &mut self, inputs: &[(&str, &Tensor)], ) -> Result<Vec<Tensor>, SessionError>

Run inference with named inputs, returning the graph outputs in order.

Source

pub fn set_trace_context(&mut self, trace: TraceContext)

Attach the shared runtime trace context. When enabled, the executor opens one span per executed op so kernels can attach kernel-variant and capture-rejection reasons to a live span. Defaults to a disabled no-op context, so untraced runs pay only a single relaxed atomic load per op.

Source

pub fn run_outputs( &mut self, inputs: &[(&str, &Tensor)], ) -> Result<Vec<SessionOutput>, SessionError>

Run inference and preserve tensor or sequence graph-output types.

Source

pub fn decode_memo_counts(&self) -> (u64, u64, u64, u64)

F5 Stage 1 decode-plan memo activity counters (primed, rebuilt, replayed, ineligible) over this session’s lifetime. replayed > 0 after a decode run proves the memo actually engaged on the real (persistent-KV-binding) path; the coordinator’s on-model A/B reads this to reject a vacuous pass.

Source

pub fn decode_view_plan_counts(&self) -> (u64, u64)

F5 Stage 2 view-plan activity counters (views_reused, dispatch_elided) over this session’s lifetime. Both > 0 after a decode run prove the invariant zero-copy view reuse and pure-view dispatch elision actually fired on the real path (not a vacuous pass); an on-model A/B reads this alongside Self::decode_memo_counts.

Source

pub fn run_with_device_bindings( &mut self, inputs: &[(&str, &Tensor)], bindings: &mut [DeviceIoBinding], ) -> Result<Vec<Option<Tensor>>, SessionError>

Run with persistent device allocations supplying graph inputs and, optionally, aliasing graph outputs. Bound outputs are returned as None because their bytes remain resident in the caller-owned allocation.

Source

pub fn allocate_device_binding( &self, input_name: impl Into<String>, output_name: Option<impl Into<String>>, dtype: DataType, physical_shape: Vec<usize>, logical_shape: Vec<usize>, ) -> Result<DeviceIoBinding, SessionError>

Allocate a persistent buffer on this session’s execution device.

Source

pub fn allocate_device_output_binding( &self, output_name: impl Into<String>, dtype: DataType, physical_shape: Vec<usize>, logical_shape: Vec<usize>, ) -> Result<DeviceIoBinding, SessionError>

Allocate a persistent buffer for a graph output without also binding it as an input.

Source

pub fn try_capture_with_device_bindings( &mut self, inputs: &[(&str, &Tensor)], bindings: &mut [DeviceIoBinding], ) -> Result<DeviceGraphCaptureResult, SessionError>

Execute once while recording the kernel launches into a device graph.

NotCapturable means the mandatory all-kernel audit rejected the run before stream capture began, so callers may safely retry eagerly.

Source

pub fn replay_device_graph( &mut self, bindings: &mut [DeviceIoBinding], ) -> Result<bool, SessionError>

Replay the installed device graph after the caller has refreshed any persistent scalar inputs. Returns true when the graph is still valid for the next step, or false when a control-flow branch flip retired it this step (the token was produced correctly via eager fallback) and the caller should re-warm and re-capture.

Source

pub fn reset_device_graph(&mut self) -> Result<bool, SessionError>

Invalidate the installed device graph before reset, rewind, shape change, or binding destruction.

Source

pub fn captured_graph_segment_count(&self) -> usize

Number of captured device-graph segments installed by the most recent Self::try_capture_with_device_bindings call.

1 for a whole-subgraph capture; >= 2 when the CUDA EP claimed the subgraph but split it into segments around non-capturable seam nodes.

Source

pub fn capture_segmentation(&self) -> &[CaptureDecline]

Structured, transparent segment boundaries from the most recent capture: one entry per non-capturable seam node the EP ran eagerly between captured segments (with its structural seam kind and CaptureSupport decline reason). Empty for a whole-subgraph capture.

Source

pub fn check_device_capture_error(&self) -> Result<u32, SessionError>

Read (without clearing) any latching device capture-safety error recorded during graph replay, as a raw violation bitmask (zero when none). Callers poll this at the per-step logits sync to fail before consuming a token produced from an out-of-range captured replay.

Source

pub fn device_allocation_counts(&self) -> Option<DeviceAllocationCounts>

Source

pub fn device_id(&self) -> DeviceId

Source

pub fn execution_provider_fallback_report( &self, ) -> Option<&ExecutionProviderFallbackReport>

Report why an explicitly requested accelerator session was assigned to CPU instead. None means the requested EP serves the whole graph.

Source

pub fn inputs(&self) -> &[IoMeta]

Input metadata.

Source

pub fn outputs(&self) -> &[IoMeta]

Output metadata.

Source

pub fn model_metadata(&self) -> &ModelMetadata

Model-level metadata from the source ModelProto.

Source

pub fn cache_stats(&self) -> CacheStats

Kernel-cache statistics (§11.1); useful to observe warmup/run reuse.

Source

pub fn control_flow_stats(&self) -> ControlFlowStats

Control-flow subgraph build/run statistics. A Loop or Scan body with a stable input-shape signature should build once and run many times.

Source

pub fn warmup(&mut self, shapes: &[WarmupShape]) -> Result<(), SessionError>

Pre-compile kernels for common shapes to avoid first-inference latency (§11.3). Phase-1 minimal: the compiled plan’s shapes already key the cache, so this repopulates it for the plan; shapes are validated to name real inputs.

Source

pub fn ep_context_config(&self) -> &EpContextDumpConfig

The EPContext dump configuration parsed from the ep.context_* session options (§21.4). Disabled by default.

Source

pub fn graph(&self) -> &Graph

The session’s (post-optimize) compiled graph.

This is the graph the executor runs and the same one Self::export_ep_context serialises — a caller identifying the NodeIds of a compiled partition (the CompiledPartition::covered_nodes) must read them from here so they reference the exact nodes the exporter will splice out. This is the compiler-integration seam: a real compiling EP inspects this graph to choose the subgraphs it claims.

Source

pub fn export_ep_context( &self, orig_path: &Path, partitions: &[CompiledPartition<'_>], ) -> Result<PathBuf, SessionError>

Export a com.microsoft::EPContext context-cache model for this session (§55.4 dump path), driven by the ep.context_* session options (Self::ep_context_config).

orig_path is the source model path the default output location (<orig>_ctx.onnx) is derived from when ep.context_file_path is unset. partitions are the EP-compiled partitions to serialise — each names the ExecutionProvider that compiled it, so the driver pulls the blob + SDK version via save_context and the source key via context_source_keys (§55.6 — nothing is hardcoded).

When ep.context_enable is false (the default) this is a no-op: no EP save_context is called and no files are written; it returns the path it would have written to.

§Compiler-integration seam

The Phase-1 CPU EP has no compile step, so no real EP yet yields CompiledPartitions — partitions is therefore supplied by the caller (proven end-to-end with a mock compiling EP in the crate tests). TODO(compiler): when a real compiling EP lands, collect its partitions from the compile/placement stage and call this internally at build time so a session created with ep.context_enable=1 dumps automatically.

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

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

Source§

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.

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

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

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

Initializes a with the given initializer. Read more
Source§

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

Dereferences the given pointer. Read more
Source§

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

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

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

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.