Expand description
ONNX protobuf encoding — the inverse of graph_builder
and weights (§19.1, §55.4 dump path).
Serialises an onnx_runtime_ir::Graph (plus model-level metadata that the
IR does not itself store) back into an ONNX ModelProto, then to protobuf
bytes via prost. This is the foundational capability the EPContext writer
(§55.4) builds on, but it is deliberately model-agnostic: it hardcodes no
op type, vendor, or model name.
§Round-trip contract
Everything the load path (decode → build → weights) preserves survives an
encode → decode round-trip byte-for-byte:
- nodes:
op_type,domain,input/outputorder (incl. skipped optional slots), attributes,doc_string; - graph inputs / outputs / interior
value_info(dtype + static & symbolic dims, symbols re-emitted by their interned name); - initializers: all supported dtypes, raw little-endian bytes byte-exact
(including
STRINGpayloads); - opset imports,
ir_version, producer fields, modeldoc_string,metadata_props.
§STRING attributes are byte-preserving (§55.3/§55.4)
ONNX STRING attributes are arbitrary byte strings — a compiled-vendor blob,
a relative path, or text — that are not guaranteed to be valid UTF-8. The IR
stores them as raw bytes (Attribute::String),
so both decode and encode round-trip the bytes exactly with zero op or
attribute-name knowledge. The EPContext ep_cache_context opaque blob is
preserved purely by this generic mechanism — the encoder contains no
op-specific branch.
§Fields deliberately not encoded
The IR Graph does not model these, so they cannot be reproduced and are
emitted empty / default:
TrainingInfoProto,FunctionProto, sparse initializers, quantization annotations (not represented in the IR). Model-local functions ARE supported on the load path —crate::function_inlineexpands every function call into its primitive body before the IR is built — but the IRGraphdoes not retain the originalFunctionProtodeclarations, so they are not re-emitted here;- nested
GraphProto.namevalues — the IR does not retain graph names for control-flow bodies, so nested graphs are emitted with an empty name.
Structs§
- Model
- An IR
Graphbundled with the model-level metadata and live weight bytes needed to encode a complete ONNXModelProto. - Model
Metadata - Model-level metadata that the IR
Graphdoes not itself carry.
Constants§
- DEFAULT_
IR_ VERSION - Default ONNX
ir_versionstamped whenModelMetadatadoes not override it (IR version 10, the version paired with opset 21).
Functions§
- encode_
model - Encode
modelinto serialized ONNX protobuf bytes. - encode_
model_ proto - Build the
ModelProtoformodelwithout serialising it (useful when the caller wants to mutate the proto before encoding, e.g. the §55.4 writer splicing inEPContextnodes). - write_
model - Encode
modeland write the serialized bytes topath.