Skip to content

Normalize input indexing map to identity to help fusion - #9103

Merged
okkwon merged 4 commits into
iree-org:mainfrom
okkwon:fuse_with_transpose
May 27, 2022
Merged

okkwon merged 4 commits into
iree-org:mainfrom
okkwon:fuse_with_transpose

Conversation

@okkwon

@okkwon okkwon commented May 12, 2022

Copy link
Copy Markdown
Member

Testing matmul + genericOp(add+transpose)

@okkwon
okkwon force-pushed the fuse_with_transpose branch 5 times, most recently from 92f324b to 4c7262f Compare May 17, 2022 21:40
@okkwon

okkwon commented May 19, 2022

Copy link
Copy Markdown
Member Author

The failing case for Vulkan is supposed to be fixed by #8836.

@okkwon
okkwon force-pushed the fuse_with_transpose branch 2 times, most recently from 4bc7a06 to 97177df Compare May 19, 2022 16:58
@okkwon okkwon self-assigned this May 19, 2022
@okkwon
okkwon marked this pull request as ready for review May 19, 2022 18:50
@okkwon
okkwon requested a review from MaheshRavishankar as a code owner May 19, 2022 18:50

@MaheshRavishankar MaheshRavishankar left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Nice! Some minor comments.

AffineMap consumerIndexingMap = consumer.getTiedIndexingMap(&use);
if (!consumerIndexingMap.isIdentity()) return false;

if (allowTranspose) {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I dont think we need this. The interchange should be making this still identity.

return failure();

for (OpOperand *operand : genericOp.getInputOperands()) {
if (!areLinalgOpsFusableUsingTileAndFuse(*operand,

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

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).

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

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?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

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

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

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 =

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I am not sure I follow this condition. I think this can be dropped.

@okkwon
okkwon marked this pull request as draft May 20, 2022 17:00
@okkwon
okkwon force-pushed the fuse_with_transpose branch from 97177df to b02a31d Compare May 20, 2022 17:28
PatternRewriter &rewriter) const override {
if (!genericOp.hasTensorSemantics()) return failure();

if (genericOp.getNumInputs() != 1) return failure();

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

This is going to make this apply to a very small set of ops. We can remove this condition

@okkwon
okkwon force-pushed the fuse_with_transpose branch from c89b3d9 to 54dd7cd Compare May 23, 2022 17:46
@okkwon
okkwon marked this pull request as ready for review May 23, 2022 18:13
@okkwon
okkwon force-pushed the fuse_with_transpose branch 4 times, most recently from 6e41894 to 6a06d35 Compare May 23, 2022 19:17
@iree-github-actions-bot

iree-github-actions-bot commented May 23, 2022 •

Copy link
Copy Markdown
Contributor

Abbreviated x86_64 Benchmark Summary (experimental)

@ commit 1386b9768baecf0148003e942f225e1ddfef140a (vs. base 2e4f780cddb9fe2eefd2e1b579981e8343ea66da)

Regressed Benchmarks 🚩

Benchmark Name Average Latency (ms) Median Latency (ms) Latency Standard Deviation (ms)
DeepLabV3 [fp32] (TFLite) 8-thread,full-inference,default-flags with IREE-Dylib @ GCP-c2-standard-16 (CPU-x86\_64-CascadeLake) 16 (vs. 15, 6.67%↑) 16 0

For more information:

@MaheshRavishankar MaheshRavishankar left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

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.

@iree-github-actions-bot

iree-github-actions-bot commented May 23, 2022 •

Copy link
Copy Markdown
Contributor

Abbreviated Benchmark Summary

@ commit 1386b9768baecf0148003e942f225e1ddfef140a (vs. base 2e4f780cddb9fe2eefd2e1b579981e8343ea66da)

Regressed Benchmarks 🚩

Benchmark Name Average Latency (ms) Median Latency (ms) Latency Standard Deviation (ms)
MobileNetV2 [fp32,imagenet] (TFLite) 4-thread,big-core,full-inference,experimental-flags with IREE-VMVX @ Pixel-6-Pro (CPU-ARMv8.2-A) 9539 (vs. 8360, 14.10%↑) 9824 666
MobileNetV3Small [fp32,imagenet] (TFLite) 4-thread,big-core,full-inference,experimental-flags with IREE-VMVX @ Pixel-4 (CPU-ARMv8.2-A) 2072 (vs. 1850, 12.00%↑) 2065 52
MobileSSD [fp32] (TFLite) 4-thread,big-core,full-inference,default-flags with IREE-Dylib @ Pixel-6-Pro (CPU-ARMv8.2-A) 61 (vs. 55, 10.91%↑) 62 4

[Top 3 out of 7 benchmark results showed]

For more information:

@MaheshRavishankar

Copy link
Copy Markdown
Collaborator

There are a few regressions. Something for CPU codegen folks to look at for later. For now we can land this under a flag.

@MaheshRavishankar

Copy link
Copy Markdown
Collaborator

The previous code didn't fuse dispatch regions, but it changes the input and output indexing maps to be identity and permutation, respectively. The change on the indexing maps results in transposed work group counts. The performance a transpose operation needs to be studied separately.

Do you know why it didn't fuse dispatch regions. It should. If it doesn't then this transformation is unnecessary

@okkwon

okkwon commented May 23, 2022

Copy link
Copy Markdown
Member Author

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.

@okkwon

okkwon commented May 24, 2022

Copy link
Copy Markdown
Member Author

Significiant regressions were found from the arm benchmarks with the experimental config.

https://gist.githubusercontent.com/iree-github-actions-bot/eb5fbfc967ab9bc09056d4e793f7bd91/raw/8c6ef9d5058ab505af8adbb8f9ccb2a9831735c0/iree-full-benchmark-result-3017.md

Fusing mmt4d + transpose caused a change in the workgroup counts.

@okkwon

okkwon commented May 25, 2022 •

Copy link
Copy Markdown
Member Author

Here is the reduced number of dispatches.

benchmark before after
mobilebert-baseline-tf2-float 752 727
mobilebertsquad 752 727
mobilebert-baseline-tf2-quant 1520 1495
bert_encoder_unrolled_fake_weights 609 560
miniLM 271 247

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.
@okkwon
okkwon force-pushed the fuse_with_transpose branch from 2991f90 to 24fca6f Compare May 25, 2022 18:24
@okkwon
okkwon requested a review from hanhanW May 25, 2022 21:31
@okkwon

okkwon commented May 25, 2022

Copy link
Copy Markdown
Member Author

@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.

@okkwon
okkwon requested a review from MaheshRavishankar May 25, 2022 21:34
@hanhanW

hanhanW commented May 26, 2022

Copy link
Copy Markdown
Contributor

Yes, I can review the change. (I was looking for such support, good to see this. :D)

@hanhanW hanhanW left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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.

@hanhanW

hanhanW commented May 26, 2022

Copy link
Copy Markdown
Contributor

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.

@okkwon

okkwon commented May 26, 2022

Copy link
Copy Markdown
Member Author

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.

@okkwon
okkwon force-pushed the fuse_with_transpose branch from 0618638 to 1c80f43 Compare May 26, 2022 16:58
@okkwon

okkwon commented May 26, 2022

Copy link
Copy Markdown
Member Author

There are two compiler options to enable the feature:
--iree-flow-normalize-input-indexing-map --iree-flow-fuse-transpose

@hanhanW hanhanW left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Overall looks nice to me!

@hanhanW

hanhanW commented May 27, 2022

Copy link
Copy Markdown
Contributor

Can you make the PR title and description more descriptive?

@okkwon okkwon changed the title Fuse with transpose Normalize input indexing map to identity to help fusion May 27, 2022
@okkwon
okkwon dismissed MaheshRavishankar’s stale review May 27, 2022 16:52

The comments are out-dated.

okkwon added 3 commits May 27, 2022 10:02
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.
@okkwon
okkwon force-pushed the fuse_with_transpose branch from 1c80f43 to 1386b97 Compare May 27, 2022 17:05
@okkwon
okkwon merged commit 3324d06 into iree-org:main May 27, 2022
@okkwon
okkwon deleted the fuse_with_transpose branch May 27, 2022 20:26
auto canTieWithOutsOperand = [&](OpOperand *outsOperand) {
if (linalgOp.getTiedIndexingMap(outsOperand) != insOperandIndexingMap) {
auto outputMap = linalgOp.getTiedIndexingMap(outsOperand);
if (clFuseTranspose) {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

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?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

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.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

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

  1. iree-flow-fuse-transpose -> iree-flow-ensure-inplacable-consumer . Make the new flag default true.
  2. At line 79
return !clEnsureInplacable || canInsOperandTieWithOutsOperand

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

There is another check in line 52, so I am afraid that hoisting the option out would change the current behavior.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

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)

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.

4 participants