Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:Onnx Onnx Text Format Parser

From Leeroopedia


Knowledge Sources
Domains Parsing, Text Format, Model Serialization
Last Updated 2026-02-10 00:00 GMT

Overview

parser.cc implements the ONNX text format parser that converts human-readable ONNX syntax into protobuf structures, enabling users to express ONNX models, graphs, nodes, and functions in a concise textual representation.

Description

This file provides the core implementation of the ONNX text format parser, split across two classes:

ParserBase::Parse(Literal&) handles low-level literal parsing including string literals (with escape sequence handling), integer literals, float literals (including scientific notation with exponents), and special float values (nan, inf, infinity). The parser uses character-by-character scanning with explicit position tracking for error reporting.

OnnxParser extends ParserBase to parse all ONNX protobuf structures from text. Key parse methods include:

  • Parse(IdList&) -- Parses comma-separated identifier lists with support for empty identifiers (used for optional node inputs/outputs). Handles backward-compatible syntax where trailing commas and empty strings are treated equivalently.
  • Parse(TensorShapeProto&) -- Parses tensor shape dimensions including symbolic parameters (e.g., "N"), concrete integer values, and unknown dimensions ("?").
  • Parse(TypeProto&) -- Parses ONNX type specifications including primitive tensor types with shapes (e.g., "float[3,4]"), sequence types ("seq(float[3])"), map types ("map(int64, float)"), optional types, and sparse tensor types.
  • Parse(AttributeProto&) -- Parses attribute values with optional type annotations, supporting ints, floats, strings, tensors, graphs, type protos, and list variants of each. Handles attribute references ("@ref_name") for function attributes.
  • Parse(NodeProto&) -- Parses complete node definitions with optional names, outputs, domain-qualified op types, overloads, attributes, and inputs.
  • Parse(GraphProto&) -- Parses graph definitions including inputs with optional initializers, outputs, value info, and node lists.
  • Parse(FunctionProto&) -- Parses function definitions with metadata (opset imports, domain, doc string, overload), typed inputs/outputs, attribute declarations, and function body nodes.
  • Parse(ModelProto&) -- Parses complete model definitions including IR version, opset imports, producer info, metadata, the main graph, and any associated functions.
  • Parse(TensorProto&) -- Parses tensor data with type-specific handling for int, float, double, string, and other data types. Supports both inline data (curly braces) and external data references (square brackets).

The parser uses macro-based error handling (PARSE_TOKEN, PARSE, MATCH) that propagate Common::Status errors through the parsing chain. Position tracking via GetCurrentPos() and GetErrorContext() provides detailed error messages with line/column information.

Usage

The text format parser is used whenever ONNX structures need to be constructed from text, such as in operator schema function body definitions (OpSchema::FunctionBody), test case construction, and interactive model building. The static template method OnnxParser::Parse<T>(data, input_string) provides a convenient entry point for parsing any supported protobuf type from a C string.

Code Reference

Source Location

Signature

// ParserBase methods
Common::Status ParserBase::Parse(Literal& result);
bool ParserBase::NextIsValidFloatString();

// OnnxParser methods (public API)
Common::Status OnnxParser::Parse(TensorShapeProto& shape);
Common::Status OnnxParser::Parse(TypeProto& typeProto);
Common::Status OnnxParser::Parse(TensorProto& tensorProto);
Common::Status OnnxParser::Parse(AttributeProto& attr);
Common::Status OnnxParser::Parse(AttrList& attrlist);
Common::Status OnnxParser::Parse(NodeProto& node);
Common::Status OnnxParser::Parse(NodeList& nodelist);
Common::Status OnnxParser::Parse(GraphProto& graph);
Common::Status OnnxParser::Parse(FunctionProto& fn);
Common::Status OnnxParser::Parse(ModelProto& model);
Common::Status OnnxParser::Parse(OpsetIdList& opsets);
Common::Status OnnxParser::Parse(StringStringList& stringStringList);

// Static convenience method
template <typename T>
static Common::Status OnnxParser::Parse(T& parsedData, const char* input);

Import

#include "onnx/defs/parser.h"

I/O Contract

Inputs

Name Type Required Description
input const char* Yes The ONNX text format string to parse. Must follow the ONNX text grammar rules for the target protobuf type.
parsedData T& (template) Yes Reference to the protobuf structure to populate (e.g., ModelProto, GraphProto, NodeProto, etc.)

Outputs

Name Type Description
return Common::Status OK status on success, or FAIL status with a detailed error message including line/column position and context on parse failure
parsedData T& (modified) The populated protobuf structure containing the parsed data

Usage Examples

#include "onnx/defs/parser.h"

using namespace ONNX_NAMESPACE;

// Parse a complete model from text
ModelProto model;
auto status = OnnxParser::Parse(model, R"(
<
   ir_version: 8,
   opset_import: ["" : 13]
>
main_graph (float[N,3] X) => (float[N,3] Y) {
   Y = Relu(X)
}
)");

// Parse a single node
NodeProto node;
OnnxParser::Parse(node, "Y = Add(X1, X2)");

// Parse a tensor type
TypeProto type;
OnnxParser::Parse(type, "float[3,4,5]");

// Parse a tensor shape
TensorShapeProto shape;
OnnxParser::Parse(shape, "N,3,4");

// Parse with error handling
if (!status.IsOK()) {
    std::cerr << status.ErrorMessage() << std::endl;
}

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment