Skip to content

Enable Linalg ElementwiseOpFusion to fuse when producer has multiple uses #9303

Closed

Description

@MaheshRavishankar

Current elementwise op fusion used in IREE (and implemented in Linalg) only fuses producers with consumers when the producers have a single use. This is done within the control function used in IREE here https://github.com/google/iree/blob/8aa71c098870873c386fc7f53e5226cb06cbf1b9/compiler/src/iree/compiler/Dialect/Flow/Transforms/FusionOfTensorOps.cpp#L48

This means a producer which has multiple uses is not fused with its consumer. This was done to avoid running into issues when fusing these can lead to SSA use-def violations w.r.t to other uses of the producer. This requirement though could be relaxed by using dominator information. Ideally you could use the dominator directly during the transformation to check if the producer/consumer are fusable without violating dominance for other uses. The problem there is that every time ops are fused the dominator information changes. The control flow of the program remains the same, but what changes is the order of operations in a block, and recomputing this information is expensive, and there is no easy way to incrementally update the order as the transformation proceeds.

An alternative approach is to run elementwise fusion in two parts.

  • The first part is similar to what is done today in IREE,
    • Fuse producer-consumer elementwise operations when the producer has a single use
    • "Fuse elementwise operations with reshape" to propagate the reshapes to edges of the function.
  • The second part computes the dominator information after the first part, annotates the ops that can be fused (without the above constraint), and then uses these annotation to decide what ops can be fused.

For the second part the specific steps would be

  1. The implementation in MLIR needs to handle creating ops with multiple result. If the producer has uses outside the current consumer, then both the producer value and the consumer value needs to be returned. The implementation of the fusion is here. Some things to consider here.
  2. Add the second part in IREE here. The dominator information can be used to annotate ops that need to be fused, and a new control function can be used that just fuses the annotated ops.
    • There is an additional restriction here
if (producer.getNumOutputs() != 1)
    return false;

which also needs to be removed.

Activity

  1. added
    bug 🐞
    help wanted
    on Jun 2, 2022
  2. removed
    bug 🐞
    help wanted
    on Jun 2, 2022
  3. added
    codegen
    on Jun 2, 2022
  4. benvanik commented on Jun 2, 2022

    @benvanik
    Collaborator

    I don't think it's directly transferrable but as a reference I had to solve some similar issues for stream partitioning with respect to obeying SSA use-def dominance, cloning into consumers, and respecting boundary I/O values. I was able to come up with an analysis that's fast to compute (O(n) runtime and storage of all candidate ops) and it produces a set of clustered ops (of which multiple clusters may contain the same op) without the need for inserting attributes/etc and that can be used to produce the final structure in one shot: https://github.com/google/iree/blob/main/compiler/src/iree/compiler/Dialect/Stream/Analysis/Partitioning/ReferencePartitioning.cpp

    Conceptually if that partitioning was using the dispatch region logic for clustering/cloning/etc it'd be able to produce dispatch regions. Not saying it's the right solution for it but that there may be parts of the design space that don't require iterative IR mutation (which is slow and can be brittle).

  5. MaheshRavishankar commented on Jun 2, 2022

    @MaheshRavishankar
    CollaboratorAuthor

    There are two approaches you can take to fusion in general. You can do some clustering to create strongly connected components (just a quick skim of things, you seem to be doing that above), where each component becomes a dispatch. Thats a pretty powerful approach. The other approach is greedy approach which is not optimal, but less state in the pass and less "smarts" in it. So its mostly doing the basic things one would do. For now the dispatch region formation is basically working under constraints of what backend (like say CUDA backend) can generate code for "efficiently", so I think the greedy approach is probably OK till backends can handle a more general dispatch region. I do see this greedy approach as a "default" partitioning scheme that forms a "reasonable reference". For anything that does some clustering, I'd rely on search to learn what the partitioning should be. The clustering could actually go "before" the greedy approach, with the greedy approach used for "cleanup" if needed.

    For the greedy approach to be a good reference though, need to remove the training wheels that were put in to get something going. So once the greedy approach is fully flushed out, we then have a good reference. The reference is getting better, but after the above is addressed, I think all the training wheels on the greedy approach will be off. Then we need to re-evaluate based on what we see in models.

  6. benvanik commented on Jun 2, 2022

    @benvanik
    Collaborator

    Agreed!

    Related to the training wheels, #9295 shows a good example of the kind of code we want to be able to entirely group into dispatch regions. Specifically as it relates to our current limitations the while's should end up inside dispatch regions, but even further literally 100% of all that tensor<f32> code should end up in a single dispatch as it's all elementwise. I haven't looked too deeply to see what we do today but know that we don't handle the loops right and doubt we are getting the ideal form of the entire program being in a single dispatch due to the num outputs logic. It'd be a good benchmark for this issue - we won't get 100% there but it's likely much more interesting as a test input showing complex producer/consumer interactions on elementwise ops!

  7. okkwon commented on Jun 3, 2022

    @okkwon
    Member

    @MaheshRavishankar do you know which benchmarks have the pattern? Do you happen to have an example input?

  8. MaheshRavishankar commented on Jun 3, 2022

    @MaheshRavishankar
    CollaboratorAuthor

    I dont have a specific pattern in a benchmark, but it is a general enough thing to do that it should affect almost all benchmarks Again it is about removing training wheels. Restricting fusion of producer -> consumer for case where producer has just one use is an ad-hoc restriction. Once that is removed, then we can measure impact etc. of it.

  9. MaheshRavishankar commented on Jun 10, 2022

    @MaheshRavishankar
    CollaboratorAuthor

    Chatting with Okwan I realized there was a mistake in how I had written the first comment corrected it now.

    Okwan also pointed out that we might not need dominator information. We need the dominator information only when the fused op is placed just before the consumer.
    You could also place the fused op just after the producer. Since the producer already dominates the consumer, and transitively the consumers uses, this is always going to be valid. Inserting operations "after" a given operation has to be done with a bit of care, but seems like a good approach for this case.
    I would still suggest adding the fusion that creates ops with multiple results as a second fusion step after the current fusion that is implemented in IREE.

  10. moved this to In Progress in IREEon Jun 28, 2022
  11. okkwon commented on Aug 3, 2022

    @okkwon
    Member

    Passing the issue to Mahesh to coordinate.

  12. MaheshRavishankar commented on Sep 29, 2022

    @MaheshRavishankar
    CollaboratorAuthor

    Is implemented under the flag --iree-flow-enable-aggressive-fusion. It is disabled by default due to backend issues that have to be worked through. Closing

  13. Repository owner moved this from In Progress to Done in IREE - ARCHIVED (do not update)on Sep 29, 2022
  14. Repository owner moved this from In Progress to Done in IREEon Sep 29, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

codegen

Type

No type

Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions