Implementation:Onnx Onnx DataTypeUtils Class
| Knowledge Sources | |
|---|---|
| Domains | Type System, Data Type Conversion |
| Last Updated | 2026-02-10 00:00 GMT |
Overview
Concrete tool for converting between ONNX data type representations provided by the ONNX library.
Description
The onnx/defs/data_type_utils.h header declares the DataTypeUtils class and the DataType type alias, which form the foundation of ONNX's C++ type system for representing and converting between data types.
DataType is defined as const std::string*, using a string pointer as a unique type identifier. This design enables efficient type comparison by pointer equality rather than string comparison.
The DataTypeUtils class (in the ONNX_NAMESPACE::Utils namespace) is a final utility class with exclusively static methods:
- ToType(const std::string& type_str) - converts a type string (e.g., "float", "tensor(float)", "seq(tensor(int32))") to a DataType pointer. Throws std::invalid_argument for invalid input.
- ToType(const TypeProto& type_proto) - converts a TypeProto protobuf to a DataType pointer.
- ToTypeProto(const DataType& data_type) - converts a DataType pointer back to a TypeProto reference.
- ToDataTypeString(int32_t tensor_data_type) - converts a numeric tensor data type enum to its string representation.
Private helper methods include:
- FromString - parses a type string into a TypeProto
- FromDataTypeString - converts a data type string to its integer enum value
- ToString - serializes a TypeProto to its string representation
- IsValidDataTypeString - validates a data type string
- GetTypeStrToProtoMap - returns the global map of type strings to TypeProto objects
- GetTypeStrLock - returns the mutex for thread-safe concurrent access to the global map
The ONNX type grammar supported is:
- <type> ::= <data_type> | tensor(<data_type>) | seq(<type>) | map(<data_type>, <type>)
- <data_type> ::= float | int32 | string | bool | uint8 | int8 | uint16 | int16 | int64 | float16 | double
A scalar type (bare <data_type> without tensor wrapper) represents zero-dimensional data.
Usage
Use DataTypeUtils when you need to convert between type string representations, TypeProto objects, and integer data type enums. It is essential for operator schema definitions, type checking, and graph validation. The thread-safe global map makes it safe to use in multi-threaded environments.
Code Reference
Source Location
- Repository: Onnx_Onnx
- File: onnx/defs/data_type_utils.h
Signature
namespace ONNX_NAMESPACE {
using DataType = const std::string*;
namespace Utils {
class DataTypeUtils final {
public:
static DataType ToType(const std::string& type_str);
static DataType ToType(const TypeProto& type_proto);
static const TypeProto& ToTypeProto(const DataType& data_type);
static std::string ToDataTypeString(int32_t tensor_data_type);
private:
static void FromString(const std::string& type_str, TypeProto& type_proto);
static int32_t FromDataTypeString(const std::string& type_str);
static std::string ToString(const TypeProto& type_proto,
const std::string& left = "", const std::string& right = "");
static bool IsValidDataTypeString(const std::string& type_str);
static std::unordered_map<std::string, TypeProto>& GetTypeStrToProtoMap();
static std::mutex& GetTypeStrLock();
};
} // namespace Utils
} // namespace ONNX_NAMESPACE
Import
#include "onnx/defs/data_type_utils.h"
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| type_str | const std::string& | Yes (for string-based ToType) | A type string following the ONNX type grammar (e.g., "tensor(float)") |
| type_proto | const TypeProto& | Yes (for proto-based ToType) | A TypeProto protobuf message |
| data_type | const DataType& | Yes (for ToTypeProto) | A DataType pointer (string pointer) to convert back to TypeProto |
| tensor_data_type | int32_t | Yes (for ToDataTypeString) | An integer data type enum value from TensorProto_DataType |
Outputs
| Name | Type | Description |
|---|---|---|
| DataType | DataType (const std::string*) | Unique string pointer identifying the type; enables fast pointer-equality comparison |
| TypeProto | const TypeProto& | The TypeProto protobuf corresponding to the given DataType |
| data_type_string | std::string | String representation of a tensor data type enum (e.g., "float", "int32") |
Usage Examples
#include "onnx/defs/data_type_utils.h"
using namespace ONNX_NAMESPACE;
using namespace ONNX_NAMESPACE::Utils;
// Convert a type string to a DataType pointer
DataType dt = DataTypeUtils::ToType("tensor(float)");
// Convert a TypeProto to a DataType pointer
TypeProto proto;
proto.mutable_tensor_type()->set_elem_type(TensorProto::FLOAT);
DataType dt2 = DataTypeUtils::ToType(proto);
// Compare types by pointer equality (fast)
if (dt == dt2) {
// same type
}
// Convert DataType back to TypeProto
const TypeProto& retrieved_proto = DataTypeUtils::ToTypeProto(dt);
// Convert an integer tensor data type to its string name
std::string name = DataTypeUtils::ToDataTypeString(TensorProto::FLOAT);
// name == "float"
// Use in operator type constraint definitions
DataType float_type = DataTypeUtils::ToType("tensor(float)");
DataType int32_type = DataTypeUtils::ToType("tensor(int32)");
DataType seq_type = DataTypeUtils::ToType("seq(tensor(float))");