ThanatosShinji / onnx-tool

A parser, editor and profiler tool for ONNX models.
https://pypi.org/project/onnx-tool/
MIT License
383 stars 51 forks source link

Add shape functions and ignore un-consumed outputs #53

Closed ashay closed 11 months ago

ashay commented 12 months ago

This PR contains two commits:

[graph] ignore nodes whose outputs are not consumed

If the input graph contains a node (e.g. an initializer) whose output is
not consumed by any node, then the graph initialization fails.  More
specifically, in looking up the list of consumer nodes, the code indexes
into the `consumedby` dictionary, but the key (i.e. the output
name) does not exist in the dictionary since the output is unused.

This patch makes a quick fix by first checking whether the output name
is present in the dictionary before looking up the value in it.

and

[shape] add shape functions for GridSample and DepthToSpace nodes

This patch registers shape functions for the GridSample and DepthToSpace
nodes based on the ONNX operator spec.
ashay commented 12 months ago

To test the shape functions for GridSample and DepthToSpace nodes, use the following scripts to generate the ONNX files:

import onnx

from onnxscript import opset19, script, FLOAT

@script()
def this_script(X: FLOAT[1, 1, 4, 4], G: FLOAT[1, 6, 6, 2]) -> FLOAT[1, 1, 6, 6]:
    return opset19.GridSample(X, G)

onnx_model = this_script.to_model_proto()
onnx.save(onnx_model, "/tmp/a.onnx")
import onnx

from onnxscript import opset19, script, FLOAT

@script()
def this_script(X: FLOAT[1, 8, 2, 3]) -> FLOAT[1, 2, 4, 6]:
    return opset19.DepthToSpace(X, blocksize=2)

onnx_model = this_script.to_model_proto()
onnx.save(onnx_model, "/tmp/b.onnx")

And then reproduce the error using the following commands:

$ python3 -m onnx_tool --in /tmp/a.onnx --out /dev/null
$ python3 -m onnx_tool --in /tmp/b.onnx --out /dev/null
ThanatosShinji commented 12 months ago

Thanks for your contribution! For personal reseason , your code review will be delayed for 3 days. Thanks again!

Best Regards


From: Ashay Rane @.> Sent: Wednesday, October 4, 2023 5:59:36 AM To: ThanatosShinji/onnx-tool @.> Cc: Subscribed @.***> Subject: Re: [ThanatosShinji/onnx-tool] Add shape functions and ignore un-consumed outputs (PR #53)

To test the shape functions for GridSample and DepthToSpace nodes, use the following scripts to generate the ONNX files:

import onnx

from onnxscript import opset19, script, FLOAT

@script() def this_script(X: FLOAT[1, 1, 4, 4], G: FLOAT[1, 6, 6, 2]) -> FLOAT[1, 1, 6, 6]: return opset19.GridSample(X, G)

onnx_model = this_script.to_model_proto() onnx.save(onnx_model, "/tmp/a.onnx")

import onnx

from onnxscript import opset19, script, FLOAT

@script() def this_script(X: FLOAT[1, 8, 2, 3]) -> FLOAT[1, 2, 4, 6]: return opset19.DepthToSpace(X, blocksize=2)

onnx_model = this_script.to_model_proto() onnx.save(onnx_model, "/tmp/b.onnx")

And then reproduce the error using the following commands:

$ python3 -m onnx_tool --in /tmp/a.onnx --out /dev/null $ python3 -m onnx_tool --in /tmp/b.onnx --out /dev/null

— Reply to this email directly, view it on GitHubhttps://github.com/ThanatosShinji/onnx-tool/pull/53#issuecomment-1745792396, or unsubscribehttps://github.com/notifications/unsubscribe-auth/AZZIQRUK25WV5BS7IAVL5RLX5SDERAVCNFSM6AAAAAA5RUP27OVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTONBVG44TEMZZGY. You are receiving this because you are subscribed to this thread.Message ID: @.***>

ashay commented 12 months ago

No worries, thanks for agreeing to review the PR!

ThanatosShinji commented 11 months ago

Good work!