
ONNX - Data Types
In ONNX (Open Neural Network Exchange), the data types used in models are a crucial aspect of model representation and computation. As a standard format for machine learning models, ONNX supports a range of data types that allow for interoperability between different machine learning frameworks.
In this tutorial, we will explore the various ONNX data types, including tensor types, element types, sparse tensors, and non-tensor types like sequences and maps.
Understanding Tensors in ONNX
ONNX primarily focuses on numerical computation involving tensors, which are multidimensional arrays. Tensors are used to represent inputs, outputs, and intermediate values in ONNX models.
Each tensor is defined by three key components −
- Element type: Specifies the data type of all elements in the tensor.
- Shape: An array describing the dimensions of the tensor. Shapes can be fixed or dynamic, and a tensor can have an empty shape (e.g., a scalar).
- Contiguous array: A fully populated array of data values.
This design optimizes ONNX for numerical computations in deep learning applications, where large, multidimensional arrays are common.
Supported Element Types
Initially, ONNX was designed to support deep learning models, which often use floating-point numbers(32-bit floats). However, the current version of ONNX supports a wide range of element types, allowing for flexibility across different machine learning and data processing tasks.
Below is a list of supported data types in ONNX −
Element Type | Description |
---|---|
FLOAT | 32-bit floating point |
UINT8 | 8-bit unsigned integer |
INT8 | 8-bit signed integer |
UINT16 | 16-bit unsigned integer |
INT16 | 16-bit signed integer |
INT32 | 32-bit signed integer |
INT64 | 64-bit signed integer |
STRING | String data type |
BOOL | Boolean type |
FLOAT16 | 16-bit floating point |
DOUBLE | 64-bit floating point |
UINT32 | 32-bit unsigned integer |
UINT64 | 64-bit unsigned integer |
COMPLEX64 | 64-bit complex number |
COMPLEX128 | 128-bit complex number |
BFLOAT16 | Brain floating point 16-bit format |
FLOAT8E4M3FN | 8-bit floating point (format E4M3FN) |
FLOAT8E4M3FNUZ | 8-bit floating point (format E4M3FNUZ) |
FLOAT8E5M2 | 8-bit floating point (format E5M2) |
FLOAT8E5M2FNUZ | 8-bit floating point (format E5M2FNUZ) |
UINT4 | 4-bit unsigned integer |
INT4 | 4-bit signed integer |
FLOAT4E2M1 | 4-bit floating point (format E2M1) |
Sparse Tensors
Sparse tensors are useful when working with data that contains a large number of zeros. ONNX supports sparse tensors, particularly 2D sparse tensors. These are represented by the class SparseTensorProto, which includes the following attributes −
- dims: Specifies the shape of the sparse tensor.
- indices: The positions of non-zero values in the tensor (stored as int64).
- values: The actual non-zero values.
Non-Tensor Data Types
In addition to tensors, ONNX also supports non-tensor data types such as −
- Sequence: A sequence of tensors. This is useful for operations that need to handle a list or collection of tensors.
- Map: A mapping of tensor values, often used for associative arrays or dictionaries.
These non-tensor types are more commonly used in classical machine learning tasks, where structures like sequences and maps are necessary to represent certain operations.