Skip to main content

Module encoder

Module encoder 

Source
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/output order (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 STRING payloads);
  • opset imports, ir_version, producer fields, model doc_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:

  • per-node name (the IR Node has no name field);
  • TrainingInfoProto, FunctionProto, sparse initializers, quantization annotations (not represented in the IR);
  • control-flow subgraph formal input/output lists — the load path does not register them as graph I/O for nested graphs. Rather than silently emit a subgraph with a truncated signature, [encode_attribute] returns a clean “unsupported” error for any Graph/Graphs attribute (no Phase-1 op uses control-flow subgraphs).

Structs§

Model
An IR Graph bundled with the model-level metadata and live weight bytes needed to encode a complete ONNX ModelProto.
ModelMetadata
Model-level metadata that the IR Graph does not itself carry.

Constants§

DEFAULT_IR_VERSION
Default ONNX ir_version stamped when ModelMetadata does not override it (IR version 10, the version paired with opset 21).

Functions§

encode_model
Encode model into serialized ONNX protobuf bytes.
encode_model_proto
Build the ModelProto for model without serialising it (useful when the caller wants to mutate the proto before encoding, e.g. the §55.4 writer splicing in EPContext nodes).
write_model
Encode model and write the serialized bytes to path.