onnx / onnx

Open standard for machine learning interoperability
https://onnx.ai/
Apache License 2.0
17.7k stars 3.65k forks source link

Optimizing Node Ordering in ONNX Graphs: Ensuring Correct Sequence for Model Generation #6017

Open kumar-utkarsh0317 opened 6 months ago

kumar-utkarsh0317 commented 6 months ago

I am working on generating an ONNX converter for some framework, and previously, I was iterating through the nodes of the ONNX model to generate a feed-forward network corresponding to each node of the ONNX model. However, after analyzing several ONNX models and the order of their nodes, I realized that I can't simply iterate in the order they were loaded because their order was pretty random.

So, I think I have to pick one node and to get the subsequent node in the model, I have to check which node is taking the output of the previous node. By doing this, I could get the next node in the model.

I think to get the first node of the model, I have to check which has such input with no initializer for that, and that node will be the first one.

I am using this approach to generate a model from an ONNX model. Is the way I am following correct, or if there is any other optimized way, then please do tell me.

gramalingam commented 6 months ago

ONNX standard requires that the nodes be topologically sorted. If the input model satisfies this, then you can just iterate through them in order. However, some model generators may not created topologically sorted nodes. If so, it is best solved by doing a topological sort of the nodes (with respect to data dependences encoded in the input/outputs).