Skip to main content

Crate onnx_runtime_loader

Crate onnx_runtime_loader 

Source
Expand description

§onnx-runtime-loader

Loads ONNX models from disk into the onnx_runtime_ir::Graph IR (see docs/ORT2.md §19).

Pipeline (load_model / load_model_with_weights):

  1. proto — decode the ONNX protobuf (prost types generated from the vendored onnx.proto3) into a ModelProto.
  2. [graph_builder] — build an onnx_runtime_ir::Graph (nodes, values, symbolic dim interning, opset imports), upholding the §3.5 invariants.
  3. weights — resolve inline and external initializer data (external files are memory-mapped into a WeightStore).
  4. Static/symbolic shape inference via onnx-runtime-shape-inference: the loader owns the “loader = shape-inference” seam, so after the Graph is built (with initializers applied) it runs the extensible per-op registry to populate every value’s shape and dtype. Values that cannot be resolved statically (genuinely data-dependent extents) are left symbolic for the session to resolve just-in-time.

§Obtaining weight bytes at session time

Use load_model_with_weights (or load_model_bytes_with_weights) to receive both the Graph and an Arc<WeightStore>. Then, given any onnx_runtime_ir::WeightRef stored in graph.initializers, call WeightStore::bytes to get the raw little-endian byte slice:

let (graph, store) = load_model_with_weights("model.onnx")?;
for (vid, weight_ref) in &graph.initializers {
    let bytes: &[u8] = store.bytes(weight_ref).expect("weight bytes live");
    // ... hand bytes to a kernel
}

The Arc keeps all memory maps alive as long as any clone of it exists, so kernel dispatch can store Arc<WeightStore> alongside the Graph without lifetime coupling.

Re-exports§

pub use encoder::DEFAULT_IR_VERSION;
pub use encoder::Model;
pub use encoder::ModelMetadata;
pub use encoder::encode_model;
pub use encoder::encode_model_proto;
pub use encoder::write_model;
pub use epcontext::EmbedMode;
pub use epcontext::EpContextBlob;
pub use epcontext::EpContextNode;
pub use epcontext::ep_context_node_ids;
pub use epcontext::ep_context_nodes;
pub use epcontext::is_ep_context_op;
pub use epcontext::resolve_ep_context;
pub use weights::ExpertQuantization;
pub use weights::ExpertStorageOrder;
pub use weights::ExpertTensorLayout;
pub use weights::ExpertWeightRegion;
pub use weights::NonPageableReason;
pub use weights::Pageability;
pub use weights::WeightRegionCatalog;
pub use weights::WeightStore;
pub use writer::EpContextDumpConfig;
pub use writer::EpContextPartition;
pub use writer::dump_ep_context;

Modules§

encoder
ONNX protobuf encoding — the inverse of graph_builder and weights (§19.1, §55.4 dump path).
epcontext
com.microsoft::EPContext node support — the load path (§55.3).
function_inline
Model-local function inlining (ONNX function expansion) at load time.
proto
ONNX protobuf decoding (§19.1).
weights
Initializer weight resolution: inline data and external mmap (§19.2, §12).
writer
com.microsoft::EPContext dump / writer path (§55.4) — the inverse of the load path in crate::epcontext.

Enums§

LoaderError
Errors produced while loading an ONNX model.

Functions§

load_model
Load a model from a filesystem path, producing a fully-built Graph.
load_model_bytes
Load a model from an in-memory protobuf buffer, producing a Graph.
load_model_bytes_with_weights
Load a model from an in-memory protobuf buffer, returning both the Graph and the live WeightStore that backs all initializer data.
load_model_with_weights
Load a model from a filesystem path, returning both the Graph and the live WeightStore that backs all initializer data.
validate_model
Fail-fast, load-time validation of everything statically knowable to be illegal or unsupported (RULES #1: fail at load, never via a silent sentinel at run time).
validate_model_proto
Validate ONNX model metadata and protobuf-level graph invariants that are intentionally not preserved by the runtime IR (notably ir_version and AttributeProto::ref_attr_name).
validate_no_control_flow
Reject subgraph-bearing (control-flow) ops the runtime cannot execute.
validate_no_dangling_refs
Reject graphs with a node input that has no source.
validate_no_initializer_producer
Reject graphs where an initializer value is also produced by a node.
validate_opset_imports
ONNX treats "" and "ai.onnx" as equivalent spellings of the default domain. Model-level imports also govern nodes nested in subgraphs.