Containers · .onnx

ONNX model / TensorProto (initializers and .pb tensors)

ONNX stores weights as TensorProto messages in ModelProto.graph.initializer (and sparse_initializer). A TensorProto has dims, data_type, name and data in raw_data (little-endian bytes), in a typed repeated field (float_data, int32_data, ...), or in an external file (external_data location/offset/length) when the model is over the 2 GB protobuf limit. Useful as an import source (extract initializers) and as an export target for tools that consume TensorProto (.pb test vectors, ONNX Runtime).

.onnx .pb .onnx.data .onnx_data

What it is
many tensors
Byte order
little
Dimension order
row major
Compression
none
Metadata
protobuf
Loading
Loading it into a library does not execute code.

How to read it

Protobuf wire format. An external data file is a plain blob; each tensor's bytes are at [offset, offset+length).

One tensor

name
TensorProto.name
dtype
TensorProto.data_type
shape
TensorProto.dims
data_offset
EXTERNAL: int(external_data['offset'] or 0) in file external_data['location']; DEFAULT: position of raw_data in the protobuf (requires a parse)
nbytes
ceil(bits*prod(dims)/8); external_data['length'] if given MUST match

Number formats

The name in the file, and the canonical dtype it maps to.

In the fileCanonicalBits
UNDEFINEDnone—
FLOATfloat3232
UINT8uint88
INT8int88
UINT16uint1616
INT16int1616
INT32int3232
INT64int6464
STRINGnone—
BOOLbool8
FLOAT16float1616
DOUBLEfloat6464
UINT32uint3232
UINT64uint6464
COMPLEX64complex6464
COMPLEX128complex128128
BFLOAT16bfloat1616
FLOAT8E4M3FNfloat8_e4m3fn8
FLOAT8E4M3FNUZfloat8_e4m3fnuz8
FLOAT8E5M2float8_e5m28
FLOAT8E5M2FNUZfloat8_e5m2fnuz8
UINT4uint44
INT4int44
FLOAT4E2M1float4_e2m1fn4
FLOAT8E8M0float8_e8m0fnu8
UINT2uint22
INT2int22
FLOAT6E2M3float6_e2m3fn6
FLOAT6E3M2float6_e3m2fn6

Sources

Nick Reynolds · @goldnode · nicks.net · rev 0.3