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 Printer

From Leeroopedia
Revision as of 16:13, 16 February 2026 by Admin (talk | contribs) (Auto-imported from implementations/Onnx_Onnx_Text_Format_Printer.md)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)


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

Overview

printer.cc implements text-based printing functionality for ONNX protobuf structures, converting models, graphs, nodes, types, and tensors into human-readable string representations that follow the ONNX text format syntax.

Description

This file implements the ProtoPrinter class, which provides overloaded print() methods for every major ONNX protobuf type. The class writes to a std::ostream and manages indentation for nested structures. Key features include:

Type printing handles all ONNX type variants: tensor types with shapes and element types, sequence types ("seq(...)"), map types ("map(key, value)"), optional types ("optional(...)"), and sparse tensor types ("sparse_tensor(...)"). Shape dimensions print as concrete values, symbolic parameters, or "?" for unknown dimensions.

Tensor printing supports both inline data and external data references. Inline data handles raw data (with ParseData for int32, int64, float, double) and typed fields (int32_data, int64_data, uint64_data, float_data, double_data, string_data). External data prints as bracket-enclosed key-value pairs. Initializer tensors include the "=" assignment syntax.

Attribute printing covers all 14 attribute types: INT, FLOAT, STRING, TENSOR, GRAPH, SPARSE_TENSOR, TYPE_PROTO, and their list variants (INTS, FLOATS, STRINGS, etc.). Attribute references use the "@ref_name" syntax. Graph attributes trigger indentation increases for readability.

Node printing formats each node with optional bracketed names, output identifiers, "=" assignment, domain-qualified op types with optional overloads, attributes (placed before or after parameters depending on whether subgraphs are present), and parenthesized input lists.

Graph and model printing render complete structures with proper formatting. Models print metadata as key-value pairs in angle brackets (ir_version, opset_import, producer info, etc.), followed by the graph and any function definitions.

The file also defines operator<< overloads for 14 protobuf types using the DEF_OP macro, enabling direct stream output of any ONNX protobuf structure.

A helper function IsValidIdentifier checks whether a string needs quoting (identifiers must start with a letter or underscore and contain only alphanumeric characters or underscores).

Usage

The printer is used for debugging, model inspection, logging, and any context where a human-readable representation of ONNX structures is needed. The operator<< overloads allow direct use with any std::ostream, including std::cout and std::stringstream. The printer output is designed to be compatible with the OnnxParser, creating a round-trip capable text format.

Code Reference

Source Location

Signature

// ProtoPrinter class (internal, not directly instantiated by users)
class ProtoPrinter {
 public:
  explicit ProtoPrinter(std::ostream& os);
  void print(const TensorShapeProto_Dimension& dim);
  void print(const TensorShapeProto& shape);
  void print(const TypeProto_Tensor& tensortype);
  void print(const TypeProto& type);
  void print(const TensorProto& tensor, bool is_initializer = false);
  void print(const ValueInfoProto& value_info);
  void print(const AttributeProto& attr);
  void print(const NodeProto& node);
  void print(const GraphProto& graph);
  void print(const FunctionProto& fn);
  void print(const ModelProto& model);
  void print(const OperatorSetIdProto& opset);
  // ... and more
};

// Stream operators (public API)
std::ostream& operator<<(std::ostream& os, const ModelProto& proto);
std::ostream& operator<<(std::ostream& os, const GraphProto& proto);
std::ostream& operator<<(std::ostream& os, const NodeProto& proto);
std::ostream& operator<<(std::ostream& os, const TypeProto& proto);
std::ostream& operator<<(std::ostream& os, const TensorProto& proto);
std::ostream& operator<<(std::ostream& os, const AttributeProto& proto);
std::ostream& operator<<(std::ostream& os, const ValueInfoProto& proto);
std::ostream& operator<<(std::ostream& os, const FunctionProto& proto);

Import

#include "onnx/defs/printer.h"

I/O Contract

Inputs

Name Type Required Description
proto const T& (any ONNX protobuf type) Yes The protobuf structure to print. Supported types include ModelProto, GraphProto, NodeProto, TypeProto, TensorProto, AttributeProto, ValueInfoProto, FunctionProto, and their sub-components.
os std::ostream& Yes The output stream to write the text representation to.
is_initializer bool No When printing a TensorProto, indicates whether to include the "=" assignment syntax (default: false).

Outputs

Name Type Description
os std::ostream& (modified) The output stream with the text representation written to it. Returns the same stream for chaining.

Usage Examples

#include "onnx/defs/printer.h"
#include <iostream>
#include <sstream>

using namespace ONNX_NAMESPACE;

// Print a model to stdout
ModelProto model;
// ... populate model ...
std::cout << model << std::endl;

// Print a node to a string
NodeProto node;
// ... populate node ...
std::stringstream ss;
ss << node;
std::string text = ss.str();

// Print a type
TypeProto type;
type.mutable_tensor_type()->set_elem_type(TensorProto_DataType_FLOAT);
type.mutable_tensor_type()->mutable_shape()->add_dim()->set_dim_value(3);
type.mutable_tensor_type()->mutable_shape()->add_dim()->set_dim_param("N");
std::cout << type << std::endl;
// Output: float[3,N]

// Print an attribute
AttributeProto attr;
attr.set_name("axis");
attr.set_type(AttributeProto_AttributeType_INT);
attr.set_i(1);
std::cout << attr << std::endl;
// Output: axis: int = 1

Related Pages

Page Connections

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