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 file | Canonical | Bits |
|---|---|---|
| UNDEFINED | none | — |
| FLOAT | float32 | 32 |
| UINT8 | uint8 | 8 |
| INT8 | int8 | 8 |
| UINT16 | uint16 | 16 |
| INT16 | int16 | 16 |
| INT32 | int32 | 32 |
| INT64 | int64 | 64 |
| STRING | none | — |
| BOOL | bool | 8 |
| FLOAT16 | float16 | 16 |
| DOUBLE | float64 | 64 |
| UINT32 | uint32 | 32 |
| UINT64 | uint64 | 64 |
| COMPLEX64 | complex64 | 64 |
| COMPLEX128 | complex128 | 128 |
| BFLOAT16 | bfloat16 | 16 |
| FLOAT8E4M3FN | float8_e4m3fn | 8 |
| FLOAT8E4M3FNUZ | float8_e4m3fnuz | 8 |
| FLOAT8E5M2 | float8_e5m2 | 8 |
| FLOAT8E5M2FNUZ | float8_e5m2fnuz | 8 |
| UINT4 | uint4 | 4 |
| INT4 | int4 | 4 |
| FLOAT4E2M1 | float4_e2m1fn | 4 |
| FLOAT8E8M0 | float8_e8m0fnu | 8 |
| UINT2 | uint2 | 2 |
| INT2 | int2 | 2 |
| FLOAT6E2M3 | float6_e2m3fn | 6 |
| FLOAT6E3M2 | float6_e3m2fn | 6 |