Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[TorchToTosa] Refactoring to separate construction of legal/illegal ops and conversion patterns. #3759

Merged
merged 1 commit into from
Dec 12, 2024

Conversation

sahas3
Copy link
Contributor

@sahas3 sahas3 commented Oct 3, 2024

This PR refactors TorchToTosa to separate the construction of legal/illegal ops and conversion patterns in their own functions:

  1. populateTorchToTosaConversionLegalOps -- populate any ops that are legal after the conversion pass
  2. populateTorchToTosaConversionIllegalOps -- populate any ops that are illegal after the conversion pass
  3. populateTorchToTosaConversionPatterns -- populate the ops conversion patterns

Currently the (il)legality of the ops that are (il)legal after the conversion pass runs is embedded within the conversion pattern. Our end goal is to write a new pass pipeline that converts torch ops to a mix of tosa, linalg, tensor, etc dialect ops. The reason we want to also emit tosa ops (instead of using the existing TorchToLinalg to emit linalg+tensor+...) is because some operations like conv2d encodes the padding behavior in the op in tosa unlike the linalg version -- this helps in lowering the tosa.conv2d to a custom implementation that does padding on the fly.

To implement this new pipeline we need to be able to separate out the illegal tosa ops from the conversion pattern itself. Otherwise we will hit an issue for ops like AtenMaxDimOp which can be lowered to both tosa and linalg + others dialects. Not all AtenMaxDimOp can be lowered successfully to tosa as the implementation uses tosa.reshape which cannot handle multiple dynamic dimensions but the TorchToLinalg lowering can handle it. In the current behavior the pipeline will stop as soon as the existing TorchToTosa conversion runs as AtenMaxDimOp will be marked as an illegal op.

Essentially we want to be able to control what the legality of the ops should be independent of the conversion pattern. This is also inline with the conversion patterns in the llvm-mlir repo such as https://github.com/llvm/llvm-project/blob/000e790be35b77a01872851646d54432a203542c/mlir/lib/Conversion/SCFToControlFlow/SCFToControlFlow.cpp#L718

"THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY."

@sahas3
Copy link
Contributor Author

sahas3 commented Oct 7, 2024

Hi @sjarus, tagging you as you recently reviewed a PR for changes in the TorchToTosa files. I think since I do not have write access yet, I couldn't directly request your feedback. Also, for context this PR is a requirement for adding a new torch to tosa+linalg pipeline that we (MathWorks) mentioned in the last TOSA meeting. It'll be great if you can take a look at these changes. Thanks!

@sahas3 sahas3 force-pushed the tosa-pattern-refactor branch from a76c138 to 49199da Compare October 8, 2024 18:22
@sjarus
Copy link
Collaborator

sjarus commented Oct 8, 2024

I'll take a look within a day.

@sjarus sjarus self-requested a review October 8, 2024 23:47
@sahas3
Copy link
Contributor Author

sahas3 commented Oct 16, 2024

Hi @sjarus , a gentle reminder to review this PR when you get a chance. Thanks!

@sahas3 sahas3 force-pushed the tosa-pattern-refactor branch from e62285c to a197fdf Compare October 17, 2024 12:19
@sahas3 sahas3 force-pushed the tosa-pattern-refactor branch from a197fdf to 14e3cea Compare October 29, 2024 01:44
@sahas3
Copy link
Contributor Author

sahas3 commented Oct 29, 2024

Hi @sjarus any thoughts on this PR?

@sahas3 sahas3 force-pushed the tosa-pattern-refactor branch from 14e3cea to 6760b46 Compare October 31, 2024 00:49
@sjarus
Copy link
Collaborator

sjarus commented Oct 31, 2024

Terribly sorry - I completely missed this PR! Please ping me on discord if you don't get a timely response from me. Reviewing this now.

sjarus
sjarus previously approved these changes Oct 31, 2024
Copy link
Collaborator

@sjarus sjarus left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

.

@sjarus sjarus dismissed their stale review October 31, 2024 05:23

updating comment

@sahas3 sahas3 force-pushed the tosa-pattern-refactor branch 2 times, most recently from 0f92856 to 7e120d4 Compare November 8, 2024 07:01
@sahas3 sahas3 requested a review from sjarus November 8, 2024 07:02
@sahas3 sahas3 force-pushed the tosa-pattern-refactor branch from 7e120d4 to ffa472f Compare November 13, 2024 03:02
@sahas3 sahas3 force-pushed the tosa-pattern-refactor branch from ffa472f to f60b53f Compare December 3, 2024 22:47
@sahas3 sahas3 force-pushed the tosa-pattern-refactor branch from f60b53f to 39aaae8 Compare December 11, 2024 00:56
@sahas3 sahas3 merged commit f03a576 into llvm:main Dec 12, 2024
3 checks passed
rahuls-cerebras added a commit that referenced this pull request Jan 3, 2025
…llegal ops and conversion patterns. (#3759)"

This reverts commit f03a576.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants