Most ONNX tensor files are part of ONNX’s backend test suite, which ships inside the
onnx package under
onnx/backend/test/data/. Each test is a model and the inputs and outputs
that a correct runtime must turn one into the other.
The folders
Each test has a folder of its own:
/model.onnx the ONNX model under testfile/test_data_set_0/ one set of inputs and expected outputs; there may be several, numbered from 0folder…/input_0.pb, input_1.pb, … one file for each input of the model, in orderfiles…/output_0.pb, output_1.pb, … one file for each outputfiles
Each .pb file is one serialised message: a
TensorProto for a tensor input or
output (this format), or an ONNX sequence or optional
value for inputs of those types. The message holds no reference to the model; its
name is usually that of the input or output.
The tests are grouped in folders:
| Folder | Holds |
|---|---|
node/ | Tests of single operators, in folders such as test_add. |
pytorch-converted/ | Test folders in the same layout. |
pytorch-operator/ | Test folders in the same layout. |
simple/ | Test folders in the same layout. |
light/ | A different layout: light_<model>.onnx beside light_<model>_output_0.pb, with no test_data_set folders. |
The two names pytorch-converted and pytorch-operator suggest
tests made from PyTorch; what each folder covers beyond its name was not checked.
Which message a file holds
The file does not say whether it is a TensorProto, a SequenceProto, an OptionalProto or a MapProto. What decides it is the type the model declares for that input or output. That the test runner reads each file as the type the model declares is an inference from the files: the runner’s code was not read for this page.
The explorer has no model to look at, so it decides from the bytes. The tensor parser is tried first and is strict: every field must be one of TensorProto’s, and the values must fit the shape exactly. A sequence or optional file fails that test within its first fields and goes to the sequence parser.
In Python, onnx.load_tensor("input_0.pb") reads a tensor file, and
onnx.numpy_helper.to_array turns it into a NumPy array.