pub struct SessionBuilder { /* private fields */ }Expand description
Builder for advanced session configuration (§20.6).
Implementations§
Source§impl SessionBuilder
impl SessionBuilder
pub fn new() -> Self
pub fn model(self, path: impl AsRef<Path>) -> Self
pub fn model_bytes(self, bytes: &[u8]) -> Self
pub fn device(self, pref: DevicePreference) -> Self
pub fn memory_limit(self, bytes: usize) -> Self
pub fn profiling(self, enable: bool) -> Self
pub fn warmup(self, shapes: Vec<WarmupShape>) -> Self
Sourcepub fn option(self, key: &str, value: &str) -> Self
pub fn option(self, key: &str, value: &str) -> Self
Set a namespaced option. Unknown keys — and unknown values for a known
key — are rejected at Self::build.
§Recognized options
| Key | Values | Default | Effect |
|---|---|---|---|
"optimization" | "none", "basic", "all" | "none" | Graph optimization level (see OptimizationLevel). |
"ep.context_enable" | "0"/"1"/"false"/"true" | "0" | Dump a *_ctx.onnx EPContext model after compile (§21.4 / §55.4). |
"ep.context_file_path" | any path | <orig>_ctx.onnx | Output path for the generated context model. |
"ep.context_embed_mode" | "0" (external) / "1" (embed) | "1" | How the compiled blob is stored in each EPContext node. |
"optimization" = "none" (the default) leaves the loaded graph
untouched, so behavior is byte-identical to a runtime with no optimizer.
"basic" runs constant folding + dead-node elimination; "all" adds
operator fusion. When any pass runs, the session re-runs shape inference
on the rewritten graph before compiling so fused/introduced nodes get
inferred shapes.
Sourcepub fn build(self) -> Result<InferenceSession, SessionError>
pub fn build(self) -> Result<InferenceSession, SessionError>
Build the session: load → detect device → optimize → compile → allocate.
The optimize stage is driven by the "optimization" session option and
defaults to OptimizationLevel::None (a no-op), so the default path is
byte-identical to loading straight into the executor. When optimization
is enabled the pipeline is:
load (+ loader shape inference)
→ run optimizer passes (constant-fold / DCE / fusion)
→ re-run shape inference on the rewritten graph
→ compile (kernel per node) → allocateThe re-inference step is essential: fusion can replace a multi-op decomposition (e.g. the 9-op LayerNorm) with a single fused node whose output has no inferred shape yet, and the compile/execute stages require every value to carry a resolved shape.
Device selection is CPU-only (auto_detect yields the CPU EP), and
“compile” resolves a kernel per node into the shape-keyed cache.