Implementation:Onnx Onnx ProtoToString
| Knowledge Sources | |
|---|---|
| Domains | Serialization, Debugging |
| Last Updated | 2026-02-10 00:00 GMT |
Overview
Concrete tool for converting ONNX protobuf types to human-readable text representations using C++ stream operators, provided by the ONNX library.
Description
This header declares a comprehensive set of C++ output stream operators (operator<<) for all major ONNX protobuf types, enabling them to be printed to any std::ostream (such as std::cout or std::stringstream). The supported types span the full ONNX protobuf hierarchy:
- TensorShapeProto_Dimension and TensorShapeProto for tensor shape information
- TypeProto_Tensor and TypeProto for type information
- TensorProto for tensor data
- ValueInfoProto and ValueInfoList for value information entries
- AttributeProto and AttrList for operator attributes
- NodeProto and NodeList for computation graph nodes
- GraphProto for complete computation graphs
- FunctionProto for function definitions
- ModelProto for complete ONNX models
The header also provides a template function ProtoToString that converts any of these protobuf types directly to a std::string by streaming the object to a std::stringstream and returning the resulting string.
Usage
Use the stream operators when you need to output ONNX protobuf structures to logs, console, or debugging output. Use ProtoToString when you need a string representation of any ONNX protobuf object for display, serialization to text, or comparison purposes. This is particularly useful during development and debugging of ONNX model processing pipelines.
Code Reference
Source Location
- Repository: Onnx_Onnx
- File: onnx/defs/printer.h
Signature
std::ostream& operator<<(std::ostream& os, const TensorShapeProto_Dimension& dim);
std::ostream& operator<<(std::ostream& os, const TensorShapeProto& shape);
std::ostream& operator<<(std::ostream& os, const TypeProto_Tensor& tensortype);
std::ostream& operator<<(std::ostream& os, const TypeProto& type);
std::ostream& operator<<(std::ostream& os, const TensorProto& tensor);
std::ostream& operator<<(std::ostream& os, const ValueInfoProto& value_info);
std::ostream& operator<<(std::ostream& os, const ValueInfoList& vilist);
std::ostream& operator<<(std::ostream& os, const AttributeProto& attr);
std::ostream& operator<<(std::ostream& os, const AttrList& attrlist);
std::ostream& operator<<(std::ostream& os, const NodeProto& node);
std::ostream& operator<<(std::ostream& os, const NodeList& nodelist);
std::ostream& operator<<(std::ostream& os, const GraphProto& graph);
std::ostream& operator<<(std::ostream& os, const FunctionProto& fn);
std::ostream& operator<<(std::ostream& os, const ModelProto& model);
template <typename ProtoType>
std::string ProtoToString(const ProtoType& proto);
Import
#include "onnx/defs/printer.h"
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| os | std::ostream& | Yes | The output stream to write the text representation to |
| proto | const ProtoType& | Yes | Any supported ONNX protobuf type (e.g., ModelProto, GraphProto, NodeProto) |
Outputs
| Name | Type | Description |
|---|---|---|
| os | std::ostream& | The same output stream, with the text representation appended (for chaining) |
| result | std::string | A string containing the text representation (from ProtoToString) |
Usage Examples
#include "onnx/defs/printer.h"
// Print a model to stdout
ModelProto model;
std::cout << model << std::endl;
// Convert a node to a string
NodeProto node;
std::string text = ProtoToString(node);
// Print tensor shape information
TensorShapeProto shape;
std::cerr << "Shape: " << shape << std::endl;
// Use with stringstream for logging
std::stringstream ss;
ss << "Graph: " << graph;
log(ss.str());