A Protocol Buffers message “must be smaller than 2 GiB when serialized”
(the size limit), so a
model whose weights are larger cannot hold them in one .onnx file. Its
tensors keep their values in other files instead, and the model file holds only where
to find them.
The tensor’s fields
A tensor with external data sets two fields of its TensorProto and leaves its value fields empty:
data_location(field 14) is EXTERNAL, 1;external_data(field 13) is a list of StringStringEntryProto pairs with the keys below.
The tensor keeps its name, dims and
data_type, so a reader knows the shape and type without opening the
other file. The explorer shows such a tensor as stored in external_data
and does not check its size against its shape.
The keys
| Key | Required | Value |
|---|---|---|
location | yes | The path of the file holding the values, relative to the folder of the model file. |
offset | no | Where the values start in that file, in bytes, as a decimal string. It should be a multiple of 4096, so that the values can be mapped into memory. |
length | no | How many bytes the values take, as a decimal string. |
checksum | no | The SHA-1 digest of the bytes. |
The bytes in the other file are laid out exactly like
raw_data: fixed-width
little-endian values, row-major, with no header. One file may hold many tensors at
different offsets, or each tensor may have a file of its own.