Repository navigation
[flow] Fuse reduction into consumer elementwise op - #9693
Conversation
c2a4e39 to
4918f27
Compare
Abbreviated x86_64 Benchmark Summary (experimental)@ commit 88e03c6a90c3a8eb9e48b16f59899a3c0fee36b8 (vs. base eeb0b1a309c2d49713a95eb95432bfb8eb5f36bd) Regressed Latencies 🚩
No improved or regressed compilation metrics 🏖️ For more information: |
|
x86 and Android perf results look good, but there is a compilation failure for CUDA. The pattern seems very simple, but CUDA fails to compile. After Outlining: Before LLVMGPUReduceToGPU: |
|
@ThomasRaoux would you mind checking if the code above is supported by the CUDA backend? |
|
I had an offline discussion with @ThomasRaoux, and he already has a plan for this, but it needs a little bit of more work. My next step would be adding an option for this transformation (turned off by default), and make it available in the repo for the later work. |
|
This is the dispatch graph: dispatch.pdf |
d1c1d3c to
8fc3e17
Compare
|
Added a unit test and an option to make it disabled by default. |
8fc3e17 to
612eb49
Compare
|
Note that the pass is disabled by default until the GPU backends can support it. |
612eb49 to
1bc1343
Compare
|
I have found that the additional check for the input indexing maps in |
e207efb to
88e03c6
Compare
|
Keeping the benchmark result links: |
|
@ThomasRaoux Resolved your comments. Please take another look. Thanks! |
|
miniLM and bertencoder see the reduction on the number of dispatches. |
|
There must be more pattern matchings with other models and I will handle the cases in a different PR. |
|
Hey @okkwon , sorry for the late update. I benchmarked the miniLM bert model on |
|
I'd like to take a pass before this lands. Can we wait till EoD today to land? |
|
Having your review would be great @MaheshRavishankar. Thanks! |
|
@hanhanW Thanks for running the benchmark! |
| /// An example: | ||
| /// s = sum(a: <16x32xf32>) -> <16xf32> | ||
| /// d = div(a: <16x32xf32>, broadcast(s to <16x32xf32>)) -> <16x32xf32> | ||
| static bool isReductionBroadcastElementwise(OpOperand *operand) { |
There was a problem hiding this comment.
It is great to see that relatively simple conditions / filters allow creating more powerful dispatch regions!
I am not clear about the structure of the code here though, these are somewhat adhoc constraints that are conjunctions of things hardcoded deep in C++. I would love to see a common effort towards more composable constraints that we can reuse and reassemble better, esp. as we decouple more and more the control from hardcoded C++.
I'd encourage you to think about refactoring to allow easily plugging and composing with the PR I sent out here: #9756 (last commit).
Another way to phrase my request is: "what are the atomic units of useful matching and filtering" that we can reuse; isReductionBroadcastElementwise looks to me like a very non-atomic that we should build out of atoms that we want to properly define to make them reusable.
There was a problem hiding this comment.
Thanks @nicolasvasilache! I agree. When I worked on this to recover the original tensor op's info, I felt like I am trying recovering the upper IR level representation (tosa or mhlo) at the linalg generic op level reversely.
This led me to thinking of the new compilation pipeline, which needs a common high level tensor ops, such as tosa, mhlo, or purely linalg name ops before linalg. This could have made the pattern matching code a lot easy and clean. This would need a great amount of discussion, so I will initiate the discussion later.
I am further working on the reduction code to support dispatch fusions including softmax and layer normalization, and I will put the refactoring effort while working on them by referencing your PR. Thanks for the pointer!
There was a problem hiding this comment.
Also, there is a GPU work based on this PR happening at the same time, so I'd like to push it first and do refactor in subsequent changes.
|
@okkwon can you summarize the discussion above a bit so that I dont rehash things. Mostly looking for divergence from what we had discussed. |
|
It was mostly about the edge cases that the pattern matching code unexpectedly matches. Matmul and dynamic dims are the cases, so I put more restrictions to match what I expect to match. |
MaheshRavishankar
left a comment
There was a problem hiding this comment.
Thanks Okwan! First set of comments here.
The refactoring work will be done in subsequent PRs.
1. Bail out dynamic shapes. 2. Bail out the case where the input type for the reduction is not the same as the output type of the elementwise operation. 3. Check whether the elementwise output has the identity indexing map.
- Check if a reduction is unary or binary. - Check if an input with a full dimension has the same shape as the elementwise op's output shape. - Check if the other input has the identity map or is broadcasted.
7af55af to
e886d24
Compare
I don't know if Okwan answered this, but this should all be producer fusion. |
MaheshRavishankar
left a comment
There was a problem hiding this comment.
Updating comments based on offline discussions.
@okkwon please document each constraint here, especially those that are made restrictive that can be generalized later vs. those that are core to the pattern being checked. It would be good to have some documentation in code about what each constraint is for.
1. remove matchReduction(), 2. remove hasOnlyScaleElementwiseOp(), and 3. remove dynamic shape check since having a same indexing map guarantees the same shape at runtime.
|
I have updated the PR to address Mahesh's comments. Thanks for the offline discussion! Also, I have added a few comments about the restriction that can be relaxed later. For now, There are several restrictions to match simple reduction + broadcast + elementwise.
This is the common pattern we can see from the real applications. As I find more patterns, I will relax the conditions to cover more cases. |
Resolved in the latest update.
MaheshRavishankar
left a comment
There was a problem hiding this comment.
Few more comments. Should be good after that.
Though as a follow up, I'd suggest dropping all the restrictive conditions. The only real conditions you need is probably that the indexing maps used for the fused operand in the producer and consumer "match". (A version of this was strawman implemented here MaheshRavishankar@11a3f70 - following whats described here #9139 (comment)). Rest all should be droppable.
In the current development stage, we take aggresssive fusion as our default fusion policy and we do type checking only when the types are known to be static. When the types are dynamic, we optimistically assume that they are compatible for fusion and do fusion aggressively.
|
I'd like to suggest doing further enhancements in subsequent PRs. The narrow conditions don't do any harm, but relaxing the conditions needs a broad testing in codegen & perf. |
|
Filed an issue: #9772 |
| // When we have static shapes, we do extra checks for the type. For dynamic | ||
| // shape cases, we do not check the shape and do aggressive fusion with high | ||
| // optimism, which is the default approach we are pursuing now. | ||
| bool hasStaticShape = |
There was a problem hiding this comment.
Nit: Move this closer to where it is used.
There was a problem hiding this comment.
Both if and else blocks just below use the variable and that's why it is outside.
Support fusion of reduction + elementwise.
The fusion is disabled by default because CUDA fails to compile.
The number of dispatches for miniLM is reduced from 271 to 221.