-
Notifications
You must be signed in to change notification settings - Fork 15.2k
[mlir][linalg] Flag to guard MoveInitOperandsToInput in LinalgFoldUnitExtendDimsPass #157941
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
[mlir][linalg] Flag to guard MoveInitOperandsToInput in LinalgFoldUnitExtendDimsPass #157941
Conversation
…entDimsPass, as it causes regression in performance.
Thank you for submitting a Pull Request (PR) to the LLVM Project! This PR will be automatically labeled and the relevant teams will be notified. If you wish to, you can add reviewers by using the "Reviewers" section on this page. If this is not working for you, it is probably because you do not have write permissions for the repository. In which case you can instead tag reviewers by name in a comment by using If you have received no comments on your PR for a week, you can request a review by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate is once a week. Please remember that you are asking for valuable time from other developers. If you have further questions, they may be answered by the LLVM GitHub User Guide. You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums. |
Hi @MaheshRavishankar, @matthias-springer @hanhanW, CC: @jverma-quic, @javedabsar1, @aankit-quic, @muthubaskaran |
@llvm/pr-subscribers-mlir @llvm/pr-subscribers-mlir-linalg Author: None (sdalvi-quic) ChangesThe pattern MoveInitOperandsToInput in the LinalgFoldUnitExtendDimsPass tries to move the init operands to input, to ensure that the element wise ops are canonicalized. I need to run split-reduction optimization before LinalgFoldUnitExtendDimsPass. The split reduction optimization is trying to optimize the Linalg op by reading from the destination operand but, the pattern MoveInitOperandsToInput is reverting it and trying to canonicalize element wise op. This is leading to 10x performance regression. Proposing to guard the pattern MoveInitOperandsToInput to have more control over enabling/disabling it. IR after Split-Reduction and before LinalgFoldUnitExtendDimsPass IR after LinalgFoldUnitExtendDimsPass Full diff: https://github.com/llvm/llvm-project/pull/157941.diff 2 Files Affected:
diff --git a/mlir/include/mlir/Dialect/Linalg/Passes.td b/mlir/include/mlir/Dialect/Linalg/Passes.td
index 44da2965e6892..7cd8da6643400 100644
--- a/mlir/include/mlir/Dialect/Linalg/Passes.td
+++ b/mlir/include/mlir/Dialect/Linalg/Passes.td
@@ -145,7 +145,10 @@ def LinalgFoldUnitExtentDimsPass : Pass<"linalg-fold-unit-extent-dims", ""> {
let options = [
Option<"useRankReducingSlices", "use-rank-reducing-slices", "bool",
/*default=*/"false",
- "Generate rank-reducing slices instead of reassociative reshapes">
+ "Generate rank-reducing slices instead of reassociative reshapes">,
+ Option<"enableMoveInitOperandsToInput", "enable-move-init-operands-to-input", "bool",
+ /*default=*/"true",
+ "Enable MoveInitOperandsToInputPattern transformation">
];
let dependentDialects = [
"linalg::LinalgDialect", "affine::AffineDialect", "memref::MemRefDialect"
diff --git a/mlir/lib/Dialect/Linalg/Transforms/DropUnitDims.cpp b/mlir/lib/Dialect/Linalg/Transforms/DropUnitDims.cpp
index 22690daa4f9e1..ba466ed3df5cd 100644
--- a/mlir/lib/Dialect/Linalg/Transforms/DropUnitDims.cpp
+++ b/mlir/lib/Dialect/Linalg/Transforms/DropUnitDims.cpp
@@ -868,7 +868,9 @@ struct LinalgFoldUnitExtentDimsPass
RankReductionStrategy::ExtractInsertSlice;
}
linalg::populateFoldUnitExtentDimsPatterns(patterns, options);
- populateMoveInitOperandsToInputPattern(patterns);
+ if (enableMoveInitOperandsToInput) {
+ populateMoveInitOperandsToInputPattern(patterns);
+ }
(void)applyPatternsGreedily(op, std::move(patterns));
}
};
|
I'll take a look in more detail soon, but general advise is don't use passes as is. From my perspective passes in MLIR are mostly for testing, especially this pass. Better to write your own pass using the patterns you want. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Need time to review
“Ping” - Just a gentle follow-up on the PR. Could you please take a look when you get a chance? Also, I’d appreciate your help in clarifying the query I’ve raised in the comments.
|
What happened here. Now I just see the changes to add a pass option... |
Hi @MaheshRavishankar, I might have misunderstood your comment. This PR adds an option to guard the MoveInitOperandsToInput pattern in the LinalgFoldUnitExtentDimsPass. The reason for this is that the pattern attempts to canonicalize element-wise ops thereby reverting any optimization, which has led to performance regressions in some cases.
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM. Please give a couple of days for others to agree/disagree before landing.
Option<"enableMoveInitOperandsToInput", "enable-move-init-operands-to-input", "bool", | ||
/*default=*/"true", | ||
"Enable MoveInitOperandsToInputPattern transformation"> | ||
]; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As you have made default 'true', this is NFC for if (enableMoveInitOperandsToInput) {
.
With that context, LGTM.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, thank you!
Ok, Actually this is also related to another PR I was reviewing. First of all this input is wrong
You are reading the
So before we go ahead with this change... id strongly suggest looking at your input... Input seems off to me. |
Thank you for raising the concern.
|
Of course, I understand thats how it works for your full program, but from an operations perspective, there is no guarantee that it will maintain/use the value of |
I see. I checked few mlir tests where I noticed that we do read from 'outs'. Example mlir/test/Dialect/Linalg/transform-tile-reduction.mlir test, the generated IR reads from the 'outs' operand inside a linalg.generic with parallel iterators. I see another test which does the same mlir/test/Dialect/Linalg/transform-op-specialize.mlir. Please let me know if I am missing something - otherwise can we proceed with the merge as it is NFC? |
The pattern MoveInitOperandsToInput in the LinalgFoldUnitExtendDimsPass tries to move the init operands to input, to ensure that the element wise ops are canonicalized. I need to run split-reduction optimization before LinalgFoldUnitExtendDimsPass. The split reduction optimization is trying to optimize the Linalg op by reading from the destination operand but, the pattern MoveInitOperandsToInput is reverting it and trying to canonicalize element wise op. This is leading to 10x performance regression. Proposing to guard the pattern MoveInitOperandsToInput to have more control over enabling/disabling it.
IR after Split-Reduction and before LinalgFoldUnitExtendDimsPass

IR after LinalgFoldUnitExtendDimsPass
