Skip to content

Data structures

Data structures

The following data structures are used for operations with ONNX models:

OnnxTypeInfo

The structure describes the type of an input or output parameter of an ONNX model

struct OnnxTypeInfo
 {
  ENUM_ONNX_TYPE        type;          // parameter type
  OnnxTensorTypeInfo    tensor;        // tensor description
  OnnxMapTypeInfo       map;           // map description
  OnnxSequenceTypeInfo  sequence;      // sequence description
 };

Only tensor (ONNX_TYPE_TENSOR) can be used as an input. In this case, only the OnnxTypeInfo::tensor field is filled with values, while other fields (map and sequence) are not defined.

Only one of the three OnnxTypeInfo types (ONNX_TYPE_TENSOR, ONNX_TYPE_MAP or ONNX_TYPE_SEQUENCE) can be used as an input. The corresponding substructure (OnnxTypeInfo::tensor, OnnxTypeInfo::map or OnnxTypeInfo::sequence) is filled depending on the type.

OnnxTensorTypeInfo

The structure describes the tensor in the input or output parameter of an ONNX model

struct OnnxTensorTypeInfo
 {
  const ENUM_ONNX_DATA_TYPE   data_type;      // data type in the tensor
  const long                  dimensions[];   // number of elements in the tensor
 };

OnnxMapTypeInfo

The structure describes the map obtained in the output parameter of an ONNX model

struct OnnxMapTypeInfo
 {
  const ENUM_ONNX_DATA_TYPE   key_type;       // key type
  const OnnxTypeInfo &        value_type;     // value type
 };

OnnxSequenceTypeInfo

The structure describes the sequence obtained in the output parameter of an ONNX model

struct OnnxSequenceTypeInfo
 {
  const OnnxTypeInfo&        value_type;      // data type in the sequence
 };

ENUM_ONNX_TYPE

The ENUM_ONNX_TYPE enumeration describes the type of a model parameter

IDDescription
ONNX_TYPE_UNKNOWNUnknown
ONNX_TYPE_TENSORTensor
ONNX_TYPE_SEQUENCESequence
ONNX_TYPE_MAPMap
ONNX_TYPE_OPAQUEAbstract (opaque)
ONNX_TYPE_SPARSETENSORSparse tensor

ENUM_ONNX_DATA_TYPE

The ENUM_ONNX_DATA_TYPE enumeration describes the type of data used

IDDescription
ONNX_DATA_TYPE_UNDEFINEDUndefined
ONNX_DATA_TYPE_FLOATfloat
ONNX_DATA_TYPE_INT88-bit int
ONNX_DATA_TYPE_UINT1616-bit uint
ONNX_DATA_TYPE_INT1616-bit int
ONNX_DATA_TYPE_INT3232-bit int
ONNX_DATA_TYPE_INT6464-bit int
ONNX_DATA_TYPE_STRINGstring
ONNX_DATA_TYPE_BOOLbool
ONNX_DATA_TYPE_FLOAT1616-bit float
ONNX_DATA_TYPE_DOUBLEdouble
ONNX_DATA_TYPE_UINT3232-bit uint
ONNX_DATA_TYPE_UINT6464-bit uint
ONNX_DATA_TYPE_COMPLEX6464-bit complex number
ONNX_DATA_TYPE_COMPLEX128128-bit complex number
ONNX_DATA_TYPE_BFLOAT1616-bit bfloat (Brain Floating Point)

ENUM_ONNX_FLAGS

The ENUM_ONNX_FLAGS enumeration describes the model run mode

IDDescription
ONNX_DEBUG_LOGSOutput debug logs
ONNX_NO_CONVERSIONDisable auto conversion, use user data as is
ONNX_COMMON_FOLDERLoad a model file from the Common\Files folder; the value is equal to the FILE_COMMON flag

Array conversion when working with ONNX models

Machine learning tasks do not always require greater computational accuracy. To speed up calculations, some models use lower-precision data types such as Float16 and even Float8. To allow users to input the relevant data into models, MQL5 provides four special functions which convert standard MQL5 types into special FP16 and FP8 types.

FunctionAction
ArrayToFP16Copies an array of type float or double into an array of type ushort with the given format
ArrayToFP8Copies an array of type float or double into an array of type uchar with the given format
ArrayFromFP16Copies an array of type ushort into an array of float or double type with the given format
ArrayFromFP8Copies an array of type uchar into an array of float or double type with the given format

These array conversion functions use special formats specified in the below enumerations.

ENUM_FLOAT16_FORMAT

The ENUM_FLOAT16_FORMAT enumeration describes two FP16 type formats.

IDDescription
FLOAT_FP16Standard 16-bit format, also known as half
FLOAT_BFP16Special brain float point format

Each of these formats has its advantages and limitations. FLOAT16 provides higher accuracy but requires more storage and computation resources. BFLOAT16, on the other hand, provides higher performance and efficiency in data processing, but may be less accurate.

ENUM_FLOAT8_FORMAT

The ENUM_FLOAT8_FORMAT enumeration describes four FP8 type formats.

FP8 (8-bit floating point) is one of the data types used to represent floating point numbers. In FP8, each number is represented by 8 data bits, typically divided into three components: sign, exponent and mantissa. This format offers a balance between accuracy and storage efficiency, making it attractive for applications that require memory and computational efficiency.

IDDescription
FLOAT_FP8_E4M3FN8-bit floating point number, 4 bits for the exponent and 3 bits for the mantissa. Typically used as coefficients.
FLOAT_FP8_E4M3FNUZ8-bit floating point number, 4 bits for the exponent and 3 bits for the mantissa. Supports NaN, does not support negative zero and Inf. Typically used as coefficients.
FLOAT_FP8_E5M2FN8-bit floating point number, 5 bits for the exponent and 2 bits for the mantissa. Supports NaN and Inf. Typically used for gradients.
FLOAT_FP8_E5M2FNUZ8-bit floating point number, 5 bits for the exponent and 2 bits for the mantissa. Supports NaN, does not support negative zero and Inf. Also used for gradients.

One of the key advantages of FP8 is its efficiency in processing large datasets. By employing compact number representation, FP8 reduces memory requirements and accelerates calculations. This is especially important in machine learning and artificial intelligence applications which often process large datasets.

Last updated on