microsoft / onnxscript

ONNX Script enables developers to naturally author ONNX functions and models using a subset of Python.
https://onnxscript.ai/
MIT License
270 stars 50 forks source link

[Pattern] Reshape-Transpose-Reshape -> Reshape #1775

Open justinchuby opened 1 month ago

justinchuby commented 1 month ago

image

titaiwangms commented 1 month ago

Is there a situation that Reshape-Transpose-Reshape is actually un-rewritable (condition function needed)? Otherwise, this should be simple.

justinchuby commented 1 month ago

Is there a situation that Reshape-Transpose-Reshape is actually un-rewritable (condition function needed)? Otherwise, this should be simple.

Potentially. I haven't thought deeply on this yet

gramalingam commented 1 week ago

We can't rewrite this Reshape-Transpose-Reshape to Reshape (and that's true always, unless the Transpose is an identity operation). This can be checked because a Reshape does NOT require any data-movement (it will not change the relative ordering of elements in the tensor), but a Transpose does (unless it does not really tranpose any axes).

gramalingam commented 1 week ago

But we can eliminate the Reshapes. So, maybe the title should be changed to "Transpose" as the target, not "Reshape"