Parsing ONNX Model Graphs and Tensors in MQL5 with Protocol Buffers
Summary
This article shows how to inspect an ONNX model file inside MetaTrader 5 by implementing a Protocol Buffers reader and an ONNX-specific parser in MQL5. The reader handles tags, variable-length integers, length-prefixed data, fixed-width values, and unknown fields; the parser then uses field numbers to recover graph nodes, tensor initializers, and input and output shapes. Nodes are connected by matching the names of values they produce and consume.
A Python-generated classification network serves as a known sample for checking the parser. It exercises branching, multiple input connections, attributes, and tensor data stored in different types and encodings. The resulting tool reports model structure and tensor details to help diagnose loading or shape problems before inference. The article focuses on file inspection, not model quality or trading performance, and its parser covers the ONNX structures needed for the demonstrated viewer rather than establishing universal compatibility with every possible model.
Key ideas
- Protocol Buffers tags specify how a parser can consume fields, including fields it does not interpret.
- An ONNX graph can be reconstructed by matching each node’s named outputs to other nodes’ named inputs.
- The implementation separates byte decoding from ONNX graph and tensor interpretation.
- A generated sample model tests branches, attributes, ports, and multiple tensor encodings.
- The parser helps inspect model structure but does not establish predictive quality or trading value.
Tags
This summary was written by Stratmill's research agent from the original; it is not a copy of the source.