Are there any guarantees on the output ordering of ONNX model beyond topological sort?

I want to convert a PyTorch model that returns multiple potentially nested tensor outputs to ONNX format. For example, the PyTorch model could return something like the following:

pytorch_model_outputs = ((tensor_1, tensor_2, tensor_3), (tensor_4,), (tensor_5, tensor_6))

If I convert this model to ONNX format using torch.onnx.export, it will flatten the outputs into a single list in the order they are returned by the graph. However, from the docs the only requirement on the ordering of nodes in the ONNX graph is that the nodes are sorted in topological order:

The list of nodes defining the top-level computation graph MUST be ordered topologically; that is, if node K follows node N in the graph, none of the data inputs of N may refer to outputs of K.

If I’m understanding this correctly, this leaves some ambiguity in the final ordering of the outputs. The following output orderings would be valid and possible:

onnx_model_outputs_ordering_1 = (tensor_1, tensor_2, tensor_3, tensor_4, tensor_5, tensor_6)
onnx_model_outputs_ordering_2 = (tensor_3, tensor_1, tensor_2, tensor_4, tensor_6, tensor_5)

In my experience so far, the output ordering has always been consistent with output_ordering_1, which is my desired ordering. But since output_ordering_2 is equally possible, it begs the question: how does one know what the actual ordering of the outputs will be short of inspecting the outputs?

1 Like

@codykala Curious if you ever found a definitive answer to this? I imagine the ONNX order can’t change if the Python definition is already a flat tuple like (tensor_1, tensor_2, ..., tensor_N) but your use-case of nested tensors is an interesting one