Implementation:Onnx Onnx TensorProto Util
| Knowledge Sources | |
|---|---|
| Domains | Tensor Serialization, Data Type Conversion |
| Last Updated | 2026-02-10 00:00 GMT |
Overview
Concrete tool for converting between C++ native types and ONNX TensorProto protobuf messages with proper endianness and alignment handling, provided by the ONNX library.
Description
This file implements template specializations for two families of conversion functions between C++ types and TensorProto:
ToTensor (scalar) - Converts a single C++ value to a TensorProto. Implemented via the DEFINE_TO_TENSOR_ONE macro, which generates template specializations that create a TensorProto, set the appropriate data type enum, and add the value to the corresponding typed data field. Supports float, bool, int32_t, int64_t, uint64_t, double, and std::string.
ToTensor (vector) - Converts a std::vector of C++ values to a TensorProto. Implemented via the DEFINE_TO_TENSOR_LIST macro, which clears the typed data field, sets the data type, and iterates over the vector to populate the field. Supports the same types as the scalar variant.
ParseData - Extracts data from a TensorProto back into a std::vector of C++ values. Implemented via the DEFINE_PARSE_DATA macro, which handles multiple data sources (typed fields and raw_data), validates data types and sizes, performs byte-order swapping on big-endian platforms, and uses memcpy for safe memory-aligned copying. Supports int32_t, int64_t, float, and double. The raw_data parsing includes careful handling of endianness conversion by swapping bytes element-by-element and uses memcpy instead of reinterpret_cast to avoid alignment issues on platforms like ARM32-v7a.
Usage
Use ToTensor when you need to create TensorProto objects from C++ values, such as when building constant nodes for function definitions or constructing test data. Use ParseData during shape inference when you need to extract concrete values from constant tensors (initializers) to perform static computations. These utilities are heavily used internally by the ONNX shape inference system and constant folding passes.
Code Reference
Source Location
- Repository: Onnx_Onnx
- File: onnx/defs/tensor_proto_util.cc
Signature
// Scalar to TensorProto (specializations for float, bool, int32_t, int64_t, uint64_t, double, std::string)
template <>
TensorProto ToTensor<type>(const type& value);
// Vector to TensorProto (specializations for float, bool, int32_t, int64_t, uint64_t, double, std::string)
template <>
TensorProto ToTensor<type>(const std::vector<type>& values);
// TensorProto to vector (specializations for int32_t, int64_t, float, double)
template <>
std::vector<type> ParseData(const TensorProto* tensor_proto);
Import
#include "onnx/defs/tensor_proto_util.h"
I/O Contract
Inputs (ToTensor - scalar)
| Name | Type | Required | Description |
|---|---|---|---|
| value | const T& | Yes | A single C++ value of type float, bool, int32_t, int64_t, uint64_t, double, or std::string |
Inputs (ToTensor - vector)
| Name | Type | Required | Description |
|---|---|---|---|
| values | const std::vector<T>& | Yes | A vector of C++ values of a supported type |
Inputs (ParseData)
| Name | Type | Required | Description |
|---|---|---|---|
| tensor_proto | const TensorProto* | Yes | A TensorProto with data stored in typed fields or raw_data; must have a defined data type matching the requested C++ type |
Outputs
| Name | Type | Description |
|---|---|---|
| tensor | TensorProto | A new TensorProto containing the input value(s) with appropriate data type set (from ToTensor) |
| data | std::vector<T> | The extracted data values from the tensor (from ParseData) |
Supported Type Mappings
| C++ Type | TensorProto DataType | Proto Field | ToTensor | ParseData |
|---|---|---|---|---|
| float | FLOAT | float_data | Yes | Yes |
| bool | BOOL | int32_data | Yes | No |
| int32_t | INT32 | int32_data | Yes | Yes |
| int64_t | INT64 | int64_data | Yes | Yes |
| uint64_t | UINT64 | uint64_data | Yes | No |
| double | DOUBLE | double_data | Yes | Yes |
| std::string | STRING | string_data | Yes | No |
Usage Examples
#include "onnx/defs/tensor_proto_util.h"
// Create a scalar tensor
TensorProto float_tensor = ToTensor<float>(3.14f);
TensorProto int_tensor = ToTensor<int64_t>(42);
// Create a vector tensor
std::vector<float> data = {1.0f, 2.0f, 3.0f};
TensorProto vec_tensor = ToTensor<float>(data);
// Parse data from a TensorProto during shape inference
const TensorProto* initializer = ...;
std::vector<int64_t> shape_values = ParseData<int64_t>(initializer);