Repository navigation
Normalize input indexing map to identity to help fusion - #9103
Conversation
92f324b to
4c7262f
Compare
|
The failing case for Vulkan is supposed to be fixed by #8836. |
4bc7a06 to
97177df
Compare
MaheshRavishankar
left a comment
There was a problem hiding this comment.
Nice! Some minor comments.
| AffineMap consumerIndexingMap = consumer.getTiedIndexingMap(&use); | ||
| if (!consumerIndexingMap.isIdentity()) return false; | ||
|
|
||
| if (allowTranspose) { |
There was a problem hiding this comment.
I dont think we need this. The interchange should be making this still identity.
| return failure(); | ||
|
|
||
| for (OpOperand *operand : genericOp.getInputOperands()) { | ||
| if (!areLinalgOpsFusableUsingTileAndFuse(*operand, |
There was a problem hiding this comment.
I see now why are using this.... I think we can avoid coupling by just checking if producer is a "linalg named op", i.e. isa<linalg::LinalgOp>(op) && !isa<linalg::GenericOp>(op).
There was a problem hiding this comment.
We may just need to normalize genericOps to have the identity indexing map if it does not egardless of the producer. Do you see any side effect?
There was a problem hiding this comment.
It can get into a loop, where you switch it w.r.t one producer and switch it again w.r.t to another. So need a check here that make sure the pattern applies only once. Checking for the first named op producer and making that identity should address this
There was a problem hiding this comment.
I pushed a change to normalize the input indexing maps to identity. Thus, once the input indexing maps are identity, the pass does not revert so we won't have a loop.
| if (indexingMap != indexingMap0) return failure(); | ||
| } | ||
|
|
||
| auto outputIndexingMap0 = |
There was a problem hiding this comment.
I am not sure I follow this condition. I think this can be dropped.
97177df to
b02a31d
Compare
b02a31d to
c89b3d9
Compare
| PatternRewriter &rewriter) const override { | ||
| if (!genericOp.hasTensorSemantics()) return failure(); | ||
|
|
||
| if (genericOp.getNumInputs() != 1) return failure(); |
There was a problem hiding this comment.
This is going to make this apply to a very small set of ops. We can remove this condition
c89b3d9 to
54dd7cd
Compare
6e41894 to
6a06d35
Compare
Abbreviated x86_64 Benchmark Summary (experimental)@ commit 1386b9768baecf0148003e942f225e1ddfef140a (vs. base 2e4f780cddb9fe2eefd2e1b579981e8343ea66da) Regressed Benchmarks 🚩
For more information: |
MaheshRavishankar
left a comment
There was a problem hiding this comment.
Not sure this works. I left a snippet of what I think should be the pattern (dont know if it works out of the box).
Also the pattern didnt seem to replace the original op. That means this change should not have any effect. Maybe worth checking that the patch actually reduces the number of dispatches.
Abbreviated Benchmark Summary@ commit 1386b9768baecf0148003e942f225e1ddfef140a (vs. base 2e4f780cddb9fe2eefd2e1b579981e8343ea66da) Regressed Benchmarks 🚩
[Top 3 out of 7 benchmark results showed] For more information: |
|
There are a few regressions. Something for CPU codegen folks to look at for later. For now we can land this under a flag. |
Do you know why it didn't fuse dispatch regions. It should. If it doesn't then this transformation is unnecessary |
Yes, a code was missing in my PR. Added it back and now the fusion works as expected. Waiting for the benchmark results. |
|
Significiant regressions were found from the arm benchmarks with the experimental config. Fusing mmt4d + transpose caused a change in the workgroup counts. |
|
Here is the reduced number of dispatches.
The improvement on PoseNet f32 with the default flags might be a fluctuation, since I don't see a change in the IR. It would be great if the benchmark run can show that the binary file is actually changed or not by comparing the binaries or hashes. The degradation on PoseNet with the experimental flags is real. The number of dispatches is reduced from 107 to 90. I will introduce a flag to make the pass enabled selectively (disabled by default). Then, we can investigate the perf issue in the backend with the experimental flags. |
When a generic op has a permutation on its input while the output indexing map is identity, interchange the genericOp to make the input indexing map identity and output indexing map a permutation. This helps fusion of linalg + genericOp without incurring transpose at the producer site.
2991f90 to
24fca6f
Compare
|
@hanhanW, would you mind reviewing this change? Mahesh is OOO. The feature is disabled by default, so it is not supposed to affect anything, but we can experiment it by toggling the flags. |
|
Yes, I can review the change. (I was looking for such support, good to see this. :D) |
hanhanW
left a comment
There was a problem hiding this comment.
Please add a test for InterchangeTransposeGenericOpsPass. I know there is a test that runs the pass + dispatch_linalg_on_tensors. It's better to also have a test which runs the pass only.
|
note: I found that the interchanged transpose op is not vectorized when the flag is on. It would create a stack buffer because ConvertToDestinationPassingStyle + upstream bufferization does not work with the case. I sent out https://reviews.llvm.org/D126454 for fixing it. |
|
Thanks @hanhanW for the review. I addressed your comments. It is great to see that you have a fix for the vectorization. I will do a perf run once the change appears in IREE. |
0618638 to
1c80f43
Compare
|
There are two compiler options to enable the feature: |
hanhanW
left a comment
There was a problem hiding this comment.
Overall looks nice to me!
|
Can you make the PR title and description more descriptive? |
There are two options: 1. --iree-flow-normalize-input-indexing-map This normalizes the input indexing map to identity, which is a favorable form for a later fusion logic. 2. --iree-flow-fuse-transpose This enables a fusion of linalg name op with a generic op with transpose.
1c80f43 to
1386b97
Compare
| auto canTieWithOutsOperand = [&](OpOperand *outsOperand) { | ||
| if (linalgOp.getTiedIndexingMap(outsOperand) != insOperandIndexingMap) { | ||
| auto outputMap = linalgOp.getTiedIndexingMap(outsOperand); | ||
| if (clFuseTranspose) { |
There was a problem hiding this comment.
I am confused about this change. We should not need to do this. With the intechange pass, the dispatch region formation should fuse the ops directly (that is why we did the intechange). You need one or the other but not both. Can you give me an example where after interchange things still didnt fuse?
There was a problem hiding this comment.
The transpose would have the following indexing maps:
input: (d0, d1) -> (d0, d1)
output: (d0, d1) -> (d1, d0)
Thus, the linalg op's input indexing map is different from the output indexing map, and the fusion is not triggered with the original code.
There was a problem hiding this comment.
Ah I see. Thanks! I'd actually hoist this option usage out of this method (this method is a hack which we might not need anymore, need to evaluate). So move this to line 79 and rename the flag
iree-flow-fuse-transpose->iree-flow-ensure-inplacable-consumer. Make the new flag default true.- At line 79
return !clEnsureInplacable || canInsOperandTieWithOutsOperand
There was a problem hiding this comment.
There is another check in line 52, so I am afraid that hoisting the option out would change the current behavior.
There was a problem hiding this comment.
Everything in this method (canInsOperandTieWithOutsOperand) is a WAR that wont work in the general case. So I am saying we add a flag to side-step this entire method for exploration of fusion with transpose-like ops (which is enabled by this change)
Testing matmul + genericOp(add+transpose)