Repository navigation
Description
In addition to the linalg fusion we do today there's another level of fusion we could be performing that combines entire dispatch regions. This can happen just based on the flow.dispatch.workgroups op and using the flow.dispatch.tensor interface.
Conceptually each dispatch region is just a function called at runtime - the call of which may be happen on a thread pool, GPU command processor, etc. The benefit of having these calls modeled in Flow/HAL is that if the execution engine can execute the work in parallel (same call on may execution units) or concurrently (overlapping with other calls) we reap those benefits. There are cases where this cannot happen, though, and we have those encoded in the flow IR use-def chains.
As an example, this is a great separation of work:
// these two run concurrently and are each parallelized:
%0 = flow.dispatch.workgroups[1024, 1, 1](%arg0)
%1 = flow.dispatch.workgroups[1024, 512, 1](%arg1)
// this reduces the inputs and runs after both complete, but uses far fewer workgroups to do so:
%2 = flow.dispatch.workgroups[8, 1, 1](%0, %1)While this is not:
// each runs sequentially:
%0 = flow.dispatch.workgroups[1, 1, 1](%arg0)
%1 = flow.dispatch.workgroups[1, 1, 1](%1)
%2 = flow.dispatch.workgroups[1, 1, 1](%2)In the second case we are getting no benefits from the separation and only incurring overhead: we are paying the cost of encoding and performing multiple calls, round-tripping the results/arguments through memory, and requiring additional transient memory.
The aspirational case here is https://www.shadertoy.com/view/wsByWV (which I will bring up til the end of time :) - that could be done in 400 dispatches or like it is here 4 with loops, conditionals, etc. We (as something executing functions) aren't restricted to what linalg can model in a single loop nest but only what data traversal hazards we have. It's all just code :)
We should be able to identify some of these cases where the separation is pessimizing performance and fix them up by folding consumers up into producers. Conceptually it's like identifying mergable dispatches, creating a new dispatch, and inlining both dispatch regions into it. The flow.dispatch.tensor arguments can be elided through and the rest of the code outside of the dispatches gets its use-def chains patched up. Being able to safely do this is one of the reasons we require tensors at this level.
There are gotchas, like if the work is parallelized it may be doing things that prevent workgroups from running within the same dispatch (such as a scatter followed by a gather). We should be able to identify this by either adding traversal information to the flow ops (similar and derived from how linalg has parallel/reduce/etc tags) or inspecting the flow.dispatch.tensor load and store ops. To start, even just combining dispatches that are all 1x1x1 would help in the situations where we can't or don't want to detensor and are performing very thin sequences of sequential work.
The major work is this cost modeling (of which to start could just be "is possible?"), and there may be some cleanup work or generalization required lower into the codegen stack if there are any lingering assumptions from back in the old pipeline around whether there is a single root linalg loop/etc. The pass to perform this work where it is possible, though, is likely just a hundred lines or so of replacing ops, using mlir::inlineRegion, and removing unneeded args/results (which in theory is handled by the closure optimizations already on the dispatch region op).
This will improve raw performance (less memory round-tripping), latency (less command processor round-tripping), memory utilization (fewer materialized transient buffers), and code size (less code needed to setup buffers/dispatch unique entry points). It may harm deduplication but that's a cost we can balance (and we need better solutions than "bitwise identical" for deduplication anyway).
Metadata
Metadata
Assignees
Labels
Type
Projects
- StatusShow more project fieldsNo status
Activity
MaheshRavishankar commentedon Sep 5, 2021
Ideally this could be done before dispatch region formation. Its easier to analyse operations at Linalg on tensors instead of at dispatch region formation level, and treat dispatch region formation as a one-to-one mapping of a single or a sequence of
linalgoperations. It would be useful to determine the root causes of these different dispatches created (what ops are in these dispatches). Some examples arecould be fused to
This effectively fuses the two dispatch region created by these operations.
Would be good to study the models we have running through IREE to find a list of missing fusions.
benvanik commentedon Sep 5, 2021
Agreed that would be a good approach! It's incredibly more efficient to compute two things at once than it is two things in sequence - can think of fusing horizontally as a way of enabling concurrent execution without another dispatch/barriers/etc.
hanhanW commentedon Sep 14, 2021
Minor issues, I think it should be fused in
benvanik commentedon Oct 25, 2022
While looking at #10903 I found that the model suffers deeply from really narrow tensor operations that don't get fused:

Detensoring didn't seem to help (or work), but even if it did there's data dependencies that we can't have host/device roundtrips around. Note that some of the tensors are not scalars but still a small value (~2-4 elements) that don't need any kind of large scale tiling.
MaheshRavishankar commentedon Oct 26, 2022
Is it just a lot of parallel work that don't have producer-consumer relationship?
benvanik commentedon Oct 26, 2022
There's likely some of that but the above is mostly sequential. I suspect it's the reuse that's breaking things. The original code likely looks like a normal program instead of ML, so it's going to be a tangle of use-def chains.
MaheshRavishankar commentedon Oct 26, 2022
Ok. Hopefully we can drop that requirement soon. It's under a flag currently cause of backend issues.
benvanik commentedon Oct 26, 2022
That'd be awesome!
benvanik commentedon Oct 26, 2022
(it's exciting to have a model that actually does this - it's been a hypothetical scenario for so long - when bored/waiting for a CI run I recommend perusing out what phoenix posted in #10903 because there's a bunch of fun stuff in it :)
MaheshRavishankar commentedon Oct 26, 2022
You can try
iree-flow-enable-aggressive-fusionto see if it helps. Right now that might not compile. But at least will give an idea of if it is covered by that or not. I'll take a look when I get time24 remaining items
Disabling detensoring by default until #6948 can be completed. (#13658)
benvanik commentedon May 17, 2023
(whoops, forgot putting issues in titles closes them 🤦)
MaheshRavishankar commentedon May 18, 2023
@benvanik can you share the input IR for this example (the input to
iree-compile)benvanik commentedon May 18, 2023
#13543, #13459, and #13648 have the IR I've been playing with - they're all roughly the same
MaheshRavishankar commentedon May 18, 2023
This file has a lot of debug metadata here. Is there a flag to strip it while printing?
benvanik commentedon May 18, 2023
good q - I sometimes do a regex replace of the #loc attributes with location(unknown), but I think passing the file through iree-opt with
--mlir-print-debuginfo=falsewould strip them tooMaheshRavishankar commentedon Jun 29, 2023
#13711 adds the basic mechanism... any other issues can be done as a follow up.
Disabling detensoring by default until iree-org#6948 can be completed. (